Skip to content
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion protocol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.24.6
require (
github.com/ethereum/go-ethereum v1.16.2
github.com/stretchr/testify v1.11.1
golang.org/x/crypto v0.40.0
)

require (
Expand All @@ -13,7 +14,6 @@ require (
github.com/holiman/uint256 v1.3.2 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/stretchr/objx v0.5.2 // indirect
golang.org/x/crypto v0.40.0 // indirect
golang.org/x/sys v0.34.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
30 changes: 25 additions & 5 deletions protocol/hashing.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
package protocol

import "github.com/ethereum/go-ethereum/crypto"
import (
"hash"
"sync"

"golang.org/x/crypto/sha3"
)

var hasherPool = sync.Pool{
New: func() any {
return sha3.NewLegacyKeccak256()
},
}
Comment on lines +10 to +14
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed?


// Keccak256 computes the Keccak256 hash of the input.
func Keccak256(data []byte) [32]byte {
hash := crypto.Keccak256(data)
var result [32]byte
copy(result[:], hash)
return result
h, ok := hasherPool.Get().(hash.Hash)
if !ok {
// This should never happen, but just in case.
h = sha3.NewLegacyKeccak256()
}

h.Reset()
h.Write(data) //nolint:revive // keccak256 never returns an error
var out [32]byte
copy(out[:], h.Sum(nil))
h.Reset()
Copy link
Contributor

@0xAustinWang 0xAustinWang Oct 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this h.reset necessary? We already reset right before usage on line 24

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. The pool is private, so there's no benefit to the extra caution.

hasherPool.Put(h)
return out
}
Loading