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
15
30namespace bb {
31
37template <typename Curve> class ShplonkProver_ {
38 using Fr = typename Curve::ScalarField;
40
41 public:
50 static Polynomial compute_batched_quotient(const size_t virtual_log_n,
51 std::span<const ProverOpeningClaim<Curve>> opening_claims,
52 const Fr& nu,
53 std::span<Fr> gemini_fold_pos_evaluations,
54 std::span<const ProverOpeningClaim<Curve>> libra_opening_claims,
55 std::span<const ProverOpeningClaim<Curve>> sumcheck_round_claims)
56 {
57 // Find the maximum polynomial size among all claims to determine the dyadic size of the batched polynomial.
58 size_t max_poly_size{ 0 };
59
60 for (const auto& claim_set : { opening_claims, libra_opening_claims, sumcheck_round_claims }) {
61 for (const auto& claim : claim_set) {
62 max_poly_size = std::max(max_poly_size, claim.polynomial.size());
63 }
64 }
65 // Q(X) = ∑ⱼ νʲ ⋅ ( fⱼ(X) − vⱼ) / ( X − xⱼ )
66 Polynomial Q(max_poly_size);
67 Polynomial tmp(max_poly_size);
68
69 Fr current_nu = Fr::one();
70
71 size_t fold_idx = 0;
72 for (const auto& claim : opening_claims) {
73
74 // Gemini Fold Polynomials have to be opened at -r^{2^j} and r^{2^j}.
75 if (claim.gemini_fold) {
76 tmp = claim.polynomial;
77 tmp.at(0) = tmp[0] - gemini_fold_pos_evaluations[fold_idx++];
78 tmp.factor_roots(-claim.opening_pair.challenge);
79 // Add the claim quotient to the batched quotient polynomial
80 Q.add_scaled(tmp, current_nu);
81 current_nu *= nu;
82 }
83
84 // Compute individual claim quotient tmp = ( fⱼ(X) − vⱼ) / ( X − xⱼ )
85 tmp = claim.polynomial;
86 tmp.at(0) = tmp[0] - claim.opening_pair.evaluation;
87 tmp.factor_roots(claim.opening_pair.challenge);
88 // Add the claim quotient to the batched quotient polynomial
89 Q.add_scaled(tmp, current_nu);
90 current_nu *= nu;
91 }
92 // We use the same batching challenge for Gemini and Libra opening claims. The number of the claims
93 // batched before adding Libra commitments and evaluations is bounded by 2 * `virtual_log_n`,
94 // which is the number of fold claims including the dummy ones.
95 if (!libra_opening_claims.empty()) {
96 current_nu = nu.pow(2 * virtual_log_n);
97 }
98
99 for (const auto& claim : libra_opening_claims) {
100 // Compute individual claim quotient tmp = ( fⱼ(X) − vⱼ) / ( X − xⱼ )
101 tmp = claim.polynomial;
102 tmp.at(0) = tmp[0] - claim.opening_pair.evaluation;
103 tmp.factor_roots(claim.opening_pair.challenge);
104
105 // Add the claim quotient to the batched quotient polynomial
106 Q.add_scaled(tmp, current_nu);
107 current_nu *= nu;
108 }
109
110 for (const auto& claim : sumcheck_round_claims) {
111
112 // Compute individual claim quotient tmp = ( fⱼ(X) − vⱼ) / ( X − xⱼ )
113 tmp = claim.polynomial;
114 tmp.at(0) = tmp[0] - claim.opening_pair.evaluation;
115 tmp.factor_roots(claim.opening_pair.challenge);
116
117 // Add the claim quotient to the batched quotient polynomial
118 Q.add_scaled(tmp, current_nu);
119 current_nu *= nu;
120 }
121 // Return batched quotient polynomial Q(X)
122 return Q;
123 };
124
136 const size_t virtual_log_n,
137 std::span<ProverOpeningClaim<Curve>> opening_claims,
138 Polynomial& batched_quotient_Q,
139 const Fr& nu_challenge,
140 const Fr& z_challenge,
141 std::span<Fr> gemini_fold_pos_evaluations,
142 std::span<ProverOpeningClaim<Curve>> libra_opening_claims = {},
143 std::span<ProverOpeningClaim<Curve>> sumcheck_opening_claims = {})
144 {
145 // Our main use case is the opening of Gemini fold polynomials and each Gemini fold is opened at 2 points.
146 const size_t num_gemini_opening_claims = 2 * opening_claims.size();
147 const size_t num_opening_claims =
148 num_gemini_opening_claims + libra_opening_claims.size() + sumcheck_opening_claims.size();
149
150 // {ẑⱼ(z)}ⱼ , where ẑⱼ(r) = 1/zⱼ(z) = 1/(z - xⱼ)
151 std::vector<Fr> inverse_vanishing_evals;
152 inverse_vanishing_evals.reserve(num_opening_claims);
153 for (const auto& claim : opening_claims) {
154 if (claim.gemini_fold) {
155 inverse_vanishing_evals.emplace_back(z_challenge + claim.opening_pair.challenge);
156 }
157 inverse_vanishing_evals.emplace_back(z_challenge - claim.opening_pair.challenge);
158 }
159
160 // Add the terms (z - uₖ) for k = 0, …, d−1 where d is the number of rounds in Sumcheck
161 for (const auto& claim : libra_opening_claims) {
162 inverse_vanishing_evals.emplace_back(z_challenge - claim.opening_pair.challenge);
163 }
164
165 for (const auto& claim : sumcheck_opening_claims) {
166 inverse_vanishing_evals.emplace_back(z_challenge - claim.opening_pair.challenge);
167 }
168
169 Fr::batch_invert(inverse_vanishing_evals);
170
171 // G(X) = Q(X) - Q_z(X) = Q(X) - ∑ⱼ νʲ ⋅ ( fⱼ(X) − vⱼ) / ( z − xⱼ ),
172 // s.t. G(r) = 0
173 Polynomial G(std::move(batched_quotient_Q)); // G(X) = Q(X)
174
175 Fr current_nu = Fr::one();
176 size_t idx = 0;
177
178 size_t fold_idx = 0;
179 for (const auto& claim : opening_claims) {
180
181 if (claim.gemini_fold) {
182 // G -= νʲ ⋅ ( fⱼ(X) − vⱼ₊) / ( z + xⱼ ), where vⱼ₊ is the positive fold evaluation
183 Fr scaling_factor = current_nu * inverse_vanishing_evals[idx++]; // = νʲ / (z + xⱼ )
184 G.add_scaled(claim.polynomial, -scaling_factor);
185 G.at(0) = G[0] + scaling_factor * gemini_fold_pos_evaluations[fold_idx++];
186
187 current_nu *= nu_challenge;
188 }
189 // G -= νʲ ⋅ ( fⱼ(X) − vⱼ) / ( z − xⱼ )
190 Fr scaling_factor = current_nu * inverse_vanishing_evals[idx++]; // = νʲ / (z − xⱼ )
191 G.add_scaled(claim.polynomial, -scaling_factor);
192 G.at(0) = G[0] + scaling_factor * claim.opening_pair.evaluation;
193
194 current_nu *= nu_challenge;
195 }
196
197 // Take into account the constant proof size in Gemini
198 if (!libra_opening_claims.empty()) {
199 current_nu = nu_challenge.pow(2 * virtual_log_n);
200 }
201
202 for (const auto& claim : libra_opening_claims) {
203 // G -= νʲ ⋅ ( fⱼ(X) − vⱼ) / ( z − xⱼ )
204 Fr scaling_factor = current_nu * inverse_vanishing_evals[idx++]; // = νʲ / (z − xⱼ )
205 G.add_scaled(claim.polynomial, -scaling_factor);
206 G.at(0) = G[0] + scaling_factor * claim.opening_pair.evaluation;
207 current_nu *= nu_challenge;
208 }
209
210 for (const auto& claim : sumcheck_opening_claims) {
211 Fr scaling_factor = current_nu * inverse_vanishing_evals[idx++]; // = νʲ / (z − xⱼ )
212 G.add_scaled(claim.polynomial, -scaling_factor);
213 G.at(0) = G[0] + scaling_factor * claim.opening_pair.evaluation;
214 current_nu *= nu_challenge;
215 }
216 // Return opening pair (z, 0) and polynomial G(X) = Q(X) - Q_z(X)
217 return { .polynomial = G, .opening_pair = { .challenge = z_challenge, .evaluation = Fr::zero() } };
218 };
227 std::span<const ProverOpeningClaim<Curve>> opening_claims)
228 {
229 std::vector<Fr> gemini_fold_pos_evaluations;
230 gemini_fold_pos_evaluations.reserve(opening_claims.size());
231
232 for (const auto& claim : opening_claims) {
233 if (claim.gemini_fold) {
234 // -r^{2^i} is stored in the claim
235 const Fr evaluation_point = -claim.opening_pair.challenge;
236 // Compute Fold_i(r^{2^i})
237 const Fr evaluation = claim.polynomial.evaluate(evaluation_point);
238 gemini_fold_pos_evaluations.emplace_back(evaluation);
239 }
240 }
241 return gemini_fold_pos_evaluations;
242 }
243
253 template <typename Transcript>
255 std::span<ProverOpeningClaim<Curve>> opening_claims,
256 const std::shared_ptr<Transcript>& transcript,
257 std::span<ProverOpeningClaim<Curve>> libra_opening_claims = {},
258 std::span<ProverOpeningClaim<Curve>> sumcheck_round_claims = {},
259 const size_t virtual_log_n = 0)
260 {
261 BB_BENCH_NAME("ShplonkProver::prove");
262 const Fr nu = transcript->template get_challenge<Fr>("Shplonk:nu");
263
264 // Compute the evaluations Fold_i(r^{2^i}) for i>0.
265 std::vector<Fr> gemini_fold_pos_evaluations = compute_gemini_fold_pos_evaluations(opening_claims);
266
267 auto batched_quotient = compute_batched_quotient(virtual_log_n,
268 opening_claims,
269 nu,
270 gemini_fold_pos_evaluations,
271 libra_opening_claims,
272 sumcheck_round_claims);
273 auto batched_quotient_commitment = commitment_key.commit(batched_quotient);
274 transcript->send_to_verifier("Shplonk:Q", batched_quotient_commitment);
275 const Fr z = transcript->template get_challenge<Fr>("Shplonk:z");
276
278 opening_claims,
279 batched_quotient,
280 nu,
281 z,
282 gemini_fold_pos_evaluations,
283 libra_opening_claims,
284 sumcheck_round_claims);
285 }
286};
287
331template <typename Curve> class ShplonkVerifier_ {
332 using Fr = typename Curve::ScalarField;
333 using GroupElement = typename Curve::Element;
336
337 // Random challenges
338 std::vector<Fr> pows_of_nu;
339 // Commitment to quotient polynomial
341 // Partial evaluation challenge
343 // Commitments \f$[f_1], \dots, [f_n]\f$
344 std::vector<Commitment> commitments;
345 // Scalar coefficients of \f$[f_1], \dots, [f_n]\f$ in the MSM needed to compute the commitment to the partially
346 // evaluated quotient
347 std::vector<Fr> scalars;
348 // Coefficient of the identity in partially evaluated quotient
350 // Target evaluation
352
353 public:
354 template <typename Transcript>
355 ShplonkVerifier_(std::vector<Commitment>& polynomial_commitments,
356 std::shared_ptr<Transcript>& transcript,
357 const size_t num_claims)
358 : pows_of_nu({ Fr(1), transcript->template get_challenge<Fr>("Shplonk:nu") })
359 , quotient(transcript->template receive_from_prover<Commitment>("Shplonk:Q"))
360 , z_challenge(transcript->template get_challenge<Fr>("Shplonk:z"))
361 , commitments({ quotient })
362 , scalars{ Fr{ 1 } }
363 {
364 if (num_claims <= 1U) {
365 throw_or_abort("Using Shplonk with just one claim. Should use batch reduction.");
366 }
367 const size_t num_commitments = commitments.size();
368 commitments.reserve(num_commitments);
369 scalars.reserve(num_commitments);
370 pows_of_nu.reserve(num_claims);
371
372 commitments.insert(commitments.end(), polynomial_commitments.begin(), polynomial_commitments.end());
373 scalars.insert(scalars.end(), commitments.size() - 1, Fr(0)); // Initialized as circuit constants
374 // The first two powers of nu have already been initialized, we need another `num_claims - 2` powers to batch
375 // all the claims
376 for (size_t idx = 0; idx < num_claims - 2; idx++) {
377 pows_of_nu.emplace_back(pows_of_nu.back() * pows_of_nu[1]);
378 }
379
380 if constexpr (Curve::is_stdlib_type) {
381 evaluation.convert_constant_to_fixed_witness(pows_of_nu[1].get_context());
382 }
383 }
384
394 {
395 commitments.emplace_back(g1_identity);
397 GroupElement result = GroupElement::batch_mul(commitments, scalars);
398
399 return { { z_challenge, evaluation }, result };
400 }
401
416 // TODO(https://github.com/AztecProtocol/barretenberg/issues/1475): Compute g1_identity inside the function body
418 {
419 commitments.emplace_back(g1_identity);
421
422 return { commitments, scalars, z_challenge };
423 }
424
432 template <typename Transcript>
434 std::shared_ptr<Transcript>& transcript)
435 {
436 // Initialize Shplonk verifier
437 const size_t num_claims = claims.size();
438 std::vector<Commitment> polynomial_commiments;
439 polynomial_commiments.reserve(num_claims);
440 for (const auto& claim : claims) {
441 polynomial_commiments.emplace_back(claim.commitment);
442 }
443 ShplonkVerifier_<Curve> verifier(polynomial_commiments, transcript, num_claims);
444
445 // Compute { 1 / (z - x_i) }
446 std::vector<Fr> inverse_vanishing_evals;
447 inverse_vanishing_evals.reserve(num_claims);
448 if constexpr (Curve::is_stdlib_type) {
449 for (const auto& claim : claims) {
450 inverse_vanishing_evals.emplace_back((verifier.z_challenge - claim.opening_pair.challenge).invert());
451 }
452 } else {
453 for (const auto& claim : claims) {
454 inverse_vanishing_evals.emplace_back(verifier.z_challenge - claim.opening_pair.challenge);
455 }
456 Fr::batch_invert(inverse_vanishing_evals);
457 }
458
459 // Update the Shplonk verifier state with each claim
460 // For each claim: s_i -= ν^i / (z - x_i) and θ += ν^i * v_i / (z - x_i)
461 for (size_t idx = 0; idx < claims.size(); idx++) {
462 // Compute ν^i / (z - x_i)
463 auto scalar_factor = verifier.pows_of_nu[idx] * inverse_vanishing_evals[idx];
464 // s_i -= ν^i / (z - x_i)
465 verifier.scalars[idx + 1] -= scalar_factor;
466 // θ += ν^i * v_i / (z - x_i)
467 verifier.identity_scalar_coefficient += scalar_factor * claims[idx].opening_pair.evaluation;
468 }
469
470 return verifier;
471 };
472
483 template <typename Transcript>
485 std::span<const OpeningClaim<Curve>> claims,
486 std::shared_ptr<Transcript>& transcript)
487 {
488 auto verifier = ShplonkVerifier_::reduce_verification_no_finalize(claims, transcript);
489 return verifier.finalize(g1_identity);
490 };
491
501 static std::vector<Fr> compute_inverted_gemini_denominators(const Fr& shplonk_eval_challenge,
502 const std::vector<Fr>& gemini_eval_challenge_powers)
503 {
504 std::vector<Fr> denominators;
505 const size_t virtual_log_n = gemini_eval_challenge_powers.size();
506 const size_t num_gemini_claims = 2 * virtual_log_n;
507 denominators.reserve(num_gemini_claims);
508
509 for (const auto& gemini_eval_challenge_power : gemini_eval_challenge_powers) {
510 // Place 1/(z - r ^ {2^j})
511 denominators.emplace_back(shplonk_eval_challenge - gemini_eval_challenge_power);
512 // Place 1/(z + r ^ {2^j})
513 denominators.emplace_back(shplonk_eval_challenge + gemini_eval_challenge_power);
514 }
515
516 if constexpr (!Curve::is_stdlib_type) {
517 Fr::batch_invert(denominators);
518 } else {
519 for (auto& denominator : denominators) {
520 denominator = denominator.invert();
521 }
522 }
523 return denominators;
524 }
525};
526
532template <typename Fr>
533static std::vector<Fr> compute_shplonk_batching_challenge_powers(const Fr& shplonk_batching_challenge,
534 const size_t virtual_log_n,
535 bool has_zk = false,
536 bool committed_sumcheck = false)
537{
538 // Minimum number of powers: 2 * virtual_log_n for the Gemini fold claims
539 size_t num_powers = 2 * virtual_log_n;
540 // Each round univariate is opened at 0, 1, and a round challenge.
541 static constexpr size_t NUM_COMMITTED_SUMCHECK_CLAIMS_PER_ROUND = 3;
542
543 // Shplonk evaluation and batching challenges are re-used in SmallSubgroupIPA.
544 if (has_zk) {
545 num_powers += NUM_SMALL_IPA_EVALUATIONS;
546 }
547
548 // Commited sumcheck adds 3 claims per round.
549 if (committed_sumcheck) {
550 num_powers += NUM_COMMITTED_SUMCHECK_CLAIMS_PER_ROUND * virtual_log_n;
551 }
552
553 std::vector<Fr> result;
554 result.reserve(num_powers);
555 result.emplace_back(Fr{ 1 });
556 for (size_t idx = 1; idx < num_powers; idx++) {
557 result.emplace_back(result[idx - 1] * shplonk_batching_challenge);
558 }
559 return result;
560}
561} // namespace bb
#define BB_BENCH_NAME(name)
Definition bb_bench.hpp:264
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:37
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:226
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:50
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:254
typename Curve::ScalarField Fr
Definition shplonk.hpp:38
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:135
bb::Polynomial< Fr > Polynomial
Definition shplonk.hpp:39
Shplonk Verifier.
Definition shplonk.hpp:331
std::vector< Fr > pows_of_nu
Definition shplonk.hpp:338
typename Curve::ScalarField Fr
Definition shplonk.hpp:332
BatchOpeningClaim< Curve > export_batch_opening_claim(const Commitment &g1_identity)
Export a BatchOpeningClaim instead of performing final batch_mul.
Definition shplonk.hpp:417
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:484
ShplonkVerifier_(std::vector< Commitment > &polynomial_commitments, std::shared_ptr< Transcript > &transcript, const size_t num_claims)
Definition shplonk.hpp:355
std::vector< Commitment > commitments
Definition shplonk.hpp:344
typename Curve::AffineElement Commitment
Definition shplonk.hpp:334
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:501
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:433
typename Curve::Element GroupElement
Definition shplonk.hpp:333
OpeningClaim< Curve > finalize(const Commitment &g1_identity)
Finalize the Shplonk verification and return the KZG opening claim.
Definition shplonk.hpp:393
std::vector< Fr > scalars
Definition shplonk.hpp:347
Commitment quotient
Definition shplonk.hpp:340
Representation of the Grumpkin Verifier Commitment Key inside a bn254 circuit.
typename Group::element Element
Definition grumpkin.hpp:63
static constexpr bool is_stdlib_type
Definition grumpkin.hpp:67
typename Group::affine_element AffineElement
Definition grumpkin.hpp:64
#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()
void throw_or_abort(std::string const &err)