Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pop instructions #486

Merged
merged 2 commits into from
Jan 17, 2025
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
25 changes: 24 additions & 1 deletion cairo/ethereum/cancun/vm/instructions/stack_instructions.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from starkware.cairo.common.math_cmp import is_le
from starkware.cairo.common.dict import DictAccess, dict_read, dict_write

from ethereum.cancun.vm.stack import push, StackDictAccess, Stack, StackStruct
from ethereum.cancun.vm.stack import push, StackDictAccess, Stack, StackStruct, pop as stack_pop
from ethereum.cancun.vm import Evm, EvmImpl
from ethereum.cancun.vm.exceptions import ExceptionalHalt, StackUnderflowError
from ethereum.cancun.vm.gas import charge_gas, GasConstants
Expand Down Expand Up @@ -109,6 +109,29 @@ func dup_n{range_check_ptr, evm: Evm}(item_number: Uint) -> ExceptionalHalt* {
return ok;
}

func pop{range_check_ptr, evm: Evm}() -> ExceptionalHalt* {
alloc_locals;
// STACK
let stack = evm.value.stack;
with stack {
let (value, err) = stack_pop();
if (cast(err, felt) != 0) {
return err;
}
}

// GAS
let err = charge_gas(Uint(GasConstants.GAS_BASE));
if (cast(err, felt) != 0) {
return err;
}

// PROGRAM COUNTER
EvmImpl.set_pc_stack(Uint(evm.value.pc.value + 1), stack);
let ok = cast(0, ExceptionalHalt*);
return ok;
}

func push0{range_check_ptr, evm: Evm}() -> ExceptionalHalt* {
return push_n{evm=evm}(Uint(0));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@
from tests.utils.evm_builder import EvmBuilder


class TestPop:
@given(evm=EvmBuilder().with_stack().with_gas_left().with_code().build())
def test_pop(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("pop", evm)
except ExceptionalHalt as cairo_error:
with strict_raises(type(cairo_error)):
stack.pop(evm)
return

stack.pop(evm)
assert evm == cairo_result


class TestPushN:
@pytest.mark.parametrize("num_bytes", range(33))
@given(evm=EvmBuilder().with_stack().with_gas_left().with_code().build())
Expand Down
Loading