diff --git a/CHANGELOG.md b/CHANGELOG.md index ebe9ac6470..1bc76455b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`. diff --git a/Cargo.lock b/Cargo.lock index 254a00c096..0625fe15a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -976,6 +976,7 @@ dependencies = [ "mimalloc", "num-traits 0.2.18", "rstest", + "serde_json", "thiserror", ] diff --git a/cairo1-run/Cargo.toml b/cairo1-run/Cargo.toml index 40b6361b07..ff1d0eb9fe 100644 --- a/cairo1-run/Cargo.toml +++ b/cairo1-run/Cargo.toml @@ -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 } diff --git a/cairo1-run/README.md b/cairo1-run/README.md index 8b7244314f..e4aafd4f6b 100644 --- a/cairo1-run/README.md +++ b/cairo1-run/README.md @@ -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 @@ -68,3 +68,15 @@ The cairo1-run cli supports the following optional arguments: * `--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 +``` diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 41892ed379..e538a68c97 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -205,14 +205,21 @@ fn run(args: impl Iterator) -> Result, 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)?;