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 / digest.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 | //! Domain-separated BLAKE3 digests and the Crumb v1 identity model. |
| 17 | //! |
| 18 | //! | identity | derivation | learner sees it | |
| 19 | //! |---------------------------|---------------------------------------------------------|-----------------| |
| 20 | //! | `CrumbOccurrenceId` | UUID v7, one per presentation/run | no | |
| 21 | //! | `CrumbDigest` | BLAKE3(canonical learner-visible Crumb v1 bytes) | no (derivable) | |
| 22 | //! | `GeneratorInstanceDigest` | BLAKE3(family_id, generator_version, seed, parameters) | no | |
| 23 | //! | `SealedRecordDigest` | BLAKE3(canonical sealed record) | no | |
| 24 | //! |
| 25 | //! Every digest uses a distinct BLAKE3 `derive_key` context, so a byte string |
| 26 | //! hashed for one purpose can never collide with the same bytes hashed for |
| 27 | //! another. |
| 28 | |
| 29 | use std::fmt; |
| 30 | |
| 31 | /// A 32-byte BLAKE3 digest. |
| 32 | #[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)] |
| 33 | pub struct Digest(pub [u8; 32]); |
| 34 | |
| 35 | impl Digest { |
| 36 | pub const ZERO: Digest = Digest([0u8; 32]); |
| 37 | |
| 38 | pub fn hex(&self) -> String { |
| 39 | self.0.iter().map(|b| format!("{b:02x}")).collect() |
| 40 | } |
| 41 | |
| 42 | pub fn short(&self) -> String { |
| 43 | self.hex()[..16].to_string() |
| 44 | } |
| 45 | |
| 46 | pub fn from_hex(s: &str) -> Option<Digest> { |
| 47 | if s.len() != 64 { |
| 48 | return None; |
| 49 | } |
| 50 | let mut out = [0u8; 32]; |
| 51 | for (i, chunk) in s.as_bytes().chunks(2).enumerate() { |
| 52 | let hi = (chunk[0] as char).to_digit(16)?; |
| 53 | let lo = (chunk[1] as char).to_digit(16)?; |
| 54 | out[i] = (hi * 16 + lo) as u8; |
| 55 | } |
| 56 | Some(Digest(out)) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | impl fmt::Debug for Digest { |
| 61 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 62 | write!(f, "{}", self.short()) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | impl fmt::Display for Digest { |
| 67 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 68 | write!(f, "{}", self.hex()) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | impl serde::Serialize for Digest { |
| 73 | fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> { |
| 74 | s.serialize_str(&self.hex()) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | impl<'de> serde::Deserialize<'de> for Digest { |
| 79 | fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> { |
| 80 | let s = String::deserialize(d)?; |
| 81 | Digest::from_hex(&s).ok_or_else(|| serde::de::Error::custom("bad digest")) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /// Every digest purpose in the system. Adding one is a schema change. |
| 86 | #[derive(Clone, Copy, Debug)] |
| 87 | pub enum Domain { |
| 88 | CrumbDigest, |
| 89 | GeneratorInstance, |
| 90 | GeneratorCode, |
| 91 | SealedRecord, |
| 92 | HeldoutSet, |
| 93 | VisibleSet, |
| 94 | Program, |
| 95 | Behavior, |
| 96 | Verifier, |
| 97 | Evaluation, |
| 98 | TraceRecord, |
| 99 | TrainingSample, |
| 100 | TraceStream, |
| 101 | Capability, |
| 102 | Promotion, |
| 103 | LedgerRecord, |
| 104 | LedgerGenesis, |
| 105 | Rng, |
| 106 | Config, |
| 107 | LearnerBank, |
| 108 | SearchState, |
| 109 | } |
| 110 | |
| 111 | impl Domain { |
| 112 | fn context(self) -> &'static str { |
| 113 | match self { |
| 114 | Domain::CrumbDigest => "crumbs v1 2026-09-27 CrumbDigest", |
| 115 | Domain::GeneratorInstance => "crumbs v1 2026-09-27 GeneratorInstanceDigest", |
| 116 | Domain::GeneratorCode => "crumbs v1 2026-09-27 GeneratorCodeDigest", |
| 117 | Domain::SealedRecord => "crumbs v1 2026-09-27 SealedRecordDigest", |
| 118 | Domain::HeldoutSet => "crumbs v1 2026-09-27 HeldoutSetDigest", |
| 119 | Domain::VisibleSet => "crumbs v1 2026-09-27 VisibleSetDigest", |
| 120 | Domain::Program => "crumbs v1 2026-09-27 CandidateProgramDigest", |
| 121 | Domain::Behavior => "crumbs v1 2026-09-27 BehaviorSignature", |
| 122 | Domain::Verifier => "crumbs v1 2026-09-27 VerifierDigest", |
| 123 | Domain::Evaluation => "crumbs v1 2026-09-27 EvaluationDigest", |
| 124 | Domain::TraceRecord => "crumbs v1 2026-09-27 TraceRecordChain", |
| 125 | Domain::TrainingSample => "crumbs v1 2026-09-27 TrainingSampleChain", |
| 126 | Domain::TraceStream => "crumbs v1 2026-09-27 TraceStreamDigest", |
| 127 | Domain::Capability => "crumbs v1 2026-09-27 CapabilityId", |
| 128 | Domain::Promotion => "crumbs v1 2026-09-27 PromotionRecord", |
| 129 | Domain::LedgerRecord => "crumbs v1 2026-09-27 CrumblineRecord", |
| 130 | Domain::LedgerGenesis => "crumbs v1 2026-09-27 CrumblineGenesis", |
| 131 | Domain::Rng => "crumbs v1 2026-09-27 DeterministicStream", |
| 132 | Domain::Config => "crumbs v1 2026-09-27 ConfigDigest", |
| 133 | Domain::LearnerBank => "crumbs v1 2026-09-27 LearnerBankDigest", |
| 134 | Domain::SearchState => "crumbs v1 2026-09-27 SearchStateDigest", |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | pub fn digest(domain: Domain, bytes: &[u8]) -> Digest { |
| 140 | Digest(blake3::derive_key(domain.context(), bytes)) |
| 141 | } |
| 142 | |
| 143 | /// Incremental hashing under a domain. |
| 144 | pub struct Hasher(blake3::Hasher); |
| 145 | |
| 146 | impl Hasher { |
| 147 | pub fn new(domain: Domain) -> Self { |
| 148 | Hasher(blake3::Hasher::new_derive_key(domain.context())) |
| 149 | } |
| 150 | pub fn update(&mut self, bytes: &[u8]) -> &mut Self { |
| 151 | self.0.update(bytes); |
| 152 | self |
| 153 | } |
| 154 | pub fn finish(&self) -> Digest { |
| 155 | Digest(*self.0.finalize().as_bytes()) |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /// UUID v7 occurrence id: identifies one presentation of a crumb in one run. |
| 160 | /// Time-ordered and deliberately NOT content-derived; it never identifies the problem. |
| 161 | #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, serde::Serialize, serde::Deserialize)] |
| 162 | pub struct CrumbOccurrenceId(pub uuid::Uuid); |
| 163 | |
| 164 | impl CrumbOccurrenceId { |
| 165 | pub fn new() -> Self { |
| 166 | CrumbOccurrenceId(uuid::Uuid::now_v7()) |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | impl Default for CrumbOccurrenceId { |
| 171 | fn default() -> Self { |
| 172 | Self::new() |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /// BLAKE3 of the canonical learner-visible bytes (which include the schema version). |
| 177 | pub type CrumbDigest = Digest; |
| 178 | /// BLAKE3(family_id || generator_version || seed || canonical generation parameters). |
| 179 | pub type GeneratorInstanceDigest = Digest; |
| 180 | /// BLAKE3 of the canonical sealed record (metadata + held-outs + provenance). |
| 181 | pub type SealedRecordDigest = Digest; |