Skip to content

Conversation

@DiegoCivi
Copy link
Contributor

@DiegoCivi DiegoCivi commented Nov 14, 2025

Fix BoundedInt Sub libfunc

The previous implementation of the sub libfunc had some cases in which it failed to return the correct result. We decided to change the algorithm to one that is simpler to understand and is mathematically correct.

We have C = A - B, where all of them are BoundedInt. We represent the a BoundedInt as:

  • X = Xo + Xd, where
    • X is a BoundedInt.
    • Xo is the lower bound of the BoundedInt range.
    • Xd is the offset.

So we want to get Cd and we have Co + Cd = ( Ao + Ad ) - ( Bo + Bd ) -> Cd = Ao + Ad - Bo - Bd - Co.

Closes #1449

Introduces Breaking Changes?

No.

Checklist

  • Linked to Github Issue.
  • Unit tests added.
  • Integration tests added.
  • This change requires new documentation.
    • Documentation has been added/updated.

@codecov-commenter
Copy link

codecov-commenter commented Nov 14, 2025

Codecov Report

❌ Patch coverage is 83.33333% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.52%. Comparing base (2241ffb) to head (ad4dc54).

Files with missing lines Patch % Lines
src/libfuncs/bounded_int.rs 83.33% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1465      +/-   ##
==========================================
+ Coverage   81.50%   81.52%   +0.01%     
==========================================
  Files         105      105              
  Lines       25868    25863       -5     
==========================================
+ Hits        21084    21085       +1     
+ Misses       4784     4778       -6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions
Copy link

github-actions bot commented Nov 14, 2025

Benchmark results Main vs HEAD.

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_insert.cairo (JIT) 2.688 ± 0.035 2.637 2.730 1.06 ± 0.02
base dict_insert.cairo (AOT) 2.530 ± 0.038 2.483 2.584 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_insert.cairo (JIT) 2.583 ± 0.013 2.560 2.600 1.04 ± 0.01
head dict_insert.cairo (AOT) 2.494 ± 0.015 2.476 2.522 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_snapshot.cairo (JIT) 2.324 ± 0.029 2.283 2.371 1.06 ± 0.02
base dict_snapshot.cairo (AOT) 2.190 ± 0.027 2.143 2.230 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_snapshot.cairo (JIT) 2.288 ± 0.012 2.274 2.308 1.04 ± 0.01
head dict_snapshot.cairo (AOT) 2.191 ± 0.014 2.166 2.210 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base factorial_2M.cairo (JIT) 2.636 ± 0.012 2.616 2.657 1.01 ± 0.01
base factorial_2M.cairo (AOT) 2.614 ± 0.023 2.586 2.653 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head factorial_2M.cairo (JIT) 2.657 ± 0.016 2.637 2.682 1.02 ± 0.01
head factorial_2M.cairo (AOT) 2.615 ± 0.013 2.602 2.648 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base fib_2M.cairo (JIT) 2.187 ± 0.017 2.168 2.228 1.03 ± 0.01
base fib_2M.cairo (AOT) 2.133 ± 0.011 2.116 2.154 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head fib_2M.cairo (JIT) 2.192 ± 0.015 2.174 2.213 1.02 ± 0.01
head fib_2M.cairo (AOT) 2.140 ± 0.018 2.115 2.165 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base heavy_circuit.cairo (JIT) 15.295 ± 0.130 15.111 15.473 1.03 ± 0.01
base heavy_circuit.cairo (AOT) 14.780 ± 0.073 14.665 14.927 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head heavy_circuit.cairo (JIT) 14.005 ± 0.099 13.889 14.182 1.03 ± 0.01
head heavy_circuit.cairo (AOT) 13.599 ± 0.071 13.509 13.695 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base linear_search.cairo (JIT) 2.405 ± 0.027 2.360 2.446 1.06 ± 0.02
base linear_search.cairo (AOT) 2.261 ± 0.032 2.230 2.316 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head linear_search.cairo (JIT) 2.389 ± 0.021 2.355 2.420 1.07 ± 0.01
head linear_search.cairo (AOT) 2.231 ± 0.019 2.196 2.265 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base logistic_map.cairo (JIT) 2.494 ± 0.023 2.472 2.545 1.08 ± 0.01
base logistic_map.cairo (AOT) 2.315 ± 0.014 2.291 2.337 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head logistic_map.cairo (JIT) 2.475 ± 0.020 2.447 2.509 1.07 ± 0.01
head logistic_map.cairo (AOT) 2.308 ± 0.017 2.280 2.336 1.00

