Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) Replace div_rem with div_mod_floor #1881

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

FrancoGiachetta
Copy link
Contributor

@FrancoGiachetta FrancoGiachetta commented Nov 25, 2024

Replace div_rem with div_mod_floor

Description

Right now, in other to execute this hint:

%{
    from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack

    q, r = divmod(pack(ids.val, PRIME), SECP_P)
    assert r == 0, f"verify_zero: Invalid input {ids.val.d0, ids.val.d1, ids.val.d2}."
    ids.q = q % PRIME
%}

We have this piece of code:

pub fn verify_zero(
    vm: &mut VirtualMachine,
    exec_scopes: &mut ExecutionScopes,
    ids_data: &HashMap<String, HintReference>,
    ap_tracking: &ApTracking,
    secp_p: &BigInt,
) -> Result<(), HintError> {
    exec_scopes.insert_value("SECP_P", secp_p.clone());
    let val = Uint384::from_var_name("val", vm, ids_data, ap_tracking)?.pack86();
    let (q, r) = val.div_rem(&secp_p);
    if !r.is_zero() {
        return Err(HintError::SecpVerifyZero(Box::new(val)));
    }

    insert_value_from_var_name("q", Felt252::from(&q), vm, ids_data, ap_tracking)
}

Investigating a little about issue #1877, I've found that there's indeed a discrepancy between the vm in rust and the one in pyhon. To mimic the divmod operation inside the hint, we use div_rem. The problem here is that div_rem does not always provide the same behaviour as divmod. While divmod uses floor_division, which means that the division gets floored to get the integer, div_rem truncates it which means just taking the integer part of the division's result.
Both methods might differ when performing a division which deals with negative values. For example, to perform the modular division of 10 / (-3), both methods would handle it differently:

  1. Both of them would perform the same division: 10 / (-3) = -3,333...
  2. Here's where they take different approaches:
  • divmod: floors the result (-4) and calculates the residual with it: 10 - (-3) * (-4) = -2
  • div_rem: truncates the result (-3) and calculates the residual with it: 10 - (-3) * (-3) = 1

As it can be seen, div_rem seems not to be a solution (verify_zero could return an error in python and not in rust). So, to solve this, I replaced div_rem with div_mod_floor which uses floor division like divmod and thus providing the same behaviour.

Checklist

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

@FrancoGiachetta FrancoGiachetta changed the title replace div_rem with div_mod_floor (fix) Replace div_rem with div_mod_floor Nov 25, 2024
@FrancoGiachetta FrancoGiachetta marked this pull request as ready for review November 25, 2024 14:08
Copy link

github-actions bot commented Nov 25, 2024

**Hyper Thereading Benchmark results**




hyperfine -r 2 -n "hyper_threading_main threads: 1" 'RAYON_NUM_THREADS=1 ./hyper_threading_main' -n "hyper_threading_pr threads: 1" 'RAYON_NUM_THREADS=1 ./hyper_threading_pr'
Benchmark 1: hyper_threading_main threads: 1
  Time (mean ± σ):     32.062 s ±  0.067 s    [User: 31.329 s, System: 0.730 s]
  Range (min … max):   32.015 s … 32.110 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 1
  Time (mean ± σ):     32.956 s ±  0.042 s    [User: 32.193 s, System: 0.762 s]
  Range (min … max):   32.927 s … 32.986 s    2 runs
 
Summary
  'hyper_threading_main threads: 1' ran
    1.03 ± 0.00 times faster than 'hyper_threading_pr threads: 1'




hyperfine -r 2 -n "hyper_threading_main threads: 2" 'RAYON_NUM_THREADS=2 ./hyper_threading_main' -n "hyper_threading_pr threads: 2" 'RAYON_NUM_THREADS=2 ./hyper_threading_pr'
Benchmark 1: hyper_threading_main threads: 2
  Time (mean ± σ):     17.628 s ±  0.007 s    [User: 30.857 s, System: 0.769 s]
  Range (min … max):   17.623 s … 17.633 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 2
  Time (mean ± σ):     17.688 s ±  0.020 s    [User: 30.982 s, System: 0.732 s]
  Range (min … max):   17.673 s … 17.702 s    2 runs
 
Summary
  'hyper_threading_main threads: 2' ran
    1.00 ± 0.00 times faster than 'hyper_threading_pr threads: 2'




hyperfine -r 2 -n "hyper_threading_main threads: 4" 'RAYON_NUM_THREADS=4 ./hyper_threading_main' -n "hyper_threading_pr threads: 4" 'RAYON_NUM_THREADS=4 ./hyper_threading_pr'
Benchmark 1: hyper_threading_main threads: 4
  Time (mean ± σ):     12.149 s ±  0.192 s    [User: 44.053 s, System: 0.983 s]
  Range (min … max):   12.013 s … 12.285 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 4
  Time (mean ± σ):     12.661 s ±  0.005 s    [User: 43.681 s, System: 0.980 s]
  Range (min … max):   12.658 s … 12.665 s    2 runs
 
Summary
  'hyper_threading_main threads: 4' ran
    1.04 ± 0.02 times faster than 'hyper_threading_pr threads: 4'




hyperfine -r 2 -n "hyper_threading_main threads: 6" 'RAYON_NUM_THREADS=6 ./hyper_threading_main' -n "hyper_threading_pr threads: 6" 'RAYON_NUM_THREADS=6 ./hyper_threading_pr'
Benchmark 1: hyper_threading_main threads: 6
  Time (mean ± σ):     12.573 s ±  0.042 s    [User: 43.716 s, System: 0.965 s]
  Range (min … max):   12.543 s … 12.602 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 6
  Time (mean ± σ):     12.103 s ±  0.414 s    [User: 44.055 s, System: 0.966 s]
  Range (min … max):   11.810 s … 12.396 s    2 runs
 
Summary
  'hyper_threading_pr threads: 6' ran
    1.04 ± 0.04 times faster than 'hyper_threading_main threads: 6'




hyperfine -r 2 -n "hyper_threading_main threads: 8" 'RAYON_NUM_THREADS=8 ./hyper_threading_main' -n "hyper_threading_pr threads: 8" 'RAYON_NUM_THREADS=8 ./hyper_threading_pr'
Benchmark 1: hyper_threading_main threads: 8
  Time (mean ± σ):     11.996 s ±  0.071 s    [User: 44.241 s, System: 1.028 s]
  Range (min … max):   11.946 s … 12.046 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 8
  Time (mean ± σ):     11.890 s ±  0.062 s    [User: 44.333 s, System: 0.942 s]
  Range (min … max):   11.847 s … 11.934 s    2 runs
 
Summary
  'hyper_threading_pr threads: 8' ran
    1.01 ± 0.01 times faster than 'hyper_threading_main threads: 8'




hyperfine -r 2 -n "hyper_threading_main threads: 16" 'RAYON_NUM_THREADS=16 ./hyper_threading_main' -n "hyper_threading_pr threads: 16" 'RAYON_NUM_THREADS=16 ./hyper_threading_pr'
Benchmark 1: hyper_threading_main threads: 16
  Time (mean ± σ):     11.918 s ±  0.023 s    [User: 44.683 s, System: 1.093 s]
  Range (min … max):   11.902 s … 11.934 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 16
  Time (mean ± σ):     11.896 s ±  0.179 s    [User: 44.453 s, System: 1.068 s]
  Range (min … max):   11.770 s … 12.023 s    2 runs
 
Summary
  'hyper_threading_pr threads: 16' ran
    1.00 ± 0.02 times faster than 'hyper_threading_main threads: 16'


Copy link

github-actions bot commented Nov 25, 2024

Benchmark Results for unmodified programs 🚀

Command Mean [s] Min [s] Max [s] Relative
base big_factorial 2.489 ± 0.035 2.453 2.549 1.00
head big_factorial 2.496 ± 0.027 2.469 2.560 1.00 ± 0.02
Command Mean [s] Min [s] Max [s] Relative
base big_fibonacci 2.449 ± 0.038 2.408 2.529 1.00 ± 0.02
head big_fibonacci 2.440 ± 0.038 2.412 2.537 1.00
Command Mean [s] Min [s] Max [s] Relative
base blake2s_integration_benchmark 9.196 ± 0.096 9.080 9.345 1.00
head blake2s_integration_benchmark 9.234 ± 0.157 9.009 9.520 1.00 ± 0.02
Command Mean [s] Min [s] Max [s] Relative
base compare_arrays_200000 2.590 ± 0.015 2.565 2.615 1.01 ± 0.02
head compare_arrays_200000 2.575 ± 0.062 2.529 2.709 1.00
Command Mean [s] Min [s] Max [s] Relative
base dict_integration_benchmark 1.721 ± 0.024 1.694 1.766 1.02 ± 0.02
head dict_integration_benchmark 1.687 ± 0.021 1.667 1.733 1.00
Command Mean [s] Min [s] Max [s] Relative
base field_arithmetic_get_square_benchmark 1.436 ± 0.018 1.421 1.470 1.00 ± 0.02
head field_arithmetic_get_square_benchmark 1.431 ± 0.014 1.417 1.459 1.00
Command Mean [s] Min [s] Max [s] Relative
base integration_builtins 9.334 ± 0.193 9.157 9.838 1.01 ± 0.02
head integration_builtins 9.211 ± 0.071 9.094 9.286 1.00
Command Mean [s] Min [s] Max [s] Relative
base keccak_integration_benchmark 9.644 ± 0.381 9.389 10.674 1.01 ± 0.04
head keccak_integration_benchmark 9.532 ± 0.195 9.344 9.987 1.00
Command Mean [s] Min [s] Max [s] Relative
base linear_search 2.616 ± 0.058 2.571 2.759 1.01 ± 0.03
head linear_search 2.578 ± 0.039 2.539 2.648 1.00
Command Mean [s] Min [s] Max [s] Relative
base math_cmp_and_pow_integration_benchmark 1.770 ± 0.051 1.737 1.910 1.01 ± 0.03
head math_cmp_and_pow_integration_benchmark 1.749 ± 0.018 1.717 1.784 1.00
Command Mean [s] Min [s] Max [s] Relative
base math_integration_benchmark 1.711 ± 0.010 1.695 1.730 1.01 ± 0.02
head math_integration_benchmark 1.700 ± 0.026 1.677 1.768 1.00
Command Mean [s] Min [s] Max [s] Relative
base memory_integration_benchmark 1.431 ± 0.013 1.418 1.450 1.00
head memory_integration_benchmark 1.432 ± 0.015 1.412 1.458 1.00 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base operations_with_data_structures_benchmarks 1.861 ± 0.036 1.832 1.925 1.02 ± 0.03
head operations_with_data_structures_benchmarks 1.828 ± 0.035 1.805 1.905 1.00
Command Mean [ms] Min [ms] Max [ms] Relative
base pedersen 592.7 ± 6.9 587.8 611.3 1.00 ± 0.02
head pedersen 590.6 ± 8.3 581.3 605.9 1.00
Command Mean [ms] Min [ms] Max [ms] Relative
base poseidon_integration_benchmark 712.2 ± 12.0 696.6 729.0 1.03 ± 0.02
head poseidon_integration_benchmark 690.7 ± 6.1 681.0 700.9 1.00
Command Mean [s] Min [s] Max [s] Relative
base secp_integration_benchmark 2.063 ± 0.006 2.055 2.074 1.00 ± 0.01
head secp_integration_benchmark 2.059 ± 0.009 2.049 2.072 1.00
Command Mean [ms] Min [ms] Max [ms] Relative
base set_integration_benchmark 682.2 ± 2.6 677.3 685.1 1.00
head set_integration_benchmark 683.1 ± 8.0 674.3 700.4 1.00 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base uint256_integration_benchmark 5.137 ± 0.151 5.033 5.453 1.02 ± 0.03
head uint256_integration_benchmark 5.056 ± 0.069 4.976 5.201 1.00

Copy link

codecov bot commented Nov 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.33%. Comparing base (22be180) to head (82dde5b).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1881   +/-   ##
=======================================
  Coverage   96.33%   96.33%           
=======================================
  Files         102      102           
  Lines       40687    40687           
=======================================
  Hits        39195    39195           
  Misses       1492     1492           

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

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.

1 participant