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 aBatch: 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; aBatchlets the program write only its messages’ bytes, so those zeros cannot go wrong. Bytes already in place become blocks with no copy throughBlock::from_bytes, and a batch throughBatch::from_blockswhere its messages fill their blocks.update_eachandTreeHasher::hashtake byte slices of any length and alignment directly (without the caller copying them into alignedBlocks);update_eachcopies short pieces into aHasher, andTreeHasher::hashhashes 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::Plainfor 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 aBatchlets 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
Batchholds 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
countmessages ofNbytes 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 (Nis 16, 32, or 48; backing storage isNbytes 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 aBatch. ForPacked<32>with an even count, the digests can also be borrowed directly as 64-byte parent pairs viaPacked::as_blocks(carry or pad an odd last digest according to your tree).- Part
- Some of a batch’s messages, in place:
Prepared::partmakes it. - Tree
Hasher - A
Modeprepared 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.
Batchholds them in blocks, any length;Packed, ownedVec<[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
whichnames:out[k]is whathashers[which[k]].finalize()would give, and that hasher is then reset for its next message (no length hint). Requiresout.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 viahash_many_rounded(ceiling 32 lanes on Zen 5 / 16 on AVX-512 / 8 on AVX2 / 4 on SSE4.1), staged plain multi-block65..=1023 Bmessages on AVX2/AVX-512 viahash_by_blocks(equal-prefix-length blocks batched in SIMD, and on Linux x86-64 AVX-512/AVX2 their final blocks compressed inoutput16/output8SIMD lanes for chunks of four or more; SSE4.1 finishes staged< 1 KiBmessages individually), andoutput16/output8for 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 viahash_by_blocksand counter-zero final blocks via NEONoutput8. - find_
counter_ range - Search
countconsecutive single-block messages derived fromtemplate(4..=64bytes) inmode, replacing the 4-byte little-endian word at byte offsetcounter_word * 4withstart.wrapping_add(off as u32)foroff in 0..count, for digests whose first little-endian 32-bit wordword0 = u32::from_le_bytes(digest[..4])satisfies(word0 & mask) == target. - hash
- The hash in
modeof the message oflenbytes that startsinput: 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::hashis the faster call. - hash_
chains_ 32_ trunc16 - Evaluate
init.len()independent 32-byte iterated hash chains inmode, advancing each chain bystepsiterations and writing its final 16-byte state toout[i](#584). - hash_
chains_ varlen_ 32_ trunc16 - Variable-step counterpart to
hash_chains_32_trunc16that advances chainibysteps[i]iterations inmodeand writes its final 16-byte state toout[i](uses stack storage up to 128 nonzero chains; allocates a call-localVec<usize>above 128; #584). - hash_
counter_ range - Hash
out.len()single-block messages derived fromtemplate(4..=64bytes) inmode, replacing the 4-byte little-endian word at byte offsetcounter_word * 4withstart.wrapping_add(i)(modulo 2^32) fori in 0..out.len(). - hash_
counter_ range_ trunc16 - Hash
out.len()single-block messages derived fromtemplate(4..=64bytes) inmode, replacing the 4-byte little-endian word at byte offsetcounter_word * 4withstart.wrapping_add(i)(modulo 2^32) fori 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]ishash(mode, items[i].0, items[i].1), each item a message ofitems[i].1bytes 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. Requiresout.len() == items.len(). - hash_
many - The hashes of a
Batch’s messages, inmode, on the calling thread:out[i]becomes thehashof messagei, 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
lenbytes: 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
lenbytes:lenis1024 << level. - update_
each - Feed each piece to its hasher:
(i, bytes)doeshashers[i].update(bytes), each hasher’s pieces in their order inpieces. Call it once with all the pieces your program has in hand (one turn of its event loop): never slower than oneupdateeach: many messages arriving at once, such as a server’s uploads, each with its ownHasher. Requires eachi < hashers.len(): a turn that panics on one that is not leaves its hashers as after a panic. Finish the messages withfinalize_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 intoChunkState, calling a non-consumingHasher::finalize()mid-chunk rehashes that staged prefix afresh; callers that finish each message once viafinalize_eachavoid that cost.