nx_competitive_state_test.nx source
↩ module page · 163 lines · 8308 B
1// nx_competitive_state_test.nx -- KAT for competitive-state classifier.
2
3import "nx_syscalls.nx"
4import "nx_etg.nx"
5import "nx_competitive_state.nx"
6
7func main() -> i64 {
8 // ----- T1 Competitive state sealed-enum completeness -----
9 var i: i64 = 0
10 while i < NX_COMP_N {
11 if nx_comp_is_valid(i) != 1 { return 1 + i }
12 i = i + 1
13 }
14 if nx_comp_is_valid(0 - 1) != 0 { return 100 }
15 if nx_comp_is_valid(NX_COMP_N) != 0 { return 101 }
16
17 // ----- T2 Entropy window sealed-enum -----
18 var w: i64 = 0
19 while w < NX_ENTROPY_WINDOW_N {
20 if nx_entropy_window_is_valid(w) != 1 { return 110 + w }
21 w = w + 1
22 }
23 if nx_entropy_window_is_valid(NX_ENTROPY_WINDOW_N) != 0 { return 115 }
24 if nx_entropy_window_days(NX_ENTROPY_WINDOW_NONE) != -1 { return 116 }
25 if nx_entropy_window_days(NX_ENTROPY_WINDOW_FAST) != 30 { return 117 }
26 if nx_entropy_window_days(NX_ENTROPY_WINDOW_MID) != 90 { return 118 }
27 if nx_entropy_window_days(NX_ENTROPY_WINDOW_SLOW) != 365 { return 119 }
28
29 // ----- T3 Production-capable threshold -----
30 if nx_comp_is_production_capable(NX_COMP_UNCLASSIFIED) != 0 { return 120 }
31 if nx_comp_is_production_capable(NX_COMP_UNCOMPETITIVE) != 0 { return 121 }
32 if nx_comp_is_production_capable(NX_COMP_LAGGING) != 0 { return 122 }
33 if nx_comp_is_production_capable(NX_COMP_PARITY) != 1 { return 123 }
34 if nx_comp_is_production_capable(NX_COMP_LEADING) != 1 { return 124 }
35 if nx_comp_is_production_capable(NX_COMP_DOMINATING) != 1 { return 125 }
36 if nx_comp_is_production_capable(NX_COMP_STALE) != 0 { return 126 }
37 if nx_comp_is_production_capable(NX_COMP_REGRESSED_FROM_LEADING) != 0 { return 127 }
38
39 // ----- T4 Ratio classifier boundaries -----
40 // ratio < 0.5 (8192) -> UNCOMPETITIVE
41 if nx_comp_classify_ratio(0) != NX_COMP_UNCOMPETITIVE { return 130 }
42 if nx_comp_classify_ratio(8191) != NX_COMP_UNCOMPETITIVE { return 131 }
43 // ratio in [0.5, 0.95) (8192..15563) -> LAGGING
44 if nx_comp_classify_ratio(8192) != NX_COMP_LAGGING { return 132 }
45 if nx_comp_classify_ratio(15563) != NX_COMP_LAGGING { return 133 }
46 // ratio in [0.95, 1.05) (15564..17203) -> PARITY
47 if nx_comp_classify_ratio(15564) != NX_COMP_PARITY { return 134 }
48 if nx_comp_classify_ratio(16384) != NX_COMP_PARITY { return 135 } // exact 1.0
49 if nx_comp_classify_ratio(17203) != NX_COMP_PARITY { return 136 }
50 // ratio in [1.05, 1.5) (17204..24575) -> LEADING
51 if nx_comp_classify_ratio(17204) != NX_COMP_LEADING { return 137 }
52 if nx_comp_classify_ratio(24575) != NX_COMP_LEADING { return 138 }
53 // ratio >= 1.5 (24576+) -> DOMINATING
54 if nx_comp_classify_ratio(24576) != NX_COMP_DOMINATING { return 139 }
55 if nx_comp_classify_ratio(32768) != NX_COMP_DOMINATING { return 140 } // 2.0
56
57 // ----- T5 NxCompetitiveRecord init defaults UNCLASSIFIED + MID -----
58 let r_buf: *u8 = sys_mmap(128)
59 let r: *NxCompetitiveRecord = r_buf as *NxCompetitiveRecord
60 nx_comp_record_init(r, 0xCAFE)
61 if r.primitive_id_hash != 0xCAFE { return 150 }
62 if r.competitive_state != NX_COMP_UNCLASSIFIED { return 151 }
63 if r.entropy_window != NX_ENTROPY_WINDOW_MID { return 152 }
64 if r.measured_at_day != 0 { return 153 }
65
66 // ----- T6 Measurement updates state + ratio + timestamp -----
67 // Measure perf_ratio = 1.0 (exactly parity)
68 nx_comp_record_measurement(r, 0xBEEF, 16384, 20260520)
69 if r.competitive_state != NX_COMP_PARITY { return 160 }
70 if r.perf_ratio_q14 != 16384 { return 161 }
71 if r.measured_at_day != 20260520 { return 162 }
72 if r.incumbent_id_hash != 0xBEEF { return 163 }
73
74 // ----- T7 Subsequent measurement at 2.0 (dominating) updates state -----
75 nx_comp_record_measurement(r, 0xBEEF, 32768, 20260521)
76 if r.competitive_state != NX_COMP_DOMINATING { return 170 }
77
78 // ----- T8 REGRESSED_FROM_LEADING: was DOMINATING; now PARITY -----
79 nx_comp_record_measurement(r, 0xBEEF, 16384, 20260530)
80 if r.competitive_state != NX_COMP_REGRESSED_FROM_LEADING { return 180 }
81
82 // ----- T9 LEADING -> LAGGING also triggers REGRESSED -----
83 nx_comp_record_init(r, 0xCAFE)
84 nx_comp_record_measurement(r, 0xBEEF, 20000, 20260520) // LEADING (~1.22)
85 if r.competitive_state != NX_COMP_LEADING { return 190 }
86 nx_comp_record_measurement(r, 0xBEEF, 14000, 20260521) // LAGGING (~0.85)
87 if r.competitive_state != NX_COMP_REGRESSED_FROM_LEADING { return 191 }
88
89 // ----- T10 PARITY -> LEADING does NOT trigger REGRESSED (escalation) -----
90 nx_comp_record_init(r, 0xCAFE)
91 nx_comp_record_measurement(r, 0xBEEF, 16384, 20260520) // PARITY
92 nx_comp_record_measurement(r, 0xBEEF, 22000, 20260521) // LEADING
93 if r.competitive_state != NX_COMP_LEADING { return 200 }
94
95 // ----- T11 Staleness detection -----
96 // MID window = 90 days; measure on day 100, check on day 200 -> stale
97 nx_comp_record_init(r, 0xCAFE)
98 nx_comp_record_measurement(r, 0xBEEF, 16384, 100)
99 if nx_comp_record_is_stale(r, 100) != 0 { return 210 } // same day = fresh
100 if nx_comp_record_is_stale(r, 189) != 0 { return 211 } // 89 days = fresh (< 90)
101 if nx_comp_record_is_stale(r, 190) != 0 { return 212 } // 90 days = fresh (not > 90)
102 if nx_comp_record_is_stale(r, 191) != 1 { return 213 } // 91 days = stale (> 90)
103 if nx_comp_record_is_stale(r, 200) != 1 { return 214 } // 100 days = stale
104
105 // ----- T12 NEVER-MEASURED is stale -----
106 let r2_buf: *u8 = sys_mmap(128)
107 let r2: *NxCompetitiveRecord = r2_buf as *NxCompetitiveRecord
108 nx_comp_record_init(r2, 0xCAFE)
109 if nx_comp_record_is_stale(r2, 12345) != 1 { return 220 }
110
111 // ----- T13 NONE entropy window = never stale -----
112 nx_comp_record_init(r2, 0xCAFE)
113 r2.entropy_window = NX_ENTROPY_WINDOW_NONE
114 nx_comp_record_measurement(r2, 0xBEEF, 16384, 100)
115 // Even after 10 years, not stale
116 if nx_comp_record_is_stale(r2, 100 + 3650) != 0 { return 230 }
117
118 // ----- T14 effective_state returns STALE if stale -----
119 nx_comp_record_init(r2, 0xCAFE)
120 r2.entropy_window = NX_ENTROPY_WINDOW_MID
121 nx_comp_record_measurement(r2, 0xBEEF, 16384, 100) // PARITY recorded
122 if nx_comp_record_effective_state(r2, 150) != NX_COMP_PARITY { return 240 }
123 if nx_comp_record_effective_state(r2, 200) != NX_COMP_STALE { return 241 }
124
125 // ----- T15 Outcome mapping with staleness -----
126 // PARITY fresh -> CONFIRMED
127 nx_comp_record_init(r2, 0xCAFE)
128 nx_comp_record_measurement(r2, 0xBEEF, 16384, 100)
129 if nx_comp_to_etg_outcome(r2, 150) != NX_ETG_OUTCOME_CONFIRMED { return 250 }
130 // PARITY stale -> FALSIFIED
131 if nx_comp_to_etg_outcome(r2, 200) != NX_ETG_OUTCOME_FALSIFIED { return 251 }
132 // LAGGING fresh -> INCONCLUSIVE
133 nx_comp_record_init(r2, 0xCAFE)
134 nx_comp_record_measurement(r2, 0xBEEF, 12000, 100) // LAGGING (~0.73)
135 if nx_comp_to_etg_outcome(r2, 150) != NX_ETG_OUTCOME_INCONCLUSIVE { return 252 }
136 // UNCOMPETITIVE fresh -> FALSIFIED
137 nx_comp_record_init(r2, 0xCAFE)
138 nx_comp_record_measurement(r2, 0xBEEF, 4000, 100) // UNCOMPETITIVE
139 if nx_comp_to_etg_outcome(r2, 150) != NX_ETG_OUTCOME_FALSIFIED { return 253 }
140
141 // ----- T16 nx_comp_attest emits valid NxEtgEntry -----
142 let e_buf: *u8 = sys_mmap(128)
143 let e: *NxEtgEntry = e_buf as *NxEtgEntry
144 nx_comp_record_init(r2, 0xCAFE)
145 nx_comp_record_measurement(r2, 0xBEEF, 18000, 100) // LEADING (~1.10)
146 let rc_a: i64 = nx_comp_attest(r2, e, 0xC0DEC0DE, 1, 150, 20260520)
147 if rc_a != 0 { return 260 }
148 if e.outcome != NX_ETG_OUTCOME_CONFIRMED { return 261 }
149 if e.claim_value != 18000 { return 262 }
150 if e.measurement_value != NX_COMP_LEADING { return 263 }
151 if e.attestation_hash == 0 { return 264 }
152
153 // ----- T17 Industry victory-lap refusal: stale LEADING -> attests STALE -----
154 nx_comp_record_init(r2, 0xCAFE)
155 nx_comp_record_measurement(r2, 0xBEEF, 22000, 100) // LEADING
156 // 200 days later, still claiming LEADING -- substrate refuses, returns STALE
157 let rc_s: i64 = nx_comp_attest(r2, e, 0xC0DEC0DE, 1, 200, 20260520)
158 if rc_s != 0 { return 270 }
159 if e.outcome != NX_ETG_OUTCOME_FALSIFIED { return 271 } // stale = falsified
160 if e.measurement_value != NX_COMP_STALE { return 272 }
161
162 return 0
163}