11#![ no_std]
22
3+ use ed448_goldilocks:: Ed448 ;
34use ed448_goldilocks:: MontgomeryPoint ;
4- use ed448_goldilocks:: Scalar ;
5+ use ed448_goldilocks:: elliptic_curve:: {
6+ bigint:: U448 , group:: GroupEncoding , scalar:: FromUintUnchecked ,
7+ } ;
58use rand_core:: { CryptoRng , RngCore } ;
69use zeroize:: Zeroize ;
710
11+ type Scalar = ed448_goldilocks:: Scalar < Ed448 > ;
12+
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,16 +25,18 @@ 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 secret = secret. as_scalar ( ) ;
29+ let point = & MontgomeryPoint :: GENERATOR * & secret;
2430 PublicKey ( point)
2531 }
2632}
2733
2834/// A PublicKey is a point on Curve448.
35+ #[ derive( Debug , PartialEq , Eq , Copy , Clone ) ]
2936pub struct PublicKey ( MontgomeryPoint ) ;
3037
3138/// A Secret is a Scalar on Curve448.
32- #[ derive( Zeroize ) ]
39+ #[ derive( Clone , Zeroize ) ]
3340#[ zeroize( drop) ]
3441pub struct Secret ( [ u8 ; 56 ] ) ;
3542
@@ -85,7 +92,7 @@ impl Secret {
8592 // Taken from dalek-x25519
8693 pub fn new < T > ( csprng : & mut T ) -> Self
8794 where
88- T : RngCore + CryptoRng ,
95+ T : RngCore + CryptoRng + ? Sized ,
8996 {
9097 let mut bytes = [ 0u8 ; 56 ] ;
9198
@@ -102,7 +109,8 @@ impl Secret {
102109
103110 /// Views a Secret as a Scalar
104111 fn as_scalar ( & self ) -> Scalar {
105- Scalar :: from_bytes ( & self . 0 )
112+ let secret = U448 :: from_le_slice ( & self . 0 ) ;
113+ Scalar :: from_uint_unchecked ( secret)
106114 }
107115
108116 /// Performs a Diffie-hellman key exchange between the secret key and an external public key
@@ -171,27 +179,29 @@ mod test {
171179 use super :: * ;
172180 use alloc:: vec;
173181
182+ use ed448_goldilocks:: { LOW_A , LOW_B , LOW_C } ;
183+
174184 #[ test]
175185 fn test_low_order ( ) {
176186 // These are also in ed448-goldilocks. We could export them, but I cannot see any use except for this test.
177- const LOW_A : MontgomeryPoint = MontgomeryPoint ( [
178- 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
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- ] ) ;
183- const LOW_B : MontgomeryPoint = MontgomeryPoint ( [
184- 0x01 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
185- 0x00 , 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- ] ) ;
189- const LOW_C : MontgomeryPoint = MontgomeryPoint ( [
190- 0xfe , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff ,
191- 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff ,
192- 0xfe , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff ,
193- 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff ,
194- ] ) ;
187+ // const LOW_A: MontgomeryPoint = MontgomeryPoint([
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+ // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
192+ // ]);
193+ // const LOW_B: MontgomeryPoint = MontgomeryPoint([
194+ // 0x01, 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+ // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
198+ // ]);
199+ // const LOW_C: MontgomeryPoint = MontgomeryPoint([
200+ // 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
201+ // 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
202+ // 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
203+ // 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
204+ // ]);
195205
196206 // Notice, that this is the only way to add low order points into the system
197207 // and this is not exposed to the user. The user will use `from_bytes` which will check for low order points.
0 commit comments