code wiki / _hdl_build / nx_ad_conversion_gate.nx
nx_ad_conversion_gate.nx source
↩ module page · 78 lines · 3650 B
1// nx_ad_conversion_gate.nx -- GATE (runnable) for ADS-018 privacy-preserving conversion measure.
2// The DELIVERABLE organ: loads the SAME rules + k-anon floor used in production FROM THE SOVEREIGN
3// STORE (adcfg:botrules, adcfg:kanon_k -- pure Nishi, no TSV) and proves the full pipeline
4// (consent-gate -> bot-filter -> aggregate -> k-anon) on baked adversarial controls, parametrized
5// by the configured K. Cannot false-green: each neg control must drop its events, and a small
6// campaign must suppress.
7//
8// happy : consented human conversions summing to >= K -> released count == the exact sum
9// consent: an UNCONSENTED bucket -> contributes 0 (consent-gated)
10// bot : a CONSENTED but bot-class bucket -> contributes 0 (invalid-traffic)
11// k-anon : a consented human campaign with < K total -> SUPPRESSED ("<k", release == -1)
12//
13// Evidence -> knowledge/status/ad_conversion.log (CONVERSIONGATE authored=organ ... verdict=GREEN).
14// license_tier: ORIGINAL
15import "nx_ad_conversion.nx"
16import "nx_ad_store.nx"
17import "nx_ad_botfilter.nx"
18import "nx_kanon.nx"
19import "nx_syscalls.nx"
20
21const CG_LOG: *u8 = "knowledge/status/ad_conversion.log"
22
23func cg_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 }
24func cg_wn(fd: i64, v: i64) -> i64 { let bb: *u8 = sys_mmap(28); var m: i64=v; if m<0 {m=0-m; sys_write(fd,"-" 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(fd, bb, k); return 0 }
25
26func cg_emit(fd: i64, k: i64, rawh: i64, relh: i64, relk: i64, ok: i64) -> i64 {
27 cg_w(fd, "CONVERSIONGATE authored=organ source=store k=" as *u8); cg_wn(fd, k)
28 cg_w(fd, " happy_raw=" as *u8); cg_wn(fd, rawh)
29 cg_w(fd, " happy_released=" as *u8); cg_wn(fd, relh)
30 cg_w(fd, " small_released=" as *u8); cg_wn(fd, relk)
31 cg_w(fd, " attribution=aggregate-only-no-visitor-id" as *u8)
32 if ok == 1 { cg_w(fd, " verdict=GREEN\n" as *u8) } else { cg_w(fd, " verdict=RED\n" as *u8) }
33 return 0
34}
35
36func cg_set(ev: *i64, b: i64, consent: i64, ctype: i64, ua: i64, src: i64, js: i64, n: i64) -> i64 {
37 ev[b * 6] = consent
38 ev[b * 6 + 1] = ctype
39 ev[b * 6 + 2] = ua
40 ev[b * 6 + 3] = src
41 ev[b * 6 + 4] = js
42 ev[b * 6 + 5] = n
43 return 0
44}
45
46func main() -> i64 {
47 let rules: *i64 = sys_mmap(BF_MAXRULES * 2 * 8) as *i64
48 let nrules: i64 = ads_botrules(rules, BF_MAXRULES)
49 let k: i64 = ads_kanon_k()
50
51 let ev: *i64 = sys_mmap(256) as *i64
52 cg_set(ev, 0, 1, 2, 0, 0, 1, k + 3) // consented human purchase
53 cg_set(ev, 1, 1, 0, 0, 0, 1, 4) // consented human click-through
54 cg_set(ev, 2, 0, 0, 0, 0, 1, 99) // UNCONSENTED human -> dropped (consent-gate)
55 cg_set(ev, 3, 1, 1, 1, 1, 0, 77) // consented BOT -> dropped (bot-filter)
56 let rawh: i64 = cv_raw(ev, 4, rules, nrules)
57 let relh: i64 = cv_release(ev, 4, rules, nrules, k)
58
59 let ev2: *i64 = sys_mmap(64) as *i64
60 cg_set(ev2, 0, 1, 0, 0, 0, 1, k - 1)
61 let raws: i64 = cv_raw(ev2, 1, rules, nrules)
62 let relk: i64 = cv_release(ev2, 1, rules, nrules, k)
63
64 var ok: i64 = 1
65 if k < 2 { ok = 0 }
66 if nrules < 1 { ok = 0 }
67 if rawh != k + 7 { ok = 0 }
68 if relh != k + 7 { ok = 0 }
69 if raws != k - 1 { ok = 0 }
70 if relk != 0 - 1 { ok = 0 }
71
72 cg_emit(1, k, rawh, relh, relk, ok)
73 let lf: i64 = sys_openat_append(CG_LOG, 420)
74 if lf >= 0 { cg_emit(lf, k, rawh, relh, relk, ok); sys_close(lf) }
75
76 if ok == 1 { return 0 }
77 return 1
78}