factor some code in

This commit is contained in:
Guillaume Ballet 2025-10-15 11:38:40 +02:00
parent 561495d39e
commit cad8721e32
No known key found for this signature in database
3 changed files with 19 additions and 55 deletions

View file

@ -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})

View file

@ -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)
}

View file

@ -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")
}