From c8348468ff7cd00cec4d366164b5afa77013fe3d Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Wed, 26 Jun 2024 16:37:33 +0300 Subject: [PATCH] feat: declare post compilation prime validation --- Cargo.lock | 14 ++++++++++++++ Cargo.toml | 3 ++- crates/gateway/Cargo.toml | 4 +++- crates/gateway/src/compilation.rs | 12 ++++++++++++ crates/gateway/src/errors.rs | 3 +++ 5 files changed, 34 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 728b4205a..52fd5fe34 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -730,6 +730,19 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ade8366b8bd5ba243f0a58f036cc0ca8a2f069cff1a2351ef1cac6b083e16fc0" +[[package]] +name = "cairo-felt" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae932292b9ba497a4e892b56aa4e0c6f329a455180fdbdc132700dfe68d9b153" +dependencies = [ + "lazy_static", + "num-bigint", + "num-integer", + "num-traits 0.2.19", + "serde", +] + [[package]] name = "cairo-lang-casm" version = "2.7.0-rc.1" @@ -5359,6 +5372,7 @@ dependencies = [ "async-trait", "axum", "blockifier 0.8.0-dev.1", + "cairo-felt", "cairo-lang-starknet-classes", "cairo-vm", "hyper", diff --git a/Cargo.toml b/Cargo.toml index e28380da9..a77c7673e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,7 @@ async-trait = "0.1.79" axum = "0.6.12" blockifier = { git = "https://github.com/starkware-libs/blockifier.git", rev = "bad24f25" } bincode = "1.3.3" +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" @@ -49,8 +50,8 @@ hyper = { version = "0.14", features = ["client", "server", "http1", "http2", "t indexmap = "2.1.0" itertools = "0.13.0" lazy_static = "1.4.0" -num-traits = "0.2" 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 = "ca83fd42" } diff --git a/crates/gateway/Cargo.toml b/crates/gateway/Cargo.toml index a202e8427..7321862a2 100644 --- a/crates/gateway/Cargo.toml +++ b/crates/gateway/Cargo.toml @@ -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 diff --git a/crates/gateway/src/compilation.rs b/crates/gateway/src/compilation.rs index 18dd58197..99c698ddd 100644 --- a/crates/gateway/src/compilation.rs +++ b/crates/gateway/src/compilation.rs @@ -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; @@ -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(()) } } diff --git a/crates/gateway/src/errors.rs b/crates/gateway/src/errors.rs index 75c30c0d6..fb9b6efdc 100644 --- a/crates/gateway/src/errors.rs +++ b/crates/gateway/src/errors.rs @@ -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; @@ -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)]