acir/circuit/opcodes/
memory_operation.rs

1use crate::native_types::Witness;
2use msgpack_tagged::MsgpackTagged;
3use serde::{Deserialize, Serialize};
4
5/// Identifier for a block of memory
6#[derive(Clone, Debug, PartialEq, Eq, Hash, Copy, Default)]
7#[derive(Serialize, Deserialize, MsgpackTagged)]
8#[cfg_attr(feature = "arb", derive(proptest_derive::Arbitrary))]
9pub struct BlockId(u32);
10
11impl BlockId {
12    /// Creates a `BlockId` identifying a block of memory by its raw index.
13    pub fn new(id: u32) -> Self {
14        BlockId(id)
15    }
16
17    /// Returns the raw index identifying this block of memory.
18    pub fn as_u32(&self) -> u32 {
19        self.0
20    }
21}
22
23impl std::fmt::Display for BlockId {
24    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25        write!(f, "b{}", self.0)
26    }
27}
28
29/// Whether a memory operation reads from or writes to memory
30#[derive(Clone, PartialEq, Eq, Debug, Hash, Copy)]
31#[derive(MsgpackTagged)]
32#[tagged(via(bool))]
33#[cfg_attr(feature = "arb", derive(proptest_derive::Arbitrary))]
34pub enum MemOpKind {
35    Read,
36    Write,
37}
38
39impl Serialize for MemOpKind {
40    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
41        serializer.serialize_bool(matches!(self, MemOpKind::Write))
42    }
43}
44
45impl<'de> Deserialize<'de> for MemOpKind {
46    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
47        Ok(if bool::deserialize(deserializer)? { MemOpKind::Write } else { MemOpKind::Read })
48    }
49}
50
51/// Operation on a block of memory
52/// We can either write or read at an index in memory
53#[derive(Clone, PartialEq, Eq, Debug, Hash)]
54#[derive(Serialize, Deserialize, MsgpackTagged)]
55#[cfg_attr(feature = "arb", derive(proptest_derive::Arbitrary))]
56pub struct MemOp {
57    #[serde(rename = "read")]
58    #[tag(0)]
59    pub operation: MemOpKind,
60    /// array index, it must be less than the array length
61    #[tag(1)]
62    pub index: Witness,
63    /// the witness we are reading into (read), or the witness whose value is written (write)
64    #[tag(2)]
65    pub value: Witness,
66}
67
68impl MemOp {
69    /// Creates a `MemOp` which reads from memory at `index` and inserts the read value
70    /// into the [`WitnessMap`][crate::native_types::WitnessMap] at `value`.
71    pub fn read_at_mem_index(index: Witness, value: Witness) -> Self {
72        MemOp { operation: MemOpKind::Read, index, value }
73    }
74
75    /// Creates a `MemOp` which writes `value` into memory at `index`.
76    pub fn write_to_mem_index(index: Witness, value: Witness) -> Self {
77        MemOp { operation: MemOpKind::Write, index, value }
78    }
79}