Skip to content

Commit

Permalink
Added consistency self tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
xvzcf committed Jun 20, 2024
1 parent cdae0f3 commit e61d8ad
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
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 libcrux-ml-dsa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ readme.workspace = true
libcrux-sha3 = { version = "0.0.2-pre.2", path = "../libcrux-sha3" }

[dev-dependencies]
rand = { version = "0.8" }
hex = { version = "0.4.3", features = ["serde"] }
serde_json = { version = "1.0" }
serde = { version = "1.0", features = ["derive"] }
49 changes: 49 additions & 0 deletions libcrux-ml-dsa/tests/self.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use rand::{rngs::OsRng, RngCore};

fn random_array<const L: usize>() -> [u8; L] {
let mut rng = OsRng;
let mut seed = [0; L];
rng.try_fill_bytes(&mut seed).unwrap();
seed
}

macro_rules! impl_consistency {
($name:ident, $key_gen:expr, $sign:expr, $verify:expr) => {
#[test]
fn $name() {
let key_generation_seed = random_array();
let signing_randomness = random_array();

// TODO: Choose the length randomly
let message = random_array::<948839>();

let key_pair = $key_gen(key_generation_seed);

let signature = $sign(key_pair.signing_key, &message, signing_randomness);

$verify(key_pair.verification_key, &message, signature)
.expect("Verification should pass since the signature was honestly generated");
}
};
}

impl_consistency!(
consistency_44,
libcrux_ml_dsa::ml_dsa_44::generate_key_pair,
libcrux_ml_dsa::ml_dsa_44::sign,
libcrux_ml_dsa::ml_dsa_44::verify
);

impl_consistency!(
consistency_65,
libcrux_ml_dsa::ml_dsa_65::generate_key_pair,
libcrux_ml_dsa::ml_dsa_65::sign,
libcrux_ml_dsa::ml_dsa_65::verify
);

impl_consistency!(
consistency_87,
libcrux_ml_dsa::ml_dsa_87::generate_key_pair,
libcrux_ml_dsa::ml_dsa_87::sign,
libcrux_ml_dsa::ml_dsa_87::verify
);

0 comments on commit e61d8ad

Please sign in to comment.