v2 / vlib / x / crypto / ascon / bench / aead.v
75 lines · 63 sloc · 2.83 KB · a10c59704b4fd795accd3542aed10e131d050ec4
Raw
1// This benchmark is for Ascon-AEAD128 in `x.crypto.ascon` compared to
2// already stocked `x.crypto.cacha20poly1305 for AEAD functionalities.
3//
4// Here is output in my tests, first item was `x.crypto.ascon` and the later
5// for `x.crypto.chacha20poly1305` on encryption or decryption part.
6//
7// Encryption..
8// -----------
9// Iterations: 10000 Total Duration: 26.008ms ns/op: 2600 B/op: 16 allocs/op: 17
10// Iterations: 10000 Total Duration: 158.865ms ns/op: 15886 B/op: 16 allocs/op: 16
11//
12// Decryption..
13// -----------
14// Iterations: 10000 Total Duration: 29.091ms ns/op: 2909 B/op: 6 allocs/op: 8
15// Iterations: 10000 Total Duration: 158.373ms ns/op: 15837 B/op: 8 allocs/op: 12
16//
17import encoding.hex
18import x.benchmark
19import x.crypto.ascon
20import x.crypto.chacha20poly1305
21
22// randomly generated key and nonce, 16-bytes of ascon key and 32-bytes of chacha20poly1305 key.
23const key_ascon = hex.decode('7857bfb462c654d1d1b02971be021235')!
24const key_cpoly = hex.decode('9d9603f4fc460e273b80795ea50eab5873c04f589226c7d591b5336feb32fcba')!
25
26// 16-bytes ascon-nonce
27const ascon_nonce = hex.decode('8b521028fb54591472d8d8ee14430835')!
28
29// 12-bytes chacha20poly1305 nonce
30const cpoly_nonce = hex.decode('9a3c83e4236ea9a2c4e482da')!
31
32const ad = 'Ascon-AEAD128 additional data'.bytes()
33const msg = 'Ascon-AEAD128 benchmarking message'.bytes()
34
35// expected ciphertext for aead128 := 4b21a18cbca65b11aaf73dc74241c89bfcec96a4c8973ae696a938e0a591e846c4eb7b2906664f2318c0fd6ec1c56424aa9b
36const ciphertext_aead128 = hex.decode('4b21a18cbca65b11aaf73dc74241c89bfcec96a4c8973ae696a938e0a591e846c4eb7b2906664f2318c0fd6ec1c56424aa9b')!
37
38fn bench_ascon_aead128_encrypt() ! {
39 _ := ascon.encrypt(key_ascon, ascon_nonce, ad, msg)!
40}
41
42fn bench_ascon_aead128_decrypt() ! {
43 _ := ascon.decrypt(key_ascon, ascon_nonce, ad, ciphertext_aead128)!
44}
45
46// expected ciphertext for chacha20poly1305
47const ciphertext_chachapoly1305 = hex.decode('67dea3c65f0f326bcf587f024140a85d9535790d9b16129210a2289eda43bb9b62746450026fc1baf466bcb8a181843cd424')!
48
49fn bench_chacha20poly1305_encrypt() ! {
50 _ := chacha20poly1305.encrypt(msg, key_cpoly, cpoly_nonce, ad)!
51}
52
53fn bench_chacha20poly1305_decrypt() ! {
54 _ := chacha20poly1305.decrypt(ciphertext_chachapoly1305, key_cpoly, cpoly_nonce, ad)!
55}
56
57fn main() {
58 cf := benchmark.BenchmarkDefaults{
59 n: 10000
60 }
61 println('Encryption..')
62 println('-----------')
63 mut b0 := benchmark.setup(bench_ascon_aead128_encrypt, cf)!
64 b0.run()
65 mut b1 := benchmark.setup(bench_chacha20poly1305_encrypt, cf)!
66 b1.run()
67
68 println('')
69 println('Decryption..')
70 println('-----------')
71 mut b2 := benchmark.setup(bench_ascon_aead128_decrypt, cf)!
72 b2.run()
73 mut b3 := benchmark.setup(bench_chacha20poly1305_decrypt, cf)!
74 b3.run()
75}
76