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#pragma once
2
4#include <array>
5#include <cstdint>
6#include <vector>
7
8namespace bb {
9// Forward declaration
10template <typename ExecutionTrace_> class UltraCircuitBuilder_;
11
12// Constants
13static constexpr uint32_t UNINITIALIZED_MEMORY_RECORD = UINT32_MAX;
14
22struct RomRecord {
23 uint32_t index_witness = 0; // Witness value of the index in the particular ROM block that contains this row.
26 uint32_t index = 0;
27 uint32_t record_witness = 0; // Record, a.k.a. "fingerprint" of the row.
28 size_t gate_index = 0; // Index in the memory block where the ROM gate will live.
29 bool operator<(const RomRecord& other) const { return index < other.index; }
30 bool operator==(const RomRecord& other) const noexcept
31 {
32 return index_witness == other.index_witness && value_column1_witness == other.value_column1_witness &&
33 value_column2_witness == other.value_column2_witness && index == other.index &&
34 record_witness == other.record_witness && gate_index == other.gate_index;
35 }
36};
37
47struct RamRecord {
51 };
52 uint32_t index_witness = 0;
53 uint32_t timestamp_witness = 0;
54 uint32_t value_witness = 0;
55 uint32_t index = 0;
56 uint32_t timestamp = 0;
58 uint32_t record_witness = 0; // Record, a.k.a. "fingerprint" of the row.
59 size_t gate_index = 0; // Index in the memory block where the RAM gate will live.
60 bool operator<(const RamRecord& other) const
61 {
62 bool index_test = (index) < (other.index);
63 return index_test || (index == other.index && timestamp < other.timestamp);
64 }
65 bool operator==(const RamRecord& other) const noexcept
66 {
67 return index_witness == other.index_witness && timestamp_witness == other.timestamp_witness &&
68 value_witness == other.value_witness && index == other.index && timestamp == other.timestamp &&
69 access_type == other.access_type && record_witness == other.record_witness &&
70 gate_index == other.gate_index;
71 }
72};
73
81 // Contains the value(s) of each index of the array. Note that each index/slot may contain _two_ values.
83 // A vector of records, each of which contains:
84 // + The constant witness with the index
85 // + The value in the memory slot
86 // + The actual index value
88 // Used to check that the state hasn't changed in tests
89 bool operator==(const RomTranscript& other) const noexcept
90 {
91 return (state == other.state && records == other.records);
92 }
93};
94
100 // Contains the value of each index of the array
101 std::vector<uint32_t> state;
102 // A vector of records, each of which contains:
103 // + The constant witness with the index
104 // + The type of operation (READ or WRITE)
105 // + The _current_ value in the memory slot
106 // + The actual index value
108 // The number of times this RAM array has been touched (i.e., has had a READ or WRITE operation performed on it).
109 // used for RAM records, to compute the timestamp when performing a read/write. Note that the timestamp is _not_ a
110 // global timestamp; rather, it is a timestamp for the RAM array in question.
111 size_t access_count = 0;
112 // Used to check that the state hasn't changed in tests
113 bool operator==(const RamTranscript& other) const noexcept
114 {
115 return (state == other.state && records == other.records && access_count == other.access_count);
116 }
117};
118
122template <typename ExecutionTrace> class RomRamLogic_ {
123 public:
124 using FF = typename ExecutionTrace::FF;
126
127 // Storage
142
143 RomRamLogic_() = default;
144
145 // ROM operations
155 size_t create_ROM_array(const size_t array_size);
156
166 const size_t rom_id,
167 const size_t index_value,
168 const uint32_t value_witness);
178 const size_t rom_id,
179 const size_t index_value,
180 const std::array<uint32_t, 2>& value_witnesses);
192 uint32_t read_ROM_array(CircuitBuilder* builder, const size_t rom_id, const uint32_t index_witness);
200 std::array<uint32_t, 2> read_ROM_array_pair(CircuitBuilder* builder,
201 const size_t rom_id,
202 const uint32_t index_witness);
227 void process_ROM_array(CircuitBuilder* builder, const size_t rom_id);
232
233 // RAM operations
243 size_t create_RAM_array(const size_t array_size);
253 const size_t ram_id,
254 const size_t index_value,
255 const uint32_t value_witness);
256 uint32_t read_RAM_array(CircuitBuilder* builder, const size_t ram_id, const uint32_t index_witness);
267 const size_t ram_id,
268 const uint32_t index_witness,
269 const uint32_t value_witness);
294 void create_final_sorted_RAM_gate(CircuitBuilder* builder, RamRecord& record, const size_t ram_array_size);
301 void process_RAM_array(CircuitBuilder* builder, const size_t ram_id);
303
304 bool operator==(const RomRamLogic_& other) const noexcept
305 {
306 return ram_arrays == other.ram_arrays && rom_arrays == other.rom_arrays;
307 }
308};
309
310} // 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