code wiki / _hdl_build / nx_nmz_gate.nx
nx_nmz_gate.nx source
↩ module page · 114 lines · 7693 B
1// nx_nmz_gate.nx -- liar-kill gate for the OMNI Nishi Media container (.nmz), built ON the sovereign substrate
2// (audit fix: REUSE nx_canon_cid NXR1 + nx_uxf_cid multicodec CID, NOT a duplicate). Asserts:
3// SUBSTRATE-REUSE: the content address is the REAL profiled CID "nxc1-05-"+sha256 (codec UXF_ARCHIVE readable
4// back via uxf_codec_of_cid) -- NOT a private djb2; proves we built ON the existing format.
5// KIND + ENTRIES : @kind="book"; book.json round-trips out of the binary-safe NXR1 record (title intact).
6// CONTENT-ADDR : flipping ONE byte CHANGES the CID (content-addressing detects tamper, like canon_cid).
7// DETERMINISM : packing twice -> byte-identical .nmz + identical CID.
8// CONVERGE : a MOBI book ALSO packs to .nmz @kind=book (one omni format, many decoders feed it).
9// GREEN = books/media migrate into the EXISTING owned format (UXF), reusing its sha256 multicodec CID. expect_exit:0
10import "nx_syscalls.nx"
11import "nx_canon_bin.nx"
12import "nx_uxf_cid.nx"
13import "nx_gate_verdict.nx"
14
15func gp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
16func gnum(v0: i64) -> i64 { var v: i64=v0; if v<0 { sys_write(1,"-" as *u8,1); v=0-v } let b: *u8=sys_mmap(24); var k: i64=0; if v==0 {b[0]=48 as u8;k=1} while v>0 {b[k]=(48+(v%10)) as u8; v=v/10; k=k+1} let o: *u8=sys_mmap(24); var j: i64=0; while j<k {o[j]=b[k-1-j];j=j+1} sys_write(1,o,k); return 0 }
17func gslen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
18
19func spawn(path: *u8, a0: *u8, a1: *u8, redir: *u8) -> i64 {
20 let pid: i64 = sys_fork()
21 if pid == 0 {
22 let fd: i64 = sys_openat_wr(redir, 420)
23 if fd >= 0 { sys_dup3(fd, 1, 0); sys_dup3(fd, 2, 0) }
24 let argv: *i64 = sys_mmap(64) as *i64
25 var n: i64 = 0
26 argv[0] = path as i64; n = 1
27 if (a0 as i64) != 0 { argv[n] = a0 as i64; n = n + 1 }
28 if (a1 as i64) != 0 { argv[n] = a1 as i64; n = n + 1 }
29 argv[n] = 0
30 let envp: *i64 = sys_mmap(16) as *i64
31 envp[0] = "PATH=/usr/bin:/bin\x00" as *u8 as i64; envp[1] = 0
32 sys_execve(path, argv, envp)
33 sys_exit(127)
34 }
35 let st: *i64 = sys_mmap(16) as *i64
36 sys_wait4(pid, st, 0)
37 return (st[0] >> 8) & 0xff
38}
39func gstreq(a: *u8, b: *u8) -> i64 { var i: i64=0; while a[i]!=(0 as u8){ if a[i]!=b[i]{return 0} i=i+1 } if b[i]!=(0 as u8){return 0} return 1 }
40func ghas(hay: *u8, hl: i64, needle: *u8) -> i64 {
41 var nl: i64=0; while needle[nl]!=(0 as u8){nl=nl+1}
42 if nl == 0 { return 0 }
43 var i: i64 = 0
44 while i + nl <= hl { var k: i64=0; var hit: i64=1; while k<nl { if hay[i+k]!=needle[k]{hit=0;k=nl}else{k=k+1} } if hit==1 {return 1} i=i+1 }
45 return 0
46}
47
48func main() -> i64 {
49 gp("=== nx_nmz_gate: OMNI Nishi Media (.nmz) on the sovereign substrate (REUSE canon_cid+uxf_cid, not a dup) ===\n" as *u8)
50 let RUNNER: *u8 = "_offc/nx_sov_build_run.elf\x00" as *u8
51 let SCRATCH: *u8 = "knowledge/status/nmz_gate_scratch.log\x00" as *u8
52
53 spawn(RUNNER, "nx_epub_fixture_build\x00" as *u8, 0 as *u8, SCRATCH)
54 spawn("_offc/nx_epub_book.elf\x00" as *u8, "knowledge/fixtures/nishi_fixture.epub\x00" as *u8, "nishi_fixture\x00" as *u8, SCRATCH)
55 spawn(RUNNER, "nx_nmz_pack\x00" as *u8, 0 as *u8, SCRATCH) // refresh _offc twin + pack nishi_fixture (kind=book)
56 let l1: *i64 = sys_mmap(8) as *i64; l1[0]=0
57 let m1: *u8 = sys_read_file("knowledge/staging/media/nmz/nishi_fixture.nmz\x00" as *u8, l1)
58 let n1: i64 = l1[0]
59 spawn(RUNNER, "nx_nmz_pack\x00" as *u8, 0 as *u8, SCRATCH) // pack again (determinism)
60 let l2: *i64 = sys_mmap(8) as *i64; l2[0]=0
61 let m2: *u8 = sys_read_file("knowledge/staging/media/nmz/nishi_fixture.nmz\x00" as *u8, l2)
62 let n2: i64 = l2[0]
63
64 // MOBI convergence
65 spawn(RUNNER, "nx_mobi_fixture_build\x00" as *u8, 0 as *u8, SCRATCH)
66 spawn(RUNNER, "nx_mobi_book\x00" as *u8, 0 as *u8, SCRATCH)
67 spawn("_offc/nx_nmz_pack.elf\x00" as *u8, "nishi_mobi_fixture\x00" as *u8, "book\x00" as *u8, SCRATCH)
68 let lm: *i64 = sys_mmap(8) as *i64; lm[0]=0
69 let mm: *u8 = sys_read_file("knowledge/staging/media/nmz/nishi_mobi_fixture.nmz\x00" as *u8, lm)
70 let nmm: i64 = lm[0]
71
72 if (m1 as i64)==0 { gp("NMZ-GATE verdict=RED reason=epub-nmz-missing\n" as *u8); sys_exit(1); return 1 }
73
74 let kindb: *u8 = sys_mmap(64); let gk: i64 = canon_get_bin(m1, n1, "@kind\x00" as *u8, kindb, 64)
75 let bjb: *u8 = sys_mmap(1048576); let gb: i64 = canon_get_bin(m1, n1, "book.json\x00" as *u8, bjb, 1048576)
76 let cid1: *u8 = sys_mmap(96); uxf_cid_profiled(UXF_ARCHIVE, m1, n1, cid1)
77 let codec: i64 = uxf_codec_of_cid(cid1)
78 // determinism
79 var det: i64 = 1
80 if n1 != n2 { det = 0 } else { var i: i64=0; while i<n1 { if m1[i]!=m2[i] { det=0; i=n1 } else { i=i+1 } } }
81 let cid2: *u8 = sys_mmap(96); uxf_cid_profiled(UXF_ARCHIVE, m2, n2, cid2)
82 let det_cid: i64 = gstreq(cid1, cid2)
83 // tamper: flip a byte -> CID must CHANGE (content-addressing)
84 if n1 > 20 { m1[20] = (m1[20] ^ (1 as u8)) as u8 }
85 let cidt: *u8 = sys_mmap(96); uxf_cid_profiled(UXF_ARCHIVE, m1, n1, cidt)
86 var tamper_changed: i64 = 0
87 if gstreq(cid1, cidt) == 0 { tamper_changed = 1 }
88
89 gp(" cid=" as *u8); gp(cid1); gp("\n @kind=" as *u8); if gk>0 { gp(kindb) }
90 gp(" codec=" as *u8); gnum(codec); gp(" deterministic=" as *u8); gnum(det); gp(" same_cid=" as *u8); gnum(det_cid)
91 gp(" tamper_changes_cid=" as *u8); gnum(tamper_changed); gp(" mobi.nmz_bytes=" as *u8); gnum(nmm); gp("\n" as *u8)
92
93 var pass: i64 = 0; var fail: i64 = 0
94 if codec == UXF_ARCHIVE { pass=pass+1 } else { fail=fail+1; gp(" FAIL cid-codec-not-UXF_ARCHIVE (not the substrate CID)\n" as *u8) }
95 if ghas(cid1, 8, "nxc1-05-" as *u8) == 1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL cid-not-profiled-form\n" as *u8) }
96 if gk > 0 { if gstreq(kindb, "book" as *u8) == 1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL kind!=book\n" as *u8) } } else { fail=fail+1; gp(" FAIL no-@kind\n" as *u8) }
97 if gb > 0 { if ghas(bjb, gb, "Alice (Nishi Fixture)" as *u8) == 1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL book.json-not-round-tripped\n" as *u8) } } else { fail=fail+1; gp(" FAIL no-book.json-entry\n" as *u8) }
98 if det == 1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL not-byte-deterministic\n" as *u8) }
99 if det_cid == 1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL cid-not-deterministic\n" as *u8) }
100 if tamper_changed == 1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL tamper-did-not-change-cid (content-addressing fake)\n" as *u8) }
101 // MOBI convergence
102 if (mm as i64) != 0 { let mk: *u8 = sys_mmap(64); let mg: i64 = canon_get_bin(mm, nmm, "@kind\x00" as *u8, mk, 64); if mg > 0 { if gstreq(mk, "book" as *u8) == 1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL mobi-kind\n" as *u8) } } else { fail=fail+1; gp(" FAIL mobi-no-kind\n" as *u8) } } else { fail=fail+1; gp(" FAIL mobi-nmz-missing\n" as *u8) }
103
104 gp("NMZ-GATE pass=" as *u8); gnum(pass); gp(" fail=" as *u8); gnum(fail)
105 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
106 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
107 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
108 let ctr__dry: *i64 = gv_ctr()
109 ctr__dry[0] = pass
110 ctr__dry[1] = pass + fail
111 let rc__dry: i64 = gv_verdict("NMZ-GATE" as *u8, ctr__dry, "EPUB+MOBI -> .nmz on the EXISTING UXF substrate: sha256 multicodec CID, kind-tagged, content-addressed, deterministic)" as *u8)
112 sys_exit(rc__dry)
113 return rc__dry
114}