code wiki / _hdl_build / nx_intra_block_gate.nx

nx_intra_block_gate.nx source

↩ module page · 142 lines · 7542 B

1// nx_intra_block_gate.nx -- SOVEREIGN proof of the PATENT-FREE intra keyframe pipeline, end-to-end on 2// an 8x8 block: pixels -> level-shift -> 2D DCT-II (nx_dct8, foundational expired math: Ahmed-Natarajan- 3// Rao 1974 / Chen 1977 / Loeffler 1989) -> scalar quantize -> 16-bit serialize -> rANS entropy code 4// (nx_rans, Duda PUBLIC-DOMAIN) -> [decode] -> dequantize -> inverse 2D DCT -> reconstruct. Every tool 5// is EXPIRED or PUBLIC-DOMAIN -> reads on no active claim (deep-research verified 2026-06-20). No CABAC, 6// no patented transform. The entropy stage is proven LOSSLESS (carries the quantized coefficients 7// bit-exact); the only loss is scalar quantization (measured + reported). All sovereign, no python, no 8// gcc, integer arithmetic (no f64). GREEN iff 7/7. Durable knowledge/status/intra_block_gate.log. 9// license_tier: ORIGINAL 10import "nx_syscalls.nx" 11import "nx_gate_emit_lib.nx" 12import "nx_dct8.nx" 13import "nx_rans.nx" 14 15func 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 } 16func 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 } 17func 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 } 18func iabs(v: i64) -> i64 { if v<0 { return 0-v } return v } 19func qround(v: i64, q: i64) -> i64 { if v>=0 { return (v + q/2)/q } return 0 - (((0-v) + q/2)/q) } 20 21// serialize 64 signed coeffs as 16-bit little-endian (128 bytes) 22func ser16(qc: *i64, out: *u8) -> i64 { 23 var i: i64=0 24 while i<64 { let v: i64 = qc[i] & 0xffff; out[2*i]=(v & 0xff) as u8; out[2*i+1]=((v>>8) & 0xff) as u8; i=i+1 } 25 return 128 26} 27// deserialize 128 bytes -> 64 signed coeffs (sign-extend 16-bit) 28func deser16(bytes: *u8, qc: *i64) -> i64 { 29 var i: i64=0 30 while i<64 { var v: i64 = (bytes[2*i] as i64) | ((bytes[2*i+1] as i64) << 8); if v>=32768 { v=v-65536 } qc[i]=v; i=i+1 } 31 return 64 32} 33 34// full intra pipeline on one 8x8 block; writes metrics into results[0..6]: 35// [0]=enclen [1]=max_abs_err [2]=sum_abs_err [3]=coeff_lossless(1/0) [4]=liarkill_ok(1/0) [5]=nonzero_coeffs 36func run_pipeline(block: *i64, DM: *i64, scratch: *i64, ti: *i64, to: *i64, Q: i64, sb: i64, results: *i64) -> i64 { 37 let M: i64 = 1 << sb 38 let sh: *i64 = sys_mmap(64*8) as *i64 39 var i: i64=0 40 while i<64 { sh[i]=block[i]-128; i=i+1 } 41 let co: *i64 = sys_mmap(64*8) as *i64 42 nx_dct8_forward_2d(DM, sh, co, scratch, ti, to) 43 let qc: *i64 = sys_mmap(64*8) as *i64 44 var nz: i64=0 45 i=0; while i<64 { qc[i]=qround(co[i], Q); if qc[i]!=0 { nz=nz+1 } i=i+1 } 46 let nb: i64 = 128 47 let bytes: *u8 = sys_mmap(nb+16) 48 ser16(qc, bytes) 49 let counts: *i64 = sys_mmap(257*8) as *i64 50 let fr: *i64 = sys_mmap(257*8) as *i64 51 let cu: *i64 = sys_mmap(257*8) as *i64 52 let s2s: *i64 = sys_mmap(M*8) as *i64 53 rans_count(bytes, nb, counts) 54 rans_normalize(counts, fr, sb) 55 rans_cum(fr, cu) 56 rans_build_slot2sym(fr, cu, s2s) 57 let cap: i64 = 2*nb+64 58 let enc: *u8 = sys_mmap(cap) 59 let enclen: i64 = rans_encode(bytes, nb, fr, cu, sb, enc, cap) 60 let rec: *u8 = sys_mmap(nb+16) 61 rans_decode(enc, enclen, nb, fr, cu, sb, s2s, rec) 62 let qc2: *i64 = sys_mmap(64*8) as *i64 63 deser16(rec, qc2) 64 var closs: i64=1 65 i=0; while i<64 { if qc2[i]!=qc[i] { closs=0 } i=i+1 } 66 let dq: *i64 = sys_mmap(64*8) as *i64 67 i=0; while i<64 { dq[i]=qc2[i]*Q; i=i+1 } 68 let rsh: *i64 = sys_mmap(64*8) as *i64 69 nx_dct8_inverse_2d(DM, dq, rsh, scratch, ti, to) 70 var maxe: i64=0; var sume: i64=0 71 i=0 72 while i<64 { 73 var p: i64 = rsh[i]+128 74 if p<0 { p=0 } 75 if p>255 { p=255 } 76 let e: i64 = iabs(p - block[i]) 77 if e>maxe { maxe=e } 78 sume=sume+e 79 i=i+1 80 } 81 // liar-kill: corrupt the stream, redecode, confirm it diverges from the clean decode 82 let ec: *u8 = sys_mmap(cap) 83 i=0; while i<enclen { ec[i]=enc[i]; i=i+1 } 84 let mid: i64 = enclen/2 85 ec[mid] = (((ec[mid] as i64)+1) & 0xff) as u8 86 let rec2: *u8 = sys_mmap(nb+16) 87 rans_decode(ec, enclen, nb, fr, cu, sb, s2s, rec2) 88 var lk: i64=0 89 i=0; while i<nb { if rec2[i]!=rec[i] { lk=1 } i=i+1 } 90 results[0]=enclen; results[1]=maxe; results[2]=sume; results[3]=closs; results[4]=lk; results[5]=nz 91 return 0 92} 93 94func main() -> i64 { 95 g_puts("=== INTRA BLOCK GATE: patent-free DCT+quant+rANS pipeline, end-to-end (sovereign) ===\n" as *u8) 96 let sb: i64 = 12 97 let Q: i64 = 16 98 let DM: *i64 = sys_mmap(64*8) as *i64 99 nx_dct8_init(DM) 100 let scratch: *i64 = sys_mmap(64*8) as *i64 101 let ti: *i64 = sys_mmap(8*8) as *i64 102 let to: *i64 = sys_mmap(8*8) as *i64 103 104 // block A: smooth gradient pixel(r,c)=16*(r+c) (0..224) 105 let A: *i64 = sys_mmap(64*8) as *i64 106 var r: i64=0 107 while r<8 { var c: i64=0; while c<8 { A[r*8+c]=(r*16+c*16); c=c+1 } r=r+1 } 108 let ra: *i64 = sys_mmap(8*8) as *i64 109 run_pipeline(A, DM, scratch, ti, to, Q, sb, ra) 110 111 // block B: flat (all 100) 112 let B: *i64 = sys_mmap(64*8) as *i64 113 var i: i64=0 114 while i<64 { B[i]=100; i=i+1 } 115 let rb: *i64 = sys_mmap(8*8) as *i64 116 run_pipeline(B, DM, scratch, ti, to, Q, sb, rb) 117 118 g_puts("-- block A (gradient): enc=" as *u8); g_num(ra[0]); g_puts("/64B maxErr=" as *u8); g_num(ra[1]) 119 g_puts(" meanErr=" as *u8); g_num(ra[2]/64); g_puts(" nzCoef=" as *u8); g_num(ra[5]); g_puts("/64\n" as *u8) 120 g_puts("-- block B (flat): enc=" as *u8); g_num(rb[0]); g_puts("/64B maxErr=" as *u8); g_num(rb[1]) 121 g_puts(" nzCoef=" as *u8); g_num(rb[5]); g_puts("/64\n" as *u8) 122 123 var pass: i64=0; let rows: i64=7 124 pass=pass+g_check(" T1 A: entropy carries quantized coeffs bit-exact" as *u8, ra[3]) 125 var t2: i64=0; if ra[0] <= 24 { t2=1 } 126 pass=pass+g_check(" T2 A: compresses >=2.6x (enc<=24 of raw 64)" as *u8, t2) 127 var t3: i64=0; if ra[1] <= 6 { t3=1 } 128 pass=pass+g_check(" T3 A: reconstruction faithful (maxErr<=6)" as *u8, t3) 129 pass=pass+g_check(" T4 B: entropy carries quantized coeffs bit-exact" as *u8, rb[3]) 130 var t5: i64=0; if rb[0] <= 8 { t5=1 } 131 pass=pass+g_check(" T5 B: flat block -> tiny stream (enc<=8)" as *u8, t5) 132 var t6: i64=0; if rb[1] <= 1 { t6=1 } 133 pass=pass+g_check(" T6 B: flat reconstruction near-exact (maxErr<=1)" as *u8, t6) 134 var t7: i64=0; if ra[4]==1 { if rb[4]==1 { t7=1 } } 135 pass=pass+g_check(" T7 liar-kill: corrupted stream diverges (both blocks)" as *u8, t7) 136 137 g_puts("----\nINTRA rows=" as *u8); g_num(rows); g_puts(" pass=" as *u8); g_num(pass); g_puts("\n" as *u8) 138 let lg: i64 = sys_openat_append("knowledge/status/intra_block_gate.log" as *u8, 0x1a4) 139 if lg>=0 { g_w(lg, "INTRA rows=" as *u8); g_wn(lg, rows); g_w(lg, " pass=" as *u8); g_wn(lg, pass); g_w(lg, " encA=" as *u8); g_wn(lg, ra[0]); g_w(lg, " maxErrA=" as *u8); g_wn(lg, ra[1]); if pass==rows { g_w(lg, " verdict=GREEN\n" as *u8) } else { g_w(lg, " verdict=RED\n" as *u8) } sys_close(lg) } 140 if pass==rows { g_puts("INTRA GREEN (sovereign patent-free intra keyframe pipeline proven end-to-end)\n" as *u8); sys_exit(0); return 0 } 141 g_puts("INTRA RED\n" as *u8); sys_exit(1); return 1 142}