starkware-libs / cairo

Cairo is the first Turing-complete language for creating provable programs for general computation.
Apache License 2.0
1.49k stars 456 forks source link

bug: Problems with Array Arguments in Cairo Runner #4747

Closed neotheprogramist closed 6 months ago

neotheprogramist commented 6 months ago

Description

While developing a Cairo runner, an issue has arisen related to passing arrays as arguments to the program's main function. The goal was to create a runner enabling the passing of arguments to the entry point, demonstrated by the following program:

fn main(n: felt252) -> felt252 {
    fib(n)
}

However, when using Arg from cairo_lang_runner::{Arg, SierraCasmRunner} to pass arrays to the main function of a Cairo program, it results in unstable and inefficient behavior.

Steps to Reproduce

To reproduce the issue, follow these steps:

  1. Clone the repository:
    git clone https://github.com/neotheprogramist/cairo-args-runner
  2. Checkout the array-issues branch:
    git checkout array-issues
  3. Run the first test case:
    echo "[[1, 1, 1], [2, 2, 2]]" | cargo run --release -- examples/arrays2/target/dev/arrays2.sierra.json

    Expected Result: '[3, 3, 3, 6]' (arr1_len, arr1_sum, arr2_len, arr2_sum), but actual result is '[3, 6, 3, 3]' (content of arr1 and arr2 is swapped).

  4. Run the second test case:
    echo "[[1, 1, 1], [2, 2, 2, 2]]" | cargo run --release -- examples/lengths/target/dev/lengths.sierra.json

    Note: When arr1 and arr2 have different lengths, the lengths are not swapped, and you cannot access the longer array beyond the length of the shorter one.

  5. Run the third test case:
    echo "[[1, 1, 1], [2, 2, 2, 2]]" | cargo run --release -- examples/arrays2/target/dev/arrays2.sierra.json

    Result: CairoRunError(VirtualMachine(FailedToComputeOperands(("op1", Relocatable { segment_index: 10, offset: 3 })))) Error: RunError(FailedRunning)

Additional Question

Concerning array serialization, when passing an array as (n, a_1, a_2, ..., a_n) using Arg::Value, the results are unexpected. For instance:

echo "[1, 1, 1, 1]" | cargo run --release -- examples/serialization2/target/dev/serialization2.sierra.json

Is there documentation on how arrays should be passed to the Sierra program as an array of Arg::Value?

Repository Link

GitHub Repository - cairo-args-runner

orizi commented 6 months ago

Arrays are represented as pointers to the begining and the end of the data. Is this an enhancement requirement? because i don't understand the bug - main runner does not accept arguments currently at all.

neotheprogramist commented 6 months ago

@orizi I recognized it as a bug because I think I'm using cairo_lang_runner (which is a part of cairo compiler repo) correctly but in my opinion it produces incorrect results. The main issue is how to use cairo_lang_runner to pass arrays as arguments correctly to the program. I'm aware that main runner does not support this feature - that's why I've built my own runner using cairo compiler components.

orizi commented 6 months ago

Oh - i better understand now. Having a look.