Skip to content

Remove midstate from the GeneralHash and HashEngine traits#3009

Merged
apoelstra merged 1 commit intorust-bitcoin:masterfrom
tcharding:07-10-midstate
Jul 14, 2024
Merged

Remove midstate from the GeneralHash and HashEngine traits#3009
apoelstra merged 1 commit intorust-bitcoin:masterfrom
tcharding:07-10-midstate

Conversation

@tcharding
Copy link
Copy Markdown
Member

@tcharding tcharding commented Jul 11, 2024

Midstates are not generic objects; they don't have universal cryptographic properties and if you are using them you should be using a specific midstate type. Therefore it shouldn't be part of GeneralHash or HashEngine. Furthermore, in practice it seems like sha2 midstates are the only ones that anybody uses, at least in bitcoin.

Remove the midstate stuff from the GeneralHash and HashEngine traits. Keep the midstate functionality as inherent functions if it is used internally. Keep the functionality on sha256 as inherent public functions.

Done as a first step towards #2918.

@github-actions github-actions bot added the C-hashes PRs modifying the hashes crate label Jul 11, 2024
@tcharding
Copy link
Copy Markdown
Member Author

The midstate functionality is only public for creating tagged hashes, there is no reason that it is part of the Hash trait.

@apoelstra please confirm claim is correct.

@Kixunil
Copy link
Copy Markdown
Collaborator

Kixunil commented Jul 11, 2024

Concept ACK, will wait for @apoelstra to confirm that removing midstate from everything that's not SHA256 is desirable.

@apoelstra
Copy link
Copy Markdown
Member

It's absolutely not "just for creating tagged hashes".

But I haven't seen midstates of non-sha256 functions used anywhere in practice, so I think we should delete them for now until somebody complains, and then we can put them back.

@Kixunil
Copy link
Copy Markdown
Collaborator

Kixunil commented Jul 11, 2024

It's absolutely not "just for creating tagged hashes".

It'd be nice to know the other use cases better as they can inform us on API decisions.

@apoelstra
Copy link
Copy Markdown
Member

apoelstra commented Jul 11, 2024

One is in Bitcoin mining -- you want to store the progress of your hash computation so that you can iterate on different continuations rather than restarting every time (this is "ASICboost"). Another is "fast merkle roots" as implemented in Elements consensus logic. (I think this might appear in Bitcoin Core as well, but it's not in consensus anywhere.)

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 8f491a81481d44d6f9679fcc75981f1cf0af1ad7

@tcharding
Copy link
Copy Markdown
Member Author

It's absolutely not "just for creating tagged hashes".

Thanks for the explanation, so this should be "is only useful in the bitcoin ecosystem for sha256 hashes and shouldn't be part of the GeneralHash trait", right?

@apoelstra
Copy link
Copy Markdown
Member

Yeah. I think a clearer description is maybe "midstates are not generic objects; they don't have universal cryptographic properties and if you are using them you should be using a specific midstate type. Therefore it shouldn't be part of GeneralHash or HashEngine. Furthermore, in practice it seems like sha2 midstates are the only ones that anybody uses, at least in bitcoin."

Midstates are not generic objects; they don't have universal
cryptographic properties and if you are using them you should be using a
specific midstate type. Therefore it shouldn't be part of `GeneralHash` or
`HashEngine`. Furthermore, in practice it seems like `sha2` midstates are
the only ones that anybody uses, at least in bitcoin.

Remove the midstate stuff from the `GeneralHash` and `HashEngine`
traits. Keep the `midstate` functionality as inherent functions if it is
used internally. Keep the functionality on `sha256` as inherent public
functions.
@tcharding tcharding changed the title Remove midstate from the Hash trait Remove midstate from the GeneralHash and HashEngine traits Jul 12, 2024
@tcharding
Copy link
Copy Markdown
Member Author

PR description, title, and commit log updated to use new text. No code changes.

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 8dcecfc

type MidState = Midstate;

/// Outputs the midstate of the hash engine. This function should not be
/// used directly unless you really know what you're doing.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Expand and format the doc, please?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This PR is a move-and-remove-only change. Would it be ok to do this in #3010 instead where we've had more discussion on this point?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Oh, I've confused myself, yeah.

Copy link
Copy Markdown
Collaborator

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

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

ACK 8dcecfc

@tcharding
Copy link
Copy Markdown
Member Author

Can this be merged as is @apoelstra, the semver CI fail is unrelated (#3018).

@apoelstra apoelstra merged commit 25fc7e5 into rust-bitcoin:master Jul 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-hashes PRs modifying the hashes crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants