github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/core/vm/common.go (about)

     1  package vm
     2  
     3  import (
     4  	"math/big"
     5  
     6  	"github.com/quickchainproject/quickchain/common"
     7  	"github.com/quickchainproject/quickchain/common/math"
     8  )
     9  
    10  // calculates the memory size required for a step
    11  func calcMemSize(off, l *big.Int) *big.Int {
    12  	if l.Sign() == 0 {
    13  		return common.Big0
    14  	}
    15  
    16  	return new(big.Int).Add(off, l)
    17  }
    18  
    19  // getData returns a slice from the data based on the start and size and pads
    20  // up to size with zero's. This function is overflow safe.
    21  func getData(data []byte, start uint64, size uint64) []byte {
    22  	length := uint64(len(data))
    23  	if start > length {
    24  		start = length
    25  	}
    26  	end := start + size
    27  	if end > length {
    28  		end = length
    29  	}
    30  	return common.RightPadBytes(data[start:end], int(size))
    31  }
    32  
    33  // getDataBig returns a slice from the data based on the start and size and pads
    34  // up to size with zero's. This function is overflow safe.
    35  func getDataBig(data []byte, start *big.Int, size *big.Int) []byte {
    36  	dlen := big.NewInt(int64(len(data)))
    37  
    38  	s := math.BigMin(start, dlen)
    39  	e := math.BigMin(new(big.Int).Add(s, size), dlen)
    40  	return common.RightPadBytes(data[s.Uint64():e.Uint64()], int(size.Uint64()))
    41  }
    42  
    43  // bigUint64 returns the integer casted to a uint64 and returns whether it
    44  // overflowed in the process.
    45  func bigUint64(v *big.Int) (uint64, bool) {
    46  	return v.Uint64(), v.BitLen() > 64
    47  }
    48  
    49  // toWordSize returns the ceiled word size required for memory expansion.
    50  func toWordSize(size uint64) uint64 {
    51  	if size > math.MaxUint64-31 {
    52  		return math.MaxUint64/32 + 1
    53  	}
    54  
    55  	return (size + 31) / 32
    56  }
    57  
    58  func allZero(b []byte) bool {
    59  	for _, byte := range b {
    60  		if byte != 0 {
    61  			return false
    62  		}
    63  	}
    64  	return true
    65  }