github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/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 "math/big" 21 22 "github.com/atheioschain/go-atheios/common" 23 "github.com/atheioschain/go-atheios/crypto" 24 "github.com/atheioschain/go-atheios/logger" 25 "github.com/atheioschain/go-atheios/logger/glog" 26 "github.com/atheioschain/go-atheios/params" 27 ) 28 29 // Precompiled contract is the basic interface for native Go contracts. The implementation 30 // requires a deterministic gas count based on the input size of the Run method of the 31 // contract. 32 type PrecompiledContract interface { 33 RequiredGas(inputSize int) *big.Int // RequiredPrice calculates the contract gas use 34 Run(input []byte) []byte // Run runs the precompiled contract 35 } 36 37 // Precompiled contains the default set of ethereum contracts 38 var PrecompiledContracts = map[common.Address]PrecompiledContract{ 39 common.BytesToAddress([]byte{1}): &ecrecover{}, 40 common.BytesToAddress([]byte{2}): &sha256{}, 41 common.BytesToAddress([]byte{3}): &ripemd160{}, 42 common.BytesToAddress([]byte{4}): &dataCopy{}, 43 } 44 45 // RunPrecompile runs and evaluate the output of a precompiled contract defined in contracts.go 46 func RunPrecompiledContract(p PrecompiledContract, input []byte, contract *Contract) (ret []byte, err error) { 47 gas := p.RequiredGas(len(input)) 48 if contract.UseGas(gas) { 49 ret = p.Run(input) 50 51 return ret, nil 52 } else { 53 return nil, ErrOutOfGas 54 } 55 } 56 57 // ECRECOVER implemented as a native contract 58 type ecrecover struct{} 59 60 func (c *ecrecover) RequiredGas(inputSize int) *big.Int { 61 return params.EcrecoverGas 62 } 63 64 func (c *ecrecover) Run(in []byte) []byte { 65 const ecRecoverInputLength = 128 66 67 in = common.RightPadBytes(in, ecRecoverInputLength) 68 // "in" is (hash, v, r, s), each 32 bytes 69 // but for ecrecover we want (r, s, v) 70 71 r := common.BytesToBig(in[64:96]) 72 s := common.BytesToBig(in[96:128]) 73 v := in[63] - 27 74 75 // tighter sig s values in homestead only apply to tx sigs 76 if common.Bytes2Big(in[32:63]).BitLen() > 0 || !crypto.ValidateSignatureValues(v, r, s, false) { 77 glog.V(logger.Detail).Infof("ECRECOVER error: v, r or s value invalid") 78 return 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 glog.V(logger.Detail).Infoln("ECRECOVER error: ", err) 85 return nil 86 } 87 88 // the first byte of pubkey is bitcoin heritage 89 return common.LeftPadBytes(crypto.Keccak256(pubKey[1:])[12:], 32) 90 } 91 92 // SHA256 implemented as a native contract 93 type sha256 struct{} 94 95 func (c *sha256) RequiredGas(inputSize int) *big.Int { 96 n := big.NewInt(int64(inputSize+31) / 32) 97 n.Mul(n, params.Sha256WordGas) 98 return n.Add(n, params.Sha256Gas) 99 } 100 func (c *sha256) Run(in []byte) []byte { 101 return crypto.Sha256(in) 102 } 103 104 // RIPMED160 implemented as a native contract 105 type ripemd160 struct{} 106 107 func (c *ripemd160) RequiredGas(inputSize int) *big.Int { 108 n := big.NewInt(int64(inputSize+31) / 32) 109 n.Mul(n, params.Ripemd160WordGas) 110 return n.Add(n, params.Ripemd160Gas) 111 } 112 func (c *ripemd160) Run(in []byte) []byte { 113 return common.LeftPadBytes(crypto.Ripemd160(in), 32) 114 } 115 116 // data copy implemented as a native contract 117 type dataCopy struct{} 118 119 func (c *dataCopy) RequiredGas(inputSize int) *big.Int { 120 n := big.NewInt(int64(inputSize+31) / 32) 121 n.Mul(n, params.IdentityWordGas) 122 123 return n.Add(n, params.IdentityGas) 124 } 125 func (c *dataCopy) Run(in []byte) []byte { 126 return in 127 }