1
1
#![ no_std]
2
2
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 ;
5
10
use rand_core:: { CryptoRng , RngCore } ;
6
11
use zeroize:: Zeroize ;
7
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,25 +25,26 @@ 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 point =
29
+ & MontgomeryXpoint :: GENERATOR * & Scalar :: from_canonical_bytes ( & secret. 0 ) . unwrap ( ) ;
24
30
PublicKey ( point)
25
31
}
26
32
}
27
33
28
34
/// A PublicKey is a point on Curve448.
29
35
#[ derive( Debug , PartialEq , Eq , Copy , Clone ) ]
30
- pub struct PublicKey ( MontgomeryPoint ) ;
36
+ pub struct PublicKey ( MontgomeryXpoint ) ;
31
37
32
38
/// A Secret is a Scalar on Curve448.
33
39
#[ derive( Clone , Zeroize ) ]
34
40
#[ zeroize( drop) ]
35
- pub struct Secret ( [ u8 ; 56 ] ) ;
41
+ pub struct Secret ( ScalarBytes < Curve448 > ) ;
36
42
37
43
/// A SharedSecret is a point on Curve448.
38
44
/// This point is the result of a Diffie-Hellman key exchange.
39
45
#[ derive( Zeroize ) ]
40
46
#[ zeroize( drop) ]
41
- pub struct SharedSecret ( MontgomeryPoint ) ;
47
+ pub struct SharedSecret ( MontgomeryXpoint ) ;
42
48
43
49
impl PublicKey {
44
50
/// Converts a bytes slice into a Public key
@@ -63,7 +69,7 @@ impl PublicKey {
63
69
64
70
// Check if the point has low order
65
71
let arr = slice_to_array ( bytes) ;
66
- let point = MontgomeryPoint ( arr) ;
72
+ let point = MontgomeryXpoint ( arr) ;
67
73
68
74
Some ( PublicKey ( point) )
69
75
}
@@ -102,8 +108,8 @@ impl Secret {
102
108
}
103
109
104
110
/// 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 ( )
107
113
}
108
114
109
115
/// Performs a Diffie-hellman key exchange between the secret key and an external public key
@@ -134,7 +140,7 @@ impl Secret {
134
140
135
141
/// Converts a secret into a byte array
136
142
pub fn as_bytes ( & self ) -> & [ u8 ; 56 ] {
137
- & self . 0
143
+ & self . 0 . as_ref ( )
138
144
}
139
145
}
140
146
@@ -158,12 +164,12 @@ pub fn x448(scalar_bytes: [u8; 56], point_bytes: [u8; 56]) -> Option<[u8; 56]> {
158
164
/// An unchecked version of the x448 function defined in RFC448
159
165
/// No checks are made on the points.
160
166
pub 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) ;
162
168
let scalar = Secret :: from ( scalar_bytes) . as_scalar ( ) ;
163
169
( & point * & scalar) . 0
164
170
}
165
171
166
- pub const X448_BASEPOINT_BYTES : [ u8 ; 56 ] = MontgomeryPoint :: GENERATOR . 0 ;
172
+ pub const X448_BASEPOINT_BYTES : [ u8 ; 56 ] = MontgomeryXpoint :: GENERATOR . 0 ;
167
173
168
174
#[ cfg( test) ]
169
175
mod test {
@@ -172,27 +178,29 @@ mod test {
172
178
use super :: * ;
173
179
use alloc:: vec;
174
180
181
+ use ed448_goldilocks:: { LOW_A , LOW_B , LOW_C } ;
182
+
175
183
#[ test]
176
184
fn test_low_order ( ) {
177
185
// 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
+ // ]);
196
204
197
205
// Notice, that this is the only way to add low order points into the system
198
206
// 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 {
393
401
0xda , 0x8d , 0x52 , 0x4d , 0xe3 , 0xd6 , 0x9b , 0xd9 , 0xd9 , 0xd6 , 0x6b , 0x99 , 0x7e , 0x37 ,
394
402
] ;
395
403
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 ;
398
406
let mut result = [ 0u8 ; 56 ] ;
399
407
400
408
// Iterate 1 time then check value on 1st iteration
0 commit comments