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>
This commit is contained in:
Dr Maxim Orlovsky 2021-06-29 13:19:18 +02:00 committed by Tobin C. Harding
parent 739660499b
commit 13af51926a
2 changed files with 18 additions and 13 deletions

View File

@ -379,12 +379,6 @@ extern "C" {
tweak: *const c_uchar) tweak: *const c_uchar)
-> c_int; -> c_int;
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_5_0_ec_pubkey_cmp")]
pub fn secp256k1_ec_pubkey_cmp(cx: *const Context,
pubkey1: *const PublicKey,
pubkey2: *const PublicKey)
-> c_int;
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_5_0_keypair_sec")] #[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_5_0_keypair_sec")]
pub fn secp256k1_keypair_sec(cx: *const Context, pub fn secp256k1_keypair_sec(cx: *const Context,
output_seckey: *mut c_uchar, output_seckey: *mut c_uchar,
@ -396,12 +390,6 @@ extern "C" {
output_pubkey: *mut PublicKey, output_pubkey: *mut PublicKey,
keypair: *const KeyPair) keypair: *const KeyPair)
-> c_int; -> c_int;
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_5_0_xonly_pubkey_cmp")]
pub fn secp256k1_xonly_pubkey_cmp(cx: *const Context,
pubkey1: *const XOnlyPublicKey,
pubkey2: *const XOnlyPublicKey)
-> c_int;
} }
#[cfg(not(fuzzing))] #[cfg(not(fuzzing))]
@ -446,6 +434,12 @@ extern "C" {
pk: *mut PublicKey) -> c_int; pk: *mut PublicKey) -> c_int;
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_5_0_ec_pubkey_cmp")]
pub fn secp256k1_ec_pubkey_cmp(cx: *const Context,
pubkey1: *const PublicKey,
pubkey2: *const PublicKey)
-> c_int;
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_5_0_ec_pubkey_tweak_add")] #[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_5_0_ec_pubkey_tweak_add")]
pub fn secp256k1_ec_pubkey_tweak_add(cx: *const Context, pub fn secp256k1_ec_pubkey_tweak_add(cx: *const Context,
pk: *mut PublicKey, pk: *mut PublicKey,
@ -552,6 +546,13 @@ extern "C" {
pubkey: *const PublicKey, pubkey: *const PublicKey,
) -> c_int; ) -> c_int;
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_5_0_xonly_pubkey_cmp")]
pub fn secp256k1_xonly_pubkey_cmp(
cx: *const Context,
pubkey1: *const XOnlyPublicKey,
pubkey2: *const XOnlyPublicKey
) -> c_int;
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_5_0_xonly_pubkey_tweak_add")] #[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_5_0_xonly_pubkey_tweak_add")]
pub fn secp256k1_xonly_pubkey_tweak_add( pub fn secp256k1_xonly_pubkey_tweak_add(
cx: *const Context, cx: *const Context,

View File

@ -82,6 +82,7 @@ pub const ONE_KEY: SecretKey = SecretKey([0, 0, 0, 0, 0, 0, 0, 0,
/// # } /// # }
/// ``` /// ```
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
#[cfg_attr(feature = "fuzzing", derive(PartialOrd, Ord))]
#[repr(transparent)] #[repr(transparent)]
pub struct PublicKey(ffi::PublicKey); pub struct PublicKey(ffi::PublicKey);
@ -671,12 +672,14 @@ impl<'de> serde::Deserialize<'de> for PublicKey {
} }
} }
#[cfg(not(fuzzing))]
impl PartialOrd for PublicKey { impl PartialOrd for PublicKey {
fn partial_cmp(&self, other: &PublicKey) -> Option<core::cmp::Ordering> { fn partial_cmp(&self, other: &PublicKey) -> Option<core::cmp::Ordering> {
Some(self.cmp(other)) Some(self.cmp(other))
} }
} }
#[cfg(not(fuzzing))]
impl Ord for PublicKey { impl Ord for PublicKey {
fn cmp(&self, other: &PublicKey) -> core::cmp::Ordering { fn cmp(&self, other: &PublicKey) -> core::cmp::Ordering {
let ret = unsafe { let ret = unsafe {
@ -1899,6 +1902,7 @@ mod test {
assert_eq!(Ok(sksum), sum1); assert_eq!(Ok(sksum), sum1);
} }
#[cfg(not(fuzzing))]
#[test] #[test]
fn pubkey_equal() { fn pubkey_equal() {
let pk1 = PublicKey::from_slice( let pk1 = PublicKey::from_slice(
@ -1909,7 +1913,7 @@ mod test {
&hex!("02e6642fd69bd211f93f7f1f36ca51a26a5290eb2dd1b0d8279a87bb0d480c8443"), &hex!("02e6642fd69bd211f93f7f1f36ca51a26a5290eb2dd1b0d8279a87bb0d480c8443"),
).unwrap(); ).unwrap();
assert!(pk1 == pk2); assert_eq!(pk1, pk2);
assert!(pk1 <= pk2); assert!(pk1 <= pk2);
assert!(pk2 <= pk1); assert!(pk2 <= pk1);
assert!(!(pk2 < pk1)); assert!(!(pk2 < pk1));