3#include "../commitment_key.test.hpp"
4#include "../gemini/gemini.hpp"
5#include "../kzg/kzg.hpp"
6#include "../shplonk/shplonk.hpp"
7#include "../utils/batch_mul_native.hpp"
15#include <gtest/gtest.h>
23 static constexpr size_t log_n = 9;
24 static constexpr size_t n = 1UL <<
log_n;
35 using Fr =
typename Flavor::Curve::ScalarField;
37 using Commitment =
typename Flavor::Curve::AffineElement;
42using TestSettings = ::testing::Types<BN254Settings, GrumpkinSettings>;
49 using Curve =
typename TypeParam::Curve;
53 using CK =
typename TypeParam::CommitmentKey;
55 CK
ck = create_commitment_key<CK>(this->n);
64 auto mle_opening_point = this->random_evaluation_point(this->log_n);
67 this->num_polynomials,
69 this->num_right_shiftable_by_k,
77 auto update_batched_eval = [&](
Fr& batched_eval,
const std::vector<Fr>& evaluations,
Fr& rho_power) {
78 for (
auto& eval : evaluations) {
79 batched_eval += eval * rho_power;
85 Fr batched_evaluation(0);
86 update_batched_eval(batched_evaluation, mock_claims.
unshifted.
evals, rho_power);
91 auto compute_batched_commitment = [&](
const std::vector<Commitment>& commitments,
Fr& rho_power) {
92 GroupElement batched = GroupElement::zero();
93 for (
auto& comm : commitments) {
94 batched += comm * rho_power;
102 GroupElement batched_commitment_unshifted =
104 GroupElement batched_commitment_to_be_shifted =
106 GroupElement batched_commitment_to_be_right_shifted_by_k =
110 GroupElement to_be_right_shifted_by_k_contribution =
111 batched_commitment_to_be_right_shifted_by_k *
113 GroupElement to_be_shifted_contribution = batched_commitment_to_be_shifted * gemini_eval_challenge.
invert();
115 GroupElement commitment_to_univariate_pos =
116 batched_commitment_unshifted + to_be_right_shifted_by_k_contribution + to_be_shifted_contribution;
118 GroupElement commitment_to_univariate_neg =
119 batched_commitment_unshifted + to_be_right_shifted_by_k_contribution - to_be_shifted_contribution;
122 commitment_to_univariate_pos * (shplonk_eval_challenge - gemini_eval_challenge).invert() +
123 commitment_to_univariate_neg *
124 (shplonk_batching_challenge * (shplonk_eval_challenge + gemini_eval_challenge).invert());
127 std::vector<Commitment> commitments;
128 std::vector<Fr> scalars;
129 Fr verifier_batched_evaluation{ 0 };
131 Fr inverted_vanishing_eval_pos = (shplonk_eval_challenge - gemini_eval_challenge).invert();
132 Fr inverted_vanishing_eval_neg = (shplonk_eval_challenge + gemini_eval_challenge).invert();
134 std::vector<Fr> inverted_vanishing_evals = { inverted_vanishing_eval_pos, inverted_vanishing_eval_neg };
137 inverted_vanishing_evals, shplonk_batching_challenge, gemini_eval_challenge);
141 commitments, scalars, verifier_batched_evaluation, rho, rho_power);
144 GroupElement shplemini_result = batch_mul_native(commitments, scalars);
146 EXPECT_EQ(commitments.size(),
149 EXPECT_EQ(batched_evaluation, verifier_batched_evaluation);
154 using Curve = TypeParam::Curve;
162 using CK =
typename TypeParam::CommitmentKey;
164 CK
ck = create_commitment_key<CK>(this->n);
171 std::vector<Fr> shplonk_batching_challenge_powers =
172 compute_shplonk_batching_challenge_powers(shplonk_batching_challenge, this->log_n);
176 std::vector<Fr> mle_opening_point = this->random_evaluation_point(this->log_n);
179 this->num_polynomials,
181 this->num_right_shiftable_by_k,
188 Fr running_scalar =
Fr(1);
194 auto fold_polynomials = GeminiProver::compute_fold_polynomials(this->log_n, mle_opening_point, batched);
196 std::vector<Commitment> prover_commitments;
197 for (
size_t l = 0; l < this->log_n - 1; ++l) {
198 auto commitment =
ck.commit(fold_polynomials[l]);
199 prover_commitments.emplace_back(commitment);
202 auto [A_0_pos, A_0_neg] =
205 const auto opening_claims = GeminiProver::construct_univariate_opening_claims(
208 std::vector<Fr> prover_evaluations;
209 for (
size_t l = 0; l < this->log_n; ++l) {
210 const auto& evaluation = opening_claims[l + 1].opening_pair.evaluation;
211 prover_evaluations.emplace_back(evaluation);
217 std::vector<Fr> expected_inverse_vanishing_evals;
218 expected_inverse_vanishing_evals.reserve(2 * this->log_n);
220 for (
size_t idx = 0; idx < this->log_n; idx++) {
221 expected_inverse_vanishing_evals.emplace_back((shplonk_eval_challenge - r_squares[idx]).invert());
222 expected_inverse_vanishing_evals.emplace_back((shplonk_eval_challenge + r_squares[idx]).invert());
225 Fr current_challenge{ shplonk_batching_challenge * shplonk_batching_challenge };
226 for (
size_t idx = 0; idx < prover_commitments.size(); ++idx) {
227 expected_result -= prover_commitments[idx] * current_challenge * expected_inverse_vanishing_evals[2 * idx + 2];
228 current_challenge *= shplonk_batching_challenge;
229 expected_result -= prover_commitments[idx] * current_challenge * expected_inverse_vanishing_evals[2 * idx + 3];
230 current_challenge *= shplonk_batching_challenge;
234 std::vector<Fr> inverse_vanishing_evals =
235 ShplonkVerifier::compute_inverted_gemini_denominators(shplonk_eval_challenge, r_squares);
237 Fr expected_constant_term_accumulator{ 0 };
238 std::vector<Fr> padding_indicator_array(this->log_n,
Fr{ 1 });
240 std::vector<Fr> gemini_fold_pos_evaluations =
242 expected_constant_term_accumulator,
246 expected_constant_term_accumulator);
247 std::vector<Commitment> commitments;
248 std::vector<Fr> scalars;
250 ShpleminiVerifier::batch_gemini_claims_received_from_prover(padding_indicator_array,
253 gemini_fold_pos_evaluations,
254 inverse_vanishing_evals,
255 shplonk_batching_challenge_powers,
258 expected_constant_term_accumulator);
261 GroupElement shplemini_result = batch_mul_native(commitments, scalars);
274 using Curve = TypeParam::Curve;
279 using CK =
typename TypeParam::CommitmentKey;
282 auto prover_transcript = TypeParam::Transcript::prover_init_empty();
286 CK
ck = create_commitment_key<CK>(
std::max<size_t>(this->n, 1ULL << (log_subgroup_size + 1)));
289 ZKData zk_sumcheck_data(this->log_n, prover_transcript,
ck);
292 std::vector<Fr> mle_opening_point = this->random_evaluation_point(this->log_n);
296 this->num_polynomials,
298 this->num_right_shiftable_by_k,
304 zk_sumcheck_data, mle_opening_point, this->log_n);
306 prover_transcript->send_to_verifier(
"Libra:claimed_evaluation", claimed_inner_product);
310 zk_sumcheck_data, mle_opening_point, claimed_inner_product, prover_transcript,
ck);
311 small_subgroup_ipa_prover.
prove();
314 const auto opening_claim = ShpleminiProver::prove(this->n,
322 TestFixture::IPA::compute_opening_proof(this->
ck(), opening_claim, prover_transcript);
332 libra_commitments[0] =
333 verifier_transcript->template receive_from_prover<Commitment>(
"Libra:concatenation_commitment");
336 const Fr libra_total_sum = verifier_transcript->template receive_from_prover<Fr>(
"Libra:Sum");
337 const Fr libra_challenge = verifier_transcript->template get_challenge<Fr>(
"Libra:Challenge");
338 const Fr libra_evaluation = verifier_transcript->template receive_from_prover<Fr>(
"Libra:claimed_evaluation");
341 EXPECT_EQ(libra_total_sum, zk_sumcheck_data.libra_total_sum);
342 EXPECT_EQ(libra_challenge, zk_sumcheck_data.libra_challenge);
343 EXPECT_EQ(libra_evaluation, claimed_inner_product);
346 libra_commitments[1] = verifier_transcript->template receive_from_prover<Commitment>(
"Libra:grand_sum_commitment");
347 libra_commitments[2] = verifier_transcript->template receive_from_prover<Commitment>(
"Libra:quotient_commitment");
351 bool consistency_checked =
true;
354 std::vector<Fr> padding_indicator_array(this->log_n,
Fr{ 1 });
356 const auto batch_opening_claim = ShpleminiVerifier::compute_batch_opening_claim(padding_indicator_array,
359 this->vk().get_g1_identity(),
363 &consistency_checked,
369 TestFixture::IPA::reduce_verify_batch_opening_claim(batch_opening_claim, this->
vk(), verifier_transcript);
370 EXPECT_EQ(result,
true);
372 const auto pairing_points =
375 EXPECT_EQ(this->
vk().pairing_check(pairing_points[0], pairing_points[1]),
true);
387 using Curve = TypeParam::Curve;
390 using CK =
typename TypeParam::CommitmentKey;
395 CK
ck = create_commitment_key<CK>(4096);
398 std::vector<Fr> challenge = this->random_evaluation_point(this->log_n);
400 auto prover_transcript = TypeParam::Transcript::prover_init_empty();
408 mock_claims.template compute_sumcheck_opening_data<TypeParam>(
409 this->log_n, this->sumcheck_univariate_length, challenge,
ck);
412 const Fr claimed_inner_product =
415 prover_transcript->send_to_verifier(
"Libra:claimed_evaluation", claimed_inner_product);
419 zk_sumcheck_data, challenge, claimed_inner_product, prover_transcript,
ck);
420 small_subgroup_ipa_prover.
prove();
423 const auto opening_claim = ShpleminiProver::prove(this->n,
433 TestFixture::IPA::compute_opening_proof(this->
ck(), opening_claim, prover_transcript);
442 libra_commitments[0] =
443 verifier_transcript->template receive_from_prover<Commitment>(
"Libra:concatenation_commitment");
446 const Fr libra_total_sum = verifier_transcript->template receive_from_prover<Fr>(
"Libra:Sum");
447 const Fr libra_challenge = verifier_transcript->template get_challenge<Fr>(
"Libra:Challenge");
448 const Fr libra_evaluation = verifier_transcript->template receive_from_prover<Fr>(
"Libra:claimed_evaluation");
453 EXPECT_EQ(libra_evaluation, claimed_inner_product);
456 libra_commitments[1] = verifier_transcript->template receive_from_prover<Commitment>(
"Libra:grand_sum_commitment");
457 libra_commitments[2] = verifier_transcript->template receive_from_prover<Commitment>(
"Libra:quotient_commitment");
459 bool consistency_checked =
true;
462 std::vector<Fr> padding_indicator_array(this->log_n,
Fr{ 1 });
464 const auto batch_opening_claim = ShpleminiVerifier::compute_batch_opening_claim(padding_indicator_array,
467 this->vk().get_g1_identity(),
471 &consistency_checked,
479 TestFixture::IPA::reduce_verify_batch_opening_claim(batch_opening_claim, this->
vk(), verifier_transcript);
480 EXPECT_EQ(result,
true);
482 const auto pairing_points =
485 EXPECT_EQ(this->
vk().pairing_check(pairing_points[0], pairing_points[1]),
true);
static std::shared_ptr< BaseTranscript > verifier_init_empty(const std::shared_ptr< BaseTranscript > &transcript)
For testing: initializes transcript based on proof data then receives junk data produced by BaseTrans...
Polynomial compute_batched(const Fr &challenge, Fr &running_scalar)
Compute batched polynomial A₀ = F + G/X as the linear combination of all polynomials to be opened.
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.
static VerifierAccumulator reduce_verify_batch_opening_claim(BatchOpeningClaim< Curve > batch_opening_claim, const std::shared_ptr< Transcript > &transcript)
Computes the input points for the pairing check needed to verify a KZG opening claim obtained from a ...
static void compute_opening_proof(const CK &ck, const ProverOpeningClaim< Curve > &opening_claim, const std::shared_ptr< Transcript > &prover_trancript)
Computes the KZG commitment to an opening proof polynomial at a single evaluation point.
bb::CommitmentKey< Curve > CommitmentKey
Structured polynomial class that represents the coefficients 'a' of a_0 + a_1 x .....
static constexpr size_t n
typename Flavor::Curve::ScalarField Fr
static constexpr size_t num_polynomials
typename Flavor::CommitmentKey CK
typename Flavor::Curve::AffineElement Commitment
static constexpr size_t log_n
static constexpr size_t sumcheck_univariate_length
static constexpr size_t num_shiftable
typename Flavor::Curve::Element GroupElement
IPA< typename Flavor::Curve, log_n > IPA
static constexpr size_t num_right_shiftable_by_k
An efficient verifier for the evaluation proofs of multilinear polynomials and their shifts.
A Curve-agnostic ZK protocol to prove inner products of small vectors.
std::array< bb::Polynomial< FF >, NUM_SMALL_IPA_EVALUATIONS > get_witness_polynomials() const
static FF compute_claimed_inner_product(ZKSumcheckData< Flavor > &zk_sumcheck_data, const std::vector< FF > &multivariate_challenge, const size_t &log_circuit_size)
For test purposes: Compute the sum of the Libra constant term and Libra univariates evaluated at Sumc...
void prove()
Compute the derived witnesses and and commit to them.
typename Group::element Element
static constexpr size_t SUBGROUP_SIZE
typename Group::affine_element AffineElement
std::vector< Fr > powers_of_evaluation_challenge(const Fr r, const size_t num_squares)
Compute squares of folding challenge r.
std::vector< Fr > powers_of_rho(const Fr rho, const size_t num_powers)
Compute powers of challenge ρ
constexpr T get_msb(const T in)
Entry point for Barretenberg command-line interface.
::testing::Types< BN254Settings, GrumpkinSettings > TestSettings
TYPED_TEST_SUITE(ShpleminiTest, TestSettings)
TYPED_TEST(ShpleminiTest, CorrectnessOfMultivariateClaimBatching)
CommitmentKey< Curve > ck
VerifierCommitmentKey< Curve > vk
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
uint32_t k_shift_magnitude
void compute_scalars_for_each_batch(std::span< const Fr > inverted_vanishing_evals, const Fr &nu_challenge, const Fr &r_challenge)
Compute scalars used to batch each set of claims, excluding contribution from batching challenge \rho...
void update_batch_mul_inputs_and_batched_evaluation(std::vector< Commitment > &commitments, std::vector< Fr > &scalars, Fr &batched_evaluation, const Fr &rho, Fr &rho_power, Fr shplonk_batching_pos={ 0 }, Fr shplonk_batching_neg={ 0 })
Append the commitments and scalars from each batch of claims to the Shplemini, vectors which subseque...
std::vector< Commitment > commitments
Constructs random polynomials, computes commitments and corresponding evaluations.
std::vector< bb::Polynomial< Fr > > round_univariates
std::vector< Commitment > sumcheck_commitments
ClaimBatcher claim_batcher
std::vector< std::array< Fr, 3 > > sumcheck_evaluations
PolynomialBatcher polynomial_batcher
ClaimData to_be_right_shifted_by_k
This structure is created to contain various polynomials and constants required by ZK Sumcheck.
BB_INLINE constexpr field pow(const uint256_t &exponent) const noexcept
constexpr field invert() const noexcept
static field random_element(numeric::RNG *engine=nullptr) noexcept