From 995c0f310c087c9cbc49112ecc48459a96310451 Mon Sep 17 00:00:00 2001 From: Egon Elbre Date: Fri, 30 Sep 2022 15:29:52 +0300 Subject: [PATCH] crypto/{sha512,sha256,sha1}: reduce Write calls in Sum MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit crypto/sha512: name old time/op new time/op delta Hash8Bytes/New-32 297ns ± 1% 288ns ± 1% -3.09% (p=0.000 n=10+10) Hash8Bytes/Sum384-32 288ns ± 1% 282ns ± 1% -2.16% (p=0.000 n=10+9) Hash8Bytes/Sum512-32 287ns ± 1% 278ns ± 1% -3.18% (p=0.000 n=9+10) Hash1K/New-32 2.12µs ± 0% 2.11µs ± 1% ~ (p=0.189 n=9+10) Hash1K/Sum384-32 2.13µs ± 1% 2.11µs ± 1% -1.07% (p=0.006 n=10+10) Hash1K/Sum512-32 2.12µs ± 1% 2.11µs ± 1% ~ (p=0.209 n=10+10) Hash8K/New-32 14.9µs ± 1% 14.9µs ± 1% ~ (p=0.469 n=10+10) Hash8K/Sum384-32 15.0µs ± 1% 14.9µs ± 1% ~ (p=0.725 n=10+10) Hash8K/Sum512-32 14.9µs ± 1% 15.0µs ± 1% ~ (p=0.684 n=10+10) crypto/sha256: name old time/op new time/op delta Hash8Bytes/New-32 190ns ± 1% 188ns ± 2% -1.23% (p=0.002 n=10+10) Hash8Bytes/Sum224-32 198ns ± 1% 190ns ± 1% -4.07% (p=0.000 n=10+10) Hash8Bytes/Sum256-32 196ns ± 1% 183ns ± 1% -6.33% (p=0.000 n=10+10) Hash1K/New-32 2.35µs ± 1% 2.35µs ± 2% ~ (p=0.897 n=10+10) Hash1K/Sum224-32 2.38µs ± 1% 2.38µs ± 2% ~ (p=0.363 n=10+10) Hash1K/Sum256-32 2.38µs ± 1% 2.37µs ± 1% ~ (p=0.203 n=10+9) Hash8K/New-32 17.3µs ± 1% 17.3µs ± 1% ~ (p=0.971 n=10+10) Hash8K/Sum224-32 17.6µs ± 1% 17.6µs ± 1% ~ (p=0.726 n=10+10) Hash8K/Sum256-32 17.6µs ± 2% 17.6µs ± 1% ~ (p=0.541 n=10+10) crypto/sha1: name old time/op new time/op delta Hash8Bytes/New-32 140ns ± 2% 134ns ± 1% -4.26% (p=0.000 n=10+10) Hash8Bytes/Sum-32 136ns ± 1% 132ns ± 1% -2.41% (p=0.000 n=10+10) Hash320Bytes/New-32 552ns ± 1% 550ns ± 1% ~ (p=0.211 n=10+9) Hash320Bytes/Sum-32 559ns ± 1% 560ns ± 2% ~ (p=0.477 n=9+9) Hash1K/New-32 1.15µs ± 1% 1.15µs ± 1% ~ (p=0.588 n=9+10) Hash1K/Sum-32 1.16µs ± 2% 1.15µs ± 1% ~ (p=0.078 n=10+10) Hash8K/New-32 7.53µs ± 1% 7.58µs ± 3% ~ (p=0.382 n=10+10) Hash8K/Sum-32 7.54µs ± 1% 7.55µs ± 1% ~ (p=0.404 n=10+10) Change-Id: I3fc06c9009f2eabc739102c14648ef93ea6783b4 Reviewed-on: https://go-review.googlesource.com/c/go/+/436917 Reviewed-by: Cherry Mui Run-TryBot: Bryan Mills TryBot-Result: Gopher Robot Reviewed-by: Bryan Mills Reviewed-by: Filippo Valsorda --- src/crypto/sha1/sha1.go | 12 +++++++----- src/crypto/sha256/sha256.go | 12 +++++++----- src/crypto/sha512/sha512.go | 16 ++++++++++------ 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/crypto/sha1/sha1.go b/src/crypto/sha1/sha1.go index bef447c7f2..43ab72a485 100644 --- a/src/crypto/sha1/sha1.go +++ b/src/crypto/sha1/sha1.go @@ -154,18 +154,20 @@ func (d *digest) Sum(in []byte) []byte { func (d *digest) checkSum() [Size]byte { len := d.len // Padding. Add a 1 bit and 0 bits until 56 bytes mod 64. - var tmp [64]byte + var tmp [64 + 8]byte // padding + length buffer tmp[0] = 0x80 + var t uint64 if len%64 < 56 { - d.Write(tmp[0 : 56-len%64]) + t = 56 - len%64 } else { - d.Write(tmp[0 : 64+56-len%64]) + t = 64 + 56 - len%64 } // Length in bits. len <<= 3 - binary.BigEndian.PutUint64(tmp[:], len) - d.Write(tmp[0:8]) + padlen := tmp[:t+8] + binary.BigEndian.PutUint64(padlen[t:], len) + d.Write(padlen) if d.nx != 0 { panic("d.nx != 0") diff --git a/src/crypto/sha256/sha256.go b/src/crypto/sha256/sha256.go index 15c907ecf7..2deafbc9fc 100644 --- a/src/crypto/sha256/sha256.go +++ b/src/crypto/sha256/sha256.go @@ -214,18 +214,20 @@ func (d *digest) Sum(in []byte) []byte { func (d *digest) checkSum() [Size]byte { len := d.len // Padding. Add a 1 bit and 0 bits until 56 bytes mod 64. - var tmp [64]byte + var tmp [64 + 8]byte // padding + length buffer tmp[0] = 0x80 + var t uint64 if len%64 < 56 { - d.Write(tmp[0 : 56-len%64]) + t = 56 - len%64 } else { - d.Write(tmp[0 : 64+56-len%64]) + t = 64 + 56 - len%64 } // Length in bits. len <<= 3 - binary.BigEndian.PutUint64(tmp[:], len) - d.Write(tmp[0:8]) + padlen := tmp[:t+8] + binary.BigEndian.PutUint64(padlen[t+0:], len) + d.Write(padlen) if d.nx != 0 { panic("d.nx != 0") diff --git a/src/crypto/sha512/sha512.go b/src/crypto/sha512/sha512.go index 3819478ed7..9ae1b3aae2 100644 --- a/src/crypto/sha512/sha512.go +++ b/src/crypto/sha512/sha512.go @@ -302,19 +302,23 @@ func (d *digest) Sum(in []byte) []byte { func (d *digest) checkSum() [Size]byte { // Padding. Add a 1 bit and 0 bits until 112 bytes mod 128. len := d.len - var tmp [128]byte + var tmp [128 + 16]byte // padding + length buffer tmp[0] = 0x80 + var t uint64 if len%128 < 112 { - d.Write(tmp[0 : 112-len%128]) + t = 112 - len%128 } else { - d.Write(tmp[0 : 128+112-len%128]) + t = 128 + 112 - len%128 } // Length in bits. len <<= 3 - binary.BigEndian.PutUint64(tmp[0:], 0) // upper 64 bits are always zero, because len variable has type uint64 - binary.BigEndian.PutUint64(tmp[8:], len) - d.Write(tmp[0:16]) + padlen := tmp[:t+16] + // Upper 64 bits are always zero, because len variable has type uint64, + // and tmp is already zeroed at that index, so we can skip updating it. + // binary.BigEndian.PutUint64(padlen[t+0:], 0) + binary.BigEndian.PutUint64(padlen[t+8:], len) + d.Write(padlen) if d.nx != 0 { panic("d.nx != 0") -- 2.50.0