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: declare post compilation prime validation #297

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 14 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 @@ -36,6 +36,7 @@ async-trait = "0.1.79"
axum = "0.6.12"
bincode = "1.3.3"
blockifier = { git = "https://github.com/starkware-libs/blockifier.git", rev = "32191d41" }
cairo-felt = "0.9.1"
cairo-lang-sierra = "2.7.0-dev.0"
cairo-lang-starknet-classes = "2.7.0-dev.0"
cairo-lang-utils = "2.7.0-dev.0"
Expand Down
4 changes: 3 additions & 1 deletion crates/gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ testing = []
[dependencies]
async-trait.workspace = true
axum.workspace = true
blockifier= { workspace = true , features = ["testing"] }
blockifier = { workspace = true , features = ["testing"] }
cairo-felt.workspace = true
cairo-lang-starknet-classes.workspace = true
cairo-vm.workspace = true
hyper.workspace = true
num-bigint.workspace = true
num-traits.workspace = true
papyrus_config.workspace = true
papyrus_rpc.workspace = true
Expand Down
12 changes: 12 additions & 0 deletions crates/gateway/src/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ use std::panic;
use std::sync::OnceLock;

use blockifier::execution::contract_class::{ClassInfo, ContractClass, ContractClassV1};
// TODO(Arni): Consider if you want to import a new crate just for a constant string.
use cairo_felt::PRIME_STR;
use cairo_lang_starknet_classes::casm_contract_class::{
CasmContractClass, CasmContractEntryPoints,
};
use num_bigint::BigUint;
use num_traits::Num;
use starknet_api::core::CompiledClassHash;
use starknet_api::rpc_transaction::RPCDeclareTransaction;
use starknet_sierra_compile::compile::compile_sierra_to_casm;
Expand Down Expand Up @@ -85,6 +89,14 @@ impl GatewayCompiler {
});
}
}

let prime = contract_class.prime.clone();
let expected_prime =
BigUint::from_str_radix(&PRIME_STR[2..], 16).expect("Error parsing field prime.");
if prime != expected_prime {
return Err(GatewayError::InvalidPrime { prime, expected_prime });
}

Ok(())
}
}
Expand Down
3 changes: 3 additions & 0 deletions crates/gateway/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use blockifier::execution::errors::ContractClassError;
use blockifier::state::errors::StateError;
use blockifier::transaction::errors::TransactionExecutionError;
use cairo_vm::types::errors::program_errors::ProgramError;
use num_bigint::BigUint;
use serde_json::{Error as SerdeError, Value};
use starknet_api::block::{BlockNumber, GasPrice};
use starknet_api::core::CompiledClassHash;
Expand Down Expand Up @@ -32,6 +33,8 @@ pub enum GatewayError {
DeclaredContractProgramError(#[from] ProgramError),
#[error("Internal server error: {0}")]
InternalServerError(#[from] JoinError),
#[error("Invalid value for field prime: {prime}. Expected: {expected_prime}")]
InvalidPrime { prime: BigUint, expected_prime: BigUint },
#[error("Error sending message: {0}")]
MessageSendError(String),
#[error(transparent)]
Expand Down
Loading