code wiki / _hdl_build / nx_nmz_native_gate.nx

nx_nmz_native_gate.nx source

↩ module page · 102 lines · 6975 B

1// nx_nmz_native_gate.nx -- proves .nmz is a SUPERIOR NATIVE format, not just a translation layer (operator). Authors 2// generated content natively (nx_nmz_author, NO lock-in source) and MEASURES the axes EPUB/MOBI/PDF fundamentally 3// lack, especially for generated content: 4// NATIVE -- @kind set, authored from raw generated text (no decoder of any lock-in format involved). 5// PROVENANCE -- @provenance is a FIRST-CLASS entry readable WITHOUT parsing the manifest; names the generator + 6// proves origin="original-generated" / "not a translation". (Lock-in formats: no native provenance.) 7// INTEGRITY -- flip ONE byte of the .nmz -> the content-address (sha256 CID) CHANGES = tamper-evident. 8// (EPUB/MOBI cannot detect a flipped byte.) 9// DETERMINISM -- author twice -> BYTE-IDENTICAL .nmz -> same CID = reproducible. (zip formats vary by ts/order.) 10// CONTENT -- the generated prose round-trips out of the binary-safe canon. 11// GREEN = people (and we) would choose .nmz natively for generated content because it is genuinely better. 12// expect_exit: 0 license_tier: ORIGINAL 13import "nx_syscalls.nx" 14import "nx_canon_bin.nx" 15import "nx_uxf_cid.nx" 16import "nx_gate_verdict.nx" 17 18func gp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 19func 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 } 20func 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 } 21func ghas(hay: *u8, hl: i64, needle: *u8) -> i64 { 22 var nl: i64=0; while needle[nl]!=(0 as u8){nl=nl+1} 23 if nl == 0 { return 0 } 24 var i: i64 = 0 25 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 } 26 return 0 27} 28func rd(path: *u8, lenp: *i64) -> *u8 { lenp[0]=0; return sys_read_file(path, lenp) } 29func cp_bytes(src: *u8, n: i64) -> *u8 { let d: *u8 = sys_mmap(n + 16); var i: i64=0; while i<n { d[i]=src[i]; i=i+1 } return d } 30func spawn(path: *u8, a0: *u8, redir: *u8) -> i64 { 31 let pid: i64 = sys_fork() 32 if pid == 0 { 33 let fd: i64 = sys_openat_wr(redir, 420) 34 if fd >= 0 { sys_dup3(fd, 1, 0); sys_dup3(fd, 2, 0) } 35 let argv: *i64 = sys_mmap(64) as *i64 36 argv[0] = path as i64 37 if (a0 as i64) != 0 { argv[1] = a0 as i64; argv[2] = 0 } else { argv[1] = 0 } 38 let envp: *i64 = sys_mmap(16) as *i64 39 envp[0] = "PATH=/usr/bin:/bin\x00" as *u8 as i64; envp[1] = 0 40 sys_execve(path, argv, envp) 41 sys_exit(127) 42 } 43 let st: *i64 = sys_mmap(16) as *i64 44 sys_wait4(pid, st, 0) 45 return (st[0] >> 8) & 0xff 46} 47 48func main() -> i64 { 49 gp("=== nx_nmz_native_gate: .nmz is a SUPERIOR NATIVE format (not a translation layer) for generated content ===\n" as *u8) 50 let RUNNER: *u8 = "_offc/nx_sov_build_run.elf\x00" as *u8 51 let SC: *u8 = "knowledge/status/nmz_native_scratch.log\x00" as *u8 52 let NMZ: *u8 = "knowledge/staging/media/nmz/nishi_native_demo.nmz\x00" as *u8 53 54 let rc1: i64 = spawn(RUNNER, "nx_nmz_author\x00" as *u8, SC) // native author -> .nmz 55 let l1: *i64 = sys_mmap(8) as *i64; let raw1: *u8 = rd(NMZ, l1); let n1: i64 = l1[0] 56 let buf1: *u8 = cp_bytes(raw1, n1) // snapshot run 1 57 spawn(RUNNER, "nx_nmz_author\x00" as *u8, SC) // author AGAIN (determinism) 58 let l2: *i64 = sys_mmap(8) as *i64; let raw2: *u8 = rd(NMZ, l2); let n2: i64 = l2[0] 59 60 let kindb: *u8 = sys_mmap(64); let gk: i64 = canon_get_bin(buf1, n1, "@kind\x00" as *u8, kindb, 64) 61 let provb: *u8 = sys_mmap(2048); let gv: i64 = canon_get_bin(buf1, n1, "@provenance\x00" as *u8, provb, 2048) 62 let chapb: *u8 = sys_mmap(4096); let gc: i64 = canon_get_bin(buf1, n1, "chap0.txt\x00" as *u8, chapb, 4096) 63 64 // CID before/after a 1-byte tamper (integrity) 65 let cidA: *u8 = sys_mmap(96); uxf_cid_profiled(UXF_ARCHIVE, buf1, n1, cidA) 66 let tam: *u8 = cp_bytes(buf1, n1); if n1 > 0 { tam[n1/2] = (tam[n1/2] ^ 0x01) as u8 } 67 let cidB: *u8 = sys_mmap(96); uxf_cid_profiled(UXF_ARCHIVE, tam, n1, cidB) 68 69 // determinism: byte-identical across two authorings 70 var ident: i64 = 1 71 if n1 != n2 { ident = 0 } else { var i: i64=0; while i<n1 { if buf1[i]!=raw2[i] { ident=0; i=n1 } else { i=i+1 } } } 72 73 gp(" author rc=" as *u8); gnum(rc1); gp(" nmz=" as *u8); gnum(n1); gp("B @kind=" as *u8); if gk>0 { gp(kindb) } 74 gp(" cidA=" as *u8); gp(cidA); gp("\n cidB(tampered)=" as *u8); gp(cidB); gp("\n" as *u8) 75 76 var pass: i64 = 0; var fail: i64 = 0 77 if rc1 == 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL author-nonzero\n" as *u8) } 78 if n1 > 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL no-nmz\n" as *u8) } 79 // NATIVE 80 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) } 81 // PROVENANCE first-class + generator + original-not-translation 82 if gv > 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL no-@provenance (the native superiority)\n" as *u8) } 83 if gv > 0 { if ghas(provb, gv, "nishi-generative-engine" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL no-generator\n" as *u8) } } 84 if gv > 0 { if ghas(provb, gv, "original-generated" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL not-marked-original\n" as *u8) } } 85 // INTEGRITY: tamper changes the address 86 if gstreq(cidA, cidB)==0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL tamper-undetected (cidA==cidB)\n" as *u8) } 87 // DETERMINISM 88 if ident == 1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL not-deterministic\n" as *u8) } 89 // CONTENT round-trips 90 if gc > 0 { if ghas(chapb, gc, "spore did not ask permission" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL content-missing\n" as *u8) } } else { fail=fail+1; gp(" FAIL no-chap0\n" as *u8) } 91 92 gp("NMZ-NATIVE-GATE pass=" as *u8); gnum(pass); gp(" fail=" as *u8); gnum(fail) 93 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 94 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 95 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 96 let ctr__dry: *i64 = gv_ctr() 97 ctr__dry[0] = pass 98 ctr__dry[1] = pass + fail 99 let rc__dry: i64 = gv_verdict("NMZ-NATIVE-GATE" as *u8, ctr__dry, "native authoring + first-class provenance + tamper-evident CID + determinism = a superior native format, not a translation layer)" as *u8) 100 sys_exit(rc__dry) 101 return rc__dry 102}