181ad6265SDimitry Andric /*===-- blake3.c - BLAKE3 C Implementation ------------------------*- C -*-===*\ 281ad6265SDimitry Andric |* *| 381ad6265SDimitry Andric |* Released into the public domain with CC0 1.0 *| 481ad6265SDimitry Andric |* See 'llvm/lib/Support/BLAKE3/LICENSE' for info. *| 581ad6265SDimitry Andric |* SPDX-License-Identifier: CC0-1.0 *| 681ad6265SDimitry Andric |* *| 781ad6265SDimitry Andric \*===----------------------------------------------------------------------===*/ 881ad6265SDimitry Andric 981ad6265SDimitry Andric #include <assert.h> 1081ad6265SDimitry Andric #include <stdbool.h> 1181ad6265SDimitry Andric #include <string.h> 1281ad6265SDimitry Andric 1381ad6265SDimitry Andric #include "blake3_impl.h" 1481ad6265SDimitry Andric 1581ad6265SDimitry Andric const char *llvm_blake3_version(void) { return BLAKE3_VERSION_STRING; } 1681ad6265SDimitry Andric 1781ad6265SDimitry Andric INLINE void chunk_state_init(blake3_chunk_state *self, const uint32_t key[8], 1881ad6265SDimitry Andric uint8_t flags) { 1981ad6265SDimitry Andric memcpy(self->cv, key, BLAKE3_KEY_LEN); 2081ad6265SDimitry Andric self->chunk_counter = 0; 2181ad6265SDimitry Andric memset(self->buf, 0, BLAKE3_BLOCK_LEN); 2281ad6265SDimitry Andric self->buf_len = 0; 2381ad6265SDimitry Andric self->blocks_compressed = 0; 2481ad6265SDimitry Andric self->flags = flags; 2581ad6265SDimitry Andric } 2681ad6265SDimitry Andric 2781ad6265SDimitry Andric INLINE void chunk_state_reset(blake3_chunk_state *self, const uint32_t key[8], 2881ad6265SDimitry Andric uint64_t chunk_counter) { 2981ad6265SDimitry Andric memcpy(self->cv, key, BLAKE3_KEY_LEN); 3081ad6265SDimitry Andric self->chunk_counter = chunk_counter; 3181ad6265SDimitry Andric self->blocks_compressed = 0; 3281ad6265SDimitry Andric memset(self->buf, 0, BLAKE3_BLOCK_LEN); 3381ad6265SDimitry Andric self->buf_len = 0; 3481ad6265SDimitry Andric } 3581ad6265SDimitry Andric 3681ad6265SDimitry Andric INLINE size_t chunk_state_len(const blake3_chunk_state *self) { 3781ad6265SDimitry Andric return (BLAKE3_BLOCK_LEN * (size_t)self->blocks_compressed) + 3881ad6265SDimitry Andric ((size_t)self->buf_len); 3981ad6265SDimitry Andric } 4081ad6265SDimitry Andric 4181ad6265SDimitry Andric INLINE size_t chunk_state_fill_buf(blake3_chunk_state *self, 4281ad6265SDimitry Andric const uint8_t *input, size_t input_len) { 4381ad6265SDimitry Andric size_t take = BLAKE3_BLOCK_LEN - ((size_t)self->buf_len); 4481ad6265SDimitry Andric if (take > input_len) { 4581ad6265SDimitry Andric take = input_len; 4681ad6265SDimitry Andric } 4781ad6265SDimitry Andric uint8_t *dest = self->buf + ((size_t)self->buf_len); 4881ad6265SDimitry Andric memcpy(dest, input, take); 4981ad6265SDimitry Andric self->buf_len += (uint8_t)take; 5081ad6265SDimitry Andric return take; 5181ad6265SDimitry Andric } 5281ad6265SDimitry Andric 5381ad6265SDimitry Andric INLINE uint8_t chunk_state_maybe_start_flag(const blake3_chunk_state *self) { 5481ad6265SDimitry Andric if (self->blocks_compressed == 0) { 5581ad6265SDimitry Andric return CHUNK_START; 5681ad6265SDimitry Andric } else { 5781ad6265SDimitry Andric return 0; 5881ad6265SDimitry Andric } 5981ad6265SDimitry Andric } 6081ad6265SDimitry Andric 6181ad6265SDimitry Andric typedef struct { 6281ad6265SDimitry Andric uint32_t input_cv[8]; 6381ad6265SDimitry Andric uint64_t counter; 6481ad6265SDimitry Andric uint8_t block[BLAKE3_BLOCK_LEN]; 6581ad6265SDimitry Andric uint8_t block_len; 6681ad6265SDimitry Andric uint8_t flags; 6781ad6265SDimitry Andric } output_t; 6881ad6265SDimitry Andric 6981ad6265SDimitry Andric INLINE output_t make_output(const uint32_t input_cv[8], 7081ad6265SDimitry Andric const uint8_t block[BLAKE3_BLOCK_LEN], 7181ad6265SDimitry Andric uint8_t block_len, uint64_t counter, 7281ad6265SDimitry Andric uint8_t flags) { 7381ad6265SDimitry Andric output_t ret; 7481ad6265SDimitry Andric memcpy(ret.input_cv, input_cv, 32); 7581ad6265SDimitry Andric memcpy(ret.block, block, BLAKE3_BLOCK_LEN); 7681ad6265SDimitry Andric ret.block_len = block_len; 7781ad6265SDimitry Andric ret.counter = counter; 7881ad6265SDimitry Andric ret.flags = flags; 7981ad6265SDimitry Andric return ret; 8081ad6265SDimitry Andric } 8181ad6265SDimitry Andric 8281ad6265SDimitry Andric // Chaining values within a given chunk (specifically the compress_in_place 8381ad6265SDimitry Andric // interface) are represented as words. This avoids unnecessary bytes<->words 8481ad6265SDimitry Andric // conversion overhead in the portable implementation. However, the hash_many 8581ad6265SDimitry Andric // interface handles both user input and parent node blocks, so it accepts 8681ad6265SDimitry Andric // bytes. For that reason, chaining values in the CV stack are represented as 8781ad6265SDimitry Andric // bytes. 8881ad6265SDimitry Andric INLINE void output_chaining_value(const output_t *self, uint8_t cv[32]) { 8981ad6265SDimitry Andric uint32_t cv_words[8]; 9081ad6265SDimitry Andric memcpy(cv_words, self->input_cv, 32); 9181ad6265SDimitry Andric blake3_compress_in_place(cv_words, self->block, self->block_len, 9281ad6265SDimitry Andric self->counter, self->flags); 9381ad6265SDimitry Andric store_cv_words(cv, cv_words); 9481ad6265SDimitry Andric } 9581ad6265SDimitry Andric 9681ad6265SDimitry Andric INLINE void output_root_bytes(const output_t *self, uint64_t seek, uint8_t *out, 9781ad6265SDimitry Andric size_t out_len) { 9881ad6265SDimitry Andric uint64_t output_block_counter = seek / 64; 9981ad6265SDimitry Andric size_t offset_within_block = seek % 64; 10081ad6265SDimitry Andric uint8_t wide_buf[64]; 10181ad6265SDimitry Andric while (out_len > 0) { 10281ad6265SDimitry Andric blake3_compress_xof(self->input_cv, self->block, self->block_len, 10381ad6265SDimitry Andric output_block_counter, self->flags | ROOT, wide_buf); 10481ad6265SDimitry Andric size_t available_bytes = 64 - offset_within_block; 10581ad6265SDimitry Andric size_t memcpy_len; 10681ad6265SDimitry Andric if (out_len > available_bytes) { 10781ad6265SDimitry Andric memcpy_len = available_bytes; 10881ad6265SDimitry Andric } else { 10981ad6265SDimitry Andric memcpy_len = out_len; 11081ad6265SDimitry Andric } 11181ad6265SDimitry Andric memcpy(out, wide_buf + offset_within_block, memcpy_len); 11281ad6265SDimitry Andric out += memcpy_len; 11381ad6265SDimitry Andric out_len -= memcpy_len; 11481ad6265SDimitry Andric output_block_counter += 1; 11581ad6265SDimitry Andric offset_within_block = 0; 11681ad6265SDimitry Andric } 11781ad6265SDimitry Andric } 11881ad6265SDimitry Andric 11981ad6265SDimitry Andric INLINE void chunk_state_update(blake3_chunk_state *self, const uint8_t *input, 12081ad6265SDimitry Andric size_t input_len) { 12181ad6265SDimitry Andric if (self->buf_len > 0) { 12281ad6265SDimitry Andric size_t take = chunk_state_fill_buf(self, input, input_len); 12381ad6265SDimitry Andric input += take; 12481ad6265SDimitry Andric input_len -= take; 12581ad6265SDimitry Andric if (input_len > 0) { 12681ad6265SDimitry Andric blake3_compress_in_place( 12781ad6265SDimitry Andric self->cv, self->buf, BLAKE3_BLOCK_LEN, self->chunk_counter, 12881ad6265SDimitry Andric self->flags | chunk_state_maybe_start_flag(self)); 12981ad6265SDimitry Andric self->blocks_compressed += 1; 13081ad6265SDimitry Andric self->buf_len = 0; 13181ad6265SDimitry Andric memset(self->buf, 0, BLAKE3_BLOCK_LEN); 13281ad6265SDimitry Andric } 13381ad6265SDimitry Andric } 13481ad6265SDimitry Andric 13581ad6265SDimitry Andric while (input_len > BLAKE3_BLOCK_LEN) { 13681ad6265SDimitry Andric blake3_compress_in_place(self->cv, input, BLAKE3_BLOCK_LEN, 13781ad6265SDimitry Andric self->chunk_counter, 13881ad6265SDimitry Andric self->flags | chunk_state_maybe_start_flag(self)); 13981ad6265SDimitry Andric self->blocks_compressed += 1; 14081ad6265SDimitry Andric input += BLAKE3_BLOCK_LEN; 14181ad6265SDimitry Andric input_len -= BLAKE3_BLOCK_LEN; 14281ad6265SDimitry Andric } 14381ad6265SDimitry Andric 144*0fca6ea1SDimitry Andric chunk_state_fill_buf(self, input, input_len); 14581ad6265SDimitry Andric } 14681ad6265SDimitry Andric 14781ad6265SDimitry Andric INLINE output_t chunk_state_output(const blake3_chunk_state *self) { 14881ad6265SDimitry Andric uint8_t block_flags = 14981ad6265SDimitry Andric self->flags | chunk_state_maybe_start_flag(self) | CHUNK_END; 15081ad6265SDimitry Andric return make_output(self->cv, self->buf, self->buf_len, self->chunk_counter, 15181ad6265SDimitry Andric block_flags); 15281ad6265SDimitry Andric } 15381ad6265SDimitry Andric 15481ad6265SDimitry Andric INLINE output_t parent_output(const uint8_t block[BLAKE3_BLOCK_LEN], 15581ad6265SDimitry Andric const uint32_t key[8], uint8_t flags) { 15681ad6265SDimitry Andric return make_output(key, block, BLAKE3_BLOCK_LEN, 0, flags | PARENT); 15781ad6265SDimitry Andric } 15881ad6265SDimitry Andric 15981ad6265SDimitry Andric // Given some input larger than one chunk, return the number of bytes that 16081ad6265SDimitry Andric // should go in the left subtree. This is the largest power-of-2 number of 16181ad6265SDimitry Andric // chunks that leaves at least 1 byte for the right subtree. 16281ad6265SDimitry Andric INLINE size_t left_len(size_t content_len) { 16381ad6265SDimitry Andric // Subtract 1 to reserve at least one byte for the right side. content_len 16481ad6265SDimitry Andric // should always be greater than BLAKE3_CHUNK_LEN. 16581ad6265SDimitry Andric size_t full_chunks = (content_len - 1) / BLAKE3_CHUNK_LEN; 16681ad6265SDimitry Andric return round_down_to_power_of_2(full_chunks) * BLAKE3_CHUNK_LEN; 16781ad6265SDimitry Andric } 16881ad6265SDimitry Andric 16981ad6265SDimitry Andric // Use SIMD parallelism to hash up to MAX_SIMD_DEGREE chunks at the same time 17081ad6265SDimitry Andric // on a single thread. Write out the chunk chaining values and return the 17181ad6265SDimitry Andric // number of chunks hashed. These chunks are never the root and never empty; 17281ad6265SDimitry Andric // those cases use a different codepath. 17381ad6265SDimitry Andric INLINE size_t compress_chunks_parallel(const uint8_t *input, size_t input_len, 17481ad6265SDimitry Andric const uint32_t key[8], 17581ad6265SDimitry Andric uint64_t chunk_counter, uint8_t flags, 17681ad6265SDimitry Andric uint8_t *out) { 17781ad6265SDimitry Andric #if defined(BLAKE3_TESTING) 17881ad6265SDimitry Andric assert(0 < input_len); 17981ad6265SDimitry Andric assert(input_len <= MAX_SIMD_DEGREE * BLAKE3_CHUNK_LEN); 18081ad6265SDimitry Andric #endif 18181ad6265SDimitry Andric 18281ad6265SDimitry Andric const uint8_t *chunks_array[MAX_SIMD_DEGREE]; 18381ad6265SDimitry Andric size_t input_position = 0; 18481ad6265SDimitry Andric size_t chunks_array_len = 0; 18581ad6265SDimitry Andric while (input_len - input_position >= BLAKE3_CHUNK_LEN) { 18681ad6265SDimitry Andric chunks_array[chunks_array_len] = &input[input_position]; 18781ad6265SDimitry Andric input_position += BLAKE3_CHUNK_LEN; 18881ad6265SDimitry Andric chunks_array_len += 1; 18981ad6265SDimitry Andric } 19081ad6265SDimitry Andric 19181ad6265SDimitry Andric blake3_hash_many(chunks_array, chunks_array_len, 19281ad6265SDimitry Andric BLAKE3_CHUNK_LEN / BLAKE3_BLOCK_LEN, key, chunk_counter, 19381ad6265SDimitry Andric true, flags, CHUNK_START, CHUNK_END, out); 19481ad6265SDimitry Andric 19581ad6265SDimitry Andric // Hash the remaining partial chunk, if there is one. Note that the empty 19681ad6265SDimitry Andric // chunk (meaning the empty message) is a different codepath. 19781ad6265SDimitry Andric if (input_len > input_position) { 19881ad6265SDimitry Andric uint64_t counter = chunk_counter + (uint64_t)chunks_array_len; 19981ad6265SDimitry Andric blake3_chunk_state chunk_state; 20081ad6265SDimitry Andric chunk_state_init(&chunk_state, key, flags); 20181ad6265SDimitry Andric chunk_state.chunk_counter = counter; 20281ad6265SDimitry Andric chunk_state_update(&chunk_state, &input[input_position], 20381ad6265SDimitry Andric input_len - input_position); 20481ad6265SDimitry Andric output_t output = chunk_state_output(&chunk_state); 20581ad6265SDimitry Andric output_chaining_value(&output, &out[chunks_array_len * BLAKE3_OUT_LEN]); 20681ad6265SDimitry Andric return chunks_array_len + 1; 20781ad6265SDimitry Andric } else { 20881ad6265SDimitry Andric return chunks_array_len; 20981ad6265SDimitry Andric } 21081ad6265SDimitry Andric } 21181ad6265SDimitry Andric 21281ad6265SDimitry Andric // Use SIMD parallelism to hash up to MAX_SIMD_DEGREE parents at the same time 21381ad6265SDimitry Andric // on a single thread. Write out the parent chaining values and return the 21481ad6265SDimitry Andric // number of parents hashed. (If there's an odd input chaining value left over, 21581ad6265SDimitry Andric // return it as an additional output.) These parents are never the root and 21681ad6265SDimitry Andric // never empty; those cases use a different codepath. 21781ad6265SDimitry Andric INLINE size_t compress_parents_parallel(const uint8_t *child_chaining_values, 21881ad6265SDimitry Andric size_t num_chaining_values, 21981ad6265SDimitry Andric const uint32_t key[8], uint8_t flags, 22081ad6265SDimitry Andric uint8_t *out) { 22181ad6265SDimitry Andric #if defined(BLAKE3_TESTING) 22281ad6265SDimitry Andric assert(2 <= num_chaining_values); 22381ad6265SDimitry Andric assert(num_chaining_values <= 2 * MAX_SIMD_DEGREE_OR_2); 22481ad6265SDimitry Andric #endif 22581ad6265SDimitry Andric 22681ad6265SDimitry Andric const uint8_t *parents_array[MAX_SIMD_DEGREE_OR_2]; 22781ad6265SDimitry Andric size_t parents_array_len = 0; 22881ad6265SDimitry Andric while (num_chaining_values - (2 * parents_array_len) >= 2) { 22981ad6265SDimitry Andric parents_array[parents_array_len] = 23081ad6265SDimitry Andric &child_chaining_values[2 * parents_array_len * BLAKE3_OUT_LEN]; 23181ad6265SDimitry Andric parents_array_len += 1; 23281ad6265SDimitry Andric } 23381ad6265SDimitry Andric 23481ad6265SDimitry Andric blake3_hash_many(parents_array, parents_array_len, 1, key, 23581ad6265SDimitry Andric 0, // Parents always use counter 0. 23681ad6265SDimitry Andric false, flags | PARENT, 23781ad6265SDimitry Andric 0, // Parents have no start flags. 23881ad6265SDimitry Andric 0, // Parents have no end flags. 23981ad6265SDimitry Andric out); 24081ad6265SDimitry Andric 24181ad6265SDimitry Andric // If there's an odd child left over, it becomes an output. 24281ad6265SDimitry Andric if (num_chaining_values > 2 * parents_array_len) { 24381ad6265SDimitry Andric memcpy(&out[parents_array_len * BLAKE3_OUT_LEN], 24481ad6265SDimitry Andric &child_chaining_values[2 * parents_array_len * BLAKE3_OUT_LEN], 24581ad6265SDimitry Andric BLAKE3_OUT_LEN); 24681ad6265SDimitry Andric return parents_array_len + 1; 24781ad6265SDimitry Andric } else { 24881ad6265SDimitry Andric return parents_array_len; 24981ad6265SDimitry Andric } 25081ad6265SDimitry Andric } 25181ad6265SDimitry Andric 25281ad6265SDimitry Andric // The wide helper function returns (writes out) an array of chaining values 25381ad6265SDimitry Andric // and returns the length of that array. The number of chaining values returned 25481ad6265SDimitry Andric // is the dyanmically detected SIMD degree, at most MAX_SIMD_DEGREE. Or fewer, 25581ad6265SDimitry Andric // if the input is shorter than that many chunks. The reason for maintaining a 25681ad6265SDimitry Andric // wide array of chaining values going back up the tree, is to allow the 25781ad6265SDimitry Andric // implementation to hash as many parents in parallel as possible. 25881ad6265SDimitry Andric // 25981ad6265SDimitry Andric // As a special case when the SIMD degree is 1, this function will still return 26081ad6265SDimitry Andric // at least 2 outputs. This guarantees that this function doesn't perform the 26181ad6265SDimitry Andric // root compression. (If it did, it would use the wrong flags, and also we 26281ad6265SDimitry Andric // wouldn't be able to implement exendable ouput.) Note that this function is 26381ad6265SDimitry Andric // not used when the whole input is only 1 chunk long; that's a different 26481ad6265SDimitry Andric // codepath. 26581ad6265SDimitry Andric // 26681ad6265SDimitry Andric // Why not just have the caller split the input on the first update(), instead 26781ad6265SDimitry Andric // of implementing this special rule? Because we don't want to limit SIMD or 26881ad6265SDimitry Andric // multi-threading parallelism for that update(). 26981ad6265SDimitry Andric static size_t blake3_compress_subtree_wide(const uint8_t *input, 27081ad6265SDimitry Andric size_t input_len, 27181ad6265SDimitry Andric const uint32_t key[8], 27281ad6265SDimitry Andric uint64_t chunk_counter, 27381ad6265SDimitry Andric uint8_t flags, uint8_t *out) { 27481ad6265SDimitry Andric // Note that the single chunk case does *not* bump the SIMD degree up to 2 27581ad6265SDimitry Andric // when it is 1. If this implementation adds multi-threading in the future, 27681ad6265SDimitry Andric // this gives us the option of multi-threading even the 2-chunk case, which 27781ad6265SDimitry Andric // can help performance on smaller platforms. 27881ad6265SDimitry Andric if (input_len <= blake3_simd_degree() * BLAKE3_CHUNK_LEN) { 27981ad6265SDimitry Andric return compress_chunks_parallel(input, input_len, key, chunk_counter, flags, 28081ad6265SDimitry Andric out); 28181ad6265SDimitry Andric } 28281ad6265SDimitry Andric 28381ad6265SDimitry Andric // With more than simd_degree chunks, we need to recurse. Start by dividing 28481ad6265SDimitry Andric // the input into left and right subtrees. (Note that this is only optimal 28581ad6265SDimitry Andric // as long as the SIMD degree is a power of 2. If we ever get a SIMD degree 28681ad6265SDimitry Andric // of 3 or something, we'll need a more complicated strategy.) 28781ad6265SDimitry Andric size_t left_input_len = left_len(input_len); 28881ad6265SDimitry Andric size_t right_input_len = input_len - left_input_len; 28981ad6265SDimitry Andric const uint8_t *right_input = &input[left_input_len]; 29081ad6265SDimitry Andric uint64_t right_chunk_counter = 29181ad6265SDimitry Andric chunk_counter + (uint64_t)(left_input_len / BLAKE3_CHUNK_LEN); 29281ad6265SDimitry Andric 29381ad6265SDimitry Andric // Make space for the child outputs. Here we use MAX_SIMD_DEGREE_OR_2 to 29481ad6265SDimitry Andric // account for the special case of returning 2 outputs when the SIMD degree 29581ad6265SDimitry Andric // is 1. 29681ad6265SDimitry Andric uint8_t cv_array[2 * MAX_SIMD_DEGREE_OR_2 * BLAKE3_OUT_LEN]; 29781ad6265SDimitry Andric size_t degree = blake3_simd_degree(); 29881ad6265SDimitry Andric if (left_input_len > BLAKE3_CHUNK_LEN && degree == 1) { 29981ad6265SDimitry Andric // The special case: We always use a degree of at least two, to make 30081ad6265SDimitry Andric // sure there are two outputs. Except, as noted above, at the chunk 30181ad6265SDimitry Andric // level, where we allow degree=1. (Note that the 1-chunk-input case is 30281ad6265SDimitry Andric // a different codepath.) 30381ad6265SDimitry Andric degree = 2; 30481ad6265SDimitry Andric } 30581ad6265SDimitry Andric uint8_t *right_cvs = &cv_array[degree * BLAKE3_OUT_LEN]; 30681ad6265SDimitry Andric 30781ad6265SDimitry Andric // Recurse! If this implementation adds multi-threading support in the 30881ad6265SDimitry Andric // future, this is where it will go. 30981ad6265SDimitry Andric size_t left_n = blake3_compress_subtree_wide(input, left_input_len, key, 31081ad6265SDimitry Andric chunk_counter, flags, cv_array); 31181ad6265SDimitry Andric size_t right_n = blake3_compress_subtree_wide( 31281ad6265SDimitry Andric right_input, right_input_len, key, right_chunk_counter, flags, right_cvs); 31381ad6265SDimitry Andric 31481ad6265SDimitry Andric // The special case again. If simd_degree=1, then we'll have left_n=1 and 31581ad6265SDimitry Andric // right_n=1. Rather than compressing them into a single output, return 31681ad6265SDimitry Andric // them directly, to make sure we always have at least two outputs. 31781ad6265SDimitry Andric if (left_n == 1) { 31881ad6265SDimitry Andric memcpy(out, cv_array, 2 * BLAKE3_OUT_LEN); 31981ad6265SDimitry Andric return 2; 32081ad6265SDimitry Andric } 32181ad6265SDimitry Andric 32281ad6265SDimitry Andric // Otherwise, do one layer of parent node compression. 32381ad6265SDimitry Andric size_t num_chaining_values = left_n + right_n; 32481ad6265SDimitry Andric return compress_parents_parallel(cv_array, num_chaining_values, key, flags, 32581ad6265SDimitry Andric out); 32681ad6265SDimitry Andric } 32781ad6265SDimitry Andric 32881ad6265SDimitry Andric // Hash a subtree with compress_subtree_wide(), and then condense the resulting 32981ad6265SDimitry Andric // list of chaining values down to a single parent node. Don't compress that 33081ad6265SDimitry Andric // last parent node, however. Instead, return its message bytes (the 33181ad6265SDimitry Andric // concatenated chaining values of its children). This is necessary when the 33281ad6265SDimitry Andric // first call to update() supplies a complete subtree, because the topmost 33381ad6265SDimitry Andric // parent node of that subtree could end up being the root. It's also necessary 33481ad6265SDimitry Andric // for extended output in the general case. 33581ad6265SDimitry Andric // 33681ad6265SDimitry Andric // As with compress_subtree_wide(), this function is not used on inputs of 1 33781ad6265SDimitry Andric // chunk or less. That's a different codepath. 33881ad6265SDimitry Andric INLINE void compress_subtree_to_parent_node( 33981ad6265SDimitry Andric const uint8_t *input, size_t input_len, const uint32_t key[8], 34081ad6265SDimitry Andric uint64_t chunk_counter, uint8_t flags, uint8_t out[2 * BLAKE3_OUT_LEN]) { 34181ad6265SDimitry Andric #if defined(BLAKE3_TESTING) 34281ad6265SDimitry Andric assert(input_len > BLAKE3_CHUNK_LEN); 34381ad6265SDimitry Andric #endif 34481ad6265SDimitry Andric 34581ad6265SDimitry Andric uint8_t cv_array[MAX_SIMD_DEGREE_OR_2 * BLAKE3_OUT_LEN]; 34681ad6265SDimitry Andric size_t num_cvs = blake3_compress_subtree_wide(input, input_len, key, 34781ad6265SDimitry Andric chunk_counter, flags, cv_array); 34881ad6265SDimitry Andric assert(num_cvs <= MAX_SIMD_DEGREE_OR_2); 34981ad6265SDimitry Andric 35081ad6265SDimitry Andric // If MAX_SIMD_DEGREE is greater than 2 and there's enough input, 35181ad6265SDimitry Andric // compress_subtree_wide() returns more than 2 chaining values. Condense 35281ad6265SDimitry Andric // them into 2 by forming parent nodes repeatedly. 35381ad6265SDimitry Andric uint8_t out_array[MAX_SIMD_DEGREE_OR_2 * BLAKE3_OUT_LEN / 2]; 35481ad6265SDimitry Andric // The second half of this loop condition is always true, and we just 35581ad6265SDimitry Andric // asserted it above. But GCC can't tell that it's always true, and if NDEBUG 35681ad6265SDimitry Andric // is set on platforms where MAX_SIMD_DEGREE_OR_2 == 2, GCC emits spurious 35781ad6265SDimitry Andric // warnings here. GCC 8.5 is particularly sensitive, so if you're changing 35881ad6265SDimitry Andric // this code, test it against that version. 35981ad6265SDimitry Andric while (num_cvs > 2 && num_cvs <= MAX_SIMD_DEGREE_OR_2) { 36081ad6265SDimitry Andric num_cvs = 36181ad6265SDimitry Andric compress_parents_parallel(cv_array, num_cvs, key, flags, out_array); 36281ad6265SDimitry Andric memcpy(cv_array, out_array, num_cvs * BLAKE3_OUT_LEN); 36381ad6265SDimitry Andric } 36481ad6265SDimitry Andric memcpy(out, cv_array, 2 * BLAKE3_OUT_LEN); 36581ad6265SDimitry Andric } 36681ad6265SDimitry Andric 36781ad6265SDimitry Andric INLINE void hasher_init_base(blake3_hasher *self, const uint32_t key[8], 36881ad6265SDimitry Andric uint8_t flags) { 36981ad6265SDimitry Andric memcpy(self->key, key, BLAKE3_KEY_LEN); 37081ad6265SDimitry Andric chunk_state_init(&self->chunk, key, flags); 37181ad6265SDimitry Andric self->cv_stack_len = 0; 37281ad6265SDimitry Andric } 37381ad6265SDimitry Andric 37481ad6265SDimitry Andric void llvm_blake3_hasher_init(blake3_hasher *self) { hasher_init_base(self, IV, 0); } 37581ad6265SDimitry Andric 37681ad6265SDimitry Andric void llvm_blake3_hasher_init_keyed(blake3_hasher *self, 37781ad6265SDimitry Andric const uint8_t key[BLAKE3_KEY_LEN]) { 37881ad6265SDimitry Andric uint32_t key_words[8]; 37981ad6265SDimitry Andric load_key_words(key, key_words); 38081ad6265SDimitry Andric hasher_init_base(self, key_words, KEYED_HASH); 38181ad6265SDimitry Andric } 38281ad6265SDimitry Andric 38381ad6265SDimitry Andric void llvm_blake3_hasher_init_derive_key_raw(blake3_hasher *self, const void *context, 38481ad6265SDimitry Andric size_t context_len) { 38581ad6265SDimitry Andric blake3_hasher context_hasher; 38681ad6265SDimitry Andric hasher_init_base(&context_hasher, IV, DERIVE_KEY_CONTEXT); 38781ad6265SDimitry Andric llvm_blake3_hasher_update(&context_hasher, context, context_len); 38881ad6265SDimitry Andric uint8_t context_key[BLAKE3_KEY_LEN]; 38981ad6265SDimitry Andric llvm_blake3_hasher_finalize(&context_hasher, context_key, BLAKE3_KEY_LEN); 39081ad6265SDimitry Andric uint32_t context_key_words[8]; 39181ad6265SDimitry Andric load_key_words(context_key, context_key_words); 39281ad6265SDimitry Andric hasher_init_base(self, context_key_words, DERIVE_KEY_MATERIAL); 39381ad6265SDimitry Andric } 39481ad6265SDimitry Andric 39581ad6265SDimitry Andric void llvm_blake3_hasher_init_derive_key(blake3_hasher *self, const char *context) { 39681ad6265SDimitry Andric llvm_blake3_hasher_init_derive_key_raw(self, context, strlen(context)); 39781ad6265SDimitry Andric } 39881ad6265SDimitry Andric 39981ad6265SDimitry Andric // As described in hasher_push_cv() below, we do "lazy merging", delaying 40081ad6265SDimitry Andric // merges until right before the next CV is about to be added. This is 40181ad6265SDimitry Andric // different from the reference implementation. Another difference is that we 40281ad6265SDimitry Andric // aren't always merging 1 chunk at a time. Instead, each CV might represent 40381ad6265SDimitry Andric // any power-of-two number of chunks, as long as the smaller-above-larger stack 40481ad6265SDimitry Andric // order is maintained. Instead of the "count the trailing 0-bits" algorithm 40581ad6265SDimitry Andric // described in the spec, we use a "count the total number of 1-bits" variant 40681ad6265SDimitry Andric // that doesn't require us to retain the subtree size of the CV on top of the 40781ad6265SDimitry Andric // stack. The principle is the same: each CV that should remain in the stack is 40881ad6265SDimitry Andric // represented by a 1-bit in the total number of chunks (or bytes) so far. 40981ad6265SDimitry Andric INLINE void hasher_merge_cv_stack(blake3_hasher *self, uint64_t total_len) { 41081ad6265SDimitry Andric size_t post_merge_stack_len = (size_t)popcnt(total_len); 41181ad6265SDimitry Andric while (self->cv_stack_len > post_merge_stack_len) { 41281ad6265SDimitry Andric uint8_t *parent_node = 41381ad6265SDimitry Andric &self->cv_stack[(self->cv_stack_len - 2) * BLAKE3_OUT_LEN]; 41481ad6265SDimitry Andric output_t output = parent_output(parent_node, self->key, self->chunk.flags); 41581ad6265SDimitry Andric output_chaining_value(&output, parent_node); 41681ad6265SDimitry Andric self->cv_stack_len -= 1; 41781ad6265SDimitry Andric } 41881ad6265SDimitry Andric } 41981ad6265SDimitry Andric 42081ad6265SDimitry Andric // In reference_impl.rs, we merge the new CV with existing CVs from the stack 42181ad6265SDimitry Andric // before pushing it. We can do that because we know more input is coming, so 42281ad6265SDimitry Andric // we know none of the merges are root. 42381ad6265SDimitry Andric // 42481ad6265SDimitry Andric // This setting is different. We want to feed as much input as possible to 42581ad6265SDimitry Andric // compress_subtree_wide(), without setting aside anything for the chunk_state. 42681ad6265SDimitry Andric // If the user gives us 64 KiB, we want to parallelize over all 64 KiB at once 42781ad6265SDimitry Andric // as a single subtree, if at all possible. 42881ad6265SDimitry Andric // 42981ad6265SDimitry Andric // This leads to two problems: 43081ad6265SDimitry Andric // 1) This 64 KiB input might be the only call that ever gets made to update. 43181ad6265SDimitry Andric // In this case, the root node of the 64 KiB subtree would be the root node 43281ad6265SDimitry Andric // of the whole tree, and it would need to be ROOT finalized. We can't 43381ad6265SDimitry Andric // compress it until we know. 43481ad6265SDimitry Andric // 2) This 64 KiB input might complete a larger tree, whose root node is 43581ad6265SDimitry Andric // similarly going to be the the root of the whole tree. For example, maybe 43681ad6265SDimitry Andric // we have 196 KiB (that is, 128 + 64) hashed so far. We can't compress the 43781ad6265SDimitry Andric // node at the root of the 256 KiB subtree until we know how to finalize it. 43881ad6265SDimitry Andric // 43981ad6265SDimitry Andric // The second problem is solved with "lazy merging". That is, when we're about 44081ad6265SDimitry Andric // to add a CV to the stack, we don't merge it with anything first, as the 44181ad6265SDimitry Andric // reference impl does. Instead we do merges using the *previous* CV that was 44281ad6265SDimitry Andric // added, which is sitting on top of the stack, and we put the new CV 44381ad6265SDimitry Andric // (unmerged) on top of the stack afterwards. This guarantees that we never 44481ad6265SDimitry Andric // merge the root node until finalize(). 44581ad6265SDimitry Andric // 44681ad6265SDimitry Andric // Solving the first problem requires an additional tool, 44781ad6265SDimitry Andric // compress_subtree_to_parent_node(). That function always returns the top 44881ad6265SDimitry Andric // *two* chaining values of the subtree it's compressing. We then do lazy 44981ad6265SDimitry Andric // merging with each of them separately, so that the second CV will always 45081ad6265SDimitry Andric // remain unmerged. (That also helps us support extendable output when we're 45181ad6265SDimitry Andric // hashing an input all-at-once.) 45281ad6265SDimitry Andric INLINE void hasher_push_cv(blake3_hasher *self, uint8_t new_cv[BLAKE3_OUT_LEN], 45381ad6265SDimitry Andric uint64_t chunk_counter) { 45481ad6265SDimitry Andric hasher_merge_cv_stack(self, chunk_counter); 45581ad6265SDimitry Andric memcpy(&self->cv_stack[self->cv_stack_len * BLAKE3_OUT_LEN], new_cv, 45681ad6265SDimitry Andric BLAKE3_OUT_LEN); 45781ad6265SDimitry Andric self->cv_stack_len += 1; 45881ad6265SDimitry Andric } 45981ad6265SDimitry Andric 46081ad6265SDimitry Andric void llvm_blake3_hasher_update(blake3_hasher *self, const void *input, 46181ad6265SDimitry Andric size_t input_len) { 46281ad6265SDimitry Andric // Explicitly checking for zero avoids causing UB by passing a null pointer 46381ad6265SDimitry Andric // to memcpy. This comes up in practice with things like: 46481ad6265SDimitry Andric // std::vector<uint8_t> v; 46581ad6265SDimitry Andric // blake3_hasher_update(&hasher, v.data(), v.size()); 46681ad6265SDimitry Andric if (input_len == 0) { 46781ad6265SDimitry Andric return; 46881ad6265SDimitry Andric } 46981ad6265SDimitry Andric 47081ad6265SDimitry Andric const uint8_t *input_bytes = (const uint8_t *)input; 47181ad6265SDimitry Andric 47281ad6265SDimitry Andric // If we have some partial chunk bytes in the internal chunk_state, we need 47381ad6265SDimitry Andric // to finish that chunk first. 47481ad6265SDimitry Andric if (chunk_state_len(&self->chunk) > 0) { 47581ad6265SDimitry Andric size_t take = BLAKE3_CHUNK_LEN - chunk_state_len(&self->chunk); 47681ad6265SDimitry Andric if (take > input_len) { 47781ad6265SDimitry Andric take = input_len; 47881ad6265SDimitry Andric } 47981ad6265SDimitry Andric chunk_state_update(&self->chunk, input_bytes, take); 48081ad6265SDimitry Andric input_bytes += take; 48181ad6265SDimitry Andric input_len -= take; 48281ad6265SDimitry Andric // If we've filled the current chunk and there's more coming, finalize this 48381ad6265SDimitry Andric // chunk and proceed. In this case we know it's not the root. 48481ad6265SDimitry Andric if (input_len > 0) { 48581ad6265SDimitry Andric output_t output = chunk_state_output(&self->chunk); 48681ad6265SDimitry Andric uint8_t chunk_cv[32]; 48781ad6265SDimitry Andric output_chaining_value(&output, chunk_cv); 48881ad6265SDimitry Andric hasher_push_cv(self, chunk_cv, self->chunk.chunk_counter); 48981ad6265SDimitry Andric chunk_state_reset(&self->chunk, self->key, self->chunk.chunk_counter + 1); 49081ad6265SDimitry Andric } else { 49181ad6265SDimitry Andric return; 49281ad6265SDimitry Andric } 49381ad6265SDimitry Andric } 49481ad6265SDimitry Andric 49581ad6265SDimitry Andric // Now the chunk_state is clear, and we have more input. If there's more than 49681ad6265SDimitry Andric // a single chunk (so, definitely not the root chunk), hash the largest whole 49781ad6265SDimitry Andric // subtree we can, with the full benefits of SIMD (and maybe in the future, 49881ad6265SDimitry Andric // multi-threading) parallelism. Two restrictions: 49981ad6265SDimitry Andric // - The subtree has to be a power-of-2 number of chunks. Only subtrees along 50081ad6265SDimitry Andric // the right edge can be incomplete, and we don't know where the right edge 50181ad6265SDimitry Andric // is going to be until we get to finalize(). 50281ad6265SDimitry Andric // - The subtree must evenly divide the total number of chunks up until this 50381ad6265SDimitry Andric // point (if total is not 0). If the current incomplete subtree is only 50481ad6265SDimitry Andric // waiting for 1 more chunk, we can't hash a subtree of 4 chunks. We have 50581ad6265SDimitry Andric // to complete the current subtree first. 50681ad6265SDimitry Andric // Because we might need to break up the input to form powers of 2, or to 50781ad6265SDimitry Andric // evenly divide what we already have, this part runs in a loop. 50881ad6265SDimitry Andric while (input_len > BLAKE3_CHUNK_LEN) { 50981ad6265SDimitry Andric size_t subtree_len = round_down_to_power_of_2(input_len); 51081ad6265SDimitry Andric uint64_t count_so_far = self->chunk.chunk_counter * BLAKE3_CHUNK_LEN; 51181ad6265SDimitry Andric // Shrink the subtree_len until it evenly divides the count so far. We know 51281ad6265SDimitry Andric // that subtree_len itself is a power of 2, so we can use a bitmasking 51381ad6265SDimitry Andric // trick instead of an actual remainder operation. (Note that if the caller 51481ad6265SDimitry Andric // consistently passes power-of-2 inputs of the same size, as is hopefully 51581ad6265SDimitry Andric // typical, this loop condition will always fail, and subtree_len will 51681ad6265SDimitry Andric // always be the full length of the input.) 51781ad6265SDimitry Andric // 51881ad6265SDimitry Andric // An aside: We don't have to shrink subtree_len quite this much. For 51981ad6265SDimitry Andric // example, if count_so_far is 1, we could pass 2 chunks to 52081ad6265SDimitry Andric // compress_subtree_to_parent_node. Since we'll get 2 CVs back, we'll still 52181ad6265SDimitry Andric // get the right answer in the end, and we might get to use 2-way SIMD 52281ad6265SDimitry Andric // parallelism. The problem with this optimization, is that it gets us 52381ad6265SDimitry Andric // stuck always hashing 2 chunks. The total number of chunks will remain 52481ad6265SDimitry Andric // odd, and we'll never graduate to higher degrees of parallelism. See 52581ad6265SDimitry Andric // https://github.com/BLAKE3-team/BLAKE3/issues/69. 52681ad6265SDimitry Andric while ((((uint64_t)(subtree_len - 1)) & count_so_far) != 0) { 52781ad6265SDimitry Andric subtree_len /= 2; 52881ad6265SDimitry Andric } 52981ad6265SDimitry Andric // The shrunken subtree_len might now be 1 chunk long. If so, hash that one 53081ad6265SDimitry Andric // chunk by itself. Otherwise, compress the subtree into a pair of CVs. 53181ad6265SDimitry Andric uint64_t subtree_chunks = subtree_len / BLAKE3_CHUNK_LEN; 53281ad6265SDimitry Andric if (subtree_len <= BLAKE3_CHUNK_LEN) { 53381ad6265SDimitry Andric blake3_chunk_state chunk_state; 53481ad6265SDimitry Andric chunk_state_init(&chunk_state, self->key, self->chunk.flags); 53581ad6265SDimitry Andric chunk_state.chunk_counter = self->chunk.chunk_counter; 53681ad6265SDimitry Andric chunk_state_update(&chunk_state, input_bytes, subtree_len); 53781ad6265SDimitry Andric output_t output = chunk_state_output(&chunk_state); 53881ad6265SDimitry Andric uint8_t cv[BLAKE3_OUT_LEN]; 53981ad6265SDimitry Andric output_chaining_value(&output, cv); 54081ad6265SDimitry Andric hasher_push_cv(self, cv, chunk_state.chunk_counter); 54181ad6265SDimitry Andric } else { 54281ad6265SDimitry Andric // This is the high-performance happy path, though getting here depends 54381ad6265SDimitry Andric // on the caller giving us a long enough input. 54481ad6265SDimitry Andric uint8_t cv_pair[2 * BLAKE3_OUT_LEN]; 54581ad6265SDimitry Andric compress_subtree_to_parent_node(input_bytes, subtree_len, self->key, 54681ad6265SDimitry Andric self->chunk.chunk_counter, 54781ad6265SDimitry Andric self->chunk.flags, cv_pair); 54881ad6265SDimitry Andric hasher_push_cv(self, cv_pair, self->chunk.chunk_counter); 54981ad6265SDimitry Andric hasher_push_cv(self, &cv_pair[BLAKE3_OUT_LEN], 55081ad6265SDimitry Andric self->chunk.chunk_counter + (subtree_chunks / 2)); 55181ad6265SDimitry Andric } 55281ad6265SDimitry Andric self->chunk.chunk_counter += subtree_chunks; 55381ad6265SDimitry Andric input_bytes += subtree_len; 55481ad6265SDimitry Andric input_len -= subtree_len; 55581ad6265SDimitry Andric } 55681ad6265SDimitry Andric 55781ad6265SDimitry Andric // If there's any remaining input less than a full chunk, add it to the chunk 55881ad6265SDimitry Andric // state. In that case, also do a final merge loop to make sure the subtree 55981ad6265SDimitry Andric // stack doesn't contain any unmerged pairs. The remaining input means we 56081ad6265SDimitry Andric // know these merges are non-root. This merge loop isn't strictly necessary 56181ad6265SDimitry Andric // here, because hasher_push_chunk_cv already does its own merge loop, but it 56281ad6265SDimitry Andric // simplifies blake3_hasher_finalize below. 56381ad6265SDimitry Andric if (input_len > 0) { 56481ad6265SDimitry Andric chunk_state_update(&self->chunk, input_bytes, input_len); 56581ad6265SDimitry Andric hasher_merge_cv_stack(self, self->chunk.chunk_counter); 56681ad6265SDimitry Andric } 56781ad6265SDimitry Andric } 56881ad6265SDimitry Andric 56981ad6265SDimitry Andric void llvm_blake3_hasher_finalize(const blake3_hasher *self, uint8_t *out, 57081ad6265SDimitry Andric size_t out_len) { 57181ad6265SDimitry Andric llvm_blake3_hasher_finalize_seek(self, 0, out, out_len); 57281ad6265SDimitry Andric #if LLVM_MEMORY_SANITIZER_BUILD 57381ad6265SDimitry Andric // Avoid false positives due to uninstrumented assembly code. 57481ad6265SDimitry Andric __msan_unpoison(out, out_len); 57581ad6265SDimitry Andric #endif 57681ad6265SDimitry Andric } 57781ad6265SDimitry Andric 57881ad6265SDimitry Andric void llvm_blake3_hasher_finalize_seek(const blake3_hasher *self, uint64_t seek, 57981ad6265SDimitry Andric uint8_t *out, size_t out_len) { 58081ad6265SDimitry Andric // Explicitly checking for zero avoids causing UB by passing a null pointer 58181ad6265SDimitry Andric // to memcpy. This comes up in practice with things like: 58281ad6265SDimitry Andric // std::vector<uint8_t> v; 58381ad6265SDimitry Andric // blake3_hasher_finalize(&hasher, v.data(), v.size()); 58481ad6265SDimitry Andric if (out_len == 0) { 58581ad6265SDimitry Andric return; 58681ad6265SDimitry Andric } 58781ad6265SDimitry Andric 58881ad6265SDimitry Andric // If the subtree stack is empty, then the current chunk is the root. 58981ad6265SDimitry Andric if (self->cv_stack_len == 0) { 59081ad6265SDimitry Andric output_t output = chunk_state_output(&self->chunk); 59181ad6265SDimitry Andric output_root_bytes(&output, seek, out, out_len); 59281ad6265SDimitry Andric return; 59381ad6265SDimitry Andric } 59481ad6265SDimitry Andric // If there are any bytes in the chunk state, finalize that chunk and do a 59581ad6265SDimitry Andric // roll-up merge between that chunk hash and every subtree in the stack. In 59681ad6265SDimitry Andric // this case, the extra merge loop at the end of blake3_hasher_update 59781ad6265SDimitry Andric // guarantees that none of the subtrees in the stack need to be merged with 59881ad6265SDimitry Andric // each other first. Otherwise, if there are no bytes in the chunk state, 59981ad6265SDimitry Andric // then the top of the stack is a chunk hash, and we start the merge from 60081ad6265SDimitry Andric // that. 60181ad6265SDimitry Andric output_t output; 60281ad6265SDimitry Andric size_t cvs_remaining; 60381ad6265SDimitry Andric if (chunk_state_len(&self->chunk) > 0) { 60481ad6265SDimitry Andric cvs_remaining = self->cv_stack_len; 60581ad6265SDimitry Andric output = chunk_state_output(&self->chunk); 60681ad6265SDimitry Andric } else { 60781ad6265SDimitry Andric // There are always at least 2 CVs in the stack in this case. 60881ad6265SDimitry Andric cvs_remaining = self->cv_stack_len - 2; 60981ad6265SDimitry Andric output = parent_output(&self->cv_stack[cvs_remaining * 32], self->key, 61081ad6265SDimitry Andric self->chunk.flags); 61181ad6265SDimitry Andric } 61281ad6265SDimitry Andric while (cvs_remaining > 0) { 61381ad6265SDimitry Andric cvs_remaining -= 1; 61481ad6265SDimitry Andric uint8_t parent_block[BLAKE3_BLOCK_LEN]; 61581ad6265SDimitry Andric memcpy(parent_block, &self->cv_stack[cvs_remaining * 32], 32); 61681ad6265SDimitry Andric output_chaining_value(&output, &parent_block[32]); 61781ad6265SDimitry Andric output = parent_output(parent_block, self->key, self->chunk.flags); 61881ad6265SDimitry Andric } 61981ad6265SDimitry Andric output_root_bytes(&output, seek, out, out_len); 62081ad6265SDimitry Andric } 62181ad6265SDimitry Andric 62281ad6265SDimitry Andric void llvm_blake3_hasher_reset(blake3_hasher *self) { 62381ad6265SDimitry Andric chunk_state_reset(&self->chunk, self->key, 0); 62481ad6265SDimitry Andric self->cv_stack_len = 0; 62581ad6265SDimitry Andric } 626