mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-23 05:06:43 +00:00
factor some code in
This commit is contained in:
parent
561495d39e
commit
cad8721e32
3 changed files with 19 additions and 55 deletions
|
|
@ -24,6 +24,7 @@ import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"hash"
|
||||||
"io"
|
"io"
|
||||||
"math/big"
|
"math/big"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -58,6 +59,22 @@ type EllipticCurve interface {
|
||||||
Unmarshal(data []byte) (x, y *big.Int)
|
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
|
// CreateAddress creates an ethereum address given the bytes and the nonce
|
||||||
func CreateAddress(b common.Address, nonce uint64) common.Address {
|
func CreateAddress(b common.Address, nonce uint64) common.Address {
|
||||||
data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})
|
data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})
|
||||||
|
|
|
||||||
|
|
@ -19,21 +19,12 @@
|
||||||
package crypto
|
package crypto
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"hash"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"golang.org/x/crypto/sha3"
|
"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
|
// NewKeccakState creates a new KeccakState
|
||||||
func NewKeccakState() KeccakState {
|
func NewKeccakState() KeccakState {
|
||||||
return sha3.NewLegacyKeccak256().(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.
|
// Keccak256 calculates and returns the Keccak256 hash of the input data.
|
||||||
func Keccak256(data ...[]byte) []byte {
|
func Keccak256(data ...[]byte) []byte {
|
||||||
b := make([]byte, 32)
|
b := make([]byte, 32)
|
||||||
|
|
@ -78,12 +61,3 @@ func Keccak256Hash(data ...[]byte) (h common.Hash) {
|
||||||
hasherPool.Put(d)
|
hasherPool.Put(d)
|
||||||
return h
|
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)
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -19,21 +19,11 @@
|
||||||
package crypto
|
package crypto
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"hash"
|
|
||||||
|
|
||||||
"github.com/ProjectZKM/Ziren/crates/go-runtime/zkvm_runtime"
|
"github.com/ProjectZKM/Ziren/crates/go-runtime/zkvm_runtime"
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"golang.org/x/crypto/sha3"
|
"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
|
// NewKeccakState creates a new KeccakState
|
||||||
// For now, we fallback to the original implementation for the stateful interface.
|
// For now, we fallback to the original implementation for the stateful interface.
|
||||||
// TODO: Implement a stateful wrapper around zkvm_runtime.Keccak256 if needed.
|
// TODO: Implement a stateful wrapper around zkvm_runtime.Keccak256 if needed.
|
||||||
|
|
@ -41,15 +31,6 @@ func NewKeccakState() KeccakState {
|
||||||
return sha3.NewLegacyKeccak256().(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.
|
// Keccak256 calculates and returns the Keccak256 hash using the Ziren zkvm_runtime implementation.
|
||||||
func Keccak256(data ...[]byte) []byte {
|
func Keccak256(data ...[]byte) []byte {
|
||||||
// For multiple data chunks, concatenate them
|
// 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.
|
// Keccak256Hash calculates and returns the Keccak256 hash as a Hash using the Ziren zkvm_runtime implementation.
|
||||||
func Keccak256Hash(data ...[]byte) (h common.Hash) {
|
func Keccak256Hash(data ...[]byte) common.Hash {
|
||||||
hash := Keccak256(data...)
|
return common.Hash(Keccak256(data...))
|
||||||
copy(h[:], hash)
|
|
||||||
return h
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keccak512 calculates and returns the Keccak512 hash of the input data.
|
|
||||||
func Keccak512(data ...[]byte) []byte {
|
|
||||||
panic("Keccak512 not implemented in ziren mode")
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue