From dd2e1b82b7bd2ba7c4a5cddf2b5eaa556a0724da Mon Sep 17 00:00:00 2001 From: Will Winder Date: Tue, 7 Oct 2025 12:51:52 -0400 Subject: [PATCH 1/8] Do not use go-ethereum for hasher. --- protocol/hashing.go | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/protocol/hashing.go b/protocol/hashing.go index 76ada32a..28cf73e2 100644 --- a/protocol/hashing.go +++ b/protocol/hashing.go @@ -1,11 +1,30 @@ 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() + }, +} // 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 { + panic("cannot get hasher") + } + + h.Reset() + h.Write(data) // nolint:revive // keccak256 never returns an error + var out [32]byte + copy(out[:], h.Sum(nil)) + h.Reset() + hasherPool.Put(h) + return out } From a5f9e2b9c791a8e1f5c0d7a29265653b55ea9615 Mon Sep 17 00:00:00 2001 From: Will Winder Date: Wed, 8 Oct 2025 08:19:14 -0400 Subject: [PATCH 2/8] Fix lint --- protocol/hashing.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocol/hashing.go b/protocol/hashing.go index 28cf73e2..d428442c 100644 --- a/protocol/hashing.go +++ b/protocol/hashing.go @@ -21,7 +21,7 @@ func Keccak256(data []byte) [32]byte { } h.Reset() - h.Write(data) // nolint:revive // keccak256 never returns an error + h.Write(data) //nolint:revive // keccak256 never returns an error var out [32]byte copy(out[:], h.Sum(nil)) h.Reset() From 1cc84fac6142d605f5a77c61746199f11d469f74 Mon Sep 17 00:00:00 2001 From: Will Winder Date: Wed, 8 Oct 2025 08:20:11 -0400 Subject: [PATCH 3/8] Avoid panic --- protocol/hashing.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/protocol/hashing.go b/protocol/hashing.go index d428442c..d0ed8525 100644 --- a/protocol/hashing.go +++ b/protocol/hashing.go @@ -17,7 +17,8 @@ var hasherPool = sync.Pool{ func Keccak256(data []byte) [32]byte { h, ok := hasherPool.Get().(hash.Hash) if !ok { - panic("cannot get hasher") + // This should never happen, but just in case. + h = sha3.NewLegacyKeccak256() } h.Reset() From 1004434ad507acb24e4c1342cc343010354ffc52 Mon Sep 17 00:00:00 2001 From: Will Winder Date: Thu, 9 Oct 2025 16:24:53 -0400 Subject: [PATCH 4/8] No need to call reset twice. --- protocol/hashing.go | 1 - 1 file changed, 1 deletion(-) diff --git a/protocol/hashing.go b/protocol/hashing.go index d0ed8525..c433de50 100644 --- a/protocol/hashing.go +++ b/protocol/hashing.go @@ -25,7 +25,6 @@ func Keccak256(data []byte) [32]byte { h.Write(data) //nolint:revive // keccak256 never returns an error var out [32]byte copy(out[:], h.Sum(nil)) - h.Reset() hasherPool.Put(h) return out } From 085ba4dbb296e3678a90f59f383c2936140fc995 Mon Sep 17 00:00:00 2001 From: Will Winder Date: Wed, 22 Oct 2025 12:05:57 -0400 Subject: [PATCH 5/8] Add benchmark for hashing with and without object pooling. --- protocol/hashing_test.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 protocol/hashing_test.go diff --git a/protocol/hashing_test.go b/protocol/hashing_test.go new file mode 100644 index 00000000..657aeeff --- /dev/null +++ b/protocol/hashing_test.go @@ -0,0 +1,25 @@ +package protocol + +import ( + "testing" + + "golang.org/x/crypto/sha3" +) + +var data = []byte("The quick brown fox jumps over the lazy dog") + +func BenchmarkHashing(b *testing.B) { + for i := 0; i < b.N; i++ { + Keccak256(data) + } +} + +func BenchmarkHashinbBaseline(b *testing.B) { + for i := 0; i < b.N; i++ { + h := sha3.NewLegacyKeccak256() + h.Write(data) //nolint:revive // keccak256 never returns an error + var out [32]byte + copy(out[:], h.Sum(nil)) + hasherPool.Put(h) + } +} From cdd33ecfdbb03839361dd972593a9157873e9c76 Mon Sep 17 00:00:00 2001 From: Will Winder Date: Wed, 22 Oct 2025 12:06:35 -0400 Subject: [PATCH 6/8] Fix lint --- protocol/hashing_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocol/hashing_test.go b/protocol/hashing_test.go index 657aeeff..cbebfcb2 100644 --- a/protocol/hashing_test.go +++ b/protocol/hashing_test.go @@ -17,7 +17,7 @@ func BenchmarkHashing(b *testing.B) { func BenchmarkHashinbBaseline(b *testing.B) { for i := 0; i < b.N; i++ { h := sha3.NewLegacyKeccak256() - h.Write(data) //nolint:revive // keccak256 never returns an error + h.Write(data) var out [32]byte copy(out[:], h.Sum(nil)) hasherPool.Put(h) From 977f9c4f477228069d5e0fdcad076b6ab5ffa125 Mon Sep 17 00:00:00 2001 From: Will Winder Date: Wed, 22 Oct 2025 12:11:49 -0400 Subject: [PATCH 7/8] tidy --- go.mod | 2 +- protocol/hashing_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 26f5cc83..f57d1bec 100644 --- a/go.mod +++ b/go.mod @@ -62,6 +62,7 @@ require ( github.com/lib/pq v1.10.9 github.com/redis/go-redis/v9 v9.0.4 github.com/testcontainers/testcontainers-go/modules/postgres v0.39.0 + golang.org/x/crypto v0.41.0 ) require ( @@ -308,7 +309,6 @@ require ( go.uber.org/multierr v1.11.0 // indirect go.uber.org/ratelimit v0.3.1 // indirect golang.org/x/arch v0.8.0 // indirect - golang.org/x/crypto v0.41.0 // indirect golang.org/x/net v0.43.0 // indirect golang.org/x/sys v0.36.0 // indirect golang.org/x/term v0.34.0 // indirect diff --git a/protocol/hashing_test.go b/protocol/hashing_test.go index cbebfcb2..c0067ea3 100644 --- a/protocol/hashing_test.go +++ b/protocol/hashing_test.go @@ -6,7 +6,7 @@ import ( "golang.org/x/crypto/sha3" ) -var data = []byte("The quick brown fox jumps over the lazy dog") +var data = []byte("The quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dog") func BenchmarkHashing(b *testing.B) { for i := 0; i < b.N; i++ { From 134e63f112dea09f7ac0dc4fec81418aa2c40b05 Mon Sep 17 00:00:00 2001 From: Will Winder Date: Wed, 22 Oct 2025 12:21:17 -0400 Subject: [PATCH 8/8] ??? --- protocol/hashing.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocol/hashing.go b/protocol/hashing.go index c433de50..386477cd 100644 --- a/protocol/hashing.go +++ b/protocol/hashing.go @@ -22,7 +22,7 @@ func Keccak256(data []byte) [32]byte { } h.Reset() - h.Write(data) //nolint:revive // keccak256 never returns an error + h.Write(data) //nolint // keccak256 never returns an error var out [32]byte copy(out[:], h.Sum(nil)) hasherPool.Put(h)