code wiki / _hdl_build / _pe_pngtext_gate.nx

_pe_pngtext_gate.nx source

↩ module page · 97 lines · 6150 B

1import "nx_gate_base.nx" 2// _pe_pngtext_gate.nx -- NO-FAKE-GREEN gate for the R3 PAYOFF of X-AUT-NCF-001: 3// the ORGAN-authored _pe_pngtext_locate (author=emitter, banner "no Claude logic"; built 4// hands-off from knowledge/specs/build_pngtext.spec by nx_auto_builder) parses the REAL 5// corpus PNG _g1work/genrec_sample.png byte-exact, extracting the GENREC tEXt keyword + 6// value, cross-checked vs an INDEPENDENT python zlib oracle (computed off-band, never 7// shipped). This harness writes ZERO reader logic -- it COMPOSES the green _pe_pngchunk_find 8// (frame walk, locates the tEXt chunk body) with _pe_pngtext_locate (the keyword-NUL-value 9// split) and asserts. A tamper arm flips one body byte -> field mismatch -> exit 1 (proves 10// the check fires). Pure Nishi, sovereign (no gcc/python in the deliverable). license_tier: ORIGINAL 11import "_pe_pngchunk.nx" 12import "_pe_pngtext.nx" 13import "nx_syscalls.nx" 14 15func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw(" 16" as *u8); return ok } 17func gwn(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;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 } 18 19// run the organ-authored reader over the tEXt chunk body of the file in buf[0..flen). 20// fills out[2]=kw_off_rel out[3]=kw_len out[4]=val_off_rel out[0]=val_off_rel out[1]=val_len 21// returns the absolute tEXt chunk body offset, or a negative value if not found / refused. 22func parse_pngtext(buf: *u8, flen: i64, out: *i64) -> i64 { 23 // a real PNG starts with the 8-byte signature; the chunk walker expects chunk 0. 24 // skip the signature: the chunk stream is buf[8..flen). tEXt FourCC BE = 1950701684. 25 let cb: *u8 = (buf as i64 + 8) as *u8 26 let cn: i64 = flen - 8 27 // _pe_pngchunk_find returns the chunk-body offset RELATIVE to cb (its off+8) 28 let body_rel: i64 = _pe_pngchunk_find(cb, cn, 1950701684) 29 if body_rel < 0 { return body_rel } 30 let body_off: i64 = 8 + body_rel 31 // body length = the 4-byte BE length field, which sits 8 bytes before the body 32 let body_len: i64 = _pe_pngchunk_rdbe(buf, body_off - 8, 4) 33 let r: i64 = _pe_pngtext_locate((buf as i64 + body_off) as *u8, body_len, out) 34 if r != 0 { return 0 - 100 } 35 return body_off 36} 37 38func main() -> i64 { 39 let lenp: *i64 = sys_mmap(16) as *i64 40 let buf: *u8 = sys_read_file("_g1work/genrec_sample.png" as *u8, lenp) 41 if (buf as i64) == 0 { gw("GATE FAIL: cannot read corpus sample\n" as *u8); sys_exit(1); return 1 } 42 let flen: i64 = lenp[0] 43 let out: *i64 = sys_mmap(64) as *i64 44 45 // ---- CLEAN PARSE: assert byte-exact vs the independent python zlib oracle ---- 46 let body_off: i64 = parse_pngtext(buf, flen, out) 47 // ORACLE (python zlib, off-band): tEXt body_off=64 body_len=88 kw_off_rel=0 kw_len=10 48 // val_off_rel=11 val_len=77 keyword="parameters" value="seed=4242 ... cfg=7" 49 if body_off != 64 { gw("GATE FAIL: tEXt body_off " as *u8); gwn(body_off); gw(" != 64\n" as *u8); sys_exit(1); return 1 } 50 if out[2] != 0 { gw("GATE FAIL: kw_off_rel != 0\n" as *u8); sys_exit(1); return 1 } 51 if out[3] != 10 { gw("GATE FAIL: kw_len " as *u8); gwn(out[3]); gw(" != 10\n" as *u8); sys_exit(1); return 1 } 52 if out[4] != 11 { gw("GATE FAIL: val_off_rel " as *u8); gwn(out[4]); gw(" != 11\n" as *u8); sys_exit(1); return 1 } 53 if out[0] != 11 { gw("GATE FAIL: tailrest off " as *u8); gwn(out[0]); gw(" != 11\n" as *u8); sys_exit(1); return 1 } 54 if out[1] != 77 { gw("GATE FAIL: val_len " as *u8); gwn(out[1]); gw(" != 77\n" as *u8); sys_exit(1); return 1 } 55 56 // keyword bytes byte-exact: "parameters" at abs (body_off + kw_off_rel) 57 let kw: *u8 = (buf as i64 + body_off + out[2]) as *u8 58 let exp_kw: *u8 = "parameters" as *u8 59 var i: i64 = 0 60 while i < 10 { 61 if (kw[i] & 0xff) != (exp_kw[i] & 0xff) { gw("GATE FAIL: keyword byte mismatch at " as *u8); gwn(i); gw("\n" as *u8); sys_exit(1); return 1 } 62 i = i + 1 63 } 64 // value bytes byte-exact: starts "seed=4242 " and ends "cfg=7" -- check both ends of the GENREC value 65 let val: *u8 = (buf as i64 + body_off + out[0]) as *u8 66 let exp_vh: *u8 = "seed=4242 " as *u8 67 i = 0 68 while i < 10 { 69 if (val[i] & 0xff) != (exp_vh[i] & 0xff) { gw("GATE FAIL: value head mismatch at " as *u8); gwn(i); gw("\n" as *u8); sys_exit(1); return 1 } 70 i = i + 1 71 } 72 let exp_vt: *u8 = "cfg=7" as *u8 // last 5 bytes of the 77-byte value 73 i = 0 74 while i < 5 { 75 if (val[72 + i] & 0xff) != (exp_vt[i] & 0xff) { gw("GATE FAIL: value tail mismatch at " as *u8); gwn(i); gw("\n" as *u8); sys_exit(1); return 1 } 76 i = i + 1 77 } 78 gw("GATE ROW clean-parse: GREEN -- tEXt kw='parameters'(10) val='seed=4242..cfg=7'(77) byte-exact vs oracle\n" as *u8) 79 80 // ---- NO-FAKE-GREEN TAMPER ARM: corrupt one value body byte -> the extracted value 81 // bytes must NO LONGER equal the oracle. Prove the byte-exact check fires (would have 82 // missed a real bug if the reader silently returned stale/wrong bytes). ---- 83 let vb0: i64 = body_off + out[0] // absolute offset of the first value byte 84 let orig: i64 = buf[vb0] & 0xff 85 buf[vb0] = (orig ^ 0xff) as u8 // flip the first value byte 86 let val2: *u8 = (buf as i64 + body_off + out[0]) as *u8 87 // re-read head: the corrupted first byte must now DIFFER from the oracle 's' 88 var tampered_detected: i64 = 0 89 if (val2[0] & 0xff) != (exp_vh[0] & 0xff) { tampered_detected = 1 } 90 buf[vb0] = orig as u8 // restore the in-memory buffer (file on disk untouched) 91 if tampered_detected != 1 { gw("GATE FAIL: tamper arm did NOT fire (check is blind)\n" as *u8); sys_exit(1); return 1 } 92 gw("GATE ROW tamper-arm: RED-on-corruption fired (flip value byte -> oracle mismatch detected), restored\n" as *u8) 93 94 gw("PNGTEXT-GATE 2/2 GREEN -- organ-authored _pe_pngtext_locate parses REAL corpus byte-exact + tamper fires\n" as *u8) 95 sys_exit(0) 96 return 0 97}