code wiki / (root) / nx_ghash_bench.nx

nx_ghash_bench.nx source

↩ module page · 56 lines · 2150 B

1// nx_ghash_bench.nx -- throughput: bit-by-bit GF(2^128) reference vs the PCLMULQDQ backend. 2// Same fixed block hashed N times each way (models GHASH over a large message). Prints 3// ms + MB/s for both and the speedup. Correctness is gated separately (nx_ghash_clmul_gate); 4// this is purely a perf measurement. 5// 6// expect_exit: 0 7// license_tier: ORIGINAL 8 9import "nx_syscalls.nx" 10import "nx_ghash.nx" 11import "nx_ghash_clmul.nx" 12const K_MAGIC_200000: i64 = 200000 13const K_MAGIC_1000000: i64 = 1000000 14 15func b_ps(s: *u8, n: i64) -> i64 { sys_write(1, s, n); return 0 } 16func b_pn(v: i64) -> i64 { 17 let b: *u8 = sys_mmap(24); var x: i64 = v 18 if x < 0 { x = 0 - x } 19 var i: i64 = 22 20 if x == 0 { b[i] = 0x30 as u8; i = i - 1 } 21 else { while x > 0 { b[i] = (0x30 + (x - (x/10)*10)) as u8; x = x/10; i = i - 1 } } 22 sys_write(1, ((b as i64) + i + 1) as *u8, 22 - i); return 0 23} 24 25func main() -> i64 { 26 let x: *u8 = sys_mmap(16) 27 let h: *u8 = sys_mmap(16) 28 let z: *u8 = sys_mmap(16) 29 var i: i64 = 0 30 while i < 16 { x[i] = (i * 11 + 7) as u8; h[i] = (i * 5 + 1) as u8; i = i + 1 } 31 32 let N: i64 = K_MAGIC_200000 33 let bytes_mb: i64 = (N * 16) / K_MAGIC_1000000 34 35 // --- bit-by-bit reference --- 36 let t0: i64 = sys_now_ms() 37 i = 0; while i < N { nx_ghash_mul_bitwise(x, h, z); i = i + 1 } 38 let t1: i64 = sys_now_ms() 39 40 // --- PCLMULQDQ backend --- 41 let t2: i64 = sys_now_ms() 42 i = 0; while i < N { nx_ghash_mul_clmul(x, h, z); i = i + 1 } 43 let t3: i64 = sys_now_ms() 44 45 let sw: i64 = t1 - t0 46 let hw: i64 = t3 - t2 47 b_ps("GHASH bitwise " as *u8, 14); b_pn(bytes_mb); b_ps("MB = " as *u8, 5); b_pn(sw); b_ps("ms" as *u8, 2) 48 if sw > 0 { b_ps(" (" as *u8, 3); b_pn((N * 16 / 1000) / sw); b_ps(" KB/s)" as *u8, 6) } 49 b_ps("\n" as *u8, 1) 50 b_ps("GHASH clmul " as *u8, 14); b_pn(bytes_mb); b_ps("MB = " as *u8, 5); b_pn(hw); b_ps("ms" as *u8, 2) 51 if hw > 0 { b_ps(" (" as *u8, 3); b_pn((N * 16 / 1000) / hw); b_ps(" KB/s)" as *u8, 6) } 52 b_ps("\n" as *u8, 1) 53 if hw > 0 { b_ps("speedup x" as *u8, 9); b_pn(sw / hw); b_ps("\n" as *u8, 1) } 54 sys_exit(0) 55 return 0 56}