code wiki / _hdl_build / nx_library_gate.nx

nx_library_gate.nx source

↩ module page · 70 lines · 3849 B

1// nx_library_gate.nx -- proves the sovereign Nishi library (nx_library): NO LINK ROT (retrieve a document by 2// CID byte-identical, which works even with the source gone), DEDUP (re-ingest same content -> DUP, count 3// unchanged), metadata retrievable, and a liar-kill neg-control (a bogus CID is not found). Uses a stable 4// test document so the gate is idempotent. GREEN iff all checks pass. license_tier: ORIGINAL 5import "nx_syscalls.nx" 6import "nx_library.nx" 7 8const LG_DOC: *u8 = "/tmp/nishi_scilib_test.txt" 9const LG_BODY: *u8 = "NISHI-LIBRARY-TEST-DOC-v1 sovereign no-link-rot corpus.\n" 10 11func lg_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 12func lg_strlen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 13func lg_row(id: *u8, ok: i64, pass: *i64) -> i64 { lg_w(" "); lg_w(id); lg_w(": "); if ok==1 { lg_w("OK\n"); pass[0]=pass[0]+1 } else { lg_w("FAIL\n") } return 0 } 14 15func main() -> i64 { 16 // write the stable test document to disk 17 let blen: i64 = lg_strlen(LG_BODY) 18 let fd: i64 = sys_openat_wr(LG_DOC, 0x1a4) 19 if fd < 0 { lg_w("LIB-GATE abort: cannot write test doc\n"); sys_exit(1); return 1 } 20 sys_write(fd, LG_BODY, blen); sys_close(fd) 21 22 let pass: *i64 = sys_mmap(8) as *i64; pass[0] = 0 23 lg_w("=== NISHI LIBRARY GATE (content-addressed, no-link-rot) ===\n") 24 25 let cid: *u8 = sys_mmap(128) 26 let st1: i64 = lib_ingest(LG_DOC, "test://lib_gate" as *u8, "Library self-test doc" as *u8, "2026-06-20" as *u8, cid) 27 lg_row("ingest returns a CID (NEW or DUP, idempotent)" as *u8, (st1 >= 0) as i64, pass) 28 29 // NO LINK ROT: retrieve the document by CID, byte-identical -- works without the source. 30 let ptr: *i64 = sys_mmap(8) as *i64; let len: *i64 = sys_mmap(8) as *i64 31 let got: i64 = lib_get(cid, ptr, len) 32 var ident: i64 = 0 33 if got == 1 { if len[0] == blen { 34 ident = 1 35 let gb: *u8 = ptr[0] as *u8 36 let body: *u8 = LG_BODY 37 var i: i64 = 0 38 while i < blen { if gb[i] != body[i] { ident = 0 } i = i + 1 } 39 } } 40 lg_row("NO-LINK-ROT: retrieve by CID is byte-identical" as *u8, ident, pass) 41 42 let c1: i64 = lib_count() 43 lg_row("library count >= 1" as *u8, (c1 >= 1) as i64, pass) 44 45 // DEDUP: re-ingest the SAME content -> DUP, count unchanged. 46 let cid2: *u8 = sys_mmap(128) 47 let st2: i64 = lib_ingest(LG_DOC, "test://lib_gate" as *u8, "Library self-test doc" as *u8, "2026-06-20" as *u8, cid2) 48 let c2: i64 = lib_count() 49 var samecid: i64 = 1; var z: i64 = 0 50 while cid[z] != (0 as u8) { if cid2[z] != cid[z] { samecid = 0 } z = z + 1 } 51 lg_row("DEDUP: re-ingest -> DUP (status 0)" as *u8, (st2 == 0) as i64, pass) 52 lg_row("DEDUP: same content -> same CID" as *u8, samecid, pass) 53 lg_row("DEDUP: count unchanged after re-ingest" as *u8, (c2 == c1) as i64, pass) 54 55 // metadata 56 let mv: *u8 = sys_mmap(256) 57 let mok: i64 = lib_meta(cid, "source" as *u8, mv, 256) 58 var msrc: i64 = 0 59 if mok == 1 { msrc = 1; var j: i64 = 0; let want: *u8 = "test://lib_gate" as *u8; while want[j] != (0 as u8) { if mv[j] != want[j] { msrc = 0 } j = j + 1 } } 60 lg_row("metadata: source round-trips" as *u8, msrc, pass) 61 62 // LIAR-KILL: a bogus CID is not found. 63 let bogus: *i64 = sys_mmap(8) as *i64; let blen2: *i64 = sys_mmap(8) as *i64 64 lg_row("LIAR-KILL: bogus CID not found" as *u8, (lib_get("nxc1-deadbeef" as *u8, bogus, blen2) != 1) as i64, pass) 65 66 lg_w("LIB-GATE rows=8 pass=") 67 var m: i64=pass[0]; let t: *u8=sys_mmap(8); var k: i64=0; if m==0{t[0]=48 as u8;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; let ob: *u8=sys_mmap(8); while i<k{ob[i]=t[k-1-i];i=i+1}; sys_write(1,ob,k) 68 if pass[0] == 8 { lg_w(" verdict=GREEN\n"); sys_exit(0); return 0 } 69 lg_w(" verdict=RED\n"); sys_exit(1); return 1 70}