From: Udalov Max Date: Sat, 13 Apr 2019 15:54:03 +0000 (+0300) Subject: crypto/sha512: use math/bits.RotateLeft64 instead of ad-hoc implementation X-Git-Tag: go1.13beta1~700 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=827044e7a629128d967e79e6b92fc17f3bc4870b;p=gostls13.git crypto/sha512: use math/bits.RotateLeft64 instead of ad-hoc implementation This makes code more readable and idiomatic and slightly increase performance. Updates #31456 Benchstat: name old time/op new time/op delta Hash8Bytes-8 281ns ± 4% 280ns ± 3% ~ (p=0.640 n=10+10) Hash1K-8 2.01µs ± 6% 2.02µs ± 3% ~ (p=0.481 n=10+10) Hash8K-8 14.2µs ± 6% 13.5µs ± 1% -4.90% (p=0.001 n=10+10) name old speed new speed delta Hash8Bytes-8 28.5MB/s ± 4% 28.5MB/s ± 3% ~ (p=0.516 n=10+10) Hash1K-8 510MB/s ± 6% 507MB/s ± 4% ~ (p=0.481 n=10+10) Hash8K-8 576MB/s ± 6% 605MB/s ± 1% +5.02% (p=0.001 n=10+10) Tested on macbook pro 2018 Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz Change-Id: I1f5b78096dd49d14ffcb9129142c4a4e05b81ff9 Reviewed-on: https://go-review.googlesource.com/c/go/+/171736 Reviewed-by: Filippo Valsorda --- diff --git a/src/crypto/sha512/sha512block.go b/src/crypto/sha512/sha512block.go index 42e8d19fe8..81569c5f84 100644 --- a/src/crypto/sha512/sha512block.go +++ b/src/crypto/sha512/sha512block.go @@ -8,6 +8,8 @@ package sha512 +import "math/bits" + var _K = []uint64{ 0x428a2f98d728ae22, 0x7137449123ef65cd, @@ -102,9 +104,9 @@ func blockGeneric(dig *digest, p []byte) { } for i := 16; i < 80; i++ { v1 := w[i-2] - t1 := (v1>>19 | v1<<(64-19)) ^ (v1>>61 | v1<<(64-61)) ^ (v1 >> 6) + t1 := bits.RotateLeft64(v1, -19) ^ bits.RotateLeft64(v1, -61) ^ (v1 >> 6) v2 := w[i-15] - t2 := (v2>>1 | v2<<(64-1)) ^ (v2>>8 | v2<<(64-8)) ^ (v2 >> 7) + t2 := bits.RotateLeft64(v2, -1) ^ bits.RotateLeft64(v2, -8) ^ (v2 >> 7) w[i] = t1 + w[i-7] + t2 + w[i-16] } @@ -112,9 +114,9 @@ func blockGeneric(dig *digest, p []byte) { a, b, c, d, e, f, g, h := h0, h1, h2, h3, h4, h5, h6, h7 for i := 0; i < 80; i++ { - t1 := h + ((e>>14 | e<<(64-14)) ^ (e>>18 | e<<(64-18)) ^ (e>>41 | e<<(64-41))) + ((e & f) ^ (^e & g)) + _K[i] + w[i] + t1 := h + (bits.RotateLeft64(e, -14) ^ bits.RotateLeft64(e, -18) ^ bits.RotateLeft64(e, -41)) + ((e & f) ^ (^e & g)) + _K[i] + w[i] - t2 := ((a>>28 | a<<(64-28)) ^ (a>>34 | a<<(64-34)) ^ (a>>39 | a<<(64-39))) + ((a & b) ^ (a & c) ^ (b & c)) + t2 := (bits.RotateLeft64(a, -28) ^ bits.RotateLeft64(a, -34) ^ bits.RotateLeft64(a, -39)) + ((a & b) ^ (a & c) ^ (b & c)) h = g g = f