Skip to content

Commit 13af519

Browse files
Make key comparison non-fuzzable
Feature guard the custom implementations of `Ord` and `PartialOrd` on `cfg(not(fuzzing))`. When fuzzing, auto-derive implementations. Co-authored-by: Tobin C. Harding <me@tobin.cc>
1 parent 7396604 commit 13af519

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

secp256k1-sys/src/lib.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -379,12 +379,6 @@ extern "C" {
379379
tweak: *const c_uchar)
380380
-> c_int;
381381

382-
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_5_0_ec_pubkey_cmp")]
383-
pub fn secp256k1_ec_pubkey_cmp(cx: *const Context,
384-
pubkey1: *const PublicKey,
385-
pubkey2: *const PublicKey)
386-
-> c_int;
387-
388382
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_5_0_keypair_sec")]
389383
pub fn secp256k1_keypair_sec(cx: *const Context,
390384
output_seckey: *mut c_uchar,
@@ -396,12 +390,6 @@ extern "C" {
396390
output_pubkey: *mut PublicKey,
397391
keypair: *const KeyPair)
398392
-> c_int;
399-
400-
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_5_0_xonly_pubkey_cmp")]
401-
pub fn secp256k1_xonly_pubkey_cmp(cx: *const Context,
402-
pubkey1: *const XOnlyPublicKey,
403-
pubkey2: *const XOnlyPublicKey)
404-
-> c_int;
405393
}
406394

407395
#[cfg(not(fuzzing))]
@@ -446,6 +434,12 @@ extern "C" {
446434
pk: *mut PublicKey) -> c_int;
447435

448436

437+
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_5_0_ec_pubkey_cmp")]
438+
pub fn secp256k1_ec_pubkey_cmp(cx: *const Context,
439+
pubkey1: *const PublicKey,
440+
pubkey2: *const PublicKey)
441+
-> c_int;
442+
449443
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_5_0_ec_pubkey_tweak_add")]
450444
pub fn secp256k1_ec_pubkey_tweak_add(cx: *const Context,
451445
pk: *mut PublicKey,
@@ -552,6 +546,13 @@ extern "C" {
552546
pubkey: *const PublicKey,
553547
) -> c_int;
554548

549+
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_5_0_xonly_pubkey_cmp")]
550+
pub fn secp256k1_xonly_pubkey_cmp(
551+
cx: *const Context,
552+
pubkey1: *const XOnlyPublicKey,
553+
pubkey2: *const XOnlyPublicKey
554+
) -> c_int;
555+
555556
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_5_0_xonly_pubkey_tweak_add")]
556557
pub fn secp256k1_xonly_pubkey_tweak_add(
557558
cx: *const Context,

src/key.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ pub const ONE_KEY: SecretKey = SecretKey([0, 0, 0, 0, 0, 0, 0, 0,
8282
/// # }
8383
/// ```
8484
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
85+
#[cfg_attr(feature = "fuzzing", derive(PartialOrd, Ord))]
8586
#[repr(transparent)]
8687
pub struct PublicKey(ffi::PublicKey);
8788

@@ -671,12 +672,14 @@ impl<'de> serde::Deserialize<'de> for PublicKey {
671672
}
672673
}
673674

675+
#[cfg(not(fuzzing))]
674676
impl PartialOrd for PublicKey {
675677
fn partial_cmp(&self, other: &PublicKey) -> Option<core::cmp::Ordering> {
676678
Some(self.cmp(other))
677679
}
678680
}
679681

682+
#[cfg(not(fuzzing))]
680683
impl Ord for PublicKey {
681684
fn cmp(&self, other: &PublicKey) -> core::cmp::Ordering {
682685
let ret = unsafe {
@@ -1899,6 +1902,7 @@ mod test {
18991902
assert_eq!(Ok(sksum), sum1);
19001903
}
19011904

1905+
#[cfg(not(fuzzing))]
19021906
#[test]
19031907
fn pubkey_equal() {
19041908
let pk1 = PublicKey::from_slice(
@@ -1909,7 +1913,7 @@ mod test {
19091913
&hex!("02e6642fd69bd211f93f7f1f36ca51a26a5290eb2dd1b0d8279a87bb0d480c8443"),
19101914
).unwrap();
19111915

1912-
assert!(pk1 == pk2);
1916+
assert_eq!(pk1, pk2);
19131917
assert!(pk1 <= pk2);
19141918
assert!(pk2 <= pk1);
19151919
assert!(!(pk2 < pk1));

0 commit comments

Comments
 (0)