Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
claim_batcher.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
12#include <optional>
13
14namespace bb {
15
28template <typename Curve> struct ClaimBatcher_ {
29 using Fr = typename Curve::ScalarField;
31
32 struct Batch {
35 // scalar used for batching the claims, excluding the power of batching challenge \rho
37 };
45
46 std::optional<Batch> unshifted; // commitments and evaluations of unshifted polynomials
47 std::optional<Batch> shifted; // commitments of to-be-shifted-by-1 polys, evals of their shifts
48 std::optional<InterleavedBatch> interleaved; // commitments to groups of polynomials to be combined by interleaving
49 // and evaluations of the resulting interleaved polynomials
50
52 Batch get_shifted() { return (shifted) ? *shifted : Batch{}; }
55 {
56 return (interleaved) ? static_cast<uint32_t>(interleaved->commitments_groups[0].size()) : 0;
57 }
58
59 Fr get_unshifted_batch_scalar() const { return unshifted ? unshifted->scalar : Fr{ 0 }; }
60
86 const Fr& nu_challenge,
87 const Fr& r_challenge)
88 {
89 const Fr& inverse_vanishing_eval_pos = inverted_vanishing_evals[0];
90 const Fr& inverse_vanishing_eval_neg = inverted_vanishing_evals[1];
91
92 if (unshifted) {
93 // (1/(z−r) + ν/(z+r))
94 unshifted->scalar = inverse_vanishing_eval_pos + nu_challenge * inverse_vanishing_eval_neg;
95 }
96 if (shifted) {
97 // r⁻¹ ⋅ (1/(z−r) − ν/(z+r))
98 shifted->scalar =
99 r_challenge.invert() * (inverse_vanishing_eval_pos - nu_challenge * inverse_vanishing_eval_neg);
100 }
101
102 if (interleaved) {
103 const size_t interleaving_denominator_index = 2 * numeric::get_msb(get_groups_to_be_interleaved_size());
104
105 if (get_groups_to_be_interleaved_size() % 2 != 0) {
106 throw_or_abort("Interleaved groups size must be even");
107 }
108
109 Fr r_shift_pos = Fr(1);
110 Fr r_shift_neg = Fr(1);
111 interleaved->shplonk_denominator = inverted_vanishing_evals[interleaving_denominator_index];
112 for (size_t i = 0; i < get_groups_to_be_interleaved_size(); i++) {
113 interleaved->scalars_pos.push_back(r_shift_pos);
114 interleaved->scalars_neg.push_back(r_shift_neg);
115 if (i < get_groups_to_be_interleaved_size() - 1) {
116 // to avoid unnecessary multiplication gates in a circuit
117 r_shift_pos *= r_challenge;
118 r_shift_neg *= (-r_challenge);
119 }
120 }
121 }
122 }
136 void update_batch_mul_inputs_and_batched_evaluation(std::vector<Commitment>& commitments,
137 std::vector<Fr>& scalars,
138 Fr& batched_evaluation,
139 const Fr& rho,
140 Fr shplonk_batching_pos = { 0 },
141 Fr shplonk_batching_neg = { 0 })
142 {
143 Fr rho_power(1);
144 // Append the commitments/scalars from a given batch to the corresponding containers; update the batched
145 // evaluation and the running batching challenge in place
146 auto aggregate_claim_data_and_update_batched_evaluation = [&](const Batch& batch, Fr& rho_power) {
147 for (auto [commitment, evaluation] : zip_view(batch.commitments, batch.evaluations)) {
148 commitments.emplace_back(std::move(commitment));
149 scalars.emplace_back(-batch.scalar * rho_power);
150 batched_evaluation += evaluation * rho_power;
151 rho_power *= rho;
152 }
153 };
154
155 // Incorporate the claim data from each batch of claims that is present in the vectors of commitments and
156 // scalars for the batch mul
157 if (unshifted) {
158 // i-th Unshifted commitment will be multiplied by ρ^i and (1/(z−r) + ν/(z+r))
159 aggregate_claim_data_and_update_batched_evaluation(*unshifted, rho_power);
160 }
161 if (shifted) {
162 // i-th shifted commitments will be multiplied by p^{k+i} and r⁻¹ ⋅ (1/(z−r) − ν/(z+r))
163 aggregate_claim_data_and_update_batched_evaluation(*shifted, rho_power);
164 }
165 if (interleaved) {
166 if (get_groups_to_be_interleaved_size() % 2 != 0) {
167 throw_or_abort("Interleaved groups size must be even");
168 }
169
170 size_t group_idx = 0;
171 for (size_t j = 0; j < interleaved->commitments_groups.size(); j++) {
172 for (size_t i = 0; i < get_groups_to_be_interleaved_size(); i++) {
173 // The j-th commitment in group i is multiplied by ρ^{k+m+i} and ν^{n+1} \cdot r^j + ν^{n+2} ⋅(-r)^j
174 // where k is the number of unshifted, m is number of shifted and n is the log_circuit_size
175 // (assuming to right-shifted-by-k commitments in this example)
176 commitments.emplace_back(std::move(interleaved->commitments_groups[j][i]));
177 scalars.emplace_back(-rho_power * interleaved->shplonk_denominator *
178 (shplonk_batching_pos * interleaved->scalars_pos[i] +
179 shplonk_batching_neg * interleaved->scalars_neg[i]));
180 }
181 batched_evaluation += interleaved->evaluations[group_idx] * rho_power;
182 if (j != interleaved->commitments_groups.size() - 1) {
183 rho_power *= rho;
184 }
185 group_idx++;
186 }
187 }
188 }
189};
190
191} // namespace bb
A template class for a reference vector. Behaves as if std::vector<T&> was possible.
typename Group::affine_element AffineElement
Definition grumpkin.hpp:63
constexpr T get_msb(const T in)
Definition get_msb.hpp:47
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
RefVector< Commitment > commitments
RefVector< Fr > evaluations
std::vector< RefVector< Commitment > > commitments_groups
Logic to support batching opening claims for unshifted and shifted polynomials in Shplemini.
std::optional< Batch > unshifted
std::optional< Batch > shifted
uint32_t get_groups_to_be_interleaved_size()
void compute_scalars_for_each_batch(std::span< const Fr > inverted_vanishing_evals, const Fr &nu_challenge, const Fr &r_challenge)
Compute scalars used to batch each set of claims, excluding contribution from batching challenge \rho...
typename Curve::ScalarField Fr
void update_batch_mul_inputs_and_batched_evaluation(std::vector< Commitment > &commitments, std::vector< Fr > &scalars, Fr &batched_evaluation, const Fr &rho, Fr shplonk_batching_pos={ 0 }, Fr shplonk_batching_neg={ 0 })
Append the commitments and scalars from each batch of claims to the Shplemini, vectors which subseque...
InterleavedBatch get_interleaved()
std::optional< InterleavedBatch > interleaved
Fr get_unshifted_batch_scalar() const
typename Curve::AffineElement Commitment
void throw_or_abort(std::string const &err)