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