Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
ultra_circuit_checker.hpp
Go to the documentation of this file.
1#pragma once
18
19#include <optional>
20
21namespace bb {
22
24 public:
25 using FF = bb::fr;
39
52 template <typename Builder> static bool check(const Builder& builder_in);
53
59 template <typename Relation, typename Builder, typename Block>
60 static bool check_relation_at_row(Builder& builder, Block& block, size_t row_idx);
61
62 private:
63 struct TagCheckData; // Container for data pertaining to generalized permutation tag check
64 struct MemoryCheckData; // Container for data pertaining to RAM/RAM record check
65 using Key = std::array<FF, 4>; // Key type for lookup table hash table
66 struct HashFunction; // Custom hash function for lookup table hash table
68
72 template <typename Builder> static Builder prepare_circuit(const Builder& builder_in);
73
84 template <typename Builder>
85 static bool check_block(Builder& builder,
86 auto& block,
87 TagCheckData& tag_data,
88 MemoryCheckData& memory_data,
89 LookupHashTable& lookup_hash_table);
90
91#ifdef ULTRA_FUZZ
92 template <typename Builder> static bool relaxed_check_memory_relation(Builder& builder);
93 template <typename Builder> static bool relaxed_check_delta_range_relation(Builder& builder);
94#endif
95
105 template <typename Relation> static bool check_relation(auto& values, auto& params);
106
107 // Variant of `check_relation` for MemoryRelation. Per-row linearly-independent subrelations are checked
108 // immediately; linearly-dependent subrelation contributions (the ROM-LogUp sum identity) are summed into
109 // `memory_data.rom_logup_sum` for a final cross-row check.
110 template <typename Memory>
111 static bool check_memory_relation_with_logup(auto& values, auto& params, MemoryCheckData& memory_data);
112
119 static bool check_lookup(auto& values, auto& lookup_hash_table);
120
127 template <typename Builder> static bool check_databus_read(auto& values, Builder& builder);
128
135 static bool check_tag_data(const TagCheckData& tag_data);
136
145 template <typename Builder> static auto init_empty_values();
146
158 template <typename Builder>
159 static void populate_values(
160 Builder& builder, auto& block, auto& values, TagCheckData& tag_data, MemoryCheckData& memory_data, size_t idx);
161
166 FF left_product = FF::one(); // product of (value + γ ⋅ tag)
167 FF right_product = FF::one(); // product of (value + γ ⋅ tau[tag])
168 const FF gamma = FF::random_element(); // randomness for the tag check
169
170 // We need to include each variable only once
171 std::unordered_set<size_t> encountered_variables;
172 };
173
178 // randomness for constructing wire 4 mem records (eta powers and rom logup gamma)
180 FF eta_two = eta * eta; // eta²
181 FF eta_three = eta_two * eta; // eta³
183
184 std::unordered_set<size_t> read_record_gates; // row indices for gates containing RAM/ROM read mem record
185 std::unordered_set<size_t> write_record_gates; // row indices for gates containing RAM/ROM write mem record
186 std::unordered_set<size_t> rom_logup_gates; // row indices for ROM-LogUp table-entry or read-access gates
187 // Accumulator for MemoryRelation's linearly-dependent subrelation 7 (ROM-LogUp sum identity). Summed
188 // across all rows by check_block; checked == 0 at the end of check_circuit.
190 // Construct hash tables for memory read/write indices to efficiently determine if row is a memory record
192 {
193 for (const auto& gate_idx : builder.memory_read_records) {
194 read_record_gates.insert(gate_idx);
195 }
196 for (const auto& gate_idx : builder.memory_write_records) {
197 write_record_gates.insert(gate_idx);
198 }
199 for (const auto& gate_idx : builder.rom_logup_records) {
200 rom_logup_gates.insert(gate_idx);
201 }
202 }
203 };
204
205 // Hash for lookups hash table for efficiently checking if lookups are present in set of tables used by circuit
207 const FF mult_const = FF(uint256_t(0x1337, 0x1336, 0x1335, 0x1334));
210
211 size_t operator()(const Key& entry) const
212 {
213 FF result = entry[0] + mult_const * entry[1] + mc_sqr * entry[2] + mc_cube * entry[3];
214 return static_cast<size_t>(result.reduce_once().data[0]);
215 }
216 };
217};
218
219} // namespace bb
A wrapper for Relations to expose methods used by the Sumcheck prover or verifier to add the contribu...
std::unordered_set< Key, HashFunction > LookupHashTable
static bool check_relation_at_row(Builder &builder, Block &block, size_t row_idx)
Evaluate a single Relation at block's row row_idx in isolation, returning true iff every subrelation ...
static bool check_memory_relation_with_logup(auto &values, auto &params, MemoryCheckData &memory_data)
static bool check_databus_read(auto &values, Builder &builder)
Check that the {index, value} pair contained in a databus read gate reflects the actual value present...
static bool check_relation(auto &values, auto &params)
Check that a given relation is satisfied for the provided inputs corresponding to a single row.
static bool check_tag_data(const TagCheckData &tag_data)
Check whether the left and right running tag products are equal.
static bool check_lookup(auto &values, auto &lookup_hash_table)
Check whether the values in a lookup gate are contained within a corresponding hash table.
static auto init_empty_values()
Helper for initializing an empty AllValues container of the right Flavor based on Builder.
static void populate_values(Builder &builder, auto &block, auto &values, TagCheckData &tag_data, MemoryCheckData &memory_data, size_t idx)
Populate the values required to check the correctness of a single "row" of the circuit.
static bool check(const Builder &builder_in)
Check the correctness of a circuit witness.
static Builder prepare_circuit(const Builder &builder_in)
Copy the builder and finalize it before checking its validity.
static bool check_block(Builder &builder, auto &block, TagCheckData &tag_data, MemoryCheckData &memory_data, LookupHashTable &lookup_hash_table)
Checks that the provided witness satisfies all gates contained in a single execution trace block.
AluTraceBuilder builder
Definition alu.test.cpp:124
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
field< Bn254FrParams > fr
Definition fr.hpp:155
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
size_t operator()(const Key &entry) const
Struct for managing memory record data for ensuring RAM/ROM correctness.
Struct for managing the running tag product data for ensuring tag correctness.
std::unordered_set< size_t > encountered_variables
static constexpr field one()
static field random_element(numeric::RNG *engine=nullptr) noexcept
BB_INLINE constexpr field sqr() const noexcept
VectorField result