Skip to content

Add BLAKE3 hash with keyed, key derivation and extendable output modes - #47

Open
dipu-bd wants to merge 1 commit into
masterfrom
ccr-3e9bf447-zsxg7p-blake3
Open

dipu-bd wants to merge 1 commit into
masterfrom
ccr-3e9bf447-zsxg7p-blake3

Conversation

@dipu-bd

@dipu-bd dipu-bd commented Nov 29, 2026

Copy link
Copy Markdown
Member

Summary

Adds BLAKE3 with all three of its modes, following Blake2b's API shape:

blake3.convert(data);                         // 32-byte hash
blake3sum('text');                            // hex string
blake3.of(64).convert(data);                  // any output length (XOF)
blake3.mac.by(key32).convert(data);           // keyed hash / MAC
blake3.deriveKey('app 2026 session v1')       // key derivation
    .convert(keyMaterial);
  • Blake3Hash (lib/src/algorithms/blake3.dart) implements the full tree mode: 1024-byte chunks, a chaining-value stack, and root output with an increasing counter for extended output.
    • The compression function holds its state in 16 local variables, with zero allocation per block.
    • Input is buffered lazily: a block or chunk is compressed only once more input arrives, because the final block and chunk need different flags (CHUNK_END, and ROOT when only one chunk exists).
  • One implementation for every platform. BLAKE3 uses only 32-bit words, so it needs no _32bit/_64bit twin. The 64-bit chunk counter is split with division rather than shifts, which keeps it exact on dart2js.
  • Public API (lib/src/blake3.dart):
    • blake3 (const), Blake3, .of(), .mac / Blake3MAC, .deriveKey() and blake3sum()
    • exported from package:hashlib and registered in the (deprecated) registry.
  • Validation: a keyed-mode key must be exactly 32 bytes, and the output length must be at least 1. Anything else throws ArgumentError.
  • README: rows in the hash and MAC tables, plus the feature list.

Test vectors (official, nothing invented)

test/fixures/blake3_vectors.dart is generated from the BLAKE3 team's official test_vectors/test_vectors.json, with its URL and SHA-256 recorded in the header.

  • All 35 input lengths × all 3 modes (hash, keyed_hash, derive_key), checked at the full 131-byte extended output and again at the default 32-byte length, as the file's instructions ask. Lengths run from 0 to 102,400 bytes and cover every block, chunk and tree boundary the file defines.
  • Not tagged vm-only, so the same vectors run on node and dart2wasm.
  • Other tests:
    • streaming in uneven chunk sizes in all three modes, matched against one-shot results at every vector length
    • bind() on a stream, checked against an official vector
    • reset()
    • key-length validation over lengths 0–99
    • output-length validation
    • names and properties
    • MAC sign/verify
  • No comparison test: none of the dev-dependency packages (crypto, hash, pointycastle) implements BLAKE3, so the official vectors are the reference.

Verification

  • dart format --output=none --set-exit-if-changed . makes no changes
  • dart analyze --fatal-infos reports no issues
  • dart test -p vm: all tests pass (113 new)
  • dart test -p node: all tests pass
  • dart test -P wasm: all tests pass
  • the integration smoke test runs
  • bash scripts/coverage.sh reports 100% of lib/ covered (6163/6163 lines)

Benchmark (benchmark/blake3.dart, compiled exe, cloud container, not the reference machine). BLAKE2s and SHA-256 are hashlib's own, for context:

Input BLAKE3 BLAKE2s SHA-256
5 MB 1.04 Gbps 949 Mbps 507 Mbps
1 KB 1.25 Gbps 1.06 Gbps 593 Mbps
10 B 102 Mbps 158 Mbps 71 Mbps

This is a scalar implementation with no SIMD or multithreading, so it runs at about BLAKE2s speed. Tiny inputs pay for setting up the chaining-value stack. Not wired into the aggregator; BENCHMARK.md is untouched.

Notes

  • Conflict with Add HKDF key derivation function (RFC 5869) #46: this PR and the HKDF PR (Add HKDF key derivation function (RFC 5869) #46) both edit the same "Batteries included" line in the README. Whichever merges second will need that line resolved, keeping both additions.
  • No CHANGELOG entry: the version heading is left to the maintainer. Suggested bullet: "Add BLAKE3 with hashing, keyed hashing (blake3.mac), key derivation (blake3.deriveKey) and extendable output (blake3.of)".

Generated by Claude Code

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

Files with missing lines Coverage Δ
lib/src/algorithms/blake3.dart 100.00% <100.00%> (ø)
lib/src/blake3.dart 100.00% <100.00%> (ø)
lib/src/core/registry.dart 100.00% <100.00%> (ø)

@dipu-bd dipu-bd mentioned this pull request Nov 29, 2026
@dipu-bd

dipu-bd commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Resolves #18

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.

3 participants