Skip to main content

Module throughput

Module throughput 

Source
Expand description

The throughput API, for power users: every way this crate makes hashing fast (batches, collections, many hashers fed in turns). The program takes on more of the work, so the calls can be fast and simple:

  • Inputs are Blocks: 64 bytes each on a 64-byte boundary, so every message starts a cache line and no block straddles two; the type holds it. A message is blocks and a length (&[Block], len), the bytes past its end ignored. A batch is a Batch: messages of one length, each in its own whole blocks, every byte past each one zero, which the batch kernels read in place, side by side; a Batch lets the program write only its messages’ bytes, so those zeros cannot go wrong. Bytes already in place become blocks with no copy through Block::from_bytes, and a batch through Batch::from_blocks where its messages fill their blocks. update_each and TreeHasher::hash take byte slices of any length and alignment directly (without the caller copying them into aligned Blocks); update_each copies short pieces into a Hasher, and TreeHasher::hash hashes caller-owned &[u8] slices on the hot path.
  • Digests go where the program says: into its slices and places.
  • Every call takes a Mode: Mode::Plain for the plain hash.

For one call per hash, any slice, the result returned, see the convenience API.

Structs§

Batch
Messages of one length in Blocks, the batch calls’ input: each message starts a block, and every byte past each message’s end is zero, since a Batch lets the program write only the messages’ own bytes (so the bytes the batch kernels read in place, past a message, can never be wrong). It owns its blocks (Batch::new), or borrows blocks already in place whose messages fill them, with no bytes past their ends (Batch::from_blocks: a Merkle layer’s 64-byte nodes, a mapped file’s records).
Block
64 bytes on a 64-byte boundary: the unit the calls read, so every message starts on a cache line. A Batch holds its messages in blocks, each message starting a block and taking whole blocks (one for an empty message).
Cv
The chaining value of a whole subtree, with its place: 2^level chunks (1024 bytes each), the index-th subtree of that size in the message. It is the only kind a left child can be.
LastCv
The chaining value of a message’s last subtree, with its place and length: the right child at some level of the tree’s right edge. It may be shorter than its left sibling, and is never a left child: no call takes one on the left, so a tree with a short subtree inside it does not compile.
Packed
count messages of N bytes each, packed end to end with nothing between them, on a 128-byte boundary: a compact layout for many short messages of one length (a Merkle layer’s leaves or digests) without per-message block padding in caller memory (N is 16, 32, or 48; backing storage is N bytes per message rounded up once to a 128-byte line). It reads and writes as a [[u8; N]] (leaves[i] = digest), and the batch calls take it as they take a Batch. For Packed<32> with an even count, the digests can also be borrowed directly as 64-byte parent pairs via Packed::as_blocks (carry or pad an odd last digest according to your tree).
Part
Some of a batch’s messages, in place: Prepared::part makes it.
TreeHasher
A Mode prepared for a program that builds or verifies a BLAKE3 tree itself (verified streaming, an outboard, a tree hashed in parts on other machines): the subtrees’ chaining values, their parents, and the root. The mode’s key is derived once, here; each call then costs its hashing alone.

Traits§

Prepared
The batch calls’ input: messages of one length, prepared so that a call reads them as they are. Batch holds them in blocks, any length; Packed, owned Vec<[u8; N]> / Box<[[u8; N]]> and borrowed &[[u8; N]] slices (N = 16, 32, or 48) hold fixed-width messages end to end. Sealed: the crate’s types and fixed-width byte-array collections/slices alone.

Functions§

