Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proptests for Signature encoding #896

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ml-dsa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ criterion = "0.5.1"
hex = { version = "0.4.3", features = ["serde"] }
hex-literal = "0.4.1"
pkcs8 = { version = "=0.11.0-rc.1", features = ["pem"] }
proptest = "1"
rand = "0.8.5"
serde = { version = "1.0.215", features = ["derive"] }
serde_json = "1.0.132"
Expand Down
2 changes: 1 addition & 1 deletion ml-dsa/src/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn use_hint<TwoGamma2: Unsigned>(h: bool, r: Elem) -> Elem {
}
}

#[derive(Clone, PartialEq)]
#[derive(Clone, PartialEq, Debug)]
pub struct Hint<P>(pub Array<Array<bool, U256>, P::K>)
where
P: SignatureParams;
Expand Down
7 changes: 3 additions & 4 deletions ml-dsa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
//! # Quickstart
//!
//! ```
//! use ml_dsa::{MlDsa65, KeyGen};
//! use signature::{Keypair, Signer, Verifier};
//! use ml_dsa::{MlDsa65, KeyGen, signature::{Keypair, Signer, Verifier}};
//!
//! let mut rng = rand::thread_rng();
//! let kp = MlDsa65::key_gen(&mut rng);
Expand Down Expand Up @@ -86,10 +85,10 @@ use crate::util::B64;

pub use crate::param::{EncodedSignature, EncodedSigningKey, EncodedVerifyingKey, MlDsaParams};
pub use crate::util::B32;
pub use signature::Error;
pub use signature::{self, Error};

/// An ML-DSA signature
#[derive(Clone, PartialEq)]
#[derive(Clone, PartialEq, Debug)]
pub struct Signature<P: MlDsaParams> {
c_tilde: Array<u8, P::Lambda>,
z: Vector<P::L>,
Expand Down
8 changes: 8 additions & 0 deletions ml-dsa/tests/sig-encoding.proptest-regressions

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions ml-dsa/tests/sig-encoding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use ml_dsa::{signature::Signer, KeyGen, MlDsa44, MlDsa65, MlDsa87, MlDsaParams, Signature, B32};
use proptest::prelude::*;

fn example_signature<P: MlDsaParams>(seed_bytes: &B32) -> Signature<P> {
let keypair = P::key_gen_internal(seed_bytes);
let msg = b"";
keypair.signing_key().sign(msg)
}

prop_compose! {
fn mldsa44_signature()(seed_bytes in any::<[u8; 32]>()) -> Signature<MlDsa44> {
example_signature::<MlDsa44>(seed_bytes.as_ref())
}
}

prop_compose! {
fn mldsa65_signature()(seed_bytes in any::<[u8; 32]>()) -> Signature<MlDsa65> {
example_signature::<MlDsa65>(seed_bytes.as_ref())
}
}

prop_compose! {
fn mldsa87_signature()(seed_bytes in any::<[u8; 32]>()) -> Signature<MlDsa87> {
example_signature::<MlDsa87>(seed_bytes.as_ref())
}
}

proptest! {
#[test]
fn mldsa44_round_trip(sig in mldsa44_signature()) {
let sig_decoded = Signature::<MlDsa44>::decode(&sig.encode());
prop_assert_eq!(Some(sig), sig_decoded);
}

#[test]
fn mldsa65_round_trip(sig in mldsa65_signature()) {
let sig_decoded = Signature::<MlDsa65>::decode(&sig.encode());
prop_assert_eq!(Some(sig), sig_decoded);
}

#[test]
fn mldsa87_round_trip(sig in mldsa87_signature()) {
let sig_decoded = Signature::<MlDsa87>::decode(&sig.encode());
prop_assert_eq!(Some(sig), sig_decoded);
}
}
Loading