Skip to content

Commit

Permalink
init nist keygen, siggen, sigver
Browse files Browse the repository at this point in the history
  • Loading branch information
eschorn1 committed Sep 21, 2024
1 parent d8d6fb2 commit 7ec2819
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 20 deletions.
6 changes: 4 additions & 2 deletions src/ml_dsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ pub(crate) fn sign_finish<
// We may have arrived via `HashML-DSA.Sign()`
let mut h6 = if oid.is_empty() {
// From ML-DSA.Sing(): 𝑀′ ← BytesToBits(IntegerToBytes(0,1) ∥ IntegerToBytes(|𝑐𝑡𝑥|,1) ∥ 𝑐𝑡𝑥) ∥ 𝑀
h_xof(&[tr, &[0u8], &[ctx.len().to_le_bytes()[0]], ctx, message])
//h_xof(&[tr, &[0u8], &[ctx.len().to_le_bytes()[0]], ctx, message]) // TODO: OMFG! <---- CAVP VECTORS WHA!!!
h_xof(&[tr, message])
} else {
// From HashML-DSA.Sign(): 𝑀′ ← BytesToBits(IntegerToBytes(1,1) ∥ IntegerToBytes(|𝑐𝑡𝑥|,1) ∥ 𝑐𝑡𝑥 ∥ OID ∥ PH𝑀 )
h_xof(&[tr, &[0x01u8], &[oid.len().to_le_bytes()[0]], ctx, oid, phm])
Expand Down Expand Up @@ -370,7 +371,8 @@ pub(crate) fn verify_finish<
// 7: µ ← H(tr || M, 512) ▷ Compute message representative µ
let mut h7 = if oid.is_empty() {
// From ML-DSA.Verify(): 5: 𝑀′ ← BytesToBits(IntegerToBytes(0,1) ∥ IntegerToBytes(|𝑐𝑡𝑥|,1) ∥ 𝑐𝑡𝑥) ∥ 𝑀
h_xof(&[tr, &[0u8], &[ctx.len().to_le_bytes()[0]], ctx, m])
// h_xof(&[tr, &[0u8], &[ctx.len().to_le_bytes()[0]], ctx, m]) // TODO: OMFG! <---- CAVP VECTORS WHA!!!
h_xof(&[tr, m])
} else {
// From HashML-DSA.Verify(): 18: 𝑀′ ← BytesToBits(IntegerToBytes(1,1) ∥ IntegerToBytes(|𝑐𝑡𝑥|,1) ∥ 𝑐𝑡𝑥 ∥ OID ∥ PH𝑀 )
h_xof(&[tr, &[0x01u8], &[oid.len().to_le_bytes()[0]], ctx, oid, phm])
Expand Down
87 changes: 69 additions & 18 deletions tests/nist_vectors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use fips204::ml_dsa_65;
#[cfg(feature = "ml-dsa-87")]
use fips204::ml_dsa_87;

use fips204::traits::{SerDes, Signer}; //{KeyGen, SerDes, Signer, Verifier};
use fips204::traits::{SerDes, Signer, Verifier};


// ----- CUSTOM RNG TO REPLAY VALUES -----
Expand Down Expand Up @@ -91,7 +91,6 @@ fn test_keygen() {
}
}

#[ignore]
#[test]
fn test_siggen() {
let vectors =
Expand All @@ -105,30 +104,82 @@ fn test_siggen() {
let message = decode(test["message"].as_str().unwrap()).unwrap();
let sig_exp = decode(test["signature"].as_str().unwrap()).unwrap();
let seed = test["rnd"].as_str();
if seed.is_none() {continue}; // TODO: no seed means 00000...00?
let x: [u8; 32];
if seed.is_none() {
x = [0u8; 32];
} else {
x = decode(seed.unwrap()).unwrap().try_into().unwrap();
};
let mut rnd = TestRng::new();
rnd.push(&decode(seed.unwrap()).unwrap());
rnd.push(&x);

#[cfg(feature = "ml-dsa-44")]
if test_group["parameterSet"] == "ML-DSA-44" {
let sk = ml_dsa_44::PrivateKey::try_from_bytes(sk_bytes.try_into().unwrap()).unwrap();
let sk =
ml_dsa_44::PrivateKey::try_from_bytes(sk_bytes.clone().try_into().unwrap())
.unwrap();
let sig_act = sk.try_sign_with_rng(&mut rnd, &message, &[]).unwrap();
assert_eq!(sig_exp, sig_act);
}

// #[cfg(feature = "ml-dsa-65")]
// if test_group["parameterSet"] == "ML-DSA-65" {
// let (pk_act, sk_act) = ml_dsa_65::try_keygen_with_rng(&mut rnd).unwrap();
// assert_eq!(pk_exp, pk_act.into_bytes());
// assert_eq!(sk_exp, sk_act.into_bytes());
// }
//
// #[cfg(feature = "ml-dsa-87")]
// if test_group["parameterSet"] == "ML-DSA-87" {
// let (pk_act, sk_act) = ml_dsa_87::try_keygen_with_rng(&mut rnd).unwrap();
// assert_eq!(pk_exp, pk_act.into_bytes());
// assert_eq!(sk_exp, sk_act.into_bytes());
// }
#[cfg(feature = "ml-dsa-65")]
if test_group["parameterSet"] == "ML-DSA-65" {
let sk =
ml_dsa_65::PrivateKey::try_from_bytes(sk_bytes.clone().try_into().unwrap())
.unwrap();
let sig_act = sk.try_sign_with_rng(&mut rnd, &message, &[]).unwrap();
assert_eq!(sig_exp, sig_act);
}

#[cfg(feature = "ml-dsa-87")]
if test_group["parameterSet"] == "ML-DSA-87" {
let sk =
ml_dsa_87::PrivateKey::try_from_bytes(sk_bytes.try_into().unwrap()).unwrap();
let sig_act = sk.try_sign_with_rng(&mut rnd, &message, &[]).unwrap();
assert_eq!(sig_exp, sig_act);
}
}
}
}

