common.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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/common/math"
  21. )
  22. // calculates the memory size required for a step
  23. func calcMemSize(off, l *big.Int) *big.Int {
  24. if l.Cmp(common.Big0) == 0 {
  25. return common.Big0
  26. }
  27. return new(big.Int).Add(off, l)
  28. }
  29. // getData returns a slice from the data based on the start and size and pads
  30. // up to size with zero's. This function is overflow safe.
  31. func getData(data []byte, start, size *big.Int) []byte {
  32. dlen := big.NewInt(int64(len(data)))
  33. s := math.BigMin(start, dlen)
  34. e := math.BigMin(new(big.Int).Add(s, size), dlen)
  35. return common.RightPadBytes(data[s.Uint64():e.Uint64()], int(size.Uint64()))
  36. }
  37. // bigUint64 returns the integer casted to a uint64 and returns whether it
  38. // overflowed in the process.
  39. func bigUint64(v *big.Int) (uint64, bool) {
  40. return v.Uint64(), v.BitLen() > 64
  41. }
  42. // toWordSize returns the ceiled word size required for memory expansion.
  43. func toWordSize(size uint64) uint64 {
  44. if size > math.MaxUint64-31 {
  45. return math.MaxUint64/32 + 1
  46. }
  47. return (size + 31) / 32
  48. }
  49. func allZero(b []byte) bool {
  50. for _, byte := range b {
  51. if byte != 0 {
  52. return false
  53. }
  54. }
  55. return true
  56. }