From 5baa05664e6dac0f473c8411f6e9d8e0f62555a9 Mon Sep 17 00:00:00 2001 From: Marc Tiehuis Date: Sat, 17 Jun 2023 20:22:16 +1200 Subject: [PATCH] wyhash: support comptime usage Closes #16070. --- lib/std/hash/wyhash.zig | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/lib/std/hash/wyhash.zig b/lib/std/hash/wyhash.zig index c36c3fe87cedb1d408266d9c09ca038dac3fd79f..3573745444e9acd204c328ad418c1471bc29c72c 100644 --- a/lib/std/hash/wyhash.zig +++ b/lib/std/hash/wyhash.zig @@ -68,18 +68,20 @@ pub const Wyhash = struct { if (self.total_len <= 16) { newSelf.smallKey(input); } else { + var offset: usize = 0; if (self.buf_len < 16) { var scratch: [16]u8 = undefined; const rem = 16 - self.buf_len; @memcpy(scratch[0..rem], self.buf[self.buf.len - rem ..][0..rem]); @memcpy(scratch[rem..][0..self.buf_len], self.buf[0..self.buf_len]); - // Same as input with lookbehind to pad to 16-bytes - input = scratch[rem..]; + // Same as input but with additional bytes preceeding start in case of a short buffer + input = &scratch; + offset = rem; } newSelf.final0(); - newSelf.final1(input); + newSelf.final1(input, offset); } return newSelf.final2(); @@ -145,19 +147,21 @@ pub const Wyhash = struct { self.state[0] ^= self.state[1] ^ self.state[2]; } - // Input must reside in a 16-byte buffer. The input slice passed be offset into it in which - // case this function will index in front of the slice. - inline fn final1(self: *Wyhash, input: []const u8) void { - std.debug.assert(input.len <= 48); + // input_lb must be at least 16-bytes long (in shorter key cases the smallKey function will be + // used instead). We use an index into a slice to for comptime processing as opposed to if we + // used pointers. + inline fn final1(self: *Wyhash, input_lb: []const u8, start_pos: usize) void { + std.debug.assert(input_lb.len >= 16); + std.debug.assert(input_lb.len - start_pos <= 48); + const input = input_lb[start_pos..]; var i: usize = 0; while (i + 16 < input.len) : (i += 16) { self.state[0] = mix(read(8, input[i..]) ^ secret[1], read(8, input[i + 8 ..]) ^ self.state[0]); } - // Possible lookbehind past pointer start. - self.a = read(8, (input.ptr + input.len - 16)[0..8]); - self.b = read(8, (input.ptr + input.len - 8)[0..8]); + self.a = read(8, input_lb[input_lb.len - 16 ..][0..8]); + self.b = read(8, input_lb[input_lb.len - 8 ..][0..8]); } inline fn final2(self: *Wyhash) u64 { @@ -180,7 +184,7 @@ pub const Wyhash = struct { } self.final0(); } - self.final1(input[i..]); + self.final1(input, i); } self.total_len = input.len; @@ -213,6 +217,14 @@ test "test vectors" { } } +test "test vectors at comptime" { + comptime { + inline for (vectors) |e| { + try expectEqual(e.expected, Wyhash.hash(e.seed, e.input)); + } + } +} + test "test vectors streaming" { const step = 5; -- 2.54.0