Skip to content

perf(felt252): compute felt252_div via a runtime binding#1648

Merged
TomerStarkware merged 1 commit into
mainfrom
tomer/felt252_div_pr
Jul 1, 2026
Merged

perf(felt252): compute felt252_div via a runtime binding#1648
TomerStarkware merged 1 commit into
mainfrom
tomer/felt252_div_pr

Conversation

@TomerStarkware

@TomerStarkware TomerStarkware commented Jun 30, 2026

Copy link
Copy Markdown
Collaborator

Stacked on #1647 (felt252_mul binding). Review/merge that first; this PR's base is tomer/felt252_mul_pr, so it shows only the division change.

What

Route felt252 division through a runtime binding (cairo_native__felt252_div) instead of lowering it inline as an extended-euclidean modular inverse followed by an i512 multiply-and-remainder.

Why

The inline form has the same hazard the mul binding removed, and then some: it legalizes into huge limb sequences — the i512 remainder being the worst offender — which makes O3 register allocation of division-heavy contracts pathologically slow. It also computed the modular inverse via a large MLIR extended-euclidean loop.

The binding uses starknet-types-core's field_div (optimized Montgomery inverse). rhs originates from a NonZero<felt252>, so it is guaranteed nonzero and from_felt_unchecked is safe. No builtin/range-check accounting is involved (felt division is a pure field op).

Changes

  • src/runtime.rscairo_native__felt252_div via field_div
  • src/metadata/runtime_bindings.rsFelt252Div binding (symbol, fn-ptr, emitter, setup_runtime); removed the now-dead ExtendedEuclideanWidth::U252, which was constructed only by the old division path (U31/U256/U384 remain, used by egcd / u256-invmod)
  • src/libfuncs/felt252.rsDiv routed to the binding; inline euclidean + i512 multiply/remainder removed

All felt252 libfunc tests pass (incl. felt252_div/felt252_div_const: division-by-zero panics, valid quotients, and negative operands).

🤖 Generated with Claude Code


This change is Reviewable

@github-actions

github-actions Bot commented Jun 30, 2026

Copy link
Copy Markdown

✅ Code is now correctly formatted.

@TomerStarkware TomerStarkware requested a review from orizi June 30, 2026 10:58

@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.

@orizi made 2 comments.
Reviewable status: 0 of 3 files reviewed, 2 unresolved discussions (waiting on TomerStarkware).


src/metadata/runtime_bindings.rs line 326 at r1 (raw file):

    /// little-endian felt252 buffers; the result is written to `dst_ptr`.
    #[allow(clippy::too_many_arguments)]
    pub fn felt252_div<'c, 'a>(

put near mul.


src/metadata/runtime_bindings.rs line 333 at r1 (raw file):

        dst_ptr: Value<'c, '_>,
        lhs_ptr: Value<'c, '_>,
        rhs_ptr: Value<'c, '_>,

and remove the allow.

Suggestion:

        [dst_ptr, lhs_ptr, rhs_ptr]: [Value<'c, '_>; 3],

@TomerStarkware TomerStarkware force-pushed the tomer/felt252_mul_pr branch 2 times, most recently from f876fb2 to 1cb3979 Compare June 30, 2026 13:13

@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.

@orizi made 1 comment.
Reviewable status: 0 of 3 files reviewed, 3 unresolved discussions (waiting on TomerStarkware).


-- commits line 1 at r2:
rebase

@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.

@orizi made 1 comment.
Reviewable status: 0 of 3 files reviewed, 4 unresolved discussions (waiting on TomerStarkware).


src/runtime.rs line 139 at r1 (raw file):

///
/// `rhs` originates from a `NonZero<felt252>`, so it is guaranteed nonzero;
/// `field_div` uses the optimized Montgomery inverse.

move near mul

@TomerStarkware TomerStarkware force-pushed the tomer/felt252_div_pr branch 2 times, most recently from 1f69923 to 79423e0 Compare June 30, 2026 14:37
Base automatically changed from tomer/felt252_mul_pr to main June 30, 2026 14:55
Route felt252 division through a runtime binding (cairo_native__felt252_div)
instead of lowering it inline as an extended-euclidean modular inverse plus
an i512 multiply-and-remainder. The inline form legalizes into huge limb
sequences (the i512 remainder being the worst), making O3 codegen of
division-heavy contracts pathologically slow — the same hazard the felt252
multiply binding removed. The binding uses starknet-types-core's field_div
(optimized Montgomery inverse); rhs is a NonZero<felt252>, so it is
guaranteed nonzero.

Also removes the now-dead ExtendedEuclideanWidth::U252 variant, which was
only ever constructed by the old division path (U31/U256/U384 remain in use
by egcd / u256-invmod).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

@TomerStarkware TomerStarkware left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@TomerStarkware made 3 comments.
Reviewable status: 0 of 3 files reviewed, 4 unresolved discussions (waiting on orizi).


-- commits line 1 at r2:

Previously, orizi wrote…

rebase

Done.


src/runtime.rs line 139 at r1 (raw file):

Previously, orizi wrote…

move near mul

Done.


src/metadata/runtime_bindings.rs line 333 at r1 (raw file):

Previously, orizi wrote…

and remove the allow.

Done.

@github-actions

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) 10.312 ± 0.083 10.249 10.536 5.78 ± 0.07
cairo-native (embedded AOT) 1.785 ± 0.017 1.761 1.811 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 1.788 ± 0.010 1.770 1.803 1.00 ± 0.01

Benchmark for program dict_snapshot

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 514.0 ± 2.5 508.1 517.3 1.00
cairo-native (embedded AOT) 1600.3 ± 7.1 1592.4 1613.0 3.11 ± 0.02
cairo-native (embedded JIT using LLVM's ORC Engine) 1636.6 ± 6.8 1627.1 1650.4 3.18 ± 0.02

Benchmark for program factorial_2M

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 4.614 ± 0.034 4.589 4.688 2.76 ± 0.02
cairo-native (embedded AOT) 1.672 ± 0.006 1.663 1.681 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 1.692 ± 0.010 1.676 1.709 1.01 ± 0.01

Benchmark for program fib_2M

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 4.524 ± 0.011 4.506 4.543 2.79 ± 0.03
cairo-native (embedded AOT) 1.621 ± 0.017 1.606 1.663 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 1.655 ± 0.019 1.633 1.707 1.02 ± 0.02

Benchmark for program linear_search

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 554.6 ± 6.7 547.4 566.3 1.00
cairo-native (embedded AOT) 1631.2 ± 14.2 1611.8 1661.5 2.94 ± 0.04
cairo-native (embedded JIT using LLVM's ORC Engine) 1662.7 ± 8.0 1648.0 1672.1 3.00 ± 0.04

Benchmark for program logistic_map

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 469.5 ± 2.7 466.5 474.0 1.00
cairo-native (embedded AOT) 1610.4 ± 5.0 1604.7 1617.9 3.43 ± 0.02
cairo-native (embedded JIT using LLVM's ORC Engine) 1645.1 ± 11.5 1631.1 1665.5 3.50 ± 0.03

@github-actions

Copy link
Copy Markdown

Benchmark results Main vs HEAD.

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_insert.cairo (JIT) 1.949 ± 0.024 1.921 1.993 1.01 ± 0.02
base dict_insert.cairo (AOT) 1.925 ± 0.021 1.896 1.958 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_insert.cairo (JIT) 1.683 ± 0.020 1.667 1.736 1.00 ± 0.01
head dict_insert.cairo (AOT) 1.682 ± 0.013 1.667 1.707 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_snapshot.cairo (JIT) 1.748 ± 0.019 1.723 1.790 1.02 ± 0.02
base dict_snapshot.cairo (AOT) 1.713 ± 0.025 1.683 1.770 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_snapshot.cairo (JIT) 1.525 ± 0.021 1.496 1.557 1.02 ± 0.02
head dict_snapshot.cairo (AOT) 1.488 ± 0.017 1.466 1.529 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base factorial_2M.cairo (JIT) 1.787 ± 0.022 1.767 1.844 1.02 ± 0.02
base factorial_2M.cairo (AOT) 1.761 ± 0.021 1.735 1.805 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head factorial_2M.cairo (JIT) 1.557 ± 0.014 1.536 1.585 1.01 ± 0.02
head factorial_2M.cairo (AOT) 1.540 ± 0.032 1.512 1.613 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base fib_2M.cairo (JIT) 1.695 ± 0.016 1.671 1.726 1.03 ± 0.01
base fib_2M.cairo (AOT) 1.646 ± 0.016 1.622 1.670 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head fib_2M.cairo (JIT) 1.508 ± 0.015 1.494 1.542 1.03 ± 0.01
head fib_2M.cairo (AOT) 1.471 ± 0.007 1.464 1.483 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base linear_search.cairo (JIT) 1.712 ± 0.019 1.689 1.739 1.03 ± 0.01
base linear_search.cairo (AOT) 1.667 ± 0.008 1.656 1.682 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head linear_search.cairo (JIT) 1.523 ± 0.007 1.510 1.532 1.01 ± 0.01
head linear_search.cairo (AOT) 1.506 ± 0.009 1.490 1.519 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base logistic_map.cairo (JIT) 1.725 ± 0.025 1.692 1.760 1.03 ± 0.02
base logistic_map.cairo (AOT) 1.677 ± 0.021 1.634 1.705 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head logistic_map.cairo (JIT) 1.499 ± 0.009 1.486 1.519 1.02 ± 0.01
head logistic_map.cairo (AOT) 1.470 ± 0.006 1.457 1.475 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 3 files and all commit messages, made 1 comment, and resolved 4 discussions.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).

@TomerStarkware TomerStarkware added this pull request to the merge queue Jul 1, 2026
Merged via the queue into main with commit aa56499 Jul 1, 2026
15 checks passed
@TomerStarkware TomerStarkware deleted the tomer/felt252_div_pr branch July 1, 2026 02: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