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/omega-mv/blob/main/research/atemporal/at1/evaluator/src/hidden.rs # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / research / atemporal / at1 / evaluator / src / hidden.rs` 361 lines ยท 13774 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 //! Hidden-set generator (documented, deterministic, seeded) and the commitment 17 //! procedure of AT1_SPEC 12.3. The procedure is public; the 256-bit seed is 18 //! not, so the cases cannot be reproduced before the reveal. 19 //! 20 //! Stream: block i = SHA-256("omega.at1.agent4.hidden.v1" || 0x00 || seed || be64(i)). 21 //! 27 cases, family = index mod 9, from the AT1_SPEC 13.1 item 7 families: 22 //! F0 rotated level (INTERACTING, PASS) F1 rotated level, target IDEAL (N1) 23 //! F2 every level coupled (INTERACTING) F3 degenerate level + rotated level 24 //! F4 spectator couplings, target IDEAL F5 every level out (N2) 25 //! F6 swapped level v_j = -2h (N3) F7 psi_0 orthogonal to the kernel (N6) 26 //! F8 rotated level with half weight (N4a) 27 //! Axes come from Pythagorean quadruples, signs and component order drawn from 28 //! the stream; |h| = 1/2 always. Expected outcome and codes are the exact 29 //! verdict of a correct implementation (honest synthetic run, re-derived 30 //! exactly); a draw is rejected and redrawn unless the family's intended code 31 //! set results and the AT1_SPEC 8.3 margin rule holds. 32 33 use crate::case::{validate_bytes, Case, Spec}; 34 use crate::model::{shadow, Mods}; 35 use crate::rat::{Cq, Q}; 36 use crate::sha256::{sha256, sha256_hex}; 37 use crate::synth::{engine_result, oracle_record, splice, EngineMut, OracleMut}; 38 39 const DOMAIN: &[u8] = b"omega.at1.agent4.hidden.v1"; 40 pub const COUNT: usize = 27; 41 42 struct Rng { 43 seed: Vec, 44 block: u64, 45 buf: Vec, 46 } 47 impl Rng { 48 fn u64(&mut self) -> u64 { 49 if self.buf.len() < 8 { 50 let mut m = DOMAIN.to_vec(); 51 m.push(0); 52 m.extend_from_slice(&self.seed); 53 m.extend_from_slice(&self.block.to_be_bytes()); 54 self.block += 1; 55 self.buf.extend_from_slice(&sha256(&m)); 56 } 57 let b: Vec = self.buf.drain(..8).collect(); 58 u64::from_be_bytes([b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]]) 59 } 60 /// Uniform in [0, n) by rejection. 61 fn below(&mut self, n: u64) -> u64 { 62 let lim = u64::MAX - u64::MAX % n; 63 loop { 64 let x = self.u64(); 65 if x < lim { 66 return x % n; 67 } 68 } 69 } 70 fn pick(&mut self, v: &[T]) -> T { 71 v[self.below(v.len() as u64) as usize].clone() 72 } 73 } 74 75 const QUADS: [[i64; 4]; 11] = [[1, 2, 2, 3], [2, 3, 6, 7], [1, 4, 8, 9], [4, 4, 7, 9], [2, 6, 9, 11], [6, 6, 7, 11], [3, 4, 12, 13], [2, 5, 14, 15], [2, 10, 11, 15], [1, 12, 12, 17], [8, 9, 12, 17]]; 76 77 /// A random rational unit axis (a, b, c)/d. 78 fn axis(g: &mut Rng) -> [Q; 3] { 79 let q = g.pick(&QUADS); 80 let perm = g.pick(&[[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]]); 81 let mut out = [Q::zero(), Q::zero(), Q::zero()]; 82 for i in 0..3 { 83 let s = if g.below(2) == 0 { 1 } else { -1 }; 84 out[i] = Q::frac(s * q[perm[i]], q[3]); 85 } 86 out 87 } 88 fn scale(a: &[Q; 3], r: &Q) -> [Q; 3] { 89 [a[0].mul(r), a[1].mul(r), a[2].mul(r)] 90 } 91 fn sub3(a: &[Q; 3], b: &[Q; 3]) -> [Q; 3] { 92 [a[0].sub(&b[0]), a[1].sub(&b[1]), a[2].sub(&b[2])] 93 } 94 fn small_c(g: &mut Rng) -> Cq { 95 let d = g.pick(&[1i64, 1, 2, 3, 5]); 96 Cq::new(Q::frac(g.below(7) as i64 - 3, d), Q::frac(g.below(7) as i64 - 3, d)) 97 } 98 99 /// The margin rule of AT1_SPEC 8.3 for a correct implementation of this case. 100 pub fn margin_ok(c: &Case) -> Result<(), String> { 101 let sh = shadow(c, &Mods::default(), false); 102 let tiny = Q::frac(1, 100_000_000_000_000_000); 103 let near0 = |x: &Q, exact: bool| if exact { x.is_zero() } else { x.abs().cmp(&tiny) != std::cmp::Ordering::Greater }; 104 let big = |x: &Q, tol: &Q| x.abs().cmp(&tol.mul(&Q::int(1000))) != std::cmp::Ordering::Less; 105 let tol_prob = Q::from_scaled(&c.tol_prob); 106 let tol_schro = Q::from_scaled(&c.tol_schro); 107 let tol_povm = Q::from_scaled(&c.tol_povm); 108 let pv = &sh.povm_residual; 109 if !(near0(&pv.q, pv.exact) || big(&pv.q, &tol_povm)) { 110 return Err(format!("povm_residual {} inside the margin", pv.q.to_f64())); 111 } 112 if sh.trivial { 113 return Ok(()); 114 } 115 let milli = Q::frac(1, 1000); 116 let mut sum = Q::zero(); 117 let mut sum_exact = true; 118 let wn = c.w.div(&Q::int(c.n as i64)); 119 for k in 0..c.m { 120 let p = &sh.p[k]; 121 sum = sum.add(&p.q); 122 sum_exact &= p.exact; 123 if !(near0(&p.q, p.exact) || p.q.cmp(&milli) != std::cmp::Ordering::Less) { 124 return Err(format!("p({}) = {} is neither 0 nor >= 1e-3", k, p.q.to_f64())); 125 } 126 let over = p.q.sub(&Q::one()); 127 if over.signum() > 0 && !big(&over, &tol_prob) { 128 return Err(format!("p({}) - 1 inside the margin", k)); 129 } 130 if c.target_ideal { 131 if let Some(pa) = &sh.pauli[k] { 132 for a in 0..3 { 133 for s in 0..2 { 134 let d = pa[a][s].q.sub(&sh.ideal[k][a][s].q); 135 if !(near0(&d, pa[a][s].exact && sh.ideal[k][a][s].exact) || big(&d, &tol_schro)) { 136 return Err(format!("ideal deviation at k={} inside the margin", k)); 137 } 138 } 139 } 140 let d = p.q.sub(&wn); 141 if !(near0(&d, p.exact) || big(&d, &tol_prob)) { 142 return Err(format!("marginal deviation from w/N at k={} inside the margin", k)); 143 } 144 } 145 } 146 } 147 let d = sum.sub(&Q::one()); 148 if !(near0(&d, sum_exact) || big(&d, &tol_prob)) { 149 return Err("sum of p(k) - 1 inside the margin".into()); 150 } 151 Ok(()) 152 } 153 154 /// One draw of family f; None when the draw is not usable. 155 fn draw(g: &mut Rng, f: usize, name: &str) -> Option { 156 let mut s = Spec::base(name); 157 let half = Q::frac(1, 2); 158 let h = scale(&axis(g), &half); 159 let degen = f == 3; 160 // clock grid: gap 1 with tau 1/4, or gap 1/2 with tau 1/2 (degenerate family); M = 4 complete 161 let n = if degen { g.pick(&[3usize, 5]) } else { g.pick(&[2usize, 3, 4, 5, 6]) }; 162 let gap = if degen { Q::frac(1, 2) } else { Q::one() }; 163 let mid = Q::frac(n as i64 - 1, 2); 164 let mut e: Vec = (0..n).map(|j| Q::int(j as i64).sub(&mid).mul(&gap)).collect(); 165 // h0 puts two adjacent levels at -1/2 and +1/2 (odd N, gap 1: h0 = 1/2); an optional common shift delta keeps that 166 let mut h0 = if !degen && n % 2 == 1 { half.clone() } else { Q::zero() }; 167 if g.below(3) == 0 { 168 let delta = Q::frac(1 + g.below(97) as i64, 1009); 169 for x in e.iter_mut() { 170 *x = x.add(&delta); 171 } 172 h0 = h0.sub(&delta); 173 } 174 s.e = e.clone(); 175 s.h = [h0.clone(), h[0].clone(), h[1].clone(), h[2].clone()]; 176 s.v = vec![[Q::zero(), Q::zero(), Q::zero()]; n]; 177 s.tau = if degen { Q::frac(1, 2) } else { Q::frac(1, 4) }; 178 s.w = Q::frac(n as i64, 4); 179 s.psi = [small_c(g), small_c(g)]; 180 if s.psi[0].is_zero() && s.psi[1].is_zero() { 181 return None; 182 } 183 let r = g.below(4) as usize; 184 s.ref_label = format!("t{}", r); 185 let off: Vec = e.iter().map(|x| x.add(&h0)).collect(); 186 let matched: Vec = (0..n).filter(|&j| off[j].abs() == half).collect(); 187 let others: Vec = (0..n).filter(|&j| off[j].abs() != half).collect(); 188 if matched.is_empty() { 189 return None; 190 } 191 let rotate = |g: &mut Rng, j: usize| -> [Q; 3] { sub3(&scale(&axis(g), &off[j].abs()), &h) }; 192 match f { 193 0 | 1 | 8 => { 194 let j = g.pick(&matched); 195 s.v[j] = rotate(g, j); 196 if f == 1 { 197 s.target_ideal = true; 198 } 199 if f == 8 { 200 s.w = Q::frac(n as i64, 8); 201 } 202 } 203 2 => { 204 for j in 0..n { 205 if off[j].is_zero() { 206 return None; 207 } 208 s.v[j] = rotate(g, j); 209 } 210 } 211 3 => { 212 let z = (0..n).find(|&j| off[j].is_zero())?; 213 s.v[z] = [h[0].neg(), h[1].neg(), h[2].neg()]; 214 let j = g.pick(&matched); 215 s.v[j] = rotate(g, j); 216 } 217 4 => { 218 if others.is_empty() { 219 return None; 220 } 221 for &j in &others { 222 // norm |E_j + h0| + 1/2 never matches the level 223 s.v[j] = sub3(&scale(&axis(g), &off[j].abs().add(&half)), &h); 224 } 225 s.target_ideal = true; 226 } 227 5 => { 228 for &j in &matched { 229 s.v[j] = sub3(&scale(&axis(g), &Q::one()), &h); 230 } 231 } 232 6 => { 233 let j = *matched.iter().find(|&&j| off[j].signum() > 0)?; 234 s.v[j] = [h[0].mul(&Q::int(-2)), h[1].mul(&Q::int(-2)), h[2].mul(&Q::int(-2))]; 235 } 236 7 => { 237 if matched.len() < 2 { 238 return None; 239 } 240 let keep = g.pick(&matched); 241 for &j in &matched { 242 if j != keep { 243 s.v[j] = sub3(&scale(&axis(g), &Q::one()), &h); 244 } 245 } 246 let nn = scale(&axis(g), &off[keep].abs()); 247 s.v[keep] = sub3(&nn, &h); 248 // kernel vector of the kept level and a psi_0 orthogonal to it 249 let sgn = if off[keep].signum() < 0 { 1 } else { -1 }; 250 let rr = off[keep].abs(); 251 let sr = if sgn > 0 { rr.clone() } else { rr.neg() }; 252 let (f0, f1) = if sr.add(&nn[2]).is_zero() { 253 (Cq::new(nn[0].clone(), nn[1].neg()), Cq::new(sr.sub(&nn[2]), Q::zero())) 254 } else { 255 (Cq::new(sr.add(&nn[2]), Q::zero()), Cq::new(nn[0].clone(), nn[1].clone())) 256 }; 257 s.psi = [f1.conj().scale(&Q::int(-1)), f0.conj()]; 258 } 259 _ => return None, 260 } 261 Some(s) 262 } 263 264 fn intended(f: usize) -> Vec<&'static str> { 265 match f { 266 1 => vec!["SCHRODINGER_DEVIATION_EXCEEDED"], 267 5 | 7 => vec!["TRIVIAL_PHYSICAL_STATE"], 268 6 => vec!["CONDITIONAL_UNDEFINED"], 269 8 => vec!["POVM_NORMALIZATION_EXCEEDED", "PROBABILITY_SUM_EXCEEDED"], 270 _ => vec![], 271 } 272 } 273 274 /// Builds case number i (deterministic in the seed). 275 fn build(g: &mut Rng, i: usize) -> (String, Vec) { 276 let f = i % 9; 277 let name = format!("at1-agent4-hidden-{:02}", i + 1); 278 for _attempt in 0..10_000 { 279 let s = match draw(g, f, &name) { 280 Some(s) => s, 281 None => continue, 282 }; 283 let c = match validate_bytes(&s.bytes()) { 284 Ok(c) => c, 285 Err(_) => continue, 286 }; 287 let r = splice(&engine_result(&c, &EngineMut::default(), "h"), &oracle_record(&c, &OracleMut::default(), "o")).unwrap(); 288 let codes: Vec<&str> = r.verdict.failure_codes.iter().map(|x| x.as_str()).collect(); 289 if codes != intended(f) { 290 continue; 291 } 292 let s2 = if codes.is_empty() { s } else { s.negative(&codes) }; 293 let c2 = match validate_bytes(&s2.bytes()) { 294 Ok(c) => c, 295 Err(_) => continue, 296 }; 297 if margin_ok(&c2).is_err() { 298 continue; 299 } 300 return (format!("H{:02}.case", i + 1), s2.bytes()); 301 } 302 panic!("hidden-gen: family {} produced no usable draw", f); 303 } 304 305 fn parse_seed(hex: &str) -> Option> { 306 if hex.len() != 64 || !hex.bytes().all(|c| c.is_ascii_hexdigit()) { 307 return None; 308 } 309 (0..32).map(|i| u8::from_str_radix(&hex[2 * i..2 * i + 2], 16).ok()).collect() 310 } 311 312 pub fn generate(seed_hex: &str, outdir: &str) -> i32 { 313 let seed = match parse_seed(seed_hex.trim()) { 314 Some(s) => s, 315 None => { 316 eprintln!("hidden-gen: the seed must be 64 hex digits (256 bits)"); 317 return 64; 318 } 319 }; 320 let mut g = Rng { seed, block: 0, buf: Vec::new() }; 321 let _ = std::fs::create_dir_all(outdir); 322 for i in 0..COUNT { 323 let (name, bytes) = build(&mut g, i); 324 if let Err(e) = std::fs::write(format!("{}/{}", outdir, name), &bytes) { 325 eprintln!("hidden-gen: {}", e); 326 return 73; 327 } 328 } 329 commit(outdir) 330 } 331 332 /// Manifest `` per .case file, sorted bytewise by name, 333 /// LF-terminated; the commitment is SHA-256 of the manifest bytes (AT1_SPEC 12.3). 334 pub fn manifest(dir: &str) -> Result { 335 let mut names: Vec = std::fs::read_dir(dir) 336 .map_err(|e| e.to_string())? 337 .filter_map(|e| e.ok().map(|e| e.file_name().to_string_lossy().into_owned())) 338 .filter(|n| n.ends_with(".case")) 339 .collect(); 340 names.sort_by(|a, b| a.as_bytes().cmp(b.as_bytes())); 341 let mut m = String::new(); 342 for n in names { 343 let b = std::fs::read(format!("{}/{}", dir, n)).map_err(|e| e.to_string())?; 344 m.push_str(&format!("{} {}\n", sha256_hex(&b), n)); 345 } 346 Ok(m) 347 } 348 349 pub fn commit(dir: &str) -> i32 { 350 match manifest(dir) { 351 Ok(m) => { 352 print!("{}", m); 353 println!("commitment {}", sha256_hex(m.as_bytes())); 354 0 355 } 356 Err(e) => { 357 eprintln!("hidden-commit: {}", e); 358 66 359 } 360 } 361 }