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/tests/boundary.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 / tests / boundary.rs` 347 lines ยท 11690 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 //! BREADCRUMB-1 and security gates: CRUMB_V1_FORMAT_PASS, SEALED_BOUNDARY_PASS, 17 //! CRUMB_DETERMINISM_PASS (golden), and the leak tests over serialization, 18 //! Debug/Display, errors, protocol transcripts, traces and training samples. 19 20 use crumbs::audit::{find_labels, find_needles, sealed_needles}; 21 use crumbs::digest::Digest; 22 use crumbs::gen::{self, registry}; 23 use crumbs::protocol::{LearnerProcess, SearchConfig}; 24 use crumbs::verify::{evaluate, Verdict}; 25 use crumbs::visible::{Encoding, VisibleCrumb, VisibleError}; 26 27 const PROBE: &str = env!("CARGO_BIN_EXE_crumbs-probe-learner"); 28 29 fn tmp(name: &str) -> String { 30 let d = std::env::temp_dir().join(format!("crumbs-test-{}-{name}", std::process::id())); 31 let _ = std::fs::remove_file(&d); 32 d.to_string_lossy().into_owned() 33 } 34 35 #[test] 36 fn crumb_v1_format_pass() { 37 let pairs = vec![ 38 (vec![1u64], vec![3u64]), 39 (vec![2], vec![5]), 40 (vec![10], vec![21]), 41 ]; 42 let c = VisibleCrumb::from_values(Encoding::DecimalUtf8, 0, 0, &pairs, None).unwrap(); 43 let b = c.to_bytes(); 44 assert_eq!(&b[..4], b"CRB1"); 45 assert_eq!(VisibleCrumb::from_bytes(&b).unwrap(), c); 46 // The digest is BLAKE3 over exactly these bytes and nothing else. 47 assert_eq!( 48 c.digest(), 49 crumbs::digest::digest(crumbs::digest::Domain::CrumbDigest, &b) 50 ); 51 // Any byte change changes identity; a sealed label cannot influence it. 52 let c2 = VisibleCrumb::from_values( 53 Encoding::DecimalUtf8, 54 0, 55 0, 56 &[(vec![1], vec![3]), (vec![2], vec![5]), (vec![10], vec![22])], 57 None, 58 ) 59 .unwrap(); 60 assert_ne!(c.digest(), c2.digest()); 61 62 // Strict decoding. 63 let mut bad = b.clone(); 64 bad[0] = b'X'; 65 assert_eq!(VisibleCrumb::from_bytes(&bad), Err(VisibleError::Magic)); 66 let mut bad = b.clone(); 67 bad[4] = 2; 68 assert_eq!(VisibleCrumb::from_bytes(&bad), Err(VisibleError::Version)); 69 let mut bad = b.clone(); 70 bad.push(0); 71 assert!( 72 VisibleCrumb::from_bytes(&bad).is_err(), 73 "trailing byte accepted" 74 ); 75 let mut bad = b.clone(); 76 bad[7] = 0x80; 77 assert!( 78 VisibleCrumb::from_bytes(&bad).is_err(), 79 "unknown flag accepted" 80 ); 81 // Non-canonical decimal ("03" instead of "3") is rejected. 82 let text = String::from_utf8_lossy(&b).to_string(); 83 assert!(text.contains('3')); 84 let mut e = crumbs::canon::Enc::new(); 85 e.bytes(b"CRB1") 86 .u16(1) 87 .u8(1) 88 .u8(0) 89 .u8(1) 90 .u8(1) 91 .u8(0) 92 .u8(0) 93 .u32(1) 94 .blob(b"1") 95 .blob(b"03"); 96 assert_eq!( 97 VisibleCrumb::from_bytes(&e.finish()), 98 Err(VisibleError::NonCanonical) 99 ); 100 101 // Raw little-endian encoding round-trips and masks to the lane width. 102 let r = VisibleCrumb::from_values( 103 Encoding::RawLe, 104 2, 105 1, 106 &[(vec![0x1_0005], vec![0x1FF])], 107 None, 108 ) 109 .unwrap(); 110 assert_eq!(r.values(), vec![(vec![5], vec![0xFF])]); 111 assert_eq!(VisibleCrumb::from_bytes(&r.to_bytes()).unwrap(), r); 112 } 113 114 /// Golden identities: any behavioural change to a generator or to the 115 /// canonical encodings without a version bump fails here. 116 #[test] 117 fn crumb_determinism_golden_pass() { 118 let reg = registry(); 119 let mut acc = blake3::Hasher::new(); 120 for f in ®.families { 121 for seed in [0u64, 1, 99] { 122 let g = gen::generate(f, seed); 123 acc.update(&g.visible.digest().0); 124 acc.update(&g.sealed.digest().0); 125 acc.update(&g.sealed.generator_instance_digest.0); 126 } 127 } 128 let got = Digest(*acc.finalize().as_bytes()).hex(); 129 let golden = include_str!("golden_registry_digest.txt").trim(); 130 assert_eq!( 131 got, golden, 132 "generator output drifted; bump GENERATOR_VERSION and re-pin" 133 ); 134 } 135 136 #[test] 137 fn sealed_boundary_pass_visible_bytes() { 138 for f in ®istry().families { 139 for seed in [0u64, 5, 11] { 140 let g = gen::generate(f, seed); 141 let bytes = g.visible.to_bytes(); 142 let hits = find_labels(&bytes); 143 assert!( 144 hits.is_empty(), 145 "{}: labels in visible bytes: {hits:?}", 146 f.key 147 ); 148 let needles = sealed_needles(&g.sealed, &g.visible.values(), 1 << 32); 149 let hits: Vec = needles 150 .iter() 151 .filter(|n| crumbs::audit::contains(&bytes, n)) 152 .map(|n| String::from_utf8(n.clone()).unwrap_or_else(|_| format!("{n:?}"))) 153 .collect(); 154 assert!( 155 hits.is_empty(), 156 "{} seed {seed}: sealed value in visible bytes: {hits:?} in {:?}", 157 f.key, 158 String::from_utf8_lossy(&bytes) 159 ); 160 // Sealed digests appear nowhere in the visible crumb. 161 for d in [ 162 g.sealed.digest(), 163 g.sealed.generator_instance_digest, 164 g.sealed.heldouts.digest(), 165 ] { 166 assert!(!crumbs::audit::contains(&bytes, &d.0)); 167 } 168 } 169 } 170 } 171 172 #[test] 173 fn sealed_boundary_pass_debug_display_errors() { 174 let f = registry().by_key("decoy-simple-rule-w12").unwrap(); 175 let g = gen::generate(f, 3); 176 for s in [ 177 format!("{:?}", g.sealed), 178 format!("{:?}", g.sealed.heldouts), 179 format!("{:?}", g.sealed.heldouts.adversarial.first().unwrap()), 180 ] { 181 assert!( 182 find_labels(s.as_bytes()).is_empty(), 183 "Debug leaked labels: {s}" 184 ); 185 for ex in g.sealed.heldouts.adversarial.iter() { 186 assert!( 187 !s.contains(&ex.output[0].to_string()) || ex.output[0] < 1000, 188 "Debug leaked a held-out value: {s}" 189 ); 190 } 191 assert!(s.contains("redacted")); 192 } 193 // Error types carry no content. 194 for e in [ 195 VisibleError::Magic, 196 VisibleError::Version, 197 VisibleError::Shape, 198 VisibleError::Lane, 199 VisibleError::Length, 200 VisibleError::NonCanonical, 201 ] { 202 let s = e.to_string(); 203 assert!(find_labels(s.as_bytes()).is_empty()); 204 assert!(s.len() < 64); 205 } 206 let pe = crumbs::protocol::ProtocolError::Violation.to_string(); 207 assert!(find_labels(pe.as_bytes()).is_empty()); 208 let de = crumbs::canon::DecodeError.to_string(); 209 assert!(find_labels(de.as_bytes()).is_empty()); 210 } 211 212 type ProbeRun = ( 213 Result, String>, 214 Vec, 215 Vec, 216 Vec>, 217 ); 218 219 fn run_probe(mode: &str, fam: &str, seed: u64, max_q: u32) -> ProbeRun { 220 let loot = tmp(&format!("{mode}-{fam}-{seed}")); 221 let mut lp = LearnerProcess::spawn(PROBE, &[mode, &loot], true).unwrap(); 222 let cfg = SearchConfig { 223 max_oracle_queries: max_q, 224 ..SearchConfig::default() 225 }; 226 lp.hello(&cfg, false).unwrap(); 227 let f = registry().by_key(fam).unwrap(); 228 let g = gen::generate(f, seed); 229 let mut verdicts = Vec::new(); 230 let mut n = 0u32; 231 let res = lp.run_crumb(&g.visible, &mut |sub| { 232 n += 1; 233 if n > max_q { 234 return Verdict::BudgetExhausted; 235 } 236 let ev = evaluate(&g.visible, &g.sealed, &sub.program, sub.hypotheses, false); 237 verdicts.push((sub.hypotheses, ev.verdict)); 238 ev.verdict 239 }); 240 let transcript = lp.transcript.clone().unwrap(); 241 let out = match res { 242 Ok(_) => { 243 let _ = lp.shutdown(); 244 Ok(verdicts) 245 } 246 Err(e) => { 247 lp.kill(); 248 Err(e.to_string()) 249 } 250 }; 251 let needles = sealed_needles(&g.sealed, &g.visible.values(), 0); 252 let loot_bytes = std::fs::read(&loot).unwrap_or_default(); 253 (out, transcript, loot_bytes, needles) 254 } 255 256 /// Everything sent to a learner is sterile: no labels, no digests, no held-out values. 257 #[test] 258 fn sealed_boundary_pass_protocol_transcript() { 259 for (fam, seed) in [ 260 ("chain-basic-l2-w16", 1u64), 261 ("decoy-simple-rule-w12", 2), 262 ("insufficient-evidence-n2", 3), 263 ("capability-03", 4), 264 ] { 265 let (res, transcript, loot, needles) = run_probe("honest", fam, seed, 4); 266 res.unwrap(); 267 // The learner received exactly the transcript, then the shutdown frame. 268 assert_eq!(loot, [transcript.clone(), vec![0x05, 0, 0, 0, 0]].concat()); 269 assert!( 270 find_labels(&transcript).is_empty(), 271 "{fam}: labels in transcript" 272 ); 273 // Held-out values that are not also visible never cross the boundary. 274 let vis = gen::generate(registry().by_key(fam).unwrap(), seed) 275 .visible 276 .to_bytes(); 277 let leaked: Vec<_> = needles 278 .iter() 279 .filter(|n| { 280 n.len() >= 8 281 && crumbs::audit::contains(&transcript, n) 282 && !crumbs::audit::contains(&vis, n) 283 }) 284 .collect(); 285 assert!(leaked.is_empty(), "{fam}: sealed bytes in transcript"); 286 } 287 } 288 289 /// Negative test: a learner that asks for held-out data through the protocol 290 /// gets nothing. The session aborts on the forged frame and no reply is sent. 291 #[test] 292 fn no_hidden_access_hostile_learner() { 293 let (res, transcript, loot, needles) = run_probe("hostile", "decoy-simple-rule-w12", 9, 4); 294 assert!(res.is_err(), "a forged frame must abort the session"); 295 // The learner received HELLO and CRUMB only. 296 let mut kinds = Vec::new(); 297 let mut i = 0; 298 while i + 5 <= loot.len() { 299 kinds.push(loot[i]); 300 let n = u32::from_le_bytes(loot[i + 1..i + 5].try_into().unwrap()) as usize; 301 i += 5 + n; 302 } 303 assert_eq!( 304 kinds, 305 vec![0x01, 0x02], 306 "learner received more than HELLO and CRUMB" 307 ); 308 assert_eq!(transcript, loot); 309 assert_eq!( 310 find_needles( 311 &loot, 312 &needles 313 .into_iter() 314 .filter(|n| n.len() >= 32) 315 .collect::>() 316 ), 317 0 318 ); 319 } 320 321 /// Negative test: the oracle cannot be mined. After the query budget every 322 /// answer is "budget exhausted", and every answer is a single byte. 323 #[test] 324 fn no_hidden_access_oracle_budget() { 325 let (res, _transcript, loot, _) = run_probe("flood", "chain-basic-l3-w16", 12, 3); 326 let verdicts = res.unwrap(); 327 assert!( 328 verdicts.len() <= 3, 329 "the sealed evaluator ran more than the budget" 330 ); 331 let mut i = 0; 332 let mut vbytes = Vec::new(); 333 while i + 5 <= loot.len() { 334 let t = loot[i]; 335 let n = u32::from_le_bytes(loot[i + 1..i + 5].try_into().unwrap()) as usize; 336 if t == 0x03 { 337 assert_eq!(n, 1, "a verdict frame carried more than one byte"); 338 vbytes.push(loot[i + 5]); 339 } 340 i += 5 + n; 341 } 342 assert!(vbytes.iter().all(|b| (1..=3).contains(b))); 343 assert!( 344 vbytes.iter().skip(3).all(|b| *b == 3), 345 "answers after the budget must be 'exhausted'" 346 ); 347 }