Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: ECCVM flavor depends on builder #5323

Merged
merged 5 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using namespace benchmark;
using namespace bb;

using Flavor = ECCVMFlavor;
using Builder = ECCVMCircuitBuilder<Flavor>;
using Builder = ECCVMCircuitBuilder;
using Composer = ECCVMComposer;

namespace {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "barretenberg/stdlib_circuit_builders/standard_circuit_builder.hpp"
#include "barretenberg/stdlib_circuit_builders/ultra_circuit_builder.hpp"

#include <optional>
// TODO(https://github.com/AztecProtocol/barretenberg/issues/928): Reorganize

namespace bb {
template <typename T>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

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

namespace bb::eccvm {
Expand Down
340 changes: 4 additions & 336 deletions barretenberg/cpp/src/barretenberg/eccvm/eccvm_circuit_builder.hpp

Large diffs are not rendered by default.

122 changes: 42 additions & 80 deletions barretenberg/cpp/src/barretenberg/eccvm/eccvm_circuit_builder.test.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
#include "eccvm_circuit_builder.hpp"
#include "barretenberg/crypto/generators/generator_data.hpp"
#include "barretenberg/crypto/pedersen_commitment/pedersen.hpp"
#include "barretenberg/eccvm/eccvm_flavor.hpp"
#include "barretenberg/eccvm/eccvm_trace_checker.hpp"
#include <gtest/gtest.h>

using namespace bb;
using G1 = bb::g1;
using Fr = typename G1::Fr;

namespace {
auto& engine = numeric::get_debug_randomness();

template <typename Flavor> class ECCVMCircuitBuilderTests : public ::testing::Test {};

using FlavorTypes = ::testing::Types<ECCVMFlavor>;
} // namespace

TYPED_TEST_SUITE(ECCVMCircuitBuilderTests, FlavorTypes);

TYPED_TEST(ECCVMCircuitBuilderTests, BaseCase)
TEST(ECCVMCircuitBuilderTests, BaseCase)
{
using Flavor = TypeParam;
using G1 = typename Flavor::CycleGroup;
using Fr = typename G1::Fr;
auto generators = G1::derive_generators("test generators", 3);
typename G1::element a = generators[0];
typename G1::element b = generators[1];
Expand All @@ -44,32 +38,27 @@ TYPED_TEST(ECCVMCircuitBuilderTests, BaseCase)
op_queue->mul_accumulate(b, x);
op_queue->mul_accumulate(c, x);

ECCVMCircuitBuilder<Flavor> circuit{ op_queue };
bool result = circuit.check_circuit();
ECCVMCircuitBuilder circuit{ op_queue };
bool result = ECCVMTraceChecker::check(circuit);
EXPECT_EQ(result, true);
}

TYPED_TEST(ECCVMCircuitBuilderTests, Add)
TEST(ECCVMCircuitBuilderTests, Add)
{
using Flavor = TypeParam;
using G1 = typename Flavor::CycleGroup;
std::shared_ptr<ECCOpQueue> op_queue = std::make_shared<ECCOpQueue>();

auto generators = G1::derive_generators("test generators", 3);
typename G1::element a = generators[0];

op_queue->add_accumulate(a);

ECCVMCircuitBuilder<Flavor> circuit{ op_queue };
bool result = circuit.check_circuit();
ECCVMCircuitBuilder circuit{ op_queue };
bool result = ECCVMTraceChecker::check(circuit);
EXPECT_EQ(result, true);
}

TYPED_TEST(ECCVMCircuitBuilderTests, Mul)
TEST(ECCVMCircuitBuilderTests, Mul)
{
using Flavor = TypeParam;
using G1 = typename Flavor::CycleGroup;
using Fr = typename G1::Fr;
std::shared_ptr<ECCOpQueue> op_queue = std::make_shared<ECCOpQueue>();

auto generators = G1::derive_generators("test generators", 3);
Expand All @@ -78,16 +67,13 @@ TYPED_TEST(ECCVMCircuitBuilderTests, Mul)

op_queue->mul_accumulate(a, x);

ECCVMCircuitBuilder<Flavor> circuit{ op_queue };
bool result = circuit.check_circuit();
ECCVMCircuitBuilder circuit{ op_queue };
bool result = ECCVMTraceChecker::check(circuit);
EXPECT_EQ(result, true);
}

TYPED_TEST(ECCVMCircuitBuilderTests, ShortMul)
TEST(ECCVMCircuitBuilderTests, ShortMul)
{
using Flavor = TypeParam;
using G1 = typename Flavor::CycleGroup;
using Fr = typename G1::Fr;
std::shared_ptr<ECCOpQueue> op_queue = std::make_shared<ECCOpQueue>();

auto generators = G1::derive_generators("test generators", 3);
Expand All @@ -102,16 +88,13 @@ TYPED_TEST(ECCVMCircuitBuilderTests, ShortMul)
op_queue->mul_accumulate(a, x);
op_queue->eq();

ECCVMCircuitBuilder<Flavor> circuit{ op_queue };
bool result = circuit.check_circuit();
ECCVMCircuitBuilder circuit{ op_queue };
bool result = ECCVMTraceChecker::check(circuit);
EXPECT_EQ(result, true);
}

TYPED_TEST(ECCVMCircuitBuilderTests, EqFails)
TEST(ECCVMCircuitBuilderTests, EqFails)
{
using Flavor = TypeParam;
using G1 = typename Flavor::CycleGroup;
using Fr = typename G1::Fr;
using ECCVMOperation = eccvm::VMOperation<G1>;
std::shared_ptr<ECCOpQueue> op_queue = std::make_shared<ECCOpQueue>();

Expand All @@ -129,28 +112,24 @@ TYPED_TEST(ECCVMCircuitBuilderTests, EqFails)
.z1 = 0,
.z2 = 0,
.mul_scalar_full = 0 });
ECCVMCircuitBuilder<Flavor> circuit{ op_queue };
bool result = circuit.check_circuit();
ECCVMCircuitBuilder circuit{ op_queue };
bool result = ECCVMTraceChecker::check(circuit);
EXPECT_EQ(result, false);
}

TYPED_TEST(ECCVMCircuitBuilderTests, EmptyRow)
TEST(ECCVMCircuitBuilderTests, EmptyRow)
{
using Flavor = TypeParam;
std::shared_ptr<ECCOpQueue> op_queue = std::make_shared<ECCOpQueue>();

op_queue->empty_row();

ECCVMCircuitBuilder<Flavor> circuit{ op_queue };
bool result = circuit.check_circuit();
ECCVMCircuitBuilder circuit{ op_queue };
bool result = ECCVMTraceChecker::check(circuit);
EXPECT_EQ(result, true);
}

TYPED_TEST(ECCVMCircuitBuilderTests, EmptyRowBetweenOps)
TEST(ECCVMCircuitBuilderTests, EmptyRowBetweenOps)
{
using Flavor = TypeParam;
using G1 = typename Flavor::CycleGroup;
using Fr = typename G1::Fr;
std::shared_ptr<ECCOpQueue> op_queue = std::make_shared<ECCOpQueue>();

auto generators = G1::derive_generators("test generators", 3);
Expand All @@ -161,16 +140,13 @@ TYPED_TEST(ECCVMCircuitBuilderTests, EmptyRowBetweenOps)
op_queue->empty_row();
op_queue->eq();

ECCVMCircuitBuilder<Flavor> circuit{ op_queue };
bool result = circuit.check_circuit();
ECCVMCircuitBuilder circuit{ op_queue };
bool result = ECCVMTraceChecker::check(circuit);
EXPECT_EQ(result, true);
}

TYPED_TEST(ECCVMCircuitBuilderTests, EndWithEq)
TEST(ECCVMCircuitBuilderTests, EndWithEq)
{
using Flavor = TypeParam;
using G1 = typename Flavor::CycleGroup;
using Fr = typename G1::Fr;
std::shared_ptr<ECCOpQueue> op_queue = std::make_shared<ECCOpQueue>();

auto generators = G1::derive_generators("test generators", 3);
Expand All @@ -180,16 +156,13 @@ TYPED_TEST(ECCVMCircuitBuilderTests, EndWithEq)
op_queue->mul_accumulate(a, x);
op_queue->eq();

ECCVMCircuitBuilder<Flavor> circuit{ op_queue };
bool result = circuit.check_circuit();
ECCVMCircuitBuilder circuit{ op_queue };
bool result = ECCVMTraceChecker::check(circuit);
EXPECT_EQ(result, true);
}

TYPED_TEST(ECCVMCircuitBuilderTests, EndWithAdd)
TEST(ECCVMCircuitBuilderTests, EndWithAdd)
{
using Flavor = TypeParam;
using G1 = typename Flavor::CycleGroup;
using Fr = typename G1::Fr;
std::shared_ptr<ECCOpQueue> op_queue = std::make_shared<ECCOpQueue>();

auto generators = G1::derive_generators("test generators", 3);
Expand All @@ -200,16 +173,13 @@ TYPED_TEST(ECCVMCircuitBuilderTests, EndWithAdd)
op_queue->eq();
op_queue->add_accumulate(a);

ECCVMCircuitBuilder<Flavor> circuit{ op_queue };
bool result = circuit.check_circuit();
ECCVMCircuitBuilder circuit{ op_queue };
bool result = ECCVMTraceChecker::check(circuit);
EXPECT_EQ(result, true);
}

TYPED_TEST(ECCVMCircuitBuilderTests, EndWithMul)
TEST(ECCVMCircuitBuilderTests, EndWithMul)
{
using Flavor = TypeParam;
using G1 = typename Flavor::CycleGroup;
using Fr = typename G1::Fr;
std::shared_ptr<ECCOpQueue> op_queue = std::make_shared<ECCOpQueue>();

auto generators = G1::derive_generators("test generators", 3);
Expand All @@ -220,16 +190,13 @@ TYPED_TEST(ECCVMCircuitBuilderTests, EndWithMul)
op_queue->eq();
op_queue->mul_accumulate(a, x);

ECCVMCircuitBuilder<Flavor> circuit{ op_queue };
bool result = circuit.check_circuit();
ECCVMCircuitBuilder circuit{ op_queue };
bool result = ECCVMTraceChecker::check(circuit);
EXPECT_EQ(result, true);
}

TYPED_TEST(ECCVMCircuitBuilderTests, EndWithNoop)
TEST(ECCVMCircuitBuilderTests, EndWithNoop)
{
using Flavor = TypeParam;
using G1 = typename Flavor::CycleGroup;
using Fr = typename G1::Fr;
std::shared_ptr<ECCOpQueue> op_queue = std::make_shared<ECCOpQueue>();

auto generators = G1::derive_generators("test generators", 3);
Expand All @@ -241,17 +208,13 @@ TYPED_TEST(ECCVMCircuitBuilderTests, EndWithNoop)
op_queue->mul_accumulate(a, x);

op_queue->empty_row();
ECCVMCircuitBuilder<Flavor> circuit{ op_queue };
bool result = circuit.check_circuit();
ECCVMCircuitBuilder circuit{ op_queue };
bool result = ECCVMTraceChecker::check(circuit);
EXPECT_EQ(result, true);
}

TYPED_TEST(ECCVMCircuitBuilderTests, MSM)
TEST(ECCVMCircuitBuilderTests, MSM)
{
using Flavor = TypeParam;
using G1 = typename Flavor::CycleGroup;
using Fr = typename G1::Fr;

static constexpr size_t max_num_msms = 9;
auto generators = G1::derive_generators("test generators", max_num_msms);

Expand All @@ -270,12 +233,11 @@ TYPED_TEST(ECCVMCircuitBuilderTests, MSM)

// single msms
for (size_t j = 1; j < max_num_msms; ++j) {
using Flavor = TypeParam;
std::shared_ptr<ECCOpQueue> op_queue = std::make_shared<ECCOpQueue>();

compute_msms(j, op_queue);
ECCVMCircuitBuilder<Flavor> circuit{ op_queue };
bool result = circuit.check_circuit();
ECCVMCircuitBuilder circuit{ op_queue };
bool result = ECCVMTraceChecker::check(circuit);
EXPECT_EQ(result, true);
}
// chain msms
Expand All @@ -284,7 +246,7 @@ TYPED_TEST(ECCVMCircuitBuilderTests, MSM)
for (size_t j = 1; j < 9; ++j) {
compute_msms(j, op_queue);
}
ECCVMCircuitBuilder<Flavor> circuit{ op_queue };
bool result = circuit.check_circuit();
ECCVMCircuitBuilder circuit{ op_queue };
bool result = ECCVMTraceChecker::check(circuit);
EXPECT_EQ(result, true);
}
5 changes: 3 additions & 2 deletions barretenberg/cpp/src/barretenberg/eccvm/eccvm_composer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ namespace bb {
* @brief Compute witness polynomials
*
*/
template <IsECCVMFlavor Flavor> void ECCVMComposer_<Flavor>::compute_witness(CircuitConstructor& circuit_constructor)
template <IsECCVMFlavor Flavor>
void ECCVMComposer_<Flavor>::compute_witness([[maybe_unused]] CircuitConstructor& circuit_constructor)
{
BB_OP_COUNT_TIME_NAME("ECCVMComposer::compute_witness");

if (computed_witness) {
return;
}

auto polynomials = circuit_constructor.compute_polynomials();
ProverPolynomials polynomials(circuit_constructor);

auto key_wires = proving_key->get_wires();
auto poly_wires = polynomials.get_wires();
Expand Down
5 changes: 3 additions & 2 deletions barretenberg/cpp/src/barretenberg/eccvm/eccvm_composer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ namespace bb {
template <IsECCVMFlavor Flavor> class ECCVMComposer_ {
public:
using FF = typename Flavor::FF;
using CircuitConstructor = ECCVMCircuitBuilder<Flavor>;
using CircuitConstructor = ECCVMCircuitBuilder;
using ProvingKey = typename Flavor::ProvingKey;
using VerificationKey = typename Flavor::VerificationKey;
using PCS = typename Flavor::PCS;
using CommitmentKey = typename Flavor::CommitmentKey;
using VerifierCommitmentKey = typename Flavor::VerifierCommitmentKey;
using ProverPolynomials = typename Flavor::ProverPolynomials;
using Transcript = typename Flavor::Transcript;

static constexpr std::string_view NAME_STRING = "ECCVM";
static constexpr size_t NUM_RESERVED_GATES = 0; // equal to the number of multilinear evaluations leaked
static constexpr size_t NUM_WIRES = CircuitConstructor::NUM_WIRES;
static constexpr size_t NUM_WIRES = Flavor::NUM_WIRES;
std::shared_ptr<ProvingKey> proving_key;
std::shared_ptr<VerificationKey> verification_key;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ TYPED_TEST_SUITE(ECCVMComposerTests, FlavorTypes);
namespace {
auto& engine = numeric::get_debug_randomness();
}
template <typename Flavor> ECCVMCircuitBuilder<Flavor> generate_circuit(numeric::RNG* engine = nullptr)
template <typename Flavor> ECCVMCircuitBuilder generate_circuit(numeric::RNG* engine = nullptr)
{
std::shared_ptr<ECCOpQueue> op_queue = std::make_shared<ECCOpQueue>();
using G1 = typename Flavor::CycleGroup;
Expand Down Expand Up @@ -61,7 +61,7 @@ template <typename Flavor> ECCVMCircuitBuilder<Flavor> generate_circuit(numeric:
op_queue->mul_accumulate(a, x);
op_queue->mul_accumulate(b, x);
op_queue->mul_accumulate(c, x);
ECCVMCircuitBuilder<Flavor> builder{ op_queue };
ECCVMCircuitBuilder builder{ op_queue };
return builder;
}

Expand Down
Loading
Loading