-
Notifications
You must be signed in to change notification settings - Fork 295
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
feat: Log-derivative based generic permutations for AVM #3428
Merged
Rumata888
merged 15 commits into
master
from
is/bonobus_generic_set-permutation_relations
Dec 7, 2023
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
34acaaf
Generalize lookup library
Rumata888 d2a1d19
Basic case
Rumata888 262a76c
Generic log-derivative-based permutations
Rumata888 087799e
File rename
Rumata888 3a38cbc
Comments in the relation
Rumata888 26dec33
A few more small updates
Rumata888 b4a3ee7
LIbrary update
Rumata888 d8e4c78
Small updates
Rumata888 fd0f20e
Format
Rumata888 b05fe16
Before making circuit builder flavorless
Rumata888 93e3f71
Renames
Rumata888 01de17c
A few comments
Rumata888 ff5b5a1
After rebase (pain)
Rumata888 b00dc64
Labels
Rumata888 a35b65c
FIx
Rumata888 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
...erg/cpp/src/barretenberg/proof_system/circuit_builder/toy_avm/toy_avm_circuit_builder.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/** | ||
* @file avm_template_circuit_builder.hpp | ||
* @author Rumata888 | ||
* @brief A circuit builder for the AVM toy version used to showcase permutation and lookup mechanisms for PIL | ||
* | ||
*/ | ||
#pragma once | ||
|
||
#include "barretenberg/common/constexpr_utils.hpp" | ||
#include "barretenberg/ecc/curves/bn254/fr.hpp" | ||
#include "barretenberg/flavor/toy_avm.hpp" | ||
#include "barretenberg/honk/proof_system/logderivative_library.hpp" | ||
#include "barretenberg/relations/relation_parameters.hpp" | ||
#include "barretenberg/relations/toy_avm/generic_permutation_relation.hpp" | ||
|
||
namespace proof_system { | ||
|
||
/** | ||
* @brief Circuit builder for the ToyAVM that is used to explain generic permutation settings | ||
* | ||
* @tparam Flavor | ||
*/ | ||
template <typename Flavor> class ToyAVMCircuitBuilder { | ||
public: | ||
using FF = typename Flavor::FF; | ||
using Polynomial = typename Flavor::Polynomial; | ||
|
||
static constexpr size_t NUM_POLYNOMIALS = Flavor::NUM_ALL_ENTITIES; | ||
static constexpr size_t NUM_WIRES = Flavor::NUM_WIRES; | ||
|
||
using AllPolynomials = typename Flavor::AllPolynomials; | ||
size_t num_gates = 0; | ||
std::array<std::vector<FF>, NUM_WIRES> wires; | ||
ToyAVMCircuitBuilder() = default; | ||
|
||
void add_row(const std::array<FF, NUM_WIRES> row) | ||
{ | ||
for (size_t i = 0; i < NUM_WIRES; i++) { | ||
wires[i].emplace_back(row[i]); | ||
} | ||
num_gates = wires[0].size(); | ||
} | ||
|
||
/** | ||
* @brief Compute the AVM Template flavor polynomial data required to generate a proof | ||
* | ||
* @return AllPolynomials | ||
*/ | ||
AllPolynomials compute_polynomials() | ||
{ | ||
|
||
const auto num_gates_log2 = static_cast<size_t>(numeric::get_msb64(num_gates)); | ||
size_t num_gates_pow2 = 1UL << (num_gates_log2 + (1UL << num_gates_log2 == num_gates ? 0 : 1)); | ||
|
||
AllPolynomials polys; | ||
for (auto& poly : polys.get_all()) { | ||
poly = Polynomial(num_gates_pow2); | ||
} | ||
|
||
polys.lagrange_first[0] = 1; | ||
|
||
for (size_t i = 0; i < num_gates; ++i) { | ||
// Fill out the witness polynomials | ||
polys.permutation_set_column_1[i] = wires[0][i]; | ||
polys.permutation_set_column_2[i] = wires[1][i]; | ||
polys.permutation_set_column_3[i] = wires[2][i]; | ||
polys.permutation_set_column_4[i] = wires[3][i]; | ||
polys.self_permutation_column[i] = wires[4][i]; | ||
// By default the permutation is over all rows where we place data | ||
polys.enable_tuple_set_permutation[i] = 1; | ||
// The same column permutation alternates between even and odd values | ||
polys.enable_single_column_permutation[i] = 1; | ||
polys.enable_first_set_permutation[i] = i & 1; | ||
polys.enable_second_set_permutation[i] = 1 - (i & 1); | ||
} | ||
return polys; | ||
} | ||
|
||
/** | ||
* @brief Check that the circuit is correct (proof should work) | ||
* | ||
*/ | ||
bool check_circuit() | ||
{ | ||
// using FirstPermutationRelation = typename std::tuple_element_t<0, Flavor::Relations>; | ||
// For now only gamma and beta are used | ||
const FF gamma = FF::random_element(); | ||
const FF beta = FF::random_element(); | ||
proof_system::RelationParameters<typename Flavor::FF> params{ | ||
.eta = 0, | ||
.beta = beta, | ||
.gamma = gamma, | ||
.public_input_delta = 0, | ||
.lookup_grand_product_delta = 0, | ||
.beta_sqr = 0, | ||
.beta_cube = 0, | ||
.eccvm_set_permutation_delta = 0, | ||
}; | ||
|
||
// Compute polynomial values | ||
auto polynomials = compute_polynomials(); | ||
const size_t num_rows = polynomials.get_polynomial_size(); | ||
|
||
// Check the tuple permutation relation | ||
proof_system::honk::logderivative_library::compute_logderivative_inverse< | ||
Flavor, | ||
honk::sumcheck::GenericPermutationRelation<honk::sumcheck::ExampleTuplePermutationSettings, FF>>( | ||
polynomials, params, num_rows); | ||
|
||
using PermutationRelation = | ||
honk::sumcheck::GenericPermutationRelation<honk::sumcheck::ExampleTuplePermutationSettings, FF>; | ||
typename honk::sumcheck::GenericPermutationRelation<honk::sumcheck::ExampleTuplePermutationSettings, | ||
typename Flavor::FF>::SumcheckArrayOfValuesOverSubrelations | ||
permutation_result; | ||
for (auto& r : permutation_result) { | ||
r = 0; | ||
} | ||
for (size_t i = 0; i < num_rows; ++i) { | ||
PermutationRelation::accumulate(permutation_result, polynomials.get_row(i), params, 1); | ||
} | ||
for (auto r : permutation_result) { | ||
if (r != 0) { | ||
info("Tuple GenericPermutationRelation failed."); | ||
return false; | ||
} | ||
} | ||
// Check the single permutation relation | ||
proof_system::honk::logderivative_library::compute_logderivative_inverse< | ||
Flavor, | ||
honk::sumcheck::GenericPermutationRelation<honk::sumcheck::ExampleSameWirePermutationSettings, FF>>( | ||
polynomials, params, num_rows); | ||
|
||
using SameWirePermutationRelation = | ||
honk::sumcheck::GenericPermutationRelation<honk::sumcheck::ExampleSameWirePermutationSettings, FF>; | ||
typename honk::sumcheck::GenericPermutationRelation<honk::sumcheck::ExampleSameWirePermutationSettings, | ||
typename Flavor::FF>::SumcheckArrayOfValuesOverSubrelations | ||
second_permutation_result; | ||
for (auto& r : second_permutation_result) { | ||
r = 0; | ||
} | ||
for (size_t i = 0; i < num_rows; ++i) { | ||
SameWirePermutationRelation::accumulate(second_permutation_result, polynomials.get_row(i), params, 1); | ||
} | ||
for (auto r : second_permutation_result) { | ||
if (r != 0) { | ||
info("Same wire GenericPermutationRelation failed."); | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
[[nodiscard]] size_t get_num_gates() const { return num_gates; } | ||
|
||
[[nodiscard]] size_t get_circuit_subgroup_size(const size_t num_rows) const | ||
{ | ||
|
||
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)); | ||
return num_rows_pow2; | ||
} | ||
}; | ||
} // namespace proof_system |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you make an issue to change this? I suspect we'll need to have more exotic lookups to fully support the AVM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just for permutations