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;
27
29
30 struct ClaimData {
32 std::vector<Commitment> commitments;
33 std::vector<Fr> evals;
34 };
35
38
40
43
44 // Containers for mock Sumcheck data
46 std::vector<Commitment> sumcheck_commitments;
48
56
68 MockClaimGenerator(const size_t poly_size,
69 const size_t num_polynomials,
70 const size_t num_to_be_shifted,
71 const std::vector<Fr>& mle_opening_point,
72 const CommitmentKey& commitment_key,
73 size_t num_interleaved = 0,
74 size_t num_to_be_interleaved = 0)
75
76 : ck(commitment_key) // Initialize the commitment key
77 , polynomial_batcher(poly_size)
78
79 {
80 size_t log_size = numeric::get_msb(poly_size);
81 // If the size of the opening point is bigger than the log of the poly size, we assume that the prover is
82 // extending all of its polynomials by zero outside of the hypercube of size 2^{log_size}.
83 bool has_virtual_rounds = (mle_opening_point.size() > log_size);
84
85 std::span<const Fr> challenge;
86
87 if (has_virtual_rounds) {
88 // The evaluation on the full domain can be obtain by scaling by extension-by-zero factor `ebz_factor`
89 // computed below.
90 challenge = std::span<const Fr>(mle_opening_point).subspan(0, log_size);
91 } else {
92 challenge = std::span<const Fr>(mle_opening_point);
93 }
94
95 BB_ASSERT_GTE(num_polynomials, num_to_be_shifted);
96 const size_t num_not_to_be_shifted = num_polynomials - num_to_be_shifted;
97
98 Fr ebz_factor = 1;
99
100 for (size_t idx = log_size; idx < mle_opening_point.size(); idx++) {
101 ebz_factor *= (Fr(1) - mle_opening_point[idx]);
102 }
103
104 // Construct claim data for polynomials that are NOT to be shifted
105 for (size_t idx = 0; idx < num_not_to_be_shifted; idx++) {
106 Polynomial poly = Polynomial::random(poly_size);
107 unshifted.commitments.push_back(ck.commit(poly));
108 unshifted.evals.push_back(poly.evaluate_mle(challenge) * ebz_factor);
109 unshifted.polys.push_back(std::move(poly));
110 }
111
112 // Construct claim data for polynomials that are to-be-shifted
113 for (size_t idx = 0; idx < num_to_be_shifted; idx++) {
114 Polynomial poly = Polynomial::random(poly_size, /*shiftable*/ 1);
115 Commitment commitment = ck.commit(poly);
116 to_be_shifted.commitments.push_back(commitment);
117 to_be_shifted.evals.push_back(poly.shifted().evaluate_mle(challenge) * ebz_factor);
118 to_be_shifted.polys.push_back(poly.share());
119 // Populate the unshifted counterpart in the unshifted claims
120 unshifted.commitments.push_back(commitment);
121 unshifted.evals.push_back(poly.evaluate_mle(challenge) * ebz_factor);
122 unshifted.polys.push_back(std::move(poly));
123 }
124
127
130 .shifted =
132 if (num_interleaved > 0) {
134 generate_interleaving_inputs(mle_opening_point, num_interleaved, num_to_be_interleaved, ck);
136 to_vector_of_ref_vectors(interleave_data.groups));
137
140 .evaluations = RefVector(interleave_data.evaluations) };
141 }
142 }
143
144 // Generate zero polynomials to test edge cases in PCS
145 MockClaimGenerator(const size_t n, const size_t num_zero_polynomials)
147 {
148 for (size_t idx = 0; idx < num_zero_polynomials; idx++) {
149 unshifted.polys.emplace_back(n);
150 unshifted.commitments.push_back(Commitment::infinity());
151 unshifted.evals.push_back(Fr(0));
152 }
153
155
158 }
159
160 // Generates mock claims by using the custom polynomials provided as input instead of random polynomials. Used for
161 // the high degree attack tests.
162 MockClaimGenerator(const size_t poly_size,
163 const std::vector<Polynomial> custom_unshifted,
164 const std::vector<Fr>& custom_unshifted_evals,
165 const CommitmentKey& commitment_key)
166
167 : ck(commitment_key)
168 , polynomial_batcher(poly_size)
169 {
170
171 // ---------- Unshifted ----------
172 for (size_t i = 0; i < custom_unshifted.size(); ++i) {
173 auto& p = custom_unshifted[i];
174 unshifted.commitments.push_back(ck.commit(p));
175 unshifted.evals.push_back(custom_unshifted_evals[i]);
176 unshifted.polys.push_back(std::move(p));
177 }
178
180
183 }
184
185 InterleaveData generate_interleaving_inputs(const std::vector<Fr>& u_challenge,
186 const size_t num_interleaved,
187 const size_t group_size,
188 const CommitmentKey& ck)
189 {
190
191 size_t N = 1 << u_challenge.size();
192 size_t MINI_CIRCUIT_N = N / group_size; // size of chunks
193
194 // Polynomials "chunks" that are interleaved in the PCS
196
197 // Concatenated polynomials
198 std::vector<Polynomial> interleaved_polynomials;
199
200 // Evaluations of interleaved polynomials
201 std::vector<Fr> c_evaluations;
202
203 // For each polynomial to be interleaved
204 for (size_t i = 0; i < num_interleaved; ++i) {
206 Polynomial interleaved_polynomial(N);
207 for (size_t j = 0; j < group_size; j++) {
208 Polynomial chunk_polynomial(N);
209 // Fill the chunk polynomial with random values and appropriately fill the space in
210 // interleaved_polynomial
211 for (size_t k = 0; k < MINI_CIRCUIT_N; k++) {
212 // Chunks should be shiftable
213 auto tmp = Fr(0);
214 if (k > 0) {
215 tmp = Fr::random_element();
216 }
217 chunk_polynomial.at(k) = tmp;
218 interleaved_polynomial.at(k * group_size + j) = tmp;
219 }
220 group.emplace_back(chunk_polynomial);
221 }
222 // Store chunks
223 groups.emplace_back(group);
224 // Store interleaved polynomial
225 interleaved_polynomials.emplace_back(interleaved_polynomial);
226 // Get evaluation
227 c_evaluations.emplace_back(interleaved_polynomial.evaluate_mle(u_challenge));
228 }
229
230 // Compute commitments of all polynomial chunks
231 std::vector<std::vector<Commitment>> groups_commitments;
232 for (size_t i = 0; i < num_interleaved; ++i) {
233 std::vector<Commitment> group_commitment;
234 for (size_t j = 0; j < group_size; j++) {
235 group_commitment.emplace_back(ck.commit(groups[i][j]));
236 }
237 groups_commitments.emplace_back(group_commitment);
238 }
239
240 return { groups, interleaved_polynomials, c_evaluations, groups_commitments };
241 }
242
243 template <typename Flavor>
244 void compute_sumcheck_opening_data(const size_t log_n,
245 const size_t sumcheck_univariate_length,
246 std::vector<Fr>& challenge,
247 const CommitmentKey& ck)
248 {
249 // Generate valid sumcheck polynomials of given length
250 auto mock_sumcheck_polynomials = ZKSumcheckData<Flavor>(log_n, sumcheck_univariate_length);
251
252 for (size_t idx = 0; idx < log_n; idx++) {
253 bb::Polynomial<Fr> round_univariate = mock_sumcheck_polynomials.libra_univariates[idx];
254
255 round_univariate.at(0) += mock_sumcheck_polynomials.libra_running_sum;
256
257 sumcheck_commitments.push_back(ck.commit(round_univariate));
258
259 sumcheck_evaluations.push_back({ round_univariate.at(0),
260 round_univariate.evaluate(Fr(1)),
261 round_univariate.evaluate(challenge[idx]) });
262
263 mock_sumcheck_polynomials.update_zk_sumcheck_data(challenge[idx], idx);
264 round_univariates.push_back(round_univariate);
265 }
266 }
267};
268
269} // namespace bb
#define BB_ASSERT_GTE(left, right,...)
Definition assert.hpp:138
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:126
void set_to_be_shifted_by_one(RefVector< Polynomial > polynomials)
Definition gemini.hpp:155
void set_interleaved(RefVector< Polynomial > results, std::vector< RefVector< Polynomial > > groups)
Definition gemini.hpp:157
void set_unshifted(RefVector< Polynomial > polynomials)
Definition gemini.hpp:154
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, size_t target_size) 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:63
group class. Represents an elliptic curve group element. Group is parametrised by Fq and Fr
Definition group.hpp:36
constexpr T get_msb(const T in)
Definition get_msb.hpp:47
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::vector< RefVector< Commitment > > commitments_groups
Logic to support batching opening claims for unshifted, shifted and interleaved polynomials in Shplem...
std::optional< Batch > unshifted
std::optional< InterleavedBatch > interleaved
std::vector< std::vector< Polynomial > > groups
std::vector< std::vector< Commitment > > group_commitments
Constructs random polynomials, computes commitments and corresponding evaluations.
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
InterleaveData generate_interleaving_inputs(const std::vector< Fr > &u_challenge, const size_t num_interleaved, const size_t group_size, const CommitmentKey &ck)
MockClaimGenerator(const size_t poly_size, const std::vector< Polynomial > custom_unshifted, const std::vector< Fr > &custom_unshifted_evals, const CommitmentKey &commitment_key)
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, size_t num_interleaved=0, size_t num_to_be_interleaved=0)
Construct claim data for a set of random polynomials with the specified type.
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.
static field random_element(numeric::RNG *engine=nullptr) noexcept