Skip to content

Commit b26b3e8

Browse files
committed
update ziren ref + use tags for crypto module
1 parent cbf86b4 commit b26b3e8

File tree

10 files changed

+127
-152
lines changed

10 files changed

+127
-152
lines changed

cmd/keeper/crypto_ziren/go.mod

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

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
@@ -4,7 +4,7 @@ go 1.24.0
44

55
require (
66
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
7+
github.com/ProjectZKM/Ziren/crates/go-runtime/zkvm_runtime v0.0.0-20251001021608-1fe7b43fc4d6
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 & 0 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=

cmd/keeper/go.ziren.mod

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

crypto/crypto.go

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,13 @@ import (
2424
"encoding/hex"
2525
"errors"
2626
"fmt"
27-
"hash"
2827
"io"
2928
"math/big"
3029
"os"
31-
"sync"
3230

3331
"github.com/ethereum/go-ethereum/common"
3432
"github.com/ethereum/go-ethereum/common/math"
3533
"github.com/ethereum/go-ethereum/rlp"
36-
"golang.org/x/crypto/sha3"
3734
)
3835

3936
// SignatureLength indicates the byte length required to carry a signature with recovery id.
@@ -61,68 +58,6 @@ type EllipticCurve interface {
6158
Unmarshal(data []byte) (x, y *big.Int)
6259
}
6360

64-
// KeccakState wraps sha3.state. In addition to the usual hash methods, it also supports
65-
// Read to get a variable amount of data from the hash state. Read is faster than Sum
66-
// because it doesn't copy the internal state, but also modifies the internal state.
67-
type KeccakState interface {
68-
hash.Hash
69-
Read([]byte) (int, error)
70-
}
71-
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-
83-
// HashData hashes the provided data using the KeccakState and returns a 32 byte hash
84-
func HashData(kh KeccakState, data []byte) (h common.Hash) {
85-
kh.Reset()
86-
kh.Write(data)
87-
kh.Read(h[:])
88-
return h
89-
}
90-
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-
12661
// CreateAddress creates an ethereum address given the bytes and the nonce
12762
func CreateAddress(b common.Address, nonce uint64) common.Address {
12863
data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})

crypto/keccak.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
"hash"
23+
"sync"
24+
25+
"github.com/ethereum/go-ethereum/common"
26+
"golang.org/x/crypto/sha3"
27+
)
28+
29+
// KeccakState wraps sha3.state. In addition to the usual hash methods, it also supports
30+
// Read to get a variable amount of data from the hash state. Read is faster than Sum
31+
// because it doesn't copy the internal state, but also modifies the internal state.
32+
type KeccakState interface {
33+
hash.Hash
34+
Read([]byte) (int, error)
35+
}
36+
37+
// NewKeccakState creates a new KeccakState
38+
func NewKeccakState() KeccakState {
39+
return sha3.NewLegacyKeccak256().(KeccakState)
40+
}
41+
42+
var hasherPool = sync.Pool{
43+
New: func() any {
44+
return sha3.NewLegacyKeccak256().(KeccakState)
45+
},
46+
}
47+
48+
// HashData hashes the provided data using the KeccakState and returns a 32 byte hash
49+
func HashData(kh KeccakState, data []byte) (h common.Hash) {
50+
kh.Reset()
51+
kh.Write(data)
52+
kh.Read(h[:])
53+
return h
54+
}
55+
56+
// Keccak256 calculates and returns the Keccak256 hash of the input data.
57+
func Keccak256(data ...[]byte) []byte {
58+
b := make([]byte, 32)
59+
d := hasherPool.Get().(KeccakState)
60+
d.Reset()
61+
for _, b := range data {
62+
d.Write(b)
63+
}
64+
d.Read(b)
65+
hasherPool.Put(d)
66+
return b
67+
}
68+
69+
// Keccak256Hash calculates and returns the Keccak256 hash of the input data,
70+
// converting it to an internal Hash data structure.
71+
func Keccak256Hash(data ...[]byte) (h common.Hash) {
72+
d := hasherPool.Get().(KeccakState)
73+
d.Reset()
74+
for _, b := range data {
75+
d.Write(b)
76+
}
77+
d.Read(h[:])
78+
hasherPool.Put(d)
79+
return h
80+
}
81+
82+
// Keccak512 calculates and returns the Keccak512 hash of the input data.
83+
func Keccak512(data ...[]byte) []byte {
84+
d := sha3.NewLegacyKeccak512()
85+
for _, b := range data {
86+
d.Write(b)
87+
}
88+
return d.Sum(nil)
89+
}

