Skip to content

Commit

Permalink
Added test that doesn't care about python
Browse files Browse the repository at this point in the history
  • Loading branch information
Rumata888 committed Jun 19, 2024
1 parent 52fcba7 commit bcf7207
Showing 1 changed file with 70 additions and 13 deletions.
83 changes: 70 additions & 13 deletions barretenberg/cpp/src/barretenberg/protogalaxy/combiner.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "barretenberg/polynomials/pow.hpp"
#include "barretenberg/protogalaxy/protogalaxy_prover.hpp"
#include "barretenberg/relations/relation_parameters.hpp"
#include "barretenberg/relations/ultra_arithmetic_relation.hpp"
#include "barretenberg/stdlib_circuit_builders/ultra_flavor.hpp"
#include "barretenberg/sumcheck/instance/instances.hpp"
#include <gtest/gtest.h>
Expand All @@ -20,7 +21,9 @@ TEST(Protogalaxy, CombinerOn2Instances)
using ProverInstance = ProverInstance_<Flavor>;
using ProverInstances = ProverInstances_<Flavor, NUM_INSTANCES>;
using ProtoGalaxyProver = ProtoGalaxyProver_<ProverInstances>;
using UltraArithmeticRelation = UltraArithmeticRelation<FF>;

constexpr size_t UNIVARIATE_LENGTH = 12;
const auto restrict_to_standard_arithmetic_relation = [](auto& polys) {
std::fill(polys.q_arith.begin(), polys.q_arith.end(), 1);
std::fill(polys.q_delta_range.begin(), polys.q_delta_range.end(), 0);
Expand All @@ -37,6 +40,7 @@ TEST(Protogalaxy, CombinerOn2Instances)
// relation.
if (is_random_input) {
std::vector<std::shared_ptr<ProverInstance>> instance_data(NUM_INSTANCES);
ASSERT(NUM_INSTANCES == 2); // Don't want to handle more here
ProtoGalaxyProver prover;

for (size_t idx = 0; idx < NUM_INSTANCES; idx++) {
Expand All @@ -50,22 +54,75 @@ TEST(Protogalaxy, CombinerOn2Instances)
}

ProverInstances instances{ instance_data };
instances.alphas.fill(bb::Univariate<FF, 12>(FF(0))); // focus on the arithmetic relation only
instances.alphas.fill(
bb::Univariate<FF, UNIVARIATE_LENGTH>(FF(0))); // focus on the arithmetic relation only
auto pow_polynomial = PowPolynomial(std::vector<FF>{ 2 });
pow_polynomial.compute_values();

// Relation parameters are all zeroes
RelationParameters<FF> relation_parameters;
// Temporary accumulator to compute the sumcheck on the second instance
typename Flavor::TupleOfArraysOfValues temporary_accumulator;

// Accumulate arithmetic relation over 2 rows on the second instance
for (size_t i = 0; i < 2; i++) {
UltraArithmeticRelation::accumulate(
std::get<0>(temporary_accumulator),
instance_data[NUM_INSTANCES - 1]->proving_key.polynomials.get_row(i),
relation_parameters,
pow_polynomial[i]);
}
// Get the result of the 0th subrelation of the arithmetic relation
FF instance_offset = std::get<0>(temporary_accumulator)[0];
// Subtract it from q_c[0] (it directly affect the target sum, making it zero and enabling the optimisation)
instance_data[1]->proving_key.polynomials.q_c[0] -= instance_offset;
std::vector<typename Flavor::ProverPolynomials>
extended_polynomials; // These hold the extensions of prover polynomials

// Manually extend all polynomials. Create new ProverPolynomials from extended values
for (size_t idx = NUM_INSTANCES; idx < UNIVARIATE_LENGTH; idx++) {

auto instance = std::make_shared<ProverInstance>();
auto prover_polynomials = get_zero_prover_polynomials<Flavor>(1);
for (auto [instance_0_polynomial, instance_1_polynomial, new_polynomial] :
zip_view(instance_data[0]->proving_key.polynomials.get_all(),
instance_data[1]->proving_key.polynomials.get_all(),
prover_polynomials.get_all())) {
for (size_t i = 0; i < /*circuit_size*/ 2; i++) {
new_polynomial[i] =
instance_0_polynomial[i] + ((instance_1_polynomial[i] - instance_0_polynomial[i]) * idx);
}
}
extended_polynomials.push_back(std::move(prover_polynomials));
}
std::array<FF, UNIVARIATE_LENGTH> precomputed_result{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
// Compute the sum for each index separately, treating each extended instance independently
for (size_t idx = 0; idx < UNIVARIATE_LENGTH; idx++) {

typename Flavor::TupleOfArraysOfValues accumulator;
if (idx < NUM_INSTANCES) {
for (size_t i = 0; i < 2; i++) {
UltraArithmeticRelation::accumulate(std::get<0>(accumulator),
instance_data[idx]->proving_key.polynomials.get_row(i),
relation_parameters,
pow_polynomial[i]);
}
} else {
for (size_t i = 0; i < 2; i++) {
UltraArithmeticRelation::accumulate(std::get<0>(accumulator),
extended_polynomials[idx - NUM_INSTANCES].get_row(i),
relation_parameters,
pow_polynomial[i]);
}
}
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 expected_result = Univariate<FF, 12>(std::array<FF, 12>{ 8600UL,
12679448UL,
73617560UL,
220571672UL,
491290520UL,
923522840UL,
1555017368UL,
2423522840UL,
3566787992UL,
5022561560UL,
6828592280UL,
9022628888UL });
auto optimised_result = prover.compute_combiner(instances, pow_polynomial);

EXPECT_EQ(result, expected_result);
EXPECT_EQ(optimised_result, expected_result);
} else {
std::vector<std::shared_ptr<ProverInstance>> instance_data(NUM_INSTANCES);
ProtoGalaxyProver prover;
Expand Down

0 comments on commit bcf7207

Please sign in to comment.