Skip to content

Commit

Permalink
Implement check_circuit
Browse files Browse the repository at this point in the history
  • Loading branch information
codygunton committed Apr 1, 2024
1 parent 9572634 commit 35a8e8c
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,6 @@ class ECCVMCircuitBuilder {
return result;
}

bool check_circuit() { return true; }

[[nodiscard]] size_t get_num_gates() const
{
// (issue #2218)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#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;
Expand Down Expand Up @@ -39,7 +39,7 @@ TEST(ECCVMCircuitBuilderTests, BaseCase)
op_queue->mul_accumulate(c, x);

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

Expand All @@ -53,7 +53,7 @@ TEST(ECCVMCircuitBuilderTests, Add)
op_queue->add_accumulate(a);

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

Expand All @@ -68,7 +68,7 @@ TEST(ECCVMCircuitBuilderTests, Mul)
op_queue->mul_accumulate(a, x);

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

Expand All @@ -89,7 +89,7 @@ TEST(ECCVMCircuitBuilderTests, ShortMul)
op_queue->eq();

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

Expand All @@ -113,7 +113,7 @@ TEST(ECCVMCircuitBuilderTests, EqFails)
.z2 = 0,
.mul_scalar_full = 0 });
ECCVMCircuitBuilder circuit{ op_queue };
bool result = circuit.check_circuit();
bool result = ECCVMTraceChecker::check_circuit(circuit);
EXPECT_EQ(result, false);
}

Expand All @@ -124,7 +124,7 @@ TEST(ECCVMCircuitBuilderTests, EmptyRow)
op_queue->empty_row();

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

Expand All @@ -141,7 +141,7 @@ TEST(ECCVMCircuitBuilderTests, EmptyRowBetweenOps)
op_queue->eq();

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

Expand All @@ -157,7 +157,7 @@ TEST(ECCVMCircuitBuilderTests, EndWithEq)
op_queue->eq();

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

Expand All @@ -174,7 +174,7 @@ TEST(ECCVMCircuitBuilderTests, EndWithAdd)
op_queue->add_accumulate(a);

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

Expand All @@ -191,7 +191,7 @@ TEST(ECCVMCircuitBuilderTests, EndWithMul)
op_queue->mul_accumulate(a, x);

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

Expand All @@ -209,7 +209,7 @@ TEST(ECCVMCircuitBuilderTests, EndWithNoop)

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

Expand Down Expand Up @@ -237,7 +237,7 @@ TEST(ECCVMCircuitBuilderTests, MSM)

