Skip to content

Commit d3386b7

Browse files
authored
p256: criterion benchmarks (#597)
Adapts the benchmarks added in the `p384` crate in #588 to the `p256` crate, with the goal of using them to measure the performance impact of switching to fiat-crypto's arithmetic implementations.
1 parent 458112a commit d3386b7

File tree

7 files changed

+146
-2
lines changed

7 files changed

+146
-2
lines changed

Cargo.lock

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

p256/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ sha2 = { version = "0.10", optional = true, default-features = false }
2727

2828
[dev-dependencies]
2929
blobby = "0.3"
30+
criterion = "0.3"
3031
ecdsa-core = { version = "0.14", package = "ecdsa", default-features = false, features = ["dev"] }
3132
hex-literal = "0.3"
3233
proptest = "1.0"
@@ -39,6 +40,7 @@ bits = ["arithmetic", "elliptic-curve/bits"]
3940
digest = ["ecdsa-core/digest", "ecdsa-core/hazmat"]
4041
ecdh = ["arithmetic", "elliptic-curve/ecdh"]
4142
ecdsa = ["arithmetic", "ecdsa-core/sign", "ecdsa-core/verify", "sha256"]
43+
expose-field = ["arithmetic"]
4244
hash2curve = ["arithmetic", "elliptic-curve/hash2curve"]
4345
jwk = ["elliptic-curve/jwk"]
4446
pem = ["elliptic-curve/pem", "ecdsa-core/pem", "pkcs8"]
@@ -52,3 +54,12 @@ voprf = ["elliptic-curve/voprf", "sha2"]
5254
[package.metadata.docs.rs]
5355
all-features = true
5456
rustdoc-args = ["--cfg", "docsrs"]
57+
58+
[[bench]]
59+
name = "field"
60+
harness = false
61+
required-features = ["expose-field"]
62+
63+
[[bench]]
64+
name = "scalar"
65+
harness = false

p256/benches/field.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//! secp256r1 field element benchmarks
2+
3+
use criterion::{
4+
criterion_group, criterion_main, measurement::Measurement, BenchmarkGroup, Criterion,
5+
};
6+
use hex_literal::hex;
7+
use p256::FieldElement;
8+
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<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, 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<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, 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<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, 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<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, 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+
}
52+
53+
criterion_group!(benches, bench_field_element);
54+
criterion_main!(benches);

p256/benches/scalar.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//! secp256r1 scalar arithmetic benchmarks
2+
3+
use criterion::{
4+
criterion_group, criterion_main, measurement::Measurement, BenchmarkGroup, Criterion,
5+
};
6+
use hex_literal::hex;
7+
use p256::{elliptic_curve::group::ff::PrimeField, ProjectivePoint, Scalar};
8+
9+
fn test_scalar_x() -> Scalar {
10+
Scalar::from_repr(
11+
hex!("519b423d715f8b581f4fa8ee59f4771a5b44c8130b4e3eacca54a56dda72b464").into(),
12+
)
13+
.unwrap()
14+
}
15+
16+
fn test_scalar_y() -> Scalar {
17+
Scalar::from_repr(
18+
hex!("0f56db78ca460b055c500064824bed999a25aaf48ebb519ac201537b85479813").into(),
19+
)
20+
.unwrap()
21+
}
22+
23+
fn bench_point_mul<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, 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<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, 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<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, 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<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, 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<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
49+
let x = test_scalar_x();
50+
group.bench_function("negate", |b| b.iter(|| -x));
51+
}
52+
53+
fn bench_scalar_invert<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, 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);
75+
criterion_main!(benches);

p256/src/arithmetic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Pure Rust implementation of group operations on secp256r1.
22
33
pub(crate) mod affine;
4-
mod field;
4+
pub(crate) mod field;
55
#[cfg(feature = "hash2curve")]
66
mod hash2curve;
77
pub(crate) mod projective;

p256/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ pub use arithmetic::{
4343
scalar::{blinded::BlindedScalar, Scalar},
4444
};
4545

46+
#[cfg(feature = "expose-field")]
47+
pub use arithmetic::field::FieldElement;
48+
4649
#[cfg(feature = "pkcs8")]
4750
#[cfg_attr(docsrs, doc(cfg(feature = "pkcs8")))]
4851
pub use elliptic_curve::pkcs8;

p384/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ bits = ["arithmetic", "elliptic-curve/bits"]
4040
digest = ["ecdsa-core/digest", "ecdsa-core/hazmat"]
4141
ecdh = ["arithmetic", "elliptic-curve/ecdh"]
4242
ecdsa = ["arithmetic", "ecdsa-core/sign", "ecdsa-core/verify", "sha384"]
43-
expose-field = []
43+
expose-field = ["arithmetic"]
4444
hash2curve = ["arithmetic", "elliptic-curve/hash2curve"]
4545
jwk = ["elliptic-curve/jwk"]
4646
pem = ["elliptic-curve/pem", "ecdsa-core/pem", "pkcs8"]

0 commit comments

Comments
 (0)