Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b68029c

Browse files
committedJun 9, 2022
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 160db5b commit b68029c

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
@@ -213,18 +213,24 @@ impl SecretKey {
213213
self.0
214214
}
215215

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

230236
/// Adds one secret key to another, modulo the curve order.
@@ -505,16 +511,21 @@ impl PublicKey {
505511
debug_assert_eq!(ret_len, ret.len());
506512
}
507513

508-
#[inline]
509514
/// Negates the public key in place.
510-
pub fn negate_assign<C: Verification>(
511-
&mut self,
512-
secp: &Secp256k1<C>
513-
) {
515+
#[inline]
516+
#[deprecated(since = "0.23.0", note = "Use negate instead")]
517+
pub fn negate_assign<C: Verification>(&mut self, secp: &Secp256k1<C>) {
518+
*self = self.negate(secp)
519+
}
520+
521+
/// Negates the public key.
522+
#[inline]
523+
pub fn negate<C: Verification>(mut self, secp: &Secp256k1<C>) -> PublicKey {
514524
unsafe {
515525
let res = ffi::secp256k1_ec_pubkey_negate(secp.ctx, &mut self.0);
516526
debug_assert_eq!(res, 1);
517527
}
528+
self
518529
}
519530

520531
/// Adds the `other` public key to `self` in place.
@@ -1912,21 +1923,21 @@ mod test {
19121923
fn test_negation() {
19131924
let s = Secp256k1::new();
19141925

1915-
let (mut sk, mut pk) = s.generate_keypair(&mut thread_rng());
1916-
1917-
let original_sk = sk;
1918-
let original_pk = pk;
1919-
1920-
assert_eq!(PublicKey::from_secret_key(&s, &sk), pk);
1921-
sk.negate_assign();
1922-
pk.negate_assign(&s);
1923-
assert_ne!(original_sk, sk);
1924-
assert_ne!(original_pk, pk);
1925-
sk.negate_assign();
1926-
pk.negate_assign(&s);
1927-
assert_eq!(original_sk, sk);
1928-
assert_eq!(original_pk, pk);
1929-
assert_eq!(PublicKey::from_secret_key(&s, &sk), pk);
1926+
let (sk, pk) = s.generate_keypair(&mut thread_rng());
1927+
1928+
assert_eq!(PublicKey::from_secret_key(&s, &sk), pk); // Sanity check.
1929+
1930+
let neg = sk.negate();
1931+
assert_ne!(sk, neg);
1932+
let back_sk = neg.negate();
1933+
assert_eq!(sk, back_sk);
1934+
1935+
let neg = pk.negate(&s);
1936+
assert_ne!(pk, neg);
1937+
let back_pk = neg.negate(&s);
1938+
assert_eq!(pk, back_pk);
1939+
1940+
assert_eq!(PublicKey::from_secret_key(&s, &back_sk), pk);
19301941
}
19311942

19321943
#[test]

0 commit comments

Comments
 (0)
Please sign in to comment.