Skip to content

Commit 089aab4

Browse files
authored
k256: add expose-field feature; fix/CI benches (#161)
The benchmarks want direct access to `FieldElement` for benchmarking purposes. This is a legitimate use case, but breaks encapsulation in that we'd like to generally prevent exposure of `FieldElement` except for "hazmat"-style use cases. This is a well-recognized problem with Rust in general: rust-lang/cargo#2911 This commit adds a semi-hidden `expose-field` feature in order to fix the benchmark. Perhaps it will be abused, but the alternative is not having a benchmark, which seems bad too. It also adds a CI build step to ensure the benchmarks compile to prevent this sort of regression in the future.
1 parent 0eb8f6f commit 089aab4

File tree

5 files changed

+29
-18
lines changed

5 files changed

+29
-18
lines changed

.github/workflows/k256.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,4 @@ jobs:
6262
- run: cargo test --all-features
6363
- run: cargo test --features field-montgomery,rand
6464
- run: cargo test --features force-32-bit,rand
65+
- run: cargo build --all-features --benches

k256/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ digest = ["elliptic-curve/digest", "ecdsa-core/digest"]
3939
ecdh = ["elliptic-curve/ecdh", "rand", "zeroize"]
4040
ecdsa = ["arithmetic", "digest", "ecdsa-core/rand", "ecdsa-core/sign", "ecdsa-core/verify", "rand", "zeroize"]
4141
endomorphism-mul = []
42+
expose-field = ["arithmetic"]
4243
field-montgomery = []
4344
force-32-bit = []
4445
keccak256 = ["digest", "sha3"]
@@ -50,10 +51,11 @@ std = ["elliptic-curve/std"]
5051
zeroize = ["elliptic-curve/zeroize"]
5152

5253
[package.metadata.docs.rs]
53-
all-features = true
54+
features = ["ecdh", "ecdsa", "sha256", "keccak256"]
5455
rustdoc-args = ["--cfg", "docsrs"]
5556

5657
[[bench]]
5758
name = "bench"
5859
path = "bench/bench.rs"
5960
harness = false
61+
required-features = ["expose-field"]

k256/bench/bench.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use core::convert::TryInto;
1+
//! secp256k1 benchmarks
2+
23
use criterion::measurement::Measurement;
34
use criterion::{criterion_group, criterion_main, BenchmarkGroup, Criterion};
4-
5+
use hex_literal::hex;
56
use k256::{elliptic_curve::FromBytes, FieldElement, ProjectivePoint, Scalar};
67

78
fn test_scalar_x() -> Scalar {
@@ -30,10 +31,8 @@ fn test_scalar_y() -> Scalar {
3031

3132
fn bench_point_mul<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
3233
let p = ProjectivePoint::generator();
33-
let ms = "AA5E28D6A97A2479A65527F7290311A3624D4CC0FA1578598EE3C2613BF99522";
34-
let m = hex::decode(&ms).unwrap();
35-
let s = Scalar::from_bytes(m[..].try_into().unwrap()).unwrap();
36-
34+
let m = hex!("AA5E28D6A97A2479A65527F7290311A3624D4CC0FA1578598EE3C2613BF99522");
35+
let s = Scalar::from_bytes(&m.into()).unwrap();
3736
group.bench_function("point-scalar mul", |b| b.iter(|| &p * &s));
3837
}
3938

@@ -82,20 +81,26 @@ fn bench_scalar(c: &mut Criterion) {
8281
}
8382

8483
fn test_field_element_x() -> FieldElement {
85-
FieldElement::from_bytes(&[
86-
0xbb, 0x48, 0x8a, 0xef, 0x41, 0x6a, 0x41, 0xd7, 0x68, 0x0d, 0x1c, 0xf0, 0x1d, 0x70, 0xf5,
87-
0x9b, 0x60, 0xd7, 0xf5, 0xf7, 0x7e, 0x30, 0xe7, 0x8b, 0x8b, 0xf9, 0xd2, 0xd8, 0x82, 0xf1,
88-
0x56, 0xa6,
89-
])
84+
FieldElement::from_bytes(
85+
&[
86+
0xbb, 0x48, 0x8a, 0xef, 0x41, 0x6a, 0x41, 0xd7, 0x68, 0x0d, 0x1c, 0xf0, 0x1d, 0x70,
87+
0xf5, 0x9b, 0x60, 0xd7, 0xf5, 0xf7, 0x7e, 0x30, 0xe7, 0x8b, 0x8b, 0xf9, 0xd2, 0xd8,
88+
0x82, 0xf1, 0x56, 0xa6,
89+
]
90+
.into(),
91+
)
9092
.unwrap()
9193
}
9294

9395
fn test_field_element_y() -> FieldElement {
94-
FieldElement::from_bytes(&[
95-
0x67, 0xe2, 0xf6, 0x80, 0x71, 0xed, 0x82, 0x81, 0xe8, 0xae, 0xd6, 0xbc, 0xf1, 0xc5, 0x20,
96-
0x7c, 0x5e, 0x63, 0x37, 0x22, 0xd9, 0x20, 0xaf, 0xd6, 0xae, 0x22, 0xd0, 0x6e, 0xeb, 0x80,
97-
0x35, 0xe3,
98-
])
96+
FieldElement::from_bytes(
97+
&[
98+
0x67, 0xe2, 0xf6, 0x80, 0x71, 0xed, 0x82, 0x81, 0xe8, 0xae, 0xd6, 0xbc, 0xf1, 0xc5,
99+
0x20, 0x7c, 0x5e, 0x63, 0x37, 0x22, 0xd9, 0x20, 0xaf, 0xd6, 0xae, 0x22, 0xd0, 0x6e,
100+
0xeb, 0x80, 0x35, 0xe3,
101+
]
102+
.into(),
103+
)
99104
.unwrap()
100105
}
101106

k256/src/arithmetic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub(crate) mod scalar;
99
#[cfg(test)]
1010
mod dev;
1111

12-
pub(crate) use field::FieldElement;
12+
pub use field::FieldElement;
1313

1414
use crate::Secp256k1;
1515
use affine::AffinePoint;

k256/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ pub use arithmetic::{
6868
scalar::{NonZeroScalar, Scalar},
6969
};
7070

71+
#[cfg(feature = "expose-field")]
72+
pub use arithmetic::FieldElement;
73+
7174
use elliptic_curve::consts::U32;
7275

7376
#[cfg(feature = "oid")]

0 commit comments

Comments
 (0)