@@ -20,8 +20,7 @@ use crate::ThirtyTwoByteHash;
20
20
#[ cfg( feature = "global-context" ) ]
21
21
use crate :: SECP256K1 ;
22
22
use crate :: {
23
- constants, ecdsa, from_hex, schnorr, AllPreallocated , Message , Scalar , Secp256k1 , Signing ,
24
- Verification ,
23
+ constants, ecdsa, from_hex, schnorr, Message , Scalar , Secp256k1 , Signing , Verification ,
25
24
} ;
26
25
27
26
/// Secret key - a 256-bit key used to create ECDSA and Taproot signatures.
@@ -610,11 +609,12 @@ impl PublicKey {
610
609
/// Negates the public key.
611
610
#[ inline]
612
611
#[ must_use = "you forgot to use the negated public key" ]
613
- pub fn negate < C : Verification > ( mut self , secp : & Secp256k1 < C > ) -> PublicKey {
614
- unsafe {
615
- let res = ffi:: secp256k1_ec_pubkey_negate ( secp. ctx . as_ptr ( ) , & mut self . 0 ) ;
616
- debug_assert_eq ! ( res, 1 ) ;
617
- }
612
+ pub fn negate ( mut self ) -> PublicKey {
613
+ let res = crate :: with_raw_global_context (
614
+ |ctx| unsafe { ffi:: secp256k1_ec_pubkey_negate ( ctx. as_ptr ( ) , & mut self . 0 ) } ,
615
+ None ,
616
+ ) ;
617
+ debug_assert_eq ! ( res, 1 ) ;
618
618
self
619
619
}
620
620
@@ -624,19 +624,17 @@ impl PublicKey {
624
624
///
625
625
/// Returns an error if the resulting key would be invalid.
626
626
#[ inline]
627
- pub fn add_exp_tweak < C : Verification > (
628
- mut self ,
629
- secp : & Secp256k1 < C > ,
630
- tweak : & Scalar ,
631
- ) -> Result < PublicKey , Error > {
632
- unsafe {
633
- if ffi:: secp256k1_ec_pubkey_tweak_add ( secp. ctx . as_ptr ( ) , & mut self . 0 , tweak. as_c_ptr ( ) )
634
- == 1
635
- {
636
- Ok ( self )
637
- } else {
638
- Err ( Error :: InvalidTweak )
639
- }
627
+ pub fn add_exp_tweak ( mut self , tweak : & Scalar ) -> Result < PublicKey , Error > {
628
+ if crate :: with_raw_global_context (
629
+ |ctx| unsafe {
630
+ ffi:: secp256k1_ec_pubkey_tweak_add ( ctx. as_ptr ( ) , & mut self . 0 , tweak. as_c_ptr ( ) )
631
+ } ,
632
+ None ,
633
+ ) == 1
634
+ {
635
+ Ok ( self )
636
+ } else {
637
+ Err ( Error :: InvalidTweak )
640
638
}
641
639
}
642
640
@@ -880,12 +878,9 @@ impl Keypair {
880
878
/// or if the encoded number is an invalid scalar.
881
879
#[ deprecated( since = "TBD" , note = "Use `from_seckey_byte_array` instead." ) ]
882
880
#[ inline]
883
- pub fn from_seckey_slice < C : Signing > (
884
- secp : & Secp256k1 < C > ,
885
- data : & [ u8 ] ,
886
- ) -> Result < Keypair , Error > {
881
+ pub fn from_seckey_slice ( data : & [ u8 ] ) -> Result < Keypair , Error > {
887
882
match <[ u8 ; constants:: SECRET_KEY_SIZE ] >:: try_from ( data) {
888
- Ok ( data) => Self :: from_seckey_byte_array ( secp , data) ,
883
+ Ok ( data) => Self :: from_seckey_byte_array ( data) ,
889
884
Err ( _) => Err ( Error :: InvalidSecretKey ) ,
890
885
}
891
886
}
@@ -896,13 +891,16 @@ impl Keypair {
896
891
///
897
892
/// [`Error::InvalidSecretKey`] if the encoded number is an invalid scalar.
898
893
#[ inline]
899
- pub fn from_seckey_byte_array < C : Signing > (
900
- secp : & Secp256k1 < C > ,
894
+ pub fn from_seckey_byte_array (
901
895
data : [ u8 ; constants:: SECRET_KEY_SIZE ] ,
902
896
) -> Result < Keypair , Error > {
903
897
unsafe {
904
898
let mut kp = ffi:: Keypair :: new ( ) ;
905
- if ffi:: secp256k1_keypair_create ( secp. ctx . as_ptr ( ) , & mut kp, data. as_c_ptr ( ) ) == 1 {
899
+ if crate :: with_raw_global_context (
900
+ |ctx| ffi:: secp256k1_keypair_create ( ctx. as_ptr ( ) , & mut kp, data. as_c_ptr ( ) ) ,
901
+ Some ( & data) ,
902
+ ) == 1
903
+ {
906
904
Ok ( Keypair ( kp) )
907
905
} else {
908
906
Err ( Error :: InvalidSecretKey )
@@ -917,13 +915,8 @@ impl Keypair {
917
915
/// [`Error::InvalidSecretKey`] if the string does not consist of exactly 64 hex characters,
918
916
/// or if the encoded number is an invalid scalar.
919
917
#[ inline]
920
- pub fn from_seckey_str < C : Signing > ( secp : & Secp256k1 < C > , s : & str ) -> Result < Keypair , Error > {
921
- let mut res = [ 0u8 ; constants:: SECRET_KEY_SIZE ] ;
922
- match from_hex ( s, & mut res) {
923
- Ok ( constants:: SECRET_KEY_SIZE ) => Keypair :: from_seckey_byte_array ( secp, res) ,
924
- _ => Err ( Error :: InvalidSecretKey ) ,
925
- }
926
- }
918
+ #[ deprecated( note = "use FromStr or parse instead" ) ]
919
+ pub fn from_seckey_str ( s : & str ) -> Result < Self , Error > { s. parse ( ) }
927
920
928
921
/// Creates a [`Keypair`] directly from a secret key string and the global [`SECP256K1`] context.
929
922
///
@@ -932,10 +925,8 @@ impl Keypair {
932
925
/// [`Error::InvalidSecretKey`] if the string does not consist of exactly 64 hex characters,
933
926
/// or if the encoded number is an invalid scalar.
934
927
#[ inline]
935
- #[ cfg( feature = "global-context" ) ]
936
- pub fn from_seckey_str_global ( s : & str ) -> Result < Keypair , Error > {
937
- Keypair :: from_seckey_str ( SECP256K1 , s)
938
- }
928
+ #[ deprecated( note = "use FromStr or parse instead" ) ]
929
+ pub fn from_seckey_str_global ( s : & str ) -> Result < Keypair , Error > { s. parse ( ) }
939
930
940
931
/// Generates a new random key pair.
941
932
/// # Examples
@@ -1107,10 +1098,11 @@ impl str::FromStr for Keypair {
1107
1098
type Err = Error ;
1108
1099
1109
1100
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
1110
- crate :: with_global_context (
1111
- |secp : & Secp256k1 < AllPreallocated > | Self :: from_seckey_str ( & secp, s) ,
1112
- None ,
1113
- )
1101
+ let mut res = [ 0u8 ; constants:: SECRET_KEY_SIZE ] ;
1102
+ match from_hex ( s, & mut res) {
1103
+ Ok ( constants:: SECRET_KEY_SIZE ) => Keypair :: from_seckey_byte_array ( res) ,
1104
+ _ => Err ( Error :: InvalidSecretKey ) ,
1105
+ }
1114
1106
}
1115
1107
}
1116
1108
@@ -1141,12 +1133,10 @@ impl<'de> serde::Deserialize<'de> for Keypair {
1141
1133
"a hex string representing 32 byte Keypair" ,
1142
1134
) )
1143
1135
} else {
1144
- let visitor = super :: serde_util:: Tuple32Visitor :: new ( "raw 32 bytes Keypair" , |data| {
1145
- crate :: with_global_context (
1146
- |secp : & Secp256k1 < AllPreallocated > | Self :: from_seckey_byte_array ( & secp, data) ,
1147
- None ,
1148
- )
1149
- } ) ;
1136
+ let visitor = super :: serde_util:: Tuple32Visitor :: new (
1137
+ "raw 32 bytes Keypair" ,
1138
+ Keypair :: from_seckey_byte_array,
1139
+ ) ;
1150
1140
d. deserialize_tuple ( constants:: SECRET_KEY_SIZE , visitor)
1151
1141
}
1152
1142
}
@@ -1725,10 +1715,9 @@ mod test {
1725
1715
}
1726
1716
1727
1717
#[ test]
1728
- #[ cfg( all ( feature = "std" , not( secp256k1_fuzz) ) ) ]
1718
+ #[ cfg( not( secp256k1_fuzz) ) ]
1729
1719
fn erased_keypair_is_valid ( ) {
1730
- let s = Secp256k1 :: new ( ) ;
1731
- let kp = Keypair :: from_seckey_byte_array ( & s, [ 1u8 ; constants:: SECRET_KEY_SIZE ] )
1720
+ let kp = Keypair :: from_seckey_byte_array ( [ 1u8 ; constants:: SECRET_KEY_SIZE ] )
1732
1721
. expect ( "valid secret key" ) ;
1733
1722
let mut kp2 = kp;
1734
1723
kp2. non_secure_erase ( ) ;
@@ -2005,24 +1994,21 @@ mod test {
2005
1994
2006
1995
let tweaked_sk = sk. add_tweak ( & tweak) . unwrap ( ) ;
2007
1996
assert_ne ! ( sk, tweaked_sk) ; // Make sure we did something.
2008
- let tweaked_pk = pk. add_exp_tweak ( & s , & tweak) . unwrap ( ) ;
1997
+ let tweaked_pk = pk. add_exp_tweak ( & tweak) . unwrap ( ) ;
2009
1998
assert_ne ! ( pk, tweaked_pk) ;
2010
1999
2011
2000
assert_eq ! ( PublicKey :: from_secret_key( & s, & tweaked_sk) , tweaked_pk) ;
2012
2001
}
2013
2002
2014
2003
#[ test]
2015
- #[ cfg( feature = "std" ) ]
2016
2004
fn tweak_add_zero ( ) {
2017
- let s = Secp256k1 :: new ( ) ;
2018
-
2019
2005
let ( sk, pk) = crate :: test_random_keypair ( ) ;
2020
2006
2021
2007
let tweak = Scalar :: ZERO ;
2022
2008
2023
2009
let tweaked_sk = sk. add_tweak ( & tweak) . unwrap ( ) ;
2024
2010
assert_eq ! ( sk, tweaked_sk) ; // Tweak by zero does nothing.
2025
- let tweaked_pk = pk. add_exp_tweak ( & s , & tweak) . unwrap ( ) ;
2011
+ let tweaked_pk = pk. add_exp_tweak ( & tweak) . unwrap ( ) ;
2026
2012
assert_eq ! ( pk, tweaked_pk) ;
2027
2013
}
2028
2014
@@ -2067,9 +2053,9 @@ mod test {
2067
2053
let back_sk = neg. negate ( ) ;
2068
2054
assert_eq ! ( sk, back_sk) ;
2069
2055
2070
- let neg = pk. negate ( & s ) ;
2056
+ let neg = pk. negate ( ) ;
2071
2057
assert_ne ! ( pk, neg) ;
2072
- let back_pk = neg. negate ( & s ) ;
2058
+ let back_pk = neg. negate ( ) ;
2073
2059
assert_eq ! ( pk, back_pk) ;
2074
2060
2075
2061
assert_eq ! ( PublicKey :: from_secret_key( & s, & back_sk) , pk) ;
@@ -2327,7 +2313,7 @@ mod test {
2327
2313
] ;
2328
2314
static SK_STR : & str = "01010101010101010001020304050607ffff0000ffff00006363636363636363" ;
2329
2315
2330
- let sk = Keypair :: from_seckey_byte_array ( SECP256K1 , SK_BYTES ) . unwrap ( ) ;
2316
+ let sk = Keypair :: from_seckey_byte_array ( SK_BYTES ) . unwrap ( ) ;
2331
2317
#[ rustfmt:: skip]
2332
2318
assert_tokens ( & sk. compact ( ) , & [
2333
2319
Token :: Tuple { len : 32 } ,
@@ -2507,7 +2493,7 @@ mod test {
2507
2493
2508
2494
static PK_STR : & str = "18845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166" ;
2509
2495
2510
- let kp = Keypair :: from_seckey_byte_array ( crate :: SECP256K1 , SK_BYTES ) . unwrap ( ) ;
2496
+ let kp = Keypair :: from_seckey_byte_array ( SK_BYTES ) . unwrap ( ) ;
2511
2497
let ( pk, _parity) = XOnlyPublicKey :: from_keypair ( & kp) ;
2512
2498
2513
2499
#[ rustfmt:: skip]
@@ -2535,11 +2521,10 @@ mod test {
2535
2521
}
2536
2522
2537
2523
#[ test]
2538
- #[ cfg( all ( any ( feature = "alloc" , feature = "global-context" ) , feature = " serde") ) ]
2524
+ #[ cfg( feature = "serde" ) ]
2539
2525
fn test_keypair_deserialize_serde ( ) {
2540
- let ctx = crate :: Secp256k1 :: new ( ) ;
2541
2526
let sec_key_str = "4242424242424242424242424242424242424242424242424242424242424242" ;
2542
- let keypair = Keypair :: from_seckey_str ( & ctx , sec_key_str) . unwrap ( ) ;
2527
+ let keypair = Keypair :: from_str ( sec_key_str) . unwrap ( ) ;
2543
2528
2544
2529
serde_test:: assert_tokens ( & keypair. readable ( ) , & [ Token :: String ( sec_key_str) ] ) ;
2545
2530
0 commit comments