_simhash_popcount_gate.nx source
↩ module page · 51 lines · 2839 B
1// _simhash_popcount_gate.nx -- byte-identity KAT for SPD-ALG-SEARCH-01 (search-popcount-swar).
2// Proves nx_simhash_hamming (now the SWAR nx_bits_popcount64_soft) returns the EXACT Hamming
3// distance over ALL distances 0..64 + sign-bit/high-bit vectors (the SWAR arithmetic-shift hazard),
4// with a NEG control proving the gate has teeth (a constant-returning impl is rejected). The old
5// 64-iter bit-test loop and the new SWAR both equal popcount(a^b), so equality here = byte-identical.
6// Emits the SPDGATE probe the speed census (speed_audit_ref.tsv) re_has-matches. Sovereign native
7// lane: exit 0 = GREEN, N = first failed assertion. Judge by exit/printed marker (LM-010), one build+run.
8import "syscalls.nx"
9import "nx_simhash.nx"
10
11const SG_LOG: *u8 = "knowledge/status/speed_gate.log"
12
13func gw(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 }
14
15func main() -> i64 {
16 // 1. Full-range KAT: distance d via vectors (0, mask_d) where mask_d has d low bits set.
17 // Built by a loop so d=64 never needs the overflowing literal 1<<64.
18 var d: i64 = 0
19 while d <= 64 {
20 var mask: i64 = 0
21 var i: i64 = 0
22 while i < d {
23 mask = mask | (1 << i)
24 i = i + 1
25 }
26 if nx_simhash_hamming(0, mask) != d { return 1 }
27 if nx_simhash_hamming(mask, 0) != d { return 2 }
28 d = d + 1
29 }
30
31 // 2. Sign-bit / high-bit vectors (the SWAR's arithmetic-shift hazard, the one real risk).
32 if nx_simhash_hamming(0, 0x8000000000000000) != 1 { return 3 } // bit 63 only
33 if nx_simhash_hamming(0xAAAAAAAAAAAAAAAA, 0x5555555555555555) != 64 { return 4 } // all bits differ
34 if nx_simhash_hamming(0xFFFFFFFF00000000, 0) != 32 { return 5 } // high half set
35 if nx_simhash_hamming(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF) != 0 { return 6 } // identical incl bit63
36
37 // 3. NEG control: the function MUST discriminate. A constant-returning (broken) impl would make
38 // these equal -> the gate would have no teeth; this clause forces a real RED on that failure.
39 if nx_simhash_hamming(0, 0xF) == nx_simhash_hamming(0, 0xFF) { return 7 }
40 if nx_simhash_hamming(0, 0xF) != 4 { return 8 }
41 if nx_simhash_hamming(0, 0xFF) != 8 { return 9 }
42
43 // GREEN: emit the probe (speed census re_has matches `feature=search-popcount-swar`).
44 gw(1, "SPDGATE feature=search-popcount-swar verdict=GREEN base_iters=64 new=swar-popcount distances=0..64 ||MARK=knowledge/status/speed_gate.log::SPDGATE::verdict=GREEN\n" as *u8)
45 let lf: i64 = sys_openat_append(SG_LOG, 420)
46 if lf >= 0 {
47 gw(lf, "SPDGATE feature=search-popcount-swar verdict=GREEN base_iters=64 new=swar-popcount distances=0..64\n" as *u8)
48 sys_close(lf)
49 }
50 return 0
51}