From: Ivan Osadchiy Date: Sat, 13 Apr 2019 13:55:24 +0000 (+0300) Subject: crypto/sha256: Use bits.RotateLeft32 instead of ad-hoc implementation X-Git-Tag: go1.13beta1~686 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=36b0593f79c17c45985b17239e5f65d13da49949;p=gostls13.git crypto/sha256: Use bits.RotateLeft32 instead of ad-hoc implementation Improves readability of the generic implementation. Updates #31456. Benchmarks (i7-4980HQ CPU) name old time/op new time/op delta Hash8Bytes-8 339ns ± 3% 337ns ± 2% ~ (p=0.595 n=5+5) Hash1K-8 5.12µs ± 6% 4.97µs ± 6% ~ (p=0.310 n=5+5) Hash8K-8 37.6µs ± 5% 38.1µs ± 6% ~ (p=0.841 n=5+5) name old speed new speed delta Hash8Bytes-8 23.6MB/s ± 3% 23.8MB/s ± 3% ~ (p=0.690 n=5+5) Hash1K-8 200MB/s ± 6% 206MB/s ± 5% ~ (p=0.310 n=5+5) Hash8K-8 218MB/s ± 5% 215MB/s ± 6% ~ (p=0.841 n=5+5) Change-Id: Ic488841699138efde76e900bce1dd38fdbc88ec6 Reviewed-on: https://go-review.googlesource.com/c/go/+/171731 Reviewed-by: Ilya Tokar Run-TryBot: Ilya Tokar TryBot-Result: Gobot Gobot --- diff --git a/src/crypto/sha256/sha256block.go b/src/crypto/sha256/sha256block.go index d43bbf0245..bd2f9da93c 100644 --- a/src/crypto/sha256/sha256block.go +++ b/src/crypto/sha256/sha256block.go @@ -8,6 +8,8 @@ package sha256 +import "math/bits" + var _K = []uint32{ 0x428a2f98, 0x71374491, @@ -87,18 +89,18 @@ func blockGeneric(dig *digest, p []byte) { } for i := 16; i < 64; i++ { v1 := w[i-2] - t1 := (v1>>17 | v1<<(32-17)) ^ (v1>>19 | v1<<(32-19)) ^ (v1 >> 10) + t1 := (bits.RotateLeft32(v1, -17)) ^ (bits.RotateLeft32(v1, -19)) ^ (v1 >> 10) v2 := w[i-15] - t2 := (v2>>7 | v2<<(32-7)) ^ (v2>>18 | v2<<(32-18)) ^ (v2 >> 3) + t2 := (bits.RotateLeft32(v2, -7)) ^ (bits.RotateLeft32(v2, -18)) ^ (v2 >> 3) w[i] = t1 + w[i-7] + t2 + w[i-16] } a, b, c, d, e, f, g, h := h0, h1, h2, h3, h4, h5, h6, h7 for i := 0; i < 64; i++ { - t1 := h + ((e>>6 | e<<(32-6)) ^ (e>>11 | e<<(32-11)) ^ (e>>25 | e<<(32-25))) + ((e & f) ^ (^e & g)) + _K[i] + w[i] + t1 := h + ((bits.RotateLeft32(e, -6)) ^ (bits.RotateLeft32(e, -11)) ^ (bits.RotateLeft32(e, -25))) + ((e & f) ^ (^e & g)) + _K[i] + w[i] - t2 := ((a>>2 | a<<(32-2)) ^ (a>>13 | a<<(32-13)) ^ (a>>22 | a<<(32-22))) + ((a & b) ^ (a & c) ^ (b & c)) + t2 := ((bits.RotateLeft32(a, -2)) ^ (bits.RotateLeft32(a, -13)) ^ (bits.RotateLeft32(a, -22))) + ((a & b) ^ (a & c) ^ (b & c)) h = g g = f