Skip to content

feature: implement blake3 hash and keyed hash modes - #641

Merged
brycx merged 42 commits into
orion-rs:masterfrom
jakub-mata:blake3
Aug 3, 2026
Merged

feature: implement blake3 hash and keyed hash modes#641
brycx merged 42 commits into
orion-rs:masterfrom
jakub-mata:blake3

Conversation

@jakub-mata

Copy link
Copy Markdown
Contributor

This PR introduces the BLAKE3 hash function into the /hazardous module, implemented according to the official BLAKE3 specifications.
Features Included:

  • hash mode: standard BLAKE3 hashing.
  • keyed hash mode: Supports message authentication (MAC) use cases using a 256-bit key.
  • extendable output (XOF): Arbitrary output lengths as per the spec.

Note: The "derive key" mode has been omitted from this PR to keep the scope focused on standard hashing. The underlying state machine has been structured so that key derivation can be easily added in a future PR if desired.

Testing includes:

  • unit tests: located at the bottom of their respective source files.
  • known-answer tests: set up to match the existing blake2 tests. The test vectors were sourced from the b3sum repository and cover both standard and keyed hashing across various chunk boundaries.

This initial implementation establishes a single-threaded baseline. Because BLAKE3's tree structure is designed for infinite parallelism, a natural next step will be adding a data-parallelism feature flag to support multi-threaded hashing for large inputs.

Resolves #394

@codecov

codecov Bot commented Jul 19, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.87786% with 27 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.02%. Comparing base (bd3a31a) to head (a0248a2).
⚠️ Report is 10 commits behind head on master.

Files with missing lines Patch % Lines
src/hazardous/hash/blake3/internal.rs 96.14% 11 Missing ⚠️
src/hazardous/hash/blake3/mod.rs 94.57% 7 Missing ⚠️
src/hazardous/hash/blake3/cvstack.rs 95.91% 4 Missing ⚠️
src/hazardous/hash/blake3/state.rs 96.20% 3 Missing ⚠️
tests/hash/blake3_kat.rs 97.29% 1 Missing ⚠️
tests/hash/mod.rs 95.65% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #641      +/-   ##
==========================================
- Coverage   99.14%   99.02%   -0.12%     
==========================================
  Files         108      113       +5     
  Lines       19961    20611     +650     
==========================================
+ Hits        19791    20411     +620     
- Misses        170      200      +30     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@brycx

brycx commented Jul 24, 2026

Copy link
Copy Markdown
Member

clippy / linting warning in a file I have not touched (I'm not sure how that happened)

You were unlucky and hit a new Rust version which introduced a new lint which made the CI fail. So no fault at all on your part. If you rebase on current master, this should be fixed now.

Note: The "derive key" mode has been omitted from this PR to keep the scope focused on standard hashing. The underlying state machine has been structured so that key derivation can be easily added in a future PR if desired.

This initial implementation establishes a single-threaded baseline. Because BLAKE3's tree structure is designed for infinite parallelism, a natural next step will be adding a data-parallelism feature flag to support multi-threaded hashing for large inputs.

This is a perfect approach, thank you for taking this into account!

@brycx brycx left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you a lot for all this hard work, @jakub-mata! I get the feeling you were careful and put a lot of effort into it, which really tells.

Let me know if any of my review comments don't make sense or you believe you have a better approach.

Comment thread tests/test_data/third_party/blake3_test_vectors.json
Comment thread tests/hash/blake3_kat.rs
Comment thread tests/hash/blake3_kat.rs
Comment thread src/hazardous/hash/blake3/mod.rs Outdated
Comment thread src/hazardous/hash/blake3/mod.rs Outdated
Comment thread src/hazardous/hash/blake3/mod.rs
Comment thread src/hazardous/hash/blake3/cvstack.rs Outdated
Comment thread src/hazardous/hash/blake3/mod.rs Outdated
Comment thread src/hazardous/hash/blake3/mod.rs
Comment thread src/hazardous/hash/blake3/mod.rs Outdated
The new version implements the `Drop` trait for various structs, where
zeroization occurs. `Blake3`, which contains the secret key used for a
keyed hash (if applicable). ChunkState and TreeStack also implement it,
as they contain the key, its traces, or internal state.

@brycx brycx left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'll have to think about what to do with the mut self and &mut self a bit more.

The rest is just adding omitted debugs where you've added the zeroization.

Comment thread src/hazardous/hash/blake3/mod.rs
Comment thread src/hazardous/hash/blake3/cvstack.rs
Comment thread src/hazardous/hash/blake3/internal.rs
Comment thread src/hazardous/hash/blake3/internal.rs
Comment thread src/hazardous/hash/blake3/mod.rs Outdated
Comment thread src/hazardous/hash/blake3/mod.rs
The previous solution used a `Mode` struct, which needed to contain
the secret key. However, then it cannot derive the `Clone` trait.
If containing its reference, lifetime specifier are needed, which
muddles the interface.

Instead, the common blake3 state is extracted and included within
specific `Blake3` and `Blake3Keyed` structs which are exposed publicly.
The chaining value (`cv`) in `ChunkState` contains the secret before
the first round is run. Futhermore, it shouldn't be leaked until the
last round is finished.

`TreeStack` may unnecessarily leak information about the output chaining
values which are then compressed. It is ommited in debugs as well.
@jakub-mata

Copy link
Copy Markdown
Contributor Author

I'm willing to change the API of finalize to accept &mut self and change the tests accordingly if the overall API consistency is more important. When you decide, let me know :)

@brycx

brycx commented Aug 2, 2026

Copy link
Copy Markdown
Member

I'm willing to change the API of finalize to accept &mut self and change the tests accordingly if the overall API consistency is more important. When you decide, let me know :)

Thank you for sticking with the PR. I'd like to have it use &mut instead of the consuming one. Again, I see the benefit of the choice clearly, it just going to stick out like a sore thumb when all others are implemented in another way.

Apart from this change, I believe this is ready to be merged. I'll add documentation in a follow-up PR.

@brycx

brycx commented Aug 3, 2026

Copy link
Copy Markdown
Member

A note for historic design rationales: Because BLAKE3 acts as both a XOF, MAC and Hash, the typical Tag-based MAC interface hasn't been applied here. There's no stack-allocatable upper bound on keyed hashes produced and enforcing a Tag seems to lock us out of having an API that can be verified in integration tests in /tests because of that, since KATs also include large input.

I haven't been able to come up with a reasonable solution for this. Should be considered further for the upcoming 0.18.0 release.

While dangerous and not what you'd expect from other modules in hazardous, it is still in that module which also has a warning. The security concern has further been specifically documented in the Security docs section.

@brycx brycx left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for all your hard work on this, @jakub-mata! Really awesome, I really appreciate it - thank you.

@brycx
brycx merged commit 20ba51d into orion-rs:master Aug 3, 2026
26 of 28 checks passed
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.

Add Blake3 support

2 participants