vouch-core 3.2.0

Vouch Protocol canonical core: JCS canonicalization, Ed25519 + ML-DSA proofs, did:key/multikey, Data Integrity (eddsa-jcs-2022), credentials, delegation, revocation. One byte-exact implementation shared by every language SDK via WASM and UniFFI/C-FFI.
Documentation
//! Proof sets (two separate proofs, eddsa-jcs-2022 and mldsa44-jcs-2024) and
//! strict Ed25519 against the shared vectors, Rust side. The Python reference, TypeScript and
//! Go read the SAME files and must give the SAME answer for every case.

use serde_json::Value;
use std::fs;
use std::path::Path;

use vouch_core::credentials::verify_with_keys;
use vouch_core::delegation::ChainOptions;
use vouch_core::multikey;

fn load(rel: &str) -> Value {
    let path = Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("../../test-vectors")
        .join(rel);
    let text = fs::read_to_string(&path).unwrap_or_else(|e| panic!("read {rel}: {e}"));
    serde_json::from_str(&text).expect("parse vector")
}

/// The full verifier over the proof-set profile vector
/// (`test-vectors/proof-set`). Supplying the ML-DSA-44 key is what makes
/// post-quantum required, so no other option is set. A proof set is exactly
/// one eddsa-jcs-2022 and one ML-DSA-44 member, each bound to the issuer.
#[test]
fn full_verifier_matches_proof_set_profile() {
    let v = load("proof-set/vector.json");
    let ed = multikey::decode(v["publicKey"].as_str().unwrap())
        .unwrap()
        .raw_key;
    let ml = multikey::decode(v["mldsa44PublicKey"].as_str().unwrap())
        .unwrap()
        .raw_key;
    let now = v["now"].as_str().unwrap();
    let opts = ChainOptions::default();
    for case in v["cases"].as_array().unwrap() {
        let name = case["name"].as_str().unwrap();
        let got = verify_with_keys(&case["credential"], &ed, Some(&ml), now, 30, &opts)
            .map(|(r, _)| r.is_valid())
            .unwrap_or(false);
        assert_eq!(Some(got), case["valid"].as_bool(), "case {name}");
    }
}

fn unhex(h: &str) -> Vec<u8> {
    (0..h.len())
        .step_by(2)
        .map(|i| u8::from_str_radix(&h[i..i + 2], 16).unwrap())
        .collect()
}

/// Strict Ed25519 (`test-vectors/ed25519-strict`): weak and unreduced keys
/// are refused at decode time, small-order R and S >= L are refused.
#[test]
fn strict_ed25519_vector_matches_reference() {
    let v = load("ed25519-strict/vector.json");
    for k in v["publicKeys"].as_array().unwrap() {
        let name = k["name"].as_str().unwrap();
        let raw = unhex(k["hex"].as_str().unwrap());
        let got = vouch_core::keys::strict_verifying_key(&raw).is_ok();
        assert_eq!(Some(got), k["valid"].as_bool(), "public key {name}");
        let mk = multikey::encode_ed25519_public(&raw).unwrap();
        assert_eq!(multikey::decode(&mk).is_ok(), got, "multikey {name}");
    }
    for s in v["signatures"].as_array().unwrap() {
        let name = s["name"].as_str().unwrap();
        let got = vouch_core::keys::verify(
            &unhex(s["publicKey"].as_str().unwrap()),
            &unhex(s["message"].as_str().unwrap()),
            &unhex(s["signature"].as_str().unwrap()),
        )
        .unwrap_or(false);
        assert_eq!(Some(got), s["valid"].as_bool(), "signature {name}");
    }
    for c in v["credentials"].as_array().unwrap() {
        let name = c["name"].as_str().unwrap();
        let cred = &c["credential"];
        let issuer = cred["issuer"].as_str().unwrap_or("");
        // With no key given the verifier resolves the did:key; a weak one never
        // resolves, so the credential never verifies.
        let got = match vouch_core::keys::did_key_to_ed25519(issuer) {
            Ok(k) => vouch_core::credentials::verify(cred, &k, c["now"].as_str().unwrap(), 30)
                .map(|r| r.is_valid())
                .unwrap_or(false),
            Err(_) => false,
        };
        assert_eq!(Some(got), c["valid"].as_bool(), "credential {name}");
    }
}