code wiki / _hdl_build / nx_ventropy_gate.nx

nx_ventropy_gate.nx source

↩ module page · 62 lines · 3688 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" 12import "nx_gate_verdict.nx" 13 14func 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 } 15func clr16(a: *i64) -> i64 { var i: i64=0; while i<16 { a[i]=0; i=i+1 } return 0 } 16 17func main() -> i64 { 18 g_puts("nx_ventropy gate (sovereign entropy coding of quantized coeffs, MEASURED)\n" as *u8) 19 var pass: i64 = 0; var total: i64 = 0 20 21 let co: *i64 = sys_mmap(16*8) as *i64 22 let de: *i64 = sys_mmap(16*8) as *i64 23 let buf: *u8 = sys_mmap(256) 24 25 // 1)+2) typical quantized block: DC + a few low-freq nonzeros 26 clr16(co); co[0]=6; co[1]=0-1; co[5]=2; co[8]=0-1 27 let bits: i64 = ve_encode(co, buf) 28 let bytes: i64 = nx_bits_bytes(bits) 29 ve_decode(buf, de) 30 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) 31 pass = pass + g_check("round-trip: encode->decode == original" as *u8, beq16(de, co)); total=total+1 32 pass = pass + g_check("compression: few-nonzero block << 32 raw bytes" as *u8, bytes < 16); total=total+1 33 34 // 3) near-static all-zero block -> EOB only 35 clr16(co) 36 let zbits: i64 = ve_encode(co, buf) 37 ve_decode(buf, de) 38 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) 39 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 40 41 // 4) high-freq: single coeff at the LAST zig-zag position (coeffs[15]) 42 clr16(co); co[15]=3 43 ve_encode(co, buf); ve_decode(buf, de) 44 pass = pass + g_check("high-freq: last-position coeff round-trips (run handled)" as *u8, beq16(de, co)); total=total+1 45 46 // 5) dense (honest): all 16 nonzero -> round-trips, bounded size (no fake compression) 47 var i: i64 = 0; while i < 16 { co[i] = (i - 8) * 3 + 1; i = i + 1 } // all nonzero, varied signs 48 let dbits: i64 = ve_encode(co, buf); ve_decode(buf, de) 49 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) 50 pass = pass + g_check("dense block round-trips + bounded (honest)" as *u8, (beq16(de, co) == 1) & (nx_bits_bytes(dbits) < 64)); total=total+1 51 52 g_puts("---- ventropy gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8) 53 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 54 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 55 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 56 let ctr__dry: *i64 = gv_ctr() 57 ctr__dry[0] = pass 58 ctr__dry[1] = total 59 let rc__dry: i64 = gv_verdict("VENTROPY-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8) 60 sys_exit(rc__dry) 61 return rc__dry 62}