|
1 | | -//! bign-curve256v1 scalar arithmetic benchmarks |
| 1 | +//! bign-curve256v1 `Scalar` benchmarks |
2 | 2 |
|
3 | | -use bignp256::{ProjectivePoint, Scalar, elliptic_curve::group::ff::PrimeField}; |
4 | | -use criterion::{ |
5 | | - BenchmarkGroup, Criterion, criterion_group, criterion_main, measurement::Measurement, |
6 | | -}; |
7 | | -use hex_literal::hex; |
| 3 | +use bignp256::Scalar; |
| 4 | +use criterion::{criterion_group, criterion_main}; |
8 | 5 |
|
9 | | -fn test_scalar_x() -> Scalar { |
10 | | - Scalar::from_repr( |
11 | | - hex!("519b423d715f8b581f4fa8ee59f4771a5b44c8130b4e3eacca54a56dda72b464").into(), |
12 | | - ) |
13 | | - .unwrap() |
14 | | -} |
| 6 | +const SCALAR_A: Scalar = |
| 7 | + Scalar::from_hex_vartime("519b423d715f8b581f4fa8ee59f4771a5b44c8130b4e3eacca54a56dda72b464"); |
| 8 | +const SCALAR_B: Scalar = |
| 9 | + Scalar::from_hex_vartime("0f56db78ca460b055c500064824bed999a25aaf48ebb519ac201537b85479813"); |
15 | 10 |
|
16 | | -fn test_scalar_y() -> Scalar { |
17 | | - Scalar::from_repr( |
18 | | - hex!("0f56db78ca460b055c500064824bed999a25aaf48ebb519ac201537b85479813").into(), |
19 | | - ) |
20 | | - .unwrap() |
21 | | -} |
22 | | - |
23 | | -fn bench_point_mul<M: Measurement>(group: &mut BenchmarkGroup<M>) { |
24 | | - let p = ProjectivePoint::GENERATOR; |
25 | | - let m = test_scalar_x(); |
26 | | - let s = Scalar::from_repr(m.into()).unwrap(); |
27 | | - group.bench_function("point-scalar mul", |b| b.iter(|| p * s)); |
28 | | -} |
29 | | - |
30 | | -fn bench_scalar_sub<M: Measurement>(group: &mut BenchmarkGroup<M>) { |
31 | | - let x = test_scalar_x(); |
32 | | - let y = test_scalar_y(); |
33 | | - group.bench_function("sub", |b| b.iter(|| x - y)); |
34 | | -} |
35 | | - |
36 | | -fn bench_scalar_add<M: Measurement>(group: &mut BenchmarkGroup<M>) { |
37 | | - let x = test_scalar_x(); |
38 | | - let y = test_scalar_y(); |
39 | | - group.bench_function("add", |b| b.iter(|| x + y)); |
40 | | -} |
41 | | - |
42 | | -fn bench_scalar_mul<M: Measurement>(group: &mut BenchmarkGroup<M>) { |
43 | | - let x = test_scalar_x(); |
44 | | - let y = test_scalar_y(); |
45 | | - group.bench_function("mul", |b| b.iter(|| x * y)); |
46 | | -} |
47 | | - |
48 | | -fn bench_scalar_negate<M: Measurement>(group: &mut BenchmarkGroup<M>) { |
49 | | - let x = test_scalar_x(); |
50 | | - group.bench_function("negate", |b| b.iter(|| -x)); |
51 | | -} |
52 | | - |
53 | | -fn bench_scalar_invert<M: Measurement>(group: &mut BenchmarkGroup<M>) { |
54 | | - let x = test_scalar_x(); |
55 | | - group.bench_function("invert", |b| b.iter(|| x.invert())); |
56 | | -} |
57 | | - |
58 | | -fn bench_point(c: &mut Criterion) { |
59 | | - let mut group = c.benchmark_group("point operations"); |
60 | | - bench_point_mul(&mut group); |
61 | | - group.finish(); |
62 | | -} |
63 | | - |
64 | | -fn bench_scalar(c: &mut Criterion) { |
65 | | - let mut group = c.benchmark_group("scalar operations"); |
66 | | - bench_scalar_sub(&mut group); |
67 | | - bench_scalar_add(&mut group); |
68 | | - bench_scalar_mul(&mut group); |
69 | | - bench_scalar_negate(&mut group); |
70 | | - bench_scalar_invert(&mut group); |
71 | | - group.finish(); |
72 | | -} |
73 | | - |
74 | | -criterion_group!(benches, bench_point, bench_scalar); |
| 11 | +primefield::bench_field!(bench_scalar, "Scalar", SCALAR_A, SCALAR_B); |
| 12 | +criterion_group!(benches, bench_scalar); |
75 | 13 | criterion_main!(benches); |
0 commit comments