Skip to content

Commit 7329a91

Browse files
committed
Implement CurveArithmetic for Curve448
1 parent c870bf0 commit 7329a91

File tree

7 files changed

+308
-33
lines changed

7 files changed

+308
-33
lines changed

ed448-goldilocks/src/constants.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,9 @@ pub const EDWARDS_BASEPOINT_ORDER: EdwardsScalar = EdwardsScalar::new(ORDER);
1818
/// \ell = 2^\{446\} + 0x8335dc163bb124b65129c96fde933d8d723a70aadc873d6d54a7bb0d.
1919
/// $$
2020
pub const DECAF_BASEPOINT_ORDER: DecafScalar = DecafScalar::new(ORDER);
21+
22+
/// `BASEPOINT_ORDER` is the order of the Curve448 basepoint, i.e.,
23+
/// $$
24+
/// \ell = 2^\{446\} + 0x8335dc163bb124b65129c96fde933d8d723a70aadc873d6d54a7bb0d.
25+
/// $$
26+
pub const MONTGOMERY_BASEPOINT_ORDER: MontgomeryScalar = MontgomeryScalar::new(ORDER);

ed448-goldilocks/src/field/element.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ use core::fmt::{self, Debug, Display, Formatter, LowerHex, UpperHex};
22
use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
33

