blake3_core
stdlib/blake3/blake3_core.nbv
blake3_core(key, static flags, input, static out_len)
BLAKE3 hash tree over input bits with a 256-bit key (the IV for the plain hash) and the mode flags (0, KEYED_HASH = 16, DERIVE_KEY_CONTEXT = 32 or DERIVE_KEY_MATERIAL = 64), returning out_len bits of extended output.
Words are 32 little-endian bits and the input and output are bytes with the least significant bit first (see bytes_to_bits), so input must be a whole number of bytes. The tree shape only depends on the input length: 1024-byte chunks of 64-byte blocks, then levels of parent nodes pairing adjacent nodes left to right and promoting an odd last node, which gives the BLAKE3 tree with the largest power of two of chunks on the left.
Inputs must be bits. Outputs are bits.
- Calls
blake3/blake3_compress,calloc,slice,bits/u32_bits_static- Precompiles
assert_eq!,clone!,concat!,div_ceil!,div_exact!,div_floor!,if_eq!,if_lt!,len!,log2_ceil!,loop!,loop_flat_map!,wtns!- Used by
blake3/blake3,blake3/blake3_derive_key,blake3/blake3_keyed
Source
# BLAKE3 hash tree over `input` bits with a 256-bit `key` (the IV for the
# plain hash) and the mode `flags` (0, KEYED_HASH = 16, DERIVE_KEY_CONTEXT =
# 32 or DERIVE_KEY_MATERIAL = 64), returning `out_len` bits of extended
# output.
#
# Words are 32 little-endian bits and the input and output are bytes with
# the least significant bit first (see bytes_to_bits), so `input` must be a
# whole number of bytes. The tree shape only depends on the input length:
# 1024-byte chunks of 64-byte blocks, then levels of parent nodes pairing
# adjacent nodes left to right and promoting an odd last node, which gives
# the BLAKE3 tree with the largest power of two of chunks on the left.
#
# Inputs must be bits. Outputs are bits.
(key, static flags, input, static out_len)
assert_eq! 256, len! key
static n = div_exact! (len! input), 8
static CHUNK_START = 1
static CHUNK_END = 2
static PARENT = 4
static ROOT = 8
# a static key (the IV) is materialized once so that no word addition is
# entirely static, which to_bits_exact can not handle. A witness key costs
# nothing here
let k = wtns! key
static mut chunk_count = 1
if_lt! 1024, n {
chunk_count = div_ceil! n, 1024
}
# all chunks but the last are full
static full = chunk_count - 1
static last_len = n - 1024 * full
static mut last_blocks = 1
if_lt! 64, last_len {
last_blocks = div_ceil! last_len, 64
}
# chaining values of the chunks and then of each level of parents, with a
# spare slot so the slices below are never empty
let mut nodes[256 * (chunk_count + 1)]
# the full chunks, compressed together block by block
if_lt! 0, full {
let mut cvs = loop_flat_map! full {
clone! k
}
loop! 16, b {
static mut f = flags
if_eq! b, 0 {
f = f + CHUNK_START
}
if_eq! b, 15 {
f = f + CHUNK_END
}
let block = loop_flat_map! full, j {
slice((clone! input), 8 * (1024 * j + 64 * b), 512)
}
let params = loop_flat_map! full, j {
concat! u32_bits_static(j), u32_bits_static(0), u32_bits_static(64), u32_bits_static(f)
}
cvs = blake3_compress(cvs, block, params, 8, 0)
}
nodes = concat! cvs, slice((clone! nodes), 256 * full, 512)
}
# the last chunk, zero padded to whole blocks. Its last block is the root
# node if it is the only chunk
let chunk = concat! slice((clone! input), 8 * 1024 * full, 8 * last_len), calloc(512 * last_blocks - 8 * last_len)
let mut cv = clone! k
loop! last_blocks - 1, b {
static mut f = flags
if_eq! b, 0 {
f = f + CHUNK_START
}
let params = concat! u32_bits_static(full), u32_bits_static(0), u32_bits_static(64), u32_bits_static(f)
cv = blake3_compress(cv, slice((clone! chunk), 512 * b, 512), params, 8, 0)
}
let mut root_cv = cv
let mut root_block = slice(chunk, 512 * (last_blocks - 1), 512)
static mut root_len = last_len - 64 * (last_blocks - 1)
static mut root_flags = flags + CHUNK_END
if_eq! last_blocks, 1 {
root_flags = root_flags + CHUNK_START
}
if_lt! 1, chunk_count {
let params = concat! u32_bits_static(full), u32_bits_static(0), u32_bits_static(root_len), u32_bits_static(root_flags)
let last_cv = blake3_compress((clone! root_cv), (clone! root_block), params, 8, 0)
nodes = concat! slice((clone! nodes), 0, 256 * full), last_cv, slice((clone! nodes), 256 * chunk_count, 256)
# parent levels down to the two children of the root
static mut count = chunk_count
loop! (log2_ceil! chunk_count), level {
if_lt! 2, count {
static pairs = div_floor! count, 2
let parent_cvs = loop_flat_map! pairs {
clone! k
}
let params = loop_flat_map! pairs {
concat! u32_bits_static(0), u32_bits_static(0), u32_bits_static(64), u32_bits_static(flags + PARENT)
}
# a parent block is the left child followed by the right child
let parents = blake3_compress(parent_cvs, slice((clone! nodes), 0, 512 * pairs), params, 8, 0)
nodes = concat! parents, slice((clone! nodes), 512 * pairs, 256 * (chunk_count + 1) - 512 * pairs), calloc(256 * pairs)
count = count - pairs
}
}
root_cv = clone! k
root_block = slice((clone! nodes), 0, 512)
root_len = 64
root_flags = flags + PARENT
}
# output blocks are the root compression with the block index as counter
static out_blocks = div_ceil! out_len, 512
static mut out_words = 16
if_lt! out_len, 257 {
out_words = 8
}
let root_cvs = loop_flat_map! out_blocks {
clone! root_cv
}
let root_blocks = loop_flat_map! out_blocks {
clone! root_block
}
let root_params = loop_flat_map! out_blocks, t {
concat! u32_bits_static(t), u32_bits_static(0), u32_bits_static(root_len), u32_bits_static(root_flags + ROOT)
}
slice(blake3_compress(root_cvs, root_blocks, root_params, out_words, 1), 0, out_len)