Skip to content

Commit 10f366e

Browse files
FiloSottilegopherbot
authored andcommitted
sha3: simplify XOR functions
name old time/op new time/op delta PermutationFunction-4 398ns ± 0% 399ns ± 1% ~ (p=0.508 n=9+10) Sha3_512_MTU-4 8.34µs ± 1% 8.36µs ± 1% ~ (p=0.101 n=10+10) Sha3_384_MTU-4 6.00µs ± 0% 6.02µs ± 1% +0.47% (p=0.000 n=8+10) Sha3_256_MTU-4 4.78µs ± 0% 4.79µs ± 1% ~ (p=0.324 n=10+10) Sha3_224_MTU-4 4.57µs ± 1% 4.57µs ± 1% ~ (p=0.288 n=10+10) Shake128_MTU-4 3.87µs ± 0% 3.86µs ± 1% -0.22% (p=0.008 n=9+9) Shake256_MTU-4 4.17µs ± 0% 4.17µs ± 0% ~ (p=0.474 n=10+8) Shake256_16x-4 59.4µs ± 0% 59.7µs ± 0% +0.48% (p=0.000 n=9+8) Shake256_1MiB-4 3.19ms ± 1% 3.20ms ± 0% ~ (p=0.105 n=10+10) Sha3_512_1MiB-4 5.97ms ± 0% 6.01ms ± 0% +0.75% (p=0.000 n=10+10) name old speed new speed delta PermutationFunction-4 502MB/s ± 0% 502MB/s ± 0% ~ (p=0.497 n=9+10) Sha3_512_MTU-4 162MB/s ± 1% 161MB/s ± 1% ~ (p=0.101 n=10+10) Sha3_384_MTU-4 225MB/s ± 0% 224MB/s ± 1% -0.47% (p=0.000 n=8+10) Sha3_256_MTU-4 282MB/s ± 0% 282MB/s ± 1% ~ (p=0.325 n=10+10) Sha3_224_MTU-4 296MB/s ± 1% 295MB/s ± 1% ~ (p=0.280 n=10+10) Shake128_MTU-4 349MB/s ± 0% 350MB/s ± 1% +0.22% (p=0.008 n=9+9) Shake256_MTU-4 324MB/s ± 0% 324MB/s ± 0% ~ (p=0.459 n=10+8) Shake256_16x-4 276MB/s ± 0% 274MB/s ± 0% -0.48% (p=0.000 n=9+8) Shake256_1MiB-4 328MB/s ± 1% 327MB/s ± 0% ~ (p=0.105 n=10+10) Sha3_512_1MiB-4 176MB/s ± 0% 174MB/s ± 0% -0.74% (p=0.000 n=10+10) Change-Id: Ib8e571f3c9a0f84096df2f38ca96da197ad5be30 Reviewed-on: https://go-review.googlesource.com/c/crypto/+/544815 Auto-Submit: Filippo Valsorda <[email protected]> Reviewed-by: Roland Shoemaker <[email protected]> Reviewed-by: Cherry Mui <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Mauri de Souza Meneguzzo <[email protected]>
1 parent 905d78a commit 10f366e

File tree

5 files changed

+178
-276
lines changed

5 files changed

+178
-276
lines changed

sha3/sha3.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type state struct {
4040
// Extendable-Output Functions (May 2014)"
4141
dsbyte byte
4242

43-
storage storageBuf
43+
storage [maxRate]byte
4444

4545
// Specific to SHA-3 and SHAKE.
4646
outputLen int // the default output size in bytes
@@ -61,15 +61,15 @@ func (d *state) Reset() {
6161
d.a[i] = 0
6262
}
6363
d.state = spongeAbsorbing
64-
d.buf = d.storage.asBytes()[:0]
64+
d.buf = d.storage[:0]
6565
}
6666

6767
func (d *state) clone() *state {
6868
ret := *d
6969
if ret.state == spongeAbsorbing {
70-
ret.buf = ret.storage.asBytes()[:len(ret.buf)]
70+
ret.buf = ret.storage[:len(ret.buf)]
7171
} else {
72-
ret.buf = ret.storage.asBytes()[d.rate-cap(d.buf) : d.rate]
72+
ret.buf = ret.storage[d.rate-cap(d.buf) : d.rate]
7373
}
7474

7575
return &ret
@@ -83,13 +83,13 @@ func (d *state) permute() {
8383
// If we're absorbing, we need to xor the input into the state
8484
// before applying the permutation.
8585
xorIn(d, d.buf)
86-
d.buf = d.storage.asBytes()[:0]
86+
d.buf = d.storage[:0]
8787
keccakF1600(&d.a)
8888
case spongeSqueezing:
8989
// If we're squeezing, we need to apply the permutation before
9090
// copying more output.
9191
keccakF1600(&d.a)
92-
d.buf = d.storage.asBytes()[:d.rate]
92+
d.buf = d.storage[:d.rate]
9393
copyOut(d, d.buf)
9494
}
9595
}
@@ -98,15 +98,15 @@ func (d *state) permute() {
9898
// the multi-bitrate 10..1 padding rule, and permutes the state.
9999
func (d *state) padAndPermute(dsbyte byte) {
100100
if d.buf == nil {
101-
d.buf = d.storage.asBytes()[:0]
101+
d.buf = d.storage[:0]
102102
}
103103
// Pad with this instance's domain-separator bits. We know that there's
104104
// at least one byte of space in d.buf because, if it were full,
105105
// permute would have been called to empty it. dsbyte also contains the
106106
// first one bit for the padding. See the comment in the state struct.
107107
d.buf = append(d.buf, dsbyte)
108108
zerosStart := len(d.buf)
109-
d.buf = d.storage.asBytes()[:d.rate]
109+
d.buf = d.storage[:d.rate]
110110
for i := zerosStart; i < d.rate; i++ {
111111
d.buf[i] = 0
112112
}
@@ -117,7 +117,7 @@ func (d *state) padAndPermute(dsbyte byte) {
117117
// Apply the permutation
118118
d.permute()
119119
d.state = spongeSqueezing
120-
d.buf = d.storage.asBytes()[:d.rate]
120+
d.buf = d.storage[:d.rate]
121121
copyOut(d, d.buf)
122122
}
123123

@@ -128,7 +128,7 @@ func (d *state) Write(p []byte) (written int, err error) {
128128
panic("sha3: Write after Read")
129129
}
130130
if d.buf == nil {
131-
d.buf = d.storage.asBytes()[:0]
131+
d.buf = d.storage[:0]
132132
}
133133
written = len(p)
134134

0 commit comments

Comments
 (0)