Skip to content

clean up Merkle Root types; update bitcoin_hashes to 1.1 - #373

Open
apoelstra wants to merge 7 commits into
BlockstreamResearch:masterfrom
apoelstra:2026-07/hashes-1.1
Open

clean up Merkle Root types; update bitcoin_hashes to 1.1#373
apoelstra wants to merge 7 commits into
BlockstreamResearch:masterfrom
apoelstra:2026-07/hashes-1.1

Conversation

@apoelstra

Copy link
Copy Markdown
Collaborator

We had a bunch of type confusion in Cmr, Amr, etc., where we'd represent our merkle roots as sha256::Midstates (which is okay, but will stop working with bitcoin_hashes 1.0 which adds a length counter to the midstate types) and then represent our IVs as the mr type (not okay, and required a bunch of macro hackery to make work).

We want the new bitcoin_hashes 1.0 midstate type, which aside from being more compatible with the upcoming rust-elements release, will let us convert midstates and engines directly into Ctx8 Simplicity values. So move to that, and clean up the existing types along the way.

@apoelstra
apoelstra force-pushed the 2026-07/hashes-1.1 branch from 7a74ffa to 7884f97 Compare July 27, 2026 14:37
… master)

No version bump of simplicity-sys; there are no API-visible changes.
@apoelstra
apoelstra force-pushed the 2026-07/hashes-1.1 branch from 7884f97 to d4aa7ac Compare July 27, 2026 15:06
There is a type confusion in all the Merkle root types: we have these
initialization vector constants which conceptually are "midstates" (or
"initial states" or whatever). But for e.g. the Amr IVs we have typed
them as Amrs.

Then in the `impl_midstate_wrapper` macro we add all these methods to
the `Amr` type to do "update" operations which conceptually are only
applicable to midstates. If we instead just type the IVs (which are
private constants anyway) as `Midstate`s, then we can add a private
extension trait `MidstateExt` to get the updating functionality, and
eliminate (most of) the macro and also the type confusion.

I will do the other mr types in the next commit; doing this one
separately because it was the simplest change so it worked as a demo.
Drop all the midstate-manipulation functions, which don't belong on the
various MR types, and make the macro invocation more evocative of the
code that it generates.
In bitcoin_hashes 1.0 the Midstate type is expanded to include the
number of hashed bits as well as the output of the most recent
compression function. It no longer makes sense to use it as a sort of
32-byte array wrapper.
And eliminate some use of hashes::hex, which is not an available module
in hashes 1.0.
@apoelstra
apoelstra force-pushed the 2026-07/hashes-1.1 branch 2 times, most recently from e597400 to b29f61d Compare July 27, 2026 15:40
This update gives us a richer Midstate type, which will be important for
working with Ctx8 Simplicity values.

Unfortunately rust-elements and bitcoin-miniscript are still using
hashes 0.14.x, so there is a type compatibility. Fortunately, we rarely
use "bare hashes" and instead use newtypes everywhere, meaning that it's
easy to work around the type incompatibility by converting to/from byte
arrays.

In fact, we don't even need to do that. Instead we just directly import
the old version of `hashes` from `elements` or `miniscript` in the
places where it's needed. As we update those deps, the imports will
update along with it.
@apoelstra
apoelstra force-pushed the 2026-07/hashes-1.1 branch from b29f61d to 2d1f560 Compare July 27, 2026 15:56

@delta1 delta1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ACK 2d1f560; tested locally

}
impl CTxEnv {
pub fn sighash_all(&self) -> sha256::Hash {
// This is a little bit sketchy, directly interpreting a midstate as a sha256 hash. Bit

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

typo nit

Suggested change
// This is a little bit sketchy, directly interpreting a midstate as a sha256 hash. Bit
// This is a little bit sketchy, directly interpreting a midstate as a sha256 hash. But

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I'm gonna leave this one be since it's an internal comment and the typo doesn't cause confusion. May fix in a followup.

@stringhandler

Copy link
Copy Markdown
Contributor

ACK 2d1f560

@apoelstra

Copy link
Copy Markdown
Collaborator Author

ACK 2d1f560; successfully ran local tests

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