mirror of
https://github.com/henrydcase/nobs.git
synced 2024-11-22 15:18:57 +00:00
SHA-3: speedups (#47)
* add function for one-off calculation * sha3: simplifies Read function * sha3: remove if from Read
This commit is contained in:
parent
45bc1a75f6
commit
8474981cfc
@ -157,44 +157,36 @@ func (c *state) Read(out []byte) (nread int, err error) {
|
|||||||
rate := c.BlockSize()
|
rate := c.BlockSize()
|
||||||
nread = len(out)
|
nread = len(out)
|
||||||
|
|
||||||
|
// finalize if not done yet
|
||||||
if !c.isSquezing {
|
if !c.isSquezing {
|
||||||
c.finalize_sha3()
|
c.finalize_sha3()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy-out bytes that are still kept in the buffer
|
// Copy-out bytes that are still kept in the buffer
|
||||||
if c.idx != 0 {
|
l := min(c.idx, len(out))
|
||||||
l := min(c.idx, len(out))
|
copy(out, buf[rate-c.idx:rate-c.idx+l])
|
||||||
idx := rate - c.idx
|
out = out[l:]
|
||||||
copy(out, buf[idx:idx+l])
|
c.idx -= l
|
||||||
out = out[l:]
|
|
||||||
c.idx -= l
|
|
||||||
}
|
|
||||||
|
|
||||||
l := len(out)
|
if len(out) == 0 {
|
||||||
if l == 0 {
|
|
||||||
// nothing else todo
|
// nothing else todo
|
||||||
return nread, nil
|
return nread, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// copy out full blocks and squeeze. at this point
|
// copy out full blocks and squeeze. at this point
|
||||||
// there is no more data in the buffer.
|
// there is no more data in the buffer.
|
||||||
nblocks := l / rate
|
nblocks := len(out) / rate
|
||||||
for i := 0; i < nblocks; i++ {
|
for nblocks > 0 {
|
||||||
keccakF1600(&c.a)
|
keccakF1600(&c.a)
|
||||||
copyOut(c, out[:rate])
|
copyOut(c, out[:rate])
|
||||||
out = out[rate:]
|
out = out[rate:]
|
||||||
}
|
nblocks--
|
||||||
|
|
||||||
// produce more if needed
|
|
||||||
l = len(out)
|
|
||||||
if l == 0 {
|
|
||||||
return nread, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
keccakF1600(&c.a)
|
keccakF1600(&c.a)
|
||||||
copyOut(c, buf)
|
copyOut(c, buf)
|
||||||
copy(out, buf[:l])
|
copy(out, buf[:len(out)])
|
||||||
c.idx = rate - l
|
c.idx = rate - len(out)
|
||||||
return nread, nil
|
return nread, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user