Files
arcanum/tests/kat_xwing.rs
T

83 lines
3.6 KiB
Rust

//! Mandatory test 1c: X-Wing known-answer tests (draft-connolly-cfrg-xwing-kem-11, Appendix C)
//! through Arcanum's own composition, plus a differential test against RustCrypto `x-wing` 0.1.1.
mod common;
use arcanum::xwing::{Ciphertext, PublicKey, SecretKey};
use common::{assert_sha256, hex, rng};
use rand_core::Rng;
use serde::Deserialize;
use x_wing::{Decapsulate, Decapsulator, KeyExport};
const VECTORS: &[u8] = include_bytes!("vectors/xwing-test-vectors.json");
const VECTORS_SHA256: &str = "409efe197550b22985b4a0419418a0c5f2c2b193426c55bd998399ec8d3e614d";
#[derive(Deserialize)]
struct Vector {
seed: String,
sk: String,
pk: String,
eseed: String,
ct: String,
ss: String,
}
fn vectors() -> Vec<Vector> {
assert_sha256(VECTORS, VECTORS_SHA256, "X-Wing vectors");
serde_json::from_slice(VECTORS).expect("X-Wing json")
}
#[test]
fn draft_11_vectors() {
let vs = vectors();
assert_eq!(vs.len(), 3, "draft -11 Appendix C has three vectors");
for (i, v) in vs.iter().enumerate() {
let seed: [u8; 32] = hex(&v.seed).try_into().unwrap();
let sk = SecretKey::from_seed(seed);
assert_eq!(sk.to_seed().as_slice(), hex(&v.sk).as_slice(), "vector {i}: sk");
assert_eq!(sk.public_key().as_bytes().as_slice(), hex(&v.pk).as_slice(), "vector {i}: pk");
let pk = PublicKey::from_bytes(&hex(&v.pk)).expect("vector pk parses");
let eseed: [u8; 64] = hex(&v.eseed).try_into().unwrap();
let (ct, ss) = pk.encapsulate_derand(&eseed).expect("vector encapsulates");
assert_eq!(ct.as_bytes().as_slice(), hex(&v.ct).as_slice(), "vector {i}: ct");
assert_eq!(ss.as_bytes().as_slice(), hex(&v.ss).as_slice(), "vector {i}: ss (encaps)");
let ct = Ciphertext::from_bytes(&hex(&v.ct)).expect("vector ct parses");
let ss = sk.decapsulate(&ct).expect("vector decapsulates");
assert_eq!(ss.as_bytes().as_slice(), hex(&v.ss).as_slice(), "vector {i}: ss (decaps)");
}
}
/// Arcanum and the RustCrypto reference must agree on keys, ciphertexts and secrets for random
/// inputs, and on the implicit-rejection output for random (garbage) ML-KEM ciphertexts.
#[test]
fn differential_against_rustcrypto_x_wing() {
let mut r = rng(7);
for round in 0..64 {
let mut seed = [0u8; 32];
r.fill_bytes(&mut seed);
let mut eseed = [0u8; 64];
r.fill_bytes(&mut eseed);
let ours = SecretKey::from_seed(seed);
let theirs = x_wing::DecapsulationKey::from(seed);
let their_pk = theirs.encapsulation_key().to_bytes();
assert_eq!(ours.public_key().as_bytes().as_slice(), their_pk.as_slice(), "round {round}: pk");
let (our_ct, our_ss) = ours.public_key().encapsulate_derand(&eseed).unwrap();
let their_ek = x_wing::EncapsulationKey::try_from(their_pk.as_slice()).unwrap();
let (their_ct, their_ss) = their_ek.encapsulate_deterministic(&eseed.into());
assert_eq!(our_ct.as_bytes().as_slice(), their_ct.as_slice(), "round {round}: ct");
assert_eq!(our_ss.as_bytes().as_slice(), their_ss.as_slice(), "round {round}: ss");
// A ciphertext whose ML-KEM part is random bytes, with an honest X25519 part.
let mut garbage = *our_ct.as_bytes();
r.fill_bytes(&mut garbage[..1088]);
let ours_ss = ours.decapsulate(&Ciphertext::from_bytes(&garbage).unwrap()).unwrap();
let theirs_ss = theirs.decapsulate(&garbage.into());
assert_eq!(ours_ss.as_bytes().as_slice(), theirs_ss.as_slice(), "round {round}: implicit rejection");
assert_ne!(ours_ss.as_bytes(), our_ss.as_bytes(), "round {round}: garbage must not decapsulate");
}
}