[go] crypto/sha256: speed-up for very small blocks

24 views
Skip to first unread message

Ilya Tocar (Gerrit)

unread,
Dec 8, 2017, 3:33:26 PM12/8/17
to Ian Lance Taylor, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Ilya Tocar has uploaded this change for review.

View Change

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
---
M src/crypto/sha256/sha256.go
1 file changed, 35 insertions(+), 29 deletions(-)

diff --git a/src/crypto/sha256/sha256.go b/src/crypto/sha256/sha256.go
index b8ddaf4..0916399 100644
--- a/src/crypto/sha256/sha256.go
+++ b/src/crypto/sha256/sha256.go
@@ -104,27 +104,35 @@
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 {
- a := [8]byte{
- byte(x >> 56),
- byte(x >> 48),
- byte(x >> 40),
- byte(x >> 32),
- byte(x >> 24),
- byte(x >> 16),
- byte(x >> 8),
- byte(x),
- }
+ var a [8]byte
+ putUint64(a[:], x)
return append(b, a[:]...)
}

func appendUint32(b []byte, x uint32) []byte {
- a := [4]byte{
- byte(x >> 24),
- byte(x >> 16),
- byte(x >> 8),
- byte(x),
- }
+ var a [4]byte
+ putUint32(a[:], x)
return append(b, a[:]...)
}

@@ -238,26 +246,24 @@

// Length in bits.
len <<= 3
- for i := uint(0); i < 8; i++ {
- tmp[i] = byte(len >> (56 - 8*i))
- }
+ putUint64(tmp[:], len)
d.Write(tmp[0:8])

if d.nx != 0 {
panic("d.nx != 0")
}

