@@ -11,7 +11,6 @@ use curve25519_dalek::{
11
11
ristretto:: { CompressedRistretto , RistrettoPoint } ,
12
12
scalar:: Scalar ,
13
13
} ;
14
- use generic_array:: { typenum:: U32 , GenericArray } ;
15
14
use rand_core:: { CryptoRng , RngCore } ;
16
15
use serde:: { Deserialize , Serialize } ;
17
16
use std:: {
@@ -21,6 +20,12 @@ use std::{
21
20
} ;
22
21
use zeroize:: { Zeroize , ZeroizeOnDrop } ;
23
22
23
+ /// X25519 secret key length
24
+ const X25519_SK_LENGTH : usize = 32 ;
25
+
26
+ /// X25519 public key length
27
+ const X25519_PK_LENGTH : usize = 32 ;
28
+
24
29
/// Asymmetric private key based on Curve25519.
25
30
///
26
31
/// Internally, a curve scalar is used. It is 128-bits long.
@@ -52,25 +57,22 @@ impl X25519PrivateKey {
52
57
}
53
58
}
54
59
55
- impl KeyTrait for X25519PrivateKey {
56
- type Length = U32 ;
57
-
58
- /// Convert the given private key into bytes.
59
- #[ inline]
60
- #[ must_use]
61
- fn to_bytes ( & self ) -> GenericArray < u8 , Self :: Length > {
62
- GenericArray :: < u8 , Self :: Length > :: from ( self . 0 . to_bytes ( ) )
60
+ impl KeyTrait < X25519_SK_LENGTH > for X25519PrivateKey {
61
+ /// Converts the given key into bytes.
62
+ fn to_bytes ( & self ) -> [ u8 ; Self :: LENGTH ] {
63
+ self . 0 . to_bytes ( )
63
64
}
64
65
66
+ /// Converts the given bytes into key.
65
67
fn try_from_bytes ( bytes : & [ u8 ] ) -> Result < Self , CryptoCoreError > {
66
68
Self :: try_from ( bytes)
67
69
}
68
70
}
69
71
70
- impl TryFrom < [ u8 ; 32 ] > for X25519PrivateKey {
72
+ impl TryFrom < [ u8 ; Self :: LENGTH ] > for X25519PrivateKey {
71
73
type Error = CryptoCoreError ;
72
74
73
- fn try_from ( bytes : [ u8 ; 32 ] ) -> Result < Self , Self :: Error > {
75
+ fn try_from ( bytes : [ u8 ; Self :: LENGTH ] ) -> Result < Self , Self :: Error > {
74
76
let scalar = Scalar :: from_canonical_bytes ( bytes) . ok_or_else ( || {
75
77
Self :: Error :: ConversionError (
76
78
"Given bytes do not represent a canonical Scalar!" . to_string ( ) ,
@@ -84,7 +86,7 @@ impl TryFrom<&[u8]> for X25519PrivateKey {
84
86
type Error = CryptoCoreError ;
85
87
86
88
fn try_from ( bytes : & [ u8 ] ) -> Result < Self , Self :: Error > {
87
- let bytes: [ u8 ; 32 ] = bytes. try_into ( ) . map_err ( |e| {
89
+ let bytes: [ u8 ; Self :: LENGTH ] = bytes. try_into ( ) . map_err ( |e| {
88
90
Self :: Error :: ConversionError ( format ! (
89
91
"Error while converting slice of size {} to `X25519PublicKey`: {}" ,
90
92
bytes. len( ) ,
@@ -95,15 +97,9 @@ impl TryFrom<&[u8]> for X25519PrivateKey {
95
97
}
96
98
}
97
99
98
- impl From < & X25519PrivateKey > for [ u8 ; 32 ] {
99
- fn from ( key : & X25519PrivateKey ) -> Self {
100
- key. 0 . to_bytes ( )
101
- }
102
- }
103
-
104
100
// Needed by serde to derive `Deserialize`. Do not use otherwise since there
105
101
// is a copy anyway
106
- impl From < X25519PrivateKey > for [ u8 ; 32 ] {
102
+ impl From < X25519PrivateKey > for [ u8 ; X25519_SK_LENGTH ] {
107
103
fn from ( key : X25519PrivateKey ) -> Self {
108
104
key. 0 . to_bytes ( )
109
105
}
@@ -240,14 +236,11 @@ impl X25519PublicKey {
240
236
}
241
237
}
242
238
243
- impl KeyTrait for X25519PublicKey {
244
- type Length = U32 ;
245
-
246
- /// Convert the given public key into an array of bytes.
239
+ impl KeyTrait < X25519_PK_LENGTH > for X25519PublicKey {
240
+ /// Converts the given public key into an array of bytes.
247
241
#[ inline]
248
- #[ must_use]
249
- fn to_bytes ( & self ) -> GenericArray < u8 , Self :: Length > {
250
- GenericArray :: < u8 , Self :: Length > :: from ( self . 0 . compress ( ) . to_bytes ( ) )
242
+ fn to_bytes ( & self ) -> [ u8 ; Self :: LENGTH ] {
243
+ self . 0 . compress ( ) . to_bytes ( )
251
244
}
252
245
253
246
fn try_from_bytes ( bytes : & [ u8 ] ) -> Result < Self , CryptoCoreError > {
@@ -261,10 +254,10 @@ impl From<&X25519PrivateKey> for X25519PublicKey {
261
254
}
262
255
}
263
256
264
- impl TryFrom < [ u8 ; 32 ] > for X25519PublicKey {
257
+ impl TryFrom < [ u8 ; Self :: LENGTH ] > for X25519PublicKey {
265
258
type Error = CryptoCoreError ;
266
259
267
- fn try_from ( bytes : [ u8 ; 32 ] ) -> Result < Self , Self :: Error > {
260
+ fn try_from ( bytes : [ u8 ; Self :: LENGTH ] ) -> Result < Self , Self :: Error > {
268
261
Ok ( Self ( CompressedRistretto ( bytes) . decompress ( ) . ok_or_else (
269
262
|| {
270
263
CryptoCoreError :: ConversionError (
@@ -279,7 +272,7 @@ impl TryFrom<&[u8]> for X25519PublicKey {
279
272
type Error = CryptoCoreError ;
280
273
281
274
fn try_from ( bytes : & [ u8 ] ) -> Result < Self , Self :: Error > {
282
- let bytes: [ u8 ; 32 ] = bytes. try_into ( ) . map_err ( |e| {
275
+ let bytes: [ u8 ; Self :: LENGTH ] = bytes. try_into ( ) . map_err ( |e| {
283
276
Self :: Error :: ConversionError ( format ! (
284
277
"Error while converting slice of size {} to `X25519PublicKey`: {}" ,
285
278
bytes. len( ) ,
@@ -292,13 +285,13 @@ impl TryFrom<&[u8]> for X25519PublicKey {
292
285
293
286
// Needed by serde to derive `Deserialize`. Do not use otherwise since there
294
287
// is a copy anyway.
295
- impl From < X25519PublicKey > for [ u8 ; 32 ] {
288
+ impl From < X25519PublicKey > for [ u8 ; X25519_PK_LENGTH ] {
296
289
fn from ( key : X25519PublicKey ) -> Self {
297
290
key. 0 . compress ( ) . to_bytes ( )
298
291
}
299
292
}
300
293
301
- impl From < & X25519PublicKey > for [ u8 ; 32 ] {
294
+ impl From < & X25519PublicKey > for [ u8 ; X25519_PK_LENGTH ] {
302
295
fn from ( key : & X25519PublicKey ) -> Self {
303
296
key. 0 . compress ( ) . to_bytes ( )
304
297
}
@@ -379,15 +372,18 @@ impl ZeroizeOnDrop for X25519PublicKey {}
379
372
#[ cfg( test) ]
380
373
mod test {
381
374
use crate :: {
382
- asymmetric_crypto:: { X25519PrivateKey , X25519PublicKey } ,
375
+ asymmetric_crypto:: {
376
+ X25519PrivateKey , X25519PublicKey , X25519_PK_LENGTH , X25519_SK_LENGTH ,
377
+ } ,
383
378
entropy:: CsRng ,
379
+ KeyTrait ,
384
380
} ;
385
381
386
382
#[ test]
387
383
fn test_private_key_serialization ( ) {
388
384
let mut rng = CsRng :: new ( ) ;
389
385
let sk = X25519PrivateKey :: new ( & mut rng) ;
390
- let bytes: [ u8 ; 32 ] = ( & sk ) . into ( ) ;
386
+ let bytes: [ u8 ; X25519_SK_LENGTH ] = sk . to_bytes ( ) ;
391
387
let recovered = X25519PrivateKey :: try_from ( bytes) . unwrap ( ) ;
392
388
assert_eq ! ( sk, recovered) ;
393
389
}
@@ -396,7 +392,7 @@ mod test {
396
392
fn test_public_key_serialization ( ) {
397
393
let mut rng = CsRng :: new ( ) ;
398
394
let pk = X25519PublicKey :: new ( & mut rng) ;
399
- let bytes: [ u8 ; 32 ] = ( & pk ) . into ( ) ;
395
+ let bytes: [ u8 ; X25519_PK_LENGTH ] = pk . to_bytes ( ) ;
400
396
let recovered = super :: X25519PublicKey :: try_from ( bytes) . unwrap ( ) ;
401
397
assert_eq ! ( pk, recovered) ;
402
398
}
0 commit comments