refactor(cast): unify downcast felt252 canonicalization branches - #1653
Merged
TomerStarkware merged 1 commit intoAug 12, 2026
Merged
Conversation
Both arms of the felt252 canonicalization in build_downcast reduce to "subtract PRIME when the felt exceeds a threshold": 0 for a non-positive destination range, HALF_PRIME for one that straddles zero. This also fixes a VM divergence: the non-positive branch subtracted PRIME unconditionally, interpreting felt 0 as -PRIME. For a destination whose lower bound is exactly 1 - PRIME no lower-bound check is emitted, so downcast<felt252, BoundedInt<1-P, U<=0>>(0) returned Some where the VM returns None (and under-charged the range check builtin, 2 vs 3). With the 0 threshold, felt 0 stays 0 and is rejected by the upper-bound check, matching the VM on both the value and the builtin counter. 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) |
8.321 ± 0.102 | 8.235 | 8.598 | 5.76 ± 0.08 |
cairo-native (embedded AOT) |
1.444 ± 0.007 | 1.432 | 1.458 | 1.00 |
cairo-native (embedded JIT using LLVM's ORC Engine) |
1.463 ± 0.007 | 1.450 | 1.472 | 1.01 ± 0.01 |
Benchmark for program dict_snapshot
Open benchmarks
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
Cairo-vm (Rust, Cairo 1) |
420.4 ± 4.9 | 414.9 | 430.6 | 1.00 |
cairo-native (embedded AOT) |
1289.7 ± 6.3 | 1278.5 | 1296.1 | 3.07 ± 0.04 |
cairo-native (embedded JIT using LLVM's ORC Engine) |
1312.1 ± 3.6 | 1306.9 | 1318.4 | 3.12 ± 0.04 |
Benchmark for program factorial_2M
Open benchmarks
| Command | Mean [s] | Min [s] | Max [s] | Relative |
|---|---|---|---|---|
Cairo-vm (Rust, Cairo 1) |
3.598 ± 0.074 | 3.558 | 3.805 | 2.68 ± 0.06 |
cairo-native (embedded AOT) |
1.342 ± 0.006 | 1.328 | 1.351 | 1.00 |
cairo-native (embedded JIT using LLVM's ORC Engine) |
1.355 ± 0.005 | 1.342 | 1.361 | 1.01 ± 0.01 |
Benchmark for program fib_2M
Open benchmarks
| Command | Mean [s] | Min [s] | Max [s] | Relative |
|---|---|---|---|---|
Cairo-vm (Rust, Cairo 1) |
3.516 ± 0.062 | 3.480 | 3.688 | 2.71 ± 0.05 |
cairo-native (embedded AOT) |
1.296 ± 0.008 | 1.283 | 1.307 | 1.00 |
cairo-native (embedded JIT using LLVM's ORC Engine) |
1.317 ± 0.005 | 1.310 | 1.325 | 1.02 ± 0.01 |
Benchmark for program linear_search
Open benchmarks
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
Cairo-vm (Rust, Cairo 1) |
448.3 ± 4.5 | 441.5 | 454.5 | 1.00 |
cairo-native (embedded AOT) |
1306.3 ± 2.7 | 1302.6 | 1311.4 | 2.91 ± 0.03 |
cairo-native (embedded JIT using LLVM's ORC Engine) |
1331.2 ± 5.4 | 1323.0 | 1339.7 | 2.97 ± 0.03 |
Benchmark for program logistic_map
Open benchmarks
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
Cairo-vm (Rust, Cairo 1) |
383.1 ± 3.4 | 379.1 | 390.3 | 1.00 |
cairo-native (embedded AOT) |
1287.4 ± 5.4 | 1279.8 | 1296.1 | 3.36 ± 0.03 |
cairo-native (embedded JIT using LLVM's ORC Engine) |
1321.1 ± 5.4 | 1313.5 | 1328.7 | 3.45 ± 0.03 |
Benchmark results Main vs HEAD.Base
Head
Base
Head
Base
Head
Base
Head
Base
Head
Base
Head
|
orizi
approved these changes
Aug 11, 2026
orizi
left a comment
Collaborator
There was a problem hiding this comment.
@orizi reviewed 1 file and all commit messages, and made 1 comment.
Reviewable status:complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).
TomerStarkware
deleted the
tomer/unify-downcast-felt252-canonicalization
branch
August 12, 2026 08:36
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.
Closes #NA
Follow-up to #1652. Both arms of the felt252 canonicalization in
build_downcastreduce to "subtract PRIME when the felt exceeds a threshold" —0for a non-positive destination range,HALF_PRIMEfor one that straddles zero — so this merges them into a single threshold-select.This change is