Skip to content

Commit a9ee7cd

Browse files
committed
Implement CurveArithmetic for Curve448
1 parent 0412d03 commit a9ee7cd

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::{U56, U57},
7171
bigint::{ArrayEncoding, Odd, U448},
7272
point::PointCompression,
@@ -79,14 +79,14 @@ use sha3::Shake256;
7979
pub struct Ed448;
8080

8181
/// Bytes of the Ed448 field
82-
pub type Ed448FieldBytes = elliptic_curve::FieldBytes<Ed448>;
82+
pub type Ed448FieldBytes = FieldBytes<Ed448>;
8383

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

8888
/// Non-zero scalar of the Ed448 scalar
89-
pub type Ed448NonZeroScalar = elliptic_curve::NonZeroScalar<Ed448>;
89+
pub type Ed448NonZeroScalar = NonZeroScalar<Ed448>;
9090

9191
impl Curve for Ed448 {
9292
type FieldBytesSize = U57;
@@ -113,7 +113,7 @@ impl FieldBytesEncoding<Ed448> for U448 {
113113
}
114114
}
115115

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

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)