From 8474981cfcd3403bc880345c62f73d195f100e57 Mon Sep 17 00:00:00 2001 From: Henry Case Date: Sat, 3 Oct 2020 23:27:08 +0100 Subject: [PATCH] SHA-3: speedups (#47) * add function for one-off calculation * sha3: simplifies Read function * sha3: remove if from Read --- hash/sha3/sha3.go | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/hash/sha3/sha3.go b/hash/sha3/sha3.go index 14be08c..d08dff8 100644 --- a/hash/sha3/sha3.go +++ b/hash/sha3/sha3.go @@ -157,44 +157,36 @@ func (c *state) Read(out []byte) (nread int, err error) { rate := c.BlockSize() nread = len(out) + // finalize if not done yet if !c.isSquezing { c.finalize_sha3() } // Copy-out bytes that are still kept in the buffer - if c.idx != 0 { - l := min(c.idx, len(out)) - idx := rate - c.idx - copy(out, buf[idx:idx+l]) - out = out[l:] - c.idx -= l - } + l := min(c.idx, len(out)) + copy(out, buf[rate-c.idx:rate-c.idx+l]) + out = out[l:] + c.idx -= l - l := len(out) - if l == 0 { + if len(out) == 0 { // nothing else todo return nread, nil } // copy out full blocks and squeeze. at this point // there is no more data in the buffer. - nblocks := l / rate - for i := 0; i < nblocks; i++ { + nblocks := len(out) / rate + for nblocks > 0 { keccakF1600(&c.a) copyOut(c, out[:rate]) out = out[rate:] - } - - // produce more if needed - l = len(out) - if l == 0 { - return nread, nil + nblocks-- } keccakF1600(&c.a) copyOut(c, buf) - copy(out, buf[:l]) - c.idx = rate - l + copy(out, buf[:len(out)]) + c.idx = rate - len(out) return nread, nil }