@@ -892,6 +892,19 @@ impl KeyPair {
892892 * SecretKey :: from_keypair ( self ) . as_ref ( )
893893 }
894894
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+
895908 /// Tweaks a keypair by adding the given tweak to the secret key and updating the public key
896909 /// accordingly.
897910 ///
@@ -913,16 +926,16 @@ impl KeyPair {
913926 /// let tweak = Scalar::random();
914927 ///
915928 /// 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");
917930 /// # }
918931 /// ```
919932 // TODO: Add checked implementation
920933 #[ inline]
921- pub fn tweak_add_assign < C : Verification > (
922- & mut self ,
934+ pub fn add_tweak < C : Verification > (
935+ mut self ,
923936 secp : & Secp256k1 < C > ,
924937 tweak : & Scalar ,
925- ) -> Result < ( ) , Error > {
938+ ) -> Result < KeyPair , Error > {
926939 unsafe {
927940 let err = ffi:: secp256k1_keypair_xonly_tweak_add (
928941 secp. ctx ,
@@ -933,7 +946,7 @@ impl KeyPair {
933946 return Err ( Error :: InvalidTweak ) ;
934947 }
935948
936- Ok ( ( ) )
949+ Ok ( self )
937950 }
938951 }
939952
@@ -1166,12 +1179,24 @@ impl XOnlyPublicKey {
11661179 }
11671180
11681181 /// 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.
11691194 ///
11701195 /// # Returns
11711196 ///
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.
11751200 ///
11761201 /// # Errors
11771202 ///
@@ -1181,22 +1206,22 @@ impl XOnlyPublicKey {
11811206 ///
11821207 /// ```
11831208 /// # #[cfg(all(feature = "std", feature = "rand-std"))] {
1184- /// use secp256k1::{Secp256k1, KeyPair, Scalar};
1209+ /// use secp256k1::{Secp256k1, KeyPair, Scalar, XOnlyPublicKey };
11851210 /// use secp256k1::rand::{RngCore, thread_rng};
11861211 ///
11871212 /// let secp = Secp256k1::new();
11881213 /// let tweak = Scalar::random();
11891214 ///
11901215 /// 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");
11931218 /// # }
11941219 /// ```
1195- pub fn tweak_add_assign < V : Verification > (
1196- & mut self ,
1220+ pub fn add_tweak < V : Verification > (
1221+ mut self ,
11971222 secp : & Secp256k1 < V > ,
11981223 tweak : & Scalar ,
1199- ) -> Result < Parity , Error > {
1224+ ) -> Result < ( XOnlyPublicKey , Parity ) , Error > {
12001225 let mut pk_parity = 0 ;
12011226 unsafe {
12021227 let mut pubkey = ffi:: PublicKey :: new ( ) ;
@@ -1220,7 +1245,8 @@ impl XOnlyPublicKey {
12201245 return Err ( Error :: InvalidPublicKey ) ;
12211246 }
12221247
1223- Parity :: from_i32 ( pk_parity) . map_err ( Into :: into)
1248+ let parity = Parity :: from_i32 ( pk_parity) ?;
1249+ Ok ( ( self , parity) )
12241250 }
12251251 }
12261252
@@ -2051,20 +2077,20 @@ mod test {
20512077 fn test_tweak_add_assign_then_tweak_add_check ( ) {
20522078 let s = Secp256k1 :: new ( ) ;
20532079
2080+ // TODO: 10 times is arbitrary, we should test this a _lot_ of times.
20542081 for _ in 0 ..10 {
20552082 let tweak = Scalar :: random ( ) ;
20562083
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 ) ;
20592086
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" ) ;
20632089
2064- let ( back , _) = XOnlyPublicKey :: from_keypair ( & kp ) ;
2090+ let ( want_tweaked_xonly , _) = XOnlyPublicKey :: from_keypair ( & tweaked_kp ) ;
20652091
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) ) ;
20682094 }
20692095 }
20702096
0 commit comments