Skip to content

<blake3pp/core.hpp>

The sequential core: the digest value type, the incremental hasher in its three modes (plain, keyed, derive_key), extended output, and the one-shot functions. Standard library only; allocates only in the std::string-returning conveniences, and in the message of a broken precondition on its way to being thrown.

digest

struct blake3pp::digest;

A 32-byte BLAKE3 digest: a regular value type with constant-time equality and hex round-tripping. The digest is the first 32 bytes of the hash's extended output stream. It formats with std::format ("{}" prints the lowercase hex) where the standard library provides <format>.

Member functions

to_hex

std::string to_hex() const;

Returns the digest as 64 lowercase hex characters.

to_hex_chars

std::array<char, 65> to_hex_chars() const noexcept;

Returns the digest as 64 lowercase hex characters plus a terminating NUL, without allocating.

matches

bool matches(std::string_view hex) const noexcept;

Verifies a hex string against this digest in one step. The comparison is constant-time, like operator==. Malformed hex counts as no match.

Parameter
hex The hex to check, 64 characters of either case.

Returns true iff hex parses and denotes exactly this digest.

Static member functions

from_hex

static std::optional<digest> from_hex(std::string_view hex) noexcept;

Parses a digest from 64 hex characters of either case.

Parameter
hex Exactly 64 hex characters.

Returns The digest, or std::nullopt if hex has any other shape.

Non-member functions

operator==

bool operator==(const digest &lhs, const digest &rhs) noexcept;

Constant-time equality (matching the Rust reference): the safe default for a value compared in security-sensitive contexts, at negligible cost.

Data members

bytes

std::array<std::byte, digest_size> bytes {};

The digest bytes; zero for a default-constructed digest.

hasher

class blake3pp::hasher;

The incremental BLAKE3 hasher: plain, keyed (MAC/PRF) or key-derivation mode, with a non-destructive finalize family. A fixed-size, trivially relocatable value, allocating nothing on any path that returns. Every finalize form leaves the hasher usable, so a digest can be taken mid-stream and feeding can continue. An instance is not thread-safe; distinct instances are independent. The chaining-value stack is sized for the spec's maximum input of 2^64 bytes.

blake3pp::hasher h;
h.update(header);
h.update(body);                      // std::span<const std::byte> or string_view
blake3pp::digest d = h.finalize();   // non-destructive
auto wide = h.finalize<64>();        // the first 64 bytes of the XOF stream

Member functions

hasher

hasher() noexcept;

A plain-mode hasher on the best variant the running CPU supports.

hasher

explicit hasher(arch a) noexcept;

A plain-mode hasher pinned to a variant.

Parameter
a The variant to run on; an unavailable one falls back to the best available (see selected_arch()).

hasher

explicit hasher(const kernel_ops &kernels);

A hasher running on a kernel table of the caller's (<blake3pp/kernel.hpp>).

Parameter
kernels The table; it must stay alive and unchanged while the hasher, a copy of it or an output_reader from it is in use.

update

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

Absorbs the next bytes of the message.

Parameter
input Any length, including zero.

update

void update(std::string_view input) noexcept;

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

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

update_last

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

Absorbs the rest of the message: the caller states that nothing follows these bytes. update() has to hold the message's final chunk back, because the node that carries BLAKE3's ROOT flag is the final chunk for a one-chunk message and the top parent for any longer one, and only the caller knows which it will be. Told that the message ends here, the final chunk joins a wide batch instead of being compressed a block at a time, which on a message of a few chunks is a large share of its time: 8 KiB messages 2.04 -> 2.83 GiB/s on the AVX2 kernel (Zen 3+, ABBA, best of 5; docs/performance.md, "What was tried").

The statement is not binding: a further update() simply means the message did not end here, and the held subtree goes back on the stack as update() would have left it. The digest is the same either way; what is lost is the batching this call bought.

Parameter
input Any length, including zero.

update_last

void update_last(std::string_view input) noexcept;

Absorbs the rest of the message, given as text (see update_last()).

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

finalize

digest finalize() const noexcept;

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

finalize

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

Extended output: fills out with the first out.size() bytes of the output stream, of which the digest is the first 32.

Parameter
out Any length.

finalize_xof

output_reader finalize_xof() const noexcept;

Extended output as a seekable stream, independent of the hasher afterwards.

finalize

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

Extended output by value: the first N bytes of the output stream. The length is a template argument because it shapes the return type; runtime lengths go through the span overload or finalize_xof().

Parameter
N The number of bytes to return.

reset

void reset() noexcept;

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

count

std::uint64_t count() const noexcept;

Total bytes absorbed since construction or reset, subtrees pushed through push_subtree_cv() included.

selected_arch

arch selected_arch() const noexcept;

The variant this hasher actually runs on (auto_detect resolved).

push_subtree_cv

void push_subtree_cv(std::span<const std::uint32_t, 8> cv, std::uint64_t subtree_chunks);

Expert seam for externally computed subtrees, used by the parallel engine and the I/O pipeline: absorbs the root chaining value of a subtree covering subtree_chunks complete chunks.

Parameter
cv The subtree's root chaining value.
subtree_chunks The number of complete chunks it covers.

key_words

std::span<const std::uint32_t, 8> key_words() const noexcept;

Expert observer pairing with push_subtree_cv(): the key schedule external subtree computation must hash under.

mode_flags

std::uint32_t mode_flags() const noexcept;

Expert observer pairing with push_subtree_cv(): the domain flags external subtree computation must hash under.

Static member functions

keyed

static hasher keyed(std::span<const std::byte, key_size> key, arch a=arch::auto_detect) noexcept;

A hasher in keyed mode: BLAKE3's MAC/PRF, its replacement for HMAC.

