Skip to content

Commit

Permalink
Builder not a template; flavor knows builder, IOU check circuit
Browse files Browse the repository at this point in the history
  • Loading branch information
codygunton committed Mar 31, 2024
1 parent 03335b3 commit 9572634
Show file tree
Hide file tree
Showing 17 changed files with 389 additions and 423 deletions.
3 changes: 2 additions & 1 deletion barretenberg/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,6 @@
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"cmake.sourceDirectory": "${workspaceFolder}/cpp"
"cmake.sourceDirectory": "${workspaceFolder}/cpp",
"clangd.path": "/usr/bin/clangd"
}
2 changes: 1 addition & 1 deletion barretenberg/barretenberg.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
// Clangd. Note that this setting may be overridden by user settings
// to the default value "clangd".
//
"clangd.path": "clangd-16",
"clangd.path": "/usr/bin/clangd",
// We should disable automatic inclusion of headers unless we decide to follow "WhyIWYU".
"clangd.arguments": [
"-header-insertion=never"
Expand Down
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
@@ -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: 5 additions & 335 deletions barretenberg/cpp/src/barretenberg/eccvm/eccvm_circuit_builder.hpp

Large diffs are not rendered by default.

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 <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 };
ECCVMCircuitBuilder circuit{ op_queue };
bool result = circuit.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 };
ECCVMCircuitBuilder circuit{ op_queue };
bool result = circuit.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 };
ECCVMCircuitBuilder circuit{ op_queue };
bool result = circuit.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 };
ECCVMCircuitBuilder circuit{ op_queue };
bool result = circuit.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 };
ECCVMCircuitBuilder circuit{ op_queue };
bool result = circuit.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 };
ECCVMCircuitBuilder circuit{ op_queue };
bool result = circuit.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 };
ECCVMCircuitBuilder circuit{ op_queue };
bool result = circuit.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 };
ECCVMCircuitBuilder circuit{ op_queue };
bool result = circuit.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 };
ECCVMCircuitBuilder circuit{ op_queue };
bool result = circuit.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 };
ECCVMCircuitBuilder circuit{ op_queue };
bool result = circuit.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 };
ECCVMCircuitBuilder circuit{ op_queue };
bool result = circuit.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,11 +233,10 @@ 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 };
ECCVMCircuitBuilder circuit{ op_queue };
bool result = circuit.check_circuit();
EXPECT_EQ(result, true);
}
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 };
ECCVMCircuitBuilder circuit{ op_queue };
bool result = circuit.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

0 comments on commit 9572634

Please sign in to comment.