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
aien-sovereign-core-mv / crates / crumbs / src / ledger.rs
| 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 | //! The Crumbline ledger: a mechanically checkable provenance chain. |
| 17 | //! |
| 18 | //! Every record binds the generator instance digest, the sealed record digest, |
| 19 | //! the verifier digest, the learner-visible digest, the learner build digest, |
| 20 | //! the evaluation receipts, the trace stream digest and the previous root. The |
| 21 | //! claim "every training record is firsthand" is then a property anyone can |
| 22 | //! re-verify from the ledger file, not a statement of doctrine. |
| 23 | //! |
| 24 | //! Lanes are separate chains. The clean-room lane refuses any record whose |
| 25 | //! provenance violates the clean-room policy. |
| 26 | |
| 27 | use crate::canon::Enc; |
| 28 | use crate::digest::{digest, CrumbOccurrenceId, Digest, Domain}; |
| 29 | use crate::provenance::{clean_room_check, CleanRoomViolation, Lane, Provenance}; |
| 30 | use crate::sealed::Population; |
| 31 | use serde::{Deserialize, Serialize}; |
| 32 | |
| 33 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 34 | pub struct CrumblineRecord { |
| 35 | pub seq: u64, |
| 36 | pub lane: Lane, |
| 37 | pub occurrence: CrumbOccurrenceId, |
| 38 | pub condition: String, |
| 39 | pub population: Population, |
| 40 | pub crumb_digest: Digest, |
| 41 | pub generator_instance_digest: Digest, |
| 42 | pub sealed_digest: Digest, |
| 43 | pub verifier_digest: Digest, |
| 44 | pub learner_digest: Digest, |
| 45 | pub evaluations: Vec<Digest>, |
| 46 | pub trace_stream_digest: Digest, |
| 47 | pub promotions: Vec<Digest>, |
| 48 | pub prev_root: Digest, |
| 49 | pub root: Digest, |
| 50 | } |
| 51 | |
| 52 | impl CrumblineRecord { |
| 53 | fn body(&self) -> Vec<u8> { |
| 54 | let mut e = Enc::new(); |
| 55 | e.bytes(b"CLR1") |
| 56 | .u64(self.seq) |
| 57 | .u8(self.lane as u8) |
| 58 | .bytes(self.occurrence.0.as_bytes()); |
| 59 | e.str(&self.condition).u8(self.population as u8); |
| 60 | for d in [ |
| 61 | &self.crumb_digest, |
| 62 | &self.generator_instance_digest, |
| 63 | &self.sealed_digest, |
| 64 | &self.verifier_digest, |
| 65 | &self.learner_digest, |
| 66 | ] { |
| 67 | e.digest(d); |
| 68 | } |
| 69 | e.u32(self.evaluations.len() as u32); |
| 70 | for d in &self.evaluations { |
| 71 | e.digest(d); |
| 72 | } |
| 73 | e.digest(&self.trace_stream_digest); |
| 74 | e.u32(self.promotions.len() as u32); |
| 75 | for d in &self.promotions { |
| 76 | e.digest(d); |
| 77 | } |
| 78 | e.digest(&self.prev_root); |
| 79 | e.finish() |
| 80 | } |
| 81 | |
| 82 | pub fn compute_root(&self) -> Digest { |
| 83 | digest(Domain::LedgerRecord, &self.body()) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | #[derive(Debug, PartialEq, Eq)] |
| 88 | pub enum LedgerError { |
| 89 | CleanRoom(CleanRoomViolation), |
| 90 | Chain, |
| 91 | } |
| 92 | |
| 93 | pub struct Ledger { |
| 94 | pub lane: Lane, |
| 95 | pub genesis: Digest, |
| 96 | pub head: Digest, |
| 97 | pub records: Vec<CrumblineRecord>, |
| 98 | } |
| 99 | |
| 100 | pub fn genesis(lane: Lane, run_id: &str) -> Digest { |
| 101 | let mut e = Enc::new(); |
| 102 | e.u8(lane as u8).str(run_id); |
| 103 | digest(Domain::LedgerGenesis, &e.finish()) |
| 104 | } |
| 105 | |
| 106 | impl Ledger { |
| 107 | pub fn new(lane: Lane, run_id: &str) -> Self { |
| 108 | let g = genesis(lane, run_id); |
| 109 | Ledger { |
| 110 | lane, |
| 111 | genesis: g, |
| 112 | head: g, |
| 113 | records: vec![], |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /// Append a record. The clean-room lane checks provenance first. |
| 118 | pub fn append( |
| 119 | &mut self, |
| 120 | mut rec: CrumblineRecord, |
| 121 | provenance: &Provenance, |
| 122 | ) -> Result<Digest, LedgerError> { |
| 123 | if self.lane == Lane::CleanRoom { |
| 124 | clean_room_check(provenance).map_err(LedgerError::CleanRoom)?; |
| 125 | } |
| 126 | rec.lane = self.lane; |
| 127 | rec.prev_root = self.head; |
| 128 | rec.root = rec.compute_root(); |
| 129 | self.head = rec.root; |
| 130 | self.records.push(rec); |
| 131 | Ok(self.head) |
| 132 | } |
| 133 | |
| 134 | /// Re-verify the whole chain from genesis. |
| 135 | pub fn verify(genesis: Digest, records: &[CrumblineRecord]) -> Result<Digest, LedgerError> { |
| 136 | let mut head = genesis; |
| 137 | for r in records { |
| 138 | if r.prev_root != head || r.compute_root() != r.root { |
| 139 | return Err(LedgerError::Chain); |
| 140 | } |
| 141 | head = r.root; |
| 142 | } |
| 143 | Ok(head) |
| 144 | } |
| 145 | |
| 146 | pub fn to_jsonl(&self) -> String { |
| 147 | let mut s = String::new(); |
| 148 | for r in &self.records { |
| 149 | s.push_str(&serde_json::to_string(r).expect("serializable")); |
| 150 | s.push('\n'); |
| 151 | } |
| 152 | s |
| 153 | } |
| 154 | } |