Skip to content

Commit a4aba00

Browse files
ludamadludamad0
andauthored
feat: ProverPolynomials owns its memory (AztecProtocol#3560)
Makes ProverPolynomials no longer use a span - Polynomial class already used shared memory, now allowing shifted polynomials to be represented by the same class - Fixes AztecProtocol#2213 by using ranges defined in entity classes - For safety, deletes the full-copy assignment and constructor of ProverPolynomials, so that we correctly std::move it around. This can possibly be removed, but wanted to catch any now-different-semantics usage and avoid extra copies - Makes explicit share() calls wherever semantics were to share before due to std::span usage. shifted() is similar, shares underlying memory, shifted - Remove cruft needed with a separate polynomial storages variable, some might remain --------- Co-authored-by: ludamad <adam@aztecprotocol.com>
1 parent 4847c19 commit a4aba00

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+751
-1405
lines changed

barretenberg/cpp/src/barretenberg/benchmark/honk_bench/ultra_honk_rounds.bench.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ BBERG_PROFILE static void test_round(State& state, size_t index) noexcept
4545
for (auto _ : state) {
4646
state.PauseTiming();
4747
honk::UltraComposer composer;
48-
// TODO: https://github.com/AztecProtocol/barretenberg/issues/761 benchmark both sparse and dense circuits
48+
// TODO(https://github.com/AztecProtocol/barretenberg/issues/761) benchmark both sparse and dense circuits
4949
honk::UltraProver prover = bench_utils::get_prover(
5050
composer, &bench_utils::generate_ecdsa_verification_test_circuit<UltraCircuitBuilder>, 10);
5151
test_round_inner(state, prover, index);

barretenberg/cpp/src/barretenberg/commitment_schemes/gemini/gemini.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ std::vector<typename barretenberg::Polynomial<typename Curve::ScalarField>> Gemi
7777
Polynomial& batched_G = gemini_polynomials.emplace_back(std::move(batched_to_be_shifted));
7878
constexpr size_t offset_to_folded = 2; // Offset because of F an G
7979
// A₀(X) = F(X) + G↺(X) = F(X) + G(X)/X.
80-
Polynomial A_0(batched_F);
80+
Polynomial A_0 = batched_F;
8181
A_0 += batched_G.shifted();
8282

8383
// Allocate everything before parallel computation

barretenberg/cpp/src/barretenberg/commitment_schemes/kzg/kzg.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ template <typename Curve> class KZG {
3333
const Polynomial& polynomial,
3434
const std::shared_ptr<BaseTranscript>& prover_trancript)
3535
{
36-
Polynomial quotient(polynomial);
36+
Polynomial quotient = polynomial;
3737
quotient[0] -= opening_pair.evaluation;
3838
// Computes the coefficients for the quotient polynomial q(X) = (p(X) - v) / (X - r) through an FFT
3939
quotient.factor_roots(opening_pair.challenge);

barretenberg/cpp/src/barretenberg/commitment_schemes/shplonk/shplonk.test.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ TYPED_TEST(ShplonkTest, ShplonkSimple)
4444

4545
// Aggregate polynomials and their opening pairs
4646
std::vector<OpeningPair> opening_pairs = { { r1, eval1 }, { r2, eval2 } };
47-
std::vector<Polynomial> polynomials = { poly1, poly2 };
47+
std::vector<Polynomial> polynomials = { poly1.share(), poly2.share() };
4848

4949
// Execute the shplonk prover functionality
5050
const Fr nu_challenge = prover_transcript->get_challenge("Shplonk:nu");

barretenberg/cpp/src/barretenberg/commitment_schemes/zeromorph/zeromorph.hpp

+28-29
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#include "barretenberg/commitment_schemes/commitment_key.hpp"
23
#include "barretenberg/common/ref_vector.hpp"
34
#include "barretenberg/common/zip_view.hpp"
45
#include "barretenberg/polynomials/polynomial.hpp"
@@ -34,8 +35,8 @@ template <typename Curve> class ZeroMorphProver_ {
3435
using Commitment = typename Curve::AffineElement;
3536
using Polynomial = barretenberg::Polynomial<FF>;
3637

37-
// TODO(#742): Set this N_max to be the number of G1 elements in the mocked zeromorph SRS once it's in place. (Then,
38-
// eventually, set it based on the real SRS). For now we set it to be large but more or less arbitrary.
38+
// TODO(#742): Set this N_max to be the number of G1 elements in the mocked zeromorph SRS once it's in place.
39+
// (Then, eventually, set it based on the real SRS). For now we set it to be large but more or less arbitrary.
3940
static const size_t N_max = 1 << 22;
4041

4142
public:
@@ -59,7 +60,7 @@ template <typename Curve> class ZeroMorphProver_ {
5960
* @param u_challenge Multivariate challenge u = (u_0, ..., u_{d-1})
6061
* @return std::vector<Polynomial> The quotients q_k
6162
*/
62-
static std::vector<Polynomial> compute_multilinear_quotients(Polynomial polynomial, std::span<FF> u_challenge)
63+
static std::vector<Polynomial> compute_multilinear_quotients(Polynomial polynomial, std::span<const FF> u_challenge)
6364
{
6465
size_t log_N = numeric::get_msb(polynomial.size());
6566
// The size of the multilinear challenge must equal the log of the polynomial size
@@ -74,12 +75,12 @@ template <typename Curve> class ZeroMorphProver_ {
7475

7576
// Compute the coefficients of q_{n-1}
7677
size_t size_q = 1 << (log_N - 1);
77-
Polynomial q = Polynomial(size_q);
78+
Polynomial q{ size_q };
7879
for (size_t l = 0; l < size_q; ++l) {
7980
q[l] = polynomial[size_q + l] - polynomial[l];
8081
}
8182

82-
quotients[log_N - 1] = q;
83+
quotients[log_N - 1] = q.share();
8384

8485
std::vector<FF> f_k;
8586
f_k.resize(size_q);
@@ -94,13 +95,13 @@ template <typename Curve> class ZeroMorphProver_ {
9495
}
9596

9697
size_q = size_q / 2;
97-
q = Polynomial(size_q);
98+
q = Polynomial{ size_q };
9899

99100
for (size_t l = 0; l < size_q; ++l) {
100101
q[l] = f_k[size_q + l] - f_k[l];
101102
}
102103

103-
quotients[log_N - k - 1] = q;
104+
quotients[log_N - k - 1] = q.share();
104105
g = f_k;
105106
}
106107

@@ -206,7 +207,7 @@ template <typename Curve> class ZeroMorphProver_ {
206207
Polynomial& g_batched,
207208
std::vector<Polynomial>& quotients,
208209
FF v_evaluation,
209-
std::span<FF> u_challenge,
210+
std::span<const FF> u_challenge,
210211
FF x_challenge,
211212
std::vector<Polynomial> concatenation_groups_batched = {})
212213
{
@@ -305,8 +306,8 @@ template <typename Curve> class ZeroMorphProver_ {
305306
}
306307

307308
/**
308-
* @brief Prove a set of multilinear evaluation claims for unshifted polynomials f_i and to-be-shifted polynomials
309-
* g_i
309+
* @brief Prove a set of multilinear evaluation claims for unshifted polynomials f_i and to-be-shifted
310+
* polynomials g_i
310311
*
311312
* @param f_polynomials Unshifted polynomials
312313
* @param g_polynomials To-be-shifted polynomials (of which the shifts h_i were evaluated by sumcheck)
@@ -315,23 +316,22 @@ template <typename Curve> class ZeroMorphProver_ {
315316
* @param commitment_key
316317
* @param transcript
317318
*/
318-
static void prove(const auto& f_polynomials,
319-
const auto& g_polynomials,
320-
auto&& f_evaluations,
321-
auto&& g_shift_evaluations,
322-
auto& multilinear_challenge,
323-
auto& commitment_key,
324-
auto transcript,
325-
const std::vector<std::span<FF>>& concatenated_polynomials = {},
319+
static void prove(const std::vector<Polynomial>& f_polynomials,
320+
const std::vector<Polynomial>& g_polynomials,
321+
const std::vector<FF>& f_evaluations,
322+
const std::vector<FF>& g_shift_evaluations,
323+
const std::vector<FF>& multilinear_challenge,
324+
const std::shared_ptr<CommitmentKey<Curve>>& commitment_key,
325+
const std::shared_ptr<BaseTranscript>& transcript,
326+
const std::vector<Polynomial>& concatenated_polynomials = {},
326327
const std::vector<FF>& concatenated_evaluations = {},
327-
// TODO(https://github.com/AztecProtocol/barretenberg/issues/743) remove span
328-
const std::vector<RefVector<std::span<FF>>>& concatenation_groups = {})
328+
const std::vector<RefVector<Polynomial>>& concatenation_groups = {})
329329
{
330330
// Generate batching challenge \rho and powers 1,...,\rho^{m-1}
331331
const FF rho = transcript->get_challenge("rho");
332332

333333
// Extract multilinear challenge u and claimed multilinear evaluations from Sumcheck output
334-
std::span<FF> u_challenge = multilinear_challenge;
334+
std::span<const FF> u_challenge = multilinear_challenge;
335335
size_t log_N = u_challenge.size();
336336
size_t N = 1 << log_N;
337337

@@ -341,16 +341,16 @@ template <typename Curve> class ZeroMorphProver_ {
341341
// v = sum_{i=0}^{m-1}\rho^i*f_i(u) + sum_{i=0}^{l-1}\rho^{m+i}*h_i(u).
342342
// Note: g_batched is formed from the to-be-shifted polynomials, but the batched evaluation incorporates the
343343
// evaluations produced by sumcheck of h_i = g_i_shifted.
344-
auto batched_evaluation = FF(0);
344+
FF batched_evaluation{ 0 };
345345
Polynomial f_batched(N); // batched unshifted polynomials
346-
FF batching_scalar = FF(1);
346+
FF batching_scalar{ 1 };
347347
for (auto [f_poly, f_eval] : zip_view(f_polynomials, f_evaluations)) {
348348
f_batched.add_scaled(f_poly, batching_scalar);
349349
batched_evaluation += batching_scalar * f_eval;
350350
batching_scalar *= rho;
351351
}
352352

353-
Polynomial g_batched(N); // batched to-be-shifted polynomials
353+
Polynomial g_batched{ N }; // batched to-be-shifted polynomials
354354
for (auto [g_poly, g_shift_eval] : zip_view(g_polynomials, g_shift_evaluations)) {
355355
g_batched.add_scaled(g_poly, batching_scalar);
356356
batched_evaluation += batching_scalar * g_shift_eval;
@@ -360,7 +360,6 @@ template <typename Curve> class ZeroMorphProver_ {
360360
size_t num_groups = concatenation_groups.size();
361361
size_t num_chunks_per_group = concatenation_groups.empty() ? 0 : concatenation_groups[0].size();
362362
// Concatenated polynomials
363-
// std::vector<Polynomial> concatenated_polynomials;
364363
Polynomial concatenated_batched(N);
365364

366365
// construct concatention_groups_batched
@@ -381,7 +380,7 @@ template <typename Curve> class ZeroMorphProver_ {
381380

382381
// Compute the full batched polynomial f = f_batched + g_batched.shifted() = f_batched + h_batched. This is the
383382
// polynomial for which we compute the quotients q_k and prove f(u) = v_batched.
384-
auto f_polynomial = f_batched;
383+
Polynomial f_polynomial = f_batched;
385384
f_polynomial += g_batched.shifted();
386385
f_polynomial += concatenated_batched;
387386

@@ -625,8 +624,8 @@ template <typename Curve> class ZeroMorphVerifier_ {
625624
}
626625

627626
/**
628-
* @brief Verify a set of multilinear evaluation claims for unshifted polynomials f_i and to-be-shifted polynomials
629-
* g_i
627+
* @brief Verify a set of multilinear evaluation claims for unshifted polynomials f_i and to-be-shifted
628+
* polynomials g_i
630629
*
631630
* @param commitments Commitments to polynomials f_i and g_i (unshifted and to-be-shifted)
632631
* @param claimed_evaluations Claimed evaluations v_i = f_i(u) and w_i = h_i(u) = g_i_shifted(u)
@@ -710,4 +709,4 @@ template <typename Curve> class ZeroMorphVerifier_ {
710709
}
711710
};
712711

713-
} // namespace proof_system::honk::pcs::zeromorph
712+
} // namespace proof_system::honk::pcs::zeromorph

barretenberg/cpp/src/barretenberg/commitment_schemes/zeromorph/zeromorph.test.cpp

+4-17
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,10 @@ template <class Curve> class ZeroMorphTest : public CommitmentTest<Curve> {
3737
*/
3838
bool execute_zeromorph_protocol(size_t NUM_UNSHIFTED, size_t NUM_SHIFTED)
3939
{
40-
bool verified = false;
41-
4240
size_t N = 16;
4341
size_t log_N = numeric::get_msb(N);
4442

45-
auto u_challenge = this->random_evaluation_point(log_N);
43+
std::vector<Fr> u_challenge = this->random_evaluation_point(log_N);
4644

4745
// Construct some random multilinear polynomials f_i and their evaluations v_i = f_i(u)
4846
std::vector<Polynomial> f_polynomials; // unshifted polynomials
@@ -94,7 +92,7 @@ template <class Curve> class ZeroMorphTest : public CommitmentTest<Curve> {
9492
auto pairing_points = ZeroMorphVerifier::verify(
9593
f_commitments, g_commitments, v_evaluations, w_evaluations, u_challenge, verifier_transcript);
9694

97-
verified = this->vk()->pairing_check(pairing_points[0], pairing_points[1]);
95+
bool verified = this->vk()->pairing_check(pairing_points[0], pairing_points[1]);
9896

9997
// The prover and verifier manifests should agree
10098
EXPECT_EQ(prover_transcript->get_manifest(), verifier_transcript->get_manifest());
@@ -225,17 +223,6 @@ template <class Curve> class ZeroMorphWithConcatenationTest : public CommitmentT
225223
// Initialize an empty BaseTranscript
226224
auto prover_transcript = BaseTranscript::prover_init_empty();
227225

228-
std::vector<std::span<Fr>> concatenated_polynomials_views;
229-
for (auto& poly : concatenated_polynomials) {
230-
concatenated_polynomials_views.emplace_back(poly);
231-
}
232-
233-
std::vector<std::vector<std::span<Fr>>> concatenation_groups_views(concatenation_groups.size());
234-
for (auto [group_of_polys, group_of_views] : zip_view(concatenation_groups, concatenation_groups_views)) {
235-
for (auto& poly : group_of_polys) {
236-
group_of_views.emplace_back(poly);
237-
}
238-
}
239226
// Execute Prover protocol
240227
ZeroMorphProver::prove(f_polynomials, // unshifted
241228
g_polynomials, // to-be-shifted
@@ -244,9 +231,9 @@ template <class Curve> class ZeroMorphWithConcatenationTest : public CommitmentT
244231
u_challenge,
245232
this->commitment_key,
246233
prover_transcript,
247-
concatenated_polynomials_views,
234+
concatenated_polynomials,
248235
c_evaluations,
249-
to_vector_of_ref_vectors(concatenation_groups_views));
236+
to_vector_of_ref_vectors(concatenation_groups));
250237

251238
auto verifier_transcript = BaseTranscript::verifier_init_empty(prover_transcript);
252239

barretenberg/cpp/src/barretenberg/common/ref_vector.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,12 @@ template <typename T> RefVector<T> concatenate(const RefVector<T>& ref_vector, c
139139
return concatenated;
140140
}
141141

142+
/* @details Ensures a nested vector holds reference objects */
142143
template <typename T> static std::vector<RefVector<T>> to_vector_of_ref_vectors(std::vector<std::vector<T>>& vec)
143144
{
144145
std::vector<RefVector<T>> result;
145146
for (std::vector<T>& inner : vec) {
146147
result.push_back(RefVector{ inner });
147148
}
148149
return result;
149-
}
150+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "std_string.hpp"
2+
#include <algorithm>
3+
#include <cctype>
4+
#include <iostream>
5+
#include <locale>
6+
#include <sstream>
7+
#include <vector>
8+
9+
namespace barretenberg::detail {
10+
std::vector<std::string> split(const std::string& str, char delimiter)
11+
{
12+
std::vector<std::string> result;
13+
std::istringstream iss(str);
14+
std::string token;
15+
16+
while (std::getline(iss, token, delimiter)) {
17+
result.push_back(token);
18+
}
19+
20+
return result;
21+
}
22+
23+
// trim from start (in place)
24+
void ltrim(std::string& s)
25+
{
26+
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { return !std::isspace(ch); }));
27+
}
28+
29+
// trim from end (in place)
30+
void rtrim(std::string& s)
31+
{
32+
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { return !std::isspace(ch); }).base(), s.end());
33+
}
34+
35+
// trim from both ends (in place)
36+
void trim(std::string& s)
37+
{
38+
rtrim(s);
39+
ltrim(s);
40+
}
41+
std::vector<std::string> split_and_trim(const std::string& str, char delimiter)
42+
{
43+
std::vector<std::string> ret = split(str, delimiter);
44+
for (std::string& part : ret) {
45+
trim(part);
46+
}
47+
return ret;
48+
}
49+
} // namespace barretenberg::detail
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <string>
2+
#include <vector>
3+
4+
namespace barretenberg::detail {
5+
std::vector<std::string> split(const std::string& str, char delimiter);
6+
// trim from start (in place)
7+
void ltrim(std::string& s);
8+
// trim from end (in place)
9+
void rtrim(std::string& s);
10+
// trim from both ends (in place)
11+
void trim(std::string& s);
12+
13+
// Used to extract variables from a macro #__VA_ARGS__
14+
std::vector<std::string> split_and_trim(const std::string& str, char delimiter);
15+
} // namespace barretenberg::detail
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#pragma once
2+
#include <vector>
3+
4+
// TODO(https://github.com/AztecProtocol/barretenberg/issues/794) namespace this once convenient
5+
/**
6+
* @brief Concatenates multiple std::vector objects into a single std::vector.
7+
*
8+
* @tparam T The type of elements in the std::vector.
9+
* @param vectors The std::vector objects to be concatenated.
10+
* @return std::vector object containing all elements from the input vectors.
11+
*/
12+
template <typename T> std::vector<T> concatenate(const std::vector<T>& vector, const auto&... vectors)
13+
{
14+
std::vector<T> concatenated;
15+
// Reserve our final space
16+
concatenated.reserve(vector.size() + (vectors.size() + ...));
17+
18+
auto append = [&](const auto& vec) { std::copy(vec.begin(), vec.end(), std::back_inserter(concatenated)); };
19+
20+
append(vector);
21+
// Unpack and append each std::vector's elements to concatenated
22+
(append(vectors), ...);
23+
24+
return concatenated;
25+
}

0 commit comments

Comments
 (0)