Skip to content

Commit b1809d1

Browse files
authored
cmd/keeper: use the ziren keccak precompile (#32816)
Uses the go module's `replace` directive to delegate keccak computation to precompiles. This is still in draft because it needs more testing. Also, it relies on a PR that I created, that hasn't been merged yet. _Note that this PR doesn't implement the stateful keccak state structure, and it reverts to the current behavior. This is a bit silly since this is what is used in the tree root computation. The runtime doesn't currently export the sponge. I will see if I can fix that in a further PR, but it is going to take more time. In the meantime, this is a useful first step_
1 parent b6a4ac9 commit b1809d1

File tree

8 files changed

+135
-56
lines changed

8 files changed

+135
-56
lines changed

cmd/keeper/getpayload_ziren.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
package main
2020

2121
import (
22-
zkruntime "github.com/zkMIPS/zkMIPS/crates/go-runtime/zkm_runtime"
22+
zkruntime "github.com/ProjectZKM/Ziren/crates/go-runtime/zkvm_runtime"
2323
)
2424

2525
// getInput reads the input payload from the zkVM runtime environment.

cmd/keeper/go.mod

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ module github.com/ethereum/go-ethereum/cmd/keeper
33
go 1.24.0
44

55
require (
6+
github.com/ProjectZKM/Ziren/crates/go-runtime/zkvm_runtime v0.0.0-20251001021608-1fe7b43fc4d6
67
github.com/ethereum/go-ethereum v0.0.0-00010101000000-000000000000
7-
github.com/zkMIPS/zkMIPS/crates/go-runtime/zkm_runtime v0.0.0-20250915074013-fbc07aa2c6f5
88
)
99

1010
require (
@@ -43,7 +43,4 @@ require (
4343
gopkg.in/yaml.v2 v2.4.0 // indirect
4444
)
4545

46-
replace (
47-
github.com/ethereum/go-ethereum => ../../
48-
github.com/zkMIPS/zkMIPS/crates/go-runtime/zkm_runtime => github.com/weilzkm/zkMIPS/crates/go-runtime/zkvm_runtime v0.0.0-20250915074013-fbc07aa2c6f5
49-
)
46+
replace github.com/ethereum/go-ethereum => ../../

cmd/keeper/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/DataDog/zstd v1.4.5 h1:EndNeuB0l9syBZhut0wns3gV1hL8zX8LIu6ZiVHWLIQ=
22
github.com/DataDog/zstd v1.4.5/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo=
3+
github.com/ProjectZKM/Ziren/crates/go-runtime/zkvm_runtime v0.0.0-20251001021608-1fe7b43fc4d6 h1:1zYrtlhrZ6/b6SAjLSfKzWtdgqK0U+HtH/VcBWh1BaU=
4+
github.com/ProjectZKM/Ziren/crates/go-runtime/zkvm_runtime v0.0.0-20251001021608-1fe7b43fc4d6/go.mod h1:ioLG6R+5bUSO1oeGSDxOV3FADARuMoytZCSX6MEMQkI=
35
github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDOSA=
46
github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8=
57
github.com/VictoriaMetrics/fastcache v1.13.0 h1:AW4mheMR5Vd9FkAPUv+NH6Nhw+fmbTMGMsNAoA/+4G0=
@@ -117,8 +119,6 @@ github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFA
117119
github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI=
118120
github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk=
119121
github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY=
120-
github.com/weilzkm/zkMIPS/crates/go-runtime/zkvm_runtime v0.0.0-20250915074013-fbc07aa2c6f5 h1:MxKlbmI7Dta6O6Nsc9OAer/rOltjoL11CVLMqCiYnxU=
121-
github.com/weilzkm/zkMIPS/crates/go-runtime/zkvm_runtime v0.0.0-20250915074013-fbc07aa2c6f5/go.mod h1:zk/SUgiiVz2U1ufZ+yM2MHPbD93W25KH5zK3qAxXbT4=
122122
golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=
123123
golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc=
124124
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df h1:UA2aFVmmsIlefxMk29Dp2juaUSth8Pyn3Tq5Y5mJGME=

crypto/crypto.go

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,10 @@ import (
2828
"io"
2929
"math/big"
3030
"os"
31-
"sync"
3231

3332
"github.com/ethereum/go-ethereum/common"
3433
"github.com/ethereum/go-ethereum/common/math"
3534
"github.com/ethereum/go-ethereum/rlp"
36-
"golang.org/x/crypto/sha3"
3735
)
3836

3937
// SignatureLength indicates the byte length required to carry a signature with recovery id.
@@ -69,17 +67,6 @@ type KeccakState interface {
6967
Read([]byte) (int, error)
7068
}
7169

72-
// NewKeccakState creates a new KeccakState
73-
func NewKeccakState() KeccakState {
74-
return sha3.NewLegacyKeccak256().(KeccakState)
75-
}
76-
77-
var hasherPool = sync.Pool{
78-
New: func() any {
79-
return sha3.NewLegacyKeccak256().(KeccakState)
80-
},
81-
}
82-
8370
// HashData hashes the provided data using the KeccakState and returns a 32 byte hash
8471
func HashData(kh KeccakState, data []byte) (h common.Hash) {
8572
kh.Reset()
@@ -88,41 +75,6 @@ func HashData(kh KeccakState, data []byte) (h common.Hash) {
8875
return h
8976
}
9077

91-
// Keccak256 calculates and returns the Keccak256 hash of the input data.
92-
func Keccak256(data ...[]byte) []byte {
93-
b := make([]byte, 32)
94-
d := hasherPool.Get().(KeccakState)
95-
d.Reset()
96-
for _, b := range data {
97-
d.Write(b)
98-
}
99-
d.Read(b)
100-
hasherPool.Put(d)
101-
return b
102-
}
103-
104-
// Keccak256Hash calculates and returns the Keccak256 hash of the input data,
105-
// converting it to an internal Hash data structure.
106-
func Keccak256Hash(data ...[]byte) (h common.Hash) {
107-
d := hasherPool.Get().(KeccakState)
108-
d.Reset()
109-
for _, b := range data {
110-
d.Write(b)
111-
}
112-
d.Read(h[:])
113-
hasherPool.Put(d)
114-
return h
115-
}
116-
117-
// Keccak512 calculates and returns the Keccak512 hash of the input data.
118-
func Keccak512(data ...[]byte) []byte {
119-
d := sha3.NewLegacyKeccak512()
120-
for _, b := range data {
121-
d.Write(b)
122-
}
123-
return d.Sum(nil)
124-
}
125-
12678
// CreateAddress creates an ethereum address given the bytes and the nonce
12779
func CreateAddress(b common.Address, nonce uint64) common.Address {
12880
data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})

crypto/keccak.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2025 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
//go:build !ziren
18+
19+
package crypto
20+
21+
import (
22+
"sync"
23+
24+
"github.com/ethereum/go-ethereum/common"
25+
"golang.org/x/crypto/sha3"
26+
)
27+
28+
// NewKeccakState creates a new KeccakState
29+
func NewKeccakState() KeccakState {
30+
return sha3.NewLegacyKeccak256().(KeccakState)
31+
}
32+
33+
var hasherPool = sync.Pool{
34+
New: func() any {
35+
return sha3.NewLegacyKeccak256().(KeccakState)
36+
},
37+
}
38+
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)
43+
d.Reset()
44+
for _, b := range data {
45+
d.Write(b)
46+
}
47+
d.Read(b)
48+
hasherPool.Put(d)
49+
return b
50+
}
51+
52+
// Keccak256Hash calculates and returns the Keccak256 hash of the input data,
53+
// converting it to an internal Hash data structure.
54+
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)
62+
return h
63+
}

