nx_etg_tournament_test.nx source
↩ module page · 123 lines · 5171 B
1// nx_etg_tournament_test.nx -- KAT for E2 tournament selector.
2//
3// Verifies:
4// T1 All-correct variants: winner = argmin(perf_metric).
5// T2 Mixed correct + failed: failed variants ignored even if
6// they have lower perf; winner is correct + argmin.
7// T3 All variants failed: returns NX_ETG_TVAL_NO_WINNER; never
8// silently picks a failed variant.
9// T4 Tie-break determinism: equal perf -> earlier variant wins.
10// T5 N=0: returns NX_ETG_TVAL_NO_WINNER (no underflow).
11// T6 N=1 correct: that variant wins regardless of its perf.
12// T7 N=1 failed: returns NX_ETG_TVAL_NO_WINNER.
13// T8 Attestation emits a valid E1 entry with outcome=RECLAIMED
14// + claim_source=PRIOR_CALIBRATION + claim_value=variants_tested
15// + measurement_value=winning_variant_id + reclamation_delta=1.
16
17import "nx_syscalls.nx"
18import "nx_etg.nx"
19import "nx_etg_tournament.nx"
20
21func main() -> i64 {
22 // Parallel arrays for variant inputs. i64 cells (8 bytes each).
23 let ids_buf: *u8 = sys_mmap(64)
24 let corr_buf: *u8 = sys_mmap(64)
25 let perf_buf: *u8 = sys_mmap(64)
26 let ids: *i64 = ids_buf as *i64
27 let corr: *i64 = corr_buf as *i64
28 let perf: *i64 = perf_buf as *i64
29
30 // ----- T1 All correct, winner = argmin(perf) -----
31 // variants: 10, 20, 30 ; correctness PASS,PASS,PASS ; perf 100, 50, 75
32 // Expected winner: variant_id 20 (perf 50).
33 ids[0] = 10; ids[1] = 20; ids[2] = 30
34 corr[0] = NX_ETG_TVAL_PASS; corr[1] = NX_ETG_TVAL_PASS; corr[2] = NX_ETG_TVAL_PASS
35 perf[0] = 100; perf[1] = 50; perf[2] = 75
36 let w1: i64 = nx_etg_tournament_select(ids, corr, perf, 3)
37 if w1 != 20 { return 1 }
38
39 // ----- T2 Mixed correct + failed -----
40 // variants: 10 (PASS perf 100), 20 (FAIL perf 5), 30 (PASS perf 75)
41 // Variant 20 has lowest perf but FAILED -> ignored. Winner = 30.
42 ids[0] = 10; ids[1] = 20; ids[2] = 30
43 corr[0] = NX_ETG_TVAL_PASS; corr[1] = NX_ETG_TVAL_FAIL; corr[2] = NX_ETG_TVAL_PASS
44 perf[0] = 100; perf[1] = 5; perf[2] = 75
45 let w2: i64 = nx_etg_tournament_select(ids, corr, perf, 3)
46 if w2 != 30 { return 2 }
47
48 // ----- T3 All variants failed -----
49 ids[0] = 10; ids[1] = 20; ids[2] = 30
50 corr[0] = NX_ETG_TVAL_FAIL; corr[1] = NX_ETG_TVAL_FAIL; corr[2] = NX_ETG_TVAL_FAIL
51 perf[0] = 100; perf[1] = 50; perf[2] = 75
52 let w3: i64 = nx_etg_tournament_select(ids, corr, perf, 3)
53 if w3 != NX_ETG_TVAL_NO_WINNER { return 3 }
54
55 // ----- T4 Tie-break determinism -----
56 // variants 10, 20, 30 all PASS, perf 50, 50, 100.
57 // Tie between ids[0]=10 and ids[1]=20 at perf 50; ids[0] is
58 // earlier -> winner = 10.
59 ids[0] = 10; ids[1] = 20; ids[2] = 30
60 corr[0] = NX_ETG_TVAL_PASS; corr[1] = NX_ETG_TVAL_PASS; corr[2] = NX_ETG_TVAL_PASS
61 perf[0] = 50; perf[1] = 50; perf[2] = 100
62 let w4: i64 = nx_etg_tournament_select(ids, corr, perf, 3)
63 if w4 != 10 { return 4 }
64
65 // ----- T5 N=0 -----
66 let w5: i64 = nx_etg_tournament_select(ids, corr, perf, 0)
67 if w5 != NX_ETG_TVAL_NO_WINNER { return 5 }
68 // N negative also safe.
69 let w5b: i64 = nx_etg_tournament_select(ids, corr, perf, 0 - 1)
70 if w5b != NX_ETG_TVAL_NO_WINNER { return 6 }
71
72 // ----- T6 N=1 correct -----
73 ids[0] = 42; corr[0] = NX_ETG_TVAL_PASS; perf[0] = 999
74 let w6: i64 = nx_etg_tournament_select(ids, corr, perf, 1)
75 if w6 != 42 { return 7 }
76
77 // ----- T7 N=1 failed -----
78 ids[0] = 42; corr[0] = NX_ETG_TVAL_FAIL; perf[0] = 1
79 let w7: i64 = nx_etg_tournament_select(ids, corr, perf, 1)
80 if w7 != NX_ETG_TVAL_NO_WINNER { return 8 }
81
82 // ----- T8 Attestation emission via E1 entry_init -----
83 let e_buf: *u8 = sys_mmap(128)
84 let e: *NxEtgEntry = e_buf as *NxEtgEntry
85 // Replay T1 scenario for attestation.
86 let rc_attest: i64 = nx_etg_tournament_attest(
87 e,
88 0xDEADBEEF, // silicon serial
89 3, // variants tested
90 20, // winning variant id
91 NX_ETG_PROBE_GPU_TENSOR,
92 1, // selector version
93 12345
94 )
95 if rc_attest != 0 { return 10 }
96 if e.outcome != NX_ETG_OUTCOME_RECLAIMED { return 11 }
97 if e.claim_source != NX_ETG_CLAIM_PRIOR_CALIBRATION { return 12 }
98 if e.claim_value != 3 { return 13 }
99 if e.measurement_value != 20 { return 14 }
100 if e.reclamation_delta != 1 { return 15 }
101 if e.silicon_serial_hash != 0xDEADBEEF { return 16 }
102 if e.probe_kind != NX_ETG_PROBE_GPU_TENSOR { return 17 }
103 if e.timestamp_q14 != 12345 { return 18 }
104 // Attestation hash is deterministic + non-zero.
105 if e.attestation_hash == 0 { return 19 }
106
107 // ----- T8b Two identical tournaments produce identical attestations -----
108 let e2_buf: *u8 = sys_mmap(128)
109 let e2: *NxEtgEntry = e2_buf as *NxEtgEntry
110 let rc_attest_2: i64 = nx_etg_tournament_attest(
111 e2,
112 0xDEADBEEF,
113 3,
114 20,
115 NX_ETG_PROBE_GPU_TENSOR,
116 1,
117 12345
118 )
119 if rc_attest_2 != 0 { return 20 }
120 if nx_etg_attestation_deterministic(e, e2) != 1 { return 21 }
121
122 return 0
123}