Skip to content

Commit 9557d56

Browse files
authored
Merge branch 'main' into notlesh/segment-arena-relocation-fix-upstream
2 parents 21338b4 + 4e473eb commit 9557d56

File tree

6 files changed

+97
-10
lines changed

6 files changed

+97
-10
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44

55
* fix: Always use a normal segment in first SegmentArena segment [#1845](https://github.com/lambdaclass/cairo-vm/pull/1845)
66

7+
* feat: add get_current_step getter [#2034](https://github.com/lambdaclass/cairo-vm/pull/2034)
8+
9+
* feat: implement VirtualMachine::is_accessed [#2033](https://github.com/lambdaclass/cairo-vm/pull/2033)
10+
11+
* Refactor: Replaced HashMap with BTreeMap to guarantee deterministic ordering of the data [#2023] (https://github.com/lambdaclass/cairo-vm/pull/2023)
12+
13+
* 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)
14+
715
* fix: Keep None values in memory segments for the prover input info [#2021](https://github.com/lambdaclass/cairo-vm/pull/2021)
816

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

CONTRIBUTING.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ Enhancement suggestions are tracked as [GitHub issues](https://github.com/lambda
110110
- You may want to **include screenshots and animated GIFs** which help you demonstrate the steps or point out the part which the suggestion is related to. You can use [this tool](https://www.cockos.com/licecap/) to record GIFs on macOS and Windows, and [this tool](https://github.com/colinkeenan/silentcast) or [this tool](https://github.com/GNOME/byzanz) on Linux.
111111
- **Explain why this enhancement would be useful** to most cairo-vm users. You may also want to point out the other projects that solved it better and which could serve as inspiration.
112112

113+
### Pushing commits to Pull Requests
114+
115+
This repository enforces commit signing on all branches. See [this page](https://docs.github.com/en/authentication/managing-commit-signature-verification/signing-commits) for more details on how to sign a commit and how to manage signing keys in Github.
113116

114117
<!-- TODO
115118
### Your First Code Contribution

vm/src/vm/runners/cairo_runner.rs

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
math_utils::safe_div_usize,
55
stdlib::{
66
any::Any,
7-
collections::{HashMap, HashSet},
7+
collections::{BTreeMap, HashMap, HashSet},
88
ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign},
99
prelude::*,
1010
},
@@ -1516,10 +1516,21 @@ impl CairoRunner {
15161516
})
15171517
.collect();
15181518

1519-
let builtins_segments = self
1520-
.get_builtin_segment_info_for_pie()?
1521-
.into_iter()
1522-
.map(|(name, info)| (info.index as usize, name))
1519+
let builtins_segments: BTreeMap<usize, BuiltinName> = self
1520+
.vm
1521+
.builtin_runners
1522+
.iter()
1523+
.filter(|builtin| {
1524+
// Those segments are not treated as builtins by the prover.
1525+
!matches!(
1526+
builtin,
1527+
BuiltinRunner::SegmentArena(_) | BuiltinRunner::Output(_)
1528+
)
1529+
})
1530+
.map(|builtin| {
1531+
let (index, _) = builtin.get_memory_segment_addresses();
1532+
(index, builtin.name())
1533+
})
15231534
.collect();
15241535

15251536
Ok(ProverInputInfo {
@@ -1543,9 +1554,9 @@ pub struct ProverInputInfo {
15431554
/// A vector of segments, where each segment is a vector of maybe relocatable values or holes (`None`).
15441555
pub relocatable_memory: Vec<Vec<Option<MaybeRelocatable>>>,
15451556
/// A map from segment index to a vector of offsets within the segment, representing the public memory addresses.
1546-
pub public_memory_offsets: HashMap<usize, Vec<usize>>,
1557+
pub public_memory_offsets: BTreeMap<usize, Vec<usize>>,
15471558
/// A map from the builtin segment index into its name.
1548-
pub builtins_segments: HashMap<usize, BuiltinName>,
1559+
pub builtins_segments: BTreeMap<usize, BuiltinName>,
15491560
}
15501561

15511562
#[derive(Clone, Debug, Eq, PartialEq)]
@@ -5592,7 +5603,29 @@ mod tests {
55925603
assert!(prover_info.public_memory_offsets.is_empty());
55935604
assert_eq!(
55945605
prover_info.builtins_segments,
5595-
HashMap::from([(2, BuiltinName::ecdsa)])
5606+
BTreeMap::from([(2, BuiltinName::ecdsa)])
55965607
);
55975608
}
5609+
5610+
#[test]
5611+
fn test_output_not_builtin_segment() {
5612+
let program_content =
5613+
include_bytes!("../../../../cairo_programs/proof_programs/split_felt.json");
5614+
let runner = crate::cairo_run::cairo_run(
5615+
program_content,
5616+
&CairoRunConfig {
5617+
trace_enabled: true,
5618+
layout: LayoutName::all_cairo,
5619+
..Default::default()
5620+
},
5621+
&mut BuiltinHintProcessor::new_empty(),
5622+
)
5623+
.unwrap();
5624+
let prover_info = runner.get_prover_input_info().unwrap();
5625+
5626+
assert!(!prover_info
5627+
.builtins_segments
5628+
.values()
5629+
.any(|v| *v == BuiltinName::output));
5630+
}
55985631
}

vm/src/vm/vm_core.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,10 @@ impl VirtualMachine {
895895
self.run_context.get_pc()
896896
}
897897

898+
pub fn get_current_step(&self) -> usize {
899+
self.current_step
900+
}
901+
898902
///Gets the integer value corresponding to the Relocatable address
899903
pub fn get_integer(&self, key: Relocatable) -> Result<Cow<Felt252>, MemoryError> {
900904
self.segments.memory.get_integer(key)
@@ -971,6 +975,10 @@ impl VirtualMachine {
971975
self.segments.memory.mem_eq(lhs, rhs, len)
972976
}
973977

978+
pub fn is_accessed(&self, addr: &Relocatable) -> Result<bool, MemoryError> {
979+
self.segments.is_accessed(addr)
980+
}
981+
974982
///Gets `n_ret` return values from memory
975983
pub fn get_return_values(&self, n_ret: usize) -> Result<Vec<MaybeRelocatable>, MemoryError> {
976984
let addr = (self.run_context.get_ap() - n_ret)
@@ -4729,12 +4737,18 @@ mod tests {
47294737
((0, 1), 0),
47304738
((0, 2), 1),
47314739
((0, 10), 10),
4732-
((1, 1), 1)
4740+
((1, 1), 1),
4741+
((1, 2), 0),
4742+
((2, 0), 0),
4743+
((-1, 0), 0),
4744+
((-1, 1), 0)
47334745
];
47344746
vm.mark_address_range_as_accessed((0, 0).into(), 3).unwrap();
47354747
vm.mark_address_range_as_accessed((0, 10).into(), 2)
47364748
.unwrap();
47374749
vm.mark_address_range_as_accessed((1, 1).into(), 1).unwrap();
4750+
vm.mark_address_range_as_accessed((-1, 0).into(), 1)
4751+
.unwrap();
47384752
//Check that the following addresses have been accessed:
47394753
// Addresses have been copied from python execution:
47404754
let mem = &vm.segments.memory.data;
@@ -4755,6 +4769,14 @@ mod tests {
47554769
.get_amount_of_accessed_addresses_for_segment(1),
47564770
Some(1)
47574771
);
4772+
assert!(vm.is_accessed(&Relocatable::from((0, 0))).unwrap());
4773+
assert!(vm.is_accessed(&Relocatable::from((0, 2))).unwrap());
4774+
assert!(vm.is_accessed(&Relocatable::from((0, 10))).unwrap());
4775+
assert!(vm.is_accessed(&Relocatable::from((1, 1))).unwrap());
4776+
assert!(!vm.is_accessed(&Relocatable::from((1, 2))).unwrap());
4777+
assert!(!vm.is_accessed(&Relocatable::from((2, 0))).unwrap());
4778+
assert!(vm.is_accessed(&Relocatable::from((-1, 0))).unwrap());
4779+
assert!(!vm.is_accessed(&Relocatable::from((-1, 1))).unwrap());
47584780
}
47594781

47604782
#[test]
@@ -5152,7 +5174,7 @@ mod tests {
51525174
let mut virtual_machine_from_builder = virtual_machine_builder.build();
51535175

51545176
assert!(virtual_machine_from_builder.run_finished);
5155-
assert_eq!(virtual_machine_from_builder.current_step, 12);
5177+
assert_eq!(virtual_machine_from_builder.get_current_step(), 12);
51565178
assert_eq!(
51575179
virtual_machine_from_builder
51585180
.builtin_runners

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)