github.com/r8d8/go-ethereum@v5.5.2+incompatible/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/ethereumproject/go-ethereum/common" 23 "github.com/ethereumproject/go-ethereum/crypto" 24 "github.com/ethereumproject/go-ethereum/logger" 25 "github.com/ethereumproject/go-ethereum/logger/glog" 26 ) 27 28 // PrecompiledAccount represents a native ethereum contract 29 type PrecompiledAccount struct { 30 Gas func(l int) *big.Int 31 fn func(in []byte) []byte 32 } 33 34 // Call calls the native function 35 func (self PrecompiledAccount) Call(in []byte) []byte { 36 return self.fn(in) 37 } 38 39 // Precompiled contains the default set of ethereum contracts 40 var Precompiled = PrecompiledContracts() 41 42 // PrecompiledContracts returns the default set of precompiled ethereum 43 // contracts defined by the ethereum yellow paper. 44 func PrecompiledContracts() map[string]*PrecompiledAccount { 45 return map[string]*PrecompiledAccount{ 46 // ECRECOVER 47 string(common.LeftPadBytes([]byte{1}, 20)): {func(l int) *big.Int { 48 return big.NewInt(3000) 49 }, ecrecoverFunc}, 50 51 // SHA256 52 string(common.LeftPadBytes([]byte{2}, 20)): {func(l int) *big.Int { 53 n := big.NewInt(int64(l+31) / 32) 54 n.Mul(n, big.NewInt(12)) 55 return n.Add(n, big.NewInt(60)) 56 }, sha256Func}, 57 58 // RIPEMD160 59 string(common.LeftPadBytes([]byte{3}, 20)): {func(l int) *big.Int { 60 n := big.NewInt(int64(l+31) / 32) 61 n.Mul(n, big.NewInt(120)) 62 return n.Add(n, big.NewInt(600)) 63 }, ripemd160Func}, 64 65 string(common.LeftPadBytes([]byte{4}, 20)): {func(l int) *big.Int { 66 n := big.NewInt(int64(l+31) / 32) 67 n.Mul(n, big.NewInt(3)) 68 return n.Add(n, big.NewInt(15)) 69 }, memCpy}, 70 } 71 } 72 73 func sha256Func(in []byte) []byte { 74 return crypto.Sha256(in) 75 } 76 77 func ripemd160Func(in []byte) []byte { 78 return common.LeftPadBytes(crypto.Ripemd160(in), 32) 79 } 80 81 func ecrecoverFunc(in []byte) []byte { 82 in = common.RightPadBytes(in, 128) 83 // "in" is (hash, v, r, s), each 32 bytes 84 // but for ecrecover we want (r, s, v) 85 86 r := new(big.Int).SetBytes(in[64:96]) 87 s := new(big.Int).SetBytes(in[96:128]) 88 // Treat V as a 256bit integer 89 vbig := new(big.Int).SetBytes(in[32:64]) 90 v := byte(vbig.Uint64()) 91 92 // tighter sig s values in homestead only apply to tx sigs 93 if !crypto.ValidateSignatureValues(v, r, s, false) { 94 glog.V(logger.Detail).Infof("ECRECOVER error: v, r or s value invalid") 95 return nil 96 } 97 98 // v needs to be at the end and normalized for libsecp256k1 99 vbignormal := new(big.Int).Sub(vbig, big.NewInt(27)) 100 vnormal := byte(vbignormal.Uint64()) 101 rsv := append(in[64:128], vnormal) 102 pubKey, err := crypto.Ecrecover(in[:32], rsv) 103 // make sure the public key is a valid one 104 if err != nil { 105 glog.V(logger.Detail).Infoln("ECRECOVER error: ", err) 106 return nil 107 } 108 109 // the first byte of pubkey is bitcoin heritage 110 return common.LeftPadBytes(crypto.Keccak256(pubKey[1:])[12:], 32) 111 } 112 113 func memCpy(in []byte) []byte { 114 return in 115 }