11#![ no_std]
22
3- use ed448_goldilocks:: MontgomeryPoint ;
4- use ed448_goldilocks:: Scalar ;
3+ use ed448_goldilocks:: Curve448 ;
4+ use ed448_goldilocks:: MontgomeryScalar ;
5+ use ed448_goldilocks:: MontgomeryScalar as Scalar ;
6+ use ed448_goldilocks:: MontgomeryXpoint ;
7+ use ed448_goldilocks:: ProjectiveMontgomeryPoint as MontgomeryPoint ;
8+ use ed448_goldilocks:: ScalarBytes ;
9+ use ed448_goldilocks:: elliptic_curve:: group:: GroupEncoding ;
510use rand_core:: { CryptoRng , RngCore } ;
611use zeroize:: Zeroize ;
712
813/// Computes a Scalar according to RFC7748
914/// given a byte array of length 56
1015impl From < [ u8 ; 56 ] > for Secret {
1116 fn from ( arr : [ u8 ; 56 ] ) -> Secret {
12- let mut secret = Secret ( arr) ;
17+ let mut secret = Secret ( arr. into ( ) ) ;
1318 secret. clamp ( ) ;
1419 secret
1520 }
@@ -20,25 +25,26 @@ impl From<[u8; 56]> for Secret {
2025/// XXX: Waiting for upstream PR to use pre-computation
2126impl From < & Secret > for PublicKey {
2227 fn from ( secret : & Secret ) -> PublicKey {
23- let point = & MontgomeryPoint :: GENERATOR * & Scalar :: from_bytes ( & secret. 0 ) ;
28+ let point =
29+ & MontgomeryXpoint :: GENERATOR * & Scalar :: from_canonical_bytes ( & secret. 0 ) . unwrap ( ) ;
2430 PublicKey ( point)
2531 }
2632}
2733
2834/// A PublicKey is a point on Curve448.
2935#[ derive( Debug , PartialEq , Eq , Copy , Clone ) ]
30- pub struct PublicKey ( MontgomeryPoint ) ;
36+ pub struct PublicKey ( MontgomeryXpoint ) ;
3137
3238/// A Secret is a Scalar on Curve448.
3339#[ derive( Clone , Zeroize ) ]
3440#[ zeroize( drop) ]
35- pub struct Secret ( [ u8 ; 56 ] ) ;
41+ pub struct Secret ( ScalarBytes < Curve448 > ) ;
3642
3743/// A SharedSecret is a point on Curve448.
3844/// This point is the result of a Diffie-Hellman key exchange.
3945#[ derive( Zeroize ) ]
4046#[ zeroize( drop) ]
41- pub struct SharedSecret ( MontgomeryPoint ) ;
47+ pub struct SharedSecret ( MontgomeryXpoint ) ;
4248
4349impl PublicKey {
4450 /// Converts a bytes slice into a Public key
@@ -63,7 +69,7 @@ impl PublicKey {
6369
6470 // Check if the point has low order
6571 let arr = slice_to_array ( bytes) ;
66- let point = MontgomeryPoint ( arr) ;
72+ let point = MontgomeryXpoint ( arr) ;
6773
6874 Some ( PublicKey ( point) )
6975 }
@@ -102,8 +108,8 @@ impl Secret {
102108 }
103109
104110 /// Views a Secret as a Scalar
105- fn as_scalar ( & self ) -> Scalar {
106- Scalar :: from_bytes ( & self . 0 )
111+ fn as_scalar ( & self ) -> MontgomeryScalar {
112+ Scalar :: from_canonical_bytes ( & self . 0 ) . unwrap ( )
107113 }
108114
109115 /// Performs a Diffie-hellman key exchange between the secret key and an external public key
@@ -134,7 +140,7 @@ impl Secret {
134140
135141 /// Converts a secret into a byte array
136142 pub fn as_bytes ( & self ) -> & [ u8 ; 56 ] {
137- & self . 0
143+ & self . 0 . as_ref ( )
138144 }
139145}
140146
@@ -158,12 +164,12 @@ pub fn x448(scalar_bytes: [u8; 56], point_bytes: [u8; 56]) -> Option<[u8; 56]> {
158164/// An unchecked version of the x448 function defined in RFC448
159165/// No checks are made on the points.
160166pub fn x448_unchecked ( scalar_bytes : [ u8 ; 56 ] , point_bytes : [ u8 ; 56 ] ) -> [ u8 ; 56 ] {
161- let point = MontgomeryPoint ( point_bytes) ;
167+ let point = MontgomeryXpoint ( point_bytes) ;
162168 let scalar = Secret :: from ( scalar_bytes) . as_scalar ( ) ;
163169 ( & point * & scalar) . 0
164170}
165171
166- pub const X448_BASEPOINT_BYTES : [ u8 ; 56 ] = MontgomeryPoint :: GENERATOR . 0 ;
172+ pub const X448_BASEPOINT_BYTES : [ u8 ; 56 ] = MontgomeryXpoint :: GENERATOR . 0 ;
167173
168174#[ cfg( test) ]
169175mod test {
@@ -172,27 +178,29 @@ mod test {
172178 use super :: * ;
173179 use alloc:: vec;
174180
181+ use ed448_goldilocks:: { LOW_A , LOW_B , LOW_C } ;
182+
175183 #[ test]
176184 fn test_low_order ( ) {
177185 // These are also in ed448-goldilocks. We could export them, but I cannot see any use except for this test.
178- const LOW_A : MontgomeryPoint = MontgomeryPoint ( [
179- 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
180- 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
181- 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
182- 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
183- ] ) ;
184- const LOW_B : MontgomeryPoint = MontgomeryPoint ( [
185- 0x01 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
186- 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
187- 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
188- 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
189- ] ) ;
190- const LOW_C : MontgomeryPoint = MontgomeryPoint ( [
191- 0xfe , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff ,
192- 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff ,
193- 0xfe , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff ,
194- 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff ,
195- ] ) ;
186+ // const LOW_A: MontgomeryPoint = MontgomeryPoint([
187+ // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
188+ // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
189+ // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
190+ // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
191+ // ]);
192+ // const LOW_B: MontgomeryPoint = MontgomeryPoint([
193+ // 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
194+ // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
195+ // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
196+ // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
197+ // ]);
198+ // const LOW_C: MontgomeryPoint = MontgomeryPoint([
199+ // 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
200+ // 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
201+ // 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
202+ // 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
203+ // ]);
196204
197205 // Notice, that this is the only way to add low order points into the system
198206 // and this is not exposed to the user. The user will use `from_bytes` which will check for low order points.
@@ -393,8 +401,8 @@ mod test {
393401 0xda , 0x8d , 0x52 , 0x4d , 0xe3 , 0xd6 , 0x9b , 0xd9 , 0xd9 , 0xd6 , 0x6b , 0x99 , 0x7e , 0x37 ,
394402 ] ;
395403
396- let mut point = MontgomeryPoint :: GENERATOR . 0 ;
397- let mut scalar = MontgomeryPoint :: GENERATOR . 0 ;
404+ let mut point = MontgomeryXpoint :: GENERATOR . 0 ;
405+ let mut scalar = MontgomeryXpoint :: GENERATOR . 0 ;
398406 let mut result = [ 0u8 ; 56 ] ;
399407
400408 // Iterate 1 time then check value on 1st iteration
0 commit comments