Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: cairo1-run expect a different number of arguments than what my main signature provides for when using starknet::circuit #1888

Closed
tdelabro opened this issue Dec 6, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@tdelabro
Copy link
Contributor

tdelabro commented Dec 6, 2024

Hey,
I’m running the following program

fn main(
    raw_inputs: Array<felt252>,
) -> Array<felt252> {
    let inputs: Inputs  = {
    let mut inputs_ref = raw_inputs.span();
     Serde::deserialize(ref inputs_ref).expect('bad program arguments')
    };

   // <do stuffs>
}

output:

cairo1-run target/release/$PROGRAM_NAME.sierra.json \
  --args "[650157136941007917207724121471813482783483983289688285320161307177215463403 1655569645808460179723299787189716707852181277595105475126938070374837146371]"  \
  --layout=$LAYOUT

all good

Now I edit my program:

fn main(
    raw_inputs: Array<felt252>,
) -> Array<felt252> {
    let in1 = CircuitElement::<CircuitInput<0>> {};
    let in2 = CircuitElement::<CircuitInput<1>> {};
    let add = circuit_add(in1, in2);
    let inv = circuit_inverse(add);
    let sub = circuit_sub(inv, in2);
    let mul = circuit_mul(inv, sub);

    let modulus = TryInto::<_, CircuitModulus>::try_into([7, 0, 0, 0]).unwrap();
    let outputs = (mul, add, inv)
        .new_inputs()
        .next([3, 0, 0, 0])
        .next([6, 0, 0, 0])
        .done()
        .eval(modulus)
        .unwrap();

    assert!(outputs.get_output(add) == u384 { limb0: 2, limb1: 0, limb2: 0, limb3: 0 });
    assert!(outputs.get_output(inv) == u384 { limb0: 4, limb1: 0, limb2: 0, limb3: 0 });
    assert!(outputs.get_output(sub) == u384 { limb0: 5, limb1: 0, limb2: 0, limb3: 0 });
    assert!(outputs.get_output(mul) == u384 { limb0: 6, limb1: 0, limb2: 0, limb3: 0 });

    let inputs: Inputs  = {
    let mut inputs_ref = raw_inputs.span();
     Serde::deserialize(ref inputs_ref).expect('bad program arguments')
    };

   // Do things
}

Let’s run:

cairo1-run target/release/$PROGRAM_NAME.sierra.json \
  --args "[650157136941007917207724121471813482783483983289688285320161307177215463403 1655569645808460179723299787189716707852181277595105475126938070374837146371]"  \
  --layout=$LAYOUT


Error: ArgumentsSizeMismatch { expected: 5, actual: 2 }

Oh oh
Adding this code makes cairo1-run expect a different number of arguments now.
This will allow the execution to happen.
"1 2 3 [650157136941007917207724121471813482783483983289688285320161307177215463403 1655569645808460179723299787189716707852181277595105475126938070374837146371]"

@tdelabro tdelabro added the bug Something isn't working label Dec 6, 2024
@FrancoGiachetta
Copy link
Contributor

FrancoGiachetta commented Dec 6, 2024

Hi @tdelabro!
Programs which involve using circuits requiere the mod_builtin feature to be active. This is done so that AddMod, MulMod and RangeCheck96 builtins are taken into account. I would suggest to compile the binary again, making sure that this feature is active. Since the builtins are implicit arguments, this might explain why the program is receiving only 2 arguments. It also explains why the add_mod segment is missing when executing the program with the stone prover.

Let me know if this solves the issue.

@tdelabro
Copy link
Contributor Author

tdelabro commented Dec 6, 2024

@FrancoGiachetta Thanks

This is not part of the latest release you published, the one I was working with: https://github.com/lambdaclass/cairo-vm/releases

And, obviously, it is not mentioned in the readme of the crate: https://github.com/lambdaclass/cairo-vm/tree/main/cairo1-run.
You need to add a section about it explaining when and why to use it

@FrancoGiachetta
Copy link
Contributor

You are totally right. I'll add it to the readme, thanks for pointing that out.

@FrancoGiachetta
Copy link
Contributor

I'll close the issue as it has been solved. Also a PR has been create to document better how to use circuits in cairo 1. If there's anything else pin us!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants