Skip to content

Commit 022faaa

Browse files
committed
Implement CurveArithmetic for Curve448
1 parent ef66614 commit 022faaa

File tree

5 files changed

+367
-21
lines changed

5 files changed

+367
-21
lines changed

ed448-goldilocks/src/lib.rs

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub use montgomery::{
6666
pub use sign::*;
6767

6868
use elliptic_curve::{
69-
Curve, FieldBytesEncoding, PrimeCurve,
69+
Curve, CurveArithmetic, FieldBytes, FieldBytesEncoding, NonZeroScalar, PrimeCurve,
7070
array::typenum::{U28, U56, U57},
7171
bigint::{ArrayEncoding, Odd, U448},
7272
point::PointCompression,
@@ -78,14 +78,14 @@ use hash2curve::GroupDigest;
7878
pub struct Ed448;
7979

8080
/// Bytes of the Ed448 field
81-
pub type Ed448FieldBytes = elliptic_curve::FieldBytes<Ed448>;
81+
pub type Ed448FieldBytes = FieldBytes<Ed448>;
8282

8383
/// Scalar bits of the Ed448 scalar
8484
#[cfg(feature = "bits")]
8585
pub type Ed448ScalarBits = elliptic_curve::scalar::ScalarBits<Ed448>;
8686

8787
/// Non-zero scalar of the Ed448 scalar
88-
pub type Ed448NonZeroScalar = elliptic_curve::NonZeroScalar<Ed448>;
88+
pub type Ed448NonZeroScalar = NonZeroScalar<Ed448>;
8989

9090
impl Curve for Ed448 {
9191
type FieldBytesSize = U57;
@@ -112,7 +112,7 @@ impl FieldBytesEncoding<Ed448> for U448 {
112112
}
113113
}
114114

115-
impl elliptic_curve::CurveArithmetic for Ed448 {
115+
impl CurveArithmetic for Ed448 {
116116
type AffinePoint = AffinePoint;
117117
type ProjectivePoint = EdwardsPoint;
118118
type Scalar = EdwardsScalar;
@@ -127,14 +127,14 @@ impl GroupDigest for Ed448 {
127127
pub struct Decaf448;
128128

129129
/// Bytes of the Decaf448 field
130-
pub type Decaf448FieldBytes = elliptic_curve::FieldBytes<Decaf448>;
130+
pub type Decaf448FieldBytes = FieldBytes<Decaf448>;
131131

132132
/// Scalar bits of the Decaf448 scalar
133133
#[cfg(feature = "bits")]
134134
pub type Decaf448ScalarBits = elliptic_curve::scalar::ScalarBits<Decaf448>;
135135

136136
/// Non-zero scalar of the Decaf448 scalar
137-
pub type Decaf448NonZeroScalar = elliptic_curve::NonZeroScalar<Decaf448>;
137+
pub type Decaf448NonZeroScalar = NonZeroScalar<Decaf448>;
138138

139139
impl Curve for Decaf448 {
140140
type FieldBytesSize = U56;
@@ -161,7 +161,7 @@ impl FieldBytesEncoding<Decaf448> for U448 {
161161
}
162162
}
163163

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

ed448-goldilocks/src/montgomery/ops.rs

Lines changed: 3 additions & 5 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, MontgomeryXpoint, ProjectiveMontgomeryPoint};
@@ -141,7 +142,7 @@ impl Mul<&MontgomeryScalar> for &ProjectiveMontgomeryPoint {
141142

142143
#[inline]
143144
fn mul(self, scalar: &MontgomeryScalar) -> ProjectiveMontgomeryPoint {
144-
MontgomeryPoint::from(self) * scalar
145+
self.to_affine() * scalar
145146
}
146147
}
147148

@@ -320,9 +321,6 @@ mod test {
320321
* MontgomeryScalar::try_from_rng(&mut OsRng).unwrap();
321322
let p3 = p1 + p2;
322323

323-
assert_eq!(
324-
MontgomeryPoint::from(p3),
325-
(MontgomeryPoint::from(p1) + p2).into()
326-
);
324+
assert_eq!(p3.to_affine(), (p1.to_affine() + p2).into());
327325
}
328326
}

0 commit comments

Comments
 (0)