Merge pull request #248 from justinmoon/pubkey-ordering

Implement lexigraphic ordering for PubKey
This commit is contained in:
Andrew Poelstra 2020-11-26 03:03:17 +00:00 committed by GitHub
commit 9083babbe2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 5 deletions

View File

@ -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]); 0, 0, 0, 0, 0, 0, 0, 1]);
/// A Secp256k1 public key, used for verification of signatures /// 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); pub struct PublicKey(ffi::PublicKey);
impl fmt::LowerHex for 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)] #[cfg(test)]
mod test { mod test {
use Secp256k1; use Secp256k1;
@ -860,10 +872,10 @@ mod test {
assert!(!(pk2 < pk1)); assert!(!(pk2 < pk1));
assert!(!(pk1 < pk2)); assert!(!(pk1 < pk2));
assert!(pk3 < pk1); assert!(pk3 > pk1);
assert!(pk1 > pk3); assert!(pk1 < pk3);
assert!(pk3 <= pk1); assert!(pk3 >= pk1);
assert!(pk1 >= pk3); assert!(pk1 <= pk3);
} }
#[cfg(feature = "serde")] #[cfg(feature = "serde")]