| ... | @@ -78,7 +78,7 @@ pub fn KeccakF(comptime f: u11) type { | ... | @@ -78,7 +78,7 @@ pub fn KeccakF(comptime f: u11) type { |
| 78 | | 78 | |
| 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 | } |
| 84 | | 84 | |
| ... | @@ -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 | } |
| 384 | | 384 | |
| | 385 | test "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 | |
| 385 | test "squeeze" { | 400 | test "squeeze" { |
| 386 | var st: State(800, 256, 22) = .init(@splat(0x80), 0x01); | 401 | var st: State(800, 256, 22) = .init(@splat(0x80), 0x01); |
| 387 | | 402 | |