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

[Cairo 1] Load arguments into VM instead of creating them via instructions #1759

Merged
merged 16 commits into from
May 16, 2024

Conversation

fmoletta
Copy link
Contributor

@fmoletta fmoletta commented May 9, 2024

Changes
Adds the function load_arguments which loads the input arguments (if any) into the VM after initialization and removes the instructions that create these arguments in create_entry_code. This change has no effect on the execution segment.
Adds test to make sure that the program_hash does not depend on the arguments used to run it.

Motive & Context
This PR solves the issue of the program_hash changing depending on the arguments passed to the vm when running it. This happens due to how cairo 1 handles arguments. Cairo 0 doesn't have arguments when running a program, this was circumvented by Cairo 1 by adding instructions to the original program that declare the arguments as constants and insert them into the execution segment (via tempvars) before calling the main function (they also add the builtin and segment arena pointers). While this allows running cairo 1 programs with arguments, it also makes the program (and therefore the program_hash) vary depending on the arguments used, as the arguments are now part of the instructions.
This PR is the later of 3 approaches to solve this issue, the previous three attempts consisted on:

  • Removing the create_entry_code altogether [WIP] Remove create_entry_code #1746 (This run into issues due to the segment arena builtin)

  • Replacing all instructions that create values in the execution segment with loading them directly into the VM before the run [Cairo 1] Replace hardcoded arguments with vm initialization #1751 (This worked similarly to this PR, but aside from loading the input arguments, it also loaded the builtin bases & segment arena values and removed the instructions that created them. While this solution worked (aka it allowed proving using Stone Prover), it involves more values being magically created, which can reduce the integrity of the proof (see discussion & possible issues))

Discussion & possible issues
As arguments are now loaded into the memory directly instead of being created by a cairo instruction, this could be seen as a hint being the one that creates those values in memory, which means that we won't be able to prove that it was those specific input values that produced an output based on a program as the prover doesn't currently take these arguments as public inputs.

Copy link

github-actions bot commented May 9, 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 ± σ):     27.028 s ±  0.010 s    [User: 26.266 s, System: 0.761 s]
  Range (min … max):   27.020 s … 27.035 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 1
  Time (mean ± σ):     27.131 s ±  0.096 s    [User: 26.459 s, System: 0.672 s]
  Range (min … max):   27.064 s … 27.199 s    2 runs
 
Summary
  'hyper_threading_main threads: 1' ran
    1.00 ± 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 ± σ):     14.781 s ±  0.046 s    [User: 26.870 s, System: 0.770 s]
  Range (min … max):   14.749 s … 14.814 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 2
  Time (mean ± σ):     14.736 s ±  0.032 s    [User: 26.851 s, System: 0.756 s]
  Range (min … max):   14.714 s … 14.759 s    2 runs
 
Summary
  'hyper_threading_pr threads: 2' ran
    1.00 ± 0.00 times faster than 'hyper_threading_main 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 ± σ):     10.869 s ±  0.590 s    [User: 39.089 s, System: 0.956 s]
  Range (min … max):   10.452 s … 11.286 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 4
  Time (mean ± σ):     10.956 s ±  0.573 s    [User: 38.827 s, System: 0.958 s]
  Range (min … max):   10.551 s … 11.362 s    2 runs
 
Summary
  'hyper_threading_main threads: 4' ran
    1.01 ± 0.08 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 ± σ):     10.605 s ±  0.116 s    [User: 39.054 s, System: 0.988 s]
  Range (min … max):   10.523 s … 10.688 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 6
  Time (mean ± σ):     10.751 s ±  0.016 s    [User: 38.869 s, System: 0.931 s]
  Range (min … max):   10.740 s … 10.762 s    2 runs
 
Summary
  'hyper_threading_main threads: 6' ran
    1.01 ± 0.01 times faster than 'hyper_threading_pr 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 ± σ):     10.445 s ±  0.025 s    [User: 39.333 s, System: 1.004 s]
  Range (min … max):   10.428 s … 10.463 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 8
  Time (mean ± σ):     10.435 s ±  0.092 s    [User: 39.381 s, System: 0.970 s]
  Range (min … max):   10.370 s … 10.500 s    2 runs
 
Summary
  'hyper_threading_pr threads: 8' ran
    1.00 ± 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 ± σ):     10.814 s ±  0.114 s    [User: 39.631 s, System: 1.083 s]
  Range (min … max):   10.733 s … 10.895 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 16
  Time (mean ± σ):     10.466 s ±  0.130 s    [User: 39.788 s, System: 0.980 s]
  Range (min … max):   10.374 s … 10.558 s    2 runs
 
Summary
  'hyper_threading_pr threads: 16' ran
    1.03 ± 0.02 times faster than 'hyper_threading_main threads: 16'


Copy link

github-actions bot commented May 9, 2024

Benchmark Results for unmodified programs 🚀

Command Mean [s] Min [s] Max [s] Relative
base big_factorial 2.033 ± 0.087 1.984 2.267 1.01 ± 0.05
head big_factorial 2.023 ± 0.038 1.986 2.098 1.00
Command Mean [s] Min [s] Max [s] Relative
base big_fibonacci 1.970 ± 0.014 1.951 1.994 1.00
head big_fibonacci 1.994 ± 0.030 1.960 2.034 1.01 ± 0.02
Command Mean [s] Min [s] Max [s] Relative
base blake2s_integration_benchmark 7.503 ± 0.121 7.343 7.672 1.00
head blake2s_integration_benchmark 7.593 ± 0.322 7.364 8.440 1.01 ± 0.05
Command Mean [s] Min [s] Max [s] Relative
base compare_arrays_200000 2.090 ± 0.021 2.071 2.131 1.00
head compare_arrays_200000 2.117 ± 0.061 2.074 2.274 1.01 ± 0.03
Command Mean [s] Min [s] Max [s] Relative
base dict_integration_benchmark 1.410 ± 0.009 1.402 1.431 1.00
head dict_integration_benchmark 1.418 ± 0.010 1.403 1.436 1.01 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base field_arithmetic_get_square_benchmark 1.286 ± 0.017 1.263 1.308 1.00
head field_arithmetic_get_square_benchmark 1.300 ± 0.070 1.257 1.493 1.01 ± 0.06
Command Mean [s] Min [s] Max [s] Relative
base integration_builtins 7.572 ± 0.099 7.417 7.698 1.00 ± 0.02
head integration_builtins 7.537 ± 0.130 7.386 7.705 1.00
Command Mean [s] Min [s] Max [s] Relative
base keccak_integration_benchmark 7.854 ± 0.133 7.649 8.102 1.01 ± 0.02
head keccak_integration_benchmark 7.749 ± 0.098 7.636 7.930 1.00
Command Mean [s] Min [s] Max [s] Relative
base linear_search 2.087 ± 0.031 2.058 2.136 1.01 ± 0.02
head linear_search 2.058 ± 0.025 2.040 2.103 1.00
Command Mean [s] Min [s] Max [s] Relative
base math_cmp_and_pow_integration_benchmark 1.698 ± 0.011 1.688 1.719 1.00
head math_cmp_and_pow_integration_benchmark 1.699 ± 0.021 1.679 1.755 1.00 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base math_integration_benchmark 1.595 ± 0.014 1.582 1.626 1.00 ± 0.01
head math_integration_benchmark 1.594 ± 0.008 1.583 1.608 1.00
Command Mean [s] Min [s] Max [s] Relative
base memory_integration_benchmark 1.189 ± 0.012 1.179 1.221 1.00 ± 0.01
head memory_integration_benchmark 1.185 ± 0.011 1.173 1.204 1.00
Command Mean [s] Min [s] Max [s] Relative
base operations_with_data_structures_benchmarks 1.815 ± 0.015 1.798 1.846 1.00
head operations_with_data_structures_benchmarks 1.823 ± 0.016 1.806 1.852 1.00 ± 0.01
Command Mean [ms] Min [ms] Max [ms] Relative
base pedersen 515.5 ± 3.2 511.5 520.2 1.00
head pedersen 519.2 ± 15.3 510.0 561.3 1.01 ± 0.03
Command Mean [ms] Min [ms] Max [ms] Relative
base poseidon_integration_benchmark 966.7 ± 4.6 960.8 973.8 1.00
head poseidon_integration_benchmark 974.7 ± 11.2 962.8 1001.8 1.01 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base secp_integration_benchmark 1.859 ± 0.017 1.840 1.883 1.00
head secp_integration_benchmark 1.859 ± 0.020 1.834 1.898 1.00 ± 0.01
Command Mean [ms] Min [ms] Max [ms] Relative
base set_integration_benchmark 644.7 ± 2.8 641.0 649.0 1.00
head set_integration_benchmark 652.4 ± 19.6 638.5 701.6 1.01 ± 0.03
Command Mean [s] Min [s] Max [s] Relative
base uint256_integration_benchmark 4.179 ± 0.049 4.096 4.248 1.01 ± 0.02
head uint256_integration_benchmark 4.149 ± 0.057 4.077 4.219 1.00

Copy link

codecov bot commented May 9, 2024

Codecov Report

Attention: Patch coverage is 92.30769% with 7 lines in your changes are missing coverage. Please review.

Project coverage is 94.79%. Comparing base (aecbb3f) to head (6400d09).

Files Patch % Lines
cairo1-run/src/cairo_run.rs 92.30% 7 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1759   +/-   ##
=======================================
  Coverage   94.79%   94.79%           
=======================================
  Files         101      101           
  Lines       38743    38787   +44     
=======================================
+ Hits        36728    36770   +42     
- Misses       2015     2017    +2     

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

@fmoletta fmoletta marked this pull request as ready for review May 9, 2024 22:20
Copy link
Collaborator

@pefontana pefontana left a comment

Choose a reason for hiding this comment

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

Nice one!

@pefontana pefontana added this pull request to the merge queue May 16, 2024
Merged via the queue into main with commit c370e2d May 16, 2024
71 of 72 checks passed
@pefontana pefontana deleted the load-args-externally branch May 16, 2024 20:54
@Okm165
Copy link

Okm165 commented Aug 19, 2024

Really good stuff this enables private inputs to the circuits!

@raphaelDkhn
Copy link

raphaelDkhn commented Oct 20, 2024

@fmoletta

Since program arguments are no longer included in the public inputs for the prover, one way to verify that a program has been executed correctly with specific arguments would to return those arguments in the program's output, right?. This way, we ensure that the arguments become part of the public inputs, similar to serialize_word in CairoZero.

Is it correct?

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.

5 participants