github.com/myafeier/go-ethereum@v1.6.8-0.20170719123245-3e0dbe0eaa72/core/vm/contracts.go (about) 1 // Copyright 2014 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package vm 18 19 import ( 20 "crypto/sha256" 21 "errors" 22 "math/big" 23 24 "github.com/ethereum/go-ethereum/common" 25 "github.com/ethereum/go-ethereum/crypto" 26 "github.com/ethereum/go-ethereum/params" 27 "golang.org/x/crypto/ripemd160" 28 ) 29 30 var errBadPrecompileInput = errors.New("bad pre compile input") 31 32 // Precompiled contract is the basic interface for native Go contracts. The implementation 33 // requires a deterministic gas count based on the input size of the Run method of the 34 // contract. 35 type PrecompiledContract interface { 36 RequiredGas(input []byte) uint64 // RequiredPrice calculates the contract gas use 37 Run(input []byte) ([]byte, error) // Run runs the precompiled contract 38 } 39 40 // PrecompiledContracts contains the default set of ethereum contracts 41 var PrecompiledContracts = map[common.Address]PrecompiledContract{ 42 common.BytesToAddress([]byte{1}): &ecrecover{}, 43 common.BytesToAddress([]byte{2}): &sha256hash{}, 44 common.BytesToAddress([]byte{3}): &ripemd160hash{}, 45 common.BytesToAddress([]byte{4}): &dataCopy{}, 46 } 47 48 // RunPrecompile runs and evaluate the output of a precompiled contract defined in contracts.go 49 func RunPrecompiledContract(p PrecompiledContract, input []byte, contract *Contract) (ret []byte, err error) { 50 gas := p.RequiredGas(input) 51 if contract.UseGas(gas) { 52 return p.Run(input) 53 } else { 54 return nil, ErrOutOfGas 55 } 56 } 57 58 // ECRECOVER implemented as a native contract 59 type ecrecover struct{} 60 61 func (c *ecrecover) RequiredGas(input []byte) uint64 { 62 return params.EcrecoverGas 63 } 64 65 func (c *ecrecover) Run(in []byte) ([]byte, error) { 66 const ecRecoverInputLength = 128 67 68 in = common.RightPadBytes(in, ecRecoverInputLength) 69 // "in" is (hash, v, r, s), each 32 bytes 70 // but for ecrecover we want (r, s, v) 71 72 r := new(big.Int).SetBytes(in[64:96]) 73 s := new(big.Int).SetBytes(in[96:128]) 74 v := in[63] - 27 75 76 // tighter sig s values in homestead only apply to tx sigs 77 if !allZero(in[32:63]) || !crypto.ValidateSignatureValues(v, r, s, false) { 78 return nil, nil 79 } 80 // v needs to be at the end for libsecp256k1 81 pubKey, err := crypto.Ecrecover(in[:32], append(in[64:128], v)) 82 // make sure the public key is a valid one 83 if err != nil { 84 return nil, nil 85 } 86 87 // the first byte of pubkey is bitcoin heritage 88 return common.LeftPadBytes(crypto.Keccak256(pubKey[1:])[12:], 32), nil 89 } 90 91 // SHA256 implemented as a native contract 92 type sha256hash struct{} 93 94 // RequiredGas returns the gas required to execute the pre-compiled contract. 95 // 96 // This method does not require any overflow checking as the input size gas costs 97 // required for anything significant is so high it's impossible to pay for. 98 func (c *sha256hash) RequiredGas(input []byte) uint64 { 99 return uint64(len(input)+31)/32*params.Sha256WordGas + params.Sha256Gas 100 } 101 func (c *sha256hash) Run(in []byte) ([]byte, error) { 102 h := sha256.Sum256(in) 103 return h[:], nil 104 } 105 106 // RIPMED160 implemented as a native contract 107 type ripemd160hash struct{} 108 109 // RequiredGas returns the gas required to execute the pre-compiled contract. 110 // 111 // This method does not require any overflow checking as the input size gas costs 112 // required for anything significant is so high it's impossible to pay for. 113 func (c *ripemd160hash) RequiredGas(input []byte) uint64 { 114 return uint64(len(input)+31)/32*params.Ripemd160WordGas + params.Ripemd160Gas 115 } 116 func (c *ripemd160hash) Run(in []byte) ([]byte, error) { 117 ripemd := ripemd160.New() 118 ripemd.Write(in) 119 return common.LeftPadBytes(ripemd.Sum(nil), 32), nil 120 } 121 122 // data copy implemented as a native contract 123 type dataCopy struct{} 124 125 // RequiredGas returns the gas required to execute the pre-compiled contract. 126 // 127 // This method does not require any overflow checking as the input size gas costs 128 // required for anything significant is so high it's impossible to pay for. 129 func (c *dataCopy) RequiredGas(input []byte) uint64 { 130 return uint64(len(input)+31)/32*params.IdentityWordGas + params.IdentityGas 131 } 132 func (c *dataCopy) Run(in []byte) ([]byte, error) { 133 return in, nil 134 }