Parameter
key Exactly key_size bytes, enforced by the span extent.
a The variant to run on.

keyed

static hasher keyed(std::span<const std::byte, key_size> key, const kernel_ops &kernels);

Keyed mode on a kernel table of the caller's, as for hasher(const kernel_ops&).

Parameter
key Exactly key_size bytes.
kernels The table, with the constructor's lifetime rule.

derive_key

static hasher derive_key(std::string_view context, arch a=arch::auto_detect) noexcept;

A hasher in key-derivation mode: the input is the key material, and the output is a subkey bound to context. The context string should be hardcoded, globally unique and application-specific (see the BLAKE3 spec); it is not a secret.

Parameter
context The domain-separation string.
a The variant to run on.

derive_key

static hasher derive_key(std::string_view context, const kernel_ops &kernels);

Key-derivation mode on a kernel table of the caller's, as for hasher(const kernel_ops&).

Parameter
context The domain-separation string.
kernels The table, with the constructor's lifetime rule.

output_reader

class blake3pp::output_reader;

Streams BLAKE3's extended output (XOF). Obtained from hasher::finalize_xof(). A small value type capturing the root node; copyable, and independent of the hasher afterwards. The stream is seekable in O(1): output block t is one compression with counter t, so positioning to byte 10 GiB costs the same as byte 0. The 32-byte digest is exactly the stream's first 32 bytes.

BLAKE3's output is at most 2^64 - 1 bytes. Bytes past that are unspecified, as in the reference implementations; consecutive fills still continue one stream, however a request is split.

Member functions

fill

void fill(std::span<std::byte> out) noexcept;

Writes the next out.size() bytes of the output stream and advances past them.

Parameter
out Any length (see the class for the end of the stream).

take

template <std::size_t N N>
std::array<std::byte, N> take() noexcept;

Returns the next N bytes of the stream by value and advances past them. The length is a template argument because it shapes the return type; runtime lengths use fill().

Parameter
N The number of bytes to take.

seek

void seek(std::uint64_t byte_offset) noexcept;

Positions the stream at an absolute byte offset, in constant time.

Parameter
byte_offset The offset of the next byte fill() will produce; any value.

position

std::uint64_t position() const noexcept;

The byte offset of the next byte fill() will produce. Past byte 2^64 - 1 it wraps; a debug build asserts instead.

Non-member functions

detail::reader_access

struct detail::reader_access;

Constants

chunk_size

constexpr std::size_t chunk_size = 1024;

The BLAKE3 chunk granularity in bytes; subtree offloading (hasher::push_subtree_cv, <blake3pp/parallel.hpp>) is expressed in units of this.

block_size

constexpr std::size_t block_size = 64;

The compression block in bytes: the granularity of extended output (each block of the XOF stream is one compression with its own counter).

digest_size

constexpr std::size_t digest_size = 32;

The default digest length in bytes; extended output continues past it.

key_size

constexpr std::size_t key_size = 32;

The key length of keyed mode in bytes (hasher::keyed, keyed_hash, hash_file_options::key take exactly this many).

Functions

to_hex

void to_hex(std::span<const std::byte> bytes, std::span<char> out);

Writes the lowercase hex of any byte sequence into a caller's buffer. Neither allocates nor NUL-terminates.

Parameter
bytes The bytes to encode (extended output, a key, a digest).
out Receives 2 * bytes.size() characters; must be that large.

to_hex

std::string to_hex(std::span<const std::byte> bytes);

Returns the lowercase hex of any byte sequence as a string.

Parameter
bytes The bytes to encode.

Returns 2 * bytes.size() lowercase hex characters.

fill

void fill(output_reader &r, std::span<std::byte> out) noexcept;

Free-function spelling of output_reader::fill(), so the sequential and the scheduler-taking form (<blake3pp/parallel.hpp>) read alike: fill(r, out) and fill(r, out, sched).

Parameter
r The reader to advance.
out Receives the next out.size() bytes of r's stream.

hash

digest hash(std::span<const std::byte> input, arch a=arch::auto_detect) noexcept;

One-shot hash of a byte sequence.

Parameter
input Any length.
a The variant to run on, as hasher(arch) takes it: auto_detect, and a variant this CPU lacks, run on the best available.

hash

digest hash(std::string_view input, arch a=arch::auto_detect) noexcept;

One-shot hash of a string's bytes.

Parameter
input The bytes of the string, not including any terminator.
a The variant to run on.

keyed_hash

digest keyed_hash(std::span<const std::byte, key_size> key, std::span<const std::byte> input, arch a=arch::auto_detect) noexcept;

One-shot keyed hash: the MAC/PRF of input under a 32-byte key.

Parameter
key Exactly key_size bytes, enforced by the span extent.
input Any length.
a The variant to run on.

keyed_hash

digest keyed_hash(std::span<const std::byte, key_size> key, std::string_view input, arch a=arch::auto_detect) noexcept;

One-shot keyed hash of a string's bytes.

Parameter
key Exactly key_size bytes.
input The bytes of the string.
a The variant to run on.

derive_key

digest derive_key(std::string_view context, std::span<const std::byte> key_material, arch a=arch::auto_detect) noexcept;

One-shot key derivation: 32 bytes derived from key_material, bound to a hardcoded, application-unique context (see hasher::derive_key).

Parameter
context The domain-separation string; not a secret.
key_material The secret to derive from.
a The variant to run on.

derive_key

digest derive_key(std::string_view context, std::string_view key_material, arch a=arch::auto_detect) noexcept;

One-shot key derivation from a string's bytes.

Parameter
context The domain-separation string; not a secret.
key_material The secret to derive from.
a The variant to run on.