From 8f69f4fd3f230ae36befd720927eafbcd8a6a8e5 Mon Sep 17 00:00:00 2001 From: LucasLvy Date: Mon, 16 Oct 2023 14:58:33 +0200 Subject: [PATCH 1/3] chore(hints): create block context module --- src/hints/block_context/mod.rs | 105 ++++++++++++++++++++++++++++++++ src/hints/mod.rs | 106 ++------------------------------- tests/hints.rs | 3 +- 3 files changed, 112 insertions(+), 102 deletions(-) create mode 100644 src/hints/block_context/mod.rs diff --git a/src/hints/block_context/mod.rs b/src/hints/block_context/mod.rs new file mode 100644 index 00000000..d8c16d83 --- /dev/null +++ b/src/hints/block_context/mod.rs @@ -0,0 +1,105 @@ +use cairo_felt::Felt252; +use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::insert_value_from_var_name; +use cairo_vm::hint_processor::hint_processor_definition::HintReference; +use cairo_vm::serde::deserialize_program::ApTracking; +use cairo_vm::types::exec_scope::ExecutionScopes; +use cairo_vm::vm::errors::hint_errors::HintError; +use cairo_vm::vm::vm_core::VirtualMachine; +use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContractClass; +use std::any::Any; +use std::collections::hash_map::IntoIter; +use std::collections::HashMap; + +use crate::io::classes::write_deprecated_class; +use crate::io::StarknetOsInput; +// Implements hint: +// +// ids.compiled_class_facts = segments.add() +// ids.n_compiled_class_facts = len(os_input.compiled_classes) +// vm_enter_scope({ +// 'compiled_class_facts': iter(os_input.compiled_classes.items()), +// }) +pub fn load_class_facts( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + let os_input = exec_scopes.get::("os_input")?; + let compiled_class_facts_ptr = vm.add_memory_segment(); + insert_value_from_var_name("compiled_class_facts", compiled_class_facts_ptr, vm, ids_data, ap_tracking)?; + + insert_value_from_var_name("n_compiled_class_facts", os_input.compiled_classes.len(), vm, ids_data, ap_tracking)?; + + let scoped_classes: Box = Box::new(os_input.compiled_classes.into_iter()); + exec_scopes.enter_scope(HashMap::from([(String::from("compiled_class_facts"), scoped_classes)])); + + Ok(()) +} + +// Implements hint: +// +// # Creates a set of deprecated class hashes to distinguish calls to deprecated entry points. +// __deprecated_class_hashes=set(os_input.deprecated_compiled_classes.keys()) +// ids.compiled_class_facts = segments.add() +// ids.n_compiled_class_facts = len(os_input.deprecated_compiled_classes) +// vm_enter_scope({ +// 'compiled_class_facts': iter(os_input.deprecated_compiled_classes.items()), +// }) +pub fn load_deprecated_class_facts( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + let os_input = exec_scopes.get::("os_input")?; + let compiled_class_facts_ptr = vm.add_memory_segment(); + insert_value_from_var_name("compiled_class_facts", compiled_class_facts_ptr, vm, ids_data, ap_tracking)?; + + insert_value_from_var_name( + "n_compiled_class_facts", + os_input.deprecated_compiled_classes.len(), + vm, + ids_data, + ap_tracking, + )?; + let scoped_classes: Box = Box::new(os_input.deprecated_compiled_classes.into_iter()); + exec_scopes.enter_scope(HashMap::from([(String::from("compiled_class_facts"), scoped_classes)])); + + Ok(()) +} + +// Implements hint: +// +// from starkware.starknet.core.os.contract_class.deprecated_class_hash import ( +// get_deprecated_contract_class_struct, +// ) +// +// compiled_class_hash, compiled_class = next(compiled_class_facts) +// +// cairo_contract = get_deprecated_contract_class_struct( +// identifiers=ids._context.identifiers, contract_class=compiled_class) +// ids.compiled_class = segments.gen_arg(cairo_contract) +pub fn load_deprecated_inner( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + let deprecated_class_iter = + exec_scopes.get_mut_ref::>("compiled_class_facts").unwrap(); + + let (class_hash, deprecated_class) = deprecated_class_iter.next().unwrap(); + + exec_scopes.insert_value("compiled_class_hash", class_hash); + + let dep_class_base = vm.add_memory_segment(); + write_deprecated_class(vm, dep_class_base, deprecated_class)?; + + insert_value_from_var_name("compiled_class", dep_class_base, vm, ids_data, ap_tracking)?; + + Ok(()) +} diff --git a/src/hints/mod.rs b/src/hints/mod.rs index 6f4a329a..eb426f3b 100644 --- a/src/hints/mod.rs +++ b/src/hints/mod.rs @@ -1,10 +1,10 @@ +pub mod block_context; pub mod hints_raw; -use std::any::Any; -use std::collections::hash_map::IntoIter; use std::collections::HashMap; use std::rc::Rc; +use crate::io::StarknetOsInput; use cairo_vm::felt::Felt252; use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::{ BuiltinHintProcessor, HintFunc, @@ -15,10 +15,6 @@ use cairo_vm::serde::deserialize_program::ApTracking; use cairo_vm::types::exec_scope::ExecutionScopes; use cairo_vm::vm::errors::hint_errors::HintError; use cairo_vm::vm::vm_core::VirtualMachine; -use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContractClass; - -use crate::io::classes::write_deprecated_class; -use crate::io::StarknetOsInput; pub fn sn_hint_processor() -> BuiltinHintProcessor { let mut hint_processor = BuiltinHintProcessor::new_empty(); @@ -26,13 +22,13 @@ pub fn sn_hint_processor() -> BuiltinHintProcessor { let sn_os_input = HintFunc(Box::new(starknet_os_input)); hint_processor.add_hint(String::from(hints_raw::STARKNET_OS_INPUT), Rc::new(sn_os_input)); - let load_class_facts = HintFunc(Box::new(load_class_facts)); + let load_class_facts = HintFunc(Box::new(block_context::load_class_facts)); hint_processor.add_hint(String::from(hints_raw::LOAD_CLASS_FACTS), Rc::new(load_class_facts)); - let load_deprecated_class_facts = HintFunc(Box::new(load_deprecated_class_facts)); + let load_deprecated_class_facts = HintFunc(Box::new(block_context::load_deprecated_class_facts)); hint_processor.add_hint(String::from(hints_raw::LOAD_DEPRECATED_CLASS_FACTS), Rc::new(load_deprecated_class_facts)); - let load_deprecated_class_inner = HintFunc(Box::new(load_deprecated_inner)); + let load_deprecated_class_inner = HintFunc(Box::new(block_context::load_deprecated_inner)); hint_processor.add_hint(String::from(hints_raw::LOAD_DEPRECATED_CLASS_INNER), Rc::new(load_deprecated_class_inner)); let check_deprecated_class_hash_hint = HintFunc(Box::new(check_deprecated_class_hash)); @@ -73,98 +69,6 @@ pub fn starknet_os_input( Ok(()) } -// Implements hint: -// -// ids.compiled_class_facts = segments.add() -// ids.n_compiled_class_facts = len(os_input.compiled_classes) -// vm_enter_scope({ -// 'compiled_class_facts': iter(os_input.compiled_classes.items()), -// }) -pub fn load_class_facts( - vm: &mut VirtualMachine, - exec_scopes: &mut ExecutionScopes, - ids_data: &HashMap, - ap_tracking: &ApTracking, - _constants: &HashMap, -) -> Result<(), HintError> { - let os_input = exec_scopes.get::("os_input")?; - let compiled_class_facts_ptr = vm.add_memory_segment(); - insert_value_from_var_name("compiled_class_facts", compiled_class_facts_ptr, vm, ids_data, ap_tracking)?; - - insert_value_from_var_name("n_compiled_class_facts", os_input.compiled_classes.len(), vm, ids_data, ap_tracking)?; - - let scoped_classes: Box = Box::new(os_input.compiled_classes.into_iter()); - exec_scopes.enter_scope(HashMap::from([(String::from("compiled_class_facts"), scoped_classes)])); - - Ok(()) -} - -// Implements hint: -// -// # Creates a set of deprecated class hashes to distinguish calls to deprecated entry points. -// __deprecated_class_hashes=set(os_input.deprecated_compiled_classes.keys()) -// ids.compiled_class_facts = segments.add() -// ids.n_compiled_class_facts = len(os_input.deprecated_compiled_classes) -// vm_enter_scope({ -// 'compiled_class_facts': iter(os_input.deprecated_compiled_classes.items()), -// }) -pub fn load_deprecated_class_facts( - vm: &mut VirtualMachine, - exec_scopes: &mut ExecutionScopes, - ids_data: &HashMap, - ap_tracking: &ApTracking, - _constants: &HashMap, -) -> Result<(), HintError> { - let os_input = exec_scopes.get::("os_input")?; - let compiled_class_facts_ptr = vm.add_memory_segment(); - insert_value_from_var_name("compiled_class_facts", compiled_class_facts_ptr, vm, ids_data, ap_tracking)?; - - insert_value_from_var_name( - "n_compiled_class_facts", - os_input.deprecated_compiled_classes.len(), - vm, - ids_data, - ap_tracking, - )?; - let scoped_classes: Box = Box::new(os_input.deprecated_compiled_classes.into_iter()); - exec_scopes.enter_scope(HashMap::from([(String::from("compiled_class_facts"), scoped_classes)])); - - Ok(()) -} - -// Implements hint: -// -// from starkware.starknet.core.os.contract_class.deprecated_class_hash import ( -// get_deprecated_contract_class_struct, -// ) -// -// compiled_class_hash, compiled_class = next(compiled_class_facts) -// -// cairo_contract = get_deprecated_contract_class_struct( -// identifiers=ids._context.identifiers, contract_class=compiled_class) -// ids.compiled_class = segments.gen_arg(cairo_contract) -pub fn load_deprecated_inner( - vm: &mut VirtualMachine, - exec_scopes: &mut ExecutionScopes, - ids_data: &HashMap, - ap_tracking: &ApTracking, - _constants: &HashMap, -) -> Result<(), HintError> { - let deprecated_class_iter = - exec_scopes.get_mut_ref::>("compiled_class_facts").unwrap(); - - let (class_hash, deprecated_class) = deprecated_class_iter.next().unwrap(); - - exec_scopes.insert_value("compiled_class_hash", class_hash); - - let dep_class_base = vm.add_memory_segment(); - write_deprecated_class(vm, dep_class_base, deprecated_class)?; - - insert_value_from_var_name("compiled_class", dep_class_base, vm, ids_data, ap_tracking)?; - - Ok(()) -} - // Implements hint: // // from starkware.python.utils import from_bytes diff --git a/tests/hints.rs b/tests/hints.rs index bdd6a5a8..f0134632 100644 --- a/tests/hints.rs +++ b/tests/hints.rs @@ -19,8 +19,9 @@ use cairo_vm::vm::vm_core::VirtualMachine; use common::load_and_write_input; use common::utils::check_output_vs_python; use rstest::*; +use snos::hints::block_context::{load_deprecated_class_facts, load_deprecated_inner}; use snos::hints::hints_raw::*; -use snos::hints::{check_deprecated_class_hash, load_deprecated_class_facts, load_deprecated_inner, starknet_os_input}; +use snos::hints::{check_deprecated_class_hash, starknet_os_input}; use snos::io::StarknetOsInput; #[fixture] From 7c1ab549771c014381120fb95547a8399bae3578 Mon Sep 17 00:00:00 2001 From: LucasLvy Date: Mon, 16 Oct 2023 16:05:27 +0200 Subject: [PATCH 2/3] feat(hints): add sequencer address --- src/hints/block_context/mod.rs | 75 +++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 28 deletions(-) diff --git a/src/hints/block_context/mod.rs b/src/hints/block_context/mod.rs index d8c16d83..895ea1e9 100644 --- a/src/hints/block_context/mod.rs +++ b/src/hints/block_context/mod.rs @@ -1,8 +1,9 @@ use cairo_felt::Felt252; -use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::insert_value_from_var_name; +use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::{insert_value_from_var_name, insert_value_into_ap}; use cairo_vm::hint_processor::hint_processor_definition::HintReference; use cairo_vm::serde::deserialize_program::ApTracking; use cairo_vm::types::exec_scope::ExecutionScopes; +use cairo_vm::types::relocatable::MaybeRelocatable; use cairo_vm::vm::errors::hint_errors::HintError; use cairo_vm::vm::vm_core::VirtualMachine; use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContractClass; @@ -12,13 +13,14 @@ use std::collections::HashMap; use crate::io::classes::write_deprecated_class; use crate::io::StarknetOsInput; -// Implements hint: -// -// ids.compiled_class_facts = segments.add() -// ids.n_compiled_class_facts = len(os_input.compiled_classes) -// vm_enter_scope({ -// 'compiled_class_facts': iter(os_input.compiled_classes.items()), -// }) + +/// Implements hint: +/// +/// ids.compiled_class_facts = segments.add() +/// ids.n_compiled_class_facts = len(os_input.compiled_classes) +/// vm_enter_scope({ +/// 'compiled_class_facts': iter(os_input.compiled_classes.items()), +/// }) pub fn load_class_facts( vm: &mut VirtualMachine, exec_scopes: &mut ExecutionScopes, @@ -38,15 +40,15 @@ pub fn load_class_facts( Ok(()) } -// Implements hint: -// -// # Creates a set of deprecated class hashes to distinguish calls to deprecated entry points. -// __deprecated_class_hashes=set(os_input.deprecated_compiled_classes.keys()) -// ids.compiled_class_facts = segments.add() -// ids.n_compiled_class_facts = len(os_input.deprecated_compiled_classes) -// vm_enter_scope({ -// 'compiled_class_facts': iter(os_input.deprecated_compiled_classes.items()), -// }) +/// Implements hint: +/// +/// # Creates a set of deprecated class hashes to distinguish calls to deprecated entry points. +/// __deprecated_class_hashes=set(os_input.deprecated_compiled_classes.keys()) +/// ids.compiled_class_facts = segments.add() +/// ids.n_compiled_class_facts = len(os_input.deprecated_compiled_classes) +/// vm_enter_scope({ +/// 'compiled_class_facts': iter(os_input.deprecated_compiled_classes.items()), +/// }) pub fn load_deprecated_class_facts( vm: &mut VirtualMachine, exec_scopes: &mut ExecutionScopes, @@ -71,17 +73,17 @@ pub fn load_deprecated_class_facts( Ok(()) } -// Implements hint: -// -// from starkware.starknet.core.os.contract_class.deprecated_class_hash import ( -// get_deprecated_contract_class_struct, -// ) -// -// compiled_class_hash, compiled_class = next(compiled_class_facts) -// -// cairo_contract = get_deprecated_contract_class_struct( -// identifiers=ids._context.identifiers, contract_class=compiled_class) -// ids.compiled_class = segments.gen_arg(cairo_contract) +/// Implements hint: +/// +/// from starkware.starknet.core.os.contract_class.deprecated_class_hash import ( +/// get_deprecated_contract_class_struct, +/// ) +/// +/// compiled_class_hash, compiled_class = next(compiled_class_facts) +/// +/// cairo_contract = get_deprecated_contract_class_struct( +/// identifiers=ids._context.identifiers, contract_class=compiled_class) +/// ids.compiled_class = segments.gen_arg(cairo_contract) pub fn load_deprecated_inner( vm: &mut VirtualMachine, exec_scopes: &mut ExecutionScopes, @@ -103,3 +105,20 @@ pub fn load_deprecated_inner( Ok(()) } + +/// Implements hint: +/// +/// os_input.general_config.sequencer_address +pub fn sequencer_address( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + _ids_data: &HashMap, + _ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + let os_input = exec_scopes.get::("os_input")?; + insert_value_into_ap( + vm, + MaybeRelocatable::Int(Felt252::from_bytes_be(&os_input.general_config.sequencer_address.0.key().bytes())), + ) +} From 1148758292256f0d86ddbc42a474771ae410fc7f Mon Sep 17 00:00:00 2001 From: LucasLvy Date: Mon, 16 Oct 2023 16:12:18 +0200 Subject: [PATCH 3/3] test(hints): nondet sequencer address --- src/hints/block_context/mod.rs | 9 +++++---- src/hints/hints_raw.rs | 3 +++ src/hints/mod.rs | 3 ++- tests/hints.rs | 7 +++++-- tests/programs/load_deprecated_class.cairo | 2 ++ 5 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/hints/block_context/mod.rs b/src/hints/block_context/mod.rs index 895ea1e9..49494439 100644 --- a/src/hints/block_context/mod.rs +++ b/src/hints/block_context/mod.rs @@ -1,3 +1,7 @@ +use std::any::Any; +use std::collections::hash_map::IntoIter; +use std::collections::HashMap; + use cairo_felt::Felt252; use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::{insert_value_from_var_name, insert_value_into_ap}; use cairo_vm::hint_processor::hint_processor_definition::HintReference; @@ -7,9 +11,6 @@ use cairo_vm::types::relocatable::MaybeRelocatable; use cairo_vm::vm::errors::hint_errors::HintError; use cairo_vm::vm::vm_core::VirtualMachine; use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContractClass; -use std::any::Any; -use std::collections::hash_map::IntoIter; -use std::collections::HashMap; use crate::io::classes::write_deprecated_class; use crate::io::StarknetOsInput; @@ -119,6 +120,6 @@ pub fn sequencer_address( let os_input = exec_scopes.get::("os_input")?; insert_value_into_ap( vm, - MaybeRelocatable::Int(Felt252::from_bytes_be(&os_input.general_config.sequencer_address.0.key().bytes())), + MaybeRelocatable::Int(Felt252::from_bytes_be(os_input.general_config.sequencer_address.0.key().bytes())), ) } diff --git a/src/hints/hints_raw.rs b/src/hints/hints_raw.rs index df219c7f..783c593f 100644 --- a/src/hints/hints_raw.rs +++ b/src/hints/hints_raw.rs @@ -25,3 +25,6 @@ pub const CHECK_DEPRECATED_CLASS_HASH: &str = compiled_class_hash\nassert computed_hash == expected_hash, (\n \"Computed compiled_class_hash is \ inconsistent with the hash in the os_input. \"\n f\"Computed hash = {computed_hash}, Expected hash = \ {expected_hash}.\")\n\nvm_load_program(compiled_class.program, ids.compiled_class.bytecode_ptr)"; + +/// This is the equivalent of nondet %{ os_input.general_config.sequencer_address %} +pub const SEQUENCER_ADDRESS: &str = "memory[ap] = to_felt_or_relocatable(os_input.general_config.sequencer_address)"; diff --git a/src/hints/mod.rs b/src/hints/mod.rs index eb426f3b..6d0e4bfa 100644 --- a/src/hints/mod.rs +++ b/src/hints/mod.rs @@ -4,7 +4,6 @@ pub mod hints_raw; use std::collections::HashMap; use std::rc::Rc; -use crate::io::StarknetOsInput; use cairo_vm::felt::Felt252; use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::{ BuiltinHintProcessor, HintFunc, @@ -16,6 +15,8 @@ use cairo_vm::types::exec_scope::ExecutionScopes; use cairo_vm::vm::errors::hint_errors::HintError; use cairo_vm::vm::vm_core::VirtualMachine; +use crate::io::StarknetOsInput; + pub fn sn_hint_processor() -> BuiltinHintProcessor { let mut hint_processor = BuiltinHintProcessor::new_empty(); diff --git a/tests/hints.rs b/tests/hints.rs index f0134632..6e1aa24d 100644 --- a/tests/hints.rs +++ b/tests/hints.rs @@ -19,7 +19,7 @@ use cairo_vm::vm::vm_core::VirtualMachine; use common::load_and_write_input; use common::utils::check_output_vs_python; use rstest::*; -use snos::hints::block_context::{load_deprecated_class_facts, load_deprecated_inner}; +use snos::hints::block_context::{load_deprecated_class_facts, load_deprecated_inner, sequencer_address}; use snos::hints::hints_raw::*; use snos::hints::{check_deprecated_class_hash, starknet_os_input}; use snos::io::StarknetOsInput; @@ -36,7 +36,7 @@ fn os_input_hint_processor(_load_and_write_input: &(StarknetOsInput, String)) -> // TODO: remove should panic once fixed #[rstest] -fn load_deprecated_class_test(mut os_input_hint_processor: BuiltinHintProcessor) { +fn block_context_test(mut os_input_hint_processor: BuiltinHintProcessor) { let program = "build/programs/load_deprecated_class.json"; let load_deprecated_class_facts_hint = HintFunc(Box::new(load_deprecated_class_facts)); @@ -51,6 +51,9 @@ fn load_deprecated_class_test(mut os_input_hint_processor: BuiltinHintProcessor) os_input_hint_processor .add_hint(String::from(CHECK_DEPRECATED_CLASS_HASH), Rc::new(check_deprecated_class_hash_hint)); + let sequencer_address_hint = HintFunc(Box::new(sequencer_address)); + os_input_hint_processor.add_hint(String::from(SEQUENCER_ADDRESS), Rc::new(sequencer_address_hint)); + let debug_hint = HintFunc(Box::new(debug_id)); os_input_hint_processor.add_hint(String::from("print('COMPILED: ', ids.hash)"), Rc::new(debug_hint)); diff --git a/tests/programs/load_deprecated_class.cairo b/tests/programs/load_deprecated_class.cairo index c727094a..d0296bb0 100644 --- a/tests/programs/load_deprecated_class.cairo +++ b/tests/programs/load_deprecated_class.cairo @@ -50,6 +50,8 @@ func main{output_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { assert n_compiled_class_facts = 5; + tempvar sequencer_address = nondet %{ os_input.general_config.sequencer_address %}; + return (); }