Skip to content

Commit

Permalink
Re-implementing PublicKey ordering using FFI
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 7946794 commit 1629aa9
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,13 +673,21 @@ impl<'de> serde::Deserialize<'de> for PublicKey {

impl PartialOrd for PublicKey {
fn partial_cmp(&self, other: &PublicKey) -> Option<core::cmp::Ordering> {
self.serialize().partial_cmp(&other.serialize())
Some(self.cmp(other))
}
}

impl Ord for PublicKey {
fn cmp(&self, other: &PublicKey) -> core::cmp::Ordering {
self.serialize().cmp(&other.serialize())
let ret = unsafe {
ffi::secp256k1_ec_pubkey_cmp(ffi::secp256k1_context_no_precomp, self.as_c_ptr(), other.as_c_ptr())
};
match ret {
0 => core::cmp::Ordering::Equal,
v if v < 0 => core::cmp::Ordering::Less,
v if v > 0 => core::cmp::Ordering::Greater,
_ => unreachable!()
}
}
}

Expand Down

0 comments on commit 1629aa9

Please sign in to comment.