//! Sign and verify a synthetic report end-to-end. //! //! No GPU required. The fixture exercises: //! 1. Canonical JSON is deterministic for a given payload. //! 2. A signed `BoxReport` and `CardReport` verify. //! 3. The card payload's `box_report` hash matches what //! `box_payload_hash` derives from the box payload. //! 4. Tampering with any signed field breaks verification. //! 5. Key derivation, canonical encoding, and signature bytes match a //! pinned fixture, so a format break cannot pass silently. use everycycle_appraise::{ TenantKey, VerifyError, box_payload_hash, canonical_json, sign_box_report, sign_card_report, verify_box_report, verify_card_report, }; use everycycle_hal::{ BoxReport, BoxReportPayload, CapabilityVector, CardReportPayload, DeviceIdentity, DriverBinding, HostIdentity, Interconnect, LaunchOverhead, Memory, PeerLink, Precision, PrecisionThroughput, ProbeEnvironment, Signature, ThermalEnvelope, }; const TENANT: &str = "tailoredmachines"; const SEED: [u8; 32] = [7u8; 32]; const REPORT_VERSION: &str = "1"; const PROBED_AT: &str = "2026-06-20T18:23:11Z"; const PROBE_SUITE: &str = "0.1.0"; fn box_payload() -> BoxReportPayload { BoxReportPayload { report_version: REPORT_VERSION.into(), probed_at: PROBED_AT.into(), host: HostIdentity { hostname: "astra".into(), dmi_string: Some("synthetic-dmi".into()), cpu_model: "synthetic-cpu".into(), }, present_devices: vec!["0000:01:00.0".into()], driver_versions: vec![DriverBinding { bus_address: "0000:01:00.0".into(), driver_version: "580.126.18".into(), }], ambient_c: 24.5, probe_suite_version: PROBE_SUITE.into(), } } fn capability() -> CapabilityVector { CapabilityVector { identity: DeviceIdentity { bus_address: "0000:01:00.0".into(), pci_ids: "10de:2c05:0000:0000".into(), vbios_hash: Some([0xab; 32]), serial: None, }, environment: ProbeEnvironment { driver_version: "580.126.18".into(), ambient_c: 24.5, duration_s: 120.0, probe_version: PROBE_SUITE.into(), }, throughput: vec![ PrecisionThroughput { precision: Precision::Fp16, tops: 250.0, matrix_engine: true, }, PrecisionThroughput { precision: Precision::Bf16, tops: 250.0, matrix_engine: true, }, PrecisionThroughput { precision: Precision::Fp8E4M3, tops: 500.0, matrix_engine: true, }, PrecisionThroughput { precision: Precision::Int8, tops: 500.0, matrix_engine: true, }, ], memory: Memory { physical_bytes: 17_179_869_184, usable_bytes: 16_000_000_000, bandwidth_read_bps: 700.0e9, bandwidth_write_bps: 680.0e9, errors_observed: 0, }, interconnect: Interconnect { host_bps: 32.0e9, peers: Vec::::new(), }, launch: LaunchOverhead { null_launch_ns: 1_800.0, memcpy_1mib_ns: 95_000.0, }, thermal: ThermalEnvelope { time_to_throttle_s: None, throttled_ratio: 1.0, sustained_power_w: 285.0, }, } } #[test] fn canonical_json_is_deterministic() { let a = canonical_json(&box_payload()).unwrap(); let b = canonical_json(&box_payload()).unwrap(); assert_eq!(a, b); let cap = capability(); let one = canonical_json(&cap).unwrap(); let two = canonical_json(&cap).unwrap(); assert_eq!(one, two); } #[test] fn signed_round_trip() { let key = TenantKey::from_seed(TENANT, SEED); let box_payload = box_payload(); let box_hash = box_payload_hash(&box_payload).unwrap(); let box_report = sign_box_report(&key, box_payload).unwrap(); verify_box_report(&box_report).expect("box report verifies"); let card_payload = CardReportPayload { report_version: REPORT_VERSION.into(), probed_at: PROBED_AT.into(), box_report: box_hash.clone(), capability: capability(), }; let card_report = sign_card_report(&key, card_payload).unwrap(); verify_card_report(&card_report).expect("card report verifies"); // The card report's box_report field matches the hash a verifier // would re-derive from the box payload it was built against. assert_eq!(card_report.payload.box_report, box_hash); } #[test] fn tampered_payload_fails() { let key = TenantKey::from_seed(TENANT, SEED); let mut box_report = sign_box_report(&key, box_payload()).unwrap(); box_report.payload.ambient_c += 1.0; assert!(matches!( verify_box_report(&box_report), Err(VerifyError::SignatureMismatch) )); let box_hash = box_payload_hash(&box_payload()).unwrap(); let mut card_report = sign_card_report( &key, CardReportPayload { report_version: REPORT_VERSION.into(), probed_at: PROBED_AT.into(), box_report: box_hash, capability: capability(), }, ) .unwrap(); card_report.payload.capability.memory.usable_bytes -= 1; assert!(matches!( verify_card_report(&card_report), Err(VerifyError::SignatureMismatch) )); } #[test] fn tampered_signature_fails() { let key = TenantKey::from_seed(TENANT, SEED); let mut box_report = sign_box_report(&key, box_payload()).unwrap(); // Flip a base64 character in the signature to a different valid one. let s = &mut box_report.signature.sig; let first = s.chars().next().unwrap(); let replacement = if first == 'A' { 'B' } else { 'A' }; s.replace_range(0..1, &replacement.to_string()); assert!(verify_box_report(&box_report).is_err()); } /// Byte-level constants pinned from the fixture above. Every other test /// in this file signs and verifies inside one process, so all of them /// stay green if seed-to-key derivation, canonical encoding, or /// signature encoding changes. These literals are the only thing that /// notices. /// /// Regenerating them is never the fix for a failure here. A mismatch /// means previously issued appraisals no longer verify, which is a /// format break that needs a version bump, not a new constant. mod pinned { /// `TenantKey::from_seed(TENANT, SEED).pubkey_b64()`. Guards /// seed-to-key derivation and the base64 alphabet. pub(crate) const PUBKEY_B64: &str = "6kpsY+KcUgq+9VB7Ey7F+ZVHdq6+vnuSQh7qaRRG0iw="; /// `canonical_json(&box_payload())`. Guards key ordering at every /// level, compact separators, and float formatting. pub(crate) const BOX_JSON: &str = r#"{"ambient_c":24.5,"driver_versions":[{"bus_address":"0000:01:00.0","driver_version":"580.126.18"}],"host":{"cpu_model":"synthetic-cpu","dmi_string":"synthetic-dmi","hostname":"astra"},"present_devices":["0000:01:00.0"],"probe_suite_version":"0.1.0","probed_at":"2026-06-20T18:23:11Z","report_version":"1"}"#; /// `box_payload_hash(&box_payload())`. Guards the SHA-256 digest and /// the lowercase hex encoding. pub(crate) const BOX_HASH: &str = "b875c714b30278ded37f8522bcec679565a97a723e30820e4f378e027e22799d"; /// Signature over `BOX_JSON`. Ed25519 nonces are derived from the /// key and message (RFC 8032), so this is reproducible. pub(crate) const BOX_SIG: &str = "gBVK+hWaX5SkmjzgwvhhsyYb9CTSo0I7xf1394G3TEQddri8CexNzs198N10cAocMu98DSrvowIHlI9oveCFCQ=="; /// Signature over the card payload, which nests `CapabilityVector` /// and so also covers enum-variant and nested-object encoding that /// the box payload never exercises. pub(crate) const CARD_SIG: &str = "8Hwfz2Lf+CAsn8iU2AsNhwH0gyO192aAjAT4bCcaSUJiUyqQsdsiiXXMckMek5OF/7Ip+sv/X069dKImdHFVDg=="; } #[test] fn seed_derives_the_pinned_pubkey() { let key = TenantKey::from_seed(TENANT, SEED); assert_eq!(key.pubkey_b64(), pinned::PUBKEY_B64); } #[test] fn box_payload_encodes_to_the_pinned_bytes() { assert_eq!(canonical_json(&box_payload()).unwrap(), pinned::BOX_JSON); assert_eq!(box_payload_hash(&box_payload()).unwrap(), pinned::BOX_HASH); } #[test] fn signatures_match_the_pinned_fixture() { let key = TenantKey::from_seed(TENANT, SEED); let box_report = sign_box_report(&key, box_payload()).unwrap(); assert_eq!(box_report.signature.alg, "ed25519"); assert_eq!(box_report.signature.tenant_identity, TENANT); assert_eq!(box_report.signature.tenant_pubkey, pinned::PUBKEY_B64); assert_eq!(box_report.signature.sig, pinned::BOX_SIG); let card_report = sign_card_report( &key, CardReportPayload { report_version: REPORT_VERSION.into(), probed_at: PROBED_AT.into(), box_report: pinned::BOX_HASH.into(), capability: capability(), }, ) .unwrap(); assert_eq!(card_report.signature.sig, pinned::CARD_SIG); } /// A report carrying the pinned signature verifies without ever calling /// the signing path, which is what an archived appraisal does. #[test] fn a_report_rebuilt_from_pinned_bytes_verifies() { let report = BoxReport { payload: box_payload(), signature: Signature { alg: "ed25519".into(), tenant_identity: TENANT.into(), tenant_pubkey: pinned::PUBKEY_B64.into(), sig: pinned::BOX_SIG.into(), }, }; verify_box_report(&report).expect("pinned signature still verifies"); } #[test] fn box_hash_changes_with_any_field() { let baseline = box_payload_hash(&box_payload()).unwrap(); let mut tweaked = box_payload(); tweaked.ambient_c = 25.0; let after = box_payload_hash(&tweaked).unwrap(); assert_ne!(baseline, after); }