@@ -869,6 +869,19 @@ impl KeyPair {
869
869
* SecretKey :: from_keypair ( self ) . as_ref ( )
870
870
}
871
871
872
+ /// Tweaks a keypair by adding the given tweak to the secret key and updating the public key
873
+ /// accordingly.
874
+ #[ inline]
875
+ #[ deprecated( since = "TODO: Set this prior to release" , note = "Use add_tweak instead" ) ]
876
+ pub fn tweak_add_assign < C : Verification > (
877
+ & mut self ,
878
+ secp : & Secp256k1 < C > ,
879
+ tweak : & [ u8 ] ,
880
+ ) -> Result < ( ) , Error > {
881
+ * self = self . add_tweak ( secp, tweak) ?;
882
+ Ok ( ( ) )
883
+ }
884
+
872
885
/// Tweaks a keypair by adding the given tweak to the secret key and updating the public key
873
886
/// accordingly.
874
887
///
@@ -888,20 +901,19 @@ impl KeyPair {
888
901
/// use secp256k1::rand::{RngCore, thread_rng};
889
902
///
890
903
/// let secp = Secp256k1::new();
891
- /// let mut tweak = [0u8; 32];
892
- /// thread_rng().fill_bytes(&mut tweak);
904
+ /// let tweak = random_32_bytes(&mut thread_rng());
893
905
///
894
906
/// let mut key_pair = KeyPair::new(&secp, &mut thread_rng());
895
- /// key_pair.tweak_add_assign (&secp, &tweak).expect("Improbable to fail with a randomly generated tweak");
907
+ /// let tweaked = key_pair.tweak_add (&secp, &tweak).expect("Improbable to fail with a randomly generated tweak");
896
908
/// # }
897
909
/// ```
898
910
// TODO: Add checked implementation
899
911
#[ inline]
900
- pub fn tweak_add_assign < C : Verification > (
901
- & mut self ,
912
+ pub fn add_tweak < C : Verification > (
913
+ mut self ,
902
914
secp : & Secp256k1 < C > ,
903
915
tweak : & [ u8 ] ,
904
- ) -> Result < ( ) , Error > {
916
+ ) -> Result < KeyPair , Error > {
905
917
if tweak. len ( ) != 32 {
906
918
return Err ( Error :: InvalidTweak ) ;
907
919
}
@@ -916,7 +928,7 @@ impl KeyPair {
916
928
return Err ( Error :: InvalidTweak ) ;
917
929
}
918
930
919
- Ok ( ( ) )
931
+ Ok ( self )
920
932
}
921
933
}
922
934
@@ -1129,12 +1141,24 @@ impl XOnlyPublicKey {
1129
1141
}
1130
1142
1131
1143
/// Tweaks an x-only PublicKey by adding the generator multiplied with the given tweak to it.
1144
+ #[ deprecated( since = "TODO: Set this prior to release" , note = "Use add_tweak instead" ) ]
1145
+ pub fn tweak_add_assign < V : Verification > (
1146
+ & mut self ,
1147
+ secp : & Secp256k1 < V > ,
1148
+ tweak : & [ u8 ] ,
1149
+ ) -> Result < Parity , Error > {
1150
+ let ( tweaked, parity) = self . add_tweak ( secp, tweak) ?;
1151
+ * self = tweaked;
1152
+ Ok ( parity)
1153
+ }
1154
+
1155
+ /// Tweaks an [`XOnlyPublicKey`] by adding the generator multiplied with the given tweak to it.
1132
1156
///
1133
1157
/// # Returns
1134
1158
///
1135
- /// An opaque type representing the parity of the tweaked key, this should be provided to
1136
- /// `tweak_add_check` which can be used to verify a tweak more efficiently than regenerating
1137
- /// it and checking equality.
1159
+ /// The newly tweaked key plus an opaque type representing the parity of the tweaked key, this
1160
+ /// should be provided to `tweak_add_check` which can be used to verify a tweak more efficiently
1161
+ /// than regenerating it and checking equality.
1138
1162
///
1139
1163
/// # Errors
1140
1164
///
@@ -1153,14 +1177,14 @@ impl XOnlyPublicKey {
1153
1177
///
1154
1178
/// let mut key_pair = KeyPair::new(&secp, &mut thread_rng());
1155
1179
/// let mut public_key = key_pair.public_key();
1156
- /// public_key.tweak_add_assign(&secp, &tweak).expect("Improbable to fail with a randomly generated tweak");
1180
+ /// let (tweaked, parity) = public_key.tweak_add_assign(&secp, &tweak).expect("Improbable to fail with a randomly generated tweak");
1157
1181
/// # }
1158
1182
/// ```
1159
- pub fn tweak_add_assign < V : Verification > (
1160
- & mut self ,
1183
+ pub fn add_tweak < V : Verification > (
1184
+ mut self ,
1161
1185
secp : & Secp256k1 < V > ,
1162
1186
tweak : & [ u8 ] ,
1163
- ) -> Result < Parity , Error > {
1187
+ ) -> Result < ( XOnlyPublicKey , Parity ) , Error > {
1164
1188
if tweak. len ( ) != 32 {
1165
1189
return Err ( Error :: InvalidTweak ) ;
1166
1190
}
@@ -1188,7 +1212,8 @@ impl XOnlyPublicKey {
1188
1212
return Err ( Error :: InvalidPublicKey ) ;
1189
1213
}
1190
1214
1191
- Parity :: from_i32 ( parity) . map_err ( Into :: into)
1215
+ let parity = Parity :: from_i32 ( parity) ?;
1216
+ Ok ( ( self , parity) )
1192
1217
}
1193
1218
}
1194
1219
@@ -2052,18 +2077,17 @@ mod test {
2052
2077
fn test_tweak_add_assign_then_tweak_add_check ( ) {
2053
2078
let s = Secp256k1 :: new ( ) ;
2054
2079
2080
+ // TODO: 10 times is arbitrary, we should test this a _lot_ of times.
2055
2081
for _ in 0 ..10 {
2056
- let mut tweak = [ 0u8 ; 32 ] ;
2057
- thread_rng ( ) . fill_bytes ( & mut tweak) ;
2082
+ let tweak = random_32_bytes ( & mut thread_rng ( ) ) ;
2058
2083
2059
- let mut kp = KeyPair :: new ( & s, & mut thread_rng ( ) ) ;
2060
- let mut pk = kp. public_key ( ) ;
2084
+ let kp = KeyPair :: new ( & s, & mut thread_rng ( ) ) ;
2085
+ let pk = kp. public_key ( ) ;
2061
2086
2062
- let orig_pk = pk;
2063
- kp. tweak_add_assign ( & s, & tweak) . expect ( "Tweak error" ) ;
2064
- let parity = pk. tweak_add_assign ( & s, & tweak) . expect ( "Tweak error" ) ;
2065
- assert_eq ! ( XOnlyPublicKey :: from_keypair( & kp) , pk) ;
2066
- assert ! ( orig_pk. tweak_add_check( & s, & pk, parity, tweak) ) ;
2087
+ let tweaked_kp = kp. add_tweak ( & s, & tweak) . expect ( "keypair tweak add failed" ) ;
2088
+ let ( tweaked_pk, parity) = pk. add_tweak ( & s, & tweak) . expect ( "pubkey tweak add failed" ) ;
2089
+ assert_eq ! ( XOnlyPublicKey :: from_keypair( & tweaked_kp) , tweaked_pk) ;
2090
+ assert ! ( pk. tweak_add_check( & s, & tweaked_pk, parity, tweak) ) ;
2067
2091
}
2068
2092
}
2069
2093
0 commit comments