Fixes golang/go#64897 Change-Id: I0c8c52d73a7d2df0f44fee36d407a87213f59bff Reviewed-on: https://go-review.googlesource.com/c/crypto/+/554435 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Filippo Valsorda <filippo@golang.org> Reviewed-by: Roland Shoemaker <roland@golang.org> Auto-Submit: Filippo Valsorda <filippo@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
62 lines
1.3 KiB
Go
62 lines
1.3 KiB
Go
// Copyright 2023 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
//go:build !noopt
|
|
|
|
package sha3_test
|
|
|
|
import (
|
|
"runtime"
|
|
"testing"
|
|
|
|
"golang.org/x/crypto/sha3"
|
|
)
|
|
|
|
var sink byte
|
|
|
|
func TestAllocations(t *testing.T) {
|
|
want := 0.0
|
|
|
|
if runtime.GOARCH == "s390x" {
|
|
// On s390x the returned hash.Hash is conditional so it escapes.
|
|
want = 3.0
|
|
}
|
|
|
|
t.Run("New", func(t *testing.T) {
|
|
if allocs := testing.AllocsPerRun(10, func() {
|
|
h := sha3.New256()
|
|
b := []byte("ABC")
|
|
h.Write(b)
|
|
out := make([]byte, 0, 32)
|
|
out = h.Sum(out)
|
|
sink ^= out[0]
|
|
}); allocs > want {
|
|
t.Errorf("expected zero allocations, got %0.1f", allocs)
|
|
}
|
|
})
|
|
t.Run("NewShake", func(t *testing.T) {
|
|
if allocs := testing.AllocsPerRun(10, func() {
|
|
h := sha3.NewShake128()
|
|
b := []byte("ABC")
|
|
h.Write(b)
|
|
out := make([]byte, 0, 32)
|
|
out = h.Sum(out)
|
|
sink ^= out[0]
|
|
h.Read(out)
|
|
sink ^= out[0]
|
|
}); allocs > want {
|
|
t.Errorf("expected zero allocations, got %0.1f", allocs)
|
|
}
|
|
})
|
|
t.Run("Sum", func(t *testing.T) {
|
|
if allocs := testing.AllocsPerRun(10, func() {
|
|
b := []byte("ABC")
|
|
out := sha3.Sum256(b)
|
|
sink ^= out[0]
|
|
}); allocs > want {
|
|
t.Errorf("expected zero allocations, got %0.1f", allocs)
|
|
}
|
|
})
|
|
}
|