-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
Relates to ethereum#1104
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,35 @@ | ||
import copy | ||
from cytoolz import ( | ||
merge | ||
) | ||
|
||
from eth import ( | ||
constants | ||
) | ||
from eth.vm import ( | ||
mnemonics, | ||
opcode_values, | ||
) | ||
from eth.vm.forks.byzantium.opcodes import ( | ||
BYZANTIUM_OPCODES | ||
) | ||
from eth.vm.logic import ( | ||
arithmetic | ||
) | ||
from eth.vm.opcode import ( | ||
as_opcode | ||
) | ||
|
||
|
||
UPDATED_OPCODES = { | ||
opcode_values.SHL: as_opcode( | ||
logic_fn=arithmetic.shl, | ||
mnemonic=mnemonics.SHL, | ||
gas_cost=constants.GAS_VERYLOW, | ||
), | ||
} | ||
|
||
CONSTANTINOPLE_OPCODES = copy.deepcopy(BYZANTIUM_OPCODES) | ||
CONSTANTINOPLE_OPCODES = merge( | ||
copy.deepcopy(BYZANTIUM_OPCODES), | ||
UPDATED_OPCODES, | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from BitVector import BitVector | ||
from cytoolz import ( | ||
curry, | ||
) | ||
|
@@ -177,3 +178,18 @@ def signextend(computation): | |
result = value | ||
|
||
computation.stack_push(result) | ||
|
||
|
||
def shl(computation): | ||
""" | ||
Bitwise left shift | ||
""" | ||
shift_length, value = computation.stack_pop(num_items=2, type_hint=constants.UINT256) | ||
bit_vector = BitVector(intVal=value, size=constants.CONST_256) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
carver
|
||
|
||
if shift_length >= constants.CONST_256: | ||
result = constants.CONST_0 | ||
else: | ||
result = bit_vector.shift_left(shift_length).int_val() | ||
|
||
computation.stack_push(result) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
MULMOD = 'MULMOD' | ||
EXP = 'EXP' | ||
SIGNEXTEND = 'SIGNEXTEND' | ||
SHL = 'SHL' | ||
# | ||
# Comparisons | ||
# | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
XOR = 0x18 | ||
NOT = 0x19 | ||
BYTE = 0x1a | ||
SHL = 0x1b | ||
|
||
|
||
# | ||
|
I guess we don't want that extra lib but I had trouble getting all test cases to work without it. The reason for this goes as follows. Consider the hex code
0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
as defined in the test which represents the number115792089237316195423570985008687907853269984665640564039457584007913129639935
Now let's shift this to the left.
This isn't what the tests were expecting and we have to look into the binary representation to understand why. So,
115792089237316195423570985008687907853269984665640564039457584007913129639935
is represented as1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
(exactly 256 bits).When I say
115792089237316195423570985008687907853269984665640564039457584007913129639935 << 1
what I actually mean to get is1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110
(bits moved left, last bit filled with a new0
, keeping bits at256
.However, what I get instead is
11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110
, which if we count the bits make257
bits.As I think about it, it makes sense because Python doesn't actually now that I want to deal with a type that is restricted to
256
bits so it just keeps growing it.I'm sure there are plenty of clever (and performant) ways to get this done without that extra lib but I first wanted to get all tests passing.