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: Complete, auditors: [Khashayar], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
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
85 const Fr& nu_challenge,
86 const Fr& r_challenge)
87 {
88 const Fr& inverse_vanishing_eval_pos = inverted_vanishing_evals[0];
89 const Fr& inverse_vanishing_eval_neg = inverted_vanishing_evals[1];
90
91 if (unshifted) {
92 // (1/(z−r) + ν/(z+r))
93 unshifted->scalar = inverse_vanishing_eval_pos + nu_challenge * inverse_vanishing_eval_neg;
94 }
95 if (shifted) {
96 // r⁻¹ ⋅ (1/(z−r) − ν/(z+r))
97 shifted->scalar =
98 r_challenge.invert() * (inverse_vanishing_eval_pos - nu_challenge * inverse_vanishing_eval_neg);
99 }
100
101 if (interleaved) {
102 const size_t interleaving_denominator_index = 2 * numeric::get_msb(get_groups_to_be_interleaved_size());
103
104 if (get_groups_to_be_interleaved_size() % 2 != 0) {
105 throw_or_abort("Interleaved groups size must be even");
106 }
107
108 Fr r_shift_pos = Fr(1);
109 Fr r_shift_neg = Fr(1);
110 interleaved->shplonk_denominator = inverted_vanishing_evals[interleaving_denominator_index];
111 for (size_t i = 0; i < get_groups_to_be_interleaved_size(); i++) {
112 interleaved->scalars_pos.push_back(r_shift_pos);
113 interleaved->scalars_neg.push_back(r_shift_neg);
114 if (i < get_groups_to_be_interleaved_size() - 1) {
115 // to avoid unnecessary multiplication gates in a circuit
116 r_shift_pos *= r_challenge;
117 r_shift_neg *= (-r_challenge);
118 }
119 }
120 }
121 }
135 void update_batch_mul_inputs_and_batched_evaluation(std::vector<Commitment>& commitments,
136 std::vector<Fr>& scalars,
137 Fr& batched_evaluation,
138 const Fr& rho,
139 Fr shplonk_batching_pos = { 0 },
140 Fr shplonk_batching_neg = { 0 })
141 {
142 Fr rho_power(1);
143 // Append the commitments/scalars from a given batch to the corresponding containers; update the batched
144 // evaluation and the running batching challenge in place
145 auto aggregate_claim_data_and_update_batched_evaluation = [&](const Batch& batch, Fr& rho_power) {
146 for (auto [commitment, evaluation] : zip_view(batch.commitments, batch.evaluations)) {
147 commitments.emplace_back(std::move(commitment));
148 scalars.emplace_back(-batch.scalar * rho_power);
149 batched_evaluation += evaluation * rho_power;
150 rho_power *= rho;
151 }
152 };
153
154 // Incorporate the claim data from each batch of claims that is present in the vectors of commitments and
155 // scalars for the batch mul
156 if (unshifted) {
157 // i-th Unshifted commitment will be multiplied by ρ^i and (1/(z−r) + ν/(z+r))
158 aggregate_claim_data_and_update_batched_evaluation(*unshifted, rho_power);
159 }
160 if (shifted) {
161 // i-th shifted commitments will be multiplied by ρ^{num_unshifted + i} and r⁻¹ ⋅ (1/(z−r) − ν/(z+r))
162 aggregate_claim_data_and_update_batched_evaluation(*shifted, rho_power);
163 }
164 if (interleaved) {
165 if (get_groups_to_be_interleaved_size() % 2 != 0) {
166 throw_or_abort("Interleaved groups size must be even");
167 }
168
169 size_t group_idx = 0;
170 for (size_t j = 0; j < interleaved->commitments_groups.size(); j++) {
171 for (size_t i = 0; i < get_groups_to_be_interleaved_size(); i++) {
172 // The j-th commitment in group i is multiplied by ρ^{m+i} and ν^{d+1} \cdot r^j + ν^{d+2} ⋅(-r)^j
173 // where d is the log_circuit_size
174 commitments.emplace_back(std::move(interleaved->commitments_groups[j][i]));
175 scalars.emplace_back(-rho_power * interleaved->shplonk_denominator *
176 (shplonk_batching_pos * interleaved->scalars_pos[i] +
177 shplonk_batching_neg * interleaved->scalars_neg[i]));
178 }
179 batched_evaluation += interleaved->evaluations[group_idx] * rho_power;
180 if (j != interleaved->commitments_groups.size() - 1) {
181 rho_power *= rho;
182 }
183 group_idx++;
184 }
185 }
186 }
187};
188
189} // 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, shifted and interleaved polynomials in Shplem...
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)