code wiki / _hdl_build / nx_ventropy_gate.nx
nx_ventropy_gate.nx source
↩ module page · 54 lines · 3246 B
1// nx_ventropy_gate.nx -- proves + MEASURES sovereign entropy coding of a quantized 4x4 block (nx_ventropy), V-R3.
2// Native nx_cc->nxasm, no node.
3// 1) round-trip: a quantized block (DC + a few low-freq) encode -> decode == original
4// 2) compression (MEASURED): a few-nonzero block -> a few bytes vs 32 raw (16 coeffs x 2B)
5// 3) near-static: an all-zero block -> ~1 byte (EOB only)
6// 4) high-frequency: a single coeff at the LAST zig-zag position round-trips (run handled)
7// 5) dense (neg, honest): a full 16-nonzero block round-trips and is bounded (no fake compression)
8// license_tier: ORIGINAL
9import "nx_syscalls.nx"
10import "nx_gate_emit_lib.nx"
11import "nx_ventropy.nx"
12
13func beq16(a: *i64, b: *i64) -> i64 { var i: i64=0; while i<16 { if a[i]!=b[i] { return 0 } i=i+1 } return 1 }
14func clr16(a: *i64) -> i64 { var i: i64=0; while i<16 { a[i]=0; i=i+1 } return 0 }
15
16func main() -> i64 {
17 g_puts("nx_ventropy gate (sovereign entropy coding of quantized coeffs, MEASURED)\n" as *u8)
18 var pass: i64 = 0; var total: i64 = 0
19
20 let co: *i64 = sys_mmap(16*8) as *i64
21 let de: *i64 = sys_mmap(16*8) as *i64
22 let buf: *u8 = sys_mmap(256)
23
24 // 1)+2) typical quantized block: DC + a few low-freq nonzeros
25 clr16(co); co[0]=6; co[1]=0-1; co[5]=2; co[8]=0-1
26 let bits: i64 = ve_encode(co, buf)
27 let bytes: i64 = nx_bits_bytes(bits)
28 ve_decode(buf, de)
29 g_puts(" [measure] 4-nonzero block -> " as *u8); g_pn(bits); g_puts(" bits = " as *u8); g_pn(bytes); g_puts(" bytes (vs 32 raw)\n" as *u8)
30 pass = pass + g_check("round-trip: encode->decode == original" as *u8, beq16(de, co)); total=total+1
31 pass = pass + g_check("compression: few-nonzero block << 32 raw bytes" as *u8, bytes < 16); total=total+1
32
33 // 3) near-static all-zero block -> EOB only
34 clr16(co)
35 let zbits: i64 = ve_encode(co, buf)
36 ve_decode(buf, de)
37 g_puts(" [measure] all-zero block -> " as *u8); g_pn(zbits); g_puts(" bits (" as *u8); g_pn(nx_bits_bytes(zbits)); g_puts(" byte)\n" as *u8)
38 pass = pass + g_check("near-static: all-zero -> ~1 byte + round-trips" as *u8, (nx_bits_bytes(zbits) <= 1) & (beq16(de, co) == 1)); total=total+1
39
40 // 4) high-freq: single coeff at the LAST zig-zag position (coeffs[15])
41 clr16(co); co[15]=3
42 ve_encode(co, buf); ve_decode(buf, de)
43 pass = pass + g_check("high-freq: last-position coeff round-trips (run handled)" as *u8, beq16(de, co)); total=total+1
44
45 // 5) dense (honest): all 16 nonzero -> round-trips, bounded size (no fake compression)
46 var i: i64 = 0; while i < 16 { co[i] = (i - 8) * 3 + 1; i = i + 1 } // all nonzero, varied signs
47 let dbits: i64 = ve_encode(co, buf); ve_decode(buf, de)
48 g_puts(" [measure] dense 16-nonzero block -> " as *u8); g_pn(nx_bits_bytes(dbits)); g_puts(" bytes (honest: no fake savings)\n" as *u8)
49 pass = pass + g_check("dense block round-trips + bounded (honest)" as *u8, (beq16(de, co) == 1) & (nx_bits_bytes(dbits) < 64)); total=total+1
50
51 g_puts("---- ventropy gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8)
52 if pass == total { g_puts("verdict=GREEN\n" as *u8); sys_exit(0); return 0 }
53 g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1
54}