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/crumbs/src/program.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 / crumbs / src / program.rs` 240 lines ยท 6199 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 //! Candidate Program v1 ("CPG1"): the learner-neutral program format of the 17 //! generic verifier contract. 18 //! 19 //! A learner submits candidates in this format; the sealed verifier executes 20 //! them with its own interpreter (it never trusts the learner's execution and 21 //! never needs to know the learner's internals). v1 programs are unary: they 22 //! map lane 0 of the input to lane 0 of the output as a chain of wrapping u64 23 //! operations. 24 //! 25 //! ```text 26 //! "CPG1" | version u16 (=1) | n u8 (<= 64) | n x (op u8, imm u64) 27 //! ``` 28 29 use crate::canon::{Dec, DecodeError, Enc}; 30 use crate::digest::{digest, Digest, Domain}; 31 32 pub const MAGIC: &[u8; 4] = b"CPG1"; 33 pub const VERSION: u16 = 1; 34 pub const MAX_STEPS: usize = 64; 35 36 #[derive( 37 Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, serde::Serialize, serde::Deserialize, 38 )] 39 #[repr(u8)] 40 pub enum Op { 41 Add = 1, 42 Sub = 2, 43 Mul = 3, 44 And = 4, 45 Or = 5, 46 Xor = 6, 47 Shl = 7, 48 Shr = 8, 49 Rotl = 9, 50 } 51 52 impl Op { 53 pub fn from_u8(v: u8) -> Option { 54 Some(match v { 55 1 => Op::Add, 56 2 => Op::Sub, 57 3 => Op::Mul, 58 4 => Op::And, 59 5 => Op::Or, 60 6 => Op::Xor, 61 7 => Op::Shl, 62 8 => Op::Shr, 63 9 => Op::Rotl, 64 _ => return None, 65 }) 66 } 67 68 #[inline] 69 pub fn apply(self, x: u64, imm: u64) -> u64 { 70 match self { 71 Op::Add => x.wrapping_add(imm), 72 Op::Sub => x.wrapping_sub(imm), 73 Op::Mul => x.wrapping_mul(imm), 74 Op::And => x & imm, 75 Op::Or => x | imm, 76 Op::Xor => x ^ imm, 77 Op::Shl => x.wrapping_shl((imm & 63) as u32), 78 Op::Shr => x.wrapping_shr((imm & 63) as u32), 79 Op::Rotl => x.rotate_left((imm & 63) as u32), 80 } 81 } 82 } 83 84 #[derive( 85 Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, serde::Serialize, serde::Deserialize, 86 )] 87 pub struct Step { 88 pub op: Op, 89 pub imm: u64, 90 } 91 92 #[derive( 93 Clone, 94 Debug, 95 PartialEq, 96 Eq, 97 Hash, 98 Default, 99 PartialOrd, 100 Ord, 101 serde::Serialize, 102 serde::Deserialize, 103 )] 104 pub struct Program { 105 pub steps: Vec, 106 } 107 108 impl Program { 109 pub fn new(steps: Vec) -> Self { 110 Program { steps } 111 } 112 113 pub fn of(steps: &[(Op, u64)]) -> Self { 114 Program { 115 steps: steps.iter().map(|&(op, imm)| Step { op, imm }).collect(), 116 } 117 } 118 119 #[inline] 120 pub fn run(&self, mut x: u64) -> u64 { 121 for s in &self.steps { 122 x = s.op.apply(x, s.imm); 123 } 124 x 125 } 126 127 pub fn then(&self, next: &Program) -> Program { 128 let mut steps = self.steps.clone(); 129 steps.extend_from_slice(&next.steps); 130 Program { steps } 131 } 132 133 pub fn len(&self) -> usize { 134 self.steps.len() 135 } 136 137 pub fn is_empty(&self) -> bool { 138 self.steps.is_empty() 139 } 140 141 pub fn to_bytes(&self) -> Vec { 142 let mut e = Enc::new(); 143 e.bytes(MAGIC).u16(VERSION).u8(self.steps.len() as u8); 144 for s in &self.steps { 145 e.u8(s.op as u8).u64(s.imm); 146 } 147 e.finish() 148 } 149 150 pub fn decode(d: &mut Dec<'_>) -> Result { 151 if d.take(4)? != MAGIC || d.u16()? != VERSION { 152 return Err(DecodeError); 153 } 154 let n = d.u8()? as usize; 155 if n > MAX_STEPS { 156 return Err(DecodeError); 157 } 158 let mut steps = Vec::with_capacity(n); 159 for _ in 0..n { 160 let op = Op::from_u8(d.u8()?).ok_or(DecodeError)?; 161 steps.push(Step { op, imm: d.u64()? }); 162 } 163 Ok(Program { steps }) 164 } 165 166 pub fn from_bytes(bytes: &[u8]) -> Result { 167 let mut d = Dec::new(bytes); 168 let p = Program::decode(&mut d)?; 169 d.finish()?; 170 Ok(p) 171 } 172 173 /// Candidate program digest (operation semantic id). 174 pub fn digest(&self) -> Digest { 175 digest(Domain::Program, &self.to_bytes()) 176 } 177 178 /// Behaviour signature over the public probe inputs. 179 pub fn behavior(&self) -> Digest { 180 let mut e = Enc::new(); 181 for &x in PROBES.iter() { 182 e.u64(self.run(x)); 183 } 184 digest(Domain::Behavior, &e.finish()) 185 } 186 187 /// Every contiguous sub-chain of length in [min_len, max_len]. 188 pub fn sub_chains(&self, min_len: usize, max_len: usize) -> Vec { 189 let mut out = Vec::new(); 190 for len in min_len..=max_len.min(self.steps.len()) { 191 for start in 0..=self.steps.len() - len { 192 out.push(Program { 193 steps: self.steps[start..start + len].to_vec(), 194 }); 195 } 196 } 197 out 198 } 199 } 200 201 /// Identity of this interpreter's semantics (part of the verifier digest). 202 pub fn std_interpreter_id() -> &'static str { 203 "cpg1-interp/1: u64 wrapping add sub mul; and or xor; shl shr rotl by imm&63" 204 } 205 206 /// Public probe inputs used for behaviour signatures (not secret). 207 pub const PROBES: [u64; 32] = [ 208 0, 209 1, 210 2, 211 3, 212 5, 213 7, 214 13, 215 42, 216 100, 217 127, 218 128, 219 255, 220 256, 221 1000, 222 4095, 223 65535, 224 65536, 225 1_000_003, 226 0x7FFF_FFFF, 227 0x8000_0000, 228 0xFFFF_FFFF, 229 0x1_0000_0000, 230 0x0123_4567_89AB_CDEF, 231 0x0F0F_0F0F_0F0F_0F0F, 232 0x5555_5555_5555_5555, 233 0xAAAA_AAAA_AAAA_AAAA, 234 0x7FFF_FFFF_FFFF_FFFF, 235 0x8000_0000_0000_0000, 236 0xFFFF_FFFF_FFFF_FFFE, 237 u64::MAX, 238 0x9E37_79B9_7F4A_7C15, 239 0xD1B5_4A32_D192_ED03, 240 ];