36 for (
size_t i = 0; i < 8; ++i) {
50 for (
size_t i = 0; i < 8; ++i) {
69 if (take > input_len) {
72 uint8_t* dest = self->
buf + ((size_t)self->
buf_len);
73 for (
size_t i = 0; i < take; ++i) {
76 self->
buf_len =
static_cast<uint8_t
>(self->
buf_len +
static_cast<uint8_t
>(take));
104 for (
size_t i = 0; i < 8; ++i) {
108 ret.
block[i] = block[i];
124 uint32_t cv_words[8];
125 for (
size_t i = 0; i < 8; ++i) {
134 uint64_t output_block_counter = seek / 64;
135 size_t offset_within_block = seek % 64;
136 uint8_t wide_buf[64];
137 while (out_len > 0) {
140 size_t available_bytes = 64 - offset_within_block;
142 if (out_len > available_bytes) {
143 memcpy_len = available_bytes;
145 memcpy_len = out_len;
147 for (
size_t i = 0; i < memcpy_len; ++i) {
148 out[i] = wide_buf[i + offset_within_block];
152 out_len -= memcpy_len;
153 output_block_counter += 1;
154 offset_within_block = 0;
218 const uint8_t* input,
size_t input_len,
const uint32_t
key[8], uint64_t chunk_counter, uint8_t flags, uint8_t* out)
220#if defined(BLAKE3_TESTING)
221 assert(0 < input_len);
226 size_t input_position = 0;
227 size_t chunks_array_len = 0;
229 chunks_array[chunks_array_len] = &input[input_position];
231 chunks_array_len += 1;
247 if (input_len > input_position) {
248 uint64_t counter = chunk_counter + (uint64_t)chunks_array_len;
251 chunk_state.chunk_counter = counter;
255 return chunks_array_len + 1;
257 return chunks_array_len;
267 size_t num_chaining_values,
268 const uint32_t
key[8],
272#if defined(BLAKE3_TESTING)
273 assert(2 <= num_chaining_values);
278 size_t parents_array_len = 0;
279 while (num_chaining_values - (2 * parents_array_len) >= 2) {
280 parents_array[parents_array_len] = &child_chaining_values[2 * parents_array_len *
BLAKE3_OUT_LEN];
281 parents_array_len += 1;
296 if (num_chaining_values > 2 * parents_array_len) {
299 child_chaining_values[2 * parents_array_len *
BLAKE3_OUT_LEN + i];
302 return parents_array_len + 1;
304 return parents_array_len;
325static size_t blake3_compress_subtree_wide(
326 const uint8_t* input,
size_t input_len,
const uint32_t
key[8], uint64_t chunk_counter, uint8_t flags, uint8_t* out)
340 size_t left_input_len =
left_len(input_len);
341 size_t right_input_len = input_len - left_input_len;
342 const uint8_t* right_input = &input[left_input_len];
343 uint64_t right_chunk_counter = chunk_counter + (uint64_t)(left_input_len /
BLAKE3_CHUNK_LEN);
361 size_t left_n = blake3_compress_subtree_wide(input, left_input_len,
key, chunk_counter, flags, cv_array);
363 blake3_compress_subtree_wide(right_input, right_input_len,
key, right_chunk_counter, flags, right_cvs);
370 out[i] = cv_array[i];
377 size_t num_chaining_values = left_n + right_n;
393 const uint32_t
key[8],
394 uint64_t chunk_counter,
398#if defined(BLAKE3_TESTING)
404 size_t num_cvs = blake3_compress_subtree_wide(input, input_len,
key, chunk_counter, flags, cv_array);
410 while (num_cvs > 2) {
413 cv_array[i] = out_array[i];
417 out[i] = cv_array[i];
423 for (
size_t i = 0; i < 8; i++) {
438 uint32_t key_words[8];
450 uint32_t context_key_words[8];
472 size_t post_merge_stack_len = (size_t)
popcnt(total_len);
529 if (input_len == 0) {
533 const uint8_t* input_bytes = (
const uint8_t*)input;
539 if (take > input_len) {
549 uint8_t chunk_cv[32];
589 while ((((uint64_t)(subtree_len - 1)) & count_so_far) != 0) {
614 input_bytes += subtree_len;
615 input_len -= subtree_len;
659 size_t cvs_remaining;
665 cvs_remaining =
static_cast<size_t>(self->
cv_stack_len - 2);
668 while (cvs_remaining > 0) {
671 for (
size_t i = 0; i < 32; i++) {
672 parent_block[i] = self->
cv_stack[cvs_remaining * 32 + i];
681void g(uint32_t* state,
size_t a,
size_t b,
size_t c,
size_t d, uint32_t x, uint32_t y)
683 state[
a] = state[
a] + state[
b] + x;
684 state[d] =
rotr32(state[d] ^ state[
a], 16);
685 state[c] = state[c] + state[d];
686 state[
b] =
rotr32(state[
b] ^ state[c], 12);
687 state[
a] = state[
a] + state[
b] + y;
688 state[d] =
rotr32(state[d] ^ state[
a], 8);
689 state[c] = state[c] + state[d];
690 state[
b] =
rotr32(state[
b] ^ state[c], 7);
693void round_fn(uint32_t state[16],
const uint32_t* msg,
size_t round)
696 const uint8_t* schedule = MSG_SCHEDULE[round];
699 g(state, 0, 4, 8, 12, msg[schedule[0]], msg[schedule[1]]);
700 g(state, 1, 5, 9, 13, msg[schedule[2]], msg[schedule[3]]);
701 g(state, 2, 6, 10, 14, msg[schedule[4]], msg[schedule[5]]);
702 g(state, 3, 7, 11, 15, msg[schedule[6]], msg[schedule[7]]);
705 g(state, 0, 5, 10, 15, msg[schedule[8]], msg[schedule[9]]);
706 g(state, 1, 6, 11, 12, msg[schedule[10]], msg[schedule[11]]);
707 g(state, 2, 7, 8, 13, msg[schedule[12]], msg[schedule[13]]);
708 g(state, 3, 4, 9, 14, msg[schedule[14]], msg[schedule[15]]);
712 const uint32_t cv[8],
718 uint32_t block_words[16];
719 block_words[0] =
load32(block + 4 * 0);
720 block_words[1] =
load32(block + 4 * 1);
721 block_words[2] =
load32(block + 4 * 2);
722 block_words[3] =
load32(block + 4 * 3);
723 block_words[4] =
load32(block + 4 * 4);
724 block_words[5] =
load32(block + 4 * 5);
725 block_words[6] =
load32(block + 4 * 6);
726 block_words[7] =
load32(block + 4 * 7);
727 block_words[8] =
load32(block + 4 * 8);
728 block_words[9] =
load32(block + 4 * 9);
729 block_words[10] =
load32(block + 4 * 10);
730 block_words[11] =
load32(block + 4 * 11);
731 block_words[12] =
load32(block + 4 * 12);
732 block_words[13] =
load32(block + 4 * 13);
733 block_words[14] =
load32(block + 4 * 14);
734 block_words[15] =
load32(block + 4 * 15);
750 state[14] = (uint32_t)block_len;
751 state[15] = (uint32_t)flags;
753 round_fn(state, &block_words[0], 0);
754 round_fn(state, &block_words[0], 1);
755 round_fn(state, &block_words[0], 2);
756 round_fn(state, &block_words[0], 3);
757 round_fn(state, &block_words[0], 4);
758 round_fn(state, &block_words[0], 5);
759 round_fn(state, &block_words[0], 6);
763 uint32_t cv[8],
const uint8_t block[
BLAKE3_BLOCK_LEN], uint8_t block_len, uint64_t counter, uint8_t flags)
766 compress_pre(state, cv, block, block_len, counter, flags);
767 cv[0] = state[0] ^ state[8];
768 cv[1] = state[1] ^ state[9];
769 cv[2] = state[2] ^ state[10];
770 cv[3] = state[3] ^ state[11];
771 cv[4] = state[4] ^ state[12];
772 cv[5] = state[5] ^ state[13];
773 cv[6] = state[6] ^ state[14];
774 cv[7] = state[7] ^ state[15];
785 compress_pre(state, cv, block, block_len, counter, flags);
787 store32(&out[0 * 4], state[0] ^ state[8]);
788 store32(&out[1 * 4], state[1] ^ state[9]);
789 store32(&out[2 * 4], state[2] ^ state[10]);
790 store32(&out[3 * 4], state[3] ^ state[11]);
791 store32(&out[4 * 4], state[4] ^ state[12]);
792 store32(&out[5 * 4], state[5] ^ state[13]);
793 store32(&out[6 * 4], state[6] ^ state[14]);
794 store32(&out[7 * 4], state[7] ^ state[15]);
795 store32(&out[8 * 4], state[8] ^ cv[0]);
796 store32(&out[9 * 4], state[9] ^ cv[1]);
797 store32(&out[10 * 4], state[10] ^ cv[2]);
798 store32(&out[11 * 4], state[11] ^ cv[3]);
799 store32(&out[12 * 4], state[12] ^ cv[4]);
800 store32(&out[13 * 4], state[13] ^ cv[5]);
801 store32(&out[14 * 4], state[14] ^ cv[6]);
802 store32(&out[15 * 4], state[15] ^ cv[7]);
807 const uint32_t
key[8],
815 for (
size_t i = 0; i < 8; i++) {
819 uint8_t block_flags = flags | flags_start;
822 block_flags |= flags_end;
835 const uint32_t
key[8],
837 bool increment_counter,
843 while (num_inputs > 0) {
845 if (increment_counter) {
854std::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]