authorgravatar for jay@jayschwa.netJay Petacat <jay@jayschwa.net> 2020-02-02 14:08:10-05:00
committergravatar for jay@jayschwa.netJay Petacat <jay@jayschwa.net> 2020-02-02 14:08:10-05:00
logd098e212ad9b18cd0f74b19dea0f4f3b11092dca
treed8b692cee926f928af06d27ad9e08f384567185f
parent4b86c1e3bbe65b8caa9c2e769af633fa8825bb94

blake3: Convert `*const [n]u8` types to `[n]u8`

I do not see many cases of constant pointers to arrays in the stdlib. In fact, this makes the code run a little faster, probably because Zig automatically converts to pointers where it makes sense.

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

lib/std/crypto/blake3.zig+19-19
......@@ -70,7 +70,7 @@ fn g(state: *[16]u32, a: usize, b: usize, c: usize, d: usize, mx: u32, my: u32)
7070 state[b] = math.rotr(u32, state[b] ^ state[c], 7);
7171}
7272
73fn round(state: *[16]u32, msg: *const [16]u32, schedule: *const [16]u8) void {
73fn round(state: *[16]u32, msg: [16]u32, schedule: [16]u8) void {
7474 // Mix the columns.
7575 g(state, 0, 4, 8, 12, msg[schedule[0]], msg[schedule[1]]);
7676 g(state, 1, 5, 9, 13, msg[schedule[2]], msg[schedule[3]]);
......@@ -85,8 +85,8 @@ fn round(state: *[16]u32, msg: *const [16]u32, schedule: *const [16]u8) void {
8585}
8686
8787fn compress(
88 chaining_value: *const [8]u32,
89 block_words: *const [16]u32,
88 chaining_value: [8]u32,
89 block_words: [16]u32,
9090 block_len: u32,
9191 counter: u64,
9292 flags: u8,
......@@ -109,7 +109,7 @@ fn compress(
109109 block_len,
110110 flags,
111111 };
112 for (MSG_SCHEDULE) |*schedule| {
112 for (MSG_SCHEDULE) |schedule| {
113113 round(&state, block_words, schedule);
114114 }
115115 for (chaining_value) |_, i| {
......@@ -143,8 +143,8 @@ const Output = struct {
143143
144144 fn chaining_value(self: *Output) [8]u32 {
145145 return first_8_words(compress(
146 &self.input_chaining_value,
147 &self.block_words,
146 self.input_chaining_value,
147 self.block_words,
148148 self.block_len,
149149 self.counter,
150150 self.flags,
......@@ -156,8 +156,8 @@ const Output = struct {
156156 var output_block_counter: usize = 0;
157157 while (out_block_it.next()) |out_block| {
158158 var words = compress(
159 &self.input_chaining_value,
160 &self.block_words,
159 self.input_chaining_value,
160 self.block_words,
161161 self.block_len,
162162 output_block_counter,
163163 self.flags | ROOT,
......@@ -216,8 +216,8 @@ const ChunkState = struct {
216216 var block_words: [16]u32 = undefined;
217217 words_from_little_endian_bytes(&self.block, &block_words);
218218 self.chaining_value = first_8_words(compress(
219 &self.chaining_value,
220 &block_words,
219 self.chaining_value,
220 block_words,
221221 BLOCK_LEN,
222222 self.chunk_counter,
223223 self.flags | self.start_flag(),
......@@ -232,7 +232,7 @@ const ChunkState = struct {
232232 }
233233 }
234234
235 fn output(self: *ChunkState) Output {
235 fn output(self: *const ChunkState) Output {
236236 var block_words: [16]u32 = undefined;
237237 words_from_little_endian_bytes(&self.block, &block_words);
238238 return Output{
......@@ -297,9 +297,9 @@ pub const Blake3 = struct {
297297 }
298298
299299 /// Construct a new `Blake3` for the keyed hash function.
300 pub fn init_keyed(key: *const [KEY_LEN]u8) Blake3 {
300 pub fn init_keyed(key: [KEY_LEN]u8) Blake3 {
301301 var key_words: [8]u32 = undefined;
302 words_from_little_endian_bytes(key, &key_words);
302 words_from_little_endian_bytes(key[0..], &key_words);
303303 return Blake3.init_internal(key_words, KEYED_HASH);
304304 }
305305
......@@ -550,7 +550,7 @@ const reference_test = .{
550550 },
551551};
552552
553fn test_blake3(hasher: *Blake3, input_len: usize, expected_hex: *const [262]u8) void {
553fn test_blake3(hasher: *Blake3, input_len: usize, expected_hex: [262]u8) void {
554554 // Setup input pattern
555555 var input_pattern: [251]u8 = undefined;
556556 for (input_pattern) |*e, i| e.* = @truncate(u8, i);
......@@ -570,18 +570,18 @@ fn test_blake3(hasher: *Blake3, input_len: usize, expected_hex: *const [262]u8)
570570
571571 // Compare to expected value
572572 var expected_bytes: [expected_hex.len / 2]u8 = undefined;
573 fmt.hexToBytes(expected_bytes[0..], expected_hex) catch unreachable;
573 fmt.hexToBytes(expected_bytes[0..], expected_hex[0..]) catch unreachable;
574574 testing.expectEqual(actual_bytes, expected_bytes);
575575}
576576
577577test "BLAKE3 reference test cases" {
578578 var hash = &Blake3.init();
579 var keyed_hash = &Blake3.init_keyed(reference_test.key);
579 var keyed_hash = &Blake3.init_keyed(reference_test.key.*);
580580 var derive_key = &Blake3.init_derive_key(reference_test.context_string);
581581
582582 for (reference_test.cases) |t| {
583 test_blake3(hash, t.input_len, t.hash);
584 test_blake3(keyed_hash, t.input_len, t.keyed_hash);
585 test_blake3(derive_key, t.input_len, t.derive_key);
583 test_blake3(hash, t.input_len, t.hash.*);
584 test_blake3(keyed_hash, t.input_len, t.keyed_hash.*);
585 test_blake3(derive_key, t.input_len, t.derive_key.*);
586586 }
587587}