Skip to content

Conversation

@tcharding
Copy link
Member

@tcharding tcharding commented Nov 7, 2022

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.

@apoelstra
Copy link
Member

Patch 1 looks like it removes the as_ptr functions from various FFI types. This seems fine to me, since these were just aliases for as_c_ptr, and we want to encourage users to use the latter (which correctly handles ZSTs).

I don't understand though what this has to do with Patch 2, which (correctly) changes the Eq impl on PublicKey to use the serialization. This seems totally independent.

Finally, the current behavior of Eq is (a) correct, with the vendored version of libsecp currently in this library, and (b) potentially faster since it avoids crossing the FFI boundary which might prevent inlining. So I worry a bit about the performance impact of this change, even though IMO it's an improvement in code quality/safety. cc @TheBlueMatt who may have comments about this.

@tcharding
Copy link
Member Author

I don't understand though what this has to do with Patch 2, which (correctly) changes the Eq impl on PublicKey to use the serialization. This seems totally independent.

You are right, not sure what I was thinking yesterday. I'll split the first patch off into its own PR.

@tcharding tcharding force-pushed the 11-07-pk-manual-eq-hash branch from 20d0381 to 587e838 Compare November 8, 2022 03:51
@tcharding tcharding changed the title Improve public API regarding derives and FFI Manually implement PartialEq, Eq, and Hash for PublicKey Nov 8, 2022
@tcharding tcharding marked this pull request as ready for review November 8, 2022 03:53
`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`.
@tcharding tcharding force-pushed the 11-07-pk-manual-eq-hash branch from 587e838 to 5ccf0c8 Compare November 8, 2022 19:31
Copy link
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 5ccf0c8

@apoelstra
Copy link
Member

I'm gonna go ahead and merge this. If people need high-performance comparisons it's not too difficult to work around this and bypass our Ord impl.

@apoelstra apoelstra merged commit 72918bf into rust-bitcoin:master Nov 12, 2022
@tcharding tcharding deleted the 11-07-pk-manual-eq-hash branch November 16, 2022 02:48
chain-forgexcr45 added a commit to chain-forgexcr45/rust-secp256k1 that referenced this pull request Sep 28, 2025
…, `Eq`, and `Hash` for `PublicKey`

5ccf0c8db7cd63ab80ff9388565e258d4faec4c6 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:
    ACK 5ccf0c8db7cd63ab80ff9388565e258d4faec4c6

Tree-SHA512: 1464308238411d259bb0493dc1eca775ec235036eef10b91f70ef17816174f452d5911ecae3b40434b71f9866be1db54d69e8ed9475a4f2801c07a800aead2b2
william2332-limf added a commit to william2332-limf/rust-secp256k1 that referenced this pull request Oct 2, 2025
…, `Eq`, and `Hash` for `PublicKey`

5ccf0c8db7cd63ab80ff9388565e258d4faec4c6 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:
    ACK 5ccf0c8db7cd63ab80ff9388565e258d4faec4c6

Tree-SHA512: 1464308238411d259bb0493dc1eca775ec235036eef10b91f70ef17816174f452d5911ecae3b40434b71f9866be1db54d69e8ed9475a4f2801c07a800aead2b2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants