Skip to content

Commit

Permalink
a bit more test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
eschorn1 committed Nov 8, 2024
1 parent 582529c commit cec69ca
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 4 deletions.
93 changes: 92 additions & 1 deletion src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,4 +528,95 @@ mod tests {
simple_bit_pack(&r, (1 << 6) - 1, &mut random_bytes);
// no panic is good news
}
}

#[test]
#[should_panic(expected = "Alg 16: b out of range")]
fn test_simple_bit_pack_b_range() {
let w = R0; //([0i32; 256]);
let mut bytes = [0u8; 32];
simple_bit_pack(&w, 0, &mut bytes); // b must be positive
}

#[test]
#[should_panic(expected = "Alg 16: w out of range")]
fn test_simple_bit_pack_w_range() {
let mut w = R0; //([0i32; 256]);
w.0[0] = 5;
let mut bytes = [0u8; 32];
simple_bit_pack(&w, 3, &mut bytes); // w coefficient > b
}

#[test]
#[should_panic(expected = "Alg 16: incorrect size of output bytes")]
fn test_simple_bit_pack_output_size() {
let w = R0; //([0i32; 256]);
let mut bytes = [0u8; 65]; // Wrong output size
simple_bit_pack(&w, 2, &mut bytes);
}

#[test]
#[should_panic(expected = "Alg 17: a out of range")]
fn test_bit_pack_a_range() {
let w = R0; //([0i32; 256]);
let mut bytes = [0u8; 32];
bit_pack(&w, -1, 2, &mut bytes); // a must be non-negative
}

#[test]
#[should_panic(expected = "Alg 17: b out of range")]
fn test_bit_pack_b_range() {
let w = R0; //([0i32; 256]);
let mut bytes = [0u8; 32];
bit_pack(&w, 0, 0, &mut bytes); // b must be positive
}

#[test]
#[should_panic(expected = "Alg 17: w out of range")]
fn test_bit_pack_w_range() {
let mut w = R0; //([0i32; 256]);
w.0[0] = 10;
let mut bytes = [0u8; 32];
bit_pack(&w, 2, 5, &mut bytes); // w coefficient outside [-a,b] range
}

#[test]
#[should_panic(expected = "Alg 18: b out of range")]
fn test_simple_bit_unpack_b_range() {
let bytes = [0u8; 32];
let _unused = simple_bit_unpack(&bytes, 0); // b must be positive
}

#[test]
#[should_panic(expected = "Alg 18: bad output size")]
fn test_simple_bit_unpack_input_size() {
let bytes = [0u8; 65]; // Wrong input size
let _unused = simple_bit_unpack(&bytes, 2);
}

#[test]
#[should_panic(expected = "Alg 20: omega+K out of range")]
fn test_hint_bit_pack_omega_k_range() {
const K: usize = 255;
let h = [R0; K];
let mut y_bytes = [0u8; 256];
hint_bit_pack::<false, K>(2, &h, &mut y_bytes); // omega + K must be < 256
}

#[test]
#[should_panic(expected = "Alg 20: h not 0/1")]
fn test_hint_bit_pack_h_range() {
const K: usize = 2;
let mut h = [R0; K];
h[0].0[0] = 2; // h must contain only 0s and 1s
let mut y_bytes = [0u8; 4];
hint_bit_pack::<false, K>(2, &h, &mut y_bytes);
}

#[test]
#[should_panic(expected = "Alg 21: omega+K too large")]
fn test_hint_bit_unpack_omega_k_range() {
const K: usize = 255;
let y_bytes = [0u8; 256];
let _unused = hint_bit_unpack::<K>(2, &y_bytes); // omega + K must be < 256
}
}
19 changes: 16 additions & 3 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ pub(crate) const fn partial_reduce64(a: i64) -> i32 {
res as i32
}


#[allow(dead_code)] // I may come back to this and experiment more
#[allow(clippy::cast_possible_truncation)]
#[allow(dead_code, clippy::cast_possible_truncation)] // I may come back to this and experiment more
pub(crate) const fn partial_reduce64b(a: i64) -> i32 {
const MM: i64 = ((1 << 64) / (Q as i128)) as i64;
debug_assert!(a < (i64::MAX / 64), "partial_reduce64b input"); // actually, works for all 64b inputs!!
Expand Down Expand Up @@ -185,3 +183,18 @@ const fn gen_zeta_table_mont() -> [i32; 256] {


pub(crate) static ZETA_TABLE_MONT: [i32; 256] = gen_zeta_table_mont();


#[cfg(test)]
mod tests {
use super::*;

#[test]
fn check_zeta() {
let val = gen_zeta_table_mont();
assert_eq!(val[0], 4193792);
assert_eq!(val[1], 25847);
assert_eq!(val[2], 5771523);

}
}
25 changes: 25 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,31 @@ macro_rules! functionality {
}
assert_eq!(pk.clone().into_bytes(), sk.get_public_key().into_bytes());
}

let (pk, sk) = try_keygen().unwrap();
let sig = sk.try_sign(&message1, &[]).unwrap();
assert!(pk.verify(&message1, &sig, &[]));
assert!(!pk.verify(&message2, &sig, &[]));
assert!(!pk.verify(&message1, &sig, &[0u8; 257]));
assert!(sk.try_sign(&message1, &[0u8; 257]).is_err());

for ph in [Ph::SHA256, Ph::SHA512, Ph::SHAKE128] {
let sig = sk.try_hash_sign(&message1, &[], &ph).unwrap();
let v = pk.hash_verify(&message1, &sig, &[], &ph);
assert!(v);
}
assert_eq!(pk.clone().into_bytes(), sk.get_public_key().into_bytes());

let (pk, _) = KG::keygen_from_seed(&[0x11u8; 32]);
let pk_bytes = pk.into_bytes();
if pk_bytes.len() == 1312 { assert_eq!(pk_bytes[0], 197) }
if pk_bytes.len() == 1952 { assert_eq!(pk_bytes[0], 177) }
if pk_bytes.len() == 2592 { assert_eq!(pk_bytes[0], 16) }

#[cfg(feature = "dudect")]
#[allow(deprecated)] {
assert!(dudect_keygen_sign_with_rng(&mut rng, &[0]).is_ok())
}
}
}

Expand Down

0 comments on commit cec69ca

Please sign in to comment.