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
52template <typename FF_> class DatabusLookupRelationImpl {
53 public:
54 using FF = FF_;
55 static constexpr size_t NUM_BUS_COLUMNS = 3; // calldata, return data
56 // the actual degree of this subrelation is 3, and requires a degree adjustment of 1.
57 // however as we reuse the accumulators used to compute this subrelation for the lookup subrelation, we set the
58 // degree to 4 which removes the need of having degree adjustments for folding.
59 static constexpr size_t INVERSE_SUBREL_LENGTH = 5; // deg + 1 of inverse correctness subrelation
60 static constexpr size_t INVERSE_SUBREL_LENGTH_ADJUSTMENT = 0;
61 // the max degree of this subrelation is 4 in both the sumcheck and protogalaxy contexts because in the latter case
62 // databus_id is always degree 0 when formed via interpolation across two instances
63 static constexpr size_t LOOKUP_SUBREL_LENGTH = 5; // deg + 1 of log-deriv lookup subrelation
64 static constexpr size_t LOOKUP_SUBREL_LENGTH_ADJUSTMENT = 0;
65 static constexpr size_t READ_TAG_BOOLEAN_CHECK_SUBREL_LENGTH =
66 3; // deg + 1 of the relation checking that read_tag_m is a boolean value
68 static constexpr size_t NUM_SUB_RELATION_PER_IDX = 3; // the number of subrelations per bus column
69
70 static constexpr std::array<size_t, NUM_SUB_RELATION_PER_IDX * NUM_BUS_COLUMNS> SUBRELATION_PARTIAL_LENGTHS{
71 INVERSE_SUBREL_LENGTH, // inverse polynomial correctness subrelation (bus_idx 0)
72 LOOKUP_SUBREL_LENGTH, // log-derivative lookup argument subrelation (bus_idx 0)
73 READ_TAG_BOOLEAN_CHECK_SUBREL_LENGTH, // read_tag_m* read_tag_m - read_tag_m (bus_idx 0)
74 INVERSE_SUBREL_LENGTH, // inverse polynomial correctness subrelation (bus_idx 1)
75 LOOKUP_SUBREL_LENGTH, // log-derivative lookup argument subrelation (bus_idx 1)
76 READ_TAG_BOOLEAN_CHECK_SUBREL_LENGTH, // read_tag_m* read_tag_m - read_tag_m (bus_idx 1)
77 INVERSE_SUBREL_LENGTH, // inverse polynomial correctness subrelation (bus_idx 2)
78 LOOKUP_SUBREL_LENGTH, // log-derivative lookup argument subrelation (bus_idx 2)
79 READ_TAG_BOOLEAN_CHECK_SUBREL_LENGTH, // read_tag_m* read_tag_m - read_tag_m (bus_idx 2)
80 };
81
93
94 static constexpr bool INVERSE_SUBREL_LIN_INDEPENDENT = true; // to be satisfied independently at each row
95 static constexpr bool LOOKUP_SUBREL_LIN_INDEPENDENT = false; // to be satisfied as a sum across all rows
96 static constexpr bool READ_TAG_BOOLEAN_CHECK_LIN_INDEPENDENT = true; // to be satisfied independently at each row
97
98 // The lookup subrelations are "linearly dependent" in the sense that they establish the value of a sum across the
99 // entire execution trace rather than a per-row identity.
105
106 template <typename AllEntities> inline static bool skip([[maybe_unused]] const AllEntities& in)
107 {
108 // Ensure the input does not contain a read gate or data that is being read
109 return in.q_busread.is_zero() && in.calldata_read_counts.is_zero() &&
110 in.secondary_calldata_read_counts.is_zero() && in.return_data_read_counts.is_zero();
111 }
112
113 // Interface for easy access of databus components by column (bus_idx)
114 template <size_t bus_idx, typename AllEntities> struct BusData;
115
116 // Specialization for calldata (bus_idx = 0)
117 template <typename AllEntities> struct BusData</*bus_idx=*/0, AllEntities> {
118 static auto& values(const AllEntities& in) { return in.calldata; }
119 static auto& selector(const AllEntities& in) { return in.q_l; }
120 static auto& inverses(AllEntities& in) { return in.calldata_inverses; }
121 static auto& inverses(const AllEntities& in) { return in.calldata_inverses; } // const version
122 static auto& read_counts(const AllEntities& in) { return in.calldata_read_counts; }
123 static auto& read_tags(const AllEntities& in) { return in.calldata_read_tags; }
124 };
125
126 // Specialization for secondary_calldata (bus_idx = 1)
127 template <typename AllEntities> struct BusData</*bus_idx=*/1, AllEntities> {
128 static auto& values(const AllEntities& in) { return in.secondary_calldata; }
129 static auto& selector(const AllEntities& in) { return in.q_r; }
130 static auto& inverses(AllEntities& in) { return in.secondary_calldata_inverses; }
131 static auto& inverses(const AllEntities& in) { return in.secondary_calldata_inverses; } // const version
132 static auto& read_counts(const AllEntities& in) { return in.secondary_calldata_read_counts; }
133 static auto& read_tags(const AllEntities& in) { return in.secondary_calldata_read_tags; }
134 };
135
136 // Specialization for return data (bus_idx = 2)
137 template <typename AllEntities> struct BusData</*bus_idx=*/2, AllEntities> {
138 static auto& values(const AllEntities& in) { return in.return_data; }
139 static auto& selector(const AllEntities& in) { return in.q_o; }
140 static auto& inverses(AllEntities& in) { return in.return_data_inverses; }
141 static auto& inverses(const AllEntities& in) { return in.return_data_inverses; } // const version
142 static auto& read_counts(const AllEntities& in) { return in.return_data_read_counts; }
143 static auto& read_tags(const AllEntities& in) { return in.return_data_read_tags; }
144 };
145
154 template <size_t bus_idx, typename AllValues> static bool operation_exists_at_row(const AllValues& row)
155 {
156 auto read_selector = get_read_selector<FF, bus_idx>(row);
157 auto read_tag = BusData<bus_idx, AllValues>::read_tags(row);
158 return (read_selector == 1 || read_tag == 1);
159 }
160
168 template <typename Accumulator, size_t bus_idx, typename AllEntities>
169 static Accumulator compute_inverse_exists(const AllEntities& in)
170 {
171 using CoefficientAccumulator = typename Accumulator::CoefficientAccumulator;
172
173 const auto is_read_gate = get_read_selector<Accumulator, bus_idx>(in); // is this a read gate
174 const auto read_tag_m =
175 CoefficientAccumulator(BusData<bus_idx, AllEntities>::read_tags(in)); // does row contain data being read
176 const Accumulator read_tag(read_tag_m);
177 // degree 2(2) 1 2 (2) 1 // Degree 3 (3)
178 return is_read_gate + read_tag - (is_read_gate * read_tag); // Degree 3 (5)
179 }
180
186 template <typename Accumulator, size_t bus_idx, typename AllEntities>
187 static Accumulator get_read_selector(const AllEntities& in)
188 {
189 using CoefficientAccumulator = typename Accumulator::CoefficientAccumulator;
190
191 auto q_busread = CoefficientAccumulator(in.q_busread);
192 auto column_selector = CoefficientAccumulator(BusData<bus_idx, AllEntities>::selector(in));
193
194 // degree 1 1 2 (2)
195 return Accumulator(q_busread * column_selector);
196 }
197
202 template <typename Accumulator, size_t bus_idx, typename AllEntities, typename Parameters>
203 static Accumulator compute_write_term(const AllEntities& in, const Parameters& params)
204 {
205 using CoefficientAccumulator = typename Accumulator::CoefficientAccumulator;
206 using ParameterCoefficientAccumulator =
208
209 const auto& id = CoefficientAccumulator(in.databus_id);
210 const auto& value = CoefficientAccumulator(BusData<bus_idx, AllEntities>::values(in));
211 const auto& gamma = ParameterCoefficientAccumulator(params.gamma);
212 const auto& beta = ParameterCoefficientAccumulator(params.beta);
213
214 // Construct value_i + idx_i*\beta + \gamma
215 // degrees 1(0) 0(1) 1(1) 0(1)
216 return Accumulator(id * beta + value + gamma); // degree 1 (1)
217 }
218
224 template <typename Accumulator, typename AllEntities, typename Parameters>
225 static Accumulator compute_read_term(const AllEntities& in, const Parameters& params)
226 {
227 using CoefficientAccumulator = typename Accumulator::CoefficientAccumulator;
228 using View = typename Accumulator::View;
229 using ParameterView = GetParameterView<Parameters, View>;
230 using ParameterCoefficientAccumulator = typename ParameterView::CoefficientAccumulator;
231
232 // Bus value stored in w_1, index into bus column stored in w_2
233 const auto& w_1 = CoefficientAccumulator(in.w_l);
234 const auto& w_2 = CoefficientAccumulator(in.w_r);
235 const auto& gamma = ParameterCoefficientAccumulator(params.gamma);
236 const auto& beta = ParameterCoefficientAccumulator(params.beta);
237
238 // Construct value + index*\beta + \gamma
239 return Accumulator((w_2 * beta) + w_1 + gamma); // degree 1 (2)
240 }
241
250 template <size_t bus_idx, typename Polynomials>
251 static void compute_logderivative_inverse(Polynomials& polynomials,
252 auto& relation_parameters,
253 const size_t circuit_size)
254 {
255 PROFILE_THIS_NAME("Databus::compute_logderivative_inverse");
256 auto& inverse_polynomial = BusData<bus_idx, Polynomials>::inverses(polynomials);
257
258 size_t min_iterations_per_thread = 1 << 6; // min number of iterations for which we'll spin up a unique thread
259 size_t num_threads = bb::calculate_num_threads_pow2(circuit_size, min_iterations_per_thread);
260 size_t iterations_per_thread = circuit_size / num_threads; // actual iterations per thread
261
262 parallel_for(num_threads, [&](size_t thread_idx) {
263 size_t start = thread_idx * iterations_per_thread;
264 size_t end = (thread_idx + 1) * iterations_per_thread;
265 bool is_read = false;
266 bool nonzero_read_count = false;
267 for (size_t i = start; i < end; ++i) {
268 // Determine if the present row contains a databus operation
269 auto q_busread = polynomials.q_busread[i];
270 if constexpr (bus_idx == 0) { // calldata
271 is_read = q_busread == 1 && polynomials.q_l[i] == 1;
272 nonzero_read_count = polynomials.calldata_read_counts[i] > 0;
273 }
274 if constexpr (bus_idx == 1) { // secondary_calldata
275 is_read = q_busread == 1 && polynomials.q_r[i] == 1;
276 nonzero_read_count = polynomials.secondary_calldata_read_counts[i] > 0;
277 }
278 if constexpr (bus_idx == 2) { // return data
279 is_read = q_busread == 1 && polynomials.q_o[i] == 1;
280 nonzero_read_count = polynomials.return_data_read_counts[i] > 0;
281 }
282 // We only compute the inverse if this row contains a read gate or data that has been read
283 if (is_read || nonzero_read_count) {
284 // TODO(https://github.com/AztecProtocol/barretenberg/issues/940): avoid get_row if possible.
285 auto row = polynomials.get_row(i); // Note: this is a copy. use sparingly!
286 auto value = compute_read_term<FF>(row, relation_parameters) *
287 compute_write_term<FF, bus_idx>(row, relation_parameters);
288 inverse_polynomial.at(i) = value;
289 }
290 }
291 });
292
293 // Compute inverse polynomial I in place by inverting the product at each row
294 // Note: zeroes are ignored as they are not used anyway
295 FF::batch_invert(inverse_polynomial.coeffs());
296 };
297
308 template <typename FF,
309 size_t bus_idx,
310 typename ContainerOverSubrelations,
311 typename AllEntities,
312 typename Parameters>
313 static void accumulate_subrelation_contributions(ContainerOverSubrelations& accumulator,
314 const AllEntities& in,
315 const Parameters& params,
316 const FF& scaling_factor)
317 {
318 using Accumulator = typename std::tuple_element_t<4, ContainerOverSubrelations>;
319 using CoefficientAccumulator = typename Accumulator::CoefficientAccumulator;
321 const auto inverses_m = CoefficientAccumulator(BusData<bus_idx, AllEntities>::inverses(in)); // Degree 1
322 Accumulator inverses(inverses_m);
323 const auto read_counts_m = CoefficientAccumulator(BusData<bus_idx, AllEntities>::read_counts(in)); // Degree 1
324 const auto read_term = compute_read_term<Accumulator>(in, params); // Degree 1 (2)
325 const auto write_term = compute_write_term<Accumulator, bus_idx>(in, params); // Degree 1 (1)
326 const auto inverse_exists = compute_inverse_exists<Accumulator, bus_idx>(in); // Degree 3 (3)
327 const auto read_selector = get_read_selector<Accumulator, bus_idx>(in); // Degree 2 (2)
328
329 // Determine which pair of subrelations to update based on which bus column is being read
330 constexpr size_t subrel_idx_1 = NUM_SUB_RELATION_PER_IDX * bus_idx;
331 constexpr size_t subrel_idx_2 = NUM_SUB_RELATION_PER_IDX * bus_idx + 1;
332 // the subrelation index for checking the read_tag is boolean
333 constexpr size_t subrel_idx_3 = NUM_SUB_RELATION_PER_IDX * bus_idx + 2;
334
335 // Establish the correctness of the polynomial of inverses I. Note: inverses is computed so that the value
336 // is 0 if !inverse_exists. Degree 3 (4)
337 // degrees 3(4) = 1(2) 1(1) 1(1) 3(3)
338 std::get<subrel_idx_1>(accumulator) += (read_term * write_term * inverses - inverse_exists) * scaling_factor;
339
340 // Establish validity of the read. Note: no scaling factor here since this constraint is enforced across the
341 // entire trace, not on a per-row basis.
342
343 // degree 3(3) = 2(2) 1(1)
344 Accumulator tmp = read_selector * write_term;
345 // degree 2(3) = 1(1) 1(2)
346 tmp -= Accumulator(read_counts_m) * read_term;
347 // degree 1(1)
348 tmp *= inverses;
349 std::get<subrel_idx_2>(accumulator) += tmp; // Deg 4 (4)
350
351 const auto read_tag_m = CoefficientAccumulator(BusData<bus_idx, AllEntities>::read_tags(in));
352 const auto read_tag = ShortAccumulator(read_tag_m);
353 // // this is done by row so we have to multiply by the scaling factor
354 // degree 1(1) 1(1) 1(1) = 2(2)
355 std::get<subrel_idx_3>(accumulator) += (read_tag * read_tag - read_tag) * scaling_factor;
356 }
357
367 template <typename ContainerOverSubrelations, typename AllEntities, typename Parameters>
368 static void accumulate(ContainerOverSubrelations& accumulator,
369 const AllEntities& in,
370 const Parameters& params,
371 const FF& scaling_factor)
372 {
373 // Accumulate the subrelation contributions for each column of the databus
374 bb::constexpr_for<0, NUM_BUS_COLUMNS, 1>([&]<size_t bus_idx>() {
375 accumulate_subrelation_contributions<FF, bus_idx>(accumulator, in, params, scaling_factor);
376 });
377 }
378};
379
381
382} // namespace bb
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 std::array< size_t, NUM_SUB_RELATION_PER_IDX *NUM_BUS_COLUMNS > TOTAL_LENGTH_ADJUSTMENTS
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.
std::conditional_t< IsField< typename Params::DataType >, typename Params::DataType, View > GetParameterView
A type to optionally extract a view of a relation parameter in a relation.
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:215
void parallel_for(size_t num_iterations, const std::function< void(size_t)> &func)
Definition thread.cpp:72
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
#define PROFILE_THIS_NAME(name)
Definition op_count.hpp:16
static void batch_invert(std::span< field > coeffs) noexcept