Merge rust-bitcoin/rust-secp256k1#506: Manually implement `PartialEq`, `Eq`, and `Hash` for `PublicKey`
5ccf0c8db7
Manually implement PartialEq, Eq, and Hash for PublicKey (Tobin C. Harding) Pull request description: `PartialEq` and `Eq` should agree with `PartialOrd` and `Ord` but we are deriving `PartialEq`/`Eq` and doing a custom implementation of `PartialOrd` and `Ord` (that calls down to ffi functions). If two keys are equal their hashes should be equal so, we should add a custom implementation of `Hash` also. In order to guarantee the digest will be the same across library versions first serialize the key before hashing it. Add custom implementation of `PartialEq`, `Eq`, and `Hash` when not fuzzing. Please note, this is for the main `PublicKey` type, the patch does not effect the `ffi::PublicKey`, nor do we call methods on the `ffi::PublicKey`. EDIT: Please note the comment below by apoelstra about the possible performance hit introduced by this PR. ACKs for top commit: apoelstra: ACK5ccf0c8db7
Tree-SHA512: 1464308238411d259bb0493dc1eca775ec235036eef10b91f70ef17816174f452d5911ecae3b40434b71f9866be1db54d69e8ed9475a4f2801c07a800aead2b2
This commit is contained in:
commit
72918bf5f7
22
src/key.rs
22
src/key.rs
|
@ -100,8 +100,8 @@ pub const ONE_KEY: SecretKey = SecretKey(constants::ONE);
|
|||
/// ```
|
||||
/// [`bincode`]: https://docs.rs/bincode
|
||||
/// [`cbor`]: https://docs.rs/cbor
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
|
||||
#[cfg_attr(fuzzing, derive(PartialOrd, Ord))]
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
#[cfg_attr(fuzzing, derive(PartialOrd, Ord, PartialEq, Eq, Hash))]
|
||||
#[repr(transparent)]
|
||||
pub struct PublicKey(ffi::PublicKey);
|
||||
|
||||
|
@ -814,6 +814,24 @@ impl Ord for PublicKey {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(not(fuzzing))]
|
||||
impl PartialEq for PublicKey {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.cmp(other) == core::cmp::Ordering::Equal
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(fuzzing))]
|
||||
impl Eq for PublicKey {}
|
||||
|
||||
#[cfg(not(fuzzing))]
|
||||
impl core::hash::Hash for PublicKey {
|
||||
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
|
||||
let ser = self.serialize();
|
||||
ser.hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
/// Opaque data structure that holds a keypair consisting of a secret and a public key.
|
||||
///
|
||||
/// # Serde support
|
||||
|
|
Loading…
Reference in New Issue