Skip to content

Commit e7e312e

Browse files
committed
Rename tweak_add_assign -> add_tweak
We now have a method `add_tweak` on the `SecretKey` and `PublicKey`. We can similarly add a method `add_tweak` that consumes self and returns the tweaked key for the `KeyPair` and `XOnlyPublicKey` types. The justification for doing so is that a local variable that calls `tweak_add_assign` changes in meaning but the identifier remains the same, this leads to cumbersome renaming of the local variable.
1 parent 21604a7 commit e7e312e

File tree

1 file changed

+48
-24
lines changed

1 file changed

+48
-24
lines changed

src/key.rs

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,19 @@ impl KeyPair {
869869
*SecretKey::from_keypair(self).as_ref()
870870
}
871871

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+
872885
/// Tweaks a keypair by adding the given tweak to the secret key and updating the public key
873886
/// accordingly.
874887
///
@@ -888,20 +901,19 @@ impl KeyPair {
888901
/// use secp256k1::rand::{RngCore, thread_rng};
889902
///
890903
/// 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());
893905
///
894906
/// 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");
896908
/// # }
897909
/// ```
898910
// TODO: Add checked implementation
899911
#[inline]
900-
pub fn tweak_add_assign<C: Verification>(
901-
&mut self,
912+
pub fn add_tweak<C: Verification>(
913+
mut self,
902914
secp: &Secp256k1<C>,
903915
tweak: &[u8],
904-
) -> Result<(), Error> {
916+
) -> Result<KeyPair, Error> {
905917
if tweak.len() != 32 {
906918
return Err(Error::InvalidTweak);
907919
}
@@ -916,7 +928,7 @@ impl KeyPair {
916928
return Err(Error::InvalidTweak);
917929
}
918930

919-
Ok(())
931+
Ok(self)
920932
}
921933
}
922934

@@ -1129,12 +1141,24 @@ impl XOnlyPublicKey {
11291141
}
11301142

11311143
/// 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.
11321156
///
11331157
/// # Returns
11341158
///
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.
11381162
///
11391163
/// # Errors
11401164
///
@@ -1153,14 +1177,14 @@ impl XOnlyPublicKey {
11531177
///
11541178
/// let mut key_pair = KeyPair::new(&secp, &mut thread_rng());
11551179
/// 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");
11571181
/// # }
11581182
/// ```
1159-
pub fn tweak_add_assign<V: Verification>(
1160-
&mut self,
1183+
pub fn add_tweak<V: Verification>(
1184+
mut self,
11611185
secp: &Secp256k1<V>,
11621186
tweak: &[u8],
1163-
) -> Result<Parity, Error> {
1187+
) -> Result<(XOnlyPublicKey, Parity), Error> {
11641188
if tweak.len() != 32 {
11651189
return Err(Error::InvalidTweak);
11661190
}
@@ -1188,7 +1212,8 @@ impl XOnlyPublicKey {
11881212
return Err(Error::InvalidPublicKey);
11891213
}
11901214

1191-
Parity::from_i32(parity).map_err(Into::into)
1215+
let parity = Parity::from_i32(parity)?;
1216+
Ok((self, parity))
11921217
}
11931218
}
11941219

@@ -2052,18 +2077,17 @@ mod test {
20522077
fn test_tweak_add_assign_then_tweak_add_check() {
20532078
let s = Secp256k1::new();
20542079

2080+
// TODO: 10 times is arbitrary, we should test this a _lot_ of times.
20552081
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());
20582083

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();
20612086

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));
20672091
}
20682092
}
20692093

0 commit comments

Comments
 (0)