Skip to content

Conversation

@Oppen
Copy link
Contributor

@Oppen Oppen commented Jun 6, 2023

Replace a substraction and shift-based logic by .checked_sub and
finding the right bit to avoid a panic.
Also fixed a bug where m >= 253 would store the result with the wrong
id for DI_BIT hint.
Added tests for both cases that confirm the bugs.

Fixes #1205

TITLE

Description

Description of the pull request changes and motivation.

Checklist

  • Linked to Github Issue
  • Unit tests added

Replace a substraction and shift-based logic by `.checked_sub` and
finding the right bit to avoid a `panic`.
Also fixed a bug where `m >= 253` would store the result with the wrong
id for `DI_BIT` hint.

Fixes #1205
@Oppen Oppen force-pushed the fix/sub_overflow_ec_utils branch from 2bc7808 to 7d01145 Compare June 6, 2023 19:08
@github-actions
Copy link

github-actions bot commented Jun 6, 2023

Benchmark Results for unmodified programs 🚀

Command Mean [s] Min [s] Max [s] Relative
base blake2s_integration_benchmark 14.253 ± 0.095 14.070 14.394 1.01 ± 0.01
head blake2s_integration_benchmark 14.048 ± 0.134 13.846 14.228 1.00
Command Mean [s] Min [s] Max [s] Relative
base compare_arrays_200000 4.726 ± 0.090 4.592 4.930 1.01 ± 0.02
head compare_arrays_200000 4.660 ± 0.040 4.588 4.697 1.00
Command Mean [s] Min [s] Max [s] Relative
base dict_integration_benchmark 3.087 ± 0.031 3.034 3.134 1.00
head dict_integration_benchmark 3.091 ± 0.035 3.042 3.159 1.00 ± 0.02
Command Mean [s] Min [s] Max [s] Relative
base factorial_multirun 4.845 ± 0.053 4.785 4.927 1.00
head factorial_multirun 4.850 ± 0.058 4.770 4.937 1.00 ± 0.02
Command Mean [s] Min [s] Max [s] Relative
base fibonacci_1000_multirun 4.237 ± 0.094 4.107 4.422 1.04 ± 0.04
head fibonacci_1000_multirun 4.089 ± 0.117 3.951 4.257 1.00
Command Mean [ms] Min [ms] Max [ms] Relative
base field_arithmetic_get_square_benchmark 202.9 ± 3.5 199.9 210.8 1.00
head field_arithmetic_get_square_benchmark 211.9 ± 11.6 202.3 242.9 1.04 ± 0.06
Command Mean [s] Min [s] Max [s] Relative
base integration_builtins 12.761 ± 0.598 12.148 13.928 1.01 ± 0.06
head integration_builtins 12.610 ± 0.357 12.163 13.124 1.00
Command Mean [s] Min [s] Max [s] Relative
base keccak_integration_benchmark 13.938 ± 0.674 13.559 15.333 1.00
head keccak_integration_benchmark 14.033 ± 0.734 13.460 15.540 1.01 ± 0.07
Command Mean [s] Min [s] Max [s] Relative
base linear_search 4.427 ± 0.076 4.370 4.573 1.00
head linear_search 4.689 ± 0.117 4.523 4.941 1.06 ± 0.03
Command Mean [s] Min [s] Max [s] Relative
base math_cmp_and_pow_integration_benchmark 3.412 ± 0.057 3.357 3.524 1.02 ± 0.02
head math_cmp_and_pow_integration_benchmark 3.345 ± 0.033 3.266 3.375 1.00
Command Mean [s] Min [s] Max [s] Relative
base math_integration_benchmark 3.180 ± 0.058 3.114 3.280 1.01 ± 0.02
head math_integration_benchmark 3.136 ± 0.050 3.032 3.201 1.00
Command Mean [s] Min [s] Max [s] Relative
base memory_integration_benchmark 2.652 ± 0.027 2.620 2.686 1.01 ± 0.02
head memory_integration_benchmark 2.628 ± 0.030 2.596 2.688 1.00
Command Mean [s] Min [s] Max [s] Relative
base operations_with_data_structures_benchmarks 3.058 ± 0.014 3.044 3.087 1.00
head operations_with_data_structures_benchmarks 3.059 ± 0.029 3.022 3.130 1.00 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base pedersen 1.008 ± 0.025 0.991 1.075 1.00 ± 0.03
head pedersen 1.005 ± 0.015 0.980 1.029 1.00
Command Mean [s] Min [s] Max [s] Relative
base poseidon_integration_benchmark 1.769 ± 0.031 1.728 1.830 1.01 ± 0.02
head poseidon_integration_benchmark 1.756 ± 0.018 1.730 1.783 1.00
Command Mean [s] Min [s] Max [s] Relative
base secp_integration_benchmark 3.848 ± 0.020 3.815 3.878 1.01 ± 0.01
head secp_integration_benchmark 3.803 ± 0.020 3.765 3.835 1.00
Command Mean [s] Min [s] Max [s] Relative
base set_integration_benchmark 1.753 ± 0.015 1.729 1.771 1.00 ± 0.02
head set_integration_benchmark 1.753 ± 0.028 1.726 1.814 1.00
Command Mean [s] Min [s] Max [s] Relative
base uint256_integration_benchmark 8.734 ± 0.380 8.176 9.326 1.06 ± 0.05
head uint256_integration_benchmark 8.225 ± 0.082 8.128 8.337 1.00

@codecov
Copy link

codecov bot commented Jun 6, 2023

Codecov Report

Merging #1209 (1f94c3d) into main (eefb615) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main    #1209   +/-   ##
=======================================
  Coverage   97.59%   97.59%           
=======================================
  Files          89       89           
  Lines       36113    36159   +46     
=======================================
+ Hits        35243    35289   +46     
  Misses        870      870           
Impacted Files Coverage Δ
..._processor/builtin_hint_processor/secp/ec_utils.rs 99.53% <100.00%> (+0.02%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Contributor

@MegaRedHand MegaRedHand left a comment

Choose a reason for hiding this comment

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

LGTM

let m = m_cow.as_ref().to_u32().unwrap_or(253);
if m >= 253 {
return insert_value_from_var_name("quad_bit", 0, vm, ids_data, ap_tracking);
return insert_value_from_var_name(result_name, 0, vm, ids_data, ap_tracking);
Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch!

// ↓↓
// 1010101__ -> 101010110
let get_bit =
|x: &BigUint, i| m.checked_sub(i).map(|i| x.bit(i.into())).unwrap_or(false) as u32;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit

Suggested change
|x: &BigUint, i| m.checked_sub(i).map(|i| x.bit(i.into())).unwrap_or(false) as u32;
|x: &BigUint, i| m.checked_sub(i).map(|j| x.bit(j.into())).unwrap_or(false) as u32;

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think your suggestion needs to change the closure parameter name too right?

Copy link
Contributor

Choose a reason for hiding this comment

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

No. Both the closure and the closure inside the closure have a parameter named i. My suggestion is to rename the inner one to j to avoid variable shadowing.

@pefontana pefontana enabled auto-merge June 7, 2023 19:28
@pefontana pefontana added this pull request to the merge queue Jun 7, 2023
Merged via the queue into main with commit 21e86c6 Jun 7, 2023
@pefontana pefontana deleted the fix/sub_overflow_ec_utils branch June 7, 2023 20:25
kariy pushed a commit to dojoengine/cairo-rs that referenced this pull request Jun 23, 2023
* fix: overflow in `n_pair_bits`

Replace a substraction and shift-based logic by `.checked_sub` and
finding the right bit to avoid a `panic`.
Also fixed a bug where `m >= 253` would store the result with the wrong
id for `DI_BIT` hint.

Fixes lambdaclass#1205

* fix changelog.md

---------

Co-authored-by: Pedro Fontana <[email protected]>
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.

Panic while executing an EC operation in a _debug_ build

5 participants