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
10#include <optional>
11
12namespace bb {
13
26template <typename Curve> struct ClaimBatcher_ {
27 using Fr = typename Curve::ScalarField;
29
30 struct Batch {
33 // scalar used for batching the claims, excluding the power of batching challenge \rho
35 };
36
37 std::optional<Batch> unshifted; // commitments and evaluations of unshifted polynomials
38 std::optional<Batch> shifted; // commitments of to-be-shifted-by-1 polys, evals of their shifts
39
41 Batch get_shifted() { return (shifted) ? *shifted : Batch{}; }
42
43 Fr get_unshifted_batch_scalar() const { return unshifted ? unshifted->scalar : Fr{ 0 }; }
44
69 const Fr& nu_challenge,
70 const Fr& r_challenge)
71 {
72 const Fr& inverse_vanishing_eval_pos = inverted_vanishing_evals[0];
73 const Fr& inverse_vanishing_eval_neg = inverted_vanishing_evals[1];
74
75 if (unshifted) {
76 // (1/(z−r) + ν/(z+r))
77 unshifted->scalar = inverse_vanishing_eval_pos + nu_challenge * inverse_vanishing_eval_neg;
78 }
79 if (shifted) {
80 // r⁻¹ ⋅ (1/(z−r) − ν/(z+r))
81 //
82 // This scalar is the verifier-side to-be-shifted-by-one PCS contract: every commitment in
83 // `shifted.commitments` is required to be a commitment to a polynomial with constant term zero.
84 // A commitment to a polynomial with poly[0] != 0 opens to G(r)/r = poly[0]/r + G_shift(r) on
85 // the commitment side, whereas the claimed MLE evaluation poly_shift(u) reconstructs to
86 // G_shift(r) at the Gemini challenge. The two sides differ by poly[0]/r, the Shplonk quotient
87 // is then not a polynomial, and the KZG pairing check rejects with overwhelming probability
88 // over the FS challenges.
89 // Regression: commitment_schemes/shplonk/shplemini.test.cpp::ToBeShiftedNonZeroConstantTermRejected.
90 shifted->scalar =
91 r_challenge.invert() * (inverse_vanishing_eval_pos - nu_challenge * inverse_vanishing_eval_neg);
92 }
93 }
104 void update_batch_mul_inputs_and_batched_evaluation(std::vector<Commitment>& commitments,
105 std::vector<Fr>& scalars,
106 Fr& batched_evaluation,
107 const Fr& rho)
108 {
109 size_t num_powers = 0;
110 num_powers += unshifted.has_value() ? unshifted->commitments.size() : 0;
111 num_powers += shifted.has_value() ? shifted->commitments.size() : 0;
112
113 Fr rho_power = Fr(1);
114 size_t power_idx = 0;
115
116 // Append the commitments/scalars from a given batch to the corresponding containers; update the batched
117 // evaluation and the running batching challenge in place
118 auto aggregate_claim_data_and_update_batched_evaluation = [&](const Batch& batch) {
119 for (auto [commitment, evaluation] : zip_view(batch.commitments, batch.evaluations)) {
120 commitments.emplace_back(std::move(commitment));
121 scalars.emplace_back(-batch.scalar * rho_power);
122 batched_evaluation += evaluation * rho_power;
123 power_idx++;
124 if (power_idx < num_powers) {
125 rho_power *= rho;
126 }
127 }
128 };
129
130 // Incorporate the claim data from each batch of claims that is present in the vectors of commitments and
131 // scalars for the batch mul
132 if (unshifted) {
133 // i-th Unshifted commitment will be multiplied by ρ^i and (1/(z−r) + ν/(z+r))
134 aggregate_claim_data_and_update_batched_evaluation(*unshifted);
135 }
136 if (shifted) {
137 // i-th shifted commitments will be multiplied by ρ^{num_unshifted + i} and r⁻¹ ⋅ (1/(z−r) − ν/(z+r))
138 aggregate_claim_data_and_update_batched_evaluation(*shifted);
139 }
140
141 BB_ASSERT_EQ(power_idx, num_powers);
142 }
143};
144
145} // namespace bb
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:83
A template class for a reference vector. Behaves as if std::vector<T&> was possible.
typename Group::affine_element AffineElement
Definition grumpkin.hpp:64
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
Logic to support batching opening claims for unshifted and shifted polynomials in Shplemini.
std::optional< Batch > unshifted
void update_batch_mul_inputs_and_batched_evaluation(std::vector< Commitment > &commitments, std::vector< Fr > &scalars, Fr &batched_evaluation, const Fr &rho)
Append the commitments and scalars from each batch of claims to the Shplemini vectors which subsequen...
std::optional< Batch > shifted
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
Fr get_unshifted_batch_scalar() const
typename Curve::AffineElement Commitment