Skip to content
This repository has been archived by the owner on Jan 9, 2025. It is now read-only.

Commit

Permalink
feat(stack): add pop implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLucqs committed Oct 10, 2022
1 parent d2fb829 commit d943c7c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/zkairvm/stack.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ from starkware.cairo.common.cairo_builtins import HashBuiltin
from starkware.cairo.common.math import assert_lt_felt
from starkware.cairo.common.math_cmp import is_not_zero
from starkware.cairo.common.uint256 import Uint256
from starkware.cairo.common.memcpy import memcpy

// Internal dependencies
from zkairvm.constants import Constants
Expand All @@ -18,7 +19,7 @@ from utils.utils import Helpers
namespace Stack {
func init{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> model.Stack {
alloc_locals;
let (local elements: Uint256*) = alloc();
let (elements: Uint256*) = alloc();
let stack: model.Stack = model.Stack(elements=elements, raw_len=0);
return stack;
}
Expand All @@ -44,11 +45,21 @@ namespace Stack {

func pop{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
self: model.Stack
) -> Uint256 {
) -> (new_stack: model.Stack, element: Uint256) {
alloc_locals;
// TODO: implement me
let element = Uint256(0, 0);
return element;
// get last elt
let len = Stack.len(self);
let element = self.elements[len - 1];
// get new segment for next stack copy
let (new_elements: Uint256*) = alloc();
// get length of new stack copy
let element_size = Uint256.SIZE;
let new_len = len - element_size;
// copy stack without last elt
memcpy(dst=new_elements, src=self.elements, len=new_len);
// create new stack
let new_stack = model.Stack(elements=new_elements, raw_len=new_len);
return (new_stack=new_stack, element=element);
}

func peek{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
Expand Down
3 changes: 3 additions & 0 deletions tests/units/zkairvm/test_stack.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func test_stack{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}
Stack.print_element_at(stack, 1);
Stack.print_element_at(stack, 2);

let (stack, element) = Stack.pop(stack);
assert element = Uint256(3, 0);

return ();
}

Expand Down

0 comments on commit d943c7c

Please sign in to comment.