Skip to content

Commit bca9ab8

Browse files
committed
rework to replace the module
1 parent a799864 commit bca9ab8

File tree

7 files changed

+346
-157
lines changed

7 files changed

+346
-157
lines changed

cmd/keeper/crypto_ziren/crypto.go

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
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+
package crypto
18+
19+
import (
20+
"errors"
21+
"syscall"
22+
"unsafe"
23+
24+
"github.com/ethereum/go-ethereum/common"
25+
originalcrypto "github.com/ethereum/go-ethereum/crypto"
26+
)
27+
28+
// Ziren zkVM system call numbers
29+
const (
30+
// SYS_KECCAK_SPONGE is the system call number for keccak sponge compression in Ziren zkVM
31+
// This performs the keccak-f[1600] permutation on a 1600-bit (200-byte) state
32+
SYS_KECCAK_SPONGE = 0x010109
33+
)
34+
35+
// Keccak256 constants
36+
const (
37+
keccakRate = 136 // 1088 bits = 136 bytes for keccak256
38+
keccakCapacity = 64 // 512 bits = 64 bytes
39+
keccakStateSize = 200 // 1600 bits = 200 bytes
40+
)
41+
42+
// zirenKeccakSponge calls the Ziren zkVM keccak sponge compression function
43+
// This performs the keccak-f[1600] permutation on the 200-byte state
44+
func zirenKeccakSponge(state *[keccakStateSize]byte) error {
45+
_, _, errno := syscall.Syscall(
46+
SYS_KECCAK_SPONGE,
47+
uintptr(unsafe.Pointer(state)), // State pointer (input/output)
48+
0, 0, // Unused parameters
49+
)
50+
51+
if errno != 0 {
52+
return errors.New("keccak sponge syscall failed")
53+
}
54+
55+
return nil
56+
}
57+
58+
// zirenKeccak256 implements full keccak256 using the Ziren sponge syscall
59+
func zirenKeccak256(data []byte) []byte {
60+
// Initialize state to zeros
61+
var state [keccakStateSize]byte
62+
63+
// Pad input according to keccak256 specification
64+
// Padding: append 0x01, then zero or more 0x00 bytes, then 0x80
65+
padded := make([]byte, len(data))
66+
copy(padded, data)
67+
padded = append(padded, 0x01) // Domain separator for keccak256
68+
69+
// Pad to multiple of rate (136 bytes for keccak256)
70+
for len(padded)%keccakRate != (keccakRate - 1) {
71+
padded = append(padded, 0x00)
72+
}
73+
padded = append(padded, 0x80) // Final padding bit
74+
75+
// Absorb phase: process input in chunks of rate size
76+
for i := 0; i < len(padded); i += keccakRate {
77+
// XOR current chunk with state
78+
for j := 0; j < keccakRate && i+j < len(padded); j++ {
79+
state[j] ^= padded[i+j]
80+
}
81+
82+
// Apply keccak-f[1600] permutation via syscall
83+
if err := zirenKeccakSponge(&state); err != nil {
84+
// Fallback to standard implementation on error
85+
return originalcrypto.Keccak256(data)
86+
}
87+
}
88+
89+
// Squeeze phase: extract 32 bytes (256 bits) for keccak256
90+
result := make([]byte, 32)
91+
copy(result, state[:32])
92+
93+
return result
94+
}
95+
96+
// Re-export everything from original crypto package except the parts we're overriding
97+
var (
98+
S256 = originalcrypto.S256
99+
PubkeyToAddress = originalcrypto.PubkeyToAddress
100+
Ecrecover = originalcrypto.Ecrecover
101+
SigToPub = originalcrypto.SigToPub
102+
Sign = originalcrypto.Sign
103+
VerifySignature = originalcrypto.VerifySignature
104+
DecompressPubkey = originalcrypto.DecompressPubkey
105+
CompressPubkey = originalcrypto.CompressPubkey
106+
HexToECDSA = originalcrypto.HexToECDSA
107+
LoadECDSA = originalcrypto.LoadECDSA
108+
SaveECDSA = originalcrypto.SaveECDSA
109+
GenerateKey = originalcrypto.GenerateKey
110+
ValidateSignatureValues = originalcrypto.ValidateSignatureValues
111+
Keccak512 = originalcrypto.Keccak512
112+
)
113+
114+
// Re-export types
115+
type (
116+
KeccakState = originalcrypto.KeccakState
117+
)
118+
119+
// zirenKeccakState implements crypto.KeccakState using the Ziren sponge precompile
120+
type zirenKeccakState struct {
121+
state [keccakStateSize]byte // 200-byte keccak state
122+
absorbed int // Number of bytes absorbed into current block
123+
buffer [keccakRate]byte // Rate-sized buffer for current block
124+
finalized bool // Whether absorption is complete
125+
}
126+
127+
func (k *zirenKeccakState) Reset() {
128+
for i := range k.state {
129+
k.state[i] = 0
130+
}
131+
for i := range k.buffer {
132+
k.buffer[i] = 0
133+
}
134+
k.absorbed = 0
135+
k.finalized = false
136+
}
137+
138+
func (k *zirenKeccakState) Clone() KeccakState {
139+
clone := &zirenKeccakState{
140+
absorbed: k.absorbed,
141+
finalized: k.finalized,
142+
}
143+
copy(clone.state[:], k.state[:])
144+
copy(clone.buffer[:], k.buffer[:])
145+
return clone
146+
}
147+
148+
func (k *zirenKeccakState) Write(data []byte) (int, error) {
149+
if k.finalized {
150+
panic("write to finalized keccak state")
151+
}
152+
153+
written := 0
154+
for len(data) > 0 {
155+
// Fill current block
156+
canWrite := keccakRate - k.absorbed
157+
if canWrite > len(data) {
158+
canWrite = len(data)
159+
}
160+
161+
copy(k.buffer[k.absorbed:], data[:canWrite])
162+
k.absorbed += canWrite
163+
data = data[canWrite:]
164+
written += canWrite
165+
166+
// If block is full, absorb it
167+
if k.absorbed == keccakRate {
168+
k.absorbBlock()
169+
}
170+
}
171+
172+
return written, nil
173+
}
174+
175+
// absorbBlock XORs the current buffer into state and applies the sponge permutation
176+
func (k *zirenKeccakState) absorbBlock() {
177+
// XOR buffer into state
178+
for i := 0; i < keccakRate; i++ {
179+
k.state[i] ^= k.buffer[i]
180+
}
181+
182+
// Apply keccak-f[1600] permutation via Ziren syscall
183+
if err := zirenKeccakSponge(&k.state); err != nil {
184+
// On error, fallback to standard Go implementation
185+
// This shouldn't happen in production but provides safety
186+
fallbackState := originalcrypto.NewKeccakState()
187+
fallbackState.Reset()
188+
fallbackState.Write(k.buffer[:k.absorbed])
189+
fallbackState.Read(k.state[:32])
190+
}
191+
192+
// Reset buffer
193+
k.absorbed = 0
194+
for i := range k.buffer {
195+
k.buffer[i] = 0
196+
}
197+
}
198+
199+
func (k *zirenKeccakState) Read(hash []byte) (int, error) {
200+
if len(hash) < 32 {
201+
return 0, errors.New("hash slice too short")
202+
}
203+
204+
if !k.finalized {
205+
k.finalize()
206+
}
207+
208+
copy(hash[:32], k.state[:32])
209+
return 32, nil
210+
}
211+
212+
// finalize completes the absorption phase with padding
213+
func (k *zirenKeccakState) finalize() {
214+
// Add keccak256 padding: 0x01, then zeros, then 0x80
215+
k.buffer[k.absorbed] = 0x01
216+
k.absorbed++
217+
218+
// Pad with zeros until we have room for final bit
219+
for k.absorbed < keccakRate-1 {
220+
k.buffer[k.absorbed] = 0x00
221+
k.absorbed++
222+
}
223+
224+
// Add final padding bit
225+
k.buffer[keccakRate-1] = 0x80
226+
k.absorbed = keccakRate
227+
228+
// Absorb final block
229+
k.absorbBlock()
230+
k.finalized = true
231+
}
232+
233+
func (k *zirenKeccakState) Sum(data []byte) []byte {
234+
hash := make([]byte, 32)
235+
k.Read(hash)
236+
return append(data, hash...)
237+
}
238+
239+
func (k *zirenKeccakState) Size() int {
240+
return 32
241+
}
242+
243+
func (k *zirenKeccakState) BlockSize() int {
244+
return 136 // keccak256 block size
245+
}
246+
247+
// Keccak256 calculates and returns the Keccak256 hash using the ziren platform precompile.
248+
func Keccak256(data ...[]byte) []byte {
249+
// For multiple data chunks, concatenate them
250+
if len(data) == 0 {
251+
return zirenKeccak256(nil)
252+
}
253+
if len(data) == 1 {
254+
return zirenKeccak256(data[0])
255+
}
256+
257+
// Concatenate multiple data chunks
258+
var totalLen int
259+
for _, d := range data {
260+
totalLen += len(d)
261+
}
262+
263+
combined := make([]byte, 0, totalLen)
264+
for _, d := range data {
265+
combined = append(combined, d...)
266+
}
267+
268+
return zirenKeccak256(combined)
269+
}
270+
271+
// Keccak256Hash calculates and returns the Keccak256 hash as a Hash using the ziren platform precompile.
272+
func Keccak256Hash(data ...[]byte) (h common.Hash) {
273+
hash := Keccak256(data...)
274+
copy(h[:], hash)
275+
return h
276+
}
277+
278+
// NewKeccakState returns a new keccak state hasher using the ziren platform precompile.
279+
func NewKeccakState() KeccakState {
280+
return &zirenKeccakState{}
281+
}

