]> git.proxmox.com Git - rustc.git/blob - vendor/p384/benches/scalar.rs
New upstream version 1.70.0+dfsg2
[rustc.git] / vendor / p384 / benches / scalar.rs
1 //! secp384r1 scalar arithmetic benchmarks
2
3 use criterion::{
4 criterion_group, criterion_main, measurement::Measurement, BenchmarkGroup, Criterion,
5 };
6 use hex_literal::hex;
7 use p384::{elliptic_curve::group::ff::PrimeField, ProjectivePoint, Scalar};
8
9 fn test_scalar_x() -> Scalar {
10 Scalar::from_repr(
11 hex!("201b432d8df14324182d6261db3e4b3f46a8284482d52e370da41e6cbdf45ec2952f5db7ccbce3bc29449f4fb080ac97").into()
12 ).unwrap()
13 }
14
15 fn test_scalar_y() -> Scalar {
16 Scalar::from_repr(
17 hex!("23d9f4ea6d87b7d6163d64256e3449255db14786401a51daa7847161bf56d494325ad2ac8ba928394e01061d882c3528").into()
18 ).unwrap()
19 }
20
21 fn bench_point_mul<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
22 let p = ProjectivePoint::GENERATOR;
23 let m = test_scalar_x();
24 let s = Scalar::from_repr(m.into()).unwrap();
25 group.bench_function("point-scalar mul", |b| b.iter(|| &p * &s));
26 }
27
28 fn bench_scalar_sub<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
29 let x = test_scalar_x();
30 let y = test_scalar_y();
31 group.bench_function("sub", |b| b.iter(|| &x - &y));
32 }
33
34 fn bench_scalar_add<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
35 let x = test_scalar_x();
36 let y = test_scalar_y();
37 group.bench_function("add", |b| b.iter(|| &x + &y));
38 }
39
40 fn bench_scalar_mul<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
41 let x = test_scalar_x();
42 let y = test_scalar_y();
43 group.bench_function("mul", |b| b.iter(|| &x * &y));
44 }
45
46 fn bench_scalar_negate<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
47 let x = test_scalar_x();
48 group.bench_function("negate", |b| b.iter(|| -x));
49 }
50
51 fn bench_scalar_invert<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
52 let x = test_scalar_x();
53 group.bench_function("invert", |b| b.iter(|| x.invert()));
54 }
55
56 fn bench_point(c: &mut Criterion) {
57 let mut group = c.benchmark_group("point operations");
58 bench_point_mul(&mut group);
59 group.finish();
60 }
61
62 fn bench_scalar(c: &mut Criterion) {
63 let mut group = c.benchmark_group("scalar operations");
64 bench_scalar_sub(&mut group);
65 bench_scalar_add(&mut group);
66 bench_scalar_mul(&mut group);
67 bench_scalar_negate(&mut group);
68 bench_scalar_invert(&mut group);
69 group.finish();
70 }
71
72 criterion_group!(benches, bench_point, bench_scalar);
73 criterion_main!(benches);