Skip to content

Commit 4d3bf91

Browse files
author
Vesselin Velichkov
committed
plonk: fixed functions plonk_compute_accumulator and plonk_compute_selector_polynomials to allocate the size of their return values inside of the function i.e. not to rely on the caller to pass inputs with correct size. addressing issue #58, bullet 7
1 parent e81d94a commit 4d3bf91

File tree

4 files changed

+27
-38
lines changed

4 files changed

+27
-38
lines changed

libsnark/zk_proof_systems/plonk/prover.tcc

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,8 @@ round_two_out_t<ppT> plonk_prover<ppT>::round_two(
194194
z1_blind_poly, z1_blind_poly, round_zero_out.zh_poly);
195195

196196
// A[0] = 1; ... A[i] = computed from (i-1)
197-
std::vector<Field> A_vector(srs.num_gates, Field(0));
198-
plonk_compute_accumulator(
199-
srs.num_gates,
200-
beta,
201-
gamma,
202-
witness,
203-
srs.H_gen,
204-
srs.H_gen_permute,
205-
A_vector);
197+
std::vector<Field> A_vector = plonk_compute_accumulator(
198+
srs.num_gates, beta, gamma, witness, srs.H_gen, srs.H_gen_permute);
206199

207200
polynomial<Field> A_poly(srs.num_gates);
208201
plonk_interpolate_polynomial_from_points<Field>(A_vector, A_poly);