cmd/keeper/crypto_ziren/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/ethereum/go-ethereum/crypto
2+
3+
go 1.24.0

cmd/keeper/go.ziren.mod

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module github.com/ethereum/go-ethereum/cmd/keeper
2+
3+
go 1.24.0
4+
5+
require (
6+
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
8+
)
9+
10+
require (
11+
github.com/StackExchange/wmi v1.2.1 // indirect
12+
github.com/VictoriaMetrics/fastcache v1.12.2 // indirect
13+
github.com/bits-and-blooms/bitset v1.20.0 // indirect
14+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
15+
github.com/consensys/gnark-crypto v0.18.0 // indirect
16+
github.com/crate-crypto/go-eth-kzg v1.3.0 // indirect
17+
github.com/crate-crypto/go-ipa v0.0.0-20240724233137-53bbb0ceb27a // indirect
18+
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
19+
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
20+
github.com/emicklei/dot v1.6.2 // indirect
21+
github.com/ethereum/c-kzg-4844/v2 v2.1.0 // indirect
22+
github.com/ethereum/go-verkle v0.2.2 // indirect
23+
github.com/ferranbt/fastssz v0.1.4 // indirect
24+
github.com/go-ole/go-ole v1.3.0 // indirect
25+
github.com/gofrs/flock v0.12.1 // indirect
26+
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
27+
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
28+
github.com/holiman/uint256 v1.3.2 // indirect
29+
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
30+
github.com/mattn/go-runewidth v0.0.13 // indirect
31+
github.com/minio/sha256-simd v1.0.0 // indirect
32+
github.com/mitchellh/mapstructure v1.4.1 // indirect
33+
github.com/olekukonko/tablewriter v0.0.5 // indirect
34+
github.com/rivo/uniseg v0.2.0 // indirect
35+
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
36+
github.com/supranational/blst v0.3.14 // indirect
37+
github.com/tklauser/go-sysconf v0.3.12 // indirect
38+
github.com/tklauser/numcpus v0.6.1 // indirect
39+
golang.org/x/crypto v0.36.0 // indirect
40+
golang.org/x/sync v0.12.0 // indirect
41+
golang.org/x/sys v0.31.0 // indirect
42+
gopkg.in/yaml.v2 v2.4.0 // indirect
43+
)
44+
45+
replace (
46+
github.com/ethereum/go-ethereum => ../../
47+
github.com/ethereum/go-ethereum/crypto => ./crypto_ziren
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+
)

core/vm/evm.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/ethereum/go-ethereum/core/tracing"
2727
"github.com/ethereum/go-ethereum/core/types"
2828
"github.com/ethereum/go-ethereum/crypto"
29-
"github.com/ethereum/go-ethereum/crypto/platcrypto"
3029
"github.com/ethereum/go-ethereum/log"
3130
"github.com/ethereum/go-ethereum/params"
3231
"github.com/holiman/uint256"
@@ -145,7 +144,7 @@ func NewEVM(blockCtx BlockContext, statedb StateDB, chainConfig *params.ChainCon
145144
chainConfig: chainConfig,
146145
chainRules: chainConfig.Rules(blockCtx.BlockNumber, blockCtx.Random != nil, blockCtx.Time),
147146
jumpDests: newMapJumpDests(),
148-
hasher: platcrypto.NewKeccakState(),
147+
hasher: crypto.NewKeccakState(),
149148
}
150149
evm.precompiles = activePrecompiledContracts(evm.chainRules)
151150

crypto/platcrypto/keccak.go

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)