nx_p256_ecverify_core_bench.nx source
↩ module page · 71 lines · 3175 B
1// nx_p256_ecverify_core_bench.nx -- SAME-RUN clean comparison of the verify's EC core (u1*G + u2*Q +
2// point_add), optimized (comb u1*G + wNAF u2*Q) vs baseline (generic + generic). Both paths use the
3// same (already-live fast) field reducer, so this isolates the SCALAR-MULT-ALGORITHM speedup. Table
4// built once (amortized). Dependent chain. expect_exit: 0 license_tier: ORIGINAL
5import "nx_syscalls.nx"
6import "nx_csprng.nx"
7import "nx_p256_comb.nx"
8import "nx_p256_scalar_mul_wnaf.nx"
9import "nx_p256_scalar_mul.nx"
10import "nx_p256_point_add.nx"
11const K_MAGIC_1000000000: i64 = 1000000000
12
13const NI: i64 = 300
14
15func bp(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
16func bn(v: i64) -> i64 {
17 let t: *u8 = sys_mmap(28); var m: i64 = v; if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
18 let b: *u8 = sys_mmap(28); var k: i64 = 0; if m == 0 { t[0] = 48 as u8; k = 1 }
19 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
20 var i: i64 = 0; while i < k { b[i] = t[k-1-i]; i = i + 1 } sys_write(1, b, k); return 0
21}
22func now_ns(ts: *i64) -> i64 { __syscall(SYS_CLOCK_GETTIME, 1, ts as i64, 0, 0, 0, 0); return ts[0] * K_MAGIC_1000000000 + ts[1] }
23
24func main() -> i64 {
25 let table: *i64 = (sys_mmap(NX_P256_COMB_BYTES)) as *i64
26 p256_comb_build(table)
27 let g: *P256Point = p256_point_alloc()
28 p256_point_load_g(g)
29 // a random Q (public key point) = d*G for a random d
30 let d: *i64 = u256_alloc()
31 let be: *u8 = sys_mmap(32); nx_csprng_fill(be, 32); be[0] = 0; u256_load_be(d, be)
32 let Q: *P256Point = p256_point_alloc()
33 p256_scalar_mul(Q, d, g); p256_point_to_affine(Q)
34 let u1: *i64 = u256_alloc(); let u2: *i64 = u256_alloc()
35 nx_csprng_fill(be, 32); u256_load_be(u1, be)
36 nx_csprng_fill(be, 32); u256_load_be(u2, be)
37 let u1G: *P256Point = p256_point_alloc()
38 let u2Q: *P256Point = p256_point_alloc()
39 let R: *P256Point = p256_point_alloc()
40 let ts: *i64 = sys_mmap(32) as *i64
41 let u1_0: i64 = u1[0]
42
43 bp("=== nx_p256_ecverify_core_bench: (comb+wNAF) vs (generic+generic), same run ===\n" as *u8)
44
45 // baseline: generic u1*G + generic u2*Q + add
46 var accb: i64 = 0
47 var t0: i64 = now_ns(ts)
48 var i: i64 = 0
49 while i < NI {
50 u1[0] = (u1[0] ^ accb); p256_scalar_mul(u1G, u1, g); p256_scalar_mul(u2Q, u2, Q)
51 p256_point_add(R, u1G, u2Q); accb = accb ^ R.x[0]; i = i + 1
52 }
53 let base_ns: i64 = now_ns(ts) - t0
54
55 u1[0] = u1_0
56 // optimized: comb u1*G + wNAF u2*Q + add
57 var acco: i64 = 0
58 t0 = now_ns(ts)
59 i = 0
60 while i < NI {
61 u1[0] = (u1[0] ^ acco); p256_scalar_mul_base(u1G, u1, table); p256_scalar_mul_wnaf(u2Q, u2, Q)
62 p256_point_add(R, u1G, u2Q); acco = acco ^ R.x[0]; i = i + 1
63 }
64 let opt_ns: i64 = now_ns(ts) - t0
65
66 bn(NI); bp(" iters\n" as *u8)
67 bp("baseline(gen+gen) = " as *u8); bn(base_ns / NI); bp(" ns\n" as *u8)
68 bp("optimized(comb+wnaf) = " as *u8); bn(opt_ns / NI); bp(" ns\n" as *u8)
69 if opt_ns > 0 { bp("speedup_x100 = " as *u8); bn(base_ns * 100 / opt_ns); bp(" (EC-core scalar-mult speedup)\n" as *u8) }
70 return 0
71}