Skip to content

Commit

Permalink
Merge pull request #35 from keep-starknet-strange/lucas/another-hint
Browse files Browse the repository at this point in the history
Block context: sequencer address
  • Loading branch information
drspacemn authored Oct 16, 2023
2 parents 2a0ab8f + 1148758 commit 1447a15
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 101 deletions.
125 changes: 125 additions & 0 deletions src/hints/block_context/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
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;
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;

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<String, HintReference>,
ap_tracking: &ApTracking,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let os_input = exec_scopes.get::<StarknetOsInput>("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<dyn Any> = 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<String, HintReference>,
ap_tracking: &ApTracking,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let os_input = exec_scopes.get::<StarknetOsInput>("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<dyn Any> = 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<String, HintReference>,
ap_tracking: &ApTracking,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let deprecated_class_iter =
exec_scopes.get_mut_ref::<IntoIter<Felt252, DeprecatedContractClass>>("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:
///
/// os_input.general_config.sequencer_address
pub fn sequencer_address(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
_ids_data: &HashMap<String, HintReference>,
_ap_tracking: &ApTracking,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let os_input = exec_scopes.get::<StarknetOsInput>("os_input")?;
insert_value_into_ap(
vm,
MaybeRelocatable::Int(Felt252::from_bytes_be(os_input.general_config.sequencer_address.0.key().bytes())),
)
}
3 changes: 3 additions & 0 deletions src/hints/hints_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)";
103 changes: 4 additions & 99 deletions src/hints/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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;

Expand All @@ -15,9 +14,7 @@ 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 {
Expand All @@ -26,13 +23,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));
Expand Down Expand Up @@ -73,98 +70,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<String, HintReference>,
ap_tracking: &ApTracking,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let os_input = exec_scopes.get::<StarknetOsInput>("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<dyn Any> = 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<String, HintReference>,
ap_tracking: &ApTracking,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let os_input = exec_scopes.get::<StarknetOsInput>("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<dyn Any> = 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<String, HintReference>,
ap_tracking: &ApTracking,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let deprecated_class_iter =
exec_scopes.get_mut_ref::<IntoIter<Felt252, DeprecatedContractClass>>("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
Expand Down
8 changes: 6 additions & 2 deletions tests/hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, sequencer_address};
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]
Expand All @@ -35,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));
Expand All @@ -50,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));

Expand Down
2 changes: 2 additions & 0 deletions tests/programs/load_deprecated_class.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
}

Expand Down

0 comments on commit 1447a15

Please sign in to comment.