spark-rsi-mv
Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger
Brought by drakestapleton
spark-rsi-mv / evidence / protocol-receipt-v1-superseded-2026-10-26 / test_canary_contract_golden.aef09a6.rs.txt
| 1 | //! Golden tests for the frozen contracts of the evidence-driven RSI cut |
| 2 | //! (docs/plans/2026-10-12-evidence-driven-rsi-cut.md, "Narrow contract freeze"). |
| 3 | //! |
| 4 | //! Three families are pinned: (1) the canonical canary observation encoding and digests of the |
| 5 | //! new version 2 records, (2) the existing signed evaluation receipt bytes, digest and |
| 6 | //! verification, (3) the existing ledger record encoding, hash and validation. A change to any |
| 7 | //! pinned value is a format change and needs a new version, never an edit of the pin. |
| 8 | |
| 9 | use p256::ecdsa::SigningKey; |
| 10 | use sha2::{Digest, Sha256}; |
| 11 | use spark_rsi::canary::observation::ObservationInput; |
| 12 | use spark_rsi::canary::{ |
| 13 | observation_id, CanaryAdapterManifest, CanaryObservationV2, CanaryRequestSpec, |
| 14 | CanaryWorkloadManifest, CandidateIdentity, LogHeader, Nondeterminism, ObservationLogWriter, |
| 15 | Provenance, SealedObservationLog, VerifiedStatus, CANARY_PROTOCOL_VERSION, |
| 16 | }; |
| 17 | use spark_rsi::evaluator::layers::LayerResult; |
| 18 | use spark_rsi::evaluator::{EvaluationMetricsSummary, EvaluationReceipt, ReceiptBindingV2}; |
| 19 | use spark_rsi::ledger::{compute_merkle_root, BlockType, LedgerBlock}; |
| 20 | use spark_rsi::supervisor::ipc::{SupervisorMessage, WorkerMessage}; |
| 21 | |
| 22 | fn sha(bytes: &[u8]) -> String { |
| 23 | hex::encode(Sha256::digest(bytes)) |
| 24 | } |
| 25 | |
| 26 | // ------------------------------------------------------------------ pinned values |
| 27 | |
| 28 | const EXPECTED_WORKLOAD_CANONICAL: &str = "{\"protocol_version\":1,\"requests\":[{\"deadline_ms\":2000,\"expected_output\":\"pong\",\"input\":\"ping\",\"nondeterminism\":\"exact\",\"request_id\":\"req-001\"},{\"deadline_ms\":2000,\"expected_output\":\"5\",\"input\":\"add 2 3\",\"nondeterminism\":\"exact\",\"request_id\":\"req-002\"}],\"workload_id\":\"golden-v1\",\"workload_version\":1}"; |
| 29 | const EXPECTED_WORKLOAD_DIGEST: &str = |
| 30 | "9431eef86afb1c4cd80a8ae023fbf391519d94ab67f6cd9820ae096bb0710870"; |
| 31 | const EXPECTED_REQUEST_001_DIGEST: &str = |
| 32 | "f4b3d03bdbef5edb951f4a5a314989098226748eb720e81c382fbebd86e18017"; |
| 33 | const EXPECTED_ADAPTER_DIGEST: &str = |
| 34 | "4bcfa2c308cc84ea007827def1dbd5d8b5d038a3c676030374ff07621a5320e6"; |
| 35 | const EXPECTED_IDENTITY_BINDING_DIGEST: &str = |
| 36 | "dabd4116b1c49c42716973eb65110e439601396086adc2d35c9cb169e429360a"; |
| 37 | const EXPECTED_OBSERVATION_ID: &str = |
| 38 | "4b269fce18d83d73147676b7358c998a548a99ceebed02816d37a059926b8181"; |
| 39 | const EXPECTED_OBSERVATION_CANONICAL: &str = "{\"candidate_executable_digest\":\"86b30dc0591deb3800c70226d5ed326e20f01f9f8288036acdc7b5bd4c86af75\",\"elapsed_us\":1234,\"generation_id\":\"gen-golden-0001\",\"observation_id\":\"4b269fce18d83d73147676b7358c998a548a99ceebed02816d37a059926b8181\",\"observed_at_ms\":1700000000000,\"provenance\":\"measured\",\"record_version\":2,\"request_digest\":\"f4b3d03bdbef5edb951f4a5a314989098226748eb720e81c382fbebd86e18017\",\"request_id\":\"req-001\",\"result_digest\":\"9795c5ff8937f23526ccb207a5684c1fc94a7854e19c021b39d944e51f5baef2\",\"status\":\"verified_success\",\"workload_digest\":\"9431eef86afb1c4cd80a8ae023fbf391519d94ab67f6cd9820ae096bb0710870\"}"; |
| 40 | const EXPECTED_OBSERVATION_DIGEST: &str = |
| 41 | "550f4dd46e1f3c197b6d8d973061bd978c722b332d5a416681216e8bf5e845c7"; |
| 42 | const EXPECTED_LOG_BYTES: &str = "{\"kind\":\"header\",\"record\":{\"candidate_executable_digest\":\"86b30dc0591deb3800c70226d5ed326e20f01f9f8288036acdc7b5bd4c86af75\",\"generation_id\":\"gen-golden-0001\",\"log_version\":1,\"opened_at_ms\":1700000000000,\"workload_digest\":\"9431eef86afb1c4cd80a8ae023fbf391519d94ab67f6cd9820ae096bb0710870\"}}\n{\"kind\":\"observation\",\"record\":{\"observation\":{\"candidate_executable_digest\":\"86b30dc0591deb3800c70226d5ed326e20f01f9f8288036acdc7b5bd4c86af75\",\"elapsed_us\":1234,\"generation_id\":\"gen-golden-0001\",\"observation_id\":\"4b269fce18d83d73147676b7358c998a548a99ceebed02816d37a059926b8181\",\"observed_at_ms\":1700000000000,\"provenance\":\"measured\",\"record_version\":2,\"request_digest\":\"f4b3d03bdbef5edb951f4a5a314989098226748eb720e81c382fbebd86e18017\",\"request_id\":\"req-001\",\"result_digest\":\"9795c5ff8937f23526ccb207a5684c1fc94a7854e19c021b39d944e51f5baef2\",\"status\":\"verified_success\",\"workload_digest\":\"9431eef86afb1c4cd80a8ae023fbf391519d94ab67f6cd9820ae096bb0710870\"},\"observation_digest\":\"550f4dd46e1f3c197b6d8d973061bd978c722b332d5a416681216e8bf5e845c7\"}}\n{\"kind\":\"rejection\",\"record\":{\"observed_at_ms\":1700000000600,\"reason\":\"unsolicited result for req-009\"}}\n{\"kind\":\"observation\",\"record\":{\"observation\":{\"candidate_executable_digest\":\"86b30dc0591deb3800c70226d5ed326e20f01f9f8288036acdc7b5bd4c86af75\",\"elapsed_us\":2000000,\"generation_id\":\"gen-golden-0001\",\"observation_id\":\"24e16074b967433cf803587aeb02d52c62817a8666e62ab2dcfe4e51bb5d0fe2\",\"observed_at_ms\":1700000002000,\"provenance\":\"measured\",\"record_version\":2,\"request_digest\":\"dfb1b215f345dc50033819a285844ec5ac12ccb07d5c64023c06a343b7cb3dbe\",\"request_id\":\"req-002\",\"result_digest\":null,\"status\":\"timeout\",\"workload_digest\":\"9431eef86afb1c4cd80a8ae023fbf391519d94ab67f6cd9820ae096bb0710870\"},\"observation_digest\":\"2830ea7d18215ec481d598814d0771e27426ebd1211e73237c8dd0e92e9e74a1\"}}\n{\"kind\":\"seal\",\"record\":{\"log_digest\":\"fb5479def502a60f902285ca26fecd75a6130957f7a4f01ba0759f743bc53ee8\",\"observation_count\":2,\"qualifying_count\":1,\"sealed_at_ms\":1700000003000}}\n"; |
| 43 | const EXPECTED_LOG_DIGEST: &str = |
| 44 | "fb5479def502a60f902285ca26fecd75a6130957f7a4f01ba0759f743bc53ee8"; |
| 45 | const EXPECTED_RECEIPT_V2_DIGEST: &str = |
| 46 | "764766d8efa6457fee8d32b17e2a6e5b42661a51afd4824f16830dac1c1b65e2"; |
| 47 | const EXPECTED_RECEIPT_V2_SIGNATURE: &str = "tpm2-p256:99d2faabc3456a1e66c67b01b14f4e027884ad7bfdd8f84dad110b0bcf9f23887d8f640a1fa69b547943766da1d833c2782ffa603591981099fd9341dcbbd797"; |
| 48 | const EXPECTED_RECEIPT_V1_DIGEST: &str = |
| 49 | "2f89a865a7cd6ea329b9df0f31ced5629e4cba6e629e91834737b8caa22cd817"; |
| 50 | const EXPECTED_PROTOCOL_KEY_FINGERPRINT: &str = |
| 51 | "8cbc48b78519c7a8a48f92d6e9e60f06645e872e6d148b660a5aef6afc990905"; |
| 52 | const EXPECTED_PROTOCOL_RECEIPT_DIGEST: &str = |
| 53 | "a7f3ac2e08162a5f2f49805d8cf4f6c8a7f38fe6bb0f2e74437d9bbfce1cdccf"; |
| 54 | const EXPECTED_PROTOCOL_RECEIPT_SIGNATURE: &str = "41b9f0735b4ecceb3e390eeb1eca82f09173ce393ab0bbb8f03d4b5c55a216027d1d0df88d5ac064ac70d7ecd63d29af8e3de04c3ff09dada42a6fa8e09c7240"; |
| 55 | const EXPECTED_LEDGER_PAYLOAD_DIGEST: &str = |
| 56 | "7b8e7b835fe25b68a1acf315430c15febccecb74f2ec198c4adf83ded8ba6c5d"; |
| 57 | const EXPECTED_LEDGER_BLOCK_HASH: &str = |
| 58 | "250ea0215642b8e1c221456e4a86d2249ffe3ac9b7d1999c7cca39e0c734a492"; |
| 59 | const EXPECTED_MERKLE_ROOT: &str = |
| 60 | "507f253911e35467cca570f695cd66b8e32baef97704eb4061b9b638070336f2"; |
| 61 | |
| 62 | const ADAPTER_TEXT: &str = "manifest_version = 1\nprotocol_version = 1\nexecutable = \"release/rsi-canary-fixture\"\nargs = [\"--adapter\"]\nworkload_id = \"golden-v1\"\nresponse_format = \"utf8-exact\"\n"; |
| 63 | |
| 64 | // ------------------------------------------------------------------ fixtures |
| 65 | |
| 66 | fn workload() -> CanaryWorkloadManifest { |
| 67 | CanaryWorkloadManifest { |
| 68 | workload_version: 1, |
| 69 | workload_id: "golden-v1".into(), |
| 70 | protocol_version: 1, |
| 71 | requests: vec![ |
| 72 | CanaryRequestSpec { |
| 73 | request_id: "req-001".into(), |
| 74 | input: "ping".into(), |
| 75 | expected_output: "pong".into(), |
| 76 | deadline_ms: 2000, |
| 77 | nondeterminism: Nondeterminism::Exact, |
| 78 | }, |
| 79 | CanaryRequestSpec { |
| 80 | request_id: "req-002".into(), |
| 81 | input: "add 2 3".into(), |
| 82 | expected_output: "5".into(), |
| 83 | deadline_ms: 2000, |
| 84 | nondeterminism: Nondeterminism::Exact, |
| 85 | }, |
| 86 | ], |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | fn identity() -> CandidateIdentity { |
| 91 | CandidateIdentity::new( |
| 92 | &sha(b"golden patch"), |
| 93 | &sha(b"golden tree"), |
| 94 | &sha(b"golden executable bytes"), |
| 95 | "gen-golden-0001", |
| 96 | ) |
| 97 | } |
| 98 | |
| 99 | fn success_observation() -> CanaryObservationV2 { |
| 100 | let w = workload(); |
| 101 | let id = identity(); |
| 102 | CanaryObservationV2::new(ObservationInput { |
| 103 | generation_id: &id.generation_id, |
| 104 | request_id: "req-001", |
| 105 | request_digest: &w.requests[0].request_digest(&w.workload_id), |
| 106 | workload_digest: &w.digest(), |
| 107 | candidate_executable_digest: &id.executable_digest, |
| 108 | result_digest: Some(&sha(b"pong")), |
| 109 | elapsed_us: 1234, |
| 110 | observed_at_ms: 1_700_000_000_000, |
| 111 | status: VerifiedStatus::VerifiedSuccess, |
| 112 | provenance: Provenance::Measured, |
| 113 | }) |
| 114 | } |
| 115 | |
| 116 | fn timeout_observation() -> CanaryObservationV2 { |
| 117 | let w = workload(); |
| 118 | let id = identity(); |
| 119 | CanaryObservationV2::new(ObservationInput { |
| 120 | generation_id: &id.generation_id, |
| 121 | request_id: "req-002", |
| 122 | request_digest: &w.requests[1].request_digest(&w.workload_id), |
| 123 | workload_digest: &w.digest(), |
| 124 | candidate_executable_digest: &id.executable_digest, |
| 125 | result_digest: None, |
| 126 | elapsed_us: 2_000_000, |
| 127 | observed_at_ms: 1_700_000_002_000, |
| 128 | status: VerifiedStatus::Timeout, |
| 129 | provenance: Provenance::Measured, |
| 130 | }) |
| 131 | } |
| 132 | |
| 133 | fn golden_log() -> (Vec<u8>, SealedObservationLog) { |
| 134 | let tmp = tempfile::tempdir().unwrap(); |
| 135 | let path = tmp.path().join("gen-golden-0001.jsonl"); |
| 136 | let w = workload(); |
| 137 | let id = identity(); |
| 138 | let mut writer = ObservationLogWriter::create( |
| 139 | &path, |
| 140 | LogHeader::new( |
| 141 | &id.generation_id, |
| 142 | &w.digest(), |
| 143 | &id.executable_digest, |
| 144 | 1_700_000_000_000, |
| 145 | ), |
| 146 | ) |
| 147 | .unwrap(); |
| 148 | writer.append_observation(&success_observation()).unwrap(); |
| 149 | writer |
| 150 | .append_rejection("unsolicited result for req-009", 1_700_000_000_600) |
| 151 | .unwrap(); |
| 152 | writer.append_observation(&timeout_observation()).unwrap(); |
| 153 | let sealed = writer.seal(1_700_000_003_000).unwrap(); |
| 154 | (std::fs::read(&path).unwrap(), sealed) |
| 155 | } |
| 156 | |
| 157 | fn layer(name: &str, passed: bool, score: f64) -> LayerResult { |
| 158 | LayerResult { |
| 159 | layer_name: name.to_string(), |
| 160 | is_hard_invariant: true, |
| 161 | passed, |
| 162 | score, |
| 163 | summary: format!("{} summary", name), |
| 164 | violations: Vec::new(), |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | fn base_receipt() -> EvaluationReceipt { |
| 169 | EvaluationReceipt { |
| 170 | cycle_id: "cycle-golden".to_string(), |
| 171 | candidate_id: "cand-golden".to_string(), |
| 172 | parent_id: "parent-golden".to_string(), |
| 173 | evaluated_at: "2026-10-12T00:00:00Z".to_string(), |
| 174 | evaluator_version: "1.0.0".to_string(), |
| 175 | passed_all_hard_invariants: true, |
| 176 | passed_statistical_gates: true, |
| 177 | admitted: true, |
| 178 | layer_results: vec![ |
| 179 | layer("correctness", true, 1.0), |
| 180 | layer("security", true, 0.5), |
| 181 | ], |
| 182 | metrics_summary: Some(EvaluationMetricsSummary { |
| 183 | latency_delta_pct: -1.0, |
| 184 | p_value: 0.01, |
| 185 | p95_ci_upper_degradation_pct: 0.5, |
| 186 | p99_ci_upper_degradation_pct: 0.7, |
| 187 | rss_growth_pct: 0.1, |
| 188 | candidate_resident_mb: 12, |
| 189 | }), |
| 190 | receipt_digest: String::new(), |
| 191 | signature: None, |
| 192 | format_version: 1, |
| 193 | binding: None, |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | fn v2_binding() -> ReceiptBindingV2 { |
| 198 | ReceiptBindingV2 { |
| 199 | subject_path: "README.md".to_string(), |
| 200 | subject_sha256: sha(b"golden readme"), |
| 201 | holdout_set_sha256: sha(b"golden holdouts"), |
| 202 | holdouts_total: 4, |
| 203 | holdouts_passed: 4, |
| 204 | policy_sha256: sha(b"golden policy"), |
| 205 | evaluator_binary_sha256: sha(b"golden judge binary"), |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | fn judge_key() -> SigningKey { |
| 210 | SigningKey::from_slice(&[7u8; 32]).unwrap() |
| 211 | } |
| 212 | |
| 213 | // ------------------------------------------------------------------ (1) canary records |
| 214 | |
| 215 | #[test] |
| 216 | fn workload_canonical_bytes_and_digests_are_frozen() { |
| 217 | let w = workload(); |
| 218 | assert_eq!( |
| 219 | String::from_utf8(w.canonical_bytes()).unwrap(), |
| 220 | EXPECTED_WORKLOAD_CANONICAL |
| 221 | ); |
| 222 | assert_eq!(w.digest(), EXPECTED_WORKLOAD_DIGEST); |
| 223 | assert_eq!( |
| 224 | w.requests[0].request_digest(&w.workload_id), |
| 225 | EXPECTED_REQUEST_001_DIGEST |
| 226 | ); |
| 227 | } |
| 228 | |
| 229 | #[test] |
| 230 | fn adapter_manifest_digest_is_frozen() { |
| 231 | let m = CanaryAdapterManifest::parse(ADAPTER_TEXT).unwrap(); |
| 232 | assert_eq!(m.workload_id, "golden-v1"); |
| 233 | assert_eq!( |
| 234 | CanaryAdapterManifest::digest_of_text(ADAPTER_TEXT), |
| 235 | EXPECTED_ADAPTER_DIGEST |
| 236 | ); |
| 237 | } |
| 238 | |
| 239 | #[test] |
| 240 | fn candidate_identity_binding_digest_is_frozen() { |
| 241 | assert_eq!( |
| 242 | identity().binding_digest(), |
| 243 | EXPECTED_IDENTITY_BINDING_DIGEST |
| 244 | ); |
| 245 | } |
| 246 | |
| 247 | #[test] |
| 248 | fn observation_encoding_id_and_digest_are_frozen() { |
| 249 | let o = success_observation(); |
| 250 | assert_eq!( |
| 251 | o.observation_id, |
| 252 | observation_id("gen-golden-0001", "req-001", &o.request_digest) |
| 253 | ); |
| 254 | assert_eq!(o.observation_id, EXPECTED_OBSERVATION_ID); |
| 255 | assert_eq!( |
| 256 | String::from_utf8(o.canonical_bytes()).unwrap(), |
| 257 | EXPECTED_OBSERVATION_CANONICAL |
| 258 | ); |
| 259 | assert_eq!(o.digest_hex(), EXPECTED_OBSERVATION_DIGEST); |
| 260 | o.validate().unwrap(); |
| 261 | // The record round-trips through JSON unchanged. |
| 262 | let back: CanaryObservationV2 = serde_json::from_slice(&o.canonical_bytes()).unwrap(); |
| 263 | assert_eq!(back, o); |
| 264 | } |
| 265 | |
| 266 | #[test] |
| 267 | fn sealed_log_bytes_and_digest_are_frozen() { |
| 268 | let (bytes, sealed) = golden_log(); |
| 269 | assert_eq!( |
| 270 | String::from_utf8(bytes.clone()).unwrap(), |
| 271 | EXPECTED_LOG_BYTES |
| 272 | ); |
| 273 | assert_eq!(sealed.log_digest(), EXPECTED_LOG_DIGEST); |
| 274 | assert_eq!(sealed.seal.observation_count, 2); |
| 275 | assert_eq!(sealed.seal.qualifying_count, 1); |
| 276 | let verified = SealedObservationLog::verify_bytes(&bytes).unwrap(); |
| 277 | assert_eq!(verified.observations, sealed.observations); |
| 278 | assert_eq!(verified.seal, sealed.seal); |
| 279 | } |
| 280 | |
| 281 | #[test] |
| 282 | fn execute_protocol_wire_format_is_frozen() { |
| 283 | let req = SupervisorMessage::ExecuteRequest { |
| 284 | protocol_version: CANARY_PROTOCOL_VERSION, |
| 285 | generation_id: "gen-golden-0001".into(), |
| 286 | request_id: "req-001".into(), |
| 287 | request_digest: "aa".repeat(32), |
| 288 | workload_digest: "bb".repeat(32), |
| 289 | input: "ping".into(), |
| 290 | deadline_ms: 2000, |
| 291 | }; |
| 292 | assert_eq!( |
| 293 | serde_json::to_string(&req).unwrap(), |
| 294 | format!( |
| 295 | "{{\"ExecuteRequest\":{{\"protocol_version\":1,\"generation_id\":\"gen-golden-0001\",\"request_id\":\"req-001\",\"request_digest\":\"{}\",\"workload_digest\":\"{}\",\"input\":\"ping\",\"deadline_ms\":2000}}}}", |
| 296 | "aa".repeat(32), |
| 297 | "bb".repeat(32) |
| 298 | ) |
| 299 | ); |
| 300 | let res = WorkerMessage::ExecuteResult { |
| 301 | protocol_version: CANARY_PROTOCOL_VERSION, |
| 302 | generation_id: "gen-golden-0001".into(), |
| 303 | request_id: "req-001".into(), |
| 304 | output: "pong".into(), |
| 305 | worker_latency_us: Some(900), |
| 306 | }; |
| 307 | assert_eq!( |
| 308 | serde_json::to_string(&res).unwrap(), |
| 309 | "{\"ExecuteResult\":{\"protocol_version\":1,\"generation_id\":\"gen-golden-0001\",\"request_id\":\"req-001\",\"output\":\"pong\",\"worker_latency_us\":900}}" |
| 310 | ); |
| 311 | let ready = WorkerMessage::Ready { |
| 312 | generation_id: "gen-golden-0001".into(), |
| 313 | pid: 4242, |
| 314 | auth_token: String::new(), |
| 315 | protocol_version: CANARY_PROTOCOL_VERSION, |
| 316 | }; |
| 317 | assert_eq!( |
| 318 | serde_json::to_string(&ready).unwrap(), |
| 319 | "{\"Ready\":{\"generation_id\":\"gen-golden-0001\",\"pid\":4242,\"auth_token\":\"\",\"protocol_version\":1}}" |
| 320 | ); |
| 321 | // A Ready from an older worker has no protocol version and reads as 0: never ready. |
| 322 | let legacy: WorkerMessage = serde_json::from_str( |
| 323 | "{\"Ready\":{\"generation_id\":\"gen-old\",\"pid\":1,\"auth_token\":\"\"}}", |
| 324 | ) |
| 325 | .unwrap(); |
| 326 | match legacy { |
| 327 | WorkerMessage::Ready { |
| 328 | protocol_version, .. |
| 329 | } => assert_eq!(protocol_version, 0), |
| 330 | other => panic!("expected Ready, got {:?}", other), |
| 331 | } |
| 332 | // A result without the diagnostic latency still parses. |
| 333 | let bare: WorkerMessage = serde_json::from_str( |
| 334 | "{\"ExecuteResult\":{\"protocol_version\":1,\"generation_id\":\"g\",\"request_id\":\"r\",\"output\":\"o\"}}", |
| 335 | ) |
| 336 | .unwrap(); |
| 337 | match bare { |
| 338 | WorkerMessage::ExecuteResult { |
| 339 | worker_latency_us, .. |
| 340 | } => assert_eq!(worker_latency_us, None), |
| 341 | other => panic!("expected ExecuteResult, got {:?}", other), |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | // ------------------------------------------------------------------ (2) evaluation receipts |
| 346 | |
| 347 | #[test] |
| 348 | fn evaluation_receipt_v2_digest_signature_and_verification_are_frozen() { |
| 349 | let judge = judge_key(); |
| 350 | let mut r = base_receipt(); |
| 351 | r.bind_and_sign_v2(v2_binding(), &judge).unwrap(); |
| 352 | assert_eq!(r.format_version, 2); |
| 353 | assert_eq!(r.receipt_digest, EXPECTED_RECEIPT_V2_DIGEST); |
| 354 | assert_eq!(r.signature.as_deref(), Some(EXPECTED_RECEIPT_V2_SIGNATURE)); |
| 355 | assert!(r.verify_digest()); |
| 356 | assert!(r.verify_signature(judge.verifying_key())); |
| 357 | r.verify_for_promotion( |
| 358 | judge.verifying_key(), |
| 359 | &sha(b"golden readme"), |
| 360 | &sha(b"golden policy"), |
| 361 | &sha(b"golden holdouts"), |
| 362 | ) |
| 363 | .unwrap(); |
| 364 | // Any change to a bound field breaks the digest and the signature. |
| 365 | let mut tampered = r.clone(); |
| 366 | tampered.binding.as_mut().unwrap().holdouts_passed = 3; |
| 367 | assert!(!tampered.verify_digest()); |
| 368 | assert!(!tampered.verify_signature(judge.verifying_key())); |
| 369 | let other = SigningKey::from_slice(&[9u8; 32]).unwrap(); |
| 370 | assert!(!r.verify_signature(other.verifying_key())); |
| 371 | } |
| 372 | |
| 373 | #[test] |
| 374 | fn evaluation_receipt_v1_digest_is_frozen() { |
| 375 | let r = base_receipt(); |
| 376 | let digest = EvaluationReceipt::compute_digest( |
| 377 | &r.cycle_id, |
| 378 | &r.candidate_id, |
| 379 | &r.parent_id, |
| 380 | r.admitted, |
| 381 | &r.layer_results, |
| 382 | ); |
| 383 | assert_eq!(digest, EXPECTED_RECEIPT_V1_DIGEST); |
| 384 | } |
| 385 | |
| 386 | #[test] |
| 387 | fn protocol_signed_evaluation_receipt_digest_and_signature_are_frozen() { |
| 388 | use aien_evaluation_protocol::{ |
| 389 | CryptographicTier, EvaluationReceipt as ProtocolReceipt, SoftwareP256Signer, Verdict, |
| 390 | VerifierIdentity, VerifierSigner, |
| 391 | }; |
| 392 | use aien_protocol_types::{Digest32, EvaluationId, Timestamp}; |
| 393 | use std::sync::Arc; |
| 394 | |
| 395 | let signer = Arc::new(SoftwareP256Signer::new( |
| 396 | SigningKey::from_slice(&[0x42u8; 32]).unwrap(), |
| 397 | )); |
| 398 | assert_eq!(signer.key_fingerprint(), EXPECTED_PROTOCOL_KEY_FINGERPRINT); |
| 399 | let receipt = ProtocolReceipt { |
| 400 | protocol_version: 1, |
| 401 | evaluation_id: EvaluationId(uuid::Uuid::from_u128( |
| 402 | 0x0123_4567_89ab_cdef_0123_4567_89ab_cdef, |
| 403 | )), |
| 404 | request_digest: Digest32([0x11; 32]), |
| 405 | subject_digest: Digest32([0x22; 32]), |
| 406 | plan_digest: Digest32([0x33; 32]), |
| 407 | evidence_root: Digest32([0x44; 32]), |
| 408 | outcomes_root: Digest32([0x55; 32]), |
| 409 | verdict: Verdict::Pass, |
| 410 | verifier: VerifierIdentity { |
| 411 | principal_id: "rsi-golden".to_string(), |
| 412 | key_id: signer.key_fingerprint(), |
| 413 | trust_epoch: 1, |
| 414 | trusted_build_digest: Digest32([0x66; 32]), |
| 415 | policy_bundle_digest: Digest32([0x77; 32]), |
| 416 | }, |
| 417 | started_at: Timestamp(1_700_000_000_000), |
| 418 | completed_at: Timestamp(1_700_000_000_500), |
| 419 | }; |
| 420 | assert_eq!( |
| 421 | hex::encode(receipt.compute_receipt_digest().0), |
| 422 | EXPECTED_PROTOCOL_RECEIPT_DIGEST |
| 423 | ); |
| 424 | let signed = receipt.sign_with_signer(signer.as_ref()).unwrap(); |
| 425 | assert_eq!( |
| 426 | hex::encode(&signed.signature), |
| 427 | EXPECTED_PROTOCOL_RECEIPT_SIGNATURE |
| 428 | ); |
| 429 | assert!(signed |
| 430 | .verify_with(&signer.verifying_key(), CryptographicTier::Tier2SoftwareKey) |
| 431 | .unwrap()); |
| 432 | let wrong = SigningKey::from_slice(&[0x99u8; 32]).unwrap(); |
| 433 | assert!(!matches!( |
| 434 | signed.verify_with(wrong.verifying_key(), CryptographicTier::Tier2SoftwareKey), |
| 435 | Ok(true) |
| 436 | )); |
| 437 | let mut tampered = signed.clone(); |
| 438 | tampered.receipt.verdict = Verdict::Fail; |
| 439 | assert!(!matches!( |
| 440 | tampered.verify_with(&signer.verifying_key(), CryptographicTier::Tier2SoftwareKey), |
| 441 | Ok(true) |
| 442 | )); |
| 443 | assert!(!matches!( |
| 444 | signed.verify_with(&signer.verifying_key(), CryptographicTier::Tier4HardwareTpm), |
| 445 | Ok(true) |
| 446 | )); |
| 447 | } |
| 448 | |
| 449 | // ------------------------------------------------------------------ (3) ledger records |
| 450 | |
| 451 | #[test] |
| 452 | fn ledger_block_encoding_hash_and_validation_are_frozen() { |
| 453 | let payload = "{\"candidate\":\"cand-golden\",\"verdict\":\"admitted\"}".to_string(); |
| 454 | let block = LedgerBlock::new( |
| 455 | 1, |
| 456 | BlockType::Evaluation, |
| 457 | "2026-10-12T00:00:00+00:00".to_string(), |
| 458 | LedgerBlock::GENESIS_PREV_HASH.to_string(), |
| 459 | payload, |
| 460 | vec![sha(b"golden blob")], |
| 461 | ); |
| 462 | assert_eq!(block.payload_digest, EXPECTED_LEDGER_PAYLOAD_DIGEST); |
| 463 | assert_eq!(block.block_hash, EXPECTED_LEDGER_BLOCK_HASH); |
| 464 | block.verify_integrity().unwrap(); |
| 465 | let mut tampered = block.clone(); |
| 466 | tampered.payload_json = "{\"candidate\":\"cand-golden\",\"verdict\":\"refused\"}".into(); |
| 467 | assert!(tampered.verify_integrity().is_err()); |
| 468 | let mut relinked = block.clone(); |
| 469 | relinked.prev_block_hash = sha(b"another parent"); |
| 470 | assert!(relinked.verify_integrity().is_err()); |
| 471 | assert_eq!( |
| 472 | compute_merkle_root(&[block.block_hash.clone(), sha(b"leaf-2"), sha(b"leaf-3")]), |
| 473 | EXPECTED_MERKLE_ROOT |
| 474 | ); |
| 475 | } |
| 476 | |
| 477 | // ------------------------------------------------------------------ pin generator |
| 478 | |
| 479 | /// Prints every pinned value. Run with `--ignored --nocapture` when a format is deliberately |
| 480 | /// versioned; never to make a failing pin pass. |
| 481 | #[test] |
| 482 | #[ignore] |
| 483 | fn print_pins() { |
| 484 | let w = workload(); |
| 485 | let o = success_observation(); |
| 486 | let (bytes, sealed) = golden_log(); |
| 487 | let judge = judge_key(); |
| 488 | let mut r2 = base_receipt(); |
| 489 | r2.bind_and_sign_v2(v2_binding(), &judge).unwrap(); |
| 490 | let r1 = base_receipt(); |
| 491 | let block = LedgerBlock::new( |
| 492 | 1, |
| 493 | BlockType::Evaluation, |
| 494 | "2026-10-12T00:00:00+00:00".to_string(), |
| 495 | LedgerBlock::GENESIS_PREV_HASH.to_string(), |
| 496 | "{\"candidate\":\"cand-golden\",\"verdict\":\"admitted\"}".to_string(), |
| 497 | vec![sha(b"golden blob")], |
| 498 | ); |
| 499 | println!( |
| 500 | "PIN workload_canonical={}", |
| 501 | String::from_utf8(w.canonical_bytes()).unwrap() |
| 502 | ); |
| 503 | println!("PIN workload_digest={}", w.digest()); |
| 504 | println!( |
| 505 | "PIN request_001_digest={}", |
| 506 | w.requests[0].request_digest(&w.workload_id) |
| 507 | ); |
| 508 | println!( |
| 509 | "PIN adapter_digest={}", |
| 510 | CanaryAdapterManifest::digest_of_text(ADAPTER_TEXT) |
| 511 | ); |
| 512 | println!( |
| 513 | "PIN identity_binding_digest={}", |
| 514 | identity().binding_digest() |
| 515 | ); |
| 516 | println!("PIN observation_id={}", o.observation_id); |
| 517 | println!( |
| 518 | "PIN observation_canonical={}", |
| 519 | String::from_utf8(o.canonical_bytes()).unwrap() |
| 520 | ); |
| 521 | println!("PIN observation_digest={}", o.digest_hex()); |
| 522 | println!( |
| 523 | "PIN log_bytes={}", |
| 524 | serde_json::to_string(&String::from_utf8(bytes).unwrap()).unwrap() |
| 525 | ); |
| 526 | println!("PIN log_digest={}", sealed.log_digest()); |
| 527 | println!("PIN receipt_v2_digest={}", r2.receipt_digest); |
| 528 | println!("PIN receipt_v2_signature={}", r2.signature.unwrap()); |
| 529 | println!( |
| 530 | "PIN receipt_v1_digest={}", |
| 531 | EvaluationReceipt::compute_digest( |
| 532 | &r1.cycle_id, |
| 533 | &r1.candidate_id, |
| 534 | &r1.parent_id, |
| 535 | r1.admitted, |
| 536 | &r1.layer_results |
| 537 | ) |
| 538 | ); |
| 539 | { |
| 540 | use aien_evaluation_protocol::{ |
| 541 | EvaluationReceipt as ProtocolReceipt, SoftwareP256Signer, Verdict, VerifierIdentity, |
| 542 | VerifierSigner, |
| 543 | }; |
| 544 | use aien_protocol_types::{Digest32, EvaluationId, Timestamp}; |
| 545 | let signer = SoftwareP256Signer::new(SigningKey::from_slice(&[0x42u8; 32]).unwrap()); |
| 546 | let receipt = ProtocolReceipt { |
| 547 | protocol_version: 1, |
| 548 | evaluation_id: EvaluationId(uuid::Uuid::from_u128( |
| 549 | 0x0123_4567_89ab_cdef_0123_4567_89ab_cdef, |
| 550 | )), |
| 551 | request_digest: Digest32([0x11; 32]), |
| 552 | subject_digest: Digest32([0x22; 32]), |
| 553 | plan_digest: Digest32([0x33; 32]), |
| 554 | evidence_root: Digest32([0x44; 32]), |
| 555 | outcomes_root: Digest32([0x55; 32]), |
| 556 | verdict: Verdict::Pass, |
| 557 | verifier: VerifierIdentity { |
| 558 | principal_id: "rsi-golden".to_string(), |
| 559 | key_id: signer.key_fingerprint(), |
| 560 | trust_epoch: 1, |
| 561 | trusted_build_digest: Digest32([0x66; 32]), |
| 562 | policy_bundle_digest: Digest32([0x77; 32]), |
| 563 | }, |
| 564 | started_at: Timestamp(1_700_000_000_000), |
| 565 | completed_at: Timestamp(1_700_000_000_500), |
| 566 | }; |
| 567 | let signed = receipt.sign_with_signer(&signer).unwrap(); |
| 568 | println!("PIN protocol_key_fingerprint={}", signer.key_fingerprint()); |
| 569 | println!( |
| 570 | "PIN protocol_receipt_digest={}", |
| 571 | hex::encode(receipt.compute_receipt_digest().0) |
| 572 | ); |
| 573 | println!( |
| 574 | "PIN protocol_receipt_signature={}", |
| 575 | hex::encode(&signed.signature) |
| 576 | ); |
| 577 | } |
| 578 | println!("PIN ledger_payload_digest={}", block.payload_digest); |
| 579 | println!("PIN ledger_block_hash={}", block.block_hash); |
| 580 | println!( |
| 581 | "PIN merkle_root={}", |
| 582 | compute_merkle_root(&[block.block_hash.clone(), sha(b"leaf-2"), sha(b"leaf-3")]) |
| 583 | ); |
| 584 | } |