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

Panic in cairo1-run When Using `Felt252Dict<Nullable<T>>` in Cairo Program #1622

Closed raphaelDkhn closed 6 months ago

raphaelDkhn commented 6 months ago

Describe the bug When running a Cairo program using the cairo1-run command, a panic occurs if the program defines a function that uses a Felt252Dict with Nullable<T> values, where T represents any type supported by Cairo.

To Reproduce The issue can be reproduced with the following Cairo code snippet:

use core::nullable::{nullable_from_box, match_nullable, FromNullableResult};

fn main() {
    // Create the dictionary
    let mut d: Felt252Dict<Nullable<Span<felt252>>> = Default::default();

    // Create the array to insert
    let a = array![8, 9, 10];

    // Insert it as a `Span`
    d.insert(0, nullable_from_box(BoxTrait::new(a.span())));
}

The program is executed using the command:

cargo run nullable_dict.cairo --layout all_cairo

This results in the following panic message:

Error: VirtualMachine(Hint((0, Internal(Memory(ExpectedInteger(Relocatable { segment_index: 1, offset: 23 }))))))

What version/commit are you on? Cairo VM version/commit: 1f98fef

Additional Context This issue seems to be related to the handling of Nullable<T> types within Felt252Dict in the Cairo VM. However I tried to run the following cairo program, and it ran successfully:

fn main() {
     let d = NullableTrait::new(1);
 }

Any assistance in resolving this panic would be greatly appreciated. Thank you :)

fmoletta commented 6 months ago

This has been fixed by #1624