Debian Codegraph @

external/blake3/blake3.c

raw symbols SCIP
1 #cxx . . $ `<file>/external/blake3/blake3.c`/include <assert.h>
2 #include <stdbool.h>
3 #include <string.h>
4 #include <stdint.h>
5
6 #include "blake3.h"cxx . . $ `<file>/external/blake3/blake3.h`/
7 #include "blake3_impl.h"cxx . . $ `<file>/external/blake3/blake3_impl.h`/
8
9 const char *blake3_versioncxx . . $ blake3_version(33a1f659e94feb42).(void) { return BLAKE3_VERSION_STRINGcxx . . $ `external/blake3/blake3.h:41:9`!; }
10
11 INLINEcxx . . $ `external/blake3/blake3_impl.h:32:9`! void chunk_state_initcxx . . $ chunk_state_init(9c7e888c3389eb38).(blake3_chunk_statecxx . . $ blake3_chunk_state# *self, const uint32_tcxx . . $ uint32_t# key[8],
12 uint8_tcxx . . $ uint8_t# flags) {
13 memcpycxx . . $ memcpy(cc817cb175f282d).(self->cvcxx . . $ $anonymous_type_7563367b72f05719_0#cv., key, BLAKE3_KEY_LENcxx . . $ `external/blake3/blake3.h:42:9`!);
14 self->chunk_countercxx . . $ $anonymous_type_7563367b72f05719_0#chunk_counter. = 0;
15 memsetcxx . . $ memset(d1de1e4fb215f1c6).(self->bufcxx . . $ $anonymous_type_7563367b72f05719_0#buf., 0, BLAKE3_BLOCK_LENcxx . . $ `external/blake3/blake3.h:44:9`!);
16 self->buf_lencxx . . $ $anonymous_type_7563367b72f05719_0#buf_len. = 0;
17 self->blocks_compressedcxx . . $ $anonymous_type_7563367b72f05719_0#blocks_compressed. = 0;
18 self->flagscxx . . $ $anonymous_type_7563367b72f05719_0#flags. = flags;
19 }
20
21 INLINEcxx . . $ `external/blake3/blake3_impl.h:32:9`! void chunk_state_resetcxx . . $ chunk_state_reset(d14647d89c9e0c8f).(blake3_chunk_statecxx . . $ blake3_chunk_state# *self, const uint32_tcxx . . $ uint32_t# key[8],
22 uint64_tcxx . . $ uint64_t# chunk_counter) {
23 memcpycxx . . $ memcpy(cc817cb175f282d).(self->cvcxx . . $ $anonymous_type_7563367b72f05719_0#cv., key, BLAKE3_KEY_LENcxx . . $ `external/blake3/blake3.h:42:9`!);
24 self->chunk_countercxx . . $ $anonymous_type_7563367b72f05719_0#chunk_counter. = chunk_counter;
25 self->blocks_compressedcxx . . $ $anonymous_type_7563367b72f05719_0#blocks_compressed. = 0;
26 memsetcxx . . $ memset(d1de1e4fb215f1c6).(self->bufcxx . . $ $anonymous_type_7563367b72f05719_0#buf., 0, BLAKE3_BLOCK_LENcxx . . $ `external/blake3/blake3.h:44:9`!);
27 self->buf_lencxx . . $ $anonymous_type_7563367b72f05719_0#buf_len. = 0;
28 }
29
30 INLINEcxx . . $ `external/blake3/blake3_impl.h:32:9`! size_t chunk_state_lencxx . . $ chunk_state_len(72b7af8eb136547f).(const blake3_chunk_statecxx . . $ blake3_chunk_state# *self) {
31 return (BLAKE3_BLOCK_LENcxx . . $ `external/blake3/blake3.h:44:9`! * (size_t)self->blocks_compressedcxx . . $ $anonymous_type_7563367b72f05719_0#blocks_compressed.) +
32 ((size_t)self->buf_lencxx . . $ $anonymous_type_7563367b72f05719_0#buf_len.);
33 }
34
35 INLINEcxx . . $ `external/blake3/blake3_impl.h:32:9`! size_t chunk_state_fill_bufcxx . . $ chunk_state_fill_buf(dd5a48874dac7771).(blake3_chunk_statecxx . . $ blake3_chunk_state# *self,
36 const uint8_tcxx . . $ uint8_t# *input, size_t input_len) {
37 size_t take = BLAKE3_BLOCK_LENcxx . . $ `external/blake3/blake3.h:44:9`! - ((size_t)self->buf_lencxx . . $ $anonymous_type_7563367b72f05719_0#buf_len.);
38 if (take > input_len) {
39 take = input_len;
40 }
41 uint8_tcxx . . $ uint8_t# *dest = self->bufcxx . . $ $anonymous_type_7563367b72f05719_0#buf. + ((size_t)self->buf_lencxx . . $ $anonymous_type_7563367b72f05719_0#buf_len.);
42 memcpycxx . . $ memcpy(cc817cb175f282d).(dest, input, take);
43 self->buf_lencxx . . $ $anonymous_type_7563367b72f05719_0#buf_len. += (uint8_tcxx . . $ uint8_t#)take;
44 return take;
45 }
46
47 INLINEcxx . . $ `external/blake3/blake3_impl.h:32:9`! uint8_tcxx . . $ uint8_t# chunk_state_maybe_start_flagcxx . . $ chunk_state_maybe_start_flag(4a0a05e7e0fc5a47).(const blake3_chunk_statecxx . . $ blake3_chunk_state# *self) {
48 if (self->blocks_compressedcxx . . $ $anonymous_type_7563367b72f05719_0#blocks_compressed. == 0) {
49 return CHUNK_STARTcxx . . $ CHUNK_START.;
50 } else {
51 return 0;
52 }
53 }
54
55 typedef structcxx . . $ $anonymous_type_cd25e2f6bb9c5ca3_0# {
56 uint32_tcxx . . $ uint32_t# input_cvcxx . . $ $anonymous_type_cd25e2f6bb9c5ca3_0#input_cv.[8];
57 uint64_tcxx . . $ uint64_t# countercxx . . $ $anonymous_type_cd25e2f6bb9c5ca3_0#counter.;
58 uint8_tcxx . . $ uint8_t# blockcxx . . $ $anonymous_type_cd25e2f6bb9c5ca3_0#block.[BLAKE3_BLOCK_LENcxx . . $ `external/blake3/blake3.h:44:9`!];
59 uint8_tcxx . . $ uint8_t# block_lencxx . . $ $anonymous_type_cd25e2f6bb9c5ca3_0#block_len.;
60 uint8_tcxx . . $ uint8_t# flagscxx . . $ $anonymous_type_cd25e2f6bb9c5ca3_0#flags.;
61 } output_tcxx . . $ output_t#;
62
63 INLINEcxx . . $ `external/blake3/blake3_impl.h:32:9`! output_tcxx . . $ output_t# make_outputcxx . . $ make_output(30ffd898b45849dd).(const uint32_tcxx . . $ uint32_t# input_cv[8],
64 const uint8_tcxx . . $ uint8_t# block[BLAKE3_BLOCK_LENcxx . . $ `external/blake3/blake3.h:44:9`!],
65 uint8_tcxx . . $ uint8_t# block_len, uint64_tcxx . . $ uint64_t# counter,
66 uint8_tcxx . . $ uint8_t# flags) {
67 output_tcxx . . $ output_t# ret;
68 memcpycxx . . $ memcpy(cc817cb175f282d).(ret.input_cvcxx . . $ $anonymous_type_cd25e2f6bb9c5ca3_0#input_cv., input_cv, 32);
69 memcpycxx . . $ memcpy(cc817cb175f282d).(ret.blockcxx . . $ $anonymous_type_cd25e2f6bb9c5ca3_0#block., block, BLAKE3_BLOCK_LENcxx . . $ `external/blake3/blake3.h:44:9`!);
70 ret.block_lencxx . . $ $anonymous_type_cd25e2f6bb9c5ca3_0#block_len. = block_len;
71 ret.countercxx . . $ $anonymous_type_cd25e2f6bb9c5ca3_0#counter. = counter;
72 ret.flagscxx . . $ $anonymous_type_cd25e2f6bb9c5ca3_0#flags. = flags;
73 return ret;
74 }
75
76 // Chaining values within a given chunk (specifically the compress_in_place
77 // interface) are represented as words. This avoids unnecessary bytes<->words
78 // conversion overhead in the portable implementation. However, the hash_many
79 // interface handles both user input and parent node blocks, so it accepts
80 // bytes. For that reason, chaining values in the CV stack are represented as
81 // bytes.
82 INLINEcxx . . $ `external/blake3/blake3_impl.h:32:9`! void output_chaining_valuecxx . . $ output_chaining_value(ac1db42c6d7f21d9).(const output_tcxx . . $ output_t# *self, uint8_tcxx . . $ uint8_t# cv[32]) {
83 uint32_tcxx . . $ uint32_t# cv_words[8];
84 memcpycxx . . $ memcpy(cc817cb175f282d).(cv_words, self->input_cvcxx . . $ $anonymous_type_cd25e2f6bb9c5ca3_0#input_cv., 32);
85 blake3_compress_in_placecxx . . $ blake3_compress_in_place(389a45aab4533bf).(cv_words, self->blockcxx . . $ $anonymous_type_cd25e2f6bb9c5ca3_0#block., self->block_lencxx . . $ $anonymous_type_cd25e2f6bb9c5ca3_0#block_len.,
86 self->countercxx . . $ $anonymous_type_cd25e2f6bb9c5ca3_0#counter., self->flagscxx . . $ $anonymous_type_cd25e2f6bb9c5ca3_0#flags.);
87 store_cv_wordscxx . . $ store_cv_words(3dae8022d7d56a90).(cv, cv_words);
88 }
89
90 INLINEcxx . . $ `external/blake3/blake3_impl.h:32:9`! void output_root_bytescxx . . $ output_root_bytes(4a25d6d8e89fd96).(const output_tcxx . . $ output_t# *self, uint64_tcxx . . $ uint64_t# seek, uint8_tcxx . . $ uint8_t# *out,
91 size_t out_len) {
92 if (out_len == 0) {
93 return;
94 }
95 uint64_tcxx . . $ uint64_t# output_block_counter = seek / 64;
96 size_t offset_within_block = seek % 64;
97 uint8_tcxx . . $ uint8_t# wide_buf[64];
98 if(offset_within_block) {
99 blake3_compress_xofcxx . . $ blake3_compress_xof(bd57d77544a3558d).(self->input_cvcxx . . $ $anonymous_type_cd25e2f6bb9c5ca3_0#input_cv., self->blockcxx . . $ $anonymous_type_cd25e2f6bb9c5ca3_0#block., self->block_lencxx . . $ $anonymous_type_cd25e2f6bb9c5ca3_0#block_len., output_block_counter, self->flagscxx . . $ $anonymous_type_cd25e2f6bb9c5ca3_0#flags. | ROOTcxx . . $ ROOT., wide_buf);
100 const size_t available_bytes = 64 - offset_within_block;
101 const size_t bytes = out_len > available_bytes ? available_bytes : out_len;
102 memcpycxx . . $ memcpy(cc817cb175f282d).(out, wide_buf + offset_within_block, bytes);
103 out += bytes;
104 out_len -= bytes;
105 output_block_counter += 1;
106 }
107 if(out_len / 64) {
108 blake3_xof_manycxx . . $ blake3_xof_many(79b1dc8506f1c101).(self->input_cvcxx . . $ $anonymous_type_cd25e2f6bb9c5ca3_0#input_cv., self->blockcxx . . $ $anonymous_type_cd25e2f6bb9c5ca3_0#block., self->block_lencxx . . $ $anonymous_type_cd25e2f6bb9c5ca3_0#block_len., output_block_counter, self->flagscxx . . $ $anonymous_type_cd25e2f6bb9c5ca3_0#flags. | ROOTcxx . . $ ROOT., out, out_len / 64);
109 }
110 output_block_counter += out_len / 64;
111 out += out_len & -64;
112 out_len -= out_len & -64;
113 if(out_len) {
114 blake3_compress_xofcxx . . $ blake3_compress_xof(bd57d77544a3558d).(self->input_cvcxx . . $ $anonymous_type_cd25e2f6bb9c5ca3_0#input_cv., self->blockcxx . . $ $anonymous_type_cd25e2f6bb9c5ca3_0#block., self->block_lencxx . . $ $anonymous_type_cd25e2f6bb9c5ca3_0#block_len., output_block_counter, self->flagscxx . . $ $anonymous_type_cd25e2f6bb9c5ca3_0#flags. | ROOTcxx . . $ ROOT., wide_buf);
115 memcpycxx . . $ memcpy(cc817cb175f282d).(out, wide_buf, out_len);
116 }
117 }
118
119 INLINEcxx . . $ `external/blake3/blake3_impl.h:32:9`! void chunk_state_updatecxx . . $ chunk_state_update(e80c501d2bbb014f).(blake3_chunk_statecxx . . $ blake3_chunk_state# *self, const uint8_tcxx . . $ uint8_t# *input,
120 size_t input_len) {
121 if (self->buf_lencxx . . $ $anonymous_type_7563367b72f05719_0#buf_len. > 0) {
122 size_t take = chunk_state_fill_bufcxx . . $ chunk_state_fill_buf(dd5a48874dac7771).(self, input, input_len);
123 input += take;
124 input_len -= take;
125 if (input_len > 0) {
126 blake3_compress_in_placecxx . . $ blake3_compress_in_place(389a45aab4533bf).(
127 self->cvcxx . . $ $anonymous_type_7563367b72f05719_0#cv., self->bufcxx . . $ $anonymous_type_7563367b72f05719_0#buf., BLAKE3_BLOCK_LENcxx . . $ `external/blake3/blake3.h:44:9`!, self->chunk_countercxx . . $ $anonymous_type_7563367b72f05719_0#chunk_counter.,
128 self->flagscxx . . $ $anonymous_type_7563367b72f05719_0#flags. | chunk_state_maybe_start_flagcxx . . $ chunk_state_maybe_start_flag(4a0a05e7e0fc5a47).(self));
129 self->blocks_compressedcxx . . $ $anonymous_type_7563367b72f05719_0#blocks_compressed. += 1;
130 self->buf_lencxx . . $ $anonymous_type_7563367b72f05719_0#buf_len. = 0;
131 memsetcxx . . $ memset(d1de1e4fb215f1c6).(self->bufcxx . . $ $anonymous_type_7563367b72f05719_0#buf., 0, BLAKE3_BLOCK_LENcxx . . $ `external/blake3/blake3.h:44:9`!);
132 }
133 }
134
135 while (input_len > BLAKE3_BLOCK_LENcxx . . $ `external/blake3/blake3.h:44:9`!) {
136 blake3_compress_in_placecxx . . $ blake3_compress_in_place(389a45aab4533bf).(self->cvcxx . . $ $anonymous_type_7563367b72f05719_0#cv., input, BLAKE3_BLOCK_LENcxx . . $ `external/blake3/blake3.h:44:9`!,
137 self->chunk_countercxx . . $ $anonymous_type_7563367b72f05719_0#chunk_counter.,
138 self->flagscxx . . $ $anonymous_type_7563367b72f05719_0#flags. | chunk_state_maybe_start_flagcxx . . $ chunk_state_maybe_start_flag(4a0a05e7e0fc5a47).(self));
139 self->blocks_compressedcxx . . $ $anonymous_type_7563367b72f05719_0#blocks_compressed. += 1;
140 input += BLAKE3_BLOCK_LENcxx . . $ `external/blake3/blake3.h:44:9`!;
141 input_len -= BLAKE3_BLOCK_LENcxx . . $ `external/blake3/blake3.h:44:9`!;
142 }
143
144 chunk_state_fill_bufcxx . . $ chunk_state_fill_buf(dd5a48874dac7771).(self, input, input_len);
145 }
146
147 INLINEcxx . . $ `external/blake3/blake3_impl.h:32:9`! output_tcxx . . $ output_t# chunk_state_outputcxx . . $ chunk_state_output(83e33d8ac68da1a0).(const blake3_chunk_statecxx . . $ blake3_chunk_state# *self) {
148 uint8_tcxx . . $ uint8_t# block_flags =
149 self->flagscxx . . $ $anonymous_type_7563367b72f05719_0#flags. | chunk_state_maybe_start_flagcxx . . $ chunk_state_maybe_start_flag(4a0a05e7e0fc5a47).(self) | CHUNK_ENDcxx . . $ CHUNK_END.;
150 return make_outputcxx . . $ make_output(30ffd898b45849dd).(self->cvcxx . . $ $anonymous_type_7563367b72f05719_0#cv., self->bufcxx . . $ $anonymous_type_7563367b72f05719_0#buf., self->buf_lencxx . . $ $anonymous_type_7563367b72f05719_0#buf_len., self->chunk_countercxx . . $ $anonymous_type_7563367b72f05719_0#chunk_counter.,
151 block_flags);
152 }
153
154 INLINEcxx . . $ `external/blake3/blake3_impl.h:32:9`! output_tcxx . . $ output_t# parent_outputcxx . . $ parent_output(bf53c26596625223).(const uint8_tcxx . . $ uint8_t# block[BLAKE3_BLOCK_LENcxx . . $ `external/blake3/blake3.h:44:9`!],
155 const uint32_tcxx . . $ uint32_t# key[8], uint8_tcxx . . $ uint8_t# flags) {
156 return make_outputcxx . . $ make_output(30ffd898b45849dd).(key, block, BLAKE3_BLOCK_LENcxx . . $ `external/blake3/blake3.h:44:9`!, 0, flags | PARENTcxx . . $ PARENT.);
157 }
158
159 // Given some input larger than one chunk, return the number of bytes that
160 // should go in the left subtree. This is the largest power-of-2 number of
161 // chunks that leaves at least 1 byte for the right subtree.
162 INLINEcxx . . $ `external/blake3/blake3_impl.h:32:9`! size_t left_subtree_lencxx . . $ left_subtree_len(ecdaa1632f49f591).(size_t input_len) {
163 // Subtract 1 to reserve at least one byte for the right side. input_len
164 // should always be greater than BLAKE3_CHUNK_LEN.
165 size_t full_chunks = (input_len - 1) / BLAKE3_CHUNK_LENcxx . . $ `external/blake3/blake3.h:45:9`!;
166 return round_down_to_power_of_2cxx . . $ round_down_to_power_of_2(ecdaa1632f49f591).(full_chunks) * BLAKE3_CHUNK_LENcxx . . $ `external/blake3/blake3.h:45:9`!;
167 }
168
169 // Use SIMD parallelism to hash up to MAX_SIMD_DEGREE chunks at the same time
170 // on a single thread. Write out the chunk chaining values and return the
171 // number of chunks hashed. These chunks are never the root and never empty;
172 // those cases use a different codepath.
173 INLINEcxx . . $ `external/blake3/blake3_impl.h:32:9`! size_t compress_chunks_parallelcxx . . $ compress_chunks_parallel(6f3c5acf120589d6).(const uint8_tcxx . . $ uint8_t# *input, size_t input_len,
174 const uint32_tcxx . . $ uint32_t# key[8],
175 uint64_tcxx . . $ uint64_t# chunk_counter, uint8_tcxx . . $ uint8_t# flags,
176 uint8_tcxx . . $ uint8_t# *out) {
177 #if defined(BLAKE3_TESTING)
178 assert(0 < input_len);
179 assert(input_len <= MAX_SIMD_DEGREE * BLAKE3_CHUNK_LEN);
180 #endif
181
182 const uint8_tcxx . . $ uint8_t# *chunks_array[MAX_SIMD_DEGREEcxx . . $ `external/blake3/blake3_impl.h:75:9`!];
183 size_t input_position = 0;
184 size_t chunks_array_len = 0;
185 while (input_len - input_position >= BLAKE3_CHUNK_LENcxx . . $ `external/blake3/blake3.h:45:9`!) {
186 chunks_array[chunks_array_len] = &input[input_position];
187 input_position += BLAKE3_CHUNK_LENcxx . . $ `external/blake3/blake3.h:45:9`!;
188 chunks_array_len += 1;
189 }
190
191 blake3_hash_manycxx . . $ blake3_hash_many(6d9d480067751c4).(chunks_array, chunks_array_len,
192 BLAKE3_CHUNK_LENcxx . . $ `external/blake3/blake3.h:45:9`! / BLAKE3_BLOCK_LENcxx . . $ `external/blake3/blake3.h:44:9`!, key, chunk_counter,
193 truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!, flags, CHUNK_STARTcxx . . $ CHUNK_START., CHUNK_ENDcxx . . $ CHUNK_END., out);
194
195 // Hash the remaining partial chunk, if there is one. Note that the empty
196 // chunk (meaning the empty message) is a different codepath.
197 if (input_len > input_position) {
198 uint64_tcxx . . $ uint64_t# counter = chunk_counter + (uint64_tcxx . . $ uint64_t#)chunks_array_len;
199 blake3_chunk_statecxx . . $ blake3_chunk_state# chunk_state;
200 chunk_state_initcxx . . $ chunk_state_init(9c7e888c3389eb38).(&chunk_state, key, flags);
201 chunk_state.chunk_countercxx . . $ $anonymous_type_7563367b72f05719_0#chunk_counter. = counter;
202 chunk_state_updatecxx . . $ chunk_state_update(e80c501d2bbb014f).(&chunk_state, &input[input_position],
203 input_len - input_position);
204 output_tcxx . . $ output_t# output = chunk_state_outputcxx . . $ chunk_state_output(83e33d8ac68da1a0).(&chunk_state);
205 output_chaining_valuecxx . . $ output_chaining_value(ac1db42c6d7f21d9).(&output, &out[chunks_array_len * BLAKE3_OUT_LENcxx . . $ `external/blake3/blake3.h:43:9`!]);
206 return chunks_array_len + 1;
207 } else {
208 return chunks_array_len;
209 }
210 }
211
212 // Use SIMD parallelism to hash up to MAX_SIMD_DEGREE parents at the same time
213 // on a single thread. Write out the parent chaining values and return the
214 // number of parents hashed. (If there's an odd input chaining value left over,
215 // return it as an additional output.) These parents are never the root and
216 // never empty; those cases use a different codepath.
217 INLINEcxx . . $ `external/blake3/blake3_impl.h:32:9`! size_t compress_parents_parallelcxx . . $ compress_parents_parallel(5a139dc2b05a9b9c).(const uint8_tcxx . . $ uint8_t# *child_chaining_values,
218 size_t num_chaining_values,
219 const uint32_tcxx . . $ uint32_t# key[8], uint8_tcxx . . $ uint8_t# flags,
220 uint8_tcxx . . $ uint8_t# *out) {
221 #if defined(BLAKE3_TESTING)
222 assert(2 <= num_chaining_values);
223 assert(num_chaining_values <= 2 * MAX_SIMD_DEGREE_OR_2);
224 #endif
225
226 const uint8_tcxx . . $ uint8_t# *parents_array[MAX_SIMD_DEGREE_OR_2cxx . . $ `external/blake3/blake3_impl.h:84:9`!];
227 size_t parents_array_len = 0;
228 while (num_chaining_values - (2 * parents_array_len) >= 2) {
229 parents_array[parents_array_len] =
230 &child_chaining_values[2 * parents_array_len * BLAKE3_OUT_LENcxx . . $ `external/blake3/blake3.h:43:9`!];
231 parents_array_len += 1;
232 }
233
234 blake3_hash_manycxx . . $ blake3_hash_many(6d9d480067751c4).(parents_array, parents_array_len, 1, key,
235 0, // Parents always use counter 0.
236 falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!, flags | PARENTcxx . . $ PARENT.,
237 0, // Parents have no start flags.
238 0, // Parents have no end flags.
239 out);
240
241 // If there's an odd child left over, it becomes an output.
242 if (num_chaining_values > 2 * parents_array_len) {
243 memcpycxx . . $ memcpy(cc817cb175f282d).(&out[parents_array_len * BLAKE3_OUT_LENcxx . . $ `external/blake3/blake3.h:43:9`!],
244 &child_chaining_values[2 * parents_array_len * BLAKE3_OUT_LENcxx . . $ `external/blake3/blake3.h:43:9`!],
245 BLAKE3_OUT_LENcxx . . $ `external/blake3/blake3.h:43:9`!);
246 return parents_array_len + 1;
247 } else {
248 return parents_array_len;
249 }
250 }
251
252 // The wide helper function returns (writes out) an array of chaining values
253 // and returns the length of that array. The number of chaining values returned
254 // is the dynamically detected SIMD degree, at most MAX_SIMD_DEGREE. Or fewer,
255 // if the input is shorter than that many chunks. The reason for maintaining a
256 // wide array of chaining values going back up the tree, is to allow the
257 // implementation to hash as many parents in parallel as possible.
258 //
259 // As a special case when the SIMD degree is 1, this function will still return
260 // at least 2 outputs. This guarantees that this function doesn't perform the
261 // root compression. (If it did, it would use the wrong flags, and also we
262 // wouldn't be able to implement extendable output.) Note that this function is
263 // not used when the whole input is only 1 chunk long; that's a different
264 // codepath.
265 //
266 // Why not just have the caller split the input on the first update(), instead
267 // of implementing this special rule? Because we don't want to limit SIMD or
268 // multi-threading parallelism for that update().
269 size_t blake3_compress_subtree_widecxx . . $ blake3_compress_subtree_wide(813e9ce5a527dc66).(const uint8_tcxx . . $ uint8_t# *input, size_t input_len,
270 const uint32_tcxx . . $ uint32_t# key[8],
271 uint64_tcxx . . $ uint64_t# chunk_counter, uint8_tcxx . . $ uint8_t# flags,
272 uint8_tcxx . . $ uint8_t# *out, boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! use_tbb) {
273 // Note that the single chunk case does *not* bump the SIMD degree up to 2
274 // when it is 1. If this implementation adds multi-threading in the future,
275 // this gives us the option of multi-threading even the 2-chunk case, which
276 // can help performance on smaller platforms.
277 if (input_len <= blake3_simd_degreecxx . . $ blake3_simd_degree(f708dce9a8856c70).() * BLAKE3_CHUNK_LENcxx . . $ `external/blake3/blake3.h:45:9`!) {
278 return compress_chunks_parallelcxx . . $ compress_chunks_parallel(6f3c5acf120589d6).(input, input_len, key, chunk_counter, flags,
279 out);
280 }
281
282 // With more than simd_degree chunks, we need to recurse. Start by dividing
283 // the input into left and right subtrees. (Note that this is only optimal
284 // as long as the SIMD degree is a power of 2. If we ever get a SIMD degree
285 // of 3 or something, we'll need a more complicated strategy.)
286 size_t left_input_len = left_subtree_lencxx . . $ left_subtree_len(ecdaa1632f49f591).(input_len);
287 size_t right_input_len = input_len - left_input_len;
288 const uint8_tcxx . . $ uint8_t# *right_input = &input[left_input_len];
289 uint64_tcxx . . $ uint64_t# right_chunk_counter =
290 chunk_counter + (uint64_tcxx . . $ uint64_t#)(left_input_len / BLAKE3_CHUNK_LENcxx . . $ `external/blake3/blake3.h:45:9`!);
291
292 // Make space for the child outputs. Here we use MAX_SIMD_DEGREE_OR_2 to
293 // account for the special case of returning 2 outputs when the SIMD degree
294 // is 1.
295 uint8_tcxx . . $ uint8_t# cv_array[2 * MAX_SIMD_DEGREE_OR_2cxx . . $ `external/blake3/blake3_impl.h:84:9`! * BLAKE3_OUT_LENcxx . . $ `external/blake3/blake3.h:43:9`!];
296 size_t degree = blake3_simd_degreecxx . . $ blake3_simd_degree(f708dce9a8856c70).();
297 if (left_input_len > BLAKE3_CHUNK_LENcxx . . $ `external/blake3/blake3.h:45:9`! && degree == 1) {
298 // The special case: We always use a degree of at least two, to make
299 // sure there are two outputs. Except, as noted above, at the chunk
300 // level, where we allow degree=1. (Note that the 1-chunk-input case is
301 // a different codepath.)
302 degree = 2;
303 }
304 uint8_tcxx . . $ uint8_t# *right_cvs = &cv_array[degree * BLAKE3_OUT_LENcxx . . $ `external/blake3/blake3.h:43:9`!];
305
306 // Recurse!
307 size_t left_n = SIZE_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:216:11`!;
308 size_t right_n = SIZE_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:216:11`!;
309
310 #if defined(BLAKE3_USE_TBB)
311 blake3_compress_subtree_wide_join_tbb(
312 key, flags, use_tbb,
313 // left-hand side
314 input, left_input_len, chunk_counter, cv_array, &left_n,
315 // right-hand side
316 right_input, right_input_len, right_chunk_counter, right_cvs, &right_n);
317 #else
318 left_n = blake3_compress_subtree_widecxx . . $ blake3_compress_subtree_wide(813e9ce5a527dc66).(
319 input, left_input_len, key, chunk_counter, flags, cv_array, use_tbb);
320 right_n = blake3_compress_subtree_widecxx . . $ blake3_compress_subtree_wide(813e9ce5a527dc66).(right_input, right_input_len, key,
321 right_chunk_counter, flags, right_cvs,
322 use_tbb);
323 #endif // BLAKE3_USE_TBB
324
325 // The special case again. If simd_degree=1, then we'll have left_n=1 and
326 // right_n=1. Rather than compressing them into a single output, return
327 // them directly, to make sure we always have at least two outputs.
328 if (left_n == 1) {
329 memcpycxx . . $ memcpy(cc817cb175f282d).(out, cv_array, 2 * BLAKE3_OUT_LENcxx . . $ `external/blake3/blake3.h:43:9`!);
330 return 2;
331 }
332
333 // Otherwise, do one layer of parent node compression.
334 size_t num_chaining_values = left_n + right_n;
335 return compress_parents_parallelcxx . . $ compress_parents_parallel(5a139dc2b05a9b9c).(cv_array, num_chaining_values, key, flags,
336 out);
337 }
338
339 // Hash a subtree with compress_subtree_wide(), and then condense the resulting
340 // list of chaining values down to a single parent node. Don't compress that
341 // last parent node, however. Instead, return its message bytes (the
342 // concatenated chaining values of its children). This is necessary when the
343 // first call to update() supplies a complete subtree, because the topmost
344 // parent node of that subtree could end up being the root. It's also necessary
345 // for extended output in the general case.
346 //
347 // As with compress_subtree_wide(), this function is not used on inputs of 1
348 // chunk or less. That's a different codepath.
349 INLINEcxx . . $ `external/blake3/blake3_impl.h:32:9`! void
350 compress_subtree_to_parent_nodecxx . . $ compress_subtree_to_parent_node(bd17891f13766bb6).(const uint8_tcxx . . $ uint8_t# *input, size_t input_len,
351 const uint32_tcxx . . $ uint32_t# key[8], uint64_tcxx . . $ uint64_t# chunk_counter,
352 uint8_tcxx . . $ uint8_t# flags, uint8_tcxx . . $ uint8_t# out[2 * BLAKE3_OUT_LENcxx . . $ `external/blake3/blake3.h:43:9`!],
353 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! use_tbb) {
354 #if defined(BLAKE3_TESTING)
355 assert(input_len > BLAKE3_CHUNK_LEN);
356 #endif
357
358 uint8_tcxx . . $ uint8_t# cv_array[MAX_SIMD_DEGREE_OR_2cxx . . $ `external/blake3/blake3_impl.h:84:9`! * BLAKE3_OUT_LENcxx . . $ `external/blake3/blake3.h:43:9`!];
359 size_t num_cvs = blake3_compress_subtree_widecxx . . $ blake3_compress_subtree_wide(813e9ce5a527dc66).(input, input_len, key,
360 chunk_counter, flags, cv_array, use_tbb);
361 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(num_cvs <= MAX_SIMD_DEGREE_OR_2cxx . . $ `external/blake3/blake3_impl.h:84:9`!);
362 // The following loop never executes when MAX_SIMD_DEGREE_OR_2 is 2, because
363 // as we just asserted, num_cvs will always be <=2 in that case. But GCC
364 // (particularly GCC 8.5) can't tell that it never executes, and if NDEBUG is
365 // set then it emits incorrect warnings here. We tried a few different
366 // hacks to silence these, but in the end our hacks just produced different
367 // warnings (see https://github.com/BLAKE3-team/BLAKE3/pull/380). Out of
368 // desperation, we ifdef out this entire loop when we know it's not needed.
369 #if MAX_SIMD_DEGREE_OR_2cxx . . $ `external/blake3/blake3_impl.h:84:9`! > 2
370 // If MAX_SIMD_DEGREE_OR_2 is greater than 2 and there's enough input,
371 // compress_subtree_wide() returns more than 2 chaining values. Condense
372 // them into 2 by forming parent nodes repeatedly.
373 uint8_tcxx . . $ uint8_t# out_array[MAX_SIMD_DEGREE_OR_2cxx . . $ `external/blake3/blake3_impl.h:84:9`! * BLAKE3_OUT_LENcxx . . $ `external/blake3/blake3.h:43:9`! / 2];
374 while (num_cvs > 2) {
375 num_cvs =
376 compress_parents_parallelcxx . . $ compress_parents_parallel(5a139dc2b05a9b9c).(cv_array, num_cvs, key, flags, out_array);
377 memcpycxx . . $ memcpy(cc817cb175f282d).(cv_array, out_array, num_cvs * BLAKE3_OUT_LENcxx . . $ `external/blake3/blake3.h:43:9`!);
378 }
379 #endif
380 memcpycxx . . $ memcpy(cc817cb175f282d).(out, cv_array, 2 * BLAKE3_OUT_LENcxx . . $ `external/blake3/blake3.h:43:9`!);
381 }
382
383 INLINEcxx . . $ `external/blake3/blake3_impl.h:32:9`! void hasher_init_basecxx . . $ hasher_init_base(b574380adc7f89dc).(blake3_hashercxx . . $ blake3_hasher# *self, const uint32_tcxx . . $ uint32_t# key[8],
384 uint8_tcxx . . $ uint8_t# flags) {
385 memcpycxx . . $ memcpy(cc817cb175f282d).(self->keycxx . . $ $anonymous_type_7563367b72f05719_1#key., key, BLAKE3_KEY_LENcxx . . $ `external/blake3/blake3.h:42:9`!);
386 chunk_state_initcxx . . $ chunk_state_init(9c7e888c3389eb38).(&self->chunkcxx . . $ $anonymous_type_7563367b72f05719_1#chunk., key, flags);
387 self->cv_stack_lencxx . . $ $anonymous_type_7563367b72f05719_1#cv_stack_len. = 0;
388 }
389
390 void blake3_hasher_initcxx . . $ blake3_hasher_init(8d666302431a0757).(blake3_hashercxx . . $ blake3_hasher# *self) { hasher_init_basecxx . . $ hasher_init_base(b574380adc7f89dc).(self, IVcxx . . $ IV., 0); }
391
392 void blake3_hasher_init_keyedcxx . . $ blake3_hasher_init_keyed(26a42452aa28e4b6).(blake3_hashercxx . . $ blake3_hasher# *self,
393 const uint8_tcxx . . $ uint8_t# key[BLAKE3_KEY_LENcxx . . $ `external/blake3/blake3.h:42:9`!]) {
394 uint32_tcxx . . $ uint32_t# key_words[8];
395 load_key_wordscxx . . $ load_key_words(c4f0954631f88756).(key, key_words);
396 hasher_init_basecxx . . $ hasher_init_base(b574380adc7f89dc).(self, key_words, KEYED_HASHcxx . . $ KEYED_HASH.);
397 }
398
399 void blake3_hasher_init_derive_key_rawcxx . . $ blake3_hasher_init_derive_key_raw(cf26545aa7beb0fb).(blake3_hashercxx . . $ blake3_hasher# *self, const void *context,
400 size_t context_len) {
401 blake3_hashercxx . . $ blake3_hasher# context_hasher;
402 hasher_init_basecxx . . $ hasher_init_base(b574380adc7f89dc).(&context_hasher, IVcxx . . $ IV., DERIVE_KEY_CONTEXTcxx . . $ DERIVE_KEY_CONTEXT.);
403 blake3_hasher_updatecxx . . $ blake3_hasher_update(cf26545aa7beb0fb).(&context_hasher, context, context_len);
404 uint8_tcxx . . $ uint8_t# context_key[BLAKE3_KEY_LENcxx . . $ `external/blake3/blake3.h:42:9`!];
405 blake3_hasher_finalizecxx . . $ blake3_hasher_finalize(e2bb5693325f82db).(&context_hasher, context_key, BLAKE3_KEY_LENcxx . . $ `external/blake3/blake3.h:42:9`!);
406 uint32_tcxx . . $ uint32_t# context_key_words[8];
407 load_key_wordscxx . . $ load_key_words(c4f0954631f88756).(context_key, context_key_words);
408 hasher_init_basecxx . . $ hasher_init_base(b574380adc7f89dc).(self, context_key_words, DERIVE_KEY_MATERIALcxx . . $ DERIVE_KEY_MATERIAL.);
409 }
410
411 void blake3_hasher_init_derive_keycxx . . $ blake3_hasher_init_derive_key(f1ea73934e53858a).(blake3_hashercxx . . $ blake3_hasher# *self, const char *context) {
412 blake3_hasher_init_derive_key_rawcxx . . $ blake3_hasher_init_derive_key_raw(cf26545aa7beb0fb).(self, context, strlencxx . . $ strlen(751346f8406fd082).(context));
413 }
414
415 // As described in hasher_push_cv() below, we do "lazy merging", delaying
416 // merges until right before the next CV is about to be added. This is
417 // different from the reference implementation. Another difference is that we
418 // aren't always merging 1 chunk at a time. Instead, each CV might represent
419 // any power-of-two number of chunks, as long as the smaller-above-larger stack
420 // order is maintained. Instead of the "count the trailing 0-bits" algorithm
421 // described in the spec, we use a "count the total number of 1-bits" variant
422 // that doesn't require us to retain the subtree size of the CV on top of the
423 // stack. The principle is the same: each CV that should remain in the stack is
424 // represented by a 1-bit in the total number of chunks (or bytes) so far.
425 INLINEcxx . . $ `external/blake3/blake3_impl.h:32:9`! void hasher_merge_cv_stackcxx . . $ hasher_merge_cv_stack(2e7b8fb94d438d10).(blake3_hashercxx . . $ blake3_hasher# *self, uint64_tcxx . . $ uint64_t# total_len) {
426 size_t post_merge_stack_len = (size_t)popcntcxx . . $ popcnt(ea88b852781e8a38).(total_len);
427 while (self->cv_stack_lencxx . . $ $anonymous_type_7563367b72f05719_1#cv_stack_len. > post_merge_stack_len) {
428 uint8_tcxx . . $ uint8_t# *parent_node =
429 &self->cv_stackcxx . . $ $anonymous_type_7563367b72f05719_1#cv_stack.[(self->cv_stack_lencxx . . $ $anonymous_type_7563367b72f05719_1#cv_stack_len. - 2) * BLAKE3_OUT_LENcxx . . $ `external/blake3/blake3.h:43:9`!];
430 output_tcxx . . $ output_t# output = parent_outputcxx . . $ parent_output(bf53c26596625223).(parent_node, self->keycxx . . $ $anonymous_type_7563367b72f05719_1#key., self->chunkcxx . . $ $anonymous_type_7563367b72f05719_1#chunk..flagscxx . . $ $anonymous_type_7563367b72f05719_0#flags.);
431 output_chaining_valuecxx . . $ output_chaining_value(ac1db42c6d7f21d9).(&output, parent_node);
432 self->cv_stack_lencxx . . $ $anonymous_type_7563367b72f05719_1#cv_stack_len. -= 1;
433 }
434 }
435
436 // In reference_impl.rs, we merge the new CV with existing CVs from the stack
437 // before pushing it. We can do that because we know more input is coming, so
438 // we know none of the merges are root.
439 //
440 // This setting is different. We want to feed as much input as possible to
441 // compress_subtree_wide(), without setting aside anything for the chunk_state.
442 // If the user gives us 64 KiB, we want to parallelize over all 64 KiB at once
443 // as a single subtree, if at all possible.
444 //
445 // This leads to two problems:
446 // 1) This 64 KiB input might be the only call that ever gets made to update.
447 // In this case, the root node of the 64 KiB subtree would be the root node
448 // of the whole tree, and it would need to be ROOT finalized. We can't
449 // compress it until we know.
450 // 2) This 64 KiB input might complete a larger tree, whose root node is
451 // similarly going to be the root of the whole tree. For example, maybe
452 // we have 196 KiB (that is, 128 + 64) hashed so far. We can't compress the
453 // node at the root of the 256 KiB subtree until we know how to finalize it.
454 //
455 // The second problem is solved with "lazy merging". That is, when we're about
456 // to add a CV to the stack, we don't merge it with anything first, as the
457 // reference impl does. Instead we do merges using the *previous* CV that was
458 // added, which is sitting on top of the stack, and we put the new CV
459 // (unmerged) on top of the stack afterwards. This guarantees that we never
460 // merge the root node until finalize().
461 //
462 // Solving the first problem requires an additional tool,
463 // compress_subtree_to_parent_node(). That function always returns the top
464 // *two* chaining values of the subtree it's compressing. We then do lazy
465 // merging with each of them separately, so that the second CV will always
466 // remain unmerged. (That also helps us support extendable output when we're
467 // hashing an input all-at-once.)
468 INLINEcxx . . $ `external/blake3/blake3_impl.h:32:9`! void hasher_push_cvcxx . . $ hasher_push_cv(1fb8d4bf05d5afd8).(blake3_hashercxx . . $ blake3_hasher# *self, uint8_tcxx . . $ uint8_t# new_cv[BLAKE3_OUT_LENcxx . . $ `external/blake3/blake3.h:43:9`!],
469 uint64_tcxx . . $ uint64_t# chunk_counter) {
470 hasher_merge_cv_stackcxx . . $ hasher_merge_cv_stack(2e7b8fb94d438d10).(self, chunk_counter);
471 memcpycxx . . $ memcpy(cc817cb175f282d).(&self->cv_stackcxx . . $ $anonymous_type_7563367b72f05719_1#cv_stack.[self->cv_stack_lencxx . . $ $anonymous_type_7563367b72f05719_1#cv_stack_len. * BLAKE3_OUT_LENcxx . . $ `external/blake3/blake3.h:43:9`!], new_cv,
472 BLAKE3_OUT_LENcxx . . $ `external/blake3/blake3.h:43:9`!);
473 self->cv_stack_lencxx . . $ $anonymous_type_7563367b72f05719_1#cv_stack_len. += 1;
474 }
475
476 INLINEcxx . . $ `external/blake3/blake3_impl.h:32:9`! void blake3_hasher_update_basecxx . . $ blake3_hasher_update_base(c6a51354bd16cdd0).(blake3_hashercxx . . $ blake3_hasher# *self, const void *input,
477 size_t input_len, boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! use_tbb) {
478 // Explicitly checking for zero avoids causing UB by passing a null pointer
479 // to memcpy. This comes up in practice with things like:
480 // std::vector<uint8_t> v;
481 // blake3_hasher_update(&hasher, v.data(), v.size());
482 if (input_len == 0) {
483 return;
484 }
485
486 const uint8_tcxx . . $ uint8_t# *input_bytes = (const uint8_tcxx . . $ uint8_t# *)input;
487
488 // If we have some partial chunk bytes in the internal chunk_state, we need
489 // to finish that chunk first.
490 if (chunk_state_lencxx . . $ chunk_state_len(72b7af8eb136547f).(&self->chunkcxx . . $ $anonymous_type_7563367b72f05719_1#chunk.) > 0) {
491 size_t take = BLAKE3_CHUNK_LENcxx . . $ `external/blake3/blake3.h:45:9`! - chunk_state_lencxx . . $ chunk_state_len(72b7af8eb136547f).(&self->chunkcxx . . $ $anonymous_type_7563367b72f05719_1#chunk.);
492 if (take > input_len) {
493 take = input_len;
494 }
495 chunk_state_updatecxx . . $ chunk_state_update(e80c501d2bbb014f).(&self->chunkcxx . . $ $anonymous_type_7563367b72f05719_1#chunk., input_bytes, take);
496 input_bytes += take;
497 input_len -= take;
498 // If we've filled the current chunk and there's more coming, finalize this
499 // chunk and proceed. In this case we know it's not the root.
500 if (input_len > 0) {
501 output_tcxx . . $ output_t# output = chunk_state_outputcxx . . $ chunk_state_output(83e33d8ac68da1a0).(&self->chunkcxx . . $ $anonymous_type_7563367b72f05719_1#chunk.);
502 uint8_tcxx . . $ uint8_t# chunk_cv[32];
503 output_chaining_valuecxx . . $ output_chaining_value(ac1db42c6d7f21d9).(&output, chunk_cv);
504 hasher_push_cvcxx . . $ hasher_push_cv(1fb8d4bf05d5afd8).(self, chunk_cv, self->chunkcxx . . $ $anonymous_type_7563367b72f05719_1#chunk..chunk_countercxx . . $ $anonymous_type_7563367b72f05719_0#chunk_counter.);
505 chunk_state_resetcxx . . $ chunk_state_reset(d14647d89c9e0c8f).(&self->chunkcxx . . $ $anonymous_type_7563367b72f05719_1#chunk., self->keycxx . . $ $anonymous_type_7563367b72f05719_1#key., self->chunkcxx . . $ $anonymous_type_7563367b72f05719_1#chunk..chunk_countercxx . . $ $anonymous_type_7563367b72f05719_0#chunk_counter. + 1);
506 } else {
507 return;
508 }
509 }
510
511 // Now the chunk_state is clear, and we have more input. If there's more than
512 // a single chunk (so, definitely not the root chunk), hash the largest whole
513 // subtree we can, with the full benefits of SIMD (and maybe in the future,
514 // multi-threading) parallelism. Two restrictions:
515 // - The subtree has to be a power-of-2 number of chunks. Only subtrees along
516 // the right edge can be incomplete, and we don't know where the right edge
517 // is going to be until we get to finalize().
518 // - The subtree must evenly divide the total number of chunks up until this
519 // point (if total is not 0). If the current incomplete subtree is only
520 // waiting for 1 more chunk, we can't hash a subtree of 4 chunks. We have
521 // to complete the current subtree first.
522 // Because we might need to break up the input to form powers of 2, or to
523 // evenly divide what we already have, this part runs in a loop.
524 while (input_len > BLAKE3_CHUNK_LENcxx . . $ `external/blake3/blake3.h:45:9`!) {
525 size_t subtree_len = round_down_to_power_of_2cxx . . $ round_down_to_power_of_2(ecdaa1632f49f591).(input_len);
526 uint64_tcxx . . $ uint64_t# count_so_far = self->chunkcxx . . $ $anonymous_type_7563367b72f05719_1#chunk..chunk_countercxx . . $ $anonymous_type_7563367b72f05719_0#chunk_counter. * BLAKE3_CHUNK_LENcxx . . $ `external/blake3/blake3.h:45:9`!;
527 // Shrink the subtree_len until it evenly divides the count so far. We know
528 // that subtree_len itself is a power of 2, so we can use a bitmasking
529 // trick instead of an actual remainder operation. (Note that if the caller
530 // consistently passes power-of-2 inputs of the same size, as is hopefully
531 // typical, this loop condition will always fail, and subtree_len will
532 // always be the full length of the input.)
533 //
534 // An aside: We don't have to shrink subtree_len quite this much. For
535 // example, if count_so_far is 1, we could pass 2 chunks to
536 // compress_subtree_to_parent_node. Since we'll get 2 CVs back, we'll still
537 // get the right answer in the end, and we might get to use 2-way SIMD
538 // parallelism. The problem with this optimization, is that it gets us
539 // stuck always hashing 2 chunks. The total number of chunks will remain
540 // odd, and we'll never graduate to higher degrees of parallelism. See
541 // https://github.com/BLAKE3-team/BLAKE3/issues/69.
542 while ((((uint64_tcxx . . $ uint64_t#)(subtree_len - 1)) & count_so_far) != 0) {
543 subtree_len /= 2;
544 }
545 // The shrunken subtree_len might now be 1 chunk long. If so, hash that one
546 // chunk by itself. Otherwise, compress the subtree into a pair of CVs.
547 uint64_tcxx . . $ uint64_t# subtree_chunks = subtree_len / BLAKE3_CHUNK_LENcxx . . $ `external/blake3/blake3.h:45:9`!;
548 if (subtree_len <= BLAKE3_CHUNK_LENcxx . . $ `external/blake3/blake3.h:45:9`!) {
549 blake3_chunk_statecxx . . $ blake3_chunk_state# chunk_state;
550 chunk_state_initcxx . . $ chunk_state_init(9c7e888c3389eb38).(&chunk_state, self->keycxx . . $ $anonymous_type_7563367b72f05719_1#key., self->chunkcxx . . $ $anonymous_type_7563367b72f05719_1#chunk..flagscxx . . $ $anonymous_type_7563367b72f05719_0#flags.);
551 chunk_state.chunk_countercxx . . $ $anonymous_type_7563367b72f05719_0#chunk_counter. = self->chunkcxx . . $ $anonymous_type_7563367b72f05719_1#chunk..chunk_countercxx . . $ $anonymous_type_7563367b72f05719_0#chunk_counter.;
552 chunk_state_updatecxx . . $ chunk_state_update(e80c501d2bbb014f).(&chunk_state, input_bytes, subtree_len);
553 output_tcxx . . $ output_t# output = chunk_state_outputcxx . . $ chunk_state_output(83e33d8ac68da1a0).(&chunk_state);
554 uint8_tcxx . . $ uint8_t# cv[BLAKE3_OUT_LENcxx . . $ `external/blake3/blake3.h:43:9`!];
555 output_chaining_valuecxx . . $ output_chaining_value(ac1db42c6d7f21d9).(&output, cv);
556 hasher_push_cvcxx . . $ hasher_push_cv(1fb8d4bf05d5afd8).(self, cv, chunk_state.chunk_countercxx . . $ $anonymous_type_7563367b72f05719_0#chunk_counter.);
557 } else {
558 // This is the high-performance happy path, though getting here depends
559 // on the caller giving us a long enough input.
560 uint8_tcxx . . $ uint8_t# cv_pair[2 * BLAKE3_OUT_LENcxx . . $ `external/blake3/blake3.h:43:9`!];
561 compress_subtree_to_parent_nodecxx . . $ compress_subtree_to_parent_node(bd17891f13766bb6).(input_bytes, subtree_len, self->keycxx . . $ $anonymous_type_7563367b72f05719_1#key.,
562 self->chunkcxx . . $ $anonymous_type_7563367b72f05719_1#chunk..chunk_countercxx . . $ $anonymous_type_7563367b72f05719_0#chunk_counter.,
563 self->chunkcxx . . $ $anonymous_type_7563367b72f05719_1#chunk..flagscxx . . $ $anonymous_type_7563367b72f05719_0#flags., cv_pair, use_tbb);
564 hasher_push_cvcxx . . $ hasher_push_cv(1fb8d4bf05d5afd8).(self, cv_pair, self->chunkcxx . . $ $anonymous_type_7563367b72f05719_1#chunk..chunk_countercxx . . $ $anonymous_type_7563367b72f05719_0#chunk_counter.);
565 hasher_push_cvcxx . . $ hasher_push_cv(1fb8d4bf05d5afd8).(self, &cv_pair[BLAKE3_OUT_LENcxx . . $ `external/blake3/blake3.h:43:9`!],
566 self->chunkcxx . . $ $anonymous_type_7563367b72f05719_1#chunk..chunk_countercxx . . $ $anonymous_type_7563367b72f05719_0#chunk_counter. + (subtree_chunks / 2));
567 }
568 self->chunkcxx . . $ $anonymous_type_7563367b72f05719_1#chunk..chunk_countercxx . . $ $anonymous_type_7563367b72f05719_0#chunk_counter. += subtree_chunks;
569 input_bytes += subtree_len;
570 input_len -= subtree_len;
571 }
572
573 // If there's any remaining input less than a full chunk, add it to the chunk
574 // state. In that case, also do a final merge loop to make sure the subtree
575 // stack doesn't contain any unmerged pairs. The remaining input means we
576 // know these merges are non-root. This merge loop isn't strictly necessary
577 // here, because hasher_push_chunk_cv already does its own merge loop, but it
578 // simplifies blake3_hasher_finalize below.
579 if (input_len > 0) {
580 chunk_state_updatecxx . . $ chunk_state_update(e80c501d2bbb014f).(&self->chunkcxx . . $ $anonymous_type_7563367b72f05719_1#chunk., input_bytes, input_len);
581 hasher_merge_cv_stackcxx . . $ hasher_merge_cv_stack(2e7b8fb94d438d10).(self, self->chunkcxx . . $ $anonymous_type_7563367b72f05719_1#chunk..chunk_countercxx . . $ $anonymous_type_7563367b72f05719_0#chunk_counter.);
582 }
583 }
584
585 void blake3_hasher_updatecxx . . $ blake3_hasher_update(cf26545aa7beb0fb).(blake3_hashercxx . . $ blake3_hasher# *self, const void *input,
586 size_t input_len) {
587 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! use_tbb = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
588 blake3_hasher_update_basecxx . . $ blake3_hasher_update_base(c6a51354bd16cdd0).(self, input, input_len, use_tbb);
589 }
590
591 #if defined(BLAKE3_USE_TBB)
592 void blake3_hasher_update_tbb(blake3_hasher *self, const void *input,
593 size_t input_len) {
594 bool use_tbb = true;
595 blake3_hasher_update_base(self, input, input_len, use_tbb);
596 }
597 #endif // BLAKE3_USE_TBB
598
599 void blake3_hasher_finalizecxx . . $ blake3_hasher_finalize(e2bb5693325f82db).(const blake3_hashercxx . . $ blake3_hasher# *self, uint8_tcxx . . $ uint8_t# *out,
600 size_t out_len) {
601 blake3_hasher_finalize_seekcxx . . $ blake3_hasher_finalize_seek(b7cba62a4a9650c2).(self, 0, out, out_len);
602 }
603
604 void blake3_hasher_finalize_seekcxx . . $ blake3_hasher_finalize_seek(b7cba62a4a9650c2).(const blake3_hashercxx . . $ blake3_hasher# *self, uint64_tcxx . . $ uint64_t# seek,
605 uint8_tcxx . . $ uint8_t# *out, size_t out_len) {
606 // Explicitly checking for zero avoids causing UB by passing a null pointer
607 // to memcpy. This comes up in practice with things like:
608 // std::vector<uint8_t> v;
609 // blake3_hasher_finalize(&hasher, v.data(), v.size());
610 if (out_len == 0) {
611 return;
612 }
613
614 // If the subtree stack is empty, then the current chunk is the root.
615 if (self->cv_stack_lencxx . . $ $anonymous_type_7563367b72f05719_1#cv_stack_len. == 0) {
616 output_tcxx . . $ output_t# output = chunk_state_outputcxx . . $ chunk_state_output(83e33d8ac68da1a0).(&self->chunkcxx . . $ $anonymous_type_7563367b72f05719_1#chunk.);
617 output_root_bytescxx . . $ output_root_bytes(4a25d6d8e89fd96).(&output, seek, out, out_len);
618 return;
619 }
620 // If there are any bytes in the chunk state, finalize that chunk and do a
621 // roll-up merge between that chunk hash and every subtree in the stack. In
622 // this case, the extra merge loop at the end of blake3_hasher_update
623 // guarantees that none of the subtrees in the stack need to be merged with
624 // each other first. Otherwise, if there are no bytes in the chunk state,
625 // then the top of the stack is a chunk hash, and we start the merge from
626 // that.
627 output_tcxx . . $ output_t# output;
628 size_t cvs_remaining;
629 if (chunk_state_lencxx . . $ chunk_state_len(72b7af8eb136547f).(&self->chunkcxx . . $ $anonymous_type_7563367b72f05719_1#chunk.) > 0) {
630 cvs_remaining = self->cv_stack_lencxx . . $ $anonymous_type_7563367b72f05719_1#cv_stack_len.;
631 output = chunk_state_outputcxx . . $ chunk_state_output(83e33d8ac68da1a0).(&self->chunkcxx . . $ $anonymous_type_7563367b72f05719_1#chunk.);
632 } else {
633 // There are always at least 2 CVs in the stack in this case.
634 cvs_remaining = self->cv_stack_lencxx . . $ $anonymous_type_7563367b72f05719_1#cv_stack_len. - 2;
635 output = parent_outputcxx . . $ parent_output(bf53c26596625223).(&self->cv_stackcxx . . $ $anonymous_type_7563367b72f05719_1#cv_stack.[cvs_remaining * 32], self->keycxx . . $ $anonymous_type_7563367b72f05719_1#key.,
636 self->chunkcxx . . $ $anonymous_type_7563367b72f05719_1#chunk..flagscxx . . $ $anonymous_type_7563367b72f05719_0#flags.);
637 }
638 while (cvs_remaining > 0) {
639 cvs_remaining -= 1;
640 uint8_tcxx . . $ uint8_t# parent_block[BLAKE3_BLOCK_LENcxx . . $ `external/blake3/blake3.h:44:9`!];
641 memcpycxx . . $ memcpy(cc817cb175f282d).(parent_block, &self->cv_stackcxx . . $ $anonymous_type_7563367b72f05719_1#cv_stack.[cvs_remaining * 32], 32);
642 output_chaining_valuecxx . . $ output_chaining_value(ac1db42c6d7f21d9).(&output, &parent_block[32]);
643 output = parent_outputcxx . . $ parent_output(bf53c26596625223).(parent_block, self->keycxx . . $ $anonymous_type_7563367b72f05719_1#key., self->chunkcxx . . $ $anonymous_type_7563367b72f05719_1#chunk..flagscxx . . $ $anonymous_type_7563367b72f05719_0#flags.);
644 }
645 output_root_bytescxx . . $ output_root_bytes(4a25d6d8e89fd96).(&output, seek, out, out_len);
646 }
647
648 void blake3_hasher_resetcxx . . $ blake3_hasher_reset(8d666302431a0757).(blake3_hashercxx . . $ blake3_hasher# *self) {
649 chunk_state_resetcxx . . $ chunk_state_reset(d14647d89c9e0c8f).(&self->chunkcxx . . $ $anonymous_type_7563367b72f05719_1#chunk., self->keycxx . . $ $anonymous_type_7563367b72f05719_1#key., 0);
650 self->cv_stack_lencxx . . $ $anonymous_type_7563367b72f05719_1#cv_stack_len. = 0;
651 }