code wiki / _hdl_build / nx_natstat_hardcases.nx
nx_natstat_hardcases.nx source
↩ module page · 90 lines · 5350 B
1// nx_natstat_hardcases.nx -- HARDEN the metric: axes 3 (colour) + 4 (ai-fingerprint) passed BOTH the target and
2// our render, so they were unproven guards. Generate synthetic images with KNOWN ground truth and assert the
3// guards BITE (drop below the pass band). No network, no decode -- we author the pixels, so the truth is certain.
4// GREEN = every guard fired where it should + stayed quiet where it shouldn't. expect_exit: 0
5import "nx_syscalls.nx"
6import "nx_natstat.nx"
7const K_MAGIC_65536: i64 = 65536
8const K_MAGIC_374761393: i64 = 374761393
9const K_MAGIC_668265263: i64 = 668265263
10const K_MAGIC_8192: i64 = 8192
11const K_MAGIC_1274126177: i64 = 1274126177
12
13func w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
14func pn(v: i64) -> i64 {
15 let b: *u8 = sys_mmap(32) as *u8
16 var x: i64 = v; var neg: i64 = 0
17 if x < 0 { neg = 1; x = 0 - x }
18 var i: i64 = 31
19 if x == 0 { b[i] = 48 as u8; i = i - 1 }
20 while x > 0 { b[i] = (48 + x % 10) as u8; x = x / 10; i = i - 1 }
21 if neg == 1 { b[i] = 45 as u8; i = i - 1 }
22 sys_write(1, (b as i64 + i + 1) as *u8, 31 - i)
23 return 0
24}
25func px(r: i64, g: i64, b: i64) -> i64 { return r + g * 256 + b * K_MAGIC_65536 }
26// decorrelated per-pixel hash (NO short period -> no lag-2/4 autocorrelation, unlike x%29 which IS periodic)
27func hsh(a: i64, b: i64) -> i64 {
28 var h: i64 = a * K_MAGIC_374761393 + b * K_MAGIC_668265263
29 h = h ^ (h / K_MAGIC_8192)
30 h = h * K_MAGIC_1274126177
31 if h < 0 { h = 0 - h }
32 return h
33}
34func main() -> i64 {
35 let W: i64 = 128
36 let H: i64 = 128
37 let gray: *i64 = sys_mmap(W * H * 8) as *i64 // grayscale ramp -> colourful ~0
38 let sat: *i64 = sys_mmap(W * H * 8) as *i64 // over-saturated colour bars -> colourful huge
39 let chk: *i64 = sys_mmap(W * H * 8) as *i64 // 2px checkerboard -> strong periodic (AI-grid) artifact
40 let nat: *i64 = sys_mmap(W * H * 8) as *i64 // mild natural-ish mottle -> should PASS both
41 var y: i64 = 0
42 while y < H {
43 var x: i64 = 0
44 while x < W {
45 let i: i64 = y * W + x
46 let ramp: i64 = 40 + y * 150 / H
47 gray[i] = px(ramp, ramp, ramp)
48 // saturated bars: pure primaries cycling every 8px
49 let band: i64 = (x / 8) % 3
50 if band == 0 { sat[i] = px(250, 10, 10) }
51 if band == 1 { sat[i] = px(10, 250, 10) }
52 if band == 2 { sat[i] = px(10, 10, 250) }
53 // 2px checkerboard: black/white -> high lag-2 autocorrelation
54 let cb: i64 = ((x / 2) + (y / 2)) % 2
55 if cb == 0 { chk[i] = px(20, 20, 20) } else { chk[i] = px(230, 230, 230) }
56 // natural-ish: smooth skin-tone base + DECORRELATED mottle (no period -> low lag-2/4 autocorrelation)
57 let n1: i64 = (hsh(x, y) % 29) - 14
58 let n2: i64 = (hsh(x + 777, y + 333) % 17) - 8
59 let bR: i64 = 170 + n1 + n2
60 nat[i] = px(bR, bR - 34 + n2, bR - 58 + n1)
61 x = x + 1
62 }
63 y = y + 1
64 }
65 var pass: i64 = 0
66 var fail: i64 = 0
67 // colour axis (band [12,58]): gray should score LOW, sat should score LOW, nat should PASS high
68 let cg: i64 = ns_colorful(gray, W, H)
69 let cs: i64 = ns_colorful(sat, W, H)
70 let cn: i64 = ns_colorful(nat, W, H)
71 let ag: i64 = ns_band(cg, 12, 58, 16) // same band as ns_assess axis 3
72 let asx: i64 = ns_band(cs, 12, 58, 16)
73 let an: i64 = ns_band(cn, 12, 58, 16)
74 w("colour: grey raw=" as *u8); pn(cg); w(" score=" as *u8); pn(ag); w(" | sat raw=" as *u8); pn(cs); w(" score=" as *u8); pn(asx); w(" | natural raw=" as *u8); pn(cn); w(" score=" as *u8); pn(an); w("\n" as *u8)
75 if ag < 400 { pass = pass + 1; w(" T1 PASS grey colour guard BITES\n" as *u8) } else { fail = fail + 1; w(" T1 FAIL grey passed colour (guard dead)\n" as *u8) }
76 if asx < 400 { pass = pass + 1; w(" T2 PASS oversaturated colour guard BITES\n" as *u8) } else { fail = fail + 1; w(" T2 FAIL oversat passed colour\n" as *u8) }
77 if an > 700 { pass = pass + 1; w(" T3 PASS natural colour stays quiet (no false positive)\n" as *u8) } else { fail = fail + 1; w(" T3 FAIL natural colour wrongly flagged\n" as *u8) }
78 // ai-fingerprint axis (band [0,130]): checkerboard should score LOW, natural should PASS high
79 let pk: i64 = ns_ai_periodicity(chk, W, H)
80 let pnat: i64 = ns_ai_periodicity(nat, W, H)
81 let apk: i64 = ns_band(pk, 0, 130, 320)
82 let apn: i64 = ns_band(pnat, 0, 130, 320)
83 w("ai-fp: checker raw=" as *u8); pn(pk); w(" score=" as *u8); pn(apk); w(" | natural raw=" as *u8); pn(pnat); w(" score=" as *u8); pn(apn); w("\n" as *u8)
84 if apk < 400 { pass = pass + 1; w(" T4 PASS checkerboard ai-fingerprint guard BITES\n" as *u8) } else { fail = fail + 1; w(" T4 FAIL periodic artifact passed ai-fp (guard dead)\n" as *u8) }
85 if apn > 700 { pass = pass + 1; w(" T5 PASS natural ai-fp stays quiet (no false positive)\n" as *u8) } else { fail = fail + 1; w(" T5 FAIL natural wrongly flagged as AI\n" as *u8) }
86 w("\n" as *u8)
87 if fail == 0 { w("GATE GREEN: all " as *u8); pn(pass); w(" guards behave -- axes 3+4 BITE on known-bad, stay quiet on known-good.\n" as *u8); return 0 }
88 w("GATE RED: " as *u8); pn(fail); w(" guard(s) misbehaved.\n" as *u8)
89 return 1
90}