Skip to content

feat: implement VirtualMachine::is_accessed #2024

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

Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

* fix: Keep None values in memory segments for the prover input info [#2021](https://github.com/lambdaclass/cairo-vm/pull/2021)

* feat: implement VirtualMachine::is_accessed [#2024](https://github.com/lambdaclass/cairo-vm/pull/2024)

* refactor: Clap attribute macros from #[clap(...)] to #[arg(...)] and #[command(...)] in v4.x [#2003] (https://github.com/lambdaclass/cairo-vm/pull/2003)

* fix: Fix `WriteReturnFp` error due to a bad loading of initial gas [#2015](https://github.com/lambdaclass/cairo-vm/pull/2015)
Expand Down
14 changes: 13 additions & 1 deletion vm/src/vm/vm_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,10 @@ impl VirtualMachine {
self.segments.memory.mem_eq(lhs, rhs, len)
}

pub fn is_accessed(&self, addr: &Relocatable) -> Result<bool, MemoryError> {
self.segments.is_accessed(addr)
}

///Gets `n_ret` return values from memory
pub fn get_return_values(&self, n_ret: usize) -> Result<Vec<MaybeRelocatable>, MemoryError> {
let addr = (self.run_context.get_ap() - n_ret)
Expand Down Expand Up @@ -4729,7 +4733,9 @@ mod tests {
((0, 1), 0),
((0, 2), 1),
((0, 10), 10),
((1, 1), 1)
((1, 1), 1),
((1, 2), 0),
((2, 0), 0)
];
vm.mark_address_range_as_accessed((0, 0).into(), 3).unwrap();
vm.mark_address_range_as_accessed((0, 10).into(), 2)
Expand All @@ -4755,6 +4761,12 @@ mod tests {
.get_amount_of_accessed_addresses_for_segment(1),
Some(1)
);
assert!(vm.is_accessed(&Relocatable::from((0, 0))).unwrap());
assert!(vm.is_accessed(&Relocatable::from((0, 2))).unwrap());
assert!(vm.is_accessed(&Relocatable::from((0, 10))).unwrap());
assert!(vm.is_accessed(&Relocatable::from((1, 1))).unwrap());
assert!(!vm.is_accessed(&Relocatable::from((1, 2))).unwrap());
assert!(!vm.is_accessed(&Relocatable::from((2, 0))).unwrap());
}

#[test]
Expand Down
25 changes: 18 additions & 7 deletions vm/src/vm/vm_memory/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,7 @@ impl Memory {
{
let relocatable: Relocatable = key.try_into().ok()?;

let data = if relocatable.segment_index.is_negative() {
&self.temp_data
} else {
&self.data
};
let (i, j) = from_relocatable_to_indexes(relocatable);
let value = data.get(i)?.get(j)?.get_value()?;
let value = self.get_cell(relocatable)?.get_value()?;
Some(Cow::Owned(self.relocate_value(&value).ok()?.into_owned()))
}

Expand Down Expand Up @@ -648,6 +642,23 @@ impl Memory {
Ok(values)
}

fn get_cell(&self, addr: Relocatable) -> Option<&MemoryCell> {
let (i, j) = from_relocatable_to_indexes(addr);
let data = if addr.segment_index < 0 {
&self.temp_data
} else {
&self.data
};
data.get(i)?.get(j)
}

pub fn is_accessed(&self, addr: &Relocatable) -> Result<bool, MemoryError> {
Ok(self
.get_cell(*addr)
.ok_or(MemoryError::UnknownMemoryCell(Box::new(*addr)))?
.is_accessed())
}

pub fn mark_as_accessed(&mut self, addr: Relocatable) {
let (i, j) = from_relocatable_to_indexes(addr);
let data = if addr.segment_index < 0 {
Expand Down
4 changes: 4 additions & 0 deletions vm/src/vm/vm_memory/memory_segments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ impl MemorySegmentManager {
}
}

pub fn is_accessed(&self, addr: &Relocatable) -> Result<bool, MemoryError> {
self.memory.is_accessed(addr)
}

/// Counts the memory holes (aka unaccessed memory cells) in memory
/// # Parameters
/// - `builtin_segment_indexes`: Set representing the segments indexes of the builtins initialized in the VM, except for the output builtin.
Expand Down
Loading