Skip to content

Commit 02ed061

Browse files
committed
Add assert for the response of ffi negate interface
The interfaces for negate should always returns 1 as mentioned secp256k1.h L574, L563. But in the future it might return 0 if the seckey or pubkey is invalid, but our type system doesn't allow that to ever happen.
1 parent 1742973 commit 02ed061

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/key.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,11 @@ impl SecretKey {
154154
&mut self
155155
) {
156156
unsafe {
157-
ffi::secp256k1_ec_privkey_negate(
157+
let res = ffi::secp256k1_ec_privkey_negate(
158158
ffi::secp256k1_context_no_precomp,
159159
self.as_mut_c_ptr()
160160
);
161+
debug_assert_eq!(res, 1);
161162
}
162163
}
163164

@@ -310,13 +311,10 @@ impl PublicKey {
310311
pub fn negate_assign<C: Verification>(
311312
&mut self,
312313
secp: &Secp256k1<C>
313-
) -> Result<(), Error> {
314+
) {
314315
unsafe {
315-
if ffi::secp256k1_ec_pubkey_negate(secp.ctx, &mut self.0 as *mut _) == 1 {
316-
Ok(())
317-
} else {
318-
Err(Error::InvalidPublicKey)
319-
}
316+
let res = ffi::secp256k1_ec_pubkey_negate(secp.ctx, &mut self.0 as *mut _);
317+
debug_assert_eq!(res, 1);
320318
}
321319
}
322320

@@ -792,11 +790,11 @@ mod test {
792790

793791
assert_eq!(PublicKey::from_secret_key(&s, &sk), pk);
794792
sk.negate_assign();
795-
assert!(pk.negate_assign(&s).is_ok());
793+
pk.negate_assign(&s);
796794
assert_ne!(original_sk, sk);
797795
assert_ne!(original_pk, pk);
798796
sk.negate_assign();
799-
assert!(pk.negate_assign(&s).is_ok());
797+
pk.negate_assign(&s);
800798
assert_eq!(original_sk, sk);
801799
assert_eq!(original_pk, pk);
802800
assert_eq!(PublicKey::from_secret_key(&s, &sk), pk);

0 commit comments

Comments
 (0)