nx_p256_fieldmul_bench.nx source
↩ module page · 43 lines · 1429 B
1// nx_p256_fieldmul_bench.nx -- a NON-const-foldable P-256 field-mul benchmark.
2// The point-loop bench derives everything from the constant generator G, so opt
3// const-folds the field arithmetic away. This seeds the inputs from a RUNTIME
4// clock and chains a=field_mul(a,b) (data-dependent), so opt MUST emit the real
5// field_mul loop -- a valid workload for measuring field_mul (G2/G3). The return
6// folds in the runtime seed so the whole computation is live.
7// license_tier: ORIGINAL
8
9import "nx_syscalls.nx"
10import "nx_u256.nx"
11import "nx_p256_field.nx"
12import "nx_p256_field_mul.nx"
13const K_MAGIC_2654435761: i64 = 2654435761
14const K_MAGIC_40503: i64 = 40503
15const K_MAGIC_200000: i64 = 200000
16
17func main() -> i64 {
18 let _s: i64 = nx_scratch_save()
19 let seed: i64 = sys_now_us() // runtime -> defeats const-fold
20 let a: *i64 = u256_alloc()
21 let b: *i64 = u256_alloc()
22 let r: *i64 = u256_alloc()
23
24 var j: i64 = 0
25 while j < 8 {
26 a[j] = (seed + j * K_MAGIC_2654435761) & 0xFFFFFFFF
27 b[j] = (seed * 3 + j * K_MAGIC_40503) & 0xFFFFFFFF
28 j = j + 1
29 }
30
31 var i: i64 = 0
32 var acc: i64 = 0
33 while i < K_MAGIC_200000 {
34 p256_field_mul(r, a, b)
35 var k: i64 = 0
36 while k < 8 { a[k] = r[k]; k = k + 1 } // data-dependent chain
37 acc = acc + (r[0] & 0xFF)
38 i = i + 1
39 }
40
41 nx_scratch_restore(_s)
42 return (acc + seed) & 255
43}