Skip to content

Derive Hash for Signature#441

Merged
apoelstra merged 1 commit intorust-bitcoin:masterfrom
tcharding:derive-hash
May 9, 2022
Merged

Derive Hash for Signature#441
apoelstra merged 1 commit intorust-bitcoin:masterfrom
tcharding:derive-hash

Conversation

@tcharding
Copy link
Copy Markdown
Member

@tcharding tcharding commented May 6, 2022

In preparation for deriving Hash in miniscript, derive Hash on the ecdsa::Signature.

ref: rust-bitcoin/rust-miniscript#226

In preparation for deriving `Hash` in miniscript, derive `Hash` on the
`ecdsa::Signature`.
@apoelstra
Copy link
Copy Markdown
Member

I wonder what guarantees Hash is supposed to have. I guess, only that it's compatible with Eq...but in particular, this implementation will give different hash values when the library is compiled with a different endianness.

Copy link
Copy Markdown
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 ef7f197

but would like another ack before merging.

@tcharding
Copy link
Copy Markdown
Member Author

Am I understanding you correctly that you think the following workflow is broken?

A: little-endian machine
B: big-endian machine

  1. A: create data structure that includes a signature
  2. A: hash data structure
  3. A: serialize data structure 2
  4. send over wire to B
  5. B: deserialzie data structure 2 (as GOT)
  6. B: create data structure 1 that includes a signature (using same data as A used)
  7. B: hash data structure 1, put hash into data structure 2 (as WANT)
  8. B: Expect GOT == WANT

If the assertion in (8) is not true doesn't that basically break all portability for any data structure that uses Hash and since its std::hash::Hash this would imply that code written with the Rust stdlib is non-portable? That seems like a pretty big deal, I'd like to think that Rust guarantees the assertion in (8)? Or am I being brain dead?

@tcharding
Copy link
Copy Markdown
Member Author

Whatever direction we take, should this PR unify the derives on ecdsa::Signature and schnorr::Signature? The schnorr sig currently has no derives.

@elichai
Copy link
Copy Markdown
Member

elichai commented May 8, 2022

Due to differences in endianness and type sizes, data fed by Hash to a Hasher should not be considered portable across platforms. Additionally the data passed by most standard library types should not be considered stable between compiler versions.

From: https://doc.rust-lang.org/std/hash/trait.Hash.html#portability

Copy link
Copy Markdown
Member

@elichai elichai left a comment

Choose a reason for hiding this comment

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

ACK ef7f197

@tcharding
Copy link
Copy Markdown
Member Author

Wow, thanks for the lesson.

@apoelstra apoelstra merged commit 6599e24 into rust-bitcoin:master May 9, 2022
@tcharding tcharding deleted the derive-hash branch May 11, 2022 22:13
chain-forgexcr45 added a commit to chain-forgexcr45/rust-secp256k1 that referenced this pull request Sep 28, 2025
ef7f1972a7e412e8b7a2e83f1c6eca0beed6559b Derive Hash for Signature (Tobin C. Harding)

Pull request description:

  In preparation for deriving `Hash` in miniscript, derive `Hash` on the `ecdsa::Signature`.

  ref: rust-bitcoin/rust-miniscript#226

ACKs for top commit:
  apoelstra:
    ACK ef7f1972a7e412e8b7a2e83f1c6eca0beed6559b
  elichai:
    ACK ef7f1972a7e412e8b7a2e83f1c6eca0beed6559b

Tree-SHA512: 7313f59971444ae18611adbafe86a09478eddd7357f2b7f3ad3bb1761609b6358b156975086f6c318eb2777018b7b2f44386321108939acbcf2d0a522e7e208e
william2332-limf added a commit to william2332-limf/rust-secp256k1 that referenced this pull request Oct 2, 2025
ef7f1972a7e412e8b7a2e83f1c6eca0beed6559b Derive Hash for Signature (Tobin C. Harding)

Pull request description:

  In preparation for deriving `Hash` in miniscript, derive `Hash` on the `ecdsa::Signature`.

  ref: rust-bitcoin/rust-miniscript#226

ACKs for top commit:
  apoelstra:
    ACK ef7f1972a7e412e8b7a2e83f1c6eca0beed6559b
  elichai:
    ACK ef7f1972a7e412e8b7a2e83f1c6eca0beed6559b

Tree-SHA512: 7313f59971444ae18611adbafe86a09478eddd7357f2b7f3ad3bb1761609b6358b156975086f6c318eb2777018b7b2f44386321108939acbcf2d0a522e7e208e
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.

3 participants