@@ -40,7 +40,7 @@ use serde::{de, Serializer, Serialize, Deserializer, Deserialize};
40
40
use crate :: crypto:: { Public as TraitPublic , CryptoTypePublicPair , UncheckedFrom , CryptoType , Derive , CryptoTypeId } ;
41
41
use sp_runtime_interface:: pass_by:: PassByInner ;
42
42
#[ cfg( feature = "full_crypto" ) ]
43
- use secp256k1 :: { PublicKey , SecretKey } ;
43
+ use libsecp256k1 :: { PublicKey , SecretKey } ;
44
44
45
45
/// An identifier used to match public keys against ecdsa keys
46
46
pub const CRYPTO_ID : CryptoTypeId = CryptoTypeId ( * b"ecds" ) ;
@@ -103,7 +103,7 @@ impl Public {
103
103
/// This will convert the full public key into the compressed format.
104
104
#[ cfg( feature = "std" ) ]
105
105
pub fn from_full ( full : & [ u8 ] ) -> Result < Self , ( ) > {
106
- secp256k1 :: PublicKey :: parse_slice ( full, None )
106
+ libsecp256k1 :: PublicKey :: parse_slice ( full, None )
107
107
. map ( |k| k. serialize_compressed ( ) )
108
108
. map ( Self )
109
109
. map_err ( |_| ( ) )
@@ -348,29 +348,29 @@ impl Signature {
348
348
/// Recover the public key from this signature and a message.
349
349
#[ cfg( feature = "full_crypto" ) ]
350
350
pub fn recover < M : AsRef < [ u8 ] > > ( & self , message : M ) -> Option < Public > {
351
- let message = secp256k1 :: Message :: parse ( & blake2_256 ( message. as_ref ( ) ) ) ;
351
+ let message = libsecp256k1 :: Message :: parse ( & blake2_256 ( message. as_ref ( ) ) ) ;
352
352
let sig: ( _ , _ ) = self . try_into ( ) . ok ( ) ?;
353
- secp256k1 :: recover ( & message, & sig. 0 , & sig. 1 )
353
+ libsecp256k1 :: recover ( & message, & sig. 0 , & sig. 1 )
354
354
. ok ( )
355
355
. map ( |recovered| Public ( recovered. serialize_compressed ( ) ) )
356
356
}
357
357
358
358
/// Recover the public key from this signature and a pre-hashed message.
359
359
#[ cfg( feature = "full_crypto" ) ]
360
360
pub fn recover_prehashed ( & self , message : & [ u8 ; 32 ] ) -> Option < Public > {
361
- let message = secp256k1 :: Message :: parse ( message) ;
362
-
361
+ let message = libsecp256k1 :: Message :: parse ( message) ;
362
+
363
363
let sig: ( _ , _ ) = self . try_into ( ) . ok ( ) ?;
364
364
365
- secp256k1 :: recover ( & message, & sig. 0 , & sig. 1 )
365
+ libsecp256k1 :: recover ( & message, & sig. 0 , & sig. 1 )
366
366
. ok ( )
367
367
. map ( |key| Public ( key. serialize_compressed ( ) ) )
368
368
}
369
369
}
370
370
371
371
#[ cfg( feature = "full_crypto" ) ]
372
- impl From < ( secp256k1 :: Signature , secp256k1 :: RecoveryId ) > for Signature {
373
- fn from ( x : ( secp256k1 :: Signature , secp256k1 :: RecoveryId ) ) -> Signature {
372
+ impl From < ( libsecp256k1 :: Signature , libsecp256k1 :: RecoveryId ) > for Signature {
373
+ fn from ( x : ( libsecp256k1 :: Signature , libsecp256k1 :: RecoveryId ) ) -> Signature {
374
374
let mut r = Self :: default ( ) ;
375
375
r. 0 [ 0 ..64 ] . copy_from_slice ( & x. 0 . serialize ( ) [ ..] ) ;
376
376
r. 0 [ 64 ] = x. 1 . serialize ( ) ;
@@ -379,12 +379,12 @@ impl From<(secp256k1::Signature, secp256k1::RecoveryId)> for Signature {
379
379
}
380
380
381
381
#[ cfg( feature = "full_crypto" ) ]
382
- impl < ' a > TryFrom < & ' a Signature > for ( secp256k1 :: Signature , secp256k1 :: RecoveryId ) {
382
+ impl < ' a > TryFrom < & ' a Signature > for ( libsecp256k1 :: Signature , libsecp256k1 :: RecoveryId ) {
383
383
type Error = ( ) ;
384
- fn try_from ( x : & ' a Signature ) -> Result < ( secp256k1 :: Signature , secp256k1 :: RecoveryId ) , Self :: Error > {
384
+ fn try_from ( x : & ' a Signature ) -> Result < ( libsecp256k1 :: Signature , libsecp256k1 :: RecoveryId ) , Self :: Error > {
385
385
Ok ( (
386
- secp256k1 :: Signature :: parse_slice ( & x. 0 [ 0 ..64 ] ) . expect ( "hardcoded to 64 bytes; qed" ) ,
387
- secp256k1 :: RecoveryId :: parse ( x. 0 [ 64 ] ) . map_err ( |_| ( ) ) ?,
386
+ libsecp256k1 :: Signature :: parse_standard_slice ( & x. 0 [ 0 ..64 ] ) . expect ( "hardcoded to 64 bytes; qed" ) ,
387
+ libsecp256k1 :: RecoveryId :: parse ( x. 0 [ 64 ] ) . map_err ( |_| ( ) ) ?,
388
388
) )
389
389
}
390
390
}
@@ -490,15 +490,15 @@ impl TraitPair for Pair {
490
490
491
491
/// Sign a message.
492
492
fn sign ( & self , message : & [ u8 ] ) -> Signature {
493
- let message = secp256k1 :: Message :: parse ( & blake2_256 ( message) ) ;
494
- secp256k1 :: sign ( & message, & self . secret ) . into ( )
493
+ let message = libsecp256k1 :: Message :: parse ( & blake2_256 ( message) ) ;
494
+ libsecp256k1 :: sign ( & message, & self . secret ) . into ( )
495
495
}
496
496
497
497
/// Verify a signature on a message. Returns true if the signature is good.
498
498
fn verify < M : AsRef < [ u8 ] > > ( sig : & Self :: Signature , message : M , pubkey : & Self :: Public ) -> bool {
499
- let message = secp256k1 :: Message :: parse ( & blake2_256 ( message. as_ref ( ) ) ) ;
499
+ let message = libsecp256k1 :: Message :: parse ( & blake2_256 ( message. as_ref ( ) ) ) ;
500
500
let sig: ( _ , _ ) = match sig. try_into ( ) { Ok ( x) => x, _ => return false } ;
501
- match secp256k1 :: recover ( & message, & sig. 0 , & sig. 1 ) {
501
+ match libsecp256k1 :: recover ( & message, & sig. 0 , & sig. 1 ) {
502
502
Ok ( actual) => pubkey. 0 [ ..] == actual. serialize_compressed ( ) [ ..] ,
503
503
_ => false ,
504
504
}
@@ -509,11 +509,11 @@ impl TraitPair for Pair {
509
509
/// This doesn't use the type system to ensure that `sig` and `pubkey` are the correct
510
510
/// size. Use it only if you're coming from byte buffers and need the speed.
511
511
fn verify_weak < P : AsRef < [ u8 ] > , M : AsRef < [ u8 ] > > ( sig : & [ u8 ] , message : M , pubkey : P ) -> bool {
512
- let message = secp256k1 :: Message :: parse ( & blake2_256 ( message. as_ref ( ) ) ) ;
512
+ let message = libsecp256k1 :: Message :: parse ( & blake2_256 ( message. as_ref ( ) ) ) ;
513
513
if sig. len ( ) != 65 { return false }
514
- let ri = match secp256k1 :: RecoveryId :: parse ( sig[ 64 ] ) { Ok ( x) => x, _ => return false } ;
515
- let sig = match secp256k1 :: Signature :: parse_slice ( & sig[ 0 ..64 ] ) { Ok ( x) => x, _ => return false } ;
516
- match secp256k1 :: recover ( & message, & sig, & ri) {
514
+ let ri = match libsecp256k1 :: RecoveryId :: parse ( sig[ 64 ] ) { Ok ( x) => x, _ => return false } ;
515
+ let sig = match libsecp256k1 :: Signature :: parse_standard_slice ( & sig[ 0 ..64 ] ) { Ok ( x) => x, _ => return false } ;
516
+ match libsecp256k1 :: recover ( & message, & sig, & ri) {
517
517
Ok ( actual) => pubkey. as_ref ( ) == & actual. serialize ( ) [ 1 ..] ,
518
518
_ => false ,
519
519
}
@@ -546,25 +546,25 @@ impl Pair {
546
546
547
547
/// Sign a pre-hashed message
548
548
pub fn sign_prehashed ( & self , message : & [ u8 ; 32 ] ) -> Signature {
549
- let message = secp256k1 :: Message :: parse ( message) ;
550
- secp256k1 :: sign ( & message, & self . secret ) . into ( )
549
+ let message = libsecp256k1 :: Message :: parse ( message) ;
550
+ libsecp256k1 :: sign ( & message, & self . secret ) . into ( )
551
551
}
552
552
553
553
/// Verify a signature on a pre-hashed message. Return `true` if the signature is valid
554
554
/// and thus matches the given `public` key.
555
555
pub fn verify_prehashed ( sig : & Signature , message : & [ u8 ; 32 ] , public : & Public ) -> bool {
556
- let message = secp256k1 :: Message :: parse ( message) ;
557
-
556
+ let message = libsecp256k1 :: Message :: parse ( message) ;
557
+
558
558
let sig: ( _ , _ ) = match sig. try_into ( ) {
559
559
Ok ( x) => x,
560
560
_ => return false ,
561
561
} ;
562
-
563
- match secp256k1 :: recover ( & message, & sig. 0 , & sig. 1 ) {
562
+
563
+ match libsecp256k1 :: recover ( & message, & sig. 0 , & sig. 1 ) {
564
564
Ok ( actual) => public. 0 [ ..] == actual. serialize_compressed ( ) [ ..] ,
565
565
_ => false ,
566
566
}
567
- }
567
+ }
568
568
}
569
569
570
570
impl CryptoType for Public {
@@ -803,7 +803,7 @@ mod test {
803
803
// `msg` shouldn't be mangled
804
804
let msg = [ 0u8 ; 32 ] ;
805
805
let sig1 = pair. sign_prehashed ( & msg) ;
806
- let sig2: Signature = secp256k1 :: sign ( & secp256k1 :: Message :: parse ( & msg) , & pair. secret ) . into ( ) ;
806
+ let sig2: Signature = libsecp256k1 :: sign ( & libsecp256k1 :: Message :: parse ( & msg) , & pair. secret ) . into ( ) ;
807
807
808
808
assert_eq ! ( sig1, sig2) ;
809
809
@@ -815,15 +815,15 @@ mod test {
815
815
// using pre-hashed `msg` works
816
816
let msg = keccak_256 ( b"this should be hashed" ) ;
817
817
let sig1 = pair. sign_prehashed ( & msg) ;
818
- let sig2: Signature = secp256k1 :: sign ( & secp256k1 :: Message :: parse ( & msg) , & pair. secret ) . into ( ) ;
818
+ let sig2: Signature = libsecp256k1 :: sign ( & libsecp256k1 :: Message :: parse ( & msg) , & pair. secret ) . into ( ) ;
819
819
820
- assert_eq ! ( sig1, sig2) ;
820
+ assert_eq ! ( sig1, sig2) ;
821
821
}
822
822
823
823
#[ test]
824
824
fn verify_prehashed_works ( ) {
825
825
let ( pair, _, _) = Pair :: generate_with_phrase ( Some ( "password" ) ) ;
826
-
826
+
827
827
// `msg` and `sig` match
828
828
let msg = keccak_256 ( b"this should be hashed" ) ;
829
829
let sig = pair. sign_prehashed ( & msg) ;
0 commit comments