Skip to content

crypto: upgrade hasher pool #31833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 2 additions & 4 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,9 +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.Write([]byte(msg))
return hasher.Sum(nil), msg
return crypto.Keccak256([]byte(msg)), msg
}

// WalletEventType represents the different event types that can be fired by
Expand Down
6 changes: 1 addition & 5 deletions cmd/evm/internal/t8ntool/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (
"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 @@ -437,10 +436,7 @@ func MakePreState(db ethdb.Database, accounts types.GenesisAlloc) *state.StateDB
}

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

// calcDifficulty is based on ethash.CalcDifficulty. This method is used in case
Expand Down
5 changes: 1 addition & 4 deletions cmd/workload/historytestgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/internal/flags"
"github.com/ethereum/go-ethereum/rlp"
"github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -131,9 +130,7 @@ func generateHistoryTests(clictx *cli.Context) error {
}

func calcReceiptsHash(rcpt []*types.Receipt) common.Hash {
h := crypto.NewKeccakState()
rlp.Encode(h, rcpt)
return common.Hash(h.Sum(nil))
return crypto.Keccak256RLPHash(rcpt)
}

func writeJSON(fileName string, value any) {
Expand Down
10 changes: 4 additions & 6 deletions consensus/clique/clique.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import (
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/trie"
"golang.org/x/crypto/sha3"
)

const (
Expand Down Expand Up @@ -651,11 +650,10 @@ func (c *Clique) APIs(chain consensus.ChainHeaderReader) []rpc.API {
}

// SealHash returns the hash of a block prior to it being sealed.
func SealHash(header *types.Header) (hash common.Hash) {
hasher := sha3.NewLegacyKeccak256()
encodeSigHeader(hasher, header)
hasher.(crypto.KeccakState).Read(hash[:])
return hash
func SealHash(header *types.Header) common.Hash {
buf := bytes.NewBuffer(nil)
encodeSigHeader(buf, header)
return crypto.Keccak256Hash(buf.Bytes())
}

// CliqueRLP returns the rlp bytes which needs to be signed for the proof-of-authority
Expand Down
9 changes: 2 additions & 7 deletions consensus/ethash/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ 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 @@ -527,8 +526,6 @@ 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()

enc := []interface{}{
header.ParentHash,
header.UncleHash,
Expand Down Expand Up @@ -559,9 +556,7 @@ func (ethash *Ethash) SealHash(header *types.Header) (hash common.Hash) {
if header.ParentBeaconRoot != nil {
panic("parent beacon root set on ethash")
}
rlp.Encode(hasher, enc)
hasher.Sum(hash[:0])
return hash
return crypto.Keccak256RLPHash(enc)
}

// accumulateRewards credits the coinbase of the given block with the mining
Expand Down
47 changes: 32 additions & 15 deletions crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,17 @@ func NewKeccakState() KeccakState {
return sha3.NewLegacyKeccak256().(KeccakState)
}

type keccakState struct {
KeccakState
hashBuf common.Hash
}

var hasherPool = sync.Pool{
New: func() any {
return sha3.NewLegacyKeccak256().(KeccakState)
return &keccakState{
hashBuf: common.Hash{},
KeccakState: sha3.NewLegacyKeccak256().(KeccakState),
}
},
}

Expand All @@ -90,28 +98,38 @@ func HashData(kh KeccakState, data []byte) (h common.Hash) {

// Keccak256 calculates and returns the Keccak256 hash of the input data.
func Keccak256(data ...[]byte) []byte {
b := make([]byte, 32)
d := hasherPool.Get().(KeccakState)
b := Keccak256Hash(data...)
return b[:]
}

// Keccak256RLP calculates and returns the Keccak256 hash of the input interface.
func Keccak256RLP(x interface{}) []byte {
b := Keccak256RLPHash(x)
return b[:]
}

// Keccak256Hash calculates and returns the Keccak256 hash of the input data,
// converting it to an internal Hash data structure.
func Keccak256Hash(data ...[]byte) common.Hash {
d := hasherPool.Get().(*keccakState)
d.Reset()
for _, b := range data {
d.Write(b)
}
d.Read(b)
d.Read(d.hashBuf[:])
hasherPool.Put(d)
return b
return d.hashBuf
}

// Keccak256Hash calculates and returns the Keccak256 hash of the input data,
// Keccak256RLPHash calculates and returns the Keccak256 hash of the input interface,
// converting it to an internal Hash data structure.
func Keccak256Hash(data ...[]byte) (h common.Hash) {
d := hasherPool.Get().(KeccakState)
func Keccak256RLPHash(x interface{}) common.Hash {
d := hasherPool.Get().(*keccakState)
d.Reset()
for _, b := range data {
d.Write(b)
}
d.Read(h[:])
rlp.Encode(d, x)
d.Read(d.hashBuf[:])
hasherPool.Put(d)
return h
return d.hashBuf
}

// Keccak512 calculates and returns the Keccak512 hash of the input data.
Expand All @@ -125,8 +143,7 @@ func Keccak512(data ...[]byte) []byte {

// CreateAddress creates an ethereum address given the bytes and the nonce
func CreateAddress(b common.Address, nonce uint64) common.Address {
data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})
return common.BytesToAddress(Keccak256(data)[12:])
return common.BytesToAddress(Keccak256RLP([]interface{}{b, nonce})[12:])
}

// CreateAddress2 creates an ethereum address given the address bytes, initial
Expand Down
12 changes: 4 additions & 8 deletions p2p/dnsdisc/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ import (
"encoding/base64"
"errors"
"fmt"
"io"
"slices"
"strings"

"github.com/ethereum/go-ethereum/crypto"
"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,19 +260,17 @@ const (
)

func subdomain(e entry) string {
h := sha3.NewLegacyKeccak256()
io.WriteString(h, e.String())
return b32format.EncodeToString(h.Sum(nil)[:16])
src := crypto.Keccak256([]byte(e.String()))
return b32format.EncodeToString(src[:16])
}

func (e *rootEntry) String() string {
return fmt.Sprintf(rootPrefix+" e=%s l=%s seq=%d sig=%s", e.eroot, e.lroot, e.seq, b64format.EncodeToString(e.sig))
}

func (e *rootEntry) sigHash() []byte {
h := sha3.NewLegacyKeccak256()
fmt.Fprintf(h, rootPrefix+" e=%s l=%s seq=%d", e.eroot, e.lroot, e.seq)
return h.Sum(nil)
prefix := fmt.Sprintf(rootPrefix+" e=%s l=%s seq=%d", e.eroot, e.lroot, e.seq)
return crypto.Keccak256([]byte(prefix))
}

func (e *rootEntry) verifySignature(pubkey *ecdsa.PublicKey) bool {
Expand Down
11 changes: 4 additions & 7 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,9 +48,8 @@ func SignV4(r *enr.Record, privkey *ecdsa.PrivateKey) error {
cpy.Set(enr.ID("v4"))
cpy.Set(Secp256k1(privkey.PublicKey))

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

h := sha3.NewLegacyKeccak256()
rlp.Encode(h, r.AppendElements(nil))
if !crypto.VerifySignature(entry, h.Sum(nil), sig) {
digestHash := crypto.Keccak256RLP(r.AppendElements(nil))
if !crypto.VerifySignature(entry, digestHash, sig) {
return enr.ErrInvalidSig
}
return nil
Expand Down