contracts.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. "crypto/sha256"
  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. "golang.org/x/crypto/ripemd160"
  25. )
  26. // Precompiled contract is the basic interface for native Go contracts. The implementation
  27. // requires a deterministic gas count based on the input size of the Run method of the
  28. // contract.
  29. type PrecompiledContract interface {
  30. RequiredGas(inputSize int) uint64 // RequiredPrice calculates the contract gas use
  31. Run(input []byte) []byte // Run runs the precompiled contract
  32. }
  33. // Precompiled contains the default set of ethereum contracts
  34. var PrecompiledContracts = map[common.Address]PrecompiledContract{
  35. common.BytesToAddress([]byte{1}): &ecrecover{},
  36. common.BytesToAddress([]byte{2}): &sha256hash{},
  37. common.BytesToAddress([]byte{3}): &ripemd160hash{},
  38. common.BytesToAddress([]byte{4}): &dataCopy{},
  39. }
  40. // RunPrecompile runs and evaluate the output of a precompiled contract defined in contracts.go
  41. func RunPrecompiledContract(p PrecompiledContract, input []byte, contract *Contract) (ret []byte, err error) {
  42. gas := p.RequiredGas(len(input))
  43. if contract.UseGas(gas) {
  44. ret = p.Run(input)
  45. return ret, nil
  46. } else {
  47. return nil, ErrOutOfGas
  48. }
  49. }
  50. // ECRECOVER implemented as a native contract
  51. type ecrecover struct{}
  52. func (c *ecrecover) RequiredGas(inputSize int) uint64 {
  53. return params.EcrecoverGas
  54. }
  55. func (c *ecrecover) Run(in []byte) []byte {
  56. const ecRecoverInputLength = 128
  57. in = common.RightPadBytes(in, ecRecoverInputLength)
  58. // "in" is (hash, v, r, s), each 32 bytes
  59. // but for ecrecover we want (r, s, v)
  60. r := common.BytesToBig(in[64:96])
  61. s := common.BytesToBig(in[96:128])
  62. v := in[63] - 27
  63. // tighter sig s values in homestead only apply to tx sigs
  64. if common.Bytes2Big(in[32:63]).BitLen() > 0 || !crypto.ValidateSignatureValues(v, r, s, false) {
  65. glog.V(logger.Detail).Infof("ECRECOVER error: v, r or s value invalid")
  66. return nil
  67. }
  68. // v needs to be at the end for libsecp256k1
  69. pubKey, err := crypto.Ecrecover(in[:32], append(in[64:128], v))
  70. // make sure the public key is a valid one
  71. if err != nil {
  72. glog.V(logger.Detail).Infoln("ECRECOVER error: ", err)
  73. return nil
  74. }
  75. // the first byte of pubkey is bitcoin heritage
  76. return common.LeftPadBytes(crypto.Keccak256(pubKey[1:])[12:], 32)
  77. }
  78. // SHA256 implemented as a native contract
  79. type sha256hash struct{}
  80. // RequiredGas returns the gas required to execute the pre-compiled contract.
  81. //
  82. // This method does not require any overflow checking as the input size gas costs
  83. // required for anything significant is so high it's impossible to pay for.
  84. func (c *sha256hash) RequiredGas(inputSize int) uint64 {
  85. return uint64(inputSize+31)/32*params.Sha256WordGas + params.Sha256Gas
  86. }
  87. func (c *sha256hash) Run(in []byte) []byte {
  88. h := sha256.Sum256(in)
  89. return h[:]
  90. }
  91. // RIPMED160 implemented as a native contract
  92. type ripemd160hash struct{}
  93. // RequiredGas returns the gas required to execute the pre-compiled contract.
  94. //
  95. // This method does not require any overflow checking as the input size gas costs
  96. // required for anything significant is so high it's impossible to pay for.
  97. func (c *ripemd160hash) RequiredGas(inputSize int) uint64 {
  98. return uint64(inputSize+31)/32*params.Ripemd160WordGas + params.Ripemd160Gas
  99. }
  100. func (c *ripemd160hash) Run(in []byte) []byte {
  101. ripemd := ripemd160.New()
  102. ripemd.Write(in)
  103. return common.LeftPadBytes(ripemd.Sum(nil), 32)
  104. }
  105. // data copy implemented as a native contract
  106. type dataCopy struct{}
  107. // RequiredGas returns the gas required to execute the pre-compiled contract.
  108. //
  109. // This method does not require any overflow checking as the input size gas costs
  110. // required for anything significant is so high it's impossible to pay for.
  111. func (c *dataCopy) RequiredGas(inputSize int) uint64 {
  112. return uint64(inputSize+31)/32*params.IdentityWordGas + params.IdentityGas
  113. }
  114. func (c *dataCopy) Run(in []byte) []byte {
  115. return in
  116. }