@@ -19,8 +19,7 @@ use crate::Error::{self, InvalidPublicKey, InvalidPublicKeySum, InvalidSecretKey
19
19
#[ cfg( feature = "global-context" ) ]
20
20
use crate :: SECP256K1 ;
21
21
use crate :: {
22
- constants, ecdsa, from_hex, schnorr, AllPreallocated , Message , Scalar , Secp256k1 , Signing ,
23
- Verification ,
22
+ constants, ecdsa, from_hex, schnorr, Message , Scalar , Secp256k1 , Signing , Verification ,
24
23
} ;
25
24
26
25
/// Secret key - a 256-bit key used to create ECDSA and Taproot signatures.
@@ -600,11 +599,12 @@ impl PublicKey {
600
599
/// Negates the public key.
601
600
#[ inline]
602
601
#[ must_use = "you forgot to use the negated public key" ]
603
- pub fn negate < C : Verification > ( mut self , secp : & Secp256k1 < C > ) -> PublicKey {
604
- unsafe {
605
- let res = ffi:: secp256k1_ec_pubkey_negate ( secp. ctx . as_ptr ( ) , & mut self . 0 ) ;
606
- debug_assert_eq ! ( res, 1 ) ;
607
- }
602
+ pub fn negate ( mut self ) -> PublicKey {
603
+ let res = crate :: with_raw_global_context (
604
+ |ctx| unsafe { ffi:: secp256k1_ec_pubkey_negate ( ctx. as_ptr ( ) , & mut self . 0 ) } ,
605
+ None ,
606
+ ) ;
607
+ debug_assert_eq ! ( res, 1 ) ;
608
608
self
609
609
}
610
610
@@ -614,19 +614,17 @@ impl PublicKey {
614
614
///
615
615
/// Returns an error if the resulting key would be invalid.
616
616
#[ inline]
617
- pub fn add_exp_tweak < C : Verification > (
618
- mut self ,
619
- secp : & Secp256k1 < C > ,
620
- tweak : & Scalar ,
621
- ) -> Result < PublicKey , Error > {
622
- unsafe {
623
- if ffi:: secp256k1_ec_pubkey_tweak_add ( secp. ctx . as_ptr ( ) , & mut self . 0 , tweak. as_c_ptr ( ) )
624
- == 1
625
- {
626
- Ok ( self )
627
- } else {
628
- Err ( Error :: InvalidTweak )
629
- }
617
+ pub fn add_exp_tweak ( mut self , tweak : & Scalar ) -> Result < PublicKey , Error > {
618
+ if crate :: with_raw_global_context (
619
+ |ctx| unsafe {
620
+ ffi:: secp256k1_ec_pubkey_tweak_add ( ctx. as_ptr ( ) , & mut self . 0 , tweak. as_c_ptr ( ) )
621
+ } ,
622
+ None ,
623
+ ) == 1
624
+ {
625
+ Ok ( self )
626
+ } else {
627
+ Err ( Error :: InvalidTweak )
630
628
}
631
629
}
632
630
@@ -870,12 +868,9 @@ impl Keypair {
870
868
/// or if the encoded number is an invalid scalar.
871
869
#[ deprecated( since = "0.31.0" , note = "Use `from_seckey_byte_array` instead." ) ]
872
870
#[ inline]
873
- pub fn from_seckey_slice < C : Signing > (
874
- secp : & Secp256k1 < C > ,
875
- data : & [ u8 ] ,
876
- ) -> Result < Keypair , Error > {
871
+ pub fn from_seckey_slice ( data : & [ u8 ] ) -> Result < Keypair , Error > {
877
872
match <[ u8 ; constants:: SECRET_KEY_SIZE ] >:: try_from ( data) {
878
- Ok ( data) => Self :: from_seckey_byte_array ( secp , data) ,
873
+ Ok ( data) => Self :: from_seckey_byte_array ( data) ,
879
874
Err ( _) => Err ( Error :: InvalidSecretKey ) ,
880
875
}
881
876
}
@@ -886,13 +881,16 @@ impl Keypair {
886
881
///
887
882
/// [`Error::InvalidSecretKey`] if the encoded number is an invalid scalar.
888
883
#[ inline]
889
- pub fn from_seckey_byte_array < C : Signing > (
890
- secp : & Secp256k1 < C > ,
884
+ pub fn from_seckey_byte_array (
891
885
data : [ u8 ; constants:: SECRET_KEY_SIZE ] ,
892
886
) -> Result < Keypair , Error > {
893
887
unsafe {
894
888
let mut kp = ffi:: Keypair :: new ( ) ;
895
- if ffi:: secp256k1_keypair_create ( secp. ctx . as_ptr ( ) , & mut kp, data. as_c_ptr ( ) ) == 1 {
889
+ if crate :: with_raw_global_context (
890
+ |ctx| ffi:: secp256k1_keypair_create ( ctx. as_ptr ( ) , & mut kp, data. as_c_ptr ( ) ) ,
891
+ Some ( & data) ,
892
+ ) == 1
893
+ {
896
894
Ok ( Keypair ( kp) )
897
895
} else {
898
896
Err ( Error :: InvalidSecretKey )
@@ -907,13 +905,8 @@ impl Keypair {
907
905
/// [`Error::InvalidSecretKey`] if the string does not consist of exactly 64 hex characters,
908
906
/// or if the encoded number is an invalid scalar.
909
907
#[ inline]
910
- pub fn from_seckey_str < C : Signing > ( secp : & Secp256k1 < C > , s : & str ) -> Result < Keypair , Error > {
911
- let mut res = [ 0u8 ; constants:: SECRET_KEY_SIZE ] ;
912
- match from_hex ( s, & mut res) {
913
- Ok ( constants:: SECRET_KEY_SIZE ) => Keypair :: from_seckey_byte_array ( secp, res) ,
914
- _ => Err ( Error :: InvalidSecretKey ) ,
915
- }
916
- }
908
+ #[ deprecated( note = "use FromStr or parse instead" ) ]
909
+ pub fn from_seckey_str ( s : & str ) -> Result < Self , Error > { s. parse ( ) }
917
910
918
911
/// Creates a [`Keypair`] directly from a secret key string and the global [`SECP256K1`] context.
919
912
///
@@ -922,10 +915,8 @@ impl Keypair {
922
915
/// [`Error::InvalidSecretKey`] if the string does not consist of exactly 64 hex characters,
923
916
/// or if the encoded number is an invalid scalar.
924
917
#[ inline]
925
- #[ cfg( feature = "global-context" ) ]
926
- pub fn from_seckey_str_global ( s : & str ) -> Result < Keypair , Error > {
927
- Keypair :: from_seckey_str ( SECP256K1 , s)
928
- }
918
+ #[ deprecated( note = "use FromStr or parse instead" ) ]
919
+ pub fn from_seckey_str_global ( s : & str ) -> Result < Keypair , Error > { s. parse ( ) }
929
920
930
921
/// Generates a new random key pair.
931
922
/// # Examples
@@ -1097,10 +1088,11 @@ impl str::FromStr for Keypair {
1097
1088
type Err = Error ;
1098
1089
1099
1090
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
1100
- crate :: with_global_context (
1101
- |secp : & Secp256k1 < AllPreallocated > | Self :: from_seckey_str ( secp, s) ,
1102
- None ,
1103
- )
1091
+ let mut res = [ 0u8 ; constants:: SECRET_KEY_SIZE ] ;
1092
+ match from_hex ( s, & mut res) {
1093
+ Ok ( constants:: SECRET_KEY_SIZE ) => Keypair :: from_seckey_byte_array ( res) ,
1094
+ _ => Err ( Error :: InvalidSecretKey ) ,
1095
+ }
1104
1096
}
1105
1097
}
1106
1098
@@ -1131,12 +1123,10 @@ impl<'de> serde::Deserialize<'de> for Keypair {
1131
1123
"a hex string representing 32 byte Keypair" ,
1132
1124
) )
1133
1125
} else {
1134
- let visitor = super :: serde_util:: Tuple32Visitor :: new ( "raw 32 bytes Keypair" , |data| {
1135
- crate :: with_global_context (
1136
- |secp : & Secp256k1 < AllPreallocated > | Self :: from_seckey_byte_array ( secp, data) ,
1137
- None ,
1138
- )
1139
- } ) ;
1126
+ let visitor = super :: serde_util:: Tuple32Visitor :: new (
1127
+ "raw 32 bytes Keypair" ,
1128
+ Keypair :: from_seckey_byte_array,
1129
+ ) ;
1140
1130
d. deserialize_tuple ( constants:: SECRET_KEY_SIZE , visitor)
1141
1131
}
1142
1132
}
@@ -1767,10 +1757,9 @@ mod test {
1767
1757
}
1768
1758
1769
1759
#[ test]
1770
- #[ cfg( all ( feature = "std" , not( secp256k1_fuzz) ) ) ]
1760
+ #[ cfg( not( secp256k1_fuzz) ) ]
1771
1761
fn erased_keypair_is_valid ( ) {
1772
- let s = Secp256k1 :: new ( ) ;
1773
- let kp = Keypair :: from_seckey_byte_array ( & s, [ 1u8 ; constants:: SECRET_KEY_SIZE ] )
1762
+ let kp = Keypair :: from_seckey_byte_array ( [ 1u8 ; constants:: SECRET_KEY_SIZE ] )
1774
1763
. expect ( "valid secret key" ) ;
1775
1764
let mut kp2 = kp;
1776
1765
kp2. non_secure_erase ( ) ;
@@ -2045,24 +2034,21 @@ mod test {
2045
2034
2046
2035
let tweaked_sk = sk. add_tweak ( & tweak) . unwrap ( ) ;
2047
2036
assert_ne ! ( sk, tweaked_sk) ; // Make sure we did something.
2048
- let tweaked_pk = pk. add_exp_tweak ( & s , & tweak) . unwrap ( ) ;
2037
+ let tweaked_pk = pk. add_exp_tweak ( & tweak) . unwrap ( ) ;
2049
2038
assert_ne ! ( pk, tweaked_pk) ;
2050
2039
2051
2040
assert_eq ! ( PublicKey :: from_secret_key( & s, & tweaked_sk) , tweaked_pk) ;
2052
2041
}
2053
2042
2054
2043
#[ test]
2055
- #[ cfg( feature = "std" ) ]
2056
2044
fn tweak_add_zero ( ) {
2057
- let s = Secp256k1 :: new ( ) ;
2058
-
2059
2045
let ( sk, pk) = crate :: test_random_keypair ( ) ;
2060
2046
2061
2047
let tweak = Scalar :: ZERO ;
2062
2048
2063
2049
let tweaked_sk = sk. add_tweak ( & tweak) . unwrap ( ) ;
2064
2050
assert_eq ! ( sk, tweaked_sk) ; // Tweak by zero does nothing.
2065
- let tweaked_pk = pk. add_exp_tweak ( & s , & tweak) . unwrap ( ) ;
2051
+ let tweaked_pk = pk. add_exp_tweak ( & tweak) . unwrap ( ) ;
2066
2052
assert_eq ! ( pk, tweaked_pk) ;
2067
2053
}
2068
2054
@@ -2107,9 +2093,9 @@ mod test {
2107
2093
let back_sk = neg. negate ( ) ;
2108
2094
assert_eq ! ( sk, back_sk) ;
2109
2095
2110
- let neg = pk. negate ( & s ) ;
2096
+ let neg = pk. negate ( ) ;
2111
2097
assert_ne ! ( pk, neg) ;
2112
- let back_pk = neg. negate ( & s ) ;
2098
+ let back_pk = neg. negate ( ) ;
2113
2099
assert_eq ! ( pk, back_pk) ;
2114
2100
2115
2101
assert_eq ! ( PublicKey :: from_secret_key( & s, & back_sk) , pk) ;
@@ -2367,7 +2353,7 @@ mod test {
2367
2353
] ;
2368
2354
static SK_STR : & str = "01010101010101010001020304050607ffff0000ffff00006363636363636363" ;
2369
2355
2370
- let sk = Keypair :: from_seckey_byte_array ( SECP256K1 , SK_BYTES ) . unwrap ( ) ;
2356
+ let sk = Keypair :: from_seckey_byte_array ( SK_BYTES ) . unwrap ( ) ;
2371
2357
#[ rustfmt:: skip]
2372
2358
assert_tokens ( & sk. compact ( ) , & [
2373
2359
Token :: Tuple { len : 32 } ,
@@ -2548,7 +2534,7 @@ mod test {
2548
2534
2549
2535
static PK_STR : & str = "18845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166" ;
2550
2536
2551
- let kp = Keypair :: from_seckey_byte_array ( crate :: SECP256K1 , SK_BYTES ) . unwrap ( ) ;
2537
+ let kp = Keypair :: from_seckey_byte_array ( SK_BYTES ) . unwrap ( ) ;
2552
2538
let ( pk, _parity) = XOnlyPublicKey :: from_keypair ( & kp) ;
2553
2539
2554
2540
#[ rustfmt:: skip]
@@ -2576,11 +2562,10 @@ mod test {
2576
2562
}
2577
2563
2578
2564
#[ test]
2579
- #[ cfg( all ( any ( feature = "alloc" , feature = "global-context" ) , feature = " serde") ) ]
2565
+ #[ cfg( feature = "serde" ) ]
2580
2566
fn test_keypair_deserialize_serde ( ) {
2581
- let ctx = crate :: Secp256k1 :: new ( ) ;
2582
2567
let sec_key_str = "4242424242424242424242424242424242424242424242424242424242424242" ;
2583
- let keypair = Keypair :: from_seckey_str ( & ctx , sec_key_str) . unwrap ( ) ;
2568
+ let keypair = Keypair :: from_str ( sec_key_str) . unwrap ( ) ;
2584
2569
2585
2570
serde_test:: assert_tokens ( & keypair. readable ( ) , & [ Token :: String ( sec_key_str) ] ) ;
2586
2571
0 commit comments