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
514 stars 144 forks source link

Constant Execution Step Count in Proof Mode for Cairo1 Programs #1738

Closed raphaelDkhn closed 5 months ago

raphaelDkhn commented 5 months ago

I am encountering a bug where the step count remains constant in proof_mode, regardless of the input, when executing a Fibonacci program in Cairo1.

Steps to Reproduce:

  1. I'm using the following Fibonacci program written in Cairo1:
    
    use core::felt252;

fn main(n: felt252) -> felt252 { let result = fib(1, 1, n); result }

fn fib(a: felt252, b: felt252, n: felt252) -> felt252 { match n { 0 => a, _ => fib(b, a + b, n - 1), } }

2. To fetch the number of steps, the following code is added in `cairo1-run/main.rs`:
```rust
let (runner, vm, _, serialized_output) = cairo_run_program(&sierra_program, cairo_run_config)?;
let resources = runner.get_execution_resources(&vm)?;
let n_steps = resources.n_steps;

println!("Number Steps: {}", n_steps);
  1. Execution results without proof mode:
    
    $ cargo run ./programs/fib.sierra.json --layout all_cairo --args '1'
    >>> Number Steps: 17

$ cargo run ./programs/fib.sierra.json --layout all_cairo --args '50'

Number Steps: 311


4. Execution results with proof mode:
```shell
$ cargo run ./programs/fib.sierra.json --layout all_cairo --args '1' --proof_mode
Number Steps: 32768

$ cargo run ./programs/fib.sierra.json --layout all_cairo --args '50' --proof_mode

Number Steps: 32768

It appears that in proof mode, the number of steps is consistently 32,768 regardless of the number of iterations in the Fibonacci function.

Expected Behavior:

The number of steps should vary based on the input parameter n, similar to the behavior observed without proof mode.

Actual Behavior:

In proof mode, the number of steps remains constant at 32,768 for any input.

Version/Commit:

I am on commit c5839fd.

Additional Context:

Similar behavior has been detected on more complex programs. I suspect that the origin of this bug causes fake proofs when trace/memory files are submitted to Platinum, as the proof time is too fast, even on very complex programs.

Okm165 commented 5 months ago

I think this is expected behaviour coz the proof mode is adding infinite loop at the end of the program to fill trace to the closest power of two. It should jump up to next power of two if u supply really big fib num. Power of two sized trace is required in proof mode coz it is needed for FFT to work on the prover side. Regular execution is not adding this kind of stuff so it behaves more naturally. Afaik :)

raphaelDkhn commented 5 months ago

I think this is expected behaviour coz the proof mode is adding infinite loop at the end of the program to fill trace to the closest power of two. It should jump up to next power of two if u supply really big fib num. Power of two sized trace is required in proof mode coz it is needed for FFT to work on the prover side. Regular execution is not adding this kind of stuff so it behaves more naturally. Afaik :)

Thanks for your answer @Okm165, pretty clear!