<blake3pp/kernel.hpp>¶
The kernel table: the four compression functions every hash in the library runs on, and the seam for running it on kernels of your own. Each built-in variant (see arch) is one kernel_ops table. A table of your own, filled with your functions or with a built-in table's and a few of yours, runs the whole library on them: the chunk and tree logic, the CV stack, the parallel engine and the file pipeline all call the kernels through the table and nothing else.
#include <blake3pp/kernel.hpp>
// Start from the variant the library would pick, and swap one kernel.
blake3pp::kernel_ops mine = blake3pp::builtin_kernel();
mine.hash_many = &my_hash_many;
// Compare it against the reference before using it.
if (const auto check = blake3pp::check_kernel(mine); !check) {
std::println(stderr, "kernel rejected: {}", check.failure);
}
// <blake3pp/core.hpp>. mine must outlive h and its output readers.
blake3pp::hasher h{mine};
A table is accepted by the hasher constructor, hasher::keyed(), hasher::derive_key() and the multi-core hash() in <blake3pp/parallel.hpp>. Each validates its shape (the shape rule in kernel_ops' documentation) and throws std::invalid_argument for a table that is malformed; whether the functions compute BLAKE3 is check_kernel()'s question, which it answers against the built-in scalar kernel.
Stability: fields are only ever appended, so fill a table with designated initializers or by copying a built-in one, and code that does keeps compiling when a field is added. A change to an existing function's signature is a breaking change.
kernel_check¶
struct blake3pp::kernel_check;
What check_kernel() found.
Member functions¶
operator bool¶
explicit operator bool() const noexcept;
passed.
Data members¶
passed¶
bool passed = true;
Whether every case matched the reference.
failure¶
std::string failure;
The first case that did not, naming the function and its arguments; empty when passed.
kernel_ops¶
struct blake3pp::kernel_ops;
One variant's kernels.
Every argument is flat: byte pointers, 32-bit words, counts. A chaining value (CV) is eight words; a block is 64 bytes (block_size in <blake3pp/core.hpp>), read as sixteen little-endian words. Byte pointers carry no alignment; word pointers point to naturally aligned std::uint32_t. Each function must be safe to call from several threads at once on different arguments, must not throw, and must not keep any pointer it was given after it returns.
The shape rule: a table the library accepts has every function pointer set, a variant other than arch::auto_detect, and a simd_degree that is a power of two from 1 to max_simd_degree(): the tree code groups chunks in powers of two.
Data members¶
variant¶
arch variant;
The variant the table reports itself as, through hasher::selected_arch(): for a table of your own, the variant whose instructions its code needs.
simd_degree¶
std::size_t simd_degree;
How many inputs hash_many processes per step at full utilisation: 1 for a scalar kernel, the SIMD width otherwise; a power of two. The library hashes chunks in calls of up to twice this many inputs.
compress_in_place¶
void(* compress_in_place)(std::uint32_t cv[8], const std::uint8_t block[64], std::uint32_t len, std::uint64_t counter, std::uint32_t flags) noexcept;
The BLAKE3 compression function, keeping the first eight output words: cv becomes the first eight words of compress(cv, block, counter, len, flags).
len is the number of meaningful bytes in block: 1 to 64, or 0 for the one block of the empty input. The bytes past len are zero. counter is the chunk counter, or 0 for a parent node.
compress_xof¶
void(* compress_xof)(const std::uint32_t cv[8], const std::uint8_t block[64], std::uint32_t len, std::uint64_t counter, std::uint32_t flags, std::uint8_t out[64]) noexcept;
The full sixteen-word compression output, as 64 little-endian bytes in out: the extended-output primitive.
Output block t of a stream is this applied to the root node with counter t.
xof_many¶
void(* xof_many)(const std::uint32_t cv[8], const std::uint8_t block[64], std::uint32_t len, std::uint64_t counter, std::uint32_t flags, std::uint8_t *out, std::size_t num_blocks) noexcept;
num_blocks consecutive 64-byte output blocks of one root node, written to out in order: block i is compress_xof() with counter counter + i.
num_blocks may be any count from 1; a SIMD kernel maps output counters to lanes.
hash_many¶
void(* hash_many)(const std::uint8_t *const *inputs, std::size_t num_inputs, std::size_t blocks, const std::uint32_t key[8], std::uint64_t counter, bool increment_counter, std::uint32_t flags, std::uint32_t flags_start, std::uint32_t flags_end, std::uint8_t *out) noexcept;
Hashes num_inputs inputs of blocks full blocks each, writing one 32-byte little-endian CV per input to out, in input order.
Input i starts from key as its CV and compresses its blocks in order with counter counter + i when increment_counter, counter otherwise. Every block gets flags; the first block also gets flags_start and the last flags_end. The library calls it in two shapes:
- whole chunks: blocks = 16, the chunk flags as start and end, the counter stepping;
- parents: one block of two CVs each, kernel_flags::parent in flags, the counter fixed at 0.
A call carries at most 2 * max_simd_degree() inputs.
Constants¶
kernel_iv¶
constexpr std::array<std::uint32_t, 8> kernel_iv = { 0x6A09E667u, 0xBB67AE85u, 0x3C6EF372u, 0xA54FF53Au, 0x510E527Fu, 0x9B05688Cu, 0x1F83D9ABu, 0x5BE0CD19u, };
The BLAKE3 IV: the key words, and so the initial chaining value, of an unkeyed hash. Its first four words are also words 8 to 11 of every compression's state.
chunk_start¶
constexpr std::uint32_t chunk_start = 1u <<0;
The first block of a chunk.
chunk_end¶
constexpr std::uint32_t chunk_end = 1u <<1;
The last block of a chunk.
parent¶
constexpr std::uint32_t parent = 1u <<2;
A parent node: its block is two child chaining values.
root¶
constexpr std::uint32_t root = 1u <<3;
The root node, whose output is the hash or the extended output.
keyed_hash¶
constexpr std::uint32_t keyed_hash = 1u <<4;
Keyed mode, on every node of the tree.
derive_key_context¶
constexpr std::uint32_t derive_key_context = 1u <<5;
The first stage of key derivation: hashing the context string.
derive_key_material¶
constexpr std::uint32_t derive_key_material = 1u <<6;
The second stage of key derivation: hashing the key material.
Functions¶
max_simd_degree¶
std::size_t max_simd_degree() noexcept;
The largest simd_degree this build of the library accepts: it sizes the staging buffers hash_many batches are gathered into, and is fixed when the library is compiled (BLAKE3PP_MAX_SIMD_DEGREE).
builtin_kernel¶
const kernel_ops & builtin_kernel(arch a=arch::auto_detect) noexcept;
A built-in variant's table, the starting point for one that swaps a single kernel.
A variant the running CPU cannot use falls back to the best one it can, as hasher(arch) does, so the result's variant says which table this is. The reference lives as long as the program.
| Parameter | |
|---|---|
a |
The variant; auto_detect is the one the library would pick. |
check_kernel¶
kernel_check check_kernel(const kernel_ops &k);
Runs a table against the built-in scalar kernel and reports the first difference.
It checks the table's shape first, then each function on its own:
- every length 0 to block_size, counters on both sides of the 32-bit boundary, and flag sets in which every flag appears;
- xof_many at every count up to four times simd_degree and three more;
- hash_many at every count up to the call bound of 2 * max_simd_degree() inputs, in both shapes and every mode's base flag;
- hash_many and compress_in_place at every input address offset from 1 to 63.
Then it hashes messages across the chunk, batch and subtree boundaries, plain, keyed and derived (a long context included), with extended output, through a hasher on the table, and compares the bytes with the scalar kernel's.
Passing is evidence, not proof: the cases are the ones the library's own variants are tested on. Every output, chaining values included, is followed by 64 guard bytes, so a write that far past it is caught. A read past an input is not detected, and a table that reads or writes further out of bounds can crash the call instead of failing it; a sanitizer build reports both. The call hashes some tens of megabytes, so it belongs at setup or in a test, not on a hot path.
| Parameter | |
|---|---|
k |
The table to check; it is used only during the call. |
Returns passed, or the first failure.