Skip to content

Commit 739dc2e

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 e7e312e commit 739dc2e

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

src/key.rs

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -218,18 +218,24 @@ impl SecretKey {
218218
self.0
219219
}
220220

221+
/// Negates the secret key.
221222
#[inline]
222-
/// Negates one secret key.
223-
pub fn negate_assign(
224-
&mut self
225-
) {
223+
#[deprecated(since = "TODO: Set this prior to release", note = "Use negate instead")]
224+
pub fn negate_assign(&mut self) {
225+
*self = self.negate()
226+
}
227+
228+
/// Negates the secret key.
229+
#[inline]
230+
pub fn negate(mut self) -> SecretKey {
226231
unsafe {
227232
let res = ffi::secp256k1_ec_seckey_negate(
228233
ffi::secp256k1_context_no_precomp,
229234
self.as_mut_c_ptr()
230235
);
231236
debug_assert_eq!(res, 1);
232237
}
238+
self
233239
}
234240

235241
/// Adds one secret key to another, modulo the curve order.
@@ -478,16 +484,21 @@ impl PublicKey {
478484
ret
479485
}
480486

481-
#[inline]
482487
/// Negates the public key in place.
483-
pub fn negate_assign<C: Verification>(
484-
&mut self,
485-
secp: &Secp256k1<C>
486-
) {
488+
#[inline]
489+
#[deprecated(since = "TODO: Set this prior to release", note = "Use negate instead")]
490+
pub fn negate_assign<C: Verification>(&mut self, secp: &Secp256k1<C>) {
491+
*self = self.negate(secp)
492+
}
493+
494+
/// Negates the public key.
495+
#[inline]
496+
pub fn negate<C: Verification>(mut self, secp: &Secp256k1<C>) -> PublicKey {
487497
unsafe {
488498
let res = ffi::secp256k1_ec_pubkey_negate(secp.ctx, &mut self.0);
489499
debug_assert_eq!(res, 1);
490500
}
501+
self
491502
}
492503

493504
/// Adds the `other` public key 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)