Files
arcanum/tests/kat_ed25519.rs
T
mnovak 2a432f5d86 test: the v0.2.0 signature tests, written before the code
The mandatory tests of docs/02_signatures.md, against the API the doc sets
out (arcanum::sign). They do not compile yet: the module does not exist.

1 kat_mldsa65: ACVP keyGen, sigGen (deterministic and hedged groups, byte
  for byte, through sign_randomized/sign_mu_randomized fed the vector's
  rnd), sigVer on the corrected file; Wycheproof sign_seed and verify.
2 kat_ed25519: RFC 8032 section 7.1 and Wycheproof, with verify_strict.
3 sig_composite: the RFC 10073 vectors s and sWithContext verify, the RFC
  key derives the RFC public key, own signatures verify under an
  independent rebuild of M', ML-DSA hedged with ctx = Label.
4 sig_halves: either half swapped in from another signature, zeroed or
  cut, or the halves swapped, is refused; the ML-DSA half fails an
  empty-ctx verifier. Not SUF-CMA (RFC 10073 9.2) is pinned.
5 sig_every_byte: any flipped byte of message, ctx, public key or
  signature fails; contexts are prefix-free; key bindings and app
  messages never verify as each other; every binding field matters.
6 sig_malformed: wrong lengths, weak and non-canonical Ed25519 keys,
  0- and 256-byte contexts, a 65536-byte key id, random bytes.
7 sig_hygiene and feature_hygiene: compile-fail checks against a
  default-features consumer (hazmat raw interface, ml-dsa internals,
  non-crypto RNG, Clone), ZeroizeOnDrop, Debug; ml-dsa and ed25519-dalek
  in the normal graph with zeroize and without hazmat or defaults. The
  allocator probe covers the signing keys.
8 golden_v1.rs is untouched.

Adds ml-dsa =0.1.2 (alloc, rand_core, zeroize) and ed25519-dalek =3.0.0
(zeroize) with default features off, base64 for the tests, and bumps the
version to 0.2.0. The fuzz target follows with the code.
2026-11-25 12:48:39 +01:00

96 lines
4.1 KiB
Rust

//! Mandatory test 2 (v0.2.0): Ed25519 known answers and edge cases, verified with `verify_strict`,
//! the check Arcanum's composite uses.
//!
//! RFC 8032 §7.1 (all five vectors, signing byte for byte) and Wycheproof `ed25519_test.json`
//! (`20b25694`). Provenance and sha256: tests/vectors/SOURCES.md. Pure Ed25519 signing is
//! deterministic by definition (RFC 8032 §5.1.6); `ed25519_dalek::Signer::sign` is that algorithm.
mod common;
use common::{assert_sha256, hex};
use ed25519_dalek::{Signature, Signer, SigningKey, VerifyingKey};
use serde_json::Value;
const RFC8032: &[u8] = include_bytes!("vectors/rfc8032-7.1-ed25519.json");
const RFC8032_SHA256: &str = "fad8e499cdc3e0324a5b4a93cdbabb33f8aed1df73ecacfdb364f433b0fdf12b";
const WYCHEPROOF: &[u8] = include_bytes!("vectors/wycheproof-ed25519_test.json");
const WYCHEPROOF_SHA256: &str = "752d2ea7d7c6cf4736381b6cbacb61f8182b126ab7cd9b058f00c50084975536";
/// Wycheproof cases where `verify_strict` disagrees with the file's verdict, with the reason.
/// Empty unless a run shows otherwise; each entry must be explained in docs/02_signatures.md.
const KNOWN_DISAGREEMENTS: &[(u64, &str)] = &[];
fn json(file: &[u8]) -> Value {
serde_json::from_slice(file).expect("vector json")
}
fn arr<const N: usize>(v: &Value) -> [u8; N] {
hex(v.as_str().expect("hex field")).try_into().expect("field length")
}
#[test]
fn vector_files_are_the_pinned_ones() {
assert_sha256(RFC8032, RFC8032_SHA256, "RFC 8032 section 7.1 extraction");
assert_sha256(WYCHEPROOF, WYCHEPROOF_SHA256, "Wycheproof ed25519_test");
}
#[test]
fn rfc8032_section_7_1() {
let tests = json(RFC8032)["tests"].as_array().unwrap().clone();
assert_eq!(tests.len(), 5);
for t in &tests {
let name = t["name"].as_str().unwrap();
let sk = SigningKey::from_bytes(&arr(&t["secret_key"]));
let pk: [u8; 32] = arr(&t["public_key"]);
let msg = hex(t["message"].as_str().unwrap());
let want: [u8; 64] = arr(&t["signature"]);
assert_eq!(sk.verifying_key().to_bytes(), pk, "TEST {name}: public key");
let sig = sk.sign(&msg);
assert_eq!(sig.to_bytes(), want, "TEST {name}: signature");
let vk = VerifyingKey::from_bytes(&pk).unwrap();
assert!(vk.verify_strict(&msg, &sig).is_ok(), "TEST {name}: verify_strict");
let mut other = msg.clone();
other.push(0);
assert!(vk.verify_strict(&other, &sig).is_err(), "TEST {name}: an extended message must fail");
let mut bad = want;
bad[0] ^= 1;
assert!(vk.verify_strict(&msg, &Signature::from_bytes(&bad)).is_err(), "TEST {name}: flipped R");
}
}
#[test]
fn wycheproof_ed25519_with_verify_strict() {
let mut disagreements = Vec::new();
let (mut valid, mut invalid) = (0, 0);
for g in json(WYCHEPROOF)["testGroups"].as_array().unwrap() {
let pk = hex(g["publicKey"]["pk"].as_str().unwrap());
let vk = <[u8; 32]>::try_from(pk.as_slice()).ok().and_then(|b| VerifyingKey::from_bytes(&b).ok());
for t in g["tests"].as_array().unwrap() {
let id = t["tcId"].as_u64().unwrap();
let msg = hex(t["msg"].as_str().unwrap());
let sig = Signature::from_slice(&hex(t["sig"].as_str().unwrap())).ok();
let ok = match (&vk, sig) {
(Some(vk), Some(sig)) => vk.verify_strict(&msg, &sig).is_ok(),
_ => false,
};
let want = match t["result"].as_str().unwrap() {
"valid" => true,
"invalid" => false,
other => panic!("tcId {id}: result {other} is not handled"),
};
if want {
valid += 1
} else {
invalid += 1
}
if ok != want {
disagreements.push((id, t["comment"].as_str().unwrap_or("").to_string(), want));
}
}
}
assert_eq!(valid + invalid, 151, "numberOfTests in the file");
let got: Vec<u64> = disagreements.iter().map(|d| d.0).collect();
let known: Vec<u64> = KNOWN_DISAGREEMENTS.iter().map(|k| k.0).collect();
assert_eq!(got, known, "verify_strict disagrees with Wycheproof on: {disagreements:?}");
}