contracts.go 4.0 KB

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