From b52915df2f39b9d250a7c5ba01dcaada602c51be Mon Sep 17 00:00:00 2001 From: Justin Moon Date: Mon, 9 Nov 2020 11:30:57 -0600 Subject: [PATCH] Implement lexigraphic ordering for PubKey --- src/key.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/key.rs b/src/key.rs index 8d6518590..a5a9cc2a4 100644 --- a/src/key.rs +++ b/src/key.rs @@ -64,7 +64,7 @@ pub const ONE_KEY: SecretKey = SecretKey([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]); /// A Secp256k1 public key, used for verification of signatures -#[derive(Copy, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] pub struct PublicKey(ffi::PublicKey); impl fmt::LowerHex for PublicKey { @@ -470,6 +470,18 @@ 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()) + } +} + +impl Ord for PublicKey { + fn cmp(&self, other: &PublicKey) -> ::core::cmp::Ordering { + self.serialize().cmp(&other.serialize()) + } +} + #[cfg(test)] mod test { use Secp256k1; @@ -860,10 +872,10 @@ mod test { assert!(!(pk2 < pk1)); assert!(!(pk1 < pk2)); - assert!(pk3 < pk1); - assert!(pk1 > pk3); - assert!(pk3 <= pk1); - assert!(pk1 >= pk3); + assert!(pk3 > pk1); + assert!(pk1 < pk3); + assert!(pk3 >= pk1); + assert!(pk1 <= pk3); } #[cfg(feature = "serde")] @@ -901,3 +913,4 @@ mod test { assert_tokens(&pk.readable(), &[Token::BorrowedStr(PK_STR)]); } } +