xref: /freebsd/sys/contrib/openzfs/module/icp/algs/blake3/blake3.c (revision 2a58b312b62f908ec92311d1bd8536dbaeb8e55b)
11f1e2261SMartin Matuska /*
21f1e2261SMartin Matuska  * CDDL HEADER START
31f1e2261SMartin Matuska  *
41f1e2261SMartin Matuska  * The contents of this file are subject to the terms of the
51f1e2261SMartin Matuska  * Common Development and Distribution License (the "License").
61f1e2261SMartin Matuska  * You may not use this file except in compliance with the License.
71f1e2261SMartin Matuska  *
81f1e2261SMartin Matuska  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
101f1e2261SMartin Matuska  * See the License for the specific language governing permissions
111f1e2261SMartin Matuska  * and limitations under the License.
121f1e2261SMartin Matuska  *
131f1e2261SMartin Matuska  * When distributing Covered Code, include this CDDL HEADER in each
141f1e2261SMartin Matuska  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151f1e2261SMartin Matuska  * If applicable, add the following below this CDDL HEADER, with the
161f1e2261SMartin Matuska  * fields enclosed by brackets "[]" replaced with your own identifying
171f1e2261SMartin Matuska  * information: Portions Copyright [yyyy] [name of copyright owner]
181f1e2261SMartin Matuska  *
191f1e2261SMartin Matuska  * CDDL HEADER END
201f1e2261SMartin Matuska  */
211f1e2261SMartin Matuska 
221f1e2261SMartin Matuska /*
231f1e2261SMartin Matuska  * Based on BLAKE3 v1.3.1, https://github.com/BLAKE3-team/BLAKE3
241f1e2261SMartin Matuska  * Copyright (c) 2019-2020 Samuel Neves and Jack O'Connor
251f1e2261SMartin Matuska  * Copyright (c) 2021-2022 Tino Reichardt <milky-zfs@mcmilk.de>
261f1e2261SMartin Matuska  */
271f1e2261SMartin Matuska 
28*2a58b312SMartin Matuska #include <sys/simd.h>
291f1e2261SMartin Matuska #include <sys/zfs_context.h>
301f1e2261SMartin Matuska #include <sys/blake3.h>
311f1e2261SMartin Matuska 
321f1e2261SMartin Matuska #include "blake3_impl.h"
331f1e2261SMartin Matuska 
341f1e2261SMartin Matuska /*
351f1e2261SMartin Matuska  * We need 1056 byte stack for blake3_compress_subtree_wide()
361f1e2261SMartin Matuska  * - we define this pragma to make gcc happy
371f1e2261SMartin Matuska  */
381f1e2261SMartin Matuska #if defined(__GNUC__)
391f1e2261SMartin Matuska #pragma GCC diagnostic ignored "-Wframe-larger-than="
401f1e2261SMartin Matuska #endif
411f1e2261SMartin Matuska 
421f1e2261SMartin Matuska /* internal used */
431f1e2261SMartin Matuska typedef struct {
441f1e2261SMartin Matuska 	uint32_t input_cv[8];
451f1e2261SMartin Matuska 	uint64_t counter;
461f1e2261SMartin Matuska 	uint8_t block[BLAKE3_BLOCK_LEN];
471f1e2261SMartin Matuska 	uint8_t block_len;
481f1e2261SMartin Matuska 	uint8_t flags;
491f1e2261SMartin Matuska } output_t;
501f1e2261SMartin Matuska 
511f1e2261SMartin Matuska /* internal flags */
521f1e2261SMartin Matuska enum blake3_flags {
531f1e2261SMartin Matuska 	CHUNK_START		= 1 << 0,
541f1e2261SMartin Matuska 	CHUNK_END		= 1 << 1,
551f1e2261SMartin Matuska 	PARENT			= 1 << 2,
561f1e2261SMartin Matuska 	ROOT			= 1 << 3,
571f1e2261SMartin Matuska 	KEYED_HASH		= 1 << 4,
581f1e2261SMartin Matuska 	DERIVE_KEY_CONTEXT	= 1 << 5,
591f1e2261SMartin Matuska 	DERIVE_KEY_MATERIAL	= 1 << 6,
601f1e2261SMartin Matuska };
611f1e2261SMartin Matuska 
621f1e2261SMartin Matuska /* internal start */
chunk_state_init(blake3_chunk_state_t * ctx,const uint32_t key[8],uint8_t flags)631f1e2261SMartin Matuska static void chunk_state_init(blake3_chunk_state_t *ctx,
641f1e2261SMartin Matuska     const uint32_t key[8], uint8_t flags)
651f1e2261SMartin Matuska {
661f1e2261SMartin Matuska 	memcpy(ctx->cv, key, BLAKE3_KEY_LEN);
671f1e2261SMartin Matuska 	ctx->chunk_counter = 0;
681f1e2261SMartin Matuska 	memset(ctx->buf, 0, BLAKE3_BLOCK_LEN);
691f1e2261SMartin Matuska 	ctx->buf_len = 0;
701f1e2261SMartin Matuska 	ctx->blocks_compressed = 0;
711f1e2261SMartin Matuska 	ctx->flags = flags;
721f1e2261SMartin Matuska }
731f1e2261SMartin Matuska 
chunk_state_reset(blake3_chunk_state_t * ctx,const uint32_t key[8],uint64_t chunk_counter)741f1e2261SMartin Matuska static void chunk_state_reset(blake3_chunk_state_t *ctx,
751f1e2261SMartin Matuska     const uint32_t key[8], uint64_t chunk_counter)
761f1e2261SMartin Matuska {
771f1e2261SMartin Matuska 	memcpy(ctx->cv, key, BLAKE3_KEY_LEN);
781f1e2261SMartin Matuska 	ctx->chunk_counter = chunk_counter;
791f1e2261SMartin Matuska 	ctx->blocks_compressed = 0;
801f1e2261SMartin Matuska 	memset(ctx->buf, 0, BLAKE3_BLOCK_LEN);
811f1e2261SMartin Matuska 	ctx->buf_len = 0;
821f1e2261SMartin Matuska }
831f1e2261SMartin Matuska 
chunk_state_len(const blake3_chunk_state_t * ctx)841f1e2261SMartin Matuska static size_t chunk_state_len(const blake3_chunk_state_t *ctx)
851f1e2261SMartin Matuska {
861f1e2261SMartin Matuska 	return (BLAKE3_BLOCK_LEN * (size_t)ctx->blocks_compressed) +
871f1e2261SMartin Matuska 	    ((size_t)ctx->buf_len);
881f1e2261SMartin Matuska }
891f1e2261SMartin Matuska 
chunk_state_fill_buf(blake3_chunk_state_t * ctx,const uint8_t * input,size_t input_len)901f1e2261SMartin Matuska static size_t chunk_state_fill_buf(blake3_chunk_state_t *ctx,
911f1e2261SMartin Matuska     const uint8_t *input, size_t input_len)
921f1e2261SMartin Matuska {
931f1e2261SMartin Matuska 	size_t take = BLAKE3_BLOCK_LEN - ((size_t)ctx->buf_len);
941f1e2261SMartin Matuska 	if (take > input_len) {
951f1e2261SMartin Matuska 		take = input_len;
961f1e2261SMartin Matuska 	}
971f1e2261SMartin Matuska 	uint8_t *dest = ctx->buf + ((size_t)ctx->buf_len);
981f1e2261SMartin Matuska 	memcpy(dest, input, take);
991f1e2261SMartin Matuska 	ctx->buf_len += (uint8_t)take;
1001f1e2261SMartin Matuska 	return (take);
1011f1e2261SMartin Matuska }
1021f1e2261SMartin Matuska 
chunk_state_maybe_start_flag(const blake3_chunk_state_t * ctx)1031f1e2261SMartin Matuska static uint8_t chunk_state_maybe_start_flag(const blake3_chunk_state_t *ctx)
1041f1e2261SMartin Matuska {
1051f1e2261SMartin Matuska 	if (ctx->blocks_compressed == 0) {
1061f1e2261SMartin Matuska 		return (CHUNK_START);
1071f1e2261SMartin Matuska 	} else {
1081f1e2261SMartin Matuska 		return (0);
1091f1e2261SMartin Matuska 	}
1101f1e2261SMartin Matuska }
1111f1e2261SMartin Matuska 
make_output(const uint32_t input_cv[8],const uint8_t * block,uint8_t block_len,uint64_t counter,uint8_t flags)1121f1e2261SMartin Matuska static output_t make_output(const uint32_t input_cv[8],
1131f1e2261SMartin Matuska     const uint8_t *block, uint8_t block_len,
1141f1e2261SMartin Matuska     uint64_t counter, uint8_t flags)
1151f1e2261SMartin Matuska {
1161f1e2261SMartin Matuska 	output_t ret;
1171f1e2261SMartin Matuska 	memcpy(ret.input_cv, input_cv, 32);
1181f1e2261SMartin Matuska 	memcpy(ret.block, block, BLAKE3_BLOCK_LEN);
1191f1e2261SMartin Matuska 	ret.block_len = block_len;
1201f1e2261SMartin Matuska 	ret.counter = counter;
1211f1e2261SMartin Matuska 	ret.flags = flags;
1221f1e2261SMartin Matuska 	return (ret);
1231f1e2261SMartin Matuska }
1241f1e2261SMartin Matuska 
1251f1e2261SMartin Matuska /*
1261f1e2261SMartin Matuska  * Chaining values within a given chunk (specifically the compress_in_place
1271f1e2261SMartin Matuska  * interface) are represented as words. This avoids unnecessary bytes<->words
1281f1e2261SMartin Matuska  * conversion overhead in the portable implementation. However, the hash_many
1291f1e2261SMartin Matuska  * interface handles both user input and parent node blocks, so it accepts
1301f1e2261SMartin Matuska  * bytes. For that reason, chaining values in the CV stack are represented as
1311f1e2261SMartin Matuska  * bytes.
1321f1e2261SMartin Matuska  */
output_chaining_value(const blake3_ops_t * ops,const output_t * ctx,uint8_t cv[32])133c7046f76SMartin Matuska static void output_chaining_value(const blake3_ops_t *ops,
1341f1e2261SMartin Matuska     const output_t *ctx, uint8_t cv[32])
1351f1e2261SMartin Matuska {
1361f1e2261SMartin Matuska 	uint32_t cv_words[8];
1371f1e2261SMartin Matuska 	memcpy(cv_words, ctx->input_cv, 32);
1381f1e2261SMartin Matuska 	ops->compress_in_place(cv_words, ctx->block, ctx->block_len,
1391f1e2261SMartin Matuska 	    ctx->counter, ctx->flags);
1401f1e2261SMartin Matuska 	store_cv_words(cv, cv_words);
1411f1e2261SMartin Matuska }
1421f1e2261SMartin Matuska 
output_root_bytes(const blake3_ops_t * ops,const output_t * ctx,uint64_t seek,uint8_t * out,size_t out_len)143c7046f76SMartin Matuska static void output_root_bytes(const blake3_ops_t *ops, const output_t *ctx,
1441f1e2261SMartin Matuska     uint64_t seek, uint8_t *out, size_t out_len)
1451f1e2261SMartin Matuska {
1461f1e2261SMartin Matuska 	uint64_t output_block_counter = seek / 64;
1471f1e2261SMartin Matuska 	size_t offset_within_block = seek % 64;
1481f1e2261SMartin Matuska 	uint8_t wide_buf[64];
1491f1e2261SMartin Matuska 	while (out_len > 0) {
1501f1e2261SMartin Matuska 		ops->compress_xof(ctx->input_cv, ctx->block, ctx->block_len,
1511f1e2261SMartin Matuska 		    output_block_counter, ctx->flags | ROOT, wide_buf);
1521f1e2261SMartin Matuska 		size_t available_bytes = 64 - offset_within_block;
1531f1e2261SMartin Matuska 		size_t memcpy_len;
1541f1e2261SMartin Matuska 		if (out_len > available_bytes) {
1551f1e2261SMartin Matuska 			memcpy_len = available_bytes;
1561f1e2261SMartin Matuska 		} else {
1571f1e2261SMartin Matuska 			memcpy_len = out_len;
1581f1e2261SMartin Matuska 		}
1591f1e2261SMartin Matuska 		memcpy(out, wide_buf + offset_within_block, memcpy_len);
1601f1e2261SMartin Matuska 		out += memcpy_len;
1611f1e2261SMartin Matuska 		out_len -= memcpy_len;
1621f1e2261SMartin Matuska 		output_block_counter += 1;
1631f1e2261SMartin Matuska 		offset_within_block = 0;
1641f1e2261SMartin Matuska 	}
1651f1e2261SMartin Matuska }
1661f1e2261SMartin Matuska 
chunk_state_update(const blake3_ops_t * ops,blake3_chunk_state_t * ctx,const uint8_t * input,size_t input_len)167c7046f76SMartin Matuska static void chunk_state_update(const blake3_ops_t *ops,
1681f1e2261SMartin Matuska     blake3_chunk_state_t *ctx, const uint8_t *input, size_t input_len)
1691f1e2261SMartin Matuska {
1701f1e2261SMartin Matuska 	if (ctx->buf_len > 0) {
1711f1e2261SMartin Matuska 		size_t take = chunk_state_fill_buf(ctx, input, input_len);
1721f1e2261SMartin Matuska 		input += take;
1731f1e2261SMartin Matuska 		input_len -= take;
1741f1e2261SMartin Matuska 		if (input_len > 0) {
1751f1e2261SMartin Matuska 			ops->compress_in_place(ctx->cv, ctx->buf,
1761f1e2261SMartin Matuska 			    BLAKE3_BLOCK_LEN, ctx->chunk_counter,
1771f1e2261SMartin Matuska 			    ctx->flags|chunk_state_maybe_start_flag(ctx));
1781f1e2261SMartin Matuska 			ctx->blocks_compressed += 1;
1791f1e2261SMartin Matuska 			ctx->buf_len = 0;
1801f1e2261SMartin Matuska 			memset(ctx->buf, 0, BLAKE3_BLOCK_LEN);
1811f1e2261SMartin Matuska 		}
1821f1e2261SMartin Matuska 	}
1831f1e2261SMartin Matuska 
1841f1e2261SMartin Matuska 	while (input_len > BLAKE3_BLOCK_LEN) {
1851f1e2261SMartin Matuska 		ops->compress_in_place(ctx->cv, input, BLAKE3_BLOCK_LEN,
1861f1e2261SMartin Matuska 		    ctx->chunk_counter,
1871f1e2261SMartin Matuska 		    ctx->flags|chunk_state_maybe_start_flag(ctx));
1881f1e2261SMartin Matuska 		ctx->blocks_compressed += 1;
1891f1e2261SMartin Matuska 		input += BLAKE3_BLOCK_LEN;
1901f1e2261SMartin Matuska 		input_len -= BLAKE3_BLOCK_LEN;
1911f1e2261SMartin Matuska 	}
1921f1e2261SMartin Matuska 
193dbd5678dSMartin Matuska 	chunk_state_fill_buf(ctx, input, input_len);
1941f1e2261SMartin Matuska }
1951f1e2261SMartin Matuska 
chunk_state_output(const blake3_chunk_state_t * ctx)1961f1e2261SMartin Matuska static output_t chunk_state_output(const blake3_chunk_state_t *ctx)
1971f1e2261SMartin Matuska {
1981f1e2261SMartin Matuska 	uint8_t block_flags =
1991f1e2261SMartin Matuska 	    ctx->flags | chunk_state_maybe_start_flag(ctx) | CHUNK_END;
2001f1e2261SMartin Matuska 	return (make_output(ctx->cv, ctx->buf, ctx->buf_len, ctx->chunk_counter,
2011f1e2261SMartin Matuska 	    block_flags));
2021f1e2261SMartin Matuska }
2031f1e2261SMartin Matuska 
parent_output(const uint8_t block[BLAKE3_BLOCK_LEN],const uint32_t key[8],uint8_t flags)2041f1e2261SMartin Matuska static output_t parent_output(const uint8_t block[BLAKE3_BLOCK_LEN],
2051f1e2261SMartin Matuska     const uint32_t key[8], uint8_t flags)
2061f1e2261SMartin Matuska {
2071f1e2261SMartin Matuska 	return (make_output(key, block, BLAKE3_BLOCK_LEN, 0, flags | PARENT));
2081f1e2261SMartin Matuska }
2091f1e2261SMartin Matuska 
2101f1e2261SMartin Matuska /*
2111f1e2261SMartin Matuska  * Given some input larger than one chunk, return the number of bytes that
2121f1e2261SMartin Matuska  * should go in the left subtree. This is the largest power-of-2 number of
2131f1e2261SMartin Matuska  * chunks that leaves at least 1 byte for the right subtree.
2141f1e2261SMartin Matuska  */
left_len(size_t content_len)2151f1e2261SMartin Matuska static size_t left_len(size_t content_len)
2161f1e2261SMartin Matuska {
2171f1e2261SMartin Matuska 	/*
2181f1e2261SMartin Matuska 	 * Subtract 1 to reserve at least one byte for the right side.
2191f1e2261SMartin Matuska 	 * content_len
2201f1e2261SMartin Matuska 	 * should always be greater than BLAKE3_CHUNK_LEN.
2211f1e2261SMartin Matuska 	 */
2221f1e2261SMartin Matuska 	size_t full_chunks = (content_len - 1) / BLAKE3_CHUNK_LEN;
2231f1e2261SMartin Matuska 	return (round_down_to_power_of_2(full_chunks) * BLAKE3_CHUNK_LEN);
2241f1e2261SMartin Matuska }
2251f1e2261SMartin Matuska 
2261f1e2261SMartin Matuska /*
2271f1e2261SMartin Matuska  * Use SIMD parallelism to hash up to MAX_SIMD_DEGREE chunks at the same time
2281f1e2261SMartin Matuska  * on a single thread. Write out the chunk chaining values and return the
2291f1e2261SMartin Matuska  * number of chunks hashed. These chunks are never the root and never empty;
2301f1e2261SMartin Matuska  * those cases use a different codepath.
2311f1e2261SMartin Matuska  */
compress_chunks_parallel(const blake3_ops_t * ops,const uint8_t * input,size_t input_len,const uint32_t key[8],uint64_t chunk_counter,uint8_t flags,uint8_t * out)232c7046f76SMartin Matuska static size_t compress_chunks_parallel(const blake3_ops_t *ops,
2331f1e2261SMartin Matuska     const uint8_t *input, size_t input_len, const uint32_t key[8],
2341f1e2261SMartin Matuska     uint64_t chunk_counter, uint8_t flags, uint8_t *out)
2351f1e2261SMartin Matuska {
2361f1e2261SMartin Matuska 	const uint8_t *chunks_array[MAX_SIMD_DEGREE];
2371f1e2261SMartin Matuska 	size_t input_position = 0;
2381f1e2261SMartin Matuska 	size_t chunks_array_len = 0;
2391f1e2261SMartin Matuska 	while (input_len - input_position >= BLAKE3_CHUNK_LEN) {
2401f1e2261SMartin Matuska 		chunks_array[chunks_array_len] = &input[input_position];
2411f1e2261SMartin Matuska 		input_position += BLAKE3_CHUNK_LEN;
2421f1e2261SMartin Matuska 		chunks_array_len += 1;
2431f1e2261SMartin Matuska 	}
2441f1e2261SMartin Matuska 
2451f1e2261SMartin Matuska 	ops->hash_many(chunks_array, chunks_array_len, BLAKE3_CHUNK_LEN /
2461f1e2261SMartin Matuska 	    BLAKE3_BLOCK_LEN, key, chunk_counter, B_TRUE, flags, CHUNK_START,
2471f1e2261SMartin Matuska 	    CHUNK_END, out);
2481f1e2261SMartin Matuska 
2491f1e2261SMartin Matuska 	/*
2501f1e2261SMartin Matuska 	 * Hash the remaining partial chunk, if there is one. Note that the
2511f1e2261SMartin Matuska 	 * empty chunk (meaning the empty message) is a different codepath.
2521f1e2261SMartin Matuska 	 */
2531f1e2261SMartin Matuska 	if (input_len > input_position) {
2541f1e2261SMartin Matuska 		uint64_t counter = chunk_counter + (uint64_t)chunks_array_len;
2551f1e2261SMartin Matuska 		blake3_chunk_state_t chunk_state;
2561f1e2261SMartin Matuska 		chunk_state_init(&chunk_state, key, flags);
2571f1e2261SMartin Matuska 		chunk_state.chunk_counter = counter;
2581f1e2261SMartin Matuska 		chunk_state_update(ops, &chunk_state, &input[input_position],
2591f1e2261SMartin Matuska 		    input_len - input_position);
2601f1e2261SMartin Matuska 		output_t output = chunk_state_output(&chunk_state);
2611f1e2261SMartin Matuska 		output_chaining_value(ops, &output, &out[chunks_array_len *
2621f1e2261SMartin Matuska 		    BLAKE3_OUT_LEN]);
2631f1e2261SMartin Matuska 		return (chunks_array_len + 1);
2641f1e2261SMartin Matuska 	} else {
2651f1e2261SMartin Matuska 		return (chunks_array_len);
2661f1e2261SMartin Matuska 	}
2671f1e2261SMartin Matuska }
2681f1e2261SMartin Matuska 
2691f1e2261SMartin Matuska /*
2701f1e2261SMartin Matuska  * Use SIMD parallelism to hash up to MAX_SIMD_DEGREE parents at the same time
2711f1e2261SMartin Matuska  * on a single thread. Write out the parent chaining values and return the
2721f1e2261SMartin Matuska  * number of parents hashed. (If there's an odd input chaining value left over,
2731f1e2261SMartin Matuska  * return it as an additional output.) These parents are never the root and
2741f1e2261SMartin Matuska  * never empty; those cases use a different codepath.
2751f1e2261SMartin Matuska  */
compress_parents_parallel(const blake3_ops_t * ops,const uint8_t * child_chaining_values,size_t num_chaining_values,const uint32_t key[8],uint8_t flags,uint8_t * out)276c7046f76SMartin Matuska static size_t compress_parents_parallel(const blake3_ops_t *ops,
2771f1e2261SMartin Matuska     const uint8_t *child_chaining_values, size_t num_chaining_values,
2781f1e2261SMartin Matuska     const uint32_t key[8], uint8_t flags, uint8_t *out)
2791f1e2261SMartin Matuska {
280bb2d13b6SMartin Matuska 	const uint8_t *parents_array[MAX_SIMD_DEGREE_OR_2] = {0};
2811f1e2261SMartin Matuska 	size_t parents_array_len = 0;
2821f1e2261SMartin Matuska 
2831f1e2261SMartin Matuska 	while (num_chaining_values - (2 * parents_array_len) >= 2) {
2841f1e2261SMartin Matuska 		parents_array[parents_array_len] = &child_chaining_values[2 *
2851f1e2261SMartin Matuska 		    parents_array_len * BLAKE3_OUT_LEN];
2861f1e2261SMartin Matuska 		parents_array_len += 1;
2871f1e2261SMartin Matuska 	}
2881f1e2261SMartin Matuska 
2891f1e2261SMartin Matuska 	ops->hash_many(parents_array, parents_array_len, 1, key, 0, B_FALSE,
2901f1e2261SMartin Matuska 	    flags | PARENT, 0, 0, out);
2911f1e2261SMartin Matuska 
2921f1e2261SMartin Matuska 	/* If there's an odd child left over, it becomes an output. */
2931f1e2261SMartin Matuska 	if (num_chaining_values > 2 * parents_array_len) {
2941f1e2261SMartin Matuska 		memcpy(&out[parents_array_len * BLAKE3_OUT_LEN],
2951f1e2261SMartin Matuska 		    &child_chaining_values[2 * parents_array_len *
2961f1e2261SMartin Matuska 		    BLAKE3_OUT_LEN], BLAKE3_OUT_LEN);
2971f1e2261SMartin Matuska 		return (parents_array_len + 1);
2981f1e2261SMartin Matuska 	} else {
2991f1e2261SMartin Matuska 		return (parents_array_len);
3001f1e2261SMartin Matuska 	}
3011f1e2261SMartin Matuska }
3021f1e2261SMartin Matuska 
3031f1e2261SMartin Matuska /*
3041f1e2261SMartin Matuska  * The wide helper function returns (writes out) an array of chaining values
3051f1e2261SMartin Matuska  * and returns the length of that array. The number of chaining values returned
3061f1e2261SMartin Matuska  * is the dyanmically detected SIMD degree, at most MAX_SIMD_DEGREE. Or fewer,
3071f1e2261SMartin Matuska  * if the input is shorter than that many chunks. The reason for maintaining a
3081f1e2261SMartin Matuska  * wide array of chaining values going back up the tree, is to allow the
3091f1e2261SMartin Matuska  * implementation to hash as many parents in parallel as possible.
3101f1e2261SMartin Matuska  *
3111f1e2261SMartin Matuska  * As a special case when the SIMD degree is 1, this function will still return
3121f1e2261SMartin Matuska  * at least 2 outputs. This guarantees that this function doesn't perform the
3131f1e2261SMartin Matuska  * root compression. (If it did, it would use the wrong flags, and also we
3141f1e2261SMartin Matuska  * wouldn't be able to implement exendable ouput.) Note that this function is
3151f1e2261SMartin Matuska  * not used when the whole input is only 1 chunk long; that's a different
3161f1e2261SMartin Matuska  * codepath.
3171f1e2261SMartin Matuska  *
3181f1e2261SMartin Matuska  * Why not just have the caller split the input on the first update(), instead
3191f1e2261SMartin Matuska  * of implementing this special rule? Because we don't want to limit SIMD or
3201f1e2261SMartin Matuska  * multi-threading parallelism for that update().
3211f1e2261SMartin Matuska  */
blake3_compress_subtree_wide(const blake3_ops_t * ops,const uint8_t * input,size_t input_len,const uint32_t key[8],uint64_t chunk_counter,uint8_t flags,uint8_t * out)322c7046f76SMartin Matuska static size_t blake3_compress_subtree_wide(const blake3_ops_t *ops,
3231f1e2261SMartin Matuska     const uint8_t *input, size_t input_len, const uint32_t key[8],
3241f1e2261SMartin Matuska     uint64_t chunk_counter, uint8_t flags, uint8_t *out)
3251f1e2261SMartin Matuska {
3261f1e2261SMartin Matuska 	/*
3271f1e2261SMartin Matuska 	 * Note that the single chunk case does *not* bump the SIMD degree up
3281f1e2261SMartin Matuska 	 * to 2 when it is 1. If this implementation adds multi-threading in
3291f1e2261SMartin Matuska 	 * the future, this gives us the option of multi-threading even the
3301f1e2261SMartin Matuska 	 * 2-chunk case, which can help performance on smaller platforms.
3311f1e2261SMartin Matuska 	 */
3321f1e2261SMartin Matuska 	if (input_len <= (size_t)(ops->degree * BLAKE3_CHUNK_LEN)) {
3331f1e2261SMartin Matuska 		return (compress_chunks_parallel(ops, input, input_len, key,
3341f1e2261SMartin Matuska 		    chunk_counter, flags, out));
3351f1e2261SMartin Matuska 	}
3361f1e2261SMartin Matuska 
3371f1e2261SMartin Matuska 
3381f1e2261SMartin Matuska 	/*
3391f1e2261SMartin Matuska 	 * With more than simd_degree chunks, we need to recurse. Start by
3401f1e2261SMartin Matuska 	 * dividing the input into left and right subtrees. (Note that this is
3411f1e2261SMartin Matuska 	 * only optimal as long as the SIMD degree is a power of 2. If we ever
3421f1e2261SMartin Matuska 	 * get a SIMD degree of 3 or something, we'll need a more complicated
3431f1e2261SMartin Matuska 	 * strategy.)
3441f1e2261SMartin Matuska 	 */
3451f1e2261SMartin Matuska 	size_t left_input_len = left_len(input_len);
3461f1e2261SMartin Matuska 	size_t right_input_len = input_len - left_input_len;
3471f1e2261SMartin Matuska 	const uint8_t *right_input = &input[left_input_len];
3481f1e2261SMartin Matuska 	uint64_t right_chunk_counter = chunk_counter +
3491f1e2261SMartin Matuska 	    (uint64_t)(left_input_len / BLAKE3_CHUNK_LEN);
3501f1e2261SMartin Matuska 
3511f1e2261SMartin Matuska 	/*
3521f1e2261SMartin Matuska 	 * Make space for the child outputs. Here we use MAX_SIMD_DEGREE_OR_2
3531f1e2261SMartin Matuska 	 * to account for the special case of returning 2 outputs when the
3541f1e2261SMartin Matuska 	 * SIMD degree is 1.
3551f1e2261SMartin Matuska 	 */
3561f1e2261SMartin Matuska 	uint8_t cv_array[2 * MAX_SIMD_DEGREE_OR_2 * BLAKE3_OUT_LEN];
3571f1e2261SMartin Matuska 	size_t degree = ops->degree;
3581f1e2261SMartin Matuska 	if (left_input_len > BLAKE3_CHUNK_LEN && degree == 1) {
3591f1e2261SMartin Matuska 
3601f1e2261SMartin Matuska 		/*
3611f1e2261SMartin Matuska 		 * The special case: We always use a degree of at least two,
3621f1e2261SMartin Matuska 		 * to make sure there are two outputs. Except, as noted above,
3631f1e2261SMartin Matuska 		 * at the chunk level, where we allow degree=1. (Note that the
3641f1e2261SMartin Matuska 		 * 1-chunk-input case is a different codepath.)
3651f1e2261SMartin Matuska 		 */
3661f1e2261SMartin Matuska 		degree = 2;
3671f1e2261SMartin Matuska 	}
3681f1e2261SMartin Matuska 	uint8_t *right_cvs = &cv_array[degree * BLAKE3_OUT_LEN];
3691f1e2261SMartin Matuska 
3701f1e2261SMartin Matuska 	/*
3711f1e2261SMartin Matuska 	 * Recurse! If this implementation adds multi-threading support in the
3721f1e2261SMartin Matuska 	 * future, this is where it will go.
3731f1e2261SMartin Matuska 	 */
3741f1e2261SMartin Matuska 	size_t left_n = blake3_compress_subtree_wide(ops, input, left_input_len,
3751f1e2261SMartin Matuska 	    key, chunk_counter, flags, cv_array);
3761f1e2261SMartin Matuska 	size_t right_n = blake3_compress_subtree_wide(ops, right_input,
3771f1e2261SMartin Matuska 	    right_input_len, key, right_chunk_counter, flags, right_cvs);
3781f1e2261SMartin Matuska 
3791f1e2261SMartin Matuska 	/*
3801f1e2261SMartin Matuska 	 * The special case again. If simd_degree=1, then we'll have left_n=1
3811f1e2261SMartin Matuska 	 * and right_n=1. Rather than compressing them into a single output,
3821f1e2261SMartin Matuska 	 * return them directly, to make sure we always have at least two
3831f1e2261SMartin Matuska 	 * outputs.
3841f1e2261SMartin Matuska 	 */
3851f1e2261SMartin Matuska 	if (left_n == 1) {
3861f1e2261SMartin Matuska 		memcpy(out, cv_array, 2 * BLAKE3_OUT_LEN);
3871f1e2261SMartin Matuska 		return (2);
3881f1e2261SMartin Matuska 	}
3891f1e2261SMartin Matuska 
3901f1e2261SMartin Matuska 	/* Otherwise, do one layer of parent node compression. */
3911f1e2261SMartin Matuska 	size_t num_chaining_values = left_n + right_n;
3921f1e2261SMartin Matuska 	return compress_parents_parallel(ops, cv_array,
3931f1e2261SMartin Matuska 	    num_chaining_values, key, flags, out);
3941f1e2261SMartin Matuska }
3951f1e2261SMartin Matuska 
3961f1e2261SMartin Matuska /*
3971f1e2261SMartin Matuska  * Hash a subtree with compress_subtree_wide(), and then condense the resulting
3981f1e2261SMartin Matuska  * list of chaining values down to a single parent node. Don't compress that
3991f1e2261SMartin Matuska  * last parent node, however. Instead, return its message bytes (the
4001f1e2261SMartin Matuska  * concatenated chaining values of its children). This is necessary when the
4011f1e2261SMartin Matuska  * first call to update() supplies a complete subtree, because the topmost
4021f1e2261SMartin Matuska  * parent node of that subtree could end up being the root. It's also necessary
4031f1e2261SMartin Matuska  * for extended output in the general case.
4041f1e2261SMartin Matuska  *
4051f1e2261SMartin Matuska  * As with compress_subtree_wide(), this function is not used on inputs of 1
4061f1e2261SMartin Matuska  * chunk or less. That's a different codepath.
4071f1e2261SMartin Matuska  */
compress_subtree_to_parent_node(const blake3_ops_t * ops,const uint8_t * input,size_t input_len,const uint32_t key[8],uint64_t chunk_counter,uint8_t flags,uint8_t out[2* BLAKE3_OUT_LEN])408c7046f76SMartin Matuska static void compress_subtree_to_parent_node(const blake3_ops_t *ops,
4091f1e2261SMartin Matuska     const uint8_t *input, size_t input_len, const uint32_t key[8],
4101f1e2261SMartin Matuska     uint64_t chunk_counter, uint8_t flags, uint8_t out[2 * BLAKE3_OUT_LEN])
4111f1e2261SMartin Matuska {
4121f1e2261SMartin Matuska 	uint8_t cv_array[MAX_SIMD_DEGREE_OR_2 * BLAKE3_OUT_LEN];
4131f1e2261SMartin Matuska 	size_t num_cvs = blake3_compress_subtree_wide(ops, input, input_len,
4141f1e2261SMartin Matuska 	    key, chunk_counter, flags, cv_array);
4151f1e2261SMartin Matuska 
4161f1e2261SMartin Matuska 	/*
4171f1e2261SMartin Matuska 	 * If MAX_SIMD_DEGREE is greater than 2 and there's enough input,
4181f1e2261SMartin Matuska 	 * compress_subtree_wide() returns more than 2 chaining values. Condense
4191f1e2261SMartin Matuska 	 * them into 2 by forming parent nodes repeatedly.
4201f1e2261SMartin Matuska 	 */
4211f1e2261SMartin Matuska 	uint8_t out_array[MAX_SIMD_DEGREE_OR_2 * BLAKE3_OUT_LEN / 2];
4221f1e2261SMartin Matuska 	while (num_cvs > 2) {
4231f1e2261SMartin Matuska 		num_cvs = compress_parents_parallel(ops, cv_array, num_cvs, key,
4241f1e2261SMartin Matuska 		    flags, out_array);
4251f1e2261SMartin Matuska 		memcpy(cv_array, out_array, num_cvs * BLAKE3_OUT_LEN);
4261f1e2261SMartin Matuska 	}
4271f1e2261SMartin Matuska 	memcpy(out, cv_array, 2 * BLAKE3_OUT_LEN);
4281f1e2261SMartin Matuska }
4291f1e2261SMartin Matuska 
hasher_init_base(BLAKE3_CTX * ctx,const uint32_t key[8],uint8_t flags)4301f1e2261SMartin Matuska static void hasher_init_base(BLAKE3_CTX *ctx, const uint32_t key[8],
4311f1e2261SMartin Matuska     uint8_t flags)
4321f1e2261SMartin Matuska {
4331f1e2261SMartin Matuska 	memcpy(ctx->key, key, BLAKE3_KEY_LEN);
4341f1e2261SMartin Matuska 	chunk_state_init(&ctx->chunk, key, flags);
4351f1e2261SMartin Matuska 	ctx->cv_stack_len = 0;
436*2a58b312SMartin Matuska 	ctx->ops = blake3_get_ops();
4371f1e2261SMartin Matuska }
4381f1e2261SMartin Matuska 
4391f1e2261SMartin Matuska /*
4401f1e2261SMartin Matuska  * As described in hasher_push_cv() below, we do "lazy merging", delaying
4411f1e2261SMartin Matuska  * merges until right before the next CV is about to be added. This is
4421f1e2261SMartin Matuska  * different from the reference implementation. Another difference is that we
4431f1e2261SMartin Matuska  * aren't always merging 1 chunk at a time. Instead, each CV might represent
4441f1e2261SMartin Matuska  * any power-of-two number of chunks, as long as the smaller-above-larger
4451f1e2261SMartin Matuska  * stack order is maintained. Instead of the "count the trailing 0-bits"
4461f1e2261SMartin Matuska  * algorithm described in the spec, we use a "count the total number of
4471f1e2261SMartin Matuska  * 1-bits" variant that doesn't require us to retain the subtree size of the
4481f1e2261SMartin Matuska  * CV on top of the stack. The principle is the same: each CV that should
4491f1e2261SMartin Matuska  * remain in the stack is represented by a 1-bit in the total number of chunks
4501f1e2261SMartin Matuska  * (or bytes) so far.
4511f1e2261SMartin Matuska  */
hasher_merge_cv_stack(BLAKE3_CTX * ctx,uint64_t total_len)4521f1e2261SMartin Matuska static void hasher_merge_cv_stack(BLAKE3_CTX *ctx, uint64_t total_len)
4531f1e2261SMartin Matuska {
4541f1e2261SMartin Matuska 	size_t post_merge_stack_len = (size_t)popcnt(total_len);
4551f1e2261SMartin Matuska 	while (ctx->cv_stack_len > post_merge_stack_len) {
4561f1e2261SMartin Matuska 		uint8_t *parent_node =
4571f1e2261SMartin Matuska 		    &ctx->cv_stack[(ctx->cv_stack_len - 2) * BLAKE3_OUT_LEN];
4581f1e2261SMartin Matuska 		output_t output =
4591f1e2261SMartin Matuska 		    parent_output(parent_node, ctx->key, ctx->chunk.flags);
4601f1e2261SMartin Matuska 		output_chaining_value(ctx->ops, &output, parent_node);
4611f1e2261SMartin Matuska 		ctx->cv_stack_len -= 1;
4621f1e2261SMartin Matuska 	}
4631f1e2261SMartin Matuska }
4641f1e2261SMartin Matuska 
4651f1e2261SMartin Matuska /*
4661f1e2261SMartin Matuska  * In reference_impl.rs, we merge the new CV with existing CVs from the stack
4671f1e2261SMartin Matuska  * before pushing it. We can do that because we know more input is coming, so
4681f1e2261SMartin Matuska  * we know none of the merges are root.
4691f1e2261SMartin Matuska  *
4701f1e2261SMartin Matuska  * This setting is different. We want to feed as much input as possible to
4711f1e2261SMartin Matuska  * compress_subtree_wide(), without setting aside anything for the chunk_state.
4721f1e2261SMartin Matuska  * If the user gives us 64 KiB, we want to parallelize over all 64 KiB at once
4731f1e2261SMartin Matuska  * as a single subtree, if at all possible.
4741f1e2261SMartin Matuska  *
4751f1e2261SMartin Matuska  * This leads to two problems:
4761f1e2261SMartin Matuska  * 1) This 64 KiB input might be the only call that ever gets made to update.
4771f1e2261SMartin Matuska  *    In this case, the root node of the 64 KiB subtree would be the root node
4781f1e2261SMartin Matuska  *    of the whole tree, and it would need to be ROOT finalized. We can't
4791f1e2261SMartin Matuska  *    compress it until we know.
4801f1e2261SMartin Matuska  * 2) This 64 KiB input might complete a larger tree, whose root node is
4811f1e2261SMartin Matuska  *    similarly going to be the the root of the whole tree. For example, maybe
4821f1e2261SMartin Matuska  *    we have 196 KiB (that is, 128 + 64) hashed so far. We can't compress the
4831f1e2261SMartin Matuska  *    node at the root of the 256 KiB subtree until we know how to finalize it.
4841f1e2261SMartin Matuska  *
4851f1e2261SMartin Matuska  * The second problem is solved with "lazy merging". That is, when we're about
4861f1e2261SMartin Matuska  * to add a CV to the stack, we don't merge it with anything first, as the
4871f1e2261SMartin Matuska  * reference impl does. Instead we do merges using the *previous* CV that was
4881f1e2261SMartin Matuska  * added, which is sitting on top of the stack, and we put the new CV
4891f1e2261SMartin Matuska  * (unmerged) on top of the stack afterwards. This guarantees that we never
4901f1e2261SMartin Matuska  * merge the root node until finalize().
4911f1e2261SMartin Matuska  *
4921f1e2261SMartin Matuska  * Solving the first problem requires an additional tool,
4931f1e2261SMartin Matuska  * compress_subtree_to_parent_node(). That function always returns the top
4941f1e2261SMartin Matuska  * *two* chaining values of the subtree it's compressing. We then do lazy
4951f1e2261SMartin Matuska  * merging with each of them separately, so that the second CV will always
4961f1e2261SMartin Matuska  * remain unmerged. (That also helps us support extendable output when we're
4971f1e2261SMartin Matuska  * hashing an input all-at-once.)
4981f1e2261SMartin Matuska  */
hasher_push_cv(BLAKE3_CTX * ctx,uint8_t new_cv[BLAKE3_OUT_LEN],uint64_t chunk_counter)4991f1e2261SMartin Matuska static void hasher_push_cv(BLAKE3_CTX *ctx, uint8_t new_cv[BLAKE3_OUT_LEN],
5001f1e2261SMartin Matuska     uint64_t chunk_counter)
5011f1e2261SMartin Matuska {
5021f1e2261SMartin Matuska 	hasher_merge_cv_stack(ctx, chunk_counter);
5031f1e2261SMartin Matuska 	memcpy(&ctx->cv_stack[ctx->cv_stack_len * BLAKE3_OUT_LEN], new_cv,
5041f1e2261SMartin Matuska 	    BLAKE3_OUT_LEN);
5051f1e2261SMartin Matuska 	ctx->cv_stack_len += 1;
5061f1e2261SMartin Matuska }
5071f1e2261SMartin Matuska 
5081f1e2261SMartin Matuska void
Blake3_Init(BLAKE3_CTX * ctx)5091f1e2261SMartin Matuska Blake3_Init(BLAKE3_CTX *ctx)
5101f1e2261SMartin Matuska {
5111f1e2261SMartin Matuska 	hasher_init_base(ctx, BLAKE3_IV, 0);
5121f1e2261SMartin Matuska }
5131f1e2261SMartin Matuska 
5141f1e2261SMartin Matuska void
Blake3_InitKeyed(BLAKE3_CTX * ctx,const uint8_t key[BLAKE3_KEY_LEN])5151f1e2261SMartin Matuska Blake3_InitKeyed(BLAKE3_CTX *ctx, const uint8_t key[BLAKE3_KEY_LEN])
5161f1e2261SMartin Matuska {
5171f1e2261SMartin Matuska 	uint32_t key_words[8];
5181f1e2261SMartin Matuska 	load_key_words(key, key_words);
5191f1e2261SMartin Matuska 	hasher_init_base(ctx, key_words, KEYED_HASH);
5201f1e2261SMartin Matuska }
5211f1e2261SMartin Matuska 
5221f1e2261SMartin Matuska static void
Blake3_Update2(BLAKE3_CTX * ctx,const void * input,size_t input_len)5231f1e2261SMartin Matuska Blake3_Update2(BLAKE3_CTX *ctx, const void *input, size_t input_len)
5241f1e2261SMartin Matuska {
5251f1e2261SMartin Matuska 	/*
5261f1e2261SMartin Matuska 	 * Explicitly checking for zero avoids causing UB by passing a null
5271f1e2261SMartin Matuska 	 * pointer to memcpy. This comes up in practice with things like:
5281f1e2261SMartin Matuska 	 *   std::vector<uint8_t> v;
5291f1e2261SMartin Matuska 	 *   blake3_hasher_update(&hasher, v.data(), v.size());
5301f1e2261SMartin Matuska 	 */
5311f1e2261SMartin Matuska 	if (input_len == 0) {
5321f1e2261SMartin Matuska 		return;
5331f1e2261SMartin Matuska 	}
5341f1e2261SMartin Matuska 
5351f1e2261SMartin Matuska 	const uint8_t *input_bytes = (const uint8_t *)input;
5361f1e2261SMartin Matuska 
5371f1e2261SMartin Matuska 	/*
5381f1e2261SMartin Matuska 	 * If we have some partial chunk bytes in the internal chunk_state, we
5391f1e2261SMartin Matuska 	 * need to finish that chunk first.
5401f1e2261SMartin Matuska 	 */
5411f1e2261SMartin Matuska 	if (chunk_state_len(&ctx->chunk) > 0) {
5421f1e2261SMartin Matuska 		size_t take = BLAKE3_CHUNK_LEN - chunk_state_len(&ctx->chunk);
5431f1e2261SMartin Matuska 		if (take > input_len) {
5441f1e2261SMartin Matuska 			take = input_len;
5451f1e2261SMartin Matuska 		}
5461f1e2261SMartin Matuska 		chunk_state_update(ctx->ops, &ctx->chunk, input_bytes, take);
5471f1e2261SMartin Matuska 		input_bytes += take;
5481f1e2261SMartin Matuska 		input_len -= take;
5491f1e2261SMartin Matuska 		/*
5501f1e2261SMartin Matuska 		 * If we've filled the current chunk and there's more coming,
5511f1e2261SMartin Matuska 		 * finalize this chunk and proceed. In this case we know it's
5521f1e2261SMartin Matuska 		 * not the root.
5531f1e2261SMartin Matuska 		 */
5541f1e2261SMartin Matuska 		if (input_len > 0) {
5551f1e2261SMartin Matuska 			output_t output = chunk_state_output(&ctx->chunk);
5561f1e2261SMartin Matuska 			uint8_t chunk_cv[32];
5571f1e2261SMartin Matuska 			output_chaining_value(ctx->ops, &output, chunk_cv);
5581f1e2261SMartin Matuska 			hasher_push_cv(ctx, chunk_cv, ctx->chunk.chunk_counter);
5591f1e2261SMartin Matuska 			chunk_state_reset(&ctx->chunk, ctx->key,
5601f1e2261SMartin Matuska 			    ctx->chunk.chunk_counter + 1);
5611f1e2261SMartin Matuska 		} else {
5621f1e2261SMartin Matuska 			return;
5631f1e2261SMartin Matuska 		}
5641f1e2261SMartin Matuska 	}
5651f1e2261SMartin Matuska 
5661f1e2261SMartin Matuska 	/*
5671f1e2261SMartin Matuska 	 * Now the chunk_state is clear, and we have more input. If there's
5681f1e2261SMartin Matuska 	 * more than a single chunk (so, definitely not the root chunk), hash
5691f1e2261SMartin Matuska 	 * the largest whole subtree we can, with the full benefits of SIMD
5701f1e2261SMartin Matuska 	 * (and maybe in the future, multi-threading) parallelism. Two
5711f1e2261SMartin Matuska 	 * restrictions:
5721f1e2261SMartin Matuska 	 * - The subtree has to be a power-of-2 number of chunks. Only
5731f1e2261SMartin Matuska 	 *   subtrees along the right edge can be incomplete, and we don't know
5741f1e2261SMartin Matuska 	 *   where the right edge is going to be until we get to finalize().
5751f1e2261SMartin Matuska 	 * - The subtree must evenly divide the total number of chunks up
5761f1e2261SMartin Matuska 	 *   until this point (if total is not 0). If the current incomplete
5771f1e2261SMartin Matuska 	 *   subtree is only waiting for 1 more chunk, we can't hash a subtree
5781f1e2261SMartin Matuska 	 *   of 4 chunks. We have to complete the current subtree first.
5791f1e2261SMartin Matuska 	 * Because we might need to break up the input to form powers of 2, or
5801f1e2261SMartin Matuska 	 * to evenly divide what we already have, this part runs in a loop.
5811f1e2261SMartin Matuska 	 */
5821f1e2261SMartin Matuska 	while (input_len > BLAKE3_CHUNK_LEN) {
5831f1e2261SMartin Matuska 		size_t subtree_len = round_down_to_power_of_2(input_len);
5841f1e2261SMartin Matuska 		uint64_t count_so_far =
5851f1e2261SMartin Matuska 		    ctx->chunk.chunk_counter * BLAKE3_CHUNK_LEN;
5861f1e2261SMartin Matuska 		/*
5871f1e2261SMartin Matuska 		 * Shrink the subtree_len until it evenly divides the count so
5881f1e2261SMartin Matuska 		 * far. We know that subtree_len itself is a power of 2, so we
5891f1e2261SMartin Matuska 		 * can use a bitmasking trick instead of an actual remainder
5901f1e2261SMartin Matuska 		 * operation. (Note that if the caller consistently passes
5911f1e2261SMartin Matuska 		 * power-of-2 inputs of the same size, as is hopefully
5921f1e2261SMartin Matuska 		 * typical, this loop condition will always fail, and
5931f1e2261SMartin Matuska 		 * subtree_len will always be the full length of the input.)
5941f1e2261SMartin Matuska 		 *
5951f1e2261SMartin Matuska 		 * An aside: We don't have to shrink subtree_len quite this
5961f1e2261SMartin Matuska 		 * much. For example, if count_so_far is 1, we could pass 2
5971f1e2261SMartin Matuska 		 * chunks to compress_subtree_to_parent_node. Since we'll get
5981f1e2261SMartin Matuska 		 * 2 CVs back, we'll still get the right answer in the end,
5991f1e2261SMartin Matuska 		 * and we might get to use 2-way SIMD parallelism. The problem
6001f1e2261SMartin Matuska 		 * with this optimization, is that it gets us stuck always
6011f1e2261SMartin Matuska 		 * hashing 2 chunks. The total number of chunks will remain
6021f1e2261SMartin Matuska 		 * odd, and we'll never graduate to higher degrees of
6031f1e2261SMartin Matuska 		 * parallelism. See
6041f1e2261SMartin Matuska 		 * https://github.com/BLAKE3-team/BLAKE3/issues/69.
6051f1e2261SMartin Matuska 		 */
6061f1e2261SMartin Matuska 		while ((((uint64_t)(subtree_len - 1)) & count_so_far) != 0) {
6071f1e2261SMartin Matuska 			subtree_len /= 2;
6081f1e2261SMartin Matuska 		}
6091f1e2261SMartin Matuska 		/*
6101f1e2261SMartin Matuska 		 * The shrunken subtree_len might now be 1 chunk long. If so,
6111f1e2261SMartin Matuska 		 * hash that one chunk by itself. Otherwise, compress the
6121f1e2261SMartin Matuska 		 * subtree into a pair of CVs.
6131f1e2261SMartin Matuska 		 */
6141f1e2261SMartin Matuska 		uint64_t subtree_chunks = subtree_len / BLAKE3_CHUNK_LEN;
6151f1e2261SMartin Matuska 		if (subtree_len <= BLAKE3_CHUNK_LEN) {
6161f1e2261SMartin Matuska 			blake3_chunk_state_t chunk_state;
6171f1e2261SMartin Matuska 			chunk_state_init(&chunk_state, ctx->key,
6181f1e2261SMartin Matuska 			    ctx->chunk.flags);
6191f1e2261SMartin Matuska 			chunk_state.chunk_counter = ctx->chunk.chunk_counter;
6201f1e2261SMartin Matuska 			chunk_state_update(ctx->ops, &chunk_state, input_bytes,
6211f1e2261SMartin Matuska 			    subtree_len);
6221f1e2261SMartin Matuska 			output_t output = chunk_state_output(&chunk_state);
6231f1e2261SMartin Matuska 			uint8_t cv[BLAKE3_OUT_LEN];
6241f1e2261SMartin Matuska 			output_chaining_value(ctx->ops, &output, cv);
6251f1e2261SMartin Matuska 			hasher_push_cv(ctx, cv, chunk_state.chunk_counter);
6261f1e2261SMartin Matuska 		} else {
6271f1e2261SMartin Matuska 			/*
6281f1e2261SMartin Matuska 			 * This is the high-performance happy path, though
6291f1e2261SMartin Matuska 			 * getting here depends on the caller giving us a long
6301f1e2261SMartin Matuska 			 * enough input.
6311f1e2261SMartin Matuska 			 */
6321f1e2261SMartin Matuska 			uint8_t cv_pair[2 * BLAKE3_OUT_LEN];
6331f1e2261SMartin Matuska 			compress_subtree_to_parent_node(ctx->ops, input_bytes,
6341f1e2261SMartin Matuska 			    subtree_len, ctx->key, ctx-> chunk.chunk_counter,
6351f1e2261SMartin Matuska 			    ctx->chunk.flags, cv_pair);
6361f1e2261SMartin Matuska 			hasher_push_cv(ctx, cv_pair, ctx->chunk.chunk_counter);
6371f1e2261SMartin Matuska 			hasher_push_cv(ctx, &cv_pair[BLAKE3_OUT_LEN],
6381f1e2261SMartin Matuska 			    ctx->chunk.chunk_counter + (subtree_chunks / 2));
6391f1e2261SMartin Matuska 		}
6401f1e2261SMartin Matuska 		ctx->chunk.chunk_counter += subtree_chunks;
6411f1e2261SMartin Matuska 		input_bytes += subtree_len;
6421f1e2261SMartin Matuska 		input_len -= subtree_len;
6431f1e2261SMartin Matuska 	}
6441f1e2261SMartin Matuska 
6451f1e2261SMartin Matuska 	/*
6461f1e2261SMartin Matuska 	 * If there's any remaining input less than a full chunk, add it to
6471f1e2261SMartin Matuska 	 * the chunk state. In that case, also do a final merge loop to make
6481f1e2261SMartin Matuska 	 * sure the subtree stack doesn't contain any unmerged pairs. The
6491f1e2261SMartin Matuska 	 * remaining input means we know these merges are non-root. This merge
6501f1e2261SMartin Matuska 	 * loop isn't strictly necessary here, because hasher_push_chunk_cv
6511f1e2261SMartin Matuska 	 * already does its own merge loop, but it simplifies
6521f1e2261SMartin Matuska 	 * blake3_hasher_finalize below.
6531f1e2261SMartin Matuska 	 */
6541f1e2261SMartin Matuska 	if (input_len > 0) {
6551f1e2261SMartin Matuska 		chunk_state_update(ctx->ops, &ctx->chunk, input_bytes,
6561f1e2261SMartin Matuska 		    input_len);
6571f1e2261SMartin Matuska 		hasher_merge_cv_stack(ctx, ctx->chunk.chunk_counter);
6581f1e2261SMartin Matuska 	}
6591f1e2261SMartin Matuska }
6601f1e2261SMartin Matuska 
6611f1e2261SMartin Matuska void
Blake3_Update(BLAKE3_CTX * ctx,const void * input,size_t todo)6621f1e2261SMartin Matuska Blake3_Update(BLAKE3_CTX *ctx, const void *input, size_t todo)
6631f1e2261SMartin Matuska {
6641f1e2261SMartin Matuska 	size_t done = 0;
6651f1e2261SMartin Matuska 	const uint8_t *data = input;
6661f1e2261SMartin Matuska 	const size_t block_max = 1024 * 64;
6671f1e2261SMartin Matuska 
6681f1e2261SMartin Matuska 	/* max feed buffer to leave the stack size small */
6691f1e2261SMartin Matuska 	while (todo != 0) {
6701f1e2261SMartin Matuska 		size_t block = (todo >= block_max) ? block_max : todo;
6711f1e2261SMartin Matuska 		Blake3_Update2(ctx, data + done, block);
6721f1e2261SMartin Matuska 		done += block;
6731f1e2261SMartin Matuska 		todo -= block;
6741f1e2261SMartin Matuska 	}
6751f1e2261SMartin Matuska }
6761f1e2261SMartin Matuska 
6771f1e2261SMartin Matuska void
Blake3_Final(const BLAKE3_CTX * ctx,uint8_t * out)6781f1e2261SMartin Matuska Blake3_Final(const BLAKE3_CTX *ctx, uint8_t *out)
6791f1e2261SMartin Matuska {
6801f1e2261SMartin Matuska 	Blake3_FinalSeek(ctx, 0, out, BLAKE3_OUT_LEN);
6811f1e2261SMartin Matuska }
6821f1e2261SMartin Matuska 
6831f1e2261SMartin Matuska void
Blake3_FinalSeek(const BLAKE3_CTX * ctx,uint64_t seek,uint8_t * out,size_t out_len)6841f1e2261SMartin Matuska Blake3_FinalSeek(const BLAKE3_CTX *ctx, uint64_t seek, uint8_t *out,
6851f1e2261SMartin Matuska     size_t out_len)
6861f1e2261SMartin Matuska {
6871f1e2261SMartin Matuska 	/*
6881f1e2261SMartin Matuska 	 * Explicitly checking for zero avoids causing UB by passing a null
6891f1e2261SMartin Matuska 	 * pointer to memcpy. This comes up in practice with things like:
6901f1e2261SMartin Matuska 	 *   std::vector<uint8_t> v;
6911f1e2261SMartin Matuska 	 *   blake3_hasher_finalize(&hasher, v.data(), v.size());
6921f1e2261SMartin Matuska 	 */
6931f1e2261SMartin Matuska 	if (out_len == 0) {
6941f1e2261SMartin Matuska 		return;
6951f1e2261SMartin Matuska 	}
6961f1e2261SMartin Matuska 	/* If the subtree stack is empty, then the current chunk is the root. */
6971f1e2261SMartin Matuska 	if (ctx->cv_stack_len == 0) {
6981f1e2261SMartin Matuska 		output_t output = chunk_state_output(&ctx->chunk);
6991f1e2261SMartin Matuska 		output_root_bytes(ctx->ops, &output, seek, out, out_len);
7001f1e2261SMartin Matuska 		return;
7011f1e2261SMartin Matuska 	}
7021f1e2261SMartin Matuska 	/*
7031f1e2261SMartin Matuska 	 * If there are any bytes in the chunk state, finalize that chunk and
7041f1e2261SMartin Matuska 	 * do a roll-up merge between that chunk hash and every subtree in the
7051f1e2261SMartin Matuska 	 * stack. In this case, the extra merge loop at the end of
7061f1e2261SMartin Matuska 	 * blake3_hasher_update guarantees that none of the subtrees in the
7071f1e2261SMartin Matuska 	 * stack need to be merged with each other first. Otherwise, if there
7081f1e2261SMartin Matuska 	 * are no bytes in the chunk state, then the top of the stack is a
7091f1e2261SMartin Matuska 	 * chunk hash, and we start the merge from that.
7101f1e2261SMartin Matuska 	 */
7111f1e2261SMartin Matuska 	output_t output;
7121f1e2261SMartin Matuska 	size_t cvs_remaining;
7131f1e2261SMartin Matuska 	if (chunk_state_len(&ctx->chunk) > 0) {
7141f1e2261SMartin Matuska 		cvs_remaining = ctx->cv_stack_len;
7151f1e2261SMartin Matuska 		output = chunk_state_output(&ctx->chunk);
7161f1e2261SMartin Matuska 	} else {
7171f1e2261SMartin Matuska 		/* There are always at least 2 CVs in the stack in this case. */
7181f1e2261SMartin Matuska 		cvs_remaining = ctx->cv_stack_len - 2;
7191f1e2261SMartin Matuska 		output = parent_output(&ctx->cv_stack[cvs_remaining * 32],
7201f1e2261SMartin Matuska 		    ctx->key, ctx->chunk.flags);
7211f1e2261SMartin Matuska 	}
7221f1e2261SMartin Matuska 	while (cvs_remaining > 0) {
7231f1e2261SMartin Matuska 		cvs_remaining -= 1;
7241f1e2261SMartin Matuska 		uint8_t parent_block[BLAKE3_BLOCK_LEN];
7251f1e2261SMartin Matuska 		memcpy(parent_block, &ctx->cv_stack[cvs_remaining * 32], 32);
7261f1e2261SMartin Matuska 		output_chaining_value(ctx->ops, &output, &parent_block[32]);
7271f1e2261SMartin Matuska 		output = parent_output(parent_block, ctx->key,
7281f1e2261SMartin Matuska 		    ctx->chunk.flags);
7291f1e2261SMartin Matuska 	}
7301f1e2261SMartin Matuska 	output_root_bytes(ctx->ops, &output, seek, out, out_len);
7311f1e2261SMartin Matuska }
732