code wiki / _hdl_build / nx_sha256_native_kat_gate.nx
nx_sha256_native_kat_gate.nx source
↩ module page · 40 lines · 2297 B
1// nx_sha256_native_kat_gate.nx -- DISTINGUISHING test for task #21: run nx_sha256_one_shot NATIVELY (x86, no wasm,
2// no VM) on "abc" and check the FIPS 180-4 KAT. If this PASSES, the algorithm in nx_sha256_wasm.nx is correct and the
3// wrong digest seen via nx_wasm_vm is a wasm-compile or VM-execution issue. If it FAILS, the algorithm itself is buggy.
4import "nx_syscalls.nx"
5import "nx_gate_emit_lib.nx"
6import "nx_sha256_wasm.nx"
7import "nx_gate_verdict.nx"
8
9func main() -> i64 {
10 g_puts("nx_sha256_wasm NATIVE KAT (x86, no wasm) -- SHA-256 of abc vs FIPS 180-4\n" as *u8)
11 let inp: *u8 = sys_mmap(16) as *u8
12 inp[0] = 97 as u8; inp[1] = 98 as u8; inp[2] = 99 as u8
13 let ctx: *u8 = sys_mmap(1024) as *u8
14 let out: *u8 = sys_mmap(64) as *u8
15 nx_sha256_one_shot(inp, 3, ctx, out)
16
17 let exp: *i64 = sys_mmap(32*8) as *i64
18 exp[0]=186; exp[1]=120; exp[2]=22; exp[3]=191; exp[4]=143; exp[5]=1; exp[6]=207; exp[7]=234
19 exp[8]=65; exp[9]=65; exp[10]=64; exp[11]=222; exp[12]=93; exp[13]=174; exp[14]=34; exp[15]=35
20 exp[16]=176; exp[17]=3; exp[18]=97; exp[19]=163; exp[20]=150; exp[21]=23; exp[22]=122; exp[23]=156
21 exp[24]=180; exp[25]=16; exp[26]=255; exp[27]=97; exp[28]=242; exp[29]=0; exp[30]=21; exp[31]=173
22
23 g_puts(" digest = " as *u8)
24 var ok: i64 = 1; var i: i64 = 0
25 while i < 32 { g_pn(out[i] as i64); g_puts(" " as *u8); if (out[i] as i64) != exp[i] { ok = 0 } i = i + 1 }
26 g_puts("\n" as *u8)
27 var pass: i64 = 0; var total: i64 = 0
28 pass = pass + g_check("native SHA-256(abc) matches FIPS KAT (algorithm correct)" as *u8, ok); total=total+1
29
30 g_puts("---- sha256 native KAT: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8)
31 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
32 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
33 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
34 let ctr__dry: *i64 = gv_ctr()
35 ctr__dry[0] = pass
36 ctr__dry[1] = total
37 let rc__dry: i64 = gv_verdict("SHA256-NATIVE-KAT-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8)
38 sys_exit(rc__dry)
39 return rc__dry
40}