Skip to content

Commit 0c12ccf

Browse files
veoxpipermerriam
authored andcommitted
eth/vm/logic/arithmetic: handle exp(X,0) == 1. (#1217)
* 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.
1 parent 8752ab5 commit 0c12ccf

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

eth/vm/logic/arithmetic.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ def exp(computation, gas_per_byte):
147147
bit_size = exponent.bit_length()
148148
byte_size = ceil8(bit_size) // 8
149149

150-
if base == 0:
150+
if exponent == 0:
151+
result = 1
152+
elif base == 0:
151153
result = 0
152154
else:
153155
result = pow(base, exponent, constants.UINT_256_CEILING)

tests/core/opcodes/test_opcodes.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,32 @@ def test_mul(vm_class, val1, val2, expected):
123123
assert result == expected
124124

125125

126+
@pytest.mark.parametrize(
127+
'vm_class, base, exponent, expected',
128+
(
129+
(ByzantiumVM, 0, 1, 0,),
130+
(ByzantiumVM, 0, 0, 1,),
131+
(SpuriousDragonVM, 0, 1, 0,),
132+
(SpuriousDragonVM, 0, 0, 1,),
133+
(TangerineWhistleVM, 0, 1, 0,),
134+
(TangerineWhistleVM, 0, 0, 1,),
135+
(HomesteadVM, 0, 1, 0,),
136+
(HomesteadVM, 0, 0, 1,),
137+
(FrontierVM, 0, 1, 0,),
138+
(FrontierVM, 0, 0, 1,),
139+
)
140+
)
141+
def test_exp(vm_class, base, exponent, expected):
142+
computation = prepare_computation(vm_class)
143+
computation.stack_push(exponent)
144+
computation.stack_push(base)
145+
computation.opcodes[opcode_values.EXP](computation)
146+
147+
result = computation.stack_pop(type_hint=constants.UINT256)
148+
149+
assert result == expected
150+
151+
126152
@pytest.mark.parametrize(
127153
# Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#shl-shift-left
128154
'vm_class, val1, val2, expected',

0 commit comments

Comments
 (0)