Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.
Merged
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
4 changes: 3 additions & 1 deletion eth/vm/logic/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ def exp(computation, gas_per_byte):
bit_size = exponent.bit_length()
byte_size = ceil8(bit_size) // 8

if base == 0:
if exponent == 0:
result = 1
elif base == 0:
result = 0
else:
result = pow(base, exponent, constants.UINT_256_CEILING)
Expand Down
26 changes: 26 additions & 0 deletions tests/core/opcodes/test_opcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,32 @@ def test_mul(vm_class, val1, val2, expected):
assert result == expected


@pytest.mark.parametrize(
'vm_class, base, exponent, expected',
(
(ByzantiumVM, 0, 1, 0,),
(ByzantiumVM, 0, 0, 1,),
(SpuriousDragonVM, 0, 1, 0,),
(SpuriousDragonVM, 0, 0, 1,),
(TangerineWhistleVM, 0, 1, 0,),
(TangerineWhistleVM, 0, 0, 1,),
(HomesteadVM, 0, 1, 0,),
(HomesteadVM, 0, 0, 1,),
(FrontierVM, 0, 1, 0,),
(FrontierVM, 0, 0, 1,),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note: looks like it would be valuable to have a parametrized fixture in conftest that returns all mainnet VMs. Then this would reduce to two cases, and we wouldn't have to remember to update the test on every new fork, like:

@pytest.mark.parametrize(
    'base, exponent, expected',
    (
        (0, 1, 0,),
        (0, 0, 1,),
    )
)
def test_exp(mainnet_vm, base, exponent, expected):
    computation = prepare_computation(mainnet_vm)
    computation.stack_push(exponent)
    computation.stack_push(base)
    computation.opcodes[opcode_values.EXP](computation)
    result = computation.stack_pop(type_hint=constants.UINT256)
    assert result == expected

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets wait for the chain-builder to land as it will make this a lot easier to do

Copy link
Member

@pipermerriam pipermerriam Aug 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chain builder has landed so this should be pretty easy now

)
)
def test_exp(vm_class, base, exponent, expected):
computation = prepare_computation(vm_class)
computation.stack_push(exponent)
computation.stack_push(base)
computation.opcodes[opcode_values.EXP](computation)

result = computation.stack_pop(type_hint=constants.UINT256)

assert result == expected


@pytest.mark.parametrize(
# Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#shl-shift-left
'vm_class, val1, val2, expected',
Expand Down