@@ -5,7 +5,6 @@ use crate::internal::ByteVector;
5
5
use crate :: internal:: { array_split_64, take_lock} ;
6
6
use clear_on_drop:: clear:: Clear ;
7
7
use ed25519_dalek;
8
- use ed25519_dalek:: PublicKey ;
9
8
use quick_error:: quick_error;
10
9
use rand;
11
10
use std;
@@ -64,10 +63,10 @@ impl From<SigningKeypair> for [u8; 64] {
64
63
impl SigningKeypair {
65
64
const ENCODED_SIZE_BYTES : usize = 64 ;
66
65
pub fn new < CR : rand:: RngCore + rand:: CryptoRng > ( rng : & Mutex < CR > ) -> SigningKeypair {
67
- let keypair = ed25519_dalek:: Keypair :: generate :: < CR > ( & mut * take_lock ( rng) ) ;
66
+ let signing_key = ed25519_dalek:: SigningKey :: generate :: < CR > ( & mut * take_lock ( rng) ) ;
68
67
69
68
//Unchecked is safe because the public is on the curve and the size is statically guaranteed.
70
- SigningKeypair :: new_unchecked ( keypair . to_bytes ( ) )
69
+ SigningKeypair :: new_unchecked ( signing_key . to_keypair_bytes ( ) )
71
70
}
72
71
///
73
72
///Create a SigningKeypair from a byte array slice. If the array is not the right length or if the public
@@ -94,10 +93,10 @@ impl SigningKeypair {
94
93
///
95
94
pub fn from_bytes ( sized_bytes : & [ u8 ; 64 ] ) -> Result < SigningKeypair , Ed25519Error > {
96
95
let ( priv_key, pub_key) = array_split_64 ( sized_bytes) ;
97
- //This can't fail because it's statically guaranteed to be 32 bytes long.
98
- let ed25519_dalek_secret = ed25519_dalek:: SecretKey :: from_bytes ( & priv_key) . unwrap ( ) ;
96
+ let ed25519_dalek_secret = ed25519_dalek:: SigningKey :: from_bytes ( & priv_key) ;
99
97
//Calculate the public key to check that the value passed in is correct.
100
- let ed25519_dalek_pub = ed25519_dalek:: PublicKey :: from ( & ed25519_dalek_secret) ;
98
+
99
+ let ed25519_dalek_pub = ed25519_dalek:: VerifyingKey :: from ( & ed25519_dalek_secret) ;
101
100
if ed25519_dalek_pub. to_bytes ( ) == pub_key {
102
101
Ok ( SigningKeypair :: new_unchecked ( * sized_bytes) )
103
102
} else {
@@ -151,9 +150,8 @@ pub struct Ed25519;
151
150
impl Ed25519Signing for Ed25519 {
152
151
fn sign < T : Hashable > ( & self , t : & T , signing_key : & SigningKeypair ) -> Ed25519Signature {
153
152
use ed25519_dalek:: Signer ;
154
- //This unwrap cannot fail. The only thing that the `from_bytes` does for validation is that the
155
- //value is 64 bytes long, which we guarantee statically.
156
- let key_pair = ed25519_dalek:: Keypair :: from_bytes ( & signing_key. bytes [ ..] ) . unwrap ( ) ;
153
+ let ( priv_key, _) = array_split_64 ( & signing_key. bytes ) ;
154
+ let key_pair = ed25519_dalek:: SigningKey :: from_bytes ( & priv_key) ;
157
155
let sig = key_pair. sign ( & t. to_bytes ( ) [ ..] ) ;
158
156
159
157
Ed25519Signature :: new ( sig. to_bytes ( ) )
@@ -166,10 +164,10 @@ impl Ed25519Signing for Ed25519 {
166
164
) -> bool {
167
165
use ed25519_dalek:: Verifier ;
168
166
169
- PublicKey :: from_bytes ( & public_key. bytes [ .. ] )
167
+ ed25519_dalek :: VerifyingKey :: from_bytes ( & public_key. bytes )
170
168
. and_then ( |pk| {
171
- TryFrom :: try_from ( & signature. bytes [ .. ] )
172
- . and_then ( |sig| pk. verify ( & t. to_bytes ( ) [ ..] , & sig) )
169
+ let sig = ed25519_dalek :: Signature :: from_bytes ( & signature. bytes ) ;
170
+ pk. verify ( & t. to_bytes ( ) [ ..] , & sig)
173
171
} )
174
172
. map ( |_| true )
175
173
. unwrap_or ( false )
@@ -197,7 +195,6 @@ pub trait Ed25519Signing {
197
195
pub ( crate ) mod test {
198
196
use super :: * ;
199
197
use crate :: internal:: array_concat_32;
200
- use ed25519_dalek:: SecretKey ;
201
198
202
199
pub fn good_signing_keypair ( ) -> SigningKeypair {
203
200
SigningKeypair :: new_unchecked ( [
@@ -209,18 +206,13 @@ pub(crate) mod test {
209
206
210
207
#[ test]
211
208
fn real_ed25519_matches_verify_good_message ( ) {
212
- let sec_key = SecretKey :: from_bytes ( & [ 1 ; 32 ] ) . unwrap ( ) ;
213
- let dalek_pub_key = ed25519_dalek:: PublicKey :: from ( & sec_key) ;
214
- let priv_key = SigningKeypair {
215
- bytes : array_concat_32 ( & sec_key. to_bytes ( ) , & dalek_pub_key. to_bytes ( ) ) ,
209
+ let sec_key = ed25519_dalek:: SigningKey :: from_bytes ( & [ 1 ; 32 ] ) ;
210
+ let keypair = SigningKeypair {
211
+ bytes : sec_key. to_keypair_bytes ( ) ,
216
212
} ;
217
213
let message = [ 100u8 ; 32 ] . to_vec ( ) ;
218
- let result = Ed25519 . sign ( & message, & priv_key) ;
219
- let verify_result = Ed25519 . verify (
220
- & message,
221
- & result,
222
- & PublicSigningKey :: new ( dalek_pub_key. to_bytes ( ) ) ,
223
- ) ;
214
+ let result = Ed25519 . sign ( & message, & keypair) ;
215
+ let verify_result = Ed25519 . verify ( & message, & result, & keypair. public_key ( ) ) ;
224
216
assert ! ( verify_result) ;
225
217
}
226
218
@@ -238,10 +230,9 @@ pub(crate) mod test {
238
230
239
231
#[ test]
240
232
fn signing_keypair_into_bytes ( ) {
241
- let sec_key = SecretKey :: from_bytes ( & [ 1 ; 32 ] ) . unwrap ( ) ;
242
- let dalek_pub_key = ed25519_dalek:: PublicKey :: from ( & sec_key) ;
233
+ let dalek_pub_key = ed25519_dalek:: VerifyingKey :: from_bytes ( & [ 1u8 ; 32 ] ) . unwrap ( ) ;
243
234
let key_pair = SigningKeypair {
244
- bytes : array_concat_32 ( & sec_key . to_bytes ( ) , & dalek_pub_key. to_bytes ( ) ) ,
235
+ bytes : array_concat_32 ( & [ 1u8 ; 32 ] , & dalek_pub_key. to_bytes ( ) ) ,
245
236
} ;
246
237
let key_pair_bytes = key_pair. bytes ( ) . clone ( ) ;
247
238
let bytes: [ u8 ; 64 ] = key_pair. into ( ) ;
0 commit comments