code wiki / _hdl_build / nx_nmz_native_gate.nx

nx_nmz_native_gate.nx source

↩ module page · 94 lines · 6586 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" 16 17func gp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 18func 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 } 19func 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 } 20func ghas(hay: *u8, hl: i64, needle: *u8) -> i64 { 21 var nl: i64=0; while needle[nl]!=(0 as u8){nl=nl+1} 22 if nl == 0 { return 0 } 23 var i: i64 = 0 24 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 } 25 return 0 26} 27func rd(path: *u8, lenp: *i64) -> *u8 { lenp[0]=0; return sys_read_file(path, lenp) } 28func 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 } 29func spawn(path: *u8, a0: *u8, redir: *u8) -> i64 { 30 let pid: i64 = sys_fork() 31 if pid == 0 { 32 let fd: i64 = sys_openat_wr(redir, 420) 33 if fd >= 0 { sys_dup3(fd, 1, 0); sys_dup3(fd, 2, 0) } 34 let argv: *i64 = sys_mmap(64) as *i64 35 argv[0] = path as i64 36 if (a0 as i64) != 0 { argv[1] = a0 as i64; argv[2] = 0 } else { argv[1] = 0 } 37 let envp: *i64 = sys_mmap(16) as *i64 38 envp[0] = "PATH=/usr/bin:/bin\x00" as *u8 as i64; envp[1] = 0 39 sys_execve(path, argv, envp) 40 sys_exit(127) 41 } 42 let st: *i64 = sys_mmap(16) as *i64 43 sys_wait4(pid, st, 0) 44 return (st[0] >> 8) & 0xff 45} 46 47func main() -> i64 { 48 gp("=== nx_nmz_native_gate: .nmz is a SUPERIOR NATIVE format (not a translation layer) for generated content ===\n" as *u8) 49 let RUNNER: *u8 = "_offc/nx_sov_build_run.elf\x00" as *u8 50 let SC: *u8 = "knowledge/status/nmz_native_scratch.log\x00" as *u8 51 let NMZ: *u8 = "knowledge/staging/media/nmz/nishi_native_demo.nmz\x00" as *u8 52 53 let rc1: i64 = spawn(RUNNER, "nx_nmz_author\x00" as *u8, SC) // native author -> .nmz 54 let l1: *i64 = sys_mmap(8) as *i64; let raw1: *u8 = rd(NMZ, l1); let n1: i64 = l1[0] 55 let buf1: *u8 = cp_bytes(raw1, n1) // snapshot run 1 56 spawn(RUNNER, "nx_nmz_author\x00" as *u8, SC) // author AGAIN (determinism) 57 let l2: *i64 = sys_mmap(8) as *i64; let raw2: *u8 = rd(NMZ, l2); let n2: i64 = l2[0] 58 59 let kindb: *u8 = sys_mmap(64); let gk: i64 = canon_get_bin(buf1, n1, "@kind\x00" as *u8, kindb, 64) 60 let provb: *u8 = sys_mmap(2048); let gv: i64 = canon_get_bin(buf1, n1, "@provenance\x00" as *u8, provb, 2048) 61 let chapb: *u8 = sys_mmap(4096); let gc: i64 = canon_get_bin(buf1, n1, "chap0.txt\x00" as *u8, chapb, 4096) 62 63 // CID before/after a 1-byte tamper (integrity) 64 let cidA: *u8 = sys_mmap(96); uxf_cid_profiled(UXF_ARCHIVE, buf1, n1, cidA) 65 let tam: *u8 = cp_bytes(buf1, n1); if n1 > 0 { tam[n1/2] = (tam[n1/2] ^ 0x01) as u8 } 66 let cidB: *u8 = sys_mmap(96); uxf_cid_profiled(UXF_ARCHIVE, tam, n1, cidB) 67 68 // determinism: byte-identical across two authorings 69 var ident: i64 = 1 70 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 } } } 71 72 gp(" author rc=" as *u8); gnum(rc1); gp(" nmz=" as *u8); gnum(n1); gp("B @kind=" as *u8); if gk>0 { gp(kindb) } 73 gp(" cidA=" as *u8); gp(cidA); gp("\n cidB(tampered)=" as *u8); gp(cidB); gp("\n" as *u8) 74 75 var pass: i64 = 0; var fail: i64 = 0 76 if rc1 == 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL author-nonzero\n" as *u8) } 77 if n1 > 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL no-nmz\n" as *u8) } 78 // NATIVE 79 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) } 80 // PROVENANCE first-class + generator + original-not-translation 81 if gv > 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL no-@provenance (the native superiority)\n" as *u8) } 82 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) } } 83 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) } } 84 // INTEGRITY: tamper changes the address 85 if gstreq(cidA, cidB)==0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL tamper-undetected (cidA==cidB)\n" as *u8) } 86 // DETERMINISM 87 if ident == 1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL not-deterministic\n" as *u8) } 88 // CONTENT round-trips 89 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) } 90 91 gp("NMZ-NATIVE-GATE pass=" as *u8); gnum(pass); gp(" fail=" as *u8); gnum(fail) 92 if fail == 0 { gp(" verdict=GREEN (native authoring + first-class provenance + tamper-evident CID + determinism = a superior native format, not a translation layer)\n" as *u8); sys_exit(0); return 0 } 93 gp(" verdict=RED\n" as *u8); sys_exit(1); return 1 94}