1 // Copyright 2015-2023 Brian Smith.
2 //
3 // Permission to use, copy, modify, and/or distribute this software for any
4 // purpose with or without fee is hereby granted, provided that the above
5 // copyright notice and this permission notice appear in all copies.
6 //
7 // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
8 // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
10 // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14 #![allow(missing_docs)]
15
16 use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion};
17 use ring::{
18 agreement::{self, EphemeralPrivateKey, UnparsedPublicKey, X25519},
19 rand,
20 };
21
generate_key(c: &mut Criterion)22 fn generate_key(c: &mut Criterion) {
23 c.bench_function("x25519_generate_key", |b| {
24 let rng = rand::SystemRandom::new();
25 b.iter(|| {
26 let private_key = EphemeralPrivateKey::generate(&X25519, &rng).unwrap();
27 let _r = black_box(private_key);
28 })
29 });
30 }
31
compute_public_key(c: &mut Criterion)32 fn compute_public_key(c: &mut Criterion) {
33 c.bench_function("x25519_compute_public_key", |b| {
34 let rng = rand::SystemRandom::new();
35 let my_private_key = EphemeralPrivateKey::generate(&X25519, &rng).unwrap();
36 b.iter(|| {
37 let public_key = my_private_key.compute_public_key();
38 let _r = black_box(public_key);
39 })
40 });
41 }
42
agree_ephemeral(c: &mut Criterion)43 fn agree_ephemeral(c: &mut Criterion) {
44 c.bench_function("x25519_agree_ephemeral", |b| {
45 let rng = rand::SystemRandom::new();
46 let peer_public_key: [u8; 32] = rand::generate(&rng).unwrap().expose();
47
48 b.iter_batched(
49 || EphemeralPrivateKey::generate(&X25519, &rng).unwrap(),
50 |my_private_key| {
51 let peer_public_key = UnparsedPublicKey::new(&X25519, &peer_public_key);
52 agreement::agree_ephemeral(my_private_key, &peer_public_key, |key_material| {
53 black_box(<[u8; 32]>::try_from(key_material).unwrap())
54 })
55 .unwrap();
56 },
57 BatchSize::LargeInput,
58 )
59 });
60 }
61
62 criterion_group!(x25519, generate_key, compute_public_key, agree_ephemeral);
63 criterion_main!(x25519);
64