Skip to content

Commit

Permalink
Added invalid signing key self test.
Browse files Browse the repository at this point in the history
  • Loading branch information
xvzcf committed Jul 1, 2024
1 parent cb3e22a commit 4d054bd
Showing 1 changed file with 76 additions and 17 deletions.
93 changes: 76 additions & 17 deletions libcrux-ml-dsa/tests/self.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use libcrux_ml_dsa::{ml_dsa_44, ml_dsa_65, ml_dsa_87};
use rand::{rngs::OsRng, RngCore};

fn random_array<const L: usize>() -> [u8; L] {
Expand All @@ -6,16 +7,34 @@ fn random_array<const L: usize>() -> [u8; L] {
rng.try_fill_bytes(&mut seed).unwrap();
seed
}
fn random_message() -> Vec<u8> {
let mut rng = OsRng;

let mut length = [0u8; 2];
rng.try_fill_bytes(&mut length).unwrap();
let length = ((length[1] as u16) << 8) | length[0] as u16;

let mut message = Vec::with_capacity(length.into());
rng.try_fill_bytes(&mut message).unwrap();

message
}

fn modify_signing_key<const SIGNING_KEY_SIZE: usize>(signing_key: &mut [u8; SIGNING_KEY_SIZE]) {
// TODO: See if we can make it more dynamic than this.
for byte in signing_key[128..].iter_mut() {
*byte = 1;
}
}

macro_rules! impl_consistency {
macro_rules! impl_consistency_test {
($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::<94883>();
let message = random_message();

let key_pair = $key_gen(key_generation_seed);

Expand All @@ -27,23 +46,63 @@ macro_rules! impl_consistency {
};
}

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

let message = random_message();

let mut key_pair = $key_gen(key_generation_seed);

modify_signing_key::<{ $signing_key_size }>(&mut key_pair.signing_key.0);

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

assert!($verify(key_pair.verification_key, &message, signature).is_err());
}
};
}

impl_consistency_test!(
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
ml_dsa_44::generate_key_pair,
ml_dsa_44::sign,
ml_dsa_44::verify
);

impl_consistency!(
impl_consistency_test!(
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
ml_dsa_65::generate_key_pair,
ml_dsa_65::sign,
ml_dsa_65::verify
);

impl_consistency!(
impl_consistency_test!(
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
ml_dsa_87::generate_key_pair,
ml_dsa_87::sign,
ml_dsa_87::verify
);

impl_invalid_signing_key_test!(
invalid_signing_key_44,
ml_dsa_44::generate_key_pair,
ml_dsa_44::SIGNING_KEY_SIZE,
ml_dsa_44::sign,
ml_dsa_44::verify
);
impl_invalid_signing_key_test!(
invalid_signing_key_65,
ml_dsa_65::generate_key_pair,
ml_dsa_65::SIGNING_KEY_SIZE,
ml_dsa_65::sign,
ml_dsa_65::verify
);
impl_invalid_signing_key_test!(
invalid_signing_key_87,
ml_dsa_87::generate_key_pair,
ml_dsa_87::SIGNING_KEY_SIZE,
ml_dsa_87::sign,
ml_dsa_87::verify
);

0 comments on commit 4d054bd

Please sign in to comment.