code wiki / _hdl_build / nx_nmz_gate.nx
nx_nmz_gate.nx source
↩ module page · 106 lines · 7311 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"
13
14func gp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
15func 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 }
16func gslen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
17
18func spawn(path: *u8, a0: *u8, a1: *u8, redir: *u8) -> i64 {
19 let pid: i64 = sys_fork()
20 if pid == 0 {
21 let fd: i64 = sys_openat_wr(redir, 420)
22 if fd >= 0 { sys_dup3(fd, 1, 0); sys_dup3(fd, 2, 0) }
23 let argv: *i64 = sys_mmap(64) as *i64
24 var n: i64 = 0
25 argv[0] = path as i64; n = 1
26 if (a0 as i64) != 0 { argv[n] = a0 as i64; n = n + 1 }
27 if (a1 as i64) != 0 { argv[n] = a1 as i64; n = n + 1 }
28 argv[n] = 0
29 let envp: *i64 = sys_mmap(16) as *i64
30 envp[0] = "PATH=/usr/bin:/bin\x00" as *u8 as i64; envp[1] = 0
31 sys_execve(path, argv, envp)
32 sys_exit(127)
33 }
34 let st: *i64 = sys_mmap(16) as *i64
35 sys_wait4(pid, st, 0)
36 return (st[0] >> 8) & 0xff
37}
38func 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 }
39func ghas(hay: *u8, hl: i64, needle: *u8) -> i64 {
40 var nl: i64=0; while needle[nl]!=(0 as u8){nl=nl+1}
41 if nl == 0 { return 0 }
42 var i: i64 = 0
43 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 }
44 return 0
45}
46
47func main() -> i64 {
48 gp("=== nx_nmz_gate: OMNI Nishi Media (.nmz) on the sovereign substrate (REUSE canon_cid+uxf_cid, not a dup) ===\n" as *u8)
49 let RUNNER: *u8 = "_offc/nx_sov_build_run.elf\x00" as *u8
50 let SCRATCH: *u8 = "knowledge/status/nmz_gate_scratch.log\x00" as *u8
51
52 spawn(RUNNER, "nx_epub_fixture_build\x00" as *u8, 0 as *u8, SCRATCH)
53 spawn("_offc/nx_epub_book.elf\x00" as *u8, "knowledge/fixtures/nishi_fixture.epub\x00" as *u8, "nishi_fixture\x00" as *u8, SCRATCH)
54 spawn(RUNNER, "nx_nmz_pack\x00" as *u8, 0 as *u8, SCRATCH) // refresh _offc twin + pack nishi_fixture (kind=book)
55 let l1: *i64 = sys_mmap(8) as *i64; l1[0]=0
56 let m1: *u8 = sys_read_file("knowledge/staging/media/nmz/nishi_fixture.nmz\x00" as *u8, l1)
57 let n1: i64 = l1[0]
58 spawn(RUNNER, "nx_nmz_pack\x00" as *u8, 0 as *u8, SCRATCH) // pack again (determinism)
59 let l2: *i64 = sys_mmap(8) as *i64; l2[0]=0
60 let m2: *u8 = sys_read_file("knowledge/staging/media/nmz/nishi_fixture.nmz\x00" as *u8, l2)
61 let n2: i64 = l2[0]
62
63 // MOBI convergence
64 spawn(RUNNER, "nx_mobi_fixture_build\x00" as *u8, 0 as *u8, SCRATCH)
65 spawn(RUNNER, "nx_mobi_book\x00" as *u8, 0 as *u8, SCRATCH)
66 spawn("_offc/nx_nmz_pack.elf\x00" as *u8, "nishi_mobi_fixture\x00" as *u8, "book\x00" as *u8, SCRATCH)
67 let lm: *i64 = sys_mmap(8) as *i64; lm[0]=0
68 let mm: *u8 = sys_read_file("knowledge/staging/media/nmz/nishi_mobi_fixture.nmz\x00" as *u8, lm)
69 let nmm: i64 = lm[0]
70
71 if (m1 as i64)==0 { gp("NMZ-GATE verdict=RED reason=epub-nmz-missing\n" as *u8); sys_exit(1); return 1 }
72
73 let kindb: *u8 = sys_mmap(64); let gk: i64 = canon_get_bin(m1, n1, "@kind\x00" as *u8, kindb, 64)
74 let bjb: *u8 = sys_mmap(1048576); let gb: i64 = canon_get_bin(m1, n1, "book.json\x00" as *u8, bjb, 1048576)
75 let cid1: *u8 = sys_mmap(96); uxf_cid_profiled(UXF_ARCHIVE, m1, n1, cid1)
76 let codec: i64 = uxf_codec_of_cid(cid1)
77 // determinism
78 var det: i64 = 1
79 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 } } }
80 let cid2: *u8 = sys_mmap(96); uxf_cid_profiled(UXF_ARCHIVE, m2, n2, cid2)
81 let det_cid: i64 = gstreq(cid1, cid2)
82 // tamper: flip a byte -> CID must CHANGE (content-addressing)
83 if n1 > 20 { m1[20] = (m1[20] ^ (1 as u8)) as u8 }
84 let cidt: *u8 = sys_mmap(96); uxf_cid_profiled(UXF_ARCHIVE, m1, n1, cidt)
85 var tamper_changed: i64 = 0
86 if gstreq(cid1, cidt) == 0 { tamper_changed = 1 }
87
88 gp(" cid=" as *u8); gp(cid1); gp("\n @kind=" as *u8); if gk>0 { gp(kindb) }
89 gp(" codec=" as *u8); gnum(codec); gp(" deterministic=" as *u8); gnum(det); gp(" same_cid=" as *u8); gnum(det_cid)
90 gp(" tamper_changes_cid=" as *u8); gnum(tamper_changed); gp(" mobi.nmz_bytes=" as *u8); gnum(nmm); gp("\n" as *u8)
91
92 var pass: i64 = 0; var fail: i64 = 0
93 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) }
94 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) }
95 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) }
96 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) }
97 if det == 1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL not-byte-deterministic\n" as *u8) }
98 if det_cid == 1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL cid-not-deterministic\n" as *u8) }
99 if tamper_changed == 1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL tamper-did-not-change-cid (content-addressing fake)\n" as *u8) }
100 // MOBI convergence
101 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) }
102
103 gp("NMZ-GATE pass=" as *u8); gnum(pass); gp(" fail=" as *u8); gnum(fail)
104 if fail == 0 { gp(" verdict=GREEN (EPUB+MOBI -> .nmz on the EXISTING UXF substrate: sha256 multicodec CID, kind-tagged, content-addressed, deterministic)\n" as *u8); sys_exit(0); return 0 }
105 gp(" verdict=RED\n" as *u8); sys_exit(1); return 1
106}