Skip to content
4 changes: 2 additions & 2 deletions accounts/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/event"
"golang.org/x/crypto/sha3"
)

// Account represents an Ethereum account located at a specific location defined
Expand Down Expand Up @@ -196,7 +196,7 @@ func TextHash(data []byte) []byte {
// This gives context to the signed message and prevents signing of transactions.
func TextAndHash(data []byte) ([]byte, string) {
msg := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data)
hasher := sha3.NewLegacyKeccak256()
hasher := crypto.NewLegacyKeccak256()
hasher.Write([]byte(msg))
return hasher.Sum(nil), msg
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/evm/internal/t8ntool/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ import (
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/triedb"
"github.com/holiman/uint256"
"golang.org/x/crypto/sha3"
)

type Prestate struct {
Expand Down Expand Up @@ -403,7 +403,7 @@ func MakePreState(db ethdb.Database, accounts types.GenesisAlloc) *state.StateDB
}

func rlpHash(x interface{}) (h common.Hash) {
hw := sha3.NewLegacyKeccak256()
hw := crypto.NewLegacyKeccak256()
rlp.Encode(hw, x)
hw.Sum(h[:0])
return h
Expand Down
3 changes: 1 addition & 2 deletions consensus/clique/clique.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import (
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie"
"golang.org/x/crypto/sha3"
)

const (
Expand Down Expand Up @@ -643,7 +642,7 @@ func (c *Clique) Close() error {

// SealHash returns the hash of a block prior to it being sealed.
func SealHash(header *types.Header) (hash common.Hash) {
hasher := sha3.NewLegacyKeccak256()
hasher := crypto.NewLegacyKeccak256()
encodeSigHeader(hasher, header)
hasher.(crypto.KeccakState).Read(hash[:])
return hash
Expand Down
4 changes: 2 additions & 2 deletions consensus/ethash/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ import (
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie"
"github.com/holiman/uint256"
"golang.org/x/crypto/sha3"
)

// Ethash proof-of-work protocol constants.
Expand Down Expand Up @@ -528,7 +528,7 @@ func (ethash *Ethash) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea

// SealHash returns the hash of a block prior to it being sealed.
func (ethash *Ethash) SealHash(header *types.Header) (hash common.Hash) {
hasher := sha3.NewLegacyKeccak256()
hasher := crypto.NewLegacyKeccak256()

enc := []interface{}{
header.ParentHash,
Expand Down
7 changes: 3 additions & 4 deletions core/rawdb/accessors_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"golang.org/x/crypto/sha3"
)

// Tests block header storage and retrieval operations.
Expand All @@ -53,7 +52,7 @@ func TestHeaderStorage(t *testing.T) {
if entry := ReadHeaderRLP(db, header.Hash(), header.Number.Uint64()); entry == nil {
t.Fatalf("Stored header RLP not found")
} else {
hasher := sha3.NewLegacyKeccak256()
hasher := crypto.NewLegacyKeccak256()
hasher.Write(entry)

if hash := common.BytesToHash(hasher.Sum(nil)); hash != header.Hash() {
Expand All @@ -74,7 +73,7 @@ func TestBodyStorage(t *testing.T) {
// Create a test body to move around the database and make sure it's really new
body := &types.Body{Uncles: []*types.Header{{Extra: []byte("test header")}}}

hasher := sha3.NewLegacyKeccak256()
hasher := crypto.NewLegacyKeccak256()
rlp.Encode(hasher, body)
hash := common.BytesToHash(hasher.Sum(nil))

Expand All @@ -91,7 +90,7 @@ func TestBodyStorage(t *testing.T) {
if entry := ReadBodyRLP(db, hash, 0); entry == nil {
t.Fatalf("Stored body RLP not found")
} else {
hasher := sha3.NewLegacyKeccak256()
hasher := crypto.NewLegacyKeccak256()
hasher.Write(entry)

if calc := common.BytesToHash(hasher.Sum(nil)); calc != hash {
Expand Down
3 changes: 1 addition & 2 deletions core/rlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"golang.org/x/crypto/sha3"
)

func getBlock(transactions int, uncles int, dataSize int) *types.Block {
Expand Down Expand Up @@ -147,7 +146,7 @@ func BenchmarkHashing(b *testing.B) {
blockRlp, _ = rlp.EncodeToBytes(block)
}
var got common.Hash
var hasher = sha3.NewLegacyKeccak256()
var hasher = crypto.NewLegacyKeccak256()
b.Run("iteratorhashing", func(b *testing.B) {
for b.Loop() {
var hash common.Hash
Expand Down
4 changes: 2 additions & 2 deletions core/state/snapshot/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"
Expand All @@ -34,11 +35,10 @@ import (
"github.com/ethereum/go-ethereum/triedb/hashdb"
"github.com/ethereum/go-ethereum/triedb/pathdb"
"github.com/holiman/uint256"
"golang.org/x/crypto/sha3"
)

func hashData(input []byte) common.Hash {
var hasher = sha3.NewLegacyKeccak256()
var hasher = crypto.NewLegacyKeccak256()
var hash common.Hash
hasher.Reset()
hasher.Write(input)
Expand Down
3 changes: 1 addition & 2 deletions core/state_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/trie"
"github.com/holiman/uint256"
"golang.org/x/crypto/sha3"
)

func u64(val uint64) *uint64 { return &val }
Expand Down Expand Up @@ -399,7 +398,7 @@ func GenerateBadBlock(parent *types.Block, engine consensus.Engine, txs types.Tr
var receipts []*types.Receipt
// The post-state result doesn't need to be correct (this is a bad block), but we do need something there
// Preferably something unique. So let's use a combo of blocknum + txhash
hasher := sha3.NewLegacyKeccak256()
hasher := crypto.NewLegacyKeccak256()
hasher.Write(header.Number.Bytes())
var cumulativeGas uint64
var nBlobs int
Expand Down
5 changes: 2 additions & 3 deletions crypto/keccak.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,16 @@ import (
"sync"

"github.com/ethereum/go-ethereum/common"
"golang.org/x/crypto/sha3"
)

// NewKeccakState creates a new KeccakState
func NewKeccakState() KeccakState {
return sha3.NewLegacyKeccak256().(KeccakState)
return NewLegacyKeccak256().(KeccakState)
}

var hasherPool = sync.Pool{
New: func() any {
return sha3.NewLegacyKeccak256().(KeccakState)
return NewLegacyKeccak256().(KeccakState)
},
}

Expand Down
9 changes: 9 additions & 0 deletions crypto/keccak_state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build !ziren && !wasm

package crypto

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

func NewLegacyKeccak256() KeccakState {
return sha3.NewLegacyKeccak256().(KeccakState)
}
55 changes: 55 additions & 0 deletions crypto/keccak_state_wasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//go:build !ziren && wasm

package crypto

import (
"sync"

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

func NewLegacyKeccak256() KeccakState {
return &simpleHashBuffer{}
}

type simpleHashBuffer struct {
buffer []byte
}

func (s *simpleHashBuffer) Write(p []byte) (n int, err error) {
s.buffer = append(s.buffer, p...)
return len(p), nil
}

func (s *simpleHashBuffer) Sum(b []byte) []byte {
currentBufferHash := make([]byte, 32)
s.Read(currentBufferHash)
return append(b, currentBufferHash...)
}

func (s *simpleHashBuffer) Reset() {
s.buffer = nil // Simply forget previous data.
}

func (s *simpleHashBuffer) Size() int {
return 32 // Keccak256 produces 32-byte hashes
}

func (s *simpleHashBuffer) BlockSize() int {
return (1600 - 512) / 8 // Keccak256 rate in bytes: sponge size 1600 bits - capacity 512 bits. Copied from sha3.
}

var realHasherPool = sync.Pool{
New: func() any {
return sha3.NewLegacyKeccak256().(KeccakState)
},
}

func (s *simpleHashBuffer) Read(bytes []byte) (int, error) {
d := realHasherPool.Get().(KeccakState)
defer realHasherPool.Put(d)

d.Reset()
d.Write(s.buffer)
return d.Read(bytes)
}
5 changes: 2 additions & 3 deletions eth/protocols/snap/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import (
"github.com/ethereum/go-ethereum/triedb"
"github.com/ethereum/go-ethereum/triedb/pathdb"
"github.com/holiman/uint256"
"golang.org/x/crypto/sha3"
)

func TestHashing(t *testing.T) {
Expand All @@ -55,7 +54,7 @@ func TestHashing(t *testing.T) {
}
var want, got string
var old = func() {
hasher := sha3.NewLegacyKeccak256()
hasher := crypto.NewLegacyKeccak256()
for i := 0; i < len(bytecodes); i++ {
hasher.Reset()
hasher.Write(bytecodes[i])
Expand Down Expand Up @@ -88,7 +87,7 @@ func BenchmarkHashing(b *testing.B) {
bytecodes[i] = buf
}
var old = func() {
hasher := sha3.NewLegacyKeccak256()
hasher := crypto.NewLegacyKeccak256()
for i := 0; i < len(bytecodes); i++ {
hasher.Reset()
hasher.Write(bytecodes[i])
Expand Down
4 changes: 2 additions & 2 deletions internal/blocktest/test_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"hash"

"github.com/ethereum/go-ethereum/common"
"golang.org/x/crypto/sha3"
"github.com/ethereum/go-ethereum/crypto"
)

// testHasher is the helper tool for transaction/receipt list hashing.
Expand All @@ -39,7 +39,7 @@ type testHasher struct {

// NewHasher returns a new testHasher instance.
func NewHasher() *testHasher {
return &testHasher{hasher: sha3.NewLegacyKeccak256()}
return &testHasher{hasher: crypto.NewLegacyKeccak256()}
}

// Reset resets the hash state.
Expand Down
5 changes: 2 additions & 3 deletions p2p/dnsdisc/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/enr"
"github.com/ethereum/go-ethereum/rlp"
"golang.org/x/crypto/sha3"
)

// Tree is a merkle tree of node records.
Expand Down Expand Up @@ -262,7 +261,7 @@ const (
)

func subdomain(e entry) string {
h := sha3.NewLegacyKeccak256()
h := crypto.NewLegacyKeccak256()
io.WriteString(h, e.String())
return b32format.EncodeToString(h.Sum(nil)[:16])
}
Expand All @@ -272,7 +271,7 @@ func (e *rootEntry) String() string {
}

func (e *rootEntry) sigHash() []byte {
h := sha3.NewLegacyKeccak256()
h := crypto.NewLegacyKeccak256()
fmt.Fprintf(h, rootPrefix+" e=%s l=%s seq=%d", e.eroot, e.lroot, e.seq)
return h.Sum(nil)
}
Expand Down
5 changes: 2 additions & 3 deletions p2p/enode/idscheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p/enr"
"github.com/ethereum/go-ethereum/rlp"
"golang.org/x/crypto/sha3"
)

// ValidSchemes is a List of known secure identity schemes.
Expand All @@ -49,7 +48,7 @@ func SignV4(r *enr.Record, privkey *ecdsa.PrivateKey) error {
cpy.Set(enr.ID("v4"))
cpy.Set(Secp256k1(privkey.PublicKey))

h := sha3.NewLegacyKeccak256()
h := crypto.NewLegacyKeccak256()
rlp.Encode(h, cpy.AppendElements(nil))
sig, err := crypto.Sign(h.Sum(nil), privkey)
if err != nil {
Expand All @@ -70,7 +69,7 @@ func (V4ID) Verify(r *enr.Record, sig []byte) error {
return errors.New("invalid public key")
}

h := sha3.NewLegacyKeccak256()
h := crypto.NewLegacyKeccak256()
rlp.Encode(h, r.AppendElements(nil))
if !crypto.VerifySignature(entry, h.Sum(nil), sig) {
return enr.ErrInvalidSig
Expand Down
5 changes: 2 additions & 3 deletions p2p/rlpx/rlpx.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import (
"github.com/ethereum/go-ethereum/crypto/ecies"
"github.com/ethereum/go-ethereum/rlp"
"github.com/golang/snappy"
"golang.org/x/crypto/sha3"
)

// Conn is an RLPx network connection. It wraps a low-level network connection. The
Expand Down Expand Up @@ -486,10 +485,10 @@ func (h *handshakeState) secrets(auth, authResp []byte) (Secrets, error) {
}

// setup sha3 instances for the MACs
mac1 := sha3.NewLegacyKeccak256()
mac1 := crypto.NewLegacyKeccak256()
mac1.Write(xor(s.MAC, h.respNonce))
mac1.Write(auth)
mac2 := sha3.NewLegacyKeccak256()
mac2 := crypto.NewLegacyKeccak256()
mac2.Write(xor(s.MAC, h.initNonce))
mac2.Write(authResp)
if h.initiator {
Expand Down
3 changes: 1 addition & 2 deletions tests/state_test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import (
"github.com/ethereum/go-ethereum/triedb/hashdb"
"github.com/ethereum/go-ethereum/triedb/pathdb"
"github.com/holiman/uint256"
"golang.org/x/crypto/sha3"
)

// StateTest checks transaction processing without block context.
Expand Down Expand Up @@ -488,7 +487,7 @@ func (tx *stTransaction) toMessage(ps stPostState, baseFee *big.Int) (*core.Mess
}

func rlpHash(x interface{}) (h common.Hash) {
hw := sha3.NewLegacyKeccak256()
hw := crypto.NewLegacyKeccak256()
rlp.Encode(hw, x)
hw.Sum(h[:0])
return h
Expand Down
Loading
Loading