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
//! Protocol time grammar against the shared vector (Rust side).
//!
//! `test-vectors/time/vector.json` is the cross-language contract: the Python
//! reference, TypeScript and Go read the SAME file, so a time one of them
//! refuses is refused everywhere, and fractions compare at full precision.

use serde_json::{json, Value};
use std::fs;

use vouch_core::credentials::verify_temporal;
use vouch_core::time::{
    check_clock_skew, clock_skew_from_json, parse_protocol_time_ns, window_state,
};

fn vector() -> Value {
    let path = concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/../../test-vectors/time/vector.json"
    );
    serde_json::from_str(&fs::read_to_string(path).expect("read time vector"))
        .expect("parse time vector")
}

#[test]
fn parse_cases_match_reference() {
    let v = vector();
    let cases = v["parse"].as_array().expect("parse cases");
    assert!(cases.len() > 50);
    for case in cases {
        let input = case["input"].as_str().unwrap();
        let got = parse_protocol_time_ns(input);
        if case["valid"] == json!(true) {
            let ns = got.unwrap_or_else(|e| panic!("{input:?} must parse: {e}"));
            let secs = ns.div_euclid(1_000_000_000) as i64;
            let nanos = ns.rem_euclid(1_000_000_000) as i64;
            assert_eq!(Some(secs), case["epoch_seconds"].as_i64(), "{input:?}");
            assert_eq!(Some(nanos), case["nanos"].as_i64(), "{input:?}");
        } else {
            assert!(got.is_err(), "{input:?} must be refused");
        }
    }
}

#[test]
fn non_string_times_are_refused() {
    let v = vector();
    for value in v["non_string"].as_array().unwrap() {
        assert!(value.as_str().is_none());
        // A credential whose window bounds are not strings never passes.
        let cred = json!({"validFrom": value, "validUntil": "2026-04-26T11:00:00Z"});
        assert!(
            verify_temporal(&cred, "2026-04-26T10:00:00Z", 0).is_err(),
            "{value} as validFrom"
        );
        let cred = json!({"validFrom": "2026-04-26T09:00:00Z", "validUntil": value});
        assert!(
            verify_temporal(&cred, "2026-04-26T10:00:00Z", 0).is_err(),
            "{value} as validUntil"
        );
    }
}

#[test]
fn compare_cases_match_reference() {
    let v = vector();
    for case in v["compare"].as_array().unwrap() {
        let a = parse_protocol_time_ns(case["a"].as_str().unwrap()).unwrap();
        let b = parse_protocol_time_ns(case["b"].as_str().unwrap()).unwrap();
        let sign = match a.cmp(&b) {
            std::cmp::Ordering::Less => -1,
            std::cmp::Ordering::Equal => 0,
            std::cmp::Ordering::Greater => 1,
        };
        assert_eq!(Some(sign), case["expected"].as_i64(), "{case}");
    }
}

#[test]
fn window_cases_match_reference() {
    let v = vector();
    for case in v["window"].as_array().unwrap() {
        let now = case["now"].as_str().unwrap();
        let from = case["validFrom"].as_str();
        let until = case["validUntil"].as_str();
        let skew = case["clockSkewSeconds"].as_i64().unwrap();
        let state = window_state(now, from, until, skew).unwrap();
        assert_eq!(state.as_str(), case["expected"].as_str().unwrap(), "{case}");

        // The credential window check agrees: only "ok" passes.
        let cred = json!({"validFrom": from, "validUntil": until});
        let ok = verify_temporal(&cred, now, skew).unwrap();
        assert_eq!(
            ok,
            case["expected"] == json!("ok"),
            "verify_temporal {case}"
        );
    }
}

#[test]
fn clock_skew_cases_match_reference() {
    let v = vector();
    for case in v["clock_skew"].as_array().unwrap() {
        let input = &case["input"];
        let valid = case["valid"] == json!(true);
        let got = clock_skew_from_json(input).and_then(check_clock_skew);
        assert_eq!(got.is_ok(), valid, "skew {input}");
        if let Some(n) = input.as_i64() {
            let cred = json!({"validFrom": "2026-04-26T09:00:00Z",
                              "validUntil": "2026-04-26T11:00:00Z"});
            assert_eq!(
                verify_temporal(&cred, "2026-04-26T10:00:00Z", n).is_ok(),
                valid,
                "verify_temporal skew {n}"
            );
        }
    }
}