code wiki / (root) / nx_fin_rules_gate.nx

nx_fin_rules_gate.nx source

↩ module page · 133 lines · 6811 B

1// nx_fin_rules_gate.nx -- R6 GATE: proves the audit rule packs round-trip through the sovereign seg-store 2// and that the audit runs off LOADED packs (not gate-supplied constants) = the data-driven, robust-for- 3// anyone invariant. Stores NCCI-bundle / MUE / Medicare-benchmark packs for jurisdiction "US-2025", loads 4// them, runs the R5 audit on the cancer bill off the loaded packs (same findings as R5), then stores a 5// second "STRICT" jurisdiction pack (MUE max 1) and shows the audit result CHANGES with the pack. Negative 6// control: an absent pack returns -1. Hermetic (/tmp prefix). Exits 0 iff ALL pass. license_tier: ORIGINAL 7import "nx_syscalls.nx" 8import "nx_money.nx" 9import "nx_fin_audit.nx" 10import "nx_fin_rules.nx" 11 12func 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 } 13func g_putn(v: i64) -> i64 { 14 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 } 15 var m: i64 = v; if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 16 let d: *u8 = sys_mmap(24); var k: i64 = 0 17 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 18 var j: i64 = k - 1 19 while j >= 0 { sys_write(1, ((d as i64)+j) as *u8, 1); j = j - 1 } 20 return 0 21} 22func chk(name: *u8, got: i64, want: i64, st: *i64) -> i64 { 23 if got == want { st[0] = st[0] + 1; g_puts(" PASS "); g_puts(name); g_puts("\n") } 24 else { st[1] = st[1] + 1; g_puts(" FAIL "); g_puts(name); g_puts(" got="); g_putn(got); g_puts(" want="); g_putn(want); g_puts("\n") } 25 return 0 26} 27func seti(a: *i64, i: i64, v: i64) -> i64 { a[i] = v; return 0 } 28 29func main() -> i64 { 30 let st: *i64 = sys_mmap(16) as *i64 31 st[0] = 0; st[1] = 0 32 33 // hermetic per-run prefix "/tmp/finrules_<us>-" 34 let pfx: *u8 = sys_mmap(64) 35 var pp: i64 = 0 36 let pre: *u8 = "/tmp/finrules_\x00" as *u8 37 while pre[pp] != (0 as u8) { pfx[pp] = pre[pp]; pp = pp + 1 } 38 let us: i64 = sys_now_us() 39 let tmp: *u8 = sys_mmap(32); var m: i64 = us; var kk: i64 = 0 40 if m == 0 { tmp[0] = 0x30 as u8; kk = 1 } 41 while m > 0 { tmp[kk] = (0x30 + (m % 10)) as u8; m = m / 10; kk = kk + 1 } 42 var z: i64 = kk - 1 43 while z >= 0 { pfx[pp] = tmp[z]; pp = pp + 1; z = z - 1 } 44 pfx[pp] = 0x2d as u8; pp = pp + 1; pfx[pp] = 0 as u8 45 46 let US: *u8 = "US-2025\x00" as *u8 47 48 // ---- store packs as DATA ---- 49 let bnd: *i64 = sys_mmap(8 * 8) as *i64 50 seti(bnd,0,96413); seti(bnd,1,36415) 51 chk("store bundle pack", rp_put(pfx, US, "bundle\x00" as *u8, bnd, 2), 0, st) 52 let mue: *i64 = sys_mmap(8 * 8) as *i64 53 seti(mue,0,85025); seti(mue,1,3) 54 chk("store MUE pack", rp_put(pfx, US, "mue\x00" as *u8, mue, 2), 0, st) 55 let bch: *i64 = sys_mmap(8 * 16) as *i64 56 seti(bch,0,99214); seti(bch,1,13000); seti(bch,2,96413); seti(bch,3,30000); seti(bch,4,85025); seti(bch,5,1100) 57 chk("store benchmark pack", rp_put(pfx, US, "bench\x00" as *u8, bch, 6), 0, st) 58 59 // ---- load + round-trip ---- 60 let lb: *i64 = sys_mmap(8 * 16) as *i64 61 let nb: i64 = rp_get(pfx, US, "bundle\x00" as *u8, lb, 16) 62 chk("bundle round-trips (n=2)", nb, 2, st) 63 chk(" bundle[0]=96413", lb[0], 96413, st) 64 chk(" bundle[1]=36415", lb[1], 36415, st) 65 let lm: *i64 = sys_mmap(8 * 16) as *i64 66 let nm: i64 = rp_get(pfx, US, "mue\x00" as *u8, lm, 16) 67 chk("MUE round-trips (n=2, max=3)", lm[1], 3, st) 68 let lc: *i64 = sys_mmap(8 * 16) as *i64 69 let nc: i64 = rp_get(pfx, US, "bench\x00" as *u8, lc, 16) 70 chk("benchmark round-trips (n=6)", nc, 6, st) 71 chk(" bench 96413 allowed=30000", lc[3], 30000, st) 72 73 // negative control: absent pack -> -1 74 chk("absent pack returns -1", rp_get(pfx, US, "nope\x00" as *u8, lb, 16), 0 - 1, st) 75 76 // ---- run the R5 audit OFF the loaded packs ---- 77 let codes: *i64 = sys_mmap(8 * 8) as *i64 78 let dates: *i64 = sys_mmap(8 * 8) as *i64 79 let units: *i64 = sys_mmap(8 * 8) as *i64 80 let billed: *i64 = sys_mmap(8 * 8) as *i64 81 seti(codes,0,99214); seti(dates,0,20250310); seti(units,0,1); seti(billed,0,40000) 82 seti(codes,1,96413); seti(dates,1,20250310); seti(units,1,1); seti(billed,1,200000) 83 seti(codes,2,96413); seti(dates,2,20250310); seti(units,2,1); seti(billed,2,200000) 84 seti(codes,3,36415); seti(dates,3,20250310); seti(units,3,1); seti(billed,3,15000) 85 seti(codes,4,85025); seti(dates,4,20250310); seti(units,4,8); seti(billed,4,80000) 86 87 // deinterleave loaded bundle -> pairA/pairB 88 let pairA: *i64 = sys_mmap(8 * 8) as *i64 89 let pairB: *i64 = sys_mmap(8 * 8) as *i64 90 var i: i64 = 0 91 while i < nb / 2 { pairA[i] = lb[2 * i]; pairB[i] = lb[2 * i + 1]; i = i + 1 } 92 // deinterleave loaded MUE -> mcode/mmax 93 let mcode: *i64 = sys_mmap(8 * 8) as *i64 94 let mmax: *i64 = sys_mmap(8 * 8) as *i64 95 i = 0 96 while i < nm / 2 { mcode[i] = lm[2 * i]; mmax[i] = lm[2 * i + 1]; i = i + 1 } 97 // deinterleave loaded benchmark -> bcode/ballow 98 let bcode: *i64 = sys_mmap(8 * 8) as *i64 99 let ballow: *i64 = sys_mmap(8 * 8) as *i64 100 i = 0 101 while i < nc / 2 { bcode[i] = lc[2 * i]; ballow[i] = lc[2 * i + 1]; i = i + 1 } 102 103 let idx: *i64 = sys_mmap(8 * 16) as *i64 104 let amt: *i64 = sys_mmap(8 * 16) as *i64 105 let oc: *i64 = sys_mmap(16) as *i64 106 107 chk("audit-off-store: unbundle count 1", aud_unbundle(codes, 5, pairA, pairB, nb / 2, billed, idx, amt, oc), 1, st) 108 chk(" unbundle overcharge 15000", oc[0], 15000, st) 109 let mc: i64 = aud_mue(codes, units, billed, 5, mcode, mmax, nm / 2, idx, amt, oc) 110 chk("audit-off-store: MUE count 1", mc, 1, st) 111 chk(" MUE overcharge 50000", oc[0], 50000, st) 112 chk("audit-off-store: fair-price(4x) count 3", aud_fairprice(codes, billed, 5, bcode, ballow, nc / 2, 400, idx, amt, oc), 3, st) 113 chk(" fair-price overcharge 235600", oc[0], 235600, st) 114 115 // ---- a STRICTER jurisdiction pack changes the result (data-driven, not magic) ---- 116 let mue2: *i64 = sys_mmap(8 * 8) as *i64 117 seti(mue2,0,85025); seti(mue2,1,1) 118 rp_put(pfx, "STRICT\x00" as *u8, "mue\x00" as *u8, mue2, 2) 119 let lm2: *i64 = sys_mmap(8 * 16) as *i64 120 let nm2: i64 = rp_get(pfx, "STRICT\x00" as *u8, "mue\x00" as *u8, lm2, 16) 121 let mcode2: *i64 = sys_mmap(8 * 8) as *i64 122 let mmax2: *i64 = sys_mmap(8 * 8) as *i64 123 i = 0 124 while i < nm2 / 2 { mcode2[i] = lm2[2 * i]; mmax2[i] = lm2[2 * i + 1]; i = i + 1 } 125 aud_mue(codes, units, billed, 5, mcode2, mmax2, nm2 / 2, idx, amt, oc) 126 g_puts(" [info] STRICT MUE (max 1) overcharge_cents="); g_putn(oc[0]); g_puts("\n") 127 chk("STRICT pack changes the audit: overcharge 70000 ($700)", oc[0], 70000, st) 128 129 g_puts("nx_fin_rules_gate: PASS="); g_putn(st[0]); g_puts(" FAIL="); g_putn(st[1]); g_puts("\n") 130 if st[1] == 0 { g_puts("R6 nx_fin_rules: GREEN\n"); return 0 } 131 g_puts("R6 nx_fin_rules: RED\n") 132 return 1 133}