Skip to content

Commit 9d27074

Browse files
refactor
1 parent c4e789b commit 9d27074

File tree

5 files changed

+24
-25
lines changed

5 files changed

+24
-25
lines changed

consensus/clique/clique.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ func (c *Clique) Close() error {
645645
func SealHash(header *types.Header) (hash common.Hash) {
646646
hasher := sha3.NewLegacyKeccak256()
647647
encodeSigHeader(hasher, header)
648-
hasher.(crypto.KeccakState).Read(hash[:])
648+
hasher.(crypto.KeccakSponge).Read(hash[:])
649649
return hash
650650
}
651651

crypto/crypto.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ type EllipticCurve interface {
5959
Unmarshal(data []byte) (x, y *big.Int)
6060
}
6161

62-
// KeccakState wraps sha3.state. In addition to the usual hash methods, it also supports
62+
// KeccakSponge wraps sha3.state. In addition to the usual hash methods, it also supports
6363
// Read to get a variable amount of data from the hash state. Read is faster than Sum
6464
// because it doesn't copy the internal state, but also modifies the internal state.
65-
type KeccakState interface {
65+
type KeccakSponge interface {
6666
hash.Hash
6767
Read([]byte) (int, error)
6868
}

crypto/keccak.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,39 +25,38 @@ import (
2525
"golang.org/x/crypto/sha3"
2626
)
2727

28-
// NewKeccakState creates a new KeccakState
29-
func NewKeccakState() KeccakState {
30-
return sha3.NewLegacyKeccak256().(KeccakState)
28+
// NewKeccakSponge creates a new KeccakSponge
29+
func NewKeccakSponge() KeccakSponge {
30+
return sha3.NewLegacyKeccak256().(KeccakSponge)
3131
}
3232

3333
var hasherPool = sync.Pool{
3434
New: func() any {
35-
return sha3.NewLegacyKeccak256().(KeccakState)
35+
return sha3.NewLegacyKeccak256().(KeccakSponge)
3636
},
3737
}
3838

39-
// Keccak256 calculates and returns the Keccak256 hash of the input data.
40-
func Keccak256(data ...[]byte) []byte {
41-
b := make([]byte, 32)
42-
d := hasherPool.Get().(KeccakState)
39+
// keccak256Sum is a helper that hashes the input data using the provided KeccakSponge and writes the result to out.
40+
func keccak256Sum(out []byte, data ...[]byte) {
41+
d := hasherPool.Get().(KeccakSponge)
4342
d.Reset()
4443
for _, b := range data {
4544
d.Write(b)
4645
}
47-
d.Read(b)
46+
d.Read(out)
4847
hasherPool.Put(d)
48+
}
49+
50+
// Keccak256 calculates and returns the Keccak256 hash of the input data.
51+
func Keccak256(data ...[]byte) []byte {
52+
b := make([]byte, 32)
53+
keccak256Sum(b, data...)
4954
return b
5055
}
5156

5257
// Keccak256Hash calculates and returns the Keccak256 hash of the input data,
5358
// converting it to an internal Hash data structure.
5459
func Keccak256Hash(data ...[]byte) (h common.Hash) {
55-
d := hasherPool.Get().(KeccakState)
56-
d.Reset()
57-
for _, b := range data {
58-
d.Write(b)
59-
}
60-
d.Read(h[:])
61-
hasherPool.Put(d)
60+
keccak256Sum(h[:], data...)
6261
return h
6362
}

crypto/keccak_ziren.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ import (
2323
"github.com/ethereum/go-ethereum/common"
2424
)
2525

26-
// zirenKeccakState implements the KeccakState interface using the Ziren zkvm_runtime.
26+
// zirenKeccakState implements the KeccakSponge interface using the Ziren zkvm_runtime.
2727
// It accumulates data written to it and uses the zkvm's Keccak256 system call for hashing.
2828
type zirenKeccakState struct {
2929
buf []byte // accumulated data
3030
result []byte // cached result
3131
dirty bool // whether new data has been written since last hash
3232
}
3333

34-
func newZirenKeccakState() KeccakState {
34+
func newZirenKeccakState() KeccakSponge {
3535
return &zirenKeccakState{
3636
buf: make([]byte, 0, 512), // pre-allocate reasonable capacity
3737
}
@@ -83,9 +83,9 @@ func (s *zirenKeccakState) computeHashIfNeeded() {
8383
}
8484
}
8585

86-
// NewKeccakState creates a new KeccakState
86+
// NewKeccakSponge creates a new KeccakSponge
8787
// This uses a Ziren-optimized implementation that leverages the zkvm_runtime.Keccak256 system call.
88-
func NewKeccakState() KeccakState {
88+
func NewKeccakState() KeccakSponge {
8989
return newZirenKeccakState()
9090
}
9191

trie/trie_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ func TestCommitSequence(t *testing.T) {
899899
addresses, accounts := makeAccounts(tc.count)
900900

901901
// This spongeDb is used to check the sequence of disk-db-writes
902-
s := &spongeDb{sponge: crypto.NewKeccakState()}
902+
s := &spongeDb{sponge: crypto.NewKeccakSponge()}
903903
db := newTestDatabase(rawdb.NewDatabase(s), rawdb.HashScheme)
904904

905905
// Fill the trie with elements
@@ -933,7 +933,7 @@ func TestCommitSequenceRandomBlobs(t *testing.T) {
933933
} {
934934
// This spongeDb is used to check the sequence of disk-db-writes
935935
prng := rand.New(rand.NewSource(int64(i)))
936-
s := &spongeDb{sponge: crypto.NewKeccakState()}
936+
s := &spongeDb{sponge: crypto.NewKeccakSponge()}
937937
db := newTestDatabase(rawdb.NewDatabase(s), rawdb.HashScheme)
938938

939939
// Fill the trie with elements

0 commit comments

Comments
 (0)