Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
databus_lookup_relation.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
7#pragma once
8#include <array>
9#include <tuple>
10
15
16namespace bb {
17
62template <typename FF_> class DatabusLookupRelationImpl {
63 public:
64 using FF = FF_;
65 static constexpr size_t NUM_BUS_COLUMNS = 3; // calldata, return data
66 // the actual degree of this subrelation is 3, and requires a degree adjustment of 1.
67 // however as we reuse the accumulators used to compute this subrelation for the lookup subrelation, we set the
68 // degree to 4 which removes the need of having degree adjustments for folding.
69 static constexpr size_t INVERSE_SUBREL_LENGTH = 5; // deg + 1 of inverse correctness subrelation
70 static constexpr size_t INVERSE_SUBREL_LENGTH_ADJUSTMENT = 0;
71 // the max degree of this subrelation is 4
72 static constexpr size_t LOOKUP_SUBREL_LENGTH = 5; // deg + 1 of log-deriv lookup subrelation
73 static constexpr size_t LOOKUP_SUBREL_LENGTH_ADJUSTMENT = 0;
74 static constexpr size_t READ_TAG_BOOLEAN_CHECK_SUBREL_LENGTH =
75 3; // deg + 1 of the relation checking that read_tag_m is a boolean value
77 static constexpr size_t NUM_SUB_RELATION_PER_IDX = 3; // the number of subrelations per bus column
78
79 static constexpr std::array<size_t, NUM_SUB_RELATION_PER_IDX * NUM_BUS_COLUMNS> SUBRELATION_PARTIAL_LENGTHS{
80 INVERSE_SUBREL_LENGTH, // inverse polynomial correctness subrelation (bus_idx 0)
81 LOOKUP_SUBREL_LENGTH, // log-derivative lookup argument subrelation (bus_idx 0)
82 READ_TAG_BOOLEAN_CHECK_SUBREL_LENGTH, // read_tag_m* read_tag_m - read_tag_m (bus_idx 0)
83 INVERSE_SUBREL_LENGTH, // inverse polynomial correctness subrelation (bus_idx 1)
84 LOOKUP_SUBREL_LENGTH, // log-derivative lookup argument subrelation (bus_idx 1)
85 READ_TAG_BOOLEAN_CHECK_SUBREL_LENGTH, // read_tag_m* read_tag_m - read_tag_m (bus_idx 1)
86 INVERSE_SUBREL_LENGTH, // inverse polynomial correctness subrelation (bus_idx 2)
87 LOOKUP_SUBREL_LENGTH, // log-derivative lookup argument subrelation (bus_idx 2)
88 READ_TAG_BOOLEAN_CHECK_SUBREL_LENGTH, // read_tag_m* read_tag_m - read_tag_m (bus_idx 2)
89 };
90
91 static constexpr bool INVERSE_SUBREL_LIN_INDEPENDENT = true; // to be satisfied independently at each row
92 static constexpr bool LOOKUP_SUBREL_LIN_INDEPENDENT = false; // to be satisfied as a sum across all rows
93 static constexpr bool READ_TAG_BOOLEAN_CHECK_LIN_INDEPENDENT = true; // to be satisfied independently at each row
94
95 // The lookup subrelations are "linearly dependent" in the sense that they establish the value of a sum across the
96 // entire execution trace rather than a per-row identity.
102
103 template <typename AllEntities> inline static bool skip([[maybe_unused]] const AllEntities& in)
104 {
105 // Ensure the input does not contain a read gate or data that is being read
106 return in.q_busread.is_zero() && in.calldata_read_counts.is_zero() &&
107 in.secondary_calldata_read_counts.is_zero() && in.return_data_read_counts.is_zero();
108 }
109
110 // Interface for easy access of databus components by column (bus_idx)
111 template <size_t bus_idx, typename AllEntities> struct BusData;
112
113 // Specialization for calldata (bus_idx = 0)
114 template <typename AllEntities> struct BusData</*bus_idx=*/0, AllEntities> {
115 static auto& values(const AllEntities& in) { return in.calldata; }
116 static auto& selector(const AllEntities& in) { return in.q_l; }
117 static auto& inverses(AllEntities& in) { return in.calldata_inverses; }
118 static auto& inverses(const AllEntities& in) { return in.calldata_inverses; } // const version
119 static auto& read_counts(const AllEntities& in) { return in.calldata_read_counts; }
120 static auto& read_tags(const AllEntities& in) { return in.calldata_read_tags; }
121 };
122
123 // Specialization for secondary_calldata (bus_idx = 1)
124 template <typename AllEntities> struct BusData</*bus_idx=*/1, AllEntities> {
125 static auto& values(const AllEntities& in) { return in.secondary_calldata; }
126 static auto& selector(const AllEntities& in) { return in.q_r; }
127 static auto& inverses(AllEntities& in) { return in.secondary_calldata_inverses; }
128 static auto& inverses(const AllEntities& in) { return in.secondary_calldata_inverses; } // const version
129 static auto& read_counts(const AllEntities& in) { return in.secondary_calldata_read_counts; }
130 static auto& read_tags(const AllEntities& in) { return in.secondary_calldata_read_tags; }
131 };
132
133 // Specialization for return data (bus_idx = 2)
134 template <typename AllEntities> struct BusData</*bus_idx=*/2, AllEntities> {
135 static auto& values(const AllEntities& in) { return in.return_data; }
136 static auto& selector(const AllEntities& in) { return in.q_o; }
137 static auto& inverses(AllEntities& in) { return in.return_data_inverses; }
138 static auto& inverses(const AllEntities& in) { return in.return_data_inverses; } // const version
139 static auto& read_counts(const AllEntities& in) { return in.return_data_read_counts; }
140 static auto& read_tags(const AllEntities& in) { return in.return_data_read_tags; }
141 };
142
151 template <size_t bus_idx, typename AllValues> static bool operation_exists_at_row(const AllValues& row)
152 {
153 auto read_selector = get_read_selector<FF, bus_idx>(row);
154 auto read_tag = BusData<bus_idx, AllValues>::read_tags(row);
155 return (read_selector == 1 || read_tag == 1);
156 }
157
167 template <typename Accumulator, size_t bus_idx, typename AllEntities>
168 static Accumulator compute_inverse_exists(const AllEntities& in)
169 {
170 using CoefficientAccumulator = typename Accumulator::CoefficientAccumulator;
171
172 const auto is_read_gate = get_read_selector<Accumulator, bus_idx>(in); // is this a read gate
173 const auto read_tag_m =
174 CoefficientAccumulator(BusData<bus_idx, AllEntities>::read_tags(in)); // does row contain data being read
175 const Accumulator read_tag(read_tag_m);
176 // Relation checking: is_read_gate == 1 || read_tag == 1
177 // Important note: the relation written below assumes that is_read_gate and read_tag are boolean values, which
178 // is guaranteed by the boolean_check subrelation. If not, fixing one of the two, the return value is a linear
179 // function in the other variable and can be set to an arbitrary value independent of the fixed value. See the
180 // boolean_check subrelation for more explanation.
181 // degree 2(2) 1 2 (2) 1 // Degree 3 (3)
182 return is_read_gate + read_tag - (is_read_gate * read_tag); // Degree 3 (5)
183 }
184
190 template <typename Accumulator, size_t bus_idx, typename AllEntities>
191 static Accumulator get_read_selector(const AllEntities& in)
192 {
193 using CoefficientAccumulator = typename Accumulator::CoefficientAccumulator;
194
195 auto q_busread = CoefficientAccumulator(in.q_busread);
196 auto column_selector = CoefficientAccumulator(BusData<bus_idx, AllEntities>::selector(in));
197
198 // degree 1 1 2 (2)
199 return Accumulator(q_busread * column_selector);
200 }
201
206 template <typename Accumulator, size_t bus_idx, typename AllEntities, typename Parameters>
207 static Accumulator compute_write_term(const AllEntities& in, const Parameters& params)
208 {
209 using CoefficientAccumulator = typename Accumulator::CoefficientAccumulator;
210 using ParameterCoefficientAccumulator = typename Parameters::DataType::CoefficientAccumulator;
211
212 const auto& id = CoefficientAccumulator(in.databus_id);
213 const auto& value = CoefficientAccumulator(BusData<bus_idx, AllEntities>::values(in));
214 const auto& gamma = ParameterCoefficientAccumulator(params.gamma);
215 const auto& beta = ParameterCoefficientAccumulator(params.beta);
216
217 // Construct value_i + idx_i*\beta + \gamma
218 // degrees 1(0) 0(1) 1(1) 0(1)
219 return Accumulator(id * beta + value + gamma); // degree 1 (1)
220 }
221
227 template <typename Accumulator, typename AllEntities, typename Parameters>
228 static Accumulator compute_read_term(const AllEntities& in, const Parameters& params)
229 {
230 using CoefficientAccumulator = typename Accumulator::CoefficientAccumulator;
231 using ParameterCoefficientAccumulator = typename Parameters::DataType::CoefficientAccumulator;
232
233 // Bus value stored in w_1, index into bus column stored in w_2
234 const auto& w_1 = CoefficientAccumulator(in.w_l);
235 const auto& w_2 = CoefficientAccumulator(in.w_r);
236 const auto& gamma = ParameterCoefficientAccumulator(params.gamma);
237 const auto& beta = ParameterCoefficientAccumulator(params.beta);
238
239 // Construct value + index*\beta + \gamma
240 return Accumulator((w_2 * beta) + w_1 + gamma); // degree 1 (2)
241 }
242
251 template <size_t bus_idx, typename Polynomials>
252 static void compute_logderivative_inverse(Polynomials& polynomials,
253 auto& relation_parameters,
254 const size_t circuit_size)
255 {
256 BB_BENCH_NAME("Databus::compute_logderivative_inverse");
257 auto& inverse_polynomial = BusData<bus_idx, Polynomials>::inverses(polynomials);
258
259 size_t min_iterations_per_thread = 1 << 6; // min number of iterations for which we'll spin up a unique thread
260 size_t num_threads = bb::calculate_num_threads_pow2(circuit_size, min_iterations_per_thread);
261 size_t iterations_per_thread = circuit_size / num_threads; // actual iterations per thread
262
263 parallel_for(num_threads, [&](size_t thread_idx) {
264 size_t start = thread_idx * iterations_per_thread;
265 size_t end = (thread_idx + 1) * iterations_per_thread;
266 bool is_read = false;
267 bool nonzero_read_count = false;
268 for (size_t i = start; i < end; ++i) {
269 // Determine if the present row contains a databus operation
270 auto q_busread = polynomials.q_busread[i];
271 if constexpr (bus_idx == 0) { // calldata
272 is_read = q_busread == 1 && polynomials.q_l[i] == 1;
273 nonzero_read_count = polynomials.calldata_read_counts[i] > 0;
274 }
275 if constexpr (bus_idx == 1) { // secondary_calldata
276 is_read = q_busread == 1 && polynomials.q_r[i] == 1;
277 nonzero_read_count = polynomials.secondary_calldata_read_counts[i] > 0;
278 }
279 if constexpr (bus_idx == 2) { // return data
280 is_read = q_busread == 1 && polynomials.q_o[i] == 1;
281 nonzero_read_count = polynomials.return_data_read_counts[i] > 0;
282 }
283 // We only compute the inverse if this row contains a read gate or data that has been read
284 if (is_read || nonzero_read_count) {
285 // TODO(https://github.com/AztecProtocol/barretenberg/issues/940): avoid get_row if possible.
286 auto row = polynomials.get_row(i); // Note: this is a copy. use sparingly!
287 auto value = compute_read_term<FF>(row, relation_parameters) *
288 compute_write_term<FF, bus_idx>(row, relation_parameters);
289 inverse_polynomial.at(i) = value;
290 }
291 }
292 });
293
294 // Compute inverse polynomial I in place by inverting the product at each row
295 // Note: zeroes are ignored as they are not used anyway
296 FF::batch_invert(inverse_polynomial.coeffs());
297 };
298
309 template <typename FF,
310 size_t bus_idx,
311 typename ContainerOverSubrelations,
312 typename AllEntities,
313 typename Parameters>
314 static void accumulate_subrelation_contributions(ContainerOverSubrelations& accumulator,
315 const AllEntities& in,
316 const Parameters& params,
317 const FF& scaling_factor)
318 {
319 using Accumulator = typename std::tuple_element_t<4, ContainerOverSubrelations>;
320 using CoefficientAccumulator = typename Accumulator::CoefficientAccumulator;
322 const auto inverses_m = CoefficientAccumulator(BusData<bus_idx, AllEntities>::inverses(in)); // Degree 1
323 Accumulator inverses(inverses_m);
324 const auto read_counts_m = CoefficientAccumulator(BusData<bus_idx, AllEntities>::read_counts(in)); // Degree 1
325 const auto read_term = compute_read_term<Accumulator>(in, params); // Degree 1
326 const auto write_term = compute_write_term<Accumulator, bus_idx>(in, params); // Degree 1
327 const auto inverse_exists = compute_inverse_exists<Accumulator, bus_idx>(in); // Degree 3
328 const auto read_selector = get_read_selector<Accumulator, bus_idx>(in); // Degree 2
329
330 // Determine which pair of subrelations to update based on which bus column is being read
331 // The inverse relation subrelation index
332 constexpr size_t subrel_idx_1 = NUM_SUB_RELATION_PER_IDX * bus_idx;
333 // The lookup relation subrelation index
334 constexpr size_t subrel_idx_2 = NUM_SUB_RELATION_PER_IDX * bus_idx + 1;
335 // The read_tag boolean check subrelation index
336 constexpr size_t subrel_idx_3 = NUM_SUB_RELATION_PER_IDX * bus_idx + 2;
337
338 // Establish the correctness of the polynomial of inverses I. Note: inverses is computed so that the value
339 // is 0 if !inverse_exists. Degree 3
340 // degrees 3 = 1 1 1 3
341 std::get<subrel_idx_1>(accumulator) += (read_term * write_term * inverses - inverse_exists) * scaling_factor;
342
343 // Establish validity of the read. Note: no scaling factor here since this constraint is enforced across the
344 // entire trace, not on a per-row basis.
345
346 // degree 3 = 2 1
347 Accumulator tmp = read_selector * write_term;
348 // degree 2 = 1 1
349 tmp -= Accumulator(read_counts_m) * read_term;
350 // degree 1
351 tmp *= inverses;
352 std::get<subrel_idx_2>(accumulator) += tmp; // Deg 4 (4)
353
354 const auto read_tag_m = CoefficientAccumulator(BusData<bus_idx, AllEntities>::read_tags(in));
355 const auto read_tag = ShortAccumulator(read_tag_m);
356 // // this is done by row so we have to multiply by the scaling factor
357 // degree 1 1 1 = 2
358 std::get<subrel_idx_3>(accumulator) += (read_tag * read_tag - read_tag) * scaling_factor;
359 }
360
368 template <typename ContainerOverSubrelations, typename AllEntities, typename Parameters>
369 static void accumulate(ContainerOverSubrelations& accumulator,
370 const AllEntities& in,
371 const Parameters& params,
372 const FF& scaling_factor)
373 {
374 // Accumulate the subrelation contributions for each column of the databus
375 bb::constexpr_for<0, NUM_BUS_COLUMNS, 1>([&]<size_t bus_idx>() {
376 accumulate_subrelation_contributions<FF, bus_idx>(accumulator, in, params, scaling_factor);
377 });
378 }
379};
380
382
383} // namespace bb
#define BB_BENCH_NAME(name)
Definition bb_bench.hpp:219
Log-derivative lookup argument relation for establishing DataBus reads.
static constexpr bool READ_TAG_BOOLEAN_CHECK_LIN_INDEPENDENT
static bool operation_exists_at_row(const AllValues &row)
Determine whether the inverse I needs to be computed at a given row for a given bus column.
static Accumulator compute_read_term(const AllEntities &in, const Parameters &params)
Compute read term denominator in log derivative lookup argument.
static constexpr size_t NUM_SUB_RELATION_PER_IDX
static bool skip(const AllEntities &in)
static constexpr bool LOOKUP_SUBREL_LIN_INDEPENDENT
static constexpr size_t LOOKUP_SUBREL_LENGTH_ADJUSTMENT
static void accumulate_subrelation_contributions(ContainerOverSubrelations &accumulator, const AllEntities &in, const Parameters &params, const FF &scaling_factor)
Accumulate the subrelation contributions for reads from a single databus column.
static constexpr std::array< size_t, NUM_SUB_RELATION_PER_IDX *NUM_BUS_COLUMNS > SUBRELATION_PARTIAL_LENGTHS
static void compute_logderivative_inverse(Polynomials &polynomials, auto &relation_parameters, const size_t circuit_size)
Construct the polynomial I whose components are the inverse of the product of the read and write term...
static constexpr size_t LOOKUP_SUBREL_LENGTH
static Accumulator compute_write_term(const AllEntities &in, const Parameters &params)
Compute write term denominator in log derivative lookup argument.
static Accumulator compute_inverse_exists(const AllEntities &in)
Compute the Accumulator whose values indicate whether the inverse is computed or not.
static constexpr std::array< bool, NUM_SUB_RELATION_PER_IDX *NUM_BUS_COLUMNS > SUBRELATION_LINEARLY_INDEPENDENT
static constexpr bool INVERSE_SUBREL_LIN_INDEPENDENT
static constexpr size_t READ_TAG_BOOLEAN_CHECK_SUBREL_LENGTH
static constexpr size_t INVERSE_SUBREL_LENGTH
static constexpr size_t READ_TAG_BOOLEAN_CHECK_SUBREL_LENGTH_ADJUSTMENT
static constexpr size_t INVERSE_SUBREL_LENGTH_ADJUSTMENT
static Accumulator get_read_selector(const AllEntities &in)
Compute scalar for read term in log derivative lookup argument.
static void accumulate(ContainerOverSubrelations &accumulator, const AllEntities &in, const Parameters &params, const FF &scaling_factor)
Accumulate the log derivative databus lookup argument subrelation contributions for each databus colu...
A wrapper for Relations to expose methods used by the Sumcheck prover or verifier to add the contribu...
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
size_t calculate_num_threads_pow2(size_t num_iterations, size_t min_iterations_per_thread)
calculates number of threads to create based on minimum iterations per thread, guaranteed power of 2
Definition thread.cpp:254
void parallel_for(size_t num_iterations, const std::function< void(size_t)> &func)
Definition thread.cpp:111
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
static void batch_invert(C &coeffs) noexcept