Skip to content

Commit

Permalink
Add option to pass Sierra programs to cairo1-run (#1719)
Browse files Browse the repository at this point in the history
* feat: cairo1 run sierra program

* Fix sierra parsing

* Read to bytes instead of string

* Add section about running scarb projects

* Remove whitespaces

---------

Co-authored-by: Vid Kersic <[email protected]>
Co-authored-by: Pedro Fontana <[email protected]>
  • Loading branch information
3 people authored Apr 19, 2024
1 parent 1ac1dcb commit a462d8d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#### Upcoming Changes

* feat: `cairo1-run` accepts Sierra programs [#1719](https://github.com/lambdaclass/cairo-vm/pull/1719)

* refactor(BREAKING): Use `BuiltinName` enum instead of string representation [#1722](https://github.com/lambdaclass/cairo-vm/pull/1722)
* `BuiltinName` moved from `crate::serde::deserialize_program` module to `crate::types::builtin_name`.
* Implement `BuiltinName` methods `to_str`, `to_str_with_suffix`, `from_str` & `from_str_with_suffix`.
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cairo1-run/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ keywords.workspace = true

[dependencies]
cairo-vm = {workspace = true, features = ["std", "cairo-1-hints", "clap"]}
serde_json = { workspace = true }

cairo-lang-sierra-type-size = { version = "2.5.4", default-features = false }
cairo-lang-sierra-ap-change = { version = "2.5.4", default-features = false }
Expand Down
14 changes: 13 additions & 1 deletion cairo1-run/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Now that you have the dependencies necessary to run the tests, you can run:
make test
```

To execute a cairo 1 program
To execute a Cairo 1 program (either as Cairo 1 source file or Sierra)

```bash
cargo run ../cairo_programs/cairo-1-programs/fibonacci.cairo
Expand Down Expand Up @@ -68,3 +68,15 @@ The cairo1-run cli supports the following optional arguments:
* `--cairo_pie_output <CAIRO_PIE_OUTPUT>`: Receives the name of a file and outputs the Cairo PIE into it. Can only be used if proof_mode, is not enabled.

* `--append_return_values`: Adds extra instructions to the program in order to append the return values to the output builtin's segment. This is the default behaviour for proof_mode.

# Running scarb projects

First compile your project running `scarb build`


Then run the compiled project's sierra file located at `project_name/target/project_name.sierra.json`

Example:
```bash
cargo run path-to-project/target/project_name.sierra.json
```
19 changes: 13 additions & 6 deletions cairo1-run/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,21 @@ fn run(args: impl Iterator<Item = String>) -> Result<Option<String>, Error> {
append_return_values: args.append_return_values,
};

let compiler_config = CompilerConfig {
replace_ids: true,
..CompilerConfig::default()
// Try to parse the file as a sierra program
let file = std::fs::read(&args.filename)?;
let sierra_program = match serde_json::from_slice(&file) {
Ok(program) => program,
Err(_) => {
// If it fails, try to compile it as a cairo program
let compiler_config = CompilerConfig {
replace_ids: true,
..CompilerConfig::default()
};
compile_cairo_project_at_path(&args.filename, compiler_config)
.map_err(|err| Error::SierraCompilation(err.to_string()))?
}
};

let sierra_program = compile_cairo_project_at_path(&args.filename, compiler_config)
.map_err(|err| Error::SierraCompilation(err.to_string()))?;

let (runner, vm, _, serialized_output) =
cairo_run::cairo_run_program(&sierra_program, cairo_run_config)?;

Expand Down

0 comments on commit a462d8d

Please sign in to comment.