acir/circuit/opcodes/
memory_operation.rs

1use crate::native_types::{Expression, Witness};
2use acir_field::AcirField;
3use serde::{Deserialize, Serialize};
4
5/// Identifier for a block of memory
6#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash, Copy, Default)]
7#[cfg_attr(feature = "arb", derive(proptest_derive::Arbitrary))]
8pub struct BlockId(pub u32);
9
10impl std::fmt::Display for BlockId {
11    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
12        write!(f, "b{}", self.0)
13    }
14}
15
16/// Operation on a block of memory
17/// We can either write or read at an index in memory
18#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug, Hash)]
19#[cfg_attr(feature = "arb", derive(proptest_derive::Arbitrary))]
20pub struct MemOp<F> {
21    /// A constant expression that can be 0 (read) or 1 (write)
22    pub operation: Expression<F>,
23    /// array index, it must be less than the array length
24    pub index: Expression<F>,
25    /// the value we are reading, when operation is 0, or the value we write at
26    /// the specified index, when operation is 1
27    pub value: Expression<F>,
28}
29
30impl<F: AcirField> MemOp<F> {
31    /// Creates a `MemOp` which reads from memory at `index` and inserts the read value
32    /// into the [`WitnessMap`][crate::native_types::WitnessMap] at `witness`
33    pub fn read_at_mem_index(index: Expression<F>, witness: Witness) -> Self {
34        MemOp { operation: Expression::zero(), index, value: witness.into() }
35    }
36
37    /// Creates a `MemOp` which writes the [`Expression`] `value` into memory at `index`.
38    pub fn write_to_mem_index(index: Expression<F>, value: Expression<F>) -> Self {
39        MemOp { operation: Expression::one(), index, value }
40    }
41}