code wiki / _hdl_build / nx_evoracle_sweep.nx
nx_evoracle_sweep.nx source
↩ module page · 356 lines · 15421 B
1// nx_evoracle_sweep.nx -- DERIVE ORACLE ROWS ACROSS THE THIRD-PARTY POPULATION, on the host that SERVES
2// the claims, and WRITE the attestation file.
3//
4// Separate organ from the single-subject producer on purpose (rule 9): measuring ONE subject and driving a
5// FLEET are different jobs, and the fleet driver has a failure mode the single-shot one does not --
6// PARTIAL COVERAGE THAT READS AS COMPLETE. So DERIVED / REFUSED / NO-ELF / NOT-GREEN are counted and
7// printed separately and never collapsed. A gate we could not test and a gate that failed must never look
8// the same, or the sweep becomes the thing it audits.
9//
10// PATHS ARE NAS-NATIVE: subjects at buildroot/runtime/_hdl_build/, artifacts at buildroot/_build/. The
11// laptop keeps them at runtime/_hdl_build/ and _build/ -- the artifact-root class this ecosystem has been
12// bitten by five times. Declared here rather than guessed.
13//
14// ROWS ARE UNSIGNED, AND at_verify_row WILL REFUSE THEM. That is the honest end state for an agent:
15// knowledge/attest_keys.conf is absent because nx_fs_write denies paths matching `key`, and an agent that
16// can register its own signing key can certify its own work. This organ supplies the DERIVATION; only a
17// key the operator holds supplies the ATTRIBUTION.
18// license_tier: ORIGINAL expect_exit: 0
19import "nx_syscalls.nx"
20import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc)
21import "nx_sha256_wasm.nx"
22import "nx_evoracle.nx"
23const K_MAGIC_1024: i64 = 1024
24const K_MAGIC_65536: i64 = 65536
25
26func w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
27func wb(b: *u8, n: i64) -> i64 { sys_write(1, b, n); return 0 }
28
29// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer
30// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the
31// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls).
32// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign.
33func nn(v: i64) -> i64 { nxi_out(v); return 0 }
34
35func hexnib(v: i64) -> i64 { if v < 10 { return 48 + v } return 87 + v }
36
37// Read <gate> <subject> lines from a conf and drive one() for each. Comments (#) and blank
38// lines skipped. Bounded: SWEEP_CONF_MAXROWS, so a runaway file cannot spin the sweep.
39const SWEEP_CONF_CAP: i64 = 65536
40const SWEEP_CONF_MAXROWS: i64 = 256
41const SWEEP_TOK_CAP: i64 = 128
42
43// ⚠THIS FUNCTION'S FIRST VERSION SILENTLY IGNORED EVERY ROW. It marked "newline found" with
44// sentinel arithmetic (e = n + n) and then recovered the position as e - n -- which returns n,
45// NOT the newline index, so every line spanned to EOF and parsed as one malformed row. The sweep
46// still printed GREEN and its DERIVED count simply did not move.
47// ★THE ONLY REASON IT WAS CAUGHT: the count was READ BACK and compared (7 before, 7 after) --
48// A FEATURE THAT ADDS NOTHING LOOKS EXACTLY LIKE A FEATURE THAT IS ABSENT, and a green verdict
49// from the surrounding organ says nothing about it. Rewritten with an explicit found-flag.
50func sweep_line_end(src: *u8, from: i64, n: i64) -> i64 {
51 var e: i64 = from
52 var done: i64 = 0
53 while done == 0 {
54 if e >= n { done = 1 }
55 if done == 0 { if src[e] == (10 as u8) { done = 1 } else { e = e + 1 } }
56 }
57 return e
58}
59
60func sweep_conf_rows(c: *i64, rb: *u8, rl: *i64, path: *u8) -> i64 {
61 let lenp: *i64 = sys_mmap(16) as *i64
62 lenp[0] = 0
63 let src: *u8 = sys_read_file(path, lenp)
64 if (src as i64) == 0 { return 0 }
65 var n: i64 = lenp[0]
66 if n > SWEEP_CONF_CAP { n = SWEEP_CONF_CAP }
67 let gbuf: *u8 = sys_mmap(SWEEP_TOK_CAP)
68 let sbuf: *u8 = sys_mmap(SWEEP_TOK_CAP)
69 var i: i64 = 0
70 var rows: i64 = 0
71 while i < n {
72 let e: i64 = sweep_line_end(src, i, n)
73 // ⚠SCANNING-FLAG, NOT A SENTINEL. Writing `st = e + 1` to mean "found" DESTROYS the
74 // position that was the whole point of the scan -- I shipped that bug twice in this one
75 // function before a read-back caught it. A loop that must REPORT WHERE it stopped cannot
76 // use its own cursor as the stop signal.
77 var st: i64 = i
78 var scanning: i64 = 1
79 while scanning == 1 {
80 if st >= e { scanning = 0 }
81 if scanning == 1 { if src[st] == (32 as u8) { st = st + 1 } else { scanning = 0 } }
82 }
83 var skip: i64 = 0
84 if st >= e { skip = 1 }
85 if skip == 0 { if src[st] == (35 as u8) { skip = 1 } }
86 if skip == 0 { if rows >= SWEEP_CONF_MAXROWS { skip = 1 } }
87 if skip == 0 {
88 var q: i64 = st
89 var gl: i64 = 0
90 var stop: i64 = 0
91 while stop == 0 {
92 if q >= e { stop = 1 }
93 if stop == 0 { if src[q] == (32 as u8) { stop = 1 } else {
94 if gl < SWEEP_TOK_CAP - 1 { gbuf[gl] = src[q]; gl = gl + 1 }
95 q = q + 1
96 } }
97 }
98 gbuf[gl] = 0 as u8
99 var sk2: i64 = 1
100 while sk2 == 1 {
101 if q >= e { sk2 = 0 }
102 if sk2 == 1 { if src[q] == (32 as u8) { q = q + 1 } else { sk2 = 0 } }
103 }
104 var sl: i64 = 0
105 while q < e {
106 if src[q] != (13 as u8) { if sl < SWEEP_TOK_CAP - 1 { sbuf[sl] = src[q]; sl = sl + 1 } }
107 q = q + 1
108 }
109 sbuf[sl] = 0 as u8
110 if gl > 0 { if sl > 0 { one(c, rb, rl, gbuf, sbuf); rows = rows + 1 } }
111 }
112 i = e + 1
113 }
114 return rows
115}
116
117func bcat(buf: *u8, at: i64, s: *u8) -> i64 {
118 var i: i64 = 0
119 while s[i] != (0 as u8) { buf[at + i] = s[i]; i = i + 1 }
120 return at + i
121}
122func bcatb(buf: *u8, at: i64, s: *u8, n: i64) -> i64 {
123 var i: i64 = 0
124 while i < n { buf[at + i] = s[i]; i = i + 1 }
125 return at + n
126}
127func bcatn(buf: *u8, at: i64, v: i64) -> i64 {
128 var m: i64 = v
129 let t: *u8 = sys_mmap(32)
130 var k: i64 = 0
131 if m == 0 { t[0] = 48 as u8; k = 1 }
132 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
133 var j: i64 = 0
134 while j < k { buf[at + j] = t[k - 1 - j]; j = j + 1 }
135 return at + k
136}
137
138func extract_ref(b: *u8, n: i64, out: *u8, cap: i64) -> i64 {
139 var at: i64 = 0 - 1
140 if at < 0 { at = evo_find(b, n, "RFC " as *u8) }
141 if at < 0 { at = evo_find(b, n, "RFC7" as *u8) }
142 if at < 0 { at = evo_find(b, n, "FIPS" as *u8) }
143 if at < 0 { at = evo_find(b, n, "NIST" as *u8) }
144 if at < 0 { at = evo_find(b, n, "IEEE" as *u8) }
145 if at < 0 { at = evo_find(b, n, "ISO/IEC" as *u8) }
146 if at < 0 { at = evo_find(b, n, "BIP-39" as *u8) }
147 if at < 0 { at = evo_find(b, n, "BIP39" as *u8) }
148 if at < 0 { at = evo_find(b, n, "Unicode" as *u8) }
149 if at < 0 { at = evo_find(b, n, "canonical reference" as *u8) }
150 if at < 0 { at = evo_find(b, n, "reference vector" as *u8) }
151 if at < 0 { at = evo_find(b, n, "OpenSSL" as *u8) }
152 if at < 0 { return 0 }
153 var k: i64 = 0
154 var p: i64 = at
155 while p < n {
156 if k >= cap - 1 { p = n }
157 else {
158 let c: i64 = b[p] as i64
159 var keep: i64 = 0
160 if c >= 48 { if c <= 57 { keep = 1 } }
161 if c >= 65 { if c <= 90 { keep = 1 } }
162 if c >= 97 { if c <= 122 { keep = 1 } }
163 if c == 46 { keep = 1 }
164 if c == 45 { keep = 1 }
165 if c == 32 { if k > 0 { if k < 9 { keep = 1 } } }
166 if keep == 0 { p = n }
167 else { out[k] = b[p] as u8; k = k + 1; p = p + 1 }
168 }
169 }
170 var done: i64 = 0
171 while done == 0 {
172 if k <= 0 { done = 1 }
173 else { if out[k - 1] == (32 as u8) { k = k - 1 } else { done = 1 } }
174 }
175 out[k] = 0 as u8
176 return k
177}
178
179func run_gate(elf: *u8) -> i64 {
180 let pid: i64 = sys_fork()
181 if pid == 0 {
182 let av: *i64 = sys_mmap(64) as *i64
183 av[0] = elf as i64
184 av[1] = 0
185 let ev: *i64 = sys_mmap(16) as *i64
186 ev[0] = 0
187 sys_execve(elf, av, ev)
188 sys_exit(127)
189 return 127
190 }
191 if pid < 0 { return 0 - 1 }
192 let st: *i64 = sys_mmap(16) as *i64
193 st[0] = 0
194 sys_wait4(pid, st, 0)
195 let raw: i64 = st[0]
196 if (raw & 127) != 0 { return 0 - 2 }
197 return (raw / 256) & 255
198}
199
200func one(c: *i64, rb: *u8, rl: *i64, name: *u8, scope: *u8) -> i64 {
201 // ⚠SUBJECTS LIVE IN TWO DIRECTORIES. This resolver knew only _hdl_build/, so every oracle
202 // subject authored in runtime/ was reported "unreadable subject" -- indistinguishable from a
203 // subject that does not exist. Same class as the compiler's one-way import resolver fixed
204 // earlier today: ★A RESOLVER THAT KNOWS ONE DIRECTORY MISSES EVERY SUBJECT IN THE OTHER, and
205 // reports it as absence. _hdl_build/ is tried FIRST (unchanged behaviour for every existing
206 // subject); runtime/ is a pure ADDITION, so nothing that resolved yesterday resolves differently.
207 let src: *u8 = sys_mmap(256)
208 var so: i64 = bcat(src, 0, "buildroot/runtime/_hdl_build/" as *u8)
209 so = bcat(src, so, name)
210 so = bcat(src, so, ".nx" as *u8)
211 src[so] = 0 as u8
212
213 let elf: *u8 = sys_mmap(256)
214 var eo: i64 = bcat(elf, 0, "buildroot/_build/" as *u8)
215 eo = bcat(elf, eo, name)
216 eo = bcat(elf, eo, ".sov.elf" as *u8)
217 elf[eo] = 0 as u8
218
219 let lp: *i64 = sys_mmap(16) as *i64
220 lp[0] = 0
221 var b: *u8 = sys_read_file(src, lp)
222 if lp[0] <= 0 {
223 let src2: *u8 = sys_mmap(256)
224 var s2: i64 = bcat(src2, 0, "buildroot/runtime/" as *u8)
225 s2 = bcat(src2, s2, name)
226 s2 = bcat(src2, s2, ".nx" as *u8)
227 src2[s2] = 0 as u8
228 lp[0] = 0
229 b = sys_read_file(src2, lp)
230 }
231 if lp[0] <= 0 { c[1] = c[1] + 1; w(" REFUSED " as *u8); w(name); w(" (unreadable subject in BOTH _hdl_build/ and runtime/)\n" as *u8); return 0 }
232
233 let party: i64 = evo_classify(b, lp[0])
234 if party != EVO_P_THIRD {
235 c[1] = c[1] + 1
236 w(" REFUSED " as *u8); w(name)
237 if party == EVO_P_FIRST { w(" party=FIRST (its answers are ours)\n" as *u8) }
238 else { w(" party=UNRESOLVED (no external authority named)\n" as *u8) }
239 return 0
240 }
241
242 let refbuf: *u8 = sys_mmap(64) as *u8
243 let rn: i64 = extract_ref(b, lp[0], refbuf, 40)
244 if rn <= 0 { c[1] = c[1] + 1; w(" REFUSED " as *u8); w(name); w(" (no quotable ref)\n" as *u8); return 0 }
245
246 let ctx: *u8 = sys_mmap(K_MAGIC_1024) as *u8
247 let dig: *u8 = sys_mmap(64) as *u8
248 nx_sha256_one_shot(b, lp[0], ctx, dig)
249 let hx: *u8 = sys_mmap(64) as *u8
250 var i: i64 = 0
251 while i < 8 {
252 hx[i * 2] = hexnib(((dig[i] as i64) / 16) & 15) as u8
253 hx[i * 2 + 1] = hexnib((dig[i] as i64) & 15) as u8
254 i = i + 1
255 }
256
257 let ep: *i64 = sys_mmap(16) as *i64
258 ep[0] = 0
259 let probe: *u8 = sys_read_file(elf, ep)
260 if ep[0] <= 0 {
261 c[2] = c[2] + 1
262 w(" NO-ELF " as *u8); w(name); w(" party=THIRD ref=" as *u8); wb(refbuf, rn)
263 w(" (build it; UNTESTED, not evidence)\n" as *u8)
264 return 0
265 }
266
267 let rc: i64 = run_gate(elf)
268 if rc != 0 {
269 c[3] = c[3] + 1
270 w(" NOT-GREEN " as *u8); w(name); w(" rc=" as *u8); nn(rc); w("\n" as *u8)
271 return 0
272 }
273
274 let ts: *i64 = sys_mmap(32) as *i64
275 ts[0] = 0
276 sys_clock_gettime_real(ts)
277
278 var o: i64 = rl[0]
279 o = bcat(rb, o, "class=oracle verdict=pass scope=" as *u8)
280 o = bcat(rb, o, scope)
281 o = bcat(rb, o, " ref=" as *u8)
282 o = bcatb(rb, o, refbuf, rn)
283 o = bcat(rb, o, " refdig=" as *u8)
284 o = bcatb(rb, o, hx, 16)
285 o = bcat(rb, o, " gate=" as *u8)
286 o = bcat(rb, o, name)
287 o = bcat(rb, o, " host=west_nas signer=nx_evoracle_sweep epoch=" as *u8)
288 o = bcatn(rb, o, ts[0])
289 o = bcat(rb, o, "\n" as *u8)
290 rl[0] = o
291
292 c[0] = c[0] + 1
293 w(" DERIVED " as *u8); w(name); w(" ref=" as *u8); wb(refbuf, rn)
294 w(" refdig=" as *u8); wb(hx, 16); w("\n" as *u8)
295 return 0
296}
297
298func main() -> i64 {
299 let c: *i64 = sys_mmap(64) as *i64
300 c[0] = 0
301 c[1] = 0
302 c[2] = 0
303 c[3] = 0
304 let rb: *u8 = sys_mmap(K_MAGIC_65536)
305 let rl: *i64 = sys_mmap(16) as *i64
306 rl[0] = 0
307
308 w("nx_evoracle_sweep -- derive oracle rows, ON THE HOST THAT SERVES THE CLAIMS\n\n" as *u8)
309 rl[0] = bcat(rb, rl[0], "# knowledge/status/evclass_sovereign.conf -- DERIVED by nx_evoracle_sweep on west_nas.\n" as *u8)
310 rl[0] = bcat(rb, rl[0], "# Each row: subject classified THIRD-party (external authority), authority QUOTED from the\n" as *u8)
311 rl[0] = bcat(rb, rl[0], "# source, sha256 of the exact bytes, THEN the gate forked and required to exit 0.\n" as *u8)
312 rl[0] = bcat(rb, rl[0], "# UNSIGNED ON PURPOSE: at_verify_row refuses these until an operator-held key is registered.\n" as *u8)
313
314 one(c, rb, rl, "nx_sha256_native_kat_gate\x00" as *u8, "sha256-native\x00" as *u8)
315 one(c, rb, rl, "nx_md5_kat\x00" as *u8, "md5\x00" as *u8)
316 one(c, rb, rl, "nx_xxhash_kat\x00" as *u8, "xxhash64\x00" as *u8)
317 one(c, rb, rl, "nx_bip39_kat\x00" as *u8, "bip39-mnemonic\x00" as *u8)
318 one(c, rb, rl, "nx_bip39_wordlist_kat\x00" as *u8, "bip39-wordlist\x00" as *u8)
319 one(c, rb, rl, "nx_x509_pubkey_ed_kat\x00" as *u8, "x509-ed25519-pubkey\x00" as *u8)
320 one(c, rb, rl, "nx_voprf_rfc_kat\x00" as *u8, "voprf\x00" as *u8)
321 one(c, rb, rl, "nx_opaque_rfc_kat\x00" as *u8, "opaque\x00" as *u8)
322 one(c, rb, rl, "nx_x25519_kat_gate\x00" as *u8, "x25519\x00" as *u8)
323 one(c, rb, rl, "nx_lex_kat\x00" as *u8, "lex\x00" as *u8)
324 one(c, rb, rl, "nx_x86_kat_gate\x00" as *u8, "x86-emit\x00" as *u8)
325
326 // ---- DATA-DRIVEN EXTENSION (2026-08-01) ------------------------------------------------
327 // The roster above is HARDCODED, which is exactly the design cause the SOTA rung plan names
328 // for stalled coverage: WHEN EXTENDING COVERAGE COSTS CODE, COVERAGE STOPS AT THE LAST
329 // URGENT THING. Adding an oracle subject required editing and rebuilding this organ, so the
330 // third-party population could only grow when someone was already in this file.
331 // Now it also costs DATA: one line per subject in the conf below, no rebuild.
332 // ADDITIVE BY CONSTRUCTION -- the hardcoded seed above is untouched, so this can add
333 // subjects and can never remove one. Format per line: <gate-name><space><subject>
334 // A missing/empty conf is a NO-OP, not an error (absence of extensions is not a failure).
335 sweep_conf_rows(c, rb, rl, "knowledge/status/evoracle_subjects.conf\x00" as *u8)
336
337 let outp: *u8 = "knowledge/status/evclass_sovereign.conf\x00" as *u8
338 let fd: i64 = sys_openat_wr(outp, 0x1a4)
339 var wrote: i64 = 0
340 if fd >= 0 {
341 var off: i64 = 0
342 while off < rl[0] {
343 let k: i64 = sys_write(fd, ((rb as i64) + off) as *u8, rl[0] - off)
344 if k <= 0 { off = rl[0] } else { off = off + k; wrote = off }
345 }
346 sys_close(fd)
347 }
348
349 w("\n-- SWEEP RESULT (each outcome named separately; none collapsed) --\n" as *u8)
350 w(" DERIVED (third-party + green run) : " as *u8); nn(c[0]); w("\n" as *u8)
351 w(" REFUSED (not third-party) : " as *u8); nn(c[1]); w("\n" as *u8)
352 w(" NO-ELF (untested, NOT evidence) : " as *u8); nn(c[2]); w("\n" as *u8)
353 w(" NOT-GREEN (ran, failed) : " as *u8); nn(c[3]); w("\n" as *u8)
354 w(" evclass_sovereign.conf bytes : " as *u8); nn(wrote); w("\n" as *u8)
355 return 0
356}