aien-protocols-mv
Canonical specifications, wire protocols, and schemas for the AIEN sovereign ecosystem
Brought by drakestapleton
aien-protocols-mv / crates / aien-evaluation-protocol / tests / aei1_negative_matrix_tests.rs
| 1 | //! AEI1 section 6 negative matrix, rows testable at receipt level. |
| 2 | |
| 3 | use aien_evaluation_protocol::{ |
| 4 | compute_merkle_root, AdaptiveEvidenceError as E, AdaptiveEvidenceReceipt, AdmissionContext, |
| 5 | CryptographicTier, EvaluationReceipt, EvidenceClass, ReplayLedger, |
| 6 | SignedAdaptiveEvidenceReceipt, SoftwareP256Signer, Verdict, VerifierIdentity, VerifierSigner, |
| 7 | AEI1_PROTOCOL_VERSION, |
| 8 | }; |
| 9 | use aien_protocol_types::{Digest32, EvaluationId, Timestamp}; |
| 10 | use p256::ecdsa::SigningKey; |
| 11 | |
| 12 | const TIER2: CryptographicTier = CryptographicTier::Tier2SoftwareKey; |
| 13 | |
| 14 | fn signer(seed: u8) -> SoftwareP256Signer { |
| 15 | SoftwareP256Signer::new(SigningKey::from_slice(&[seed; 32]).expect("valid p256 key")) |
| 16 | } |
| 17 | |
| 18 | fn outcomes() -> Vec<Digest32> { |
| 19 | vec![Digest32([10; 32]), Digest32([11; 32]), Digest32([12; 32])] |
| 20 | } |
| 21 | |
| 22 | fn body(s: &SoftwareP256Signer) -> AdaptiveEvidenceReceipt { |
| 23 | AdaptiveEvidenceReceipt { |
| 24 | base: EvaluationReceipt { |
| 25 | protocol_version: AEI1_PROTOCOL_VERSION, |
| 26 | evaluation_id: EvaluationId::new_v4(), |
| 27 | request_digest: Digest32([1; 32]), |
| 28 | subject_digest: Digest32([2; 32]), |
| 29 | plan_digest: Digest32([3; 32]), |
| 30 | evidence_root: Digest32([4; 32]), |
| 31 | outcomes_root: compute_merkle_root(&outcomes()), |
| 32 | verdict: Verdict::Pass, |
| 33 | verifier: VerifierIdentity { |
| 34 | principal_id: "judge".into(), |
| 35 | key_id: s.key_fingerprint(), |
| 36 | trust_epoch: 1, |
| 37 | trusted_build_digest: Digest32([6; 32]), |
| 38 | policy_bundle_digest: Digest32([7; 32]), |
| 39 | }, |
| 40 | started_at: Timestamp(1000), |
| 41 | completed_at: Timestamp(1050), |
| 42 | }, |
| 43 | world_id: Digest32([20; 32]), |
| 44 | cortex_session: 5, |
| 45 | generation: 9, |
| 46 | execution_id: [21; 32], |
| 47 | transcript_digest: Digest32([22; 32]), |
| 48 | cortex_chain_head: Digest32([23; 32]), |
| 49 | producer_subsystem: "omega-living-body".into(), |
| 50 | proposer: "proposer-agent".into(), |
| 51 | proposer_key_fingerprint: signer(77).key_fingerprint(), |
| 52 | evidence_class: EvidenceClass::Production, |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | struct Fixture { |
| 57 | judge: SoftwareP256Signer, |
| 58 | promoter: SoftwareP256Signer, |
| 59 | } |
| 60 | |
| 61 | impl Fixture { |
| 62 | fn new() -> Self { |
| 63 | Self { |
| 64 | judge: signer(42), |
| 65 | promoter: signer(99), |
| 66 | } |
| 67 | } |
| 68 | fn receipt(&self) -> SignedAdaptiveEvidenceReceipt { |
| 69 | body(&self.judge).sign_with_signer(&self.judge).unwrap() |
| 70 | } |
| 71 | fn check(&self, r: &SignedAdaptiveEvidenceReceipt, ledger: &mut ReplayLedger) -> Result<(), E> { |
| 72 | self.check_as( |
| 73 | r, |
| 74 | ledger, |
| 75 | &self.promoter.key_fingerprint(), |
| 76 | "promoter-subsystem", |
| 77 | ) |
| 78 | } |
| 79 | fn check_as( |
| 80 | &self, |
| 81 | r: &SignedAdaptiveEvidenceReceipt, |
| 82 | ledger: &mut ReplayLedger, |
| 83 | promoter_fp: &str, |
| 84 | promoter_sub: &str, |
| 85 | ) -> Result<(), E> { |
| 86 | let vk = self.judge.verifying_key(); |
| 87 | r.verify_strict( |
| 88 | &AdmissionContext { |
| 89 | trusted_key: &vk, |
| 90 | expected_tier: TIER2, |
| 91 | admitted_subject: Digest32([2; 32]), |
| 92 | expected_policy_digest: Digest32([7; 32]), |
| 93 | expected_execution_id: [21; 32], |
| 94 | expected_transcript_digest: Digest32([22; 32]), |
| 95 | promoter_key_fingerprint: promoter_fp, |
| 96 | promoter_subsystem: promoter_sub, |
| 97 | }, |
| 98 | ledger, |
| 99 | ) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | #[test] |
| 104 | fn genuine_receipt_is_admitted_once() { |
| 105 | let f = Fixture::new(); |
| 106 | assert_eq!(f.check(&f.receipt(), &mut ReplayLedger::default()), Ok(())); |
| 107 | } |
| 108 | |
| 109 | #[test] |
| 110 | fn n1_every_added_field_is_covered_by_the_signature() { |
| 111 | let f = Fixture::new(); |
| 112 | type Mutator = Box<dyn Fn(&mut AdaptiveEvidenceReceipt)>; |
| 113 | let muts: Vec<(&str, Mutator)> = vec![ |
| 114 | ("world_id", Box::new(|e| e.world_id = Digest32([1; 32]))), |
| 115 | ("cortex_session", Box::new(|e| e.cortex_session += 1)), |
| 116 | ("generation", Box::new(|e| e.generation += 1)), |
| 117 | ("execution_id", Box::new(|e| e.execution_id = [0; 32])), |
| 118 | ( |
| 119 | "transcript_digest", |
| 120 | Box::new(|e| e.transcript_digest = Digest32([0; 32])), |
| 121 | ), |
| 122 | ( |
| 123 | "cortex_chain_head", |
| 124 | Box::new(|e| e.cortex_chain_head = Digest32([0; 32])), |
| 125 | ), |
| 126 | ( |
| 127 | "producer_subsystem", |
| 128 | Box::new(|e| e.producer_subsystem.push('x')), |
| 129 | ), |
| 130 | ("proposer", Box::new(|e| e.proposer.push('x'))), |
| 131 | ( |
| 132 | "proposer_fp", |
| 133 | Box::new(|e| e.proposer_key_fingerprint.push('x')), |
| 134 | ), |
| 135 | ("base.verdict", Box::new(|e| e.base.verdict = Verdict::Fail)), |
| 136 | ( |
| 137 | "base.completed_at", |
| 138 | Box::new(|e| e.base.completed_at = Timestamp(1)), |
| 139 | ), |
| 140 | ]; |
| 141 | for (name, m) in muts { |
| 142 | let mut r = f.receipt(); |
| 143 | m(&mut r.evidence); |
| 144 | assert!( |
| 145 | matches!( |
| 146 | f.check(&r, &mut ReplayLedger::default()), |
| 147 | Err(E::Signature(_)) |
| 148 | ), |
| 149 | "field {name} not covered" |
| 150 | ); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | #[test] |
| 155 | fn n2_missing_outcome_record_changes_outcomes_root() { |
| 156 | let f = Fixture::new(); |
| 157 | let r = f.receipt(); |
| 158 | assert!(r.evidence.outcomes_match(&outcomes())); |
| 159 | assert!(!r.evidence.outcomes_match(&outcomes()[..2])); |
| 160 | } |
| 161 | |
| 162 | #[test] |
| 163 | fn n3_reordered_outcome_records_change_outcomes_root() { |
| 164 | let f = Fixture::new(); |
| 165 | let mut swapped = outcomes(); |
| 166 | swapped.swap(0, 1); |
| 167 | assert!(!f.receipt().evidence.outcomes_match(&swapped)); |
| 168 | } |
| 169 | |
| 170 | #[test] |
| 171 | fn n4_altered_policy_digest_is_refused() { |
| 172 | let f = Fixture::new(); |
| 173 | let mut e = body(&f.judge); |
| 174 | e.base.verifier.policy_bundle_digest = Digest32([8; 32]); |
| 175 | let r = e.sign_with_signer(&f.judge).unwrap(); // validly signed, wrong policy |
| 176 | assert_eq!( |
| 177 | f.check(&r, &mut ReplayLedger::default()), |
| 178 | Err(E::PolicyMismatch) |
| 179 | ); |
| 180 | } |
| 181 | |
| 182 | #[test] |
| 183 | fn n5_same_evaluation_id_twice_is_refused() { |
| 184 | let f = Fixture::new(); |
| 185 | let r = f.receipt(); |
| 186 | let mut ledger = ReplayLedger::default(); |
| 187 | assert_eq!(f.check(&r, &mut ledger), Ok(())); |
| 188 | assert!(matches!(f.check(&r, &mut ledger), Err(E::Replay(_)))); |
| 189 | } |
| 190 | |
| 191 | #[test] |
| 192 | fn n5_execution_id_reused_for_another_candidate_is_refused() { |
| 193 | let f = Fixture::new(); |
| 194 | let mut ledger = ReplayLedger::default(); |
| 195 | assert_eq!(f.check(&f.receipt(), &mut ledger), Ok(())); |
| 196 | // Different evaluation, same execution_id, different candidate (consumer admits that candidate). |
| 197 | let mut e = body(&f.judge); |
| 198 | e.base.subject_digest = Digest32([3; 32]); |
| 199 | let r = e.sign_with_signer(&f.judge).unwrap(); |
| 200 | let vk = f.judge.verifying_key(); |
| 201 | let res = r.verify_strict( |
| 202 | &AdmissionContext { |
| 203 | trusted_key: &vk, |
| 204 | expected_tier: TIER2, |
| 205 | admitted_subject: Digest32([3; 32]), |
| 206 | expected_policy_digest: Digest32([7; 32]), |
| 207 | expected_execution_id: [21; 32], |
| 208 | expected_transcript_digest: Digest32([22; 32]), |
| 209 | promoter_key_fingerprint: &f.promoter.key_fingerprint(), |
| 210 | promoter_subsystem: "promoter-subsystem", |
| 211 | }, |
| 212 | &mut ledger, |
| 213 | ); |
| 214 | assert!(matches!(res, Err(E::Replay(_)))); |
| 215 | } |
| 216 | |
| 217 | #[test] |
| 218 | fn n5_receipt_reused_for_another_candidate_is_refused() { |
| 219 | let f = Fixture::new(); |
| 220 | let mut r = f.receipt(); |
| 221 | r.evidence.base.subject_digest = Digest32([3; 32]); // retarget: breaks signature |
| 222 | assert!(matches!( |
| 223 | f.check(&r, &mut ReplayLedger::default()), |
| 224 | Err(E::Signature(_)) |
| 225 | )); |
| 226 | } |
| 227 | |
| 228 | #[test] |
| 229 | fn n6_unsigned_receipt_is_refused() { |
| 230 | let f = Fixture::new(); |
| 231 | let mut r = f.receipt(); |
| 232 | r.signature.clear(); |
| 233 | assert_eq!(f.check(&r, &mut ReplayLedger::default()), Err(E::Unsigned)); |
| 234 | } |
| 235 | |
| 236 | #[test] |
| 237 | fn n7_subject_digest_differing_from_admitted_bytes_is_refused() { |
| 238 | let f = Fixture::new(); |
| 239 | let mut e = body(&f.judge); |
| 240 | e.base.subject_digest = Digest32([9; 32]); |
| 241 | let r = e.sign_with_signer(&f.judge).unwrap(); |
| 242 | assert_eq!( |
| 243 | f.check(&r, &mut ReplayLedger::default()), |
| 244 | Err(E::SubjectMismatch) |
| 245 | ); |
| 246 | } |
| 247 | |
| 248 | #[test] |
| 249 | fn n_self_a_promoter_key_equals_producer_key() { |
| 250 | let f = Fixture::new(); |
| 251 | let fp = f.judge.key_fingerprint(); |
| 252 | let res = f.check_as( |
| 253 | &f.receipt(), |
| 254 | &mut ReplayLedger::default(), |
| 255 | &fp, |
| 256 | "promoter-subsystem", |
| 257 | ); |
| 258 | assert!(matches!(res, Err(E::SelfApproval(_)))); |
| 259 | } |
| 260 | |
| 261 | #[test] |
| 262 | fn n_self_b_promoter_key_equals_proposer_key() { |
| 263 | let f = Fixture::new(); |
| 264 | let fp = signer(77).key_fingerprint(); |
| 265 | let res = f.check_as( |
| 266 | &f.receipt(), |
| 267 | &mut ReplayLedger::default(), |
| 268 | &fp, |
| 269 | "promoter-subsystem", |
| 270 | ); |
| 271 | assert!(matches!(res, Err(E::SelfApproval(_)))); |
| 272 | } |
| 273 | |
| 274 | #[test] |
| 275 | fn n_self_c_promoter_subsystem_equals_producer_subsystem() { |
| 276 | let f = Fixture::new(); |
| 277 | let fp = f.promoter.key_fingerprint(); |
| 278 | let res = f.check_as( |
| 279 | &f.receipt(), |
| 280 | &mut ReplayLedger::default(), |
| 281 | &fp, |
| 282 | "omega-living-body", |
| 283 | ); |
| 284 | assert!(matches!(res, Err(E::SelfApproval(_)))); |
| 285 | } |
| 286 | |
| 287 | #[test] |
| 288 | fn n9_flipped_evidence_class_breaks_signature_and_fixture_is_not_production() { |
| 289 | let f = Fixture::new(); |
| 290 | let mut r = f.receipt(); |
| 291 | r.evidence.evidence_class = EvidenceClass::Fixture; |
| 292 | assert!(matches!( |
| 293 | f.check(&r, &mut ReplayLedger::default()), |
| 294 | Err(E::Signature(_)) |
| 295 | )); |
| 296 | let mut e = body(&f.judge); |
| 297 | e.evidence_class = EvidenceClass::Fixture; |
| 298 | let r = e.sign_with_signer(&f.judge).unwrap(); |
| 299 | assert_eq!( |
| 300 | f.check(&r, &mut ReplayLedger::default()), |
| 301 | Err(E::NotProduction) |
| 302 | ); |
| 303 | } |
| 304 | |
| 305 | #[test] |
| 306 | fn n10_claimed_tier_mismatch_is_refused() { |
| 307 | let f = Fixture::new(); |
| 308 | let mut r = f.receipt(); |
| 309 | r.cryptographic_tier = CryptographicTier::Tier4HardwareTpm; |
| 310 | assert!(matches!( |
| 311 | f.check(&r, &mut ReplayLedger::default()), |
| 312 | Err(E::Signature(_)) |
| 313 | )); |
| 314 | } |
| 315 | |
| 316 | #[test] |
| 317 | fn n11_transcript_join_mismatch_is_refused() { |
| 318 | let f = Fixture::new(); |
| 319 | let mut e = body(&f.judge); |
| 320 | e.transcript_digest = Digest32([0xee; 32]); |
| 321 | let r = e.sign_with_signer(&f.judge).unwrap(); |
| 322 | assert_eq!( |
| 323 | f.check(&r, &mut ReplayLedger::default()), |
| 324 | Err(E::TranscriptJoinMismatch) |
| 325 | ); |
| 326 | } |
| 327 | |
| 328 | #[test] |
| 329 | fn n12_receipt_with_old_protocol_version_is_refused() { |
| 330 | let f = Fixture::new(); |
| 331 | let mut e = body(&f.judge); |
| 332 | e.base.protocol_version = 1; |
| 333 | let r = e.sign_with_signer(&f.judge).unwrap(); |
| 334 | assert_eq!( |
| 335 | f.check(&r, &mut ReplayLedger::default()), |
| 336 | Err(E::VersionMismatch(1)) |
| 337 | ); |
| 338 | } |
| 339 | |
| 340 | #[test] |
| 341 | fn zero_world_id_is_refused() { |
| 342 | let f = Fixture::new(); |
| 343 | let mut e = body(&f.judge); |
| 344 | e.world_id = Digest32::ZERO; |
| 345 | let r = e.sign_with_signer(&f.judge).unwrap(); |
| 346 | assert_eq!( |
| 347 | f.check(&r, &mut ReplayLedger::default()), |
| 348 | Err(E::MissingWorld) |
| 349 | ); |
| 350 | } |
| 351 | |
| 352 | #[test] |
| 353 | #[ignore = "n2_n3_cortex_and_transcript_record_gaps_need_trn1_verifier_and_cx_verify_not_in_this_crate"] |
| 354 | fn n2_n3_cortex_and_transcript_record_gaps_need_trn1_verifier_and_cx_verify_not_in_this_crate() {} |
| 355 | |
| 356 | #[test] |
| 357 | #[ignore = "step6_cortex_chain_head_reachability_is_open_question_q4_no_contract_yet"] |
| 358 | fn step6_cortex_chain_head_reachability_is_open_question_q4_no_contract_yet() {} |
| 359 | |
| 360 | #[test] |
| 361 | #[ignore = "n9_fixture_file_path_loader_and_observation_types_do_not_exist_yet"] |
| 362 | fn n9_fixture_file_path_loader_and_observation_types_do_not_exist_yet() {} |
| 363 | |
| 364 | #[test] |
| 365 | #[ignore = "negative_controls_defective_verifier_variants_need_a_mutation_harness_not_built"] |
| 366 | fn negative_controls_defective_verifier_variants_need_a_mutation_harness_not_built() {} |