Skip to content

Commit 41fed38

Browse files
authored
elliptic-curve: change Curve::ORDER to be Odd (#2006)
Previously it was `NonZero`. This associated constant is for the curve's prime order subgroup, which will always be odd. There are many modular algorithms which require the modulus is odd, and bounding the `ORDER` on `Odd` ensures those algorithms will be compatible with the scalar field. The `primefield` crate and the params we are computing for every field of every curve are also `Odd` (i.e. the `modulus` of `MontyParams`).
1 parent a54207a commit 41fed38

3 files changed

Lines changed: 11 additions & 14 deletions

File tree

elliptic-curve/src/dev.rs

Lines changed: 2 additions & 2 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, NonZero, U256},
9+
bigint::{Limb, Odd, U256},
1010
error::{Error, Result},
1111
ops::{Invert, LinearCombination, Reduce, ShrAssign},
1212
point::{AffineCoordinates, NonIdentity},
@@ -70,7 +70,7 @@ impl Curve for MockCurve {
7070
type FieldBytesSize = U32;
7171
type Uint = U256;
7272

73-
const ORDER: NonZero<U256> = NonZero::<U256>::from_be_hex(
73+
const ORDER: Odd<U256> = Odd::<U256>::from_be_hex(
7474
"ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551",
7575
);
7676
}

elliptic-curve/src/lib.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub use {
133133
#[cfg(feature = "pkcs8")]
134134
pub use pkcs8;
135135

136-
use bigint::NonZero;
136+
use bigint::Odd;
137137
use core::{
138138
fmt::Debug,
139139
ops::{Add, ShrAssign},
@@ -174,11 +174,8 @@ pub trait Curve: 'static + Copy + Clone + Debug + Default + Eq + Ord + Send + Sy
174174
+ FieldBytesEncoding<Self>
175175
+ ShrAssign<usize>;
176176

177-
/// Order of this elliptic curve, i.e. number of elements in the scalar
178-
/// field.
179-
// TODO(tarcieri): make `Odd`? the prime order subgroup should always have an odd number of
180-
// elements, even if there is a cofactor
181-
const ORDER: NonZero<Self::Uint>;
177+
/// Order of this curve's prime order subgroup, i.e. number of elements in the scalar field.
178+
const ORDER: Odd<Self::Uint>;
182179
}
183180

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

elliptic-curve/src/scalar/primitive.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::{
44
Curve, Error, FieldBytes, FieldBytesEncoding, Result,
55
array::Array,
6-
bigint::{Limb, NonZero, prelude::*},
6+
bigint::{Limb, Odd, prelude::*},
77
scalar::FromUintUnchecked,
88
scalar::IsHigh,
99
};
@@ -62,12 +62,12 @@ where
6262
};
6363

6464
/// Scalar modulus.
65-
pub const MODULUS: NonZero<C::Uint> = C::ORDER;
65+
pub const MODULUS: Odd<C::Uint> = C::ORDER;
6666

6767
/// Generate a random [`ScalarPrimitive`].
6868
pub fn random<R: CryptoRng + ?Sized>(rng: &mut R) -> Self {
6969
Self {
70-
inner: C::Uint::random_mod(rng, &Self::MODULUS),
70+
inner: C::Uint::random_mod(rng, Self::MODULUS.as_nz_ref()),
7171
}
7272
}
7373

@@ -254,7 +254,7 @@ where
254254

255255
fn add(self, other: &Self) -> Self {
256256
Self {
257-
inner: self.inner.add_mod(&other.inner, &Self::MODULUS),
257+
inner: self.inner.add_mod(&other.inner, Self::MODULUS.as_nz_ref()),
258258
}
259259
}
260260
}
@@ -296,7 +296,7 @@ where
296296

297297
fn sub(self, other: &Self) -> Self {
298298
Self {
299-
inner: self.inner.sub_mod(&other.inner, &Self::MODULUS),
299+
inner: self.inner.sub_mod(&other.inner, Self::MODULUS.as_nz_ref()),
300300
}
301301
}
302302
}
@@ -327,7 +327,7 @@ where
327327

328328
fn neg(self) -> Self {
329329
Self {
330-
inner: self.inner.neg_mod(&Self::MODULUS),
330+
inner: self.inner.neg_mod(Self::MODULUS.as_nz_ref()),
331331
}
332332
}
333333
}

0 commit comments

Comments
 (0)