diff options
author | Felix Lange <fjl@users.noreply.github.com> | 2017-02-18 16:24:12 +0800 |
---|---|---|
committer | Jeffrey Wilcke <jeffrey@ethereum.org> | 2017-02-18 16:24:12 +0800 |
commit | 9b0af513867fad4aeb3516e4711dd0ea4f5bc90c (patch) | |
tree | b37d808d57873c6aec550431534e26602dfd0475 /core/vm | |
parent | bf21549faa7de6e2b920855468b14856c6f503c4 (diff) | |
download | dexon-9b0af513867fad4aeb3516e4711dd0ea4f5bc90c.tar dexon-9b0af513867fad4aeb3516e4711dd0ea4f5bc90c.tar.gz dexon-9b0af513867fad4aeb3516e4711dd0ea4f5bc90c.tar.bz2 dexon-9b0af513867fad4aeb3516e4711dd0ea4f5bc90c.tar.lz dexon-9b0af513867fad4aeb3516e4711dd0ea4f5bc90c.tar.xz dexon-9b0af513867fad4aeb3516e4711dd0ea4f5bc90c.tar.zst dexon-9b0af513867fad4aeb3516e4711dd0ea4f5bc90c.zip |
crypto: add btcec fallback for sign/recover without cgo (#3680)
* vendor: add github.com/btcsuite/btcd/btcec
* crypto: add btcec fallback for sign/recover without cgo
This commit adds a non-cgo fallback implementation of secp256k1
operations.
* crypto, core/vm: remove wrappers for sha256, ripemd160
Diffstat (limited to 'core/vm')
-rw-r--r-- | core/vm/contracts.go | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/core/vm/contracts.go b/core/vm/contracts.go index 593b6ca55..1447c2cad 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 |