From d527e00902121add4d5795c9c5bca43919fa84f3 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Thu, 16 Feb 2017 00:13:18 +0100 Subject: [PATCH] crypto, core/vm: remove wrappers for sha256, ripemd160 --- core/vm/contracts.go | 26 ++++++++++++++++---------- crypto/crypto.go | 16 ---------------- crypto/crypto_test.go | 28 ++++------------------------ 3 files changed, 20 insertions(+), 50 deletions(-) diff --git a/core/vm/contracts.go b/core/vm/contracts.go index 593b6ca55c..1447c2cada 100644 --- a/core/vm/contracts.go +++ b/core/vm/contracts.go @@ -17,11 +17,14 @@ package vm import ( + "crypto/sha256" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/params" + "golang.org/x/crypto/ripemd160" ) // 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 var PrecompiledContracts = map[common.Address]PrecompiledContract{ common.BytesToAddress([]byte{1}): &ecrecover{}, - common.BytesToAddress([]byte{2}): &sha256{}, - common.BytesToAddress([]byte{3}): &ripemd160{}, + common.BytesToAddress([]byte{2}): &sha256hash{}, + common.BytesToAddress([]byte{3}): &ripemd160hash{}, common.BytesToAddress([]byte{4}): &dataCopy{}, } @@ -88,31 +91,34 @@ func (c *ecrecover) Run(in []byte) []byte { } // SHA256 implemented as a native contract -type sha256 struct{} +type sha256hash struct{} // 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 // 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 } -func (c *sha256) Run(in []byte) []byte { - return crypto.Sha256(in) +func (c *sha256hash) Run(in []byte) []byte { + h := sha256.Sum256(in) + return h[:] } // RIPMED160 implemented as a native contract -type ripemd160 struct{} +type ripemd160hash struct{} // 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 // 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 } -func (c *ripemd160) Run(in []byte) []byte { - return common.LeftPadBytes(crypto.Ripemd160(in), 32) +func (c *ripemd160hash) Run(in []byte) []byte { + ripemd := ripemd160.New() + ripemd.Write(in) + return common.LeftPadBytes(ripemd.Sum(nil), 32) } // data copy implemented as a native contract diff --git a/crypto/crypto.go b/crypto/crypto.go index 3a6896951b..9d67d82e1d 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -20,7 +20,6 @@ import ( "crypto/ecdsa" "crypto/elliptic" "crypto/rand" - "crypto/sha256" "encoding/hex" "errors" "io" @@ -31,7 +30,6 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto/sha3" "github.com/ethereum/go-ethereum/rlp" - "golang.org/x/crypto/ripemd160" ) var ( @@ -57,7 +55,6 @@ func Keccak256Hash(data ...[]byte) (h common.Hash) { } // 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...) } // 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:]) } -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. func ToECDSA(prv []byte) *ecdsa.PrivateKey { if len(prv) == 0 { diff --git a/crypto/crypto_test.go b/crypto/crypto_test.go index 73425a6b62..e518ac22d8 100644 --- a/crypto/crypto_test.go +++ b/crypto/crypto_test.go @@ -28,7 +28,6 @@ import ( "time" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/crypto/secp256k1" ) var testAddrHex = "970e8128ab834e8eac17ab8e3812f010678cf791" @@ -37,30 +36,12 @@ var testPrivHex = "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232 // These tests are sanity checks. // 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. -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) { msg := []byte("abc") exp, _ := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45") 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) { a := []byte("hello world") amount := 1000000 @@ -225,14 +206,13 @@ func checkAddr(t *testing.T, addr0, addr1 common.Address) { func TestPythonIntegration(t *testing.T) { kh := "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032" k0, _ := HexToECDSA(kh) - k1 := FromECDSA(k0) msg0 := Keccak256([]byte("foo")) - sig0, _ := secp256k1.Sign(msg0, k1) + sig0, _ := Sign(msg0, k0) 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) - fmt.Printf("msg: %x, privkey: %x sig: %x\n", msg1, k1, sig1) + t.Logf("msg: %x, privkey: %s sig: %x\n", msg0, kh, sig0) + t.Logf("msg: %x, privkey: %s sig: %x\n", msg1, kh, sig1) }