Skip to content

Commit 90185ac

Browse files
authored
bignp256: write benchmarks with macros (#1592)
Similar to e.g. #1589
1 parent 8a3900d commit 90185ac

File tree

8 files changed

+67
-127
lines changed

8 files changed

+67
-127
lines changed

.github/workflows/bignp256.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ concurrency:
2626
cancel-in-progress: true
2727

2828
jobs:
29+
benches:
30+
runs-on: ubuntu-latest
31+
steps:
32+
- uses: actions/checkout@v6
33+
- uses: dtolnay/rust-toolchain@master
34+
with:
35+
toolchain: 1.85.0 # MSRV
36+
- run: cargo build --all-features --benches
37+
2938
build:
3039
runs-on: ubuntu-latest
3140
strategy:

.github/workflows/bp256.yml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,11 @@ concurrency:
2828
jobs:
2929
benches:
3030
runs-on: ubuntu-latest
31-
strategy:
32-
matrix:
33-
rust:
34-
- 1.85.0 # MSRV
35-
- stable
3631
steps:
3732
- uses: actions/checkout@v6
3833
- uses: dtolnay/rust-toolchain@master
3934
with:
40-
toolchain: ${{ matrix.rust }}
35+
toolchain: 1.85.0 # MSRV
4136
- run: cargo build --all-features --benches
4237

4338
build:

.github/workflows/bp384.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ concurrency:
2626
cancel-in-progress: true
2727

2828
jobs:
29+
benches:
30+
runs-on: ubuntu-latest
31+
steps:
32+
- uses: actions/checkout@v6
33+
- uses: dtolnay/rust-toolchain@master
34+
with:
35+
toolchain: 1.85.0 # MSRV
36+
- run: cargo build --all-features --benches
37+
2938
build:
3039
runs-on: ubuntu-latest
3140
strategy:

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bignp256/Cargo.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ signature = { version = "3.0.0-rc.6", optional = true }
3737

3838
[dev-dependencies]
3939
criterion = "0.7"
40+
elliptic-curve = { version = "0.14.0-rc.20", default-features = false, features = ["dev"] }
4041
getrandom = { version = "0.4.0-rc.0", features = ["sys_rng"] }
41-
hex = { version = "0.4" }
4242
hex-literal = "1"
4343
primeorder = { version = "0.14.0-rc.3", features = ["dev"] }
4444
proptest = "1"
@@ -61,10 +61,17 @@ test-vectors = ["dep:hex-literal"]
6161
[[bench]]
6262
name = "field"
6363
harness = false
64+
required-features = ["arithmetic"]
65+
66+
[[bench]]
67+
name = "point"
68+
harness = false
69+
required-features = ["arithmetic"]
6470

6571
[[bench]]
6672
name = "scalar"
6773
harness = false
74+
required-features = ["arithmetic"]
6875

6976
[package.metadata.docs.rs]
7077
all-features = true

bignp256/benches/field.rs

Lines changed: 9 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,15 @@
1-
//! bign-curve256v1 field element benchmarks
1+
//! bign-curve256v1 `FieldElement` benchmarks
22
33
use bignp256::arithmetic::FieldElement;
4-
use criterion::{
5-
BenchmarkGroup, Criterion, criterion_group, criterion_main, measurement::Measurement,
6-
};
7-
use hex_literal::hex;
4+
use criterion::{criterion_group, criterion_main};
85

9-
fn test_field_element_x() -> FieldElement {
10-
FieldElement::from_bytes(
11-
&hex!("1ccbe91c075fc7f4f033bfa248db8fccd3565de94bbfb12f3c59ff46c271bf83").into(),
12-
)
13-
.unwrap()
14-
}
15-
16-
fn test_field_element_y() -> FieldElement {
17-
FieldElement::from_bytes(
18-
&hex!("ce4014c68811f9a21a1fdb2c0e6113e06db7ca93b7404e78dc7ccd5ca89a4ca9").into(),
19-
)
20-
.unwrap()
21-
}
22-
23-
fn bench_field_element_mul<M: Measurement>(group: &mut BenchmarkGroup<M>) {
24-
let x = test_field_element_x();
25-
let y = test_field_element_y();
26-
group.bench_function("mul", |b| b.iter(|| x * y));
27-
}
28-
29-
fn bench_field_element_square<M: Measurement>(group: &mut BenchmarkGroup<M>) {
30-
let x = test_field_element_x();
31-
group.bench_function("square", |b| b.iter(|| x.square()));
32-
}
33-
34-
fn bench_field_element_sqrt<M: Measurement>(group: &mut BenchmarkGroup<M>) {
35-
let x = test_field_element_x();
36-
group.bench_function("sqrt", |b| b.iter(|| x.sqrt()));
37-
}
38-
39-
fn bench_field_element_invert<M: Measurement>(group: &mut BenchmarkGroup<M>) {
40-
let x = test_field_element_x();
41-
group.bench_function("invert", |b| b.iter(|| x.invert()));
42-
}
43-
44-
fn bench_field_element(c: &mut Criterion) {
45-
let mut group = c.benchmark_group("field element operations");
46-
bench_field_element_mul(&mut group);
47-
bench_field_element_square(&mut group);
48-
bench_field_element_invert(&mut group);
49-
bench_field_element_sqrt(&mut group);
50-
group.finish();
51-
}
6+
const FE_A: FieldElement = FieldElement::from_hex_vartime(
7+
"1ccbe91c075fc7f4f033bfa248db8fccd3565de94bbfb12f3c59ff46c271bf83",
8+
);
9+
const FE_B: FieldElement = FieldElement::from_hex_vartime(
10+
"ce4014c68811f9a21a1fdb2c0e6113e06db7ca93b7404e78dc7ccd5ca89a4ca9",
11+
);
5212

13+
primefield::bench_field!(bench_field_element, "FieldElement", FE_A, FE_B);
5314
criterion_group!(benches, bench_field_element);
5415
criterion_main!(benches);

bignp256/benches/point.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//! bign-curve256v1 `ProjectivePoint` benchmarks
2+
3+
use bignp256::{ProjectivePoint, Scalar};
4+
use criterion::{criterion_group, criterion_main};
5+
6+
const SCALAR_A: Scalar =
7+
Scalar::from_hex_vartime("9bb0d8b72602b70dd5cfed99607a2e2c021dd0fe3b3af842df02c06f8c1a0f4e");
8+
const SCALAR_B: Scalar =
9+
Scalar::from_hex_vartime("6494152e2b6c34768296d2ea0e984f89a77f0d7399b70f2e29789128423a9bea");
10+
const SCALAR_C: Scalar =
11+
Scalar::from_hex_vartime("a316f6a92d2f8359218cf9f68900d9f791ad2ad77aee07686adeb5ec7c7b8cb3");
12+
13+
elliptic_curve::bench_projective!(
14+
bench_projective,
15+
"ProjectivePoint",
16+
ProjectivePoint::GENERATOR * SCALAR_A,
17+
ProjectivePoint::GENERATOR * SCALAR_B,
18+
SCALAR_C
19+
);
20+
21+
criterion_group!(benches, bench_projective);
22+
criterion_main!(benches);

bignp256/benches/scalar.rs

Lines changed: 9 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,13 @@
1-
//! bign-curve256v1 scalar arithmetic benchmarks
1+
//! bign-curve256v1 `Scalar` benchmarks
22
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};
85

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");
1510

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);
7513
criterion_main!(benches);

0 commit comments

Comments
 (0)