Skip to content

Commit ff3f606

Browse files
committed
Merge rust-bitcoin#5025: Add chacha20_poly1305 Criterion AEAD benchmarks
2e824d9 Add chacha20_poly1305 Criterion AEAD benchmarks (Jamil Lambert, PhD) Pull request description: Existing benches only use the raw ChaCha20 keystream without Poly1305/authentication overhead. Add Criterion benchmarks to ChaCha20-Poly1305 using Authenticated Encryption with Associated Data. The three benchmarks are encryption with and without additional authenticated data, and decryption. ACKs for top commit: apoelstra: ACK 2e824d9; successfully ran local tests tcharding: ACK 2e824d9 Tree-SHA512: 96e11fd1032a3e6b57a00541e2cdd935d7dc497cae541c11636a2236c111c0d4e23edd17d8b666e1902dbb421b26514d52233d6fa495bb9cf8ec19dc3e13a2ec
2 parents eb17995 + 2e824d9 commit ff3f606

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

benches/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ path = "bitcoin/witness.rs"
2929
harness = false
3030

3131

32+
[[bench]]
33+
name = "chacha20poly1305"
34+
path = "chacha20_poly1305/chacha20poly1305.rs"
35+
harness = false
36+
3237
[[bench]]
3338
name = "chacha20"
3439
path = "chacha20_poly1305/chacha20.rs"
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// SPDX-License-Identifier: CC0-1.0
2+
3+
use std::hint::black_box;
4+
5+
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
6+
use std::time::Duration;
7+
8+
use chacha20_poly1305::{ChaCha20Poly1305, Key, Nonce};
9+
10+
fn bench_chacha20poly1305(c: &mut Criterion) {
11+
let mut g = c.benchmark_group("chacha20poly1305");
12+
g.measurement_time(Duration::from_secs(5)).warm_up_time(Duration::from_secs(2));
13+
14+
for &size in &[128usize, 1024, 16 * 1024, 64 * 1024] {
15+
let key = Key::new([0u8; 32]);
16+
let nonce = Nonce::new([0u8; 12]);
17+
18+
let pt = vec![0u8; size];
19+
let aad: &[u8] = b"dummy_aad";
20+
21+
g.throughput(Throughput::Bytes(size as u64));
22+
23+
g.bench_function(BenchmarkId::new("encrypt_no_aad", size), |b| {
24+
b.iter(|| {
25+
let mut buf = pt.clone();
26+
let cipher = ChaCha20Poly1305::new(key, nonce);
27+
let tag = cipher.encrypt(black_box(&mut buf), None);
28+
black_box(tag);
29+
});
30+
});
31+
32+
g.bench_function(BenchmarkId::new("encrypt_with_aad", size), |b| {
33+
b.iter(|| {
34+
let mut buf = pt.clone();
35+
let cipher = ChaCha20Poly1305::new(key, nonce);
36+
let tag = cipher.encrypt(black_box(&mut buf), Some(aad));
37+
black_box(tag);
38+
});
39+
});
40+
41+
let mut ct = pt.clone();
42+
let tag = ChaCha20Poly1305::new(key, nonce).encrypt(&mut ct, Some(aad));
43+
44+
g.bench_function(BenchmarkId::new("decrypt_ok", size), |b| {
45+
b.iter(|| {
46+
let mut buf = ct.clone();
47+
let cipher = ChaCha20Poly1305::new(key, nonce);
48+
let res = cipher.decrypt(black_box(&mut buf), tag, Some(aad));
49+
black_box(res.unwrap());
50+
});
51+
});
52+
}
53+
54+
g.finish();
55+
}
56+
57+
criterion_group!(benches, bench_chacha20poly1305);
58+
criterion_main!(benches);

0 commit comments

Comments
 (0)