ci: check each feature individually (#18)

* ci: check each feature individually

* fix test all targets

* Update ristretto.rs

* Update lizard_ristretto.rs

---------

Co-authored-by: zz-sol <allaboutshop10@163.com>
This commit is contained in:
Yihau Chen 2026-05-20 10:23:03 +08:00 committed by GitHub
parent 154b58b3f4
commit 4413a1284a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 110 additions and 72 deletions

View file

@ -32,11 +32,23 @@ jobs:
- name: Check formatting
run: cargo fmt --all -- --check
- name: Check benchmarks compile
run: cargo bench --workspace --all-targets --no-run
- name: Lint
run: cargo clippy --workspace --all-targets -- -D warnings
- name: Test
run: cargo test --workspace --all-targets
feature-check:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
- name: Install cargo-hack
uses: taiki-e/install-action@cca35edeb1d01366c2843b68fc3ca441446d73d3 # v2.77.1
with:
tool: cargo-hack
- name: Check
run: |
cargo hack check --each-feature --all-targets

View file

@ -47,7 +47,7 @@ rustc_version.workspace = true
[[bench]]
name = "dalek_benchmarks"
harness = false
required-features = ["alloc"]
required-features = ["alloc", "digest", "group", "rand_core"]
[dependencies]
cfg-if.workspace = true
@ -80,7 +80,7 @@ group-bits = ["group", "ff/bits"]
digest = ["dep:digest"]
lizard = ["digest"]
pkcs8 = ["dep:pkcs8", "ed25519/pkcs8", "alloc"]
pem = ["pkcs8", "dep:der", "ed25519/pem"]
pem = ["pkcs8", "dep:der", "ed25519/pem", "zeroize"]
[lints.rust.unexpected_cfgs]
level = "warn"

View file

@ -5,17 +5,24 @@ use curve25519::ed_sigs::*;
use ed25519::signature::Verifier as _;
use ed25519_dalek::VerifyingKey as DalekVerifyingKey;
fn signing_key_from_index(index: u64) -> SigningKey {
let mut seed = [0u8; 32];
seed[..8].copy_from_slice(&index.to_le_bytes());
SigningKey::from(seed)
}
fn sigs_with_distinct_pubkeys() -> impl Iterator<Item = (VerificationKeyBytes, Signature)> {
std::iter::repeat_with(|| {
let sk = SigningKey::new(rand::thread_rng());
(0u64..).map(|i| {
let sk = signing_key_from_index(i);
let pk_bytes = VerificationKeyBytes::from(&sk);
let sig = sk.sign(b"");
(pk_bytes, sig)
})
}
#[cfg(all(feature = "alloc", feature = "rand_core"))]
fn sigs_with_same_pubkey() -> impl Iterator<Item = (VerificationKeyBytes, Signature)> {
let sk = SigningKey::new(rand::thread_rng());
let sk = signing_key_from_index(0);
let pk_bytes = VerificationKeyBytes::from(&sk);
std::iter::repeat_with(move || {
let sig = sk.sign(b"");
@ -24,7 +31,7 @@ fn sigs_with_same_pubkey() -> impl Iterator<Item = (VerificationKeyBytes, Signat
}
fn single_verify_inputs() -> (VerificationKey, Signature, DalekVerifyingKey) {
let sk = SigningKey::new(rand::thread_rng());
let sk = signing_key_from_index(0);
let vk = VerificationKey::from(&sk);
let sig = sk.sign(b"");
let vk_bytes: [u8; 32] = vk.into();
@ -51,7 +58,7 @@ fn bench_batch_verify(c: &mut Criterion) {
})
},
);
#[cfg(feature = "alloc")]
#[cfg(all(feature = "alloc", feature = "rand_core"))]
group.bench_with_input(
BenchmarkId::new("Signatures with Distinct Pubkeys", n),
&sigs,
@ -65,9 +72,9 @@ fn bench_batch_verify(c: &mut Criterion) {
})
},
);
#[cfg(feature = "alloc")]
#[cfg(all(feature = "alloc", feature = "rand_core"))]
let sigs = sigs_with_same_pubkey().take(*n).collect::<Vec<_>>();
#[cfg(feature = "alloc")]
#[cfg(all(feature = "alloc", feature = "rand_core"))]
group.bench_with_input(
BenchmarkId::new("Signatures with the Same Pubkey", n),
&sigs,

View file

@ -1,5 +1,8 @@
//! Ed25519 signing and verification (ZIP-215 / HEEA-accelerated).
use crate::scalar::Scalar;
use sha2::{Digest, Sha512};
#[cfg(test)]
mod tests;
@ -15,3 +18,10 @@ pub use ::ed25519::Signature;
pub use error::Error;
pub use signing_key::SigningKey;
pub use verification_key::{VerificationKey, VerificationKeyBytes};
pub(crate) fn scalar_from_sha512(hash: Sha512) -> Scalar {
let output = hash.finalize();
let mut bytes = [0u8; 64];
bytes.copy_from_slice(output.as_slice());
Scalar::from_bytes_mod_order_wide(&bytes)
}

View file

@ -51,19 +51,22 @@
use alloc::vec::Vec;
use core::convert::TryFrom;
use crate::scalar::Scalar;
#[cfg(feature = "rand_core")]
use crate::{
edwards::{CompressedEdwardsY, EdwardsPoint},
scalar::Scalar,
traits::{IsIdentity, VartimeMultiscalarMul},
};
use hashbrown::HashMap;
#[cfg(feature = "rand_core")]
use rand_core::{CryptoRng, RngCore};
use sha2::{Sha512, digest::Update};
use super::{Error, VerificationKey, VerificationKeyBytes};
use super::{Error, VerificationKey, VerificationKeyBytes, scalar_from_sha512};
use ed25519::Signature;
// Shim to generate a u128 without importing `rand`.
#[cfg(feature = "rand_core")]
fn gen_u128<R: RngCore + CryptoRng>(mut rng: R) -> u128 {
let mut bytes = [0u8; 16];
rng.fill_bytes(&mut bytes[..]);
@ -86,7 +89,7 @@ impl<'msg, M: AsRef<[u8]> + ?Sized> From<(VerificationKeyBytes, Signature, &'msg
fn from(tup: (VerificationKeyBytes, Signature, &'msg M)) -> Self {
let (vk_bytes, sig, msg) = tup;
// Compute k now to avoid dependency on the msg lifetime.
let k = Scalar::from_hash(
let k = scalar_from_sha512(
Sha512::default()
.chain(&sig.r_bytes()[..])
.chain(&vk_bytes.0[..])
@ -141,6 +144,7 @@ impl Verifier {
/// Perform batch verification, returning `Ok(())` if all signatures were
/// valid and `Err` otherwise.
#[cfg(feature = "rand_core")]
#[allow(non_snake_case)]
pub fn verify<R: RngCore + CryptoRng>(self, mut rng: R) -> Result<(), Error> {
// The batch verification equation is

View file

@ -7,15 +7,17 @@ const ALGORITHM_ID: AlgorithmIdentifierRef<'_> = AlgorithmIdentifierRef {
};
use super::Error;
use crate::{constants, scalar::Scalar};
use crate::{edwards::EdwardsPoint, scalar::Scalar};
#[cfg(all(feature = "pem", feature = "pkcs8"))]
use alloc::string::String;
use core::convert::TryFrom;
#[cfg(feature = "pkcs8")]
use core::convert::TryInto;
#[cfg(feature = "rand_core")]
use rand_core::{CryptoRng, RngCore};
use sha2::{Digest, Sha512, digest::Update};
use subtle::ConstantTimeEq;
#[cfg(feature = "zeroize")]
use zeroize::Zeroize;
use ed25519::{Signature, signature::Signer};
@ -40,6 +42,7 @@ use zeroize::Zeroizing;
#[cfg(all(feature = "pem", feature = "pkcs8"))]
use pkcs8::der::pem::PemLabel;
use super::scalar_from_sha512;
use super::{VerificationKey, VerificationKeyBytes};
/// The length of a ed25519 `SecretKey`, in bytes.
@ -67,6 +70,7 @@ pub struct SigningKey {
vk: VerificationKey,
}
#[cfg(feature = "zeroize")]
impl Zeroize for SigningKey {
fn zeroize(&mut self) {
self.seed.zeroize();
@ -145,7 +149,7 @@ impl From<SecretKey> for SigningKey {
};
// Compute the public key as A = [s]B.
let A = &s * constants::ED25519_BASEPOINT_TABLE;
let A = EdwardsPoint::mul_base(&s);
SigningKey {
seed,
@ -326,6 +330,7 @@ impl SigningKey {
}
/// Generate a new signing key.
#[cfg(feature = "rand_core")]
pub fn new<R: RngCore + CryptoRng>(mut rng: R) -> SigningKey {
let mut bytes = [0u8; 32];
rng.fill_bytes(&mut bytes[..]);
@ -340,13 +345,11 @@ impl SigningKey {
/// Create a signature on `msg` using this key.
#[allow(non_snake_case)]
pub fn sign(&self, msg: &[u8]) -> Signature {
let r = Scalar::from_hash(Sha512::default().chain(&self.prefix[..]).chain(msg));
let r = scalar_from_sha512(Sha512::default().chain(&self.prefix[..]).chain(msg));
let R_bytes = (&r * constants::ED25519_BASEPOINT_TABLE)
.compress()
.to_bytes();
let R_bytes = EdwardsPoint::mul_base(&r).compress().to_bytes();
let k = Scalar::from_hash(
let k = scalar_from_sha512(
Sha512::default()
.chain(&R_bytes[..])
.chain(&self.vk.A_bytes.0[..])

View file

@ -1,4 +1,4 @@
#![cfg(feature = "alloc")]
#![cfg(all(feature = "alloc", feature = "rand_core"))]
use crate::ed_sigs::*;
use alloc::vec::Vec;
@ -6,8 +6,10 @@ use alloc::vec::Vec;
#[test]
fn batch_verify() {
let mut batch = batch::Verifier::new();
for _ in 0..32 {
let sk = SigningKey::new(rand::thread_rng());
for i in 0..32 {
let mut seed = [0u8; 32];
seed[0] = i as u8;
let sk = SigningKey::from(seed);
let pk_bytes = VerificationKeyBytes::from(&sk);
let msg = b"BatchVerifyTest";
let sig = sk.sign(&msg[..]);
@ -22,7 +24,9 @@ fn batch_verify_with_one_bad_sig() {
let mut batch = batch::Verifier::new();
let mut items = Vec::new();
for i in 0..32 {
let sk = SigningKey::new(rand::thread_rng());
let mut seed = [0u8; 32];
seed[0] = i as u8;
let sk = SigningKey::from(seed);
let pk_bytes = VerificationKeyBytes::from(&sk);
let msg = b"BatchVerifyTest";
let sig = if i != bad_index {

View file

@ -9,8 +9,7 @@ use ed25519::Signature;
#[test]
fn test_verify_heea_invalid_signature() {
let mut rng = rand::thread_rng();
let signing_key = SigningKey::new(&mut rng);
let signing_key = SigningKey::from([1u8; 32]);
let verification_key = VerificationKey::from(&signing_key);
let msg = b"Original message";
@ -39,10 +38,10 @@ fn test_verify_heea_invalid_signature() {
#[test]
fn test_verify_heea_multiple_signatures() {
let mut rng = rand::thread_rng();
for i in 0..100 {
let signing_key = SigningKey::new(&mut rng);
let mut seed = [0u8; 32];
seed[0] = i;
let signing_key = SigningKey::from(seed);
let verification_key = VerificationKey::from(&signing_key);
let msg = format!("Message number {}", i);
@ -72,8 +71,7 @@ fn test_verify_heea_multiple_signatures() {
#[test]
fn test_default_verification_matches_zebra() {
let mut rng = rand::thread_rng();
let signing_key = SigningKey::new(&mut rng);
let signing_key = SigningKey::from([2u8; 32]);
let verification_key = VerificationKey::from(&signing_key);
let msg = b"default verification mode";
let signature = signing_key.sign(msg);

View file

@ -1,12 +1,12 @@
#![cfg(feature = "std")]
use crate::{
constants::EIGHT_TORSION, digest::Update, edwards::CompressedEdwardsY, scalar::Scalar,
traits::IsIdentity,
constants::EIGHT_TORSION, ed_sigs::scalar_from_sha512, edwards::CompressedEdwardsY,
scalar::Scalar, traits::IsIdentity,
};
use color_eyre::Report;
use once_cell::sync::Lazy;
use sha2::Sha512;
use sha2::{Sha512, digest::Update};
use std::vec::Vec;
use super::util;
@ -51,7 +51,7 @@ pub static SMALL_ORDER_SIGS: Lazy<Vec<TestCase>> = Lazy::new(|| {
// * R is not an excluded point
// * R + [k]A = 0
// * R is canonically encoded (because the check recomputes R)
let k = Scalar::from_hash(
let k = scalar_from_sha512(
Sha512::default()
.chain(&sig_bytes[0..32])
.chain(vk_bytes)
@ -84,7 +84,7 @@ fn conformance() -> Result<(), Report> {
Ok(())
}
#[cfg(feature = "alloc")]
#[cfg(all(feature = "alloc", feature = "rand_core"))]
#[test]
fn individual_matches_batch_verification() -> Result<(), Report> {
use crate::ed_sigs::{VerificationKey, VerificationKeyBytes, batch};

View file

@ -6,7 +6,7 @@ use crate::ed_sigs::{Signature, SigningKey, VerificationKey, VerificationKeyByte
#[test]
fn parsing() {
let sk = SigningKey::new(rand::thread_rng());
let sk = SigningKey::from([1u8; 32]);
let pk = VerificationKey::from(&sk);
let pkb = VerificationKeyBytes::from(&sk);
let sig = sk.sign(b"test");
@ -37,7 +37,7 @@ fn parsing() {
#[test]
fn sign_and_verify() {
let sk = SigningKey::new(rand::thread_rng());
let sk = SigningKey::from([2u8; 32]);
let pk = VerificationKey::from(&sk);
let msg = b"ed25519-zebra test message";

View file

@ -19,6 +19,7 @@ use crate::{
};
use core::convert::{TryFrom, TryInto};
use sha2::{Sha512, digest::Update};
#[cfg(feature = "zeroize")]
use zeroize::DefaultIsZeroes;
use ed25519::{Signature, signature::Verifier};
@ -32,7 +33,7 @@ use pkcs8::spki::{
#[cfg(feature = "pkcs8")]
use pkcs8::{Document, ObjectIdentifier};
use super::Error;
use super::{Error, scalar_from_sha512};
/// The length of an ed25519 `VerificationKey`, in bytes.
pub const VERIFICATION_KEY_LENGTH: usize = 32;
@ -217,6 +218,7 @@ impl Default for VerificationKey {
}
}
#[cfg(feature = "zeroize")]
impl DefaultIsZeroes for VerificationKey {}
impl From<VerificationKey> for [u8; 32] {
@ -296,7 +298,7 @@ impl Verifier<Signature> for VerificationKey {
impl VerificationKey {
fn challenge_scalar(&self, signature: &Signature, msg: &[u8]) -> Scalar {
Scalar::from_hash(
scalar_from_sha512(
Sha512::default()
.chain(&signature.r_bytes()[..])
.chain(&self.A_bytes.0[..])

View file

@ -220,9 +220,16 @@ impl RistrettoPoint {
mod test {
use super::*;
use crate::{edwards::EdwardsPoint, ristretto::CompressedRistretto};
use rand_core::RngCore;
use sha2::Sha256;
fn test_bytes(index: u8) -> [u8; 32] {
core::array::from_fn(|i| index.wrapping_mul(37).wrapping_add(i as u8))
}
fn test_uniform_bytes(index: u8) -> [u8; 64] {
core::array::from_fn(|i| index.wrapping_mul(37).wrapping_add(i as u8))
}
/// Return the coset self + E\[4\]
fn xcoset4(pt: &RistrettoPoint) -> [EdwardsPoint; 4] {
[
@ -281,9 +288,8 @@ mod test {
// Tests that lizard_decode of a random point is None
#[test]
fn lizard_invalid() {
let mut rng = rand::thread_rng();
for _ in 0..100 {
let pt = RistrettoPoint::random(&mut rng);
for i in 0..100 {
let pt = RistrettoPoint::from_uniform_bytes(&test_uniform_bytes(i));
assert!(
pt.lizard_decode::<Sha256>().is_none(),
"random point {:02x?} is a valid Lizard encoding",
@ -297,24 +303,20 @@ mod test {
// is the identity
#[test]
fn elligator_inv() {
let mut rng = rand::thread_rng();
for i in 0..100 {
let mut fe_bytes = [0u8; 32];
if i == 0 {
let mut fe_bytes = if i == 0 {
// Test for first corner-case: fe = 0
fe_bytes = [0u8; 32];
[0u8; 32]
} else if i == 1 {
// Test for second corner-case: fe = +sqrt(i*d)
fe_bytes = [
[
168, 27, 92, 74, 203, 42, 48, 117, 170, 109, 234, 14, 45, 169, 188, 205, 21,
110, 235, 115, 153, 84, 52, 117, 151, 235, 123, 244, 88, 85, 179, 5,
];
]
} else {
// For the rest, just generate a random field element to test.
rng.fill_bytes(&mut fe_bytes);
}
// For the rest, use distinct field elements to test.
test_bytes(i)
};
// Make fe positive (even) and less than the modulus
fe_bytes[0] &= 254;
fe_bytes[31] &= 127;
@ -342,11 +344,8 @@ mod test {
// Tests that map_to_curve ○ map_to_curve_inverse is the identity
#[test]
fn map_to_curve_inverse() {
let mut rng = rand::thread_rng();
for _ in 0..100 {
let mut input = [0u8; 32];
rng.fill_bytes(&mut input);
for i in 0..100 {
let input = test_bytes(i);
// Map to Ristretto and invert it
let pt = RistrettoPoint::map_to_curve(input);
@ -365,11 +364,8 @@ mod test {
// return value in the first 8 elements
#[test]
fn map_pos_felem_to_curve_inverse() {
let mut rng = rand::thread_rng();
for _ in 0..100 {
let mut input = [0u8; 32];
rng.fill_bytes(&mut input);
for i in 0..100 {
let mut input = test_bytes(i);
// Clear bits so we can call map_to_curve_restricted
input[31] &= 0b00111111;

View file

@ -512,7 +512,8 @@ mod test {
use crate::constants;
#[cfg(feature = "rand_core")]
use rand::{CryptoRng, RngCore};
use rand::CryptoRng;
use rand::RngCore;
#[test]
fn identity_in_different_coordinates() {

View file

@ -1248,9 +1248,9 @@ impl Zeroize for RistrettoPoint {
mod test {
use super::*;
use crate::edwards::CompressedEdwardsY;
#[cfg(feature = "group")]
#[cfg(all(feature = "alloc", feature = "group"))]
use proptest::prelude::*;
#[cfg(all(feature = "rand_core", feature = "group"))]
#[cfg(all(feature = "alloc", feature = "rand_core", feature = "group"))]
use rand::rngs::OsRng;
#[test]
@ -1490,7 +1490,7 @@ mod test {
}
}
#[cfg(feature = "group")]
#[cfg(all(feature = "alloc", feature = "group"))]
proptest! {
#[test]
fn multiply_double_and_compress_random_points(

View file

@ -1446,6 +1446,7 @@ pub(crate) mod test {
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
use rand::RngCore;
/// x = 2238329342913194256032495932344128051776374960164957527413114840482143558222
pub static X: Scalar = Scalar {

View file

@ -422,7 +422,7 @@ mod tests {
#[cfg(all(feature = "rand_core", feature = "digest"))]
use crate::{Scalar, digest::Update, traits::HEEADecomposition};
#[cfg(feature = "rand_core")]
#[cfg(all(feature = "rand_core", feature = "digest"))]
use rand_core::RngCore;
#[test]