Skip to content

Conversation

FrancoGiachetta
Copy link
Contributor

@FrancoGiachetta FrancoGiachetta commented Sep 30, 2025

Support wasm with cairo1

Description

This PR makes some changes so that a cairo 1 program can be run with wasm:

  1. cairo1-run depends on cairo-vm with the std feature set. Since wasm does not support std features, this PR creates an std feature for cairo1-run, which was set as a default feature to maintain compatibility. When used with wasm, default-features are set to false.
  2. Changes std implementations for HashMap and Peekable with their no_std ones.
  3. Due to point 2., I made cairo1-run's cli to requiere std feature.
  4. Changes the initial gas used in cairo1-run/src/cairo-run.rs to be u64 instead of usize. This was needed by wasm to be able to compile properly.
  5. Added a workflow to check wasm with cairo 1.

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.

Copy link

github-actions bot commented Sep 30, 2025

**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 ± σ):     24.679 s ±  0.057 s    [User: 23.784 s, System: 0.892 s]
  Range (min … max):   24.639 s … 24.719 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 1
  Time (mean ± σ):     24.678 s ±  0.000 s    [User: 23.772 s, System: 0.904 s]
  Range (min … max):   24.677 s … 24.678 s    2 runs
 
Summary
  hyper_threading_pr threads: 1 ran
    1.00 ± 0.00 times faster than hyper_threading_main 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 ± σ):     13.515 s ±  0.032 s    [User: 23.983 s, System: 0.896 s]
  Range (min … max):   13.492 s … 13.538 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 2
  Time (mean ± σ):     13.486 s ±  0.005 s    [User: 24.047 s, System: 0.865 s]
  Range (min … max):   13.483 s … 13.490 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.164 s ±  0.172 s    [User: 36.480 s, System: 1.118 s]
  Range (min … max):   10.042 s … 10.286 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 4
  Time (mean ± σ):     10.292 s ±  0.294 s    [User: 36.527 s, System: 1.065 s]
  Range (min … max):   10.084 s … 10.500 s    2 runs
 
Summary
  hyper_threading_main threads: 4 ran
    1.01 ± 0.03 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.168 s ±  0.052 s    [User: 36.858 s, System: 1.124 s]
  Range (min … max):   10.131 s … 10.204 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 6
  Time (mean ± σ):     10.157 s ±  0.044 s    [User: 36.783 s, System: 1.087 s]
  Range (min … max):   10.126 s … 10.188 s    2 runs
 
Summary
  hyper_threading_pr threads: 6 ran
    1.00 ± 0.01 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 ± σ):      9.974 s ±  0.100 s    [User: 37.007 s, System: 1.137 s]
  Range (min … max):    9.903 s … 10.044 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 8
  Time (mean ± σ):      9.854 s ±  0.100 s    [User: 37.279 s, System: 1.141 s]
  Range (min … max):    9.783 s …  9.924 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 ± σ):     10.041 s ±  0.040 s    [User: 37.175 s, System: 1.251 s]
  Range (min … max):   10.013 s … 10.069 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 16
  Time (mean ± σ):     10.151 s ±  0.094 s    [User: 37.213 s, System: 1.281 s]
  Range (min … max):   10.085 s … 10.217 s    2 runs
 
Summary
  hyper_threading_main threads: 16 ran
    1.01 ± 0.01 times faster than hyper_threading_pr threads: 16


Copy link

github-actions bot commented Sep 30, 2025

Benchmark Results for unmodified programs 🚀

Command Mean [s] Min [s] Max [s] Relative
base big_factorial 2.071 ± 0.015 2.054 2.109 1.00
head big_factorial 2.093 ± 0.017 2.069 2.112 1.01 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base big_fibonacci 2.014 ± 0.020 1.988 2.041 1.00
head big_fibonacci 2.025 ± 0.029 1.991 2.085 1.01 ± 0.02
Command Mean [s] Min [s] Max [s] Relative
base blake2s_integration_benchmark 7.215 ± 0.134 7.049 7.452 1.00
head blake2s_integration_benchmark 7.277 ± 0.249 7.038 7.940 1.01 ± 0.04
Command Mean [s] Min [s] Max [s] Relative
base compare_arrays_200000 2.152 ± 0.018 2.125 2.185 1.01 ± 0.01
head compare_arrays_200000 2.140 ± 0.016 2.110 2.166 1.00
Command Mean [s] Min [s] Max [s] Relative
base dict_integration_benchmark 1.398 ± 0.010 1.387 1.423 1.00
head dict_integration_benchmark 1.421 ± 0.004 1.415 1.427 1.02 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base field_arithmetic_get_square_benchmark 1.187 ± 0.009 1.176 1.198 1.00
head field_arithmetic_get_square_benchmark 1.196 ± 0.009 1.184 1.214 1.01 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base integration_builtins 7.296 ± 0.098 7.166 7.423 1.00
head integration_builtins 7.334 ± 0.064 7.179 7.416 1.01 ± 0.02
Command Mean [s] Min [s] Max [s] Relative
base keccak_integration_benchmark 7.356 ± 0.129 7.200 7.611 1.00
head keccak_integration_benchmark 7.385 ± 0.079 7.228 7.456 1.00 ± 0.02
Command Mean [s] Min [s] Max [s] Relative
base linear_search 2.112 ± 0.011 2.097 2.138 1.00
head linear_search 2.120 ± 0.016 2.101 2.148 1.00 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base math_cmp_and_pow_integration_benchmark 1.481 ± 0.010 1.463 1.495 1.00
head math_cmp_and_pow_integration_benchmark 1.496 ± 0.011 1.475 1.514 1.01 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base math_integration_benchmark 1.432 ± 0.011 1.415 1.445 1.00
head math_integration_benchmark 1.460 ± 0.022 1.435 1.499 1.02 ± 0.02
Command Mean [s] Min [s] Max [s] Relative
base memory_integration_benchmark 1.206 ± 0.017 1.182 1.247 1.00 ± 0.02
head memory_integration_benchmark 1.201 ± 0.011 1.185 1.214 1.00
Command Mean [s] Min [s] Max [s] Relative
base operations_with_data_structures_benchmarks 1.525 ± 0.019 1.502 1.562 1.00 ± 0.01
head operations_with_data_structures_benchmarks 1.521 ± 0.007 1.511 1.530 1.00
Command Mean [ms] Min [ms] Max [ms] Relative
base pedersen 527.9 ± 2.8 523.8 531.8 1.00 ± 0.01
head pedersen 526.1 ± 2.4 522.0 529.2 1.00
Command Mean [ms] Min [ms] Max [ms] Relative
base poseidon_integration_benchmark 610.3 ± 2.3 606.0 613.5 1.01 ± 0.01
head poseidon_integration_benchmark 603.5 ± 2.3 599.0 606.1 1.00
Command Mean [s] Min [s] Max [s] Relative
base secp_integration_benchmark 1.751 ± 0.013 1.738 1.785 1.00
head secp_integration_benchmark 1.765 ± 0.012 1.753 1.794 1.01 ± 0.01
Command Mean [ms] Min [ms] Max [ms] Relative
base set_integration_benchmark 643.9 ± 4.0 640.2 652.7 1.00
head set_integration_benchmark 644.6 ± 2.4 641.3 648.2 1.00 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base uint256_integration_benchmark 4.057 ± 0.021 4.022 4.095 1.00
head uint256_integration_benchmark 4.097 ± 0.031 4.046 4.156 1.01 ± 0.01

@FrancoGiachetta FrancoGiachetta force-pushed the support-wasm-with-cairo1 branch from 74c759b to 709abe8 Compare October 1, 2025 15:04
@FrancoGiachetta FrancoGiachetta force-pushed the support-wasm-with-cairo1 branch 2 times, most recently from 03f6ebe to 563201a Compare October 1, 2025 19:14
@FrancoGiachetta FrancoGiachetta force-pushed the support-wasm-with-cairo1 branch from 563201a to 597ebac Compare October 1, 2025 19:28
Copy link

codecov bot commented Oct 2, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.68%. Comparing base (958ae7e) to head (186a862).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2216   +/-   ##
=======================================
  Coverage   96.68%   96.68%           
=======================================
  Files         104      104           
  Lines       44027    44027           
=======================================
  Hits        42566    42566           
  Misses       1461     1461           

☔ 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@FrancoGiachetta FrancoGiachetta force-pushed the support-wasm-with-cairo1 branch from fc2da60 to 64604a6 Compare October 2, 2025 13:31
@FrancoGiachetta FrancoGiachetta marked this pull request as ready for review October 2, 2025 13:57
@FrancoGiachetta FrancoGiachetta mentioned this pull request Oct 2, 2025
6 tasks
@FrancoGiachetta FrancoGiachetta force-pushed the support-wasm-with-cairo1 branch from d7daaad to 0d40642 Compare October 2, 2025 14:02
};

let sierra_program = {
let program_str = include_str!("../../../cairo_programs/cairo-1-programs/bitwise.sierra");
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we are missing the bitwise.sierra. I am running make test and seeing the following error:

error: couldn't read `examples/wasm-demo-cairo1/src/../../../cairo_programs/cairo-1-programs/bitwise.sierra`: No such file or directory (os error 2)
  --> examples/wasm-demo-cairo1/src/lib.rs:36:27
   |
36 |         let program_str = include_str!("../../../cairo_programs/cairo-1-programs/bitwise.sierra");

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed the Makefile so that it takes into account that file when testing.

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.

2 participants