2015-07-07 00:54:22 +00:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 16:48:40 +00:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 00:54:22 +00:00
|
|
|
//
|
2015-07-23 16:35:11 +00:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 00:54:22 +00:00
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2015-07-22 16:48:40 +00:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 00:54:22 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 16:48:40 +00:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 00:54:22 +00:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 16:48:40 +00:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 00:54:22 +00:00
|
|
|
|
2014-10-18 11:31:20 +00:00
|
|
|
package vm
|
2014-07-22 09:54:48 +00:00
|
|
|
|
|
|
|
import (
|
2015-03-13 12:44:15 +00:00
|
|
|
"math"
|
2014-10-14 09:48:52 +00:00
|
|
|
"math/big"
|
|
|
|
|
2015-03-16 10:27:38 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2015-03-16 20:46:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-08-30 08:19:10 +00:00
|
|
|
U256 = common.U256 // Shortcut to common.U256
|
|
|
|
S256 = common.S256 // Shortcut to common.S256
|
2014-10-14 09:48:52 +00:00
|
|
|
)
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
// calculates the memory size required for a step
|
2014-10-14 09:48:52 +00:00
|
|
|
func calcMemSize(off, l *big.Int) *big.Int {
|
2015-03-16 10:27:38 +00:00
|
|
|
if l.Cmp(common.Big0) == 0 {
|
|
|
|
return common.Big0
|
2014-10-14 09:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return new(big.Int).Add(off, l)
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
// getData returns a slice from the data based on the start and size and pads
|
|
|
|
// up to size with zero's. This function is overflow safe.
|
2015-03-19 21:45:03 +00:00
|
|
|
func getData(data []byte, start, size *big.Int) []byte {
|
|
|
|
dlen := big.NewInt(int64(len(data)))
|
2015-03-13 12:44:15 +00:00
|
|
|
|
2015-03-19 21:45:03 +00:00
|
|
|
s := common.BigMin(start, dlen)
|
|
|
|
e := common.BigMin(new(big.Int).Add(s, size), dlen)
|
|
|
|
return common.RightPadBytes(data[s.Uint64():e.Uint64()], int(size.Uint64()))
|
2015-03-13 12:44:15 +00:00
|
|
|
}
|
2015-03-28 19:03:25 +00:00
|
|
|
|
2017-02-02 14:25:42 +00:00
|
|
|
// bigUint64 returns the integer casted to a uint64 and returns whether it
|
|
|
|
// overflowed in the process.
|
|
|
|
func bigUint64(v *big.Int) (uint64, bool) {
|
|
|
|
return v.Uint64(), v.BitLen() > 64
|
|
|
|
}
|
|
|
|
|
|
|
|
// toWordSize returns the ceiled word size required for memory expansion.
|
|
|
|
func toWordSize(size uint64) uint64 {
|
|
|
|
if size > math.MaxUint64-31 {
|
|
|
|
return math.MaxUint64/32 + 1
|
2015-03-28 19:03:25 +00:00
|
|
|
}
|
|
|
|
|
2017-02-02 14:25:42 +00:00
|
|
|
return (size + 31) / 32
|
2015-03-28 19:03:25 +00:00
|
|
|
}
|