Skip to content

<blake3pp/parallel.hpp>

Parallel BLAKE3 over the sender/receiver model. BLAKE3's binary Merkle tree makes the parallel decomposition exact rather than heuristic. Any power-of-2, position-aligned run of chunks reduces to one chaining value independently of everything else, so the digest does not depend on how the work was divided.

The engine partitions the input into equal such subtrees. The scheduler's execution agents pull them from a shared counter until none are left, which means a slow agent takes fewer instead of holding up the join. Their chaining values are folded lanes-wide in groups inside the parallel phase, one CV per window reaches the hasher's CV-stack discipline, and the tail finishes sequentially.

The provider is a build-time choice, BLAKE3PP_EXECUTION_PROVIDER: std::execution where the standard library ships it, beman.execution as the conformance-first polyfill, NVIDIA stdexec as the performance workhorse. The same source compiles against all three, as with the simd providers.

What this allocates: the window pool, each window carrying its part table (stack_budget bytes, 32 KiB by default) and the chaining values its groups fold into, the reducer's nodes and the operation cells, all taken once when a run is planned and freed with it. One table per window grows later, on the first window of a shape that needs it: the lane slots a window's parts leave their kernel batches in, n_parts * width chaining values, up to 512 KiB at the default budget on a 16-lane kernel. The part table is a member of a window rather than a frame of the caller, which is what stack_budget bounds -- the agents' own frames are a constant of the build (BLAKE3PP_MAX_SIMD_DEGREE and BLAKE3PP_SUBTREE_FOLD; see docs/freestanding.md).

parallel_hasher

class blake3pp::parallel_hasher;

The incremental counterpart of the multi-core hash(). It offers the same update(), finalize() and reset() interface as hasher, and fans the subtree hashing out over a scheduler internally.

What a caller can rely on: - The digest equals the sequential one. - All alignment and final-chunk discipline lives here, not with the caller. - The window buffer is allocated at construction. Each fan-out also allocates the pipeline's window objects and reducer storage for that call. - finalize() is non-destructive, like hasher's. - A moved-from hasher is in the state reset() leaves; its next update() allocates the buffer again. - Not thread-safe. The scheduler's workers are used only inside update() and the finalize forms.

Input accumulates into an aligned window, and a full window is fanned out as soon as one more byte arrives. That is the "one byte in reserve" which keeps BLAKE3's final chunk with the hasher for ROOT finalization.

blake3pp::parallel_hasher ph{blake3pp::get_parallel_scheduler()};
while (auto block = source.next_block()) {
  ph.update(*block);
}
blake3pp::digest d = ph.finalize();   // == the sequential digest
Parameter
Scheduler Any std::execution-style scheduler, held by value.
Budget What a window's part table may take; see stack_budget.

Member functions

parallel_hasher

explicit parallel_hasher(Scheduler sched, const parallel_hasher_options &opts={});

Plain mode.

Parameter
sched Where the subtree reductions run.
opts The variant and the window size.

parallel_hasher

parallel_hasher(Scheduler sched, std::span<const std::byte, key_size> key, const parallel_hasher_options &opts={});

Keyed (MAC/PRF) mode.

Parameter
sched Where the subtree reductions run.
key Exactly key_size bytes, enforced by the span extent.
opts The variant and the window size.

parallel_hasher

parallel_hasher(Scheduler sched, std::string_view context, const parallel_hasher_options &opts={});

Key-derivation mode: the input is the key material, domain-separated by context (see hasher::derive_key).

Parameter
sched Where the subtree reductions run.
context The domain-separation string; not a secret.
opts The variant and the window size.

parallel_hasher

parallel_hasher(const parallel_hasher &)=default;

Copies the message so far, with its mode, key, variant and window.

operator=

parallel_hasher & operator=(const parallel_hasher &)=default;

Copies the message so far, with its mode, key, variant and window.

parallel_hasher

parallel_hasher(parallel_hasher &&other) noexcept;

Takes the message, mode, key, variant and window; other is left as reset() leaves it.

operator=

parallel_hasher & operator=(parallel_hasher &&other) noexcept;

Takes the message, mode, key, variant and window; other is left as reset() leaves it.

update

void update(std::span<const std::byte> input);

Absorbs the next bytes of the message; complete windows are fanned out over the scheduler from here. Whole windows of input are hashed where they lie, so the cost in copying is bounded: what finishes a window a previous call left part-built, which is under one window, and the tail held back for the next call, which is at most one. The buffer that holds them is one window, never more.

Parameter
input Any length, including zero.

update

void update(std::string_view input);

Absorbs the next bytes of the message, given as text.

Parameter
input The bytes of the string, not including any terminator.

finalize

digest finalize() const;

The digest of everything absorbed so far; the hasher stays usable.

finalize

void finalize(std::span<std::byte> out) const;

Extended output: fills out with the first out.size() bytes of the output stream.

Parameter
out Any length.

finalize

template <std::size_t N N>
std::array<std::byte, N> finalize() const;

Extended output by value: the first N bytes of the output stream.

Parameter
N The number of bytes to return.

finalize_xof

output_reader finalize_xof() const;

Extended output as a seekable stream, independent of this parallel_hasher afterwards.

reset

void reset() noexcept;

Returns the hasher to its just-constructed state, keeping its mode, key, variant and window.

count

std::uint64_t count() const noexcept;

Total bytes absorbed since construction or reset.