context_key
Hash a key-derivation context string and return a ContextKey.
finalize_each
The hash of each message which names: out[k] is what hashers[which[k]].finalize() would give, and that hasher is then reset for its next message (no length hint). Requires out.len() == which.len(), each index < hashers.len(), and distinct indices: it panics on an index named twice, in every build. A call that is refused hashes nothing and leaves every hasher as it was. Messages that finish in one call hash side by side where the CPU has the lanes (with at least two eligible hashers in a 128-hasher window): on x86-64, staged plain 1 KiB chunks via hash_many_rounded (ceiling 32 lanes on Zen 5 / 16 on AVX-512 / 8 on AVX2 / 4 on SSE4.1), staged plain multi-block 65..=1023 B messages on AVX2/AVX-512 via hash_by_blocks (equal-prefix-length blocks batched in SIMD, and on Linux x86-64 AVX-512/AVX2 their final blocks compressed in output16/output8 SIMD lanes for chunks of four or more; SSE4.1 finishes staged < 1 KiB messages individually), and output16/output8 for every 4+ eligible counter-zero final blocks packed from remaining hashers on Linux AVX-512/AVX2; on AArch64, staged plain messages of up to 1024 bytes via hash_by_blocks and counter-zero final blocks via NEON output8.
find_counter_range
Search count consecutive single-block messages derived from template (4..=64 bytes) in mode, replacing the 4-byte little-endian word at byte offset counter_word * 4 with start.wrapping_add(off as u32) for off in 0..count, for digests whose first little-endian 32-bit word word0 = u32::from_le_bytes(digest[..4]) satisfies (word0 & mask) == target.
hash
The hash in mode of the message of len bytes that starts input: a batch of one. The message starts a block, so on a cache line; the bytes past its end, in its last block and any after, are ignored. Tuned for call after call: for a hash now and then amid other work, convenience::hash is the faster call.
hash_chains_32_trunc16
Evaluate init.len() independent 32-byte iterated hash chains in mode, advancing each chain by steps iterations and writing its final 16-byte state to out[i] (#584).
hash_chains_varlen_32_trunc16
Variable-step counterpart to hash_chains_32_trunc16 that advances chain i by steps[i] iterations in mode and writes its final 16-byte state to out[i] (uses stack storage up to 128 nonzero chains; allocates a call-local Vec<usize> above 128; #584).
hash_counter_range
Hash out.len() single-block messages derived from template (4..=64 bytes) in mode, replacing the 4-byte little-endian word at byte offset counter_word * 4 with start.wrapping_add(i) (modulo 2^32) for i in 0..out.len().
hash_counter_range_trunc16
Hash out.len() single-block messages derived from template (4..=64 bytes) in mode, replacing the 4-byte little-endian word at byte offset counter_word * 4 with start.wrapping_add(i) (modulo 2^32) for i in 0..out.len(), writing the first 16 bytes of each BLAKE3 digest.
hash_each
The hash of each message, of any lengths, on the calling thread: out[i] is hash(mode, items[i].0, items[i].1), each item a message of items[i].1 bytes starting its blocks (the bytes past its end ignored). Short messages hash together, so a collection of small items (a directory’s files, a repository’s objects) takes less time than one call each. Requires out.len() == items.len().
hash_many
The hashes of a Batch’s messages, in mode, on the calling thread: out[i] becomes the hash of message i, one digest a message. This is the shape of a Merkle tree’s layer: leaves of one size, or nodes of two 32-byte children, which the layer below writes straight into the next layer’s batch (Batch::digests_mut).
left_len
The bytes of the left subtree of a message, or of a subtree, of len bytes: the largest power of two of chunks below its chunk count. The right subtree holds the rest.
level_of
The level of whole subtrees of len bytes: len is 1024 << level.
update_each
Feed each piece to its hasher: (i, bytes) does hashers[i].update(bytes), each hasher’s pieces in their order in pieces. Call it once with all the pieces your program has in hand (one turn of its event loop): never slower than one update each: many messages arriving at once, such as a server’s uploads, each with its own Hasher. Requires each i < hashers.len(): a turn that panics on one that is not leaves its hashers as after a panic. Finish the messages with finalize_each: plain messages of up to 1024 bytes wait whole in their stage (on both x86-64 and AArch64), so those that finish together hash side by side there. Because a staged plain first chunk has not yet compressed into ChunkState, calling a non-consuming Hasher::finalize() mid-chunk rehashes that staged prefix afresh; callers that finish each message once via finalize_each avoid that cost.