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
18struct RomRecord {
19 uint32_t index_witness = 0;
22 uint32_t index = 0;
23 uint32_t record_witness = 0;
24 size_t gate_index = 0;
25 bool operator<(const RomRecord& other) const { return index < other.index; }
26 bool operator==(const RomRecord& other) const noexcept
27 {
28 return index_witness == other.index_witness && value_column1_witness == other.value_column1_witness &&
29 value_column2_witness == other.value_column2_witness && index == other.index &&
30 record_witness == other.record_witness && gate_index == other.gate_index;
31 }
32};
33
37struct RamRecord {
41 };
42 uint32_t index_witness = 0;
43 uint32_t timestamp_witness = 0;
44 uint32_t value_witness = 0;
45 uint32_t index = 0;
46 uint32_t timestamp = 0;
48 uint32_t record_witness = 0;
49 size_t gate_index = 0;
50 bool operator<(const RamRecord& other) const
51 {
52 bool index_test = (index) < (other.index);
53 return index_test || (index == other.index && timestamp < other.timestamp);
54 }
55 bool operator==(const RamRecord& other) const noexcept
56 {
57 return index_witness == other.index_witness && timestamp_witness == other.timestamp_witness &&
58 value_witness == other.value_witness && index == other.index && timestamp == other.timestamp &&
59 access_type == other.access_type && record_witness == other.record_witness &&
60 gate_index == other.gate_index;
61 }
62};
63
69 // Contains the value of each index of the array
71 // A vector of records, each of which contains:
72 // + The constant witness with the index
73 // + The value in the memory slot
74 // + The actual index value
76 // Used to check that the state hasn't changed in tests
77 bool operator==(const RomTranscript& other) const noexcept
78 {
79 return (state == other.state && records == other.records);
80 }
81};
82
88 // Contains the value of each index of the array
89 std::vector<uint32_t> state;
90 // A vector of records, each of which contains:
91 // + The constant witness with the index
92 // + The value in the memory slot
93 // + The actual index value
95 // used for RAM records, to compute the timestamp when performing a read/write
96 size_t access_count = 0;
97 // Used to check that the state hasn't changed in tests
98 bool operator==(const RamTranscript& other) const noexcept
99 {
100 return (state == other.state && records == other.records && access_count == other.access_count);
101 }
102};
103
107template <typename ExecutionTrace> class RomRamLogic_ {
108 public:
109 using FF = typename ExecutionTrace::FF;
111
112 // Storage
128
129 RomRamLogic_() = default;
130
131 // ROM operations
141 size_t create_ROM_array(const size_t array_size);
142
152 const size_t rom_id,
153 const size_t index_value,
154 const uint32_t value_witness);
164 const size_t rom_id,
165 const size_t index_value,
166 const std::array<uint32_t, 2>& value_witnesses);
175 uint32_t read_ROM_array(CircuitBuilder* builder, const size_t rom_id, const uint32_t index_witness);
183 std::array<uint32_t, 2> read_ROM_array_pair(CircuitBuilder* builder,
184 const size_t rom_id,
185 const uint32_t index_witness);
210 void process_ROM_array(CircuitBuilder* builder, const size_t rom_id);
215
216 // RAM operations
226 size_t create_RAM_array(const size_t array_size);
236 const size_t ram_id,
237 const size_t index_value,
238 const uint32_t value_witness);
239 uint32_t read_RAM_array(CircuitBuilder* builder, const size_t ram_id, const uint32_t index_witness);
241 const size_t ram_id,
242 const uint32_t index_witness,
243 const uint32_t value_witness);
268 void create_final_sorted_RAM_gate(CircuitBuilder* builder, RamRecord& record, const size_t ram_array_size);
275 void process_RAM_array(CircuitBuilder* builder, const size_t ram_id);
277
278 bool operator==(const RomRamLogic_& other) const noexcept
279 {
280 return ram_arrays == other.ram_arrays && rom_arrays == other.rom_arrays;
281 }
282};
283
284} // 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 ram_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)
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:123
Entry point for Barretenberg command-line interface.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
A RAM memory record that can be ordered.
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
Each ram array is an instance of memory transcript. It saves values and indexes for a particular memo...
std::vector< RamRecord > records
bool operator==(const RamTranscript &other) const noexcept
std::vector< uint32_t > state
A ROM memory record that can be ordered.
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
Each rom array is an instance of memory transcript. It saves values and indexes for a particular memo...
std::vector< std::array< uint32_t, 2 > > state
std::vector< RomRecord > records
bool operator==(const RomTranscript &other) const noexcept