Files
arcanum/tests/kat_mlkem768.rs

131 lines
5.2 KiB
Rust

//! Mandatory test 1a: ML-KEM-768 known-answer tests from NIST ACVP (FIPS 203).
//!
//! Runs the vetted `ml-kem` crate, exactly as Arcanum uses it, against the official vectors.
//! Provenance and sha256 pins: tests/vectors/SOURCES.md.
mod common;
use common::{assert_sha256, hex};
#[allow(deprecated)]
use ml_kem::ExpandedKeyEncoding;
use ml_kem::array::Array;
use ml_kem::{B32, Decapsulate, DecapsulationKey, EncapsulationKey, KeyExport, MlKem768, Seed};
use serde_json::Value;
const KEYGEN: &[u8] = include_bytes!("vectors/acvp-ml-kem-keyGen-FIPS203-internalProjection.json");
const KEYGEN_SHA256: &str = "d7a62a2c3476957f56dd8d24f9004ea6776ccfe995ffe71a65bb9506dc9c7b1b";
const ENCDEC: &[u8] = include_bytes!("vectors/acvp-ml-kem-encapDecap-FIPS203-internalProjection.json");
const ENCDEC_SHA256: &str = "a556952ce869bb89c3a3196a701dad89647c193a34c86eafb61a9d710d5b810f";
/// The ML-KEM-768 test groups for one `function` ("" for keyGen, which has none).
fn groups(file: &[u8], function: &str) -> Vec<Value> {
let v: Value = serde_json::from_slice(file).expect("ACVP json");
v["testGroups"]
.as_array()
.expect("testGroups")
.iter()
.filter(|g| g["parameterSet"] == "ML-KEM-768")
.filter(|g| g["function"].as_str().unwrap_or("") == function)
.cloned()
.collect()
}
fn tests_of(groups: &[Value]) -> Vec<Value> {
groups.iter().flat_map(|g| g["tests"].as_array().expect("tests").clone()).collect()
}
fn field(t: &Value, name: &str) -> Vec<u8> {
hex(t[name].as_str().unwrap_or_else(|| panic!("field {name}")))
}
fn ek_from(bytes: &[u8]) -> Result<EncapsulationKey<MlKem768>, ml_kem::InvalidKey> {
let arr = Array::try_from(bytes).expect("ek length");
EncapsulationKey::<MlKem768>::new(&arr)
}
#[allow(deprecated)]
fn dk_from_expanded(bytes: &[u8]) -> Result<DecapsulationKey<MlKem768>, ml_kem::InvalidKey> {
let arr = Array::try_from(bytes).expect("dk length");
DecapsulationKey::<MlKem768>::from_expanded_bytes(&arr)
}
#[test]
fn vector_files_are_the_pinned_ones() {
assert_sha256(KEYGEN, KEYGEN_SHA256, "ACVP keyGen");
assert_sha256(ENCDEC, ENCDEC_SHA256, "ACVP encapDecap");
}
#[test]
#[allow(deprecated)]
fn acvp_keygen_768() {
let tests = tests_of(&groups(KEYGEN, ""));
assert_eq!(tests.len(), 25, "ACVP keyGen ML-KEM-768 has 25 cases");
for t in &tests {
let mut seed = field(t, "d");
seed.extend(field(t, "z"));
let dk = DecapsulationKey::<MlKem768>::from_seed(Seed::try_from(seed.as_slice()).unwrap());
assert_eq!(dk.encapsulation_key().to_bytes().as_slice(), field(t, "ek").as_slice(), "tcId {}", t["tcId"]);
assert_eq!(dk.to_expanded_bytes().as_slice(), field(t, "dk").as_slice(), "tcId {}", t["tcId"]);
}
}
#[test]
fn acvp_encapsulation_768() {
let tests = tests_of(&groups(ENCDEC, "encapsulation"));
assert_eq!(tests.len(), 25);
for t in &tests {
let ek = ek_from(&field(t, "ek")).expect("valid ek");
let m = B32::try_from(field(t, "m").as_slice()).unwrap();
let (c, k) = ek.encapsulate_deterministic(&m);
assert_eq!(c.as_slice(), field(t, "c").as_slice(), "tcId {}", t["tcId"]);
assert_eq!(k.as_slice(), field(t, "k").as_slice(), "tcId {}", t["tcId"]);
}
}
/// Includes "modified ciphertext" cases: implicit rejection must return the vector's K-bar.
#[test]
fn acvp_decapsulation_768_including_implicit_rejection() {
let tests = tests_of(&groups(ENCDEC, "decapsulation"));
assert_eq!(tests.len(), 10);
let mut modified = 0;
for t in &tests {
let dk = dk_from_expanded(&field(t, "dk")).expect("valid dk");
let c = Array::try_from(field(t, "c").as_slice()).unwrap();
let k = dk.decapsulate(&c);
assert_eq!(k.as_slice(), field(t, "k").as_slice(), "tcId {}", t["tcId"]);
if t["reason"] == "modified ciphertext" {
modified += 1;
}
}
assert!(modified > 0, "the set must exercise implicit rejection");
}
/// FIPS 203 §7.2 encapsulation key check: the modulus check must reject the bad keys.
#[test]
fn acvp_encapsulation_key_check_768() {
let tests = tests_of(&groups(ENCDEC, "encapsulationKeyCheck"));
assert_eq!(tests.len(), 10);
let mut rejected = 0;
for t in &tests {
let expected = t["testPassed"].as_bool().unwrap();
assert_eq!(ek_from(&field(t, "ek")).is_ok(), expected, "tcId {} ({})", t["tcId"], t["reason"]);
// Arcanum's own parser must agree: a hybrid key built on a bad ML-KEM key is refused.
let mut hybrid = field(t, "ek");
hybrid.extend([9u8; 32]);
assert_eq!(arcanum::xwing::PublicKey::from_bytes(&hybrid).is_ok(), expected, "tcId {}", t["tcId"]);
rejected += usize::from(!expected);
}
assert!(rejected > 0, "the set must contain keys that fail the check");
}
/// FIPS 203 §7.3 decapsulation key check (hash of ek inside dk).
#[test]
fn acvp_decapsulation_key_check_768() {
let tests = tests_of(&groups(ENCDEC, "decapsulationKeyCheck"));
assert_eq!(tests.len(), 10);
for t in &tests {
let expected = t["testPassed"].as_bool().unwrap();
assert_eq!(dk_from_expanded(&field(t, "dk")).is_ok(), expected, "tcId {} ({})", t["tcId"], t["reason"]);
}
}