Skip to content

Commit cea7efe

Browse files
authored
belt-dwp: add simple benches (#697)
1 parent a1e58ca commit cea7efe

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

.github/workflows/belt-dwp.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,5 @@ jobs:
6666
- run: cargo test --target ${{ matrix.target }} --release
6767
- run: cargo test --target ${{ matrix.target }} --release --features heapless
6868
- run: cargo test --target ${{ matrix.target }} --release --all-features
69-
- run: cargo build --target ${{ matrix.target }} --benches
69+
- env: RUSTC_BOOTSTRAP=1
70+
run: cargo build --target ${{ matrix.target }} --benches

belt-dwp/benches/mod.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#![feature(test)]
2+
extern crate test;
3+
4+
use aead::{
5+
AeadInOut, KeyInit,
6+
array::Array,
7+
consts::{U16, U32},
8+
};
9+
use belt_dwp::BeltDwp;
10+
use hex_literal::hex;
11+
use test::Bencher;
12+
13+
const KEY: Array<u8, U32> = Array(hex!(
14+
"8899AABBCCDDEEFF0011223344556677"
15+
"FEDCBA98765432100123456789ABCDEF"
16+
));
17+
const NONCE: Array<u8, U16> = Array(hex!("1122334455667700FFEEDDCCBBAA9988"));
18+
19+
#[bench]
20+
fn encrypt_aad_only_16kb(b: &mut Bencher) {
21+
let c = BeltDwp::new(&KEY);
22+
let aad = vec![0; 16 * 1024];
23+
let mut buf = [];
24+
25+
b.iter(|| {
26+
let (aad, buf, nonce) = test::black_box((&aad, &mut buf[..], &NONCE));
27+
let res = c.encrypt_inout_detached(nonce, aad, buf.into()).unwrap();
28+
test::black_box(res);
29+
});
30+
31+
b.bytes = 16 * 1024;
32+
}
33+
34+
#[bench]
35+
fn encrypt_msg_only_16kb(b: &mut Bencher) {
36+
let c = BeltDwp::new(&KEY);
37+
let aad = [];
38+
let mut buf = vec![0; 16 * 1024];
39+
40+
b.iter(|| {
41+
let (aad, buf, nonce) = test::black_box((&aad, &mut buf[..], &NONCE));
42+
let res = c.encrypt_inout_detached(nonce, aad, buf.into()).unwrap();
43+
test::black_box(res);
44+
});
45+
46+
b.bytes = 16 * 1024;
47+
}
48+
49+
#[bench]
50+
fn decrypt_aad_only_16kb(b: &mut Bencher) {
51+
let c = BeltDwp::new(&KEY);
52+
let aad = vec![0; 16 * 1024];
53+
let mut buf = [];
54+
let tag = c
55+
.encrypt_inout_detached(&NONCE, &aad, (&mut buf[..]).into())
56+
.unwrap();
57+
58+
b.iter(|| {
59+
let (aad, buf, nonce, tag) = test::black_box((&aad, &mut buf[..], &NONCE, &tag));
60+
let res = c.decrypt_inout_detached(nonce, aad, buf.into(), tag);
61+
let _ = test::black_box(res);
62+
});
63+
64+
b.bytes = 16 * 1024;
65+
}
66+
67+
#[bench]
68+
fn decrypt_msg_only_16kb(b: &mut Bencher) {
69+
let c = BeltDwp::new(&KEY);
70+
let aad = [];
71+
let mut ct_buf = vec![0u8; 16 * 1024];
72+
let tag = c
73+
.encrypt_inout_detached(&NONCE, &aad, (&mut ct_buf[..]).into())
74+
.unwrap();
75+
76+
let mut buf = ct_buf.clone();
77+
b.iter(|| {
78+
let (aad, buf, nonce, tag) = test::black_box((&aad, &mut buf[..], &NONCE, &tag));
79+
let res = c.decrypt_inout_detached(nonce, aad, buf.into(), tag);
80+
let _ = test::black_box(res);
81+
buf.copy_from_slice(&ct_buf);
82+
});
83+
84+
b.bytes = 16 * 1024;
85+
}

0 commit comments

Comments
 (0)