- h := d.h[:]
- if d.is224 {
- h = d.h[:7]
- }
-
var digest [Size]byte
- for i, s := range h {
- digest[i*4] = byte(s >> 24)
- digest[i*4+1] = byte(s >> 16)
- digest[i*4+2] = byte(s >> 8)
- digest[i*4+3] = byte(s)
+
+ putUint32(digest[0:], d.h[0])
+ putUint32(digest[4:], d.h[1])
+ putUint32(digest[8:], d.h[2])
+ 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

To view, visit change 82995. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I50ca80fc28c279fbb758b7c849f67d8c66391eb6
Gerrit-Change-Number: 82995
Gerrit-PatchSet: 1
Gerrit-Owner: Ilya Tocar <ilya....@intel.com>
Gerrit-Reviewer: Ilya Tocar <ilya....@intel.com>

Gobot Gobot (Gerrit)

unread,
Dec 8, 2017, 3:33:45 PM12/8/17
to Ilya Tocar, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

TryBots beginning. Status page: https://farmer.golang.org/try?commit=1ab71b41

View Change

    To view, visit change 82995. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-MessageType: comment
    Gerrit-Change-Id: I50ca80fc28c279fbb758b7c849f67d8c66391eb6
    Gerrit-Change-Number: 82995
    Gerrit-PatchSet: 1
    Gerrit-Owner: Ilya Tocar <ilya....@intel.com>
    Gerrit-Reviewer: Ilya Tocar <ilya....@intel.com>
    Gerrit-CC: Gobot Gobot <go...@golang.org>
    Gerrit-Comment-Date: Fri, 08 Dec 2017 20:33:42 +0000
    Gerrit-HasComments: No
    Gerrit-HasLabels: No

    Ilya Tocar (Gerrit)

    unread,
    Dec 8, 2017, 3:33:49 PM12/8/17
    to goph...@pubsubhelper.golang.org, Gobot Gobot, golang-co...@googlegroups.com

    R=go1.11

    View Change

      To view, visit change 82995. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-MessageType: comment
      Gerrit-Change-Id: I50ca80fc28c279fbb758b7c849f67d8c66391eb6
      Gerrit-Change-Number: 82995
      Gerrit-PatchSet: 1
      Gerrit-Owner: Ilya Tocar <ilya....@intel.com>
      Gerrit-Reviewer: Ilya Tocar <ilya....@intel.com>
      Gerrit-CC: Gobot Gobot <go...@golang.org>
      Gerrit-Comment-Date: Fri, 08 Dec 2017 20:33:45 +0000
      Gerrit-HasComments: No
      Gerrit-HasLabels: No

      Gobot Gobot (Gerrit)

      unread,
      Dec 8, 2017, 3:43:22 PM12/8/17
      to Ilya Tocar, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

      TryBots are happy.

      Patch set 1:TryBot-Result +1

      View Change

        To view, visit change 82995. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-MessageType: comment
        Gerrit-Change-Id: I50ca80fc28c279fbb758b7c849f67d8c66391eb6
        Gerrit-Change-Number: 82995
        Gerrit-PatchSet: 1
        Gerrit-Owner: Ilya Tocar <ilya....@intel.com>
        Gerrit-Reviewer: Gobot Gobot <go...@golang.org>
        Gerrit-Reviewer: Ilya Tocar <ilya....@intel.com>
        Gerrit-Comment-Date: Fri, 08 Dec 2017 20:43:18 +0000
        Gerrit-HasComments: No
        Gerrit-HasLabels: Yes

        Ilya Tocar (Gerrit)

        unread,
        Feb 20, 2018, 12:03:18 PM2/20/18
        to goph...@pubsubhelper.golang.org, Gobot Gobot, golang-co...@googlegroups.com

        Patch set 2:Run-TryBot +1

        View Change

          To view, visit change 82995. To unsubscribe, or for help writing mail filters, visit settings.

          Gerrit-Project: go
          Gerrit-Branch: master
          Gerrit-MessageType: comment
          Gerrit-Change-Id: I50ca80fc28c279fbb758b7c849f67d8c66391eb6
          Gerrit-Change-Number: 82995
          Gerrit-PatchSet: 2
          Gerrit-Owner: Ilya Tocar <ilya....@intel.com>
          Gerrit-Reviewer: Gobot Gobot <go...@golang.org>
          Gerrit-Reviewer: Ilya Tocar <ilya....@intel.com>
          Gerrit-Comment-Date: Tue, 20 Feb 2018 17:03:16 +0000
          Gerrit-HasComments: No
          Gerrit-HasLabels: Yes

          Gobot Gobot (Gerrit)

          unread,
          Feb 20, 2018, 12:03:29 PM2/20/18
          to Ilya Tocar, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

          TryBots beginning. Status page: https://farmer.golang.org/try?commit=efb51fa2

          View Change

            To view, visit change 82995. To unsubscribe, or for help writing mail filters, visit settings.

            Gerrit-Project: go
            Gerrit-Branch: master
            Gerrit-MessageType: comment
            Gerrit-Change-Id: I50ca80fc28c279fbb758b7c849f67d8c66391eb6
            Gerrit-Change-Number: 82995
            Gerrit-PatchSet: 2
            Gerrit-Owner: Ilya Tocar <ilya....@intel.com>
            Gerrit-Reviewer: Gobot Gobot <go...@golang.org>
            Gerrit-Reviewer: Ilya Tocar <ilya....@intel.com>
            Gerrit-Comment-Date: Tue, 20 Feb 2018 17:03:27 +0000
            Gerrit-HasComments: No
            Gerrit-HasLabels: No

            Gobot Gobot (Gerrit)

            unread,
            Feb 20, 2018, 12:13:29 PM2/20/18
            to Ilya Tocar, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

            TryBots are happy.

            Patch set 2:TryBot-Result +1

            View Change

              To view, visit change 82995. To unsubscribe, or for help writing mail filters, visit settings.

              Gerrit-Project: go
              Gerrit-Branch: master
              Gerrit-MessageType: comment
              Gerrit-Change-Id: I50ca80fc28c279fbb758b7c849f67d8c66391eb6
              Gerrit-Change-Number: 82995
              Gerrit-PatchSet: 2
              Gerrit-Owner: Ilya Tocar <ilya....@intel.com>
              Gerrit-Reviewer: Gobot Gobot <go...@golang.org>
              Gerrit-Reviewer: Ilya Tocar <ilya....@intel.com>
              Gerrit-Comment-Date: Tue, 20 Feb 2018 17:13:27 +0000
              Gerrit-HasComments: No
              Gerrit-HasLabels: Yes

              Filippo Valsorda (Gerrit)

              unread,
              Feb 20, 2018, 6:39:07 PM2/20/18
              to Ilya Tocar, goph...@pubsubhelper.golang.org, Filippo Valsorda, Gobot Gobot, golang-co...@googlegroups.com

              Patch set 2:Code-Review +2

              View Change

                To view, visit change 82995. To unsubscribe, or for help writing mail filters, visit settings.

                Gerrit-Project: go
                Gerrit-Branch: master
                Gerrit-MessageType: comment
                Gerrit-Change-Id: I50ca80fc28c279fbb758b7c849f67d8c66391eb6
                Gerrit-Change-Number: 82995
                Gerrit-PatchSet: 2
                Gerrit-Owner: Ilya Tocar <ilya....@intel.com>
                Gerrit-Reviewer: Filippo Valsorda <h...@filippo.io>
                Gerrit-Reviewer: Gobot Gobot <go...@golang.org>
                Gerrit-Reviewer: Ilya Tocar <ilya....@intel.com>
                Gerrit-Comment-Date: Tue, 20 Feb 2018 23:39:04 +0000
                Gerrit-HasComments: No
                Gerrit-HasLabels: Yes

                Filippo Valsorda (Gerrit)

                unread,
                Feb 20, 2018, 6:39:13 PM2/20/18
                to Filippo Valsorda, Ilya Tocar, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Gobot Gobot, golang-co...@googlegroups.com

                Filippo Valsorda merged this change by Ilya Tocar.

                View Change

                Approvals: Filippo Valsorda: Looks good to me, approved Ilya Tocar: Run TryBots Gobot Gobot: TryBots succeeded
                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....@intel.com>
                TryBot-Result: Gobot Gobot <go...@golang.org>
                Reviewed-by: Filippo Valsorda <h...@filippo.io>
                Gerrit-MessageType: merged
                Gerrit-Change-Id: I50ca80fc28c279fbb758b7c849f67d8c66391eb6
                Gerrit-Change-Number: 82995
                Gerrit-PatchSet: 3
                Gerrit-Owner: Ilya Tocar <ilya....@intel.com>
                Reply all
                Reply to author
                Forward
                0 new messages