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

Fix cairo1 Felt252DictEntryUpdate hint #1624

Merged
merged 7 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ use num_traits::One;
use crate::stdlib::collections::HashMap;
use crate::stdlib::prelude::*;

use crate::types::relocatable::MaybeRelocatable;
use crate::vm::errors::hint_errors::HintError;
use crate::Felt252;
use crate::{types::relocatable::Relocatable, vm::vm_core::VirtualMachine};

/// Stores the data of a specific dictionary.
pub struct DictTrackerExecScope {
/// The data of the dictionary.
data: HashMap<Felt252, Felt252>,
data: HashMap<Felt252, MaybeRelocatable>,
/// The index of the dictionary in the dict_infos segment.
#[allow(dead_code)]
idx: usize,
Expand Down Expand Up @@ -80,13 +81,13 @@ impl DictManagerExecScope {
}

/// Inserts a value to the dict tracker corresponding to a given pointer to a dict segment.
pub fn insert_to_tracker(&mut self, dict_end: Relocatable, key: Felt252, value: Felt252) {
pub fn insert_to_tracker(&mut self, dict_end: Relocatable, key: Felt252, value: MaybeRelocatable) {
self.get_dict_tracker_mut(dict_end).data.insert(key, value);
}

/// Gets a value from the dict tracker corresponding to a given pointer to a dict segment.
/// None if the key does not exist in the tracker data.
pub fn get_from_tracker(&self, dict_end: Relocatable, key: &Felt252) -> Option<Felt252> {
pub fn get_from_tracker(&self, dict_end: Relocatable, key: &Felt252) -> Option<MaybeRelocatable> {
self.get_dict_tracker(dict_end).ok()?.data.get(key).cloned()
}
}
Expand Down
11 changes: 8 additions & 3 deletions vm/src/hint_processor/cairo_1_hint_processor/hint_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::hint_processor::cairo_1_hint_processor::dict_manager::DictSquashExecScope;
use crate::hint_processor::hint_processor_definition::HintReference;
use crate::stdlib::{boxed::Box, collections::HashMap, prelude::*};
use crate::types::relocatable::Relocatable;
use crate::types::relocatable::{MaybeRelocatable, Relocatable};
use crate::vm::runners::cairo_runner::ResourceTracker;
use crate::vm::runners::cairo_runner::RunResources;
use crate::Felt252;
Expand Down Expand Up @@ -588,7 +588,8 @@
let (dict_base, dict_offset) = extract_buffer(dict_ptr)?;
let dict_address = get_ptr(vm, dict_base, &dict_offset)?;
let key = res_operand_get_val(vm, key)?;
let value = res_operand_get_val(vm, value)?;
// TODO: Fix, change to MaybeRelocatable
let value = MaybeRelocatable::Int(res_operand_get_val(vm, value)?);

Check warning on line 592 in vm/src/hint_processor/cairo_1_hint_processor/hint_processor.rs

View check run for this annotation

Codecov / codecov/patch

vm/src/hint_processor/cairo_1_hint_processor/hint_processor.rs#L592

Added line #L592 was not covered by tests
let dict_manager_exec_scope =
exec_scopes.get_mut_ref::<DictManagerExecScope>("dict_manager_exec_scope")?;

Expand Down Expand Up @@ -829,7 +830,11 @@
let (dict_base, dict_offset) = extract_buffer(dict_ptr)?;
let dict_address = get_ptr(vm, dict_base, &dict_offset)?;
let key = get_double_deref_val(vm, dict_base, &(dict_offset + Felt252::from(-3)))?;
let value = res_operand_get_val(vm, value)?;
let value: MaybeRelocatable = if let ResOperand::Deref(cell) = value {
get_mayberelocatable(vm, cell)?
} else {
MaybeRelocatable::Int(res_operand_get_val(vm, value)?)

Check warning on line 836 in vm/src/hint_processor/cairo_1_hint_processor/hint_processor.rs

View check run for this annotation

Codecov / codecov/patch

vm/src/hint_processor/cairo_1_hint_processor/hint_processor.rs#L836

Added line #L836 was not covered by tests
};
let dict_manager_exec_scope = exec_scopes
.get_mut_ref::<DictManagerExecScope>("dict_manager_exec_scope")
.map_err(|_| {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::stdlib::prelude::*;
use crate::types::relocatable::MaybeRelocatable;
use crate::types::{errors::math_errors::MathError, relocatable::Relocatable};
use crate::vm::errors::{hint_errors::HintError, vm_errors::VirtualMachineError};
use crate::vm::vm_core::VirtualMachine;
Expand Down Expand Up @@ -29,6 +30,16 @@
Ok((cell, base_offset))
}

pub(crate) fn get_mayberelocatable(
vm: &VirtualMachine,
cell: &CellRef,
) -> Result<MaybeRelocatable, VirtualMachineError> {
let relocatable = cell_ref_to_relocatable(cell, vm)?;
vm.get_maybe(&relocatable).ok_or_else(|| {
VirtualMachineError::InvalidMemoryValueTemporaryAddress(Box::new(relocatable))

Check warning on line 39 in vm/src/hint_processor/cairo_1_hint_processor/hint_processor_utils.rs

View check run for this annotation

Codecov / codecov/patch

vm/src/hint_processor/cairo_1_hint_processor/hint_processor_utils.rs#L39

Added line #L39 was not covered by tests
})
}

/// Fetches the value of `res_operand` from the vm.
pub(crate) fn get_val(
vm: &VirtualMachine,
Expand Down
Loading