Merge pull request #26 from alekseysidorov/derive-standard-traits

Implement Ord for arrays
This commit is contained in:
Andrew Poelstra 2018-06-01 19:21:10 +00:00 committed by GitHub
commit e3b08c2f5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 3 deletions

View File

@ -1,7 +1,7 @@
[package]
name = "secp256k1"
version = "0.9.0"
version = "0.9.1"
authors = [ "Dawid Ciężarkiewicz <dpc@ucore.info>",
"Andrew Poelstra <apoelstra@wpsoftware.net>" ]
license = "CC0-1.0"

View File

@ -49,10 +49,9 @@ 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, Hash)]
#[derive(Copy, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash)]
pub struct PublicKey(ffi::PublicKey);
#[cfg(any(test, feature = "rand"))]
fn random_32_bytes<R: Rng>(rng: &mut R) -> [u8; 32] {
let mut ret = [0u8; 32];
@ -559,6 +558,31 @@ mod test {
assert_eq!(sum1, sum2);
assert_eq!(sum1.unwrap(), exp_sum);
}
#[test]
fn pubkey_equal() {
let s = Secp256k1::new();
let pk1 = PublicKey::from_slice(
&s,
&hex!("0241cc121c419921942add6db6482fb36243faf83317c866d2a28d8c6d7089f7ba"),
).unwrap();
let pk2 = pk1.clone();
let pk3 = PublicKey::from_slice(
&s,
&hex!("02e6642fd69bd211f93f7f1f36ca51a26a5290eb2dd1b0d8279a87bb0d480c8443"),
).unwrap();
assert!(pk1 == pk2);
assert!(pk1 <= pk2);
assert!(pk2 <= pk1);
assert!(!(pk2 < pk1));
assert!(!(pk1 < pk2));
assert!(pk3 < pk1);
assert!(pk1 > pk3);
assert!(pk3 <= pk1);
assert!(pk1 >= pk3);
}
}

View File

@ -51,6 +51,20 @@ macro_rules! impl_array_newtype {
impl Eq for $thing {}
impl PartialOrd for $thing {
#[inline]
fn partial_cmp(&self, other: &$thing) -> Option<::std::cmp::Ordering> {
self[..].partial_cmp(&other[..])
}
}
impl Ord for $thing {
#[inline]
fn cmp(&self, other: &$thing) -> ::std::cmp::Ordering {
self[..].cmp(&other[..])
}
}
impl Clone for $thing {
#[inline]
fn clone(&self) -> $thing {