Skip to content

Commit

Permalink
Add zero segment (#1668)
Browse files Browse the repository at this point in the history
* Ignore pesky pie zip files

* Add zero segment

* Handle zero segment when counting memory holes

* Add Changelog entry

* Fix macro
  • Loading branch information
fmoletta authored Mar 26, 2024
1 parent 44de614 commit 42e0416
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
**/*.memory
**/*.air_public_input
**/*.air_private_input
**/*.pie.zip
**/*.swp
bench/results
.python-version
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#### Upcoming Changes

* feat: Add zero segment [#1668](https://github.com/lambdaclass/cairo-vm/pull/1668)

* feat: Bump cairo_lang to 0.13.1 in testing env [#1687](https://github.com/lambdaclass/cairo-vm/pull/1687)

* feat(BREAKING): Use return type info from sierra when serializing return values in cairo1-run crate [#1665](https://github.com/lambdaclass/cairo-vm/pull/1665)
Expand Down
1 change: 0 additions & 1 deletion vm/src/hint_processor/builtin_hint_processor/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ mod test {
use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData;
use crate::hint_processor::builtin_hint_processor::hint_code;
use crate::hint_processor::hint_processor_definition::{HintProcessorLogic, HintReference};
use crate::stdlib::collections::HashMap;
use crate::types::exec_scope::ExecutionScopes;
use crate::utils::test_utils::*;
use crate::vm::vm_core::VirtualMachine;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ mod tests {
use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData;
use crate::hint_processor::hint_processor_definition::HintProcessorLogic;
use crate::hint_processor::hint_processor_definition::HintReference;
use crate::stdlib::collections::HashMap;
use crate::types::exec_scope::ExecutionScopes;
use crate::vm::vm_core::VirtualMachine;

Expand Down
1 change: 0 additions & 1 deletion vm/src/hint_processor/hint_processor_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ fn get_offset_value_reference(
#[cfg(test)]
mod tests {
use super::*;
use crate::stdlib::collections::HashMap;

use crate::{
relocatable,
Expand Down
11 changes: 3 additions & 8 deletions vm/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,9 @@ pub mod test_utils {
macro_rules! segments {
($( (($si:expr, $off:expr), $val:tt) ),* $(,)? ) => {
{
let memory = memory!($( (($si, $off), $val) ),*);
$crate::vm::vm_memory::memory_segments::MemorySegmentManager {
memory,
segment_sizes: HashMap::new(),
segment_used_sizes: None,
public_memory_offsets: HashMap::new(),
}

let mut segments = $crate::vm::vm_memory::memory_segments::MemorySegmentManager::new();
segments.memory = memory!($( (($si, $off), $val) ),*);
segments
}

};
Expand Down
1 change: 0 additions & 1 deletion vm/src/vm/runners/builtin_runner/bitwise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ mod tests {
use super::*;
use crate::relocatable;
use crate::serde::deserialize_program::BuiltinName;
use crate::stdlib::collections::HashMap;
use crate::vm::errors::memory_errors::MemoryError;
use crate::vm::runners::builtin_runner::BuiltinRunner;
use crate::vm::vm_core::VirtualMachine;
Expand Down
1 change: 0 additions & 1 deletion vm/src/vm/runners/builtin_runner/ec_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,6 @@ mod tests {
use super::*;
use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor;
use crate::serde::deserialize_program::BuiltinName;
use crate::stdlib::collections::HashMap;
use crate::types::program::Program;
use crate::utils::test_utils::*;
use crate::vm::errors::cairo_run_errors::CairoRunError;
Expand Down
1 change: 0 additions & 1 deletion vm/src/vm/runners/builtin_runner/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ mod tests {
use super::*;
use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor;
use crate::serde::deserialize_program::BuiltinName;
use crate::stdlib::collections::HashMap;
use crate::types::program::Program;
use crate::utils::test_utils::*;
use crate::vm::runners::cairo_runner::CairoRunner;
Expand Down
1 change: 0 additions & 1 deletion vm/src/vm/runners/builtin_runner/keccak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ impl KeccakBuiltinRunner {
mod tests {
use super::*;
use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor;
use crate::stdlib::collections::HashMap;
use crate::types::program::Program;
use crate::utils::test_utils::*;
use crate::vm::runners::cairo_runner::CairoRunner;
Expand Down
1 change: 0 additions & 1 deletion vm/src/vm/runners/builtin_runner/range_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ mod tests {
use super::*;
use crate::relocatable;
use crate::serde::deserialize_program::BuiltinName;
use crate::stdlib::collections::HashMap;
use crate::vm::vm_memory::memory::Memory;
use crate::{
hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor,
Expand Down
1 change: 0 additions & 1 deletion vm/src/vm/runners/builtin_runner/segment_arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ fn gen_arg(segments: &mut MemorySegmentManager, data: &[MaybeRelocatable; 3]) ->
#[cfg(test)]
mod tests {
use super::*;
use crate::stdlib::collections::HashMap;
use crate::vm::vm_core::VirtualMachine;
use crate::{relocatable, utils::test_utils::*, vm::runners::builtin_runner::BuiltinRunner};
#[cfg(target_arch = "wasm32")]
Expand Down
1 change: 0 additions & 1 deletion vm/src/vm/security.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ mod test {
use super::*;
use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor;
use crate::serde::deserialize_program::BuiltinName;
use crate::stdlib::collections::HashMap;

use crate::types::relocatable::Relocatable;

Expand Down
10 changes: 4 additions & 6 deletions vm/src/vm/vm_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1221,7 +1221,6 @@ mod tests {
use crate::vm::runners::builtin_runner::{
BITWISE_BUILTIN_NAME, EC_OP_BUILTIN_NAME, HASH_BUILTIN_NAME,
};
use crate::vm::vm_memory::memory::Memory;
use crate::{
any_box,
hint_processor::builtin_hint_processor::builtin_hint_processor_definition::{
Expand Down Expand Up @@ -4321,11 +4320,10 @@ mod tests {
ap: 18,
fp: 0,
})
.segments(MemorySegmentManager {
segment_sizes: HashMap::new(),
segment_used_sizes: Some(vec![1]),
public_memory_offsets: HashMap::new(),
memory: Memory::new(),
.segments({
let mut segments = MemorySegmentManager::new();
segments.segment_used_sizes = Some(vec![1]);
segments
})
.skip_instruction_execution(true)
.trace(Some(vec![TraceEntry {
Expand Down
59 changes: 54 additions & 5 deletions vm/src/vm/vm_memory/memory_segments.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use core::cmp::max;
use core::fmt;

use crate::Felt252;
use num_traits::Zero;

use crate::stdlib::prelude::*;
use crate::stdlib::{any::Any, collections::HashMap};
use crate::vm::runners::cairo_runner::CairoArg;
Expand All @@ -12,13 +16,20 @@ use crate::{
},
};

use super::memory::MemoryCell;

pub struct MemorySegmentManager {
pub segment_sizes: HashMap<usize, usize>,
pub segment_used_sizes: Option<Vec<usize>>,
pub(crate) memory: Memory,
// A map from segment index to a list of pairs (offset, page_id) that constitute the
// public memory. Note that the offset is absolute (not based on the page_id).
pub public_memory_offsets: HashMap<usize, Vec<(usize, usize)>>,
// Segment index of the zero segment index, a memory segment filled with zeroes, used exclusively by builtin runners
// This segment will never have index 0 so we use 0 to represent uninitialized value
zero_segment_index: usize,
// Segment size of the zero segment index
zero_segment_size: usize,
}

impl MemorySegmentManager {
Expand Down Expand Up @@ -71,6 +82,8 @@ impl MemorySegmentManager {
segment_used_sizes: None,
public_memory_offsets: HashMap::new(),
memory: Memory::new(),
zero_segment_index: 0,
zero_segment_size: 0,
}
}

Expand Down Expand Up @@ -206,11 +219,16 @@ impl MemorySegmentManager {
if i > builtin_segments_start && i <= builtin_segments_end {
continue;
}
let accessed_amount = match self.memory.get_amount_of_accessed_addresses_for_segment(i)
{
Some(accessed_amount) if accessed_amount > 0 => accessed_amount,
_ => continue,
};
let accessed_amount =
// Instead of marking the values in the zero segment until zero_segment_size as accessed we use zero_segment_size as accessed_amount
if !self.zero_segment_index.is_zero() && i == self.zero_segment_index {
self.zero_segment_size
} else {
match self.memory.get_amount_of_accessed_addresses_for_segment(i) {
Some(accessed_amount) if accessed_amount > 0 => accessed_amount,
_ => continue,
}
};
let segment_size = self
.get_segment_size(i)
.ok_or(MemoryError::MissingSegmentUsedSizes)?;
Expand Down Expand Up @@ -263,6 +281,37 @@ impl MemorySegmentManager {
self.public_memory_offsets
.insert(segment_index, public_memory.cloned().unwrap_or_default());
}

// TODO: remove allow
#[allow(unused)]
// Creates the zero segment if it wasn't previously created
// Fills the segment with the value 0 until size is reached
// Returns the index of the zero segment
pub(crate) fn add_zero_segment(&mut self, size: usize) -> usize {
if self.zero_segment_index.is_zero() {
self.zero_segment_index = self.add().segment_index as usize;
}
// Fil zero segment with zero values until size is reached
for _ in 0..self.zero_segment_size.saturating_sub(size) {
// As zero_segment_index is only accessible to the segment manager
// we can asume that it is always valid and index direcly into it
self.memory.data[self.zero_segment_index]
.push(Some(MemoryCell::new(Felt252::ZERO.into())))
}
self.zero_segment_size = max(self.zero_segment_size, size);
self.zero_segment_index
}

// TODO: remove allow
#[allow(unused)]
// Finalizes the zero segment and clears it's tracking data from the manager
pub(crate) fn finalize_zero_segment(&mut self) {
if !self.zero_segment_index.is_zero() {
self.finalize(Some(self.zero_segment_size), self.zero_segment_index, None);
self.zero_segment_index = 0;
self.zero_segment_size = 0;
}
}
}

impl Default for MemorySegmentManager {
Expand Down

0 comments on commit 42e0416

Please sign in to comment.