42 for (
size_t i = 0; i < 8; ++i) {
56 for (
size_t i = 0; i < 8; ++i) {
75 if (take > input_len) {
78 uint8_t* dest = self->
buf + ((size_t)self->
buf_len);
79 for (
size_t i = 0; i < take; ++i) {
82 self->
buf_len =
static_cast<uint8_t
>(self->
buf_len +
static_cast<uint8_t
>(take));
110 for (
size_t i = 0; i < 8; ++i) {
114 ret.
block[i] = block[i];
130 uint32_t cv_words[8];
131 for (
size_t i = 0; i < 8; ++i) {
140 uint64_t output_block_counter = seek / 64;
141 size_t offset_within_block = seek % 64;
142 uint8_t wide_buf[64];
143 while (out_len > 0) {
146 size_t available_bytes = 64 - offset_within_block;
148 if (out_len > available_bytes) {
149 memcpy_len = available_bytes;
151 memcpy_len = out_len;
153 for (
size_t i = 0; i < memcpy_len; ++i) {
154 out[i] = wide_buf[i + offset_within_block];
158 out_len -= memcpy_len;
159 output_block_counter += 1;
160 offset_within_block = 0;
224 const uint8_t* input,
size_t input_len,
const uint32_t
key[8], uint64_t chunk_counter, uint8_t flags, uint8_t* out)
226#if defined(BLAKE3_TESTING)
227 assert(0 < input_len);
232 size_t input_position = 0;
233 size_t chunks_array_len = 0;
235 chunks_array[chunks_array_len] = &input[input_position];
237 chunks_array_len += 1;
253 if (input_len > input_position) {
254 uint64_t counter = chunk_counter + (uint64_t)chunks_array_len;
257 chunk_state.chunk_counter = counter;
261 return chunks_array_len + 1;
263 return chunks_array_len;
273 size_t num_chaining_values,
274 const uint32_t
key[8],
278#if defined(BLAKE3_TESTING)
279 assert(2 <= num_chaining_values);
284 size_t parents_array_len = 0;
285 while (num_chaining_values - (2 * parents_array_len) >= 2) {
286 parents_array[parents_array_len] = &child_chaining_values[2 * parents_array_len *
BLAKE3_OUT_LEN];
287 parents_array_len += 1;
302 if (num_chaining_values > 2 * parents_array_len) {
305 child_chaining_values[2 * parents_array_len *
BLAKE3_OUT_LEN + i];
308 return parents_array_len + 1;
310 return parents_array_len;
331static size_t blake3_compress_subtree_wide(
332 const uint8_t* input,
size_t input_len,
const uint32_t
key[8], uint64_t chunk_counter, uint8_t flags, uint8_t* out)
346 size_t left_input_len =
left_len(input_len);
347 size_t right_input_len = input_len - left_input_len;
348 const uint8_t* right_input = &input[left_input_len];
349 uint64_t right_chunk_counter = chunk_counter + (uint64_t)(left_input_len /
BLAKE3_CHUNK_LEN);
367 size_t left_n = blake3_compress_subtree_wide(input, left_input_len,
key, chunk_counter, flags, cv_array);
369 blake3_compress_subtree_wide(right_input, right_input_len,
key, right_chunk_counter, flags, right_cvs);
376 out[i] = cv_array[i];
383 size_t num_chaining_values = left_n + right_n;
399 const uint32_t
key[8],
400 uint64_t chunk_counter,
404#if defined(BLAKE3_TESTING)
410 size_t num_cvs = blake3_compress_subtree_wide(input, input_len,
key, chunk_counter, flags, cv_array);
416 while (num_cvs > 2) {
419 cv_array[i] = out_array[i];
423 out[i] = cv_array[i];
429 for (
size_t i = 0; i < 8; i++) {
444 uint32_t key_words[8];
456 uint32_t context_key_words[8];
478 size_t post_merge_stack_len = (size_t)
popcnt(total_len);
535 if (input_len == 0) {
539 const uint8_t* input_bytes = (
const uint8_t*)input;
545 if (take > input_len) {
555 uint8_t chunk_cv[32];
595 while ((((uint64_t)(subtree_len - 1)) & count_so_far) != 0) {
620 input_bytes += subtree_len;
621 input_len -= subtree_len;
665 size_t cvs_remaining;
671 cvs_remaining =
static_cast<size_t>(self->
cv_stack_len - 2);
674 while (cvs_remaining > 0) {
677 for (
size_t i = 0; i < 32; i++) {
678 parent_block[i] = self->
cv_stack[cvs_remaining * 32 + i];
687void g(uint32_t* state,
size_t a,
size_t b,
size_t c,
size_t d, uint32_t x, uint32_t y)
689 state[
a] = state[
a] + state[
b] + x;
690 state[d] =
rotr32(state[d] ^ state[
a], 16);
691 state[c] = state[c] + state[d];
692 state[
b] =
rotr32(state[
b] ^ state[c], 12);
693 state[
a] = state[
a] + state[
b] + y;
694 state[d] =
rotr32(state[d] ^ state[
a], 8);
695 state[c] = state[c] + state[d];
696 state[
b] =
rotr32(state[
b] ^ state[c], 7);
699void round_fn(uint32_t state[16],
const uint32_t* msg,
size_t round)
702 const uint8_t* schedule = MSG_SCHEDULE[round];
705 g(state, 0, 4, 8, 12, msg[schedule[0]], msg[schedule[1]]);
706 g(state, 1, 5, 9, 13, msg[schedule[2]], msg[schedule[3]]);
707 g(state, 2, 6, 10, 14, msg[schedule[4]], msg[schedule[5]]);
708 g(state, 3, 7, 11, 15, msg[schedule[6]], msg[schedule[7]]);
711 g(state, 0, 5, 10, 15, msg[schedule[8]], msg[schedule[9]]);
712 g(state, 1, 6, 11, 12, msg[schedule[10]], msg[schedule[11]]);
713 g(state, 2, 7, 8, 13, msg[schedule[12]], msg[schedule[13]]);
714 g(state, 3, 4, 9, 14, msg[schedule[14]], msg[schedule[15]]);
718 const uint32_t cv[8],
724 uint32_t block_words[16];
725 block_words[0] =
load32(block + 4 * 0);
726 block_words[1] =
load32(block + 4 * 1);
727 block_words[2] =
load32(block + 4 * 2);
728 block_words[3] =
load32(block + 4 * 3);
729 block_words[4] =
load32(block + 4 * 4);
730 block_words[5] =
load32(block + 4 * 5);
731 block_words[6] =
load32(block + 4 * 6);
732 block_words[7] =
load32(block + 4 * 7);
733 block_words[8] =
load32(block + 4 * 8);
734 block_words[9] =
load32(block + 4 * 9);
735 block_words[10] =
load32(block + 4 * 10);
736 block_words[11] =
load32(block + 4 * 11);
737 block_words[12] =
load32(block + 4 * 12);
738 block_words[13] =
load32(block + 4 * 13);
739 block_words[14] =
load32(block + 4 * 14);
740 block_words[15] =
load32(block + 4 * 15);
756 state[14] = (uint32_t)block_len;
757 state[15] = (uint32_t)flags;
759 round_fn(state, &block_words[0], 0);
760 round_fn(state, &block_words[0], 1);
761 round_fn(state, &block_words[0], 2);
762 round_fn(state, &block_words[0], 3);
763 round_fn(state, &block_words[0], 4);
764 round_fn(state, &block_words[0], 5);
765 round_fn(state, &block_words[0], 6);
769 uint32_t cv[8],
const uint8_t block[
BLAKE3_BLOCK_LEN], uint8_t block_len, uint64_t counter, uint8_t flags)
772 compress_pre(state, cv, block, block_len, counter, flags);
773 cv[0] = state[0] ^ state[8];
774 cv[1] = state[1] ^ state[9];
775 cv[2] = state[2] ^ state[10];
776 cv[3] = state[3] ^ state[11];
777 cv[4] = state[4] ^ state[12];
778 cv[5] = state[5] ^ state[13];
779 cv[6] = state[6] ^ state[14];
780 cv[7] = state[7] ^ state[15];
791 compress_pre(state, cv, block, block_len, counter, flags);
793 store32(&out[0 * 4], state[0] ^ state[8]);
794 store32(&out[1 * 4], state[1] ^ state[9]);
795 store32(&out[2 * 4], state[2] ^ state[10]);
796 store32(&out[3 * 4], state[3] ^ state[11]);
797 store32(&out[4 * 4], state[4] ^ state[12]);
798 store32(&out[5 * 4], state[5] ^ state[13]);
799 store32(&out[6 * 4], state[6] ^ state[14]);
800 store32(&out[7 * 4], state[7] ^ state[15]);
801 store32(&out[8 * 4], state[8] ^ cv[0]);
802 store32(&out[9 * 4], state[9] ^ cv[1]);
803 store32(&out[10 * 4], state[10] ^ cv[2]);
804 store32(&out[11 * 4], state[11] ^ cv[3]);
805 store32(&out[12 * 4], state[12] ^ cv[4]);
806 store32(&out[13 * 4], state[13] ^ cv[5]);
807 store32(&out[14 * 4], state[14] ^ cv[6]);
808 store32(&out[15 * 4], state[15] ^ cv[7]);
813 const uint32_t
key[8],
821 for (
size_t i = 0; i < 8; i++) {
825 uint8_t block_flags = flags | flags_start;
828 block_flags |= flags_end;
841 const uint32_t
key[8],
843 bool increment_counter,
849 while (num_inputs > 0) {
851 if (increment_counter) {
860std::vector<uint8_t>
blake3s(std::vector<uint8_t>
const& input,
#define MAX_SIMD_DEGREE_OR_2
StrictMock< MockContext > context
#define BLAKE3_VERSION_STRING
INLINE void chunk_state_reset(blake3_chunk_state *self, const uint32_t key[8], uint64_t chunk_counter)
void blake3_hasher_init_derive_key_raw(blake3_hasher *self, const void *context, size_t context_len)
struct blake3_full::output_t__ output_t
INLINE void store32(void *dst, uint32_t w)
INLINE output_t parent_output(const uint8_t block[BLAKE3_BLOCK_LEN], const uint32_t key[8], uint8_t flags)
void blake3_hasher_init_keyed(blake3_hasher *self, const uint8_t key[BLAKE3_KEY_LEN])
INLINE unsigned int popcnt(uint64_t x)
void blake3_hasher_finalize_seek(const blake3_hasher *self, uint64_t seek, uint8_t *out, size_t out_len)
size_t blake3_simd_degree(void)
const char * blake3_version(void)
void blake3_hasher_init_derive_key(blake3_hasher *self, const char *context)
void blake3_hasher_update(blake3_hasher *self, const void *input, size_t input_len)
INLINE output_t chunk_state_output(const blake3_chunk_state *self)
INLINE size_t chunk_state_fill_buf(blake3_chunk_state *self, const uint8_t *input, size_t input_len)
INLINE void hasher_merge_cv_stack(blake3_hasher *self, uint64_t total_len)
INLINE void hasher_push_cv(blake3_hasher *self, uint8_t new_cv[BLAKE3_OUT_LEN], uint64_t chunk_counter)
std::vector< uint8_t > blake3s(std::vector< uint8_t > const &input, const mode mode_id, const uint8_t key[BLAKE3_KEY_LEN], const char *context)
INLINE uint64_t round_down_to_power_of_2(uint64_t x)
INLINE size_t left_len(size_t content_len)
void round_fn(uint32_t state[16], const uint32_t *msg, size_t round)
void blake3_compress_xof(const uint32_t cv[8], const uint8_t block[BLAKE3_BLOCK_LEN], uint8_t block_len, uint64_t counter, uint8_t flags, uint8_t out[64])
INLINE uint32_t load32(const void *src)
INLINE output_t make_output(const uint32_t input_cv[8], const uint8_t block[BLAKE3_BLOCK_LEN], uint8_t block_len, uint64_t counter, uint8_t flags)
INLINE void output_root_bytes(const output_t *self, uint64_t seek, uint8_t *out, size_t out_len)
INLINE uint8_t chunk_state_maybe_start_flag(const blake3_chunk_state *self)
INLINE void store_cv_words(uint8_t bytes_out[32], uint32_t cv_words[8])
INLINE size_t compress_chunks_parallel(const uint8_t *input, size_t input_len, const uint32_t key[8], uint64_t chunk_counter, uint8_t flags, uint8_t *out)
INLINE uint32_t counter_high(uint64_t counter)
INLINE void chunk_state_update(blake3_chunk_state *self, const uint8_t *input, size_t input_len)
INLINE size_t compress_parents_parallel(const uint8_t *child_chaining_values, size_t num_chaining_values, const uint32_t key[8], uint8_t flags, uint8_t *out)
INLINE void output_chaining_value(const output_t *self, uint8_t cv[32])
INLINE void compress_subtree_to_parent_node(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])
void g(uint32_t *state, size_t a, size_t b, size_t c, size_t d, uint32_t x, uint32_t y)
INLINE void hasher_init_base(blake3_hasher *self, const uint32_t key[8], uint8_t flags)
void blake3_hasher_finalize(const blake3_hasher *self, uint8_t *out, size_t out_len)
void blake3_hasher_init(blake3_hasher *self)
INLINE uint32_t counter_low(uint64_t counter)
void blake3s_hash_one(const uint8_t *input, size_t blocks, const uint32_t key[8], uint64_t counter, uint8_t flags, uint8_t flags_start, uint8_t flags_end, uint8_t out[BLAKE3_OUT_LEN])
INLINE uint32_t rotr32(uint32_t w, uint32_t c)
INLINE void chunk_state_init(blake3_chunk_state *self, const uint32_t key[8], uint8_t flags)
INLINE void load_key_words(const uint8_t key[BLAKE3_KEY_LEN], uint32_t key_words[8])
void blake3_hash_many(const uint8_t *const *inputs, size_t num_inputs, size_t blocks, const uint32_t key[8], uint64_t counter, bool increment_counter, uint8_t flags, uint8_t flags_start, uint8_t flags_end, uint8_t *out)
void blake3_compress_in_place(uint32_t cv[8], const uint8_t block[BLAKE3_BLOCK_LEN], uint8_t block_len, uint64_t counter, uint8_t flags)
INLINE size_t chunk_state_len(const blake3_chunk_state *self)
void compress_pre(uint32_t state[16], const uint32_t cv[8], const uint8_t block[BLAKE3_BLOCK_LEN], uint8_t block_len, uint64_t counter, uint8_t flags)
uint8_t blocks_compressed
uint8_t buf[BLAKE3_BLOCK_LEN]
uint8_t cv_stack[(BLAKE3_MAX_DEPTH+1) *BLAKE3_OUT_LEN]
uint8_t block[BLAKE3_BLOCK_LEN]