compute_msms(j, op_queue);
ECCVMCircuitBuilder circuit{ op_queue };
bool result = circuit.check_circuit();
bool result = ECCVMTraceChecker::check_circuit(circuit);
EXPECT_EQ(result, true);
}
// chain msms
Expand All @@ -247,6 +247,6 @@ TEST(ECCVMCircuitBuilderTests, MSM)
compute_msms(j, op_queue);
}
ECCVMCircuitBuilder circuit{ op_queue };
bool result = circuit.check_circuit();
bool result = ECCVMTraceChecker::check_circuit(circuit);
EXPECT_EQ(result, true);
}
4 changes: 3 additions & 1 deletion barretenberg/cpp/src/barretenberg/eccvm/eccvm_flavor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,9 @@ class ECCVMFlavor {

const auto num_rows_log2 = static_cast<size_t>(numeric::get_msb64(num_rows));
size_t num_rows_pow2 = 1UL << (num_rows_log2 + (1UL << num_rows_log2 == num_rows ? 0 : 1));

for (auto& poly : get_all()) {
poly = Polynomial(num_rows_pow2);
}
lagrange_first[0] = 1;
lagrange_second[1] = 1;
lagrange_last[lagrange_last.size() - 1] = 1;
Expand Down
84 changes: 84 additions & 0 deletions barretenberg/cpp/src/barretenberg/eccvm/eccvm_trace_checker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include "eccvm_trace_checker.hpp"
#include "barretenberg/eccvm/eccvm_flavor.hpp"

using namespace bb;

using Flavor = ECCVMFlavor;
using Builder = typename ECCVMFlavor::CircuitBuilder;
using FF = typename ECCVMFlavor::FF;
using ProverPolynomials = typename ECCVMFlavor::ProverPolynomials;

bool ECCVMTraceChecker::check_circuit(Builder& builder)
{
const FF gamma = FF::random_element();
const FF beta = FF::random_element();
const FF beta_sqr = beta.sqr();
const FF beta_cube = beta_sqr * beta;
auto eccvm_set_permutation_delta =
gamma * (gamma + beta_sqr) * (gamma + beta_sqr + beta_sqr) * (gamma + beta_sqr + beta_sqr + beta_sqr);
eccvm_set_permutation_delta = eccvm_set_permutation_delta.invert();
bb::RelationParameters<typename Flavor::FF> params{
.eta = 0,
.beta = beta,
.gamma = gamma,
.public_input_delta = 0,
.lookup_grand_product_delta = 0,
.beta_sqr = beta_sqr,
.beta_cube = beta_cube,
.eccvm_set_permutation_delta = eccvm_set_permutation_delta,
};

ProverPolynomials polynomials(builder);
const size_t num_rows = polynomials.get_polynomial_size();
compute_logderivative_inverse<Flavor, ECCVMLookupRelation<FF>>(polynomials, params, num_rows);
compute_permutation_grand_product<Flavor, ECCVMSetRelation<FF>>(num_rows, polynomials, params);

polynomials.z_perm_shift = Polynomial(polynomials.z_perm.shifted());

const auto evaluate_relation = [&]<typename Relation>(const std::string& relation_name) {
typename Relation::SumcheckArrayOfValuesOverSubrelations result;
for (auto& r : result) {
r = 0;
}
constexpr size_t NUM_SUBRELATIONS = result.size();

for (size_t i = 0; i < num_rows; ++i) {
Relation::accumulate(result, polynomials.get_row(i), params, 1);

bool x = true;
for (size_t j = 0; j < NUM_SUBRELATIONS; ++j) {
if (result[j] != 0) {
info("Relation ", relation_name, ", subrelation index ", j, " failed at row ", i);
x = false;
}
}
if (!x) {
return false;
}
}
return true;
};

bool result = true;
result = result && evaluate_relation.template operator()<ECCVMTranscriptRelation<FF>>("ECCVMTranscriptRelation");
result = result && evaluate_relation.template operator()<ECCVMPointTableRelation<FF>>("ECCVMPointTableRelation");
result = result && evaluate_relation.template operator()<ECCVMWnafRelation<FF>>("ECCVMWnafRelation");
result = result && evaluate_relation.template operator()<ECCVMMSMRelation<FF>>("ECCVMMSMRelation");
result = result && evaluate_relation.template operator()<ECCVMSetRelation<FF>>("ECCVMSetRelation");

using LookupRelation = ECCVMLookupRelation<FF>;
typename ECCVMLookupRelation<typename Flavor::FF>::SumcheckArrayOfValuesOverSubrelations lookup_result;
for (auto& r : lookup_result) {
r = 0;
}
for (size_t i = 0; i < num_rows; ++i) {
LookupRelation::accumulate(lookup_result, polynomials.get_row(i), params, 1);
}
for (auto r : lookup_result) {
if (r != 0) {
info("Relation ECCVMLookupRelation failed.");
return false;
}
}
return result;
}
85 changes: 5 additions & 80 deletions barretenberg/cpp/src/barretenberg/eccvm/eccvm_trace_checker.hpp
Original file line number Diff line number Diff line change
@@ -1,83 +1,8 @@
#pragma once

#include "eccvm_circuit_builder.hpp"
namespace bb {

class ECCVMTraceChecker {
// bool check_circuit()
// {
// const FF gamma = FF::random_element();
// const FF beta = FF::random_element();
// const FF beta_sqr = beta.sqr();
// const FF beta_cube = beta_sqr * beta;
// auto eccvm_set_permutation_delta =
// gamma * (gamma + beta_sqr) * (gamma + beta_sqr + beta_sqr) * (gamma + beta_sqr + beta_sqr + beta_sqr);
// eccvm_set_permutation_delta = eccvm_set_permutation_delta.invert();
// bb::RelationParameters<typename Flavor::FF> params{
// .eta = 0,
// .beta = beta,
// .gamma = gamma,
// .public_input_delta = 0,
// .lookup_grand_product_delta = 0,
// .beta_sqr = beta_sqr,
// .beta_cube = beta_cube,
// .eccvm_set_permutation_delta = eccvm_set_permutation_delta,
// };

// auto polynomials = compute_polynomials();
// const size_t num_rows = polynomials.get_polynomial_size();
// compute_logderivative_inverse<Flavor, ECCVMLookupRelation<FF>>(polynomials, params, num_rows);
// compute_permutation_grand_product<Flavor, ECCVMSetRelation<FF>>(num_rows, polynomials, params);

// polynomials.z_perm_shift = Polynomial(polynomials.z_perm.shifted());

// const auto evaluate_relation = [&]<typename Relation>(const std::string& relation_name) {
// typename Relation::SumcheckArrayOfValuesOverSubrelations result;
// for (auto& r : result) {
// r = 0;
// }
// constexpr size_t NUM_SUBRELATIONS = result.size();

// for (size_t i = 0; i < num_rows; ++i) {
// Relation::accumulate(result, polynomials.get_row(i), params, 1);

// bool x = true;
// for (size_t j = 0; j < NUM_SUBRELATIONS; ++j) {
// if (result[j] != 0) {
// info("Relation ", relation_name, ", subrelation index ", j, " failed at row ", i);
// x = false;
// }
// }
// if (!x) {
// return false;
// }
// }
// return true;
// };

// bool result = true;
// result =
// result && evaluate_relation.template operator()<ECCVMTranscriptRelation<FF>>("ECCVMTranscriptRelation");
// result =
// result && evaluate_relation.template operator()<ECCVMPointTableRelation<FF>>("ECCVMPointTableRelation");
// result = result && evaluate_relation.template operator()<ECCVMWnafRelation<FF>>("ECCVMWnafRelation");
// result = result && evaluate_relation.template operator()<ECCVMMSMRelation<FF>>("ECCVMMSMRelation");
// result = result && evaluate_relation.template operator()<ECCVMSetRelation<FF>>("ECCVMSetRelation");

// using LookupRelation = ECCVMLookupRelation<FF>;
// typename ECCVMLookupRelation<typename Flavor::FF>::SumcheckArrayOfValuesOverSubrelations lookup_result;
// for (auto& r : lookup_result) {
// r = 0;
// }
// for (size_t i = 0; i < num_rows; ++i) {
// LookupRelation::accumulate(lookup_result, polynomials.get_row(i), params, 1);
// }
// for (auto r : lookup_result) {
// if (r != 0) {
// info("Relation ECCVMLookupRelation failed.");
// return false;
// }
// }
// return result;
// }
// }
}
public:
static bool check_circuit(ECCVMCircuitBuilder&);
};
} // namespace bb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "barretenberg/common/ref_vector.hpp"
#include "barretenberg/common/zip_view.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/relations/relation_parameters.hpp"
#include <typeinfo>
Expand Down

0 comments on commit 35a8e8c

Please sign in to comment.