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 // To achieve fixed proof size in Ultra and Mega, the multilinear opening challenge is be padded to a fixed size.
60 const size_t virtual_log_n = multilinear_challenge.size();
61 const size_t log_n = numeric::get_msb(circuit_size);
62
63 // Get the batching challenge
64 const Fr rho = transcript->template get_challenge<Fr>("rho");
65
66 Polynomial A_0 = polynomial_batcher.compute_batched(rho);
67
68 // Construct the d-1 Gemini foldings of A₀(X)
69 std::vector<Polynomial> fold_polynomials = compute_fold_polynomials(log_n, multilinear_challenge, A_0, has_zk);
70
71 // If virtual_log_n >= log_n, pad the fold commitments with dummy group elements [1]_1.
72 for (size_t l = 0; l < virtual_log_n - 1; l++) {
73 std::string label = "Gemini:FOLD_" + std::to_string(l + 1);
74 // When has_zk is true, we are sending commitments to 0. Seems to work, but maybe brittle.
75 transcript->send_to_verifier(label, commitment_key.commit(fold_polynomials[l]));
76 }
77 const Fr r_challenge = transcript->template get_challenge<Fr>("Gemini:r");
78
79 const bool gemini_challenge_in_small_subgroup = (has_zk) && (r_challenge.pow(Curve::SUBGROUP_SIZE) == Fr(1));
80
81 // If Gemini evaluation challenge lands in the multiplicative subgroup used by SmallSubgroupIPA protocol, the
82 // evaluations of prover polynomials at this challenge would leak witness data.
83 // TODO(https://github.com/AztecProtocol/barretenberg/issues/1194). Handle edge cases in PCS
84 if (gemini_challenge_in_small_subgroup) {
85 throw_or_abort("Gemini evaluation challenge is in the SmallSubgroup.");
86 }
87
88 // Compute polynomials A₀₊(X) = F(X) + G(X)/r and A₀₋(X) = F(X) - G(X)/r
89 auto [A_0_pos, A_0_neg] = polynomial_batcher.compute_partially_evaluated_batch_polynomials(r_challenge);
90 // Construct claims for the d + 1 univariate evaluations A₀₊(r), A₀₋(-r), and Foldₗ(−r^{2ˡ}), l = 1, ..., d-1
91 std::vector<Claim> claims = construct_univariate_opening_claims(
92 virtual_log_n, std::move(A_0_pos), std::move(A_0_neg), std::move(fold_polynomials), r_challenge);
93
94 for (size_t l = 1; l <= virtual_log_n; l++) {
95 std::string label = "Gemini:a_" + std::to_string(l);
96 transcript->send_to_verifier(label, claims[l].opening_pair.evaluation);
97 }
98
99 // If running Gemini for the Translator VM polynomials, A₀(r) = A₀₊(r) + P₊(rˢ) and A₀(-r) = A₀₋(-r) + P₋(rˢ)
100 // where s is the size of the interleaved group assumed even. The prover sends P₊(rˢ) and P₋(rˢ) to the verifier
101 // so it can reconstruct the evaluation of A₀(r) and A₀(-r) respectively
102 // TODO(https://github.com/AztecProtocol/barretenberg/issues/1282)
103 if (polynomial_batcher.has_interleaved()) {
104 auto [P_pos, P_neg] = polynomial_batcher.compute_partially_evaluated_interleaved_polynomial(r_challenge);
105 Fr r_pow = r_challenge.pow(polynomial_batcher.get_group_size());
106 Fr P_pos_eval = P_pos.evaluate(r_pow);
107 Fr P_neg_eval = P_neg.evaluate(r_pow);
108 claims.emplace_back(Claim{ std::move(P_pos), { r_pow, P_pos_eval } });
109 transcript->send_to_verifier("Gemini:P_pos", P_pos_eval);
110 claims.emplace_back(Claim{ std::move(P_neg), { r_pow, P_neg_eval } });
111 transcript->send_to_verifier("Gemini:P_neg", P_neg_eval);
112 }
113
114 return claims;
115};
116
124template <typename Curve>
126 const size_t log_n, std::span<const Fr> multilinear_challenge, const Polynomial& A_0, const bool& has_zk)
127{
128 BB_BENCH_NAME("Gemini::compute_fold_polynomials");
129 const size_t virtual_log_n = multilinear_challenge.size();
130
131 // Cost per iteration: 1 subtraction + 1 multiplication + 1 addition
132 constexpr size_t fold_iteration_cost =
134
135 // Reserve and allocate space for m-1 Fold polynomials, the foldings of the full batched polynomial A₀
136 std::vector<Polynomial> fold_polynomials;
137 fold_polynomials.reserve(virtual_log_n - 1);
138 for (size_t l = 0; l < log_n - 1; ++l) {
139 // size of the previous polynomial/2
140 const size_t n_l = 1 << (log_n - l - 1);
141
142 // A_l_fold = Aₗ₊₁(X) = (1-uₗ)⋅even(Aₗ)(X) + uₗ⋅odd(Aₗ)(X)
143 fold_polynomials.emplace_back(Polynomial(n_l));
144 }
145
146 // A_l = Aₗ(X) is the polynomial being folded
147 // in the first iteration, we take the batched polynomial
148 // in the next iteration, it is the previously folded one
149 auto A_l = A_0.data();
150 for (size_t l = 0; l < log_n - 1; ++l) {
151 // size of the previous polynomial/2
152 const size_t n_l = 1 << (log_n - l - 1);
153
154 // Opening point is the same for all
155 const Fr u_l = multilinear_challenge[l];
156
157 // A_l_fold = Aₗ₊₁(X) = (1-uₗ)⋅even(Aₗ)(X) + uₗ⋅odd(Aₗ)(X)
158 auto A_l_fold = fold_polynomials[l].data();
159
161 n_l,
162 [&](size_t j) {
163 // fold(Aₗ)[j] = (1-uₗ)⋅even(Aₗ)[j] + uₗ⋅odd(Aₗ)[j]
164 // = (1-uₗ)⋅Aₗ[2j] + uₗ⋅Aₗ[2j+1]
165 // = Aₗ₊₁[j]
166 A_l_fold[j] = A_l[j << 1] + u_l * (A_l[(j << 1) + 1] - A_l[j << 1]);
167 },
168 fold_iteration_cost);
169 // set Aₗ₊₁ = Aₗ for the next iteration
170 A_l = A_l_fold;
171 }
172
173 // Perform virtual rounds.
174 // After the first `log_n - 1` rounds, the prover's `fold` univariates stabilize. With ZK, the verifier multiplies
175 // the evaluations by 0, otherwise, when `virtual_log_n > log_n`, the prover honestly computes and sends the
176 // constant folds.
177 const auto& last = fold_polynomials.back();
178 const Fr u_last = multilinear_challenge[log_n - 1];
179 const Fr final_eval = last.at(0) + u_last * (last.at(1) - last.at(0));
180 Polynomial const_fold(1);
181 // Temporary fix: when we're running a zk proof, the verifier uses a `padding_indicator_array`. So the evals in
182 // rounds past `log_n - 1` will be ignored. Hence the prover also needs to ignore them, otherwise Shplonk will fail.
183 const_fold.at(0) = final_eval * Fr(static_cast<int>(!has_zk));
184 fold_polynomials.emplace_back(const_fold);
185
186 // FOLD_{log_n+1}, ..., FOLD_{d_v-1}
187 Fr tail = Fr(1);
188 for (size_t k = log_n; k < virtual_log_n - 1; ++k) {
189 tail *= (Fr(1) - multilinear_challenge[k]); // multiply by (1 - u_k)
190 Polynomial next_const(1);
191 next_const.at(0) = final_eval * tail * Fr(static_cast<int>(!has_zk));
192 fold_polynomials.emplace_back(next_const);
193 }
194
195 return fold_polynomials;
196};
197
219template <typename Curve>
221 const size_t log_n,
222 Polynomial&& A_0_pos,
223 Polynomial&& A_0_neg,
224 std::vector<Polynomial>&& fold_polynomials,
225 const Fr& r_challenge)
226{
227 std::vector<Claim> claims;
228
229 // Compute evaluation of partially evaluated batch polynomial (positive) A₀₊(r)
230 Fr a_0_pos = A_0_pos.evaluate(r_challenge);
231 claims.emplace_back(Claim{ std::move(A_0_pos), { r_challenge, a_0_pos } });
232 // Compute evaluation of partially evaluated batch polynomial (negative) A₀₋(-r)
233 Fr a_0_neg = A_0_neg.evaluate(-r_challenge);
234 claims.emplace_back(Claim{ std::move(A_0_neg), { -r_challenge, a_0_neg } });
235
236 // Compute univariate opening queries rₗ = r^{2ˡ} for l = 0, 1, ..., m-1
237 std::vector<Fr> r_squares = gemini::powers_of_evaluation_challenge(r_challenge, log_n);
238
239 // Each fold polynomial Aₗ has to be opened at −r^{2ˡ} and r^{2ˡ}. To avoid storing two copies of Aₗ for l = 1,...,
240 // m-1, we use a flag that is processed by ShplonkProver.
241 const bool gemini_fold = true;
242
243 // Compute the remaining m opening pairs {−r^{2ˡ}, Aₗ(−r^{2ˡ})}, l = 1, ..., m-1.
244 for (size_t l = 0; l < log_n - 1; ++l) {
245 Fr evaluation = fold_polynomials[l].evaluate(-r_squares[l + 1]);
246 claims.emplace_back(Claim{ std::move(fold_polynomials[l]), { -r_squares[l + 1], evaluation }, gemini_fold });
247 }
248
249 return claims;
250};
251
252} // namespace bb
#define BB_BENCH_NAME(name)
Definition bb_bench.hpp:225
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: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
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
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
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:106
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.
Structured polynomial class that represents the coefficients 'a' of a_0 + a_1 x .....
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:74
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
constexpr T get_msb(const T in)
Definition get_msb.hpp:47
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)