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
14
51namespace bb {
52
67namespace gemini {
76template <class Fr> inline std::vector<Fr> powers_of_rho(const Fr& rho, const size_t num_powers)
77{
78 std::vector<Fr> rhos;
79 rhos.reserve(num_powers);
80 if (num_powers >= 1) {
81 rhos.emplace_back(Fr(1));
82 }
83 for (size_t j = 1; j < num_powers; j++) {
84 rhos.emplace_back(rhos[j - 1] * rho);
85 }
86 return rhos;
87};
88
96template <class Fr> inline std::vector<Fr> powers_of_evaluation_challenge(const Fr& r, const size_t num_squares)
97{
98 std::vector<Fr> squares;
99 squares.reserve(num_squares);
100 squares.emplace_back(r);
101 for (size_t j = 1; j < num_squares; j++) {
102 squares.emplace_back(squares[j - 1].sqr());
103 }
104 return squares;
105};
106} // namespace gemini
107
108template <typename Curve> class GeminiProver_ {
109 using Fr = typename Curve::ScalarField;
113
114 public:
130
131 size_t full_batched_size = 0; // size of the full batched polynomial (generally the circuit size)
132 size_t actual_data_size_ = 0; // max end_index across all polynomials (actual data extent)
133
134 Polynomial batched_unshifted; // linear combination of unshifted polynomials
135 Polynomial batched_to_be_shifted_by_one; // linear combination of to-be-shifted polynomials
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
141 PolynomialBatcher(const size_t full_batched_size, const size_t actual_data_size = 0)
143 , actual_data_size_(actual_data_size == 0 ? full_batched_size : actual_data_size)
146 {}
147
148 bool has_unshifted() const { return unshifted.size() > 0; }
149 bool has_to_be_shifted_by_one() const { return to_be_shifted_by_one.size() > 0; }
150
151 // Set references to the polynomials to be batched
152 void set_unshifted(RefVector<Polynomial> polynomials) { unshifted = polynomials; }
154
163 Polynomial compute_batched(const Fr& challenge)
164 {
165 BB_BENCH_NAME("compute_batched");
166 Fr running_scalar(1);
167
168 // Batch base polynomials via a single fused parallel_for over the destination range,
169 // amortising N× parallel_for startup overhead into 1×. Updates running_scalar in place.
170 auto batch = [&](Polynomial& batched, const RefVector<Polynomial>& polynomials_to_batch) {
171 const size_t n = polynomials_to_batch.size();
173 std::vector<Fr> scalars;
174 sources.reserve(n);
175 scalars.reserve(n);
176 for (size_t i = 0; i < n; ++i) {
177 sources.emplace_back(polynomials_to_batch[i]);
178 scalars.push_back(running_scalar);
179 running_scalar *= challenge;
180 }
182 batched, std::span<const PolynomialSpan<const Fr>>(sources), std::span<const Fr>(scalars));
183 };
184
185 Polynomial full_batched(full_batched_size);
186
187 if (has_unshifted()) {
189 full_batched += batched_unshifted;
190 }
191
194 full_batched += batched_to_be_shifted_by_one.shifted();
195 }
196
197 return full_batched;
198 }
199
207 {
209
210 if (has_unshifted()) {
211 A_0_pos += batched_unshifted;
212 }
213
214 Polynomial A_0_neg = A_0_pos;
215
216 Fr r_inv = r_challenge.invert();
220 }
221
222 return { A_0_pos, A_0_neg };
223 };
224 };
225
226 static std::vector<Polynomial> compute_fold_polynomials(const size_t log_n,
227 std::span<const Fr> multilinear_challenge,
228 const Polynomial& A_0);
229
231 Polynomial&& A_0_pos,
232 Polynomial&& A_0_neg,
233 std::vector<Polynomial>&& fold_polynomials,
234 const Fr& r_challenge);
235
236 template <typename Transcript>
237 static std::vector<Claim> prove(size_t circuit_size,
238 PolynomialBatcher& polynomial_batcher,
239 std::span<Fr> multilinear_challenge,
240 const CommitmentKey<Curve>& commitment_key,
241 const std::shared_ptr<Transcript>& transcript,
242 bool has_zk = false);
243
244}; // namespace bb
245
249template <typename Curve> class GeminiVerifier_ {
250 using Fr = typename Curve::ScalarField;
252
253 public:
262 static std::vector<Commitment> get_fold_commitments(const size_t virtual_log_n, auto& transcript)
263 {
264 std::vector<Commitment> fold_commitments;
265 fold_commitments.reserve(virtual_log_n - 1);
266 for (size_t i = 1; i < virtual_log_n; ++i) {
267 const Commitment commitment =
268 transcript->template receive_from_prover<Commitment>("Gemini:FOLD_" + std::to_string(i));
269 fold_commitments.emplace_back(commitment);
270 }
271 return fold_commitments;
272 }
273
283 static std::vector<Fr> get_gemini_evaluations(const size_t virtual_log_n, auto& transcript)
284 {
285 std::vector<Fr> gemini_evaluations;
286 gemini_evaluations.reserve(virtual_log_n);
287
288 for (size_t i = 1; i <= virtual_log_n; ++i) {
289 const Fr evaluation = transcript->template receive_from_prover<Fr>("Gemini:a_" + std::to_string(i));
290 gemini_evaluations.emplace_back(evaluation);
291 }
292 return gemini_evaluations;
293 }
294
318 static std::vector<Fr> compute_fold_pos_evaluations(const Fr& batched_evaluation,
319 std::span<const Fr> evaluation_point, // size = virtual_log_n
320 std::span<const Fr> challenge_powers, // size = virtual_log_n
321 std::span<const Fr> fold_neg_evals) // size = virtual_log_n
322 {
323 const size_t virtual_log_n = evaluation_point.size();
324
325 std::vector<Fr> evals(fold_neg_evals.begin(), fold_neg_evals.end());
326
327 Fr eval_pos_prev = batched_evaluation;
328
329 std::vector<Fr> fold_pos_evaluations;
330 fold_pos_evaluations.reserve(virtual_log_n);
331
332 // Solve the sequence of linear equations
333 for (size_t l = virtual_log_n; l != 0; --l) {
334 // Get r²⁽ˡ⁻¹⁾
335 const Fr& challenge_power = challenge_powers[l - 1];
336 // Get uₗ₋₁
337 const Fr& u = evaluation_point[l - 1];
338 // Get A₍ₗ₋₁₎(−r²⁽ˡ⁻¹⁾)
339 const Fr& eval_neg = evals[l - 1];
340 // Compute the numerator
341 Fr eval_pos = ((challenge_power * eval_pos_prev * 2) - eval_neg * (challenge_power * (Fr(1) - u) - u));
342 // Divide by the denominator
343 eval_pos *= (challenge_power * (Fr(1) - u) + u).invert();
344
345 eval_pos_prev = eval_pos;
346 fold_pos_evaluations.emplace_back(eval_pos_prev);
347 }
348
349 std::reverse(fold_pos_evaluations.begin(), fold_pos_evaluations.end());
350
351 return fold_pos_evaluations;
352 }
353};
354
355} // namespace bb
#define BB_BENCH_NAME(name)
Definition bb_bench.hpp:264
CommitmentKey object over a pairing group 𝔾₁.
Class responsible for computation of the batched multilinear polynomials required by the Gemini proto...
Definition gemini.hpp:129
void set_to_be_shifted_by_one(RefVector< Polynomial > polynomials)
Definition gemini.hpp:153
PolynomialBatcher(const size_t full_batched_size, const size_t actual_data_size=0)
Definition gemini.hpp:141
void set_unshifted(RefVector< Polynomial > polynomials)
Definition gemini.hpp:152
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:163
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:206
RefVector< Polynomial > unshifted
Definition gemini.hpp:138
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:109
typename Curve::AffineElement Commitment
Definition gemini.hpp:110
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.
Gemini Verifier utility methods used by ShpleminiVerifier.
Definition gemini.hpp:249
typename Curve::ScalarField Fr
Definition gemini.hpp:250
static std::vector< Fr > compute_fold_pos_evaluations(const Fr &batched_evaluation, std::span< const Fr > evaluation_point, std::span< const Fr > challenge_powers, std::span< const Fr > fold_neg_evals)
Compute .
Definition gemini.hpp:318
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:262
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:283
typename Curve::AffineElement Commitment
Definition gemini.hpp:251
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:36
A template class for a reference vector. Behaves as if std::vector<T&> was possible.
typename Group::affine_element AffineElement
Definition grumpkin.hpp:64
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
std::vector< Fr > powers_of_rho(const Fr &rho, const size_t num_powers)
Compute powers of challenge ρ
Definition gemini.hpp:76
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
void add_scaled_batch(Polynomial< Fr > &dst, std::span< const PolynomialSpan< const Fr > > sources, std::span< const Fr > scalars)
Fused parallel batched add: dst += sum_i scalars[i] * sources[i].
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::string to_string(bb::avm2::ValueTag tag)
Curve::ScalarField Fr