Skip to content

Commit 7b9e1c5

Browse files
feat: implement VirtualMachine::is_accessed (#2033)
* feat: implement VirtualMachine::is_accessed Signed-off-by: Dori Medini <[email protected]> * chore: refactor, test, changelog Signed-off-by: Dori Medini <[email protected]> --------- Signed-off-by: Dori Medini <[email protected]>
1 parent 60e6f24 commit 7b9e1c5

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
## Cairo-VM Changelog
22

33
#### Upcoming Changes
4+
5+
* feat: implement VirtualMachine::is_accessed [#2033](https://github.com/lambdaclass/cairo-vm/pull/2033)
6+
47
* Refactor: Replaced HashMap with BTreeMap to guarantee deterministic ordering of the data [#2023] (https://github.com/lambdaclass/cairo-vm/pull/2023)
58

69
* fix: Updated the logic for collecting builtin segment data for prover input info, removing dependency on the existence of stop pointers. [#2022](https://github.com/lambdaclass/cairo-vm/pull/2022)

vm/src/vm/vm_core.rs

Lines changed: 19 additions & 1 deletion
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)
@@ -4729,12 +4733,18 @@ mod tests {
47294733
((0, 1), 0),
47304734
((0, 2), 1),
47314735
((0, 10), 10),
4732-
((1, 1), 1)
4736+
((1, 1), 1),
4737+
((1, 2), 0),
4738+
((2, 0), 0),
4739+
((-1, 0), 0),
4740+
((-1, 1), 0)
47334741
];
47344742
vm.mark_address_range_as_accessed((0, 0).into(), 3).unwrap();
47354743
vm.mark_address_range_as_accessed((0, 10).into(), 2)
47364744
.unwrap();
47374745
vm.mark_address_range_as_accessed((1, 1).into(), 1).unwrap();
4746+
vm.mark_address_range_as_accessed((-1, 0).into(), 1)
4747+
.unwrap();
47384748
//Check that the following addresses have been accessed:
47394749
// Addresses have been copied from python execution:
47404750
let mem = &vm.segments.memory.data;
@@ -4755,6 +4765,14 @@ mod tests {
47554765
.get_amount_of_accessed_addresses_for_segment(1),
47564766
Some(1)
47574767
);
4768+
assert!(vm.is_accessed(&Relocatable::from((0, 0))).unwrap());
4769+
assert!(vm.is_accessed(&Relocatable::from((0, 2))).unwrap());
4770+
assert!(vm.is_accessed(&Relocatable::from((0, 10))).unwrap());
4771+
assert!(vm.is_accessed(&Relocatable::from((1, 1))).unwrap());
4772+
assert!(!vm.is_accessed(&Relocatable::from((1, 2))).unwrap());
4773+
assert!(!vm.is_accessed(&Relocatable::from((2, 0))).unwrap());
4774+
assert!(vm.is_accessed(&Relocatable::from((-1, 0))).unwrap());
4775+
assert!(!vm.is_accessed(&Relocatable::from((-1, 1))).unwrap());
47584776
}
47594777

47604778
#[test]

vm/src/vm/vm_memory/memory.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,23 @@ impl Memory {
648648
Ok(values)
649649
}
650650

651+
fn get_cell(&self, addr: Relocatable) -> Option<&MemoryCell> {
652+
let (i, j) = from_relocatable_to_indexes(addr);
653+
let data = if addr.segment_index < 0 {
654+
&self.temp_data
655+
} else {
656+
&self.data
657+
};
658+
data.get(i)?.get(j)
659+
}
660+
661+
pub fn is_accessed(&self, addr: &Relocatable) -> Result<bool, MemoryError> {
662+
Ok(self
663+
.get_cell(*addr)
664+
.ok_or(MemoryError::UnknownMemoryCell(Box::new(*addr)))?
665+
.is_accessed())
666+
}
667+
651668
pub fn mark_as_accessed(&mut self, addr: Relocatable) {
652669
let (i, j) = from_relocatable_to_indexes(addr);
653670
let data = if addr.segment_index < 0 {

vm/src/vm/vm_memory/memory_segments.rs

Lines changed: 4 additions & 0 deletions
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)