Skip to content

Commit 40508e0

Browse files
authored
elliptic-curve: make Curve::Order a NonZero<C::Uint> (#1969)
After RustCrypto/crypto-bigint#917 it's much more ergonomic to have the order be `NonZero`. We could also consider making it `Odd`, although `NonZero` is the immediate need so this is the most straightforward transition.
1 parent 9ca2984 commit 40508e0

File tree

5 files changed

+16
-18
lines changed

5 files changed

+16
-18
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ blobby = { git = "https://github.com/RustCrypto/utils" }
2323
# https://github.com/RustCrypto/utils/pull/1200
2424
# https://github.com/RustCrypto/utils/pull/1201
2525
block-buffer = { git = "https://github.com/RustCrypto/utils", branch = "block-buffer/read-buf" }
26+
crypto-bigint = { git = "https://github.com/RustCrypto/crypto-bigint" }

elliptic-curve/src/dev.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use crate::{
77
BatchNormalize, Curve, CurveArithmetic, CurveGroup, FieldBytesEncoding, PrimeCurve,
88
array::typenum::U32,
9-
bigint::{Limb, U256},
9+
bigint::{Limb, NonZero, U256},
1010
error::{Error, Result},
1111
ops::{Invert, LinearCombination, Reduce, ShrAssign},
1212
point::{AffineCoordinates, NonIdentity},
@@ -70,8 +70,9 @@ impl Curve for MockCurve {
7070
type FieldBytesSize = U32;
7171
type Uint = U256;
7272

73-
const ORDER: U256 =
74-
U256::from_be_hex("ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551");
73+
const ORDER: NonZero<U256> = NonZero::<U256>::from_be_hex(
74+
"ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551",
75+
);
7576
}
7677

7778
impl PrimeCurve for MockCurve {}

elliptic-curve/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ pub use {
135135
#[cfg(feature = "pkcs8")]
136136
pub use pkcs8;
137137

138+
use bigint::NonZero;
138139
use core::{
139140
fmt::Debug,
140141
ops::{Add, ShrAssign},
@@ -179,8 +180,9 @@ pub trait Curve: 'static + Copy + Clone + Debug + Default + Eq + Ord + Send + Sy
179180

180181
/// Order of this elliptic curve, i.e. number of elements in the scalar
181182
/// field.
182-
// TODO(tarcieri): make `NonZero` or `Odd`?
183-
const ORDER: Self::Uint;
183+
// TODO(tarcieri): make `Odd`? the prime order subgroup should always have an odd number of
184+
// elements, even if there is a cofactor
185+
const ORDER: NonZero<Self::Uint>;
184186
}
185187

186188
/// Marker trait for elliptic curves with prime order.

elliptic-curve/src/scalar/primitive.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,12 @@ where
6262
};
6363

6464
/// Scalar modulus.
65-
// TODO(tarcieri): make `NonZero` or `Odd`?
66-
pub const MODULUS: C::Uint = C::ORDER;
65+
pub const MODULUS: NonZero<C::Uint> = C::ORDER;
6766

6867
/// Generate a random [`ScalarPrimitive`].
6968
pub fn random<R: CryptoRng + ?Sized>(rng: &mut R) -> Self {
7069
Self {
71-
inner: C::Uint::random_mod(rng, &NonZero::new(Self::MODULUS).unwrap()),
70+
inner: C::Uint::random_mod(rng, &Self::MODULUS),
7271
}
7372
}
7473

@@ -255,9 +254,7 @@ where
255254

256255
fn add(self, other: &Self) -> Self {
257256
Self {
258-
inner: self
259-
.inner
260-
.add_mod(&other.inner, &NonZero::new(Self::MODULUS).unwrap()),
257+
inner: self.inner.add_mod(&other.inner, &Self::MODULUS),
261258
}
262259
}
263260
}
@@ -299,9 +296,7 @@ where
299296

300297
fn sub(self, other: &Self) -> Self {
301298
Self {
302-
inner: self
303-
.inner
304-
.sub_mod(&other.inner, &NonZero::new(Self::MODULUS).unwrap()),
299+
inner: self.inner.sub_mod(&other.inner, &Self::MODULUS),
305300
}
306301
}
307302
}
@@ -332,7 +327,7 @@ where
332327

333328
fn neg(self) -> Self {
334329
Self {
335-
inner: self.inner.neg_mod(&NonZero::new(Self::MODULUS).unwrap()),
330+
inner: self.inner.neg_mod(&Self::MODULUS),
336331
}
337332
}
338333
}
@@ -362,7 +357,7 @@ where
362357
C: Curve,
363358
{
364359
fn is_high(&self) -> Choice {
365-
let n_2 = C::ORDER >> 1u32;
360+
let n_2 = Self::MODULUS.get() >> 1u32;
366361
self.inner.ct_gt(&n_2)
367362
}
368363
}

0 commit comments

Comments
 (0)