@@ -892,6 +892,19 @@ impl KeyPair {
892
892
* SecretKey :: from_keypair ( self ) . as_ref ( )
893
893
}
894
894
895
+ /// Tweaks a keypair by adding the given tweak to the secret key and updating the public key
896
+ /// accordingly.
897
+ #[ inline]
898
+ #[ deprecated( since = "0.23.0" , note = "Use add_tweak instead" ) ]
899
+ pub fn tweak_add_assign < C : Verification > (
900
+ & mut self ,
901
+ secp : & Secp256k1 < C > ,
902
+ tweak : & Scalar ,
903
+ ) -> Result < ( ) , Error > {
904
+ * self = self . add_tweak ( secp, tweak) ?;
905
+ Ok ( ( ) )
906
+ }
907
+
895
908
/// Tweaks a keypair by adding the given tweak to the secret key and updating the public key
896
909
/// accordingly.
897
910
///
@@ -913,16 +926,16 @@ impl KeyPair {
913
926
/// let tweak = Scalar::random();
914
927
///
915
928
/// let mut key_pair = KeyPair::new(&secp, &mut thread_rng());
916
- /// key_pair.tweak_add_assign (&secp, &tweak).expect("Improbable to fail with a randomly generated tweak");
929
+ /// let tweaked = key_pair.add_tweak (&secp, &tweak).expect("Improbable to fail with a randomly generated tweak");
917
930
/// # }
918
931
/// ```
919
932
// TODO: Add checked implementation
920
933
#[ inline]
921
- pub fn tweak_add_assign < C : Verification > (
922
- & mut self ,
934
+ pub fn add_tweak < C : Verification > (
935
+ mut self ,
923
936
secp : & Secp256k1 < C > ,
924
937
tweak : & Scalar ,
925
- ) -> Result < ( ) , Error > {
938
+ ) -> Result < KeyPair , Error > {
926
939
unsafe {
927
940
let err = ffi:: secp256k1_keypair_xonly_tweak_add (
928
941
secp. ctx ,
@@ -933,7 +946,7 @@ impl KeyPair {
933
946
return Err ( Error :: InvalidTweak ) ;
934
947
}
935
948
936
- Ok ( ( ) )
949
+ Ok ( self )
937
950
}
938
951
}
939
952
@@ -1166,12 +1179,24 @@ impl XOnlyPublicKey {
1166
1179
}
1167
1180
1168
1181
/// Tweaks an x-only PublicKey by adding the generator multiplied with the given tweak to it.
1182
+ #[ deprecated( since = "0.23.0" , note = "Use add_tweak instead" ) ]
1183
+ pub fn tweak_add_assign < V : Verification > (
1184
+ & mut self ,
1185
+ secp : & Secp256k1 < V > ,
1186
+ tweak : & Scalar ,
1187
+ ) -> Result < Parity , Error > {
1188
+ let ( tweaked, parity) = self . add_tweak ( secp, tweak) ?;
1189
+ * self = tweaked;
1190
+ Ok ( parity)
1191
+ }
1192
+
1193
+ /// Tweaks an [`XOnlyPublicKey`] by adding the generator multiplied with the given tweak to it.
1169
1194
///
1170
1195
/// # Returns
1171
1196
///
1172
- /// An opaque type representing the parity of the tweaked key, this should be provided to
1173
- /// `tweak_add_check` which can be used to verify a tweak more efficiently than regenerating
1174
- /// it and checking equality.
1197
+ /// The newly tweaked key plus an opaque type representing the parity of the tweaked key, this
1198
+ /// should be provided to `tweak_add_check` which can be used to verify a tweak more efficiently
1199
+ /// than regenerating it and checking equality.
1175
1200
///
1176
1201
/// # Errors
1177
1202
///
@@ -1181,22 +1206,22 @@ impl XOnlyPublicKey {
1181
1206
///
1182
1207
/// ```
1183
1208
/// # #[cfg(all(feature = "std", feature = "rand-std"))] {
1184
- /// use secp256k1::{Secp256k1, KeyPair, Scalar};
1209
+ /// use secp256k1::{Secp256k1, KeyPair, Scalar, XOnlyPublicKey };
1185
1210
/// use secp256k1::rand::{RngCore, thread_rng};
1186
1211
///
1187
1212
/// let secp = Secp256k1::new();
1188
1213
/// let tweak = Scalar::random();
1189
1214
///
1190
1215
/// let mut key_pair = KeyPair::new(&secp, &mut thread_rng());
1191
- /// let (mut public_key , _parity) = key_pair.x_only_public_key();
1192
- /// public_key.tweak_add_assign (&secp, &tweak).expect("Improbable to fail with a randomly generated tweak");
1216
+ /// let (xonly , _parity) = key_pair.x_only_public_key();
1217
+ /// let tweaked = xonly.add_tweak (&secp, &tweak).expect("Improbable to fail with a randomly generated tweak");
1193
1218
/// # }
1194
1219
/// ```
1195
- pub fn tweak_add_assign < V : Verification > (
1196
- & mut self ,
1220
+ pub fn add_tweak < V : Verification > (
1221
+ mut self ,
1197
1222
secp : & Secp256k1 < V > ,
1198
1223
tweak : & Scalar ,
1199
- ) -> Result < Parity , Error > {
1224
+ ) -> Result < ( XOnlyPublicKey , Parity ) , Error > {
1200
1225
let mut pk_parity = 0 ;
1201
1226
unsafe {
1202
1227
let mut pubkey = ffi:: PublicKey :: new ( ) ;
@@ -1220,7 +1245,8 @@ impl XOnlyPublicKey {
1220
1245
return Err ( Error :: InvalidPublicKey ) ;
1221
1246
}
1222
1247
1223
- Parity :: from_i32 ( pk_parity) . map_err ( Into :: into)
1248
+ let parity = Parity :: from_i32 ( pk_parity) ?;
1249
+ Ok ( ( self , parity) )
1224
1250
}
1225
1251
}
1226
1252
@@ -2051,20 +2077,20 @@ mod test {
2051
2077
fn test_tweak_add_assign_then_tweak_add_check ( ) {
2052
2078
let s = Secp256k1 :: new ( ) ;
2053
2079
2080
+ // TODO: 10 times is arbitrary, we should test this a _lot_ of times.
2054
2081
for _ in 0 ..10 {
2055
2082
let tweak = Scalar :: random ( ) ;
2056
2083
2057
- let mut kp = KeyPair :: new ( & s, & mut thread_rng ( ) ) ;
2058
- let ( mut pk , _parity ) = kp . x_only_public_key ( ) ;
2084
+ let kp = KeyPair :: new ( & s, & mut thread_rng ( ) ) ;
2085
+ let ( xonly , _ ) = XOnlyPublicKey :: from_keypair ( & kp ) ;
2059
2086
2060
- let orig_pk = pk;
2061
- kp. tweak_add_assign ( & s, & tweak) . expect ( "Tweak error" ) ;
2062
- let parity = pk. tweak_add_assign ( & s, & tweak) . expect ( "Tweak error" ) ;
2087
+ let tweaked_kp = kp. add_tweak ( & s, & tweak) . expect ( "keypair tweak add failed" ) ;
2088
+ let ( tweaked_xonly, parity) = xonly. add_tweak ( & s, & tweak) . expect ( "xonly pubkey tweak failed" ) ;
2063
2089
2064
- let ( back , _) = XOnlyPublicKey :: from_keypair ( & kp ) ;
2090
+ let ( want_tweaked_xonly , _) = XOnlyPublicKey :: from_keypair ( & tweaked_kp ) ;
2065
2091
2066
- assert_eq ! ( back , pk ) ;
2067
- assert ! ( orig_pk . tweak_add_check( & s, & pk , parity, tweak) ) ;
2092
+ assert_eq ! ( tweaked_xonly , want_tweaked_xonly ) ;
2093
+ assert ! ( xonly . tweak_add_check( & s, & tweaked_xonly , parity, tweak) ) ;
2068
2094
}
2069
2095
}
2070
2096
0 commit comments