crypto/keccak_ziren.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2025 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
//go:build ziren
18+
19+
package crypto
20+
21+
import (
22+
"github.com/ProjectZKM/Ziren/crates/go-runtime/zkvm_runtime"
23+
"github.com/ethereum/go-ethereum/common"
24+
"golang.org/x/crypto/sha3"
25+
)
26+
27+
// NewKeccakState creates a new KeccakState
28+
// For now, we fallback to the original implementation for the stateful interface.
29+
// TODO: Implement a stateful wrapper around zkvm_runtime.Keccak256 if needed.
30+
func NewKeccakState() KeccakState {
31+
return sha3.NewLegacyKeccak256().(KeccakState)
32+
}
33+
34+
// Keccak256 calculates and returns the Keccak256 hash using the Ziren zkvm_runtime implementation.
35+
func Keccak256(data ...[]byte) []byte {
36+
// For multiple data chunks, concatenate them
37+
if len(data) == 0 {
38+
result := zkvm_runtime.Keccak256(nil)
39+
return result[:]
40+
}
41+
if len(data) == 1 {
42+
result := zkvm_runtime.Keccak256(data[0])
43+
return result[:]
44+
}
45+
46+
// Concatenate multiple data chunks
47+
var totalLen int
48+
for _, d := range data {
49+
totalLen += len(d)
50+
}
51+
52+
combined := make([]byte, 0, totalLen)
53+
for _, d := range data {
54+
combined = append(combined, d...)
55+
}
56+
57+
result := zkvm_runtime.Keccak256(combined)
58+
return result[:]
59+
}
60+
61+
// Keccak256Hash calculates and returns the Keccak256 hash as a Hash using the Ziren zkvm_runtime implementation.
62+
func Keccak256Hash(data ...[]byte) common.Hash {
63+
return common.Hash(Keccak256(data...))
64+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ require (
8181
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.7.0 // indirect
8282
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0 // indirect
8383
github.com/DataDog/zstd v1.4.5 // indirect
84+
github.com/ProjectZKM/Ziren/crates/go-runtime/zkvm_runtime v0.0.0-20251001021608-1fe7b43fc4d6
8485
github.com/StackExchange/wmi v1.2.1 // indirect
8586
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.13 // indirect
8687
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.43 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ github.com/DataDog/zstd v1.4.5 h1:EndNeuB0l9syBZhut0wns3gV1hL8zX8LIu6ZiVHWLIQ=
1414
github.com/DataDog/zstd v1.4.5/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo=
1515
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
1616
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
17+
github.com/ProjectZKM/Ziren/crates/go-runtime/zkvm_runtime v0.0.0-20251001021608-1fe7b43fc4d6 h1:1zYrtlhrZ6/b6SAjLSfKzWtdgqK0U+HtH/VcBWh1BaU=
18+
github.com/ProjectZKM/Ziren/crates/go-runtime/zkvm_runtime v0.0.0-20251001021608-1fe7b43fc4d6/go.mod h1:ioLG6R+5bUSO1oeGSDxOV3FADARuMoytZCSX6MEMQkI=
1719
github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDOSA=
1820
github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8=
1921
github.com/VictoriaMetrics/fastcache v1.13.0 h1:AW4mheMR5Vd9FkAPUv+NH6Nhw+fmbTMGMsNAoA/+4G0=

0 commit comments

Comments
 (0)