]> Cypherpunks repositories - gostls13.git/commitdiff
crypto/sha1: use math/bits.RotateLeft32 instead of ad-hoc implementation.
authorUdalov Max <re.udalov@gmail.com>
Sat, 13 Apr 2019 13:18:15 +0000 (16:18 +0300)
committerIlya Tokar <tocarip@gmail.com>
Sat, 13 Apr 2019 13:56:23 +0000 (13:56 +0000)
This makes code more idiomatic and shows small performance gains of generic benchmarks.

Updates: #31456

name            old time/op    new time/op    delta
Hash8Bytes-8       275ns ± 4%     270ns ± 0%    ~     (p=0.213 n=9+8)
Hash320Bytes-8    1.46µs ± 5%    1.39µs ± 1%  -4.54%  (p=0.000 n=10+10)
Hash1K-8          3.99µs ± 5%    3.86µs ± 1%  -3.38%  (p=0.023 n=10+10)
Hash8K-8          28.9µs ± 0%    28.9µs ± 1%    ~     (p=0.315 n=10+10)

name            old speed      new speed      delta
Hash8Bytes-8    28.8MB/s ± 9%  29.6MB/s ± 0%    ~     (p=0.151 n=10+8)
Hash320Bytes-8   220MB/s ± 5%   230MB/s ± 1%  +4.65%  (p=0.000 n=10+10)
Hash1K-8         257MB/s ± 5%   265MB/s ± 1%  +3.38%  (p=0.023 n=10+10)
Hash8K-8         283MB/s ± 0%   284MB/s ± 1%    ~     (p=0.315 n=10+10)

Change-Id: Iee63aa042614e3bbeda9aaf5236180d4153f03c4
Reviewed-on: https://go-review.googlesource.com/c/go/+/171729
Reviewed-by: Ilya Tokar <tocarip@gmail.com>
Run-TryBot: Ilya Tokar <tocarip@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/crypto/sha1/sha1block.go

index 1d37544940f120485a636ce72204e0c1d3ae1261..321d34351c1f5e92b42e52114af76c30b84f3f45 100644 (file)
@@ -4,6 +4,10 @@
 
 package sha1
 
+import (
+       "math/bits"
+)
+
 const (
        _K0 = 0x5A827999
        _K1 = 0x6ED9EBA1
@@ -33,48 +37,37 @@ func blockGeneric(dig *digest, p []byte) {
                i := 0
                for ; i < 16; i++ {
                        f := b&c | (^b)&d
-                       a5 := a<<5 | a>>(32-5)
-                       b30 := b<<30 | b>>(32-30)
-                       t := a5 + f + e + w[i&0xf] + _K0
-                       a, b, c, d, e = t, a, b30, c, d
+                       t := bits.RotateLeft32(a, 5) + f + e + w[i&0xf] + _K0
+                       a, b, c, d, e = t, a, bits.RotateLeft32(b, 30), c, d
                }
                for ; i < 20; i++ {
                        tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[(i)&0xf]
                        w[i&0xf] = tmp<<1 | tmp>>(32-1)
 
                        f := b&c | (^b)&d
-                       a5 := a<<5 | a>>(32-5)
-                       b30 := b<<30 | b>>(32-30)
-                       t := a5 + f + e + w[i&0xf] + _K0
-                       a, b, c, d, e = t, a, b30, c, d
+                       t := bits.RotateLeft32(a, 5) + f + e + w[i&0xf] + _K0
+                       a, b, c, d, e = t, a, bits.RotateLeft32(b, 30), c, d
                }
                for ; i < 40; i++ {
                        tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[(i)&0xf]
                        w[i&0xf] = tmp<<1 | tmp>>(32-1)
                        f := b ^ c ^ d
-                       a5 := a<<5 | a>>(32-5)
-                       b30 := b<<30 | b>>(32-30)
-                       t := a5 + f + e + w[i&0xf] + _K1
-                       a, b, c, d, e = t, a, b30, c, d
+                       t := bits.RotateLeft32(a, 5) + f + e + w[i&0xf] + _K1
+                       a, b, c, d, e = t, a, bits.RotateLeft32(b, 30), c, d
                }
                for ; i < 60; i++ {
                        tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[(i)&0xf]
                        w[i&0xf] = tmp<<1 | tmp>>(32-1)
                        f := ((b | c) & d) | (b & c)
-
-                       a5 := a<<5 | a>>(32-5)
-                       b30 := b<<30 | b>>(32-30)
-                       t := a5 + f + e + w[i&0xf] + _K2
-                       a, b, c, d, e = t, a, b30, c, d
+                       t := bits.RotateLeft32(a, 5) + f + e + w[i&0xf] + _K2
+                       a, b, c, d, e = t, a, bits.RotateLeft32(b, 30), c, d
                }
                for ; i < 80; i++ {
                        tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[(i)&0xf]
                        w[i&0xf] = tmp<<1 | tmp>>(32-1)
                        f := b ^ c ^ d
-                       a5 := a<<5 | a>>(32-5)
-                       b30 := b<<30 | b>>(32-30)
-                       t := a5 + f + e + w[i&0xf] + _K3
-                       a, b, c, d, e = t, a, b30, c, d
+                       t := bits.RotateLeft32(a, 5) + f + e + w[i&0xf] + _K3
+                       a, b, c, d, e = t, a, bits.RotateLeft32(b, 30), c, d
                }
 
                h0 += a