parallel_hasher_options

struct blake3pp::parallel_hasher_options;

parallel_hasher's knobs.

Data members

a

arch a = arch::auto_detect;

The SIMD variant of the internal hasher.

window_bytes

std::size_t window_bytes = detail::default_window_bytes;

Bytes accumulated before a window is fanned out; rounded by detail::rounded_window_bytes to a power of two in [64 KiB, 1 GiB] on a 64-bit target, and below a lower cap on a 32-bit one, where max_queue_depth windows have to fit in size_t. Buffered input below one window hashes sequentially at finalize().

Constants

default_fill_segment_bytes

constexpr std::size_t default_fill_segment_bytes = 4 * 1024 * 1024;

The segment a parallel XOF fill splits its request into by default, and the granularity a generator writing that stream out naturally works in.

Functions

hash

template <stack_budget Budget Budget default_stack_budget, class Scheduler>
digest hash(std::span<const std::byte> input, Scheduler &&sched, const kernel_ops &kernels);

Multi-core hash on a kernel table of the caller's (<blake3pp/kernel.hpp>), as hasher(const kernel_ops&) takes one.

Parameter
Budget What its part table may take; see stack_budget.
Scheduler Any std::execution-style scheduler.
input Any length.
sched Where the subtree reductions run.
kernels The table; it must outlive the call.

hash

template <stack_budget Budget Budget default_stack_budget, class Scheduler>
digest hash(std::span<const std::byte> input, Scheduler &&sched, arch a=arch::auto_detect);

Multi-core one-shot hash: the subtree reductions of input run on sched, and the digest is identical to the sequential hash(input). Any std::execution-style scheduler works; inputs too small for parallelism to pay for itself take the sequential path.

auto sched = blake3pp::get_parallel_scheduler();
blake3pp::digest d = blake3pp::hash(big_buffer, sched);
Parameter
Budget What its part table may take; see stack_budget.
Scheduler Any std::execution-style scheduler.
input Any length.
sched Where the subtree reductions run.
a The variant to run on.

hash

template <stack_budget Budget Budget default_stack_budget, class Scheduler>
digest hash(std::string_view input, Scheduler &&sched, arch a=arch::auto_detect);

Multi-core one-shot hash of a string's bytes.

Parameter
Budget What its part table may take; see stack_budget.
Scheduler Any std::execution-style scheduler.
input The bytes of the string.
sched Where the subtree reductions run.
a The variant to run on.

keyed_hash

template <stack_budget Budget Budget default_stack_budget, class Scheduler>
digest keyed_hash(std::span<const std::byte, key_size> key, std::span<const std::byte> input, Scheduler &&sched, arch a=arch::auto_detect);

Multi-core keyed one-shot: the MAC/PRF of input under a 32-byte key, same decomposition as hash().

Parameter
Budget What its part table may take; see stack_budget.
Scheduler Any std::execution-style scheduler.
key Exactly key_size bytes, enforced by the span extent.
input Any length.
sched Where the subtree reductions run.
a The variant to run on.

keyed_hash

template <stack_budget Budget Budget default_stack_budget, class Scheduler>
digest keyed_hash(std::span<const std::byte, key_size> key, std::string_view input, Scheduler &&sched, arch a=arch::auto_detect);

Multi-core keyed one-shot of a string's bytes.

Parameter
Budget What its part table may take; see stack_budget.
Scheduler Any std::execution-style scheduler.
key Exactly key_size bytes.
input The bytes of the string.
sched Where the subtree reductions run.
a The variant to run on.

derive_key

template <stack_budget Budget Budget default_stack_budget, class Scheduler>
digest derive_key(std::string_view context, std::span<const std::byte> key_material, Scheduler &&sched, arch a=arch::auto_detect);

Multi-core key derivation, for key material large enough to matter (a file's worth of entropy, a whole seed image); see core.hpp's derive_key() for the context contract.

Parameter
Budget What its part table may take; see stack_budget.
Scheduler Any std::execution-style scheduler.
context The domain-separation string; not a secret.
key_material The secret to derive from.
sched Where the subtree reductions run.
a The variant to run on.

derive_key

template <stack_budget Budget Budget default_stack_budget, class Scheduler>
digest derive_key(std::string_view context, std::string_view key_material, Scheduler &&sched, arch a=arch::auto_detect);

Multi-core key derivation from a string's bytes.

Parameter
Budget What its part table may take; see stack_budget.
Scheduler Any std::execution-style scheduler.
context The domain-separation string; not a secret.
key_material The secret to derive from.
sched Where the subtree reductions run.
a The variant to run on.

fill

template <class Scheduler>
void fill(output_reader &r, std::span<std::byte> out, Scheduler &&sched, std::size_t segment_bytes=default_fill_segment_bytes);

Fills a buffer from an output reader on every core: the request is split into segments, and each task copies the reader, seeks its own segment and fills it straight into the caller's buffer. Extended output is seekable in O(1), which makes this exact. r advances past out afterwards exactly as r.fill(out) would have. Requests of one segment or less take the sequential path.

Parameter
Scheduler Any std::execution-style scheduler.
r The reader to advance.
out Receives the next out.size() bytes of r's stream.
sched Where the segments are filled.
segment_bytes Bytes per task; rounded down to a multiple of block_size, so every task starts on the wide path when r.position() is a multiple of block_size. Defaults to default_fill_segment_bytes.