Skip to content

refactor(cast): unify downcast felt252 canonicalization branches - #1653

Merged
TomerStarkware merged 1 commit into
mainfrom
tomer/unify-downcast-felt252-canonicalization
Aug 12, 2026
Merged

refactor(cast): unify downcast felt252 canonicalization branches#1653
TomerStarkware merged 1 commit into
mainfrom
tomer/unify-downcast-felt252-canonicalization

Conversation

@TomerStarkware

@TomerStarkware TomerStarkware commented Aug 5, 2026

Copy link
Copy Markdown
Collaborator

Closes #NA

Follow-up to #1652. 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 — so this merges them into a single threshold-select.


This change is Reviewable

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>
@TomerStarkware
TomerStarkware requested a review from orizi August 5, 2026 14:19
@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown

Benchmarking results

Benchmark for program dict_insert

Open benchmarks
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

@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown

Benchmark results Main vs HEAD.

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_insert.cairo (JIT) 1.923 ± 0.048 1.848 2.009 1.04 ± 0.03
base dict_insert.cairo (AOT) 1.852 ± 0.018 1.828 1.883 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_insert.cairo (JIT) 1.906 ± 0.019 1.885 1.949 1.01 ± 0.01
head dict_insert.cairo (AOT) 1.880 ± 0.011 1.862 1.895 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_snapshot.cairo (JIT) 1.671 ± 0.021 1.650 1.712 1.00
base dict_snapshot.cairo (AOT) 1.687 ± 0.015 1.657 1.705 1.01 ± 0.02

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_snapshot.cairo (JIT) 1.692 ± 0.012 1.670 1.711 1.00
head dict_snapshot.cairo (AOT) 1.721 ± 0.024 1.665 1.738 1.02 ± 0.02

Base

Command Mean [s] Min [s] Max [s] Relative
base factorial_2M.cairo (JIT) 1.780 ± 0.022 1.747 1.813 1.02 ± 0.04
base factorial_2M.cairo (AOT) 1.747 ± 0.061 1.677 1.847 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head factorial_2M.cairo (JIT) 1.766 ± 0.014 1.743 1.794 1.00 ± 0.02
head factorial_2M.cairo (AOT) 1.763 ± 0.024 1.732 1.806 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base fib_2M.cairo (JIT) 1.695 ± 0.037 1.655 1.772 1.01 ± 0.03
base fib_2M.cairo (AOT) 1.686 ± 0.038 1.642 1.741 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head fib_2M.cairo (JIT) 1.741 ± 0.023 1.719 1.801 1.01 ± 0.03
head fib_2M.cairo (AOT) 1.723 ± 0.038 1.677 1.818 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base linear_search.cairo (JIT) 1.728 ± 0.020 1.703 1.759 1.01 ± 0.02
base linear_search.cairo (AOT) 1.713 ± 0.031 1.668 1.760 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head linear_search.cairo (JIT) 1.786 ± 0.023 1.758 1.828 1.00
head linear_search.cairo (AOT) 1.789 ± 0.057 1.714 1.899 1.00 ± 0.03

Base

Command Mean [s] Min [s] Max [s] Relative
base logistic_map.cairo (JIT) 1.733 ± 0.031 1.697 1.795 1.04 ± 0.03
base logistic_map.cairo (AOT) 1.663 ± 0.042 1.599 1.747 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head logistic_map.cairo (JIT) 1.770 ± 0.030 1.725 1.818 1.02 ± 0.02
head logistic_map.cairo (AOT) 1.736 ± 0.030 1.705 1.802 1.00

@orizi orizi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

@orizi reviewed 1 file and all commit messages, and made 1 comment.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).

@TomerStarkware
TomerStarkware added this pull request to the merge queue Aug 12, 2026
Merged via the queue into main with commit 5e2c8b6 Aug 12, 2026
15 checks passed
@TomerStarkware
TomerStarkware deleted the tomer/unify-downcast-felt252-canonicalization branch August 12, 2026 08:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants