Skip to content
This repository was archived by the owner on Jul 8, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ classifiers =
License :: CC0 1.0 Universal (CC0 1.0) Public Domain Dedication

[options]
packages = ethereum, ethereum/vm
packages = ethereum, ethereum/vm, ethereum/vm/instructions
package_dir =
=src

Expand Down
64 changes: 53 additions & 11 deletions src/ethereum/base_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

from typing import Optional, Tuple, Type

U255_MAX_VALUE = (2 ** 255) - 1
U255_CEIL_VALUE = 2 ** 255
U256_MAX_VALUE = (2 ** 256) - 1
U256_CEIL_VALUE = 2 ** 256


class Uint(int):
"""
Expand Down Expand Up @@ -280,11 +285,31 @@ def from_be_bytes(cls: Type, buffer: "Bytes") -> "U256":

return cls(int.from_bytes(buffer, "big"))

@classmethod
def from_signed(cls: Type, value: int) -> "U256":
"""
Converts a signed number into a 256-bit unsigned integer.

Parameters
----------
value :
Signed number

Returns
-------
self : `U256`
Unsigned integer obtained from `value`.
"""
if value >= 0:
return cls(value)

return cls(value & U256_MAX_VALUE)

def __new__(cls: Type, value: int) -> "U256":
if not isinstance(value, int):
raise TypeError()

if value < 0 or value >= 2 ** 256:
if value < 0 or value > U256_MAX_VALUE:
raise ValueError()

return super(cls, cls).__new__(cls, value)
Expand Down Expand Up @@ -315,7 +340,8 @@ def wrapping_add(self, right: int) -> "U256":
if result == NotImplemented:
return NotImplemented

result %= 2 ** 256
# This is a fast way of ensuring that the result is < (2 ** 256)
result &= self.MAX_VALUE
return self.__class__(result)

def __iadd__(self, right: int) -> "U256":
Expand Down Expand Up @@ -344,7 +370,8 @@ def wrapping_sub(self, right: int) -> "U256":
if result == NotImplemented:
return NotImplemented

result %= 2 ** 256
# This is a fast way of ensuring that the result is < (2 ** 256)
result &= self.MAX_VALUE
return self.__class__(result)

def __rsub__(self, left: int) -> "U256":
Expand Down Expand Up @@ -375,7 +402,8 @@ def wrapping_mul(self, right: int) -> "U256":
if result == NotImplemented:
return NotImplemented

result %= 2 ** 256
# This is a fast way of ensuring that the result is < (2 ** 256)
result &= self.MAX_VALUE
return self.__class__(result)

def __mul__(self, right: int) -> "U256":
Expand Down Expand Up @@ -432,9 +460,6 @@ def __rmod__(self, left: int) -> "U256":
if not isinstance(left, int):
return NotImplemented

if left < 0 or left > self.MAX_VALUE:
raise ValueError()

result = super(U256, self).__rmod__(left)
return self.__class__(result)

Expand Down Expand Up @@ -466,7 +491,7 @@ def unchecked_pow(self, right: int, modulo: Optional[int] = None) -> int:
if not isinstance(modulo, int):
return NotImplemented

if modulo < 0 or modulo > self.MAX_VALUE:
if modulo < 0 or modulo > U256_CEIL_VALUE:
raise ValueError()

if not isinstance(right, int):
Expand All @@ -483,7 +508,8 @@ def wrapping_pow(self, right: int, modulo: Optional[int] = None) -> "U256":
if result == NotImplemented:
return NotImplemented

result %= 2 ** 256
# This is a fast way of ensuring that the result is < (2 ** 256)
result &= self.MAX_VALUE
return self.__class__(result)

def __pow__(self, right: int, modulo: Optional[int] = None) -> "U256":
Expand All @@ -499,7 +525,7 @@ def __rpow__(self, left: int, modulo: Optional[int] = None) -> "U256":
if not isinstance(modulo, int):
return NotImplemented

if modulo < 0 or modulo > self.MAX_VALUE:
if modulo < 0 or modulo > U256_CEIL_VALUE:
raise ValueError()

if not isinstance(left, int):
Expand Down Expand Up @@ -542,8 +568,24 @@ def to_be_bytes(self) -> "Bytes":
byte_length = (bit_length + 7) // 8
return self.to_bytes(byte_length, "big")

def to_signed(self) -> int:
"""
Converts this 256-bit unsigned integer into a signed integer.

