code wiki / (root) / nx_p256_sign_bench.nx

nx_p256_sign_bench.nx source

↩ module page · 54 lines · 2287 B

1// nx_p256_sign_bench.nx -- measure the ACTUAL per-handshake server crypto: 2// P-256 ECDSA sign (TLS 1.3 CertificateVerify uses the le_ecdsa_key.bin 3// P-256 key). Tonight I mis-benched ed25519; the real cost is this. If 4// sign time ~= the measured ~80ms handshake, the handshake is crypto-bound 5// (P-256 comb tables = the lever), not Nagle-bound. __rdtsc + us clock, 1:1. 6// 7// license_tier: ORIGINAL 8 9import "nx_syscalls.nx" 10import "nx_ecdsa_p256_sign.nx" 11 12func bdec(n: i64) -> i64 { 13 if n == 0 { sys_write(1, "0" as *u8, 1); return 0 } 14 var m: i64 = n 15 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 16 let d: *u8 = sys_mmap(24); var k: i64 = 0 17 while m > 0 { d[k] = (0x30 + (m % 10)) as u8; m = m / 10; k = k + 1 } 18 var j: i64 = k - 1 19 while j >= 0 { sys_write(1, ((d as i64)+j) as *u8, 1); j = j - 1 } 20 return 0 21} 22func msfix(us: i64) -> i64 { 23 bdec(us / 1000); sys_write(1, ".", 1) 24 let f: i64 = us % 1000 25 if f < 100 { sys_write(1, "0", 1) } 26 if f < 10 { sys_write(1, "0", 1) } 27 bdec(f); return 0 28} 29 30func main() -> i64 { 31 let priv: *i64 = sys_mmap(64) as *i64 32 let hash: *i64 = sys_mmap(64) as *i64 33 let r: *i64 = sys_mmap(64) as *i64 34 let s: *i64 = sys_mmap(64) as *i64 35 // a small valid scalar + a test digest (4x64-bit limbs each) 36 priv[0] = 0x00abcdef12345678; priv[1] = 0x0000000000000011; priv[2] = 0; priv[3] = 0 37 hash[0] = 0x1122334455667788; hash[1] = 0x99aabbccddeeff00; hash[2] = 0x0f0f0f0f0f0f0f0f; hash[3] = 0x00ff00ff00ff00ff 38 let N: i64 = 200 39 40 // warmup (also triggers any one-time comb-table build) 41 nx_ecdsa_p256_sign(priv, hash, r, s) 42 43 let c0: i64 = __rdtsc(); let u0: i64 = sys_now_us() 44 var i: i64 = 0 45 while i < N { nx_ecdsa_p256_sign(priv, hash, r, s); i = i + 1 } 46 let u1: i64 = sys_now_us(); let c1: i64 = __rdtsc() 47 let us: i64 = u1 - u0; let cyc: i64 = c1 - c0 48 49 sys_write(1, "P-256 ECDSA sign (the real TLS handshake op, N=200):\n", 53) 50 sys_write(1, " ", 2); msfix(us / N); sys_write(1, " ms/op ", 9); bdec(cyc / N); sys_write(1, " cyc/op\n", 8) 51 sys_write(1, " => compare to the ~80ms measured handshake: if close, handshake is\n", 69) 52 sys_write(1, " P-256-crypto-bound (comb tables = the lever), not Nagle-bound.\n", 67) 53 return 0 54}