Skip to content

Commit

Permalink
refactor: Move sumcheck (#3189)
Browse files Browse the repository at this point in the history
Part four of my breaking-up of honk/ moves sumcheck out of honk.
  • Loading branch information
codygunton authored Nov 2, 2023
1 parent f1ff849 commit 410cae3
Show file tree
Hide file tree
Showing 44 changed files with 232 additions and 201 deletions.
1 change: 1 addition & 0 deletions barretenberg/cpp/scripts/bb-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ TESTS=(
join_split_example_proofs_inner_proof_data_tests
join_split_example_proofs_notes_tests
srs_tests
sumcheck_tests
transcript_tests
dsl_tests
)
Expand Down
1 change: 1 addition & 0 deletions barretenberg/cpp/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ add_subdirectory(barretenberg/serialize)
add_subdirectory(barretenberg/solidity_helpers)
add_subdirectory(barretenberg/srs)
add_subdirectory(barretenberg/stdlib)
add_subdirectory(barretenberg/sumcheck)
add_subdirectory(barretenberg/transcript)
add_subdirectory(barretenberg/wasi)

Expand Down
2 changes: 1 addition & 1 deletion barretenberg/cpp/src/barretenberg/honk/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
barretenberg_module(honk numeric ecc srs proof_system transcript commitment_schemes flavor)
barretenberg_module(honk proof_system commitment_schemes sumcheck)

if(TESTING)
# TODO: Re-enable all these warnings once PoC is finished
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
#include <vector>

#include "barretenberg/honk/composer/eccvm_composer.hpp"
#include "barretenberg/honk/sumcheck/sumcheck_round.hpp"
#include "barretenberg/honk/utils/grand_product_delta.hpp"
#include "barretenberg/numeric/uint256/uint256.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/proof_system/circuit_builder/eccvm/eccvm_circuit_builder.hpp"
#include "barretenberg/proof_system/library/grand_product_delta.hpp"
#include "barretenberg/proof_system/relations/permutation_relation.hpp"
#include "barretenberg/proof_system/relations/relation_parameters.hpp"
#include "barretenberg/sumcheck/sumcheck_round.hpp"

using namespace proof_system::honk;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "barretenberg/flavor/goblin_translator.hpp"
#include "barretenberg/honk/composer/ultra_composer.hpp"
#include "barretenberg/honk/proof_system/grand_product_library.hpp"
#include "barretenberg/honk/proof_system/permutation_library.hpp"
#include "barretenberg/proof_system/library/grand_product_library.hpp"
#include "barretenberg/proof_system/relations/auxiliary_relation.hpp"
#include "barretenberg/proof_system/relations/ecc_op_queue_relation.hpp"
#include "barretenberg/proof_system/relations/elliptic_relation.hpp"
Expand Down
180 changes: 180 additions & 0 deletions barretenberg/cpp/src/barretenberg/honk/composer/sumcheck.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
#include "barretenberg/sumcheck/sumcheck.hpp"
#include "barretenberg/ecc/curves/bn254/fr.hpp"
#include "barretenberg/honk/composer/ultra_composer.hpp"
#include "barretenberg/proof_system/library/grand_product_delta.hpp"
#include "barretenberg/proof_system/library/grand_product_library.hpp"
#include "barretenberg/proof_system/plookup_tables/fixed_base/fixed_base.hpp"
#include "barretenberg/proof_system/relations/auxiliary_relation.hpp"
#include "barretenberg/proof_system/relations/elliptic_relation.hpp"
#include "barretenberg/proof_system/relations/gen_perm_sort_relation.hpp"
#include "barretenberg/proof_system/relations/lookup_relation.hpp"
#include "barretenberg/proof_system/relations/permutation_relation.hpp"
#include "barretenberg/proof_system/relations/ultra_arithmetic_relation.hpp"
#include "barretenberg/transcript/transcript.hpp"

#include <gtest/gtest.h>

using namespace proof_system::honk;
using namespace proof_system::honk::sumcheck;

using Flavor = proof_system::honk::flavor::Ultra;
using FF = typename Flavor::FF;

namespace test_sumcheck_round {

class SumcheckTestsRealCircuit : public ::testing::Test {
protected:
static void SetUpTestSuite() { barretenberg::srs::init_crs_factory("../srs_db/ignition"); }
};

/**
* @brief Test the Ultra Sumcheck Prover and Verifier for a real circuit
*
*/
TEST_F(SumcheckTestsRealCircuit, Ultra)
{
using Flavor = flavor::Ultra;
using FF = typename Flavor::FF;

// Create a composer and a dummy circuit with a few gates
auto builder = proof_system::UltraCircuitBuilder();
FF a = FF::one();

// Add some basic add gates, with a public input for good measure
uint32_t a_idx = builder.add_public_variable(a);
FF b = FF::one();
FF c = a + b;
FF d = a + c;
uint32_t b_idx = builder.add_variable(b);
uint32_t c_idx = builder.add_variable(c);
uint32_t d_idx = builder.add_variable(d);
for (size_t i = 0; i < 16; i++) {
builder.create_add_gate({ a_idx, b_idx, c_idx, 1, 1, -1, 0 });
builder.create_add_gate({ d_idx, c_idx, a_idx, 1, -1, -1, 0 });
}

// Add a big add gate with use of next row to test q_arith = 2
FF e = a + b + c + d;
uint32_t e_idx = builder.add_variable(e);

uint32_t zero_idx = builder.zero_idx;
builder.create_big_add_gate({ a_idx, b_idx, c_idx, d_idx, -1, -1, -1, -1, 0 }, true); // use next row
builder.create_big_add_gate({ zero_idx, zero_idx, zero_idx, e_idx, 0, 0, 0, 0, 0 }, false);

// Add some lookup gates (related to pedersen hashing)
auto pedersen_input_value = FF::random_element();
const FF input_hi =
uint256_t(pedersen_input_value)
.slice(plookup::fixed_base::table::BITS_PER_LO_SCALAR,
plookup::fixed_base::table::BITS_PER_LO_SCALAR + plookup::fixed_base::table::BITS_PER_HI_SCALAR);
const FF input_lo = uint256_t(pedersen_input_value).slice(0, plookup::fixed_base::table::BITS_PER_LO_SCALAR);
const auto input_hi_index = builder.add_variable(input_hi);
const auto input_lo_index = builder.add_variable(input_lo);

const auto sequence_data_hi = plookup::get_lookup_accumulators(plookup::MultiTableId::FIXED_BASE_LEFT_HI, input_hi);
const auto sequence_data_lo = plookup::get_lookup_accumulators(plookup::MultiTableId::FIXED_BASE_LEFT_LO, input_lo);

builder.create_gates_from_plookup_accumulators(
plookup::MultiTableId::FIXED_BASE_LEFT_HI, sequence_data_hi, input_hi_index);
builder.create_gates_from_plookup_accumulators(
plookup::MultiTableId::FIXED_BASE_LEFT_LO, sequence_data_lo, input_lo_index);

// Add a sort gate (simply checks that consecutive inputs have a difference of < 4)
a_idx = builder.add_variable(FF(0));
b_idx = builder.add_variable(FF(1));
c_idx = builder.add_variable(FF(2));
d_idx = builder.add_variable(FF(3));
builder.create_sort_constraint({ a_idx, b_idx, c_idx, d_idx });

// Add an elliptic curve addition gate
grumpkin::g1::affine_element p1 = grumpkin::g1::affine_element::random_element();
grumpkin::g1::affine_element p2 = grumpkin::g1::affine_element::random_element();

grumpkin::g1::affine_element p3(grumpkin::g1::element(p1) + grumpkin::g1::element(p2));

uint32_t x1 = builder.add_variable(p1.x);
uint32_t y1 = builder.add_variable(p1.y);
uint32_t x2 = builder.add_variable(p2.x);
uint32_t y2 = builder.add_variable(p2.y);
uint32_t x3 = builder.add_variable(p3.x);
uint32_t y3 = builder.add_variable(p3.y);

builder.create_ecc_add_gate({ x1, y1, x2, y2, x3, y3, 1 });

// Add some RAM gates
uint32_t ram_values[8]{
builder.add_variable(FF::random_element()), builder.add_variable(FF::random_element()),
builder.add_variable(FF::random_element()), builder.add_variable(FF::random_element()),
builder.add_variable(FF::random_element()), builder.add_variable(FF::random_element()),
builder.add_variable(FF::random_element()), builder.add_variable(FF::random_element()),
};

size_t ram_id = builder.create_RAM_array(8);

for (size_t i = 0; i < 8; ++i) {
builder.init_RAM_element(ram_id, i, ram_values[i]);
}

a_idx = builder.read_RAM_array(ram_id, builder.add_variable(5));
EXPECT_EQ(a_idx != ram_values[5], true);

b_idx = builder.read_RAM_array(ram_id, builder.add_variable(4));
c_idx = builder.read_RAM_array(ram_id, builder.add_variable(1));

builder.write_RAM_array(ram_id, builder.add_variable(4), builder.add_variable(500));
d_idx = builder.read_RAM_array(ram_id, builder.add_variable(4));

EXPECT_EQ(builder.get_variable(d_idx), 500);

// ensure these vars get used in another arithmetic gate
const auto e_value = builder.get_variable(a_idx) + builder.get_variable(b_idx) + builder.get_variable(c_idx) +
builder.get_variable(d_idx);
e_idx = builder.add_variable(e_value);

builder.create_big_add_gate({ a_idx, b_idx, c_idx, d_idx, -1, -1, -1, -1, 0 }, true);
builder.create_big_add_gate(
{
builder.zero_idx,
builder.zero_idx,
builder.zero_idx,
e_idx,
0,
0,
0,
0,
0,
},
false);

// Create a prover (it will compute proving key and witness)
auto composer = UltraComposer();
auto instance = composer.create_instance(builder);

// Generate eta, beta and gamma
FF eta = FF::random_element();
FF beta = FF::random_element();
FF gamma = FF::random_element();

instance->initialise_prover_polynomials();
instance->compute_sorted_accumulator_polynomials(eta);
instance->compute_grand_product_polynomials(beta, gamma);

Flavor::Transcript prover_transcript = Flavor::Transcript::prover_init_empty();
auto circuit_size = instance->proving_key->circuit_size;

auto sumcheck_prover = SumcheckProver<Flavor>(circuit_size, prover_transcript);

auto prover_output = sumcheck_prover.prove(instance->prover_polynomials, instance->relation_parameters);

Flavor::Transcript verifier_transcript = Flavor::Transcript::verifier_init_empty(prover_transcript);

auto sumcheck_verifier = SumcheckVerifier<Flavor>(circuit_size);

auto verifier_output = sumcheck_verifier.verify(instance->relation_parameters, verifier_transcript);

auto verified = verifier_output.verified.value();

ASSERT_TRUE(verified);
}

} // namespace test_sumcheck_round
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "barretenberg/honk/composer/ultra_composer.hpp"
#include "barretenberg/honk/proof_system/grand_product_library.hpp"
#include "barretenberg/proof_system/circuit_builder/ultra_circuit_builder.hpp"
#include "barretenberg/proof_system/composer/composer_lib.hpp"
#include "barretenberg/proof_system/composer/permutation_lib.hpp"
#include "barretenberg/proof_system/library/grand_product_library.hpp"

namespace proof_system::honk {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once
#include "barretenberg/flavor/flavor.hpp"
#include "barretenberg/honk/instance/prover_instance.hpp"
#include "barretenberg/honk/proof_system/goblin_merge/merge_prover.hpp"
#include "barretenberg/honk/proof_system/goblin_merge/merge_verifier.hpp"
#include "barretenberg/honk/proof_system/protogalaxy_prover.hpp"
Expand All @@ -9,6 +8,7 @@
#include "barretenberg/honk/proof_system/ultra_verifier.hpp"
#include "barretenberg/proof_system/composer/composer_lib.hpp"
#include "barretenberg/srs/global_crs.hpp"
#include "barretenberg/sumcheck/instance/prover_instance.hpp"

namespace proof_system::honk {
template <UltraFlavor Flavor> class UltraComposer_ {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
#include "barretenberg/common/serialize.hpp"
#include "barretenberg/ecc/curves/bn254/fr.hpp"
#include "barretenberg/honk/proof_system/ultra_prover.hpp"
#include "barretenberg/honk/sumcheck/sumcheck_round.hpp"
#include "barretenberg/honk/utils/grand_product_delta.hpp"
#include "barretenberg/numeric/uint256/uint256.hpp"
#include "barretenberg/proof_system/circuit_builder/ultra_circuit_builder.hpp"
#include "barretenberg/proof_system/library/grand_product_delta.hpp"
#include "barretenberg/proof_system/plookup_tables/fixed_base/fixed_base.hpp"
#include "barretenberg/proof_system/plookup_tables/types.hpp"
#include "barretenberg/proof_system/relations/permutation_relation.hpp"
#include "barretenberg/proof_system/relations/relation_parameters.hpp"
#include "barretenberg/sumcheck/sumcheck_round.hpp"
#include <cstddef>
#include <cstdint>
#include <gtest/gtest.h>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "barretenberg/flavor/ultra.hpp"
#include "barretenberg/honk/instance/instances.hpp"
#include "barretenberg/honk/proof_system/protogalaxy_prover.hpp"
#include "barretenberg/honk/utils/testing.hpp"
#include "barretenberg/proof_system/relations/relation_parameters.hpp"
#include "barretenberg/sumcheck/instance/instances.hpp"
#include <gtest/gtest.h>

using namespace proof_system::honk;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
#include "barretenberg/commitment_schemes/commitment_key.hpp"
#include "barretenberg/honk/proof_system/lookup_library.hpp"
#include "barretenberg/honk/proof_system/permutation_library.hpp"
#include "barretenberg/honk/sumcheck/sumcheck.hpp"
#include "barretenberg/honk/utils/power_polynomial.hpp"
#include "barretenberg/honk/proof_system/power_polynomial.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/polynomials/univariate.hpp" // will go away
#include "barretenberg/proof_system/library/grand_product_library.hpp"
#include "barretenberg/proof_system/relations/lookup_relation.hpp"
#include "barretenberg/proof_system/relations/permutation_relation.hpp"
#include "barretenberg/sumcheck/sumcheck.hpp"
#include <algorithm>
#include <array>
#include <cstddef>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#include "barretenberg/commitment_schemes/gemini/gemini.hpp"
#include "barretenberg/commitment_schemes/shplonk/shplonk.hpp"
#include "barretenberg/flavor/ecc_vm.hpp"
#include "barretenberg/honk/sumcheck/sumcheck_output.hpp"
#include "barretenberg/plonk/proof_system/types/proof.hpp"
#include "barretenberg/proof_system/relations/relation_parameters.hpp"
#include "barretenberg/sumcheck/sumcheck_output.hpp"
#include "barretenberg/transcript/transcript.hpp"

namespace proof_system::honk {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "./eccvm_verifier.hpp"
#include "barretenberg/commitment_schemes/gemini/gemini.hpp"
#include "barretenberg/commitment_schemes/shplonk/shplonk.hpp"
#include "barretenberg/honk/utils/power_polynomial.hpp"
#include "barretenberg/honk/proof_system/power_polynomial.hpp"
#include "barretenberg/numeric/bitop/get_msb.hpp"
#include "barretenberg/transcript/transcript.hpp"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
#include "barretenberg/flavor/ecc_vm.hpp"
#include "barretenberg/honk/sumcheck/sumcheck.hpp"
#include "barretenberg/plonk/proof_system/types/proof.hpp"
#include "barretenberg/sumcheck/sumcheck.hpp"

namespace proof_system::honk {
template <typename Flavor> class ECCVMVerifier_ {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include "barretenberg/honk/sumcheck/sumcheck.hpp"
#include "barretenberg/sumcheck/sumcheck.hpp"
#include <typeinfo>

namespace proof_system::honk::lookup_library {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
#include "barretenberg/honk/sumcheck/sumcheck.hpp"
#include "barretenberg/plonk/proof_system/proving_key/proving_key.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/sumcheck/sumcheck.hpp"
#include <typeinfo>

namespace proof_system::honk::permutation_library {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
// Note these tests are a bit hacky

// #include "instances.hpp"
// #include "barretenberg/ecc/curves/bn254/bn254.hpp"
// #include "barretenberg/honk/proof_system/grand_product_library.hpp"
// #include "barretenberg/polynomials/polynomial.hpp"
// #include "barretenberg/srs/factories/file_crs_factory.hpp"
// #include <gtest/gtest.h>
#include "barretenberg/honk/composer/ultra_composer.hpp"
#include "protogalaxy_prover.hpp"
#include <gtest/gtest.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
#include "barretenberg/flavor/flavor.hpp"
#include "barretenberg/flavor/goblin_ultra.hpp"
#include "barretenberg/flavor/ultra.hpp"
#include "barretenberg/honk/instance/instances.hpp"
#include "barretenberg/honk/proof_system/folding_result.hpp"
#include "barretenberg/polynomials/pow.hpp"
#include "barretenberg/polynomials/univariate.hpp"
#include "barretenberg/proof_system/relations/relation_parameters.hpp"
#include "barretenberg/proof_system/relations/utils.hpp"
#include "barretenberg/sumcheck/instance/instances.hpp"

namespace proof_system::honk {
template <class ProverInstances_> class ProtoGalaxyProver_ {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "protogalaxy_verifier.hpp"
#include "barretenberg/honk/utils/grand_product_delta.hpp"
#include "barretenberg/proof_system/library/grand_product_delta.hpp"
namespace proof_system::honk {
template <class VerifierInstances>
VerifierFoldingResult<typename VerifierInstances::Flavor> ProtoGalaxyVerifier_<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#include "barretenberg/flavor/flavor.hpp"
#include "barretenberg/flavor/goblin_ultra.hpp"
#include "barretenberg/flavor/ultra.hpp"
#include "barretenberg/honk/instance/instances.hpp"
#include "barretenberg/honk/proof_system/folding_result.hpp"
#include "barretenberg/sumcheck/instance/instances.hpp"
#include "barretenberg/transcript/transcript.hpp"

namespace proof_system::honk {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "ultra_prover.hpp"
#include "barretenberg/honk/sumcheck/sumcheck.hpp"
#include "barretenberg/honk/utils/power_polynomial.hpp"
#include "barretenberg/honk/proof_system/power_polynomial.hpp"
#include "barretenberg/sumcheck/sumcheck.hpp"

namespace proof_system::honk {

Expand Down
Loading

0 comments on commit 410cae3

Please sign in to comment.