-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sha3: make APIs usable with zero allocations
The "buf points into storage" pattern is nice, but causes the whole state struct to escape, since escape analysis can't track the pointer once it's assigned to buf. Change-Id: I31c0e83f946d66bedb5a180e96ab5d5e936eb322 Reviewed-on: https://go-review.googlesource.com/c/crypto/+/544817 Reviewed-by: Cherry Mui <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Roland Shoemaker <[email protected]> Reviewed-by: Mauri de Souza Meneguzzo <[email protected]> Auto-Submit: Filippo Valsorda <[email protected]>
- Loading branch information
1 parent
59b5a86
commit 477a5b4
Showing
2 changed files
with
77 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// 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 ( | ||
"testing" | ||
|
||
"golang.org/x/crypto/sha3" | ||
) | ||
|
||
var sink byte | ||
|
||
func TestAllocations(t *testing.T) { | ||
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 > 0 { | ||
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 > 0 { | ||
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 > 0 { | ||
t.Errorf("expected zero allocations, got %0.1f", allocs) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters