fix(felt252): canonicalize non-canonical felt252_const literals - #1660
Open
TomerStarkware wants to merge 1 commit into
Open
fix(felt252): canonicalize non-canonical felt252_const literals#1660TomerStarkware wants to merge 1 commit into
TomerStarkware wants to merge 1 commit into
Conversation
felt_to_unsigned only mapped negatives to PRIME - |v| and left positives untouched, so felt252_const<PRIME> (or any literal >= PRIME, or a negative with |v| > PRIME) produced a non-canonical i252 bit pattern. felt252_is_zero and other raw-bit comparisons then diverged from the VM (const<PRIME> took the non-zero branch). Reduce mod PRIME in both arms so the result is always in [0, PRIME). Adds a unit test for felt_to_unsigned and a hand-written Sierra regression test for felt252_const<C> -> felt252_is_zero. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Benchmarking resultsBenchmark for program
|
| Command | Mean [s] | Min [s] | Max [s] | Relative |
|---|---|---|---|---|
Cairo-vm (Rust, Cairo 1) |
10.508 ± 0.051 | 10.402 | 10.580 | 5.27 ± 0.07 |
cairo-native (embedded AOT) |
1.995 ± 0.026 | 1.968 | 2.044 | 1.00 |
cairo-native (embedded JIT using LLVM's ORC Engine) |
2.012 ± 0.049 | 1.941 | 2.078 | 1.01 ± 0.03 |
Benchmark for program dict_snapshot
Open benchmarks
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
Cairo-vm (Rust, Cairo 1) |
569.3 ± 10.3 | 556.1 | 587.1 | 1.00 |
cairo-native (embedded AOT) |
1786.8 ± 27.6 | 1728.7 | 1829.5 | 3.14 ± 0.07 |
cairo-native (embedded JIT using LLVM's ORC Engine) |
1786.6 ± 25.9 | 1743.4 | 1829.2 | 3.14 ± 0.07 |
Benchmark for program factorial_2M
Open benchmarks
| Command | Mean [s] | Min [s] | Max [s] | Relative |
|---|---|---|---|---|
Cairo-vm (Rust, Cairo 1) |
4.615 ± 0.074 | 4.564 | 4.813 | 2.58 ± 0.06 |
cairo-native (embedded AOT) |
1.791 ± 0.034 | 1.761 | 1.884 | 1.00 |
cairo-native (embedded JIT using LLVM's ORC Engine) |
1.828 ± 0.032 | 1.783 | 1.882 | 1.02 ± 0.03 |
Benchmark for program fib_2M
Open benchmarks
| Command | Mean [s] | Min [s] | Max [s] | Relative |
|---|---|---|---|---|
Cairo-vm (Rust, Cairo 1) |
4.513 ± 0.052 | 4.452 | 4.641 | 2.52 ± 0.05 |
cairo-native (embedded AOT) |
1.787 ± 0.026 | 1.753 | 1.840 | 1.00 |
cairo-native (embedded JIT using LLVM's ORC Engine) |
1.844 ± 0.049 | 1.794 | 1.974 | 1.03 ± 0.03 |
Benchmark for program linear_search
Open benchmarks
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
Cairo-vm (Rust, Cairo 1) |
631.5 ± 10.2 | 614.6 | 643.9 | 1.00 |
cairo-native (embedded AOT) |
1826.0 ± 49.1 | 1766.7 | 1925.3 | 2.89 ± 0.09 |
cairo-native (embedded JIT using LLVM's ORC Engine) |
1822.0 ± 32.0 | 1761.9 | 1853.5 | 2.89 ± 0.07 |
Benchmark for program logistic_map
Open benchmarks
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
Cairo-vm (Rust, Cairo 1) |
520.3 ± 10.2 | 507.5 | 536.2 | 1.00 |
cairo-native (embedded AOT) |
1768.3 ± 26.3 | 1716.1 | 1805.3 | 3.40 ± 0.08 |
cairo-native (embedded JIT using LLVM's ORC Engine) |
1788.3 ± 61.3 | 1728.1 | 1901.6 | 3.44 ± 0.14 |
Benchmark results Main vs HEAD.Base
Head
Base
Head
Base
Head
Base
Head
Base
Head
Base
Head
|
orizi
approved these changes
Aug 30, 2026
orizi
left a comment
Collaborator
There was a problem hiding this comment.
@orizi reviewed 2 files and all commit messages, and made 1 comment.
Reviewable status:complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
felt_to_unsigned(src/utils.rs) only mapped negatives toPRIME - |v|and left positives untouched, sofelt252_const<PRIME>(or any literal>= PRIME, or a negative with|v| > PRIME) produced a non-canonical i252 bit pattern.felt252_is_zerocompares raw bits, sofelt252_const<PRIME> -> felt252_is_zerotook the non-zero branch while the VM takes the zero branch. Divergence reproduces at all OptLevels.Fix: reduce mod
PRIMEin both arms so the result is always in[0, PRIME). This also covers the otherfelt_to_unsignedcallers (felt252 binary ops with a const operand,const_as_immediate, starknet consts).Reach: not via on-chain declare (contract-class Sierra caps literals at PRIME−1), but reachable for any consumer fed a
.sierra/.sierra.json/ programmaticProgram(tooling,cairo-native-run, test runners).Tests
utils::tests::test_felt_to_unsigned_canonicalizes(0, 5, -1, PRIME, PRIME+5, -PRIME, -PRIME-1, -2·PRIME+3).felt252::test::felt252_const_non_canonical_is_zero— fails on main (const<PRIME>→ 222), passes with the fix (→ 111).cargo test --lib -- felt252 const starknet: 144 passed; clippy clean.🤖 Generated with Claude Code
This change is