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 // Q(X) = ∑ⱼ νʲ ⋅ ( fⱼ(X) − vⱼ) / ( X − xⱼ )
65 Polynomial Q(max_poly_size);
66 Polynomial tmp(max_poly_size);
67
68 Fr current_nu = Fr::one();
69
70 size_t fold_idx = 0;
71 for (const auto& claim : opening_claims) {
72
73 // Gemini Fold Polynomials have to be opened at -r^{2^j} and r^{2^j}.
74 if (claim.gemini_fold) {
75 tmp = claim.polynomial;
76 tmp.at(0) = tmp[0] - gemini_fold_pos_evaluations[fold_idx++];
77 tmp.factor_roots(-claim.opening_pair.challenge);
78 // Add the claim quotient to the batched quotient polynomial
79 Q.add_scaled(tmp, current_nu);
80 current_nu *= nu;
81 }
82
83 // Compute individual claim quotient tmp = ( fⱼ(X) − vⱼ) / ( X − xⱼ )
84 tmp = claim.polynomial;
85 tmp.at(0) = tmp[0] - claim.opening_pair.evaluation;
86 tmp.factor_roots(claim.opening_pair.challenge);
87 // Add the claim quotient to the batched quotient polynomial
88 Q.add_scaled(tmp, current_nu);
89 current_nu *= nu;
90 }
91 // We use the same batching challenge for Gemini and Libra opening claims. The number of the claims
92 // batched before adding Libra commitments and evaluations is bounded by 2 * `virtual_log_n`,
93 // which is the number of fold claims including the dummy ones.
94 if (!libra_opening_claims.empty()) {
95 current_nu = nu.pow(2 * virtual_log_n);
96 }
97
98 for (const auto& claim : libra_opening_claims) {
99 // Compute individual claim quotient tmp = ( fⱼ(X) − vⱼ) / ( X − xⱼ )
100 tmp = claim.polynomial;
101 tmp.at(0) = tmp[0] - claim.opening_pair.evaluation;
102 tmp.factor_roots(claim.opening_pair.challenge);
103
104 // Add the claim quotient to the batched quotient polynomial
105 Q.add_scaled(tmp, current_nu);
106 current_nu *= nu;
107 }
108
109 for (const auto& claim : sumcheck_round_claims) {
110
111 // Compute individual claim quotient tmp = ( fⱼ(X) − vⱼ) / ( X − xⱼ )
112 tmp = claim.polynomial;
113 tmp.at(0) = tmp[0] - claim.opening_pair.evaluation;
114 tmp.factor_roots(claim.opening_pair.challenge);
115
116 // Add the claim quotient to the batched quotient polynomial
117 Q.add_scaled(tmp, current_nu);
118 current_nu *= nu;
119 }
120 // Return batched quotient polynomial Q(X)
121 return Q;
122 };
123
135 const size_t virtual_log_n,
136 std::span<ProverOpeningClaim<Curve>> opening_claims,
137 Polynomial& batched_quotient_Q,
138 const Fr& nu_challenge,
139 const Fr& z_challenge,
140 std::span<Fr> gemini_fold_pos_evaluations,
141 std::span<ProverOpeningClaim<Curve>> libra_opening_claims = {},
142 std::span<ProverOpeningClaim<Curve>> sumcheck_opening_claims = {})
143 {
144 // Our main use case is the opening of Gemini fold polynomials and each Gemini fold is opened at 2 points.
145 const size_t num_gemini_opening_claims = 2 * opening_claims.size();
146 const size_t num_opening_claims =
147 num_gemini_opening_claims + libra_opening_claims.size() + sumcheck_opening_claims.size();
148
149 // {ẑⱼ(z)}ⱼ , where ẑⱼ(r) = 1/zⱼ(z) = 1/(z - xⱼ)
150 std::vector<Fr> inverse_vanishing_evals;
151 inverse_vanishing_evals.reserve(num_opening_claims);
152 for (const auto& claim : opening_claims) {
153 if (claim.gemini_fold) {
154 inverse_vanishing_evals.emplace_back(z_challenge + claim.opening_pair.challenge);
155 }
156 inverse_vanishing_evals.emplace_back(z_challenge - claim.opening_pair.challenge);
157 }
158
159 // Add the terms (z - uₖ) for k = 0, …, d−1 where d is the number of rounds in Sumcheck
160 for (const auto& claim : libra_opening_claims) {
161 inverse_vanishing_evals.emplace_back(z_challenge - claim.opening_pair.challenge);
162 }
163
164 for (const auto& claim : sumcheck_opening_claims) {
165 inverse_vanishing_evals.emplace_back(z_challenge - claim.opening_pair.challenge);
166 }
167
168 Fr::batch_invert(inverse_vanishing_evals);
169
170 // G(X) = Q(X) - Q_z(X) = Q(X) - ∑ⱼ νʲ ⋅ ( fⱼ(X) − vⱼ) / ( z − xⱼ ),
171 // s.t. G(r) = 0
172 Polynomial G(std::move(batched_quotient_Q)); // G(X) = Q(X)
173
174 // G₀ = ∑ⱼ νʲ ⋅ vⱼ / ( z − xⱼ )
175 Fr current_nu = Fr::one();
176 Polynomial tmp(G.size());
177 size_t idx = 0;
178
179 size_t fold_idx = 0;
180 for (auto& claim : opening_claims) {
181
182 if (claim.gemini_fold) {
183 tmp = claim.polynomial;
184 tmp.at(0) = tmp[0] - gemini_fold_pos_evaluations[fold_idx++];
185 Fr scaling_factor = current_nu * inverse_vanishing_evals[idx++]; // = νʲ / (z − xⱼ )
186 // G -= νʲ ⋅ ( fⱼ(X) − vⱼ) / ( z − xⱼ )
187 G.add_scaled(tmp, -scaling_factor);
188
189 current_nu *= nu_challenge;
190 }
191 // tmp = νʲ ⋅ ( fⱼ(X) − vⱼ) / ( z − xⱼ )
192 claim.polynomial.at(0) = claim.polynomial[0] - claim.opening_pair.evaluation;
193 Fr scaling_factor = current_nu * inverse_vanishing_evals[idx++]; // = νʲ / (z − xⱼ )
194
195 // G -= νʲ ⋅ ( fⱼ(X) − vⱼ) / ( z − xⱼ )
196 G.add_scaled(claim.polynomial, -scaling_factor);
197
198 current_nu *= nu_challenge;
199 }
200
201 // Take into account the constant proof size in Gemini
202 if (!libra_opening_claims.empty()) {
203 current_nu = nu_challenge.pow(2 * virtual_log_n);
204 }
205
206 for (auto& claim : libra_opening_claims) {
207 // Compute individual claim quotient tmp = ( fⱼ(X) − vⱼ) / ( X − xⱼ )
208 claim.polynomial.at(0) = claim.polynomial[0] - claim.opening_pair.evaluation;
209 Fr scaling_factor = current_nu * inverse_vanishing_evals[idx++]; // = νʲ / (z − xⱼ )
210
211 // Add the claim quotient to the batched quotient polynomial
212 G.add_scaled(claim.polynomial, -scaling_factor);
213 current_nu *= nu_challenge;
214 }
215
216 for (auto& claim : sumcheck_opening_claims) {
217 claim.polynomial.at(0) = claim.polynomial[0] - claim.opening_pair.evaluation;
218 Fr scaling_factor = current_nu * inverse_vanishing_evals[idx++]; // = νʲ / (z − xⱼ )
219
220 // Add the claim quotient to the batched quotient polynomial
221 G.add_scaled(claim.polynomial, -scaling_factor);
222 current_nu *= nu_challenge;
223 }
224 // Return opening pair (z, 0) and polynomial G(X) = Q(X) - Q_z(X)
225 return { .polynomial = G, .opening_pair = { .challenge = z_challenge, .evaluation = Fr::zero() } };
226 };
235 std::span<const ProverOpeningClaim<Curve>> opening_claims)
236 {
237 std::vector<Fr> gemini_fold_pos_evaluations;
238 gemini_fold_pos_evaluations.reserve(opening_claims.size());
239
240 for (const auto& claim : opening_claims) {
241 if (claim.gemini_fold) {
242 // -r^{2^i} is stored in the claim
243 const Fr evaluation_point = -claim.opening_pair.challenge;
244 // Compute Fold_i(r^{2^i})
245 const Fr evaluation = claim.polynomial.evaluate(evaluation_point);
246 gemini_fold_pos_evaluations.emplace_back(evaluation);
247 }
248 }
249 return gemini_fold_pos_evaluations;
250 }
251
261 template <typename Transcript>
263 std::span<ProverOpeningClaim<Curve>> opening_claims,
264 const std::shared_ptr<Transcript>& transcript,
265 std::span<ProverOpeningClaim<Curve>> libra_opening_claims = {},
266 std::span<ProverOpeningClaim<Curve>> sumcheck_round_claims = {},
267 const size_t virtual_log_n = 0)
268 {
269 const Fr nu = transcript->template get_challenge<Fr>("Shplonk:nu");
270
271 // Compute the evaluations Fold_i(r^{2^i}) for i>0.
272 std::vector<Fr> gemini_fold_pos_evaluations = compute_gemini_fold_pos_evaluations(opening_claims);
273
274 auto batched_quotient = compute_batched_quotient(virtual_log_n,
275 opening_claims,
276 nu,
277 gemini_fold_pos_evaluations,
278 libra_opening_claims,
279 sumcheck_round_claims);
280 auto batched_quotient_commitment = commitment_key.commit(batched_quotient);
281 transcript->send_to_verifier("Shplonk:Q", batched_quotient_commitment);
282 const Fr z = transcript->template get_challenge<Fr>("Shplonk:z");
283
285 opening_claims,
286 batched_quotient,
287 nu,
288 z,
289 gemini_fold_pos_evaluations,
290 libra_opening_claims,
291 sumcheck_round_claims);
292 }
293};
294
338template <typename Curve> class ShplonkVerifier_ {
339 using Fr = typename Curve::ScalarField;
340 using GroupElement = typename Curve::Element;
343
344 // Random challenges
345 std::vector<Fr> pows_of_nu;
346 // Commitment to quotient polynomial
348 // Partial evaluation challenge
350 // Commitments \f$[f_1], \dots, [f_n]\f$
351 std::vector<Commitment> commitments;
352 // Scalar coefficients of \f$[f_1], \dots, [f_n]\f$ in the MSM needed to compute the commitment to the partially
353 // evaluated quotient
354 std::vector<Fr> scalars;
355 // Coefficient of the identity in partially evaluated quotient
357 // Target evaluation
359
360 public:
361 template <typename Transcript>
362 ShplonkVerifier_(std::vector<Commitment>& polynomial_commitments,
363 std::shared_ptr<Transcript>& transcript,
364 const size_t num_claims)
365 : pows_of_nu({ Fr(1), transcript->template get_challenge<Fr>("Shplonk:nu") })
366 , quotient(transcript->template receive_from_prover<Commitment>("Shplonk:Q"))
367 , z_challenge(transcript->template get_challenge<Fr>("Shplonk:z"))
368 , commitments({ quotient })
369 , scalars{ Fr{ 1 } }
370 {
371 BB_ASSERT_GT(num_claims, 1U, "Using Shplonk with just one claim. Should use batch reduction.");
372 const size_t num_commitments = commitments.size();
373 commitments.reserve(num_commitments);
374 scalars.reserve(num_commitments);
375 pows_of_nu.reserve(num_claims);
376
377 commitments.insert(commitments.end(), polynomial_commitments.begin(), polynomial_commitments.end());
378 scalars.insert(scalars.end(), commitments.size() - 1, Fr(0)); // Initialized as circuit constants
379 // The first two powers of nu have already been initialized, we need another `num_claims - 2` powers to batch
380 // all the claims
381 for (size_t idx = 0; idx < num_claims - 2; idx++) {
382 pows_of_nu.emplace_back(pows_of_nu.back() * pows_of_nu[1]);
383 }
384
385 if constexpr (Curve::is_stdlib_type) {
386 evaluation.convert_constant_to_fixed_witness(pows_of_nu[1].get_context());
387 }
388 }
389
399 {
400 commitments.emplace_back(g1_identity);
402 GroupElement result = GroupElement::batch_mul(commitments, scalars);
403
404 return { { z_challenge, evaluation }, result };
405 }
406
421 // TODO(https://github.com/AztecProtocol/barretenberg/issues/1475): Compute g1_identity inside the function body
423 {
424 commitments.emplace_back(g1_identity);
426
427 return { commitments, scalars, z_challenge };
428 }
429
437 template <typename Transcript>
439 std::shared_ptr<Transcript>& transcript)
440 {
441 // Initialize Shplonk verifier
442 const size_t num_claims = claims.size();
443 std::vector<Commitment> polynomial_commiments;
444 polynomial_commiments.reserve(num_claims);
445 for (const auto& claim : claims) {
446 polynomial_commiments.emplace_back(claim.commitment);
447 }
448 ShplonkVerifier_<Curve> verifier(polynomial_commiments, transcript, num_claims);
449
450 // Compute { 1 / (z - x_i) }
451 std::vector<Fr> inverse_vanishing_evals;
452 inverse_vanishing_evals.reserve(num_claims);
453 if constexpr (Curve::is_stdlib_type) {
454 for (const auto& claim : claims) {
455 inverse_vanishing_evals.emplace_back((verifier.z_challenge - claim.opening_pair.challenge).invert());
456 }
457 } else {
458 for (const auto& claim : claims) {
459 inverse_vanishing_evals.emplace_back(verifier.z_challenge - claim.opening_pair.challenge);
460 }
461 Fr::batch_invert(inverse_vanishing_evals);
462 }
463
464 // Update the Shplonk verifier state with each claim
465 // For each claim: s_i -= ν^i / (z - x_i) and θ += ν^i * v_i / (z - x_i)
466 for (size_t idx = 0; idx < claims.size(); idx++) {
467 // Compute ν^i / (z - x_i)
468 auto scalar_factor = verifier.pows_of_nu[idx] * inverse_vanishing_evals[idx];
469 // s_i -= ν^i / (z - x_i)
470 verifier.scalars[idx + 1] -= scalar_factor;
471 // θ += ν^i * v_i / (z - x_i)
472 verifier.identity_scalar_coefficient += scalar_factor * claims[idx].opening_pair.evaluation;
473 }
474
475 return verifier;
476 };
477
488 template <typename Transcript>
490 std::span<const OpeningClaim<Curve>> claims,
491 std::shared_ptr<Transcript>& transcript)
492 {
493 auto verifier = ShplonkVerifier_::reduce_verification_no_finalize(claims, transcript);
494 return verifier.finalize(g1_identity);
495 };
496
506 static std::vector<Fr> compute_inverted_gemini_denominators(const Fr& shplonk_eval_challenge,
507 const std::vector<Fr>& gemini_eval_challenge_powers)
508 {
509 std::vector<Fr> denominators;
510 const size_t virtual_log_n = gemini_eval_challenge_powers.size();
511 const size_t num_gemini_claims = 2 * virtual_log_n;
512 denominators.reserve(num_gemini_claims);
513
514 for (const auto& gemini_eval_challenge_power : gemini_eval_challenge_powers) {
515 // Place 1/(z - r ^ {2^j})
516 denominators.emplace_back(shplonk_eval_challenge - gemini_eval_challenge_power);
517 // Place 1/(z + r ^ {2^j})
518 denominators.emplace_back(shplonk_eval_challenge + gemini_eval_challenge_power);
519 }
520
521 if constexpr (!Curve::is_stdlib_type) {
522 Fr::batch_invert(denominators);
523 } else {
524 for (auto& denominator : denominators) {
525 denominator = denominator.invert();
526 }
527 }
528 return denominators;
529 }
530};
531
537template <typename Fr>
538static std::vector<Fr> compute_shplonk_batching_challenge_powers(const Fr& shplonk_batching_challenge,
539 const size_t virtual_log_n,
540 bool has_zk = false,
541 bool committed_sumcheck = false)
542{
543 // Minimum number of powers: 2 * virtual_log_n for the Gemini fold claims
544 size_t num_powers = 2 * virtual_log_n;
545 // Each round univariate is opened at 0, 1, and a round challenge.
546 static constexpr size_t NUM_COMMITTED_SUMCHECK_CLAIMS_PER_ROUND = 3;
547
548 // Shplonk evaluation and batching challenges are re-used in SmallSubgroupIPA.
549 if (has_zk) {
550 num_powers += NUM_SMALL_IPA_EVALUATIONS;
551 }
552
553 // Commited sumcheck adds 3 claims per round.
554 if (committed_sumcheck) {
555 num_powers += NUM_COMMITTED_SUMCHECK_CLAIMS_PER_ROUND * virtual_log_n;
556 }
557
558 std::vector<Fr> result;
559 result.reserve(num_powers);
560 result.emplace_back(Fr{ 1 });
561 for (size_t idx = 1; idx < num_powers; idx++) {
562 result.emplace_back(result[idx - 1] * shplonk_batching_challenge);
563 }
564 return result;
565}
566} // namespace bb
#define BB_ASSERT_GT(left, right,...)
Definition assert.hpp:113
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:55
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:36
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:234
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:262
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:134
bb::Polynomial< Fr > Polynomial
Definition shplonk.hpp:38
Shplonk Verifier.
Definition shplonk.hpp:338
std::vector< Fr > pows_of_nu
Definition shplonk.hpp:345
typename Curve::ScalarField Fr
Definition shplonk.hpp:339
BatchOpeningClaim< Curve > export_batch_opening_claim(const Commitment &g1_identity)
Export a BatchOpeningClaim instead of performing final batch_mul.
Definition shplonk.hpp:422
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:489
ShplonkVerifier_(std::vector< Commitment > &polynomial_commitments, std::shared_ptr< Transcript > &transcript, const size_t num_claims)
Definition shplonk.hpp:362
std::vector< Commitment > commitments
Definition shplonk.hpp:351
typename Curve::AffineElement Commitment
Definition shplonk.hpp:341
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:506
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:438
typename Curve::Element GroupElement
Definition shplonk.hpp:340
OpeningClaim< Curve > finalize(const Commitment &g1_identity)
Finalize the Shplonk verification and return the KZG opening claim.
Definition shplonk.hpp:398
std::vector< Fr > scalars
Definition shplonk.hpp:354
Commitment quotient
Definition shplonk.hpp:347
Representation of the Grumpkin Verifier Commitment Key inside a bn254 circuit.
typename Group::element Element
Definition grumpkin.hpp:64
static constexpr bool is_stdlib_type
Definition grumpkin.hpp:68
typename Group::affine_element AffineElement
Definition grumpkin.hpp:65
#define G(r, i, a, b, c, d)
Definition blake2s.cpp:116
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:155
static constexpr field one()
BB_INLINE constexpr field pow(const uint256_t &exponent) const noexcept
static void batch_invert(C &coeffs) noexcept
Batch invert a collection of field elements using Montgomery's trick.
static constexpr field zero()