github.com/avence12/go-ethereum@v1.5.10-0.20170320123548-1dfd65f6d047/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  	"math/big"
    22  
    23  	"github.com/ethereum/go-ethereum/common"
    24  	"github.com/ethereum/go-ethereum/crypto"
    25  	"github.com/ethereum/go-ethereum/log"
    26  	"github.com/ethereum/go-ethereum/params"
    27  	"golang.org/x/crypto/ripemd160"
    28  )
    29  
    30  // Precompiled contract is the basic interface for native Go contracts. The implementation
    31  // requires a deterministic gas count based on the input size of the Run method of the
    32  // contract.
    33  type PrecompiledContract interface {
    34  	RequiredGas(inputSize int) uint64 // RequiredPrice calculates the contract gas use
    35  	Run(input []byte) []byte          // Run runs the precompiled contract
    36  }
    37  
    38  // Precompiled contains the default set of ethereum contracts
    39  var PrecompiledContracts = map[common.Address]PrecompiledContract{
    40  	common.BytesToAddress([]byte{1}): &ecrecover{},
    41  	common.BytesToAddress([]byte{2}): &sha256hash{},
    42  	common.BytesToAddress([]byte{3}): &ripemd160hash{},
    43  	common.BytesToAddress([]byte{4}): &dataCopy{},
    44  }
    45  
    46  // RunPrecompile runs and evaluate the output of a precompiled contract defined in contracts.go
    47  func RunPrecompiledContract(p PrecompiledContract, input []byte, contract *Contract) (ret []byte, err error) {
    48  	gas := p.RequiredGas(len(input))
    49  	if contract.UseGas(gas) {
    50  		ret = p.Run(input)
    51  
    52  		return ret, nil
    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(inputSize int) uint64 {
    62  	return params.EcrecoverGas
    63  }
    64  
    65  func (c *ecrecover) Run(in []byte) []byte {
    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  		log.Trace("ECRECOVER error: v, r or s value invalid")
    79  		return nil
    80  	}
    81  	// v needs to be at the end for libsecp256k1
    82  	pubKey, err := crypto.Ecrecover(in[:32], append(in[64:128], v))
    83  	// make sure the public key is a valid one
    84  	if err != nil {
    85  		log.Trace("ECRECOVER failed", "err", err)
    86  		return nil
    87  	}
    88  
    89  	// the first byte of pubkey is bitcoin heritage
    90  	return common.LeftPadBytes(crypto.Keccak256(pubKey[1:])[12:], 32)
    91  }
    92  
    93  // SHA256 implemented as a native contract
    94  type sha256hash struct{}
    95  
    96  // RequiredGas returns the gas required to execute the pre-compiled contract.
    97  //
    98  // This method does not require any overflow checking as the input size gas costs
    99  // required for anything significant is so high it's impossible to pay for.
   100  func (c *sha256hash) RequiredGas(inputSize int) uint64 {
   101  	return uint64(inputSize+31)/32*params.Sha256WordGas + params.Sha256Gas
   102  }
   103  func (c *sha256hash) Run(in []byte) []byte {
   104  	h := sha256.Sum256(in)
   105  	return h[:]
   106  }
   107  
   108  // RIPMED160 implemented as a native contract
   109  type ripemd160hash struct{}
   110  
   111  // RequiredGas returns the gas required to execute the pre-compiled contract.
   112  //
   113  // This method does not require any overflow checking as the input size gas costs
   114  // required for anything significant is so high it's impossible to pay for.
   115  func (c *ripemd160hash) RequiredGas(inputSize int) uint64 {
   116  	return uint64(inputSize+31)/32*params.Ripemd160WordGas + params.Ripemd160Gas
   117  }
   118  func (c *ripemd160hash) Run(in []byte) []byte {
   119  	ripemd := ripemd160.New()
   120  	ripemd.Write(in)
   121  	return common.LeftPadBytes(ripemd.Sum(nil), 32)
   122  }
   123  
   124  // data copy implemented as a native contract
   125  type dataCopy struct{}
   126  
   127  // RequiredGas returns the gas required to execute the pre-compiled contract.
   128  //
   129  // This method does not require any overflow checking as the input size gas costs
   130  // required for anything significant is so high it's impossible to pay for.
   131  func (c *dataCopy) RequiredGas(inputSize int) uint64 {
   132  	return uint64(inputSize+31)/32*params.IdentityWordGas + params.IdentityGas
   133  }
   134  func (c *dataCopy) Run(in []byte) []byte {
   135  	return in
   136  }