Skip to content

Commit

Permalink
Making key comparison non-fuzziable
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky authored and tcharding committed Jun 14, 2022
1 parent 5771a32 commit 9589d0d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
25 changes: 13 additions & 12 deletions secp256k1-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,6 @@ extern "C" {
tweak: *const c_uchar)
-> 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")]
pub fn secp256k1_keypair_sec(cx: *const Context,
output_seckey: *mut c_uchar,
Expand All @@ -396,12 +390,6 @@ extern "C" {
output_pubkey: *mut PublicKey,
keypair: *const KeyPair)
-> 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))]
Expand Down Expand Up @@ -446,6 +434,12 @@ extern "C" {
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")]
pub fn secp256k1_ec_pubkey_tweak_add(cx: *const Context,
pk: *mut PublicKey,
Expand Down Expand Up @@ -552,6 +546,13 @@ extern "C" {
pubkey: *const PublicKey,
) -> 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")]
pub fn secp256k1_xonly_pubkey_tweak_add(
cx: *const Context,
Expand Down
5 changes: 4 additions & 1 deletion src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,12 +671,14 @@ impl<'de> serde::Deserialize<'de> for PublicKey {
}
}

#[cfg(not(fuzzing))]
impl PartialOrd for PublicKey {
fn partial_cmp(&self, other: &PublicKey) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}

#[cfg(not(fuzzing))]
impl Ord for PublicKey {
fn cmp(&self, other: &PublicKey) -> core::cmp::Ordering {
let ret = unsafe {
Expand Down Expand Up @@ -1904,6 +1906,7 @@ mod test {
assert_eq!(Ok(sksum), sum1);
}

#[cfg(not(fuzzing))]
#[test]
fn pubkey_equal() {
let pk1 = PublicKey::from_slice(
Expand All @@ -1914,7 +1917,7 @@ mod test {
&hex!("02e6642fd69bd211f93f7f1f36ca51a26a5290eb2dd1b0d8279a87bb0d480c8443"),
).unwrap();

assert!(pk1 == pk2);
assert_eq!(pk1, pk2);
assert!(pk1 <= pk2);
assert!(pk2 <= pk1);
assert!(!(pk2 < pk1));
Expand Down

0 comments on commit 9589d0d

Please sign in to comment.