contracts.go 4.7 KB

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