feat: Implement BLAKE2X XOF (Blake2Xb and Blake2Xs) #704
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 introduces an implementation of Blake2X – inspired from, but greatly extends on #677. This contributes to #1.
This is a complete implementation of the Blake2X extensible-output function (XOF) for both its 64-bit (
Blake2Xb
) and 32-bit (Blake2Xs
) variants. This new functionality is gated behind theblake2x
cargo feature flag.1. Implementation and Design
The implementation follows the design principles of the existing crate, utilizing a macro-driven approach to provide generic logic for both Blake2Xb and Blake2Xs, which minimizes code duplication.
Core Logic (
macros.rs
): A new macro,blake2x_impl!
, has been introduced.Algorithm (
blake2x.rs
): The implementation follows the two-phase process specified by the Blake2X algorithm:H₀
) using the underlyingBlake2bVarCore
orBlake2sVarCore
. Crucially, it incorporates the total desired output length (xof_len
) into the parameter block during this phase. This ensures that outputs of different lengths are cryptographically distinct, a key security feature of Blake2X.finalize_xof()
method returns aReader
struct. This reader generates the final hash output incrementally. For each block of output requested, it computes a new hash by feeding the root hashH₀
into the base BLAKE2 function, but with a unique parameter block for each "expansion node" (differentiated by an incrementingnode_offset
). This logic is encapsulated in theexpand_node
helper function.Public API: New public-facing structs
Blake2xb
,Blake2xs
, and their correspondingReader
types are exposed.2. Testing and Validation
The implementation is supported by a comprehensive test suite in
tests/blake2x.rs
that validates correctness through two primary resources: official test vectors and a reference implementation.Test Vectors:
tests/data/
directory now includesblake2xb-kat.json
andblake2xs-kat.json
. These are the test vectors sourced directly from the BLAKE2 RFC repository.Reference Implementation (
b2rs
):b2rs
crate as a reference implementation. This crate is maintained on GitHub by Jean-Philippe Aumasson (@veorq), one of the original authors of the BLAKE2 algorithm.b2rs
in two ways:b2rs
.b2rs
to verify intermediate values of the Blake2X computation, such as the value of the root hash (H₀
) and the output of the first expansion node. This confirms that our internal parameter block construction and hashing logic are correct, not just the final result.All tests, including functional checks for progressive reads and constructor behavior, are passing.
Edit: the last 2 commits of the PR respectively fix a typo-detection CI false positive, and unrelated clippy warnings.