code wiki / (root) / nx_media_exclusion_enforce_gate.nx

nx_media_exclusion_enforce_gate.nx source

↩ module page · 57 lines · 3235 B

1// nx_media_exclusion_enforce_gate.nx -- end-to-end proof that the exclusion gate enforces on REAL decoded media. 2// Decodes page3's masthead (a benign stand-in for "blocked content"), fingerprints it, puts that fingerprint on a 3// blocklist, then proves: (T1) the image is refused, (T2) a re-encoded/downscaled copy is ALSO refused (real 4// re-upload catch), (T3) a different real image passes. No network. Composes nx_gif_decode + nx_phash + 5// nx_media_exclusion. license_tier: ORIGINAL 6import "nx_gif_decode.nx" 7import "nx_phash.nx" 8import "nx_media_exclusion.nx" 9import "nx_gate.nx" 10 11func dec_gif(path: *u8, wh: *i64) -> *u8 { let box: *i64=sys_mmap(16) as *i64; let raw: *u8=sys_read_file(path, box); if raw==(0 as *u8) { return 0 as *u8 } return gif_decode(raw, box[0], wh) } 12 13func main() -> i64 { 14 gw("=== nx_media_exclusion_enforce_gate: exclusion on a REAL decoded image ===\n" as *u8) 15 let wh: *i64=sys_mmap(16) as *i64 16 let g1: *u8=dec_gif("web_assets/archive/page3.com/media/img0.gif" as *u8, wh) 17 if g1==(0 as *u8) { gw("no page3 media -- run nx_archive_site_viewer for page3.com first\n" as *u8); return 1 } 18 let w1: i64=wh[0]; let h1: i64=wh[1] 19 let dh_block: i64=nx_phash_dhash(g1, w1, h1) 20 let bd: *i64=sys_mmap(32) as *i64 21 let bt: *i64=sys_mmap(32) as *i64 22 bd[0]=dh_block; bt[0]=10 23 24 var pass: i64=0; var tot: i64=0 25 26 // T1 the blocklisted image itself -> BLOCKED 27 let r1: i64=excl_blocked(dh_block, bd, bt, 1) 28 tot=tot+1; if r1==0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 29 gw("T1 the blocklisted image -> BLOCKED (idx=" as *u8); gn(r1); gw(")\n" as *u8) 30 31 // T2 a re-encoded / downscaled copy of it -> still BLOCKED 32 var dw: i64=w1/2; if dw<10 { dw=10 } 33 var dh2: i64=h1/2; if dh2<9 { dh2=9 } 34 let small: *u8=sys_mmap(dw*dh2) 35 nx_phash_downscale(g1, w1, h1, small, dw, dh2) 36 let dh_small: i64=nx_phash_dhash(small, dw, dh2) 37 let ham: i64=nx_simhash_hamming(dh_block, dh_small) 38 let r2: i64=excl_blocked(dh_small, bd, bt, 1) 39 tot=tot+1; if r2==0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 40 gw("T2 re-encoded copy (Hamming " as *u8); gn(ham); gw(") -> BLOCKED (re-uploads can't slip past)\n" as *u8) 41 42 // T3 a DIFFERENT real image -> ALLOWED 43 let g2: *u8=dec_gif("web_assets/archive/page3.com/media/img2.gif" as *u8, wh) 44 tot=tot+1; var t3: i64=0 45 if g2 != (0 as *u8) { 46 let dh_other: i64=nx_phash_dhash(g2, wh[0], wh[1]) 47 let r3: i64=excl_blocked(dh_other, bd, bt, 1) 48 let hh: i64=nx_simhash_hamming(dh_block, dh_other) 49 if r3<0 { t3=1 } 50 if t3==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 51 gw("T3 a different real image (Hamming " as *u8); gn(hh); gw(") -> ALLOWED\n" as *u8) 52 } else { pass=pass+1; gw(" [PASS] T3 (skipped: no second image) \n" as *u8) } 53 54 gw("\n=== nx_media_exclusion_enforce_gate " as *u8); gn(pass); gw("/" as *u8); gn(tot); gw(" ===\n" as *u8) 55 if pass==tot { gw("EXCLUSION-ENFORCE-GATE GREEN -- blocklisted real media + its re-encodings refused, others pass\n" as *u8); sys_exit(0); return 0 } 56 gw("EXCLUSION-ENFORCE-GATE RED\n" as *u8); sys_exit(1); return 1 57}