Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-protocols-mv/blob/main/crates/aien-evaluation-protocol/src/adaptive.rs # aien-protocols-mv Canonical specifications, wire protocols, and schemas for the AIEN sovereign ecosystem Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-protocols-mv / crates / aien-evaluation-protocol / src / adaptive.rs` 214 lines ยท 7996 bytes Raw Blame History 1 //! AEI1 extension receipt (specs/evidence/ADAPTIVE_EVIDENCE_INTERFACE.md section 2). 2 //! Wraps `EvaluationReceipt` unchanged; the added fields enter a new signed digest. 3 4 use crate::{ 5 compute_merkle_root, verifying_key_fingerprint, CryptographicTier, EvaluationError, 6 EvaluationReceipt, VerifierSigner, 7 }; 8 use aien_protocol_types::{Digest32, EvaluationId}; 9 use p256::ecdsa::signature::Verifier; 10 use p256::ecdsa::{Signature, VerifyingKey}; 11 use serde::{Deserialize, Serialize}; 12 use sha2::{Digest, Sha256}; 13 use std::collections::{HashMap, HashSet}; 14 15 /// `EvaluationReceipt.protocol_version` required for an AEI1 receipt (AEI1 2.1: bump so old verifiers refuse). 16 pub const AEI1_PROTOCOL_VERSION: u32 = crate::FRAMED_RECEIPT_PROTOCOL_VERSION; 17 18 #[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)] 19 pub enum EvidenceClass { 20 Production, 21 Fixture, 22 } 23 24 #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] 25 pub struct AdaptiveEvidenceReceipt { 26 pub base: EvaluationReceipt, 27 pub world_id: Digest32, 28 pub cortex_session: u64, 29 pub generation: u64, 30 pub execution_id: [u8; 32], 31 pub transcript_digest: Digest32, 32 pub cortex_chain_head: Digest32, 33 pub producer_subsystem: String, 34 pub proposer: String, 35 /// Key fingerprint of the proposer; `proposer` alone is a name, so N-self (b) needs this. 36 pub proposer_key_fingerprint: String, 37 pub evidence_class: EvidenceClass, 38 } 39 40 fn put_str(h: &mut Sha256, s: &str) { 41 h.update((s.len() as u64).to_le_bytes()); 42 h.update(s.as_bytes()); 43 } 44 45 impl AdaptiveEvidenceReceipt { 46 pub fn compute_digest(&self) -> Digest32 { 47 let mut h = Sha256::new(); 48 h.update(b"AEI1\0"); 49 h.update(self.base.compute_receipt_digest().0); 50 h.update(self.world_id.0); 51 h.update(self.cortex_session.to_le_bytes()); 52 h.update(self.generation.to_le_bytes()); 53 h.update(self.execution_id); 54 h.update(self.transcript_digest.0); 55 h.update(self.cortex_chain_head.0); 56 put_str(&mut h, &self.producer_subsystem); 57 put_str(&mut h, &self.proposer); 58 put_str(&mut h, &self.proposer_key_fingerprint); 59 h.update([match self.evidence_class { 60 EvidenceClass::Production => 0u8, 61 EvidenceClass::Fixture => 1u8, 62 }]); 63 Digest32(h.finalize().into()) 64 } 65 66 pub fn sign_with_signer( 67 &self, 68 signer: &dyn VerifierSigner, 69 ) -> Result { 70 let fingerprint = signer.key_fingerprint(); 71 if self.base.verifier.key_id != fingerprint { 72 return Err(EvaluationError::KeyIdentityMismatch { 73 expected: self.base.verifier.key_id.clone(), 74 actual: fingerprint, 75 }); 76 } 77 Ok(SignedAdaptiveEvidenceReceipt { 78 evidence: self.clone(), 79 signature: signer.sign_digest(&self.compute_digest().0)?, 80 cryptographic_tier: signer.cryptographic_tier(), 81 key_fingerprint: fingerprint, 82 }) 83 } 84 85 /// N2/N3 at receipt level: the evaluator outcome digests, in order, must reproduce `outcomes_root`. 86 pub fn outcomes_match(&self, outcome_digests: &[Digest32]) -> bool { 87 compute_merkle_root(outcome_digests) == self.base.outcomes_root 88 } 89 } 90 91 #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] 92 pub struct SignedAdaptiveEvidenceReceipt { 93 pub evidence: AdaptiveEvidenceReceipt, 94 pub signature: Vec, 95 pub cryptographic_tier: CryptographicTier, 96 pub key_fingerprint: String, 97 } 98 99 /// What the consumer already trusts or is about to admit. Never read from the receipt. 100 pub struct AdmissionContext<'a> { 101 pub trusted_key: &'a VerifyingKey, 102 pub expected_tier: CryptographicTier, 103 pub admitted_subject: Digest32, 104 pub expected_policy_digest: Digest32, 105 pub expected_execution_id: [u8; 32], 106 pub expected_transcript_digest: Digest32, 107 pub promoter_key_fingerprint: &'a str, 108 pub promoter_subsystem: &'a str, 109 } 110 111 /// Replay memory: consumed evaluation ids and which candidate each execution id was used for. 112 #[derive(Default)] 113 pub struct ReplayLedger { 114 evaluations: HashSet, 115 executions: HashMap<[u8; 32], Digest32>, 116 } 117 118 #[derive(thiserror::Error, Debug, Eq, PartialEq)] 119 pub enum AdaptiveEvidenceError { 120 #[error("unsigned receipt")] 121 Unsigned, 122 #[error("signature, key or tier check failed: {0}")] 123 Signature(String), 124 #[error("protocol version {0} is not the AEI1 version")] 125 VersionMismatch(u32), 126 #[error("receipt subject does not match the admitted candidate")] 127 SubjectMismatch, 128 #[error("policy digest mismatch")] 129 PolicyMismatch, 130 #[error("execution_id or transcript_digest does not match the transcript")] 131 TranscriptJoinMismatch, 132 #[error("evidence class is not Production")] 133 NotProduction, 134 #[error("world_id is zero")] 135 MissingWorld, 136 #[error("self-approval: {0}")] 137 SelfApproval(&'static str), 138 #[error("replay: {0}")] 139 Replay(&'static str), 140 } 141 142 impl SignedAdaptiveEvidenceReceipt { 143 /// AEI1 2.2 steps 1 to 4, 7, 8 plus replay. Refuses at the first failure; consumes the 144 /// ledger entry only on success. 145 pub fn verify_strict( 146 &self, 147 ctx: &AdmissionContext<'_>, 148 ledger: &mut ReplayLedger, 149 ) -> Result<(), AdaptiveEvidenceError> { 150 use AdaptiveEvidenceError as E; 151 let ev = &self.evidence; 152 if self.signature.is_empty() { 153 return Err(E::Unsigned); 154 } 155 if ev.base.protocol_version != AEI1_PROTOCOL_VERSION { 156 return Err(E::VersionMismatch(ev.base.protocol_version)); 157 } 158 if self.cryptographic_tier != ctx.expected_tier { 159 return Err(E::Signature("tier mismatch".into())); 160 } 161 let fp = verifying_key_fingerprint(ctx.trusted_key); 162 if self.key_fingerprint != fp || ev.base.verifier.key_id != fp { 163 return Err(E::Signature("key fingerprint mismatch".into())); 164 } 165 let sig = 166 Signature::from_slice(&self.signature).map_err(|e| E::Signature(e.to_string()))?; 167 ctx.trusted_key 168 .verify(&ev.compute_digest().0, &sig) 169 .map_err(|e| E::Signature(e.to_string()))?; 170 if ev.base.subject_digest != ctx.admitted_subject { 171 return Err(E::SubjectMismatch); 172 } 173 if ev.base.verifier.policy_bundle_digest != ctx.expected_policy_digest { 174 return Err(E::PolicyMismatch); 175 } 176 if ev.execution_id != ctx.expected_execution_id 177 || ev.transcript_digest != ctx.expected_transcript_digest 178 { 179 return Err(E::TranscriptJoinMismatch); 180 } 181 if ev.world_id == Digest32::ZERO { 182 return Err(E::MissingWorld); 183 } 184 if ev.evidence_class != EvidenceClass::Production { 185 return Err(E::NotProduction); 186 } 187 if ctx.promoter_key_fingerprint == self.key_fingerprint { 188 return Err(E::SelfApproval("promoter key is the producer key")); 189 } 190 if ctx.promoter_key_fingerprint == ev.proposer_key_fingerprint { 191 return Err(E::SelfApproval("promoter key is the proposer key")); 192 } 193 if ctx.promoter_subsystem == ev.producer_subsystem { 194 return Err(E::SelfApproval( 195 "promoter subsystem is the producer subsystem", 196 )); 197 } 198 if ledger.evaluations.contains(&ev.base.evaluation_id) { 199 return Err(E::Replay("evaluation_id already consumed")); 200 } 201 if ledger 202 .executions 203 .get(&ev.execution_id) 204 .is_some_and(|s| *s != ev.base.subject_digest) 205 { 206 return Err(E::Replay("execution_id already used for another candidate")); 207 } 208 ledger.evaluations.insert(ev.base.evaluation_id); 209 ledger 210 .executions 211 .insert(ev.execution_id, ev.base.subject_digest); 212 Ok(()) 213 } 214 }