nx_aead_bench.nx source
↩ module page · 54 lines · 2061 B
1// nx_aead_bench.nx -- reliable cipher cost split (run in WSL where the monotonic
2// clock is real ns, not the Windows ~15ms-granular FILETIME). Times ChaCha20 and
3// Poly1305 over one large buffer to find which half of the AEAD decrypt is the
4// measured TLS-transfer bottleneck, and the raw MB/s of each (hardware-up target).
5//
6// expect_exit: 0
7
8import "nx_syscalls.nx"
9import "nx_chacha20.nx"
10import "nx_poly1305.nx"
11const K_MAGIC_2970000: i64 = 2970000
12const K_MAGIC_1000000: i64 = 1000000
13
14func b_ps(s: *u8, n: i64) -> i64 { sys_write(1, s, n); return 0 }
15func b_pn(v: i64) -> i64 {
16 let b: *u8 = sys_mmap(24)
17 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)
23 return 0
24}
25
26func main() -> i64 {
27 let N: i64 = K_MAGIC_2970000 // ~10x a wiki page so the timer has signal
28 let buf: *u8 = sys_mmap(N)
29 let out: *u8 = sys_mmap(N)
30 let key: *u8 = sys_mmap(32)
31 let nonce: *u8 = sys_mmap(16)
32 let otk: *u8 = sys_mmap(32)
33 let tag: *u8 = sys_mmap(32)
34 var i: i64 = 0
35 while i < 32 { key[i] = (i * 7 + 1) as u8; otk[i] = (i * 5 + 3) as u8; i = i + 1 }
36 i = 0
37 while i < N { buf[i] = (i & 0xff) as u8; i = i + 1 }
38
39 let t0: i64 = sys_now_ms()
40 chacha20_encrypt(key, 1, nonce, buf, N, out)
41 let t1: i64 = sys_now_ms()
42 poly1305_mac(otk, buf, N, tag)
43 let t2: i64 = sys_now_ms()
44
45 let cha_ms: i64 = t1 - t0
46 let poly_ms: i64 = t2 - t1
47 let mb: i64 = N / K_MAGIC_1000000
48 b_ps("aead-bench " as *u8, 11); b_pn(mb); b_ps("MB: chacha20=" as *u8, 13); b_pn(cha_ms)
49 b_ps("ms poly1305=" as *u8, 12); b_pn(poly_ms); b_ps("ms\n" as *u8, 3)
50 if cha_ms > 0 { b_ps(" chacha20 MB/s=" as *u8, 16); b_pn((N / 1000) / cha_ms); b_ps("\n" as *u8, 1) }
51 if poly_ms > 0 { b_ps(" poly1305 MB/s=" as *u8, 16); b_pn((N / 1000) / poly_ms); b_ps("\n" as *u8, 1) }
52 sys_exit(0)
53 return 0
54}