nx_triangulation_crypto_fft.nx source
↩ module page · 134 lines · 5680 B
1// nx_triangulation_crypto_fft.nx -- triangulation battery 3.
2// Covers hash functions (FNV-1a / DJB2 / Jenkins / Adler-32), FFT/IFFT
3// roundtrip, and Bloom filter insert/contains roundtrip. All checks
4// are substrate-internal; no external dependencies.
5
6// nx_safety_envelope:
7// intended_use: AUTO_APPLIED -- primitive-specific tuning queued
8// sil_target: SIL1
9// evidence: [bulk_applied_2026-05-16, see-file-comment-for-detail]
10// verdict: NOT_YET_EVALUATED
11
12import "nx_syscalls.nx"
13import "nx_runtime.nx"
14import "nx_tier.nx"
15import "nx_classical_unpatented_3.nx"
16import "nx_fft.nx"
17import "nx_sketch_bloom.nx"
18
19func nx_tri_check(label: *u8, aut: nx_int, w1: nx_int, w2: nx_int,
20 agree: *nx_int, fail: *nx_int) {
21 print(label); print(": AUT=" as *u8); print_i64(aut)
22 print(" W1=" as *u8); print_i64(w1); print(" W2=" as *u8); print_i64(w2)
23 if aut == w1 {
24 if aut == w2 {
25 println(" -> TRIANGULATED" as *u8)
26 agree[0] = agree[0] + 1
27 return
28 }
29 }
30 println(" -> DISAGREE" as *u8)
31 fail[0] = fail[0] + 1
32}
33
34func main() -> nx_exit {
35 let agree: *nx_int = (sys_mmap(8)) as *nx_int
36 let fail: *nx_int = (sys_mmap(8)) as *nx_int
37 agree[0] = 0
38 fail[0] = 0
39
40 println("=== TRIANGULATION: CRYPTO + FFT + BLOOM BATTERY ===" as *u8)
41
42 // === Hash functions: cross-hash determinism ========================
43 // For each hash, test that hash(input) is consistent across two
44 // independent invocations (W2 = re-hash same input) AND matches a
45 // known relationship between hashes of different inputs.
46 let in_a: *u8 = "a" as *u8
47 let in_b: *u8 = "b" as *u8
48
49 // FNV-1a 32: hash("a") and hash("b") must differ; hash("a") second call must match.
50 let fnv_a_1: nx_int = nx_fnv1a_32_buf(in_a, 1)
51 let fnv_a_2: nx_int = nx_fnv1a_32_buf(in_a, 1)
52 let fnv_b: nx_int = nx_fnv1a_32_buf(in_b, 1)
53 nx_tri_check("fnv1a('a') determinism " as *u8,
54 fnv_a_1, fnv_a_2, fnv_a_2, agree, fail)
55 nx_tri_check("fnv1a('a') vs fnv1a('b') " as *u8,
56 nx_fnv1a_32_buf(in_a, 1) - nx_fnv1a_32_buf(in_b, 1),
57 fnv_a_1 - fnv_b,
58 fnv_a_1 - fnv_b,
59 agree, fail)
60
61 // DJB2: same determinism check
62 let djb_a_1: nx_int = nx_djb2_buf(in_a, 1)
63 let djb_a_2: nx_int = nx_djb2_buf(in_a, 1)
64 let djb_b: nx_int = nx_djb2_buf(in_b, 1)
65 nx_tri_check("djb2('a') determinism " as *u8,
66 djb_a_1, djb_a_2, djb_a_2, agree, fail)
67
68 // Jenkins one-at-a-time
69 let jen_a_1: nx_int = nx_jenkins_oaat_buf(in_a, 1)
70 let jen_a_2: nx_int = nx_jenkins_oaat_buf(in_a, 1)
71 nx_tri_check("jenkins('a') determinism " as *u8,
72 jen_a_1, jen_a_2, jen_a_2, agree, fail)
73
74 // Adler-32: hash("a") should equal published value 0x00620062 = 6422626
75 // (Adler-32 of single byte 'a' (0x61=97): s1 = 1+97=98, s2 = 0+98=98,
76 // result = (s2 << 16) | s1 = (98<<16)|98 = 0x00620062 = 6422626)
77 let adl_a_1: nx_int = nx_adler32_buf(in_a, 1)
78 let adl_a_2: nx_int = nx_adler32_buf(in_a, 1)
79 nx_tri_check("adler32('a') known vector " as *u8,
80 adl_a_1, 6422626, adl_a_2, agree, fail)
81
82 // === FFT roundtrip ================================================
83 // FFT then IFFT should give back the original array (scaled by 1/N).
84 // W1: original input value; W2: scaled inverse re-evaluation.
85 // Set up an 8-element complex array (real-only initially).
86 let re: *nx_int = (sys_mmap(64)) as *nx_int
87 let im: *nx_int = (sys_mmap(64)) as *nx_int
88 re[0] = 1000; re[1] = 0; re[2] = 0; re[3] = 0
89 re[4] = 0; re[5] = 0; re[6] = 0; re[7] = 0
90 im[0] = 0; im[1] = 0; im[2] = 0; im[3] = 0
91 im[4] = 0; im[5] = 0; im[6] = 0; im[7] = 0
92 let _f: nx_int = nx_fft_forward(re, im, 8)
93 let _g: nx_int = nx_fft_inverse(re, im, 8)
94 // After IFFT, re[0] should be back near original 1000 (allow small drift).
95 // Tolerance: within 2.
96 let after: nx_int = re[0]
97 var roundtrip_ok: nx_int = 0
98 if after >= 998 {
99 if after <= 1002 { roundtrip_ok = 1 }
100 }
101 nx_tri_check("fft_forward+inverse[0] " as *u8,
102 roundtrip_ok, 1, 1, agree, fail)
103
104 // === Bloom filter insert/contains roundtrip =======================
105 // After insert(key), contains(key) must return 1.
106 // Before insert OR for a different key, contains may return 0.
107 let bf: *BloomS = nx_bloom_alloc(1024, 4)
108 let _x: nx_int = nx_bloom_insert(bf, in_a, 1)
109 let c_a: nx_int = nx_bloom_contains(bf, in_a, 1)
110 let c_a2: nx_int = nx_bloom_contains(bf, in_a, 1)
111 nx_tri_check("bloom(insert a, contains a)" as *u8,
112 c_a, 1, c_a2, agree, fail)
113
114 // === Adler-32 on longer input ====================================
115 let in_aa: *u8 = "aa" as *u8
116 let adl_aa: nx_int = nx_adler32_buf(in_aa, 2)
117 let adl_aa_check: nx_int = nx_adler32_buf(in_aa, 2)
118 nx_tri_check("adler32('aa') determinism " as *u8,
119 adl_aa, adl_aa_check, adl_aa_check, agree, fail)
120
121 // FNV-1a of empty string: published constant 0x811C9DC5 = 2166136261
122 let in_empty: *u8 = "" as *u8
123 let fnv_empty: nx_int = nx_fnv1a_32_buf(in_empty, 0)
124 nx_tri_check("fnv1a('') = offset_basis " as *u8,
125 fnv_empty, 2166136261, 2166136261, agree, fail)
126
127 println("" as *u8)
128 println("============================================" as *u8)
129 print("TRIANGULATED: " as *u8); print_i64(agree[0]); println("" as *u8)
130 print("DISAGREE: " as *u8); print_i64(fail[0]); println("" as *u8)
131 println("============================================" as *u8)
132 if fail[0] > 0 { return 1 }
133 return 0
134}