#[test]
fn test_sigver() {
let vectors =
fs::read_to_string("./tests/nist_vectors/ML-DSA-sigVer-FIPS204/internalProjection.json")
.expect("Unable to read file");
let v: Value = serde_json::from_str(&vectors).unwrap();

for test_group in v["testGroups"].as_array().unwrap().iter() {
//let sk_bytes = decode(test_group["sk"].as_str().unwrap()).unwrap();
let pk_bytes = decode(test_group["pk"].as_str().unwrap()).unwrap();
for test in test_group["tests"].as_array().unwrap().iter() {
let message = decode(test["message"].as_str().unwrap()).unwrap();
let signature = decode(test["signature"].as_str().unwrap()).unwrap();
let test_passed = test["testPassed"].as_bool().unwrap();

#[cfg(feature = "ml-dsa-44")]
if test_group["parameterSet"] == "ML-DSA-44" {
let pk = ml_dsa_44::PublicKey::try_from_bytes(pk_bytes.clone().try_into().unwrap())
.unwrap();
let res = pk.verify(&message, &signature.clone().try_into().unwrap(), &[]);
assert_eq!(res, test_passed);
}

#[cfg(feature = "ml-dsa-65")]
if test_group["parameterSet"] == "ML-DSA-65" {
let pk = ml_dsa_65::PublicKey::try_from_bytes(pk_bytes.clone().try_into().unwrap())
.unwrap();
let res = pk.verify(&message, &signature.clone().try_into().unwrap(), &[]);
assert_eq!(res, test_passed);
}

#[cfg(feature = "ml-dsa-87")]
if test_group["parameterSet"] == "ML-DSA-87" {
let pk = ml_dsa_87::PublicKey::try_from_bytes(pk_bytes.clone().try_into().unwrap())
.unwrap();
let res = pk.verify(&message, &signature.try_into().unwrap(), &[]);
assert_eq!(res, test_passed);
}
}
}
}

0 comments on commit 7ec2819

Please sign in to comment.