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

feat: add CASM serialization of Cairo programs #5912

Open
wants to merge 29 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
9a74268
feat: add serialization of sierra to casm for cairo programs
zmalatrax Jun 27, 2024
742c2ad
chore: trunk fmt
zmalatrax Jun 27, 2024
9759007
refactor: extract compiler_version to cairo_lang_sierra_to_casm
zmalatrax Jun 28, 2024
b7cc0a4
refactor: entrypoint and builtins fields instead of sierra function
zmalatrax Jun 28, 2024
b5ee35d
chore: remove unused serde traits
zmalatrax Jul 4, 2024
0b0dd4d
dev: revert to using UnorderedHashMap for const_offset
zmalatrax Jul 4, 2024
b6599b3
chore: remove unused serde traits
zmalatrax Jul 4, 2024
ea92978
chore: remove unused add_pythonic_hint CLI argument
zmalatrax Jul 4, 2024
7be0b4b
feat: add input parameters and return type to serialized artifacts
zmalatrax Jul 4, 2024
1fc62c6
chore: rename input_args to input_args_type
zmalatrax Jul 4, 2024
cfb50cd
refactor: return_type is a Vec<String> rather than a String
zmalatrax Jul 4, 2024
c2480c3
chore: remove find_function fn
zmalatrax Jul 8, 2024
1dd3b7c
feat: serialize all functions in a dictionary with type sizes
zmalatrax Jul 8, 2024
1003d2a
feat: skip pythonic_hints if none
zmalatrax Jul 8, 2024
de24d59
fix: pythonic_hints as Option
zmalatrax Jul 9, 2024
6308817
refactor: export type_resolver to cairo_lang_sierra
zmalatrax Jul 9, 2024
c6fb7b0
refactor: use type_resolver instead of debug name to extract builtins
zmalatrax Jul 9, 2024
7b1bfe5
refactor: use type_resolver instead of debug name to extract builtins
zmalatrax Jul 9, 2024
b67e799
fix: serialize the casm instruction offset rather than the sierra sta…
zmalatrax Jul 9, 2024
4847871
refactor: make get_long_id public
zmalatrax Jul 12, 2024
c23c94d
refactor: refactor input and output args extraction
zmalatrax Jul 12, 2024
6e6351b
fix: add cairo-lang-sierra-to-casm to starknet-sierra-upgrade-validat…
zmalatrax Oct 24, 2024
d8c7ca2
refactor: remove pythonic_hints
zmalatrax Oct 24, 2024
ea14e6a
chore: rustfmt
zmalatrax Oct 25, 2024
6ad5c6d
chore: machete
zmalatrax Oct 25, 2024
584bc2f
chore: docs
zmalatrax Oct 25, 2024
c794df9
chore: clippy
zmalatrax Oct 25, 2024
402c62c
chore: add serde feature to cairo-lang-casm
zmalatrax Oct 25, 2024
87b3063
refactor: remove generic_id from serialized output
zmalatrax Oct 25, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ members = [
"crates/bin/cairo-test",
"crates/bin/generate-syntax",
"crates/bin/sierra-compile",
"crates/bin/sierra-compile-json",
"crates/bin/starknet-compile",
"crates/bin/starknet-sierra-compile",
"crates/bin/starknet-sierra-extract-code",
Expand Down
15 changes: 15 additions & 0 deletions crates/bin/sierra-compile-json/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "sierra-compile-json"
version.workspace = true
edition.workspace = true
repository.workspace = true
license-file.workspace = true
description = "Compiler executable for the Sierra intemediate representation - Outputs a serialized JSON"

[dependencies]
anyhow.workspace = true
clap.workspace = true
serde_json.workspace = true

cairo-lang-sierra = { path = "../../cairo-lang-sierra", version = "~2.8.4" }
cairo-lang-sierra-to-casm = { path = "../../cairo-lang-sierra-to-casm", version = "~2.8.4" }
53 changes: 53 additions & 0 deletions crates/bin/sierra-compile-json/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::fs;

use anyhow::Context;
use cairo_lang_sierra::ProgramParser;
use cairo_lang_sierra_to_casm::compiler::{CasmCairoProgram, SierraToCasmConfig, compile};
use cairo_lang_sierra_to_casm::metadata::calc_metadata;
use clap::Parser;

/// Compiles a Sierra file (Cairo Program) into serialized CASM.
/// Exits with 0/1 if the compilation succeeds/fails.
#[derive(Parser, Debug)]
#[clap(version, verbatim_doc_comment)]
struct Args {
/// The path of the file to compile.
file: String,
/// The output file name (default: stdout).
output: Option<String>,
/// Add gas usage check
#[arg(long, default_value_t = false)]
gas_usage_check: bool,
}

fn main() -> anyhow::Result<()> {
let args = Args::parse();

let sierra_code = fs::read_to_string(args.file).with_context(|| "Could not read file!")?;
let Ok(sierra_program) = ProgramParser::new().parse(&sierra_code) else {
anyhow::bail!("Failed to parse Sierra program.")
};

let sierra_to_casm_config =
SierraToCasmConfig { gas_usage_check: args.gas_usage_check, max_bytecode_size: usize::MAX };

let cairo_program = compile(
&sierra_program,
&calc_metadata(&sierra_program, Default::default())
.with_context(|| "Failed calculating Sierra variables.")?,
sierra_to_casm_config,
)
.with_context(|| "Compilation failed.")?;

let casm_cairo_program = CasmCairoProgram::new(&sierra_program, &cairo_program)
.with_context(|| "Sierra to Casm compilation failed.")?;

let res = serde_json::to_string(&casm_cairo_program)
.with_context(|| "Casm contract Serialization failed.")?;

match args.output {
Some(path) => fs::write(path, res).with_context(|| "Failed to write casm contract.")?,
None => println!("{res}"),
}
Ok(())
}
7 changes: 3 additions & 4 deletions crates/bin/starknet-sierra-upgrade-validate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ reqwest = { version = "0.12", features = ["json"] }
serde = { workspace = true, default-features = true }
serde_json.workspace = true
tokio.workspace = true
cairo-lang-sierra-to-casm = { path = "../../cairo-lang-sierra-to-casm", version = "~2.8.4" }
cairo-lang-starknet-classes = { path = "../../cairo-lang-starknet-classes", version = "~2.8.4" }
cairo-lang-utils = { path = "../../cairo-lang-utils", version = "~2.8.4", features = [
"serde",
] }
cairo-lang-utils = { path = "../../cairo-lang-utils", version = "~2.8.4", features = ["serde"] }

# This is not a direct dependency of this package, but it is included here to make sure that the
# This is not a direct dependency of this package, but it is included here to make sure that the
# `vendored` feature is enabled (needed for the compilation to succeed on remote machines).
openssl = { workspace = true, features = ["vendored"] }

Expand Down
6 changes: 3 additions & 3 deletions crates/bin/starknet-sierra-upgrade-validate/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};

use anyhow::Context;
use cairo_lang_sierra_to_casm::compiler_version::VersionId;
use cairo_lang_starknet_classes::allowed_libfuncs::{AllowedLibfuncsError, ListSelector};
use cairo_lang_starknet_classes::casm_contract_class::{
CasmContractClass, StarknetSierraCompilationError,
};
use cairo_lang_starknet_classes::compiler_version::VersionId;
use cairo_lang_starknet_classes::contract_class::{ContractClass, ContractEntryPoints};
use cairo_lang_utils::bigint::BigUintAsHex;
use clap::{Parser, arg};
Expand Down Expand Up @@ -52,8 +52,8 @@ struct Cli {
/// files should be provided.
#[arg(
long,
requires_all = &["FullnodeArgs"],
required_unless_present = "input_files",
requires_all = &["FullnodeArgs"],
required_unless_present = "input_files",
conflicts_with = "input_files"
)]
fullnode_url: Option<String>,
Expand Down
10 changes: 8 additions & 2 deletions crates/cairo-lang-sierra-to-casm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@ description = "Emitting of CASM instructions from Sierra code."

[dependencies]
assert_matches.workspace = true
cairo-lang-casm = { path = "../cairo-lang-casm", version = "~2.8.4" }
cairo-lang-casm = { path = "../cairo-lang-casm", version = "~2.8.4", features = ["serde"] }
cairo-lang-sierra = { path = "../cairo-lang-sierra", version = "~2.8.4" }
cairo-lang-sierra-ap-change = { path = "../cairo-lang-sierra-ap-change", version = "~2.8.4" }
cairo-lang-sierra-gas = { path = "../cairo-lang-sierra-gas", version = "~2.8.4" }
cairo-lang-sierra-generator = { path = "../cairo-lang-sierra-generator", version = "~2.8.4" }
cairo-lang-sierra-type-size = { path = "../cairo-lang-sierra-type-size", version = "~2.8.4" }
cairo-lang-utils = { path = "../cairo-lang-utils", version = "~2.8.4", features = ["serde"] }
indoc.workspace = true
itertools = { workspace = true, default-features = true }
convert_case.workspace = true
starknet-types-core.workspace = true
num-bigint = { workspace = true, default-features = true }
num-traits = { workspace = true, default-features = true }
num-integer.workspace = true
serde = { workspace = true, default-features = true }
thiserror.workspace = true

[dev-dependencies]
Expand All @@ -27,7 +31,9 @@ indoc.workspace = true
pretty_assertions.workspace = true
test-case.workspace = true
test-log.workspace = true
cairo-lang-test-utils = { path = "../cairo-lang-test-utils", features = ["testing"] }
cairo-lang-test-utils = { path = "../cairo-lang-test-utils", features = [
"testing",
] }

[features]
testing = []
Loading