Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
mock_witness_generator.hpp
Go to the documentation of this file.
1#pragma once
2
10
11namespace bb {
17template <typename Curve> struct MockClaimGenerator {
18 public:
20 using Fr = typename Curve::ScalarField;
26
28
29 struct ClaimData {
31 std::vector<Commitment> commitments;
32 std::vector<Fr> evals;
33 };
34
37
39
42
43 // Containers for mock Sumcheck data
45 std::vector<Commitment> sumcheck_commitments;
47
59 MockClaimGenerator(const size_t poly_size,
60 const size_t num_polynomials,
61 const size_t num_to_be_shifted,
62 const std::vector<Fr>& mle_opening_point,
63 const CommitmentKey& commitment_key)
64
65 : ck(commitment_key) // Initialize the commitment key
66 , polynomial_batcher(poly_size)
67
68 {
69 size_t log_size = numeric::get_msb(poly_size);
70 // If the size of the opening point is bigger than the log of the poly size, we assume that the prover is
71 // extending all of its polynomials by zero outside of the hypercube of size 2^{log_size}.
72 bool has_virtual_rounds = (mle_opening_point.size() > log_size);
73
74 std::span<const Fr> challenge;
75
76 if (has_virtual_rounds) {
77 // The evaluation on the full domain can be obtain by scaling by extension-by-zero factor `ebz_factor`
78 // computed below.
79 challenge = std::span<const Fr>(mle_opening_point).subspan(0, log_size);
80 } else {
81 challenge = std::span<const Fr>(mle_opening_point);
82 }
83
84 BB_ASSERT_GTE(num_polynomials, num_to_be_shifted);
85 const size_t num_not_to_be_shifted = num_polynomials - num_to_be_shifted;
86
87 Fr ebz_factor = 1;
88
89 for (size_t idx = log_size; idx < mle_opening_point.size(); idx++) {
90 ebz_factor *= (Fr(1) - mle_opening_point[idx]);
91 }
92
93 // Construct claim data for polynomials that are NOT to be shifted
94 for (size_t idx = 0; idx < num_not_to_be_shifted; idx++) {
95 Polynomial poly = Polynomial::random(poly_size);
96 unshifted.commitments.push_back(ck.commit(poly));
97 unshifted.evals.push_back(poly.evaluate_mle(challenge) * ebz_factor);
98 unshifted.polys.push_back(std::move(poly));
99 }
100
101 // Construct claim data for polynomials that are to-be-shifted
102 for (size_t idx = 0; idx < num_to_be_shifted; idx++) {
103 Polynomial poly = Polynomial::random(poly_size, /*shiftable*/ 1);
104 Commitment commitment = ck.commit(poly);
105 to_be_shifted.commitments.push_back(commitment);
106 to_be_shifted.evals.push_back(poly.shifted().evaluate_mle(challenge) * ebz_factor);
107 to_be_shifted.polys.push_back(poly.share());
108 // Populate the unshifted counterpart in the unshifted claims
109 unshifted.commitments.push_back(commitment);
110 unshifted.evals.push_back(poly.evaluate_mle(challenge) * ebz_factor);
111 unshifted.polys.push_back(std::move(poly));
112 }
113
116
119 .shifted =
121 }
122
123 // Generate zero polynomials to test edge cases in PCS
124 MockClaimGenerator(const size_t n, const size_t num_zero_polynomials)
126 {
127 for (size_t idx = 0; idx < num_zero_polynomials; idx++) {
128 unshifted.polys.emplace_back(n);
129 unshifted.commitments.push_back(Commitment::infinity());
130 unshifted.evals.push_back(Fr(0));
131 }
132
134
137 }
138
139 // Generates mock claims by using the custom polynomials provided as input instead of random polynomials. Used for
140 // the high degree attack tests.
141 MockClaimGenerator(const size_t poly_size,
142 const std::vector<Polynomial> custom_unshifted,
143 const std::vector<Fr>& custom_unshifted_evals,
144 const CommitmentKey& commitment_key)
145
146 : ck(commitment_key)
147 , polynomial_batcher(poly_size)
148 {
149
150 // ---------- Unshifted ----------
151 for (size_t i = 0; i < custom_unshifted.size(); ++i) {
152 auto& p = custom_unshifted[i];
153 unshifted.commitments.push_back(ck.commit(p));
154 unshifted.evals.push_back(custom_unshifted_evals[i]);
155 unshifted.polys.push_back(std::move(p));
156 }
157
159
162 }
163
164 template <typename Flavor>
165 void compute_sumcheck_opening_data(const size_t log_n,
166 const size_t sumcheck_univariate_length,
167 std::vector<Fr>& challenge,
168 const CommitmentKey& ck)
169 {
170 // Generate valid sumcheck polynomials of given length
171 auto mock_sumcheck_polynomials = ZKSumcheckData<Flavor>(log_n, sumcheck_univariate_length);
172
173 for (size_t idx = 0; idx < log_n; idx++) {
174 bb::Polynomial<Fr> round_univariate = mock_sumcheck_polynomials.libra_univariates[idx];
175
176 round_univariate.at(0) += mock_sumcheck_polynomials.libra_running_sum;
177
178 sumcheck_commitments.push_back(ck.commit(round_univariate));
179
180 sumcheck_evaluations.push_back({ round_univariate.at(0),
181 round_univariate.evaluate(Fr(1)),
182 round_univariate.evaluate(challenge[idx]) });
183
184 mock_sumcheck_polynomials.update_zk_sumcheck_data(challenge[idx], idx);
185 round_univariates.push_back(round_univariate);
186 }
187 }
188};
189
190} // namespace bb
#define BB_ASSERT_GTE(left, right,...)
Definition assert.hpp:128
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:125
void set_to_be_shifted_by_one(RefVector< Polynomial > polynomials)
Definition gemini.hpp:149
void set_unshifted(RefVector< Polynomial > polynomials)
Definition gemini.hpp:148
Structured polynomial class that represents the coefficients 'a' of a_0 + a_1 x .....
Polynomial shifted() const
Returns a Polynomial the left-shift of self.
static Polynomial random(size_t size, size_t start_index=0)
Fr evaluate(const Fr &z) const
Fr evaluate_mle(std::span< const Fr > evaluation_points, bool shift=false) const
evaluate multi-linear extension p(X_0,…,X_{n-1}) = \sum_i a_i*L_i(X_0,…,X_{n-1}) at u = (u_0,...
Polynomial share() const
Fr & at(size_t index)
Our mutable accessor, unlike operator[]. We abuse precedent a bit to differentiate at() and operator[...
A template class for a reference vector. Behaves as if std::vector<T&> was possible.
typename Group::affine_element AffineElement
Definition grumpkin.hpp:65
constexpr T get_msb(const T in)
Definition get_msb.hpp:49
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
Logic to support batching opening claims for unshifted and shifted polynomials in Shplemini.
std::optional< Batch > unshifted
Constructs random polynomials, computes commitments and corresponding evaluations.
MockClaimGenerator(const size_t poly_size, const size_t num_polynomials, const size_t num_to_be_shifted, const std::vector< Fr > &mle_opening_point, const CommitmentKey &commitment_key)
Construct claim data for a set of random polynomials with the specified type.
std::vector< bb::Polynomial< Fr > > round_univariates
std::vector< Fr > const_size_mle_opening_point
std::vector< Commitment > sumcheck_commitments
std::vector< std::array< Fr, 3 > > sumcheck_evaluations
typename Curve::AffineElement Commitment
MockClaimGenerator(const size_t poly_size, const std::vector< Polynomial > custom_unshifted, const std::vector< Fr > &custom_unshifted_evals, const CommitmentKey &commitment_key)
typename Curve::ScalarField Fr
MockClaimGenerator(const size_t n, const size_t num_zero_polynomials)
void compute_sumcheck_opening_data(const size_t log_n, const size_t sumcheck_univariate_length, std::vector< Fr > &challenge, const CommitmentKey &ck)
This structure is created to contain various polynomials and constants required by ZK Sumcheck.