2121//! Secp256k1 keys.
2222
2323use asn1_der:: typed:: { DerDecodable , Sequence } ;
24- use rand:: RngCore ;
2524use sha2:: { Digest as ShaDigestTrait , Sha256 } ;
26- use secp256k1 :: { Message , Signature } ;
25+ use libsecp256k1 :: { Message , Signature } ;
2726use super :: error:: { DecodingError , SigningError } ;
2827use zeroize:: Zeroize ;
2928use core:: fmt;
@@ -61,7 +60,7 @@ impl fmt::Debug for Keypair {
6160/// Promote a Secp256k1 secret key into a keypair.
6261impl From < SecretKey > for Keypair {
6362 fn from ( secret : SecretKey ) -> Keypair {
64- let public = PublicKey ( secp256k1 :: PublicKey :: from_secret_key ( & secret. 0 ) ) ;
63+ let public = PublicKey ( libsecp256k1 :: PublicKey :: from_secret_key ( & secret. 0 ) ) ;
6564 Keypair { secret, public }
6665 }
6766}
@@ -75,7 +74,7 @@ impl From<Keypair> for SecretKey {
7574
7675/// A Secp256k1 secret key.
7776#[ derive( Clone ) ]
78- pub struct SecretKey ( secp256k1 :: SecretKey ) ;
77+ pub struct SecretKey ( libsecp256k1 :: SecretKey ) ;
7978
8079impl fmt:: Debug for SecretKey {
8180 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
@@ -86,24 +85,15 @@ impl fmt::Debug for SecretKey {
8685impl SecretKey {
8786 /// Generate a new Secp256k1 secret key.
8887 pub fn generate ( ) -> SecretKey {
89- let mut r = rand:: thread_rng ( ) ;
90- let mut b = [ 0 ; secp256k1:: util:: SECRET_KEY_SIZE ] ;
91- // This is how it is done in `secp256k1::SecretKey::random` which
92- // we do not use here because it uses `rand::Rng` from rand-0.4.
93- loop {
94- r. fill_bytes ( & mut b) ;
95- if let Ok ( k) = secp256k1:: SecretKey :: parse ( & b) {
96- return SecretKey ( k)
97- }
98- }
88+ SecretKey ( libsecp256k1:: SecretKey :: random ( & mut rand:: thread_rng ( ) ) )
9989 }
10090
10191 /// Create a secret key from a byte slice, zeroing the slice on success.
10292 /// If the bytes do not constitute a valid Secp256k1 secret key, an
10393 /// error is returned.
10494 pub fn from_bytes ( mut sk : impl AsMut < [ u8 ] > ) -> Result < SecretKey , DecodingError > {
10595 let sk_bytes = sk. as_mut ( ) ;
106- let secret = secp256k1 :: SecretKey :: parse_slice ( & * sk_bytes)
96+ let secret = libsecp256k1 :: SecretKey :: parse_slice ( & * sk_bytes)
10797 . map_err ( |_| DecodingError :: new ( "failed to parse secp256k1 secret key" ) ) ?;
10898 sk_bytes. zeroize ( ) ;
10999 Ok ( SecretKey ( secret) )
@@ -146,13 +136,13 @@ impl SecretKey {
146136 pub fn sign_hash ( & self , msg : & [ u8 ] ) -> Result < Vec < u8 > , SigningError > {
147137 let m = Message :: parse_slice ( msg)
148138 . map_err ( |_| SigningError :: new ( "failed to parse secp256k1 digest" ) ) ?;
149- Ok ( secp256k1 :: sign ( & m, & self . 0 ) . 0 . serialize_der ( ) . as_ref ( ) . into ( ) )
139+ Ok ( libsecp256k1 :: sign ( & m, & self . 0 ) . 0 . serialize_der ( ) . as_ref ( ) . into ( ) )
150140 }
151141}
152142
153143/// A Secp256k1 public key.
154144#[ derive( PartialEq , Eq , Clone ) ]
155- pub struct PublicKey ( secp256k1 :: PublicKey ) ;
145+ pub struct PublicKey ( libsecp256k1 :: PublicKey ) ;
156146
157147impl fmt:: Debug for PublicKey {
158148 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
@@ -173,7 +163,7 @@ impl PublicKey {
173163 /// Verify the Secp256k1 DER-encoded signature on a raw 256-bit message using the public key.
174164 pub fn verify_hash ( & self , msg : & [ u8 ] , sig : & [ u8 ] ) -> bool {
175165 Message :: parse_slice ( msg)
176- . and_then ( |m| Signature :: parse_der ( sig) . map ( |s| secp256k1 :: verify ( & m, & s, & self . 0 ) ) )
166+ . and_then ( |m| Signature :: parse_der ( sig) . map ( |s| libsecp256k1 :: verify ( & m, & s, & self . 0 ) ) )
177167 . unwrap_or ( false )
178168 }
179169
@@ -191,7 +181,7 @@ impl PublicKey {
191181 /// Decode a public key from a byte slice in the the format produced
192182 /// by `encode`.
193183 pub fn decode ( k : & [ u8 ] ) -> Result < PublicKey , DecodingError > {
194- secp256k1 :: PublicKey :: parse_slice ( k, Some ( secp256k1 :: PublicKeyFormat :: Compressed ) )
184+ libsecp256k1 :: PublicKey :: parse_slice ( k, Some ( libsecp256k1 :: PublicKeyFormat :: Compressed ) )
195185 . map_err ( |_| DecodingError :: new ( "failed to parse secp256k1 public key" ) )
196186 . map ( PublicKey )
197187 }
0 commit comments