code wiki / _hdl_build / nx_dr_dedup_gate.nx
nx_dr_dedup_gate.nx source
↩ module page · 39 lines · 2830 B
1// nx_dr_dedup_gate.nx -- exceed-gate for CAP-DR-DEDUP. Proves content-addressing: identical content -> identical
2// hash (so it dedups to one blob), distinct content -> distinct hash (stored separately), sha256 KAT deterministic.
3// Sovereign: nx_syscalls + nx_sha256. expect_exit: 0
4import "nx_syscalls.nx"
5import "nx_sha256.nx"
6import "nx_gate_verdict.nx"
7
8func gp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
9func gn(v: i64) -> i64 { var t: i64=v; if t<0{sys_write(1,"-" as *u8,1);t=0-t} let tm:*u8=sys_mmap(24); var k:i64=0; if t==0{tm[0]=48 as u8;k=1} while t>0{tm[k]=(48+(t%10)) as u8;t=t/10;k=k+1} let b:*u8=sys_mmap(24); var j:i64=0; while j<k{b[j]=tm[k-1-j];j=j+1} sys_write(1,b,k); return 0 }
10func g_hex(buf: *u8, n: i64, out: *u8) -> i64 {
11 let dig: *u8 = sys_mmap(64); sha256_digest(buf, n, dig)
12 let hx: *u8 = "0123456789abcdef" as *u8
13 var h: i64=0; while h<32 { let by: i64=(dig[h] as i64)&0xff; out[h*2]=hx[(by>>4)&15]; out[h*2+1]=hx[by&15]; h=h+1 } out[64]=0 as u8; return 0
14}
15func g_eq(a: *u8, b: *u8, n: i64) -> i64 { var i: i64=0; while i<n { if a[i]!=b[i] { return 0 } i=i+1 } return 1 }
16
17func main(argc: i64, argv: *i64) -> i64 {
18 gp("=== nx_dr_dedup_gate (content-addressing: identical->one blob, distinct->separate) ===\n" as *u8)
19 var pass: i64=0; var fail: i64=0
20 let hA: *u8=sys_mmap(80); let hB: *u8=sys_mmap(80); let hC: *u8=sys_mmap(80)
21 g_hex("account data block v1" as *u8, 21, hA)
22 g_hex("account data block v1" as *u8, 21, hB) // identical bytes
23 g_hex("account data block v2" as *u8, 21, hC) // one byte different
24 if g_eq(hA, hB, 64) == 1 { pass=pass+1; gp(" T1 identical content -> identical hash (DEDUP to one blob) PASS\n" as *u8) } else { fail=fail+1; gp(" T1 FAIL\n" as *u8) }
25 if g_eq(hA, hC, 64) == 0 { pass=pass+1; gp(" T2 distinct content -> distinct hash (stored separately) PASS\n" as *u8) } else { fail=fail+1; gp(" T2 FAIL\n" as *u8) }
26 let kat: *u8 = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" as *u8
27 g_hex("abc" as *u8, 3, hA)
28 if g_eq(hA, kat, 64) == 1 { pass=pass+1; gp(" T3 sha256(\"abc\") KAT deterministic PASS\n" as *u8) } else { fail=fail+1; gp(" T3 FAIL\n" as *u8) }
29 gp("RESULT pass=" as *u8); gn(pass); gp(" fail=" as *u8); gn(fail)
30 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
31 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
32 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
33 let ctr__dry: *i64 = gv_ctr()
34 ctr__dry[0] = pass
35 ctr__dry[1] = pass + fail
36 let rc__dry: i64 = gv_verdict("DR-DEDUP-GATE" as *u8, ctr__dry, "store-once-by-hash)" as *u8)
37 sys_exit(rc__dry)
38 return rc__dry
39}