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 / canon.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 | //! Canonical little-endian encoding primitives. All identities are computed |
| 17 | //! over bytes produced here, never over serde/JSON output. |
| 18 | |
| 19 | use crate::digest::Digest; |
| 20 | |
| 21 | #[derive(Default, Clone)] |
| 22 | pub struct Enc { |
| 23 | pub buf: Vec<u8>, |
| 24 | } |
| 25 | |
| 26 | impl Enc { |
| 27 | pub fn new() -> Self { |
| 28 | Enc { buf: Vec::new() } |
| 29 | } |
| 30 | pub fn bytes(&mut self, b: &[u8]) -> &mut Self { |
| 31 | self.buf.extend_from_slice(b); |
| 32 | self |
| 33 | } |
| 34 | pub fn u8(&mut self, v: u8) -> &mut Self { |
| 35 | self.buf.push(v); |
| 36 | self |
| 37 | } |
| 38 | pub fn u16(&mut self, v: u16) -> &mut Self { |
| 39 | self.bytes(&v.to_le_bytes()) |
| 40 | } |
| 41 | pub fn u32(&mut self, v: u32) -> &mut Self { |
| 42 | self.bytes(&v.to_le_bytes()) |
| 43 | } |
| 44 | pub fn u64(&mut self, v: u64) -> &mut Self { |
| 45 | self.bytes(&v.to_le_bytes()) |
| 46 | } |
| 47 | pub fn digest(&mut self, d: &Digest) -> &mut Self { |
| 48 | self.bytes(&d.0) |
| 49 | } |
| 50 | /// u32 length prefix + bytes. |
| 51 | pub fn blob(&mut self, b: &[u8]) -> &mut Self { |
| 52 | self.u32(b.len() as u32).bytes(b) |
| 53 | } |
| 54 | pub fn str(&mut self, s: &str) -> &mut Self { |
| 55 | self.blob(s.as_bytes()) |
| 56 | } |
| 57 | pub fn u64s(&mut self, v: &[u64]) -> &mut Self { |
| 58 | self.u32(v.len() as u32); |
| 59 | for x in v { |
| 60 | self.u64(*x); |
| 61 | } |
| 62 | self |
| 63 | } |
| 64 | pub fn finish(self) -> Vec<u8> { |
| 65 | self.buf |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /// Decoding error: a bare code, never content. |
| 70 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 71 | pub struct DecodeError; |
| 72 | |
| 73 | impl std::fmt::Display for DecodeError { |
| 74 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 75 | f.write_str("malformed canonical bytes") |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | impl std::error::Error for DecodeError {} |
| 80 | |
| 81 | pub struct Dec<'a> { |
| 82 | buf: &'a [u8], |
| 83 | pos: usize, |
| 84 | } |
| 85 | |
| 86 | impl<'a> Dec<'a> { |
| 87 | pub fn new(buf: &'a [u8]) -> Self { |
| 88 | Dec { buf, pos: 0 } |
| 89 | } |
| 90 | pub fn take(&mut self, n: usize) -> Result<&'a [u8], DecodeError> { |
| 91 | if n > self.buf.len() - self.pos { |
| 92 | return Err(DecodeError); |
| 93 | } |
| 94 | let s = &self.buf[self.pos..self.pos + n]; |
| 95 | self.pos += n; |
| 96 | Ok(s) |
| 97 | } |
| 98 | pub fn u8(&mut self) -> Result<u8, DecodeError> { |
| 99 | Ok(self.take(1)?[0]) |
| 100 | } |
| 101 | pub fn u16(&mut self) -> Result<u16, DecodeError> { |
| 102 | Ok(u16::from_le_bytes(self.take(2)?.try_into().unwrap())) |
| 103 | } |
| 104 | pub fn u32(&mut self) -> Result<u32, DecodeError> { |
| 105 | Ok(u32::from_le_bytes(self.take(4)?.try_into().unwrap())) |
| 106 | } |
| 107 | pub fn u64(&mut self) -> Result<u64, DecodeError> { |
| 108 | Ok(u64::from_le_bytes(self.take(8)?.try_into().unwrap())) |
| 109 | } |
| 110 | pub fn digest(&mut self) -> Result<Digest, DecodeError> { |
| 111 | Ok(Digest(self.take(32)?.try_into().unwrap())) |
| 112 | } |
| 113 | pub fn blob(&mut self, max: usize) -> Result<&'a [u8], DecodeError> { |
| 114 | let n = self.u32()? as usize; |
| 115 | if n > max { |
| 116 | return Err(DecodeError); |
| 117 | } |
| 118 | self.take(n) |
| 119 | } |
| 120 | pub fn done(&self) -> bool { |
| 121 | self.pos == self.buf.len() |
| 122 | } |
| 123 | pub fn finish(&self) -> Result<(), DecodeError> { |
| 124 | if self.done() { |
| 125 | Ok(()) |
| 126 | } else { |
| 127 | Err(DecodeError) |
| 128 | } |
| 129 | } |
| 130 | } |