@@ -909,6 +909,19 @@ impl KeyPair {
909909 * SecretKey :: from_keypair ( self ) . as_ref ( )
910910 }
911911
912+ /// Tweaks a keypair by adding the given tweak to the secret key and updating the public key
913+ /// accordingly.
914+ #[ inline]
915+ #[ deprecated( since = "0.23.0" , note = "Use add_tweak instead" ) ]
916+ pub fn tweak_add_assign < C : Verification > (
917+ & mut self ,
918+ secp : & Secp256k1 < C > ,
919+ tweak : & [ u8 ] ,
920+ ) -> Result < ( ) , Error > {
921+ * self = self . add_tweak ( secp, tweak) ?;
922+ Ok ( ( ) )
923+ }
924+
912925 /// Tweaks a keypair by adding the given tweak to the secret key and updating the public key
913926 /// accordingly.
914927 ///
@@ -928,20 +941,19 @@ impl KeyPair {
928941 /// use secp256k1::rand::{RngCore, thread_rng};
929942 ///
930943 /// let secp = Secp256k1::new();
931- /// let mut tweak = [0u8; 32];
932- /// thread_rng().fill_bytes(&mut tweak);
944+ /// let tweak = random_32_bytes(&mut thread_rng());
933945 ///
934946 /// let mut key_pair = KeyPair::new(&secp, &mut thread_rng());
935- /// key_pair.tweak_add_assign (&secp, &tweak).expect("Improbable to fail with a randomly generated tweak");
947+ /// let tweaked = key_pair.tweak_add (&secp, &tweak).expect("Improbable to fail with a randomly generated tweak");
936948 /// # }
937949 /// ```
938950 // TODO: Add checked implementation
939951 #[ inline]
940- pub fn tweak_add_assign < C : Verification > (
941- & mut self ,
952+ pub fn add_tweak < C : Verification > (
953+ mut self ,
942954 secp : & Secp256k1 < C > ,
943955 tweak : & [ u8 ] ,
944- ) -> Result < ( ) , Error > {
956+ ) -> Result < KeyPair , Error > {
945957 if tweak. len ( ) != 32 {
946958 return Err ( Error :: InvalidTweak ) ;
947959 }
@@ -956,7 +968,7 @@ impl KeyPair {
956968 return Err ( Error :: InvalidTweak ) ;
957969 }
958970
959- Ok ( ( ) )
971+ Ok ( self )
960972 }
961973 }
962974
@@ -1189,12 +1201,24 @@ impl XOnlyPublicKey {
11891201 }
11901202
11911203 /// Tweaks an x-only PublicKey by adding the generator multiplied with the given tweak to it.
1204+ #[ deprecated( since = "0.23.0" , note = "Use add_tweak instead" ) ]
1205+ pub fn tweak_add_assign < V : Verification > (
1206+ & mut self ,
1207+ secp : & Secp256k1 < V > ,
1208+ tweak : & [ u8 ] ,
1209+ ) -> Result < Parity , Error > {
1210+ let ( tweaked, parity) = self . add_tweak ( secp, tweak) ?;
1211+ * self = tweaked;
1212+ Ok ( parity)
1213+ }
1214+
1215+ /// Tweaks an [`XOnlyPublicKey`] by adding the generator multiplied with the given tweak to it.
11921216 ///
11931217 /// # Returns
11941218 ///
1195- /// An opaque type representing the parity of the tweaked key, this should be provided to
1196- /// `tweak_add_check` which can be used to verify a tweak more efficiently than regenerating
1197- /// it and checking equality.
1219+ /// The newly tweaked key plus an opaque type representing the parity of the tweaked key, this
1220+ /// should be provided to `tweak_add_check` which can be used to verify a tweak more efficiently
1221+ /// than regenerating it and checking equality.
11981222 ///
11991223 /// # Errors
12001224 ///
@@ -1212,15 +1236,15 @@ impl XOnlyPublicKey {
12121236 /// thread_rng().fill_bytes(&mut tweak);
12131237 ///
12141238 /// let mut key_pair = KeyPair::new(&secp, &mut thread_rng());
1215- /// let ( mut public_key, _parity) = key_pair.x_only_public_key ();
1216- /// public_key.tweak_add_assign (&secp, &tweak).expect("Improbable to fail with a randomly generated tweak");
1239+ /// let mut public_key = key_pair.public_key ();
1240+ /// let (tweaked, parity) = public_key.add_tweak (&secp, &tweak).expect("Improbable to fail with a randomly generated tweak");
12171241 /// # }
12181242 /// ```
1219- pub fn tweak_add_assign < V : Verification > (
1220- & mut self ,
1243+ pub fn add_tweak < V : Verification > (
1244+ mut self ,
12211245 secp : & Secp256k1 < V > ,
12221246 tweak : & [ u8 ] ,
1223- ) -> Result < Parity , Error > {
1247+ ) -> Result < ( XOnlyPublicKey , Parity ) , Error > {
12241248 if tweak. len ( ) != 32 {
12251249 return Err ( Error :: InvalidTweak ) ;
12261250 }
@@ -1248,7 +1272,8 @@ impl XOnlyPublicKey {
12481272 return Err ( Error :: InvalidPublicKey ) ;
12491273 }
12501274
1251- Parity :: from_i32 ( pk_parity) . map_err ( Into :: into)
1275+ let parity = Parity :: from_i32 ( pk_parity) ?;
1276+ Ok ( ( self , parity) )
12521277 }
12531278 }
12541279
@@ -2079,21 +2104,21 @@ mod test {
20792104 fn test_tweak_add_assign_then_tweak_add_check ( ) {
20802105 let s = Secp256k1 :: new ( ) ;
20812106
2107+ // TODO: 10 times is arbitrary, we should test this a _lot_ of times.
20822108 for _ in 0 ..10 {
2083- let mut tweak = [ 0u8 ; 32 ] ;
2084- thread_rng ( ) . fill_bytes ( & mut tweak) ;
2109+ let tweak = random_32_bytes ( & mut thread_rng ( ) ) ;
20852110
2086- let mut kp = KeyPair :: new ( & s, & mut thread_rng ( ) ) ;
2087- let ( mut pk , _parity) = kp . x_only_public_key ( ) ;
2111+ let kp = KeyPair :: new ( & s, & mut thread_rng ( ) ) ;
2112+ let ( xonly , _parity) = XOnlyPublicKey :: from_keypair ( & kp ) ;
20882113
2089- let orig_pk = pk;
2090- kp. tweak_add_assign ( & s, & tweak) . expect ( "Tweak error" ) ;
2091- let parity = pk. tweak_add_assign ( & s, & tweak) . expect ( "Tweak error" ) ;
2114+ let tweaked_kp = kp. add_tweak ( & s, & tweak) . expect ( "keypair tweak add failed" ) ;
2115+ let ( tweaked_xonly, parity) = xonly. add_tweak ( & s, & tweak) . expect ( "xonly pubkey tweak failed" ) ;
20922116
2093- let ( back, _) = XOnlyPublicKey :: from_keypair ( & kp) ;
2117+ let ( want_tweaked_xonly, other_parity) = XOnlyPublicKey :: from_keypair ( & tweaked_kp) ;
2118+ assert_eq ! ( parity, other_parity) ; // Sanity check.
20942119
2095- assert_eq ! ( back , pk ) ;
2096- assert ! ( orig_pk . tweak_add_check( & s, & pk , parity, tweak) ) ;
2120+ assert_eq ! ( tweaked_xonly , want_tweaked_xonly ) ;
2121+ assert ! ( xonly . tweak_add_check( & s, & tweaked_xonly , parity, tweak) ) ;
20972122 }
20982123 }
20992124
0 commit comments