From cad8721e32cebc46b261c1c045f5a1fabb9af57c Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Wed, 15 Oct 2025 11:38:40 +0200 Subject: [PATCH] factor some code in --- crypto/crypto.go | 17 +++++++++++++++++ crypto/keccak.go | 26 -------------------------- crypto/keccak_ziren.go | 31 ++----------------------------- 3 files changed, 19 insertions(+), 55 deletions(-) diff --git a/crypto/crypto.go b/crypto/crypto.go index 6eacf41de9..db6b6ee071 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -24,6 +24,7 @@ import ( "encoding/hex" "errors" "fmt" + "hash" "io" "math/big" "os" @@ -58,6 +59,22 @@ type EllipticCurve interface { Unmarshal(data []byte) (x, y *big.Int) } +// KeccakState wraps sha3.state. In addition to the usual hash methods, it also supports +// Read to get a variable amount of data from the hash state. Read is faster than Sum +// because it doesn't copy the internal state, but also modifies the internal state. +type KeccakState interface { + hash.Hash + Read([]byte) (int, error) +} + +// HashData hashes the provided data using the KeccakState and returns a 32 byte hash +func HashData(kh KeccakState, data []byte) (h common.Hash) { + kh.Reset() + kh.Write(data) + kh.Read(h[:]) + return h +} + // CreateAddress creates an ethereum address given the bytes and the nonce func CreateAddress(b common.Address, nonce uint64) common.Address { data, _ := rlp.EncodeToBytes([]interface{}{b, nonce}) diff --git a/crypto/keccak.go b/crypto/keccak.go index 9eb782d119..0ad79a63c1 100644 --- a/crypto/keccak.go +++ b/crypto/keccak.go @@ -19,21 +19,12 @@ package crypto import ( - "hash" "sync" "github.com/ethereum/go-ethereum/common" "golang.org/x/crypto/sha3" ) -// KeccakState wraps sha3.state. In addition to the usual hash methods, it also supports -// Read to get a variable amount of data from the hash state. Read is faster than Sum -// because it doesn't copy the internal state, but also modifies the internal state. -type KeccakState interface { - hash.Hash - Read([]byte) (int, error) -} - // NewKeccakState creates a new KeccakState func NewKeccakState() KeccakState { return sha3.NewLegacyKeccak256().(KeccakState) @@ -45,14 +36,6 @@ var hasherPool = sync.Pool{ }, } -// HashData hashes the provided data using the KeccakState and returns a 32 byte hash -func HashData(kh KeccakState, data []byte) (h common.Hash) { - kh.Reset() - kh.Write(data) - kh.Read(h[:]) - return h -} - // Keccak256 calculates and returns the Keccak256 hash of the input data. func Keccak256(data ...[]byte) []byte { b := make([]byte, 32) @@ -78,12 +61,3 @@ func Keccak256Hash(data ...[]byte) (h common.Hash) { hasherPool.Put(d) return h } - -// Keccak512 calculates and returns the Keccak512 hash of the input data. -func Keccak512(data ...[]byte) []byte { - d := sha3.NewLegacyKeccak512() - for _, b := range data { - d.Write(b) - } - return d.Sum(nil) -} diff --git a/crypto/keccak_ziren.go b/crypto/keccak_ziren.go index 081e3e634e..033c0ec42c 100644 --- a/crypto/keccak_ziren.go +++ b/crypto/keccak_ziren.go @@ -19,21 +19,11 @@ package crypto import ( - "hash" - "github.com/ProjectZKM/Ziren/crates/go-runtime/zkvm_runtime" "github.com/ethereum/go-ethereum/common" "golang.org/x/crypto/sha3" ) -// KeccakState wraps sha3.state. In addition to the usual hash methods, it also supports -// Read to get a variable amount of data from the hash state. Read is faster than Sum -// because it doesn't copy the internal state, but also modifies the internal state. -type KeccakState interface { - hash.Hash - Read([]byte) (int, error) -} - // NewKeccakState creates a new KeccakState // For now, we fallback to the original implementation for the stateful interface. // TODO: Implement a stateful wrapper around zkvm_runtime.Keccak256 if needed. @@ -41,15 +31,6 @@ func NewKeccakState() KeccakState { return sha3.NewLegacyKeccak256().(KeccakState) } -// HashData hashes the provided data using the KeccakState and returns a 32 byte hash -// For now, we fallback to the original implementation for the stateful interface. -func HashData(kh KeccakState, data []byte) (h common.Hash) { - kh.Reset() - kh.Write(data) - kh.Read(h[:]) - return h -} - // Keccak256 calculates and returns the Keccak256 hash using the Ziren zkvm_runtime implementation. func Keccak256(data ...[]byte) []byte { // For multiple data chunks, concatenate them @@ -78,14 +59,6 @@ func Keccak256(data ...[]byte) []byte { } // Keccak256Hash calculates and returns the Keccak256 hash as a Hash using the Ziren zkvm_runtime implementation. -func Keccak256Hash(data ...[]byte) (h common.Hash) { - hash := Keccak256(data...) - copy(h[:], hash) - return h +func Keccak256Hash(data ...[]byte) common.Hash { + return common.Hash(Keccak256(data...)) } - -// Keccak512 calculates and returns the Keccak512 hash of the input data. -func Keccak512(data ...[]byte) []byte { - panic("Keccak512 not implemented in ziren mode") -} -