code wiki / _hdl_build / nx_pub_reader_receipt_gate.nx

nx_pub_reader_receipt_gate.nx source

↩ module page · 73 lines · 4641 B

1import "nx_gate_gn.nx" 2// nx_pub_reader_receipt_gate.nx -- liar-kill gate: passing the reader/shelf to the publisher yields a SIGNED, 3// VERIFIABLE deployment receipt (operator "get a receipt it deployed"). Self-contained + sovereign (fork/exec): 4// render -> shelf -> handoff (publish) -> reader-receipt (emit+verify signed receipts), then assert the receipts 5// are PUBLISHED, keyed by the real content-sha, live-bytes==ledger (MATCH), and the receipt organ self-reports 6// 4/4 (which it only does when every Ed25519 signature verifies). Teeth: a bogus dest has NO receipt. 7// expect_exit: 0 8import "nx_syscalls.nx" 9 10func gp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 11func gslen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 12func gfind(hay: *u8, hl: i64, needle: *u8) -> i64 { 13 let nl: i64 = gslen(needle); if nl == 0 { return 0-1 } 14 var i: i64 = 0 15 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 i} i=i+1 } 16 return 0-1 17} 18func ghas(hay: *u8, hl: i64, needle: *u8) -> i64 { if gfind(hay, hl, needle) >= 0 { return 1 } return 0 } 19func spawn(path: *u8, a0: *u8, a1: *u8, redir: *u8) -> i64 { 20 let pid: i64 = sys_fork() 21 if pid == 0 { 22 let fd: i64 = sys_openat_wr(redir, 420) 23 if fd >= 0 { sys_dup3(fd, 1, 0); sys_dup3(fd, 2, 0) } 24 let argv: *i64 = sys_mmap(64) as *i64 25 var n: i64 = 0 26 argv[0] = path as i64; n = 1 27 if (a0 as i64) != 0 { argv[n] = a0 as i64; n = n + 1 } 28 if (a1 as i64) != 0 { argv[n] = a1 as i64; n = n + 1 } 29 argv[n] = 0 30 let envp: *i64 = sys_mmap(16) as *i64 31 envp[0] = "PATH=/usr/bin:/bin\x00" as *u8 as i64; envp[1] = 0 32 sys_execve(path, argv, envp) 33 sys_exit(127) 34 } 35 let st: *i64 = sys_mmap(16) as *i64 36 sys_wait4(pid, st, 0) 37 return (st[0] >> 8) & 0xff 38} 39 40func main() -> i64 { 41 gp("=== nx_pub_reader_receipt_gate: signed deployment receipt from the publisher (return-and-report) ===\n" as *u8) 42 let RUNNER: *u8 = "_offc/nx_sov_build_run.elf\x00" as *u8 43 let SC: *u8 = "knowledge/status/pub_reader_receipt_gate_scratch.log\x00" as *u8 44 45 spawn("_offc/nx_epub_book.elf\x00" as *u8, "knowledge/fixtures/nishi_fixture.epub\x00" as *u8, "nishi_fixture\x00" as *u8, SC) 46 spawn(RUNNER, "nx_reader_render\x00" as *u8, 0 as *u8, SC) 47 spawn(RUNNER, "nx_library_shelf\x00" as *u8, 0 as *u8, SC) 48 spawn(RUNNER, "nx_pub_reader_handoff\x00" as *u8, 0 as *u8, SC) 49 let rrc: i64 = spawn(RUNNER, "nx_pub_reader_receipt\x00" as *u8, 0 as *u8, SC) 50 gp(" receipt organ runner-exit=" as *u8); gn(rrc); gp(" (0 => all 4 PUBLISHED + live==ledger + signature verified)\n" as *u8) 51 52 var pass: i64 = 0; var fail: i64 = 0 53 // the receipt organ exits 0 ONLY if every artifact is PUBLISHED, live-bytes==ledger-sha, AND its Ed25519 sig verifies 54 if rrc == 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL receipt-not-4of4\n" as *u8) } 55 56 // durable signed receipts on disk, keyed by content-sha + dest 57 let lp: *i64 = sys_mmap(8) as *i64; lp[0]=0 58 let rc: *u8 = sys_read_file("knowledge/publish/reader-receipts.tsv\x00" as *u8, lp) 59 if (rc as i64) == 0 { gp("PUB-READER-RECEIPT-GATE verdict=RED reason=receipts-missing\n" as *u8); sys_exit(1); return 1 } 60 let n: i64 = lp[0] 61 if ghas(rc, n, "PUBLISHED" as *u8) == 1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL no-PUBLISHED\n" as *u8) } 62 if ghas(rc, n, "library_shelf.html" as *u8) == 1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL no-shelf-receipt\n" as *u8) } 63 if ghas(rc, n, "reader/nishi_fixture/cover.png" as *u8) == 1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL no-cover-receipt\n" as *u8) } 64 if ghas(rc, n, "MATCH" as *u8) == 1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL no-MATCH(live==ledger)\n" as *u8) } 65 // keyed by the REAL content sha (cross-check vs the ledger's shelf sha) 66 if ghas(rc, n, "b2de5dfb8652484e182b42d6b2a24211f662c3c487eeaabd31d64bcad21976dc" as *u8) == 1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL receipt-not-keyed-by-content-sha\n" as *u8) } 67 // teeth: no receipt for a bogus dest 68 if gfind(rc, n, "reader/bogus_xyz/none.png" as *u8) < 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL teeth\n" as *u8) } 69 70 gp("PUB-READER-RECEIPT-GATE pass=" as *u8); gn(pass); gp(" fail=" as *u8); gn(fail) 71 if fail == 0 { gp(" verdict=GREEN (publisher returned signed PUBLISHED receipts; live-bytes==ledger; Ed25519 verified)\n" as *u8); sys_exit(0); return 0 } 72 gp(" verdict=RED\n" as *u8); sys_exit(1); return 1 73}