Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement lexigraphic ordering for PubKey #248

Merged
merged 1 commit into from Nov 26, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/key.rs
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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")]
Expand Down