authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2026-10-08 13:14:29+02:00
committergravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2026-10-16 08:23:23+02:00
log3ee1001f7a5277cc5d61cc9ae75dc5ce45ec38eb
treed2c16447d81f1c16079767883b156caf0fb55717
parentfc78a22b96035b023c4fb138baee0ac9dcc4c42b

std.crypto.keccak_p: make addByte properly work with any size

Not just for keccak_p[1600].

2 files changed, 17 insertions(+), 1 deletions(-)

lib/std/crypto.zig+1
...@@ -302,6 +302,7 @@ test {...@@ -302,6 +302,7 @@ test {
302 _ = core.aes;302 _ = core.aes;
303 _ = core.keccak;303 _ = core.keccak;
304 _ = core.Ascon;304 _ = core.Ascon;
305 _ = core.keccak;
305 _ = core.modes;306 _ = core.modes;
306307
307 _ = dh.X25519;308 _ = dh.X25519;
lib/std/crypto/keccak_p.zig+16-1
...@@ -78,7 +78,7 @@ pub fn KeccakF(comptime f: u11) type {...@@ -78,7 +78,7 @@ pub fn KeccakF(comptime f: u11) type {
7878
79 /// XOR a byte into the state at a given offset.79 /// XOR a byte into the state at a given offset.
80 pub fn addByte(self: *Self, byte: u8, offset: usize) void {80 pub fn addByte(self: *Self, byte: u8, offset: usize) void {
81 const z = @sizeOf(T) * @as(math.Log2Int(T), @truncate(offset % @sizeOf(T)));81 const z: math.Log2Int(T) = @intCast(8 * (offset % @sizeOf(T)));
82 self.st[offset / @sizeOf(T)] ^= @as(T, byte) << z;82 self.st[offset / @sizeOf(T)] ^= @as(T, byte) << z;
83 }83 }
8484
...@@ -382,6 +382,21 @@ test "Keccak-f800" {...@@ -382,6 +382,21 @@ test "Keccak-f800" {
382 try std.testing.expectEqualSlices(u32, &st.st, &expected);382 try std.testing.expectEqualSlices(u32, &st.st, &expected);
383}383}
384384
385test "addByte" {
386 inline for (.{ 200, 400, 800, 1600 }) |f| {
387 const F = KeccakF(f);
388 for (0..F.block_bytes) |offset| {
389 var a: F = .{};
390 a.addByte(0xa5, offset);
391 var bytes: [F.block_bytes]u8 = @splat(0);
392 bytes[offset] = 0xa5;
393 var b: F = .{};
394 b.addBytes(&bytes);
395 try std.testing.expectEqual(b.st, a.st);
396 }
397 }
398}
399
385test "squeeze" {400test "squeeze" {
386 var st: State(800, 256, 22) = .init(@splat(0x80), 0x01);401 var st: State(800, 256, 22) = .init(@splat(0x80), 0x01);
387402