44
use super::ConstMontyType;
5-
use crate::{
6-
AffinePoint, Decaf448, DecafPoint, Ed448, EdwardsPoint,
7-
curve::twedwards::extended::ExtendedPoint as TwistedExtendedPoint,
8-
};
5+
use crate::curve::twedwards::extended::ExtendedPoint as TwistedExtendedPoint;
6+
use crate::{AffinePoint, Decaf448, DecafPoint, Ed448, EdwardsPoint, ORDER};
97
use elliptic_curve::{
108
array::Array,
119
bigint::{
@@ -16,7 +14,10 @@ use elliptic_curve::{
1614
zeroize::DefaultIsZeroes,
1715
};
1816
use hash2curve::{FromOkm, MapToCurve};
19-
use subtle::{Choice, ConditionallyNegatable, ConditionallySelectable, ConstantTimeEq};
17+
use subtle::{
18+
Choice, ConditionallyNegatable, ConditionallySelectable, ConstantTimeEq, ConstantTimeLess,
19+
CtOption,
20+
};
2021

2122
#[derive(Clone, Copy, Default)]
2223
pub struct FieldElement(pub(crate) ConstMontyType);
@@ -320,6 +321,12 @@ impl FieldElement {
320321
Self(ConstMontyType::new(&U448::from_le_slice(bytes)))
321322
}
322323

324+
pub fn from_repr(bytes: &[u8; 56]) -> CtOption<Self> {
325+
let integer = U448::from_le_slice(bytes);
326+
let is_some = integer.ct_lt(&ORDER);
327+
CtOption::new(Self(ConstMontyType::from_montgomery(integer)), is_some)
328+
}
329+
323330
pub fn double(&self) -> Self {
324331
Self(self.0.add(&self.0))
325332
}

ed448-goldilocks/src/lib.rs

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub use ristretto::{CompressedRistretto, RistrettoPoint};
7070
pub use sign::*;
7171

7272
use elliptic_curve::{
73-
Curve, FieldBytesEncoding, PrimeCurve,
73+
Curve, CurveArithmetic, FieldBytes, FieldBytesEncoding, NonZeroScalar, PrimeCurve,
7474
array::typenum::{U28, U56, U57},
7575
bigint::{ArrayEncoding, U448},
7676
point::PointCompression,
@@ -82,14 +82,14 @@ use hash2curve::GroupDigest;
8282
pub struct Ed448;
8383

8484
/// Bytes of the Ed448 field
85-
pub type Ed448FieldBytes = elliptic_curve::FieldBytes<Ed448>;
85+
pub type Ed448FieldBytes = FieldBytes<Ed448>;
8686

8787
/// Scalar bits of the Ed448 scalar
8888
#[cfg(feature = "bits")]
8989
pub type Ed448ScalarBits = elliptic_curve::scalar::ScalarBits<Ed448>;
9090

9191
/// Non-zero scalar of the Ed448 scalar
92-
pub type Ed448NonZeroScalar = elliptic_curve::NonZeroScalar<Ed448>;
92+
pub type Ed448NonZeroScalar = NonZeroScalar<Ed448>;
9393

9494
impl Curve for Ed448 {
9595
type FieldBytesSize = U57;
@@ -116,7 +116,7 @@ impl FieldBytesEncoding<Ed448> for U448 {
116116
}
117117
}
118118

119-
impl elliptic_curve::CurveArithmetic for Ed448 {
119+
impl CurveArithmetic for Ed448 {
120120
type AffinePoint = AffinePoint;
121121
type ProjectivePoint = EdwardsPoint;
122122
type Scalar = EdwardsScalar;
@@ -131,14 +131,14 @@ impl GroupDigest for Ed448 {
131131
pub struct Decaf448;
132132

133133
/// Bytes of the Decaf448 field
134-
pub type Decaf448FieldBytes = elliptic_curve::FieldBytes<Decaf448>;
134+
pub type Decaf448FieldBytes = FieldBytes<Decaf448>;
135135

136136
/// Scalar bits of the Decaf448 scalar
137137
#[cfg(feature = "bits")]
138138
pub type Decaf448ScalarBits = elliptic_curve::scalar::ScalarBits<Decaf448>;
139139

140140
/// Non-zero scalar of the Decaf448 scalar
141-
pub type Decaf448NonZeroScalar = elliptic_curve::NonZeroScalar<Decaf448>;
141+
pub type Decaf448NonZeroScalar = NonZeroScalar<Decaf448>;
142142

143143
impl Curve for Decaf448 {
144144
type FieldBytesSize = U56;
@@ -165,7 +165,7 @@ impl FieldBytesEncoding<Decaf448> for U448 {
165165
}
166166
}
167167

168-
impl elliptic_curve::CurveArithmetic for Decaf448 {
168+
impl CurveArithmetic for Decaf448 {
169169
type AffinePoint = DecafAffinePoint;
170170
type ProjectivePoint = DecafPoint;
171171
type Scalar = DecafScalar;
@@ -178,3 +178,44 @@ impl GroupDigest for Decaf448 {
178178
/// Curve448 curve.
179179
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
180180
pub struct Curve448;
181+
182+
/// Bytes of the Curve448 field
183+
pub type Curve448FieldBytes = FieldBytes<Curve448>;
184+
185+
/// Scalar bits of the Curve448 scalar
186+
#[cfg(feature = "bits")]
187+
pub type Curve448ScalarBits = elliptic_curve::scalar::ScalarBits<Curve448>;
188+
189+
/// Non-zero scalar of the Curve448 scalar
190+
pub type Curve448NonZeroScalar = NonZeroScalar<Curve448>;
191+
192+
impl Curve for Curve448 {
193+
type FieldBytesSize = U56;
194+
type Uint = U448;
195+
196+
const ORDER: U448 = ORDER;
197+
}
198+
199+
impl PrimeCurve for Curve448 {}
200+
201+
impl PointCompression for Curve448 {
202+
const COMPRESS_POINTS: bool = true;
203+
}
204+
205+
impl FieldBytesEncoding<Curve448> for U448 {
206+
fn decode_field_bytes(field_bytes: &Curve448FieldBytes) -> Self {
207+
U448::from_le_slice(field_bytes)
208+
}
209+
210+
fn encode_field_bytes(&self) -> Curve448FieldBytes {
211+
let mut data = Curve448FieldBytes::default();
212+
data.copy_from_slice(&self.to_le_byte_array()[..]);
213+
data
214+
}
215+
}
216+
217+
impl CurveArithmetic for Curve448 {
218+
type AffinePoint = MontgomeryPoint;
219+
type ProjectivePoint = ProjectiveMontgomeryPoint;
220+
type Scalar = MontgomeryScalar;
221+
}

ed448-goldilocks/src/montgomery/ops.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::field::{ConstMontyType, FieldElement};
33
use core::borrow::Borrow;
44
use core::iter::Sum;
55
use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
6+
use elliptic_curve::CurveGroup;
67
use elliptic_curve::bigint::U448;
78

89
use super::{MontgomeryPoint, MontgomeryScalar, ProjectiveMontgomeryPoint};

0 commit comments

Comments
 (0)