code wiki / _hdl_build / nx_rans_gate.nx
nx_rans_gate.nx source
↩ module page · 112 lines · 6520 B
1// nx_rans_gate.nx -- SOVEREIGN liar-kill gate for nx_rans (the patent-free rANS entropy keystone).
2// Proves ENTIRELY within Nishi (no python, no 3rd party): for an entropy stage whose encoder AND
3// decoder we both own, lossless round-trip IS the complete correctness criterion.
4// T1 round-trip bit-exact on a skewed 3-symbol buffer (decode(encode(x)) == x)
5// T2 it actually COMPRESSES (enc < raw) -- honest entropy coding, not a memcpy
6// T3 round-trip bit-exact on a 64-symbol buffer (full pipeline over a wide alphabet)
7// T4 degenerate single-symbol round-trip + near-zero output (entropy-correct: 0 bits/sym -> 4-byte state)
8// T5 determinism: re-encoding the same input yields byte-identical output
9// T6 LIAR-KILL: a CORRUPTED stream does NOT reproduce the input -- the decoder genuinely depends on
10// the bitstream, so the gate cannot pass by echoing
11// GREEN iff 6/6. Durable knowledge/status/rans_gate.log. license_tier: ORIGINAL
12import "nx_syscalls.nx"
13import "nx_gate_emit_lib.nx"
14import "nx_rans.nx"
15
16func g_num(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)}; let t: *u8=sys_mmap(28); 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; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(1,bb,k); return 0 }
17func g_w(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 }
18func g_wn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; let t: *u8=sys_mmap(28); 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; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(fd,bb,k); return 0 }
19func bufeq(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 }
20
21// full static-rANS encode pipeline; fills freqs/cum/slot2sym (needed by the decoder); returns enclen.
22func enc_pipeline(data: *u8, n: i64, sb: i64, freqs: *i64, cum: *i64, slot2sym: *i64, out: *u8, outcap: i64) -> i64 {
23 let counts: *i64 = sys_mmap(257*8) as *i64
24 rans_count(data, n, counts)
25 if rans_normalize(counts, freqs, sb) < 0 { return 0 - 1 }
26 rans_cum(freqs, cum)
27 rans_build_slot2sym(freqs, cum, slot2sym)
28 return rans_encode(data, n, freqs, cum, sb, out, outcap)
29}
30
31func main() -> i64 {
32 g_puts("=== rANS GATE: patent-free entropy keystone, lossless round-trip self-proof (no python) ===\n" as *u8)
33 let sb: i64 = 12
34 let M: i64 = 1 << sb
35
36 // ---- buffer 1: skewed 3-symbol (compressible) ----
37 let N1: i64 = 4096
38 let b1: *u8 = sys_mmap(N1+16)
39 var i: i64 = 0
40 while i < N1 { var c: i64=65; let m: i64=i%10; if m<2 { c=66 } if m==2 { c=67 } b1[i]=c as u8; i=i+1 }
41 let f1: *i64 = sys_mmap(257*8) as *i64
42 let c1: *i64 = sys_mmap(257*8) as *i64
43 let s1: *i64 = sys_mmap(M*8) as *i64
44 let cap1: i64 = 2*N1+64
45 let e1: *u8 = sys_mmap(cap1)
46 let enclen1: i64 = enc_pipeline(b1, N1, sb, f1, c1, s1, e1, cap1)
47 let d1: *u8 = sys_mmap(N1+16)
48 let consumed1: i64 = rans_decode(e1, enclen1, N1, f1, c1, sb, s1, d1)
49 var t1: i64=0; if enclen1>0 { if bufeq(b1, d1, N1)==1 { if consumed1==enclen1 { t1=1 } } }
50 var t2: i64=0; if enclen1 < N1 { t2=1 }
51
52 // ---- buffer 2: 64-symbol wide alphabet ----
53 let N2: i64 = 2048
54 let b2: *u8 = sys_mmap(N2+16)
55 i=0; while i<N2 { b2[i]=(((i*7+3)%64)+32) as u8; i=i+1 }
56 let f2: *i64 = sys_mmap(257*8) as *i64
57 let c2: *i64 = sys_mmap(257*8) as *i64
58 let s2: *i64 = sys_mmap(M*8) as *i64
59 let cap2: i64 = 2*N2+64
60 let e2: *u8 = sys_mmap(cap2)
61 let enclen2: i64 = enc_pipeline(b2, N2, sb, f2, c2, s2, e2, cap2)
62 let d2: *u8 = sys_mmap(N2+16)
63 rans_decode(e2, enclen2, N2, f2, c2, sb, s2, d2)
64 var t3: i64=0; if enclen2>0 { if bufeq(b2, d2, N2)==1 { t3=1 } }
65
66 // ---- buffer 3: degenerate single symbol ----
67 let N3: i64 = 1000
68 let b3: *u8 = sys_mmap(N3+16)
69 i=0; while i<N3 { b3[i]=88 as u8; i=i+1 }
70 let f3: *i64 = sys_mmap(257*8) as *i64
71 let c3: *i64 = sys_mmap(257*8) as *i64
72 let s3: *i64 = sys_mmap(M*8) as *i64
73 let cap3: i64 = 2*N3+64
74 let e3: *u8 = sys_mmap(cap3)
75 let enclen3: i64 = enc_pipeline(b3, N3, sb, f3, c3, s3, e3, cap3)
76 let d3: *u8 = sys_mmap(N3+16)
77 rans_decode(e3, enclen3, N3, f3, c3, sb, s3, d3)
78 var t4: i64=0; if bufeq(b3, d3, N3)==1 { if enclen3<=8 { t4=1 } }
79
80 // ---- T5 determinism: re-encode buffer 1 ----
81 let f1b: *i64 = sys_mmap(257*8) as *i64
82 let c1b: *i64 = sys_mmap(257*8) as *i64
83 let s1b: *i64 = sys_mmap(M*8) as *i64
84 let e1b: *u8 = sys_mmap(cap1)
85 let enclen1b: i64 = enc_pipeline(b1, N1, sb, f1b, c1b, s1b, e1b, cap1)
86 var t5: i64=0; if enclen1b==enclen1 { if bufeq(e1, e1b, enclen1)==1 { t5=1 } }
87
88 // ---- T6 liar-kill: corrupt the stream, decode must diverge from the original ----
89 let ec: *u8 = sys_mmap(cap1)
90 i=0; while i<enclen1 { ec[i]=e1[i]; i=i+1 }
91 let mid: i64 = enclen1/2
92 ec[mid] = (((ec[mid] as i64)+1) & 0xff) as u8
93 let dc: *u8 = sys_mmap(N1+16)
94 rans_decode(ec, enclen1, N1, f1, c1, sb, s1, dc)
95 var t6: i64=0; if bufeq(b1, dc, N1)==0 { t6=1 }
96
97 var pass: i64=0; let rows: i64=6
98 pass=pass+g_check(" T1 round-trip bit-exact (skewed 3-symbol)" as *u8, t1)
99 pass=pass+g_check(" T2 compresses (enc < raw)" as *u8, t2)
100 pass=pass+g_check(" T3 round-trip bit-exact (64-symbol alphabet)" as *u8, t3)
101 pass=pass+g_check(" T4 degenerate single-symbol round-trip + 4-byte output" as *u8, t4)
102 pass=pass+g_check(" T5 determinism (re-encode byte-identical)" as *u8, t5)
103 pass=pass+g_check(" T6 liar-kill: corrupted stream != original" as *u8, t6)
104
105 g_puts("----\nRANS rows=" as *u8); g_num(rows); g_puts(" pass=" as *u8); g_num(pass)
106 g_puts(" enc1=" as *u8); g_num(enclen1); g_puts("/" as *u8); g_num(N1)
107 g_puts(" enc3=" as *u8); g_num(enclen3); g_puts("\n" as *u8)
108 let lg: i64 = sys_openat_append("knowledge/status/rans_gate.log" as *u8, 0x1a4)
109 if lg>=0 { g_w(lg, "RANS rows=" as *u8); g_wn(lg, rows); g_w(lg, " pass=" as *u8); g_wn(lg, pass); g_w(lg, " enc1=" as *u8); g_wn(lg, enclen1); g_w(lg, "/" as *u8); g_wn(lg, N1); if pass==rows { g_w(lg, " verdict=GREEN\n" as *u8) } else { g_w(lg, " verdict=RED\n" as *u8) } sys_close(lg) }
110 if pass==rows { g_puts("RANS GREEN (sovereign patent-free rANS entropy coder: lossless round-trip proven)\n" as *u8); sys_exit(0); return 0 }
111 g_puts("RANS RED\n" as *u8); sys_exit(1); return 1
112}