libsnark/zk_proof_systems/plonk/tests/test_plonk.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,9 @@ circuit_t<ppT> plonk_circuit_description_from_example(
187187
// transposed gates matrix over the Lagrange basis q_poly = \sum_i
188188
// q[i] * L[i] where q[i] is a coefficient (a scalar Field
189189
// element) and L[i] is a polynomial with Field coefficients
190-
std::vector<polynomial<Field>> Q_polys;
191-
Q_polys.resize(num_qpolys, polynomial<Field>(num_gates));
192-
plonk_compute_selector_polynomials<Field>(gates_matrix_transpose, Q_polys);
190+
std::vector<polynomial<Field>> Q_polys =
191+
plonk_compute_selector_polynomials<Field>(
192+
num_gates, num_qpolys, gates_matrix_transpose);
193193

194194
// omega[0] are the n roots of unity, omega[1] are omega[0]*k1,
195195
// omega[2] are omega[0]*k2
@@ -252,15 +252,8 @@ void test_plonk_compute_accumulator(
252252
{
253253
using Field = libff::Fr<ppT>;
254254
// A[0] = 1; ... A[i] = computed from (i-1)
255-
std::vector<Field> A_vector(srs.num_gates, Field(0));
256-
plonk_compute_accumulator(
257-
srs.num_gates,
258-
beta,
259-
gamma,
260-
witness,
261-
srs.H_gen,
262-
srs.H_gen_permute,
263-
A_vector);
255+
std::vector<Field> A_vector = plonk_compute_accumulator(
256+
srs.num_gates, beta, gamma, witness, srs.H_gen, srs.H_gen_permute);
264257
polynomial<Field> A_poly(srs.num_gates);
265258
plonk_interpolate_polynomial_from_points<Field>(A_vector, A_poly);
266259

libsnark/zk_proof_systems/plonk/utils.hpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ void plonk_interpolate_polynomial_from_points(
9797
/// values L, R, M, O and C for each gate; the number of columns is
9898
/// equal to the number of gates. L_basis is the Lagrange basis.
9999
template<typename FieldT>
100-
void plonk_compute_selector_polynomials(
101-
const std::vector<std::vector<FieldT>> &gates_matrix_transpose,
102-
std::vector<polynomial<FieldT>> &Q_polys);
100+
std::vector<polynomial<FieldT>> plonk_compute_selector_polynomials(
101+
const size_t &num_gates,
102+
const std::vector<std::vector<FieldT>> &gates_matrix_transpose);
103103

104104
/// This function computes the sets H, k1H, k2H. H is a
105105
/// multiplicative subgroup containing the n-th roots of unity in Fr,
@@ -198,14 +198,13 @@ FieldT plonk_compute_accumulator_factor(
198198

199199
/// A: accumulatro vector
200200
template<typename FieldT>
201-
void plonk_compute_accumulator(
202-
const size_t n, // num_gates
201+
std::vector<FieldT> plonk_compute_accumulator(
202+
const size_t num_gates,
203203
const FieldT beta,
204204
const FieldT gamma,
205205
const std::vector<FieldT> &witness,
206206
const std::vector<FieldT> &H_gen, // H, Hk1, Hk2
207-
const std::vector<FieldT> &H_gen_permute,
208-
std::vector<FieldT> &A);
207+
const std::vector<FieldT> &H_gen_permute);
209208

210209
} // namespace libsnark
211210

libsnark/zk_proof_systems/plonk/utils.tcc

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,21 @@ void plonk_interpolate_polynomial_from_points(
9696
/// values L, R, M, O and C for each gate; the number of columns is
9797
/// equal to the number of gates. L_basis is the Lagrange basis.
9898
template<typename FieldT>
99-
void plonk_compute_selector_polynomials(
100-
const std::vector<std::vector<FieldT>> &gates_matrix_transpose,
101-
std::vector<polynomial<FieldT>> &Q_polys)
99+
std::vector<polynomial<FieldT>> plonk_compute_selector_polynomials(
100+
const size_t &num_gates,
101+
const size_t &num_qpolys,
102+
const std::vector<std::vector<FieldT>> &gates_matrix_transpose)
102103
{
103-
assert(gates_matrix_transpose.size() == Q_polys.size());
104-
assert(gates_matrix_transpose[0].size() == Q_polys[0].size());
105-
size_t num_qpolys = gates_matrix_transpose.size();
104+
assert(gates_matrix_transpose.size() == num_qpolys);
105+
assert(gates_matrix_transpose[0].size() == num_gates);
106+
107+
std::vector<polynomial<FieldT>> Q_polys;
108+
Q_polys.resize(num_qpolys, polynomial<FieldT>(num_gates));
106109
for (size_t i = 0; i < num_qpolys; ++i) {
107110
std::vector<FieldT> q_vec = gates_matrix_transpose[i];
108111
plonk_interpolate_polynomial_from_points<FieldT>(q_vec, Q_polys[i]);
109112
}
113+
return Q_polys;
110114
};
111115

112116
template<typename FieldT>
@@ -353,24 +357,24 @@ FieldT plonk_compute_accumulator_factor(
353357

354358
// - A: accumulator vector
355359
template<typename FieldT>
356-
void plonk_compute_accumulator(
360+
std::vector<FieldT> plonk_compute_accumulator(
357361
const size_t num_gates,
358362
const FieldT beta,
359363
const FieldT gamma,
360364
const std::vector<FieldT> &witness,
361365
const std::vector<FieldT> &H_gen, // H, Hk1, Hk2
362-
const std::vector<FieldT> &H_gen_permute,
363-
std::vector<FieldT> &A)
366+
const std::vector<FieldT> &H_gen_permute)
364367
{
365368
assert(num_gates);
366369
assert(witness.size() == (NUM_HSETS * num_gates));
367370
assert(H_gen.size() == (NUM_HSETS * num_gates));
368371
assert(H_gen_permute.size() == (NUM_HSETS * num_gates));
369-
assert(A.size() == num_gates);
372+
std::vector<FieldT> A(num_gates, FieldT(0));
370373
for (size_t i = 0; i < num_gates; ++i) {
371374
A[i] = plonk_compute_accumulator_factor(
372375
i, num_gates, beta, gamma, witness, H_gen, H_gen_permute, A);
373376
}
377+
return A;
374378
}
375379

376380
} // namespace libsnark

0 commit comments

Comments
 (0)