openvm_circuit/system/memory/controller/
dimensions.rs

1use derive_new::new;
2use openvm_stark_backend::p3_util::log2_strict_usize;
3use serde::{Deserialize, Serialize};
4
5use crate::{arch::MemoryConfig, system::memory::CHUNK};
6
7// indicates that there are 2^`as_height` address spaces numbered starting from `as_offset`,
8// and that each address space has 2^`address_height` addresses numbered starting from 0
9#[derive(Clone, Copy, Debug, Serialize, Deserialize, new)]
10pub struct MemoryDimensions {
11    /// Address space height
12    pub as_height: usize,
13    /// Pointer height
14    pub address_height: usize,
15    /// Address space offset
16    pub as_offset: u32,
17}
18
19impl MemoryDimensions {
20    pub fn overall_height(&self) -> usize {
21        self.as_height + self.address_height
22    }
23    /// Convert an address label (address space, block id) to its index in the memory merkle tree.
24    ///
25    /// Assumes that `label = (addr_space, block_id)` satisfies `block_id < 2^address_height`.
26    ///
27    /// This function is primarily for internal use for accessing the memory merkle tree.
28    /// Users should use a higher-level API when possible.
29    pub fn label_to_index(&self, (addr_space, block_id): (u32, u32)) -> u64 {
30        debug_assert!(block_id < (1 << self.address_height));
31        (((addr_space - self.as_offset) as u64) << self.address_height) + block_id as u64
32    }
33}
34
35impl MemoryConfig {
36    pub fn memory_dimensions(&self) -> MemoryDimensions {
37        MemoryDimensions {
38            as_height: self.as_height,
39            address_height: self.pointer_max_bits - log2_strict_usize(CHUNK),
40            as_offset: self.as_offset,
41        }
42    }
43}