Skip to content

Conversation

@DiegoCivi
Copy link
Contributor

@DiegoCivi DiegoCivi commented Nov 14, 2025

Fix BoundedInt Add libfunc

The previous implementation of the add 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 that fixes the errors.

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. When having BoundedInt additions we need to implement the AddHelper like the following:

impl AddHelper of AddHelper<BoundedInt<1, 30>, BoundedInt<0, 10>> {
    type Result = BoundedInt<1, 40>;
}

fn run_addition() -> BoundedInt<1, 40> {
    let a: BoundedInt<1, 30> = 10.try_into().unwrap();
    let b: BoundedInt<0, 10> = 5.try_into().unwrap();
    return add(a, b);
}

In this case we have:

  • Ao = 1
  • Ad = 9
  • Bo = 0
  • Bd = 5
  • Co = 1

And as we can see Co = Ao + Bo. If this is not satisfied, then the Cairo code won´t compile. Then, by having Cd = Ao + Ad + Bo + Bd - Co and Co = Ao + Bo we can simplify the addition to Cd = Ad + Bd.

Closes #1448

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 80.00000% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.50%. Comparing base (2241ffb) to head (32fa505).

Files with missing lines Patch % Lines
src/libfuncs/bounded_int.rs 80.00% 6 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1463   +/-   ##
=======================================
  Coverage   81.50%   81.50%           
=======================================
  Files         105      105           
  Lines       25868    25858   -10     
=======================================
- Hits        21084    21076    -8     
+ Misses       4784     4782    -2     

☔ 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.548 ± 0.011 2.528 2.566 1.04 ± 0.01
base dict_insert.cairo (AOT) 2.453 ± 0.021 2.429 2.492 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_insert.cairo (JIT) 2.571 ± 0.032 2.532 2.626 1.03 ± 0.01
head dict_insert.cairo (AOT) 2.489 ± 0.015 2.464 2.509 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_snapshot.cairo (JIT) 2.257 ± 0.018 2.228 2.290 1.06 ± 0.01
base dict_snapshot.cairo (AOT) 2.139 ± 0.017 2.105 2.175 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_snapshot.cairo (JIT) 2.278 ± 0.014 2.259 2.305 1.04 ± 0.01
head dict_snapshot.cairo (AOT) 2.184 ± 0.018 2.162 2.223 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base factorial_2M.cairo (JIT) 2.619 ± 0.011 2.603 2.637 1.02 ± 0.01
base factorial_2M.cairo (AOT) 2.574 ± 0.010 2.559 2.591 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head factorial_2M.cairo (JIT) 2.625 ± 0.016 2.606 2.662 1.01 ± 0.01
head factorial_2M.cairo (AOT) 2.592 ± 0.016 2.561 2.618 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base fib_2M.cairo (JIT) 2.151 ± 0.017 2.124 2.188 1.02 ± 0.01
base fib_2M.cairo (AOT) 2.103 ± 0.014 2.084 2.126 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head fib_2M.cairo (JIT) 2.181 ± 0.026 2.142 2.217 1.02 ± 0.02
head fib_2M.cairo (AOT) 2.134 ± 0.025 2.099 2.186 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base heavy_circuit.cairo (JIT) 14.884 ± 0.040 14.812 14.941 1.03 ± 0.00
base heavy_circuit.cairo (AOT) 14.477 ± 0.040 14.408 14.544 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head heavy_circuit.cairo (JIT) 14.024 ± 0.065 13.936 14.127 1.03 ± 0.01
head heavy_circuit.cairo (AOT) 13.624 ± 0.086 13.518 13.790 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base linear_search.cairo (JIT) 2.327 ± 0.014 2.308 2.347 1.06 ± 0.01
base linear_search.cairo (AOT) 2.197 ± 0.022 2.163 2.222 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head linear_search.cairo (JIT) 2.350 ± 0.013 2.328 2.369 1.05 ± 0.02
head linear_search.cairo (AOT) 2.234 ± 0.035 2.195 2.296 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base logistic_map.cairo (JIT) 2.425 ± 0.015 2.397 2.446 1.07 ± 0.01
base logistic_map.cairo (AOT) 2.270 ± 0.014 2.248 2.292 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head logistic_map.cairo (JIT) 2.473 ± 0.015 2.451 2.506 1.08 ± 0.01
head logistic_map.cairo (AOT) 2.288 ± 0.017 2.267 2.319 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) 10.948 ± 0.060 10.877 11.088 4.55 ± 0.04
cairo-native (embedded AOT) 2.408 ± 0.018 2.372 2.432 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 2.496 ± 0.023 2.464 2.528 1.04 ± 0.01

Benchmark for program dict_snapshot

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 519.6 ± 6.8 508.2 527.1 1.00
cairo-native (embedded AOT) 2106.6 ± 16.8 2079.8 2136.8 4.05 ± 0.06
cairo-native (embedded JIT using LLVM's ORC Engine) 2211.6 ± 20.3 2193.2 2257.7 4.26 ± 0.07

Benchmark for program factorial_2M

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 4.752 ± 0.036 4.724 4.848 1.87 ± 0.02
cairo-native (embedded AOT) 2.538 ± 0.015 2.511 2.557 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 2.591 ± 0.021 2.563 2.624 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.739 ± 0.087 4.682 4.957 2.29 ± 0.05
cairo-native (embedded AOT) 2.067 ± 0.019 2.034 2.099 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 2.116 ± 0.010 2.103 2.135 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.526 ± 0.056 9.459 9.641 1.00
cairo-native (embedded AOT) 14.467 ± 0.046 14.378 14.523 1.52 ± 0.01
cairo-native (embedded JIT using LLVM's ORC Engine) 14.876 ± 0.066 14.751 14.963 1.56 ± 0.01

Benchmark for program linear_search

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 563.0 ± 5.4 552.9 572.5 1.00
cairo-native (embedded AOT) 2149.7 ± 14.1 2128.4 2169.0 3.82 ± 0.04
cairo-native (embedded JIT using LLVM's ORC Engine) 2294.4 ± 18.0 2267.6 2332.4 4.08 ± 0.05

Benchmark for program logistic_map

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 375.1 ± 5.8 369.0 390.1 1.00
cairo-native (embedded AOT) 2228.9 ± 13.5 2209.2 2248.9 5.94 ± 0.10
cairo-native (embedded JIT using LLVM's ORC Engine) 2411.8 ± 16.4 2389.0 2446.3 6.43 ± 0.11

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 added this pull request to the merge queue Nov 28, 2025
Merged via the queue into main with commit 4794e1d Nov 28, 2025
30 checks passed
@gabrielbosio gabrielbosio deleted the fix-bi-add branch November 28, 2025 22:44
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_add libfunc

5 participants