@@ -5,7 +5,6 @@ use crate::internal::ByteVector;
55use crate :: internal:: { array_split_64, take_lock} ;
66use clear_on_drop:: clear:: Clear ;
77use ed25519_dalek;
8- use ed25519_dalek:: PublicKey ;
98use quick_error:: quick_error;
109use rand;
1110use std;
@@ -64,10 +63,10 @@ impl From<SigningKeypair> for [u8; 64] {
6463impl SigningKeypair {
6564 const ENCODED_SIZE_BYTES : usize = 64 ;
6665 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) ) ;
6867
6968 //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 ( ) )
7170 }
7271 ///
7372 ///Create a SigningKeypair from a byte array slice. If the array is not the right length or if the public
@@ -93,16 +92,11 @@ impl SigningKeypair {
9392 ///match the private key.
9493 ///
9594 pub fn from_bytes ( sized_bytes : & [ u8 ; 64 ] ) -> Result < SigningKeypair , Ed25519Error > {
96- 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 ( ) ;
95+ let ( _, pub_key) = array_split_64 ( sized_bytes) ;
9996 //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) ;
101- if ed25519_dalek_pub. to_bytes ( ) == pub_key {
102- Ok ( SigningKeypair :: new_unchecked ( * sized_bytes) )
103- } else {
104- Err ( Ed25519Error :: PublicKeyInvalid ( pub_key) )
105- }
97+ let ed25519_dalek_pub = ed25519_dalek:: VerifyingKey :: from_bytes ( & pub_key)
98+ . map_err ( |_| Ed25519Error :: PublicKeyInvalid ( pub_key) ) ?;
99+ Ok ( SigningKeypair :: new_unchecked ( * sized_bytes) )
106100 }
107101
108102 pub ( crate ) fn new_unchecked ( bytes : [ u8 ; 64 ] ) -> SigningKeypair {
@@ -153,7 +147,7 @@ impl Ed25519Signing for Ed25519 {
153147 use ed25519_dalek:: Signer ;
154148 //This unwrap cannot fail. The only thing that the `from_bytes` does for validation is that the
155149 //value is 64 bytes long, which we guarantee statically.
156- let key_pair = ed25519_dalek:: Keypair :: from_bytes ( & signing_key. bytes [ .. ] ) . unwrap ( ) ;
150+ let key_pair = ed25519_dalek:: SigningKey :: from_bytes ( signing_key. public_key ( ) . bytes ( ) ) ;
157151 let sig = key_pair. sign ( & t. to_bytes ( ) [ ..] ) ;
158152
159153 Ed25519Signature :: new ( sig. to_bytes ( ) )
@@ -166,10 +160,10 @@ impl Ed25519Signing for Ed25519 {
166160 ) -> bool {
167161 use ed25519_dalek:: Verifier ;
168162
169- PublicKey :: from_bytes ( & public_key. bytes [ .. ] )
163+ ed25519_dalek :: VerifyingKey :: from_bytes ( & public_key. bytes )
170164 . and_then ( |pk| {
171- TryFrom :: try_from ( & signature. bytes [ .. ] )
172- . and_then ( |sig| pk. verify ( & t. to_bytes ( ) [ ..] , & sig) )
165+ let sig = ed25519_dalek :: Signature :: from_bytes ( & signature. bytes ) ;
166+ pk. verify ( & t. to_bytes ( ) [ ..] , & sig)
173167 } )
174168 . map ( |_| true )
175169 . unwrap_or ( false )
@@ -197,7 +191,6 @@ pub trait Ed25519Signing {
197191pub ( crate ) mod test {
198192 use super :: * ;
199193 use crate :: internal:: array_concat_32;
200- use ed25519_dalek:: SecretKey ;
201194
202195 pub fn good_signing_keypair ( ) -> SigningKeypair {
203196 SigningKeypair :: new_unchecked ( [
@@ -209,10 +202,9 @@ pub(crate) mod test {
209202
210203 #[ test]
211204 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) ;
205+ let dalek_pub_key = ed25519_dalek:: VerifyingKey :: from_bytes ( & [ 1u8 ; 32 ] ) . unwrap ( ) ;
214206 let priv_key = SigningKeypair {
215- bytes : array_concat_32 ( & sec_key . to_bytes ( ) , & dalek_pub_key. to_bytes ( ) ) ,
207+ bytes : array_concat_32 ( & [ 1u8 ; 32 ] , & dalek_pub_key. to_bytes ( ) ) ,
216208 } ;
217209 let message = [ 100u8 ; 32 ] . to_vec ( ) ;
218210 let result = Ed25519 . sign ( & message, & priv_key) ;
@@ -238,10 +230,9 @@ pub(crate) mod test {
238230
239231 #[ test]
240232 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 ( ) ;
243234 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 ( ) ) ,
245236 } ;
246237 let key_pair_bytes = key_pair. bytes ( ) . clone ( ) ;
247238 let bytes: [ u8 ; 64 ] = key_pair. into ( ) ;
0 commit comments