Returns
-------
signed_int : `int`
Signed integer obtained from 256-bit unsigned integer.
"""
if self <= U255_MAX_VALUE:
# This means that the sign bit is 0
return int(self)

# -1 * (2's complement of U256 value)
return int(self) - U256_CEIL_VALUE


U256.MAX_VALUE = U256(2 ** 256 - 1)
U256.MAX_VALUE = U256(U256_MAX_VALUE)


Bytes = bytes
Expand Down
26 changes: 26 additions & 0 deletions src/ethereum/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
Utility functions used in this application.
"""


def get_sign(value: int) -> int:
"""
Determines the sign of a number.

Parameters
----------
value : `int`
The value whose sign is to be determined.

Returns
-------
sign : `int`
The sign of the number (-1 or 0 or 1).
The return value is based on math signum function.
"""
if value < 0:
return -1
elif value == 0:
return 0
else:
return 1
1 change: 1 addition & 0 deletions src/ethereum/vm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ class Evm:
depth: Uint
env: Environment
refund_counter: Uint
running: bool
3 changes: 3 additions & 0 deletions src/ethereum/vm/gas.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
GAS_STORAGE_SET = U256(20000)
GAS_STORAGE_UPDATE = U256(5000)
GAS_STORAGE_CLEAR_REFUND = U256(15000)
GAS_LOW = U256(5)
GAS_MID = U256(8)
GAS_EXPONENTIATION = U256(10)


def subtract_gas(gas_left: U256, amount: U256) -> U256:
Expand Down
198 changes: 198 additions & 0 deletions src/ethereum/vm/instructions/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
"""
EVM Instruction Encoding (Opcodes)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. contents:: Table of Contents
:backlinks: none
:local:

Introduction
------------

Machine readable representations of EVM instructions, and a mapping to their
implementations.
"""

import enum
from typing import Callable, Dict

from . import arithmetic as arithmetic_instructions
from . import computation as computation_instructions
from . import stack as stack_instructions
from . import storage as storage_instructions


class Ops(enum.Enum):
"""
Enum for EVM Opcodes
"""

# Arithmetic Ops
ADD = 0x01
MUL = 0x02
SUB = 0x03
DIV = 0x04
SDIV = 0x05
MOD = 0x06
SMOD = 0x07
ADDMOD = 0x08
MULMOD = 0x09
EXP = 0x0A
SIGNEXTEND = 0x0B

# Computation Ops
STOP = 0x00

# Storage Ops
SSTORE = 0x55

# Push Operations
PUSH1 = 0x60
PUSH2 = 0x61
PUSH3 = 0x62
PUSH4 = 0x63
PUSH5 = 0x64
PUSH6 = 0x65
PUSH7 = 0x66
PUSH8 = 0x67
PUSH9 = 0x68
PUSH10 = 0x69
PUSH11 = 0x6A
PUSH12 = 0x6B
PUSH13 = 0x6C
PUSH14 = 0x6D
PUSH15 = 0x6E
PUSH16 = 0x6F
PUSH17 = 0x70
PUSH18 = 0x71
PUSH19 = 0x72
PUSH20 = 0x73
PUSH21 = 0x74
PUSH22 = 0x75
PUSH23 = 0x76
PUSH24 = 0x77
PUSH25 = 0x78
PUSH26 = 0x79
PUSH27 = 0x7A
PUSH28 = 0x7B
PUSH29 = 0x7C
PUSH30 = 0x7D
PUSH31 = 0x7E
PUSH32 = 0x7F

# Dup operations
DUP1 = 0x80
DUP2 = 0x81
DUP3 = 0x82
DUP4 = 0x83
DUP5 = 0x84
DUP6 = 0x85
DUP7 = 0x86
DUP8 = 0x87
DUP9 = 0x88
DUP10 = 0x89
DUP11 = 0x8A
DUP12 = 0x8B
DUP13 = 0x8C
DUP14 = 0x8D
DUP15 = 0x8E
DUP16 = 0x8F

# Swap operations
SWAP1 = 0x90
SWAP2 = 0x91
SWAP3 = 0x92
SWAP4 = 0x93
SWAP5 = 0x94
SWAP6 = 0x95
SWAP7 = 0x96
SWAP8 = 0x97
SWAP9 = 0x98
SWAP10 = 0x99
SWAP11 = 0x9A
SWAP12 = 0x9B
SWAP13 = 0x9C
SWAP14 = 0x9D
SWAP15 = 0x9E
SWAP16 = 0x9F


