diff options
author | Felix Lange <fjl@users.noreply.github.com> | 2017-02-27 05:21:51 +0800 |
---|---|---|
committer | Jeffrey Wilcke <jeffrey@ethereum.org> | 2017-02-27 05:21:51 +0800 |
commit | 5c8fe28b725bd9b128edceae3215132ea741641b (patch) | |
tree | 4bcbfeb1a21536edbba06d1715752999faa7f085 /core/vm | |
parent | 50ee279f2585e9e2eee30f0e1d4a3bf5f781bd72 (diff) | |
download | dexon-5c8fe28b725bd9b128edceae3215132ea741641b.tar.gz dexon-5c8fe28b725bd9b128edceae3215132ea741641b.tar.zst dexon-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')
-rw-r--r-- | core/vm/common.go | 20 | ||||
-rw-r--r-- | core/vm/contracts.go | 9 | ||||
-rw-r--r-- | core/vm/instructions.go | 60 | ||||
-rw-r--r-- | core/vm/memory_table.go | 8 | ||||
-rw-r--r-- | core/vm/runtime/runtime_test.go | 4 |
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) } |