Skip to content

Commit

Permalink
chore(bb): prereq work for polynomial mem optimization (#7949)
Browse files Browse the repository at this point in the history
This PR: Splitting the implementation of plonk and honk into different
polynomial classes.

Greater work: We will be moving to 'polynomials with more structure' ->
aka tracking a size and a virtual size* where 'size' is actually
allocated memory, and virtual size is conceptually all 0's. This should
save a good deal of memory.

* I would call virtual size one more than the degree of the polynomial,
but it's actually a bit weird to call it that when [size, virtual size)
indices are all 0 coefficients. It's more like one more than the maximum
degree of the polynomial, and only really used as an upper bound for
loops or debug bounds checks.
  • Loading branch information
ludamad authored Aug 14, 2024
1 parent eedbc11 commit 5ca5138
Show file tree
Hide file tree
Showing 78 changed files with 1,363 additions and 635 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "barretenberg/common/op_count.hpp"
#include "barretenberg/common/thread.hpp"
#include "barretenberg/ecc/curves/bn254/bn254.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/srs/global_crs.hpp"
#include <benchmark/benchmark.h>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "barretenberg/commitment_schemes/ipa/ipa.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
#include <benchmark/benchmark.h>

using namespace benchmark;
Expand All @@ -7,6 +8,7 @@ using namespace bb;
namespace {
using Curve = curve::Grumpkin;
using Fr = Curve::ScalarField;
using Polynomial = Polynomial<Fr>;

constexpr size_t MIN_POLYNOMIAL_DEGREE_LOG2 = 10;
constexpr size_t MAX_POLYNOMIAL_DEGREE_LOG2 = 16;
Expand All @@ -31,7 +33,7 @@ void ipa_open(State& state) noexcept
state.PauseTiming();
size_t n = 1 << static_cast<size_t>(state.range(0));
// Construct the polynomial
Polynomial<Fr> poly(n);
Polynomial poly(n);
for (size_t i = 0; i < n; ++i) {
poly[i] = Fr::random_element(&engine);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include "barretenberg/commitment_schemes/commitment_key.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/srs/factories/mem_bn254_crs_factory.hpp"
#include <benchmark/benchmark.h>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "barretenberg/common/op_count.hpp"
#include "barretenberg/ecc/scalar_multiplication/scalar_multiplication.hpp"
#include "barretenberg/numeric/bitop/pow.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/polynomials/polynomial_arithmetic.hpp"
#include "barretenberg/srs/factories/crs_factory.hpp"
#include "barretenberg/srs/factories/file_crs_factory.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "barretenberg/commitment_schemes/commitment_key.hpp"
#include "barretenberg/commitment_schemes/verification_key.hpp"
#include "barretenberg/ecc/curves/bn254/g1.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/srs/factories/file_crs_factory.hpp"
#include "claim.hpp"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace bb {
* @return std::vector<Polynomial>
*/
template <typename Curve>
std::vector<typename bb::Polynomial<typename Curve::ScalarField>> GeminiProver_<Curve>::compute_gemini_polynomials(
std::vector<typename GeminiProver_<Curve>::Polynomial> GeminiProver_<Curve>::compute_gemini_polynomials(
std::span<const Fr> mle_opening_point, Polynomial&& batched_unshifted, Polynomial&& batched_to_be_shifted)
{
const size_t num_variables = mle_opening_point.size(); // m
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ namespace bb {
*/
template <typename Curve> struct GeminiProverOutput {
std::vector<OpeningPair<Curve>> opening_pairs;
std::vector<bb::Polynomial<typename Curve::ScalarField>> witnesses;
std::vector<Polynomial<typename Curve::ScalarField>> witnesses;
};

namespace gemini {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "./mock_transcript.hpp"
#include "barretenberg/commitment_schemes/commitment_key.hpp"
#include "barretenberg/commitment_schemes/verification_key.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/srs/factories/file_crs_factory.hpp"

namespace bb {
Expand Down Expand Up @@ -36,6 +35,8 @@ class ProxyCaller {
};
} // namespace bb

using namespace bb;

/**
* @brief Initialize SRS, commitment key, verification key
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ template <typename Curve_> class IPA {
* 7. Compute \f$\vec{b}_{i-1}=\vec{b}_{i\_low}+u_{i-1}^{-1}\cdot \vec{b}_{i\_high}\f$​
*
*7. Send the final \f$\vec{a}_{0} = (a_0)\f$ to the verifier
*/
*/
template <typename Transcript>
static void compute_opening_proof_internal(const std::shared_ptr<CK>& ck,
const ProverOpeningClaim<Curve>& opening_claim,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "barretenberg/common/mem.hpp"
#include "barretenberg/ecc/curves/bn254/fq12.hpp"
#include "barretenberg/ecc/curves/types.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/polynomials/polynomial_arithmetic.hpp"
#include <gtest/gtest.h>
#include <utility>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "../claim.hpp"
#include "barretenberg/commitment_schemes/commitment_key.hpp"
#include "barretenberg/commitment_schemes/verification_key.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/transcript/transcript.hpp"

#include <memory>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include "../commitment_key.test.hpp"
#include "barretenberg/commitment_schemes/claim.hpp"
#include "barretenberg/polynomials/polynomial.hpp"

#include "barretenberg/ecc/curves/bn254/g1.hpp"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include "../commitment_key.test.hpp"
#include "barretenberg/commitment_schemes/claim.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
namespace bb {
template <class Params> class ShplonkTest : public CommitmentTest<Params> {};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp"
#include "barretenberg/ecc/scalar_multiplication/scalar_multiplication.hpp"
#include "barretenberg/numeric/bitop/pow.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/polynomials/polynomial_arithmetic.hpp"
#include "barretenberg/srs/global_crs.hpp"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ template <typename Curve> class ZeroMorphProver_ {
std::vector<FF> f_k;
f_k.resize(size_q);

std::vector<FF> g(polynomial.data().get(), polynomial.data().get() + size_q);
std::vector<FF> g(polynomial.data(), polynomial.data() + size_q);

// Compute q_k in reverse order from k= n-2, i.e. q_{n-2}, ..., q_0
for (size_t k = 1; k < log_N; ++k) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp"
#include "barretenberg/honk/proof_system/logderivative_library.hpp"
#include "barretenberg/honk/proof_system/permutation_library.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/relations/relation_parameters.hpp"
#include "barretenberg/stdlib_circuit_builders/op_queue/ecc_op_queue.hpp"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "barretenberg/eccvm/eccvm_verifier.hpp"
#include "barretenberg/numeric/uint256/uint256.hpp"
#include "barretenberg/plonk_honk_shared/library/grand_product_delta.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/relations/permutation_relation.hpp"
#include "barretenberg/relations/relation_parameters.hpp"
#include "barretenberg/sumcheck/sumcheck_round.hpp"
Expand Down
1 change: 1 addition & 0 deletions barretenberg/cpp/src/barretenberg/eccvm/eccvm_flavor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "barretenberg/flavor/flavor.hpp"
#include "barretenberg/flavor/flavor_macros.hpp"
#include "barretenberg/flavor/relation_definitions.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/polynomials/univariate.hpp"
#include "barretenberg/relations/ecc_vm/ecc_bools_relation.hpp"
#include "barretenberg/relations/ecc_vm/ecc_lookup_relation.hpp"
Expand Down
1 change: 0 additions & 1 deletion barretenberg/cpp/src/barretenberg/eccvm/eccvm_prover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "barretenberg/honk/proof_system/logderivative_library.hpp"
#include "barretenberg/honk/proof_system/permutation_library.hpp"
#include "barretenberg/plonk_honk_shared/library/grand_product_library.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/relations/permutation_relation.hpp"
#include "barretenberg/sumcheck/sumcheck.hpp"

Expand Down
18 changes: 9 additions & 9 deletions barretenberg/cpp/src/barretenberg/flavor/flavor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,20 +411,20 @@ concept IsHonkFlavor = IsAnyOf<T, UltraFlavor, UltraKeccakFlavor, UltraFlavorWit
template <typename T>
concept IsUltraFlavor = IsAnyOf<T, UltraFlavor, UltraKeccakFlavor, UltraFlavorWithZK, MegaFlavor>;

template <typename T>
template <typename T>
concept IsGoblinFlavor = IsAnyOf<T, MegaFlavor,
MegaRecursiveFlavor_<UltraCircuitBuilder>,
MegaRecursiveFlavor_<MegaCircuitBuilder>, MegaRecursiveFlavor_<CircuitSimulatorBN254>>;

template <typename T>
concept IsRecursiveFlavor = IsAnyOf<T, UltraRecursiveFlavor_<UltraCircuitBuilder>,
UltraRecursiveFlavor_<MegaCircuitBuilder>,
template <typename T>
concept IsRecursiveFlavor = IsAnyOf<T, UltraRecursiveFlavor_<UltraCircuitBuilder>,
UltraRecursiveFlavor_<MegaCircuitBuilder>,
UltraRecursiveFlavor_<CircuitSimulatorBN254>,
MegaRecursiveFlavor_<UltraCircuitBuilder>,
MegaRecursiveFlavor_<MegaCircuitBuilder>,
MegaRecursiveFlavor_<CircuitSimulatorBN254>,
TranslatorRecursiveFlavor_<UltraCircuitBuilder>,
TranslatorRecursiveFlavor_<MegaCircuitBuilder>,
MegaRecursiveFlavor_<CircuitSimulatorBN254>,
TranslatorRecursiveFlavor_<UltraCircuitBuilder>,
TranslatorRecursiveFlavor_<MegaCircuitBuilder>,
TranslatorRecursiveFlavor_<CircuitSimulatorBN254>,
ECCVMRecursiveFlavor_<UltraCircuitBuilder>>;

Expand All @@ -433,15 +433,15 @@ template <typename T> concept IsECCVMRecursiveFlavor = IsAnyOf<T, ECCVMRecursive

template <typename T> concept IsGrumpkinFlavor = IsAnyOf<T, ECCVMFlavor>;

template <typename T> concept IsFoldingFlavor = IsAnyOf<T, UltraFlavor,
template <typename T> concept IsFoldingFlavor = IsAnyOf<T, UltraFlavor,
// Note(md): must be here to use oink prover
UltraKeccakFlavor,
UltraFlavorWithZK,
MegaFlavor,
UltraRecursiveFlavor_<UltraCircuitBuilder>,
UltraRecursiveFlavor_<MegaCircuitBuilder>,
UltraRecursiveFlavor_<CircuitSimulatorBN254>,
MegaRecursiveFlavor_<UltraCircuitBuilder>,
MegaRecursiveFlavor_<UltraCircuitBuilder>,
MegaRecursiveFlavor_<MegaCircuitBuilder>, MegaRecursiveFlavor_<CircuitSimulatorBN254>>;

template <typename Container, typename Element>
Expand Down
3 changes: 1 addition & 2 deletions barretenberg/cpp/src/barretenberg/flavor/flavor.test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/srs/factories/crs_factory.hpp"
#include "barretenberg/stdlib_circuit_builders/ultra_flavor.hpp"
#include <cstddef>
Expand Down Expand Up @@ -78,7 +77,7 @@ TEST(Flavor, GetRow)
});
Flavor::ProverPolynomials prover_polynomials;
for (auto [poly, entry] : zip_view(prover_polynomials.get_all(), data)) {
poly = entry;
poly = Flavor::Polynomial(entry);
}
auto row0 = prover_polynomials.get_row(0);
auto row1 = prover_polynomials.get_row(1);
Expand Down
4 changes: 2 additions & 2 deletions barretenberg/cpp/src/barretenberg/flavor/plonk_flavors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Standard {
using ProvingKey = plonk::proving_key;
using Curve = curve::BN254;
using FF = Curve::ScalarField;
using Polynomial = bb::Polynomial<FF>;
using Polynomial = bb::LegacyPolynomial<FF>;
static constexpr size_t NUM_WIRES = CircuitBuilder::NUM_WIRES;
// Whether or not the first row of the execution trace is reserved for 0s to enable shifts
static constexpr bool has_zero_row = false;
Expand All @@ -24,7 +24,7 @@ class Ultra {
using ProvingKey = plonk::proving_key;
using Curve = curve::BN254;
using FF = Curve::ScalarField;
using Polynomial = bb::Polynomial<FF>;
using Polynomial = bb::LegacyPolynomial<FF>;
static constexpr size_t NUM_WIRES = CircuitBuilder::NUM_WIRES;
// Whether or not the first row of the execution trace is reserved for 0s to enable shifts
static constexpr bool has_zero_row = false;
Expand Down
1 change: 1 addition & 0 deletions barretenberg/cpp/src/barretenberg/goblin/goblin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "barretenberg/goblin/mock_circuits.hpp"
#include "barretenberg/goblin/types.hpp"
#include "barretenberg/plonk_honk_shared/instance_inspector.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/stdlib/honk_recursion/verifier/merge_recursive_verifier.hpp"
#include "barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp"
#include "barretenberg/stdlib_circuit_builders/mega_flavor.hpp"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once
#include "barretenberg/common/ref_vector.hpp"
#include "barretenberg/common/zip_view.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/relations/relation_parameters.hpp"
#include <typeinfo>

Expand Down
1 change: 0 additions & 1 deletion barretenberg/cpp/src/barretenberg/honk/utils/testing.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once
#include "barretenberg/common/zip_view.hpp"
#include "barretenberg/polynomials/polynomial.hpp"

namespace bb {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "barretenberg/plonk/proof_system/widgets/transition_widgets/arithmetic_widget.hpp"
#include "barretenberg/plonk_honk_shared/composer/composer_lib.hpp"
#include "barretenberg/plonk_honk_shared/composer/permutation_lib.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/polynomials/legacy_polynomial.hpp"

#include <cstddef>
#include <cstdint>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include "../../../polynomials/polynomial.hpp"
#include "../../../polynomials/legacy_polynomial.hpp"
#include "../../../polynomials/polynomial_arithmetic.hpp"
#include "../types/commitment_open_proof.hpp"
#include "../types/program_settings.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "../types/program_settings.hpp"
#include "barretenberg/common/mem.hpp"
#include "barretenberg/plonk/work_queue/work_queue.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/polynomials/legacy_polynomial.hpp"
#include "barretenberg/polynomials/polynomial_arithmetic.hpp"
#include "barretenberg/srs/factories/file_crs_factory.hpp"
#include <gtest/gtest.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "barretenberg/ecc/scalar_multiplication/scalar_multiplication.hpp"
#include "barretenberg/plonk/proof_system/types/prover_settings.hpp"
#include "barretenberg/polynomials/iterate_over_domain.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/polynomials/legacy_polynomial.hpp"
#include "barretenberg/polynomials/polynomial_arithmetic.hpp"
#include <chrono>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "barretenberg/plonk/proof_system/types/polynomial_manifest.hpp"
#include "barretenberg/plonk_honk_shared/types/aggregation_object_type.hpp"
#include "barretenberg/polynomials/evaluation_domain.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/polynomials/legacy_polynomial.hpp"
#include "barretenberg/srs/factories/crs_factory.hpp"

#ifdef __wasm__
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
#include "barretenberg/numeric/bitop/get_msb.hpp"
#include "barretenberg/polynomials/iterate_over_domain.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/polynomials/legacy_polynomial.hpp"

namespace bb::plonk {
template <typename program_settings>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "barretenberg/common/throw_or_abort.hpp"
#include "barretenberg/numeric/bitop/get_msb.hpp"
#include "barretenberg/polynomials/iterate_over_domain.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/polynomials/legacy_polynomial.hpp"

namespace bb::plonk {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "barretenberg/plonk/proof_system/proving_key/proving_key.hpp"
#include "barretenberg/plonk/proof_system/public_inputs/public_inputs.hpp"
#include "barretenberg/polynomials/iterate_over_domain.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/polynomials/legacy_polynomial.hpp"
#include "barretenberg/polynomials/polynomial_arithmetic.hpp"
#include "barretenberg/transcript/transcript.hpp"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "work_queue.hpp"
#include "barretenberg/ecc/scalar_multiplication/scalar_multiplication.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/polynomials/legacy_polynomial.hpp"
#include "barretenberg/polynomials/polynomial_arithmetic.hpp"

namespace bb::plonk {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace bb {

template <typename Flavor>
void construct_lookup_table_polynomials(RefArray<typename Flavor::Polynomial, 4> table_polynomials,
void construct_lookup_table_polynomials(const RefArray<typename Flavor::Polynomial, 4>& table_polynomials,
const typename Flavor::CircuitBuilder& circuit,
size_t dyadic_circuit_size,
size_t additional_offset = 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "barretenberg/flavor/flavor.hpp"
#include "barretenberg/plonk/proof_system/proving_key/proving_key.hpp"
#include "barretenberg/polynomials/iterate_over_domain.hpp"
#include "barretenberg/polynomials/polynomial.hpp"

#include <algorithm>
#include <cstddef>
Expand Down Expand Up @@ -347,10 +346,11 @@ void compute_monomial_and_coset_fft_polynomials_from_lagrange(std::string label,
* @brief Compute Lagrange Polynomials L_0 and L_{n-1} and put them in the polynomial cache
*/
template <typename FF>
inline std::tuple<Polynomial<FF>, Polynomial<FF>> compute_first_and_last_lagrange_polynomials(const size_t circuit_size)
inline std::tuple<LegacyPolynomial<FF>, LegacyPolynomial<FF>> compute_first_and_last_lagrange_polynomials(
const size_t circuit_size)
{
Polynomial<FF> lagrange_polynomial_0(circuit_size);
Polynomial<FF> lagrange_polynomial_n_min_1(circuit_size);
LegacyPolynomial<FF> lagrange_polynomial_0(circuit_size);
LegacyPolynomial<FF> lagrange_polynomial_n_min_1(circuit_size);
lagrange_polynomial_0[0] = 1;

lagrange_polynomial_n_min_1[circuit_size - 1] = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#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"
#include <typeinfo>

Expand Down
Loading

0 comments on commit 5ca5138

Please sign in to comment.