Skip to content

add value and type constructors for variable-length buffers and sha256 ctx8#372

Open
apoelstra wants to merge 5 commits into
BlockstreamResearch:masterfrom
apoelstra:2026-07/sha2ctx-1
Open

add value and type constructors for variable-length buffers and sha256 ctx8#372
apoelstra wants to merge 5 commits into
BlockstreamResearch:masterfrom
apoelstra:2026-07/sha2ctx-1

Conversation

@apoelstra

Copy link
Copy Markdown
Collaborator

This PR lays the groundwork for constructing and manipulating SHA256 contexts in an ergonomic way. This lets you construct variable-length buffers, and by producting those with a couple fixed-size words you can create a context. It also provides precomputed types for various sizes of buffers and Ctx8 itself.

A later PR will implement conversions between these objects and Rust sha256::Engine objects, but this may require an update to bitcoin-hashes 1.0 so I am still working out how to do that.

The core::array::from_fn has been stable since Rust 1.63. We can use
this to build an array, rather than constructing a vector, converting
it to an arary, and unwrapping the conversion.

This saves an allocation, eliminates a panic path, and (IMHO) makes
the code easier to read.

I need to duplicate this logic for the buffer type in the next commit,
so I figured I should first clean it up.
This function panics if given an n which is out of range. This is
arguably fine but we might as well return a Result, which gives the user
the choice of whether or not to panic.

Because the overwhelming number of calls in this codebase provide a
fixed compile-time constant, also add a Final::two_two_n_fixed method
which takes a complie-time constant and fails to compile if it's out
of range.
Add precomputation tables for both `Tmr` and `Final`. Write a unit test
which does a direct computation of the type and verifies all the precomp
vectors. Also check against the Ctx8 output type of the sha256 init jet.

Don't provide a Final::buffer8_two_n_plus_one_fixed function that avoids
the Result by doing a compile-time check. It seems unlikely that this
will be called with compile-time fixed values, except for the value 5
which is used in the Ctx8 type. But in the next commit we'll
special-case Ctx8.
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.

1 participant