code wiki / _hdl_build / nx_dp_leak_gate.nx
nx_dp_leak_gate.nx source
↩ module page · 174 lines · 9842 B
1// nx_dp_leak_gate.nx -- STANDING "RED on leak" guarantee for rung 0: the PUBLIC corpus must carry NO document
2// bearing an internal/SECRET marker. Dual-mode:
3// (gate) no args -> hermetic 3-tooth proof in a fresh nonce domain (seed leak -> detect -> reclassify -> clean)
4// (live) `scan <domain>` -> run leak_scan against the REAL store (search-level closure proof)
5//
6// leak_scan(domain): for each internal marker, dss_search (the LIVE consent-filtered search) returns BM25
7// candidates; each candidate's doc bytes are re-read (dp_read) and the LITERAL marker string is re-confirmed
8// in the bytes (dss_search tokenizes -- the literal re-confirm kills false positives). Distinct leaking cids
9// are summed. 0 = clean. NOTE: the markers' dashes are word-internal (preceded by a token char) so dss_search
10// treats them as inert separators, NOT as -NOT operators -- the markers tokenize into positive OR terms.
11// STANDALONE check organ -- imports only; modifies/redeploys NO daemon. license_tier: ORIGINAL
12import "nx_dp_reclassify.nx" // dpr_run (+ dp_*/dss_*/ss_*/sys_* transitively)
13import "nx_docportal_search_seg.nx" // dss_search
14import "nx_docportal_lib.nx" // dp_read / dp_ingest_policy / DP_VIS_PUBLIC / DP_USE_PUB_SEARCH
15
16const LK_MAXR: i64 = 512 // dss_search candidate cap per marker (leaking internal docs rank high on rare markers)
17
18func g_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
19func g_num(v: i64) -> i64 {
20 let bb: *u8 = sys_mmap(28); var m: i64 = v; if m < 0 { m = 0 - m; sys_write(1, "-" as *u8, 1) }
21 let t: *u8 = sys_mmap(28); var k: i64 = 0; if m == 0 { t[0] = 48 as u8; k = 1 }
22 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
23 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } sys_write(1, bb, k); return 0
24}
25func g_w(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 }
26func g_wn(fd: i64, v: i64) -> i64 {
27 let bb: *u8 = sys_mmap(28); var m: i64 = v; if m < 0 { m = 0 - m }
28 let t: *u8 = sys_mmap(28); var k: i64 = 0; if m == 0 { t[0] = 48 as u8; k = 1 }
29 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
30 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } sys_write(fd, bb, k); return 0
31}
32func g_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
33func g_cat(out: *u8, o: i64, s: *u8) -> i64 { var i: i64 = 0; while s[i] != (0 as u8) { out[o] = s[i]; o = o + 1; i = i + 1 } return o }
34func g_catnum(out: *u8, o: i64, v: i64) -> i64 {
35 if v == 0 { out[o] = 48 as u8; return o + 1 }
36 var m: i64 = v; let t: *u8 = sys_mmap(28); var k: i64 = 0
37 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
38 var i: i64 = 0; while i < k { out[o] = t[k - 1 - i]; o = o + 1; i = i + 1 } return o
39}
40func lk_streq(a: *u8, b: *u8) -> i64 {
41 var i: i64 = 0
42 while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 }
43 if b[i] != (0 as u8) { return 0 }
44 return 1
45}
46// literal substring test (case-sensitive; markers are stored with their exact case)
47func lk_contains(hay: *u8, hn: i64, needle: *u8, nn: i64) -> i64 {
48 if nn == 0 { return 0 }
49 if hn < nn { return 0 }
50 var i: i64 = 0
51 while i + nn <= hn {
52 var j: i64 = 0
53 var hit: i64 = 1
54 while j < nn { if hay[i + j] != needle[j] { hit = 0; j = nn } else { j = j + 1 } }
55 if hit == 1 { return 1 }
56 i = i + 1
57 }
58 return 0
59}
60
61// THE SCAN: distinct PUBLIC docs whose bytes literally contain an internal/SECRET marker, found via the LIVE
62// consent-filtered search. Returns the count (0 = clean). Prints one line per NEW confirmed leak (diagnostics).
63func leak_scan(domain: *u8) -> i64 {
64 let m0: *u8 = "nishi-internal-research-artifact" as *u8
65 let m1: *u8 = "S-CLASS-EXCEED" as *u8
66 let m2: *u8 = "Origin class: nishi-internal" as *u8
67 // Open the PUBLIC shard ONCE and re-confirm candidates via ss_hget. This is functionally identical to
68 // dp_read(domain, DP_VIS_PUBLIC, cid) -- same prefix ("knowledge/store/dp-<domain>-pub-"), same "doc:<cid>"
69 // key, same newest-wins/tombstone semantics -- but dp_read re-ss_open's (full-shard reload) PER candidate,
70 // which is O(candidates x shard) on the live corpus. Hoisting the open = 1 load here + 3 inside dss_search.
71 let prefix: *u8 = sys_mmap(512)
72 dss_prefix(domain, prefix)
73 let h: *i64 = ss_open(prefix)
74 if (h as i64) == 0 { return 0 } // empty/absent shard -> no leaks (cross-shard isolation inherited)
75 let cids: *i64 = sys_mmap(LK_MAXR * 8) as *i64
76 let scores: *i64 = sys_mmap(LK_MAXR * 8) as *i64
77 let seen: *i64 = sys_mmap(LK_MAXR * 3 * 8) as *i64 // deduped leaking cids (<=3*LK_MAXR distinct)
78 var nseen: i64 = 0
79 let dkey: *u8 = sys_mmap(64)
80 let pq: *i64 = sys_mmap(16) as *i64
81 let lq: *i64 = sys_mmap(16) as *i64
82 var mi: i64 = 0
83 while mi < 3 {
84 var marker: *u8 = m0
85 if mi == 1 { marker = m1 }
86 if mi == 2 { marker = m2 }
87 let mlen: i64 = g_slen(marker)
88 let n: i64 = dss_search(domain, marker, mlen, cids, scores, LK_MAXR)
89 var k: i64 = 0
90 while k < n {
91 let cid: i64 = cids[k]
92 dss_mkkey(cid, dkey)
93 if ss_hget(h, dkey, pq, lq) == 1 {
94 if lk_contains(pq[0] as *u8, lq[0], marker, mlen) == 1 {
95 var dup: i64 = 0
96 var s: i64 = 0
97 while s < nseen { if seen[s] == cid { dup = 1; s = nseen } else { s = s + 1 } }
98 if dup == 0 {
99 seen[nseen] = cid; nseen = nseen + 1
100 g_puts(" LEAK cid=" as *u8); g_num(cid); g_puts(" marker=" as *u8); g_puts(marker); g_puts("\n" as *u8)
101 }
102 }
103 }
104 k = k + 1
105 }
106 mi = mi + 1
107 }
108 return nseen
109}
110
111func main(argc: i64, argv: *i64) -> i64 {
112 // LIVE SCAN MODE: `nx_dp_leak_gate scan <domain>` -> leak_scan on the real store, print the count.
113 if argc >= 3 {
114 if lk_streq(argv[1] as *u8, "scan" as *u8) == 1 {
115 let dom: *u8 = argv[2] as *u8
116 g_puts("=== DP LEAK SCAN (live) domain=" as *u8); g_puts(dom); g_puts(" ===\n" as *u8)
117 let n: i64 = leak_scan(dom)
118 g_puts("leak_scan domain=" as *u8); g_puts(dom); g_puts(" confirmed_leaks=" as *u8); g_num(n); g_puts("\n" as *u8)
119 if n == 0 { g_puts("LEAK-SCAN CLEAN (0 confirmed internal/SECRET markers in the public corpus)\n" as *u8); return 0 }
120 g_puts("LEAK-SCAN DIRTY (>=1 leak -- reclassify needed)\n" as *u8); return 1
121 }
122 }
123
124 // HERMETIC GATE MODE: fresh nonce domain so append-only tombstones never poison a re-run.
125 g_puts("=== DP LEAK GATE (public corpus carries NO internal/SECRET marker; RED on leak) ===\n" as *u8)
126 let dom: *u8 = sys_mmap(64)
127 var dn: i64 = g_cat(dom, 0, "dpleak" as *u8)
128 dn = g_catnum(dom, dn, sys_now_realtime_sec())
129 dom[dn] = 0 as u8
130
131 let tLeak: *u8 = "SCLASSLEAK NISHI S-CLASS-EXCEED PROGRAM hermetic-gate internal backlog fixture." as *u8
132 let tClean: *u8 = "CLEANFIX Public probate timeline overview for Utah families." as *u8
133 let cidL: *i64 = sys_mmap(16) as *i64
134 let cidC: *i64 = sys_mmap(16) as *i64
135 dp_ingest_policy(dom, DP_VIS_PUBLIC, DP_USE_PUB_SEARCH, tLeak, g_slen(tLeak), cidL)
136 dp_ingest_policy(dom, DP_VIS_PUBLIC, DP_USE_PUB_SEARCH, tClean, g_slen(tClean), cidC)
137
138 var pass: i64 = 0
139
140 // T1: a real leak in the public corpus is DETECTED
141 let n1: i64 = leak_scan(dom)
142 var t1: i64 = 0; if n1 >= 1 { t1 = 1 } pass = pass + t1
143 g_puts(" T1 leak DETECTED (leak_scan=" as *u8); g_num(n1); g_puts(", expect >=1): " as *u8)
144 if t1 == 1 { g_puts("PASS\n" as *u8) } else { g_puts("FAIL\n" as *u8) }
145
146 // T2: reclassify the leaking doc -> the corpus is CLEAN
147 let rc: i64 = dpr_run(dom, cidL[0])
148 let n2: i64 = leak_scan(dom)
149 var t2: i64 = 0; if rc == 0 { if n2 == 0 { t2 = 1 } } pass = pass + t2
150 g_puts(" T2 reclassify closes the leak (rc=" as *u8); g_num(rc); g_puts(" leak_scan=" as *u8); g_num(n2); g_puts(", expect 0): " as *u8)
151 if t2 == 1 { g_puts("PASS\n" as *u8) } else { g_puts("FAIL\n" as *u8) }
152
153 // T3: neg-control -- the clean public doc is still searchable AND was never counted as a leak (no false positive)
154 let scids: *i64 = sys_mmap(16 * 8) as *i64
155 let sscores: *i64 = sys_mmap(16 * 8) as *i64
156 let qc: *u8 = "cleanfix" as *u8
157 let ncl: i64 = dss_search(dom, qc, g_slen(qc), scids, sscores, 16)
158 var foundClean: i64 = 0
159 if ncl >= 1 { if scids[0] == cidC[0] { foundClean = 1 } }
160 var t3: i64 = 0; if foundClean == 1 { if n2 == 0 { t3 = 1 } } pass = pass + t3
161 g_puts(" T3 neg-control clean doc searchable + not counted (found=" as *u8); g_num(foundClean); g_puts(" leak_scan=" as *u8); g_num(n2); g_puts("): " as *u8)
162 if t3 == 1 { g_puts("PASS\n" as *u8) } else { g_puts("FAIL\n" as *u8) }
163
164 g_puts("----\nDP-LEAK rows=3 pass=" as *u8); g_num(pass); g_puts("\n" as *u8)
165 let lg: i64 = sys_openat_append("knowledge/status/dp_leak_gate.log" as *u8, 0x1a4)
166 if lg >= 0 {
167 g_w(lg, "DP-LEAK detect=" as *u8); g_wn(lg, t1); g_w(lg, " reclassify_clean=" as *u8); g_wn(lg, t2)
168 g_w(lg, " negcontrol=" as *u8); g_wn(lg, t3); g_w(lg, " rows=3 pass=" as *u8); g_wn(lg, pass)
169 if pass == 3 { g_w(lg, " verdict=GREEN\n" as *u8) } else { g_w(lg, " verdict=RED\n" as *u8) }
170 sys_close(lg)
171 }
172 if pass == 3 { g_puts("DP-LEAK GREEN (public corpus clean by construction; leak detected + closed + neg-control -- measured)\n" as *u8); sys_exit(0); return 0 }
173 g_puts("DP-LEAK RED\n" as *u8); sys_exit(1); return 1
174}