Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
gemini_impl.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Planned, 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 "gemini.hpp"
11
48namespace bb {
49template <typename Curve>
50template <typename Transcript>
52 size_t circuit_size,
53 PolynomialBatcher& polynomial_batcher,
54 std::span<Fr> multilinear_challenge,
55 const CommitmentKey<Curve>& commitment_key,
56 const std::shared_ptr<Transcript>& transcript,
57 bool has_zk)
58{
59 BB_BENCH_NAME("GeminiProver::prove");
60 // To achieve fixed proof size in Ultra and Mega, the multilinear opening challenge is be padded to a fixed size.
61 const size_t virtual_log_n = multilinear_challenge.size();
62 const size_t log_n = numeric::get_msb(circuit_size);
63
64 // Get the batching challenge
65 const Fr rho = transcript->template get_challenge<Fr>("rho");
66
67 Polynomial A_0 = polynomial_batcher.compute_batched(rho);
68
69 // Construct the d-1 Gemini foldings of A₀(X)
70 std::vector<Polynomial> fold_polynomials = compute_fold_polynomials(log_n, multilinear_challenge, A_0);
71
72 // If virtual_log_n >= log_n, pad the fold commitments with dummy group elements [1]_1.
73 for (size_t l = 0; l < virtual_log_n - 1; l++) {
74 std::string label = "Gemini:FOLD_" + std::to_string(l + 1);
75 // Virtual-round fold polynomials are constant; their commitments are zeroed by the verifier.
76 transcript->send_to_verifier(label, commitment_key.commit(fold_polynomials[l]));
77 }
78 const Fr r_challenge = transcript->template get_challenge<Fr>("Gemini:r");
79
80 const bool gemini_challenge_in_small_subgroup = (has_zk) && (r_challenge.pow(Curve::SUBGROUP_SIZE) == Fr(1));
81
82 // If Gemini evaluation challenge lands in the multiplicative subgroup used by SmallSubgroupIPA protocol, the
83 // evaluations of prover polynomials at this challenge would leak witness data.
84 // TODO(https://github.com/AztecProtocol/barretenberg/issues/1194). Handle edge cases in PCS
85 if (gemini_challenge_in_small_subgroup) {
86 throw_or_abort("Gemini evaluation challenge is in the SmallSubgroup.");
87 }
88
89 // Compute polynomials A₀₊(X) = F(X) + G(X)/r and A₀₋(X) = F(X) - G(X)/r
90 auto [A_0_pos, A_0_neg] = polynomial_batcher.compute_partially_evaluated_batch_polynomials(r_challenge);
91 // Construct claims for the d + 1 univariate evaluations A₀₊(r), A₀₋(-r), and Foldₗ(−r^{2ˡ}), l = 1, ..., d-1
92 std::vector<Claim> claims = construct_univariate_opening_claims(
93 virtual_log_n, std::move(A_0_pos), std::move(A_0_neg), std::move(fold_polynomials), r_challenge);
94
95 for (size_t l = 1; l <= virtual_log_n; l++) {
96 std::string label = "Gemini:a_" + std::to_string(l);
97 transcript->send_to_verifier(label, claims[l].opening_pair.evaluation);
98 }
99
100 return claims;
101};
102
110template <typename Curve>
112 const size_t log_n, std::span<const Fr> multilinear_challenge, const Polynomial& A_0)
113{
114 BB_BENCH_NAME("Gemini::compute_fold_polynomials");
115 BB_ASSERT_GTE(log_n, size_t(2), "Gemini folding requires at least 4-element polynomials");
116 const size_t virtual_log_n = multilinear_challenge.size();
117
118 // Cost per iteration: 1 subtraction + 1 multiplication + 1 addition
119 constexpr size_t fold_iteration_cost =
121
122 // Track the actual data extent through fold rounds. Only non-zero coefficients need folding;
123 // beyond this extent, all values are zero and contribute nothing.
124 // At minimum, the disabled head region must be covered (masking values live at rows 1..3).
125 size_t actual_size = std::max(A_0.end_index(), static_cast<size_t>(NUM_DISABLED_ROWS_IN_SUMCHECK));
126
127 // Reserve and allocate space for m-1 Fold polynomials, the foldings of the full batched polynomial A₀
128 std::vector<Polynomial> fold_polynomials;
129 fold_polynomials.reserve(virtual_log_n - 1);
130 for (size_t l = 0; l < log_n - 1; ++l) {
131 const size_t fold_size = (actual_size + 1) / 2;
132
133 // A_l_fold = Aₗ₊₁(X) = (1-uₗ)⋅even(Aₗ)(X) + uₗ⋅odd(Aₗ)(X)
134 fold_polynomials.emplace_back(Polynomial(fold_size));
135 actual_size = fold_size;
136 }
137
138 // A_l = Aₗ(X) is the polynomial being folded
139 // in the first iteration, we take the batched polynomial
140 // in the next iteration, it is the previously folded one
141 actual_size = A_0.end_index();
142 auto A_l = A_0.data();
143 for (size_t l = 0; l < log_n - 1; ++l) {
144 const size_t fold_size = (actual_size + 1) / 2;
145 const size_t num_pairs = actual_size / 2; // number of full even/odd pairs
146
147 // Opening point is the same for all; use zero for rounds beyond the challenge size
148 const Fr u_l = l < virtual_log_n ? multilinear_challenge[l] : Fr(0);
149
150 // A_l_fold = Aₗ₊₁(X) = (1-uₗ)⋅even(Aₗ)(X) + uₗ⋅odd(Aₗ)(X)
151 auto A_l_fold = fold_polynomials[l].data();
152
154 num_pairs,
155 [&](size_t j) {
156 // fold(Aₗ)[j] = (1-uₗ)⋅even(Aₗ)[j] + uₗ⋅odd(Aₗ)[j]
157 // = (1-uₗ)⋅Aₗ[2j] + uₗ⋅Aₗ[2j+1]
158 // = Aₗ₊₁[j]
159 A_l_fold[j] = A_l[j << 1] + u_l * (A_l[(j << 1) + 1] - A_l[j << 1]);
160 },
161 fold_iteration_cost);
162 // If odd number of coefficients, the last one has no partner (implicitly 0)
163 if (actual_size & 1) {
164 A_l_fold[num_pairs] = A_l[actual_size - 1] * (Fr(1) - u_l);
165 }
166 // set Aₗ₊₁ = Aₗ for the next iteration
167 A_l = A_l_fold;
168 actual_size = fold_size;
169 }
170
171 // Virtual rounds (indices log_n .. virtual_log_n - 1).
172 // After real folding, the fold polynomials are constant. Since each constant polynomial evaluates to its own
173 // value at every point, (f(X) - f(x)) / (X - x) = 0, so these contribute nothing to the Shplonk quotient Q(X).
174 // On the verifier side, these constant fold polynomials contribute nothing to the Shplonk quotient.
175 const auto& last = fold_polynomials.back();
176 const Fr u_last = (log_n - 1) < virtual_log_n ? multilinear_challenge[log_n - 1] : Fr(0);
177 const Fr final_eval = last.at(0) + u_last * (last.at(1) - last.at(0));
178 Polynomial const_fold(1);
179 const_fold.at(0) = final_eval;
180 fold_polynomials.emplace_back(const_fold);
181
182 // FOLD_{log_n+1}, ..., FOLD_{d_v-1}
183 Fr tail = Fr(1);
184 for (size_t k = log_n; k < virtual_log_n - 1; ++k) {
185 tail *= (Fr(1) - multilinear_challenge[k]); // multiply by (1 - u_k)
186 Polynomial next_const(1);
187 next_const.at(0) = final_eval * tail;
188 fold_polynomials.emplace_back(next_const);
189 }
190
191 return fold_polynomials;
192};
193
215template <typename Curve>
217 const size_t log_n,
218 Polynomial&& A_0_pos,
219 Polynomial&& A_0_neg,
220 std::vector<Polynomial>&& fold_polynomials,
221 const Fr& r_challenge)
222{
223 std::vector<Claim> claims;
224
225 // Compute evaluation of partially evaluated batch polynomial (positive) A₀₊(r)
226 Fr a_0_pos = A_0_pos.evaluate(r_challenge);
227 claims.emplace_back(Claim{ std::move(A_0_pos), { r_challenge, a_0_pos } });
228 // Compute evaluation of partially evaluated batch polynomial (negative) A₀₋(-r)
229 Fr a_0_neg = A_0_neg.evaluate(-r_challenge);
230 claims.emplace_back(Claim{ std::move(A_0_neg), { -r_challenge, a_0_neg } });
231
232 // Compute univariate opening queries rₗ = r^{2ˡ} for l = 0, 1, ..., m-1
233 std::vector<Fr> r_squares = gemini::powers_of_evaluation_challenge(r_challenge, log_n);
234
235 // Each fold polynomial Aₗ has to be opened at −r^{2ˡ} and r^{2ˡ}. To avoid storing two copies of Aₗ for l = 1,...,
236 // m-1, we use a flag that is processed by ShplonkProver.
237 const bool gemini_fold = true;
238
239 // Compute the remaining m opening pairs {−r^{2ˡ}, Aₗ(−r^{2ˡ})}, l = 1, ..., m-1.
240 for (size_t l = 0; l < log_n - 1; ++l) {
241 Fr evaluation = fold_polynomials[l].evaluate(-r_squares[l + 1]);
242 claims.emplace_back(Claim{ std::move(fold_polynomials[l]), { -r_squares[l + 1], evaluation }, gemini_fold });
243 }
244
245 return claims;
246};
247
248} // namespace bb
#define BB_ASSERT_GTE(left, right,...)
Definition assert.hpp:128
#define BB_BENCH_NAME(name)
Definition bb_bench.hpp:264
CommitmentKey object over a pairing group 𝔾₁.
Commitment commit(PolynomialSpan< const Fr > polynomial) const
Uses the ProverSRS to create a commitment to p(X)
Class responsible for computation of the batched multilinear polynomials required by the Gemini proto...
Definition gemini.hpp:128
Polynomial compute_batched(const Fr &challenge)
Compute batched polynomial A₀ = F + G/X as the linear combination of all polynomials to be opened,...
Definition gemini.hpp:181
std::pair< Polynomial, Polynomial > compute_partially_evaluated_batch_polynomials(const Fr &r_challenge)
Compute partially evaluated batched polynomials A₀(X, r) = A₀₊ = F + G/r, A₀(X, -r) = A₀₋ = F - G/r.
Definition gemini.hpp:248
static std::vector< Claim > prove(size_t circuit_size, PolynomialBatcher &polynomial_batcher, std::span< Fr > multilinear_challenge, const CommitmentKey< Curve > &commitment_key, const std::shared_ptr< Transcript > &transcript, bool has_zk=false)
static std::vector< Claim > construct_univariate_opening_claims(const size_t log_n, Polynomial &&A_0_pos, Polynomial &&A_0_neg, std::vector< Polynomial > &&fold_polynomials, const Fr &r_challenge)
Computes/aggragates d+1 univariate polynomial opening claims of the form {polynomial,...
typename Curve::ScalarField Fr
Definition gemini.hpp:108
static std::vector< Polynomial > compute_fold_polynomials(const size_t log_n, std::span< const Fr > multilinear_challenge, const Polynomial &A_0)
Computes d-1 fold polynomials Fold_i, i = 1, ..., d-1.
Structured polynomial class that represents the coefficients 'a' of a_0 + a_1 x .....
size_t end_index() const
Fr & at(size_t index)
Our mutable accessor, unlike operator[]. We abuse precedent a bit to differentiate at() and operator[...
Polynomial p and an opening pair (r,v) such that p(r) = v.
Definition claim.hpp:36
static constexpr size_t SUBGROUP_SIZE
Definition grumpkin.hpp:72
std::vector< Fr > powers_of_evaluation_challenge(const Fr &r, const size_t num_squares)
Compute squares of folding challenge r.
Definition gemini.hpp:96
constexpr T get_msb(const T in)
Definition get_msb.hpp:50
constexpr size_t FF_ADDITION_COST
Definition thread.hpp:132
constexpr size_t FF_MULTIPLICATION_COST
Definition thread.hpp:134
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
void parallel_for_heuristic(size_t num_points, const std::function< void(size_t, size_t, size_t)> &func, size_t heuristic_cost)
Split a loop into several loops running in parallel based on operations in 1 iteration.
Definition thread.cpp:171
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::string to_string(bb::avm2::ValueTag tag)
Curve::ScalarField Fr
void throw_or_abort(std::string const &err)