lambdaclass / cairo-vm

cairo-vm is a Rust implementation of the Cairo VM. Cairo (CPU Algebraic Intermediate Representation) is a programming language for writing provable programs, where one party can prove to another that a certain computation was executed correctly without the need for this party to re-execute the same program.
https://lambdaclass.github.io/cairo-vm
Apache License 2.0
504 stars 138 forks source link

Double unwrap for `program_base` in Cairo runner `initialize_state` #1638

Closed tcoratger closed 5 months ago

tcoratger commented 6 months ago

Issue Description

In the initialize_state function of the CairoRunner structure, there is redundant code in my opinion, that can be improved for clarity and efficiency.

At the beginning of the function, there is a conditional check (if let Some(prog_base) = self.program_base) to ensure that self.program_base is not None.

https://github.com/lambdaclass/cairo-vm/blob/4ea224a1068b43aaa3899d24e834475106e8a78f/vm/src/vm/runners/cairo_runner.rs#L414-L431

However, a few lines later, the unwrap method is used again on self.program_base to obtain base, and in case of None, it sets base to Relocatable::from((0, 0)).

https://github.com/lambdaclass/cairo-vm/blob/4ea224a1068b43aaa3899d24e834475106e8a78f/vm/src/vm/runners/cairo_runner.rs#L425-L427

This redundant check and assignment can be eliminated because the None case will never happen inside the if statement, making the base declaration unnecessary. Instead, we can directly use the prog_base variable in the loop to mark memory segments as accessed.