code wiki / (root) / nx_aead_bench2.nx

nx_aead_bench2.nx source

↩ module page · 58 lines · 2027 B

1// nx_aead_bench2.nx -- replicate the LIVE per-record AEAD decrypt pattern (23 records 2// of ~13KB) in WSL (real clock) to isolate the measured ~1ms/KB transfer slowdown. 3// If this is ~300ms, the AEAD decrypt path itself is the bottleneck; if ~10ms, the 4// slowness is outside it (record framing / loop). 5// 6// expect_exit: 0 7 8import "nx_syscalls.nx" 9import "nx_chacha20_poly1305.nx" 10const K_MAGIC_13000: i64 = 13000 11 12func b_ps(s: *u8, n: i64) -> i64 { sys_write(1, s, n); return 0 } 13func b_pn(v: i64) -> i64 { 14 let b: *u8 = sys_mmap(24) 15 var x: i64 = v 16 if x < 0 { x = 0 - x } 17 var i: i64 = 22 18 if x == 0 { b[i] = 0x30 as u8; i = i - 1 } 19 else { while x > 0 { b[i] = (0x30 + (x - (x/10)*10)) as u8; x = x / 10; i = i - 1 } } 20 sys_write(1, ((b as i64) + i + 1) as *u8, 22 - i) 21 return 0 22} 23 24func main() -> i64 { 25 let REC: i64 = K_MAGIC_13000 26 let NREC: i64 = 23 27 let pt: *u8 = sys_mmap(REC) 28 let ct: *u8 = sys_mmap(REC + 64) 29 let tag: *u8 = sys_mmap(32) 30 let out: *u8 = sys_mmap(REC + 64) 31 let key: *u8 = sys_mmap(32) 32 let nonce: *u8 = sys_mmap(16) 33 let aad: *u8 = sys_mmap(16) 34 var i: i64 = 0 35 while i < 32 { key[i] = (i + 1) as u8; i = i + 1 } 36 i = 0 37 while i < REC { pt[i] = (i & 0xff) as u8; i = i + 1 } 38 i = 0 39 while i < 5 { aad[i] = (i + 7) as u8; i = i + 1 } 40 41 // one encrypt to get a valid (ct, tag) so the decrypt runs the full path 42 nx_chacha20_poly1305_encrypt(key, nonce, aad, 5, pt, REC, ct, tag) 43 44 let t0: i64 = sys_now_ms() 45 var k: i64 = 0 46 while k < NREC { 47 nx_chacha20_poly1305_decrypt(key, nonce, aad, 5, ct, REC, tag, out) 48 k = k + 1 49 } 50 let t1: i64 = sys_now_ms() 51 52 b_ps("aead-decrypt " as *u8, 13); b_pn(NREC); b_ps(" x " as *u8, 3); b_pn(REC) 53 b_ps("B = " as *u8, 3); b_pn(t1 - t0); b_ps("ms total\n" as *u8, 9) 54 let kb: i64 = (NREC * REC) / 1000 55 if (t1 - t0) > 0 { b_ps(" throughput KB/s=" as *u8, 18); b_pn(kb * 1000 / (t1 - t0)); b_ps("\n" as *u8, 1) } 56 sys_exit(0) 57 return 0 58}