-
Notifications
You must be signed in to change notification settings - Fork 6
EIP 145 - Shift opcodes #43
Changes from 1 commit
02b876a
f5e5b15
7326008
da88266
e299263
e729125
9815571
8d652be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ import ( | |
|
||
"github.com/eth-classic/go-ethereum/common" | ||
"github.com/eth-classic/go-ethereum/crypto" | ||
"github.com/ethereum/go-ethereum/common/math" | ||
) | ||
|
||
var callStipend = big.NewInt(2300) // Free gas given at beginning of call. | ||
|
@@ -244,6 +245,49 @@ func opMulmod(instr instruction, pc *uint64, env Environment, contract *Contract | |
} | ||
} | ||
|
||
func opSHL(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Build is failing because the function signature doesn't match the updated. Add the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is instr instruction added as an argument? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's what it used to be, this is the old function signature There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's also preventing build since he's passing these in as instrFn in the jump table, so either we remove that field or we add an additional field to instrFn and all the other opcodes |
||
shift, value := math.U256(stack.pop()), math.U256(stack.pop()) | ||
|
||
if shift.Cmp(big.NewInt(256)) >= 0 { | ||
value.SetUint64(0) | ||
} else { | ||
n := uint(shift.Uint64()) | ||
math.U256(value.Lsh(value, n)) | ||
} | ||
stack.push(value) | ||
} | ||
|
||
func opSHR(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) { | ||
shift, value := math.U256(stack.pop()), math.U256(stack.pop()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove references to math package |
||
|
||
if shift.Cmp(big.NewInt(256)) >= 0 { | ||
value.SetUint64(0) | ||
} else { | ||
n := uint(shift.Uint64()) | ||
math.U256(value.Rsh(value, n)) | ||
} | ||
stack.push(value) | ||
} | ||
|
||
func opSAR(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) { | ||
shift, value := math.U256(stack.pop()), math.S256(stack.pop()) | ||
|
||
if shift.Cmp(big.NewInt(256)) >= 0 { | ||
if value.Sign() >= 0 { | ||
value.SetUint64(0) | ||
} else { | ||
value.SetInt64(-1) | ||
} | ||
|
||
stack.push(math.U256(value)) | ||
} else { | ||
n := uint(shift.Uint64()) | ||
// value | ||
value.Rsh(value, n) | ||
stack.push(math.U256(value)) | ||
} | ||
} | ||
|
||
func opSha3(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) { | ||
offset, size := stack.pop(), stack.pop() | ||
hash := crypto.Keccak256(memory.Get(offset.Int64(), size.Int64())) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
// Copyright 2017 The go-ethereum Authors | ||
// This file is part of the go-ethereum library. | ||
// | ||
// The go-ethereum library is free software: you can redistribute it and/or modify | ||
// 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. | ||
// | ||
// The go-ethereum library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package vm | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/eth-classic/go-ethereum/common" | ||
) | ||
|
||
type TwoOperandTestcase struct { | ||
X string | ||
Y string | ||
Expected string | ||
} | ||
|
||
type twoOperandParams struct { | ||
x string | ||
y string | ||
} | ||
|
||
var commonParams []*twoOperandParams | ||
var twoOpMethods map[string]instrFn | ||
|
||
func init() { | ||
|
||
// Params is a list of common edgecases that should be used for some common tests | ||
params := []string{ | ||
"0000000000000000000000000000000000000000000000000000000000000000", // 0 | ||
"0000000000000000000000000000000000000000000000000000000000000001", // +1 | ||
"0000000000000000000000000000000000000000000000000000000000000005", // +5 | ||
"7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe", // + max -1 | ||
"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", // + max | ||
"8000000000000000000000000000000000000000000000000000000000000000", // - max | ||
"8000000000000000000000000000000000000000000000000000000000000001", // - max+1 | ||
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb", // - 5 | ||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", // - 1 | ||
} | ||
// Params are combined so each param is used on each 'side' | ||
commonParams = make([]*twoOperandParams, len(params)*len(params)) | ||
for i, x := range params { | ||
for j, y := range params { | ||
commonParams[i*len(params)+j] = &twoOperandParams{x, y} | ||
} | ||
} | ||
twoOpMethods = map[string]instrFn{ | ||
"add": opAdd, | ||
"sub": opSub, | ||
"mul": opMul, | ||
"div": opDiv, | ||
"sdiv": opSdiv, | ||
"mod": opMod, | ||
"smod": opSmod, | ||
"exp": opExp, | ||
"signext": opSignExtend, | ||
"lt": opLt, | ||
"gt": opGt, | ||
"slt": opSlt, | ||
"sgt": opSgt, | ||
"eq": opEq, | ||
"and": opAnd, | ||
"or": opOr, | ||
"xor": opXor, | ||
"byte": opByte, | ||
"shl": opSHL, | ||
"shr": opSHR, | ||
"sar": opSAR, | ||
} | ||
} | ||
|
||
func testTwoOperandOp(t *testing.T, tests []TwoOperandTestcase, opFn instrFn, name string) { | ||
|
||
var ( | ||
stack = newstack() | ||
pc = uint64(0) | ||
) | ||
|
||
for i, test := range tests { | ||
x := new(big.Int).SetBytes(common.Hex2Bytes(test.X)) | ||
y := new(big.Int).SetBytes(common.Hex2Bytes(test.Y)) | ||
expected := new(big.Int).SetBytes(common.Hex2Bytes(test.Expected)) | ||
stack.push(x) | ||
stack.push(y) | ||
opFn(instruction{}, &pc, nil, nil, nil, stack) | ||
actual := stack.pop() | ||
|
||
if actual.Cmp(expected) != 0 { | ||
t.Errorf("Testcase %v %d, %v(%x, %x): expected %x, got %x", name, i, name, x, y, expected, actual) | ||
} else { | ||
t.Log("Worked") | ||
} | ||
|
||
} | ||
} | ||
|
||
func TestByteOp(t *testing.T) { | ||
tests := []TwoOperandTestcase{ | ||
{"ABCDEF0908070605040302010000000000000000000000000000000000000000", "00", "AB"}, | ||
{"ABCDEF0908070605040302010000000000000000000000000000000000000000", "01", "CD"}, | ||
{"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", "00", "00"}, | ||
{"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", "01", "CD"}, | ||
{"0000000000000000000000000000000000000000000000000000000000102030", "1F", "30"}, | ||
{"0000000000000000000000000000000000000000000000000000000000102030", "1E", "20"}, | ||
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "20", "00"}, | ||
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "FFFFFFFFFFFFFFFF", "00"}, | ||
} | ||
testTwoOperandOp(t, tests, opByte, "byte") | ||
} | ||
|
||
func TestSHL(t *testing.T) { | ||
// Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#shl-shift-left | ||
tests := []TwoOperandTestcase{ | ||
{"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000002"}, | ||
{"0000000000000000000000000000000000000000000000000000000000000001", "ff", "8000000000000000000000000000000000000000000000000000000000000000"}, | ||
{"0000000000000000000000000000000000000000000000000000000000000001", "0100", "0000000000000000000000000000000000000000000000000000000000000000"}, | ||
{"0000000000000000000000000000000000000000000000000000000000000001", "0101", "0000000000000000000000000000000000000000000000000000000000000000"}, | ||
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, | ||
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"}, | ||
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "8000000000000000000000000000000000000000000000000000000000000000"}, | ||
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"}, | ||
{"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"}, | ||
{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"}, | ||
} | ||
testTwoOperandOp(t, tests, opSHL, "shl") | ||
} | ||
|
||
func TestSHR(t *testing.T) { | ||
// Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#shr-logical-shift-right | ||
tests := []TwoOperandTestcase{ | ||
{"0000000000000000000000000000000000000000000000000000000000000001", "00", "0000000000000000000000000000000000000000000000000000000000000001"}, | ||
{"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000000"}, | ||
{"8000000000000000000000000000000000000000000000000000000000000000", "01", "4000000000000000000000000000000000000000000000000000000000000000"}, | ||
{"8000000000000000000000000000000000000000000000000000000000000000", "ff", "0000000000000000000000000000000000000000000000000000000000000001"}, | ||
{"8000000000000000000000000000000000000000000000000000000000000000", "0100", "0000000000000000000000000000000000000000000000000000000000000000"}, | ||
{"8000000000000000000000000000000000000000000000000000000000000000", "0101", "0000000000000000000000000000000000000000000000000000000000000000"}, | ||
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, | ||
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, | ||
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "0000000000000000000000000000000000000000000000000000000000000001"}, | ||
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"}, | ||
{"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"}, | ||
} | ||
testTwoOperandOp(t, tests, opSHR, "shr") | ||
} | ||
|
||
func TestSAR(t *testing.T) { | ||
// Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#sar-arithmetic-shift-right | ||
tests := []TwoOperandTestcase{ | ||
{"0000000000000000000000000000000000000000000000000000000000000001", "00", "0000000000000000000000000000000000000000000000000000000000000001"}, | ||
{"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000000"}, | ||
{"8000000000000000000000000000000000000000000000000000000000000000", "01", "c000000000000000000000000000000000000000000000000000000000000000"}, | ||
{"8000000000000000000000000000000000000000000000000000000000000000", "ff", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, | ||
{"8000000000000000000000000000000000000000000000000000000000000000", "0100", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, | ||
{"8000000000000000000000000000000000000000000000000000000000000000", "0101", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, | ||
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, | ||
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, | ||
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, | ||
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, | ||
{"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"}, | ||
{"4000000000000000000000000000000000000000000000000000000000000000", "fe", "0000000000000000000000000000000000000000000000000000000000000001"}, | ||
{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "f8", "000000000000000000000000000000000000000000000000000000000000007f"}, | ||
{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "fe", "0000000000000000000000000000000000000000000000000000000000000001"}, | ||
{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "0000000000000000000000000000000000000000000000000000000000000000"}, | ||
{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"}, | ||
} | ||
|
||
testTwoOperandOp(t, tests, opSAR, "sar") | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did it not work out cleanly to add in the tests from the testdata directory (from ETH as well) to include with these? These tests included should be sufficient, just curious |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove autoimport