Skip to content

Commit 3dddd2e

Browse files
committed
Implement negate that consumes self
The method `negate_assign` (on pub/sec key) is cumbersome to use because a local variable that uses these methods changes meaning but keeps the same identifier. It would be more useful if we had methods that consumed `self` and returned a new key. Add method `negate` that consumes self and returns the negated key. Deprecated the `negate_assign` methods.
1 parent 5aacc42 commit 3dddd2e

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

src/key.rs

+35-24
Original file line numberDiff line numberDiff line change
@@ -214,18 +214,24 @@ impl SecretKey {
214214
self.0
215215
}
216216

217+
/// Negates the secret key.
217218
#[inline]
218-
/// Negates one secret key.
219-
pub fn negate_assign(
220-
&mut self
221-
) {
219+
#[deprecated(since = "0.23.0", note = "Use negate instead")]
220+
pub fn negate_assign(&mut self) {
221+
*self = self.negate()
222+
}
223+
224+
/// Negates the secret key.
225+
#[inline]
226+
pub fn negate(mut self) -> SecretKey {
222227
unsafe {
223228
let res = ffi::secp256k1_ec_seckey_negate(
224229
ffi::secp256k1_context_no_precomp,
225230
self.as_mut_c_ptr()
226231
);
227232
debug_assert_eq!(res, 1);
228233
}
234+
self
229235
}
230236

231237
/// Adds one secret key to another, modulo the curve order.
@@ -498,16 +504,21 @@ impl PublicKey {
498504
debug_assert_eq!(ret_len, ret.len());
499505
}
500506

501-
#[inline]
502507
/// Negates the public key in place.
503-
pub fn negate_assign<C: Verification>(
504-
&mut self,
505-
secp: &Secp256k1<C>
506-
) {
508+
#[inline]
509+
#[deprecated(since = "0.23.0", note = "Use negate instead")]
510+
pub fn negate_assign<C: Verification>(&mut self, secp: &Secp256k1<C>) {
511+
*self = self.negate(secp)
512+
}
513+
514+
/// Negates the public key.
515+
#[inline]
516+
pub fn negate<C: Verification>(mut self, secp: &Secp256k1<C>) -> PublicKey {
507517
unsafe {
508518
let res = ffi::secp256k1_ec_pubkey_negate(secp.ctx, &mut self.0);
509519
debug_assert_eq!(res, 1);
510520
}
521+
self
511522
}
512523

513524
/// Adds `other * G` to `self` in place.
@@ -1885,21 +1896,21 @@ mod test {
18851896
fn test_negation() {
18861897
let s = Secp256k1::new();
18871898

1888-
let (mut sk, mut pk) = s.generate_keypair(&mut thread_rng());
1889-
1890-
let original_sk = sk;
1891-
let original_pk = pk;
1892-
1893-
assert_eq!(PublicKey::from_secret_key(&s, &sk), pk);
1894-
sk.negate_assign();
1895-
pk.negate_assign(&s);
1896-
assert_ne!(original_sk, sk);
1897-
assert_ne!(original_pk, pk);
1898-
sk.negate_assign();
1899-
pk.negate_assign(&s);
1900-
assert_eq!(original_sk, sk);
1901-
assert_eq!(original_pk, pk);
1902-
assert_eq!(PublicKey::from_secret_key(&s, &sk), pk);
1899+
let (sk, pk) = s.generate_keypair(&mut thread_rng());
1900+
1901+
assert_eq!(PublicKey::from_secret_key(&s, &sk), pk); // Sanity check.
1902+
1903+
let neg = sk.negate();
1904+
assert_ne!(sk, neg);
1905+
let back_sk = neg.negate();
1906+
assert_eq!(sk, back_sk);
1907+
1908+
let neg = pk.negate(&s);
1909+
assert_ne!(pk, neg);
1910+
let back_pk = neg.negate(&s);
1911+
assert_eq!(pk, back_pk);
1912+
1913+
assert_eq!(PublicKey::from_secret_key(&s, &back_sk), pk);
19031914
}
19041915

19051916
#[test]

0 commit comments

Comments
 (0)