diff --git a/barretenberg/cpp/src/barretenberg/benchmark/honk_bench/ultra_honk_rounds.bench.cpp b/barretenberg/cpp/src/barretenberg/benchmark/honk_bench/ultra_honk_rounds.bench.cpp index 46642345ef4..a406c1a8591 100644 --- a/barretenberg/cpp/src/barretenberg/benchmark/honk_bench/ultra_honk_rounds.bench.cpp +++ b/barretenberg/cpp/src/barretenberg/benchmark/honk_bench/ultra_honk_rounds.bench.cpp @@ -45,7 +45,7 @@ BBERG_PROFILE static void test_round(State& state, size_t index) noexcept for (auto _ : state) { state.PauseTiming(); honk::UltraComposer composer; - // TODO: https://github.com/AztecProtocol/barretenberg/issues/761 benchmark both sparse and dense circuits + // TODO(https://github.com/AztecProtocol/barretenberg/issues/761) benchmark both sparse and dense circuits honk::UltraProver prover = bench_utils::get_prover( composer, &bench_utils::generate_ecdsa_verification_test_circuit, 10); test_round_inner(state, prover, index); diff --git a/barretenberg/cpp/src/barretenberg/commitment_schemes/gemini/gemini.cpp b/barretenberg/cpp/src/barretenberg/commitment_schemes/gemini/gemini.cpp index 5e3c91504b2..bbed763a388 100644 --- a/barretenberg/cpp/src/barretenberg/commitment_schemes/gemini/gemini.cpp +++ b/barretenberg/cpp/src/barretenberg/commitment_schemes/gemini/gemini.cpp @@ -77,7 +77,7 @@ std::vector> Gemi Polynomial& batched_G = gemini_polynomials.emplace_back(std::move(batched_to_be_shifted)); constexpr size_t offset_to_folded = 2; // Offset because of F an G // A₀(X) = F(X) + G↺(X) = F(X) + G(X)/X. - Polynomial A_0(batched_F); + Polynomial A_0 = batched_F; A_0 += batched_G.shifted(); // Allocate everything before parallel computation diff --git a/barretenberg/cpp/src/barretenberg/commitment_schemes/kzg/kzg.hpp b/barretenberg/cpp/src/barretenberg/commitment_schemes/kzg/kzg.hpp index 926923d767e..3c34333f45e 100644 --- a/barretenberg/cpp/src/barretenberg/commitment_schemes/kzg/kzg.hpp +++ b/barretenberg/cpp/src/barretenberg/commitment_schemes/kzg/kzg.hpp @@ -33,7 +33,7 @@ template class KZG { const Polynomial& polynomial, const std::shared_ptr& prover_trancript) { - Polynomial quotient(polynomial); + Polynomial quotient = polynomial; quotient[0] -= opening_pair.evaluation; // Computes the coefficients for the quotient polynomial q(X) = (p(X) - v) / (X - r) through an FFT quotient.factor_roots(opening_pair.challenge); diff --git a/barretenberg/cpp/src/barretenberg/commitment_schemes/shplonk/shplonk.test.cpp b/barretenberg/cpp/src/barretenberg/commitment_schemes/shplonk/shplonk.test.cpp index aa3734fe4e1..12232f946a8 100644 --- a/barretenberg/cpp/src/barretenberg/commitment_schemes/shplonk/shplonk.test.cpp +++ b/barretenberg/cpp/src/barretenberg/commitment_schemes/shplonk/shplonk.test.cpp @@ -44,7 +44,7 @@ TYPED_TEST(ShplonkTest, ShplonkSimple) // Aggregate polynomials and their opening pairs std::vector opening_pairs = { { r1, eval1 }, { r2, eval2 } }; - std::vector polynomials = { poly1, poly2 }; + std::vector polynomials = { poly1.share(), poly2.share() }; // Execute the shplonk prover functionality const Fr nu_challenge = prover_transcript->get_challenge("Shplonk:nu"); diff --git a/barretenberg/cpp/src/barretenberg/commitment_schemes/zeromorph/zeromorph.hpp b/barretenberg/cpp/src/barretenberg/commitment_schemes/zeromorph/zeromorph.hpp index 80a8c3e4508..034bf007d20 100644 --- a/barretenberg/cpp/src/barretenberg/commitment_schemes/zeromorph/zeromorph.hpp +++ b/barretenberg/cpp/src/barretenberg/commitment_schemes/zeromorph/zeromorph.hpp @@ -1,4 +1,5 @@ #pragma once +#include "barretenberg/commitment_schemes/commitment_key.hpp" #include "barretenberg/common/ref_vector.hpp" #include "barretenberg/common/zip_view.hpp" #include "barretenberg/polynomials/polynomial.hpp" @@ -34,8 +35,8 @@ template class ZeroMorphProver_ { using Commitment = typename Curve::AffineElement; using Polynomial = barretenberg::Polynomial; - // TODO(#742): Set this N_max to be the number of G1 elements in the mocked zeromorph SRS once it's in place. (Then, - // eventually, set it based on the real SRS). For now we set it to be large but more or less arbitrary. + // TODO(#742): Set this N_max to be the number of G1 elements in the mocked zeromorph SRS once it's in place. + // (Then, eventually, set it based on the real SRS). For now we set it to be large but more or less arbitrary. static const size_t N_max = 1 << 22; public: @@ -59,7 +60,7 @@ template class ZeroMorphProver_ { * @param u_challenge Multivariate challenge u = (u_0, ..., u_{d-1}) * @return std::vector The quotients q_k */ - static std::vector compute_multilinear_quotients(Polynomial polynomial, std::span u_challenge) + static std::vector compute_multilinear_quotients(Polynomial polynomial, std::span u_challenge) { size_t log_N = numeric::get_msb(polynomial.size()); // The size of the multilinear challenge must equal the log of the polynomial size @@ -74,12 +75,12 @@ template class ZeroMorphProver_ { // Compute the coefficients of q_{n-1} size_t size_q = 1 << (log_N - 1); - Polynomial q = Polynomial(size_q); + Polynomial q{ size_q }; for (size_t l = 0; l < size_q; ++l) { q[l] = polynomial[size_q + l] - polynomial[l]; } - quotients[log_N - 1] = q; + quotients[log_N - 1] = q.share(); std::vector f_k; f_k.resize(size_q); @@ -94,13 +95,13 @@ template class ZeroMorphProver_ { } size_q = size_q / 2; - q = Polynomial(size_q); + q = Polynomial{ size_q }; for (size_t l = 0; l < size_q; ++l) { q[l] = f_k[size_q + l] - f_k[l]; } - quotients[log_N - k - 1] = q; + quotients[log_N - k - 1] = q.share(); g = f_k; } @@ -206,7 +207,7 @@ template class ZeroMorphProver_ { Polynomial& g_batched, std::vector& quotients, FF v_evaluation, - std::span u_challenge, + std::span u_challenge, FF x_challenge, std::vector concatenation_groups_batched = {}) { @@ -305,8 +306,8 @@ template class ZeroMorphProver_ { } /** - * @brief Prove a set of multilinear evaluation claims for unshifted polynomials f_i and to-be-shifted polynomials - * g_i + * @brief Prove a set of multilinear evaluation claims for unshifted polynomials f_i and to-be-shifted + * polynomials g_i * * @param f_polynomials Unshifted polynomials * @param g_polynomials To-be-shifted polynomials (of which the shifts h_i were evaluated by sumcheck) @@ -315,23 +316,22 @@ template class ZeroMorphProver_ { * @param commitment_key * @param transcript */ - static void prove(const auto& f_polynomials, - const auto& g_polynomials, - auto&& f_evaluations, - auto&& g_shift_evaluations, - auto& multilinear_challenge, - auto& commitment_key, - auto transcript, - const std::vector>& concatenated_polynomials = {}, + static void prove(const std::vector& f_polynomials, + const std::vector& g_polynomials, + const std::vector& f_evaluations, + const std::vector& g_shift_evaluations, + const std::vector& multilinear_challenge, + const std::shared_ptr>& commitment_key, + const std::shared_ptr& transcript, + const std::vector& concatenated_polynomials = {}, const std::vector& concatenated_evaluations = {}, - // TODO(https://github.com/AztecProtocol/barretenberg/issues/743) remove span - const std::vector>>& concatenation_groups = {}) + const std::vector>& concatenation_groups = {}) { // Generate batching challenge \rho and powers 1,...,\rho^{m-1} const FF rho = transcript->get_challenge("rho"); // Extract multilinear challenge u and claimed multilinear evaluations from Sumcheck output - std::span u_challenge = multilinear_challenge; + std::span u_challenge = multilinear_challenge; size_t log_N = u_challenge.size(); size_t N = 1 << log_N; @@ -341,16 +341,16 @@ template class ZeroMorphProver_ { // v = sum_{i=0}^{m-1}\rho^i*f_i(u) + sum_{i=0}^{l-1}\rho^{m+i}*h_i(u). // Note: g_batched is formed from the to-be-shifted polynomials, but the batched evaluation incorporates the // evaluations produced by sumcheck of h_i = g_i_shifted. - auto batched_evaluation = FF(0); + FF batched_evaluation{ 0 }; Polynomial f_batched(N); // batched unshifted polynomials - FF batching_scalar = FF(1); + FF batching_scalar{ 1 }; for (auto [f_poly, f_eval] : zip_view(f_polynomials, f_evaluations)) { f_batched.add_scaled(f_poly, batching_scalar); batched_evaluation += batching_scalar * f_eval; batching_scalar *= rho; } - Polynomial g_batched(N); // batched to-be-shifted polynomials + Polynomial g_batched{ N }; // batched to-be-shifted polynomials for (auto [g_poly, g_shift_eval] : zip_view(g_polynomials, g_shift_evaluations)) { g_batched.add_scaled(g_poly, batching_scalar); batched_evaluation += batching_scalar * g_shift_eval; @@ -360,7 +360,6 @@ template class ZeroMorphProver_ { size_t num_groups = concatenation_groups.size(); size_t num_chunks_per_group = concatenation_groups.empty() ? 0 : concatenation_groups[0].size(); // Concatenated polynomials - // std::vector concatenated_polynomials; Polynomial concatenated_batched(N); // construct concatention_groups_batched @@ -381,7 +380,7 @@ template class ZeroMorphProver_ { // Compute the full batched polynomial f = f_batched + g_batched.shifted() = f_batched + h_batched. This is the // polynomial for which we compute the quotients q_k and prove f(u) = v_batched. - auto f_polynomial = f_batched; + Polynomial f_polynomial = f_batched; f_polynomial += g_batched.shifted(); f_polynomial += concatenated_batched; @@ -625,8 +624,8 @@ template class ZeroMorphVerifier_ { } /** - * @brief Verify a set of multilinear evaluation claims for unshifted polynomials f_i and to-be-shifted polynomials - * g_i + * @brief Verify a set of multilinear evaluation claims for unshifted polynomials f_i and to-be-shifted + * polynomials g_i * * @param commitments Commitments to polynomials f_i and g_i (unshifted and to-be-shifted) * @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 class ZeroMorphVerifier_ { } }; -} // namespace proof_system::honk::pcs::zeromorph \ No newline at end of file +} // namespace proof_system::honk::pcs::zeromorph diff --git a/barretenberg/cpp/src/barretenberg/commitment_schemes/zeromorph/zeromorph.test.cpp b/barretenberg/cpp/src/barretenberg/commitment_schemes/zeromorph/zeromorph.test.cpp index d8b878d24f2..11b13d9c43a 100644 --- a/barretenberg/cpp/src/barretenberg/commitment_schemes/zeromorph/zeromorph.test.cpp +++ b/barretenberg/cpp/src/barretenberg/commitment_schemes/zeromorph/zeromorph.test.cpp @@ -37,12 +37,10 @@ template class ZeroMorphTest : public CommitmentTest { */ bool execute_zeromorph_protocol(size_t NUM_UNSHIFTED, size_t NUM_SHIFTED) { - bool verified = false; - size_t N = 16; size_t log_N = numeric::get_msb(N); - auto u_challenge = this->random_evaluation_point(log_N); + std::vector u_challenge = this->random_evaluation_point(log_N); // Construct some random multilinear polynomials f_i and their evaluations v_i = f_i(u) std::vector f_polynomials; // unshifted polynomials @@ -94,7 +92,7 @@ template class ZeroMorphTest : public CommitmentTest { auto pairing_points = ZeroMorphVerifier::verify( f_commitments, g_commitments, v_evaluations, w_evaluations, u_challenge, verifier_transcript); - verified = this->vk()->pairing_check(pairing_points[0], pairing_points[1]); + bool verified = this->vk()->pairing_check(pairing_points[0], pairing_points[1]); // The prover and verifier manifests should agree EXPECT_EQ(prover_transcript->get_manifest(), verifier_transcript->get_manifest()); @@ -225,17 +223,6 @@ template class ZeroMorphWithConcatenationTest : public CommitmentT // Initialize an empty BaseTranscript auto prover_transcript = BaseTranscript::prover_init_empty(); - std::vector> concatenated_polynomials_views; - for (auto& poly : concatenated_polynomials) { - concatenated_polynomials_views.emplace_back(poly); - } - - std::vector>> concatenation_groups_views(concatenation_groups.size()); - for (auto [group_of_polys, group_of_views] : zip_view(concatenation_groups, concatenation_groups_views)) { - for (auto& poly : group_of_polys) { - group_of_views.emplace_back(poly); - } - } // Execute Prover protocol ZeroMorphProver::prove(f_polynomials, // unshifted g_polynomials, // to-be-shifted @@ -244,9 +231,9 @@ template class ZeroMorphWithConcatenationTest : public CommitmentT u_challenge, this->commitment_key, prover_transcript, - concatenated_polynomials_views, + concatenated_polynomials, c_evaluations, - to_vector_of_ref_vectors(concatenation_groups_views)); + to_vector_of_ref_vectors(concatenation_groups)); auto verifier_transcript = BaseTranscript::verifier_init_empty(prover_transcript); diff --git a/barretenberg/cpp/src/barretenberg/common/ref_vector.hpp b/barretenberg/cpp/src/barretenberg/common/ref_vector.hpp index 5304f8ea77a..523d75b8f5f 100644 --- a/barretenberg/cpp/src/barretenberg/common/ref_vector.hpp +++ b/barretenberg/cpp/src/barretenberg/common/ref_vector.hpp @@ -139,6 +139,7 @@ template RefVector concatenate(const RefVector& ref_vector, c return concatenated; } +/* @details Ensures a nested vector holds reference objects */ template static std::vector> to_vector_of_ref_vectors(std::vector>& vec) { std::vector> result; @@ -146,4 +147,4 @@ template static std::vector> to_vector_of_ref_vectors( result.push_back(RefVector{ inner }); } return result; -} +} \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/common/std_string.cpp b/barretenberg/cpp/src/barretenberg/common/std_string.cpp new file mode 100644 index 00000000000..dc876db9ae2 --- /dev/null +++ b/barretenberg/cpp/src/barretenberg/common/std_string.cpp @@ -0,0 +1,49 @@ +#include "std_string.hpp" +#include +#include +#include +#include +#include +#include + +namespace barretenberg::detail { +std::vector split(const std::string& str, char delimiter) +{ + std::vector result; + std::istringstream iss(str); + std::string token; + + while (std::getline(iss, token, delimiter)) { + result.push_back(token); + } + + return result; +} + +// trim from start (in place) +void ltrim(std::string& s) +{ + s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { return !std::isspace(ch); })); +} + +// trim from end (in place) +void rtrim(std::string& s) +{ + s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { return !std::isspace(ch); }).base(), s.end()); +} + +// trim from both ends (in place) +void trim(std::string& s) +{ + rtrim(s); + ltrim(s); +} +std::vector split_and_trim(const std::string& str, char delimiter) +{ + std::vector ret = split(str, delimiter); + for (std::string& part : ret) { + trim(part); + } + return ret; +} +} // namespace barretenberg::detail \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/common/std_string.hpp b/barretenberg/cpp/src/barretenberg/common/std_string.hpp new file mode 100644 index 00000000000..5b5d132c921 --- /dev/null +++ b/barretenberg/cpp/src/barretenberg/common/std_string.hpp @@ -0,0 +1,15 @@ +#include +#include + +namespace barretenberg::detail { +std::vector split(const std::string& str, char delimiter); +// trim from start (in place) +void ltrim(std::string& s); +// trim from end (in place) +void rtrim(std::string& s); +// trim from both ends (in place) +void trim(std::string& s); + +// Used to extract variables from a macro #__VA_ARGS__ +std::vector split_and_trim(const std::string& str, char delimiter); +} // namespace barretenberg::detail \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/common/std_vector.hpp b/barretenberg/cpp/src/barretenberg/common/std_vector.hpp new file mode 100644 index 00000000000..c3bf373820c --- /dev/null +++ b/barretenberg/cpp/src/barretenberg/common/std_vector.hpp @@ -0,0 +1,25 @@ +#pragma once +#include + +// TODO(https://github.com/AztecProtocol/barretenberg/issues/794) namespace this once convenient +/** + * @brief Concatenates multiple std::vector objects into a single std::vector. + * + * @tparam T The type of elements in the std::vector. + * @param vectors The std::vector objects to be concatenated. + * @return std::vector object containing all elements from the input vectors. + */ +template std::vector concatenate(const std::vector& vector, const auto&... vectors) +{ + std::vector concatenated; + // Reserve our final space + concatenated.reserve(vector.size() + (vectors.size() + ...)); + + auto append = [&](const auto& vec) { std::copy(vec.begin(), vec.end(), std::back_inserter(concatenated)); }; + + append(vector); + // Unpack and append each std::vector's elements to concatenated + (append(vectors), ...); + + return concatenated; +} \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/common/zip_view.hpp b/barretenberg/cpp/src/barretenberg/common/zip_view.hpp index f4683049c35..964888ac644 100644 --- a/barretenberg/cpp/src/barretenberg/common/zip_view.hpp +++ b/barretenberg/cpp/src/barretenberg/common/zip_view.hpp @@ -75,6 +75,7 @@ static_assert(__cplusplus >= 201703L, " must be c++17 or greater"); // could be rewritten in c++11, but the features you must use will be buggy // in an older compiler anyways. +#include "barretenberg/common/assert.hpp" #include #include #include @@ -154,13 +155,22 @@ class zip_iterator { } }; +enum class ZipAllowDifferentSizes { FLAG }; template class zip_view { using arg_indexes = std::make_index_sequence; public: zip_view(S... args) : args(std::forward(args)...) - {} + { + // min size matches max size + ASSERT(size() == max_size_impl(arg_indexes{})); + } + zip_view(ZipAllowDifferentSizes /*unused*/, S... args) + : args(std::forward(args)...) + { + // Same in a release build, in a debug build doesn't error with different container sizes + } auto begin() const { return get_begins(arg_indexes{}); } auto end() const { return get_ends(arg_indexes{}); } [[nodiscard]] std::size_t size() const { return size_impl(arg_indexes{}); } @@ -176,6 +186,10 @@ template class zip_view { return zip_iterator(std::make_tuple(std::get(args).end()...), std::make_tuple(std::get(args).end()...)); } template auto size_impl(std::index_sequence) const + { + return std::min({ std::size_t(std::get(args).size())... }); + } + template auto max_size_impl(std::index_sequence) const { return std::max({ std::size_t(std::get(args).size())... }); } @@ -188,3 +202,6 @@ template class zip_view { // deduction guide, template zip_view(S&&...) -> zip_view; + +// deduction guide, +template zip_view(ZipAllowDifferentSizes, S&&...) -> zip_view; diff --git a/barretenberg/cpp/src/barretenberg/eccvm/eccvm_composer.cpp b/barretenberg/cpp/src/barretenberg/eccvm/eccvm_composer.cpp index c6df4cbddb2..97202fa2da4 100644 --- a/barretenberg/cpp/src/barretenberg/eccvm/eccvm_composer.cpp +++ b/barretenberg/cpp/src/barretenberg/eccvm/eccvm_composer.cpp @@ -84,7 +84,7 @@ std::shared_ptr ECCVMComposer_::compute_pro const size_t n = proving_key->circuit_size; typename Flavor::Polynomial lagrange_polynomial_second(n); lagrange_polynomial_second[1] = 1; - proving_key->lagrange_second = lagrange_polynomial_second; + proving_key->lagrange_second = lagrange_polynomial_second.share(); } proving_key->contains_recursive_proof = false; @@ -119,4 +119,4 @@ std::shared_ptr ECCVMComposer_::comput } template class ECCVMComposer_; -} // namespace proof_system::honk \ No newline at end of file +} // namespace proof_system::honk diff --git a/barretenberg/cpp/src/barretenberg/eccvm/eccvm_prover.cpp b/barretenberg/cpp/src/barretenberg/eccvm/eccvm_prover.cpp index 60b5804a47d..f43a46d64f1 100644 --- a/barretenberg/cpp/src/barretenberg/eccvm/eccvm_prover.cpp +++ b/barretenberg/cpp/src/barretenberg/eccvm/eccvm_prover.cpp @@ -29,115 +29,16 @@ ECCVMProver_::ECCVMProver_(const std::shared_ptrtranscript_add; - prover_polynomials.transcript_mul = key->transcript_mul; - prover_polynomials.transcript_eq = key->transcript_eq; - prover_polynomials.transcript_collision_check = key->transcript_collision_check; - prover_polynomials.transcript_msm_transition = key->transcript_msm_transition; - prover_polynomials.transcript_pc = key->transcript_pc; - prover_polynomials.transcript_msm_count = key->transcript_msm_count; - prover_polynomials.transcript_Px = key->transcript_Px; - prover_polynomials.transcript_Py = key->transcript_Py; - prover_polynomials.transcript_z1 = key->transcript_z1; - prover_polynomials.transcript_z2 = key->transcript_z2; - prover_polynomials.transcript_z1zero = key->transcript_z1zero; - prover_polynomials.transcript_z2zero = key->transcript_z2zero; - prover_polynomials.transcript_op = key->transcript_op; - prover_polynomials.transcript_accumulator_x = key->transcript_accumulator_x; - prover_polynomials.transcript_accumulator_y = key->transcript_accumulator_y; - prover_polynomials.transcript_msm_x = key->transcript_msm_x; - prover_polynomials.transcript_msm_y = key->transcript_msm_y; - prover_polynomials.precompute_pc = key->precompute_pc; - prover_polynomials.precompute_point_transition = key->precompute_point_transition; - prover_polynomials.precompute_round = key->precompute_round; - prover_polynomials.precompute_scalar_sum = key->precompute_scalar_sum; - prover_polynomials.precompute_s1hi = key->precompute_s1hi; - prover_polynomials.precompute_s1lo = key->precompute_s1lo; - prover_polynomials.precompute_s2hi = key->precompute_s2hi; - prover_polynomials.precompute_s2lo = key->precompute_s2lo; - prover_polynomials.precompute_s3hi = key->precompute_s3hi; - prover_polynomials.precompute_s3lo = key->precompute_s3lo; - prover_polynomials.precompute_s4hi = key->precompute_s4hi; - prover_polynomials.precompute_s4lo = key->precompute_s4lo; - prover_polynomials.precompute_skew = key->precompute_skew; - prover_polynomials.precompute_dx = key->precompute_dx; - prover_polynomials.precompute_dy = key->precompute_dy; - prover_polynomials.precompute_tx = key->precompute_tx; - prover_polynomials.precompute_ty = key->precompute_ty; - prover_polynomials.msm_transition = key->msm_transition; - prover_polynomials.msm_add = key->msm_add; - prover_polynomials.msm_double = key->msm_double; - prover_polynomials.msm_skew = key->msm_skew; - prover_polynomials.msm_accumulator_x = key->msm_accumulator_x; - prover_polynomials.msm_accumulator_y = key->msm_accumulator_y; - prover_polynomials.msm_pc = key->msm_pc; - prover_polynomials.msm_size_of_msm = key->msm_size_of_msm; - prover_polynomials.msm_count = key->msm_count; - prover_polynomials.msm_round = key->msm_round; - prover_polynomials.msm_add1 = key->msm_add1; - prover_polynomials.msm_add2 = key->msm_add2; - prover_polynomials.msm_add3 = key->msm_add3; - prover_polynomials.msm_add4 = key->msm_add4; - prover_polynomials.msm_x1 = key->msm_x1; - prover_polynomials.msm_y1 = key->msm_y1; - prover_polynomials.msm_x2 = key->msm_x2; - prover_polynomials.msm_y2 = key->msm_y2; - prover_polynomials.msm_x3 = key->msm_x3; - prover_polynomials.msm_y3 = key->msm_y3; - prover_polynomials.msm_x4 = key->msm_x4; - prover_polynomials.msm_y4 = key->msm_y4; - prover_polynomials.msm_collision_x1 = key->msm_collision_x1; - prover_polynomials.msm_collision_x2 = key->msm_collision_x2; - prover_polynomials.msm_collision_x3 = key->msm_collision_x3; - prover_polynomials.msm_collision_x4 = key->msm_collision_x4; - prover_polynomials.msm_lambda1 = key->msm_lambda1; - prover_polynomials.msm_lambda2 = key->msm_lambda2; - prover_polynomials.msm_lambda3 = key->msm_lambda3; - prover_polynomials.msm_lambda4 = key->msm_lambda4; - prover_polynomials.msm_slice1 = key->msm_slice1; - prover_polynomials.msm_slice2 = key->msm_slice2; - prover_polynomials.msm_slice3 = key->msm_slice3; - prover_polynomials.msm_slice4 = key->msm_slice4; - prover_polynomials.transcript_accumulator_empty = key->transcript_accumulator_empty; - prover_polynomials.transcript_reset_accumulator = key->transcript_reset_accumulator; - prover_polynomials.precompute_select = key->precompute_select; - prover_polynomials.lookup_read_counts_0 = key->lookup_read_counts_0; - prover_polynomials.lookup_read_counts_1 = key->lookup_read_counts_1; - prover_polynomials.transcript_mul_shift = key->transcript_mul.shifted(); - prover_polynomials.transcript_msm_count_shift = key->transcript_msm_count.shifted(); - prover_polynomials.transcript_accumulator_x_shift = key->transcript_accumulator_x.shifted(); - prover_polynomials.transcript_accumulator_y_shift = key->transcript_accumulator_y.shifted(); - prover_polynomials.precompute_scalar_sum_shift = key->precompute_scalar_sum.shifted(); - prover_polynomials.precompute_s1hi_shift = key->precompute_s1hi.shifted(); - prover_polynomials.precompute_dx_shift = key->precompute_dx.shifted(); - prover_polynomials.precompute_dy_shift = key->precompute_dy.shifted(); - prover_polynomials.precompute_tx_shift = key->precompute_tx.shifted(); - prover_polynomials.precompute_ty_shift = key->precompute_ty.shifted(); - prover_polynomials.msm_transition_shift = key->msm_transition.shifted(); - prover_polynomials.msm_add_shift = key->msm_add.shifted(); - prover_polynomials.msm_double_shift = key->msm_double.shifted(); - prover_polynomials.msm_skew_shift = key->msm_skew.shifted(); - prover_polynomials.msm_accumulator_x_shift = key->msm_accumulator_x.shifted(); - prover_polynomials.msm_accumulator_y_shift = key->msm_accumulator_y.shifted(); - prover_polynomials.msm_count_shift = key->msm_count.shifted(); - prover_polynomials.msm_round_shift = key->msm_round.shifted(); - prover_polynomials.msm_add1_shift = key->msm_add1.shifted(); - prover_polynomials.msm_pc_shift = key->msm_pc.shifted(); - prover_polynomials.precompute_pc_shift = key->precompute_pc.shifted(); - prover_polynomials.transcript_pc_shift = key->transcript_pc.shifted(); - prover_polynomials.precompute_round_shift = key->precompute_round.shifted(); - prover_polynomials.transcript_accumulator_empty_shift = key->transcript_accumulator_empty.shifted(); - prover_polynomials.precompute_select_shift = key->precompute_select.shifted(); - prover_polynomials.lagrange_first = key->lagrange_first; - prover_polynomials.lagrange_second = key->lagrange_second; - prover_polynomials.lagrange_last = key->lagrange_last; - - prover_polynomials.lookup_inverses = key->lookup_inverses; + // this will be initialized properly later key->z_perm = Polynomial(key->circuit_size); - prover_polynomials.z_perm = key->z_perm; - prover_polynomials.z_perm_shift = key->z_perm; // this will be initialized properly later + for (auto [prover_poly, key_poly] : zip_view(prover_polynomials.get_unshifted(), key->get_all())) { + ASSERT(flavor_get_label(prover_polynomials, prover_poly) == flavor_get_label(*key, key_poly)); + prover_poly = key_poly.share(); + } + for (auto [prover_poly, key_poly] : zip_view(prover_polynomials.get_shifted(), key->get_to_be_shifted())) { + ASSERT(flavor_get_label(prover_polynomials, prover_poly) == (flavor_get_label(*key, key_poly) + "_shift")); + prover_poly = key_poly.shifted(); + } } /** @@ -186,7 +87,7 @@ template void ECCVMProver_::execute_log_derivative_ logderivative_library::compute_logderivative_inverse( prover_polynomials, relation_parameters, key->circuit_size); transcript->send_to_verifier(commitment_labels.lookup_inverses, commitment_key->commit(key->lookup_inverses)); - prover_polynomials.lookup_inverses = key->lookup_inverses; + prover_polynomials.lookup_inverses = key->lookup_inverses.share(); } /** diff --git a/barretenberg/cpp/src/barretenberg/flavor/ecc_vm.hpp b/barretenberg/cpp/src/barretenberg/flavor/ecc_vm.hpp index 2d864059b96..2322c43f5a2 100644 --- a/barretenberg/cpp/src/barretenberg/flavor/ecc_vm.hpp +++ b/barretenberg/cpp/src/barretenberg/flavor/ecc_vm.hpp @@ -201,7 +201,7 @@ template class ECCVMBa template class WitnessEntities : public WireEntities, public DerivedWitnessEntities { public: - DEFINE_COMPOUND_GET_ALL(WireEntities::get_all(), DerivedWitnessEntities::get_all()) + DEFINE_COMPOUND_GET_ALL(WireEntities, DerivedWitnessEntities) RefVector get_wires() { return WireEntities::get_all(); }; // The sorted concatenations of table and witness data needed for plookup. RefVector get_sorted_polynomials() { return {}; }; @@ -240,6 +240,38 @@ template class ECCVMBa precompute_select_shift, // column 24 z_perm_shift); // column 25 }; + + template + static RefVector get_to_be_shifted(PrecomputedAndWitnessEntitiesSuperset& entities) + { + // NOTE: must match order of ShiftedEntities above! + return { entities.transcript_mul, + entities.transcript_msm_count, + entities.transcript_accumulator_x, + entities.transcript_accumulator_y, + entities.precompute_scalar_sum, + entities.precompute_s1hi, + entities.precompute_dx, + entities.precompute_dy, + entities.precompute_tx, + entities.precompute_ty, + entities.msm_transition, + entities.msm_add, + entities.msm_double, + entities.msm_skew, + entities.msm_accumulator_x, + entities.msm_accumulator_y, + entities.msm_count, + entities.msm_round, + entities.msm_add1, + entities.msm_pc, + entities.precompute_pc, + entities.transcript_pc, + entities.precompute_round, + entities.transcript_accumulator_empty, + entities.precompute_select, + entities.z_perm }; + } /** * @brief A base class labelling all entities (for instance, all of the polynomials used by the prover during * sumcheck) in this Honk variant along with particular subsets of interest @@ -263,44 +295,14 @@ template class ECCVMBa {} // get_wires is inherited - DEFINE_COMPOUND_GET_ALL(PrecomputedEntities::get_all(), - WitnessEntities::get_all(), - ShiftedEntities::get_all()) + DEFINE_COMPOUND_GET_ALL(PrecomputedEntities, WitnessEntities, ShiftedEntities) // Gemini-specific getters. RefVector get_unshifted() { return concatenate(PrecomputedEntities::get_all(), WitnessEntities::get_all()); }; - RefVector get_to_be_shifted() - { - return { this->transcript_mul, - this->transcript_msm_count, - this->transcript_accumulator_x, - this->transcript_accumulator_y, - this->precompute_scalar_sum, - this->precompute_s1hi, - this->precompute_dx, - this->precompute_dy, - this->precompute_tx, - this->precompute_ty, - this->msm_transition, - this->msm_add, - this->msm_double, - this->msm_skew, - this->msm_accumulator_x, - this->msm_accumulator_y, - this->msm_count, - this->msm_round, - this->msm_add1, - this->msm_pc, - this->precompute_pc, - this->transcript_pc, - this->precompute_round, - this->transcript_accumulator_empty, - this->precompute_select, - this->z_perm }; - } + RefVector get_to_be_shifted() { return ECCVMBase::get_to_be_shifted(*this); } RefVector get_shifted() { return ShiftedEntities::get_all(); }; }; @@ -316,6 +318,7 @@ template class ECCVMBa using Base = ProvingKey_, WitnessEntities>; using Base::Base; + RefVector get_to_be_shifted() { return ECCVMBase::get_to_be_shifted(*this); } // The plookup wires that store plookup read data. std::array get_table_column_wires() { return {}; }; }; @@ -347,29 +350,6 @@ template class ECCVMBa AllValues(std::array _data_in) { this->_data = _data_in; } }; - /** - * @brief An owning container of polynomials. - * @warning When this was introduced it broke some of our design principles. - * - Execution trace builders don't handle "polynomials" because the interpretation of the execution trace - * columns as polynomials is a detail of the proving system, and trace builders are (sometimes in practice, - * always in principle) reusable for different proving protocols (e.g., Plonk and Honk). - * - Polynomial storage is handled by key classes. Polynomials aren't moved, but are accessed elsewhere by - * std::spans. - * - * We will consider revising this data model: TODO(https://github.com/AztecProtocol/barretenberg/issues/743) - */ - class AllPolynomials : public AllEntities { - public: - [[nodiscard]] size_t get_polynomial_size() const { return this->lagrange_first.size(); } - AllValues get_row(const size_t row_idx) const - { - AllValues result; - for (auto [result_field, polynomial] : zip_view(result.get_all(), this->get_all())) { - result_field = polynomial[row_idx]; - } - return result; - } - }; /** * @brief A container for polynomials produced after the first round of sumcheck. * @todo TODO(#394) Use polynomial classes for guaranteed memory alignment. @@ -403,10 +383,18 @@ template class ECCVMBa using ExtendedEdges = ProverUnivariates; /** - * @brief A container for the prover polynomials handles; only stores spans. + * @brief A container for the prover polynomials. */ - class ProverPolynomials : public AllEntities { + class ProverPolynomials : public AllEntities { public: + // Define all operations as default, except move construction/assignment + ProverPolynomials() = default; + ProverPolynomials& operator=(const ProverPolynomials&) = delete; + ProverPolynomials(const ProverPolynomials& o) = delete; + ProverPolynomials(ProverPolynomials&& o) noexcept = default; + ProverPolynomials& operator=(ProverPolynomials&& o) noexcept = default; + ~ProverPolynomials() = default; + [[nodiscard]] size_t get_polynomial_size() const { return this->lagrange_first.size(); } /** * @brief Returns the evaluations of all prover polynomials at one point on the boolean hypercube, which * represents one row in the execution trace. diff --git a/barretenberg/cpp/src/barretenberg/flavor/flavor.cpp b/barretenberg/cpp/src/barretenberg/flavor/flavor.cpp index 91a791d8b93..69e8e706929 100644 --- a/barretenberg/cpp/src/barretenberg/flavor/flavor.cpp +++ b/barretenberg/cpp/src/barretenberg/flavor/flavor.cpp @@ -1,3 +1,3 @@ #include "flavor.hpp" -// This file is here to make sure that a flavor object file is created. +// This file is here to make sure that a flavor object file is created. \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/flavor/flavor.hpp b/barretenberg/cpp/src/barretenberg/flavor/flavor.hpp index d2b16d473cf..959580c53c9 100644 --- a/barretenberg/cpp/src/barretenberg/flavor/flavor.hpp +++ b/barretenberg/cpp/src/barretenberg/flavor/flavor.hpp @@ -64,7 +64,9 @@ */ #pragma once +#include "barretenberg/common/ref_vector.hpp" #include "barretenberg/common/std_array.hpp" +#include "barretenberg/common/std_vector.hpp" #include "barretenberg/common/zip_view.hpp" #include "barretenberg/polynomials/barycentric.hpp" #include "barretenberg/polynomials/evaluation_domain.hpp" @@ -105,7 +107,14 @@ class ProvingKey_ : public PrecomputedPolynomials, public WitnessPolynomials { std::vector recursive_proof_public_input_indices; barretenberg::EvaluationDomain evaluation_domain; - auto precomputed_polynomials_get_all() { return PrecomputedPolynomials::get_all(); } + std::vector get_labels() const + { + return concatenate(PrecomputedPolynomials::get_labels(), WitnessPolynomials::get_labels()); + } + // This order matters! must match get_unshifted in entity classes + RefVector get_all() { return concatenate(get_precomputed_polynomials(), get_witness_polynomials()); } + RefVector get_witness_polynomials() { return WitnessPolynomials::get_all(); } + RefVector get_precomputed_polynomials() { return PrecomputedPolynomials::get_all(); } ProvingKey_() = default; ProvingKey_(const size_t circuit_size, const size_t num_public_inputs) { @@ -299,5 +308,15 @@ template concept UltraFlavor = IsAnyOf concept ECCVMFlavor = IsAnyOf; +template +inline std::string flavor_get_label(Container&& container, const Element& element) { + for (auto [label, data] : zip_view(container.get_labels(), container.get_all())) { + if (&data == &element) { + return label; + } + } + return "(unknown label)"; +} + // clang-format on } // namespace proof_system diff --git a/barretenberg/cpp/src/barretenberg/flavor/flavor.test.cpp b/barretenberg/cpp/src/barretenberg/flavor/flavor.test.cpp index ad4692dc26b..07d93358de2 100644 --- a/barretenberg/cpp/src/barretenberg/flavor/flavor.test.cpp +++ b/barretenberg/cpp/src/barretenberg/flavor/flavor.test.cpp @@ -38,66 +38,6 @@ TEST(Flavor, Getters) prover_polynomials.get_shifted().size() + prover_polynomials.get_unshifted().size()); // Commitment lables are stored in the flavor. EXPECT_EQ(commitment_labels.w_r, "W_R"); - - auto get_test_polynomial = [](size_t& idx) { - Flavor::Polynomial poly(4); - for (size_t i = 0; i < 4; i++) { - poly[i] = idx++; - }; - return poly; - }; - - size_t idx = 0; - auto w_l = get_test_polynomial(idx); - auto w_r = get_test_polynomial(idx); - auto w_o = get_test_polynomial(idx); - auto w_4 = get_test_polynomial(idx); - auto z_perm = get_test_polynomial(idx); - auto z_perm_shift = get_test_polynomial(idx); - auto q_m = get_test_polynomial(idx); - auto q_l = get_test_polynomial(idx); - auto q_r = get_test_polynomial(idx); - auto q_o = get_test_polynomial(idx); - auto q_c = get_test_polynomial(idx); - auto sigma_1 = get_test_polynomial(idx); - auto sigma_2 = get_test_polynomial(idx); - auto sigma_3 = get_test_polynomial(idx); - auto id_1 = get_test_polynomial(idx); - auto id_2 = get_test_polynomial(idx); - auto id_3 = get_test_polynomial(idx); - auto lagrange_first = get_test_polynomial(idx); - auto lagrange_last = get_test_polynomial(idx); - - prover_polynomials.w_l = w_l; - prover_polynomials.w_r = w_r; - prover_polynomials.w_o = w_o; - prover_polynomials.w_4 = w_4; - prover_polynomials.z_perm = z_perm; - prover_polynomials.z_perm_shift = z_perm_shift; - prover_polynomials.q_m = q_m; - prover_polynomials.q_l = q_l; - prover_polynomials.q_r = q_r; - prover_polynomials.q_o = q_o; - prover_polynomials.q_c = q_c; - prover_polynomials.sigma_1 = sigma_1; - prover_polynomials.sigma_2 = sigma_2; - prover_polynomials.sigma_3 = sigma_3; - prover_polynomials.id_1 = id_1; - prover_polynomials.id_2 = id_2; - prover_polynomials.id_3 = id_3; - prover_polynomials.lagrange_first = lagrange_first; - prover_polynomials.lagrange_last = lagrange_last; - - // You can set polynomial values directly through the symbol names - // and then access the values through the getters. - idx = 0; - for (auto& poly : prover_polynomials.get_wires()) { - EXPECT_EQ(poly[0], 4 * idx); - EXPECT_EQ(poly[1], 4 * idx + 1); - EXPECT_EQ(poly[2], 4 * idx + 2); - EXPECT_EQ(poly[3], 4 * idx + 3); - ++idx; - }; } TEST(Flavor, AllEntitiesSpecialMemberFunctions) @@ -115,7 +55,7 @@ TEST(Flavor, AllEntitiesSpecialMemberFunctions) // Test some special member functions. - polynomials_A.w_l = random_poly; + polynomials_A.w_l = random_poly.share(); ASSERT_EQ(random_poly, polynomials_A.w_l); diff --git a/barretenberg/cpp/src/barretenberg/flavor/flavor_macros.hpp b/barretenberg/cpp/src/barretenberg/flavor/flavor_macros.hpp index d25bb4eb513..de19c2d6bd2 100644 --- a/barretenberg/cpp/src/barretenberg/flavor/flavor_macros.hpp +++ b/barretenberg/cpp/src/barretenberg/flavor/flavor_macros.hpp @@ -8,14 +8,34 @@ #include "barretenberg/common/ref_vector.hpp" #include "barretenberg/common/std_array.hpp" +#include "barretenberg/common/std_string.hpp" +#include "barretenberg/common/std_vector.hpp" #include #include #include -template auto _refs_to_pointer_array(Refs&... refs) +namespace barretenberg::detail { +template constexpr std::size_t _va_count(Args&&... /*unused*/) { - return std::array{ &refs... }; + return sizeof...(Args); } +template constexpr std::size_t _sum_base_class_size(const T& arg) +{ + return (static_cast(arg).size() + ...); +} +template auto _concatenate_base_class_get_all(T& arg) +{ + return concatenate(static_cast(arg).get_all()...); +} +template auto _concatenate_base_class_get_all_const(const T& arg) +{ + return concatenate(static_cast(arg).get_all()...); +} +template auto _concatenate_base_class_get_labels(const T& arg) +{ + return concatenate(static_cast(arg).get_labels()...); +} +} // namespace barretenberg::detail #define DEFINE_REF_VIEW(...) \ [[nodiscard]] auto get_all() \ @@ -36,14 +56,30 @@ template auto _refs_to_pointer_array(Refs&... refs) */ #define DEFINE_FLAVOR_MEMBERS(DataType, ...) \ DataType __VA_ARGS__; \ - DEFINE_REF_VIEW(__VA_ARGS__) + DEFINE_REF_VIEW(__VA_ARGS__) \ + std::vector get_labels() const \ + { \ + return barretenberg::detail::split_and_trim(#__VA_ARGS__, ','); \ + } \ + constexpr std::size_t size() const \ + { \ + return barretenberg::detail::_va_count(__VA_ARGS__); \ + } #define DEFINE_COMPOUND_GET_ALL(...) \ [[nodiscard]] auto get_all() \ { \ - return concatenate(__VA_ARGS__); \ + return barretenberg::detail::_concatenate_base_class_get_all(*this); \ } \ [[nodiscard]] auto get_all() const \ { \ - return concatenate(__VA_ARGS__); \ + return barretenberg::detail::_concatenate_base_class_get_all_const(*this); \ + } \ + constexpr std::size_t size() const \ + { \ + return barretenberg::detail::_sum_base_class_size(*this); \ + } \ + std::vector get_labels() const \ + { \ + return barretenberg::detail::_concatenate_base_class_get_labels(*this); \ } diff --git a/barretenberg/cpp/src/barretenberg/flavor/generated/AvmMini_flavor.hpp b/barretenberg/cpp/src/barretenberg/flavor/generated/AvmMini_flavor.hpp index 718d61f3c89..899af8d2fbc 100644 --- a/barretenberg/cpp/src/barretenberg/flavor/generated/AvmMini_flavor.hpp +++ b/barretenberg/cpp/src/barretenberg/flavor/generated/AvmMini_flavor.hpp @@ -234,14 +234,21 @@ class AvmMiniFlavor { using Base = ProvingKey_, WitnessEntities>; using Base::Base; + RefVector get_to_be_shifted() + { + return { + memTrace_m_rw, + memTrace_m_addr, + memTrace_m_val, + + }; + }; // The plookup wires that store plookup read data. std::array get_table_column_wires() { return {}; }; }; using VerificationKey = VerificationKey_>; - using ProverPolynomials = AllEntities; - using FoldedPolynomials = AllEntities>; class AllValues : public AllEntities { @@ -250,19 +257,32 @@ class AvmMiniFlavor { using Base::Base; }; - class AllPolynomials : public AllEntities { + /** + * @brief A container for the prover polynomials handles. + */ + class ProverPolynomials : public AllEntities { public: - [[nodiscard]] size_t get_polynomial_size() const { return this->memTrace_m_clk.size(); } - [[nodiscard]] AllValues get_row(const size_t row_idx) const + // Define all operations as default, except move construction/assignment + ProverPolynomials() = default; + ProverPolynomials& operator=(const ProverPolynomials&) = delete; + ProverPolynomials(const ProverPolynomials& o) = delete; + ProverPolynomials(ProverPolynomials&& o) noexcept = default; + ProverPolynomials& operator=(ProverPolynomials&& o) noexcept = default; + ~ProverPolynomials() = default; + [[nodiscard]] size_t get_polynomial_size() const { return avmMini_clk.size(); } + /** + * @brief Returns the evaluations of all prover polynomials at one point on the boolean hypercube, which + * represents one row in the execution trace. + */ + [[nodiscard]] AllValues get_row(size_t row_idx) const { AllValues result; - for (auto [result_field, polynomial] : zip_view(result.get_all(), get_all())) { + for (auto [result_field, polynomial] : zip_view(result.get_all(), this->get_all())) { result_field = polynomial[row_idx]; } return result; } }; - using RowPolynomials = AllEntities; class PartiallyEvaluatedMultivariates : public AllEntities { diff --git a/barretenberg/cpp/src/barretenberg/flavor/goblin_translator.hpp b/barretenberg/cpp/src/barretenberg/flavor/goblin_translator.hpp index 24809478010..02ea81495a1 100644 --- a/barretenberg/cpp/src/barretenberg/flavor/goblin_translator.hpp +++ b/barretenberg/cpp/src/barretenberg/flavor/goblin_translator.hpp @@ -246,10 +246,10 @@ class GoblinTranslator { public DerivedWitnessEntities, public ConcatenatedRangeConstraints { public: - DEFINE_COMPOUND_GET_ALL(WireNonshiftedEntities::get_all(), - WireToBeShiftedEntities::get_all(), - DerivedWitnessEntities::get_all(), - ConcatenatedRangeConstraints::get_all()) + DEFINE_COMPOUND_GET_ALL(WireNonshiftedEntities, + WireToBeShiftedEntities, + DerivedWitnessEntities, + ConcatenatedRangeConstraints) RefVector get_wires() { @@ -342,12 +342,21 @@ class GoblinTranslator { }; // everything but ConcatenatedRangeConstraints + // TODO(https://github.com/AztecProtocol/barretenberg/issues/810) RefVector get_unshifted_wires() { return concatenate(WireNonshiftedEntities::get_all(), WireToBeShiftedEntities::get_all(), DerivedWitnessEntities::get_all()); - }; + } + // everything but ConcatenatedRangeConstraints + // TODO(https://github.com/AztecProtocol/barretenberg/issues/810) + std::vector get_unshifted_wire_labels() + { + return concatenate(WireNonshiftedEntities::get_labels(), + WireToBeShiftedEntities::get_labels(), + DerivedWitnessEntities::get_labels()); + } RefVector get_to_be_shifted() { return concatenate(WireToBeShiftedEntities::get_all(), @@ -647,9 +656,7 @@ class GoblinTranslator { this->ordered_range_constraints_4 }; } - DEFINE_COMPOUND_GET_ALL(PrecomputedEntities::get_all(), - WitnessEntities::get_all(), - ShiftedEntities::get_all()) + DEFINE_COMPOUND_GET_ALL(PrecomputedEntities, WitnessEntities, ShiftedEntities) /** * @brief Get the polynomials that are concatenated for the permutation relation * @@ -758,95 +765,8 @@ class GoblinTranslator { // Gemini-specific getters. RefVector get_unshifted() { - return { - this->x_lo_y_hi, - this->x_hi_z_1, - this->y_lo_z_2, - this->p_x_low_limbs, - this->p_x_low_limbs_range_constraint_0, - this->p_x_low_limbs_range_constraint_1, - this->p_x_low_limbs_range_constraint_2, - this->p_x_low_limbs_range_constraint_3, - this->p_x_low_limbs_range_constraint_4, - this->p_x_low_limbs_range_constraint_tail, - this->p_x_high_limbs, - this->p_x_high_limbs_range_constraint_0, - this->p_x_high_limbs_range_constraint_1, - this->p_x_high_limbs_range_constraint_2, - this->p_x_high_limbs_range_constraint_3, - this->p_x_high_limbs_range_constraint_4, - this->p_x_high_limbs_range_constraint_tail, - this->p_y_low_limbs, - this->p_y_low_limbs_range_constraint_0, - this->p_y_low_limbs_range_constraint_1, - this->p_y_low_limbs_range_constraint_2, - this->p_y_low_limbs_range_constraint_3, - this->p_y_low_limbs_range_constraint_4, - this->p_y_low_limbs_range_constraint_tail, - this->p_y_high_limbs, - this->p_y_high_limbs_range_constraint_0, - this->p_y_high_limbs_range_constraint_1, - this->p_y_high_limbs_range_constraint_2, - this->p_y_high_limbs_range_constraint_3, - this->p_y_high_limbs_range_constraint_4, - this->p_y_high_limbs_range_constraint_tail, - this->z_low_limbs, - this->z_low_limbs_range_constraint_0, - this->z_low_limbs_range_constraint_1, - this->z_low_limbs_range_constraint_2, - this->z_low_limbs_range_constraint_3, - this->z_low_limbs_range_constraint_4, - this->z_low_limbs_range_constraint_tail, - this->z_high_limbs, - this->z_high_limbs_range_constraint_0, - this->z_high_limbs_range_constraint_1, - this->z_high_limbs_range_constraint_2, - this->z_high_limbs_range_constraint_3, - this->z_high_limbs_range_constraint_4, - this->z_high_limbs_range_constraint_tail, - this->accumulators_binary_limbs_0, - this->accumulators_binary_limbs_1, - this->accumulators_binary_limbs_2, - this->accumulators_binary_limbs_3, - this->accumulator_low_limbs_range_constraint_0, - this->accumulator_low_limbs_range_constraint_1, - this->accumulator_low_limbs_range_constraint_2, - this->accumulator_low_limbs_range_constraint_3, - this->accumulator_low_limbs_range_constraint_4, - this->accumulator_low_limbs_range_constraint_tail, - this->accumulator_high_limbs_range_constraint_0, - this->accumulator_high_limbs_range_constraint_1, - this->accumulator_high_limbs_range_constraint_2, - this->accumulator_high_limbs_range_constraint_3, - this->accumulator_high_limbs_range_constraint_4, - this->accumulator_high_limbs_range_constraint_tail, - this->quotient_low_binary_limbs, - this->quotient_high_binary_limbs, - this->quotient_low_limbs_range_constraint_0, - this->quotient_low_limbs_range_constraint_1, - this->quotient_low_limbs_range_constraint_2, - this->quotient_low_limbs_range_constraint_3, - this->quotient_low_limbs_range_constraint_4, - this->quotient_low_limbs_range_constraint_tail, - this->quotient_high_limbs_range_constraint_0, - this->quotient_high_limbs_range_constraint_1, - this->quotient_high_limbs_range_constraint_2, - this->quotient_high_limbs_range_constraint_3, - this->quotient_high_limbs_range_constraint_4, - this->quotient_high_limbs_range_constraint_tail, - this->relation_wide_limbs, - this->relation_wide_limbs_range_constraint_0, - this->relation_wide_limbs_range_constraint_1, - this->relation_wide_limbs_range_constraint_2, - this->relation_wide_limbs_range_constraint_3, - this->ordered_range_constraints_0, - this->ordered_range_constraints_1, - this->ordered_range_constraints_2, - this->ordered_range_constraints_3, - this->ordered_range_constraints_4, - - this->z_perm, - }; + return concatenate(PrecomputedEntities::get_all(), + WitnessEntities::get_unshifted_wires()); } // get_to_be_shifted is inherited RefVector get_shifted() @@ -962,11 +882,11 @@ class GoblinTranslator { { os << "{ "; std::ios_base::fmtflags f(os.flags()); - for (size_t i = 0; i < NUM_ALL_ENTITIES - 1; i++) { - os << "e[" << std::setw(2) << i << "] = " << (a._data[i]) << ",\n"; + auto entities = a.get_all(); + for (size_t i = 0; i < entities.size() - 1; i++) { + os << "e[" << std::setw(2) << i << "] = " << (entities[i]) << ",\n"; } - os << "e[" << std::setw(2) << (NUM_ALL_ENTITIES - 1) << "] = " << std::get(a._data) - << " }"; + os << "e[" << std::setw(2) << (entities.size() - 1) << "] = " << entities[entities.size() - 1] << " }"; os.flags(f); return os; @@ -989,6 +909,19 @@ class GoblinTranslator { using Base = ProvingKey_, WitnessEntities>; using Base::Base; + // TODO(https://github.com/AztecProtocol/barretenberg/issues/810): get around this by properly having + // concatenated range be a concept outside of witnessentities + std::vector get_labels() + { + return concatenate(PrecomputedEntities::get_labels(), + WitnessEntities::get_unshifted_wire_labels()); + } + RefVector get_all() + { + return concatenate(PrecomputedEntities::get_all(), + WitnessEntities::get_unshifted_wires()); + } + ProvingKey(const size_t circuit_size) : ProvingKey_, WitnessEntities>(circuit_size, 0) @@ -1017,10 +950,17 @@ class GoblinTranslator { using Base::Base; }; /** - * @brief A container for the prover polynomials handles; only stores spans. + * @brief A container for the prover polynomials handles. */ - class ProverPolynomials : public AllEntities { + class ProverPolynomials : public AllEntities { public: + // Define all operations as default, except move construction/assignment + ProverPolynomials() = default; + ProverPolynomials& operator=(const ProverPolynomials&) = delete; + ProverPolynomials(const ProverPolynomials& o) = delete; + ProverPolynomials(ProverPolynomials&& o) noexcept = default; + ProverPolynomials& operator=(ProverPolynomials&& o) noexcept = default; + ~ProverPolynomials() = default; [[nodiscard]] size_t get_polynomial_size() const { return this->op.size(); } /** * @brief Returns the evaluations of all prover polynomials at one point on the boolean hypercube, which @@ -1041,28 +981,6 @@ class GoblinTranslator { */ using ProverPolynomialIds = AllEntities; - /** - * @brief An owning container of polynomials. - * @warning When this was introduced it broke some of our design principles. - * - Execution trace builders don't handle "polynomials" because the interpretation of the execution trace - * columns as polynomials is a detail of the proving system, and trace builders are (sometimes in practice, - * always in principle) reusable for different proving protocols (e.g., Plonk and Honk). - * - Polynomial storage is handled by key classes. Polynomials aren't moved, but are accessed elsewhere by - * std::spans. - * - * We will consider revising this data model: TODO(https://github.com/AztecProtocol/barretenberg/issues/743) - */ - class AllPolynomials : public AllEntities { - public: - [[nodiscard]] AllValues get_row(const size_t row_idx) const - { - AllValues result; - for (auto [result_field, polynomial] : zip_view(result.get_all(), this->get_all())) { - result_field = polynomial[row_idx]; - } - return result; - } - }; /** * @brief A container for polynomials produced after the first round of sumcheck. * @todo TODO(#394) Use polynomial classes for guaranteed memory alignment. diff --git a/barretenberg/cpp/src/barretenberg/flavor/goblin_ultra.hpp b/barretenberg/cpp/src/barretenberg/flavor/goblin_ultra.hpp index ce322761ccb..0849e885773 100644 --- a/barretenberg/cpp/src/barretenberg/flavor/goblin_ultra.hpp +++ b/barretenberg/cpp/src/barretenberg/flavor/goblin_ultra.hpp @@ -187,7 +187,7 @@ class GoblinUltra { template class WitnessEntities : public WireEntities, public DerivedEntities { public: - DEFINE_COMPOUND_GET_ALL(WireEntities::get_all(), DerivedEntities::get_all()) + DEFINE_COMPOUND_GET_ALL(WireEntities, DerivedEntities) RefVector get_wires() { return WireEntities::get_all(); }; RefVector get_ecc_op_wires() @@ -228,9 +228,7 @@ class GoblinUltra { public WitnessEntities, public ShiftedEntities { public: - DEFINE_COMPOUND_GET_ALL(PrecomputedEntities::get_all(), - WitnessEntities::get_all(), - ShiftedEntities::get_all()) + DEFINE_COMPOUND_GET_ALL(PrecomputedEntities, WitnessEntities, ShiftedEntities) RefVector get_wires() { return { this->w_l, this->w_r, this->w_o, this->w_4 }; }; RefVector get_ecc_op_wires() @@ -240,105 +238,16 @@ class GoblinUltra { // Gemini-specific getters. RefVector get_unshifted() { - return { this->q_c, - this->q_l, - this->q_r, - this->q_o, - this->q_4, - this->q_m, - this->q_arith, - this->q_sort, - this->q_elliptic, - this->q_aux, - this->q_lookup, - this->q_busread, - this->q_poseidon2_external, - this->q_poseidon2_internal, - this->sigma_1, - this->sigma_2, - this->sigma_3, - this->sigma_4, - this->id_1, - this->id_2, - this->id_3, - this->id_4, - this->table_1, - this->table_2, - this->table_3, - this->table_4, - this->lagrange_first, - this->lagrange_last, - this->lagrange_ecc_op, - this->databus_id, - this->w_l, - this->w_r, - this->w_o, - this->w_4, - this->sorted_accum, - this->z_perm, - this->z_lookup, - this->ecc_op_wire_1, - this->ecc_op_wire_2, - this->ecc_op_wire_3, - this->ecc_op_wire_4, - this->calldata, - this->calldata_read_counts, - this->lookup_inverses }; + return concatenate(PrecomputedEntities::get_all(), WitnessEntities::get_all()); }; - RefVector get_witness() - { - return { this->w_l, - this->w_r, - this->w_o, - this->w_4, - this->sorted_accum, - this->z_perm, - this->z_lookup, - this->ecc_op_wire_1, - this->ecc_op_wire_2, - this->ecc_op_wire_3, - this->ecc_op_wire_4, - this->calldata, - this->calldata_read_counts, - this->lookup_inverses }; - }; + RefVector get_witness() { return WitnessEntities::get_all(); }; RefVector get_to_be_shifted() { return { this->table_1, this->table_2, this->table_3, this->table_4, this->w_l, this->w_r, this->w_o, this->w_4, this->sorted_accum, this->z_perm, this->z_lookup }; }; - RefVector get_precomputed() - { - return { this->q_m, - this->q_c, - this->q_l, - this->q_r, - this->q_o, - this->q_4, - this->q_arith, - this->q_sort, - this->q_elliptic, - this->q_aux, - this->q_lookup, - this->q_busread, - this->sigma_1, - this->sigma_2, - this->sigma_3, - this->sigma_4, - this->id_1, - this->id_2, - this->id_3, - this->id_4, - this->table_1, - this->table_2, - this->table_3, - this->table_4, - this->lagrange_first, - this->lagrange_last, - this->lagrange_ecc_op, - this->databus_id }; - } + RefVector get_precomputed() { return PrecomputedEntities::get_all(); } RefVector get_shifted() { return ShiftedEntities::get_all(); }; }; @@ -358,6 +267,11 @@ class GoblinUltra { size_t num_ecc_op_gates; // needed to determine public input offset + RefVector get_to_be_shifted() + { + return { this->table_1, this->table_2, this->table_3, this->table_4, this->w_l, this->w_r, + this->w_o, this->w_4, this->sorted_accum, this->z_perm, this->z_lookup }; + }; // The plookup wires that store plookup read data. std::array get_table_column_wires() { return { w_l, w_r, w_o }; }; }; @@ -410,10 +324,17 @@ class GoblinUltra { }; /** - * @brief A container for the prover polynomials handles; only stores spans. + * @brief A container for the prover polynomials handles. */ - class ProverPolynomials : public AllEntities { + class ProverPolynomials : public AllEntities { public: + // Define all operations as default, except move construction/assignment + ProverPolynomials() = default; + ProverPolynomials& operator=(const ProverPolynomials&) = delete; + ProverPolynomials(const ProverPolynomials& o) = delete; + ProverPolynomials(ProverPolynomials&& o) noexcept = default; + ProverPolynomials& operator=(ProverPolynomials&& o) noexcept = default; + ~ProverPolynomials() = default; [[nodiscard]] size_t get_polynomial_size() const { return q_c.size(); } [[nodiscard]] AllValues get_row(size_t row_idx) const { @@ -425,29 +346,6 @@ class GoblinUltra { } }; - /** - * @brief An owning container of polynomials. - * @warning When this was introduced it broke some of our design principles. - * - Execution trace builders don't handle "polynomials" because the interpretation of the execution trace - * columns as polynomials is a detail of the proving system, and trace builders are (sometimes in practice, - * always in principle) reusable for different proving protocols (e.g., Plonk and Honk). - * - Polynomial storage is handled by key classes. Polynomials aren't moved, but are accessed elsewhere by - * std::spans. - * - * We will consider revising this data model: TODO(https://github.com/AztecProtocol/barretenberg/issues/743) - */ - class AllPolynomials : public AllEntities { - public: - [[nodiscard]] AllValues get_row(const size_t row_idx) const - { - AllValues result; - for (auto [result_field, polynomial] : zip_view(result.get_all(), this->get_all())) { - result_field = polynomial[row_idx]; - } - return result; - } - }; - /** * @brief A container for the witness commitments. */ @@ -677,4 +575,4 @@ class GoblinUltra { using Transcript = Transcript_; }; -} // namespace proof_system::honk::flavor \ No newline at end of file +} // namespace proof_system::honk::flavor diff --git a/barretenberg/cpp/src/barretenberg/flavor/toy_avm.hpp b/barretenberg/cpp/src/barretenberg/flavor/toy_avm.hpp index ba0c7c2b465..b781e842d3e 100644 --- a/barretenberg/cpp/src/barretenberg/flavor/toy_avm.hpp +++ b/barretenberg/cpp/src/barretenberg/flavor/toy_avm.hpp @@ -213,34 +213,18 @@ class ToyAVM { using Base::Base; }; - /** - * @brief An owning container of polynomials. - * @warning When this was introduced it broke some of our design principles. - * - Execution trace builders don't handle "polynomials" because the interpretation of the execution trace - * columns as polynomials is a detail of the proving system, and trace builders are (sometimes in practice, - * always in principle) reusable for different proving protocols (e.g., Plonk and Honk). - * - Polynomial storage is handled by key classes. Polynomials aren't moved, but are accessed elsewhere by - * std::spans. - * - * We will consider revising this data model: TODO(https://github.com/AztecProtocol/barretenberg/issues/743) - */ - class AllPolynomials : public AllEntities { - public: - [[nodiscard]] size_t get_polynomial_size() const { return this->lagrange_first.size(); } - AllValues get_row(const size_t row_idx) const - { - AllValues result; - for (auto [result_field, polynomial] : zip_view(result.get_all(), this->get_all())) { - result_field = polynomial[row_idx]; - } - return result; - } - }; /** * @brief A container for polynomials handles; only stores spans. */ - class ProverPolynomials : public AllEntities { + class ProverPolynomials : public AllEntities { public: + // Define all operations as default, except move construction/assignment + ProverPolynomials() = default; + ProverPolynomials& operator=(const ProverPolynomials&) = delete; + ProverPolynomials(const ProverPolynomials& o) = delete; + ProverPolynomials(ProverPolynomials&& o) noexcept = default; + ProverPolynomials& operator=(ProverPolynomials&& o) noexcept = default; + ~ProverPolynomials() = default; [[nodiscard]] size_t get_polynomial_size() const { return enable_tuple_set_permutation.size(); } [[nodiscard]] AllValues get_row(const size_t row_idx) const { diff --git a/barretenberg/cpp/src/barretenberg/flavor/ultra.hpp b/barretenberg/cpp/src/barretenberg/flavor/ultra.hpp index 450571eaaba..5dad1648b59 100644 --- a/barretenberg/cpp/src/barretenberg/flavor/ultra.hpp +++ b/barretenberg/cpp/src/barretenberg/flavor/ultra.hpp @@ -137,7 +137,32 @@ class Ultra { z_perm, // column 5 z_lookup) // column 6 - RefVector get_wires() { return { w_l, w_r, w_o, w_4 }; }; + RefVector get_wires() { return { w_l, w_r, w_o, w_4, sorted_accum, z_perm, z_lookup }; }; + }; + + /** + * @brief Class for ShiftedEntities, containing shifted witness and table polynomials. + */ + template class ShiftedEntities { + public: + DEFINE_FLAVOR_MEMBERS(DataType, + table_1_shift, // column 0 + table_2_shift, // column 1 + table_3_shift, // column 2 + table_4_shift, // column 3 + w_l_shift, // column 4 + w_r_shift, // column 5 + w_o_shift, // column 6 + w_4_shift, // column 7 + sorted_accum_shift, // column 8 + z_perm_shift, // column 9 + z_lookup_shift) // column 10 + + RefVector get_shifted() + { + return { table_1_shift, table_2_shift, table_3_shift, table_4_shift, w_l_shift, w_r_shift, + w_o_shift, w_4_shift, sorted_accum_shift, z_perm_shift, z_lookup_shift }; + }; }; /** @@ -200,7 +225,7 @@ class Ultra { // Gemini-specific getters. RefVector get_unshifted() { - return { q_c, q_l, q_r, q_o, q_4, q_m, q_arith, q_sort, + return { q_m, q_c, q_l, q_r, q_o, q_4, q_arith, q_sort, q_elliptic, q_aux, q_lookup, sigma_1, sigma_2, sigma_3, sigma_4, id_1, id_2, id_3, id_4, table_1, table_2, table_3, table_4, lagrange_first, lagrange_last, w_l, w_r, w_o, w_4, sorted_accum, z_perm, z_lookup @@ -245,6 +270,11 @@ class Ultra { std::vector memory_read_records; std::vector memory_write_records; + RefVector get_to_be_shifted() + { + return { this->table_1, this->table_2, this->table_3, this->table_4, this->w_l, this->w_r, + this->w_o, this->w_4, this->sorted_accum, this->z_perm, this->z_lookup }; + }; // The plookup wires that store plookup read data. std::array get_table_column_wires() { return { w_l, w_r, w_o }; }; }; @@ -270,10 +300,17 @@ class Ultra { }; /** - * @brief A container for polynomials handles; only stores spans. + * @brief A container for polynomials handles. */ - class ProverPolynomials : public AllEntities { + class ProverPolynomials : public AllEntities { public: + // Define all operations as default, except move construction/assignment + ProverPolynomials() = default; + ProverPolynomials& operator=(const ProverPolynomials&) = delete; + ProverPolynomials(const ProverPolynomials& o) = delete; + ProverPolynomials(ProverPolynomials&& o) noexcept = default; + ProverPolynomials& operator=(ProverPolynomials&& o) noexcept = default; + ~ProverPolynomials() = default; [[nodiscard]] size_t get_polynomial_size() const { return q_c.size(); } [[nodiscard]] AllValues get_row(const size_t row_idx) const { @@ -285,29 +322,6 @@ class Ultra { } }; - /** - * @brief An owning container of polynomials. - * @warning When this was introduced it broke some of our design principles. - * - Execution trace builders don't handle "polynomials" because the interpretation of the execution trace - * columns as polynomials is a detail of the proving system, and trace builders are (sometimes in practice, - * always in principle) reusable for different proving protocols (e.g., Plonk and Honk). - * - Polynomial storage is handled by key classes. Polynomials aren't moved, but are accessed elsewhere by - * std::spans. - * - * We will consider revising this data model: TODO(https://github.com/AztecProtocol/barretenberg/issues/743) - */ - class AllPolynomials : public AllEntities { - public: - [[nodiscard]] AllValues get_row(const size_t row_idx) const - { - AllValues result; - for (auto [result_field, polynomial] : zip_view(result.get_all(), this->get_all())) { - result_field = polynomial[row_idx]; - } - return result; - } - }; - /** * @brief A container for storing the partially evaluated multivariates produced by sumcheck. */ diff --git a/barretenberg/cpp/src/barretenberg/flavor/ultra_recursive.hpp b/barretenberg/cpp/src/barretenberg/flavor/ultra_recursive.hpp index df0ac96c120..d4591f885f3 100644 --- a/barretenberg/cpp/src/barretenberg/flavor/ultra_recursive.hpp +++ b/barretenberg/cpp/src/barretenberg/flavor/ultra_recursive.hpp @@ -216,7 +216,7 @@ template class UltraRecursive_ { // Gemini-specific getters. RefVector get_unshifted() { - return { q_c, q_l, q_r, q_o, q_4, q_m, q_arith, q_sort, + return { q_m, q_c, q_l, q_r, q_o, q_4, q_arith, q_sort, q_elliptic, q_aux, q_lookup, sigma_1, sigma_2, sigma_3, sigma_4, id_1, id_2, id_3, id_4, table_1, table_2, table_3, table_4, lagrange_first, lagrange_last, w_l, w_r, w_o, w_4, sorted_accum, z_perm, z_lookup diff --git a/barretenberg/cpp/src/barretenberg/honk/proof_system/permutation_library.hpp b/barretenberg/cpp/src/barretenberg/honk/proof_system/permutation_library.hpp index 5c1c4392a8f..eb14f30c063 100644 --- a/barretenberg/cpp/src/barretenberg/honk/proof_system/permutation_library.hpp +++ b/barretenberg/cpp/src/barretenberg/honk/proof_system/permutation_library.hpp @@ -154,13 +154,14 @@ void compute_permutation_grand_products(std::shared_ptr& full_polynomial = PermutationRelation::get_grand_product_polynomial(full_polynomials); - auto& key_polynomial = PermutationRelation::get_grand_product_polynomial(*key); - full_polynomial = key_polynomial; + barretenberg::Polynomial& full_polynomial = + PermutationRelation::get_grand_product_polynomial(full_polynomials); + barretenberg::Polynomial& key_polynomial = PermutationRelation::get_grand_product_polynomial(*key); + full_polynomial = key_polynomial.share(); compute_permutation_grand_product( key->circuit_size, full_polynomials, relation_parameters); - std::span& full_polynomial_shift = + barretenberg::Polynomial& full_polynomial_shift = PermutationRelation::get_shifted_grand_product_polynomial(full_polynomials); full_polynomial_shift = key_polynomial.shifted(); }); @@ -185,9 +186,6 @@ void compute_permutation_grand_products(std::shared_ptr void compute_concatenated_polynomials(StorageHandle* proving_key) { - // TODO(AD): use RefVector here, see https://github.com/AztecProtocol/barretenberg/issues/743 - // RefVector makes PolynomialHandle now redundant. Can scale back use of auto then too. - // using PolynomialHandle = typename Flavor::PolynomialHandle; // Concatenation groups are vectors of polynomials that are concatenated together auto concatenation_groups = proving_key->get_concatenation_groups(); @@ -421,13 +419,13 @@ template inline void compute_lagrange_polynomials_for_goblin_t lagrange_polynomial_odd_in_minicircuit[i] = 1; lagrange_polynomial_even_in_minicircut[i + 1] = 1; } - proving_key->lagrange_odd_in_minicircuit = lagrange_polynomial_odd_in_minicircuit; + proving_key->lagrange_odd_in_minicircuit = lagrange_polynomial_odd_in_minicircuit.share(); - proving_key->lagrange_even_in_minicircuit = lagrange_polynomial_even_in_minicircut; + proving_key->lagrange_even_in_minicircuit = lagrange_polynomial_even_in_minicircut.share(); lagrange_polynomial_second[1] = 1; lagrange_polynomial_second_to_last_in_minicircuit[Flavor::MINI_CIRCUIT_SIZE - 2] = 1; - proving_key->lagrange_second_to_last_in_minicircuit = lagrange_polynomial_second_to_last_in_minicircuit; - proving_key->lagrange_second = lagrange_polynomial_second; + proving_key->lagrange_second_to_last_in_minicircuit = lagrange_polynomial_second_to_last_in_minicircuit.share(); + proving_key->lagrange_second = lagrange_polynomial_second.share(); } } // namespace proof_system::honk::permutation_library \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/honk/utils/testing.hpp b/barretenberg/cpp/src/barretenberg/honk/utils/testing.hpp index dda23952741..d0eed0fca88 100644 --- a/barretenberg/cpp/src/barretenberg/honk/utils/testing.hpp +++ b/barretenberg/cpp/src/barretenberg/honk/utils/testing.hpp @@ -9,54 +9,38 @@ namespace proof_system::honk { * function returns an array of data pointed to by the ProverPolynomials. */ template -std::pair get_sequential_prover_polynomials( - const size_t log_circuit_size, const size_t starting_value) +typename Flavor::ProverPolynomials get_sequential_prover_polynomials(const size_t log_circuit_size, + const size_t starting_value) { using FF = typename Flavor::FF; - using ProverPolynomials = typename Flavor::ProverPolynomials; using Polynomial = typename Flavor::Polynomial; - typename Flavor::AllPolynomials storage; + typename Flavor::ProverPolynomials prover_polynomials; size_t circuit_size = 1 << log_circuit_size; size_t value_idx = starting_value; - for (auto& polynomial : storage.get_all()) { + for (auto& polynomial : prover_polynomials.get_all()) { polynomial = Polynomial(circuit_size); for (auto& value : polynomial) { value = FF(value_idx++); } } - - ProverPolynomials prover_polynomials; - for (auto [prover_poly, storage_poly] : zip_view(prover_polynomials.get_all(), storage.get_all())) { - prover_poly = storage_poly; - } - - return std::pair(std::move(storage), prover_polynomials); + return prover_polynomials; } -template -std::pair get_zero_prover_polynomials( - const size_t log_circuit_size) +template typename Flavor::ProverPolynomials get_zero_prover_polynomials(const size_t log_circuit_size) { using FF = typename Flavor::FF; - using ProverPolynomials = typename Flavor::ProverPolynomials; using Polynomial = typename Flavor::Polynomial; - typename Flavor::AllPolynomials storage; + typename Flavor::ProverPolynomials prover_polynomials; size_t circuit_size = 1 << log_circuit_size; - for (auto& polynomial : storage.get_all()) { + for (auto& polynomial : prover_polynomials.get_all()) { polynomial = Polynomial(circuit_size); for (auto& value : polynomial) { value = FF(0); } } - - ProverPolynomials prover_polynomials; - for (auto [prover_poly, storage_poly] : zip_view(prover_polynomials.get_all(), storage.get_all())) { - prover_poly = storage_poly; - } - - return std::pair(std::move(storage), prover_polynomials); + return prover_polynomials; } } // namespace proof_system::honk \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/honk/utils/testing.test.cpp b/barretenberg/cpp/src/barretenberg/honk/utils/testing.test.cpp deleted file mode 100644 index 3e150e4a2d5..00000000000 --- a/barretenberg/cpp/src/barretenberg/honk/utils/testing.test.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include "testing.hpp" -#include "barretenberg/flavor/ultra.hpp" -#include - -namespace barretenberg::test_testing_utils { - -TEST(HonkTestingUtils, ProverPolynomials) -{ - using Flavor = proof_system::honk::flavor::Ultra; - auto [storage, prover_polynomials] = - proof_system::honk::get_sequential_prover_polynomials(/*log_circuit_size=*/2, /*starting_value=*/0); - auto& first_polynomial = prover_polynomials.get_all()[0]; - EXPECT_EQ(storage.get_all()[0][0], first_polynomial[0]); - EXPECT_EQ(storage.get_all()[0][1], first_polynomial[1]); -}; - -} // namespace barretenberg::test_testing_utils diff --git a/barretenberg/cpp/src/barretenberg/polynomials/polynomial.cpp b/barretenberg/cpp/src/barretenberg/polynomials/polynomial.cpp index 727575b7d56..b5427deeee1 100644 --- a/barretenberg/cpp/src/barretenberg/polynomials/polynomial.cpp +++ b/barretenberg/cpp/src/barretenberg/polynomials/polynomial.cpp @@ -15,6 +15,22 @@ #include namespace barretenberg { + +// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays) +template std::shared_ptr _allocate_aligned_memory(const size_t n_elements) +{ + // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays) + return std::static_pointer_cast(get_mem_slab(sizeof(Fr) * n_elements)); +} + +template void Polynomial::allocate_backing_memory(size_t n_elements) +{ + size_ = n_elements; + // capacity() is size_ plus padding for shifted polynomials + backing_memory_ = _allocate_aligned_memory(capacity()); + coefficients_ = backing_memory_.get(); +} + /** * Constructors / Destructors **/ @@ -24,15 +40,10 @@ namespace barretenberg { * * @param initial_size The initial size of the polynomial. */ -template -Polynomial::Polynomial(size_t initial_size) - : coefficients_(nullptr) - , size_(initial_size) +template Polynomial::Polynomial(size_t initial_size) { - if (capacity() > 0) { - coefficients_ = allocate_aligned_memory(sizeof(Fr) * capacity()); - } - memset(static_cast(coefficients_.get()), 0, sizeof(Fr) * capacity()); + allocate_backing_memory(initial_size); + memset(static_cast(coefficients_), 0, sizeof(Fr) * capacity()); } /** @@ -42,16 +53,11 @@ Polynomial::Polynomial(size_t initial_size) * @param initial_size The initial size of the polynomial. * @param flag Signals that we do not zero memory. */ -template -Polynomial::Polynomial(size_t initial_size, DontZeroMemory flag) - : coefficients_(nullptr) - , size_(initial_size) +template Polynomial::Polynomial(size_t initial_size, DontZeroMemory flag) { // Flag is unused, but we don't memset 0 if passed. (void)flag; - if (capacity() > 0) { - coefficients_ = allocate_aligned_memory(sizeof(Fr) * capacity()); - } + allocate_backing_memory(initial_size); } template @@ -59,97 +65,115 @@ Polynomial::Polynomial(const Polynomial& other) : Polynomial(other, other.size()) {} -template -Polynomial::Polynomial(const Polynomial& other, const size_t target_size) - : size_(std::max(target_size, other.size())) +// fully copying "expensive" constructor +template Polynomial::Polynomial(const Polynomial& other, const size_t target_size) { - // info("Polynomial EXPENSIVE Copy ctor size ", size_); - coefficients_ = allocate_aligned_memory(sizeof(Fr) * capacity()); + allocate_backing_memory(std::max(target_size, other.size())); - if (other.coefficients_ != nullptr) { - memcpy(static_cast(coefficients_.get()), - static_cast(other.coefficients_.get()), - sizeof(Fr) * other.size_); - } + memcpy(static_cast(coefficients_), static_cast(other.coefficients_), sizeof(Fr) * other.size_); zero_memory_beyond(other.size_); } +// move constructor template Polynomial::Polynomial(Polynomial&& other) noexcept - : coefficients_(std::exchange(other.coefficients_, nullptr)) + : backing_memory_(std::exchange(other.backing_memory_, nullptr)) + , coefficients_(std::exchange(other.coefficients_, nullptr)) , size_(std::exchange(other.size_, 0)) -{ - // info("Move ctor Polynomial took ownership of ", coefficients_, " size ", size_); -} +{} -template -Polynomial::Polynomial(std::span coefficients) - : size_(coefficients.size()) +// span constructor +template Polynomial::Polynomial(std::span coefficients) { - coefficients_ = allocate_aligned_memory(sizeof(Fr) * capacity()); - // info("Polynomial span ctor new buf at ", coefficients_, " size ", size_); - memcpy(static_cast(coefficients_.get()), + allocate_backing_memory(coefficients.size()); + memcpy(static_cast(coefficients_), static_cast(coefficients.data()), sizeof(Fr) * coefficients.size()); zero_memory_beyond(size_); } +// interpolation constructor template Polynomial::Polynomial(std::span interpolation_points, std::span evaluations) : Polynomial(interpolation_points.size()) { ASSERT(size_ > 0); - // info("Polynomial INTERPOLATION ctor."); - polynomial_arithmetic::compute_efficient_interpolation( - evaluations.data(), coefficients_.get(), interpolation_points.data(), size_); + evaluations.data(), coefficients_, interpolation_points.data(), size_); } -template Polynomial::~Polynomial() {} - // Assignments +// full copy "expensive" assignment template Polynomial& Polynomial::operator=(const Polynomial& other) { - // info("Polynomial EXPENSIVE copy assignment."); - size_ = other.size_; - - coefficients_ = allocate_aligned_memory(sizeof(Fr) * capacity()); - - if (other.coefficients_ != nullptr) { - memcpy(static_cast(coefficients_.get()), - static_cast(other.coefficients_.get()), - sizeof(Fr) * other.size_); + if (this == &other) { + return *this; } + allocate_backing_memory(other.size_); + memcpy(static_cast(coefficients_), static_cast(other.coefficients_), sizeof(Fr) * other.size_); zero_memory_beyond(size_); return *this; } +template Polynomial& Polynomial::operator=(std::span coefficients) noexcept +{ + // move assign a Polynomial constructed with the span + *this = Polynomial{ coefficients }; + return *this; +} + +// ####### template Polynomial& Polynomial::operator=(Polynomial&& other) noexcept { if (&other == this) { return *this; } - // info("Polynomial move assignment."); // simultaneously set members and clear other + backing_memory_ = std::exchange(other.backing_memory_, nullptr); coefficients_ = std::exchange(other.coefficients_, nullptr); size_ = std::exchange(other.size_, 0); - return *this; } -// ####### +template Polynomial Polynomial::share() const +{ + Polynomial p; + p.backing_memory_ = backing_memory_; + p.size_ = size_; + p.coefficients_ = coefficients_; + return p; +} template Fr Polynomial::evaluate(const Fr& z, const size_t target_size) const { - return polynomial_arithmetic::evaluate(coefficients_.get(), z, target_size); + return polynomial_arithmetic::evaluate(coefficients_, z, target_size); } template Fr Polynomial::evaluate(const Fr& z) const { - return polynomial_arithmetic::evaluate(coefficients_.get(), z, size_); + return polynomial_arithmetic::evaluate(coefficients_, z, size_); +} + +template bool Polynomial::operator==(Polynomial const& rhs) const +{ + // If either is empty, both must be + if (is_empty() || rhs.is_empty()) { + return is_empty() && rhs.is_empty(); + } + // Size must agree + if (size() != rhs.size()) { + return false; + } + // Each coefficient must agree + for (size_t i = 0; i < size(); i++) { + if (coefficients_[i] != rhs.coefficients_[i]) { + return false; + } + } + return true; } /** @@ -169,8 +193,8 @@ template void Polynomial::zero_memory_beyond(const size_t star size_t delta = end - start_position; if (delta > 0) { - ASSERT(coefficients_); - memset(static_cast(&coefficients_.get()[start_position]), 0, sizeof(Fr) * delta); + ASSERT(backing_memory_); + memset(static_cast(&coefficients_[start_position]), 0, sizeof(Fr) * delta); } } @@ -185,7 +209,7 @@ void Polynomial::fft(const EvaluationDomain& domain) ASSERT(in_place_operation_viable(domain.size)); zero_memory_beyond(domain.size); - polynomial_arithmetic::fft(coefficients_.get(), domain); + polynomial_arithmetic::fft(coefficients_, domain); } template @@ -195,7 +219,7 @@ void Polynomial::partial_fft(const EvaluationDomain& domain, Fr constant ASSERT(in_place_operation_viable(domain.size)); zero_memory_beyond(domain.size); - polynomial_arithmetic::partial_fft(coefficients_.get(), domain, constant, is_coset); + polynomial_arithmetic::partial_fft(coefficients_, domain, constant, is_coset); } template @@ -205,7 +229,7 @@ void Polynomial::coset_fft(const EvaluationDomain& domain) ASSERT(in_place_operation_viable(domain.size)); zero_memory_beyond(domain.size); - polynomial_arithmetic::coset_fft(coefficients_.get(), domain); + polynomial_arithmetic::coset_fft(coefficients_, domain); } template @@ -219,7 +243,7 @@ void Polynomial::coset_fft(const EvaluationDomain& domain, ASSERT(in_place_operation_viable(extended_size)); zero_memory_beyond(extended_size); - polynomial_arithmetic::coset_fft(coefficients_.get(), domain, large_domain, domain_extension); + polynomial_arithmetic::coset_fft(coefficients_, domain, large_domain, domain_extension); } template @@ -229,7 +253,7 @@ void Polynomial::coset_fft_with_constant(const EvaluationDomain& domain, ASSERT(in_place_operation_viable(domain.size)); zero_memory_beyond(domain.size); - polynomial_arithmetic::coset_fft_with_constant(coefficients_.get(), domain, constant); + polynomial_arithmetic::coset_fft_with_constant(coefficients_, domain, constant); } template @@ -239,7 +263,7 @@ void Polynomial::coset_fft_with_generator_shift(const EvaluationDomain& ASSERT(in_place_operation_viable(domain.size)); zero_memory_beyond(domain.size); - polynomial_arithmetic::coset_fft_with_generator_shift(coefficients_.get(), domain, constant); + polynomial_arithmetic::coset_fft_with_generator_shift(coefficients_, domain, constant); } template @@ -249,7 +273,7 @@ void Polynomial::ifft(const EvaluationDomain& domain) ASSERT(in_place_operation_viable(domain.size)); zero_memory_beyond(domain.size); - polynomial_arithmetic::ifft(coefficients_.get(), domain); + polynomial_arithmetic::ifft(coefficients_, domain); } template @@ -259,7 +283,7 @@ void Polynomial::ifft_with_constant(const EvaluationDomain& domain, cons ASSERT(in_place_operation_viable(domain.size)); zero_memory_beyond(domain.size); - polynomial_arithmetic::ifft_with_constant(coefficients_.get(), domain, constant); + polynomial_arithmetic::ifft_with_constant(coefficients_, domain, constant); } template @@ -269,21 +293,21 @@ void Polynomial::coset_ifft(const EvaluationDomain& domain) ASSERT(in_place_operation_viable(domain.size)); zero_memory_beyond(domain.size); - polynomial_arithmetic::coset_ifft(coefficients_.get(), domain); + polynomial_arithmetic::coset_ifft(coefficients_, domain); } template Fr Polynomial::compute_kate_opening_coefficients(const Fr& z) requires polynomial_arithmetic::SupportsFFT { - return polynomial_arithmetic::compute_kate_opening_coefficients(coefficients_.get(), coefficients_.get(), z, size_); + return polynomial_arithmetic::compute_kate_opening_coefficients(coefficients_, coefficients_, z, size_); } template Fr Polynomial::compute_barycentric_evaluation(const Fr& z, const EvaluationDomain& domain) requires polynomial_arithmetic::SupportsFFT { - return polynomial_arithmetic::compute_barycentric_evaluation(coefficients_.get(), domain.size, z, domain); + return polynomial_arithmetic::compute_barycentric_evaluation(coefficients_, domain.size, z, domain); } template @@ -293,7 +317,19 @@ Fr Polynomial::evaluate_from_fft(const EvaluationDomain& large_domain, requires polynomial_arithmetic::SupportsFFT { - return polynomial_arithmetic::evaluate_from_fft(coefficients_.get(), large_domain, z, small_domain); + return polynomial_arithmetic::evaluate_from_fft(coefficients_, large_domain, z, small_domain); +} + +template Polynomial Polynomial::shifted() const +{ + ASSERT(size_ > 0); + ASSERT(coefficients_[0].is_zero()); + ASSERT(coefficients_[size_].is_zero()); // relies on MAXIMUM_COEFFICIENT_SHIFT >= 1 + Polynomial p; + p.backing_memory_ = backing_memory_; + p.size_ = size_; + p.coefficients_ = coefficients_ + 1; + return p; } // TODO(#723): This method is used for the transcript aggregation protocol. For convenience we currently enforce that @@ -302,7 +338,7 @@ Fr Polynomial::evaluate_from_fft(const EvaluationDomain& large_domain, template void Polynomial::set_to_right_shifted(std::span coeffs_in, size_t shift_size) { // Ensure we're not trying to shift self - ASSERT(coefficients_.get() != coeffs_in.data()); + ASSERT(coefficients_ != coeffs_in.data()); auto size_in = coeffs_in.size(); ASSERT(size_in > 0); @@ -315,15 +351,14 @@ template void Polynomial::set_to_right_shifted(std::span c } // Set size of self equal to size of input and allocate memory - size_ = size_in; - coefficients_ = allocate_aligned_memory(sizeof(Fr) * capacity()); + allocate_backing_memory(size_in); // Zero out the first shift_size-many coefficients of self - memset(static_cast(coefficients_.get()), 0, sizeof(Fr) * shift_size); + memset(static_cast(coefficients_), 0, sizeof(Fr) * shift_size); // Copy all but the last shift_size many input coeffs into self at the shift_size-th index. std::size_t num_to_copy = size_ - shift_size; - memcpy(static_cast(coefficients_.get() + shift_size), + memcpy(static_cast(coefficients_ + shift_size), static_cast(coeffs_in.data()), sizeof(Fr) * num_to_copy); zero_memory_beyond(size_); @@ -342,7 +377,7 @@ template void Polynomial::add_scaled(std::span other size_t offset = j * range_per_thread; size_t end = (j == num_threads - 1) ? offset + range_per_thread + leftovers : offset + range_per_thread; for (size_t i = offset; i < end; ++i) { - coefficients_.get()[i] += scaling_factor * other[i]; + coefficients_[i] += scaling_factor * other[i]; } }); } @@ -359,7 +394,7 @@ template Polynomial& Polynomial::operator+=(std::span Polynomial& Polynomial::operator-=(std::span Polynomial& Polynomial::operator*=(const Fr scali size_t offset = j * range_per_thread; size_t end = (j == num_threads - 1) ? offset + range_per_thread + leftovers : offset + range_per_thread; for (size_t i = offset; i < end; ++i) { - coefficients_.get()[i] *= scaling_factor; + coefficients_[i] *= scaling_factor; } }); @@ -417,10 +452,11 @@ template Fr Polynomial::evaluate_mle(std::span evalu size_t n_l = 1 << (m - 1); // temporary buffer of half the size of the polynomial - pointer tmp_ptr = allocate_aligned_memory(sizeof(Fr) * n_l); + // TODO(AD): Make this a polynomial with DontZeroMemory::FLAG + pointer tmp_ptr = _allocate_aligned_memory(sizeof(Fr) * n_l); auto tmp = tmp_ptr.get(); - Fr* prev = coefficients_.get(); + Fr* prev = coefficients_; if (shift) { ASSERT(prev[0] == Fr::zero()); prev++; @@ -485,12 +521,7 @@ template Polynomial Polynomial::partial_evaluate_mle(std:: return result; } -template typename Polynomial::pointer Polynomial::allocate_aligned_memory(const size_t size) const -{ - return std::static_pointer_cast(get_mem_slab(size)); -} - template class Polynomial; template class Polynomial; -} // namespace barretenberg +} // namespace barretenberg \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/polynomials/polynomial.hpp b/barretenberg/cpp/src/barretenberg/polynomials/polynomial.hpp index caf74ad39dc..d8424e445fd 100644 --- a/barretenberg/cpp/src/barretenberg/polynomials/polynomial.hpp +++ b/barretenberg/cpp/src/barretenberg/polynomials/polynomial.hpp @@ -17,6 +17,7 @@ template class Polynomial { using value_type = Fr; using difference_type = std::ptrdiff_t; using reference = value_type&; + // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays) using pointer = std::shared_ptr; using const_pointer = pointer; using iterator = Fr*; @@ -46,67 +47,48 @@ template class Polynomial { */ Polynomial(std::span interpolation_points, std::span evaluations); + // move assignment Polynomial& operator=(Polynomial&& other) noexcept; + Polynomial& operator=(std::span coefficients) noexcept; Polynomial& operator=(const Polynomial& other); - ~Polynomial(); + ~Polynomial() = default; /** * Return a shallow clone of the polynomial. i.e. underlying memory is shared. */ - Polynomial clone() - { - Polynomial p; - p.coefficients_ = coefficients_; - p.size_ = size_; - return p; - } + Polynomial share() const; std::array hash() const { return sha256::sha256(byte_span()); } void clear() { - coefficients_.reset(); + // to keep the invariant that backing_memory_ can handle capacity() we do NOT reset backing_memory_ + // backing_memory_.reset(); + coefficients_ = nullptr; size_ = 0; } - bool operator==(Polynomial const& rhs) const - { - // If either is empty, both must be - if (is_empty() || rhs.is_empty()) { - return is_empty() && rhs.is_empty(); - } - // Size must agree - if (size() != rhs.size()) { - return false; - } - // Each coefficient must agree - for (size_t i = 0; i < size(); i++) { - if (coefficients_.get()[i] != rhs.coefficients_.get()[i]) { - return false; - } - } - return true; - } + bool operator==(Polynomial const& rhs) const; // Const and non const versions of coefficient accessors - Fr const& operator[](const size_t i) const { return coefficients_.get()[i]; } + Fr const& operator[](const size_t i) const { return coefficients_[i]; } - Fr& operator[](const size_t i) { return coefficients_.get()[i]; } + Fr& operator[](const size_t i) { return coefficients_[i]; } Fr const& at(const size_t i) const { ASSERT(i < capacity()); - return coefficients_.get()[i]; + return coefficients_[i]; }; Fr& at(const size_t i) { ASSERT(i < capacity()); - return coefficients_.get()[i]; + return coefficients_[i]; }; - [[nodiscard]] Fr evaluate(const Fr& z, const size_t target_size) const; - [[nodiscard]] Fr evaluate(const Fr& z) const; + Fr evaluate(const Fr& z, size_t target_size) const; + Fr evaluate(const Fr& z) const; Fr compute_barycentric_evaluation(const Fr& z, const EvaluationDomain& domain) requires polynomial_arithmetic::SupportsFFT; @@ -122,9 +104,9 @@ template class Polynomial { requires polynomial_arithmetic::SupportsFFT; void coset_fft(const EvaluationDomain& domain, const EvaluationDomain& large_domain, - const size_t domain_extension) + size_t domain_extension) requires polynomial_arithmetic::SupportsFFT; - void coset_fft_with_constant(const EvaluationDomain& domain, const Fr& costant) + void coset_fft_with_constant(const EvaluationDomain& domain, const Fr& constant) requires polynomial_arithmetic::SupportsFFT; void coset_fft_with_generator_shift(const EvaluationDomain& domain, const Fr& constant) requires polynomial_arithmetic::SupportsFFT; @@ -137,7 +119,7 @@ template class Polynomial { Fr compute_kate_opening_coefficients(const Fr& z) requires polynomial_arithmetic::SupportsFFT; - bool is_empty() const { return (coefficients_ == nullptr) || (size_ == 0); } + bool is_empty() const { return size_ == 0; } /** * @brief Returns an std::span of the left-shift of self. @@ -145,13 +127,7 @@ template class Polynomial { * @details If the n coefficients of self are (0, a₁, …, aₙ₋₁), * we returns the view of the n-1 coefficients (a₁, …, aₙ₋₁). */ - std::span shifted() const - { - ASSERT(size_ > 0); - ASSERT(coefficients_[0].is_zero()); - ASSERT(coefficients_.get()[size_].is_zero()); // relies on DEFAULT_CAPACITY_INCREASE >= 1 - return std::span{ coefficients_.get() + 1, size_ }; - } + Polynomial shifted() const; /** * @brief Set self to the right shift of input coefficients @@ -192,7 +168,7 @@ template class Polynomial { * * @param scaling_factor s */ - Polynomial& operator*=(const Fr scaling_factor); + Polynomial& operator*=(Fr scaling_factor); /** * @brief evaluates p(X) = ∑ᵢ aᵢ⋅Xⁱ considered as multi-linear extension p(X₀,…,Xₘ₋₁) = ∑ᵢ aᵢ⋅Lᵢ(X₀,…,Xₘ₋₁) @@ -238,42 +214,42 @@ template class Polynomial { void factor_roots(std::span roots) { polynomial_arithmetic::factor_roots(std::span{ *this }, roots); }; void factor_roots(const Fr& root) { polynomial_arithmetic::factor_roots(std::span{ *this }, root); }; -#ifdef __clang__ - // Needed for clang versions earlier than 14.0.3, but breaks gcc. - // Can remove once ecosystem is firmly upgraded. - operator std::span() { return std::span(coefficients_.get(), size_); } - operator std::span() const { return std::span(coefficients_.get(), size_); } -#endif - - iterator begin() { return coefficients_.get(); } - iterator end() { return coefficients_.get() + size_; } - pointer data() { return coefficients_; } + iterator begin() { return coefficients_; } + iterator end() { return coefficients_ + size_; } + pointer data() { return backing_memory_; } std::span byte_span() const { - return std::span((uint8_t*)coefficients_.get(), size_ * sizeof(fr)); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + return { reinterpret_cast(coefficients_), size_ * sizeof(Fr) }; } - const_iterator begin() const { return coefficients_.get(); } - const_iterator end() const { return coefficients_.get() + size_; } - const_pointer data() const { return coefficients_; } + const_iterator begin() const { return coefficients_; } + const_iterator end() const { return coefficients_ + size_; } + const_pointer data() const { return backing_memory_; } std::size_t size() const { return size_; } - std::size_t capacity() const { return size_ + DEFAULT_CAPACITY_INCREASE; } + std::size_t capacity() const { return size_ + MAXIMUM_COEFFICIENT_SHIFT; } private: + // allocate a fresh memory pointer for backing memory + // DOES NOT initialize memory + void allocate_backing_memory(size_t n_elements); + // safety check for in place operations bool in_place_operation_viable(size_t domain_size = 0) { return (size() >= domain_size); } - pointer allocate_aligned_memory(const size_t size) const; - - void zero_memory_beyond(const size_t start_position); + void zero_memory_beyond(size_t start_position); // When a polynomial is instantiated from a size alone, the memory allocated corresponds to - // input size + DEFAULT_CAPACITY_INCREASE. A DEFAULT_CAPACITY_INCREASE of >= 1 is required to ensure - // that polynomials can be 'shifted' via a span of the 1st to size+1th coefficients. - const static size_t DEFAULT_CAPACITY_INCREASE = 1; - - pointer coefficients_; + // input size + MAXIMUM_COEFFICIENT_SHIFT to support 'shifted' coefficients efficiently. + const static size_t MAXIMUM_COEFFICIENT_SHIFT = 1; + + // The memory + // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays) + std::shared_ptr backing_memory_; + // A pointer into backing_memory_ to support std::span-like functionality. This allows for coefficient subsets + // and shifts. + Fr* coefficients_ = nullptr; // The size_ effectively represents the 'usable' length of the coefficients array but may be less than the true // 'capacity' of the array. It is not explicitly tied to the degree and is not changed by any operations on the // polynomial. @@ -282,6 +258,12 @@ template class Polynomial { template inline std::ostream& operator<<(std::ostream& os, Polynomial const& p) { + if (p.size() == 0) { + return os << "[]"; + } + if (p.size() == 1) { + return os << "[ data " << p[0] << "]"; + } return os << "[ data\n" << " " << p[0] << ",\n" << " " << p[1] << ",\n" @@ -291,11 +273,8 @@ template inline std::ostream& operator<<(std::ostream& os, Polynom << "]"; } -// Done -// N.B. grumpkin polynomials don't support fast fourier transforms using roots of unity! -// TODO: use template junk to disable fft methods if Fr::SUPPORTS_FFTS == false -// extern template class Polynomial; extern template class Polynomial; +// N.B. grumpkin polynomials don't support fast fourier transforms using roots of unity! extern template class Polynomial; using polynomial = Polynomial; @@ -316,4 +295,4 @@ using polynomial = Polynomial; * // cannot convert a const polynomial to a non-const span * static_assert(!std::convertible_to>); * static_assert(std::convertible_to>); - */ + */ \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/polynomials/polynomial_arithmetic.test.cpp b/barretenberg/cpp/src/barretenberg/polynomials/polynomial_arithmetic.test.cpp index b0a57e135d8..84a02cbc65a 100644 --- a/barretenberg/cpp/src/barretenberg/polynomials/polynomial_arithmetic.test.cpp +++ b/barretenberg/cpp/src/barretenberg/polynomials/polynomial_arithmetic.test.cpp @@ -1219,7 +1219,7 @@ TYPED_TEST(PolynomialTests, move_construct_and_assign) coeff = FF::random_element(); } - // construct a new poly FFom the original via the move constructor + // construct a new poly from the original via the move constructor Polynomial polynomial_b(std::move(polynomial_a)); // verifiy that source poly is appropriately destroyed diff --git a/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/eccvm/eccvm_circuit_builder.hpp b/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/eccvm/eccvm_circuit_builder.hpp index bdcbd4fa4ad..a15dd61ea35 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/eccvm/eccvm_circuit_builder.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/eccvm/eccvm_circuit_builder.hpp @@ -39,7 +39,7 @@ template class ECCVMCircuitBuilder { using VMOperation = proof_system_eccvm::VMOperation; std::shared_ptr op_queue; using ScalarMul = proof_system_eccvm::ScalarMul; - using AllPolynomials = typename Flavor::AllPolynomials; + using ProverPolynomials = typename Flavor::ProverPolynomials; ECCVMCircuitBuilder() : op_queue(std::make_shared()){}; @@ -315,9 +315,9 @@ template class ECCVMCircuitBuilder { (reads come from msm_x/y1, msm_x/y2) * lookup_read_counts_1: stores number of times a point has been read from a Straus precomputation table (reads come from msm_x/y3, msm_x/y4) - * @return AllPolynomials + * @return ProverPolynomials */ - AllPolynomials compute_polynomials() + ProverPolynomials compute_polynomials() { const auto msms = get_msms(); const auto flattened_muls = get_flattened_scalar_muls(msms); @@ -339,7 +339,7 @@ template class ECCVMCircuitBuilder { const auto num_rows_log2 = static_cast(numeric::get_msb64(num_rows)); size_t num_rows_pow2 = 1UL << (num_rows_log2 + (1UL << num_rows_log2 == num_rows ? 0 : 1)); - AllPolynomials polys; + ProverPolynomials polys; for (auto& poly : polys.get_all()) { poly = Polynomial(num_rows_pow2); } diff --git a/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/generated/AvmMini_circuit_builder.hpp b/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/generated/AvmMini_circuit_builder.hpp index 77cae117e47..4d91532f27a 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/generated/AvmMini_circuit_builder.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/generated/AvmMini_circuit_builder.hpp @@ -51,7 +51,7 @@ class AvmMiniCircuitBuilder { // TODO: template using Polynomial = Flavor::Polynomial; - using AllPolynomials = Flavor::AllPolynomials; + using ProverPolynomials = Flavor::ProverPolynomials; static constexpr size_t num_fixed_columns = 25; static constexpr size_t num_polys = 22; @@ -59,10 +59,10 @@ class AvmMiniCircuitBuilder { void set_trace(std::vector&& trace) { rows = std::move(trace); } - AllPolynomials compute_polynomials() + ProverPolynomials compute_polynomials() { const auto num_rows = get_circuit_subgroup_size(); - AllPolynomials polys; + ProverPolynomials polys; // Allocate mem for each column for (auto& poly : polys.get_all()) { @@ -103,7 +103,7 @@ class AvmMiniCircuitBuilder { [[maybe_unused]] bool check_circuit() { - auto polys = compute_polynomials(); + ProverPolynomials polys = compute_polynomials(); const size_t num_rows = polys.get_polynomial_size(); const auto evaluate_relation = [&](const std::string& relation_name) { diff --git a/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/toy_avm/toy_avm_circuit_builder.hpp b/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/toy_avm/toy_avm_circuit_builder.hpp index 659187b4131..93199a9dda0 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/toy_avm/toy_avm_circuit_builder.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/toy_avm/toy_avm_circuit_builder.hpp @@ -28,7 +28,7 @@ template class ToyAVMCircuitBuilder { static constexpr size_t NUM_POLYNOMIALS = Flavor::NUM_ALL_ENTITIES; static constexpr size_t NUM_WIRES = Flavor::NUM_WIRES; - using AllPolynomials = typename Flavor::AllPolynomials; + using ProverPolynomials = typename Flavor::ProverPolynomials; size_t num_gates = 0; std::array, NUM_WIRES> wires; ToyAVMCircuitBuilder() = default; @@ -44,16 +44,16 @@ template class ToyAVMCircuitBuilder { /** * @brief Compute the AVM Template flavor polynomial data required to generate a proof * - * @return AllPolynomials + * @return ProverPolynomials */ - AllPolynomials compute_polynomials() + ProverPolynomials compute_polynomials() { const auto num_gates_log2 = static_cast(numeric::get_msb64(num_gates)); size_t num_gates_pow2 = 1UL << (num_gates_log2 + (1UL << num_gates_log2 == num_gates ? 0 : 1)); - AllPolynomials polys; - for (auto& poly : polys.get_all()) { + ProverPolynomials polys; + for (Polynomial& poly : polys.get_all()) { poly = Polynomial(num_gates_pow2); } diff --git a/barretenberg/cpp/src/barretenberg/proof_system/composer/composer_lib.hpp b/barretenberg/cpp/src/barretenberg/proof_system/composer/composer_lib.hpp index 728a5cbfda3..10b1d751107 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/composer/composer_lib.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/composer/composer_lib.hpp @@ -35,13 +35,14 @@ void construct_selector_polynomials(const typename Flavor::CircuitBuilder& circu for (size_t i = 0; i < num_ecc_op_gates; ++i) { ecc_op_selector[i + op_gate_offset] = 1; } - proving_key->lagrange_ecc_op = ecc_op_selector; + proving_key->lagrange_ecc_op = ecc_op_selector.share(); } // TODO(#398): Loose coupling here! Would rather build up pk from arithmetization if constexpr (IsHonkFlavor) { - for (auto [poly, selector_values] : - zip_view(proving_key->precomputed_polynomials_get_all(), circuit_constructor.selectors.get())) { + for (auto [poly, selector_values] : zip_view(ZipAllowDifferentSizes::FLAG, + proving_key->get_precomputed_polynomials(), + circuit_constructor.selectors.get())) { ASSERT(proving_key->circuit_size >= selector_values.size()); // Copy the selector values for all gates, keeping the rows at which we store public inputs as 0. @@ -50,7 +51,7 @@ void construct_selector_polynomials(const typename Flavor::CircuitBuilder& circu for (size_t i = 0; i < selector_values.size(); ++i) { selector_poly_lagrange[i + gate_offset] = selector_values[i]; } - poly = selector_poly_lagrange; + poly = selector_poly_lagrange.share(); } } else if constexpr (IsPlonkFlavor) { size_t selector_idx = 0; diff --git a/barretenberg/cpp/src/barretenberg/proof_system/composer/permutation_lib.hpp b/barretenberg/cpp/src/barretenberg/proof_system/composer/permutation_lib.hpp index 90a7f0152f0..8383586d3cb 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/composer/permutation_lib.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/composer/permutation_lib.hpp @@ -409,7 +409,7 @@ void compute_plonk_permutation_lagrange_polynomials_from_mapping( std::string index = std::to_string(i + 1); barretenberg::polynomial polynomial_lagrange(key->circuit_size); compute_standard_plonk_lagrange_polynomial(polynomial_lagrange, mappings[i], key->small_domain); - key->polynomial_store.put(label + "_" + index + "_lagrange", std::move(polynomial_lagrange)); + key->polynomial_store.put(label + "_" + index + "_lagrange", polynomial_lagrange.share()); } } @@ -440,8 +440,8 @@ void compute_monomial_and_coset_fft_polynomials_from_lagrange(std::string label, barretenberg::polynomial sigma_fft(sigma_polynomial, key->large_domain.size); sigma_fft.coset_fft(key->large_domain); - key->polynomial_store.put(prefix, std::move(sigma_polynomial)); - key->polynomial_store.put(prefix + "_fft", std::move(sigma_fft)); + key->polynomial_store.put(prefix, sigma_polynomial.share()); + key->polynomial_store.put(prefix + "_fft", sigma_fft.share()); } } @@ -476,10 +476,10 @@ template inline void compute_first_and_last_lagrange_polynomia typename Flavor::Polynomial lagrange_polynomial_0(n); typename Flavor::Polynomial lagrange_polynomial_n_min_1(n); lagrange_polynomial_0[0] = 1; - proving_key->lagrange_first = lagrange_polynomial_0; + proving_key->lagrange_first = lagrange_polynomial_0.share(); lagrange_polynomial_n_min_1[n - 1] = 1; - proving_key->lagrange_last = lagrange_polynomial_n_min_1; + proving_key->lagrange_last = lagrange_polynomial_n_min_1.share(); } /** diff --git a/barretenberg/cpp/src/barretenberg/proof_system/library/grand_product_library.hpp b/barretenberg/cpp/src/barretenberg/proof_system/library/grand_product_library.hpp index e1c2ac20115..e4afe8fe7ab 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/library/grand_product_library.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/library/grand_product_library.hpp @@ -1,6 +1,7 @@ #pragma once #include "barretenberg/common/constexpr_utils.hpp" #include "barretenberg/common/thread.hpp" +#include "barretenberg/common/zip_view.hpp" #include "barretenberg/plonk/proof_system/proving_key/proving_key.hpp" #include "barretenberg/polynomials/polynomial.hpp" #include "barretenberg/relations/relation_parameters.hpp" @@ -57,22 +58,21 @@ void compute_grand_product(const size_t circuit_size, // Allocate numerator/denominator polynomials that will serve as scratch space // TODO(zac) we can re-use the permutation polynomial as the numerator polynomial. Reduces readability - Polynomial numerator = Polynomial{ circuit_size }; - Polynomial denominator = Polynomial{ circuit_size }; + Polynomial numerator{ circuit_size }; + Polynomial denominator{ circuit_size }; // Step (1) // Populate `numerator` and `denominator` with the algebra described by Relation const size_t num_threads = circuit_size >= get_num_cpus_pow2() ? get_num_cpus_pow2() : 1; const size_t block_size = circuit_size / num_threads; - auto full_polynomial_pointers = full_polynomials.get_all(); + auto full_polynomials_view = full_polynomials.get_all(); parallel_for(num_threads, [&](size_t thread_idx) { const size_t start = thread_idx * block_size; const size_t end = (thread_idx + 1) * block_size; for (size_t i = start; i < end; ++i) { typename Flavor::AllValues evaluations; - auto evaluations_pointer = evaluations.get_all(); - for (size_t k = 0; k < Flavor::NUM_ALL_ENTITIES; ++k) { - evaluations_pointer[k] = full_polynomial_pointers[k].size() > i ? full_polynomial_pointers[k][i] : 0; + for (auto [eval, full_poly] : zip_view(evaluations.get_all(), full_polynomials_view)) { + eval = full_poly.size() > i ? full_poly[i] : 0; } numerator[i] = GrandProdRelation::template compute_grand_product_numerator( evaluations, relation_parameters); @@ -155,12 +155,13 @@ void compute_grand_products(std::shared_ptr& key, // Assign the grand product polynomial to the relevant std::span member of `full_polynomials` (and its shift) // For example, for UltraPermutationRelation, this will be `full_polynomials.z_perm` // For example, for LookupRelation, this will be `full_polynomials.z_lookup` - std::span& full_polynomial = GrandProdRelation::get_grand_product_polynomial(full_polynomials); + barretenberg::Polynomial& full_polynomial = + GrandProdRelation::get_grand_product_polynomial(full_polynomials); auto& key_polynomial = GrandProdRelation::get_grand_product_polynomial(*key); - full_polynomial = key_polynomial; + full_polynomial = key_polynomial.share(); compute_grand_product(key->circuit_size, full_polynomials, relation_parameters); - std::span& full_polynomial_shift = + barretenberg::Polynomial& full_polynomial_shift = GrandProdRelation::get_shifted_grand_product_polynomial(full_polynomials); full_polynomial_shift = key_polynomial.shifted(); }); diff --git a/barretenberg/cpp/src/barretenberg/proof_system/library/grand_product_library.test.cpp b/barretenberg/cpp/src/barretenberg/proof_system/library/grand_product_library.test.cpp index a3a3408c3a9..da0d6b3a203 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/library/grand_product_library.test.cpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/library/grand_product_library.test.cpp @@ -90,28 +90,11 @@ template class GrandProductTests : public testing::Test { }; typename Flavor::ProverPolynomials prover_polynomials; - prover_polynomials.w_l = proving_key->w_l; - prover_polynomials.w_r = proving_key->w_r; - prover_polynomials.w_o = proving_key->w_o; - prover_polynomials.q_m = proving_key->q_m; - prover_polynomials.q_l = proving_key->q_l; - prover_polynomials.q_r = proving_key->q_r; - prover_polynomials.q_o = proving_key->q_o; - prover_polynomials.q_c = proving_key->q_c; - prover_polynomials.sigma_1 = proving_key->sigma_1; - prover_polynomials.sigma_2 = proving_key->sigma_2; - prover_polynomials.sigma_3 = proving_key->sigma_3; - prover_polynomials.id_1 = proving_key->id_1; - prover_polynomials.id_2 = proving_key->id_2; - prover_polynomials.id_3 = proving_key->id_3; - prover_polynomials.lagrange_first = proving_key->lagrange_first; - prover_polynomials.lagrange_last = proving_key->lagrange_last; - if constexpr (Flavor::NUM_WIRES == 4) { - prover_polynomials.w_4 = proving_key->w_4; - prover_polynomials.sigma_4 = proving_key->sigma_4; - prover_polynomials.id_4 = proving_key->id_4; + for (auto [prover_poly, key_poly] : zip_view(prover_polynomials.get_unshifted(), proving_key->get_all())) { + ASSERT(proof_system::flavor_get_label(prover_polynomials, prover_poly) == + proof_system::flavor_get_label(*proving_key, key_poly)); + prover_poly = key_poly.share(); } - prover_polynomials.z_perm = proving_key->z_perm; // Method 1: Compute z_perm using 'compute_grand_product_polynomial' as the prover would in practice constexpr size_t PERMUTATION_RELATION_INDEX = 0; @@ -237,7 +220,7 @@ template class GrandProductTests : public testing::Test { auto lookup_index_selector = get_random_polynomial(circuit_size); auto lookup_selector = get_random_polynomial(circuit_size); - proving_key->sorted_accum = sorted_batched; + proving_key->sorted_accum = sorted_batched.share(); populate_span(proving_key->q_r, column_1_step_size); populate_span(proving_key->q_m, column_2_step_size); populate_span(proving_key->q_c, column_3_step_size); @@ -257,30 +240,22 @@ template class GrandProductTests : public testing::Test { .lookup_grand_product_delta = 1, }; - Flavor::ProverPolynomials prover_polynomials; - prover_polynomials.w_l = proving_key->w_l; - prover_polynomials.w_r = proving_key->w_r; - prover_polynomials.w_o = proving_key->w_o; - prover_polynomials.w_l_shift = proving_key->w_l.shifted(); - prover_polynomials.w_r_shift = proving_key->w_r.shifted(); - prover_polynomials.w_o_shift = proving_key->w_o.shifted(); - prover_polynomials.sorted_accum = proving_key->sorted_accum; - prover_polynomials.sorted_accum_shift = proving_key->sorted_accum.shifted(); - prover_polynomials.table_1 = proving_key->table_1; - prover_polynomials.table_2 = proving_key->table_2; - prover_polynomials.table_3 = proving_key->table_3; - prover_polynomials.table_4 = proving_key->table_4; - prover_polynomials.table_1_shift = proving_key->table_1.shifted(); - prover_polynomials.table_2_shift = proving_key->table_2.shifted(); - prover_polynomials.table_3_shift = proving_key->table_3.shifted(); - prover_polynomials.table_4_shift = proving_key->table_4.shifted(); - prover_polynomials.q_m = proving_key->q_m; - prover_polynomials.q_r = proving_key->q_r; - prover_polynomials.q_o = proving_key->q_o; - prover_polynomials.q_c = proving_key->q_c; - prover_polynomials.q_lookup = proving_key->q_lookup; - prover_polynomials.z_perm = proving_key->z_perm; - prover_polynomials.z_lookup = proving_key->z_lookup; + typename Flavor::ProverPolynomials prover_polynomials; + for (auto [prover_poly, key_poly] : zip_view(prover_polynomials.get_unshifted(), proving_key->get_all())) { + ASSERT(proof_system::flavor_get_label(prover_polynomials, prover_poly) == + proof_system::flavor_get_label(*proving_key, key_poly)); + prover_poly = key_poly.share(); + } + for (auto [prover_poly, key_poly] : + zip_view(prover_polynomials.get_shifted(), proving_key->get_to_be_shifted())) { + ASSERT(proof_system::flavor_get_label(prover_polynomials, prover_poly) == + proof_system::flavor_get_label(*proving_key, key_poly) + "_shift"); + prover_poly = key_poly.shifted(); + } + // Test a few assignments + EXPECT_EQ(&proving_key->z_lookup[0], &prover_polynomials.z_lookup[0]); + EXPECT_EQ(&proving_key->sigma_1[0], &prover_polynomials.sigma_1[0]); + EXPECT_EQ(&proving_key->lagrange_last[0], &prover_polynomials.lagrange_last[0]); // Method 1: Compute z_lookup using the prover library method constexpr size_t LOOKUP_RELATION_INDEX = 1; @@ -377,4 +352,4 @@ TYPED_TEST(GrandProductTests, GrandProductLookup) TestFixture::test_lookup_grand_product_construction(); } -} // namespace grand_product_library_tests \ No newline at end of file +} // namespace grand_product_library_tests diff --git a/barretenberg/cpp/src/barretenberg/proof_system/polynomial_store/polynomial_store.cpp b/barretenberg/cpp/src/barretenberg/proof_system/polynomial_store/polynomial_store.cpp index 1cfad444e9f..8712f4c4f92 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/polynomial_store/polynomial_store.cpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/polynomial_store/polynomial_store.cpp @@ -26,7 +26,7 @@ template barretenberg::Polynomial PolynomialStore::get(std { // info("poly store get: ", key); // Take a shallow copy of the polynomial. Compiler will move the shallow copy to call site. - auto p = polynomial_map.at(key).clone(); + auto p = polynomial_map.at(key).share(); // info("got ", key, ": ", p.hash()); return p; }; diff --git a/barretenberg/cpp/src/barretenberg/proof_system/polynomial_store/polynomial_store_cache.cpp b/barretenberg/cpp/src/barretenberg/proof_system/polynomial_store/polynomial_store_cache.cpp index 5d06bdbb41a..7bf6912c651 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/polynomial_store/polynomial_store_cache.cpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/polynomial_store/polynomial_store_cache.cpp @@ -31,7 +31,7 @@ PolynomialStoreCache::Polynomial PolynomialStoreCache::get(std::string const& ke auto it = cache_.find(key); if (it != cache_.end()) { // info("cache get hit ", key); - return it->second.clone(); + return it->second.share(); } // info("cache get miss ", key); diff --git a/barretenberg/cpp/src/barretenberg/protogalaxy/combiner.test.cpp b/barretenberg/cpp/src/barretenberg/protogalaxy/combiner.test.cpp index 68f22195659..d69b45a9ef2 100644 --- a/barretenberg/cpp/src/barretenberg/protogalaxy/combiner.test.cpp +++ b/barretenberg/cpp/src/barretenberg/protogalaxy/combiner.test.cpp @@ -37,17 +37,15 @@ TEST(Protogalaxy, CombinerOn2Instances) // relation. if (is_random_input) { std::vector> instance_data(NUM_INSTANCES); - std::array storage_arrays; ProtoGalaxyProver prover; std::vector pow_betas = { FF(1), FF(2) }; for (size_t idx = 0; idx < NUM_INSTANCES; idx++) { auto instance = std::make_shared(); - auto [storage, prover_polynomials] = proof_system::honk::get_sequential_prover_polynomials( + auto prover_polynomials = proof_system::honk::get_sequential_prover_polynomials( /*log_circuit_size=*/1, idx * 128); restrict_to_standard_arithmetic_relation(prover_polynomials); - storage_arrays[idx] = std::move(storage); - instance->prover_polynomials = prover_polynomials; + instance->prover_polynomials = std::move(prover_polynomials); instance->instance_size = 2; instance_data[idx] = instance; } @@ -73,17 +71,15 @@ TEST(Protogalaxy, CombinerOn2Instances) EXPECT_EQ(result, expected_result); } else { std::vector> instance_data(NUM_INSTANCES); - std::array storage_arrays; ProtoGalaxyProver prover; std::vector pow_betas = { FF(1), FF(2) }; for (size_t idx = 0; idx < NUM_INSTANCES; idx++) { auto instance = std::make_shared(); - auto [storage, prover_polynomials] = proof_system::honk::get_zero_prover_polynomials( + auto prover_polynomials = proof_system::honk::get_zero_prover_polynomials( /*log_circuit_size=*/1); restrict_to_standard_arithmetic_relation(prover_polynomials); - storage_arrays[idx] = std::move(storage); - instance->prover_polynomials = prover_polynomials; + instance->prover_polynomials = std::move(prover_polynomials); instance->instance_size = 2; instance_data[idx] = instance; } @@ -166,16 +162,14 @@ TEST(Protogalaxy, CombinerOn4Instances) auto run_test = [&]() { std::vector> instance_data(NUM_INSTANCES); - std::array storage_arrays; ProtoGalaxyProver prover; std::vector pow_betas = { FF(1), FF(2) }; for (size_t idx = 0; idx < NUM_INSTANCES; idx++) { auto instance = std::make_shared(); - auto [storage, prover_polynomials] = proof_system::honk::get_zero_prover_polynomials( + auto prover_polynomials = proof_system::honk::get_zero_prover_polynomials( /*log_circuit_size=*/1); - storage_arrays[idx] = std::move(storage); - instance->prover_polynomials = prover_polynomials; + instance->prover_polynomials = std::move(prover_polynomials); instance->instance_size = 2; instance_data[idx] = instance; } diff --git a/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_prover.cpp b/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_prover.cpp index 4a95f231d52..80c33a1aab4 100644 --- a/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_prover.cpp +++ b/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_prover.cpp @@ -198,33 +198,23 @@ std::shared_ptr ProtoGalaxyProver_send_to_verifier("next_gate_challenge_" + std::to_string(idx), instances.next_gate_challenges[idx]); } - // Allocate space, initialised to 0, for the prover polynomials of the next accumulator - AllPolynomials storage; - for (auto& polynomial : storage.get_all()) { - polynomial = typename Flavor::Polynomial(instances[0]->instance_size); - for (auto& value : polynomial) { - value = FF(0); - } - } + // Initialize prover polynomials ProverPolynomials acc_prover_polynomials; - size_t poly_idx = 0; - auto prover_polynomial_pointers = acc_prover_polynomials.get_all(); - for (auto& polynomial : storage.get_all()) { - prover_polynomial_pointers[poly_idx] = polynomial; - poly_idx++; + for (auto& polynomial : acc_prover_polynomials.get_all()) { + polynomial = typename Flavor::Polynomial(instances[0]->instance_size); } // Fold the prover polynomials auto acc_poly_views = acc_prover_polynomials.get_all(); for (size_t inst_idx = 0; inst_idx < ProverInstances::NUM; inst_idx++) { - auto inst_poly_views = instances[inst_idx]->prover_polynomials.get_all(); - for (auto [acc_poly_view, inst_poly_view] : zip_view(acc_poly_views, inst_poly_views)) { - for (size_t poly_idx = 0; poly_idx < inst_poly_view.size(); poly_idx++) { - (acc_poly_view)[poly_idx] += (inst_poly_view)[poly_idx] * lagranges[inst_idx]; + for (auto [acc_poly, inst_poly] : + zip_view(acc_prover_polynomials.get_all(), instances[inst_idx]->prover_polynomials.get_all())) { + for (auto [acc_field, inst_field] : zip_view(acc_poly, inst_poly)) { + acc_field += inst_field * lagranges[inst_idx]; } } } - next_accumulator->prover_polynomials = acc_prover_polynomials; + next_accumulator->prover_polynomials = std::move(acc_prover_polynomials); // Fold the witness commtiments and send them to the verifier auto witness_labels = next_accumulator->commitment_labels.get_witness(); diff --git a/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_prover.hpp b/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_prover.hpp index 9c881d82a89..83aa42ae4ff 100644 --- a/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_prover.hpp +++ b/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_prover.hpp @@ -28,7 +28,6 @@ template class ProtoGalaxyProver_ { using CommitmentKey = typename Flavor::CommitmentKey; using WitnessCommitments = typename Flavor::WitnessCommitments; using Commitment = typename Flavor::Commitment; - using AllPolynomials = typename Flavor::AllPolynomials; using BaseUnivariate = Univariate; // The length of ExtendedUnivariate is the largest length (==max_relation_degree + 1) of a univariate polynomial diff --git a/barretenberg/cpp/src/barretenberg/serialize/msgpack_schema.test.cpp b/barretenberg/cpp/src/barretenberg/serialize/msgpack_schema.test.cpp index 9a035daf83b..e9f83617578 100644 --- a/barretenberg/cpp/src/barretenberg/serialize/msgpack_schema.test.cpp +++ b/barretenberg/cpp/src/barretenberg/serialize/msgpack_schema.test.cpp @@ -77,10 +77,10 @@ TEST(msgpack_tests, msgpack_schema_sanity) { EXPECT_EQ( msgpack_schema_to_string(good_example), - "{\"__typename\":\"GoodExample\",\"a\":[\"alias\",[\"Fr\",\"bin32\"]],\"b\":[\"alias\",[\"Fr\",\"bin32\"]]}\n"); + "{\"__typename\":\"GoodExample\",\"a\":[\"alias\",[\"fr\",\"bin32\"]],\"b\":[\"alias\",[\"fr\",\"bin32\"]]}\n"); EXPECT_EQ(msgpack_schema_to_string(complicated_schema), - "{\"__typename\":\"ComplicatedSchema\",\"array\":[\"vector\",[[\"array\",[[\"alias\",[\"Fr\",\"bin32\"]]," - "20]]]],\"good_or_not\":[\"optional\",[{\"__typename\":\"GoodExample\",\"a\":[\"alias\",[\"Fr\"," - "\"bin32\"]],\"b\":[\"alias\",[\"Fr\",\"bin32\"]]}]],\"bare\":[\"alias\",[\"Fr\",\"bin32\"]],\"huh\":[" - "\"variant\",[[\"alias\",[\"Fr\",\"bin32\"]],\"GoodExample\"]]}\n"); + "{\"__typename\":\"ComplicatedSchema\",\"array\":[\"vector\",[[\"array\",[[\"alias\",[\"fr\",\"bin32\"]]," + "20]]]],\"good_or_not\":[\"optional\",[{\"__typename\":\"GoodExample\",\"a\":[\"alias\",[\"fr\"," + "\"bin32\"]],\"b\":[\"alias\",[\"fr\",\"bin32\"]]}]],\"bare\":[\"alias\",[\"fr\",\"bin32\"]],\"huh\":[" + "\"variant\",[[\"alias\",[\"fr\",\"bin32\"]],\"GoodExample\"]]}\n"); } diff --git a/barretenberg/cpp/src/barretenberg/sumcheck/instance/prover_instance.cpp b/barretenberg/cpp/src/barretenberg/sumcheck/instance/prover_instance.cpp index 4e5a5d66123..836a4728bd6 100644 --- a/barretenberg/cpp/src/barretenberg/sumcheck/instance/prover_instance.cpp +++ b/barretenberg/cpp/src/barretenberg/sumcheck/instance/prover_instance.cpp @@ -55,10 +55,10 @@ template void ProverInstance_::compute_witness(Circuit& c // Construct the conventional wire polynomials auto wire_polynomials = construct_wire_polynomials_base(circuit, dyadic_circuit_size); - proving_key->w_l = wire_polynomials[0]; - proving_key->w_r = wire_polynomials[1]; - proving_key->w_o = wire_polynomials[2]; - proving_key->w_4 = wire_polynomials[3]; + proving_key->w_l = wire_polynomials[0].share(); + proving_key->w_r = wire_polynomials[1].share(); + proving_key->w_o = wire_polynomials[2].share(); + proving_key->w_4 = wire_polynomials[3].share(); // If Goblin, construct the ECC op queue wire and databus polynomials if constexpr (IsGoblinFlavor) { @@ -172,10 +172,10 @@ template void ProverInstance_::construct_ecc_op_wire_poly } } - proving_key->ecc_op_wire_1 = op_wire_polynomials[0]; - proving_key->ecc_op_wire_2 = op_wire_polynomials[1]; - proving_key->ecc_op_wire_3 = op_wire_polynomials[2]; - proving_key->ecc_op_wire_4 = op_wire_polynomials[3]; + proving_key->ecc_op_wire_1 = op_wire_polynomials[0].share(); + proving_key->ecc_op_wire_2 = op_wire_polynomials[1].share(); + proving_key->ecc_op_wire_3 = op_wire_polynomials[2].share(); + proving_key->ecc_op_wire_4 = op_wire_polynomials[3].share(); } /** @@ -198,8 +198,8 @@ void ProverInstance_::construct_databus_polynomials(Circuit& circuit) calldata_read_counts[idx] = circuit.get_variable(circuit.calldata_read_counts[idx]); } - proving_key->calldata = public_calldata; - proving_key->calldata_read_counts = calldata_read_counts; + proving_key->calldata = public_calldata.share(); + proving_key->calldata_read_counts = calldata_read_counts.share(); } template @@ -256,10 +256,10 @@ std::shared_ptr ProverInstance_::compute_pr // Polynomial memory is zeroed out when constructed with size hint, so we don't have to initialize trailing // space - proving_key->table_1 = poly_q_table_column_1; - proving_key->table_2 = poly_q_table_column_2; - proving_key->table_3 = poly_q_table_column_3; - proving_key->table_4 = poly_q_table_column_4; + proving_key->table_1 = poly_q_table_column_1.share(); + proving_key->table_2 = poly_q_table_column_2.share(); + proving_key->table_3 = poly_q_table_column_3.share(); + proving_key->table_4 = poly_q_table_column_4.share(); proving_key->recursive_proof_public_input_indices = std::vector(recursive_proof_public_input_indices.begin(), recursive_proof_public_input_indices.end()); @@ -273,7 +273,7 @@ std::shared_ptr ProverInstance_::compute_pr for (size_t i = 0; i < databus_id.size(); ++i) { databus_id[i] = i; } - proving_key->databus_id = databus_id; + proving_key->databus_id = databus_id.share(); } return proving_key; @@ -281,65 +281,15 @@ std::shared_ptr ProverInstance_::compute_pr template void ProverInstance_::initialize_prover_polynomials() { - prover_polynomials.q_c = proving_key->q_c; - prover_polynomials.q_l = proving_key->q_l; - prover_polynomials.q_r = proving_key->q_r; - prover_polynomials.q_o = proving_key->q_o; - prover_polynomials.q_m = proving_key->q_m; - prover_polynomials.sigma_1 = proving_key->sigma_1; - prover_polynomials.sigma_2 = proving_key->sigma_2; - prover_polynomials.sigma_3 = proving_key->sigma_3; - prover_polynomials.id_1 = proving_key->id_1; - prover_polynomials.id_2 = proving_key->id_2; - prover_polynomials.id_3 = proving_key->id_3; - prover_polynomials.lagrange_first = proving_key->lagrange_first; - prover_polynomials.lagrange_last = proving_key->lagrange_last; - prover_polynomials.w_l = proving_key->w_l; - prover_polynomials.w_r = proving_key->w_r; - prover_polynomials.w_o = proving_key->w_o; - - prover_polynomials.q_4 = proving_key->q_4; - prover_polynomials.q_arith = proving_key->q_arith; - prover_polynomials.q_sort = proving_key->q_sort; - prover_polynomials.q_elliptic = proving_key->q_elliptic; - prover_polynomials.q_aux = proving_key->q_aux; - prover_polynomials.q_lookup = proving_key->q_lookup; - prover_polynomials.sigma_4 = proving_key->sigma_4; - prover_polynomials.id_4 = proving_key->id_4; - prover_polynomials.table_1 = proving_key->table_1; - prover_polynomials.table_2 = proving_key->table_2; - prover_polynomials.table_3 = proving_key->table_3; - prover_polynomials.table_4 = proving_key->table_4; - prover_polynomials.table_1_shift = proving_key->table_1.shifted(); - prover_polynomials.table_2_shift = proving_key->table_2.shifted(); - prover_polynomials.table_3_shift = proving_key->table_3.shifted(); - prover_polynomials.table_4_shift = proving_key->table_4.shifted(); - prover_polynomials.w_l_shift = proving_key->w_l.shifted(); - prover_polynomials.w_r_shift = proving_key->w_r.shifted(); - prover_polynomials.w_o_shift = proving_key->w_o.shifted(); - - if constexpr (IsGoblinFlavor) { - prover_polynomials.ecc_op_wire_1 = proving_key->ecc_op_wire_1; - prover_polynomials.ecc_op_wire_2 = proving_key->ecc_op_wire_2; - prover_polynomials.ecc_op_wire_3 = proving_key->ecc_op_wire_3; - prover_polynomials.ecc_op_wire_4 = proving_key->ecc_op_wire_4; - prover_polynomials.lagrange_ecc_op = proving_key->lagrange_ecc_op; - // DataBus polynomials - prover_polynomials.calldata = proving_key->calldata; - prover_polynomials.calldata_read_counts = proving_key->calldata_read_counts; - prover_polynomials.lookup_inverses = proving_key->lookup_inverses; - prover_polynomials.q_busread = proving_key->q_busread; - prover_polynomials.databus_id = proving_key->databus_id; - prover_polynomials.q_poseidon2_external = proving_key->q_poseidon2_external; - prover_polynomials.q_poseidon2_internal = proving_key->q_poseidon2_internal; + for (auto [prover_poly, key_poly] : zip_view(prover_polynomials.get_unshifted(), proving_key->get_all())) { + ASSERT(flavor_get_label(prover_polynomials, prover_poly) == flavor_get_label(*proving_key, key_poly)); + prover_poly = key_poly.share(); + } + for (auto [prover_poly, key_poly] : zip_view(prover_polynomials.get_shifted(), proving_key->get_to_be_shifted())) { + ASSERT(flavor_get_label(prover_polynomials, prover_poly) == + (flavor_get_label(*proving_key, key_poly) + "_shift")); + prover_poly = key_poly.shifted(); } - - // These polynomials have not yet been computed; initialize them so prover_polynomials is "full" and we can use - // utilities like get_row() - prover_polynomials.z_perm = proving_key->z_perm; - prover_polynomials.z_lookup = proving_key->z_lookup; - prover_polynomials.z_perm_shift = proving_key->z_perm.shifted(); - prover_polynomials.z_lookup_shift = proving_key->z_lookup.shifted(); std::span public_wires_source = prover_polynomials.w_r; @@ -363,12 +313,12 @@ template void ProverInstance_::compute_sorted_accumulator relation_parameters.eta = eta; // Compute sorted witness-table accumulator compute_sorted_list_accumulator(eta); - prover_polynomials.sorted_accum = proving_key->sorted_accum; + prover_polynomials.sorted_accum = proving_key->sorted_accum.share(); prover_polynomials.sorted_accum_shift = proving_key->sorted_accum.shifted(); // Finalize fourth wire polynomial by adding lookup memory records add_plookup_memory_records_to_wire_4(eta); - prover_polynomials.w_4 = proving_key->w_4; + prover_polynomials.w_4 = proving_key->w_4.share(); prover_polynomials.w_4_shift = proving_key->w_4.shifted(); } @@ -400,7 +350,7 @@ template void ProverInstance_::compute_sorted_list_accumu T0 += sorted_polynomials[0][i]; sorted_list_accumulator[i] = T0; } - proving_key->sorted_accum = sorted_list_accumulator; + proving_key->sorted_accum = sorted_list_accumulator.share(); } /** diff --git a/barretenberg/cpp/src/barretenberg/sumcheck/instance/prover_instance.hpp b/barretenberg/cpp/src/barretenberg/sumcheck/instance/prover_instance.hpp index ea09aa6fceb..5c688ffecd5 100644 --- a/barretenberg/cpp/src/barretenberg/sumcheck/instance/prover_instance.hpp +++ b/barretenberg/cpp/src/barretenberg/sumcheck/instance/prover_instance.hpp @@ -107,4 +107,4 @@ template class ProverInstance_ { extern template class ProverInstance_; extern template class ProverInstance_; -} // namespace proof_system::honk \ No newline at end of file +} // namespace proof_system::honk diff --git a/barretenberg/cpp/src/barretenberg/sumcheck/sumcheck.hpp b/barretenberg/cpp/src/barretenberg/sumcheck/sumcheck.hpp index bfd9c2d7f54..e2a0a703cf6 100644 --- a/barretenberg/cpp/src/barretenberg/sumcheck/sumcheck.hpp +++ b/barretenberg/cpp/src/barretenberg/sumcheck/sumcheck.hpp @@ -70,7 +70,7 @@ template class SumcheckProver { * * @details */ - SumcheckOutput prove(ProverPolynomials full_polynomials, + SumcheckOutput prove(ProverPolynomials& full_polynomials, const proof_system::RelationParameters& relation_parameters, FF alpha) // pass by value, not by reference { diff --git a/barretenberg/cpp/src/barretenberg/sumcheck/sumcheck.test.cpp b/barretenberg/cpp/src/barretenberg/sumcheck/sumcheck.test.cpp index 2eaa93341a8..ac2f53ccd02 100644 --- a/barretenberg/cpp/src/barretenberg/sumcheck/sumcheck.test.cpp +++ b/barretenberg/cpp/src/barretenberg/sumcheck/sumcheck.test.cpp @@ -33,50 +33,9 @@ barretenberg::Polynomial random_poly(size_t size) ProverPolynomials construct_ultra_full_polynomials(auto& input_polynomials) { ProverPolynomials full_polynomials; - full_polynomials.q_c = input_polynomials[0]; - full_polynomials.q_l = input_polynomials[1]; - full_polynomials.q_r = input_polynomials[2]; - full_polynomials.q_o = input_polynomials[3]; - full_polynomials.q_4 = input_polynomials[4]; - full_polynomials.q_m = input_polynomials[5]; - full_polynomials.q_arith = input_polynomials[6]; - full_polynomials.q_sort = input_polynomials[7]; - full_polynomials.q_elliptic = input_polynomials[8]; - full_polynomials.q_aux = input_polynomials[9]; - full_polynomials.q_lookup = input_polynomials[10]; - full_polynomials.sigma_1 = input_polynomials[11]; - full_polynomials.sigma_2 = input_polynomials[12]; - full_polynomials.sigma_3 = input_polynomials[13]; - full_polynomials.sigma_4 = input_polynomials[14]; - full_polynomials.id_1 = input_polynomials[15]; - full_polynomials.id_2 = input_polynomials[16]; - full_polynomials.id_3 = input_polynomials[17]; - full_polynomials.id_4 = input_polynomials[18]; - full_polynomials.table_1 = input_polynomials[19]; - full_polynomials.table_2 = input_polynomials[20]; - full_polynomials.table_3 = input_polynomials[21]; - full_polynomials.table_4 = input_polynomials[22]; - full_polynomials.lagrange_first = input_polynomials[23]; - full_polynomials.lagrange_last = input_polynomials[24]; - full_polynomials.w_l = input_polynomials[25]; - full_polynomials.w_r = input_polynomials[26]; - full_polynomials.w_o = input_polynomials[27]; - full_polynomials.w_4 = input_polynomials[28]; - full_polynomials.sorted_accum = input_polynomials[29]; - full_polynomials.z_perm = input_polynomials[30]; - full_polynomials.z_lookup = input_polynomials[31]; - full_polynomials.table_1_shift = input_polynomials[32]; - full_polynomials.table_2_shift = input_polynomials[33]; - full_polynomials.table_3_shift = input_polynomials[34]; - full_polynomials.table_4_shift = input_polynomials[35]; - full_polynomials.w_l_shift = input_polynomials[36]; - full_polynomials.w_r_shift = input_polynomials[37]; - full_polynomials.w_o_shift = input_polynomials[38]; - full_polynomials.w_4_shift = input_polynomials[39]; - full_polynomials.sorted_accum_shift = input_polynomials[40]; - full_polynomials.z_perm_shift = input_polynomials[41]; - full_polynomials.z_lookup_shift = input_polynomials[42]; - + for (auto [full_poly, input_poly] : zip_view(full_polynomials.get_all(), input_polynomials)) { + full_poly = input_poly.share(); + } return full_polynomials; } diff --git a/barretenberg/cpp/src/barretenberg/sumcheck/sumcheck_round.hpp b/barretenberg/cpp/src/barretenberg/sumcheck/sumcheck_round.hpp index 7ec78a73799..2a52cd665f7 100644 --- a/barretenberg/cpp/src/barretenberg/sumcheck/sumcheck_round.hpp +++ b/barretenberg/cpp/src/barretenberg/sumcheck/sumcheck_round.hpp @@ -89,7 +89,7 @@ template class SumcheckProverRound { size_t edge_idx) { for (auto [extended_edge, multivariate] : zip_view(extended_edges.get_all(), multivariates.get_all())) { - auto edge = barretenberg::Univariate({ multivariate[edge_idx], multivariate[edge_idx + 1] }); + barretenberg::Univariate edge({ multivariate[edge_idx], multivariate[edge_idx + 1] }); extended_edge = edge.template extend_to(); } } diff --git a/barretenberg/cpp/src/barretenberg/translator_vm/goblin_translator_composer.cpp b/barretenberg/cpp/src/barretenberg/translator_vm/goblin_translator_composer.cpp index ba6345c2a64..1e23cf12f4a 100644 --- a/barretenberg/cpp/src/barretenberg/translator_vm/goblin_translator_composer.cpp +++ b/barretenberg/cpp/src/barretenberg/translator_vm/goblin_translator_composer.cpp @@ -99,7 +99,7 @@ void GoblinTranslatorComposer::compute_witness(CircuitBuilder& circuit_builder) // Construct the conventional wire polynomials auto wire_polynomials = construct_wire_polynomials_base_goblin_translator(circuit_builder, dyadic_circuit_size); - // Most of the witness polynomials are the original wire polynomials + // TODO(AD): figure out how to get this in a loop, why does NUM_WIRES=81? @kesha proving_key->op = wire_polynomials[0]; proving_key->x_lo_y_hi = wire_polynomials[1]; proving_key->x_hi_z_1 = wire_polynomials[2]; diff --git a/barretenberg/cpp/src/barretenberg/translator_vm/goblin_translator_prover.cpp b/barretenberg/cpp/src/barretenberg/translator_vm/goblin_translator_prover.cpp index aeaa9a2719a..b398f982d30 100644 --- a/barretenberg/cpp/src/barretenberg/translator_vm/goblin_translator_prover.cpp +++ b/barretenberg/cpp/src/barretenberg/translator_vm/goblin_translator_prover.cpp @@ -33,217 +33,20 @@ GoblinTranslatorProver::GoblinTranslatorProver(const std::shared_ptrop; - prover_polynomials.x_lo_y_hi = key->x_lo_y_hi; - prover_polynomials.x_hi_z_1 = key->x_hi_z_1; - prover_polynomials.y_lo_z_2 = key->y_lo_z_2; - prover_polynomials.p_x_low_limbs = key->p_x_low_limbs; - prover_polynomials.p_x_low_limbs_range_constraint_0 = key->p_x_low_limbs_range_constraint_0; - prover_polynomials.p_x_low_limbs_range_constraint_1 = key->p_x_low_limbs_range_constraint_1; - prover_polynomials.p_x_low_limbs_range_constraint_2 = key->p_x_low_limbs_range_constraint_2; - prover_polynomials.p_x_low_limbs_range_constraint_3 = key->p_x_low_limbs_range_constraint_3; - prover_polynomials.p_x_low_limbs_range_constraint_4 = key->p_x_low_limbs_range_constraint_4; - prover_polynomials.p_x_low_limbs_range_constraint_tail = key->p_x_low_limbs_range_constraint_tail; - prover_polynomials.p_x_high_limbs = key->p_x_high_limbs; - prover_polynomials.p_x_high_limbs_range_constraint_0 = key->p_x_high_limbs_range_constraint_0; - prover_polynomials.p_x_high_limbs_range_constraint_1 = key->p_x_high_limbs_range_constraint_1; - prover_polynomials.p_x_high_limbs_range_constraint_2 = key->p_x_high_limbs_range_constraint_2; - prover_polynomials.p_x_high_limbs_range_constraint_3 = key->p_x_high_limbs_range_constraint_3; - prover_polynomials.p_x_high_limbs_range_constraint_4 = key->p_x_high_limbs_range_constraint_4; - prover_polynomials.p_x_high_limbs_range_constraint_tail = key->p_x_high_limbs_range_constraint_tail; - prover_polynomials.p_y_low_limbs = key->p_y_low_limbs; - prover_polynomials.p_y_low_limbs_range_constraint_0 = key->p_y_low_limbs_range_constraint_0; - prover_polynomials.p_y_low_limbs_range_constraint_1 = key->p_y_low_limbs_range_constraint_1; - prover_polynomials.p_y_low_limbs_range_constraint_2 = key->p_y_low_limbs_range_constraint_2; - prover_polynomials.p_y_low_limbs_range_constraint_3 = key->p_y_low_limbs_range_constraint_3; - prover_polynomials.p_y_low_limbs_range_constraint_4 = key->p_y_low_limbs_range_constraint_4; - prover_polynomials.p_y_low_limbs_range_constraint_tail = key->p_y_low_limbs_range_constraint_tail; - prover_polynomials.p_y_high_limbs = key->p_y_high_limbs; - prover_polynomials.p_y_high_limbs_range_constraint_0 = key->p_y_high_limbs_range_constraint_0; - prover_polynomials.p_y_high_limbs_range_constraint_1 = key->p_y_high_limbs_range_constraint_1; - prover_polynomials.p_y_high_limbs_range_constraint_2 = key->p_y_high_limbs_range_constraint_2; - prover_polynomials.p_y_high_limbs_range_constraint_3 = key->p_y_high_limbs_range_constraint_3; - prover_polynomials.p_y_high_limbs_range_constraint_4 = key->p_y_high_limbs_range_constraint_4; - prover_polynomials.p_y_high_limbs_range_constraint_tail = key->p_y_high_limbs_range_constraint_tail; - prover_polynomials.z_low_limbs = key->z_low_limbs; - prover_polynomials.z_low_limbs_range_constraint_0 = key->z_low_limbs_range_constraint_0; - prover_polynomials.z_low_limbs_range_constraint_1 = key->z_low_limbs_range_constraint_1; - prover_polynomials.z_low_limbs_range_constraint_2 = key->z_low_limbs_range_constraint_2; - prover_polynomials.z_low_limbs_range_constraint_3 = key->z_low_limbs_range_constraint_3; - prover_polynomials.z_low_limbs_range_constraint_4 = key->z_low_limbs_range_constraint_4; - prover_polynomials.z_low_limbs_range_constraint_tail = key->z_low_limbs_range_constraint_tail; - prover_polynomials.z_high_limbs = key->z_high_limbs; - prover_polynomials.z_high_limbs_range_constraint_0 = key->z_high_limbs_range_constraint_0; - prover_polynomials.z_high_limbs_range_constraint_1 = key->z_high_limbs_range_constraint_1; - prover_polynomials.z_high_limbs_range_constraint_2 = key->z_high_limbs_range_constraint_2; - prover_polynomials.z_high_limbs_range_constraint_3 = key->z_high_limbs_range_constraint_3; - prover_polynomials.z_high_limbs_range_constraint_4 = key->z_high_limbs_range_constraint_4; - prover_polynomials.z_high_limbs_range_constraint_tail = key->z_high_limbs_range_constraint_tail; - prover_polynomials.accumulators_binary_limbs_0 = key->accumulators_binary_limbs_0; - prover_polynomials.accumulators_binary_limbs_1 = key->accumulators_binary_limbs_1; - prover_polynomials.accumulators_binary_limbs_2 = key->accumulators_binary_limbs_2; - prover_polynomials.accumulators_binary_limbs_3 = key->accumulators_binary_limbs_3; - prover_polynomials.accumulator_low_limbs_range_constraint_0 = key->accumulator_low_limbs_range_constraint_0; - prover_polynomials.accumulator_low_limbs_range_constraint_1 = key->accumulator_low_limbs_range_constraint_1; - prover_polynomials.accumulator_low_limbs_range_constraint_2 = key->accumulator_low_limbs_range_constraint_2; - prover_polynomials.accumulator_low_limbs_range_constraint_3 = key->accumulator_low_limbs_range_constraint_3; - prover_polynomials.accumulator_low_limbs_range_constraint_4 = key->accumulator_low_limbs_range_constraint_4; - prover_polynomials.accumulator_low_limbs_range_constraint_tail = key->accumulator_low_limbs_range_constraint_tail; - prover_polynomials.accumulator_high_limbs_range_constraint_0 = key->accumulator_high_limbs_range_constraint_0; - prover_polynomials.accumulator_high_limbs_range_constraint_1 = key->accumulator_high_limbs_range_constraint_1; - prover_polynomials.accumulator_high_limbs_range_constraint_2 = key->accumulator_high_limbs_range_constraint_2; - prover_polynomials.accumulator_high_limbs_range_constraint_3 = key->accumulator_high_limbs_range_constraint_3; - prover_polynomials.accumulator_high_limbs_range_constraint_4 = key->accumulator_high_limbs_range_constraint_4; - prover_polynomials.accumulator_high_limbs_range_constraint_tail = key->accumulator_high_limbs_range_constraint_tail; - prover_polynomials.quotient_low_binary_limbs = key->quotient_low_binary_limbs; - prover_polynomials.quotient_high_binary_limbs = key->quotient_high_binary_limbs; - prover_polynomials.quotient_low_limbs_range_constraint_0 = key->quotient_low_limbs_range_constraint_0; - prover_polynomials.quotient_low_limbs_range_constraint_1 = key->quotient_low_limbs_range_constraint_1; - prover_polynomials.quotient_low_limbs_range_constraint_2 = key->quotient_low_limbs_range_constraint_2; - prover_polynomials.quotient_low_limbs_range_constraint_3 = key->quotient_low_limbs_range_constraint_3; - prover_polynomials.quotient_low_limbs_range_constraint_4 = key->quotient_low_limbs_range_constraint_4; - prover_polynomials.quotient_low_limbs_range_constraint_tail = key->quotient_low_limbs_range_constraint_tail; - prover_polynomials.quotient_high_limbs_range_constraint_0 = key->quotient_high_limbs_range_constraint_0; - prover_polynomials.quotient_high_limbs_range_constraint_1 = key->quotient_high_limbs_range_constraint_1; - prover_polynomials.quotient_high_limbs_range_constraint_2 = key->quotient_high_limbs_range_constraint_2; - prover_polynomials.quotient_high_limbs_range_constraint_3 = key->quotient_high_limbs_range_constraint_3; - prover_polynomials.quotient_high_limbs_range_constraint_4 = key->quotient_high_limbs_range_constraint_4; - prover_polynomials.quotient_high_limbs_range_constraint_tail = key->quotient_high_limbs_range_constraint_tail; - prover_polynomials.relation_wide_limbs = key->relation_wide_limbs; - prover_polynomials.relation_wide_limbs_range_constraint_0 = key->relation_wide_limbs_range_constraint_0; - prover_polynomials.relation_wide_limbs_range_constraint_1 = key->relation_wide_limbs_range_constraint_1; - prover_polynomials.relation_wide_limbs_range_constraint_2 = key->relation_wide_limbs_range_constraint_2; - prover_polynomials.relation_wide_limbs_range_constraint_3 = key->relation_wide_limbs_range_constraint_3; + for (auto [prover_poly, key_poly] : zip_view(prover_polynomials.get_unshifted(), key->get_all())) { + ASSERT(flavor_get_label(prover_polynomials, prover_poly) == flavor_get_label(*key, key_poly)); + prover_poly = key_poly.share(); + } + for (auto [prover_poly, key_poly] : zip_view(prover_polynomials.get_shifted(), key->get_to_be_shifted())) { + ASSERT(flavor_get_label(prover_polynomials, prover_poly) == flavor_get_label(*key, key_poly) + "_shift"); + prover_poly = key_poly.shifted(); + } + // TODO(https://github.com/AztecProtocol/barretenberg/issues/810): resolve weirdness around concatenated range + // constraints prover_polynomials.concatenated_range_constraints_0 = key->concatenated_range_constraints_0; prover_polynomials.concatenated_range_constraints_1 = key->concatenated_range_constraints_1; prover_polynomials.concatenated_range_constraints_2 = key->concatenated_range_constraints_2; prover_polynomials.concatenated_range_constraints_3 = key->concatenated_range_constraints_3; - prover_polynomials.ordered_range_constraints_0 = key->ordered_range_constraints_0; - prover_polynomials.ordered_range_constraints_1 = key->ordered_range_constraints_1; - prover_polynomials.ordered_range_constraints_2 = key->ordered_range_constraints_2; - prover_polynomials.ordered_range_constraints_3 = key->ordered_range_constraints_3; - prover_polynomials.ordered_range_constraints_4 = key->ordered_range_constraints_4; - prover_polynomials.x_lo_y_hi_shift = key->x_lo_y_hi.shifted(); - prover_polynomials.x_hi_z_1_shift = key->x_hi_z_1.shifted(); - prover_polynomials.y_lo_z_2_shift = key->y_lo_z_2.shifted(); - prover_polynomials.p_x_low_limbs_shift = key->p_x_low_limbs.shifted(); - prover_polynomials.p_x_low_limbs_range_constraint_0_shift = key->p_x_low_limbs_range_constraint_0.shifted(); - prover_polynomials.p_x_low_limbs_range_constraint_1_shift = key->p_x_low_limbs_range_constraint_1.shifted(); - prover_polynomials.p_x_low_limbs_range_constraint_2_shift = key->p_x_low_limbs_range_constraint_2.shifted(); - prover_polynomials.p_x_low_limbs_range_constraint_3_shift = key->p_x_low_limbs_range_constraint_3.shifted(); - prover_polynomials.p_x_low_limbs_range_constraint_4_shift = key->p_x_low_limbs_range_constraint_4.shifted(); - prover_polynomials.p_x_low_limbs_range_constraint_tail_shift = key->p_x_low_limbs_range_constraint_tail.shifted(); - prover_polynomials.p_x_high_limbs_shift = key->p_x_high_limbs.shifted(); - prover_polynomials.p_x_high_limbs_range_constraint_0_shift = key->p_x_high_limbs_range_constraint_0.shifted(); - prover_polynomials.p_x_high_limbs_range_constraint_1_shift = key->p_x_high_limbs_range_constraint_1.shifted(); - prover_polynomials.p_x_high_limbs_range_constraint_2_shift = key->p_x_high_limbs_range_constraint_2.shifted(); - prover_polynomials.p_x_high_limbs_range_constraint_3_shift = key->p_x_high_limbs_range_constraint_3.shifted(); - prover_polynomials.p_x_high_limbs_range_constraint_4_shift = key->p_x_high_limbs_range_constraint_4.shifted(); - prover_polynomials.p_x_high_limbs_range_constraint_tail_shift = key->p_x_high_limbs_range_constraint_tail.shifted(); - prover_polynomials.p_y_low_limbs_shift = key->p_y_low_limbs.shifted(); - prover_polynomials.p_y_low_limbs_range_constraint_0_shift = key->p_y_low_limbs_range_constraint_0.shifted(); - prover_polynomials.p_y_low_limbs_range_constraint_1_shift = key->p_y_low_limbs_range_constraint_1.shifted(); - prover_polynomials.p_y_low_limbs_range_constraint_2_shift = key->p_y_low_limbs_range_constraint_2.shifted(); - prover_polynomials.p_y_low_limbs_range_constraint_3_shift = key->p_y_low_limbs_range_constraint_3.shifted(); - prover_polynomials.p_y_low_limbs_range_constraint_4_shift = key->p_y_low_limbs_range_constraint_4.shifted(); - prover_polynomials.p_y_low_limbs_range_constraint_tail_shift = key->p_y_low_limbs_range_constraint_tail.shifted(); - prover_polynomials.p_y_high_limbs_shift = key->p_y_high_limbs.shifted(); - prover_polynomials.p_y_high_limbs_range_constraint_0_shift = key->p_y_high_limbs_range_constraint_0.shifted(); - prover_polynomials.p_y_high_limbs_range_constraint_1_shift = key->p_y_high_limbs_range_constraint_1.shifted(); - prover_polynomials.p_y_high_limbs_range_constraint_2_shift = key->p_y_high_limbs_range_constraint_2.shifted(); - prover_polynomials.p_y_high_limbs_range_constraint_3_shift = key->p_y_high_limbs_range_constraint_3.shifted(); - prover_polynomials.p_y_high_limbs_range_constraint_4_shift = key->p_y_high_limbs_range_constraint_4.shifted(); - prover_polynomials.p_y_high_limbs_range_constraint_tail_shift = key->p_y_high_limbs_range_constraint_tail.shifted(); - prover_polynomials.z_low_limbs_shift = key->z_low_limbs.shifted(); - prover_polynomials.z_low_limbs_range_constraint_0_shift = key->z_low_limbs_range_constraint_0.shifted(); - prover_polynomials.z_low_limbs_range_constraint_1_shift = key->z_low_limbs_range_constraint_1.shifted(); - prover_polynomials.z_low_limbs_range_constraint_2_shift = key->z_low_limbs_range_constraint_2.shifted(); - prover_polynomials.z_low_limbs_range_constraint_3_shift = key->z_low_limbs_range_constraint_3.shifted(); - prover_polynomials.z_low_limbs_range_constraint_4_shift = key->z_low_limbs_range_constraint_4.shifted(); - prover_polynomials.z_low_limbs_range_constraint_tail_shift = key->z_low_limbs_range_constraint_tail.shifted(); - prover_polynomials.z_high_limbs_shift = key->z_high_limbs.shifted(); - prover_polynomials.z_high_limbs_range_constraint_0_shift = key->z_high_limbs_range_constraint_0.shifted(); - prover_polynomials.z_high_limbs_range_constraint_1_shift = key->z_high_limbs_range_constraint_1.shifted(); - prover_polynomials.z_high_limbs_range_constraint_2_shift = key->z_high_limbs_range_constraint_2.shifted(); - prover_polynomials.z_high_limbs_range_constraint_3_shift = key->z_high_limbs_range_constraint_3.shifted(); - prover_polynomials.z_high_limbs_range_constraint_4_shift = key->z_high_limbs_range_constraint_4.shifted(); - prover_polynomials.z_high_limbs_range_constraint_tail_shift = key->z_high_limbs_range_constraint_tail.shifted(); - prover_polynomials.accumulators_binary_limbs_0_shift = key->accumulators_binary_limbs_0.shifted(); - prover_polynomials.accumulators_binary_limbs_1_shift = key->accumulators_binary_limbs_1.shifted(); - prover_polynomials.accumulators_binary_limbs_2_shift = key->accumulators_binary_limbs_2.shifted(); - prover_polynomials.accumulators_binary_limbs_3_shift = key->accumulators_binary_limbs_3.shifted(); - prover_polynomials.accumulator_low_limbs_range_constraint_0_shift = - key->accumulator_low_limbs_range_constraint_0.shifted(); - prover_polynomials.accumulator_low_limbs_range_constraint_1_shift = - key->accumulator_low_limbs_range_constraint_1.shifted(); - prover_polynomials.accumulator_low_limbs_range_constraint_2_shift = - key->accumulator_low_limbs_range_constraint_2.shifted(); - prover_polynomials.accumulator_low_limbs_range_constraint_3_shift = - key->accumulator_low_limbs_range_constraint_3.shifted(); - prover_polynomials.accumulator_low_limbs_range_constraint_4_shift = - key->accumulator_low_limbs_range_constraint_4.shifted(); - prover_polynomials.accumulator_low_limbs_range_constraint_tail_shift = - key->accumulator_low_limbs_range_constraint_tail.shifted(); - prover_polynomials.accumulator_high_limbs_range_constraint_0_shift = - key->accumulator_high_limbs_range_constraint_0.shifted(); - prover_polynomials.accumulator_high_limbs_range_constraint_1_shift = - key->accumulator_high_limbs_range_constraint_1.shifted(); - prover_polynomials.accumulator_high_limbs_range_constraint_2_shift = - key->accumulator_high_limbs_range_constraint_2.shifted(); - prover_polynomials.accumulator_high_limbs_range_constraint_3_shift = - key->accumulator_high_limbs_range_constraint_3.shifted(); - prover_polynomials.accumulator_high_limbs_range_constraint_4_shift = - key->accumulator_high_limbs_range_constraint_4.shifted(); - prover_polynomials.accumulator_high_limbs_range_constraint_tail_shift = - key->accumulator_high_limbs_range_constraint_tail.shifted(); - prover_polynomials.quotient_low_binary_limbs_shift = key->quotient_low_binary_limbs.shifted(); - prover_polynomials.quotient_high_binary_limbs_shift = key->quotient_high_binary_limbs.shifted(); - prover_polynomials.quotient_low_limbs_range_constraint_0_shift = - key->quotient_low_limbs_range_constraint_0.shifted(); - prover_polynomials.quotient_low_limbs_range_constraint_1_shift = - key->quotient_low_limbs_range_constraint_1.shifted(); - prover_polynomials.quotient_low_limbs_range_constraint_2_shift = - key->quotient_low_limbs_range_constraint_2.shifted(); - prover_polynomials.quotient_low_limbs_range_constraint_3_shift = - key->quotient_low_limbs_range_constraint_3.shifted(); - prover_polynomials.quotient_low_limbs_range_constraint_4_shift = - key->quotient_low_limbs_range_constraint_4.shifted(); - prover_polynomials.quotient_low_limbs_range_constraint_tail_shift = - key->quotient_low_limbs_range_constraint_tail.shifted(); - prover_polynomials.quotient_high_limbs_range_constraint_0_shift = - key->quotient_high_limbs_range_constraint_0.shifted(); - prover_polynomials.quotient_high_limbs_range_constraint_1_shift = - key->quotient_high_limbs_range_constraint_1.shifted(); - prover_polynomials.quotient_high_limbs_range_constraint_2_shift = - key->quotient_high_limbs_range_constraint_2.shifted(); - prover_polynomials.quotient_high_limbs_range_constraint_3_shift = - key->quotient_high_limbs_range_constraint_3.shifted(); - prover_polynomials.quotient_high_limbs_range_constraint_4_shift = - key->quotient_high_limbs_range_constraint_4.shifted(); - prover_polynomials.quotient_high_limbs_range_constraint_tail_shift = - key->quotient_high_limbs_range_constraint_tail.shifted(); - prover_polynomials.relation_wide_limbs_shift = key->relation_wide_limbs.shifted(); - prover_polynomials.relation_wide_limbs_range_constraint_0_shift = - key->relation_wide_limbs_range_constraint_0.shifted(); - prover_polynomials.relation_wide_limbs_range_constraint_1_shift = - key->relation_wide_limbs_range_constraint_1.shifted(); - prover_polynomials.relation_wide_limbs_range_constraint_2_shift = - key->relation_wide_limbs_range_constraint_2.shifted(); - prover_polynomials.relation_wide_limbs_range_constraint_3_shift = - key->relation_wide_limbs_range_constraint_3.shifted(); - prover_polynomials.ordered_range_constraints_0_shift = key->ordered_range_constraints_0.shifted(); - prover_polynomials.ordered_range_constraints_1_shift = key->ordered_range_constraints_1.shifted(); - prover_polynomials.ordered_range_constraints_2_shift = key->ordered_range_constraints_2.shifted(); - prover_polynomials.ordered_range_constraints_3_shift = key->ordered_range_constraints_3.shifted(); - prover_polynomials.ordered_range_constraints_4_shift = key->ordered_range_constraints_4.shifted(); - prover_polynomials.lagrange_first = key->lagrange_first; - prover_polynomials.lagrange_last = key->lagrange_last; - prover_polynomials.lagrange_odd_in_minicircuit = key->lagrange_odd_in_minicircuit; - prover_polynomials.lagrange_even_in_minicircuit = key->lagrange_even_in_minicircuit; - prover_polynomials.lagrange_second = key->lagrange_second; - prover_polynomials.lagrange_second_to_last_in_minicircuit = key->lagrange_second_to_last_in_minicircuit; - prover_polynomials.ordered_extra_range_constraints_numerator = key->ordered_extra_range_constraints_numerator; } /** diff --git a/barretenberg/cpp/src/barretenberg/ultra_honk/protogalaxy.test.cpp b/barretenberg/cpp/src/barretenberg/ultra_honk/protogalaxy.test.cpp index caf1285e0b8..75995bf429e 100644 --- a/barretenberg/cpp/src/barretenberg/ultra_honk/protogalaxy.test.cpp +++ b/barretenberg/cpp/src/barretenberg/ultra_honk/protogalaxy.test.cpp @@ -38,50 +38,9 @@ barretenberg::Polynomial get_random_polynomial(size_t size) ProverPolynomials construct_ultra_full_polynomials(auto& input_polynomials) { ProverPolynomials full_polynomials; - full_polynomials.q_c = input_polynomials[0]; - full_polynomials.q_l = input_polynomials[1]; - full_polynomials.q_r = input_polynomials[2]; - full_polynomials.q_o = input_polynomials[3]; - full_polynomials.q_4 = input_polynomials[4]; - full_polynomials.q_m = input_polynomials[5]; - full_polynomials.q_arith = input_polynomials[6]; - full_polynomials.q_sort = input_polynomials[7]; - full_polynomials.q_elliptic = input_polynomials[8]; - full_polynomials.q_aux = input_polynomials[9]; - full_polynomials.q_lookup = input_polynomials[10]; - full_polynomials.sigma_1 = input_polynomials[11]; - full_polynomials.sigma_2 = input_polynomials[12]; - full_polynomials.sigma_3 = input_polynomials[13]; - full_polynomials.sigma_4 = input_polynomials[14]; - full_polynomials.id_1 = input_polynomials[15]; - full_polynomials.id_2 = input_polynomials[16]; - full_polynomials.id_3 = input_polynomials[17]; - full_polynomials.id_4 = input_polynomials[18]; - full_polynomials.table_1 = input_polynomials[19]; - full_polynomials.table_2 = input_polynomials[20]; - full_polynomials.table_3 = input_polynomials[21]; - full_polynomials.table_4 = input_polynomials[22]; - full_polynomials.lagrange_first = input_polynomials[23]; - full_polynomials.lagrange_last = input_polynomials[24]; - full_polynomials.w_l = input_polynomials[25]; - full_polynomials.w_r = input_polynomials[26]; - full_polynomials.w_o = input_polynomials[27]; - full_polynomials.w_4 = input_polynomials[28]; - full_polynomials.sorted_accum = input_polynomials[29]; - full_polynomials.z_perm = input_polynomials[30]; - full_polynomials.z_lookup = input_polynomials[31]; - full_polynomials.table_1_shift = input_polynomials[32]; - full_polynomials.table_2_shift = input_polynomials[33]; - full_polynomials.table_3_shift = input_polynomials[34]; - full_polynomials.table_4_shift = input_polynomials[35]; - full_polynomials.w_l_shift = input_polynomials[36]; - full_polynomials.w_r_shift = input_polynomials[37]; - full_polynomials.w_o_shift = input_polynomials[38]; - full_polynomials.w_4_shift = input_polynomials[39]; - full_polynomials.sorted_accum_shift = input_polynomials[40]; - full_polynomials.z_perm_shift = input_polynomials[41]; - full_polynomials.z_lookup_shift = input_polynomials[42]; - + for (auto [prover_poly, input_poly] : zip_view(full_polynomials.get_all(), input_polynomials)) { + prover_poly = input_poly.share(); + } return full_polynomials; } @@ -184,7 +143,7 @@ TEST_F(ProtoGalaxyTests, PerturbatorPolynomial) } auto accumulator = std::make_shared(); - accumulator->prover_polynomials = full_polynomials; + accumulator->prover_polynomials = std::move(full_polynomials); accumulator->folding_parameters = { betas, target_sum }; accumulator->relation_parameters = relation_parameters; accumulator->alpha = alpha; @@ -309,7 +268,7 @@ TEST_F(ProtoGalaxyTests, ComputeNewAccumulator) accumulator->witness_commitments = construct_witness_commitments(); accumulator->instance_size = instance_size; accumulator->log_instance_size = log_instance_size; - accumulator->prover_polynomials = full_polynomials; + accumulator->prover_polynomials = std::move(full_polynomials); accumulator->folding_parameters = { betas, target_sum }; accumulator->relation_parameters = relation_parameters; accumulator->alpha = alpha; diff --git a/barretenberg/cpp/src/barretenberg/ultra_honk/relation_correctness.test.cpp b/barretenberg/cpp/src/barretenberg/ultra_honk/relation_correctness.test.cpp index 6dfcbe4c4eb..5cc43dad5fc 100644 --- a/barretenberg/cpp/src/barretenberg/ultra_honk/relation_correctness.test.cpp +++ b/barretenberg/cpp/src/barretenberg/ultra_honk/relation_correctness.test.cpp @@ -31,7 +31,7 @@ void ensure_non_zero(auto& polynomial) * @tparam relation_idx Index into a tuple of provided relations * @tparam Flavor */ -template void check_relation(auto circuit_size, auto polynomials, auto params) +template void check_relation(auto circuit_size, auto& polynomials, auto params) { using AllValues = typename Flavor::AllValues; for (size_t i = 0; i < circuit_size; i++) { @@ -67,7 +67,7 @@ template void check_relation(auto circuit_s * @tparam Flavor */ template -void check_linearly_dependent_relation(auto circuit_size, auto polynomials, auto params) +void check_linearly_dependent_relation(auto circuit_size, auto& polynomials, auto params) { using AllValues = typename Flavor::AllValues; // Define the appropriate SumcheckArrayOfValuesOverSubrelations type for this relation and initialize to zero @@ -292,7 +292,7 @@ TEST_F(RelationCorrectnessTests, UltraRelationCorrectness) // Construct the round for applying sumcheck relations and results for storing computed results using Relations = typename Flavor::Relations; - auto prover_polynomials = instance->prover_polynomials; + auto& prover_polynomials = instance->prover_polynomials; auto params = instance->relation_parameters; // Check that each relation is satisfied across each row of the prover polynomials check_relation>(circuit_size, prover_polynomials, params); @@ -352,7 +352,7 @@ TEST_F(RelationCorrectnessTests, GoblinUltraRelationCorrectness) // Construct the round for applying sumcheck relations and results for storing computed results using Relations = typename Flavor::Relations; - auto prover_polynomials = instance->prover_polynomials; + auto& prover_polynomials = instance->prover_polynomials; auto params = instance->relation_parameters; // Check that each relation is satisfied across each row of the prover polynomials @@ -390,18 +390,8 @@ TEST_F(RelationCorrectnessTests, GoblinTranslatorPermutationRelationCorrectness) // Create storage for polynomials ProverPolynomials prover_polynomials; - std::vector polynomial_container; - auto polynomial_get_all = prover_polynomials.get_all(); - size_t z_perm_index = 0; - for (size_t i = 0; i < polynomial_get_all.size(); i++) { - // TODO(https://github.com/AztecProtocol/barretenberg/issues/743) wouldn't be needed if ProverPolynomials held - // memory - if (&prover_polynomials.z_perm == &polynomial_get_all[i]) { - z_perm_index = i; - } - Polynomial temporary_polynomial(circuit_size); - polynomial_container.push_back(temporary_polynomial); - polynomial_get_all[i] = polynomial_container[i]; + for (Polynomial& prover_poly : prover_polynomials.get_all()) { + prover_poly = Polynomial{ circuit_size }; } // Fill in lagrange polynomials used in the permutation relation @@ -491,7 +481,7 @@ TEST_F(RelationCorrectnessTests, GoblinTranslatorPermutationRelationCorrectness) // Compute the grand product polynomial grand_product_library::compute_grand_product>( circuit_size, prover_polynomials, params); - prover_polynomials.z_perm_shift = polynomial_container[z_perm_index].shifted(); + prover_polynomials.z_perm_shift = prover_polynomials.z_perm.shifted(); using Relations = typename Flavor::Relations; @@ -515,20 +505,9 @@ TEST_F(RelationCorrectnessTests, GoblinTranslatorGenPermSortRelationCorrectness) proof_system::RelationParameters params; ProverPolynomials prover_polynomials; - std::vector polynomial_container; - - auto polynomial_get_all = prover_polynomials.get_all(); - size_t ordered_range_constraints_index = 0; // Allocate polynomials - for (size_t i = 0; i < polynomial_get_all.size(); i++) { - Polynomial temporary_polynomial(circuit_size); - // TODO(https://github.com/AztecProtocol/barretenberg/issues/743) wouldn't be needed if ProverPolynomials held - // memory - if (&prover_polynomials.ordered_range_constraints_0 == &polynomial_get_all[i]) { - ordered_range_constraints_index = i; - } - polynomial_container.push_back(temporary_polynomial); - polynomial_get_all[i] = polynomial_container[i]; + for (Polynomial& polynomial : prover_polynomials.get_all()) { + polynomial = Polynomial{ circuit_size }; } // Construct lagrange polynomials that are needed for Goblin Translator's GenPermSort Relation @@ -573,16 +552,11 @@ TEST_F(RelationCorrectnessTests, GoblinTranslatorGenPermSortRelationCorrectness) }); // Get shifted polynomials - prover_polynomials.ordered_range_constraints_0_shift = - polynomial_container[ordered_range_constraints_index].shifted(); - prover_polynomials.ordered_range_constraints_1_shift = - polynomial_container[ordered_range_constraints_index + 1].shifted(); - prover_polynomials.ordered_range_constraints_2_shift = - polynomial_container[ordered_range_constraints_index + 2].shifted(); - prover_polynomials.ordered_range_constraints_3_shift = - polynomial_container[ordered_range_constraints_index + 3].shifted(); - prover_polynomials.ordered_range_constraints_4_shift = - polynomial_container[ordered_range_constraints_index + 4].shifted(); + prover_polynomials.ordered_range_constraints_0_shift = prover_polynomials.ordered_range_constraints_0.shifted(); + prover_polynomials.ordered_range_constraints_1_shift = prover_polynomials.ordered_range_constraints_1.shifted(); + prover_polynomials.ordered_range_constraints_2_shift = prover_polynomials.ordered_range_constraints_2.shifted(); + prover_polynomials.ordered_range_constraints_3_shift = prover_polynomials.ordered_range_constraints_3.shifted(); + prover_polynomials.ordered_range_constraints_4_shift = prover_polynomials.ordered_range_constraints_4.shifted(); using Relations = typename Flavor::Relations; @@ -635,15 +609,15 @@ TEST_F(RelationCorrectnessTests, GoblinTranslatorExtraRelationsCorrectness) for (auto& id : shifted_ids) { shifted_id_set.emplace(id); } - // Assign spans to non-shifted prover polynomials + // Assign to non-shifted prover polynomials auto polynomial_get_all = prover_polynomials.get_all(); for (size_t i = 0; i < polynomial_get_all.size(); i++) { if (!shifted_id_set.contains(i)) { - polynomial_get_all[i] = polynomial_container[i]; + polynomial_get_all[i] = polynomial_container[i].share(); } } - // Assign shifted spans to shifted prover polynomials using ids + // Assign to shifted prover polynomials using ids for (size_t i = 0; i < shifted_ids.size(); i++) { auto shifted_id = shifted_ids[i]; auto to_be_shifted_id = prover_polynomial_ids.get_to_be_shifted()[i]; @@ -737,7 +711,7 @@ TEST_F(RelationCorrectnessTests, GoblinTranslatorDecompositionRelationCorrectnes // Assign spans to non-shifted prover polynomials for (size_t i = 0; i < polynomial_get_all.size(); i++) { if (!shifted_id_set.contains(i)) { - polynomial_get_all[i] = polynomial_container[i]; + polynomial_get_all[i] = polynomial_container[i].share(); } } @@ -1153,14 +1127,14 @@ TEST_F(RelationCorrectnessTests, GoblinTranslatorNonNativeRelationCorrectness) for (auto& id : shifted_ids) { shifted_id_set.emplace(id); } - // Assign spans to non-shifted prover polynomials + // Assign to non-shifted prover polynomials for (size_t i = 0; i < polynomial_get_all.size(); i++) { if (!shifted_id_set.contains(i)) { - polynomial_get_all[i] = polynomial_container[i]; + polynomial_get_all[i] = polynomial_container[i].share(); } } - // Assign shifted spans to shifted prover polynomials using ids + // Assign to shifted prover polynomials using ids for (size_t i = 0; i < shifted_ids.size(); i++) { auto shifted_id = shifted_ids[i]; auto to_be_shifted_id = prover_polynomial_ids.get_to_be_shifted()[i]; diff --git a/barretenberg/cpp/src/barretenberg/vm/generated/AvmMini_composer.cpp b/barretenberg/cpp/src/barretenberg/vm/generated/AvmMini_composer.cpp index 6b6bb8f5550..3493608cf75 100644 --- a/barretenberg/cpp/src/barretenberg/vm/generated/AvmMini_composer.cpp +++ b/barretenberg/cpp/src/barretenberg/vm/generated/AvmMini_composer.cpp @@ -17,28 +17,10 @@ void AvmMiniComposer::compute_witness(CircuitConstructor& circuit) auto polynomials = circuit.compute_polynomials(); - proving_key->avmMini_clk = polynomials.avmMini_clk; - proving_key->avmMini_first = polynomials.avmMini_first; - proving_key->memTrace_m_clk = polynomials.memTrace_m_clk; - proving_key->memTrace_m_sub_clk = polynomials.memTrace_m_sub_clk; - proving_key->memTrace_m_addr = polynomials.memTrace_m_addr; - proving_key->memTrace_m_val = polynomials.memTrace_m_val; - proving_key->memTrace_m_lastAccess = polynomials.memTrace_m_lastAccess; - proving_key->memTrace_m_rw = polynomials.memTrace_m_rw; - proving_key->avmMini_subop = polynomials.avmMini_subop; - proving_key->avmMini_ia = polynomials.avmMini_ia; - proving_key->avmMini_ib = polynomials.avmMini_ib; - proving_key->avmMini_ic = polynomials.avmMini_ic; - proving_key->avmMini_mem_op_a = polynomials.avmMini_mem_op_a; - proving_key->avmMini_mem_op_b = polynomials.avmMini_mem_op_b; - proving_key->avmMini_mem_op_c = polynomials.avmMini_mem_op_c; - proving_key->avmMini_rwa = polynomials.avmMini_rwa; - proving_key->avmMini_rwb = polynomials.avmMini_rwb; - proving_key->avmMini_rwc = polynomials.avmMini_rwc; - proving_key->avmMini_mem_idx_a = polynomials.avmMini_mem_idx_a; - proving_key->avmMini_mem_idx_b = polynomials.avmMini_mem_idx_b; - proving_key->avmMini_mem_idx_c = polynomials.avmMini_mem_idx_c; - proving_key->avmMini_last = polynomials.avmMini_last; + for (auto [key_poly, prover_poly] : zip_view(proving_key->get_all(), polynomials.get_unshifted())) { + ASSERT(flavor_get_label(*proving_key, key_poly) == flavor_get_label(polynomials, prover_poly)); + key_poly = prover_poly; + } computed_witness = true; } diff --git a/barretenberg/cpp/src/barretenberg/vm/generated/AvmMini_prover.cpp b/barretenberg/cpp/src/barretenberg/vm/generated/AvmMini_prover.cpp index b7daa36ddb3..d13c8d92c1a 100644 --- a/barretenberg/cpp/src/barretenberg/vm/generated/AvmMini_prover.cpp +++ b/barretenberg/cpp/src/barretenberg/vm/generated/AvmMini_prover.cpp @@ -29,43 +29,16 @@ AvmMiniProver::AvmMiniProver(std::shared_ptr input_key, : key(input_key) , commitment_key(commitment_key) { - // TODO: take every polynomial and assign it to the key!! - - prover_polynomials.avmMini_clk = key->avmMini_clk; - prover_polynomials.avmMini_first = key->avmMini_first; - prover_polynomials.memTrace_m_clk = key->memTrace_m_clk; - prover_polynomials.memTrace_m_sub_clk = key->memTrace_m_sub_clk; - prover_polynomials.memTrace_m_addr = key->memTrace_m_addr; - prover_polynomials.memTrace_m_val = key->memTrace_m_val; - prover_polynomials.memTrace_m_lastAccess = key->memTrace_m_lastAccess; - prover_polynomials.memTrace_m_rw = key->memTrace_m_rw; - prover_polynomials.avmMini_subop = key->avmMini_subop; - prover_polynomials.avmMini_ia = key->avmMini_ia; - prover_polynomials.avmMini_ib = key->avmMini_ib; - prover_polynomials.avmMini_ic = key->avmMini_ic; - prover_polynomials.avmMini_mem_op_a = key->avmMini_mem_op_a; - prover_polynomials.avmMini_mem_op_b = key->avmMini_mem_op_b; - prover_polynomials.avmMini_mem_op_c = key->avmMini_mem_op_c; - prover_polynomials.avmMini_rwa = key->avmMini_rwa; - prover_polynomials.avmMini_rwb = key->avmMini_rwb; - prover_polynomials.avmMini_rwc = key->avmMini_rwc; - prover_polynomials.avmMini_mem_idx_a = key->avmMini_mem_idx_a; - prover_polynomials.avmMini_mem_idx_b = key->avmMini_mem_idx_b; - prover_polynomials.avmMini_mem_idx_c = key->avmMini_mem_idx_c; - prover_polynomials.avmMini_last = key->avmMini_last; - - prover_polynomials.memTrace_m_addr = key->memTrace_m_addr; - prover_polynomials.memTrace_m_addr_shift = key->memTrace_m_addr.shifted(); - - prover_polynomials.memTrace_m_rw = key->memTrace_m_rw; - prover_polynomials.memTrace_m_rw_shift = key->memTrace_m_rw.shifted(); - - prover_polynomials.memTrace_m_val = key->memTrace_m_val; - prover_polynomials.memTrace_m_val_shift = key->memTrace_m_val.shifted(); - - // prover_polynomials.lookup_inverses = key->lookup_inverses; - // key->z_perm = Polynomial(key->circuit_size); - // prover_polynomials.z_perm = key->z_perm; + for (auto [prover_poly, key_poly] : zip_view(prover_polynomials.get_unshifted(), key->get_all())) { + ASSERT(proof_system::flavor_get_label(prover_polynomials, prover_poly) == + proof_system::flavor_get_label(*key, key_poly)); + prover_poly = key_poly.share(); + } + for (auto [prover_poly, key_poly] : zip_view(prover_polynomials.get_shifted(), key->get_to_be_shifted())) { + ASSERT(proof_system::flavor_get_label(prover_polynomials, prover_poly) == + proof_system::flavor_get_label(*key, key_poly) + "_shift"); + prover_poly = key_poly.shifted(); + } } /**