Files
arcanum/tests/common/mod.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

73 lines
2.3 KiB
Rust

//! Helpers shared by the integration tests. Test fixtures only: no real keys, no network.
#![allow(dead_code)]
use chacha20::ChaCha20Rng;
use rand_core::SeedableRng;
use sha2::{Digest, Sha256};
/// A deterministic RNG for tests. Never used outside tests.
pub fn rng(label: u8) -> ChaCha20Rng {
ChaCha20Rng::from_seed([label; 32])
}
pub fn hex(s: &str) -> Vec<u8> {
hex::decode(s).expect("vector hex")
}
pub fn arr<const N: usize>(s: &str) -> [u8; N] {
hex(s).try_into().expect("vector length")
}
/// Fail loudly if a pinned vector file is not the one recorded in tests/vectors/SOURCES.md.
pub fn assert_sha256(bytes: &[u8], expected_hex: &str, name: &str) {
let got = hex::encode(Sha256::digest(bytes));
assert_eq!(got, expected_hex, "{name} is not the pinned file (see tests/vectors/SOURCES.md)");
}
/// Standard base64 (RFC 4648 §4, with padding), as the RFC 10073 vectors use.
pub fn b64(s: &str) -> Vec<u8> {
use base64::Engine;
base64::engine::general_purpose::STANDARD.decode(s).expect("vector base64")
}
/// An "RNG" that returns exactly the given bytes, then panics. It lets a known-answer test feed a
/// vector's `rnd` to the hedged signing path that Arcanum uses (`sign_randomized`), and proves
/// that path draws exactly 32 bytes. Claims `TryCryptoRng` only so the signing API accepts it.
pub struct FixedRng {
bytes: Vec<u8>,
pos: usize,
}
impl FixedRng {
pub fn new(bytes: &[u8]) -> Self {
Self { bytes: bytes.to_vec(), pos: 0 }
}
pub fn used(&self) -> usize {
self.pos
}
}
impl rand_core::TryRng for FixedRng {
type Error = core::convert::Infallible;
fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
let mut b = [0u8; 4];
self.try_fill_bytes(&mut b)?;
Ok(u32::from_le_bytes(b))
}
fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
let mut b = [0u8; 8];
self.try_fill_bytes(&mut b)?;
Ok(u64::from_le_bytes(b))
}
fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Self::Error> {
let end = self.pos + dst.len();
assert!(end <= self.bytes.len(), "FixedRng: asked for more bytes than the vector gives");
dst.copy_from_slice(&self.bytes[self.pos..end]);
self.pos = end;
Ok(())
}
}
impl rand_core::TryCryptoRng for FixedRng {}