Skip to content

Commit 976f07b

Browse files
authored
bp384: add cfg(bp384_backend = "bignum") (#1584)
Adds support for an experimental backend which uses `crypto-bigint` as the field element representation, as an off-by-default alternative to `fiat-crypto`, similar to what was introduced in `p384` in #1548 and `bp256` in #1583.
1 parent 92c3340 commit 976f07b

File tree

6 files changed

+80
-4
lines changed

6 files changed

+80
-4
lines changed

.github/workflows/bp384.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,7 @@ jobs:
6868
- run: cargo test --no-default-features
6969
- run: cargo test
7070
- run: cargo test --all-features
71+
- env:
72+
RUSTFLAGS: '--cfg bp384_backend="bignum"'
73+
RUSTDOCFLAGS: '--cfg bp384_backend="bignum"'
74+
run: cargo test --release --all-features

bp256/src/arithmetic/scalar.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//! Apache License (Version 2.0), and the BSD 1-Clause License;
1111
//! users may pick which license to apply.
1212
13+
#[cfg(not(bp256_backend = "bignum"))]
1314
#[cfg_attr(target_pointer_width = "32", path = "scalar/bp256_scalar_32.rs")]
1415
#[cfg_attr(target_pointer_width = "64", path = "scalar/bp256_scalar_64.rs")]
1516
#[allow(
@@ -21,7 +22,6 @@
2122
#[allow(dead_code)] // TODO(tarcieri): remove this when we can use `const _` to silence warnings
2223
mod scalar_impl;
2324

24-
use self::scalar_impl::*;
2525
use crate::{BrainpoolP256r1, BrainpoolP256t1, FieldBytes, ORDER, ORDER_HEX, U256};
2626
use elliptic_curve::{
2727
bigint::{ArrayEncoding, Limb},
@@ -31,6 +31,9 @@ use elliptic_curve::{
3131
subtle::{Choice, ConditionallySelectable, ConstantTimeEq, ConstantTimeGreater, CtOption},
3232
};
3333

34+
#[cfg(not(bp256_backend = "bignum"))]
35+
use self::scalar_impl::*;
36+
3437
#[cfg(doc)]
3538
use core::ops::{Add, Mul, Sub};
3639

@@ -50,6 +53,14 @@ primefield::monty_field_element! {
5053
doc: "Element in the brainpoolP256 scalar field modulo n"
5154
}
5255

56+
#[cfg(bp256_backend = "bignum")]
57+
primefield::monty_field_arithmetic! {
58+
name: Scalar,
59+
params: ScalarParams,
60+
uint: U256
61+
}
62+
63+
#[cfg(not(bp256_backend = "bignum"))]
5364
primefield::fiat_monty_field_arithmetic! {
5465
name: Scalar,
5566
params: ScalarParams,
@@ -111,12 +122,15 @@ impl Reduce<FieldBytes> for Scalar {
111122
#[cfg(test)]
112123
mod tests {
113124
use super::{Scalar, U256};
125+
#[cfg(not(bp256_backend = "bignum"))]
114126
use super::{
115127
ScalarParams, fiat_bp256_scalar_montgomery_domain_field_element, fiat_bp256_scalar_msat,
116128
fiat_bp256_scalar_non_montgomery_domain_field_element, fiat_bp256_scalar_to_montgomery,
117129
};
118130

119131
primefield::test_primefield!(Scalar, U256);
132+
133+
#[cfg(not(bp256_backend = "bignum"))]
120134
primefield::test_fiat_monty_field_arithmetic!(
121135
name: Scalar,
122136
params: ScalarParams,

bp384/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ sha384 = ["ecdsa/digest", "ecdsa/hazmat", "sha2"]
3737

3838
[package.metadata.docs.rs]
3939
all-features = true
40+
41+
[lints.rust.unexpected_cfgs]
42+
level = "warn"
43+
check-cfg = ['cfg(bp384_backend, values("bignum", "fiat"))'] # default: "fiat"

bp384/src/arithmetic/field.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//! Apache License (Version 2.0), and the BSD 1-Clause License;
1111
//! users may pick which license to apply.
1212
13+
#[cfg(not(bp384_backend = "bignum"))]
1314
#[cfg_attr(target_pointer_width = "32", path = "field/bp384_32.rs")]
1415
#[cfg_attr(target_pointer_width = "64", path = "field/bp384_64.rs")]
1516
#[allow(
@@ -21,13 +22,15 @@
2122
#[allow(dead_code)] // TODO(tarcieri): remove this when we can use `const _` to silence warnings
2223
mod field_impl;
2324

24-
use self::field_impl::*;
2525
use crate::U384;
2626
use elliptic_curve::{
2727
ff::PrimeField,
2828
subtle::{Choice, ConstantTimeEq, CtOption},
2929
};
3030

31+
#[cfg(not(bp384_backend = "bignum"))]
32+
use self::field_impl::*;
33+
3134
/// Constant representing the modulus serialized as hex.
3235
const MODULUS_HEX: &str = "8cb91e82a3386d280f5d6f7e50e641df152f7109ed5456b412b1da197fb71123acd3a729901d1a71874700133107ec53";
3336

@@ -44,9 +47,17 @@ primefield::monty_field_element! {
4447
name: FieldElement,
4548
params: FieldParams,
4649
uint: U384,
47-
doc: "Element in the brainpoolP256 finite field modulo p"
50+
doc: "Element in the brainpoolP384 finite field modulo p"
51+
}
52+
53+
#[cfg(bp384_backend = "bignum")]
54+
primefield::monty_field_arithmetic! {
55+
name: FieldElement,
56+
params: FieldParams,
57+
uint: U384
4858
}
4959

60+
#[cfg(not(bp384_backend = "bignum"))]
5061
primefield::fiat_monty_field_arithmetic! {
5162
name: FieldElement,
5263
params: FieldParams,
@@ -69,12 +80,15 @@ primefield::fiat_monty_field_arithmetic! {
6980
#[cfg(test)]
7081
mod tests {
7182
use super::{FieldElement, U384};
83+
#[cfg(not(bp384_backend = "bignum"))]
7284
use super::{
7385
FieldParams, fiat_bp384_montgomery_domain_field_element, fiat_bp384_msat,
7486
fiat_bp384_non_montgomery_domain_field_element, fiat_bp384_to_montgomery,
7587
};
7688

7789
primefield::test_primefield!(FieldElement, U384);
90+
91+
#[cfg(not(bp384_backend = "bignum"))]
7892
primefield::test_fiat_monty_field_arithmetic!(
7993
name: FieldElement,
8094
params: FieldParams,

bp384/src/arithmetic/scalar.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//! Apache License (Version 2.0), and the BSD 1-Clause License;
1111
//! users may pick which license to apply.
1212
13+
#[cfg(not(bp384_backend = "bignum"))]
1314
#[cfg_attr(target_pointer_width = "32", path = "scalar/bp384_scalar_32.rs")]
1415
#[cfg_attr(target_pointer_width = "64", path = "scalar/bp384_scalar_64.rs")]
1516
#[allow(
@@ -21,7 +22,6 @@
2122
#[allow(dead_code)] // TODO(tarcieri): remove this when we can use `const _` to silence warnings
2223
mod scalar_impl;
2324

24-
use self::scalar_impl::*;
2525
use crate::{BrainpoolP384r1, BrainpoolP384t1, FieldBytes, ORDER, ORDER_HEX, U384};
2626
use elliptic_curve::{
2727
bigint::{ArrayEncoding, Limb},
@@ -31,6 +31,9 @@ use elliptic_curve::{
3131
subtle::{Choice, ConditionallySelectable, ConstantTimeEq, ConstantTimeGreater, CtOption},
3232
};
3333

34+
#[cfg(not(bp384_backend = "bignum"))]
35+
use self::scalar_impl::*;
36+
3437
#[cfg(doc)]
3538
use core::ops::{Add, Mul, Sub};
3639

@@ -50,6 +53,14 @@ primefield::monty_field_element! {
5053
doc: "Element in the brainpoolP256 scalar field modulo n"
5154
}
5255

56+
#[cfg(bp384_backend = "bignum")]
57+
primefield::monty_field_arithmetic! {
58+
name: Scalar,
59+
params: ScalarParams,
60+
uint: U384
61+
}
62+
63+
#[cfg(not(bp384_backend = "bignum"))]
5364
primefield::fiat_monty_field_arithmetic! {
5465
name: Scalar,
5566
params: ScalarParams,
@@ -111,12 +122,15 @@ impl Reduce<FieldBytes> for Scalar {
111122
#[cfg(test)]
112123
mod tests {
113124
use super::{Scalar, U384};
125+
#[cfg(not(bp384_backend = "bignum"))]
114126
use super::{
115127
ScalarParams, fiat_bp384_scalar_montgomery_domain_field_element, fiat_bp384_scalar_msat,
116128
fiat_bp384_scalar_non_montgomery_domain_field_element, fiat_bp384_scalar_to_montgomery,
117129
};
118130

119131
primefield::test_primefield!(Scalar, U384);
132+
133+
#[cfg(not(bp384_backend = "bignum"))]
120134
primefield::test_fiat_monty_field_arithmetic!(
121135
name: Scalar,
122136
params: ScalarParams,

bp384/src/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,32 @@
1515
unused_qualifications
1616
)]
1717

18+
//! ## Backends
19+
//!
20+
//! This crate has support for two different field arithmetic backends which can be selected using
21+
//! `cfg(bp384_backend)`, e.g. to select the `bignum` backend:
22+
//!
23+
//! ```console
24+
//! $ RUSTFLAGS='--cfg bp384_backend="bignum"' cargo test
25+
//! ```
26+
//!
27+
//! Or it can be set through [`.cargo/config`][buildrustflags]:
28+
//!
29+
//! ```toml
30+
//! [build]
31+
//! rustflags = ['--cfg', 'bp384_backend="bignum"']
32+
//! ```
33+
//!
34+
//! The available backends are:
35+
//! - `bignum`: experimental backend provided by [crypto-bigint]. May offer better performance in
36+
//! some cases along with smaller code size, but might also have bugs.
37+
//! - `fiat` (default): formally verified implementation synthesized by [fiat-crypto] which should
38+
//! be correct for all inputs (though there's a possibility of bugs in the code which glues to it)
39+
//!
40+
//! [buildrustflags]: https://doc.rust-lang.org/cargo/reference/config.html#buildrustflags
41+
//! [crypto-bigint]: https://github.com/RustCrypto/crypto-bigint
42+
//! [fiat-crypto]: https://github.com/mit-plv/fiat-crypto
43+
1844
pub mod r1;
1945
pub mod t1;
2046

0 commit comments

Comments
 (0)