op_implementation: Dict[Ops, Callable] = {
Ops.STOP: computation_instructions.stop,
Ops.ADD: arithmetic_instructions.add,
Ops.MUL: arithmetic_instructions.mul,
Ops.SUB: arithmetic_instructions.sub,
Ops.DIV: arithmetic_instructions.div,
Ops.SDIV: arithmetic_instructions.sdiv,
Ops.MOD: arithmetic_instructions.mod,
Ops.SMOD: arithmetic_instructions.smod,
Ops.ADDMOD: arithmetic_instructions.addmod,
Ops.MULMOD: arithmetic_instructions.mulmod,
Ops.EXP: arithmetic_instructions.exp,
Ops.SIGNEXTEND: arithmetic_instructions.signextend,
Ops.SSTORE: storage_instructions.sstore,
Ops.PUSH1: stack_instructions.push1,
Ops.PUSH2: stack_instructions.push2,
Ops.PUSH3: stack_instructions.push3,
Ops.PUSH4: stack_instructions.push4,
Ops.PUSH5: stack_instructions.push5,
Ops.PUSH6: stack_instructions.push6,
Ops.PUSH7: stack_instructions.push7,
Ops.PUSH8: stack_instructions.push8,
Ops.PUSH9: stack_instructions.push9,
Ops.PUSH10: stack_instructions.push10,
Ops.PUSH11: stack_instructions.push11,
Ops.PUSH12: stack_instructions.push12,
Ops.PUSH13: stack_instructions.push13,
Ops.PUSH14: stack_instructions.push14,
Ops.PUSH15: stack_instructions.push15,
Ops.PUSH16: stack_instructions.push16,
Ops.PUSH17: stack_instructions.push17,
Ops.PUSH18: stack_instructions.push18,
Ops.PUSH19: stack_instructions.push19,
Ops.PUSH20: stack_instructions.push20,
Ops.PUSH21: stack_instructions.push21,
Ops.PUSH22: stack_instructions.push22,
Ops.PUSH23: stack_instructions.push23,
Ops.PUSH24: stack_instructions.push24,
Ops.PUSH25: stack_instructions.push25,
Ops.PUSH26: stack_instructions.push26,
Ops.PUSH27: stack_instructions.push27,
Ops.PUSH28: stack_instructions.push28,
Ops.PUSH29: stack_instructions.push29,
Ops.PUSH30: stack_instructions.push30,
Ops.PUSH31: stack_instructions.push31,
Ops.PUSH32: stack_instructions.push32,
Ops.DUP1: stack_instructions.dup1,
Ops.DUP2: stack_instructions.dup2,
Ops.DUP3: stack_instructions.dup3,
Ops.DUP4: stack_instructions.dup4,
Ops.DUP5: stack_instructions.dup5,
Ops.DUP6: stack_instructions.dup6,
Ops.DUP7: stack_instructions.dup7,
Ops.DUP8: stack_instructions.dup8,
Ops.DUP9: stack_instructions.dup9,
Ops.DUP10: stack_instructions.dup10,
Ops.DUP11: stack_instructions.dup11,
Ops.DUP12: stack_instructions.dup12,
Ops.DUP13: stack_instructions.dup13,
Ops.DUP14: stack_instructions.dup14,
Ops.DUP15: stack_instructions.dup15,
Ops.DUP16: stack_instructions.dup16,
Ops.SWAP1: stack_instructions.swap1,
Ops.SWAP2: stack_instructions.swap2,
Ops.SWAP3: stack_instructions.swap3,
Ops.SWAP4: stack_instructions.swap4,
Ops.SWAP5: stack_instructions.swap5,
Ops.SWAP6: stack_instructions.swap6,
Ops.SWAP7: stack_instructions.swap7,
Ops.SWAP8: stack_instructions.swap8,
Ops.SWAP9: stack_instructions.swap9,
Ops.SWAP10: stack_instructions.swap10,
Ops.SWAP11: stack_instructions.swap11,
Ops.SWAP12: stack_instructions.swap12,
Ops.SWAP13: stack_instructions.swap13,
Ops.SWAP14: stack_instructions.swap14,
Ops.SWAP15: stack_instructions.swap15,
Ops.SWAP16: stack_instructions.swap16,
}
Loading