Skip to content

Commit

Permalink
Merge branch 'main' into remove-felt-to-from-biguint
Browse files Browse the repository at this point in the history
  • Loading branch information
fmoletta authored Jan 24, 2024
2 parents 518a5ec + 0a39534 commit e40f8a8
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 87 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
* felt_to_biguint
* felt_to_bigint

* feat: Add `print_output` flag to `cairo-1` crate [#1575] (https://github.com/lambdaclass/cairo-vm/pull/1575)

* bugfixes(BREAKING): Fix memory hole count inconsistencies #[1585] (https://github.com/lambdaclass/cairo-vm/pull/1585)
* Output builtin memory segment is no longer skipped when counting memory holes
* Temporary memory cells now keep their accessed status when relocated
* BREAKING: Signature change: `get_memory_holes(&self, builtin_count: usize) -> Result<usize, MemoryError>` -> `get_memory_holes(&self, builtin_count: usize, has_output_builtin: bool) -> Result<usize, MemoryError>`

* feat: Add `cairo_pie_output` flag to `cairo1-run` [#1581] (https://github.com/lambdaclass/cairo-vm/pull/1581)

* feat: Add `cairo_pie_output` flag to `cairo_vm_cli` [#1578] (https://github.com/lambdaclass/cairo-vm/pull/1578)
Expand All @@ -20,7 +27,7 @@

* feat: Add `air_private_input` flag to `cairo1-run` [#1559] (https://github.com/lambdaclass/cairo-vm/pull/1559)

* feat: Add `args` flag to `cairo1-run` [#15551] (https://github.com/lambdaclass/cairo-vm/pull/15551)
* feat: Add `args` flag to `cairo1-run` [#1551] (https://github.com/lambdaclass/cairo-vm/pull/1551)

* feat: Add `air_public_input` flag to `cairo1-run` [#1539] (https://github.com/lambdaclass/cairo-vm/pull/1539)

Expand Down
191 changes: 119 additions & 72 deletions cairo1-run/src/main.rs

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions cairo_programs/cairo-1-programs/struct_span_return.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use core::array::SpanTrait;
use core::array::ArrayTrait;


fn main() -> Array<Array<u32>> {
let mut numbers = ArrayTrait::new();
let mut numbers_a = ArrayTrait::new();
let mut numbers_b = ArrayTrait::new();
numbers_a.append(4_u32);
numbers_a.append(3_u32);
numbers_b.append(2_u32);
numbers_b.append(1_u32);
numbers.append(numbers_a);
numbers.append(numbers_b);

numbers
}
5 changes: 0 additions & 5 deletions vm/src/tests/cairo_pie_comparator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
with ZipFile(filename1) as cairo_lang_pie_zip, ZipFile(filename2) as cairo_vm_pie_zip:
# Compare json files
for file in json_files:
# Skipping this check due to a bug in the memory holes count
# TODO: Remove it once the bug is fixed
if (filename1 == "../../../cairo_programs/_keccak_alternative_hint.pie.zip" or filename1 == "../../../cairo_programs/relocate_temporary_segment_append.pie.zip" or filename1 == "../../../cairo_programs/keccak_alternative_hint.pie.zip") and file == "execution_resources.json":
print(f"Comparison skipped for {filename1}/{file} vs {filename2}/{file}")
continue
with cairo_lang_pie_zip.open(file) as cairo_lang_file, cairo_vm_pie_zip.open(file) as cairo_vm_file:
cl_content = json.load(cairo_lang_file)
cv_content = json.load(cairo_vm_file)
Expand Down
5 changes: 4 additions & 1 deletion vm/src/vm/runners/cairo_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,10 @@ impl CairoRunner {

/// Count the number of holes present in the segments.
pub fn get_memory_holes(&self, vm: &VirtualMachine) -> Result<usize, MemoryError> {
vm.segments.get_memory_holes(vm.builtin_runners.len())
vm.segments.get_memory_holes(
vm.builtin_runners.len(),
self.program.builtins.contains(&BuiltinName::output),
)
}

/// Check if there are enough trace cells to fill the entire diluted checks.
Expand Down
4 changes: 4 additions & 0 deletions vm/src/vm/vm_memory/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ impl Memory {
if let Some(cell) = cell {
// Rely on Memory::insert to catch memory inconsistencies
self.insert(addr, cell.get_value())?;
// If the cell is accessed, mark the relocated one as accessed too
if cell.is_accessed() {
self.mark_as_accessed(addr)
}
}
addr = (addr + 1)?;
}
Expand Down
25 changes: 17 additions & 8 deletions vm/src/vm/vm_memory/memory_segments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,24 @@ impl MemorySegmentManager {
}
}

pub fn get_memory_holes(&self, builtin_count: usize) -> Result<usize, MemoryError> {
pub fn get_memory_holes(
&self,
builtin_count: usize,
has_output_builtin: bool,
) -> Result<usize, MemoryError> {
let data = &self.memory.data;
let mut memory_holes = 0;
let builtin_segments_start = 1; // program segment + execution segment
let builtin_segments_start = if has_output_builtin {
2 // program segment + execution segment + output segment
} else {
1 // program segment + execution segment
};
let builtin_segments_end = builtin_segments_start + builtin_count;
// Count the memory holes for each segment by substracting the amount of accessed_addresses from the segment's size
// Segments without accesses addresses are not accounted for when counting memory holes
for i in 0..data.len() {
// Instead of marking all of the builtin segment's address as accessed, we just skip them when counting memory holes
// Output builtin is extempt from this behaviour
if i > builtin_segments_start && i <= builtin_segments_end {
continue;
}
Expand Down Expand Up @@ -593,7 +602,7 @@ mod tests {
.memory
.mark_as_accessed((0, 0).into());
assert_eq!(
memory_segment_manager.get_memory_holes(0),
memory_segment_manager.get_memory_holes(0, false),
Err(MemoryError::MissingSegmentUsedSizes),
);
}
Expand All @@ -610,7 +619,7 @@ mod tests {
.mark_as_accessed((0, i).into());
}
assert_eq!(
memory_segment_manager.get_memory_holes(0),
memory_segment_manager.get_memory_holes(0, false),
Err(MemoryError::SegmentHasMoreAccessedAddressesThanSize(
Box::new((0, 3, 2))
)),
Expand All @@ -622,15 +631,15 @@ mod tests {
fn get_memory_holes_empty() {
let mut memory_segment_manager = MemorySegmentManager::new();
memory_segment_manager.segment_used_sizes = Some(Vec::new());
assert_eq!(memory_segment_manager.get_memory_holes(0), Ok(0),);
assert_eq!(memory_segment_manager.get_memory_holes(0, false), Ok(0),);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn get_memory_holes_empty2() {
let mut memory_segment_manager = MemorySegmentManager::new();
memory_segment_manager.segment_used_sizes = Some(vec![4]);
assert_eq!(memory_segment_manager.get_memory_holes(0), Ok(0),);
assert_eq!(memory_segment_manager.get_memory_holes(0, false), Ok(0),);
}

#[test]
Expand All @@ -653,7 +662,7 @@ mod tests {
.memory
.mark_as_accessed((0, i).into());
}
assert_eq!(memory_segment_manager.get_memory_holes(0), Ok(2),);
assert_eq!(memory_segment_manager.get_memory_holes(0, false), Ok(2),);
}

#[test]
Expand All @@ -678,7 +687,7 @@ mod tests {
.memory
.mark_as_accessed((0, i).into());
}
assert_eq!(memory_segment_manager.get_memory_holes(0), Ok(7),);
assert_eq!(memory_segment_manager.get_memory_holes(0, false), Ok(7),);
}

#[test]
Expand Down

0 comments on commit e40f8a8

Please sign in to comment.