cmd/keeper/crypto_ziren/crypto.go renamed to crypto/keccak_ziren.go

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,40 @@
1515
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
1616

1717
//go:build ziren
18-
// +build ziren
1918

2019
package crypto
2120

2221
import (
22+
"hash"
23+
2324
"github.com/ProjectZKM/Ziren/crates/go-runtime/zkvm_runtime"
2425
"github.com/ethereum/go-ethereum/common"
25-
originalcrypto "github.com/ethereum/go-ethereum/crypto"
26+
"golang.org/x/crypto/sha3"
2627
)
2728

28-
// Re-export everything from original crypto package except the parts we're overriding
29-
var (
30-
S256 = originalcrypto.S256
31-
PubkeyToAddress = originalcrypto.PubkeyToAddress
32-
Ecrecover = originalcrypto.Ecrecover
33-
SigToPub = originalcrypto.SigToPub
34-
Sign = originalcrypto.Sign
35-
VerifySignature = originalcrypto.VerifySignature
36-
DecompressPubkey = originalcrypto.DecompressPubkey
37-
CompressPubkey = originalcrypto.CompressPubkey
38-
HexToECDSA = originalcrypto.HexToECDSA
39-
LoadECDSA = originalcrypto.LoadECDSA
40-
SaveECDSA = originalcrypto.SaveECDSA
41-
GenerateKey = originalcrypto.GenerateKey
42-
ValidateSignatureValues = originalcrypto.ValidateSignatureValues
43-
Keccak512 = originalcrypto.Keccak512
44-
)
29+
// KeccakState wraps sha3.state. In addition to the usual hash methods, it also supports
30+
// Read to get a variable amount of data from the hash state. Read is faster than Sum
31+
// because it doesn't copy the internal state, but also modifies the internal state.
32+
type KeccakState interface {
33+
hash.Hash
34+
Read([]byte) (int, error)
35+
}
4536

46-
// Re-export types
47-
type (
48-
KeccakState = originalcrypto.KeccakState
49-
)
37+
// NewKeccakState creates a new KeccakState
38+
// For now, we fallback to the original implementation for the stateful interface.
39+
// TODO: Implement a stateful wrapper around zkvm_runtime.Keccak256 if needed.
40+
func NewKeccakState() KeccakState {
41+
return sha3.NewLegacyKeccak256().(KeccakState)
42+
}
43+
44+
// HashData hashes the provided data using the KeccakState and returns a 32 byte hash
45+
// For now, we fallback to the original implementation for the stateful interface.
46+
func HashData(kh KeccakState, data []byte) (h common.Hash) {
47+
kh.Reset()
48+
kh.Write(data)
49+
kh.Read(h[:])
50+
return h
51+
}
5052

5153
// Keccak256 calculates and returns the Keccak256 hash using the Ziren zkvm_runtime implementation.
5254
func Keccak256(data ...[]byte) []byte {
@@ -82,9 +84,8 @@ func Keccak256Hash(data ...[]byte) (h common.Hash) {
8284
return h
8385
}
8486

85-
// NewKeccakState returns a new keccak state hasher.
86-
// For now, we fallback to the original implementation for the stateful interface.
87-
// TODO: Implement a stateful wrapper around zkvm_runtime.Keccak256 if needed.
88-
func NewKeccakState() KeccakState {
89-
return originalcrypto.NewKeccakState()
90-
}
87+
// Keccak512 calculates and returns the Keccak512 hash of the input data.
88+
func Keccak512(data ...[]byte) []byte {
89+
panic("Keccak512 not implemented in ziren mode")
90+
}
91+

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)