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
509 stars 142 forks source link

Passing Struct Arguments to CairoRun #1506

Closed raphaelDkhn closed 9 months ago

raphaelDkhn commented 9 months ago

I'm working with CairoVM to execute Cairo programs including arguments. However, I'm facing challenges when these arguments are complex data structures.

The program I aim to run using CairoVM is as follows:

struct Tensor<T> {
    shape: Span<usize>,
    data: Span<T>,
}

fn predict(x: Tensor<u32>) -> Tensor<u32> {
    // ... (program logic here)
}

The challenge arises with the argument, which is a struct in this case. I'm struggling to properly construct this struct argument to pass it to cairo_runner.run_from_entrypoint.

As a temporary workaround, I thought to serialize the struct and passing the array of felt into the function instead. However, I'm seeking a more efficient or appropriate method for handling this. Any suggestions or guidance would be greatly appreciated.

pefontana commented 9 months ago

Hi @raphaelDkhn ! Can you leave here the .cairo program and the code where you are executing the cairo_runner.run_from_entrypoint, so we can replicate it?

raphaelDkhn commented 9 months ago

Hi @pefontana !

Here you have the cairo program.

It takes a Tensor struct (from Orion lib) as argument. I'm not sure how to pass the struct in call data when I call run_from_entrypoint function here.

Thank you!

raphaelDkhn commented 9 months ago

I finally got it to work by passing the struct as a serialized array:

fn main() {
    let program_bytes = include_bytes!("../example/target/dev/example.casm.json");
    let program = serde_json::from_slice::<CasmContractClass>(program_bytes).unwrap();
    let program_builtins = get_casm_contract_builtins(&program, 0);

    let calldata: Vec<MaybeRelocatable> = vec![
        MaybeRelocatable::from(2),
        MaybeRelocatable::from(2),
        MaybeRelocatable::from(2),
        MaybeRelocatable::from(4),
        MaybeRelocatable::from(1),
        MaybeRelocatable::from(2),
        MaybeRelocatable::from(3),
        MaybeRelocatable::from(4),
    ];

    let _ret = run_cairo_1_entrypoint(&program, &program_builtins, 0, &calldata);
}
pefontana commented 9 months ago

Great @raphaelDkhn ! So, we can help you with something more?

raphaelDkhn commented 9 months ago

I think, all good! We can close the issue. Thank you!

pefontana commented 9 months ago

Thank you @raphaelDkhn !! Any other issue you encounter please ask us, happy to help