@@ -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
@@ -1113,25 +1100,17 @@ impl serde::Serialize for Keypair {
1113
1100
}
1114
1101
1115
1102
#[ cfg( feature = "serde" ) ]
1116
- #[ allow( unused_variables) ] // For `data` under some feature combinations (the unconditional panic below).
1117
- #[ cfg( all( feature = "serde" , any( feature = "global-context" , feature = "alloc" ) ) ) ]
1118
1103
impl < ' de > serde:: Deserialize < ' de > for Keypair {
1119
1104
fn deserialize < D : serde:: Deserializer < ' de > > ( d : D ) -> Result < Self , D :: Error > {
1120
1105
if d. is_human_readable ( ) {
1121
1106
d. deserialize_str ( super :: serde_util:: FromStrVisitor :: new (
1122
1107
"a hex string representing 32 byte Keypair" ,
1123
1108
) )
1124
1109
} 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
- } ) ;
1110
+ let visitor = super :: serde_util:: Tuple32Visitor :: new (
1111
+ "raw 32 bytes Keypair" ,
1112
+ Keypair :: from_seckey_byte_array,
1113
+ ) ;
1135
1114
d. deserialize_tuple ( constants:: SECRET_KEY_SIZE , visitor)
1136
1115
}
1137
1116
}
@@ -1713,10 +1692,9 @@ mod test {
1713
1692
}
1714
1693
1715
1694
#[ test]
1716
- #[ cfg( all ( feature = "std" , not( secp256k1_fuzz) ) ) ]
1695
+ #[ cfg( not( secp256k1_fuzz) ) ]
1717
1696
fn erased_keypair_is_valid ( ) {
1718
- let s = Secp256k1 :: new ( ) ;
1719
- let kp = Keypair :: from_seckey_byte_array ( & s, [ 1u8 ; constants:: SECRET_KEY_SIZE ] )
1697
+ let kp = Keypair :: from_seckey_byte_array ( [ 1u8 ; constants:: SECRET_KEY_SIZE ] )
1720
1698
. expect ( "valid secret key" ) ;
1721
1699
let mut kp2 = kp;
1722
1700
kp2. non_secure_erase ( ) ;
@@ -1993,7 +1971,7 @@ mod test {
1993
1971
1994
1972
let tweaked_sk = sk. add_tweak ( & tweak) . unwrap ( ) ;
1995
1973
assert_ne ! ( sk, tweaked_sk) ; // Make sure we did something.
1996
- let tweaked_pk = pk. add_exp_tweak ( & s , & tweak) . unwrap ( ) ;
1974
+ let tweaked_pk = pk. add_exp_tweak ( & tweak) . unwrap ( ) ;
1997
1975
assert_ne ! ( pk, tweaked_pk) ;
1998
1976
1999
1977
assert_eq ! ( PublicKey :: from_secret_key( & s, & tweaked_sk) , tweaked_pk) ;
@@ -2010,7 +1988,7 @@ mod test {
2010
1988
2011
1989
let tweaked_sk = sk. add_tweak ( & tweak) . unwrap ( ) ;
2012
1990
assert_eq ! ( sk, tweaked_sk) ; // Tweak by zero does nothing.
2013
- let tweaked_pk = pk. add_exp_tweak ( & s , & tweak) . unwrap ( ) ;
1991
+ let tweaked_pk = pk. add_exp_tweak ( & tweak) . unwrap ( ) ;
2014
1992
assert_eq ! ( pk, tweaked_pk) ;
2015
1993
}
2016
1994
@@ -2057,9 +2035,9 @@ mod test {
2057
2035
let back_sk = neg. negate ( ) ;
2058
2036
assert_eq ! ( sk, back_sk) ;
2059
2037
2060
- let neg = pk. negate ( & s ) ;
2038
+ let neg = pk. negate ( ) ;
2061
2039
assert_ne ! ( pk, neg) ;
2062
- let back_pk = neg. negate ( & s ) ;
2040
+ let back_pk = neg. negate ( ) ;
2063
2041
assert_eq ! ( pk, back_pk) ;
2064
2042
2065
2043
assert_eq ! ( PublicKey :: from_secret_key( & s, & back_sk) , pk) ;
@@ -2319,7 +2297,7 @@ mod test {
2319
2297
] ;
2320
2298
static SK_STR : & str = "01010101010101010001020304050607ffff0000ffff00006363636363636363" ;
2321
2299
2322
- let sk = Keypair :: from_seckey_byte_array ( SECP256K1 , SK_BYTES ) . unwrap ( ) ;
2300
+ let sk = Keypair :: from_seckey_byte_array ( SK_BYTES ) . unwrap ( ) ;
2323
2301
#[ rustfmt:: skip]
2324
2302
assert_tokens ( & sk. compact ( ) , & [
2325
2303
Token :: Tuple { len : 32 } ,
@@ -2499,7 +2477,7 @@ mod test {
2499
2477
2500
2478
static PK_STR : & str = "18845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166" ;
2501
2479
2502
- let kp = Keypair :: from_seckey_byte_array ( crate :: SECP256K1 , SK_BYTES ) . unwrap ( ) ;
2480
+ let kp = Keypair :: from_seckey_byte_array ( SK_BYTES ) . unwrap ( ) ;
2503
2481
let ( pk, _parity) = XOnlyPublicKey :: from_keypair ( & kp) ;
2504
2482
2505
2483
#[ rustfmt:: skip]
@@ -2529,11 +2507,10 @@ mod test {
2529
2507
}
2530
2508
2531
2509
#[ test]
2532
- #[ cfg( all ( any ( feature = "alloc" , feature = "global-context" ) , feature = " serde") ) ]
2510
+ #[ cfg( feature = "serde" ) ]
2533
2511
fn test_keypair_deserialize_serde ( ) {
2534
- let ctx = crate :: Secp256k1 :: new ( ) ;
2535
2512
let sec_key_str = "4242424242424242424242424242424242424242424242424242424242424242" ;
2536
- let keypair = Keypair :: from_seckey_str ( & ctx , sec_key_str) . unwrap ( ) ;
2513
+ let keypair = Keypair :: from_str ( sec_key_str) . unwrap ( ) ;
2537
2514
2538
2515
serde_test:: assert_tokens ( & keypair. readable ( ) , & [ Token :: String ( sec_key_str) ] ) ;
2539
2516
0 commit comments