Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
shplonk.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
14
29namespace bb {
30
36template <typename Curve> class ShplonkProver_ {
37 using Fr = typename Curve::ScalarField;
39
40 public:
49 static Polynomial compute_batched_quotient(const size_t virtual_log_n,
50 std::span<const ProverOpeningClaim<Curve>> opening_claims,
51 const Fr& nu,
52 std::span<Fr> gemini_fold_pos_evaluations,
53 std::span<const ProverOpeningClaim<Curve>> libra_opening_claims,
54 std::span<const ProverOpeningClaim<Curve>> sumcheck_round_claims)
55 {
56 // Find the maximum polynomial size among all claims to determine the dyadic size of the batched polynomial.
57 size_t max_poly_size{ 0 };
58
59 for (const auto& claim_set : { opening_claims, libra_opening_claims, sumcheck_round_claims }) {
60 for (const auto& claim : claim_set) {
61 max_poly_size = std::max(max_poly_size, claim.polynomial.size());
62 }
63 }
64 // The polynomials in Sumcheck Round claims and Libra opening claims are generally not dyadic,
65 // so we round up to the next power of 2.
66 max_poly_size = numeric::round_up_power_2(max_poly_size);
67
68 // Q(X) = ∑ⱼ νʲ ⋅ ( fⱼ(X) − vⱼ) / ( X − xⱼ )
69 Polynomial Q(max_poly_size);
70 Polynomial tmp(max_poly_size);
71
72 Fr current_nu = Fr::one();
73
74 size_t fold_idx = 0;
75 for (const auto& claim : opening_claims) {
76
77 // Gemini Fold Polynomials have to be opened at -r^{2^j} and r^{2^j}.
78 if (claim.gemini_fold) {
79 tmp = claim.polynomial;
80 tmp.at(0) = tmp[0] - gemini_fold_pos_evaluations[fold_idx++];
81 tmp.factor_roots(-claim.opening_pair.challenge);
82 // Add the claim quotient to the batched quotient polynomial
83 Q.add_scaled(tmp, current_nu);
84 current_nu *= nu;
85 }
86
87 // Compute individual claim quotient tmp = ( fⱼ(X) − vⱼ) / ( X − xⱼ )
88 tmp = claim.polynomial;
89 tmp.at(0) = tmp[0] - claim.opening_pair.evaluation;
90 tmp.factor_roots(claim.opening_pair.challenge);
91 // Add the claim quotient to the batched quotient polynomial
92 Q.add_scaled(tmp, current_nu);
93 current_nu *= nu;
94 }
95 // We use the same batching challenge for Gemini and Libra opening claims. The number of the claims
96 // batched before adding Libra commitments and evaluations is bounded by 2 * `virtual_log_n` + 2, where
97 // 2 * `virtual_log_n` is the number of fold claims including the dummy ones, and +2 is reserved for
98 // interleaving.
99 if (!libra_opening_claims.empty()) {
100 current_nu = nu.pow(2 * virtual_log_n + NUM_INTERLEAVING_CLAIMS);
101 }
102
103 for (const auto& claim : libra_opening_claims) {
104 // Compute individual claim quotient tmp = ( fⱼ(X) − vⱼ) / ( X − xⱼ )
105 tmp = claim.polynomial;
106 tmp.at(0) = tmp[0] - claim.opening_pair.evaluation;
107 tmp.factor_roots(claim.opening_pair.challenge);
108
109 // Add the claim quotient to the batched quotient polynomial
110 Q.add_scaled(tmp, current_nu);
111 current_nu *= nu;
112 }
113
114 for (const auto& claim : sumcheck_round_claims) {
115
116 // Compute individual claim quotient tmp = ( fⱼ(X) − vⱼ) / ( X − xⱼ )
117 tmp = claim.polynomial;
118 tmp.at(0) = tmp[0] - claim.opening_pair.evaluation;
119 tmp.factor_roots(claim.opening_pair.challenge);
120
121 // Add the claim quotient to the batched quotient polynomial
122 Q.add_scaled(tmp, current_nu);
123 current_nu *= nu;
124 }
125 // Return batched quotient polynomial Q(X)
126 return Q;
127 };
128
140 const size_t virtual_log_n,
141 std::span<ProverOpeningClaim<Curve>> opening_claims,
142 Polynomial& batched_quotient_Q,
143 const Fr& nu_challenge,
144 const Fr& z_challenge,
145 std::span<Fr> gemini_fold_pos_evaluations,
146 std::span<ProverOpeningClaim<Curve>> libra_opening_claims = {},
147 std::span<ProverOpeningClaim<Curve>> sumcheck_opening_claims = {})
148 {
149 // Our main use case is the opening of Gemini fold polynomials and each Gemini fold is opened at 2 points.
150 const size_t num_gemini_opening_claims = 2 * opening_claims.size();
151 const size_t num_opening_claims =
152 num_gemini_opening_claims + libra_opening_claims.size() + sumcheck_opening_claims.size();
153
154 // {ẑⱼ(z)}ⱼ , where ẑⱼ(r) = 1/zⱼ(z) = 1/(z - xⱼ)
155 std::vector<Fr> inverse_vanishing_evals;
156 inverse_vanishing_evals.reserve(num_opening_claims);
157 for (const auto& claim : opening_claims) {
158 if (claim.gemini_fold) {
159 inverse_vanishing_evals.emplace_back(z_challenge + claim.opening_pair.challenge);
160 }
161 inverse_vanishing_evals.emplace_back(z_challenge - claim.opening_pair.challenge);
162 }
163
164 // Add the terms (z - uₖ) for k = 0, …, d−1 where d is the number of rounds in Sumcheck
165 for (const auto& claim : libra_opening_claims) {
166 inverse_vanishing_evals.emplace_back(z_challenge - claim.opening_pair.challenge);
167 }
168
169 for (const auto& claim : sumcheck_opening_claims) {
170 inverse_vanishing_evals.emplace_back(z_challenge - claim.opening_pair.challenge);
171 }
172
173 Fr::batch_invert(inverse_vanishing_evals);
174
175 // G(X) = Q(X) - Q_z(X) = Q(X) - ∑ⱼ νʲ ⋅ ( fⱼ(X) − vⱼ) / ( z − xⱼ ),
176 // s.t. G(r) = 0
177 Polynomial G(std::move(batched_quotient_Q)); // G(X) = Q(X)
178
179 // G₀ = ∑ⱼ νʲ ⋅ vⱼ / ( z − xⱼ )
180 Fr current_nu = Fr::one();
181 Polynomial tmp(G.size());
182 size_t idx = 0;
183
184 size_t fold_idx = 0;
185 for (auto& claim : opening_claims) {
186
187 if (claim.gemini_fold) {
188 tmp = claim.polynomial;
189 tmp.at(0) = tmp[0] - gemini_fold_pos_evaluations[fold_idx++];
190 Fr scaling_factor = current_nu * inverse_vanishing_evals[idx++]; // = νʲ / (z − xⱼ )
191 // G -= νʲ ⋅ ( fⱼ(X) − vⱼ) / ( z − xⱼ )
192 G.add_scaled(tmp, -scaling_factor);
193
194 current_nu *= nu_challenge;
195 }
196 // tmp = νʲ ⋅ ( fⱼ(X) − vⱼ) / ( z − xⱼ )
197 claim.polynomial.at(0) = claim.polynomial[0] - claim.opening_pair.evaluation;
198 Fr scaling_factor = current_nu * inverse_vanishing_evals[idx++]; // = νʲ / (z − xⱼ )
199
200 // G -= νʲ ⋅ ( fⱼ(X) − vⱼ) / ( z − xⱼ )
201 G.add_scaled(claim.polynomial, -scaling_factor);
202
203 current_nu *= nu_challenge;
204 }
205
206 // Take into account the constant proof size in Gemini
207 if (!libra_opening_claims.empty()) {
208 current_nu = nu_challenge.pow(2 * virtual_log_n + NUM_INTERLEAVING_CLAIMS);
209 }
210
211 for (auto& claim : libra_opening_claims) {
212 // Compute individual claim quotient tmp = ( fⱼ(X) − vⱼ) / ( X − xⱼ )
213 claim.polynomial.at(0) = claim.polynomial[0] - claim.opening_pair.evaluation;
214 Fr scaling_factor = current_nu * inverse_vanishing_evals[idx++]; // = νʲ / (z − xⱼ )
215
216 // Add the claim quotient to the batched quotient polynomial
217 G.add_scaled(claim.polynomial, -scaling_factor);
218 current_nu *= nu_challenge;
219 }
220
221 for (auto& claim : sumcheck_opening_claims) {
222 claim.polynomial.at(0) = claim.polynomial[0] - claim.opening_pair.evaluation;
223 Fr scaling_factor = current_nu * inverse_vanishing_evals[idx++]; // = νʲ / (z − xⱼ )
224
225 // Add the claim quotient to the batched quotient polynomial
226 G.add_scaled(claim.polynomial, -scaling_factor);
227 current_nu *= nu_challenge;
228 }
229 // Return opening pair (z, 0) and polynomial G(X) = Q(X) - Q_z(X)
230 return { .polynomial = G, .opening_pair = { .challenge = z_challenge, .evaluation = Fr::zero() } };
231 };
240 std::span<const ProverOpeningClaim<Curve>> opening_claims)
241 {
242 std::vector<Fr> gemini_fold_pos_evaluations;
243 gemini_fold_pos_evaluations.reserve(opening_claims.size());
244
245 for (const auto& claim : opening_claims) {
246 if (claim.gemini_fold) {
247 // -r^{2^i} is stored in the claim
248 const Fr evaluation_point = -claim.opening_pair.challenge;
249 // Compute Fold_i(r^{2^i})
250 const Fr evaluation = claim.polynomial.evaluate(evaluation_point);
251 gemini_fold_pos_evaluations.emplace_back(evaluation);
252 }
253 }
254 return gemini_fold_pos_evaluations;
255 }
256
266 template <typename Transcript>
268 std::span<ProverOpeningClaim<Curve>> opening_claims,
269 const std::shared_ptr<Transcript>& transcript,
270 std::span<ProverOpeningClaim<Curve>> libra_opening_claims = {},
271 std::span<ProverOpeningClaim<Curve>> sumcheck_round_claims = {},
272 const size_t virtual_log_n = 0)
273 {
274 const Fr nu = transcript->template get_challenge<Fr>("Shplonk:nu");
275
276 // Compute the evaluations Fold_i(r^{2^i}) for i>0.
277 std::vector<Fr> gemini_fold_pos_evaluations = compute_gemini_fold_pos_evaluations(opening_claims);
278
279 auto batched_quotient = compute_batched_quotient(virtual_log_n,
280 opening_claims,
281 nu,
282 gemini_fold_pos_evaluations,
283 libra_opening_claims,
284 sumcheck_round_claims);
285 auto batched_quotient_commitment = commitment_key.commit(batched_quotient);
286 transcript->send_to_verifier("Shplonk:Q", batched_quotient_commitment);
287 const Fr z = transcript->template get_challenge<Fr>("Shplonk:z");
288
290 opening_claims,
291 batched_quotient,
292 nu,
293 z,
294 gemini_fold_pos_evaluations,
295 libra_opening_claims,
296 sumcheck_round_claims);
297 }
298};
299
343template <typename Curve> class ShplonkVerifier_ {
344 using Fr = typename Curve::ScalarField;
345 using GroupElement = typename Curve::Element;
348
349 // Random challenges
350 std::vector<Fr> pows_of_nu;
351 // Commitment to quotient polynomial
353 // Partial evaluation challenge
355 // Commitments \f$[f_1], \dots, [f_n]\f$
356 std::vector<Commitment> commitments;
357 // Scalar coefficients of \f$[f_1], \dots, [f_n]\f$ in the MSM needed to compute the commitment to the partially
358 // evaluated quotient
359 std::vector<Fr> scalars;
360 // Coefficient of the identity in partially evaluated quotient
362 // Target evaluation
364
365 public:
366 template <typename Transcript>
367 ShplonkVerifier_(std::vector<Commitment>& polynomial_commitments,
368 std::shared_ptr<Transcript>& transcript,
369 const size_t num_claims)
370 : pows_of_nu({ Fr(1), transcript->template get_challenge<Fr>("Shplonk:nu") })
371 , quotient(transcript->template receive_from_prover<Commitment>("Shplonk:Q"))
372 , z_challenge(transcript->template get_challenge<Fr>("Shplonk:z"))
373 , commitments({ quotient })
374 , scalars{ Fr{ 1 } }
375 {
376 BB_ASSERT_GT(num_claims, 1U, "Using Shplonk with just one claim. Should use batch reduction.");
377 const size_t num_commitments = commitments.size();
378 commitments.reserve(num_commitments);
379 scalars.reserve(num_commitments);
380 pows_of_nu.reserve(num_claims);
381
382 commitments.insert(commitments.end(), polynomial_commitments.begin(), polynomial_commitments.end());
383 scalars.insert(scalars.end(), commitments.size() - 1, Fr(0)); // Initialized as circuit constants
384 // The first two powers of nu have already been initialized, we need another `num_claims - 2` powers to batch
385 // all the claims
386 for (size_t idx = 0; idx < num_claims - 2; idx++) {
387 pows_of_nu.emplace_back(pows_of_nu.back() * pows_of_nu[1]);
388 }
389
390 if constexpr (Curve::is_stdlib_type) {
391 evaluation.convert_constant_to_fixed_witness(pows_of_nu[1].get_context());
392 }
393 }
394
404 {
405 commitments.emplace_back(g1_identity);
407 GroupElement result;
408 if constexpr (Curve::is_stdlib_type) {
409 result = GroupElement::batch_mul(commitments, scalars);
410 } else {
411 result = batch_mul_native<Curve>(commitments, scalars);
412 }
413
414 return { { z_challenge, evaluation }, result };
415 }
416
431 // TODO(https://github.com/AztecProtocol/barretenberg/issues/1475): Compute g1_identity inside the function body
433 {
434 commitments.emplace_back(g1_identity);
436
437 return { commitments, scalars, z_challenge };
438 }
439
447 template <typename Transcript>
449 std::shared_ptr<Transcript>& transcript)
450 {
451 // Initialize Shplonk verifier
452 const size_t num_claims = claims.size();
453 std::vector<Commitment> polynomial_commiments;
454 polynomial_commiments.reserve(num_claims);
455 for (const auto& claim : claims) {
456 polynomial_commiments.emplace_back(claim.commitment);
457 }
458 ShplonkVerifier_<Curve> verifier(polynomial_commiments, transcript, num_claims);
459
460 // Compute { 1 / (z - x_i) }
461 std::vector<Fr> inverse_vanishing_evals;
462 inverse_vanishing_evals.reserve(num_claims);
463 if constexpr (Curve::is_stdlib_type) {
464 for (const auto& claim : claims) {
465 inverse_vanishing_evals.emplace_back((verifier.z_challenge - claim.opening_pair.challenge).invert());
466 }
467 } else {
468 for (const auto& claim : claims) {
469 inverse_vanishing_evals.emplace_back(verifier.z_challenge - claim.opening_pair.challenge);
470 }
471 Fr::batch_invert(inverse_vanishing_evals);
472 }
473
474 // Update the Shplonk verifier state with each claim
475 // For each claim: s_i -= ν^i / (z - x_i) and θ += ν^i * v_i / (z - x_i)
476 for (size_t idx = 0; idx < claims.size(); idx++) {
477 // Compute ν^i / (z - x_i)
478 auto scalar_factor = verifier.pows_of_nu[idx] * inverse_vanishing_evals[idx];
479 // s_i -= ν^i / (z - x_i)
480 verifier.scalars[idx + 1] -= scalar_factor;
481 // θ += ν^i * v_i / (z - x_i)
482 verifier.identity_scalar_coefficient += scalar_factor * claims[idx].opening_pair.evaluation;
483 }
484
485 return verifier;
486 };
487
498 template <typename Transcript>
500 std::span<const OpeningClaim<Curve>> claims,
501 std::shared_ptr<Transcript>& transcript)
502 {
503 auto verifier = ShplonkVerifier_::reduce_verification_no_finalize(claims, transcript);
504 return verifier.finalize(g1_identity);
505 };
506
516 static std::vector<Fr> compute_inverted_gemini_denominators(const Fr& shplonk_eval_challenge,
517 const std::vector<Fr>& gemini_eval_challenge_powers)
518 {
519 std::vector<Fr> denominators;
520 const size_t virtual_log_n = gemini_eval_challenge_powers.size();
521 const size_t num_gemini_claims = 2 * virtual_log_n;
522 denominators.reserve(num_gemini_claims);
523
524 for (const auto& gemini_eval_challenge_power : gemini_eval_challenge_powers) {
525 // Place 1/(z - r ^ {2^j})
526 denominators.emplace_back(shplonk_eval_challenge - gemini_eval_challenge_power);
527 // Place 1/(z + r ^ {2^j})
528 denominators.emplace_back(shplonk_eval_challenge + gemini_eval_challenge_power);
529 }
530
531 if constexpr (!Curve::is_stdlib_type) {
532 Fr::batch_invert(denominators);
533 } else {
534 for (auto& denominator : denominators) {
535 denominator = denominator.invert();
536 }
537 }
538 return denominators;
539 }
540};
541
547template <typename Fr>
548static std::vector<Fr> compute_shplonk_batching_challenge_powers(const Fr& shplonk_batching_challenge,
549 const size_t virtual_log_n,
550 bool has_zk = false,
551 bool committed_sumcheck = false)
552{
553 // Minimum size of `denominators`
554 size_t num_powers = 2 * virtual_log_n + NUM_INTERLEAVING_CLAIMS;
555 // Each round univariate is opened at 0, 1, and a round challenge.
556 static constexpr size_t NUM_COMMITTED_SUMCHECK_CLAIMS_PER_ROUND = 3;
557
558 // Shplonk evaluation and batching challenges are re-used in SmallSubgroupIPA.
559 if (has_zk) {
560 num_powers += NUM_SMALL_IPA_EVALUATIONS;
561 }
562
563 // Commited sumcheck adds 3 claims per round.
564 if (committed_sumcheck) {
565 num_powers += NUM_COMMITTED_SUMCHECK_CLAIMS_PER_ROUND * virtual_log_n;
566 }
567
568 std::vector<Fr> result;
569 result.reserve(num_powers);
570 result.emplace_back(Fr{ 1 });
571 for (size_t idx = 1; idx < num_powers; idx++) {
572 result.emplace_back(result[idx - 1] * shplonk_batching_challenge);
573 }
574 return result;
575}
576} // namespace bb
#define BB_ASSERT_GT(left, right,...)
Definition assert.hpp:123
CommitmentKey object over a pairing group 𝔾₁.
Commitment commit(PolynomialSpan< const Fr > polynomial) const
Uses the ProverSRS to create a commitment to p(X)
Unverified claim (C,r,v) for some witness polynomial p(X) such that.
Definition claim.hpp:53
Structured polynomial class that represents the coefficients 'a' of a_0 + a_1 x .....
void add_scaled(PolynomialSpan< const Fr > other, const Fr &scaling_factor)
adds the polynomial q(X) 'other', multiplied by a scaling factor.
Fr & at(size_t index)
Our mutable accessor, unlike operator[]. We abuse precedent a bit to differentiate at() and operator[...
void factor_roots(const Fr &root)
Divides p(X) by (X-r) in-place. Assumes that p(rⱼ)=0 for all j.
Polynomial p and an opening pair (r,v) such that p(r) = v.
Definition claim.hpp:34
Shplonk Prover.
Definition shplonk.hpp:36
static std::vector< Fr > compute_gemini_fold_pos_evaluations(std::span< const ProverOpeningClaim< Curve > > opening_claims)
Compute evaluations of fold polynomials Fold_i at r^{2^i} for i>0. TODO(https://github....
Definition shplonk.hpp:239
static Polynomial compute_batched_quotient(const size_t virtual_log_n, std::span< const ProverOpeningClaim< Curve > > opening_claims, const Fr &nu, std::span< Fr > gemini_fold_pos_evaluations, std::span< const ProverOpeningClaim< Curve > > libra_opening_claims, std::span< const ProverOpeningClaim< Curve > > sumcheck_round_claims)
Compute batched quotient polynomial Q(X) = ∑ⱼ νʲ ⋅ ( fⱼ(X) − vⱼ) / ( X − xⱼ )
Definition shplonk.hpp:49
static ProverOpeningClaim< Curve > prove(const CommitmentKey< Curve > &commitment_key, std::span< ProverOpeningClaim< Curve > > opening_claims, const std::shared_ptr< Transcript > &transcript, std::span< ProverOpeningClaim< Curve > > libra_opening_claims={}, std::span< ProverOpeningClaim< Curve > > sumcheck_round_claims={}, const size_t virtual_log_n=0)
Returns a batched opening claim equivalent to a set of opening claims consisting of polynomials,...
Definition shplonk.hpp:267
typename Curve::ScalarField Fr
Definition shplonk.hpp:37
static ProverOpeningClaim< Curve > compute_partially_evaluated_batched_quotient(const size_t virtual_log_n, std::span< ProverOpeningClaim< Curve > > opening_claims, Polynomial &batched_quotient_Q, const Fr &nu_challenge, const Fr &z_challenge, std::span< Fr > gemini_fold_pos_evaluations, std::span< ProverOpeningClaim< Curve > > libra_opening_claims={}, std::span< ProverOpeningClaim< Curve > > sumcheck_opening_claims={})
Compute partially evaluated batched quotient polynomial difference Q(X) - Q_z(X)
Definition shplonk.hpp:139
bb::Polynomial< Fr > Polynomial
Definition shplonk.hpp:38
Shplonk Verifier.
Definition shplonk.hpp:343
std::vector< Fr > pows_of_nu
Definition shplonk.hpp:350
typename Curve::ScalarField Fr
Definition shplonk.hpp:344
BatchOpeningClaim< Curve > export_batch_opening_claim(const Commitment &g1_identity)
Export a BatchOpeningClaim instead of performing final batch_mul.
Definition shplonk.hpp:432
static OpeningClaim< Curve > reduce_verification(Commitment g1_identity, std::span< const OpeningClaim< Curve > > claims, std::shared_ptr< Transcript > &transcript)
Recomputes the new claim commitment [G] given the proof and the challenge r. No verification happens ...
Definition shplonk.hpp:499
ShplonkVerifier_(std::vector< Commitment > &polynomial_commitments, std::shared_ptr< Transcript > &transcript, const size_t num_claims)
Definition shplonk.hpp:367
std::vector< Commitment > commitments
Definition shplonk.hpp:356
typename Curve::AffineElement Commitment
Definition shplonk.hpp:346
static std::vector< Fr > compute_inverted_gemini_denominators(const Fr &shplonk_eval_challenge, const std::vector< Fr > &gemini_eval_challenge_powers)
Computes .
Definition shplonk.hpp:516
static ShplonkVerifier_< Curve > reduce_verification_no_finalize(std::span< const OpeningClaim< Curve > > claims, std::shared_ptr< Transcript > &transcript)
Instantiate a Shplonk verifier and update its state with the provided claims.
Definition shplonk.hpp:448
typename Curve::Element GroupElement
Definition shplonk.hpp:345
OpeningClaim< Curve > finalize(const Commitment &g1_identity)
Finalize the Shplonk verification and return the KZG opening claim.
Definition shplonk.hpp:403
std::vector< Fr > scalars
Definition shplonk.hpp:359
Commitment quotient
Definition shplonk.hpp:352
Representation of the Grumpkin Verifier Commitment Key inside a bn254 circuit.
typename Group::element Element
Definition grumpkin.hpp:62
static constexpr bool is_stdlib_type
Definition grumpkin.hpp:69
typename Group::affine_element AffineElement
Definition grumpkin.hpp:63
#define G(r, i, a, b, c, d)
Definition blake2s.cpp:116
constexpr T round_up_power_2(const T in)
Definition get_msb.hpp:52
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
An accumulator consisting of the Shplonk evaluation challenge and vectors of commitments and scalars.
Definition claim.hpp:151
static constexpr field one()
BB_INLINE constexpr field pow(const uint256_t &exponent) const noexcept
static void batch_invert(C &coeffs) noexcept
static constexpr field zero()