Cairo consts that didn't match runtime: what to check in classes built before 2.21

Until Cairo 2.21 this compiled without a warning:

#[derive(Drop, Copy)]
struct S {
    a: u8,
    b: u8,
}

impl SPartialEq of PartialEq<S> {
    fn eq(lhs: @S, rhs: @S) -> bool {
        lhs.a == rhs.a
    }
}

const SNAP_EQ: bool = @S { a: 1, b: 2 } == @S { a: 1, b: 3 };

SNAP_EQ is false. Write the same comparison inside a function and it’s true, because at runtime your eq runs and it ignores b. The const evaluator let the comparison in because it goes through core’s snapshot impl, and then it compared the two structs field by field, so your eq never ran. Tuples and a derived Option took the same path. The repro is in issue 10394, and 2.21 now rejects these consts with UnsupportedConstant (PR 10395).

After that one I went through this year’s merged compiler PRs looking for the same thing, a value worked out at build time that doesn’t match what the same code does when it runs. Most of what I found were compiler crashes, which don’t matter once a class is declared. A few change what the compiled code does.

More of the same this year

-1 and PRIME - 1 are the same field element, but before 2.19.0 the const evaluator could hold them as two different numbers. A const == between them was false, and a const match could take the wrong arm. PR 9959 fixed it for plain felts, and PR 10037 for felts inside structs, enums, tuples and NonZero.

Before 2.20.0, when the optimizer folded storage_base_address_from_felt252 on a constant at or above 2^251 - 256, it kept the raw value, while the runtime subtracts that bound first (PR 10210). Storage variable addresses are an sn_keccak of the name, under 2^250, so this mostly matters for code that builds addresses by hand from constants like -1.

const C: i8 = -128_i8 % -1; still folds to 0 in the current release, while the same remainder at runtime panics with ‘attempt to divide with overflow’. Division by -1 in a const was already rejected and % slipped through. PR 10470 fixed it on main on 4 November, after 2.21.0 was cut.

Rebuilding is the check

The folded value is part of the Sierra you declared. The network only compiles Sierra to CASM, so a newer compiler on the sequencer side doesn’t touch a constant that’s already in your class. A class built with 2.18 keeps its values until you declare a new one.

Finding what built a live class takes one call. starknet_getClassAt returns sierra_program, and its first six felts are the Sierra version and then the compiler version, three felts each (felt252_serde.rs at v2.21.0). A mainnet class I read this week starts with 1, 7, 0, 2, 15, 0, so Sierra 1.7.0 built by Cairo 2.15.0.

For anything older than 2.21 I’d rebuild the same commit with Scarb 2.21. A new UnsupportedConstant on a const that compiled before is the PartialEq case, and it means the deployed class holds a value you didn’t mean. The felt and storage cases give you no new error, so for those I’d grep the source for const comparisons or matches on felts that can be negative, and for storage_base_address_from_felt252 called with a constant.

Tests use the same compiler

A test of a function that uses the const gets the same folded value, so it only fails if it asserts the value you meant, written out by hand. The repro in PR 10470 runs the same remainder through an #[inline(never)] identity function so the optimizer can’t fold the runtime side too. I’d write that kind of test for any const that guards something, like a fee cap or a role flag.

If your CI still runs cairo-test, it had its own problem before 2.21. It looked tests up by name suffix, so with foo::a::foo::b::check and foo::b::check in one crate it ran the first one twice and the failing test reported ok (issue 10392).