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: Complete, auditors: [Sherlock], commit: e6694849223 }
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 {
30 TABLE_ENTRY, // row written by set_ROM_element / set_ROM_element_pair
31 READ, // row written by read_ROM_array / read_ROM_array_pair
32 };
33 uint32_t index_witness = 0; // Witness value of the index in the particular ROM block that contains this row.
36 uint32_t index = 0;
38 uint32_t record_witness = 0; // Record, a.k.a. "fingerprint" of the row.
39 size_t gate_index = 0; // Index in the memory block where the ROM gate will live.
40 bool operator<(const RomRecord& other) const
41 {
42 return index < other.index || (index == other.index && gate_index < other.gate_index);
43 }
44 bool operator==(const RomRecord& other) const noexcept
45 {
46 return index_witness == other.index_witness && value_column1_witness == other.value_column1_witness &&
47 value_column2_witness == other.value_column2_witness && index == other.index &&
48 access_type == other.access_type && record_witness == other.record_witness &&
49 gate_index == other.gate_index;
50 }
51};
52
62struct RamRecord {
66 };
67 uint32_t index_witness = 0;
68 uint32_t timestamp_witness = 0;
69 uint32_t value_witness = 0;
70 uint32_t index = 0;
71 uint32_t timestamp = 0;
73 uint32_t record_witness = 0; // Record, a.k.a. "fingerprint" of the row.
74 size_t gate_index = 0; // Index in the memory block where the RAM gate will live.
75 bool operator<(const RamRecord& other) const
76 {
77 bool index_test = (index) < (other.index);
78 return index_test || (index == other.index && timestamp < other.timestamp);
79 }
80 bool operator==(const RamRecord& other) const noexcept
81 {
82 return index_witness == other.index_witness && timestamp_witness == other.timestamp_witness &&
83 value_witness == other.value_witness && index == other.index && timestamp == other.timestamp &&
84 access_type == other.access_type && record_witness == other.record_witness &&
85 gate_index == other.gate_index;
86 }
87};
88
96 // Contains the value(s) of each index of the array. Note that each index/slot may contain _two_ values.
98 // A vector of records, each of which contains:
99 // + The constant witness with the index
100 // + The value in the memory slot
101 // + The actual index value
103 // Whether this ROM array uses the LogUp scheme (single-value tables) or the sorted-trace scheme
104 // (value-pair tables). Set on the first set/read call; once set, the array commits to that scheme.
105 bool use_logup = false;
106 // Used to check that the state hasn't changed in tests
107 bool operator==(const RomTranscript& other) const noexcept
108 {
109 return (state == other.state && records == other.records && use_logup == other.use_logup);
110 }
111};
112
118 // Contains the value of each index of the array
119 std::vector<uint32_t> state;
120 // A vector of records, each of which contains:
121 // + The constant witness with the index
122 // + The type of operation (READ or WRITE)
123 // + The _current_ value in the memory slot
124 // + The actual index value
126 // The number of times this RAM array has been touched (i.e., has had a READ or WRITE operation performed on it).
127 // used for RAM records, to compute the timestamp when performing a read/write. Note that the timestamp is _not_ a
128 // global timestamp; rather, it is a timestamp for the RAM array in question.
129 uint32_t access_count = 0;
130 // Used to check that the state hasn't changed in tests
131 bool operator==(const RamTranscript& other) const noexcept
132 {
133 return (state == other.state && records == other.records && access_count == other.access_count);
134 }
135};
136
140template <typename ExecutionTrace> class RomRamLogic_ {
141 public:
142 using FF = typename ExecutionTrace::FF;
144
145 // Storage
160
161 RomRamLogic_() = default;
162
163 // ROM operations
173 size_t create_ROM_array(const size_t array_size);
174
184 const size_t rom_id,
185 const size_t index_value,
186 const uint32_t value_witness);
196 const size_t rom_id,
197 const size_t index_value,
198 const std::array<uint32_t, 2>& value_witnesses);
210 uint32_t read_ROM_array(CircuitBuilder* builder, const size_t rom_id, const uint32_t index_witness);
218 std::array<uint32_t, 2> read_ROM_array_pair(CircuitBuilder* builder,
219 const size_t rom_id,
220 const uint32_t index_witness);
245 void process_ROM_array(CircuitBuilder* builder, const size_t rom_id);
246
247 // ---- ROM LogUp scheme (single-value tables only) ----
248
260 void create_ROM_logup_gate(CircuitBuilder* builder, RomRecord& record, const size_t rom_id, const bool is_read);
261
269 void process_ROM_logup_array(CircuitBuilder* builder, const size_t rom_id);
270
275
276 // RAM operations
286 size_t create_RAM_array(const size_t array_size);
296 const size_t ram_id,
297 const size_t index_value,
298 const uint32_t value_witness);
299 uint32_t read_RAM_array(CircuitBuilder* builder, const size_t ram_id, const uint32_t index_witness);
310 const size_t ram_id,
311 const uint32_t index_witness,
312 const uint32_t value_witness);
337 void create_final_sorted_RAM_gate(CircuitBuilder* builder, RamRecord& record, const size_t ram_array_size);
344 void process_RAM_array(CircuitBuilder* builder, const size_t ram_id);
346
347 bool operator==(const RomRamLogic_& other) const noexcept
348 {
349 return ram_arrays == other.ram_arrays && rom_arrays == other.rom_arrays;
350 }
351};
352
353} // 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 process_ROM_logup_array(CircuitBuilder *builder, const size_t rom_id)
Finalize a LogUp-style ROM array.
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 create_ROM_logup_gate(CircuitBuilder *builder, RomRecord &record, const size_t rom_id, const bool is_read)
Emit a ROM-LogUp gate (table entry or read access) at circuit construction time.
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
AccessType access_type
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