Repository Public Apache-2.0
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SPB Quantum Crypto7// File : crates/spb-pqc-core/tests/acvp_mlkem.rs8// Module : Tests9// Repository : https://git.spboucher.ai/spb-quantum-crypto10// Project Website : https://pqce.spboucher.ai11// Created : 2026-12-0312// Last Modified : 2026-12-0313//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : Apache-2.033//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041//! NIST ACVP known answers for ML-KEM (FIPS 203): keyGen and encapDecap,42//! including the encapsulation / decapsulation key checks. 100 % of the43//! vectors in the pinned files are replayed.44#![cfg(feature = "kat")]4546mod common;47use common::*;48use spb_pqc_core::kat;49use spb_pqc_core::kem::{KemAlg, KemPrivateKey, KemPublicKey};5051fn alg(g: &serde_json::Value) -> KemAlg {52 KemAlg::from_name(s(g, "parameterSet")).unwrap()53}5455fn arr32(v: &[u8]) -> [u8; 32] {56 v.try_into().expect("32 bytes")57}5859#[test]60fn mlkem_keygen() {61 let v = load("ML-KEM-keyGen-FIPS203");62 let mut n = 0;63 for g in groups(&v) {64 let a = alg(g);65 for t in tests(g) {66 let (ek, dk) =67 kat::mlkem_keygen(a, &arr32(&bytes(t, "d")), &arr32(&bytes(t, "z"))).unwrap();68 assert_eq!(hex(&ek), s(t, "ek"), "tcId {}", t["tcId"]);69 assert_eq!(hex(&dk), s(t, "dk"), "tcId {}", t["tcId"]);70 n += 1;71 }72 }73 println!("ML-KEM keyGen: {n}/{n} vectors passed");74 assert_eq!(n, 75);75}7677#[test]78fn mlkem_encap_decap_and_key_checks() {79 let v = load("ML-KEM-encapDecap-FIPS203");80 let (mut enc, mut dec, mut ekc, mut dkc) = (0, 0, 0, 0);81 for g in groups(&v) {82 let a = alg(g);83 for t in tests(g) {84 let id = &t["tcId"];85 match s(g, "function") {86 "encapsulation" => {87 let (c, k) =88 kat::mlkem_encaps(a, &bytes(t, "ek"), &arr32(&bytes(t, "m"))).unwrap();89 assert_eq!(hex(&c), s(t, "c"), "tcId {id}");90 assert_eq!(hex(&k), s(t, "k"), "tcId {id}");91 // and the matching decapsulation key recovers it92 assert_eq!(kat::mlkem_decaps(a, &bytes(t, "dk"), &c).unwrap(), k);93 enc += 1;94 }95 "decapsulation" => {96 // valid and modified ciphertexts alike: the expected K is given97 // (implicit rejection value for modified ones).98 let k = kat::mlkem_decaps(a, &bytes(t, "dk"), &bytes(t, "c")).unwrap();99 assert_eq!(hex(&k), s(t, "k"), "tcId {id} ({})", t["reason"]);100 dec += 1;101 }102 "encapsulationKeyCheck" => {103 let ok = KemPublicKey::from_bytes(a, &bytes(t, "ek")).is_ok();104 assert_eq!(105 ok,106 t["testPassed"].as_bool().unwrap(),107 "tcId {id} ({})",108 t["reason"]109 );110 ekc += 1;111 }112 "decapsulationKeyCheck" => {113 let ok = KemPrivateKey::from_bytes(a, &bytes(t, "dk")).is_ok();114 assert_eq!(115 ok,116 t["testPassed"].as_bool().unwrap(),117 "tcId {id} ({})",118 t["reason"]119 );120 dkc += 1;121 }122 other => panic!("unknown function {other}"),123 }124 }125 }126 println!("ML-KEM encapDecap: encapsulation {enc}, decapsulation {dec}, ek checks {ekc}, dk checks {dkc} — all passed");127 assert_eq!(enc + dec + ekc + dkc, 165);128}129