Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
rom_ram_logic.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Complete, auditors: [Raju], commit: 05a381f8b31ae4648e480f1369e911b148216e8b}
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
7#pragma once
8
10#include <array>
11#include <cstdint>
12#include <vector>
13
14namespace bb {
15// Forward declaration
16template <typename ExecutionTrace_> class UltraCircuitBuilder_;
17
18// Constants
19static constexpr uint32_t UNINITIALIZED_MEMORY_RECORD = UINT32_MAX;
20
28struct RomRecord {
29 uint32_t index_witness = 0; // Witness value of the index in the particular ROM block that contains this row.
32 uint32_t index = 0;
33 uint32_t record_witness = 0; // Record, a.k.a. "fingerprint" of the row.
34 size_t gate_index = 0; // Index in the memory block where the ROM gate will live.
35 bool operator<(const RomRecord& other) const { return index < other.index; }
36 bool operator==(const RomRecord& other) const noexcept
37 {
38 return index_witness == other.index_witness && value_column1_witness == other.value_column1_witness &&
39 value_column2_witness == other.value_column2_witness && index == other.index &&
40 record_witness == other.record_witness && gate_index == other.gate_index;
41 }
42};
43
53struct RamRecord {
57 };
58 uint32_t index_witness = 0;
59 uint32_t timestamp_witness = 0;
60 uint32_t value_witness = 0;
61 uint32_t index = 0;
62 uint32_t timestamp = 0;
64 uint32_t record_witness = 0; // Record, a.k.a. "fingerprint" of the row.
65 size_t gate_index = 0; // Index in the memory block where the RAM gate will live.
66 bool operator<(const RamRecord& other) const
67 {
68 bool index_test = (index) < (other.index);
69 return index_test || (index == other.index && timestamp < other.timestamp);
70 }
71 bool operator==(const RamRecord& other) const noexcept
72 {
73 return index_witness == other.index_witness && timestamp_witness == other.timestamp_witness &&
74 value_witness == other.value_witness && index == other.index && timestamp == other.timestamp &&
75 access_type == other.access_type && record_witness == other.record_witness &&
76 gate_index == other.gate_index;
77 }
78};
79
87 // Contains the value(s) of each index of the array. Note that each index/slot may contain _two_ values.
89 // A vector of records, each of which contains:
90 // + The constant witness with the index
91 // + The value in the memory slot
92 // + The actual index value
94 // Used to check that the state hasn't changed in tests
95 bool operator==(const RomTranscript& other) const noexcept
96 {
97 return (state == other.state && records == other.records);
98 }
99};
100
106 // Contains the value of each index of the array
107 std::vector<uint32_t> state;
108 // A vector of records, each of which contains:
109 // + The constant witness with the index
110 // + The type of operation (READ or WRITE)
111 // + The _current_ value in the memory slot
112 // + The actual index value
114 // The number of times this RAM array has been touched (i.e., has had a READ or WRITE operation performed on it).
115 // used for RAM records, to compute the timestamp when performing a read/write. Note that the timestamp is _not_ a
116 // global timestamp; rather, it is a timestamp for the RAM array in question.
117 size_t access_count = 0;
118 // Used to check that the state hasn't changed in tests
119 bool operator==(const RamTranscript& other) const noexcept
120 {
121 return (state == other.state && records == other.records && access_count == other.access_count);
122 }
123};
124
128template <typename ExecutionTrace> class RomRamLogic_ {
129 public:
130 using FF = typename ExecutionTrace::FF;
132
133 // Storage
148
149 RomRamLogic_() = default;
150
151 // ROM operations
161 size_t create_ROM_array(const size_t array_size);
162
172 const size_t rom_id,
173 const size_t index_value,
174 const uint32_t value_witness);
184 const size_t rom_id,
185 const size_t index_value,
186 const std::array<uint32_t, 2>& value_witnesses);
198 uint32_t read_ROM_array(CircuitBuilder* builder, const size_t rom_id, const uint32_t index_witness);
206 std::array<uint32_t, 2> read_ROM_array_pair(CircuitBuilder* builder,
207 const size_t rom_id,
208 const uint32_t index_witness);
233 void process_ROM_array(CircuitBuilder* builder, const size_t rom_id);
238
239 // RAM operations
249 size_t create_RAM_array(const size_t array_size);
259 const size_t ram_id,
260 const size_t index_value,
261 const uint32_t value_witness);
262 uint32_t read_RAM_array(CircuitBuilder* builder, const size_t ram_id, const uint32_t index_witness);
273 const size_t ram_id,
274 const uint32_t index_witness,
275 const uint32_t value_witness);
300 void create_final_sorted_RAM_gate(CircuitBuilder* builder, RamRecord& record, const size_t ram_array_size);
307 void process_RAM_array(CircuitBuilder* builder, const size_t ram_id);
309
310 bool operator==(const RomRamLogic_& other) const noexcept
311 {
312 return ram_arrays == other.ram_arrays && rom_arrays == other.rom_arrays;
313 }
314};
315
316} // namespace bb
ROM/RAM logic handler for UltraCircuitBuilder.
size_t create_ROM_array(const size_t array_size)
Create a new read-only memory region.
uint32_t read_ROM_array(CircuitBuilder *builder, const size_t rom_id, const uint32_t index_witness)
Read a single element from ROM.
void process_ROM_array(CircuitBuilder *builder, const size_t rom_id)
Compute additional gates required to validate ROM reads. Called when generating the proving key.
void create_sorted_RAM_gate(CircuitBuilder *builder, RamRecord &record)
Gate that performs consistency checks to validate that a claimed RAM read/write value is correct.
void process_ROM_arrays(CircuitBuilder *builder)
Process all of the ROM arrays.
std::array< uint32_t, 2 > read_ROM_array_pair(CircuitBuilder *builder, const size_t rom_id, const uint32_t index_witness)
Read a pair of elements from ROM.
void set_ROM_element(CircuitBuilder *builder, const size_t rom_id, const size_t index_value, const uint32_t value_witness)
Initialize a rom cell to equal value_witness
void create_final_sorted_RAM_gate(CircuitBuilder *builder, RamRecord &record, const size_t ram_array_size)
Performs consistency checks to validate that a claimed RAM read/write value is correct....
void process_RAM_arrays(CircuitBuilder *builder)
void init_RAM_element(CircuitBuilder *builder, const size_t ram_id, const size_t index_value, const uint32_t value_witness)
Initialize a RAM cell to equal value_witness
void create_sorted_ROM_gate(CircuitBuilder *builder, RomRecord &record)
Gate that performs consistency checks to validate that a claimed ROM read value is correct.
std::vector< RomTranscript > rom_arrays
Each entry in rom_arrays represents an independent ROM table. RomTranscript tracks the current table ...
void write_RAM_array(CircuitBuilder *builder, const size_t ram_id, const uint32_t index_witness, const uint32_t value_witness)
Write a cell in a RAM array.
void set_ROM_element_pair(CircuitBuilder *builder, const size_t rom_id, const size_t index_value, const std::array< uint32_t, 2 > &value_witnesses)
Initialize a ROM array element with a pair of witness values.
bool operator==(const RomRamLogic_ &other) const noexcept
std::vector< RamTranscript > ram_arrays
Each entry in ram_arrays represents an independent RAM table. RamTranscript tracks the current table ...
void create_ROM_gate(CircuitBuilder *builder, RomRecord &record)
Gate that'reads' from a ROM table, i.e., the table index is a witness not precomputed.
RomRamLogic_()=default
uint32_t read_RAM_array(CircuitBuilder *builder, const size_t ram_id, const uint32_t index_witness)
typename ExecutionTrace::FF FF
void create_RAM_gate(CircuitBuilder *builder, RamRecord &record)
Gate that performs a read/write operation into a RAM table, i.e. table index is a witness not precomp...
void process_RAM_array(CircuitBuilder *builder, const size_t ram_id)
Compute additional gates required to validate RAM read/writes. Called when generating the proving key...
size_t create_RAM_array(const size_t array_size)
Create a new updatable memory region.
AluTraceBuilder builder
Definition alu.test.cpp:124
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
A RAM memory record that can be ordered, first by index, then by timestamp.
uint32_t index_witness
uint32_t value_witness
AccessType access_type
uint32_t record_witness
bool operator<(const RamRecord &other) const
uint32_t timestamp_witness
bool operator==(const RamRecord &other) const noexcept
RamTranscript contains the RamRecords for a particular RAM table (recording READ and WRITE operations...
std::vector< RamRecord > records
bool operator==(const RamTranscript &other) const noexcept
std::vector< uint32_t > state
A ROM memory record that can be ordered, where the ordering is given by the index (a....
uint32_t value_column1_witness
bool operator<(const RomRecord &other) const
uint32_t index_witness
uint32_t record_witness
uint32_t value_column2_witness
bool operator==(const RomRecord &other) const noexcept
RomTranscript contains the RomRecords for a particular ROM table as well as the vector whose ith entr...
std::vector< std::array< uint32_t, 2 > > state
std::vector< RomRecord > records
bool operator==(const RomTranscript &other) const noexcept