Skip to content

Commit 7c112cb

Browse files
authored
primeorder: use FieldArithmetic trait (#1833)
Uses the new `hazmat` trait added to `elliptic-curve` in RustCrypto/traits#2458 to define the associated `FieldElement` type for a given curve.
1 parent 2d9a7cc commit 7c112cb

19 files changed

Lines changed: 93 additions & 54 deletions

File tree

Cargo.lock

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

bignp256/src/arithmetic.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88
pub(crate) mod field;
99
pub(crate) mod scalar;
1010

11-
pub use self::scalar::Scalar;
11+
pub use self::{field::FieldElement, scalar::Scalar};
12+
pub use elliptic_curve::{CurveArithmetic, PrimeCurveArithmetic, hazmat::FieldArithmetic};
13+
pub use primeorder::{PrimeCurveParams, point_arithmetic};
1214

13-
pub use self::field::FieldElement;
1415
use crate::BignP256;
15-
pub use elliptic_curve::{CurveArithmetic, PrimeCurveArithmetic};
1616
use primeorder::backend;
17-
pub use primeorder::{PrimeCurveParams, point_arithmetic};
1817

1918
/// Elliptic curve point in affine coordinates.
2019
pub type AffinePoint = primeorder::AffinePoint<BignP256>;
@@ -28,12 +27,15 @@ impl CurveArithmetic for BignP256 {
2827
type Scalar = Scalar;
2928
}
3029

30+
impl FieldArithmetic for BignP256 {
31+
type FieldElement = FieldElement;
32+
}
33+
3134
impl PrimeCurveArithmetic for BignP256 {
3235
type CurveGroup = ProjectivePoint;
3336
}
3437

3538
impl PrimeCurveParams for BignP256 {
36-
type FieldElement = FieldElement;
3739
type PointArithmetic = point_arithmetic::EquationAIsGeneric;
3840
type Backend = backend::VariableOnly;
3941

bp256/benches/field.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
33
use bp256::BrainpoolP256r1;
44
use criterion::{criterion_group, criterion_main};
5-
use primeorder::PrimeCurveParams;
5+
use elliptic_curve::hazmat::FieldArithmetic;
66

7-
type FieldElement = <BrainpoolP256r1 as PrimeCurveParams>::FieldElement;
7+
type FieldElement = <BrainpoolP256r1 as FieldArithmetic>::FieldElement;
88

99
const FE_A: FieldElement = FieldElement::from_hex_vartime(
1010
"8bd2aeb9cb7e57cb2c4b482ffc81b7afb9de27e1e3bd23c23a4453bd9ace3262",

bp256/src/r1/arithmetic.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use super::BrainpoolP256r1;
44
use crate::{FieldElement, Scalar};
5-
use elliptic_curve::{CurveArithmetic, PrimeCurveArithmetic};
5+
use elliptic_curve::{CurveArithmetic, PrimeCurveArithmetic, hazmat::FieldArithmetic};
66
use primeorder::{PrimeCurveParams, backend, point_arithmetic};
77

88
/// Elliptic curve point in affine coordinates.
@@ -23,12 +23,15 @@ impl CurveArithmetic for BrainpoolP256r1 {
2323
type Scalar = Scalar;
2424
}
2525

26+
impl FieldArithmetic for BrainpoolP256r1 {
27+
type FieldElement = FieldElement;
28+
}
29+
2630
impl PrimeCurveArithmetic for BrainpoolP256r1 {
2731
type CurveGroup = ProjectivePoint;
2832
}
2933

3034
impl PrimeCurveParams for BrainpoolP256r1 {
31-
type FieldElement = FieldElement;
3235
type PointArithmetic = point_arithmetic::EquationAIsGeneric;
3336
type Backend = backend::VariableOnly;
3437

bp256/src/t1/arithmetic.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use super::BrainpoolP256t1;
44
use crate::{FieldElement, Scalar};
5-
use elliptic_curve::{CurveArithmetic, PrimeCurveArithmetic};
5+
use elliptic_curve::{CurveArithmetic, PrimeCurveArithmetic, hazmat::FieldArithmetic};
66
use primeorder::{PrimeCurveParams, backend, point_arithmetic};
77

88
/// Elliptic curve point in affine coordinates.
@@ -27,8 +27,11 @@ impl PrimeCurveArithmetic for BrainpoolP256t1 {
2727
type CurveGroup = ProjectivePoint;
2828
}
2929

30-
impl PrimeCurveParams for BrainpoolP256t1 {
30+
impl FieldArithmetic for BrainpoolP256t1 {
3131
type FieldElement = FieldElement;
32+
}
33+
34+
impl PrimeCurveParams for BrainpoolP256t1 {
3235
type PointArithmetic = point_arithmetic::EquationAIsGeneric;
3336
type Backend = backend::VariableOnly;
3437

bp384/benches/field.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
33
use bp384::BrainpoolP384r1;
44
use criterion::{criterion_group, criterion_main};
5-
use primeorder::PrimeCurveParams;
5+
use elliptic_curve::hazmat::FieldArithmetic;
66

7-
type FieldElement = <BrainpoolP384r1 as PrimeCurveParams>::FieldElement;
7+
type FieldElement = <BrainpoolP384r1 as FieldArithmetic>::FieldElement;
88

99
const FE_A: FieldElement = FieldElement::from_hex_vartime(
1010
"1d1c64f068cf45ffa2a63a81b7c13f6b8847a3e77ef14fe3db7fcafe0cbd10e8e826e03436d646aaef87b2e247d4af1e",

bp384/src/r1/arithmetic.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use super::BrainpoolP384r1;
44
use crate::{FieldElement, Scalar};
5-
use elliptic_curve::{CurveArithmetic, PrimeCurveArithmetic};
5+
use elliptic_curve::{CurveArithmetic, PrimeCurveArithmetic, hazmat::FieldArithmetic};
66
use primeorder::{PrimeCurveParams, backend, point_arithmetic};
77

88
/// Elliptic curve point in affine coordinates.
@@ -23,12 +23,15 @@ impl CurveArithmetic for BrainpoolP384r1 {
2323
type Scalar = Scalar;
2424
}
2525

26+
impl FieldArithmetic for BrainpoolP384r1 {
27+
type FieldElement = FieldElement;
28+
}
29+
2630
impl PrimeCurveArithmetic for BrainpoolP384r1 {
2731
type CurveGroup = ProjectivePoint;
2832
}
2933

3034
impl PrimeCurveParams for BrainpoolP384r1 {
31-
type FieldElement = FieldElement;
3235
type PointArithmetic = point_arithmetic::EquationAIsGeneric;
3336
type Backend = backend::VariableOnly;
3437

bp384/src/t1/arithmetic.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use super::BrainpoolP384t1;
44
use crate::{FieldElement, Scalar};
5-
use elliptic_curve::{CurveArithmetic, PrimeCurveArithmetic};
5+
use elliptic_curve::{CurveArithmetic, PrimeCurveArithmetic, hazmat::FieldArithmetic};
66
use primeorder::{PrimeCurveParams, backend, point_arithmetic};
77

88
/// Elliptic curve point in affine coordinates.
@@ -23,12 +23,15 @@ impl CurveArithmetic for BrainpoolP384t1 {
2323
type Scalar = Scalar;
2424
}
2525

26+
impl FieldArithmetic for BrainpoolP384t1 {
27+
type FieldElement = FieldElement;
28+
}
29+
2630
impl PrimeCurveArithmetic for BrainpoolP384t1 {
2731
type CurveGroup = ProjectivePoint;
2832
}
2933

3034
impl PrimeCurveParams for BrainpoolP384t1 {
31-
type FieldElement = FieldElement;
3235
type PointArithmetic = point_arithmetic::EquationAIsGeneric;
3336
type Backend = backend::VariableOnly;
3437

k256/src/arithmetic.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@ pub use field::FieldElement;
1717

1818
use self::{affine::AffinePoint, projective::ProjectivePoint, scalar::Scalar};
1919
use crate::Secp256k1;
20-
use elliptic_curve::CurveArithmetic;
20+
use elliptic_curve::{CurveArithmetic, hazmat::FieldArithmetic};
2121

2222
impl CurveArithmetic for Secp256k1 {
2323
type AffinePoint = AffinePoint;
2424
type ProjectivePoint = ProjectivePoint;
2525
type Scalar = Scalar;
2626
}
2727

28+
impl FieldArithmetic for Secp256k1 {
29+
type FieldElement = FieldElement;
30+
}
31+
2832
const CURVE_EQUATION_B_SINGLE: u32 = 7u32;
2933

3034
#[rustfmt::skip]

k256/src/arithmetic/field.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ use num_bigint::{BigUint, ToBigUint};
4242
const MODULUS_HEX: &str = "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f";
4343

4444
/// An element in the finite field used for curve coordinates.
45+
///
46+
/// <div class="warning">
47+
/// <b>Security Warning</b>
48+
///
49+
/// This type implements lazy normalization. Failure to use it correctly can lead to miscomputation!
50+
/// Please be extremely sure you understand how this normalization works before you do ANYTHING
51+
/// with this type.
52+
/// </div>
4553
#[derive(Clone, Copy, Debug)]
4654
pub struct FieldElement(FieldElementImpl);
4755

0 commit comments

Comments
 (0)