Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dict hints #455

Merged
merged 9 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/starknet-os/src/hints/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Compression constants
pub const N_UNIQUE_VALUE_BUCKETS: u64 = 6;
pub const TOTAL_N_BUCKETS: u64 = N_UNIQUE_VALUE_BUCKETS + 1;
137 changes: 137 additions & 0 deletions crates/starknet-os/src/hints/dict.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
use std::collections::HashMap;

use cairo_vm::hint_processor::builtin_hint_processor::dict_manager::Dictionary;
use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::{
get_maybe_relocatable_from_var_name, get_ptr_from_var_name, 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::types::relocatable::MaybeRelocatable;
use cairo_vm::vm::errors::hint_errors::HintError;
use cairo_vm::vm::vm_core::VirtualMachine;
use cairo_vm::Felt252;
use indoc::indoc;

use super::constants::TOTAL_N_BUCKETS;
use crate::hints::vars;

pub const DICTIONARY_FROM_BUCKET: &str =
indoc! {r#"initial_dict = {bucket_index: 0 for bucket_index in range(ids.TOTAL_N_BUCKETS)}"#};
pub fn dictionary_from_bucket(
_vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
_ids_data: &HashMap<String, HintReference>,
_ap_tracking: &ApTracking,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let initial_dict: HashMap<MaybeRelocatable, MaybeRelocatable> =
(0..TOTAL_N_BUCKETS).map(|bucket_index| (Felt252::from(bucket_index).into(), Felt252::ZERO.into())).collect();
exec_scopes.insert_box(vars::scopes::INITIAL_DICT, Box::new(initial_dict));
Ok(())
}

pub const GET_PREV_OFFSET: &str = indoc! {r#"
dict_tracker = __dict_manager.get_tracker(ids.dict_ptr)
ids.prev_offset = dict_tracker.data[ids.bucket_index]"#
};

pub fn get_prev_offset(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let dict_ptr = get_ptr_from_var_name(vars::ids::DICT_PTR, vm, ids_data, ap_tracking)?;

let dict_tracker = match exec_scopes.get_dict_manager()?.borrow().get_tracker(dict_ptr)?.data.clone() {
Dictionary::SimpleDictionary(hash_map) => hash_map,
Dictionary::DefaultDictionary { dict, .. } => dict,
};

let bucket_index = get_maybe_relocatable_from_var_name(vars::ids::BUCKET_INDEX, vm, ids_data, ap_tracking)?;

let prev_offset = dict_tracker.get(&bucket_index).unwrap().clone();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handling this unwrap is the only thing I can thing of.


exec_scopes.insert_box(vars::scopes::DICT_TRACKER, Box::new(dict_tracker));
insert_value_from_var_name(vars::ids::PREV_OFFSET, prev_offset, vm, ids_data, ap_tracking)?;
Ok(())
}

#[cfg(test)]
mod tests {
use std::cell::RefCell;
use std::rc::Rc;

use cairo_vm::hint_processor::builtin_hint_processor::dict_manager::DictManager;
use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::get_integer_from_var_name;
use cairo_vm::types::relocatable::Relocatable;
use rstest::rstest;

use super::*;

#[rstest]
fn test_dictionary_from_bucket() {
let mut vm = VirtualMachine::new(false);
vm.add_memory_segment();
vm.add_memory_segment();
vm.set_fp(2);

let ap_tracking = ApTracking::new();
let constants = HashMap::new();
let ids_data = HashMap::new();

vm.insert_value(Relocatable::from((1, 0)), Felt252::from(2)).unwrap();

let mut exec_scopes: ExecutionScopes = Default::default();

dictionary_from_bucket(&mut vm, &mut exec_scopes, &ids_data, &ap_tracking, &constants).unwrap();

let initial_dict: HashMap<MaybeRelocatable, MaybeRelocatable> =
exec_scopes.get(vars::scopes::INITIAL_DICT).unwrap();

assert_eq!(
initial_dict,
HashMap::from_iter(
[(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0)]
.map(|v| (Felt252::from(v.0).into(), Felt252::from(v.1).into()))
)
);
}

#[rstest]
fn test_get_prev_offset() {
let mut vm = VirtualMachine::new(false);
vm.add_memory_segment();
vm.add_memory_segment();
vm.set_fp(3);

let ap_tracking = ApTracking::new();
let constants = HashMap::new();
let ids_data = HashMap::from([
(vars::ids::DICT_PTR.to_string(), HintReference::new_simple(-3)),
(vars::ids::BUCKET_INDEX.to_string(), HintReference::new_simple(-2)),
(vars::ids::PREV_OFFSET.to_string(), HintReference::new_simple(-1)),
]);

let mut exec_scopes: ExecutionScopes = Default::default();

let mut dict_manager = DictManager::new();

let dict_ptr =
dict_manager.new_dict(&mut vm, HashMap::from([((1, 0).into(), MaybeRelocatable::from(123))])).unwrap();

insert_value_from_var_name(vars::ids::DICT_PTR, dict_ptr, &mut vm, &ids_data, &ap_tracking).unwrap();

insert_value_from_var_name(vars::ids::BUCKET_INDEX, (1, 0), &mut vm, &ids_data, &ap_tracking).unwrap();

exec_scopes.insert_value(vars::scopes::DICT_MANAGER, Rc::new(RefCell::new(dict_manager)));

get_prev_offset(&mut vm, &mut exec_scopes, &ids_data, &ap_tracking, &constants).unwrap();

let offset = get_integer_from_var_name(vars::ids::PREV_OFFSET, &vm, &ids_data, &ap_tracking).unwrap();

assert_eq!(offset, Felt252::from(123));
}
}
4 changes: 4 additions & 0 deletions crates/starknet-os/src/hints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ mod bls_field;
mod bls_utils;
pub mod builtins;
mod compiled_class;
mod constants;
mod deprecated_compiled_class;
mod dict;
mod execute_transactions;
pub mod execution;
mod find_element;
Expand Down Expand Up @@ -254,6 +256,8 @@ fn hints<PCS>() -> HashMap<String, HintImpl> where
hints.insert(compiled_class::SET_AP_TO_SEGMENT_HASH.into(), compiled_class::set_ap_to_segment_hash);
hints.insert(secp::READ_EC_POINT_ADDRESS.into(), secp::read_ec_point_from_address);
hints.insert(execute_transactions::SHA2_FINALIZE.into(), execute_transactions::sha2_finalize);
hints.insert(dict::DICTIONARY_FROM_BUCKET.into(), dict::dictionary_from_bucket);
hints.insert(dict::GET_PREV_OFFSET.into(), dict::get_prev_offset);
hints
}

Expand Down
4 changes: 4 additions & 0 deletions crates/starknet-os/src/hints/vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod scopes {
pub const DESCEND: &str = "descend";
pub const DESCENT_MAP: &str = "descent_map";
pub const DICT_MANAGER: &str = "dict_manager";
pub const DICT_TRACKER: &str = "dict_tracker";
pub const EXECUTION_HELPER: &str = "execution_helper";
pub const FIND_ELEMENT_MAX_SIZE: &str = "__find_element_max_size";
pub const INITIAL_DICT: &str = "initial_dict";
Expand Down Expand Up @@ -164,6 +165,9 @@ pub mod ids {
pub const N_UPDATES_SMALL_PACKING_BOUND: &str =
"starkware.starknet.core.os.state.output.N_UPDATES_SMALL_PACKING_BOUND";
pub const FULL_OUTPUT: &str = "full_output";
pub const PREV_OFFSET: &str = "prev_offset";
pub const BUCKET_INDEX: &str = "bucket_index";
pub const DICT_PTR: &str = "dict_ptr";
}

pub mod constants {
Expand Down
Loading