mirror of
https://github.com/saymrwulf/anza-cryptography-source.git
synced 2026-11-20 10:04:09 +00:00
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:
parent
154b58b3f4
commit
4413a1284a
16 changed files with 110 additions and 72 deletions
18
.github/workflows/ci.yml
vendored
18
.github/workflows/ci.yml
vendored
|
|
@ -32,11 +32,23 @@ jobs:
|
||||||
- name: Check formatting
|
- name: Check formatting
|
||||||
run: cargo fmt --all -- --check
|
run: cargo fmt --all -- --check
|
||||||
|
|
||||||
- name: Check benchmarks compile
|
|
||||||
run: cargo bench --workspace --all-targets --no-run
|
|
||||||
|
|
||||||
- name: Lint
|
- name: Lint
|
||||||
run: cargo clippy --workspace --all-targets -- -D warnings
|
run: cargo clippy --workspace --all-targets -- -D warnings
|
||||||
|
|
||||||
- name: Test
|
- name: Test
|
||||||
run: cargo test --workspace --all-targets
|
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
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@ rustc_version.workspace = true
|
||||||
[[bench]]
|
[[bench]]
|
||||||
name = "dalek_benchmarks"
|
name = "dalek_benchmarks"
|
||||||
harness = false
|
harness = false
|
||||||
required-features = ["alloc"]
|
required-features = ["alloc", "digest", "group", "rand_core"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
cfg-if.workspace = true
|
cfg-if.workspace = true
|
||||||
|
|
@ -80,7 +80,7 @@ group-bits = ["group", "ff/bits"]
|
||||||
digest = ["dep:digest"]
|
digest = ["dep:digest"]
|
||||||
lizard = ["digest"]
|
lizard = ["digest"]
|
||||||
pkcs8 = ["dep:pkcs8", "ed25519/pkcs8", "alloc"]
|
pkcs8 = ["dep:pkcs8", "ed25519/pkcs8", "alloc"]
|
||||||
pem = ["pkcs8", "dep:der", "ed25519/pem"]
|
pem = ["pkcs8", "dep:der", "ed25519/pem", "zeroize"]
|
||||||
|
|
||||||
[lints.rust.unexpected_cfgs]
|
[lints.rust.unexpected_cfgs]
|
||||||
level = "warn"
|
level = "warn"
|
||||||
|
|
|
||||||
|
|
@ -5,17 +5,24 @@ use curve25519::ed_sigs::*;
|
||||||
use ed25519::signature::Verifier as _;
|
use ed25519::signature::Verifier as _;
|
||||||
use ed25519_dalek::VerifyingKey as DalekVerifyingKey;
|
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)> {
|
fn sigs_with_distinct_pubkeys() -> impl Iterator<Item = (VerificationKeyBytes, Signature)> {
|
||||||
std::iter::repeat_with(|| {
|
(0u64..).map(|i| {
|
||||||
let sk = SigningKey::new(rand::thread_rng());
|
let sk = signing_key_from_index(i);
|
||||||
let pk_bytes = VerificationKeyBytes::from(&sk);
|
let pk_bytes = VerificationKeyBytes::from(&sk);
|
||||||
let sig = sk.sign(b"");
|
let sig = sk.sign(b"");
|
||||||
(pk_bytes, sig)
|
(pk_bytes, sig)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(all(feature = "alloc", feature = "rand_core"))]
|
||||||
fn sigs_with_same_pubkey() -> impl Iterator<Item = (VerificationKeyBytes, Signature)> {
|
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);
|
let pk_bytes = VerificationKeyBytes::from(&sk);
|
||||||
std::iter::repeat_with(move || {
|
std::iter::repeat_with(move || {
|
||||||
let sig = sk.sign(b"");
|
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) {
|
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 vk = VerificationKey::from(&sk);
|
||||||
let sig = sk.sign(b"");
|
let sig = sk.sign(b"");
|
||||||
let vk_bytes: [u8; 32] = vk.into();
|
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(
|
group.bench_with_input(
|
||||||
BenchmarkId::new("Signatures with Distinct Pubkeys", n),
|
BenchmarkId::new("Signatures with Distinct Pubkeys", n),
|
||||||
&sigs,
|
&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<_>>();
|
let sigs = sigs_with_same_pubkey().take(*n).collect::<Vec<_>>();
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(all(feature = "alloc", feature = "rand_core"))]
|
||||||
group.bench_with_input(
|
group.bench_with_input(
|
||||||
BenchmarkId::new("Signatures with the Same Pubkey", n),
|
BenchmarkId::new("Signatures with the Same Pubkey", n),
|
||||||
&sigs,
|
&sigs,
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,8 @@
|
||||||
//! Ed25519 signing and verification (ZIP-215 / HEEA-accelerated).
|
//! Ed25519 signing and verification (ZIP-215 / HEEA-accelerated).
|
||||||
|
|
||||||
|
use crate::scalar::Scalar;
|
||||||
|
use sha2::{Digest, Sha512};
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
||||||
|
|
@ -15,3 +18,10 @@ pub use ::ed25519::Signature;
|
||||||
pub use error::Error;
|
pub use error::Error;
|
||||||
pub use signing_key::SigningKey;
|
pub use signing_key::SigningKey;
|
||||||
pub use verification_key::{VerificationKey, VerificationKeyBytes};
|
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)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -51,19 +51,22 @@
|
||||||
use alloc::vec::Vec;
|
use alloc::vec::Vec;
|
||||||
use core::convert::TryFrom;
|
use core::convert::TryFrom;
|
||||||
|
|
||||||
|
use crate::scalar::Scalar;
|
||||||
|
#[cfg(feature = "rand_core")]
|
||||||
use crate::{
|
use crate::{
|
||||||
edwards::{CompressedEdwardsY, EdwardsPoint},
|
edwards::{CompressedEdwardsY, EdwardsPoint},
|
||||||
scalar::Scalar,
|
|
||||||
traits::{IsIdentity, VartimeMultiscalarMul},
|
traits::{IsIdentity, VartimeMultiscalarMul},
|
||||||
};
|
};
|
||||||
use hashbrown::HashMap;
|
use hashbrown::HashMap;
|
||||||
|
#[cfg(feature = "rand_core")]
|
||||||
use rand_core::{CryptoRng, RngCore};
|
use rand_core::{CryptoRng, RngCore};
|
||||||
use sha2::{Sha512, digest::Update};
|
use sha2::{Sha512, digest::Update};
|
||||||
|
|
||||||
use super::{Error, VerificationKey, VerificationKeyBytes};
|
use super::{Error, VerificationKey, VerificationKeyBytes, scalar_from_sha512};
|
||||||
use ed25519::Signature;
|
use ed25519::Signature;
|
||||||
|
|
||||||
// Shim to generate a u128 without importing `rand`.
|
// Shim to generate a u128 without importing `rand`.
|
||||||
|
#[cfg(feature = "rand_core")]
|
||||||
fn gen_u128<R: RngCore + CryptoRng>(mut rng: R) -> u128 {
|
fn gen_u128<R: RngCore + CryptoRng>(mut rng: R) -> u128 {
|
||||||
let mut bytes = [0u8; 16];
|
let mut bytes = [0u8; 16];
|
||||||
rng.fill_bytes(&mut bytes[..]);
|
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 {
|
fn from(tup: (VerificationKeyBytes, Signature, &'msg M)) -> Self {
|
||||||
let (vk_bytes, sig, msg) = tup;
|
let (vk_bytes, sig, msg) = tup;
|
||||||
// Compute k now to avoid dependency on the msg lifetime.
|
// Compute k now to avoid dependency on the msg lifetime.
|
||||||
let k = Scalar::from_hash(
|
let k = scalar_from_sha512(
|
||||||
Sha512::default()
|
Sha512::default()
|
||||||
.chain(&sig.r_bytes()[..])
|
.chain(&sig.r_bytes()[..])
|
||||||
.chain(&vk_bytes.0[..])
|
.chain(&vk_bytes.0[..])
|
||||||
|
|
@ -141,6 +144,7 @@ impl Verifier {
|
||||||
|
|
||||||
/// Perform batch verification, returning `Ok(())` if all signatures were
|
/// Perform batch verification, returning `Ok(())` if all signatures were
|
||||||
/// valid and `Err` otherwise.
|
/// valid and `Err` otherwise.
|
||||||
|
#[cfg(feature = "rand_core")]
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
pub fn verify<R: RngCore + CryptoRng>(self, mut rng: R) -> Result<(), Error> {
|
pub fn verify<R: RngCore + CryptoRng>(self, mut rng: R) -> Result<(), Error> {
|
||||||
// The batch verification equation is
|
// The batch verification equation is
|
||||||
|
|
|
||||||
|
|
@ -7,15 +7,17 @@ const ALGORITHM_ID: AlgorithmIdentifierRef<'_> = AlgorithmIdentifierRef {
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::Error;
|
use super::Error;
|
||||||
use crate::{constants, scalar::Scalar};
|
use crate::{edwards::EdwardsPoint, scalar::Scalar};
|
||||||
#[cfg(all(feature = "pem", feature = "pkcs8"))]
|
#[cfg(all(feature = "pem", feature = "pkcs8"))]
|
||||||
use alloc::string::String;
|
use alloc::string::String;
|
||||||
use core::convert::TryFrom;
|
use core::convert::TryFrom;
|
||||||
#[cfg(feature = "pkcs8")]
|
#[cfg(feature = "pkcs8")]
|
||||||
use core::convert::TryInto;
|
use core::convert::TryInto;
|
||||||
|
#[cfg(feature = "rand_core")]
|
||||||
use rand_core::{CryptoRng, RngCore};
|
use rand_core::{CryptoRng, RngCore};
|
||||||
use sha2::{Digest, Sha512, digest::Update};
|
use sha2::{Digest, Sha512, digest::Update};
|
||||||
use subtle::ConstantTimeEq;
|
use subtle::ConstantTimeEq;
|
||||||
|
#[cfg(feature = "zeroize")]
|
||||||
use zeroize::Zeroize;
|
use zeroize::Zeroize;
|
||||||
|
|
||||||
use ed25519::{Signature, signature::Signer};
|
use ed25519::{Signature, signature::Signer};
|
||||||
|
|
@ -40,6 +42,7 @@ use zeroize::Zeroizing;
|
||||||
#[cfg(all(feature = "pem", feature = "pkcs8"))]
|
#[cfg(all(feature = "pem", feature = "pkcs8"))]
|
||||||
use pkcs8::der::pem::PemLabel;
|
use pkcs8::der::pem::PemLabel;
|
||||||
|
|
||||||
|
use super::scalar_from_sha512;
|
||||||
use super::{VerificationKey, VerificationKeyBytes};
|
use super::{VerificationKey, VerificationKeyBytes};
|
||||||
|
|
||||||
/// The length of a ed25519 `SecretKey`, in bytes.
|
/// The length of a ed25519 `SecretKey`, in bytes.
|
||||||
|
|
@ -67,6 +70,7 @@ pub struct SigningKey {
|
||||||
vk: VerificationKey,
|
vk: VerificationKey,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "zeroize")]
|
||||||
impl Zeroize for SigningKey {
|
impl Zeroize for SigningKey {
|
||||||
fn zeroize(&mut self) {
|
fn zeroize(&mut self) {
|
||||||
self.seed.zeroize();
|
self.seed.zeroize();
|
||||||
|
|
@ -145,7 +149,7 @@ impl From<SecretKey> for SigningKey {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Compute the public key as A = [s]B.
|
// Compute the public key as A = [s]B.
|
||||||
let A = &s * constants::ED25519_BASEPOINT_TABLE;
|
let A = EdwardsPoint::mul_base(&s);
|
||||||
|
|
||||||
SigningKey {
|
SigningKey {
|
||||||
seed,
|
seed,
|
||||||
|
|
@ -326,6 +330,7 @@ impl SigningKey {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generate a new signing key.
|
/// Generate a new signing key.
|
||||||
|
#[cfg(feature = "rand_core")]
|
||||||
pub fn new<R: RngCore + CryptoRng>(mut rng: R) -> SigningKey {
|
pub fn new<R: RngCore + CryptoRng>(mut rng: R) -> SigningKey {
|
||||||
let mut bytes = [0u8; 32];
|
let mut bytes = [0u8; 32];
|
||||||
rng.fill_bytes(&mut bytes[..]);
|
rng.fill_bytes(&mut bytes[..]);
|
||||||
|
|
@ -340,13 +345,11 @@ impl SigningKey {
|
||||||
/// Create a signature on `msg` using this key.
|
/// Create a signature on `msg` using this key.
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
pub fn sign(&self, msg: &[u8]) -> Signature {
|
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)
|
let R_bytes = EdwardsPoint::mul_base(&r).compress().to_bytes();
|
||||||
.compress()
|
|
||||||
.to_bytes();
|
|
||||||
|
|
||||||
let k = Scalar::from_hash(
|
let k = scalar_from_sha512(
|
||||||
Sha512::default()
|
Sha512::default()
|
||||||
.chain(&R_bytes[..])
|
.chain(&R_bytes[..])
|
||||||
.chain(&self.vk.A_bytes.0[..])
|
.chain(&self.vk.A_bytes.0[..])
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
#![cfg(feature = "alloc")]
|
#![cfg(all(feature = "alloc", feature = "rand_core"))]
|
||||||
|
|
||||||
use crate::ed_sigs::*;
|
use crate::ed_sigs::*;
|
||||||
use alloc::vec::Vec;
|
use alloc::vec::Vec;
|
||||||
|
|
@ -6,8 +6,10 @@ use alloc::vec::Vec;
|
||||||
#[test]
|
#[test]
|
||||||
fn batch_verify() {
|
fn batch_verify() {
|
||||||
let mut batch = batch::Verifier::new();
|
let mut batch = batch::Verifier::new();
|
||||||
for _ in 0..32 {
|
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 pk_bytes = VerificationKeyBytes::from(&sk);
|
||||||
let msg = b"BatchVerifyTest";
|
let msg = b"BatchVerifyTest";
|
||||||
let sig = sk.sign(&msg[..]);
|
let sig = sk.sign(&msg[..]);
|
||||||
|
|
@ -22,7 +24,9 @@ fn batch_verify_with_one_bad_sig() {
|
||||||
let mut batch = batch::Verifier::new();
|
let mut batch = batch::Verifier::new();
|
||||||
let mut items = Vec::new();
|
let mut items = Vec::new();
|
||||||
for i in 0..32 {
|
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 pk_bytes = VerificationKeyBytes::from(&sk);
|
||||||
let msg = b"BatchVerifyTest";
|
let msg = b"BatchVerifyTest";
|
||||||
let sig = if i != bad_index {
|
let sig = if i != bad_index {
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,7 @@ use ed25519::Signature;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_verify_heea_invalid_signature() {
|
fn test_verify_heea_invalid_signature() {
|
||||||
let mut rng = rand::thread_rng();
|
let signing_key = SigningKey::from([1u8; 32]);
|
||||||
let signing_key = SigningKey::new(&mut rng);
|
|
||||||
let verification_key = VerificationKey::from(&signing_key);
|
let verification_key = VerificationKey::from(&signing_key);
|
||||||
|
|
||||||
let msg = b"Original message";
|
let msg = b"Original message";
|
||||||
|
|
@ -39,10 +38,10 @@ fn test_verify_heea_invalid_signature() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_verify_heea_multiple_signatures() {
|
fn test_verify_heea_multiple_signatures() {
|
||||||
let mut rng = rand::thread_rng();
|
|
||||||
|
|
||||||
for i in 0..100 {
|
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 verification_key = VerificationKey::from(&signing_key);
|
||||||
|
|
||||||
let msg = format!("Message number {}", i);
|
let msg = format!("Message number {}", i);
|
||||||
|
|
@ -72,8 +71,7 @@ fn test_verify_heea_multiple_signatures() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_default_verification_matches_zebra() {
|
fn test_default_verification_matches_zebra() {
|
||||||
let mut rng = rand::thread_rng();
|
let signing_key = SigningKey::from([2u8; 32]);
|
||||||
let signing_key = SigningKey::new(&mut rng);
|
|
||||||
let verification_key = VerificationKey::from(&signing_key);
|
let verification_key = VerificationKey::from(&signing_key);
|
||||||
let msg = b"default verification mode";
|
let msg = b"default verification mode";
|
||||||
let signature = signing_key.sign(msg);
|
let signature = signing_key.sign(msg);
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
#![cfg(feature = "std")]
|
#![cfg(feature = "std")]
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
constants::EIGHT_TORSION, digest::Update, edwards::CompressedEdwardsY, scalar::Scalar,
|
constants::EIGHT_TORSION, ed_sigs::scalar_from_sha512, edwards::CompressedEdwardsY,
|
||||||
traits::IsIdentity,
|
scalar::Scalar, traits::IsIdentity,
|
||||||
};
|
};
|
||||||
use color_eyre::Report;
|
use color_eyre::Report;
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use sha2::Sha512;
|
use sha2::{Sha512, digest::Update};
|
||||||
use std::vec::Vec;
|
use std::vec::Vec;
|
||||||
|
|
||||||
use super::util;
|
use super::util;
|
||||||
|
|
@ -51,7 +51,7 @@ pub static SMALL_ORDER_SIGS: Lazy<Vec<TestCase>> = Lazy::new(|| {
|
||||||
// * R is not an excluded point
|
// * R is not an excluded point
|
||||||
// * R + [k]A = 0
|
// * R + [k]A = 0
|
||||||
// * R is canonically encoded (because the check recomputes R)
|
// * R is canonically encoded (because the check recomputes R)
|
||||||
let k = Scalar::from_hash(
|
let k = scalar_from_sha512(
|
||||||
Sha512::default()
|
Sha512::default()
|
||||||
.chain(&sig_bytes[0..32])
|
.chain(&sig_bytes[0..32])
|
||||||
.chain(vk_bytes)
|
.chain(vk_bytes)
|
||||||
|
|
@ -84,7 +84,7 @@ fn conformance() -> Result<(), Report> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(all(feature = "alloc", feature = "rand_core"))]
|
||||||
#[test]
|
#[test]
|
||||||
fn individual_matches_batch_verification() -> Result<(), Report> {
|
fn individual_matches_batch_verification() -> Result<(), Report> {
|
||||||
use crate::ed_sigs::{VerificationKey, VerificationKeyBytes, batch};
|
use crate::ed_sigs::{VerificationKey, VerificationKeyBytes, batch};
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ use crate::ed_sigs::{Signature, SigningKey, VerificationKey, VerificationKeyByte
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parsing() {
|
fn parsing() {
|
||||||
let sk = SigningKey::new(rand::thread_rng());
|
let sk = SigningKey::from([1u8; 32]);
|
||||||
let pk = VerificationKey::from(&sk);
|
let pk = VerificationKey::from(&sk);
|
||||||
let pkb = VerificationKeyBytes::from(&sk);
|
let pkb = VerificationKeyBytes::from(&sk);
|
||||||
let sig = sk.sign(b"test");
|
let sig = sk.sign(b"test");
|
||||||
|
|
@ -37,7 +37,7 @@ fn parsing() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn sign_and_verify() {
|
fn sign_and_verify() {
|
||||||
let sk = SigningKey::new(rand::thread_rng());
|
let sk = SigningKey::from([2u8; 32]);
|
||||||
let pk = VerificationKey::from(&sk);
|
let pk = VerificationKey::from(&sk);
|
||||||
|
|
||||||
let msg = b"ed25519-zebra test message";
|
let msg = b"ed25519-zebra test message";
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ use crate::{
|
||||||
};
|
};
|
||||||
use core::convert::{TryFrom, TryInto};
|
use core::convert::{TryFrom, TryInto};
|
||||||
use sha2::{Sha512, digest::Update};
|
use sha2::{Sha512, digest::Update};
|
||||||
|
#[cfg(feature = "zeroize")]
|
||||||
use zeroize::DefaultIsZeroes;
|
use zeroize::DefaultIsZeroes;
|
||||||
|
|
||||||
use ed25519::{Signature, signature::Verifier};
|
use ed25519::{Signature, signature::Verifier};
|
||||||
|
|
@ -32,7 +33,7 @@ use pkcs8::spki::{
|
||||||
#[cfg(feature = "pkcs8")]
|
#[cfg(feature = "pkcs8")]
|
||||||
use pkcs8::{Document, ObjectIdentifier};
|
use pkcs8::{Document, ObjectIdentifier};
|
||||||
|
|
||||||
use super::Error;
|
use super::{Error, scalar_from_sha512};
|
||||||
|
|
||||||
/// The length of an ed25519 `VerificationKey`, in bytes.
|
/// The length of an ed25519 `VerificationKey`, in bytes.
|
||||||
pub const VERIFICATION_KEY_LENGTH: usize = 32;
|
pub const VERIFICATION_KEY_LENGTH: usize = 32;
|
||||||
|
|
@ -217,6 +218,7 @@ impl Default for VerificationKey {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "zeroize")]
|
||||||
impl DefaultIsZeroes for VerificationKey {}
|
impl DefaultIsZeroes for VerificationKey {}
|
||||||
|
|
||||||
impl From<VerificationKey> for [u8; 32] {
|
impl From<VerificationKey> for [u8; 32] {
|
||||||
|
|
@ -296,7 +298,7 @@ impl Verifier<Signature> for VerificationKey {
|
||||||
|
|
||||||
impl VerificationKey {
|
impl VerificationKey {
|
||||||
fn challenge_scalar(&self, signature: &Signature, msg: &[u8]) -> Scalar {
|
fn challenge_scalar(&self, signature: &Signature, msg: &[u8]) -> Scalar {
|
||||||
Scalar::from_hash(
|
scalar_from_sha512(
|
||||||
Sha512::default()
|
Sha512::default()
|
||||||
.chain(&signature.r_bytes()[..])
|
.chain(&signature.r_bytes()[..])
|
||||||
.chain(&self.A_bytes.0[..])
|
.chain(&self.A_bytes.0[..])
|
||||||
|
|
|
||||||
|
|
@ -220,9 +220,16 @@ impl RistrettoPoint {
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::{edwards::EdwardsPoint, ristretto::CompressedRistretto};
|
use crate::{edwards::EdwardsPoint, ristretto::CompressedRistretto};
|
||||||
use rand_core::RngCore;
|
|
||||||
use sha2::Sha256;
|
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\]
|
/// Return the coset self + E\[4\]
|
||||||
fn xcoset4(pt: &RistrettoPoint) -> [EdwardsPoint; 4] {
|
fn xcoset4(pt: &RistrettoPoint) -> [EdwardsPoint; 4] {
|
||||||
[
|
[
|
||||||
|
|
@ -281,9 +288,8 @@ mod test {
|
||||||
// Tests that lizard_decode of a random point is None
|
// Tests that lizard_decode of a random point is None
|
||||||
#[test]
|
#[test]
|
||||||
fn lizard_invalid() {
|
fn lizard_invalid() {
|
||||||
let mut rng = rand::thread_rng();
|
for i in 0..100 {
|
||||||
for _ in 0..100 {
|
let pt = RistrettoPoint::from_uniform_bytes(&test_uniform_bytes(i));
|
||||||
let pt = RistrettoPoint::random(&mut rng);
|
|
||||||
assert!(
|
assert!(
|
||||||
pt.lizard_decode::<Sha256>().is_none(),
|
pt.lizard_decode::<Sha256>().is_none(),
|
||||||
"random point {:02x?} is a valid Lizard encoding",
|
"random point {:02x?} is a valid Lizard encoding",
|
||||||
|
|
@ -297,24 +303,20 @@ mod test {
|
||||||
// is the identity
|
// is the identity
|
||||||
#[test]
|
#[test]
|
||||||
fn elligator_inv() {
|
fn elligator_inv() {
|
||||||
let mut rng = rand::thread_rng();
|
|
||||||
|
|
||||||
for i in 0..100 {
|
for i in 0..100 {
|
||||||
let mut fe_bytes = [0u8; 32];
|
let mut fe_bytes = if i == 0 {
|
||||||
|
|
||||||
if i == 0 {
|
|
||||||
// Test for first corner-case: fe = 0
|
// Test for first corner-case: fe = 0
|
||||||
fe_bytes = [0u8; 32];
|
[0u8; 32]
|
||||||
} else if i == 1 {
|
} else if i == 1 {
|
||||||
// Test for second corner-case: fe = +sqrt(i*d)
|
// 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,
|
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,
|
110, 235, 115, 153, 84, 52, 117, 151, 235, 123, 244, 88, 85, 179, 5,
|
||||||
];
|
]
|
||||||
} else {
|
} else {
|
||||||
// For the rest, just generate a random field element to test.
|
// For the rest, use distinct field elements to test.
|
||||||
rng.fill_bytes(&mut fe_bytes);
|
test_bytes(i)
|
||||||
}
|
};
|
||||||
// Make fe positive (even) and less than the modulus
|
// Make fe positive (even) and less than the modulus
|
||||||
fe_bytes[0] &= 254;
|
fe_bytes[0] &= 254;
|
||||||
fe_bytes[31] &= 127;
|
fe_bytes[31] &= 127;
|
||||||
|
|
@ -342,11 +344,8 @@ mod test {
|
||||||
// Tests that map_to_curve ○ map_to_curve_inverse is the identity
|
// Tests that map_to_curve ○ map_to_curve_inverse is the identity
|
||||||
#[test]
|
#[test]
|
||||||
fn map_to_curve_inverse() {
|
fn map_to_curve_inverse() {
|
||||||
let mut rng = rand::thread_rng();
|
for i in 0..100 {
|
||||||
|
let input = test_bytes(i);
|
||||||
for _ in 0..100 {
|
|
||||||
let mut input = [0u8; 32];
|
|
||||||
rng.fill_bytes(&mut input);
|
|
||||||
|
|
||||||
// Map to Ristretto and invert it
|
// Map to Ristretto and invert it
|
||||||
let pt = RistrettoPoint::map_to_curve(input);
|
let pt = RistrettoPoint::map_to_curve(input);
|
||||||
|
|
@ -365,11 +364,8 @@ mod test {
|
||||||
// return value in the first 8 elements
|
// return value in the first 8 elements
|
||||||
#[test]
|
#[test]
|
||||||
fn map_pos_felem_to_curve_inverse() {
|
fn map_pos_felem_to_curve_inverse() {
|
||||||
let mut rng = rand::thread_rng();
|
for i in 0..100 {
|
||||||
|
let mut input = test_bytes(i);
|
||||||
for _ in 0..100 {
|
|
||||||
let mut input = [0u8; 32];
|
|
||||||
rng.fill_bytes(&mut input);
|
|
||||||
|
|
||||||
// Clear bits so we can call map_to_curve_restricted
|
// Clear bits so we can call map_to_curve_restricted
|
||||||
input[31] &= 0b00111111;
|
input[31] &= 0b00111111;
|
||||||
|
|
|
||||||
|
|
@ -512,7 +512,8 @@ mod test {
|
||||||
use crate::constants;
|
use crate::constants;
|
||||||
|
|
||||||
#[cfg(feature = "rand_core")]
|
#[cfg(feature = "rand_core")]
|
||||||
use rand::{CryptoRng, RngCore};
|
use rand::CryptoRng;
|
||||||
|
use rand::RngCore;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn identity_in_different_coordinates() {
|
fn identity_in_different_coordinates() {
|
||||||
|
|
|
||||||
|
|
@ -1248,9 +1248,9 @@ impl Zeroize for RistrettoPoint {
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::edwards::CompressedEdwardsY;
|
use crate::edwards::CompressedEdwardsY;
|
||||||
#[cfg(feature = "group")]
|
#[cfg(all(feature = "alloc", feature = "group"))]
|
||||||
use proptest::prelude::*;
|
use proptest::prelude::*;
|
||||||
#[cfg(all(feature = "rand_core", feature = "group"))]
|
#[cfg(all(feature = "alloc", feature = "rand_core", feature = "group"))]
|
||||||
use rand::rngs::OsRng;
|
use rand::rngs::OsRng;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -1490,7 +1490,7 @@ mod test {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "group")]
|
#[cfg(all(feature = "alloc", feature = "group"))]
|
||||||
proptest! {
|
proptest! {
|
||||||
#[test]
|
#[test]
|
||||||
fn multiply_double_and_compress_random_points(
|
fn multiply_double_and_compress_random_points(
|
||||||
|
|
|
||||||
|
|
@ -1446,6 +1446,7 @@ pub(crate) mod test {
|
||||||
|
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
use alloc::vec::Vec;
|
use alloc::vec::Vec;
|
||||||
|
use rand::RngCore;
|
||||||
|
|
||||||
/// x = 2238329342913194256032495932344128051776374960164957527413114840482143558222
|
/// x = 2238329342913194256032495932344128051776374960164957527413114840482143558222
|
||||||
pub static X: Scalar = Scalar {
|
pub static X: Scalar = Scalar {
|
||||||
|
|
|
||||||
|
|
@ -422,7 +422,7 @@ mod tests {
|
||||||
#[cfg(all(feature = "rand_core", feature = "digest"))]
|
#[cfg(all(feature = "rand_core", feature = "digest"))]
|
||||||
use crate::{Scalar, digest::Update, traits::HEEADecomposition};
|
use crate::{Scalar, digest::Update, traits::HEEADecomposition};
|
||||||
|
|
||||||
#[cfg(feature = "rand_core")]
|
#[cfg(all(feature = "rand_core", feature = "digest"))]
|
||||||
use rand_core::RngCore;
|
use rand_core::RngCore;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue