mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 06:36:43 +00:00
Merge develop into upstream_merge
This commit is contained in:
commit
08ffda54c8
1 changed files with 19 additions and 4 deletions
|
|
@ -28,6 +28,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"math/big"
|
"math/big"
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/common/math"
|
"github.com/ethereum/go-ethereum/common/math"
|
||||||
|
|
@ -68,9 +69,21 @@ type KeccakState interface {
|
||||||
Read([]byte) (int, error)
|
Read([]byte) (int, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// hasherPool holds LegacyKeccak256 hashers for reuse.
|
||||||
|
var hasherPool = sync.Pool{
|
||||||
|
New: func() interface{} { return sha3.NewLegacyKeccak256() },
|
||||||
|
}
|
||||||
|
|
||||||
// NewKeccakState creates a new KeccakState
|
// NewKeccakState creates a new KeccakState
|
||||||
func NewKeccakState() KeccakState {
|
func NewKeccakState() KeccakState {
|
||||||
return sha3.NewLegacyKeccak256().(KeccakState)
|
return hasherPool.Get().(KeccakState)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PutKeccakState puts the KeccakState back into the pool.
|
||||||
|
// The state must not be used after calling this function.
|
||||||
|
func PutKeccakState(kh KeccakState) {
|
||||||
|
kh.Reset()
|
||||||
|
hasherPool.Put(kh)
|
||||||
}
|
}
|
||||||
|
|
||||||
// HashData hashes the provided data using the KeccakState and returns a 32 byte hash
|
// HashData hashes the provided data using the KeccakState and returns a 32 byte hash
|
||||||
|
|
@ -78,7 +91,6 @@ func HashData(kh KeccakState, data []byte) (h common.Hash) {
|
||||||
kh.Reset()
|
kh.Reset()
|
||||||
kh.Write(data)
|
kh.Write(data)
|
||||||
kh.Read(h[:])
|
kh.Read(h[:])
|
||||||
|
|
||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -86,13 +98,14 @@ func HashData(kh KeccakState, data []byte) (h common.Hash) {
|
||||||
func Keccak256(data ...[]byte) []byte {
|
func Keccak256(data ...[]byte) []byte {
|
||||||
b := make([]byte, 32)
|
b := make([]byte, 32)
|
||||||
d := NewKeccakState()
|
d := NewKeccakState()
|
||||||
|
defer PutKeccakState(d)
|
||||||
|
d.Reset()
|
||||||
|
|
||||||
for _, b := range data {
|
for _, b := range data {
|
||||||
d.Write(b)
|
d.Write(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
d.Read(b)
|
d.Read(b)
|
||||||
|
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -100,12 +113,14 @@ func Keccak256(data ...[]byte) []byte {
|
||||||
// converting it to an internal Hash data structure.
|
// converting it to an internal Hash data structure.
|
||||||
func Keccak256Hash(data ...[]byte) (h common.Hash) {
|
func Keccak256Hash(data ...[]byte) (h common.Hash) {
|
||||||
d := NewKeccakState()
|
d := NewKeccakState()
|
||||||
|
defer PutKeccakState(d)
|
||||||
|
d.Reset()
|
||||||
|
|
||||||
for _, b := range data {
|
for _, b := range data {
|
||||||
d.Write(b)
|
d.Write(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
d.Read(h[:])
|
d.Read(h[:])
|
||||||
|
|
||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue