Skip to content

Commit

Permalink
feat(blockifier): add impl for GasCosts in versioned_constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonatan-Starkware committed Nov 19, 2024
1 parent 66e1652 commit 1624253
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crates/blockifier/cairo_native
Submodule cairo_native updated 72 files
+0 −19 .github/ISSUE_TEMPLATE/daily_failure.md
+0 −36 .github/actions/install-linux-deps/action.yml
+6 −2 .github/workflows/bench-hyperfine.yml
+0 −175 .github/workflows/daily.yml
+0 −150 .github/workflows/starknet-blocks.yml
+63 −64 Cargo.lock
+2 −3 Cargo.toml
+1 −2 Makefile
+82 −18 benches/benches.rs
+0 −17 benches/compile_time.rs
+3 −54 benches/libfuncs.rs
+1 −15 docs/sierra.md
+0 −8 examples/erc20.rs
+0 −8 examples/starknet.rs
+3 −8 programs/benches/factorial_2M.c
+3 −8 programs/benches/fib_2M.c
+3 −8 programs/benches/logistic_map.c
+48 −42 programs/compile_benches/dijkstra.cairo
+17 −8 programs/compile_benches/extended_euclidean_algorithm.cairo
+27 −78 programs/compile_benches/fast_power.cairo
+285 −0 programs/compile_benches/sha256.cairo
+0 −539 programs/compile_benches/sha512.cairo
+1 −1 runtime/Cargo.toml
+1 −28 runtime/src/lib.rs
+38 −10 scripts/bench-hyperfine.sh
+0 −51 scripts/cmp_state_dumps.sh
+0 −30 scripts/diff-check.sh
+9 −2 src/arch/aarch64.rs
+9 −2 src/arch/x86_64.rs
+0 −1 src/bin/utils/mod.rs
+12 −13 src/cache/aot.rs
+8 −10 src/cache/jit.rs
+11 −20 src/compiler.rs
+0 −107 src/error.rs
+32 −81 src/executor.rs
+3 −51 src/executor/aot.rs
+46 −328 src/executor/contract.rs
+2 −23 src/executor/jit.rs
+1 −2 src/ffi.rs
+8 −13 src/libfuncs.rs
+17 −28 src/libfuncs/array.rs
+10 −43 src/libfuncs/bounded_int.rs
+1 −4 src/libfuncs/cast.rs
+3 −5 src/libfuncs/circuit.rs
+5 −10 src/libfuncs/const.rs
+2 −2 src/libfuncs/debug.rs
+4 −9 src/libfuncs/enum.rs
+2 −9 src/libfuncs/felt252.rs
+2 −2 src/libfuncs/felt252_dict_entry.rs
+2 −3 src/libfuncs/function_call.rs
+33 −125 src/libfuncs/gas.rs
+0 −181 src/libfuncs/int_range.rs
+602 −456 src/libfuncs/starknet.rs
+1,508 −677 src/libfuncs/starknet/secp256.rs
+7 −40 src/metadata/gas.rs
+0 −44 src/metadata/runtime_bindings.rs
+2 −43 src/starknet.rs
+0 −8 src/starknet_stub.rs
+9 −24 src/types.rs
+24 −19 src/types/array.rs
+2 −6 src/types/builtin_costs.rs
+0 −46 src/types/int_range.rs
+2 −110 src/utils.rs
+1 −87 src/utils/block_ext.rs
+3 −65 src/values.rs
+6 −6 tests/alexandria/Scarb.lock
+4 −4 tests/alexandria/Scarb.toml
+17 −33 tests/common.rs
+2 −13 tests/tests/starknet/keccak.rs
+1 −6 tests/tests/starknet/programs/syscalls.cairo
+0 −8 tests/tests/starknet/secp256.rs
+0 −28 tests/tests/starknet/syscalls.rs
71 changes: 71 additions & 0 deletions crates/blockifier/src/versioned_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,67 @@ pub struct GasCosts {
pub sha256_process_block_gas_cost: u64,
}

impl GasCosts {
pub fn get_builtin_gas_cost(&self, builtin: &BuiltinName) -> Result<u64, GasCostsError> {
const KECCAK_BUILTIN_GAS_COST : u64 = 136189;
match *builtin {
BuiltinName::range_check => Ok(self.range_check_gas_cost),
BuiltinName::pedersen => Ok(self.pedersen_gas_cost),
BuiltinName::bitwise => Ok(self.bitwise_builtin_gas_cost),
BuiltinName::ec_op => Ok(self.ecop_gas_cost),
//TODO (Yonatan): once keccak_builtin_gas_cost is being inserted to the versioned constants, replace the constant with field's value
BuiltinName::keccak => Ok(KECCAK_BUILTIN_GAS_COST),
BuiltinName::poseidon => Ok(self.poseidon_gas_cost),
BuiltinName::range_check96 => Ok(self.range_check_gas_cost),
BuiltinName::add_mod => Ok(self.add_mod_gas_cost),
BuiltinName::mul_mod => Ok(self.mul_mod_gas_cost),
BuiltinName::segment_arena => Err(GasCostsError::VirtualBuiltin),
// The following are unsupported builtins in Cairo 1
BuiltinName::output => Err(GasCostsError::UnsupportedCairo1Builtin {builtin: *builtin,}),
BuiltinName::ecdsa => Err(GasCostsError::UnsupportedCairo1Builtin {builtin: *builtin,}),
}
}

pub fn get_syscall_gas_cost(&self, selector: &SyscallSelector) -> Result<u64, GasCostsError> {
match selector {
SyscallSelector::CallContract => Ok(self.call_contract_gas_cost),
SyscallSelector::Deploy => Ok(self.deploy_gas_cost),
SyscallSelector::EmitEvent => Ok(self.emit_event_gas_cost),
SyscallSelector::GetBlockHash => Ok(self.get_block_hash_gas_cost),
SyscallSelector::GetExecutionInfo => Ok(self.get_execution_info_gas_cost),
SyscallSelector::GetClassHashAt => Ok(self.get_class_hash_at_gas_cost),
SyscallSelector::Keccak => Ok(self.keccak_gas_cost),
SyscallSelector::Sha256ProcessBlock => Ok(self.sha256_process_block_gas_cost),
SyscallSelector::LibraryCall => Ok(self.library_call_gas_cost),
SyscallSelector::ReplaceClass => Ok(self.replace_class_gas_cost),
SyscallSelector::Secp256k1Add => Ok(self.secp256k1_add_gas_cost),
SyscallSelector::Secp256k1GetPointFromX => Ok(self.secp256k1_get_point_from_x_gas_cost),
SyscallSelector::Secp256k1GetXy => Ok(self.secp256k1_get_xy_gas_cost),
SyscallSelector::Secp256k1Mul => Ok(self.secp256k1_mul_gas_cost),
SyscallSelector::Secp256k1New => Ok(self.secp256k1_new_gas_cost),
SyscallSelector::Secp256r1Add => Ok(self.secp256r1_add_gas_cost),
SyscallSelector::Secp256r1GetPointFromX => Ok(self.secp256r1_get_point_from_x_gas_cost),
SyscallSelector::Secp256r1GetXy => Ok(self.secp256r1_get_xy_gas_cost),
SyscallSelector::Secp256r1Mul => Ok(self.secp256r1_mul_gas_cost),
SyscallSelector::Secp256r1New => Ok(self.secp256r1_new_gas_cost),
SyscallSelector::SendMessageToL1 => Ok(self.send_message_to_l1_gas_cost),
SyscallSelector::StorageRead => Ok(self.storage_read_gas_cost),
SyscallSelector::StorageWrite => Ok(self.storage_write_gas_cost),
// The following are unsupported syscalls in Cairo 1
SyscallSelector::DelegateCall => Err(GasCostsError::UnsupportedCairo1Syscall{selector: *selector,}),
SyscallSelector::DelegateL1Handler => Err(GasCostsError::UnsupportedCairo1Syscall{selector: *selector,}),
SyscallSelector::GetBlockNumber => Err(GasCostsError::UnsupportedCairo1Syscall{selector: *selector,}),
SyscallSelector::GetBlockTimestamp => Err(GasCostsError::UnsupportedCairo1Syscall{selector: *selector,}),
SyscallSelector::GetCallerAddress => Err(GasCostsError::UnsupportedCairo1Syscall{selector: *selector,}),
SyscallSelector::GetContractAddress => Err(GasCostsError::UnsupportedCairo1Syscall{selector: *selector,}),
SyscallSelector::GetTxInfo => Err(GasCostsError::UnsupportedCairo1Syscall{selector: *selector,}),
SyscallSelector::GetSequencerAddress => Err(GasCostsError::UnsupportedCairo1Syscall{selector: *selector,}),
SyscallSelector::GetTxSignature => Err(GasCostsError::UnsupportedCairo1Syscall{selector: *selector,}),
SyscallSelector::LibraryCallL1Handler => Err(GasCostsError::UnsupportedCairo1Syscall{selector: *selector,}),
}
}
}

// Below, serde first deserializes the json into a regular IndexMap wrapped by the newtype
// `OsConstantsRawJson`, then calls the `try_from` of the newtype, which handles the
// conversion into actual values.
Expand Down Expand Up @@ -761,6 +822,16 @@ pub enum OsConstantsSerdeError {
ValidationError(String),
}

#[derive(Debug, Error)]
pub enum GasCostsError {
#[error("used syscall: {:?} is not supported in a Cairo 0 contract", selector)]
UnsupportedCairo1Syscall {selector: SyscallSelector},
#[error("used builtin: {:?} is not supported in a Cairo 1 contract", builtin)]
UnsupportedCairo1Builtin {builtin: BuiltinName},
#[error("a virtual builtin cannot be used in a smart contract")]
VirtualBuiltin,
}

#[derive(Clone, Debug, Deserialize)]
#[serde(try_from = "ResourceParamsRaw")]
pub struct ResourcesParams {
Expand Down

0 comments on commit 1624253

Please sign in to comment.