code wiki / (root) / nx_ch_census_race_gate.nx

nx_ch_census_race_gate.nx source

↩ module page · 242 lines · 9703 B

1// nx_ch_census_race_gate.nx -- the REFEREE for the CH-census autonomy race (Referee=SCORE, 2// no self-grade: this gate is mechanical -- neither lane's permil is asserted by its author). 3// Both lanes get byte-identical staged inputs and the same 12 scoring rows: 4// real block: 5 rows -- SNI/GR/SG/SV/KS body offsets vs the SHIPPED tls13_ext_find oracle 5// (the shipped parser predates both lanes = neutral ground truth) 6// trunc block: 1 row -- EXTN must be -1 (truncation REFUSED; the gate built the cut, so it 7// knows the truth by construction) 8// empty block: 6 rows -- EXTN=0 and all five fields -1 (by construction) 9// Output: RACE row= lines + per-lane permil + verdict (TEAM-EXCEEDS / TEAM-MATCHES / 10// TEAM-BEHIND) -> stdout + knowledge/status/race_ch_census.log (Archivist rule). 11// Run AFTER nx_ch_census_race_stage + both lane ELFs are built. CWD=nxc2/. 12// license_tier: ORIGINAL 13import "nx_syscalls.nx" 14import "nx_tls13_hello.nx" 15 16func g_puts(fd: i64, s: *u8) -> i64 { 17 var n: i64 = 0 18 while s[n] != (0 as u8) { n = n + 1 } 19 sys_write(fd, s, n) 20 return 0 21} 22 23func g_putn(fd: i64, v: i64) -> i64 { 24 let bb: *u8 = sys_mmap(28) 25 var m: i64 = v 26 if m < 0 { m = 0 - m; sys_write(fd, "-\x00" as *u8, 1) } 27 let t: *u8 = sys_mmap(28) 28 var k: i64 = 0 29 if m == 0 { t[0] = 48 as u8; k = 1 } 30 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 31 var i: i64 = 0 32 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } 33 sys_write(fd, bb, k) 34 return 0 35} 36 37// stage one source file as the lanes' fixed input path 38func g_stage(src: *u8) -> i64 { 39 let lenp: *i64 = sys_mmap(16) as *i64 40 let b: *u8 = sys_read_file(src, lenp) 41 let n: i64 = lenp[0] 42 let fd: i64 = sys_openat_wr("/tmp/race_input.bin\x00" as *u8, 0x1a4) 43 if fd < 0 { return 0 - 1 } 44 if n > 0 { sys_write(fd, b, n) } 45 sys_close(fd) 46 return n 47} 48 49// run a lane ELF with stdout captured to /tmp/race_lane_out.txt 50func g_run_lane(elf: *u8) -> i64 { 51 let pid: i64 = sys_fork() 52 if pid == 0 { 53 let ofd: i64 = sys_openat_wr("/tmp/race_lane_out.txt\x00" as *u8, 0x1a4) 54 if ofd >= 0 { sys_dup3(ofd, 1, 0) } 55 let argv: *i64 = sys_mmap(32) as *i64 56 argv[0] = elf as i64; argv[1] = 0 57 let envp: *i64 = sys_mmap(16) as *i64 58 envp[0] = 0 59 sys_execve(elf, argv, envp) 60 sys_exit(127) 61 } 62 let st: *i64 = sys_mmap(16) as *i64 63 sys_wait4(pid, st, 0) 64 return st[0] 65} 66 67// parse "<key><signed int>" from the captured lane output; -999 = key missing 68func g_parse(buf: *u8, blen: i64, key: *u8) -> i64 { 69 var klen: i64 = 0 70 while key[klen] != (0 as u8) { klen = klen + 1 } 71 var i: i64 = 0 72 var at: i64 = 0 - 1 73 while i + klen <= blen { 74 var j: i64 = 0 75 var ok: i64 = 1 76 while j < klen { 77 if buf[i + j] != key[j] { ok = 0; j = klen } 78 if ok == 1 { j = j + 1 } 79 } 80 if ok == 1 { at = i; i = blen } 81 i = i + 1 82 } 83 if at < 0 { return 0 - 999 } 84 var p: i64 = at + klen 85 var neg: i64 = 0 86 if p < blen { if buf[p] == (45 as u8) { neg = 1; p = p + 1 } } 87 var v: i64 = 0 88 var got: i64 = 0 89 var scanning: i64 = 1 90 while scanning == 1 { 91 if p >= blen { scanning = 0 } 92 if scanning == 1 { 93 let c: i64 = buf[p] & 0xff 94 if c < 48 { scanning = 0 } 95 if c > 57 { scanning = 0 } 96 if scanning == 1 { v = v * 10 + (c - 48); got = 1; p = p + 1 } 97 } 98 } 99 if got == 0 { return 0 - 999 } 100 if neg == 1 { return 0 - v } 101 return v 102} 103 104// one scored row: prints to stdout + log, returns 1 iff got==want 105func g_row(logfd: i64, rowname: *u8, got: i64, want: i64) -> i64 { 106 var fdi: i64 = 0 107 while fdi < 2 { 108 var fd: i64 = 1 109 if fdi == 1 { fd = logfd } 110 if fd > 0 { 111 g_puts(fd, "RACE row=\x00" as *u8) 112 g_puts(fd, rowname) 113 g_puts(fd, " got=\x00" as *u8) 114 g_putn(fd, got) 115 g_puts(fd, " want=\x00" as *u8) 116 g_putn(fd, want) 117 if got == want { g_puts(fd, " verdict=PASS\n\x00" as *u8) } 118 if got != want { g_puts(fd, " verdict=FAIL\n\x00" as *u8) } 119 } 120 fdi = fdi + 1 121 } 122 if got == want { return 1 } 123 return 0 124} 125 126// run one lane over the 3 staged cases; exp = the 5 real-case oracle offsets. 127// rows = 12; returns rows passed. names[] selects the lane's row labels. 128func g_lane(logfd: i64, elf: *u8, names: *i64, exp: *i64) -> i64 { 129 var passed: i64 = 0 130 let lenp: *i64 = sys_mmap(16) as *i64 131 132 g_stage("/tmp/race_block.bin\x00" as *u8) 133 g_run_lane(elf) 134 var ob: *u8 = sys_read_file("/tmp/race_lane_out.txt\x00" as *u8, lenp) 135 var on: i64 = lenp[0] 136 passed = passed + g_row(logfd, names[0] as *u8, g_parse(ob, on, "SNI=\x00" as *u8), exp[0]) 137 passed = passed + g_row(logfd, names[1] as *u8, g_parse(ob, on, "GR=\x00" as *u8), exp[1]) 138 passed = passed + g_row(logfd, names[2] as *u8, g_parse(ob, on, "SG=\x00" as *u8), exp[2]) 139 passed = passed + g_row(logfd, names[3] as *u8, g_parse(ob, on, "SV=\x00" as *u8), exp[3]) 140 passed = passed + g_row(logfd, names[4] as *u8, g_parse(ob, on, "KS=\x00" as *u8), exp[4]) 141 142 g_stage("/tmp/race_trunc.bin\x00" as *u8) 143 g_run_lane(elf) 144 ob = sys_read_file("/tmp/race_lane_out.txt\x00" as *u8, lenp) 145 on = lenp[0] 146 passed = passed + g_row(logfd, names[5] as *u8, g_parse(ob, on, "EXTN=\x00" as *u8), 0 - 1) 147 148 g_stage("/tmp/race_empty.bin\x00" as *u8) 149 g_run_lane(elf) 150 ob = sys_read_file("/tmp/race_lane_out.txt\x00" as *u8, lenp) 151 on = lenp[0] 152 passed = passed + g_row(logfd, names[6] as *u8, g_parse(ob, on, "EXTN=\x00" as *u8), 0) 153 passed = passed + g_row(logfd, names[7] as *u8, g_parse(ob, on, "SNI=\x00" as *u8), 0 - 1) 154 passed = passed + g_row(logfd, names[8] as *u8, g_parse(ob, on, "GR=\x00" as *u8), 0 - 1) 155 passed = passed + g_row(logfd, names[9] as *u8, g_parse(ob, on, "SG=\x00" as *u8), 0 - 1) 156 passed = passed + g_row(logfd, names[10] as *u8, g_parse(ob, on, "SV=\x00" as *u8), 0 - 1) 157 passed = passed + g_row(logfd, names[11] as *u8, g_parse(ob, on, "KS=\x00" as *u8), 0 - 1) 158 return passed 159} 160 161func main() -> i64 { 162 let logfd: i64 = sys_openat_append("knowledge/status/race_ch_census.log\x00" as *u8, 0x1a4) 163 var fdi: i64 = 0 164 while fdi < 2 { 165 var fd: i64 = 1 166 if fdi == 1 { fd = logfd } 167 if fd > 0 { 168 g_puts(fd, "RACE epoch=\x00" as *u8) 169 g_putn(fd, sys_now_realtime_sec()) 170 g_puts(fd, " assignment=ch-extension-census lanes=team,claude rows=12\n\x00" as *u8) 171 } 172 fdi = fdi + 1 173 } 174 175 // ---- oracle: the SHIPPED extension finder on the real block ---- 176 let lenp: *i64 = sys_mmap(16) as *i64 177 let blk: *u8 = sys_read_file("/tmp/race_block.bin\x00" as *u8, lenp) 178 let bn: i64 = lenp[0] 179 if bn <= 0 { g_puts(1, "STAGE MISSING -- run nx_ch_census_race_stage first\n\x00" as *u8); return 2 } 180 let exp: *i64 = sys_mmap(64) as *i64 181 let types: *i64 = sys_mmap(64) as *i64 182 types[0] = 0; types[1] = 10; types[2] = 13; types[3] = 43; types[4] = 51 183 let p_off: *i64 = sys_mmap(16) as *i64 184 let p_len: *i64 = sys_mmap(16) as *i64 185 var ti: i64 = 0 186 while ti < 5 { 187 var want: i64 = 0 - 1 188 if tls13_ext_find(blk, bn, types[ti], p_off, p_len) == NX_TLS13_HELLO_VERDICT_OK { want = *p_off } 189 exp[ti] = want 190 ti = ti + 1 191 } 192 193 let tnames: *i64 = sys_mmap(8 * 12) as *i64 194 tnames[0] = "team:real:SNI\x00" as *u8 as i64 195 tnames[1] = "team:real:GR\x00" as *u8 as i64 196 tnames[2] = "team:real:SG\x00" as *u8 as i64 197 tnames[3] = "team:real:SV\x00" as *u8 as i64 198 tnames[4] = "team:real:KS\x00" as *u8 as i64 199 tnames[5] = "team:trunc:EXTN\x00" as *u8 as i64 200 tnames[6] = "team:empty:EXTN\x00" as *u8 as i64 201 tnames[7] = "team:empty:SNI\x00" as *u8 as i64 202 tnames[8] = "team:empty:GR\x00" as *u8 as i64 203 tnames[9] = "team:empty:SG\x00" as *u8 as i64 204 tnames[10] = "team:empty:SV\x00" as *u8 as i64 205 tnames[11] = "team:empty:KS\x00" as *u8 as i64 206 let cnames: *i64 = sys_mmap(8 * 12) as *i64 207 cnames[0] = "claude:real:SNI\x00" as *u8 as i64 208 cnames[1] = "claude:real:GR\x00" as *u8 as i64 209 cnames[2] = "claude:real:SG\x00" as *u8 as i64 210 cnames[3] = "claude:real:SV\x00" as *u8 as i64 211 cnames[4] = "claude:real:KS\x00" as *u8 as i64 212 cnames[5] = "claude:trunc:EXTN\x00" as *u8 as i64 213 cnames[6] = "claude:empty:EXTN\x00" as *u8 as i64 214 cnames[7] = "claude:empty:SNI\x00" as *u8 as i64 215 cnames[8] = "claude:empty:GR\x00" as *u8 as i64 216 cnames[9] = "claude:empty:SG\x00" as *u8 as i64 217 cnames[10] = "claude:empty:SV\x00" as *u8 as i64 218 cnames[11] = "claude:empty:KS\x00" as *u8 as i64 219 220 let tp: i64 = g_lane(logfd, "/tmp/_team_ch_census.elf\x00" as *u8, tnames, exp) 221 let cp: i64 = g_lane(logfd, "/tmp/_claude_ch_census.elf\x00" as *u8, cnames, exp) 222 let tpermil: i64 = tp * 1000 / 12 223 let cpermil: i64 = cp * 1000 / 12 224 225 fdi = 0 226 while fdi < 2 { 227 var fd: i64 = 1 228 if fdi == 1 { fd = logfd } 229 if fd > 0 { 230 g_puts(fd, "RACE team_permil=\x00" as *u8) 231 g_putn(fd, tpermil) 232 g_puts(fd, " claude_permil=\x00" as *u8) 233 g_putn(fd, cpermil) 234 if tpermil > cpermil { g_puts(fd, " verdict=TEAM-EXCEEDS\n\x00" as *u8) } 235 if tpermil == cpermil { g_puts(fd, " verdict=TEAM-MATCHES\n\x00" as *u8) } 236 if tpermil < cpermil { g_puts(fd, " verdict=TEAM-BEHIND\n\x00" as *u8) } 237 } 238 fdi = fdi + 1 239 } 240 if logfd > 0 { sys_close(logfd) } 241 return 0 242}