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 {
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
36 {
37 return index < other.index || (index == other.index && gate_index < other.gate_index);
38 }
39 bool operator==(const RomRecord& other) const noexcept
40 {
41 return index_witness == other.index_witness && value_column1_witness == other.value_column1_witness &&
42 value_column2_witness == other.value_column2_witness && index == other.index &&
43 record_witness == other.record_witness && gate_index == other.gate_index;
44 }
45};
46
56struct RamRecord {
60 };
61 uint32_t index_witness = 0;
62 uint32_t timestamp_witness = 0;
63 uint32_t value_witness = 0;
64 uint32_t index = 0;
65 uint32_t timestamp = 0;
67 uint32_t record_witness = 0; // Record, a.k.a. "fingerprint" of the row.
68 size_t gate_index = 0; // Index in the memory block where the RAM gate will live.
69 bool operator<(const RamRecord& other) const
70 {
71 bool index_test = (index) < (other.index);
72 return index_test || (index == other.index && timestamp < other.timestamp);
73 }
74 bool operator==(const RamRecord& other) const noexcept
75 {
76 return index_witness == other.index_witness && timestamp_witness == other.timestamp_witness &&
77 value_witness == other.value_witness && index == other.index && timestamp == other.timestamp &&
78 access_type == other.access_type && record_witness == other.record_witness &&
79 gate_index == other.gate_index;
80 }
81};
82
90 // Contains the value(s) of each index of the array. Note that each index/slot may contain _two_ values.
92 // A vector of records, each of which contains:
93 // + The constant witness with the index
94 // + The value in the memory slot
95 // + The actual index value
97 // Used to check that the state hasn't changed in tests
98 bool operator==(const RomTranscript& other) const noexcept
99 {
100 return (state == other.state && records == other.records);
101 }
102};
103
109 // Contains the value of each index of the array
110 std::vector<uint32_t> state;
111 // A vector of records, each of which contains:
112 // + The constant witness with the index
113 // + The type of operation (READ or WRITE)
114 // + The _current_ value in the memory slot
115 // + The actual index value
117 // The number of times this RAM array has been touched (i.e., has had a READ or WRITE operation performed on it).
118 // used for RAM records, to compute the timestamp when performing a read/write. Note that the timestamp is _not_ a
119 // global timestamp; rather, it is a timestamp for the RAM array in question.
120 size_t access_count = 0;
121 // Used to check that the state hasn't changed in tests
122 bool operator==(const RamTranscript& other) const noexcept
123 {
124 return (state == other.state && records == other.records && access_count == other.access_count);
125 }
126};
127
131template <typename ExecutionTrace> class RomRamLogic_ {
132 public:
133 using FF = typename ExecutionTrace::FF;
135
136 // Storage
151
152 RomRamLogic_() = default;
153
154 // ROM operations
164 size_t create_ROM_array(const size_t array_size);
165
175 const size_t rom_id,
176 const size_t index_value,
177 const uint32_t value_witness);
187 const size_t rom_id,
188 const size_t index_value,
189 const std::array<uint32_t, 2>& value_witnesses);
201 uint32_t read_ROM_array(CircuitBuilder* builder, const size_t rom_id, const uint32_t index_witness);
209 std::array<uint32_t, 2> read_ROM_array_pair(CircuitBuilder* builder,
210 const size_t rom_id,
211 const uint32_t index_witness);
236 void process_ROM_array(CircuitBuilder* builder, const size_t rom_id);
241
242 // RAM operations
252 size_t create_RAM_array(const size_t array_size);
262 const size_t ram_id,
263 const size_t index_value,
264 const uint32_t value_witness);
265 uint32_t read_RAM_array(CircuitBuilder* builder, const size_t ram_id, const uint32_t index_witness);
276 const size_t ram_id,
277 const uint32_t index_witness,
278 const uint32_t value_witness);
303 void create_final_sorted_RAM_gate(CircuitBuilder* builder, RamRecord& record, const size_t ram_array_size);
310 void process_RAM_array(CircuitBuilder* builder, const size_t ram_id);
312
313 bool operator==(const RomRamLogic_& other) const noexcept
314 {
315 return ram_arrays == other.ram_arrays && rom_arrays == other.rom_arrays;
316 }
317};
318
319} // 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