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 / cortex-rs / src / evidence.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 | use chrono::{DateTime, Duration, Utc}; |
| 17 | use sha2::{Digest, Sha256}; |
| 18 | |
| 19 | #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] |
| 20 | #[serde(rename_all = "camelCase")] |
| 21 | pub struct EvidenceRecord { |
| 22 | pub id: String, |
| 23 | pub source_kind: String, |
| 24 | pub source_id: String, |
| 25 | pub commitment: String, |
| 26 | pub merkle_root: Option<String>, |
| 27 | pub segment_id: Option<String>, |
| 28 | pub retention_class: String, |
| 29 | pub retain_until: Option<String>, |
| 30 | pub created_at: String, |
| 31 | } |
| 32 | |
| 33 | pub fn hex_encode(bytes: &[u8]) -> String { |
| 34 | const HEX: &[u8; 16] = b"0123456789abcdef"; |
| 35 | let mut out = String::with_capacity(bytes.len() * 2); |
| 36 | for byte in bytes { |
| 37 | out.push(HEX[(byte >> 4) as usize] as char); |
| 38 | out.push(HEX[(byte & 0x0f) as usize] as char); |
| 39 | } |
| 40 | out |
| 41 | } |
| 42 | |
| 43 | pub fn is_commitment(hash: &str) -> bool { |
| 44 | hash.len() == 64 && hash.bytes().all(|b| b.is_ascii_hexdigit()) && hash != "hash_ref" |
| 45 | } |
| 46 | |
| 47 | pub fn retain_until(class: &str, now: DateTime<Utc>) -> Option<String> { |
| 48 | match class { |
| 49 | "permanent" => None, |
| 50 | "ephemeral" => Some((now + Duration::days(1)).to_rfc3339()), |
| 51 | _ => Some((now + Duration::days(365)).to_rfc3339()), |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | pub fn merkle_root(leaves: &[impl AsRef<[u8]>]) -> String { |
| 56 | if leaves.is_empty() { |
| 57 | return format!("{:x}", Sha256::digest(b"")); |
| 58 | } |
| 59 | let mut level: Vec<[u8; 32]> = leaves |
| 60 | .iter() |
| 61 | .map(|leaf| { |
| 62 | let digest = Sha256::digest(leaf.as_ref()); |
| 63 | let mut out = [0u8; 32]; |
| 64 | out.copy_from_slice(&digest); |
| 65 | out |
| 66 | }) |
| 67 | .collect(); |
| 68 | while level.len() > 1 { |
| 69 | if level.len() % 2 == 1 { |
| 70 | level.push(*level.last().unwrap()); |
| 71 | } |
| 72 | let mut next = Vec::with_capacity(level.len() / 2); |
| 73 | for pair in level.chunks(2) { |
| 74 | let mut hasher = Sha256::new(); |
| 75 | hasher.update(pair[0]); |
| 76 | hasher.update(pair[1]); |
| 77 | let digest = hasher.finalize(); |
| 78 | let mut out = [0u8; 32]; |
| 79 | out.copy_from_slice(&digest); |
| 80 | next.push(out); |
| 81 | } |
| 82 | level = next; |
| 83 | } |
| 84 | hex_encode(&level[0]) |
| 85 | } |
| 86 | |
| 87 | #[cfg(test)] |
| 88 | mod tests { |
| 89 | use super::*; |
| 90 | |
| 91 | #[test] |
| 92 | fn merkle_root_changes_when_a_leaf_changes() { |
| 93 | let a = merkle_root(&[b"aaa", b"bbb"]); |
| 94 | let b = merkle_root(&[b"aaa", b"ccc"]); |
| 95 | assert_ne!(a, b); |
| 96 | assert_eq!(a.len(), 64); |
| 97 | } |
| 98 | |
| 99 | #[test] |
| 100 | fn ephemeral_retention_is_shorter_than_standard() { |
| 101 | let now = Utc::now(); |
| 102 | let ephemeral = retain_until("ephemeral", now).unwrap(); |
| 103 | let standard = retain_until("standard", now).unwrap(); |
| 104 | assert!(ephemeral < standard); |
| 105 | assert!(retain_until("permanent", now).is_none()); |
| 106 | } |
| 107 | } |