Commit e476e110 authored by Ilya Tocar's avatar Ilya Tocar Committed by Filippo Valsorda

crypto/sha256: speed-up for very small blocks

Similar to https://golang.org/cl/54391, but for sha256
name          old time/op    new time/op    delta
Hash8Bytes-8     209ns ± 1%     191ns ± 1%  -8.65%  (p=0.000 n=10+9)
Hash1K-8        2.49µs ± 1%    2.47µs ± 2%  -0.74%  (p=0.045 n=9+10)
Hash8K-8        18.4µs ± 1%    18.2µs ± 0%  -0.98%  (p=0.009 n=10+10)

name          old speed      new speed      delta
Hash8Bytes-8  38.1MB/s ± 1%  41.8MB/s ± 1%  +9.47%  (p=0.000 n=10+9)
Hash1K-8       412MB/s ± 1%   415MB/s ± 2%    ~     (p=0.051 n=9+10)
Hash8K-8       445MB/s ± 1%   450MB/s ± 0%  +0.98%  (p=0.009 n=10+10)

Change-Id: I50ca80fc28c279fbb758b7c849f67d8c66391eb6
Reviewed-on: https://go-review.googlesource.com/82995
Run-TryBot: Ilya Tocar <ilya.tocar@intel.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarFilippo Valsorda <hi@filippo.io>
parent 9c854d65
...@@ -104,27 +104,35 @@ func (d *digest) UnmarshalBinary(b []byte) error { ...@@ -104,27 +104,35 @@ func (d *digest) UnmarshalBinary(b []byte) error {
return nil return nil
} }
func putUint32(x []byte, s uint32) {
_ = x[3]
x[0] = byte(s >> 24)
x[1] = byte(s >> 16)
x[2] = byte(s >> 8)
x[3] = byte(s)
}
func putUint64(x []byte, s uint64) {
_ = x[7]
x[0] = byte(s >> 56)
x[1] = byte(s >> 48)
x[2] = byte(s >> 40)
x[3] = byte(s >> 32)
x[4] = byte(s >> 24)
x[5] = byte(s >> 16)
x[6] = byte(s >> 8)
x[7] = byte(s)
}
func appendUint64(b []byte, x uint64) []byte { func appendUint64(b []byte, x uint64) []byte {
a := [8]byte{ var a [8]byte
byte(x >> 56), putUint64(a[:], x)
byte(x >> 48),
byte(x >> 40),
byte(x >> 32),
byte(x >> 24),
byte(x >> 16),
byte(x >> 8),
byte(x),
}
return append(b, a[:]...) return append(b, a[:]...)
} }
func appendUint32(b []byte, x uint32) []byte { func appendUint32(b []byte, x uint32) []byte {
a := [4]byte{ var a [4]byte
byte(x >> 24), putUint32(a[:], x)
byte(x >> 16),
byte(x >> 8),
byte(x),
}
return append(b, a[:]...) return append(b, a[:]...)
} }
...@@ -238,26 +246,24 @@ func (d *digest) checkSum() [Size]byte { ...@@ -238,26 +246,24 @@ func (d *digest) checkSum() [Size]byte {
// Length in bits. // Length in bits.
len <<= 3 len <<= 3
for i := uint(0); i < 8; i++ { putUint64(tmp[:], len)
tmp[i] = byte(len >> (56 - 8*i))
}
d.Write(tmp[0:8]) d.Write(tmp[0:8])
if d.nx != 0 { if d.nx != 0 {
panic("d.nx != 0") panic("d.nx != 0")
} }
h := d.h[:]
if d.is224 {
h = d.h[:7]
}
var digest [Size]byte var digest [Size]byte
for i, s := range h {
digest[i*4] = byte(s >> 24) putUint32(digest[0:], d.h[0])
digest[i*4+1] = byte(s >> 16) putUint32(digest[4:], d.h[1])
digest[i*4+2] = byte(s >> 8) putUint32(digest[8:], d.h[2])
digest[i*4+3] = byte(s) putUint32(digest[12:], d.h[3])
putUint32(digest[16:], d.h[4])
putUint32(digest[20:], d.h[5])
putUint32(digest[24:], d.h[6])
if !d.is224 {
putUint32(digest[28:], d.h[7])
} }
return digest return digest
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment