add value and type constructors for variable-length buffers and sha256 ctx8#372
Merged
apoelstra merged 5 commits intoJul 21, 2026
Merged
Conversation
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.
Collaborator
Author
|
On c5f250c successfully ran local tests |
apoelstra
force-pushed
the
2026-07/sha2ctx-1
branch
from
July 20, 2026 02:23
c5f250c to
93a1c31
Compare
delta1
approved these changes
Jul 20, 2026
| /// # Panics | ||
| /// | ||
| /// Panics if you request a number `n` greater than or equal to the length | ||
| /// of [`Tmr::BUFFER8_TWO_N_PLUS_1`]. |
Contributor
There was a problem hiding this comment.
Suggested change
| /// of [`Tmr::BUFFER8_TWO_N_PLUS_1`]. | |
| /// of [`Tmr::BUFFER8_TWO_N_PLUS_ONE`]. |
| } | ||
| } | ||
|
|
||
| /// Slice exceeded the maximum capacity of the buffer type in [`Value::buffer8_two_n`]. |
Contributor
There was a problem hiding this comment.
Suggested change
| /// Slice exceeded the maximum capacity of the buffer type in [`Value::buffer8_two_n`]. | |
| /// Slice exceeded the maximum capacity of the buffer type in [`Value::buffer8_two_n_plus_one`]. |
| } | ||
|
|
||
| fn initialize_buffers(write: &mut Option<[Arc<Final>; N_BUFFERS]>) { | ||
| // (TWO^8)^<1 = S(TWO^8) |
Contributor
There was a problem hiding this comment.
Suggested change
| // (TWO^8)^<1 = S(TWO^8) | |
| // (TWO^8)^<2 = S(TWO^8) |
| pub fn two_two_n(n: usize) -> Arc<Self> { | ||
| super::precomputed::nth_power_of_2(n) | ||
| #[inline] | ||
| pub fn two_two_n(n: usize) -> Result<Arc<Self>, TypeTooLargeError> { |
Contributor
There was a problem hiding this comment.
Maybe a changelog note about this public signature change?
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.
apoelstra
force-pushed
the
2026-07/sha2ctx-1
branch
from
July 20, 2026 14:26
93a1c31 to
9281768
Compare
Collaborator
Author
|
Addressed all nits. |
Collaborator
Author
|
On 9281768 successfully ran local tests |
delta1
approved these changes
Jul 20, 2026
Collaborator
Author
|
Can you also take a look at #371 and BlockstreamResearch/simplicity#346 ? Both are small and simple. |
Contributor
|
Hey @apoelstra sure, have just reviewed and tested those |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Ctx8itself.A later PR will implement conversions between these objects and Rust
sha256::Engineobjects, but this may require an update to bitcoin-hashes 1.0 so I am still working out how to do that.