Skip to content

Commit

Permalink
eth/vm/logic/arithmetic: handle exp(X,0) == 1. (#1217)
Browse files Browse the repository at this point in the history
* eth/vm/logic/arithmetic: handle exp(X,0) == 1.

Caught by `exp8.json` test (not yet tracked in tests fixture).

Ref:

https://github.com/ethereum/tests/blob/10ab37c095bb87d2e781bcf112b6104912fccb44/VMTests/vmArithmeticTest/exp8.json

Accidentally caught in PR #1181.

* tests/opcodes: exponentiation: 0^0==1, 0^1==0.
  • Loading branch information
veox authored and pipermerriam committed Aug 28, 2018
1 parent 8752ab5 commit 0c12ccf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
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,),
)
)
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

0 comments on commit 0c12ccf

Please sign in to comment.