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<Batch> right_shifted_by_k; // commitments of to-be-right-shifted-by-k polys, evals of their shifts
49 std::optional<InterleavedBatch> interleaved; // commitments to groups of polynomials to be combined by interleaving
50 // and evaluations of the resulting interleaved polynomials
51
53 Batch get_shifted() { return (shifted) ? *shifted : Batch{}; }
57 {
58 return (interleaved) ? static_cast<uint32_t>(interleaved->commitments_groups[0].size()) : 0;
59 }
60
61 uint32_t k_shift_magnitude = 0; // magnitude of right-shift-by-k (assumed even)
62
63 Fr get_unshifted_batch_scalar() const { return unshifted ? unshifted->scalar : Fr{ 0 }; }
64
91 const Fr& nu_challenge,
92 const Fr& r_challenge)
93 {
94 const Fr& inverse_vanishing_eval_pos = inverted_vanishing_evals[0];
95 const Fr& inverse_vanishing_eval_neg = inverted_vanishing_evals[1];
96
97 if (unshifted) {
98 // (1/(z−r) + ν/(z+r))
99 unshifted->scalar = inverse_vanishing_eval_pos + nu_challenge * inverse_vanishing_eval_neg;
100 }
101 if (shifted) {
102 // r⁻¹ ⋅ (1/(z−r) − ν/(z+r))
103 shifted->scalar =
104 r_challenge.invert() * (inverse_vanishing_eval_pos - nu_challenge * inverse_vanishing_eval_neg);
105 }
106 if (right_shifted_by_k) {
107 // r^k ⋅ (1/(z−r) + ν/(z+r))
108 right_shifted_by_k->scalar = r_challenge.pow(k_shift_magnitude) *
109 (inverse_vanishing_eval_pos + nu_challenge * inverse_vanishing_eval_neg);
110 }
111
112 if (interleaved) {
113 const size_t interleaving_denominator_index = 2 * numeric::get_msb(get_groups_to_be_interleaved_size());
114
115 if (get_groups_to_be_interleaved_size() % 2 != 0) {
116 throw_or_abort("Interleaved groups size must be even");
117 }
118
119 Fr r_shift_pos = Fr(1);
120 Fr r_shift_neg = Fr(1);
121 interleaved->shplonk_denominator = inverted_vanishing_evals[interleaving_denominator_index];
122 for (size_t i = 0; i < get_groups_to_be_interleaved_size(); i++) {
123 interleaved->scalars_pos.push_back(r_shift_pos);
124 interleaved->scalars_neg.push_back(r_shift_neg);
125 if (i < get_groups_to_be_interleaved_size() - 1) {
126 // to avoid unnecessary multiplication gates in a circuit
127 r_shift_pos *= r_challenge;
128 r_shift_neg *= (-r_challenge);
129 }
130 }
131 }
132 }
146 void update_batch_mul_inputs_and_batched_evaluation(std::vector<Commitment>& commitments,
147 std::vector<Fr>& scalars,
148 Fr& batched_evaluation,
149 const Fr& rho,
150 Fr& rho_power,
151 Fr shplonk_batching_pos = { 0 },
152 Fr shplonk_batching_neg = { 0 })
153 {
154 // Append the commitments/scalars from a given batch to the corresponding containers; update the batched
155 // evaluation and the running batching challenge in place
156 auto aggregate_claim_data_and_update_batched_evaluation = [&](const Batch& batch, Fr& rho_power) {
157 for (auto [commitment, evaluation] : zip_view(batch.commitments, batch.evaluations)) {
158 commitments.emplace_back(std::move(commitment));
159 scalars.emplace_back(-batch.scalar * rho_power);
160 batched_evaluation += evaluation * rho_power;
161 rho_power *= rho;
162 }
163 };
164
165 // Incorporate the claim data from each batch of claims that is present in the vectors of commitments and
166 // scalars for the batch mul
167 if (unshifted) {
168 // i-th Unshifted commitment will be multiplied by ρ^i and (1/(z−r) + ν/(z+r))
169 aggregate_claim_data_and_update_batched_evaluation(*unshifted, rho_power);
170 }
171 if (shifted) {
172 // i-th shifted commitments will be multiplied by p^{k+i} and r⁻¹ ⋅ (1/(z−r) − ν/(z+r))
173 aggregate_claim_data_and_update_batched_evaluation(*shifted, rho_power);
174 }
175 if (right_shifted_by_k) {
176 // i-th right-shifted-by-k commitments will be multiplied by ρ^{k+m+i} and r^k ⋅ (1/(z−r) + ν/(z+r))
177 aggregate_claim_data_and_update_batched_evaluation(*right_shifted_by_k, rho_power);
178 }
179 if (interleaved) {
180 if (get_groups_to_be_interleaved_size() % 2 != 0) {
181 throw_or_abort("Interleaved groups size must be even");
182 }
183
184 size_t group_idx = 0;
185 for (size_t j = 0; j < interleaved->commitments_groups.size(); j++) {
186 for (size_t i = 0; i < get_groups_to_be_interleaved_size(); i++) {
187 // The j-th commitment in group i is multiplied by ρ^{k+m+i} and ν^{n+1} \cdot r^j + ν^{n+2} ⋅(-r)^j
188 // where k is the number of unshifted, m is number of shifted and n is the log_circuit_size
189 // (assuming to right-shifted-by-k commitments in this example)
190 commitments.emplace_back(std::move(interleaved->commitments_groups[j][i]));
191 scalars.emplace_back(-rho_power * interleaved->shplonk_denominator *
192 (shplonk_batching_pos * interleaved->scalars_pos[i] +
193 shplonk_batching_neg * interleaved->scalars_neg[i]));
194 }
195 batched_evaluation += interleaved->evaluations[group_idx] * rho_power;
196 if (j != interleaved->commitments_groups.size() - 1) {
197 rho_power *= rho;
198 }
199 group_idx++;
200 }
201 }
202 }
203};
204
205} // 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:56
constexpr T get_msb(const T in)
Definition get_msb.hpp:47
Entry point for Barretenberg command-line interface.
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
Batch get_right_shifted_by_k()
void update_batch_mul_inputs_and_batched_evaluation(std::vector< Commitment > &commitments, std::vector< Fr > &scalars, Fr &batched_evaluation, const Fr &rho, Fr &rho_power, 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< Batch > right_shifted_by_k
std::optional< InterleavedBatch > interleaved
Fr get_unshifted_batch_scalar() const
typename Curve::AffineElement Commitment
void throw_or_abort(std::string const &err)