mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-02-26 15:47:21 +00:00
The upstream libray has removed the assembly-based implementation of keccak. We need to maintain our own library to avoid a peformance regression. --------- Co-authored-by: lightclient <lightclient@protonmail.com>
63 lines
1.7 KiB
Go
63 lines
1.7 KiB
Go
// Copyright 2025 The go-ethereum Authors
|
|
// This file is part of the go-ethereum library.
|
|
//
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//go:build !ziren
|
|
|
|
package crypto
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/ethereum/go-ethereum/crypto/keccak"
|
|
)
|
|
|
|
// NewKeccakState creates a new KeccakState
|
|
func NewKeccakState() KeccakState {
|
|
return keccak.NewLegacyKeccak256().(KeccakState)
|
|
}
|
|
|
|
var hasherPool = sync.Pool{
|
|
New: func() any {
|
|
return keccak.NewLegacyKeccak256().(KeccakState)
|
|
},
|
|
}
|
|
|
|
// Keccak256 calculates and returns the Keccak256 hash of the input data.
|
|
func Keccak256(data ...[]byte) []byte {
|
|
b := make([]byte, 32)
|
|
d := hasherPool.Get().(KeccakState)
|
|
d.Reset()
|
|
for _, b := range data {
|
|
d.Write(b)
|
|
}
|
|
d.Read(b)
|
|
hasherPool.Put(d)
|
|
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) (h common.Hash) {
|
|
d := hasherPool.Get().(KeccakState)
|
|
d.Reset()
|
|
for _, b := range data {
|
|
d.Write(b)
|
|
}
|
|
d.Read(h[:])
|
|
hasherPool.Put(d)
|
|
return h
|
|
}
|