Skip to content

Commit 7afa904

Browse files
committed
factor some code in
1 parent 561495d commit 7afa904

File tree

3 files changed

+19
-55
lines changed

3 files changed

+19
-55
lines changed

crypto/crypto.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"encoding/hex"
2525
"errors"
2626
"fmt"
27+
"hash"
2728
"io"
2829
"math/big"
2930
"os"
@@ -42,6 +43,22 @@ const RecoveryIDOffset = 64
4243
// DigestLength sets the signature digest exact length
4344
const DigestLength = 32
4445

46+
// KeccakState wraps sha3.state. In addition to the usual hash methods, it also supports
47+
// Read to get a variable amount of data from the hash state. Read is faster than Sum
48+
// because it doesn't copy the internal state, but also modifies the internal state.
49+
type KeccakState interface {
50+
hash.Hash
51+
Read([]byte) (int, error)
52+
}
53+
54+
// HashData hashes the provided data using the KeccakState and returns a 32 byte hash
55+
func HashData(kh KeccakState, data []byte) (h common.Hash) {
56+
kh.Reset()
57+
kh.Write(data)
58+
kh.Read(h[:])
59+
return h
60+
}
61+
4562
var (
4663
secp256k1N = S256().Params().N
4764
secp256k1halfN = new(big.Int).Div(secp256k1N, big.NewInt(2))

crypto/keccak.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,12 @@
1919
package crypto
2020

2121
import (
22-
"hash"
2322
"sync"
2423

2524
"github.com/ethereum/go-ethereum/common"
2625
"golang.org/x/crypto/sha3"
2726
)
2827

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-
3728
// NewKeccakState creates a new KeccakState
3829
func NewKeccakState() KeccakState {
3930
return sha3.NewLegacyKeccak256().(KeccakState)
@@ -45,14 +36,6 @@ var hasherPool = sync.Pool{
4536
},
4637
}
4738

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-
5639
// Keccak256 calculates and returns the Keccak256 hash of the input data.
5740
func Keccak256(data ...[]byte) []byte {
5841
b := make([]byte, 32)
@@ -78,12 +61,3 @@ func Keccak256Hash(data ...[]byte) (h common.Hash) {
7861
hasherPool.Put(d)
7962
return h
8063
}
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-
}

crypto/keccak_ziren.go

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,18 @@
1919
package crypto
2020

2121
import (
22-
"hash"
23-
2422
"github.com/ProjectZKM/Ziren/crates/go-runtime/zkvm_runtime"
2523
"github.com/ethereum/go-ethereum/common"
2624
"golang.org/x/crypto/sha3"
2725
)
2826

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-
3727
// NewKeccakState creates a new KeccakState
3828
// For now, we fallback to the original implementation for the stateful interface.
3929
// TODO: Implement a stateful wrapper around zkvm_runtime.Keccak256 if needed.
4030
func NewKeccakState() KeccakState {
4131
return sha3.NewLegacyKeccak256().(KeccakState)
4232
}
4333

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-
}
52-
5334
// Keccak256 calculates and returns the Keccak256 hash using the Ziren zkvm_runtime implementation.
5435
func Keccak256(data ...[]byte) []byte {
5536
// For multiple data chunks, concatenate them
@@ -78,14 +59,6 @@ func Keccak256(data ...[]byte) []byte {
7859
}
7960

8061
// Keccak256Hash calculates and returns the Keccak256 hash as a Hash using the Ziren zkvm_runtime implementation.
81-
func Keccak256Hash(data ...[]byte) (h common.Hash) {
82-
hash := Keccak256(data...)
83-
copy(h[:], hash)
84-
return h
85-
}
86-
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")
62+
func Keccak256Hash(data ...[]byte) common.Hash {
63+
return common.Hash(Keccak256(data...))
9064
}
91-

0 commit comments

Comments
 (0)