code wiki / _hdl_build / nx_benchmark_ref.nx

nx_benchmark_ref.nx source

↩ module page · 101 lines · 6548 B

1// nx_benchmark_ref.nx -- DURABLE benchmark-reference REGISTRY (operator: "its fine to have our benchmarks but they 2// must exist SEPARATE or you appear to hallucinate and use them"). When a sovereign organ is built clean-room from 3// an external reference (a spec, an OSS algorithm), the reference itself must be MATERIALIZED as a separate, durable, 4// content-addressed artifact -- NOT left in the overwrite-prone srch_latest.raw and NOT merely named in prose (which 5// reads as hallucination). Each reference lands in knowledge/benchmarks/refs/<id>.ref (the real fetched bytes) + 6// <id>.meta (provenance: source_url, content_cid via the sovereign substrate hash, byte count, what it documents, 7// tier=REFERENCE-ONLY). SEPARATION is structural: refs are DATA under knowledge/benchmarks/refs/, sovereign code is 8// .nx under runtime/ and imports ONLY .nx -- no organ imports or reads a ref, so it is provably "learned-from, never 9// used". verify re-hashes the stored bytes and checks they match the recorded cid (real + untampered, not invented). 10// Usage: nx_benchmark_ref register <id> <source_url> <srcfile> <documents> | verify <id> license_tier: ORIGINAL 11import "nx_syscalls.nx" 12import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 13import "nx_uxf_cid.nx" 14const K_MAGIC_1024: i64 = 1024 15 16func br_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 17func br_w(fd: i64, s: *u8) -> i64 { sys_write(fd, s, br_slen(s)); return 0 } 18func br_p(s: *u8) -> i64 { br_w(1, s); return 0 } 19// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 20// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 21// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 22// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 23func br_n(v: i64) -> i64 { nxi_out(v); return 0 } 24func ensure_dir(path: *u8) -> i64 { __syscall(258, 0-100, path, 0x1ed, 0, 0, 0); return 0 } 25func cat(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){dst[off+i]=s[i];i=i+1} return off+i } 26func contains(hay: *u8, hl: i64, needle: *u8) -> i64 { 27 let nl: i64 = br_slen(needle); if nl == 0 { return 0 } 28 var i: i64 = 0 29 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 } 30 return 0 31} 32func refpath(id: *u8, ext: *u8, out: *u8) -> i64 { 33 var o: i64 = cat(out, 0, "knowledge/benchmarks/refs/" as *u8); o = cat(out, o, id); o = cat(out, o, ext); out[o]=0 as u8; return o 34} 35 36// materialize a reference as a durable, content-addressed artifact + provenance. ret 0 ok / negative on error. 37func register_ref(id: *u8, url: *u8, srcfile: *u8, documents: *u8) -> i64 { 38 let lp: *i64 = sys_mmap(8) as *i64; lp[0]=0 39 let bytes: *u8 = sys_read_file(srcfile, lp) 40 if (bytes as i64)==0 { br_p("REF-REGISTER-FAIL read-src " as *u8); br_p(srcfile); br_p("\n" as *u8); return 0-1 } 41 let n: i64 = lp[0] 42 ensure_dir("knowledge/benchmarks" as *u8) 43 ensure_dir("knowledge/benchmarks/refs" as *u8) 44 let cid: *u8 = sys_mmap(96); uxf_cid_profiled(UXF_DATA, bytes, n, cid) 45 46 let rp: *u8 = sys_mmap(K_MAGIC_1024); refpath(id, ".ref" as *u8, rp) 47 let rfd: i64 = sys_openat_wr(rp, 0x1a4) 48 if rfd < 0 { br_p("REF-REGISTER-FAIL open-ref\n" as *u8); return 0-1 } 49 sys_write(rfd, bytes, n); sys_close(rfd) 50 51 let mp: *u8 = sys_mmap(K_MAGIC_1024); refpath(id, ".meta" as *u8, mp) 52 let mfd: i64 = sys_openat_wr(mp, 0x1a4) 53 if mfd < 0 { br_p("REF-REGISTER-FAIL open-meta\n" as *u8); return 0-1 } 54 br_w(mfd, "Nishi benchmark reference artifact -- REFERENCE ONLY, NOT A DEPENDENCY.\n" as *u8) 55 br_w(mfd, "No sovereign organ imports or reads this file; it is learned-from (clean-room), never used.\n" as *u8) 56 br_w(mfd, "id: " as *u8); br_w(mfd, id); br_w(mfd, "\n" as *u8) 57 br_w(mfd, "source_url: " as *u8); br_w(mfd, url); br_w(mfd, "\n" as *u8) 58 br_w(mfd, "content_cid: " as *u8); br_w(mfd, cid); br_w(mfd, "\n" as *u8) 59 br_w(mfd, "bytes: " as *u8); let nb: *u8=sys_mmap(24); var m: i64=n; var k: i64=0; if m==0{nb[0]=48 as u8;k=1} while m>0{nb[k]=(48+(m%10)) as u8;m=m/10;k=k+1} let ob: *u8=sys_mmap(24); var j: i64=0; while j<k{ob[j]=nb[k-1-j];j=j+1} sys_write(mfd,ob,k); br_w(mfd, "\n" as *u8) 60 br_w(mfd, "documents: " as *u8); br_w(mfd, documents); br_w(mfd, "\n" as *u8) 61 br_w(mfd, "tier: REFERENCE-ONLY\n" as *u8) 62 br_w(mfd, "relation: learned-from-spec, clean-room reimplemented in sovereign NishiLang\n" as *u8) 63 sys_close(mfd) 64 65 br_p("REF-REGISTER id=" as *u8); br_p(id); br_p(" bytes=" as *u8); br_n(n); br_p(" cid=" as *u8); br_p(cid); br_p(" -> " as *u8); br_p(rp); br_p("\n" as *u8) 66 return 0 67} 68 69// verify a stored reference: re-hash its bytes, confirm the cid matches what the meta recorded. 1 real / 0 mismatch / -1 missing. 70func verify_ref(id: *u8) -> i64 { 71 let rp: *u8 = sys_mmap(K_MAGIC_1024); refpath(id, ".ref" as *u8, rp) 72 let lp: *i64 = sys_mmap(8) as *i64; lp[0]=0 73 let bytes: *u8 = sys_read_file(rp, lp) 74 if (bytes as i64)==0 { return 0-1 } 75 let n: i64 = lp[0] 76 let cid: *u8 = sys_mmap(96); uxf_cid_profiled(UXF_DATA, bytes, n, cid) 77 let mp: *u8 = sys_mmap(K_MAGIC_1024); refpath(id, ".meta" as *u8, mp) 78 let lm: *i64 = sys_mmap(8) as *i64; lm[0]=0 79 let meta: *u8 = sys_read_file(mp, lm) 80 if (meta as i64)==0 { return 0-1 } 81 if contains(meta, lm[0], cid) == 1 { return 1 } 82 return 0 83} 84 85func streq(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 } 86 87func main(argc: i64, argv: *i64) -> i64 { 88 if argc >= 6 { if streq(argv[1] as *u8, "register" as *u8)==1 { 89 let r: i64 = register_ref(argv[2] as *u8, argv[3] as *u8, argv[4] as *u8, argv[5] as *u8) 90 if r == 0 { sys_exit(0); return 0 } sys_exit(1); return 1 91 } } 92 if argc >= 3 { if streq(argv[1] as *u8, "verify" as *u8)==1 { 93 let v: i64 = verify_ref(argv[2] as *u8) 94 br_p("REF-VERIFY id=" as *u8); br_p(argv[2] as *u8); br_p(" result=" as *u8) 95 if v==1 { br_p("REAL (cid matches stored bytes)\n" as *u8); sys_exit(0); return 0 } 96 if v==0 { br_p("TAMPERED (cid mismatch)\n" as *u8); sys_exit(1); return 1 } 97 br_p("MISSING\n" as *u8); sys_exit(2); return 2 98 } } 99 br_p("usage: nx_benchmark_ref register <id> <source_url> <srcfile> <documents> | verify <id>\n" as *u8) 100 sys_exit(0); return 0 101}