1
1
#![ no_std]
2
2
3
+ use ed448_goldilocks:: Ed448 ;
3
4
use ed448_goldilocks:: MontgomeryPoint ;
4
- use ed448_goldilocks:: Scalar ;
5
+ use ed448_goldilocks:: elliptic_curve:: {
6
+ bigint:: U448 , group:: GroupEncoding , scalar:: FromUintUnchecked ,
7
+ } ;
5
8
use rand_core:: { CryptoRng , RngCore } ;
6
9
use zeroize:: Zeroize ;
7
10
11
+ type Scalar = ed448_goldilocks:: Scalar < Ed448 > ;
12
+
8
13
/// Computes a Scalar according to RFC7748
9
14
/// given a byte array of length 56
10
15
impl From < [ u8 ; 56 ] > for Secret {
11
16
fn from ( arr : [ u8 ; 56 ] ) -> Secret {
12
- let mut secret = Secret ( arr) ;
17
+ let mut secret = Secret ( arr. into ( ) ) ;
13
18
secret. clamp ( ) ;
14
19
secret
15
20
}
@@ -20,16 +25,18 @@ impl From<[u8; 56]> for Secret {
20
25
/// XXX: Waiting for upstream PR to use pre-computation
21
26
impl From < & Secret > for PublicKey {
22
27
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;
24
30
PublicKey ( point)
25
31
}
26
32
}
27
33
28
34
/// A PublicKey is a point on Curve448.
35
+ #[ derive( Debug , PartialEq , Eq , Copy , Clone ) ]
29
36
pub struct PublicKey ( MontgomeryPoint ) ;
30
37
31
38
/// A Secret is a Scalar on Curve448.
32
- #[ derive( Zeroize ) ]
39
+ #[ derive( Clone , Zeroize ) ]
33
40
#[ zeroize( drop) ]
34
41
pub struct Secret ( [ u8 ; 56 ] ) ;
35
42
@@ -85,7 +92,7 @@ impl Secret {
85
92
// Taken from dalek-x25519
86
93
pub fn new < T > ( csprng : & mut T ) -> Self
87
94
where
88
- T : RngCore + CryptoRng ,
95
+ T : RngCore + CryptoRng + ? Sized ,
89
96
{
90
97
let mut bytes = [ 0u8 ; 56 ] ;
91
98
@@ -102,7 +109,8 @@ impl Secret {
102
109
103
110
/// Views a Secret as a Scalar
104
111
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)
106
114
}
107
115
108
116
/// Performs a Diffie-hellman key exchange between the secret key and an external public key
@@ -171,27 +179,29 @@ mod test {
171
179
use super :: * ;
172
180
use alloc:: vec;
173
181
182
+ use ed448_goldilocks:: { LOW_A , LOW_B , LOW_C } ;
183
+
174
184
#[ test]
175
185
fn test_low_order ( ) {
176
186
// 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
+ // ]);
195
205
196
206
// Notice, that this is the only way to add low order points into the system
197
207
// and this is not exposed to the user. The user will use `from_bytes` which will check for low order points.
0 commit comments