contracts.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. "errors"
  20. "math/big"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/crypto"
  23. "github.com/ethereum/go-ethereum/params"
  24. "golang.org/x/crypto/ripemd160"
  25. )
  26. var errBadPrecompileInput = errors.New("bad pre compile input")
  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(input []byte) uint64 // RequiredPrice calculates the contract gas use
  32. Run(input []byte) ([]byte, error) // Run runs the precompiled contract
  33. }
  34. // PrecompiledContracts 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(input)
  44. if contract.UseGas(gas) {
  45. return p.Run(input)
  46. } else {
  47. return nil, ErrOutOfGas
  48. }
  49. }
  50. // ECRECOVER implemented as a native contract
  51. type ecrecover struct{}
  52. func (c *ecrecover) RequiredGas(input []byte) uint64 {
  53. return params.EcrecoverGas
  54. }
  55. func (c *ecrecover) Run(in []byte) ([]byte, error) {
  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 := new(big.Int).SetBytes(in[64:96])
  61. s := new(big.Int).SetBytes(in[96:128])
  62. v := in[63] - 27
  63. // tighter sig s values in homestead only apply to tx sigs
  64. if !allZero(in[32:63]) || !crypto.ValidateSignatureValues(v, r, s, false) {
  65. return nil, 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. return nil, nil
  72. }
  73. // the first byte of pubkey is bitcoin heritage
  74. return common.LeftPadBytes(crypto.Keccak256(pubKey[1:])[12:], 32), nil
  75. }
  76. // SHA256 implemented as a native contract
  77. type sha256hash struct{}
  78. // RequiredGas returns the gas required to execute the pre-compiled contract.
  79. //
  80. // This method does not require any overflow checking as the input size gas costs
  81. // required for anything significant is so high it's impossible to pay for.
  82. func (c *sha256hash) RequiredGas(input []byte) uint64 {
  83. return uint64(len(input)+31)/32*params.Sha256WordGas + params.Sha256Gas
  84. }
  85. func (c *sha256hash) Run(in []byte) ([]byte, error) {
  86. h := sha256.Sum256(in)
  87. return h[:], nil
  88. }
  89. // RIPMED160 implemented as a native contract
  90. type ripemd160hash struct{}
  91. // RequiredGas returns the gas required to execute the pre-compiled contract.
  92. //
  93. // This method does not require any overflow checking as the input size gas costs
  94. // required for anything significant is so high it's impossible to pay for.
  95. func (c *ripemd160hash) RequiredGas(input []byte) uint64 {
  96. return uint64(len(input)+31)/32*params.Ripemd160WordGas + params.Ripemd160Gas
  97. }
  98. func (c *ripemd160hash) Run(in []byte) ([]byte, error) {
  99. ripemd := ripemd160.New()
  100. ripemd.Write(in)
  101. return common.LeftPadBytes(ripemd.Sum(nil), 32), nil
  102. }
  103. // data copy implemented as a native contract
  104. type dataCopy struct{}
  105. // RequiredGas returns the gas required to execute the pre-compiled contract.
  106. //
  107. // This method does not require any overflow checking as the input size gas costs
  108. // required for anything significant is so high it's impossible to pay for.
  109. func (c *dataCopy) RequiredGas(input []byte) uint64 {
  110. return uint64(len(input)+31)/32*params.IdentityWordGas + params.IdentityGas
  111. }
  112. func (c *dataCopy) Run(in []byte) ([]byte, error) {
  113. return in, nil
  114. }