Skip to content

Commit 89a2495

Browse files
authored
x.crypto.chacha20: improves the internals of chacha20, add a bench (#25311)
1 parent 5d98162 commit 89a2495

6 files changed

Lines changed: 817 additions & 608 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// This is a benchmark for`x.crypto.chacha20` encryption and decryption
2+
//
3+
// Current output on my tests
4+
//
5+
// Chacha20 Encryption
6+
// -----------
7+
// Iterations: 10000 Total Duration: 76.045ms ns/op: 7604 B/op: 4 allocs/op: 2
8+
//
9+
// ChaCha20 Decryption
10+
// -----------
11+
// Iterations: 10000 Total Duration: 71.275ms ns/op: 7127 B/op: 11 allocs/op: 14
12+
//
13+
// After the patch
14+
// Chacha20 Encryption
15+
// -----------
16+
// Iterations: 10000 Total Duration: 46.833ms ns/op: 4683 B/op: 11 allocs/op: 11
17+
//
18+
// ChaCha20 Decryption
19+
// -----------
20+
// Iterations: 10000 Total Duration: 48.242ms ns/op: 4824 B/op: 3 allocs/op: 4
21+
//
22+
import x.benchmark
23+
import encoding.hex
24+
import x.crypto.chacha20
25+
26+
// randomly generated key and nonce, 32-bytes of key, 12-bytes of nonce
27+
const key = hex.decode('9d9603f4fc460e273b80795ea50eab5873c04f589226c7d591b5336feb32fcba')!
28+
const nonce = hex.decode('9a3c83e4236ea9a2c4e482da')!
29+
30+
const plaintext = 'ChaCha20 encrypt decrypt benchmarking message'.bytes()
31+
32+
// expected ciphertext
33+
const ciphertext = hex.decode('dbddb264e4c478d96805b2d557649232b4b3f37c51035464d12e3675e5e36ce6f6822b49dd6494ccd5213a89c9')!
34+
35+
fn bench_chacha20_encrypt() ! {
36+
_ := chacha20.encrypt(key, nonce, plaintext)!
37+
}
38+
39+
fn bench_chacha20_decrypt() ! {
40+
_ := chacha20.decrypt(key, nonce, ciphertext)!
41+
}
42+
43+
fn main() {
44+
cf := benchmark.BenchmarkDefaults{
45+
n: 10000
46+
}
47+
println('Chacha20 Encryption')
48+
println('-----------')
49+
mut b0 := benchmark.setup(bench_chacha20_encrypt, cf)!
50+
b0.run()
51+
52+
println('')
53+
println('ChaCha20 Decryption')
54+
println('-----------')
55+
mut b1 := benchmark.setup(bench_chacha20_decrypt, cf)!
56+
b1.run()
57+
}

0 commit comments

Comments
 (0)