authorgravatar for jay@jayschwa.netJay Petacat <jay@jayschwa.net> 2020-02-02 14:42:57-05:00
committergravatar for jay@jayschwa.netJay Petacat <jay@jayschwa.net> 2020-02-02 14:42:57-05:00
logb143fc0d32cff6bb6c2d6abe704c33de3f83ba35
tree2bb10bf2a22b12a3ca8cf0b0171b1a2784237662
parentd098e212ad9b18cd0f74b19dea0f4f3b11092dca

blake3: Name and const pointer refinements


1 files changed, 14 insertions(+), 14 deletions(-)

lib/std/crypto/blake3.zig+14-14
......@@ -119,11 +119,11 @@ fn compress(
119119 return state;
120120}
121121
122fn first_8_words(compression_output: [16]u32) [8]u32 {
123 return @ptrCast(*const [8]u32, &compression_output).*;
122fn first_8_words(words: [16]u32) [8]u32 {
123 return @ptrCast(*const [8]u32, &words).*;
124124}
125125
126fn words_from_little_endian_bytes(bytes: []const u8, words: []u32) void {
126fn words_from_little_endian_bytes(words: []u32, bytes: []const u8) void {
127127 var byte_slice = bytes;
128128 for (words) |*word| {
129129 word.* = mem.readIntSliceLittle(u32, byte_slice);
......@@ -141,7 +141,7 @@ const Output = struct {
141141 counter: u64,
142142 flags: u8,
143143
144 fn chaining_value(self: *Output) [8]u32 {
144 fn chaining_value(self: *const Output) [8]u32 {
145145 return first_8_words(compress(
146146 self.input_chaining_value,
147147 self.block_words,
......@@ -151,7 +151,7 @@ const Output = struct {
151151 ));
152152 }
153153
154 fn root_output_bytes(self: *Output, output: []u8) void {
154 fn root_output_bytes(self: *const Output, output: []u8) void {
155155 var out_block_it = ChunkIterator.init(output, 2 * OUT_LEN);
156156 var output_block_counter: usize = 0;
157157 while (out_block_it.next()) |out_block| {
......@@ -214,7 +214,7 @@ const ChunkState = struct {
214214 // input is coming, so this compression is not CHUNK_END.
215215 if (self.block_len == BLOCK_LEN) {
216216 var block_words: [16]u32 = undefined;
217 words_from_little_endian_bytes(&self.block, &block_words);
217 words_from_little_endian_bytes(&block_words, &self.block);
218218 self.chaining_value = first_8_words(compress(
219219 self.chaining_value,
220220 block_words,
......@@ -234,7 +234,7 @@ const ChunkState = struct {
234234
235235 fn output(self: *const ChunkState) Output {
236236 var block_words: [16]u32 = undefined;
237 words_from_little_endian_bytes(&self.block, &block_words);
237 words_from_little_endian_bytes(&block_words, &self.block);
238238 return Output{
239239 .input_chaining_value = self.chaining_value,
240240 .block_words = block_words,
......@@ -299,7 +299,7 @@ pub const Blake3 = struct {
299299 /// Construct a new `Blake3` for the keyed hash function.
300300 pub fn init_keyed(key: [KEY_LEN]u8) Blake3 {
301301 var key_words: [8]u32 = undefined;
302 words_from_little_endian_bytes(key[0..], &key_words);
302 words_from_little_endian_bytes(&key_words, key[0..]);
303303 return Blake3.init_internal(key_words, KEYED_HASH);
304304 }
305305
......@@ -311,7 +311,7 @@ pub const Blake3 = struct {
311311 var context_key: [KEY_LEN]u8 = undefined;
312312 context_hasher.final(context_key[0..]);
313313 var context_key_words: [8]u32 = undefined;
314 words_from_little_endian_bytes(&context_key, &context_key_words);
314 words_from_little_endian_bytes(&context_key_words, &context_key);
315315 return Blake3.init_internal(context_key_words, DERIVE_KEY_MATERIAL);
316316 }
317317
......@@ -327,12 +327,12 @@ pub const Blake3 = struct {
327327 self.cv_stack_len = 0;
328328 }
329329
330 fn push_stack(self: *Blake3, cv: [8]u32) void {
330 fn push_cv(self: *Blake3, cv: [8]u32) void {
331331 self.cv_stack[self.cv_stack_len] = cv;
332332 self.cv_stack_len += 1;
333333 }
334334
335 fn pop_stack(self: *Blake3) [8]u32 {
335 fn pop_cv(self: *Blake3) [8]u32 {
336336 self.cv_stack_len -= 1;
337337 return self.cv_stack[self.cv_stack_len];
338338 }
......@@ -348,10 +348,10 @@ pub const Blake3 = struct {
348348 // by the number of trailing 0-bits in the new total number of chunks.
349349 var chunk_counter = total_chunks;
350350 while (chunk_counter & 1 == 0) {
351 new_cv = parent_cv(self.pop_stack(), new_cv, self.key, self.flags);
351 new_cv = parent_cv(self.pop_cv(), new_cv, self.key, self.flags);
352352 chunk_counter >>= 1;
353353 }
354 self.push_stack(new_cv);
354 self.push_cv(new_cv);
355355 }
356356
357357 /// Add input to the hash state. This can be called any number of times.
......@@ -376,7 +376,7 @@ pub const Blake3 = struct {
376376 }
377377
378378 /// Finalize the hash and write any number of output bytes.
379 pub fn final(self: *Blake3, out_slice: []u8) void {
379 pub fn final(self: *const Blake3, out_slice: []u8) void {
380380 // Starting with the Output from the current chunk, compute all the
381381 // parent chaining values along the right edge of the tree, until we
382382 // have the root Output.