code wiki / _hdl_build / nx_kanon.nx
nx_kanon.nx source
↩ module page · 47 lines · 2138 B
1// nx_kanon.nx -- LIB (PURE math, no I/O): the k-anonymity FLOOR primitive. Load-bearing privacy
2// foundation under ADS-018 conversion measurement AND W-AN-CHARTER-002 / W-CLI-C6 anti-harm
3// analytics (DRY rule 15 -- ONE k-anon home). Non-ads named on purpose so the analytics charter
4// can ride it without importing ad code.
5//
6// THE INVARIANT: an aggregate count is RELEASED only when it cannot single out a person -- i.e.
7// count == 0 (reveals nobody) or count >= K. A count in [1, K-1] is SUPPRESSED. After release, NO
8// released cell carries a count in [1, K-1] (cells_below_k == 0).
9//
10// PURE: K arrives as a parameter; the SOURCE of K is config in the sovereign store (the ad engine
11// reads it via nx_ad_store -> adcfg:kanon_k), never a TSV/conf flat file. Keeping this lib pure
12// means any consumer (ads, analytics) supplies its own K source. license_tier: ORIGINAL
13import "nx_syscalls.nx"
14
15// kn_release: 1 if a cell with count n MAY be released under floor k, else 0.
16// release iff n == 0 (reveals no person) or n >= k. cells in [1, k-1] are SUPPRESSED.
17func kn_release(n: i64, k: i64) -> i64 {
18 if n == 0 { return 1 }
19 if n >= k { return 1 }
20 return 0
21}
22
23// kn_apply: write the RELEASED value of each raw cell into out[] -- the true count if releasable,
24// else the sentinel -1 ("<k", suppressed). Returns the suppressed-cell count.
25func kn_apply(raw: *i64, m: i64, k: i64, out: *i64) -> i64 {
26 var sup: i64 = 0
27 var i: i64 = 0
28 while i < m {
29 if kn_release(raw[i], k) == 1 { out[i] = raw[i] } else { out[i] = 0 - 1; sup = sup + 1 }
30 i = i + 1
31 }
32 return sup
33}
34
35// kn_count_below: the INVARIANT CHECKER / violation detector. Over a RELEASED set vals[0..m], count
36// cells that LEAK (0 < val < k). A correct release has 0. The gate proves this detector FIRES on a
37// planted leak -- so GREEN means "proven to catch a violation", never "always returns 0".
38func kn_count_below(vals: *i64, m: i64, k: i64) -> i64 {
39 var c: i64 = 0
40 var i: i64 = 0
41 while i < m {
42 let v: i64 = vals[i]
43 if v > 0 { if v < k { c = c + 1 } }
44 i = i + 1
45 }
46 return c
47}