| ... | ... | @@ -14,17 +14,19 @@ pub const Keccak_512 = Keccak(512, 0x01); |
| 14 | 14 | fn Keccak(comptime bits: usize, comptime delim: u8) type { |
| 15 | 15 | return struct { |
| 16 | 16 | const Self = @This(); |
| 17 | | pub const block_length = 200; |
| 17 | /// The output length, in bytes. |
| 18 | 18 | pub const digest_length = bits / 8; |
| 19 | /// The block length, or rate, in bytes. |
| 20 | pub const block_length = 200 - bits / 4; |
| 21 | /// Keccak does not have any options. |
| 19 | 22 | pub const Options = struct {}; |
| 20 | 23 | |
| 21 | 24 | s: [200]u8, |
| 22 | 25 | offset: usize, |
| 23 | | rate: usize, |
| 24 | 26 | |
| 25 | 27 | pub fn init(options: Options) Self { |
| 26 | 28 | _ = options; |
| 27 | | return Self{ .s = [_]u8{0} ** 200, .offset = 0, .rate = 200 - (bits / 4) }; |
| 29 | return Self{ .s = [_]u8{0} ** 200, .offset = 0 }; |
| 28 | 30 | } |
| 29 | 31 | |
| 30 | 32 | pub fn hash(b: []const u8, out: *[digest_length]u8, options: Options) void { |
| ... | ... | @@ -36,7 +38,7 @@ fn Keccak(comptime bits: usize, comptime delim: u8) type { |
| 36 | 38 | pub fn update(d: *Self, b: []const u8) void { |
| 37 | 39 | var ip: usize = 0; |
| 38 | 40 | var len = b.len; |
| 39 | | var rate = d.rate - d.offset; |
| 41 | var rate = block_length - d.offset; |
| 40 | 42 | var offset = d.offset; |
| 41 | 43 | |
| 42 | 44 | // absorb |
| ... | ... | @@ -48,7 +50,7 @@ fn Keccak(comptime bits: usize, comptime delim: u8) type { |
| 48 | 50 | |
| 49 | 51 | ip += rate; |
| 50 | 52 | len -= rate; |
| 51 | | rate = d.rate; |
| 53 | rate = block_length; |
| 52 | 54 | offset = 0; |
| 53 | 55 | } |
| 54 | 56 | |
| ... | ... | @@ -61,7 +63,7 @@ fn Keccak(comptime bits: usize, comptime delim: u8) type { |
| 61 | 63 | pub fn final(d: *Self, out: *[digest_length]u8) void { |
| 62 | 64 | // padding |
| 63 | 65 | d.s[d.offset] ^= delim; |
| 64 | | d.s[d.rate - 1] ^= 0x80; |
| 66 | d.s[block_length - 1] ^= 0x80; |
| 65 | 67 | |
| 66 | 68 | keccakF(1600, &d.s); |
| 67 | 69 | |
| ... | ... | @@ -69,11 +71,11 @@ fn Keccak(comptime bits: usize, comptime delim: u8) type { |
| 69 | 71 | var op: usize = 0; |
| 70 | 72 | var len: usize = bits / 8; |
| 71 | 73 | |
| 72 | | while (len >= d.rate) { |
| 73 | | mem.copy(u8, out[op..], d.s[0..d.rate]); |
| 74 | while (len >= block_length) { |
| 75 | mem.copy(u8, out[op..], d.s[0..block_length]); |
| 74 | 76 | keccakF(1600, &d.s); |
| 75 | | op += d.rate; |
| 76 | | len -= d.rate; |
| 77 | op += block_length; |
| 78 | len -= block_length; |
| 77 | 79 | } |
| 78 | 80 | |
| 79 | 81 | mem.copy(u8, out[op..], d.s[0..len]); |