@@ -41,15 +41,35 @@ impl Keypair {
41
41
/// Encode the keypair into a byte array by concatenating the bytes
42
42
/// of the secret scalar and the compressed public point,
43
43
/// an informal standard for encoding Ed25519 keypairs.
44
+ #[ deprecated( since = "0.2.0" , note = "Renamed to `Keypair::to_bytes`" ) ]
44
45
pub fn encode ( & self ) -> [ u8 ; 64 ] {
46
+ self . to_bytes ( )
47
+ }
48
+
49
+ /// Convert the keypair into a byte array by concatenating the bytes
50
+ /// of the secret scalar and the compressed public point,
51
+ /// an informal standard for encoding Ed25519 keypairs.
52
+ pub fn to_bytes ( & self ) -> [ u8 ; 64 ] {
45
53
self . 0 . to_bytes ( )
46
54
}
47
55
48
56
/// Decode a keypair from the [binary format](https://datatracker.ietf.org/doc/html/rfc8032#section-5.1.5)
49
- /// produced by [`Keypair::encode `], zeroing the input on success.
57
+ /// produced by [`Keypair::to_bytes `], zeroing the input on success.
50
58
///
51
59
/// Note that this binary format is the same as `ed25519_dalek`'s and `ed25519_zebra`'s.
60
+ #[ deprecated(
61
+ since = "0.2.0" ,
62
+ note = "This method name does not follow Rust naming conventions, use `Keypair::try_from_bytes` instead."
63
+ ) ]
52
64
pub fn decode ( kp : & mut [ u8 ] ) -> Result < Keypair , DecodingError > {
65
+ Self :: try_from_bytes ( kp)
66
+ }
67
+
68
+ /// Try to parse a keypair from the [binary format](https://datatracker.ietf.org/doc/html/rfc8032#section-5.1.5)
69
+ /// produced by [`Keypair::to_bytes`], zeroing the input on success.
70
+ ///
71
+ /// Note that this binary format is the same as `ed25519_dalek`'s and `ed25519_zebra`'s.
72
+ pub fn try_from_bytes ( kp : & mut [ u8 ] ) -> Result < Keypair , DecodingError > {
53
73
ed25519:: Keypair :: from_bytes ( kp)
54
74
. map ( |k| {
55
75
kp. zeroize ( ) ;
@@ -70,7 +90,7 @@ impl Keypair {
70
90
71
91
/// Get the secret key of this keypair.
72
92
pub fn secret ( & self ) -> SecretKey {
73
- SecretKey :: from_bytes ( & mut self . 0 . secret . to_bytes ( ) )
93
+ SecretKey :: try_from_bytes ( & mut self . 0 . secret . to_bytes ( ) )
74
94
. expect ( "ed25519::SecretKey::from_bytes(to_bytes(k)) != k" )
75
95
}
76
96
}
@@ -86,7 +106,7 @@ impl fmt::Debug for Keypair {
86
106
impl Clone for Keypair {
87
107
fn clone ( & self ) -> Keypair {
88
108
let mut sk_bytes = self . 0 . secret . to_bytes ( ) ;
89
- let secret = SecretKey :: from_bytes ( & mut sk_bytes)
109
+ let secret = SecretKey :: try_from_bytes ( & mut sk_bytes)
90
110
. expect ( "ed25519::SecretKey::from_bytes(to_bytes(k)) != k" )
91
111
. 0 ;
92
112
@@ -164,12 +184,31 @@ impl PublicKey {
164
184
165
185
/// Encode the public key into a byte array in compressed form, i.e.
166
186
/// where one coordinate is represented by a single bit.
187
+ #[ deprecated(
188
+ since = "0.2.0" ,
189
+ note = "Renamed to `PublicKey::to_bytes` to reflect actual behaviour."
190
+ ) ]
167
191
pub fn encode ( & self ) -> [ u8 ; 32 ] {
192
+ self . to_bytes ( )
193
+ }
194
+
195
+ /// Convert the public key to a byte array in compressed form, i.e.
196
+ /// where one coordinate is represented by a single bit.
197
+ pub fn to_bytes ( & self ) -> [ u8 ; 32 ] {
168
198
self . 0 . to_bytes ( )
169
199
}
170
200
171
- /// Decode a public key from a byte array as produced by `encode`.
201
+ /// Decode a public key from a byte array as produced by `to_bytes`.
202
+ #[ deprecated(
203
+ since = "0.2.0" ,
204
+ note = "This method name does not follow Rust naming conventions, use `PublicKey::try_from_bytes` instead."
205
+ ) ]
172
206
pub fn decode ( k : & [ u8 ] ) -> Result < PublicKey , DecodingError > {
207
+ Self :: try_from_bytes ( k)
208
+ }
209
+
210
+ /// Try to parse a public key from a byte array containing the actual key as produced by `to_bytes`.
211
+ pub fn try_from_bytes ( k : & [ u8 ] ) -> Result < PublicKey , DecodingError > {
173
212
ed25519:: PublicKey :: from_bytes ( k)
174
213
. map_err ( |e| DecodingError :: failed_to_parse ( "Ed25519 public key" , e) )
175
214
. map ( PublicKey )
@@ -189,7 +228,8 @@ impl AsRef<[u8]> for SecretKey {
189
228
impl Clone for SecretKey {
190
229
fn clone ( & self ) -> SecretKey {
191
230
let mut sk_bytes = self . 0 . to_bytes ( ) ;
192
- Self :: from_bytes ( & mut sk_bytes) . expect ( "ed25519::SecretKey::from_bytes(to_bytes(k)) != k" )
231
+ Self :: try_from_bytes ( & mut sk_bytes)
232
+ . expect ( "ed25519::SecretKey::from_bytes(to_bytes(k)) != k" )
193
233
}
194
234
}
195
235
@@ -214,7 +254,20 @@ impl SecretKey {
214
254
/// Create an Ed25519 secret key from a byte slice, zeroing the input on success.
215
255
/// If the bytes do not constitute a valid Ed25519 secret key, an error is
216
256
/// returned.
257
+ #[ deprecated(
258
+ since = "0.2.0" ,
259
+ note = "This method name does not follow Rust naming conventions, use `SecretKey::try_from_bytes` instead."
260
+ ) ]
261
+ #[ allow( unused_mut) ]
217
262
pub fn from_bytes ( mut sk_bytes : impl AsMut < [ u8 ] > ) -> Result < SecretKey , DecodingError > {
263
+ Self :: try_from_bytes ( sk_bytes)
264
+ }
265
+
266
+ /// Try to parse an Ed25519 secret key from a byte slice
267
+ /// containing the actual key, zeroing the input on success.
268
+ /// If the bytes do not constitute a valid Ed25519 secret key, an error is
269
+ /// returned.
270
+ pub fn try_from_bytes ( mut sk_bytes : impl AsMut < [ u8 ] > ) -> Result < SecretKey , DecodingError > {
218
271
let sk_bytes = sk_bytes. as_mut ( ) ;
219
272
let secret = ed25519:: SecretKey :: from_bytes ( & * sk_bytes)
220
273
. map_err ( |e| DecodingError :: failed_to_parse ( "Ed25519 secret key" , e) ) ?;
@@ -236,8 +289,8 @@ mod tests {
236
289
fn ed25519_keypair_encode_decode ( ) {
237
290
fn prop ( ) -> bool {
238
291
let kp1 = Keypair :: generate ( ) ;
239
- let mut kp1_enc = kp1. encode ( ) ;
240
- let kp2 = Keypair :: decode ( & mut kp1_enc) . unwrap ( ) ;
292
+ let mut kp1_enc = kp1. to_bytes ( ) ;
293
+ let kp2 = Keypair :: try_from_bytes ( & mut kp1_enc) . unwrap ( ) ;
241
294
eq_keypairs ( & kp1, & kp2) && kp1_enc. iter ( ) . all ( |b| * b == 0 )
242
295
}
243
296
QuickCheck :: new ( ) . tests ( 10 ) . quickcheck ( prop as fn ( ) -> _ ) ;
@@ -248,7 +301,7 @@ mod tests {
248
301
fn prop ( ) -> bool {
249
302
let kp1 = Keypair :: generate ( ) ;
250
303
let mut sk = kp1. 0 . secret . to_bytes ( ) ;
251
- let kp2 = Keypair :: from ( SecretKey :: from_bytes ( & mut sk) . unwrap ( ) ) ;
304
+ let kp2 = Keypair :: from ( SecretKey :: try_from_bytes ( & mut sk) . unwrap ( ) ) ;
252
305
eq_keypairs ( & kp1, & kp2) && sk == [ 0u8 ; 32 ]
253
306
}
254
307
QuickCheck :: new ( ) . tests ( 10 ) . quickcheck ( prop as fn ( ) -> _ ) ;
0 commit comments