Skip to content

Commit

Permalink
refactor(Protogalaxy): Isolate some state and clarify skipped zero co…
Browse files Browse the repository at this point in the history
…mputation (#8173)

Some steps toward clarifying state during Protogalaxy proof
construction:
 - Move accumulators into the class that contains state.
- Reduce size of Prover header. Move internal functions into a purely
static class. This accounts for most of the diff.
- Clarify the known-zero-value while removing loose coupling of template
parameters.

The next step will be to reduce the amount of state in ProverInstances.
  • Loading branch information
codygunton authored Aug 26, 2024
1 parent bfbc4b2 commit 7395b95
Show file tree
Hide file tree
Showing 9 changed files with 559 additions and 587 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "barretenberg/eccvm/eccvm_flavor.hpp"
#include "barretenberg/protogalaxy/protogalaxy_prover.hpp"
#include "barretenberg/protogalaxy/protogalaxy_prover_internal.hpp" // just for an alias; should perhaps move to prover
#include "barretenberg/stdlib_circuit_builders/mega_flavor.hpp"
#include "barretenberg/stdlib_circuit_builders/ultra_flavor.hpp"
#include "barretenberg/sumcheck/instance/instances.hpp"
#include "barretenberg/translator_vm/translator_flavor.hpp"
#include <benchmark/benchmark.h>

Expand Down Expand Up @@ -53,8 +54,7 @@ template <typename Flavor, typename Relation> void execute_relation_for_univaria
template <typename Flavor, typename Relation> void execute_relation_for_pg_univariates(::benchmark::State& state)
{
using ProverInstances = ProverInstances_<Flavor>;
using ProtoGalaxyProver = ProtoGalaxyProver_<ProverInstances>;
using Input = ProtoGalaxyProver::ExtendedUnivariates;
using Input = ProtogalaxyProverInternal<ProverInstances>::ExtendedUnivariates;
using Accumulator = typename Relation::template ProtogalaxyTupleOfUnivariatesOverSubrelations<ProverInstances::NUM>;

execute_relation<Flavor, Relation, Input, Accumulator>(state);
Expand Down
36 changes: 21 additions & 15 deletions barretenberg/cpp/src/barretenberg/protogalaxy/combiner.test.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "barretenberg/honk/utils/testing.hpp"
#include "barretenberg/polynomials/pow.hpp"
#include "barretenberg/protogalaxy/protogalaxy_prover.hpp"
#include "barretenberg/relations/relation_parameters.hpp"
#include "barretenberg/protogalaxy/protogalaxy_prover_internal.hpp"
#include "barretenberg/relations/ultra_arithmetic_relation.hpp"
#include "barretenberg/stdlib_circuit_builders/ultra_flavor.hpp"
#include "barretenberg/sumcheck/instance/instances.hpp"
Expand All @@ -21,6 +20,7 @@ TEST(Protogalaxy, CombinerOn2Instances)
using ProverInstance = ProverInstance_<Flavor>;
using ProverInstances = ProverInstances_<Flavor, NUM_INSTANCES>;
using ProtoGalaxyProver = ProtoGalaxyProver_<ProverInstances>;
using Fun = ProtogalaxyProverInternal<ProverInstances>;

const auto restrict_to_standard_arithmetic_relation = [](auto& polys) {
std::fill(polys.q_arith.begin(), polys.q_arith.end(), 1);
Expand Down Expand Up @@ -56,7 +56,7 @@ TEST(Protogalaxy, CombinerOn2Instances)
ProverInstances instances{ instance_data };
instances.alphas.fill(bb::Univariate<FF, 12>(FF(0))); // focus on the arithmetic relation only
auto pow_polynomial = PowPolynomial(std::vector<FF>{ 2 });
auto result = prover.compute_combiner</*OptimisationEnabled=*/false>(instances, pow_polynomial);
auto result = Fun::compute_combiner(instances, pow_polynomial, prover.state.univariate_accumulators);
// The expected_result values are computed by running the python script combiner_example_gen.py
auto expected_result = Univariate<FF, 12>(std::array<FF, 12>{ 9704UL,
13245288UL,
Expand Down Expand Up @@ -134,8 +134,9 @@ TEST(Protogalaxy, CombinerOn2Instances)
0 0 0 0 0 0 0 0 0 6 18 36 60 90 */

auto pow_polynomial = PowPolynomial(std::vector<FF>{ 2 });
auto result = prover.compute_combiner</*OptimisationEnabled=*/false>(instances, pow_polynomial);
auto optimised_result = prover.compute_combiner(instances, pow_polynomial);
auto result = Fun::compute_combiner(instances, pow_polynomial, prover.state.univariate_accumulators);
auto optimised_result =
Fun::compute_combiner(instances, pow_polynomial, prover.state.optimised_univariate_accumulators);
auto expected_result =
Univariate<FF, 12>(std::array<FF, 12>{ 0, 0, 12, 36, 72, 120, 180, 252, 336, 432, 540, 660 });

Expand All @@ -154,6 +155,7 @@ TEST(Protogalaxy, CombinerOptimizationConsistency)
using ProverInstance = ProverInstance_<Flavor>;
using ProverInstances = ProverInstances_<Flavor, NUM_INSTANCES>;
using ProtoGalaxyProver = ProtoGalaxyProver_<ProverInstances>;
using Fun = ProtogalaxyProverInternal<ProverInstances>;
using UltraArithmeticRelation = UltraArithmeticRelation<FF>;

constexpr size_t UNIVARIATE_LENGTH = 12;
Expand Down Expand Up @@ -252,8 +254,9 @@ TEST(Protogalaxy, CombinerOptimizationConsistency)
precomputed_result[idx] = std::get<0>(accumulator)[0];
}
auto expected_result = Univariate<FF, UNIVARIATE_LENGTH>(precomputed_result);
auto result = prover.compute_combiner</*OptimisationEnabled=*/false>(instances, pow_polynomial);
auto optimised_result = prover.compute_combiner(instances, pow_polynomial);
auto result = Fun::compute_combiner(instances, pow_polynomial, prover.state.univariate_accumulators);
auto optimised_result =
Fun::compute_combiner(instances, pow_polynomial, prover.state.optimised_univariate_accumulators);

EXPECT_EQ(result, expected_result);
EXPECT_EQ(optimised_result, expected_result);
Expand Down Expand Up @@ -320,8 +323,9 @@ TEST(Protogalaxy, CombinerOptimizationConsistency)
0 0 0 0 0 0 0 0 0 6 18 36 60 90 */

auto pow_polynomial = PowPolynomial(std::vector<FF>{ 2 });
auto result = prover.compute_combiner</*OptimisationEnabled=*/false>(instances, pow_polynomial);
auto optimised_result = prover.compute_combiner(instances, pow_polynomial);
auto result = Fun::compute_combiner(instances, pow_polynomial, prover.state.univariate_accumulators);
auto optimised_result =
Fun::compute_combiner(instances, pow_polynomial, prover.state.optimised_univariate_accumulators);
auto expected_result =
Univariate<FF, 12>(std::array<FF, 12>{ 0, 0, 12, 36, 72, 120, 180, 252, 336, 432, 540, 660 });

Expand All @@ -333,15 +337,16 @@ TEST(Protogalaxy, CombinerOptimizationConsistency)
run_test(false);
};

// Tests a combiner on 4 instances, note currently we don't plan
// to fold with num instances > 2, this would require an additional explicit instantiation in
// protogalaxy_prover_ultra.cpp. Currently, we rather save the compile time.
// TEST(Protogalaxy, CombinerOn4Instances)
// // Tests a combiner on 4 instances, note currently we don't plan
// // to fold with num instances > 2, this would require an additional explicit instantiation in
// // protogalaxy_prover_ultra.cpp. Currently, we rather save the compile time.
// TEST(Protogalaxy, DISABLED_CombinerOn4Instances)
// {
// constexpr size_t NUM_INSTANCES = 4;
// using ProverInstance = ProverInstance_<Flavor>;
// using ProverInstances = ProverInstances_<Flavor, NUM_INSTANCES>;
// using ProtoGalaxyProver = ProtoGalaxyProver_<ProverInstances>;
// using Fun = ProtogalaxyProverInternal<ProverInstances>;

// const auto zero_all_selectors = [](auto& polys) {
// std::fill(polys.q_arith.begin(), polys.q_arith.end(), 0);
Expand Down Expand Up @@ -376,8 +381,9 @@ TEST(Protogalaxy, CombinerOptimizationConsistency)
// zero_all_selectors(instances[3]->proving_key.polynomials);

// auto pow_polynomial = PowPolynomial(std::vector<FF>{ 2 });
// auto result = prover.compute_combiner</*OptimisationEnabled=*/false>(instances, pow_polynomial);
// auto optimised_result = prover.compute_combiner(instances, pow_polynomial);
// auto result = Fun::compute_combiner(instances, pow_polynomial, prover.state.univariate_accumulators);
// auto optimised_result =
// Fun::compute_combiner(instances, pow_polynomial, prover.state.optimised_univariate_accumulators);
// std::array<FF, 40> zeroes;
// std::fill(zeroes.begin(), zeroes.end(), 0);
// auto expected_result = Univariate<FF, 40>(zeroes);
Expand Down
19 changes: 10 additions & 9 deletions barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy.test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "barretenberg/goblin/mock_circuits.hpp"
#include "barretenberg/polynomials/pow.hpp"
#include "barretenberg/protogalaxy/protogalaxy_prover.hpp"
#include "barretenberg/protogalaxy/protogalaxy_prover_internal.hpp"
#include "barretenberg/protogalaxy/protogalaxy_verifier.hpp"
#include "barretenberg/protogalaxy/prover_verifier_shared.hpp"
#include "barretenberg/stdlib_circuit_builders/mock_circuits.hpp"
Expand Down Expand Up @@ -37,6 +38,7 @@ template <typename Flavor> class ProtoGalaxyTests : public testing::Test {
using DeciderVerifier = DeciderVerifier_<Flavor>;
using FoldingProver = ProtoGalaxyProver_<ProverInstances>;
using FoldingVerifier = ProtoGalaxyVerifier_<VerifierInstances>;
using Fun = ProtogalaxyProverInternal<ProverInstances>;

using TupleOfInstances =
std::tuple<std::vector<std::shared_ptr<ProverInstance>>, std::vector<std::shared_ptr<VerifierInstance>>>;
Expand Down Expand Up @@ -93,7 +95,7 @@ template <typename Flavor> class ProtoGalaxyTests : public testing::Test {
static void check_accumulator_target_sum_manual(std::shared_ptr<ProverInstance>& accumulator, bool expected_result)
{
auto instance_size = accumulator->proving_key.circuit_size;
auto expected_honk_evals = ProtoGalaxyProver::compute_full_honk_evaluations(
auto expected_honk_evals = Fun::compute_full_honk_evaluations(
accumulator->proving_key.polynomials, accumulator->alphas, accumulator->relation_parameters);
// Construct pow(\vec{betas*}) as in the paper
auto expected_pows = PowPolynomial(accumulator->gate_challenges);
Expand Down Expand Up @@ -146,7 +148,7 @@ template <typename Flavor> class ProtoGalaxyTests : public testing::Test {
for (auto& alpha : instance->alphas) {
alpha = FF::random_element();
}
auto full_honk_evals = ProtoGalaxyProver::compute_full_honk_evaluations(
auto full_honk_evals = Fun::compute_full_honk_evaluations(
instance->proving_key.polynomials, instance->alphas, instance->relation_parameters);

// Evaluations should be 0 for valid circuit
Expand All @@ -165,7 +167,7 @@ template <typename Flavor> class ProtoGalaxyTests : public testing::Test {
std::vector<FF> betas = { FF(5), FF(8), FF(11) };
std::vector<FF> deltas = { FF(2), FF(4), FF(8) };
std::vector<FF> full_honk_evaluations = { FF(1), FF(1), FF(1), FF(1), FF(1), FF(1), FF(1), FF(1) };
auto perturbator = ProtoGalaxyProver::construct_perturbator_coefficients(betas, deltas, full_honk_evaluations);
auto perturbator = Fun::construct_perturbator_coefficients(betas, deltas, full_honk_evaluations);
std::vector<FF> expected_values = { FF(648), FF(936), FF(432), FF(64) };
EXPECT_EQ(perturbator.size(), 4); // log(instance_size) + 1
for (size_t i = 0; i < perturbator.size(); i++) {
Expand Down Expand Up @@ -195,8 +197,7 @@ template <typename Flavor> class ProtoGalaxyTests : public testing::Test {
alpha = FF::random_element();
}

auto full_honk_evals =
ProtoGalaxyProver::compute_full_honk_evaluations(full_polynomials, alphas, relation_parameters);
auto full_honk_evals = Fun::compute_full_honk_evaluations(full_polynomials, alphas, relation_parameters);
std::vector<FF> betas(log_instance_size);
for (size_t idx = 0; idx < log_instance_size; idx++) {
betas[idx] = FF::random_element();
Expand All @@ -220,7 +221,7 @@ template <typename Flavor> class ProtoGalaxyTests : public testing::Test {
accumulator->alphas = alphas;

auto deltas = compute_round_challenge_pows(log_instance_size, FF::random_element());
auto perturbator = ProtoGalaxyProver::compute_perturbator(accumulator, deltas);
auto perturbator = Fun::compute_perturbator(accumulator, deltas);

// Ensure the constant coefficient of the perturbator is equal to the target sum as indicated by the paper
EXPECT_EQ(perturbator[0], target_sum);
Expand All @@ -235,7 +236,7 @@ template <typename Flavor> class ProtoGalaxyTests : public testing::Test {
{
auto compressed_perturbator = FF(2); // F(\alpha) in the paper
auto combiner = bb::Univariate<FF, 12>(std::array<FF, 12>{ 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 });
auto combiner_quotient = ProtoGalaxyProver::compute_combiner_quotient(compressed_perturbator, combiner);
auto combiner_quotient = Fun::compute_combiner_quotient(compressed_perturbator, combiner);

// K(i) = (G(i) - ( L_0(i) * F(\alpha)) / Z(i), i = {2,.., 13} for ProverInstances::NUM = 2
// K(i) = (G(i) - (1 - i) * F(\alpha)) / i * (i - 1)
Expand Down Expand Up @@ -274,7 +275,7 @@ template <typename Flavor> class ProtoGalaxyTests : public testing::Test {
instance2->relation_parameters.eta = 3;

ProverInstances instances{ { instance1, instance2 } };
ProtoGalaxyProver::combine_relation_parameters(instances);
Fun::combine_relation_parameters(instances);

bb::Univariate<FF, 11> expected_eta{ { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21 } };
EXPECT_EQ(instances.relation_parameters.eta, expected_eta);
Expand All @@ -301,7 +302,7 @@ template <typename Flavor> class ProtoGalaxyTests : public testing::Test {
instance2->alphas.fill(4);

ProverInstances instances{ { instance1, instance2 } };
ProtoGalaxyProver::combine_alpha(instances);
Fun::combine_alpha(instances);

bb::Univariate<FF, 12> expected_alpha{ { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24 } };
for (const auto& alpha : instances.alphas) {
Expand Down
Loading

0 comments on commit 7395b95

Please sign in to comment.