Skip to content

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

Merged
apoelstra merged 5 commits into
BlockstreamResearch:masterfrom
apoelstra:2026-07/sha2ctx-1
Jul 21, 2026
Merged

add value and type constructors for variable-length buffers and sha256 ctx8#372
apoelstra merged 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.
@apoelstra

Copy link
Copy Markdown
Collaborator Author

On c5f250c successfully ran local tests

@apoelstra
apoelstra force-pushed the 2026-07/sha2ctx-1 branch from c5f250c to 93a1c31 Compare July 20, 2026 02:23

@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 93a1c31; tested locally

Left some minor comments, which can be fixed in a follow-up if you like

Comment thread src/types/precomputed.rs Outdated
/// # Panics
///
/// Panics if you request a number `n` greater than or equal to the length
/// of [`Tmr::BUFFER8_TWO_N_PLUS_1`].

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.

Suggested change
/// of [`Tmr::BUFFER8_TWO_N_PLUS_1`].
/// of [`Tmr::BUFFER8_TWO_N_PLUS_ONE`].

Comment thread src/value.rs Outdated
}
}

/// Slice exceeded the maximum capacity of the buffer type in [`Value::buffer8_two_n`].

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.

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`].

Comment thread src/types/precomputed.rs Outdated
}

fn initialize_buffers(write: &mut Option<[Arc<Final>; N_BUFFERS]>) {
// (TWO^8)^<1 = S(TWO^8)

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.

Suggested change
// (TWO^8)^<1 = S(TWO^8)
// (TWO^8)^<2 = S(TWO^8)

Comment thread src/types/final_data.rs
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> {

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.

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
apoelstra force-pushed the 2026-07/sha2ctx-1 branch from 93a1c31 to 9281768 Compare July 20, 2026 14:26
@apoelstra

Copy link
Copy Markdown
Collaborator Author

Addressed all nits.

@apoelstra

Copy link
Copy Markdown
Collaborator Author

On 9281768 successfully ran local tests

@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 9281768

@apoelstra

Copy link
Copy Markdown
Collaborator Author

Can you also take a look at #371 and BlockstreamResearch/simplicity#346 ? Both are small and simple.

@delta1

delta1 commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Hey @apoelstra sure, have just reviewed and tested those

@apoelstra
apoelstra merged commit 3990877 into BlockstreamResearch:master Jul 21, 2026
24 checks passed
@apoelstra
apoelstra deleted the 2026-07/sha2ctx-1 branch July 21, 2026 16:08
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.

2 participants