Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
gemini.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
8
15
52namespace bb {
53
68namespace gemini {
77template <class Fr> inline std::vector<Fr> powers_of_rho(const Fr& rho, const size_t num_powers)
78{
79 std::vector<Fr> rhos = { Fr(1), rho };
80 rhos.reserve(num_powers);
81 for (size_t j = 2; j < num_powers; j++) {
82 rhos.emplace_back(rhos[j - 1] * rho);
83 }
84 return rhos;
85};
86
94template <class Fr> inline std::vector<Fr> powers_of_evaluation_challenge(const Fr& r, const size_t num_squares)
95{
96 std::vector<Fr> squares = { r };
97 squares.reserve(num_squares);
98 for (size_t j = 1; j < num_squares; j++) {
99 squares.emplace_back(squares[j - 1].sqr());
100 }
101 return squares;
102};
103} // namespace gemini
104
105template <typename Curve> class GeminiProver_ {
106 using Fr = typename Curve::ScalarField;
110
111 public:
127
128 size_t full_batched_size = 0; // size of the full batched polynomial (generally the circuit size)
129
130 Polynomial batched_unshifted; // linear combination of unshifted polynomials
131 Polynomial batched_to_be_shifted_by_one; // linear combination of to-be-shifted polynomials
132 Polynomial batched_interleaved; // linear combination of interleaved polynomials
133 // linear combination of the groups to be interleaved where polynomial i in the batched group is obtained by
134 // linearly combining the i-th polynomial in each group
136
137 public:
138 RefVector<Polynomial> unshifted; // set of unshifted polynomials
139 RefVector<Polynomial> to_be_shifted_by_one; // set of polynomials to be left shifted by 1
140 RefVector<Polynomial> interleaved; // the interleaved polynomials used in Translator
141 std::vector<RefVector<Polynomial>> groups_to_be_interleaved; // groups of polynomials to be interleaved
142
148
149 bool has_unshifted() const { return unshifted.size() > 0; }
150 bool has_to_be_shifted_by_one() const { return to_be_shifted_by_one.size() > 0; }
151 bool has_interleaved() const { return interleaved.size() > 0; }
152
153 // Set references to the polynomials to be batched
154 void set_unshifted(RefVector<Polynomial> polynomials) { unshifted = polynomials; }
156
158 {
159 // Ensure the Gemini subprotocol for interleaved polynomials operates correctly
160 if (groups[0].size() % 2 != 0) {
161 throw_or_abort("Group size must be even ");
162 }
163 interleaved = results;
165 }
166
175 Polynomial compute_batched(const Fr& challenge)
176 {
177 Fr running_scalar(1);
178 BB_BENCH_NAME("compute_batched");
179 // lambda for batching polynomials; updates the running scalar in place
180 auto batch = [&](Polynomial& batched, const RefVector<Polynomial>& polynomials_to_batch) {
181 for (auto& poly : polynomials_to_batch) {
182 batched.add_scaled(poly, running_scalar);
183 running_scalar *= challenge;
184 }
185 };
186
187 Polynomial full_batched(full_batched_size);
188
189 // compute the linear combination F of the unshifted polynomials
190 if (has_unshifted()) {
192 full_batched += batched_unshifted; // A₀ += F
193 }
194
195 // compute the linear combination G of the to-be-shifted polynomials
198 full_batched += batched_to_be_shifted_by_one.shifted(); // A₀ += G/X
199 }
200
201 // compute the linear combination of the interleaved polynomials and groups
202 if (has_interleaved()) {
204 for (size_t i = 0; i < groups_to_be_interleaved[0].size(); ++i) {
206 }
207
208 for (size_t i = 0; i < groups_to_be_interleaved.size(); ++i) {
209 batched_interleaved.add_scaled(interleaved[i], running_scalar);
210 // Use parallel chunking for the batching operations
211 parallel_for([this, running_scalar, i](const ThreadChunk& chunk) {
212 for (size_t j = 0; j < groups_to_be_interleaved[0].size(); ++j) {
213 batched_group[j].add_scaled_chunk(chunk, groups_to_be_interleaved[i][j], running_scalar);
214 }
215 });
216 running_scalar *= challenge;
217 }
218
219 full_batched += batched_interleaved;
220 }
221
222 return full_batched;
223 }
224
232 {
233 // Initialize A₀₊ and compute A₀₊ += F as necessary
234 Polynomial A_0_pos(full_batched_size); // A₀₊
235
236 if (has_unshifted()) {
237 A_0_pos += batched_unshifted; // A₀₊ += F
238 }
239
240 Polynomial A_0_neg = A_0_pos;
241
243 Fr r_inv = r_challenge.invert(); // r⁻¹
244 batched_to_be_shifted_by_one *= r_inv; // G = G/r
245
246 A_0_pos += batched_to_be_shifted_by_one; // A₀₊ += G/r
247 A_0_neg -= batched_to_be_shifted_by_one; // A₀₋ -= G/r
248 }
249
250 return { A_0_pos, A_0_neg };
251 };
264 {
265 Polynomial P_pos(batched_group[0]);
266 Polynomial P_neg(batched_group[0]);
267
268 Fr current_r_shift_pos = r_challenge;
269 Fr current_r_shift_neg = -r_challenge;
270 for (size_t i = 1; i < batched_group.size(); i++) {
271 // Add r^i * Pᵢ(X) to P₊(X)
272 P_pos.add_scaled(batched_group[i], current_r_shift_pos);
273 // Add (-r)^i * Pᵢ(X) to P₋(X)
274 P_neg.add_scaled(batched_group[i], current_r_shift_neg);
275 // Update the current power of r
276 current_r_shift_pos *= r_challenge;
277 current_r_shift_neg *= -r_challenge;
278 }
279
280 return { P_pos, P_neg };
281 }
282
283 size_t get_group_size() { return batched_group.size(); }
284 };
285
286 static std::vector<Polynomial> compute_fold_polynomials(const size_t log_n,
287 std::span<const Fr> multilinear_challenge,
288 const Polynomial& A_0,
289 const bool& has_zk = false);
290
292 const size_t log_n,
293 PolynomialBatcher& polynomial_batcher,
294 const Fr& r_challenge,
295 const std::vector<Polynomial>& batched_groups_to_be_concatenated = {});
296
298 Polynomial&& A_0_pos,
299 Polynomial&& A_0_neg,
300 std::vector<Polynomial>&& fold_polynomials,
301 const Fr& r_challenge);
302
303 template <typename Transcript>
304 static std::vector<Claim> prove(size_t circuit_size,
305 PolynomialBatcher& polynomial_batcher,
306 std::span<Fr> multilinear_challenge,
307 const CommitmentKey<Curve>& commitment_key,
308 const std::shared_ptr<Transcript>& transcript,
309 bool has_zk = false);
310
311}; // namespace bb
312
316template <typename Curve> class GeminiVerifier_ {
317 using Fr = typename Curve::ScalarField;
319
320 public:
329 static std::vector<Commitment> get_fold_commitments(const size_t virtual_log_n, auto& transcript)
330 {
331 std::vector<Commitment> fold_commitments;
332 fold_commitments.reserve(virtual_log_n - 1);
333 for (size_t i = 1; i < virtual_log_n; ++i) {
334 const Commitment commitment =
335 transcript->template receive_from_prover<Commitment>("Gemini:FOLD_" + std::to_string(i));
336 fold_commitments.emplace_back(commitment);
337 }
338 return fold_commitments;
339 }
340
350 static std::vector<Fr> get_gemini_evaluations(const size_t virtual_log_n, auto& transcript)
351 {
352 std::vector<Fr> gemini_evaluations;
353 gemini_evaluations.reserve(virtual_log_n);
354
355 for (size_t i = 1; i <= virtual_log_n; ++i) {
356 const Fr evaluation = transcript->template receive_from_prover<Fr>("Gemini:a_" + std::to_string(i));
357 gemini_evaluations.emplace_back(evaluation);
358 }
359 return gemini_evaluations;
360 }
361
397 static std::vector<Fr> compute_fold_pos_evaluations(std::span<const Fr> padding_indicator_array,
398 const Fr& batched_evaluation,
399 std::span<const Fr> evaluation_point, // size = virtual_log_n
400 std::span<const Fr> challenge_powers, // size = virtual_log_n
401 std::span<const Fr> fold_neg_evals, // size = virtual_log_n
402 Fr p_neg = Fr(0))
403 {
404 const size_t virtual_log_n = evaluation_point.size();
405
406 std::vector<Fr> evals(fold_neg_evals.begin(), fold_neg_evals.end());
407
408 Fr eval_pos_prev = batched_evaluation;
409
410 std::vector<Fr> fold_pos_evaluations;
411 fold_pos_evaluations.reserve(virtual_log_n);
412
413 // Add the contribution of P-((-r)ˢ) to get A_0(-r), which is 0 if there are no interleaved polynomials
414 evals[0] += p_neg;
415 // Solve the sequence of linear equations
416 for (size_t l = virtual_log_n; l != 0; --l) {
417 // Get r²⁽ˡ⁻¹⁾
418 const Fr& challenge_power = challenge_powers[l - 1];
419 // Get uₗ₋₁
420 const Fr& u = evaluation_point[l - 1];
421 // Get A₍ₗ₋₁₎(−r²⁽ˡ⁻¹⁾)
422 const Fr& eval_neg = evals[l - 1];
423 // Compute the numerator
424 Fr eval_pos = ((challenge_power * eval_pos_prev * 2) - eval_neg * (challenge_power * (Fr(1) - u) - u));
425 // Divide by the denominator
426 eval_pos *= (challenge_power * (Fr(1) - u) + u).invert();
427
428 // If current index is bigger than log_n, we propagate `batched_evaluation` to the next
429 // round. Otherwise, current `eval_pos` A₍ₗ₋₁₎(r²⁽ˡ⁻¹⁾) becomes `eval_pos_prev` in the round l-2.
430 eval_pos_prev =
431 padding_indicator_array[l - 1] * eval_pos + (Fr{ 1 } - padding_indicator_array[l - 1]) * eval_pos_prev;
432 // If current index is bigger than log_n, we emplace 0, which is later multiplied against
433 // Commitment::one().
434 fold_pos_evaluations.emplace_back(padding_indicator_array[l - 1] * eval_pos_prev);
435 }
436
437 std::reverse(fold_pos_evaluations.begin(), fold_pos_evaluations.end());
438
439 return fold_pos_evaluations;
440 }
441};
442
443} // namespace bb
#define BB_BENCH_NAME(name)
Definition bb_bench.hpp:219
CommitmentKey object over a pairing group 𝔾₁.
Class responsible for computation of the batched multilinear polynomials required by the Gemini proto...
Definition gemini.hpp:126
std::pair< Polynomial, Polynomial > compute_partially_evaluated_interleaved_polynomial(const Fr &r_challenge)
Compute the partially evaluated polynomials P₊(X, r) and P₋(X, -r)
Definition gemini.hpp:263
void set_to_be_shifted_by_one(RefVector< Polynomial > polynomials)
Definition gemini.hpp:155
void set_interleaved(RefVector< Polynomial > results, std::vector< RefVector< Polynomial > > groups)
Definition gemini.hpp:157
RefVector< Polynomial > interleaved
Definition gemini.hpp:140
std::vector< RefVector< Polynomial > > groups_to_be_interleaved
Definition gemini.hpp:141
void set_unshifted(RefVector< Polynomial > polynomials)
Definition gemini.hpp:154
std::vector< Polynomial > batched_group
Definition gemini.hpp:135
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:175
RefVector< Polynomial > to_be_shifted_by_one
Definition gemini.hpp:139
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:231
RefVector< Polynomial > unshifted
Definition gemini.hpp:138
PolynomialBatcher(const size_t full_batched_size)
Definition gemini.hpp:143
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)
bb::Polynomial< Fr > Polynomial
Definition gemini.hpp:108
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:106
static std::pair< Polynomial, Polynomial > compute_partially_evaluated_batch_polynomials(const size_t log_n, PolynomialBatcher &polynomial_batcher, const Fr &r_challenge, const std::vector< Polynomial > &batched_groups_to_be_concatenated={})
typename Curve::AffineElement Commitment
Definition gemini.hpp:107
static std::vector< Polynomial > compute_fold_polynomials(const size_t log_n, std::span< const Fr > multilinear_challenge, const Polynomial &A_0, const bool &has_zk=false)
Computes d-1 fold polynomials Fold_i, i = 1, ..., d-1.
Gemini Verifier utility methods used by ShpleminiVerifier.
Definition gemini.hpp:316
typename Curve::ScalarField Fr
Definition gemini.hpp:317
static std::vector< Fr > compute_fold_pos_evaluations(std::span< const Fr > padding_indicator_array, const Fr &batched_evaluation, std::span< const Fr > evaluation_point, std::span< const Fr > challenge_powers, std::span< const Fr > fold_neg_evals, Fr p_neg=Fr(0))
Compute .
Definition gemini.hpp:397
static std::vector< Commitment > get_fold_commitments(const size_t virtual_log_n, auto &transcript)
Receive the fold commitments from the prover. This method is used by Shplemini where padding may be e...
Definition gemini.hpp:329
static std::vector< Fr > get_gemini_evaluations(const size_t virtual_log_n, auto &transcript)
Receive the fold evaluations from the prover. This method is used by Shplemini where padding may be e...
Definition gemini.hpp:350
typename Curve::AffineElement Commitment
Definition gemini.hpp:318
Structured polynomial class that represents the coefficients 'a' of a_0 + a_1 x .....
Polynomial shifted() const
Returns a Polynomial the left-shift of self.
void add_scaled(PolynomialSpan< const Fr > other, const Fr &scaling_factor)
adds the polynomial q(X) 'other', multiplied by a scaling factor.
Polynomial p and an opening pair (r,v) such that p(r) = v.
Definition claim.hpp:34
A template class for a reference vector. Behaves as if std::vector<T&> was possible.
typename Group::affine_element AffineElement
Definition grumpkin.hpp:63
std::vector< Fr > powers_of_evaluation_challenge(const Fr &r, const size_t num_squares)
Compute squares of folding challenge r.
Definition gemini.hpp:94
std::vector< Fr > powers_of_rho(const Fr &rho, const size_t num_powers)
Compute powers of challenge ρ
Definition gemini.hpp:77
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
void parallel_for(size_t num_iterations, const std::function< void(size_t)> &func)
Definition thread.cpp:111
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)