@github-actions
Copy link

github-actions bot commented Nov 14, 2025

Benchmarking results

Benchmark for program dict_insert

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 11.024 ± 0.049 10.957 11.115 4.39 ± 0.08
cairo-native (embedded AOT) 2.510 ± 0.045 2.441 2.601 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 2.583 ± 0.031 2.534 2.644 1.03 ± 0.02

Benchmark for program dict_snapshot

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 525.7 ± 7.7 516.4 538.6 1.00
cairo-native (embedded AOT) 2194.9 ± 35.9 2149.3 2281.3 4.18 ± 0.09
cairo-native (embedded JIT using LLVM's ORC Engine) 2314.9 ± 28.0 2270.0 2358.1 4.40 ± 0.08

Benchmark for program factorial_2M

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 4.747 ± 0.012 4.727 4.768 1.81 ± 0.01
cairo-native (embedded AOT) 2.623 ± 0.009 2.613 2.637 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 2.665 ± 0.017 2.641 2.693 1.02 ± 0.01

Benchmark for program fib_2M

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 4.727 ± 0.037 4.699 4.827 2.17 ± 0.02
cairo-native (embedded AOT) 2.179 ± 0.017 2.155 2.206 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 2.214 ± 0.021 2.180 2.251 1.02 ± 0.01

Benchmark for program heavy_circuit

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 9.654 ± 0.103 9.527 9.810 1.00
cairo-native (embedded AOT) 13.675 ± 0.099 13.568 13.850 1.42 ± 0.02
cairo-native (embedded JIT using LLVM's ORC Engine) 14.156 ± 0.114 13.947 14.351 1.47 ± 0.02

Benchmark for program linear_search

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 574.4 ± 6.2 566.7 585.8 1.00
cairo-native (embedded AOT) 2245.6 ± 15.6 2221.0 2269.6 3.91 ± 0.05
cairo-native (embedded JIT using LLVM's ORC Engine) 2351.7 ± 17.9 2333.3 2387.9 4.09 ± 0.05

Benchmark for program logistic_map

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 383.5 ± 4.0 376.6 389.5 1.00
cairo-native (embedded AOT) 2310.4 ± 12.4 2287.1 2326.5 6.02 ± 0.07
cairo-native (embedded JIT using LLVM's ORC Engine) 2482.0 ± 13.1 2461.2 2500.9 6.47 ± 0.08

Copy link
Contributor

@JulianGCalderon JulianGCalderon left a comment

Choose a reason for hiding this comment

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

Looks good! Can you modify the tests to use test_case?

Comment on lines 1073 to 1074
lower_bound: i32,
upper_bound: i32,
Copy link
Contributor

Choose a reason for hiding this comment

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

The lower and upper bound are given by the sierra, so there is no need on checking it, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactored in 30200bb

Co-authored-by: Julian Gonzalez Calderon <gonzalezcalderonjulian@gmail.com>
Copy link
Contributor

@JulianGCalderon JulianGCalderon left a comment

Choose a reason for hiding this comment

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

LGTM!

gabrielbosio
gabrielbosio previously approved these changes Nov 28, 2025
Co-authored-by: Gabriel Bosio <38794644+gabrielbosio@users.noreply.github.com>
@gabrielbosio gabrielbosio added this pull request to the merge queue Nov 28, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to a conflict with the base branch Nov 28, 2025
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.

Review bounded_int_sub libfunc

5 participants