1515// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
1616
1717//go:build ziren
18- // +build ziren
1918
2019package crypto
2120
2221import (
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.
5254func 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+
0 commit comments