code wiki / _hdl_build / nx_pub_receipt_gate.nx
nx_pub_receipt_gate.nx source
↩ module page · 78 lines · 6714 B
1// nx_pub_receipt_gate.nx -- referee for the SOVEREIGN-STORED publish receipt system (nx_pub_receipt on nx_seg_store +
2// canon records, NO tsv). Proves the research-grounded breadth: ACK/NACK, DSN states, return-receipt query, dead-
3// letter, idempotence, status transition (latest-wins in the additive store), no-false-published for an unknown
4// artifact, provenance (the stored canon record carries the content sha), and non-repudiation (genuine receipt
5// verifies; forged key + a tampered binding fail). -> knowledge/status/pub_receipt_gate.log.
6import "nx_pub_receipt.nx"
7import "nx_ed25519_signature.nx"
8import "nx_syscalls.nx"
9
10func g_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
11func g_n(v: i64) -> i64 { var m: i64=v; if m<0{g_w("-" as *u8);m=0-m} let t:*u8=sys_mmap(24); 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 o:*u8=sys_mmap(24); while i<k{o[i]=t[k-1-i];i=i+1} sys_write(1,o,k); return 0 }
12func gw(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 }
13func g_cat(dst: *u8, off: i64, s: *u8) -> i64 { var o: i64=off; var i: i64=0; while s[i]!=(0 as u8){dst[o]=s[i];o=o+1;i=i+1} return o }
14func chk(got: i64, want: i64, label: *u8) -> i64 { g_w(" "); g_w(label); g_w(" got="); g_n(got); g_w(" want="); g_n(want); if got==want { g_w(" PASS\n"); return 1 } g_w(" FAIL\n"); return 0 }
15
16func main() -> i64 {
17 g_w("=== NISHI PUBLISH-RECEIPT GATE (sovereign seg_store + canon records; no tsv; signed non-repudiation) ===\n" as *u8)
18 var pass: i64=0; var rows: i64=0
19 // fresh per-run seg_store prefix (now_ms-suffixed; pids get REUSED -> stale): empty store -> clean, re-runnable
20 let pid: i64=sys_now_ms()
21 let pfx: *u8=sys_mmap(128); var po: i64=g_cat(pfx,0,"/tmp/nx_rcptst_" as *u8); let nb: *u8=sys_mmap(24)
22 var m: i64=pid; 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} var z: i64=k-1; while z>=0{pfx[po]=nb[z];po=po+1;z=z-1} pfx[po]=0 as u8
23 __syscall(83, pfx as i64, 493, 0, 0, 0, 0) // mkdir 0755
24
25 let priv: *u8=sys_mmap(32); var i: i64=0; while i<32 { priv[i]=(i+1) as u8; i=i+1 }
26 let pub: *u8=sys_mmap(32); ed25519_pub_from_priv(priv, pub)
27 let priv2: *u8=sys_mmap(32); var j: i64=0; while j<32 { priv2[j]=(99+j) as u8; j=j+1 }
28 let pub2: *u8=sys_mmap(32); ed25519_pub_from_priv(priv2, pub2)
29
30 let req: *u8="acct-admin-32992a14" as *u8; let site: *u8="nishifamily.com" as *u8
31 let s1: *u8="deadbeefcafe0001" as *u8; let d1: *u8="/nas/nishi_hr.log" as *u8
32 let s2: *u8="deadbeefcafe0002" as *u8; let d2: *u8="/nas/andelin.log" as *u8
33 let s3: *u8="deadbeefcafe0003" as *u8; let d3: *u8="/nas/broken.bin" as *u8
34 let s4: *u8="deadbeefcafe0004" as *u8; let d4: *u8="/nas/transition" as *u8
35 let s5: *u8="deadbeefcafe0005" as *u8; let d5: *u8="/nas/denied" as *u8
36
37 rows=rows+1; pass=pass+chk(rcpt_emit(pfx, priv, "PUBLISHED" as *u8, req, s1, d1, site, "MATCH" as *u8, 1000, 1), 1, "emit PUBLISHED -> 1" as *u8)
38 rcpt_emit(pfx, priv, "HELD" as *u8, req, s2, d2, site, "NONE" as *u8, 1001, 2)
39 rcpt_emit(pfx, priv, "FAILED" as *u8, req, s3, d3, site, "MISMATCH" as *u8, 1002, 3)
40 rcpt_emit(pfx, priv, "REJECTED" as *u8, req, s5, d5, site, "NONE" as *u8, 1003, 4)
41 rcpt_emit(pfx, priv, "HELD" as *u8, req, s4, d4, site, "NONE" as *u8, 1004, 5) // transition step 1
42 rcpt_emit(pfx, priv, "PUBLISHED" as *u8, req, s4, d4, site, "MATCH" as *u8, 1005, 6) // transition step 2
43
44 rows=rows+1; pass=pass+chk(rcpt_is_published(pfx, s1, d1), 1, "ACK: PUBLISHED receipt -> is_published" as *u8)
45 rows=rows+1; pass=pass+chk(rcpt_status_code(pfx, s2, d2), RC_HELD, "DSN: HELD state" as *u8)
46 rows=rows+1; pass=pass+chk(rcpt_status_code(pfx, s3, d3), RC_FAILED, "dead-letter: FAILED receipt (not dropped)" as *u8)
47 rows=rows+1; pass=pass+chk(rcpt_status_code(pfx, s5, d5), RC_REJECTED, "NACK: REJECTED receipt" as *u8)
48 rows=rows+1; pass=pass+chk(rcpt_status_code(pfx, s4, d4), RC_PUBLISHED, "transition HELD->PUBLISHED: latest wins (additive store)" as *u8)
49 rows=rows+1; pass=pass+chk(rcpt_emit(pfx, priv, "PUBLISHED" as *u8, req, s1, d1, site, "MATCH" as *u8, 1006, 7), 0, "idempotence: re-emit same state -> skip (0)" as *u8)
50 rows=rows+1; pass=pass+chk(rcpt_is_published(pfx, "deadbeefcafe9999" as *u8, "/nas/none" as *u8), 0, "no receipt for unknown artifact -> NOT published" as *u8)
51
52 // provenance: the stored canon record carries the content sha
53 let ok: *i64=sys_mmap(64*8) as *i64; let ov: *i64=sys_mmap(64*8) as *i64
54 let nf: i64=rcpt_get(pfx, s1, d1, ok, ov, 64)
55 let shafield: *u8=rcpt_field(ok, ov, nf, "sha" as *u8)
56 var provok: i64=0; if (shafield as i64)!=0 { provok=rc_streq(shafield, s1) }
57 rows=rows+1; pass=pass+chk(provok, 1, "provenance: stored canon record carries the content sha" as *u8)
58
59 // non-repudiation
60 rows=rows+1; pass=pass+chk(rcpt_verify(pfx, pub, s1, d1), 1, "non-repudiation: genuine receipt verifies w/ publisher pubkey" as *u8)
61 rows=rows+1; pass=pass+chk(rcpt_verify(pfx, pub2, s1, d1), 0, "forged key: genuine receipt does NOT verify under a different pubkey" as *u8)
62
63 // tamper-detection at the binding level: sign verify=MATCH, verify a verify=MISMATCH binding -> 0
64 let tss: *u8=sys_mmap(8); tss[0]=49 as u8; tss[1]=0 as u8 // "1"
65 let sighex: *u8=sys_mmap(160); rc_sign(priv, "PUBLISHED" as *u8, s1, d1, "MATCH" as *u8, tss, sighex)
66 let sig: *u8=sys_mmap(64); rc_unhex(sighex, 64, sig)
67 let bm: *u8=sys_mmap(2048); let nm: i64=rc_bind(bm, "PUBLISHED" as *u8, s1, d1, "MATCH" as *u8, tss)
68 rows=rows+1; pass=pass+chk(ed25519_verify_full(pub, bm, nm, sig), 1, " (genuine binding verifies)" as *u8)
69 let bt: *u8=sys_mmap(2048); let nt: i64=rc_bind(bt, "PUBLISHED" as *u8, s1, d1, "MISMATCH" as *u8, tss)
70 rows=rows+1; pass=pass+chk(ed25519_verify_full(pub, bt, nt, sig), 0, "tampered binding (verify field altered) FAILS verify" as *u8)
71
72 g_w("----\nNISHI-PUB-RECEIPT-GATE rows=" as *u8); g_n(rows); g_w(" pass=" as *u8); g_n(pass)
73 if pass==rows { g_w(" verdict=GREEN\n" as *u8) } else { g_w(" verdict=RED\n" as *u8) }
74 let lg: i64=sys_openat_append("knowledge/status/pub_receipt_gate.log" as *u8, 0x1a4)
75 if lg>=0 { gw(lg, "NISHI-PUB-RECEIPT-GATE pass=" as *u8); let bb:*u8=sys_mmap(8); var mm:i64=pass; var kk:i64=0; if mm==0{bb[0]=48 as u8;kk=1} while mm>0{bb[kk]=(48+(mm%10)) as u8;mm=mm/10;kk=kk+1} sys_write(lg,bb,kk); if pass==rows{gw(lg," verdict=GREEN\n" as *u8)}else{gw(lg," verdict=RED\n" as *u8)} sys_close(lg) }
76 if pass==rows { sys_exit(0); return 0 }
77 sys_exit(1); return 1
78}