Skip to content

Commit

Permalink
feat: declare post compilation prime validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Jul 3, 2024
1 parent 0f229c0 commit cc45be8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ axum = "0.6.12"
# starknet-api version. This should be removed once we have a mono-repo.
blockifier = { git = "https://github.com/starkware-libs/blockifier.git", branch = "main-mempool" }
bincode = "1.3.3"
cairo-felt = "0.9.1"
cairo-lang-sierra = "2.6.0"
cairo-lang-starknet-classes = "2.6.0"
cairo-lang-utils = "2.6.0"
Expand All @@ -51,6 +52,7 @@ hyper = { version = "0.14", features = ["client", "http1", "http2"] }
indexmap = "2.1.0"
itertools = "0.13.0"
num-bigint = { version = "0.4.5", default-features = false }
num-traits = "0.2"
# TODO(YaelD, 28/5/2024): The special Papyrus version is needed in order to be aligned with the
# starknet-api version. This should be removed once we have a mono-repo.
papyrus_common = { git = "https://github.com/starkware-libs/papyrus.git", rev = "56c6fdc2" }
Expand Down
6 changes: 5 additions & 1 deletion crates/gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ testing = []
[dependencies]
async-trait.workspace = true
axum.workspace = true
blockifier= { workspace = true , features = ["testing"] }
assert_matches.workspace = true
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
reqwest.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 @@ -3,9 +3,13 @@ use std::sync::OnceLock;

use blockifier::execution::contract_class::{ClassInfo, ContractClass, ContractClassV1};
use blockifier::execution::execution_utils::felt_to_stark_felt;
// 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_api::transaction::Builtin;
Expand Down Expand Up @@ -106,5 +110,13 @@ fn validate_casm_class(contract_class: &CasmContractClass) -> Result<(), Gateway
});
}
}

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(())
}
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

0 comments on commit cc45be8

Please sign in to comment.