mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-27 15:16:43 +00:00
crypto, core/vm: remove wrappers for sha256, ripemd160
This commit is contained in:
parent
34a257546c
commit
d527e00902
3 changed files with 20 additions and 50 deletions
|
|
@ -17,11 +17,14 @@
|
||||||
package vm
|
package vm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/logger"
|
"github.com/ethereum/go-ethereum/logger"
|
||||||
"github.com/ethereum/go-ethereum/logger/glog"
|
"github.com/ethereum/go-ethereum/logger/glog"
|
||||||
"github.com/ethereum/go-ethereum/params"
|
"github.com/ethereum/go-ethereum/params"
|
||||||
|
"golang.org/x/crypto/ripemd160"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Precompiled contract is the basic interface for native Go contracts. The implementation
|
// Precompiled contract is the basic interface for native Go contracts. The implementation
|
||||||
|
|
@ -35,8 +38,8 @@ type PrecompiledContract interface {
|
||||||
// Precompiled contains the default set of ethereum contracts
|
// Precompiled contains the default set of ethereum contracts
|
||||||
var PrecompiledContracts = map[common.Address]PrecompiledContract{
|
var PrecompiledContracts = map[common.Address]PrecompiledContract{
|
||||||
common.BytesToAddress([]byte{1}): &ecrecover{},
|
common.BytesToAddress([]byte{1}): &ecrecover{},
|
||||||
common.BytesToAddress([]byte{2}): &sha256{},
|
common.BytesToAddress([]byte{2}): &sha256hash{},
|
||||||
common.BytesToAddress([]byte{3}): &ripemd160{},
|
common.BytesToAddress([]byte{3}): &ripemd160hash{},
|
||||||
common.BytesToAddress([]byte{4}): &dataCopy{},
|
common.BytesToAddress([]byte{4}): &dataCopy{},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -88,31 +91,34 @@ func (c *ecrecover) Run(in []byte) []byte {
|
||||||
}
|
}
|
||||||
|
|
||||||
// SHA256 implemented as a native contract
|
// SHA256 implemented as a native contract
|
||||||
type sha256 struct{}
|
type sha256hash struct{}
|
||||||
|
|
||||||
// RequiredGas returns the gas required to execute the pre-compiled contract.
|
// RequiredGas returns the gas required to execute the pre-compiled contract.
|
||||||
//
|
//
|
||||||
// This method does not require any overflow checking as the input size gas costs
|
// This method does not require any overflow checking as the input size gas costs
|
||||||
// required for anything significant is so high it's impossible to pay for.
|
// required for anything significant is so high it's impossible to pay for.
|
||||||
func (c *sha256) RequiredGas(inputSize int) uint64 {
|
func (c *sha256hash) RequiredGas(inputSize int) uint64 {
|
||||||
return uint64(inputSize+31)/32*params.Sha256WordGas + params.Sha256Gas
|
return uint64(inputSize+31)/32*params.Sha256WordGas + params.Sha256Gas
|
||||||
}
|
}
|
||||||
func (c *sha256) Run(in []byte) []byte {
|
func (c *sha256hash) Run(in []byte) []byte {
|
||||||
return crypto.Sha256(in)
|
h := sha256.Sum256(in)
|
||||||
|
return h[:]
|
||||||
}
|
}
|
||||||
|
|
||||||
// RIPMED160 implemented as a native contract
|
// RIPMED160 implemented as a native contract
|
||||||
type ripemd160 struct{}
|
type ripemd160hash struct{}
|
||||||
|
|
||||||
// RequiredGas returns the gas required to execute the pre-compiled contract.
|
// RequiredGas returns the gas required to execute the pre-compiled contract.
|
||||||
//
|
//
|
||||||
// This method does not require any overflow checking as the input size gas costs
|
// This method does not require any overflow checking as the input size gas costs
|
||||||
// required for anything significant is so high it's impossible to pay for.
|
// required for anything significant is so high it's impossible to pay for.
|
||||||
func (c *ripemd160) RequiredGas(inputSize int) uint64 {
|
func (c *ripemd160hash) RequiredGas(inputSize int) uint64 {
|
||||||
return uint64(inputSize+31)/32*params.Ripemd160WordGas + params.Ripemd160Gas
|
return uint64(inputSize+31)/32*params.Ripemd160WordGas + params.Ripemd160Gas
|
||||||
}
|
}
|
||||||
func (c *ripemd160) Run(in []byte) []byte {
|
func (c *ripemd160hash) Run(in []byte) []byte {
|
||||||
return common.LeftPadBytes(crypto.Ripemd160(in), 32)
|
ripemd := ripemd160.New()
|
||||||
|
ripemd.Write(in)
|
||||||
|
return common.LeftPadBytes(ripemd.Sum(nil), 32)
|
||||||
}
|
}
|
||||||
|
|
||||||
// data copy implemented as a native contract
|
// data copy implemented as a native contract
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,6 @@ import (
|
||||||
"crypto/ecdsa"
|
"crypto/ecdsa"
|
||||||
"crypto/elliptic"
|
"crypto/elliptic"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/sha256"
|
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
|
@ -31,7 +30,6 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/crypto/sha3"
|
"github.com/ethereum/go-ethereum/crypto/sha3"
|
||||||
"github.com/ethereum/go-ethereum/rlp"
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
"golang.org/x/crypto/ripemd160"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -57,7 +55,6 @@ func Keccak256Hash(data ...[]byte) (h common.Hash) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deprecated: For backward compatibility as other packages depend on these
|
// Deprecated: For backward compatibility as other packages depend on these
|
||||||
func Sha3(data ...[]byte) []byte { return Keccak256(data...) }
|
|
||||||
func Sha3Hash(data ...[]byte) common.Hash { return Keccak256Hash(data...) }
|
func Sha3Hash(data ...[]byte) common.Hash { return Keccak256Hash(data...) }
|
||||||
|
|
||||||
// Creates an ethereum address given the bytes and the nonce
|
// Creates an ethereum address given the bytes and the nonce
|
||||||
|
|
@ -66,19 +63,6 @@ func CreateAddress(b common.Address, nonce uint64) common.Address {
|
||||||
return common.BytesToAddress(Keccak256(data)[12:])
|
return common.BytesToAddress(Keccak256(data)[12:])
|
||||||
}
|
}
|
||||||
|
|
||||||
func Sha256(data []byte) []byte {
|
|
||||||
hash := sha256.Sum256(data)
|
|
||||||
|
|
||||||
return hash[:]
|
|
||||||
}
|
|
||||||
|
|
||||||
func Ripemd160(data []byte) []byte {
|
|
||||||
ripemd := ripemd160.New()
|
|
||||||
ripemd.Write(data)
|
|
||||||
|
|
||||||
return ripemd.Sum(nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ToECDSA creates a private key with the given D value.
|
// ToECDSA creates a private key with the given D value.
|
||||||
func ToECDSA(prv []byte) *ecdsa.PrivateKey {
|
func ToECDSA(prv []byte) *ecdsa.PrivateKey {
|
||||||
if len(prv) == 0 {
|
if len(prv) == 0 {
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/crypto/secp256k1"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var testAddrHex = "970e8128ab834e8eac17ab8e3812f010678cf791"
|
var testAddrHex = "970e8128ab834e8eac17ab8e3812f010678cf791"
|
||||||
|
|
@ -37,30 +36,12 @@ var testPrivHex = "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232
|
||||||
// These tests are sanity checks.
|
// These tests are sanity checks.
|
||||||
// They should ensure that we don't e.g. use Sha3-224 instead of Sha3-256
|
// They should ensure that we don't e.g. use Sha3-224 instead of Sha3-256
|
||||||
// and that the sha3 library uses keccak-f permutation.
|
// and that the sha3 library uses keccak-f permutation.
|
||||||
func TestSha3(t *testing.T) {
|
|
||||||
msg := []byte("abc")
|
|
||||||
exp, _ := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45")
|
|
||||||
checkhash(t, "Sha3-256", func(in []byte) []byte { return Keccak256(in) }, msg, exp)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSha3Hash(t *testing.T) {
|
func TestSha3Hash(t *testing.T) {
|
||||||
msg := []byte("abc")
|
msg := []byte("abc")
|
||||||
exp, _ := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45")
|
exp, _ := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45")
|
||||||
checkhash(t, "Sha3-256-array", func(in []byte) []byte { h := Keccak256Hash(in); return h[:] }, msg, exp)
|
checkhash(t, "Sha3-256-array", func(in []byte) []byte { h := Keccak256Hash(in); return h[:] }, msg, exp)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSha256(t *testing.T) {
|
|
||||||
msg := []byte("abc")
|
|
||||||
exp, _ := hex.DecodeString("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")
|
|
||||||
checkhash(t, "Sha256", Sha256, msg, exp)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRipemd160(t *testing.T) {
|
|
||||||
msg := []byte("abc")
|
|
||||||
exp, _ := hex.DecodeString("8eb208f7e05d987a9b044a8e98c6b087f15a0bfc")
|
|
||||||
checkhash(t, "Ripemd160", Ripemd160, msg, exp)
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkSha3(b *testing.B) {
|
func BenchmarkSha3(b *testing.B) {
|
||||||
a := []byte("hello world")
|
a := []byte("hello world")
|
||||||
amount := 1000000
|
amount := 1000000
|
||||||
|
|
@ -225,14 +206,13 @@ func checkAddr(t *testing.T, addr0, addr1 common.Address) {
|
||||||
func TestPythonIntegration(t *testing.T) {
|
func TestPythonIntegration(t *testing.T) {
|
||||||
kh := "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032"
|
kh := "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032"
|
||||||
k0, _ := HexToECDSA(kh)
|
k0, _ := HexToECDSA(kh)
|
||||||
k1 := FromECDSA(k0)
|
|
||||||
|
|
||||||
msg0 := Keccak256([]byte("foo"))
|
msg0 := Keccak256([]byte("foo"))
|
||||||
sig0, _ := secp256k1.Sign(msg0, k1)
|
sig0, _ := Sign(msg0, k0)
|
||||||
|
|
||||||
msg1 := common.FromHex("00000000000000000000000000000000")
|
msg1 := common.FromHex("00000000000000000000000000000000")
|
||||||
sig1, _ := secp256k1.Sign(msg0, k1)
|
sig1, _ := Sign(msg0, k0)
|
||||||
|
|
||||||
fmt.Printf("msg: %x, privkey: %x sig: %x\n", msg0, k1, sig0)
|
t.Logf("msg: %x, privkey: %s sig: %x\n", msg0, kh, sig0)
|
||||||
fmt.Printf("msg: %x, privkey: %x sig: %x\n", msg1, k1, sig1)
|
t.Logf("msg: %x, privkey: %s sig: %x\n", msg1, kh, sig1)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue