-
Notifications
You must be signed in to change notification settings - Fork 200
Support wasm with cairo1 #2216
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
Merged
Merged
Support wasm with cairo1 #2216
Changes from 24 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
80031ce
add support for wasm with cairo 1
FrancoGiachetta 056e966
correct Cargo.lock
FrancoGiachetta 5540f96
fix workflow
FrancoGiachetta 45eaa2a
fix workflow
FrancoGiachetta ebc6cab
add std feature for cairo1-run
FrancoGiachetta 43a3c72
use a different program as an example
FrancoGiachetta 709abe8
fmt + fix workflow
FrancoGiachetta 02d1fea
update changelog
FrancoGiachetta 1972c3e
use default features for cairo 1
FrancoGiachetta 6d048d9
try fix ci
FrancoGiachetta d9a1438
compile example
FrancoGiachetta 00c29f3
use cairo 1 compiler
FrancoGiachetta 1d0fc9b
try to fix ci
FrancoGiachetta 0356219
correct compiler command
FrancoGiachetta 597ebac
fetch program from cache
FrancoGiachetta c7bb8c8
try fix ci
FrancoGiachetta 31f833c
save in cache
FrancoGiachetta 0e09763
add sierra path to CAIRO_PROGRAMS_PATH
FrancoGiachetta 2af23d2
try fix
FrancoGiachetta 32a2f6c
change hashFiles function
FrancoGiachetta b0cafef
make cairo1-run cli require std feature
FrancoGiachetta 64604a6
remove unnecessary file path in hashFiles function
FrancoGiachetta 4ae89c8
format toml
FrancoGiachetta 0d40642
improve readme + remove todo
FrancoGiachetta 186a862
update makefile so that make tests does not complain
FrancoGiachetta 51df761
Merge branch 'main' into support-wasm-with-cairo1
FrancoGiachetta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| [package] | ||
| name = "wasm-demo-cairo1" | ||
| description = "A demo using cairo-vm in a WASM environment" | ||
| version.workspace = true | ||
| edition.workspace = true | ||
| license.workspace = true | ||
| repository.workspace = true | ||
| readme = "README.md" | ||
| keywords.workspace = true | ||
| publish = false | ||
|
|
||
| [lib] | ||
| crate-type = ["cdylib", "rlib"] | ||
|
|
||
| [features] | ||
| default = ["console_error_panic_hook"] | ||
|
|
||
| [dependencies] | ||
| wasm-bindgen = "0.2.100" | ||
|
|
||
| # The `console_error_panic_hook` crate provides better debugging of panics by | ||
| # logging them with `console.error`. This is great for development, but requires | ||
| # all the `std::fmt` and `std::panicking` infrastructure, so isn't great for | ||
| # code size when deploying. | ||
| console_error_panic_hook = { version = "0.1.6", optional = true } | ||
|
|
||
| serde_json = { workspace = true } | ||
|
|
||
| cairo-lang-sierra = { workspace = true } | ||
| cairo-vm = { workspace = true } | ||
| cairo1-run = { path = "../../cairo1-run", default-features = false } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # Demo of `cairo-vm` on WebAssembly (Cairo 1) | ||
|
|
||
| While cairo-vm is compatible with WebAssembly, it doesn't implement any bindings to it. | ||
| Instead, create a new WebAssembly crate with cairo-vm and cairo1-run as dependencies and implement the required functionality there. | ||
|
|
||
| Since mimalloc is not automatically compilable to WebAssembly, the cairo-vm dependency should disable the default features, which will in turn disable mimalloc. Simliar to this, WebAssembly is | ||
| not compatible with rust standard library. For this reason, cairo1-run should also disable the default features. | ||
|
|
||
| A working example is provided in this repository. | ||
|
|
||
| ## Dependencies | ||
|
|
||
| To compile and run the example you need: | ||
|
|
||
| - a Cairo 1 or Cairo 2 compiler | ||
| - the _wasm-pack_ crate | ||
| - some HTTP server (for example: the `live-server` npm module) | ||
|
|
||
| > **Note** | ||
| > The first two dependencies can be installed via the repository's installation script (see ["Installation script"](../../README.md#installation-script)) | ||
|
|
||
| ## Building | ||
|
|
||
| To build the example, first you need to compile your Cairo Program, using either cairo 1 or cairo 2 compiler: | ||
|
|
||
| If cairo 1 is used: | ||
|
|
||
| ```sh | ||
| cairo-compile -rs ../../cairo_programs/cairo-1-programs/bitwise.cairo ../../cairo_programs/cairo-1-programs/bitwise.sierra | ||
| ``` | ||
|
|
||
| If cairo 2 is used: | ||
|
|
||
| ```sh | ||
| cairo-compile -r ../../cairo_programs/cairo-1-programs/bitwise.cairo ../../cairo_programs/cairo-1-programs/bitwise.sierra | ||
| ``` | ||
|
|
||
| > WARNING: This example uses `cairo1-run::cairo_run_program` which expectes the program to be compiled with debug names. By default, `cairo-compile` does not include debug names in sierra. Due to this, the flag `-r` or `--replace-ids` is needed. | ||
|
|
||
| compile the WebAssembly package: | ||
|
|
||
| ```sh | ||
| wasm-pack build --target=web | ||
| ``` | ||
|
|
||
| This will generate a javascript module that is directly loadable by the browser. | ||
|
|
||
| ## Running | ||
|
|
||
| To run the example webpage, you need to run an HTTP server. | ||
| For example, using the _live-server_ npm module: | ||
|
|
||
| ```sh | ||
| # while in <repo>/examples/wasm-demo-cairo1 | ||
| npx live-server | ||
| ``` | ||
|
|
||
| > **Warning** | ||
| > Trying to run `index.html` directly (i.e. URL starts with `file://`) will result in a CORS error. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| fn main() -> u128 { | ||
| let a = 1234_u128; | ||
| let b = 5678_u128; | ||
|
|
||
| let c0 = a & b; | ||
| let c1 = a ^ b; | ||
| let c2 = a | b; | ||
|
|
||
| let c3 = c0 + c1 + c2; | ||
|
|
||
| c3 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
|
|
||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
|
|
||
| <title>Cairo WebAssembly Demo with Cairo 1</title> | ||
|
|
||
| <script type="module"> | ||
| import init, { runCairoProgram } from './pkg/wasm_demo_cairo1.js'; | ||
|
|
||
| // Initialize WebAssembly module. | ||
| // Note: This calls the `start()` function automatically. | ||
| await init(); | ||
|
|
||
| // Run cairo-rs through our proxy function. | ||
| const value = await runCairoProgram(); | ||
|
|
||
| // Display the result. | ||
| document.getElementById('result').innerHTML += value; | ||
| </script> | ||
| </head> | ||
|
|
||
| <body> | ||
| <p id="result">Result: </p> | ||
| </body> | ||
|
|
||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| mod utils; | ||
|
|
||
| use cairo1_run::Cairo1RunConfig; | ||
| use cairo_lang_sierra::ProgramParser; | ||
| use cairo_vm::types::layout_name::LayoutName; | ||
| use wasm_bindgen::prelude::*; | ||
|
|
||
| #[wasm_bindgen] | ||
| extern "C" { | ||
| #[wasm_bindgen(js_namespace = console)] | ||
| fn log(msg: &str); | ||
| } | ||
|
|
||
| #[wasm_bindgen(start)] | ||
| pub fn start() { | ||
| crate::utils::set_panic_hook(); | ||
| } | ||
|
|
||
| macro_rules! wrap_error { | ||
| ($xp: expr) => { | ||
| $xp.map_err(|e| JsValue::from_str(&format!("Error from CairoRunner: {}", e.to_string()))) | ||
| }; | ||
| } | ||
|
|
||
| #[wasm_bindgen(js_name = runCairoProgram)] | ||
| pub fn run_cairo_program() -> Result<String, JsValue> { | ||
| let cairo_run_config = Cairo1RunConfig { | ||
| layout: LayoutName::all_cairo, | ||
| relocate_mem: true, | ||
| trace_enabled: true, | ||
| serialize_output: true, | ||
| ..Default::default() | ||
| }; | ||
|
|
||
| let sierra_program = { | ||
| let program_str = include_str!("../../../cairo_programs/cairo-1-programs/bitwise.sierra"); | ||
| wrap_error!(ProgramParser::new().parse(program_str))? | ||
| }; | ||
|
|
||
| let (_, _, serialized_output) = wrap_error!(cairo1_run::cairo_run_program( | ||
| &sierra_program, | ||
| cairo_run_config | ||
| ))?; | ||
|
|
||
| let output = serialized_output.unwrap(); | ||
|
|
||
| log(&output); | ||
|
|
||
| Ok(output) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| pub fn set_panic_hook() { | ||
| // When the `console_error_panic_hook` feature is enabled, we can call the | ||
| // `set_panic_hook` function at least once during initialization, and then | ||
| // we will get better error messages if our code ever panics. | ||
| // | ||
| // For more details see | ||
| // https://github.com/rustwasm/console_error_panic_hook#readme | ||
| #[cfg(feature = "console_error_panic_hook")] | ||
| console_error_panic_hook::set_once(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.