Skip to content

Commit 8404678

Browse files
feat: implement VirtualMachine::is_accessed
Signed-off-by: Dori Medini <[email protected]> refactor: implement get_cell to share code Signed-off-by: Dori Medini <[email protected]> chore: changelog entry Signed-off-by: Dori Medini <[email protected]>
1 parent 7c4ecdf commit 8404678

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#### Upcoming Changes
44

5+
* feat: implement VirtualMachine::is_accessed [#2024](https://github.com/lambdaclass/cairo-vm/pull/2024)
6+
57
* fix: Keep None values in memory segments for the prover input info [#2021](https://github.com/lambdaclass/cairo-vm/pull/2021)
68

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

vm/src/vm/vm_core.rs

+4
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,10 @@ impl VirtualMachine {
971971
self.segments.memory.mem_eq(lhs, rhs, len)
972972
}
973973

974+
pub fn is_accessed(&self, addr: &Relocatable) -> Result<bool, MemoryError> {
975+
self.segments.is_accessed(addr)
976+
}
977+
974978
///Gets `n_ret` return values from memory
975979
pub fn get_return_values(&self, n_ret: usize) -> Result<Vec<MaybeRelocatable>, MemoryError> {
976980
let addr = (self.run_context.get_ap() - n_ret)

vm/src/vm/vm_memory/memory.rs

+18-7
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,7 @@ impl Memory {
244244
{
245245
let relocatable: Relocatable = key.try_into().ok()?;
246246

247-
let data = if relocatable.segment_index.is_negative() {
248-
&self.temp_data
249-
} else {
250-
&self.data
251-
};
252-
let (i, j) = from_relocatable_to_indexes(relocatable);
253-
let value = data.get(i)?.get(j)?.get_value()?;
247+
let value = self.get_cell(relocatable)?.get_value()?;
254248
Some(Cow::Owned(self.relocate_value(&value).ok()?.into_owned()))
255249
}
256250

@@ -648,6 +642,23 @@ impl Memory {
648642
Ok(values)
649643
}
650644

645+
fn get_cell(&self, addr: Relocatable) -> Option<&MemoryCell> {
646+
let (i, j) = from_relocatable_to_indexes(addr);
647+
let data = if addr.segment_index < 0 {
648+
&self.temp_data
649+
} else {
650+
&self.data
651+
};
652+
data.get(i)?.get(j)
653+
}
654+
655+
pub fn is_accessed(&self, addr: &Relocatable) -> Result<bool, MemoryError> {
656+
Ok(self
657+
.get_cell(*addr)
658+
.ok_or(MemoryError::UnknownMemoryCell(Box::new(*addr)))?
659+
.is_accessed())
660+
}
661+
651662
pub fn mark_as_accessed(&mut self, addr: Relocatable) {
652663
let (i, j) = from_relocatable_to_indexes(addr);
653664
let data = if addr.segment_index < 0 {

vm/src/vm/vm_memory/memory_segments.rs

+4
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ impl MemorySegmentManager {
200200
}
201201
}
202202

203+
pub fn is_accessed(&self, addr: &Relocatable) -> Result<bool, MemoryError> {
204+
self.memory.is_accessed(addr)
205+
}
206+
203207
/// Counts the memory holes (aka unaccessed memory cells) in memory
204208
/// # Parameters
205209
/// - `builtin_segment_indexes`: Set representing the segments indexes of the builtins initialized in the VM, except for the output builtin.

0 commit comments

Comments
 (0)