//! 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(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 = disagreements.iter().map(|d| d.0).collect(); let known: Vec = KNOWN_DISAGREEMENTS.iter().map(|k| k.0).collect(); assert_eq!(got, known, "verify_strict disagrees with Wycheproof on: {disagreements:?}"); }