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-sovereign-core-mv/blob/main/crates/aien-proof/src/ledger_bind.rs # aien-sovereign-core-mv AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination. 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-sovereign-core-mv / crates / aien-proof / src / ledger_bind.rs` 113 lines ยท 4088 bytes Raw Blame History 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 //! Crumb ledger cross checks for evidence receipts. 17 //! 18 //! The ledger remains the append only operational audit stream. Receipts 19 //! reference relevant ledger event IDs and digests instead of duplicating 20 //! the ledger, and verification confirms the chain, the referenced event, 21 //! and the output hash match. 22 23 use std::path::Path; 24 25 use crate::evidence::Receipt; 26 use crate::ledger; 27 28 /// Check a receipt's ledger reference. Returns `Ok(None)` when the receipt 29 /// carries no reference, `Ok(Some(problem))` with a fail closed reason when 30 /// the reference does not check out. 31 pub fn check_ledger_ref(receipt: &Receipt, store: &Path) -> Result, String> { 32 let reference = match &receipt.ledger { 33 Some(r) => r, 34 None => return Ok(None), 35 }; 36 let path = store.join(ledger::LEDGER_FILE); 37 ledger::verify(&path).map_err(|e| format!("ledger chain broken: {e}"))?; 38 let event = ledger::read_event(&path, reference.index)?.ok_or_else(|| { 39 format!( 40 "receipt references ledger event {} which does not exist", 41 reference.index 42 ) 43 })?; 44 if ledger::hex(&event.hash) != reference.hash.to_ascii_lowercase() { 45 return Ok(Some(format!( 46 "receipt ledger hash does not match event {}", 47 reference.index 48 ))); 49 } 50 if ledger::hex(&event.payload_hash) != receipt.output_digest.to_ascii_lowercase() { 51 return Ok(Some( 52 "receipt output digest does not match the ledger payload hash".to_string(), 53 )); 54 } 55 Ok(None) 56 } 57 58 #[cfg(test)] 59 mod tests { 60 use super::*; 61 use crate::evidence::fixtures::{sample, sealed}; 62 use crate::evidence::{LedgerRef, Tier, Verdict}; 63 64 #[test] 65 fn matching_ledger_ref_passes() { 66 let store = crate::testutil::temp_dir("ledgerbind-ok"); 67 let output = b"seed output"; 68 let event = ledger::append(&store, "tester", "seed-0b", "pass exit=0", output).unwrap(); 69 let mut r = sample(); 70 r.output_digest = ledger::hex(&event.payload_hash); 71 r.ledger = Some(LedgerRef { 72 index: event.index, 73 hash: ledger::hex(&event.hash), 74 }); 75 let r = sealed(r); 76 assert_eq!(check_ledger_ref(&r, &store).unwrap(), None); 77 } 78 79 #[test] 80 fn output_digest_mismatch_fails_closed() { 81 let store = crate::testutil::temp_dir("ledgerbind-digest"); 82 let event = ledger::append(&store, "tester", "seed-0b", "pass exit=0", b"real").unwrap(); 83 let mut r = sample(); 84 r.output_digest = "d".repeat(64); 85 r.ledger = Some(LedgerRef { 86 index: event.index, 87 hash: ledger::hex(&event.hash), 88 }); 89 let r = sealed(r); 90 assert!(check_ledger_ref(&r, &store).unwrap().is_some()); 91 } 92 93 #[test] 94 fn missing_event_fails_closed() { 95 let store = crate::testutil::temp_dir("ledgerbind-missing"); 96 ledger::append(&store, "tester", "seed-0b", "pass exit=0", b"real").unwrap(); 97 let mut r = sample(); 98 r.ledger = Some(LedgerRef { 99 index: 7, 100 hash: "e".repeat(64), 101 }); 102 let r = sealed(r); 103 assert!(check_ledger_ref(&r, &store) 104 .unwrap_err() 105 .contains("does not exist")); 106 } 107 108 #[test] 109 fn tier_and_verdict_reexports_used() { 110 assert_eq!(Tier::Qemu.as_str(), "QEMU"); 111 assert_eq!(Verdict::Pass.as_str(), "PASS"); 112 } 113 }