aboutsummaryrefslogtreecommitdiffstats
path: root/core/vm/instructions.go
diff options
context:
space:
mode:
authorFelix Lange <fjl@users.noreply.github.com>2017-02-27 05:21:51 +0800
committerJeffrey Wilcke <jeffrey@ethereum.org>2017-02-27 05:21:51 +0800
commit5c8fe28b725bd9b128edceae3215132ea741641b (patch)
tree4bcbfeb1a21536edbba06d1715752999faa7f085 /core/vm/instructions.go
parent50ee279f2585e9e2eee30f0e1d4a3bf5f781bd72 (diff)
downloadgo-tangerine-5c8fe28b725bd9b128edceae3215132ea741641b.tar.gz
go-tangerine-5c8fe28b725bd9b128edceae3215132ea741641b.tar.zst
go-tangerine-5c8fe28b725bd9b128edceae3215132ea741641b.zip
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString Move denomination values to params instead. * common: delete dead code * common: move big integer operations to common/math This commit consolidates all big integer operations into common/math and adds tests and documentation. There should be no change in semantics for BigPow, BigMin, BigMax, S256, U256, Exp and their behaviour is now locked in by tests. The BigD, BytesToBig and Bytes2Big functions don't provide additional value, all uses are replaced by new(big.Int).SetBytes(). BigToBytes is now called PaddedBigBytes, its minimum output size parameter is now specified as the number of bytes instead of bits. The single use of this function is in the EVM's MSTORE instruction. Big and String2Big are replaced by ParseBig, which is slightly stricter. It previously accepted leading zeros for hexadecimal inputs but treated decimal inputs as octal if a leading zero digit was present. ParseUint64 is used in places where String2Big was used to decode a uint64. The new functions MustParseBig and MustParseUint64 are now used in many places where parsing errors were previously ignored. * common: delete unused big integer variables * accounts/abi: replace uses of BytesToBig with use of encoding/binary * common: remove BytesToBig * common: remove Bytes2Big * common: remove BigTrue * cmd/utils: add BigFlag and use it for error-checked integer flags While here, remove environment variable processing for DirectoryFlag because we don't use it. * core: add missing error checks in genesis block parser * common: remove String2Big * cmd/evm: use utils.BigFlag * common/math: check for 256 bit overflow in ParseBig This is supposed to prevent silent overflow/truncation of values in the genesis block JSON. Without this check, a genesis block that set a balance larger than 256 bits would lead to weird behaviour in the VM. * cmd/utils: fixup import
Diffstat (limited to 'core/vm/instructions.go')
-rw-r--r--core/vm/instructions.go60
1 files changed, 30 insertions, 30 deletions
diff --git a/core/vm/instructions.go b/core/vm/instructions.go
index 39e5c0587..8b7bcc4d6 100644
--- a/core/vm/instructions.go
+++ b/core/vm/instructions.go
@@ -31,7 +31,7 @@ var bigZero = new(big.Int)
func opAdd(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
x, y := stack.pop(), stack.pop()
- stack.push(U256(x.Add(x, y)))
+ stack.push(math.U256(x.Add(x, y)))
evm.interpreter.intPool.put(y)
@@ -40,7 +40,7 @@ func opAdd(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac
func opSub(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
x, y := stack.pop(), stack.pop()
- stack.push(U256(x.Sub(x, y)))
+ stack.push(math.U256(x.Sub(x, y)))
evm.interpreter.intPool.put(y)
@@ -49,7 +49,7 @@ func opSub(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac
func opMul(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
x, y := stack.pop(), stack.pop()
- stack.push(U256(x.Mul(x, y)))
+ stack.push(math.U256(x.Mul(x, y)))
evm.interpreter.intPool.put(y)
@@ -59,7 +59,7 @@ func opMul(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac
func opDiv(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
x, y := stack.pop(), stack.pop()
if y.Cmp(common.Big0) != 0 {
- stack.push(U256(x.Div(x, y)))
+ stack.push(math.U256(x.Div(x, y)))
} else {
stack.push(new(big.Int))
}
@@ -70,7 +70,7 @@ func opDiv(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac
}
func opSdiv(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
- x, y := S256(stack.pop()), S256(stack.pop())
+ x, y := math.S256(stack.pop()), math.S256(stack.pop())
if y.Cmp(common.Big0) == 0 {
stack.push(new(big.Int))
return nil, nil
@@ -85,7 +85,7 @@ func opSdiv(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta
res := x.Div(x.Abs(x), y.Abs(y))
res.Mul(res, n)
- stack.push(U256(res))
+ stack.push(math.U256(res))
}
evm.interpreter.intPool.put(y)
return nil, nil
@@ -96,14 +96,14 @@ func opMod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac
if y.Cmp(common.Big0) == 0 {
stack.push(new(big.Int))
} else {
- stack.push(U256(x.Mod(x, y)))
+ stack.push(math.U256(x.Mod(x, y)))
}
evm.interpreter.intPool.put(y)
return nil, nil
}
func opSmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
- x, y := S256(stack.pop()), S256(stack.pop())
+ x, y := math.S256(stack.pop()), math.S256(stack.pop())
if y.Cmp(common.Big0) == 0 {
stack.push(new(big.Int))
@@ -118,7 +118,7 @@ func opSmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta
res := x.Mod(x.Abs(x), y.Abs(y))
res.Mul(res, n)
- stack.push(U256(res))
+ stack.push(math.U256(res))
}
evm.interpreter.intPool.put(y)
return nil, nil
@@ -140,13 +140,13 @@ func opSignExtend(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stac
num := stack.pop()
mask := back.Lsh(common.Big1, bit)
mask.Sub(mask, common.Big1)
- if common.BitTest(num, int(bit)) {
+ if num.Bit(int(bit)) > 0 {
num.Or(num, mask.Not(mask))
} else {
num.And(num, mask)
}
- stack.push(U256(num))
+ stack.push(math.U256(num))
}
evm.interpreter.intPool.put(back)
@@ -155,7 +155,7 @@ func opSignExtend(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stac
func opNot(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
x := stack.pop()
- stack.push(U256(x.Not(x)))
+ stack.push(math.U256(x.Not(x)))
return nil, nil
}
@@ -184,8 +184,8 @@ func opGt(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack
}
func opSlt(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
- x, y := S256(stack.pop()), S256(stack.pop())
- if x.Cmp(S256(y)) < 0 {
+ x, y := math.S256(stack.pop()), math.S256(stack.pop())
+ if x.Cmp(math.S256(y)) < 0 {
stack.push(evm.interpreter.intPool.get().SetUint64(1))
} else {
stack.push(new(big.Int))
@@ -196,7 +196,7 @@ func opSlt(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac
}
func opSgt(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
- x, y := S256(stack.pop()), S256(stack.pop())
+ x, y := math.S256(stack.pop()), math.S256(stack.pop())
if x.Cmp(y) > 0 {
stack.push(evm.interpreter.intPool.get().SetUint64(1))
} else {
@@ -269,7 +269,7 @@ func opAddmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S
if z.Cmp(bigZero) > 0 {
add := x.Add(x, y)
add.Mod(add, z)
- stack.push(U256(add))
+ stack.push(math.U256(add))
} else {
stack.push(new(big.Int))
}
@@ -282,7 +282,7 @@ func opMulmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S
if z.Cmp(bigZero) > 0 {
mul := x.Mul(x, y)
mul.Mod(mul, z)
- stack.push(U256(mul))
+ stack.push(math.U256(mul))
} else {
stack.push(new(big.Int))
}
@@ -300,14 +300,14 @@ func opSha3(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta
evm.StateDB.AddPreimage(common.BytesToHash(hash), data)
}
- stack.push(common.BytesToBig(hash))
+ stack.push(new(big.Int).SetBytes(hash))
evm.interpreter.intPool.put(offset, size)
return nil, nil
}
func opAddress(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
- stack.push(common.Bytes2Big(contract.Address().Bytes()))
+ stack.push(contract.Address().Big())
return nil, nil
}
@@ -335,7 +335,7 @@ func opCallValue(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack
}
func opCalldataLoad(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
- stack.push(common.Bytes2Big(getData(contract.Input, stack.pop(), common.Big32)))
+ stack.push(new(big.Int).SetBytes(getData(contract.Input, stack.pop(), common.Big32)))
return nil, nil
}
@@ -427,22 +427,22 @@ func opCoinbase(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack
}
func opTimestamp(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
- stack.push(U256(new(big.Int).Set(evm.Time)))
+ stack.push(math.U256(new(big.Int).Set(evm.Time)))
return nil, nil
}
func opNumber(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
- stack.push(U256(new(big.Int).Set(evm.BlockNumber)))
+ stack.push(math.U256(new(big.Int).Set(evm.BlockNumber)))
return nil, nil
}
func opDifficulty(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
- stack.push(U256(new(big.Int).Set(evm.Difficulty)))
+ stack.push(math.U256(new(big.Int).Set(evm.Difficulty)))
return nil, nil
}
func opGasLimit(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
- stack.push(U256(new(big.Int).Set(evm.GasLimit)))
+ stack.push(math.U256(new(big.Int).Set(evm.GasLimit)))
return nil, nil
}
@@ -453,7 +453,7 @@ func opPop(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac
func opMload(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
offset := stack.pop()
- val := common.BigD(memory.Get(offset.Int64(), 32))
+ val := new(big.Int).SetBytes(memory.Get(offset.Int64(), 32))
stack.push(val)
evm.interpreter.intPool.put(offset)
@@ -463,7 +463,7 @@ func opMload(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *St
func opMstore(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
// pop value of the stack
mStart, val := stack.pop(), stack.pop()
- memory.Set(mStart.Uint64(), 32, common.BigToBytes(val, 256))
+ memory.Set(mStart.Uint64(), 32, math.PaddedBigBytes(val, 32))
evm.interpreter.intPool.put(mStart, val)
return nil, nil
@@ -505,7 +505,7 @@ func opJump(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta
}
func opJumpi(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
pos, cond := stack.pop(), stack.pop()
- if cond.Cmp(common.BigTrue) >= 0 {
+ if cond.BitLen() > 0 {
if !contract.jumpdests.has(contract.CodeHash, contract.Code, pos) {
nop := contract.GetOp(pos.Uint64())
return nil, fmt.Errorf("invalid jump destination (%v) %v", nop, pos)
@@ -572,7 +572,7 @@ func opCall(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta
gas := stack.pop().Uint64()
// pop gas and value of the stack.
addr, value := stack.pop(), stack.pop()
- value = U256(value)
+ value = math.U256(value)
// pop input size and offset
inOffset, inSize := stack.pop(), stack.pop()
// pop return size and offset
@@ -605,7 +605,7 @@ func opCallCode(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack
gas := stack.pop().Uint64()
// pop gas and value of the stack.
addr, value := stack.pop(), stack.pop()
- value = U256(value)
+ value = math.U256(value)
// pop input size and offset
inOffset, inSize := stack.pop(), stack.pop()
// pop return size and offset
@@ -711,7 +711,7 @@ func makeLog(size int) executionFunc {
func makePush(size uint64, bsize *big.Int) executionFunc {
return func(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
byts := getData(contract.Code, evm.interpreter.intPool.get().SetUint64(*pc+1), bsize)
- stack.push(common.Bytes2Big(byts))
+ stack.push(new(big.Int).SetBytes(byts))
*pc += size
return nil, nil
}