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: Complete, auditors: [Sergei], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
7#pragma once
8#include <array>
9#include <tuple>
10
16
17namespace bb {
18
61template <typename FF_> class DatabusLookupRelationImpl {
62 public:
63 using FF = FF_;
64
65 // Note: All three subrelations use length 6 to make efficient use of shared computation. Shortening (1b)/(2) to
66 // length 5 forces the shared factors (lookup_term, table_term, read_selector, inverses, `I*L*T - 1`) to be
67 // recomputed and is a net loss in performance.
68 static constexpr size_t INVERSE_READ_SUBREL_LENGTH = 6; // deg 5: (I*L*T - 1) * is_read
69 static constexpr size_t INVERSE_WRITE_SUBREL_LENGTH = 6; // deg 4: (I*L*T - 1) * count
70 static constexpr size_t LOOKUP_SUBREL_LENGTH = 6; // deg 4: (is_read*T - count*L) * I
71 static constexpr size_t NUM_SUB_RELATION_PER_IDX = 3; // the number of subrelations per bus column
72
73 // Per-bus subrelation layout: (1a) inverse-on-read, (1b) inverse-on-write, (2) lookup identity.
74 // The whole-relation arrays below repeat this layout once per bus column.
75 static constexpr std::array<size_t, NUM_SUB_RELATION_PER_IDX> PER_BUS_SUBREL_LENGTHS{
79 };
80 // (1a) and (1b) are per-row identities; (2) is a sum across the trace.
81 static constexpr std::array<bool, NUM_SUB_RELATION_PER_IDX> PER_BUS_LIN_INDEPENDENT{ true, true, false };
82
83 template <typename T, size_t N>
85 {
87 for (size_t bus = 0; bus < NUM_BUS_COLUMNS; ++bus) {
88 for (size_t i = 0; i < N; ++i) {
89 out[(bus * N) + i] = pattern[i];
90 }
91 }
92 return out;
93 }
94
95 static constexpr std::array<size_t, NUM_SUB_RELATION_PER_IDX * NUM_BUS_COLUMNS> SUBRELATION_PARTIAL_LENGTHS =
97 static constexpr std::array<bool, NUM_SUB_RELATION_PER_IDX * NUM_BUS_COLUMNS> SUBRELATION_LINEARLY_INDEPENDENT =
99
100 template <typename AllEntities> inline static bool skip([[maybe_unused]] const AllEntities& in)
101 {
102 // Ensure the input does not contain a read gate or data that is being read
103 if (!in.q_busread.is_zero()) {
104 return false;
105 }
106 bool all_counts_zero = true;
107 bb::constexpr_for<0, NUM_BUS_COLUMNS, 1>(
108 [&]<size_t bus_idx>() { all_counts_zero &= BusData<bus_idx, AllEntities>::read_counts(in).is_zero(); });
109 return all_counts_zero;
110 }
111
112 // Interface for easy access of databus components by column (bus_idx)
113 template <size_t bus_idx, typename AllEntities> struct BusData;
114
115 // Specialization for calldata (bus_idx = 0)
116 template <typename AllEntities> struct BusData</*bus_idx=*/0, AllEntities> {
117 static auto& values(const AllEntities& in) { return in.calldata; }
118 static auto& selector(const AllEntities& in) { return in.q_l; }
119 static auto& inverses(AllEntities& in) { return in.calldata_inverses; }
120 static auto& inverses(const AllEntities& in) { return in.calldata_inverses; } // const version
121 static auto& read_counts(const AllEntities& in) { return in.calldata_read_counts; }
122 };
123
124 // Specialization for secondary_calldata (bus_idx = 1)
125 template <typename AllEntities> struct BusData</*bus_idx=*/1, AllEntities> {
126 static auto& values(const AllEntities& in) { return in.secondary_calldata; }
127 static auto& selector(const AllEntities& in) { return in.q_r; }
128 static auto& inverses(AllEntities& in) { return in.secondary_calldata_inverses; }
129 static auto& inverses(const AllEntities& in) { return in.secondary_calldata_inverses; } // const version
130 static auto& read_counts(const AllEntities& in) { return in.secondary_calldata_read_counts; }
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 };
141
149 template <typename Accumulator, size_t bus_idx, typename AllEntities>
150 static Accumulator get_read_selector(const AllEntities& in)
151 {
152 using CoefficientAccumulator = typename Accumulator::CoefficientAccumulator;
153
154 auto q_busread = CoefficientAccumulator(in.q_busread);
155 auto column_selector = CoefficientAccumulator(BusData<bus_idx, AllEntities>::selector(in));
156
157 // degree 1 1 2 (2)
158 return Accumulator(q_busread * column_selector);
159 }
160
165 template <typename Accumulator, size_t bus_idx, typename AllEntities, typename Parameters>
166 static Accumulator compute_table_term(const AllEntities& in, const Parameters& params)
167 {
168 using CoefficientAccumulator = typename Accumulator::CoefficientAccumulator;
169 using ParameterCoefficientAccumulator = typename Parameters::DataType::CoefficientAccumulator;
170
171 const auto& id = CoefficientAccumulator(in.databus_id);
172 const auto& value = CoefficientAccumulator(BusData<bus_idx, AllEntities>::values(in));
173 const auto& gamma = ParameterCoefficientAccumulator(params.gamma);
174 const auto& beta = ParameterCoefficientAccumulator(params.beta);
175
176 // Construct value_i + idx_i*\beta + \gamma
177 // degrees 1(0) 0(1) 1(1) 0(1)
178 return Accumulator(id * beta + value + gamma); // degree 1 (1)
179 }
180
186 template <typename Accumulator, typename AllEntities, typename Parameters>
187 static Accumulator compute_lookup_term(const AllEntities& in, const Parameters& params)
188 {
189 using CoefficientAccumulator = typename Accumulator::CoefficientAccumulator;
190 using ParameterCoefficientAccumulator = typename Parameters::DataType::CoefficientAccumulator;
191
192 // Bus value stored in w_1, index into bus column stored in w_2
193 const auto& w_1 = CoefficientAccumulator(in.w_l);
194 const auto& w_2 = CoefficientAccumulator(in.w_r);
195 const auto& gamma = ParameterCoefficientAccumulator(params.gamma);
196 const auto& beta = ParameterCoefficientAccumulator(params.beta);
197
198 // Construct value + index*\beta + \gamma
199 return Accumulator((w_2 * beta) + w_1 + gamma); // degree 1 (2)
200 }
201
213 template <size_t bus_idx, typename Polynomials>
214 static void compute_logderivative_inverse(Polynomials& polynomials,
215 auto& relation_parameters,
216 const size_t circuit_size,
217 const size_t start_index = 0)
218 {
219 BB_BENCH_NAME("Databus::compute_logderivative_inverse");
220 auto& inverse_polynomial = BusData<bus_idx, Polynomials>::inverses(polynomials);
221 const auto& column_selector = BusData<bus_idx, Polynomials>::selector(polynomials);
222 const auto& read_counts = BusData<bus_idx, Polynomials>::read_counts(polynomials);
223
224 const size_t num_rows = circuit_size - start_index;
225 size_t min_iterations_per_thread = 1 << 6; // min number of iterations for which we'll spin up a unique thread
226 size_t num_threads = bb::calculate_num_threads(num_rows, min_iterations_per_thread);
227
228 parallel_for(num_threads, [&](ThreadChunk chunk) {
229 BB_BENCH_TRACY_NAME("Databus::compute_inverses/chunk");
230 for (size_t j : chunk.range(num_rows)) {
231 size_t i = j + start_index;
232 // Determine if the present row contains a databus operation
233 const bool is_read = polynomials.q_busread[i] == 1 && column_selector[i] == 1;
234 const bool nonzero_read_count = read_counts[i] > 0;
235 // We only compute the inverse if this row contains a read gate or data that has been read
236 if (is_read || nonzero_read_count) {
237 // TODO(https://github.com/AztecProtocol/barretenberg/issues/940): avoid get_row if possible.
238 auto row = polynomials.get_row(i); // Note: this is a copy. use sparingly!
239 auto value = compute_lookup_term<FF>(row, relation_parameters) *
240 compute_table_term<FF, bus_idx>(row, relation_parameters);
241 inverse_polynomial.at(i) = value;
242 }
243 }
244 });
245
246 // Compute inverse polynomial I in place by inverting the product at each row
247 // Note: zeroes are ignored as they are not used anyway
248 FF::batch_invert(inverse_polynomial.coeffs());
249 };
250
258 template <typename FF,
259 size_t bus_idx,
260 typename ContainerOverSubrelations,
261 typename AllEntities,
262 typename Parameters>
263 static void accumulate_subrelation_contributions(ContainerOverSubrelations& accumulator,
264 const AllEntities& in,
265 const Parameters& params,
266 const FF& scaling_factor)
267 {
268 // Subrelation indices for this bus column
269 constexpr size_t subrel_idx_inv_read = NUM_SUB_RELATION_PER_IDX * bus_idx; // (1a)
270 constexpr size_t subrel_idx_inv_write = NUM_SUB_RELATION_PER_IDX * bus_idx + 1; // (1b)
271 constexpr size_t subrel_idx_lookup = NUM_SUB_RELATION_PER_IDX * bus_idx + 2; // (2)
272
274 using CoefficientAccumulator = typename Accumulator::CoefficientAccumulator;
275
276 const auto inverses_m = CoefficientAccumulator(BusData<bus_idx, AllEntities>::inverses(in));
277 const auto read_counts_m = CoefficientAccumulator(BusData<bus_idx, AllEntities>::read_counts(in));
278
279 const Accumulator inverses(inverses_m);
280 const Accumulator read_counts(read_counts_m);
281 const auto lookup_term = compute_lookup_term<Accumulator>(in, params);
282 const auto table_term = compute_table_term<Accumulator, bus_idx>(in, params);
283 const auto read_selector = get_read_selector<Accumulator, bus_idx>(in);
284
285 // Shared factor in (1a) and (1b): I*L*T - 1
286 const auto common = lookup_term * table_term * inverses - FF(1);
287
288 // (1a) Inverse correctness on read rows: (I*L*T - 1) * is_read = 0
289 std::get<subrel_idx_inv_read>(accumulator) += (common * read_selector) * scaling_factor;
290
291 // (1b) Inverse correctness on write rows: (I*L*T - 1) * count = 0
292 std::get<subrel_idx_inv_write>(accumulator) += (common * read_counts) * scaling_factor;
293
294 // (2) Lookup identity: (is_read*T - count*L) * I = 0.
295 // No scaling factor here since this constraint is enforced across the entire trace, not per-row.
296 Accumulator tmp = read_selector * table_term;
297 tmp -= read_counts * lookup_term;
298 tmp *= inverses;
299 std::get<subrel_idx_lookup>(accumulator) += tmp;
300 }
301
309 template <typename ContainerOverSubrelations, typename AllEntities, typename Parameters>
310 static void accumulate(ContainerOverSubrelations& accumulator,
311 const AllEntities& in,
312 const Parameters& params,
313 const FF& scaling_factor)
314 {
315 // Accumulate the subrelation contributions for each column of the databus
316 bb::constexpr_for<0, NUM_BUS_COLUMNS, 1>([&]<size_t bus_idx>() {
317 accumulate_subrelation_contributions<FF, bus_idx>(accumulator, in, params, scaling_factor);
318 });
319 }
320};
321
323
324} // namespace bb
#define BB_BENCH_NAME(name)
Definition bb_bench.hpp:264
#define BB_BENCH_TRACY_NAME(name)
Definition bb_bench.hpp:256
Log-derivative lookup argument relation for establishing DataBus reads.
static constexpr size_t INVERSE_WRITE_SUBREL_LENGTH
static constexpr std::array< T, N *NUM_BUS_COLUMNS > repeat_per_bus(const std::array< T, N > &pattern)
static constexpr size_t NUM_SUB_RELATION_PER_IDX
static bool skip(const AllEntities &in)
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, const size_t start_index=0)
Construct the polynomial whose components are the inverse of the product of the read and write terms...
static constexpr size_t INVERSE_READ_SUBREL_LENGTH
static constexpr size_t LOOKUP_SUBREL_LENGTH
static constexpr std::array< bool, NUM_SUB_RELATION_PER_IDX *NUM_BUS_COLUMNS > SUBRELATION_LINEARLY_INDEPENDENT
static constexpr std::array< bool, NUM_SUB_RELATION_PER_IDX > PER_BUS_LIN_INDEPENDENT
static Accumulator compute_lookup_term(const AllEntities &in, const Parameters &params)
Compute read term denominator in log derivative lookup argument.
static Accumulator get_read_selector(const AllEntities &in)
Compute scalar for read term in log derivative lookup argument.
static Accumulator compute_table_term(const AllEntities &in, const Parameters &params)
Compute write term denominator in log derivative lookup argument.
static constexpr std::array< size_t, NUM_SUB_RELATION_PER_IDX > PER_BUS_SUBREL_LENGTHS
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
constexpr size_t NUM_BUS_COLUMNS
The DataBus; facilitates storage of public circuit input/output.
Definition databus.hpp:76
size_t calculate_num_threads(size_t num_iterations, size_t min_iterations_per_thread)
calculates number of threads to create based on minimum iterations per thread
Definition thread.cpp:232
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
auto range(size_t size, size_t offset=0) const
Definition thread.hpp:152
static void batch_invert(C &coeffs) noexcept
Batch invert a collection of field elements using Montgomery's trick.