aboutsummaryrefslogtreecommitdiffstats
path: root/core/vm
diff options
context:
space:
mode:
Diffstat (limited to 'core/vm')
-rw-r--r--core/vm/common.go20
-rw-r--r--core/vm/contracts.go9
-rw-r--r--core/vm/instructions.go60
-rw-r--r--core/vm/memory_table.go8
-rw-r--r--core/vm/runtime/runtime_test.go4
5 files changed, 53 insertions, 48 deletions
diff --git a/core/vm/common.go b/core/vm/common.go
index b7b9a6a9c..ef40c4a95 100644
--- a/core/vm/common.go
+++ b/core/vm/common.go
@@ -17,15 +17,10 @@
package vm
import (
- "math"
"math/big"
"github.com/ethereum/go-ethereum/common"
-)
-
-var (
- U256 = common.U256 // Shortcut to common.U256
- S256 = common.S256 // Shortcut to common.S256
+ "github.com/ethereum/go-ethereum/common/math"
)
// calculates the memory size required for a step
@@ -42,8 +37,8 @@ func calcMemSize(off, l *big.Int) *big.Int {
func getData(data []byte, start, size *big.Int) []byte {
dlen := big.NewInt(int64(len(data)))
- s := common.BigMin(start, dlen)
- e := common.BigMin(new(big.Int).Add(s, size), dlen)
+ s := math.BigMin(start, dlen)
+ e := math.BigMin(new(big.Int).Add(s, size), dlen)
return common.RightPadBytes(data[s.Uint64():e.Uint64()], int(size.Uint64()))
}
@@ -61,3 +56,12 @@ func toWordSize(size uint64) uint64 {
return (size + 31) / 32
}
+
+func allZero(b []byte) bool {
+ for _, byte := range b {
+ if byte != 0 {
+ return false
+ }
+ }
+ return true
+}
diff --git a/core/vm/contracts.go b/core/vm/contracts.go
index 2793d2aa7..0479adfda 100644
--- a/core/vm/contracts.go
+++ b/core/vm/contracts.go
@@ -19,6 +19,7 @@ package vm
import (
"crypto/sha256"
"fmt"
+ "math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
@@ -69,13 +70,13 @@ func (c *ecrecover) Run(in []byte) []byte {
// "in" is (hash, v, r, s), each 32 bytes
// but for ecrecover we want (r, s, v)
- r := common.BytesToBig(in[64:96])
- s := common.BytesToBig(in[96:128])
+ r := new(big.Int).SetBytes(in[64:96])
+ s := new(big.Int).SetBytes(in[96:128])
v := in[63] - 27
// tighter sig s values in homestead only apply to tx sigs
- if common.Bytes2Big(in[32:63]).BitLen() > 0 || !crypto.ValidateSignatureValues(v, r, s, false) {
- log.Trace(fmt.Sprintf("ECRECOVER error: v, r or s value invalid"))
+ if !allZero(in[32:63]) || !crypto.ValidateSignatureValues(v, r, s, false) {
+ log.Trace("ECRECOVER error: v, r or s value invalid")
return nil
}
// v needs to be at the end for libsecp256k1
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
}
diff --git a/core/vm/memory_table.go b/core/vm/memory_table.go
index 4db994837..3141a2f61 100644
--- a/core/vm/memory_table.go
+++ b/core/vm/memory_table.go
@@ -3,7 +3,7 @@ package vm
import (
"math/big"
- "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/common/math"
)
func memorySha3(stack *Stack) *big.Int {
@@ -42,20 +42,20 @@ func memoryCall(stack *Stack) *big.Int {
x := calcMemSize(stack.Back(5), stack.Back(6))
y := calcMemSize(stack.Back(3), stack.Back(4))
- return common.BigMax(x, y)
+ return math.BigMax(x, y)
}
func memoryCallCode(stack *Stack) *big.Int {
x := calcMemSize(stack.Back(5), stack.Back(6))
y := calcMemSize(stack.Back(3), stack.Back(4))
- return common.BigMax(x, y)
+ return math.BigMax(x, y)
}
func memoryDelegateCall(stack *Stack) *big.Int {
x := calcMemSize(stack.Back(4), stack.Back(5))
y := calcMemSize(stack.Back(2), stack.Back(3))
- return common.BigMax(x, y)
+ return math.BigMax(x, y)
}
func memoryReturn(stack *Stack) *big.Int {
diff --git a/core/vm/runtime/runtime_test.go b/core/vm/runtime/runtime_test.go
index 8ad74a89a..fe39e97a0 100644
--- a/core/vm/runtime/runtime_test.go
+++ b/core/vm/runtime/runtime_test.go
@@ -87,7 +87,7 @@ func TestExecute(t *testing.T) {
t.Fatal("didn't expect error", err)
}
- num := common.BytesToBig(ret)
+ num := new(big.Int).SetBytes(ret)
if num.Cmp(big.NewInt(10)) != 0 {
t.Error("Expected 10, got", num)
}
@@ -111,7 +111,7 @@ func TestCall(t *testing.T) {
t.Fatal("didn't expect error", err)
}
- num := common.BytesToBig(ret)
+ num := new(big.Int).SetBytes(ret)
if num.Cmp(big.NewInt(10)) != 0 {
t.Error("Expected 10, got", num)
}