Skip to content

Commit 4bea991

Browse files
committed
use my keccak PR
1 parent bca9ab8 commit 4bea991

File tree

3 files changed

+35
-223
lines changed

3 files changed

+35
-223
lines changed

cmd/keeper/crypto_ziren/crypto.go

Lines changed: 20 additions & 211 deletions
Original file line numberDiff line numberDiff line change
@@ -14,85 +14,17 @@
1414
// You should have received a copy of the GNU Lesser General Public License
1515
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
1616

17+
//go:build ziren
18+
// +build ziren
19+
1720
package crypto
1821

1922
import (
20-
"errors"
21-
"syscall"
22-
"unsafe"
23-
23+
"github.com/ProjectZKM/Ziren/crates/go-runtime/zkvm_runtime"
2424
"github.com/ethereum/go-ethereum/common"
2525
originalcrypto "github.com/ethereum/go-ethereum/crypto"
2626
)
2727

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-
9628
// Re-export everything from original crypto package except the parts we're overriding
9729
var (
9830
S256 = originalcrypto.S256
@@ -116,166 +48,43 @@ type (
11648
KeccakState = originalcrypto.KeccakState
11749
)
11850

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.
51+
// Keccak256 calculates and returns the Keccak256 hash using the Ziren zkvm_runtime implementation.
24852
func Keccak256(data ...[]byte) []byte {
24953
// For multiple data chunks, concatenate them
25054
if len(data) == 0 {
251-
return zirenKeccak256(nil)
55+
result := zkvm_runtime.Keccak256(nil)
56+
return result[:]
25257
}
25358
if len(data) == 1 {
254-
return zirenKeccak256(data[0])
59+
result := zkvm_runtime.Keccak256(data[0])
60+
return result[:]
25561
}
256-
62+
25763
// Concatenate multiple data chunks
25864
var totalLen int
25965
for _, d := range data {
26066
totalLen += len(d)
26167
}
262-
68+
26369
combined := make([]byte, 0, totalLen)
26470
for _, d := range data {
26571
combined = append(combined, d...)
26672
}
267-
268-
return zirenKeccak256(combined)
73+
74+
result := zkvm_runtime.Keccak256(combined)
75+
return result[:]
26976
}
27077

271-
// Keccak256Hash calculates and returns the Keccak256 hash as a Hash using the ziren platform precompile.
78+
// Keccak256Hash calculates and returns the Keccak256 hash as a Hash using the Ziren zkvm_runtime implementation.
27279
func Keccak256Hash(data ...[]byte) (h common.Hash) {
27380
hash := Keccak256(data...)
27481
copy(h[:], hash)
27582
return h
27683
}
27784

278-
// NewKeccakState returns a new keccak state hasher using the ziren platform precompile.
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.
27988
func NewKeccakState() KeccakState {
280-
return &zirenKeccakState{}
281-
}
89+
return originalcrypto.NewKeccakState()
90+
}

