@@ -593,11 +593,12 @@ impl PublicKey {
593
593
/// Negates the public key.
594
594
#[ inline]
595
595
#[ must_use = "you forgot to use the negated public key" ]
596
- pub fn negate < C : Verification > ( mut self , secp : & Secp256k1 < C > ) -> PublicKey {
597
- unsafe {
598
- let res = ffi:: secp256k1_ec_pubkey_negate ( secp. ctx . as_ptr ( ) , & mut self . 0 ) ;
599
- debug_assert_eq ! ( res, 1 ) ;
600
- }
596
+ pub fn negate ( mut self ) -> PublicKey {
597
+ let res = crate :: with_raw_global_context (
598
+ |ctx| unsafe { ffi:: secp256k1_ec_pubkey_negate ( ctx. as_ptr ( ) , & mut self . 0 ) } ,
599
+ None ,
600
+ ) ;
601
+ debug_assert_eq ! ( res, 1 ) ;
601
602
self
602
603
}
603
604
@@ -607,19 +608,17 @@ impl PublicKey {
607
608
///
608
609
/// Returns an error if the resulting key would be invalid.
609
610
#[ inline]
610
- pub fn add_exp_tweak < C : Verification > (
611
- mut self ,
612
- secp : & Secp256k1 < C > ,
613
- tweak : & Scalar ,
614
- ) -> Result < PublicKey , Error > {
615
- unsafe {
616
- if ffi:: secp256k1_ec_pubkey_tweak_add ( secp. ctx . as_ptr ( ) , & mut self . 0 , tweak. as_c_ptr ( ) )
617
- == 1
618
- {
619
- Ok ( self )
620
- } else {
621
- Err ( Error :: InvalidTweak )
622
- }
611
+ pub fn add_exp_tweak ( mut self , tweak : & Scalar ) -> Result < PublicKey , Error > {
612
+ if crate :: with_raw_global_context (
613
+ |ctx| unsafe {
614
+ ffi:: secp256k1_ec_pubkey_tweak_add ( ctx. as_ptr ( ) , & mut self . 0 , tweak. as_c_ptr ( ) )
615
+ } ,
616
+ None ,
617
+ ) == 1
618
+ {
619
+ Ok ( self )
620
+ } else {
621
+ Err ( Error :: InvalidTweak )
623
622
}
624
623
}
625
624
@@ -863,12 +862,9 @@ impl Keypair {
863
862
/// or if the encoded number is an invalid scalar.
864
863
#[ deprecated( since = "TBD" , note = "Use `from_seckey_byte_array` instead." ) ]
865
864
#[ inline]
866
- pub fn from_seckey_slice < C : Signing > (
867
- secp : & Secp256k1 < C > ,
868
- data : & [ u8 ] ,
869
- ) -> Result < Keypair , Error > {
865
+ pub fn from_seckey_slice ( data : & [ u8 ] ) -> Result < Keypair , Error > {
870
866
match <[ u8 ; constants:: SECRET_KEY_SIZE ] >:: try_from ( data) {
871
- Ok ( data) => Self :: from_seckey_byte_array ( secp , data) ,
867
+ Ok ( data) => Self :: from_seckey_byte_array ( data) ,
872
868
Err ( _) => Err ( Error :: InvalidSecretKey ) ,
873
869
}
874
870
}
@@ -879,13 +875,16 @@ impl Keypair {
879
875
///
880
876
/// [`Error::InvalidSecretKey`] if the encoded number is an invalid scalar.
881
877
#[ inline]
882
- pub fn from_seckey_byte_array < C : Signing > (
883
- secp : & Secp256k1 < C > ,
878
+ pub fn from_seckey_byte_array (
884
879
data : [ u8 ; constants:: SECRET_KEY_SIZE ] ,
885
880
) -> Result < Keypair , Error > {
886
881
unsafe {
887
882
let mut kp = ffi:: Keypair :: new ( ) ;
888
- if ffi:: secp256k1_keypair_create ( secp. ctx . as_ptr ( ) , & mut kp, data. as_c_ptr ( ) ) == 1 {
883
+ if crate :: with_raw_global_context (
884
+ |ctx| ffi:: secp256k1_keypair_create ( ctx. as_ptr ( ) , & mut kp, data. as_c_ptr ( ) ) ,
885
+ Some ( & data) ,
886
+ ) == 1
887
+ {
889
888
Ok ( Keypair ( kp) )
890
889
} else {
891
890
Err ( Error :: InvalidSecretKey )
@@ -900,13 +899,8 @@ impl Keypair {
900
899
/// [`Error::InvalidSecretKey`] if the string does not consist of exactly 64 hex characters,
901
900
/// or if the encoded number is an invalid scalar.
902
901
#[ inline]
903
- pub fn from_seckey_str < C : Signing > ( secp : & Secp256k1 < C > , s : & str ) -> Result < Keypair , Error > {
904
- let mut res = [ 0u8 ; constants:: SECRET_KEY_SIZE ] ;
905
- match from_hex ( s, & mut res) {
906
- Ok ( constants:: SECRET_KEY_SIZE ) => Keypair :: from_seckey_byte_array ( secp, res) ,
907
- _ => Err ( Error :: InvalidSecretKey ) ,
908
- }
909
- }
902
+ #[ deprecated( note = "use FromStr or parse instead" ) ]
903
+ pub fn from_seckey_str ( s : & str ) -> Result < Self , Error > { s. parse ( ) }
910
904
911
905
/// Creates a [`Keypair`] directly from a secret key string and the global [`SECP256K1`] context.
912
906
///
@@ -915,10 +909,8 @@ impl Keypair {
915
909
/// [`Error::InvalidSecretKey`] if the string does not consist of exactly 64 hex characters,
916
910
/// or if the encoded number is an invalid scalar.
917
911
#[ inline]
918
- #[ cfg( feature = "global-context" ) ]
919
- pub fn from_seckey_str_global ( s : & str ) -> Result < Keypair , Error > {
920
- Keypair :: from_seckey_str ( SECP256K1 , s)
921
- }
912
+ #[ deprecated( note = "use FromStr or parse instead" ) ]
913
+ pub fn from_seckey_str_global ( s : & str ) -> Result < Keypair , Error > { s. parse ( ) }
922
914
923
915
/// Generates a new random key pair.
924
916
/// # Examples
@@ -1076,20 +1068,15 @@ impl<'a> From<&'a Keypair> for PublicKey {
1076
1068
fn from ( pair : & ' a Keypair ) -> Self { PublicKey :: from_keypair ( pair) }
1077
1069
}
1078
1070
1079
- #[ cfg( any( feature = "global-context" , feature = "alloc" ) ) ]
1080
1071
impl str:: FromStr for Keypair {
1081
1072
type Err = Error ;
1082
1073
1083
- #[ allow( unused_variables, unreachable_code) ] // When built with no default features.
1084
1074
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
1085
- #[ cfg( feature = "global-context" ) ]
1086
- let ctx = SECP256K1 ;
1087
-
1088
- #[ cfg( all( not( feature = "global-context" ) , feature = "alloc" ) ) ]
1089
- let ctx = Secp256k1 :: signing_only ( ) ;
1090
-
1091
- #[ allow( clippy:: needless_borrow) ]
1092
- Keypair :: from_seckey_str ( & ctx, s)
1075
+ let mut res = [ 0u8 ; constants:: SECRET_KEY_SIZE ] ;
1076
+ match from_hex ( s, & mut res) {
1077
+ Ok ( constants:: SECRET_KEY_SIZE ) => Keypair :: from_seckey_byte_array ( res) ,
1078
+ _ => Err ( Error :: InvalidSecretKey ) ,
1079
+ }
1093
1080
}
1094
1081
}
1095
1082
@@ -1122,16 +1109,10 @@ impl<'de> serde::Deserialize<'de> for Keypair {
1122
1109
"a hex string representing 32 byte Keypair" ,
1123
1110
) )
1124
1111
} else {
1125
- let visitor = super :: serde_util:: Tuple32Visitor :: new ( "raw 32 bytes Keypair" , |data| {
1126
- #[ cfg( feature = "global-context" ) ]
1127
- let ctx = SECP256K1 ;
1128
-
1129
- #[ cfg( all( not( feature = "global-context" ) , feature = "alloc" ) ) ]
1130
- let ctx = Secp256k1 :: signing_only ( ) ;
1131
-
1132
- #[ allow( clippy:: needless_borrow) ]
1133
- Keypair :: from_seckey_byte_array ( & ctx, data)
1134
- } ) ;
1112
+ let visitor = super :: serde_util:: Tuple32Visitor :: new (
1113
+ "raw 32 bytes Keypair" ,
1114
+ Keypair :: from_seckey_byte_array,
1115
+ ) ;
1135
1116
d. deserialize_tuple ( constants:: SECRET_KEY_SIZE , visitor)
1136
1117
}
1137
1118
}
@@ -1713,10 +1694,9 @@ mod test {
1713
1694
}
1714
1695
1715
1696
#[ test]
1716
- #[ cfg( all ( feature = "std" , not( secp256k1_fuzz) ) ) ]
1697
+ #[ cfg( not( secp256k1_fuzz) ) ]
1717
1698
fn erased_keypair_is_valid ( ) {
1718
- let s = Secp256k1 :: new ( ) ;
1719
- let kp = Keypair :: from_seckey_byte_array ( & s, [ 1u8 ; constants:: SECRET_KEY_SIZE ] )
1699
+ let kp = Keypair :: from_seckey_byte_array ( [ 1u8 ; constants:: SECRET_KEY_SIZE ] )
1720
1700
. expect ( "valid secret key" ) ;
1721
1701
let mut kp2 = kp;
1722
1702
kp2. non_secure_erase ( ) ;
@@ -1993,7 +1973,7 @@ mod test {
1993
1973
1994
1974
let tweaked_sk = sk. add_tweak ( & tweak) . unwrap ( ) ;
1995
1975
assert_ne ! ( sk, tweaked_sk) ; // Make sure we did something.
1996
- let tweaked_pk = pk. add_exp_tweak ( & s , & tweak) . unwrap ( ) ;
1976
+ let tweaked_pk = pk. add_exp_tweak ( & tweak) . unwrap ( ) ;
1997
1977
assert_ne ! ( pk, tweaked_pk) ;
1998
1978
1999
1979
assert_eq ! ( PublicKey :: from_secret_key( & s, & tweaked_sk) , tweaked_pk) ;
@@ -2010,7 +1990,7 @@ mod test {
2010
1990
2011
1991
let tweaked_sk = sk. add_tweak ( & tweak) . unwrap ( ) ;
2012
1992
assert_eq ! ( sk, tweaked_sk) ; // Tweak by zero does nothing.
2013
- let tweaked_pk = pk. add_exp_tweak ( & s , & tweak) . unwrap ( ) ;
1993
+ let tweaked_pk = pk. add_exp_tweak ( & tweak) . unwrap ( ) ;
2014
1994
assert_eq ! ( pk, tweaked_pk) ;
2015
1995
}
2016
1996
@@ -2057,9 +2037,9 @@ mod test {
2057
2037
let back_sk = neg. negate ( ) ;
2058
2038
assert_eq ! ( sk, back_sk) ;
2059
2039
2060
- let neg = pk. negate ( & s ) ;
2040
+ let neg = pk. negate ( ) ;
2061
2041
assert_ne ! ( pk, neg) ;
2062
- let back_pk = neg. negate ( & s ) ;
2042
+ let back_pk = neg. negate ( ) ;
2063
2043
assert_eq ! ( pk, back_pk) ;
2064
2044
2065
2045
assert_eq ! ( PublicKey :: from_secret_key( & s, & back_sk) , pk) ;
@@ -2319,7 +2299,7 @@ mod test {
2319
2299
] ;
2320
2300
static SK_STR : & str = "01010101010101010001020304050607ffff0000ffff00006363636363636363" ;
2321
2301
2322
- let sk = Keypair :: from_seckey_byte_array ( SECP256K1 , SK_BYTES ) . unwrap ( ) ;
2302
+ let sk = Keypair :: from_seckey_byte_array ( SK_BYTES ) . unwrap ( ) ;
2323
2303
#[ rustfmt:: skip]
2324
2304
assert_tokens ( & sk. compact ( ) , & [
2325
2305
Token :: Tuple { len : 32 } ,
@@ -2499,7 +2479,7 @@ mod test {
2499
2479
2500
2480
static PK_STR : & str = "18845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166" ;
2501
2481
2502
- let kp = Keypair :: from_seckey_byte_array ( crate :: SECP256K1 , SK_BYTES ) . unwrap ( ) ;
2482
+ let kp = Keypair :: from_seckey_byte_array ( SK_BYTES ) . unwrap ( ) ;
2503
2483
let ( pk, _parity) = XOnlyPublicKey :: from_keypair ( & kp) ;
2504
2484
2505
2485
#[ rustfmt:: skip]
@@ -2529,11 +2509,10 @@ mod test {
2529
2509
}
2530
2510
2531
2511
#[ test]
2532
- #[ cfg( all ( any ( feature = "alloc" , feature = "global-context" ) , feature = " serde") ) ]
2512
+ #[ cfg( feature = "serde" ) ]
2533
2513
fn test_keypair_deserialize_serde ( ) {
2534
- let ctx = crate :: Secp256k1 :: new ( ) ;
2535
2514
let sec_key_str = "4242424242424242424242424242424242424242424242424242424242424242" ;
2536
- let keypair = Keypair :: from_seckey_str ( & ctx , sec_key_str) . unwrap ( ) ;
2515
+ let keypair = Keypair :: from_str ( sec_key_str) . unwrap ( ) ;
2537
2516
2538
2517
serde_test:: assert_tokens ( & keypair. readable ( ) , & [ Token :: String ( sec_key_str) ] ) ;
2539
2518
0 commit comments