kkrt-labs / cairo-vm-ts

A typescript implementation of the Cairo VM
Apache License 2.0
20 stars 13 forks source link

research: builtin stack creation and use of `initial_stack` method in Cairo VM #63

Closed zmalatrax closed 5 months ago

zmalatrax commented 5 months ago

Every builtins implement the same method initial_stack(self) which returns the value to append to the stack during initialization: either the start address of the segment or nothing.

def initial_stack(self) -> List[MaybeRelocatable]:
    return [self.base] if self.included else []

The flags self.included is true if the builtin (its name) is in program.builtins. self.included is false when the builtins used by the program is a sub-set of the layout of the run, those builtins are not initialized.

Therefore, there is no need to implement an initialStack() method as this behaviour is already handled in addSegment: we return the start address of the segment when we initialize it.

However, there is the flag allow_missing_builtins (used in proof mode) which allows to run a program with builtins that are not included in the layout of the run. For those builtins, the value 0 is appended to the stack and the segment is initialized. So, when we'll implement the proof mode, we might need to handle this case but for now, no need to change how the stack is created.

Source from CairoVM Python: builtin runner initial_stack(), builtin initialization, stack creation, builtin factory, included flag value

zmalatrax commented 5 months ago

Actually the SegmentArena builtin has a different initialization than the other builtins (initial_stack() having the same behaviour though).

Therefore, when this builtin will be implemented, we'll have to think how addSegment(builtin: ProxyHandler) should be updated.

segment_arena.cairo