cmd/keeper/go.mod

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ require (
1313
github.com/bits-and-blooms/bitset v1.20.0 // indirect
1414
github.com/cespare/xxhash/v2 v2.3.0 // indirect
1515
github.com/consensys/gnark-crypto v0.18.0 // indirect
16-
github.com/crate-crypto/go-eth-kzg v1.3.0 // indirect
16+
github.com/crate-crypto/go-eth-kzg v1.4.0 // indirect
1717
github.com/crate-crypto/go-ipa v0.0.0-20240724233137-53bbb0ceb27a // indirect
1818
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
1919
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
2020
github.com/emicklei/dot v1.6.2 // indirect
21-
github.com/ethereum/c-kzg-4844/v2 v2.1.0 // indirect
21+
github.com/ethereum/c-kzg-4844/v2 v2.1.3 // indirect
22+
github.com/ethereum/go-bigmodexpfix v0.0.0-20250911101455-f9e208c548ab // indirect
2223
github.com/ethereum/go-verkle v0.2.2 // indirect
2324
github.com/ferranbt/fastssz v0.1.4 // indirect
2425
github.com/go-ole/go-ole v1.3.0 // indirect
@@ -33,12 +34,12 @@ require (
3334
github.com/olekukonko/tablewriter v0.0.5 // indirect
3435
github.com/rivo/uniseg v0.2.0 // indirect
3536
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
36-
github.com/supranational/blst v0.3.14 // indirect
37+
github.com/supranational/blst v0.3.16-0.20250831170142-f48500c1fdbe // indirect
3738
github.com/tklauser/go-sysconf v0.3.12 // indirect
3839
github.com/tklauser/numcpus v0.6.1 // indirect
3940
golang.org/x/crypto v0.36.0 // indirect
4041
golang.org/x/sync v0.12.0 // indirect
41-
golang.org/x/sys v0.31.0 // indirect
42+
golang.org/x/sys v0.36.0 // indirect
4243
gopkg.in/yaml.v2 v2.4.0 // indirect
4344
)
4445

cmd/keeper/go.sum

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 h1:zuQyyAK
2727
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06/go.mod h1:7nc4anLGjupUW/PeY5qiNYsdNXj7zopG+eqsS7To5IQ=
2828
github.com/consensys/gnark-crypto v0.18.0 h1:vIye/FqI50VeAr0B3dx+YjeIvmc3LWz4yEfbWBpTUf0=
2929
github.com/consensys/gnark-crypto v0.18.0/go.mod h1:L3mXGFTe1ZN+RSJ+CLjUt9x7PNdx8ubaYfDROyp2Z8c=
30-
github.com/crate-crypto/go-eth-kzg v1.3.0 h1:05GrhASN9kDAidaFJOda6A4BEvgvuXbazXg/0E3OOdI=
31-
github.com/crate-crypto/go-eth-kzg v1.3.0/go.mod h1:J9/u5sWfznSObptgfa92Jq8rTswn6ahQWEuiLHOjCUI=
30+
github.com/crate-crypto/go-eth-kzg v1.4.0 h1:WzDGjHk4gFg6YzV0rJOAsTK4z3Qkz5jd4RE3DAvPFkg=
31+
github.com/crate-crypto/go-eth-kzg v1.4.0/go.mod h1:J9/u5sWfznSObptgfa92Jq8rTswn6ahQWEuiLHOjCUI=
3232
github.com/crate-crypto/go-ipa v0.0.0-20240724233137-53bbb0ceb27a h1:W8mUrRp6NOVl3J+MYp5kPMoUZPp7aOYHtaua31lwRHg=
3333
github.com/crate-crypto/go-ipa v0.0.0-20240724233137-53bbb0ceb27a/go.mod h1:sTwzHBvIzm2RfVCGNEBZgRyjwK40bVoun3ZnGOCafNM=
3434
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -42,8 +42,10 @@ github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 h1:YLtO71vCjJRCBcrPMtQ9nqBsqpA1
4242
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeCxkaw7y45JueMRL4DIyJDKs=
4343
github.com/emicklei/dot v1.6.2 h1:08GN+DD79cy/tzN6uLCT84+2Wk9u+wvqP+Hkx/dIR8A=
4444
github.com/emicklei/dot v1.6.2/go.mod h1:DeV7GvQtIw4h2u73RKBkkFdvVAz0D9fzeJrgPW6gy/s=
45-
github.com/ethereum/c-kzg-4844/v2 v2.1.0 h1:gQropX9YFBhl3g4HYhwE70zq3IHFRgbbNPw0Shwzf5w=
46-
github.com/ethereum/c-kzg-4844/v2 v2.1.0/go.mod h1:TC48kOKjJKPbN7C++qIgt0TJzZ70QznYR7Ob+WXl57E=
45+
github.com/ethereum/c-kzg-4844/v2 v2.1.3 h1:DQ21UU0VSsuGy8+pcMJHDS0CV1bKmJmxsJYK8l3MiLU=
46+
github.com/ethereum/c-kzg-4844/v2 v2.1.3/go.mod h1:fyNcYI/yAuLWJxf4uzVtS8VDKeoAaRM8G/+ADz/pRdA=
47+
github.com/ethereum/go-bigmodexpfix v0.0.0-20250911101455-f9e208c548ab h1:rvv6MJhy07IMfEKuARQ9TKojGqLVNxQajaXEp/BoqSk=
48+
github.com/ethereum/go-bigmodexpfix v0.0.0-20250911101455-f9e208c548ab/go.mod h1:IuLm4IsPipXKF7CW5Lzf68PIbZ5yl7FFd74l/E0o9A8=
4749
github.com/ethereum/go-verkle v0.2.2 h1:I2W0WjnrFUIzzVPwm8ykY+7pL2d4VhlsePn4j7cnFk8=
4850
github.com/ethereum/go-verkle v0.2.2/go.mod h1:M3b90YRnzqKyyzBEWJGqj8Qff4IDeXnzFw0P9bFw3uk=
4951
github.com/ferranbt/fastssz v0.1.4 h1:OCDB+dYDEQDvAgtAGnTSidK1Pe2tW3nFV40XyMkTeDY=
@@ -114,8 +116,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
114116
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
115117
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
116118
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
117-
github.com/supranational/blst v0.3.14 h1:xNMoHRJOTwMn63ip6qoWJ2Ymgvj7E2b9jY2FAwY+qRo=
118-
github.com/supranational/blst v0.3.14/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw=
119+
github.com/supranational/blst v0.3.16-0.20250831170142-f48500c1fdbe h1:nbdqkIGOGfUAD54q1s2YBcBz/WcsxCO9HUQ4aGV5hUw=
120+
github.com/supranational/blst v0.3.16-0.20250831170142-f48500c1fdbe/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw=
119121
github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU=
120122
github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI=
121123
github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk=
@@ -133,8 +135,8 @@ golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
133135
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
134136
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
135137
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
136-
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
137-
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
138+
golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k=
139+
golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
138140
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
139141
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
140142
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=

0 commit comments

Comments
 (0)