Skip to content

Commit

Permalink
p384: criterion benchmarks
Browse files Browse the repository at this point in the history
I'd like to use these to measure performance changes if the
`fiat-crypto` field implementations are converted to `const fn`,
specifically leveraging criterion's support to track percentages by
which performance has increased/decreased.

Adapted from the benchmarks in the `k256` crate.
  • Loading branch information
tarcieri committed Jun 2, 2022
1 parent f179ec2 commit c50eabd
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 71 deletions.
38 changes: 2 additions & 36 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion k256/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ std = ["ecdsa-core/std", "elliptic-curve/std"] # TODO: use weak activation for `
test-vectors = ["hex-literal"]

[package.metadata.docs.rs]
features = ["ecdh", "ecdsa", "schnorr", "sha256", "keccak256"]
features = ["ecdh", "ecdsa", "keccak256", "schnorr"]
rustdoc-args = ["--cfg", "docsrs"]

[[bench]]
Expand Down
17 changes: 11 additions & 6 deletions p384/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,11 @@ sha2 = { version = "0.10", optional = true, default-features = false }

[dev-dependencies]
blobby = "0.3"
criterion = "0.3"
ecdsa-core = { version = "0.14", package = "ecdsa", default-features = false, features = ["dev"] }
hex-literal = "0.3"
proptest = "1.0"
rand_core = { version = "0.6", features = ["getrandom"] }
benchmark-simple = "0.1.7"

[[bench]]
name = "benchmark"
harness = false

[features]
default = ["arithmetic", "ecdh", "ecdsa", "pem", "std"]
Expand All @@ -40,6 +36,7 @@ bits = ["arithmetic", "elliptic-curve/bits"]
digest = ["ecdsa-core/digest", "ecdsa-core/hazmat"]
ecdh = ["arithmetic", "elliptic-curve/ecdh"]
ecdsa = ["arithmetic", "ecdsa-core/sign", "ecdsa-core/verify", "sha384"]
expose-field = []
hash2curve = ["arithmetic", "elliptic-curve/hash2curve"]
jwk = ["elliptic-curve/jwk"]
pem = ["elliptic-curve/pem", "ecdsa-core/pem", "pkcs8"]
Expand All @@ -51,5 +48,13 @@ test-vectors = ["hex-literal"]
voprf = ["elliptic-curve/voprf", "sha2"]

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[[bench]]
name = "field"
harness = false
required-features = ["expose-field"]

[[bench]]
name = "scalar"
harness = false
28 changes: 0 additions & 28 deletions p384/benches/benchmark.rs

This file was deleted.

54 changes: 54 additions & 0 deletions p384/benches/field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//! secp384r1 field element benchmarks

use criterion::{
criterion_group, criterion_main, measurement::Measurement, BenchmarkGroup, Criterion,
};
use hex_literal::hex;
use p384::FieldElement;

fn test_field_element_x() -> FieldElement {
FieldElement::from_sec1(
hex!("c2b47944fb5de342d03285880177ca5f7d0f2fcad7678cce4229d6e1932fcac11bfc3c3e97d942a3c56bf34123013dbf").into()
)
.unwrap()
}

fn test_field_element_y() -> FieldElement {
FieldElement::from_sec1(
hex!("37257906a8223866eda0743c519616a76a758ae58aee81c5fd35fbf3a855b7754a36d4a0672df95d6c44a81cf7620c2d").into()
)
.unwrap()
}

fn bench_field_element_mul<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
let x = test_field_element_x();
let y = test_field_element_y();
group.bench_function("mul", |b| b.iter(|| &x * &y));
}

fn bench_field_element_square<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
let x = test_field_element_x();
group.bench_function("square", |b| b.iter(|| x.square()));
}

fn bench_field_element_sqrt<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
let x = test_field_element_x();
group.bench_function("sqrt", |b| b.iter(|| x.sqrt()));
}

fn bench_field_element_invert<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
let x = test_field_element_x();
group.bench_function("invert", |b| b.iter(|| x.invert()));
}

fn bench_field_element(c: &mut Criterion) {
let mut group = c.benchmark_group("field element operations");
bench_field_element_mul(&mut group);
bench_field_element_square(&mut group);
bench_field_element_invert(&mut group);
bench_field_element_sqrt(&mut group);
group.finish();
}

criterion_group!(benches, bench_field_element);
criterion_main!(benches);
73 changes: 73 additions & 0 deletions p384/benches/scalar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//! secp384r1 scalar arithmetic benchmarks

use criterion::{
criterion_group, criterion_main, measurement::Measurement, BenchmarkGroup, Criterion,
};
use hex_literal::hex;
use p384::{elliptic_curve::group::ff::PrimeField, ProjectivePoint, Scalar};

fn test_scalar_x() -> Scalar {
Scalar::from_repr(
hex!("201b432d8df14324182d6261db3e4b3f46a8284482d52e370da41e6cbdf45ec2952f5db7ccbce3bc29449f4fb080ac97").into()
).unwrap()
}

fn test_scalar_y() -> Scalar {
Scalar::from_repr(
hex!("23d9f4ea6d87b7d6163d64256e3449255db14786401a51daa7847161bf56d494325ad2ac8ba928394e01061d882c3528").into()
).unwrap()
}

fn bench_point_mul<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
let p = ProjectivePoint::GENERATOR;
let m = test_scalar_x();
let s = Scalar::from_repr(m.into()).unwrap();
group.bench_function("point-scalar mul", |b| b.iter(|| &p * &s));
}

fn bench_scalar_sub<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
let x = test_scalar_x();
let y = test_scalar_y();
group.bench_function("sub", |b| b.iter(|| &x - &y));
}

fn bench_scalar_add<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
let x = test_scalar_x();
let y = test_scalar_y();
group.bench_function("add", |b| b.iter(|| &x + &y));
}

fn bench_scalar_mul<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
let x = test_scalar_x();
let y = test_scalar_y();
group.bench_function("mul", |b| b.iter(|| &x * &y));
}

fn bench_scalar_negate<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
let x = test_scalar_x();
group.bench_function("negate", |b| b.iter(|| -x));
}

fn bench_scalar_invert<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
let x = test_scalar_x();
group.bench_function("invert", |b| b.iter(|| x.invert()));
}

fn bench_point(c: &mut Criterion) {
let mut group = c.benchmark_group("point operations");
bench_point_mul(&mut group);
group.finish();
}

fn bench_scalar(c: &mut Criterion) {
let mut group = c.benchmark_group("scalar operations");
bench_scalar_sub(&mut group);
bench_scalar_add(&mut group);
bench_scalar_mul(&mut group);
bench_scalar_negate(&mut group);
bench_scalar_invert(&mut group);
group.finish();
}

criterion_group!(benches, bench_point, bench_scalar);
criterion_main!(benches);
3 changes: 3 additions & 0 deletions p384/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ pub use elliptic_curve::{self, bigint::U384};
#[cfg(feature = "arithmetic")]
pub use arithmetic::{affine::AffinePoint, projective::ProjectivePoint, scalar::Scalar};

#[cfg(feature = "expose-field")]
pub use arithmetic::field::FieldElement;

#[cfg(feature = "pkcs8")]
#[cfg_attr(docsrs, doc(cfg(feature = "pkcs8")))]
pub use elliptic_curve::pkcs8;
Expand Down

0 comments on commit c50eabd

Please sign in to comment.