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

feat: pil lookups w/ xor table example #3880

Merged
merged 9 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
34 changes: 31 additions & 3 deletions barretenberg/cpp/pil/avm/toy_avm.pil
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,36 @@ namespace toy(256);
q_tuple_set { set_1_column_1, set_1_column_2 } is { set_2_column_1, set_2_column_2 };

// Relation not used -> we currently require a single relation for codegen
pol commit x;
x' - x = 0;
q_tuple_set * (1 - q_tuple_set) = 0;

// Also needs a fixed relation
pol fixed first = [1] + [0]*;
pol fixed first = [1] + [0]*;

// Lookup related stuff

// For each xor term we need:
// - The witness wire it is over
// - The column being lookuped
// - A shift of the column being lookuped
// - An accumulator for each of the tables

// constraint wires
pol commit xor_a;
pol commit xor_b;
pol commit xor_c;

// Precomputed tables
pol commit table_xor_a;
pol commit table_xor_b;
pol commit table_xor_c;

pol commit q_xor;
pol commit q_xor_table;

q_xor * (1 - q_xor) = 0;
q_xor_table * (1 - q_xor_table) = 0;

// Note - if no right hand side selector column is provided, then we will need to build the table ourselves
// Note - we can also take advantage of pil creating the lookup columns for us here -> I may be able to do some codegen here !
#[lookup_xor]
q_xor { xor_a, xor_b, xor_c } in q_xor_table { table_xor_a, table_xor_b, table_xor_c };
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class AvmMiniFlavor {
// the unshifted and one for the shifted
static constexpr size_t NUM_ALL_ENTITIES = 46;

using Relations = std::tuple<AvmMini_vm::mem_trace<FF>, AvmMini_vm::avm_mini<FF>>;
using Relations = std::tuple<AvmMini_vm::avm_mini<FF>, AvmMini_vm::mem_trace<FF>>;

static constexpr size_t MAX_PARTIAL_RELATION_LENGTH = compute_max_partial_relation_length<Relations>();

Expand Down Expand Up @@ -202,12 +202,12 @@ class AvmMiniFlavor {
avmMini_mem_idx_b,
avmMini_mem_idx_c,
avmMini_last,
memTrace_m_val_shift,
avmMini_internal_return_ptr_shift,
avmMini_pc_shift,
memTrace_m_addr_shift,
memTrace_m_tag_shift,
memTrace_m_rw_shift,
avmMini_internal_return_ptr_shift,
avmMini_pc_shift)
memTrace_m_val_shift,
memTrace_m_rw_shift)

RefVector<DataType> get_wires()
{
Expand Down Expand Up @@ -251,12 +251,12 @@ class AvmMiniFlavor {
avmMini_mem_idx_b,
avmMini_mem_idx_c,
avmMini_last,
memTrace_m_val_shift,
avmMini_internal_return_ptr_shift,
avmMini_pc_shift,
memTrace_m_addr_shift,
memTrace_m_tag_shift,
memTrace_m_rw_shift,
avmMini_internal_return_ptr_shift,
avmMini_pc_shift };
memTrace_m_val_shift,
memTrace_m_rw_shift };
};
RefVector<DataType> get_unshifted()
{
Expand Down Expand Up @@ -303,17 +303,18 @@ class AvmMiniFlavor {
};
RefVector<DataType> get_to_be_shifted()
{
return { memTrace_m_val, memTrace_m_addr, memTrace_m_tag, memTrace_m_rw, avmMini_internal_return_ptr,
avmMini_pc };
return {
avmMini_internal_return_ptr, avmMini_pc, memTrace_m_addr, memTrace_m_tag, memTrace_m_val, memTrace_m_rw
};
};
RefVector<DataType> get_shifted()
{
return { memTrace_m_val_shift,
return { avmMini_internal_return_ptr_shift,
avmMini_pc_shift,
memTrace_m_addr_shift,
memTrace_m_tag_shift,
memTrace_m_rw_shift,
avmMini_internal_return_ptr_shift,
avmMini_pc_shift };
memTrace_m_val_shift,
memTrace_m_rw_shift };
};
};

Expand All @@ -326,8 +327,9 @@ class AvmMiniFlavor {

RefVector<DataType> get_to_be_shifted()
{
return { memTrace_m_val, memTrace_m_addr, memTrace_m_tag, memTrace_m_rw, avmMini_internal_return_ptr,
avmMini_pc };
return {
avmMini_internal_return_ptr, avmMini_pc, memTrace_m_addr, memTrace_m_tag, memTrace_m_val, memTrace_m_rw
};
};

// The plookup wires that store plookup read data.
Expand Down
99 changes: 80 additions & 19 deletions barretenberg/cpp/src/barretenberg/flavor/generated/Toy_flavor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ class ToyFlavor {
using VerifierCommitmentKey = pcs::VerifierCommitmentKey<Curve>;

static constexpr size_t NUM_PRECOMPUTED_ENTITIES = 1;
static constexpr size_t NUM_WITNESS_ENTITIES = 7;
static constexpr size_t NUM_WITNESS_ENTITIES = 16;
static constexpr size_t NUM_WIRES = NUM_WITNESS_ENTITIES + NUM_PRECOMPUTED_ENTITIES;
// We have two copies of the witness entities, so we subtract the number of fixed ones (they have no shift), one for
// the unshifted and one for the shifted
static constexpr size_t NUM_ALL_ENTITIES = 9;
static constexpr size_t NUM_ALL_ENTITIES = 17;

using Relations = std::tuple<Toy_vm::toy_avm<FF>, sumcheck::two_column_perm_relation<FF>>;

Expand Down Expand Up @@ -81,13 +81,24 @@ class ToyFlavor {
toy_set_1_column_2,
toy_set_2_column_1,
toy_set_2_column_2,
toy_x,
two_column_perm)
toy_xor_a,
toy_xor_b,
toy_xor_c,
toy_table_xor_a,
toy_table_xor_b,
toy_table_xor_c,
toy_q_xor,
toy_q_xor_table,
two_column_perm,
lookup_xor,
lookup_xor_counts)

RefVector<DataType> get_wires()
{
return { toy_q_tuple_set, toy_set_1_column_1, toy_set_1_column_2, toy_set_2_column_1, toy_set_2_column_2,
toy_x, two_column_perm };
return { toy_q_tuple_set, toy_set_1_column_1, toy_set_1_column_2, toy_set_2_column_1,
toy_set_2_column_2, toy_xor_a, toy_xor_b, toy_xor_c,
toy_table_xor_a, toy_table_xor_b, toy_table_xor_c, toy_q_xor,
toy_q_xor_table, two_column_perm, lookup_xor, lookup_xor_counts };
};
RefVector<DataType> get_sorted_polynomials() { return {}; };
};
Expand All @@ -101,22 +112,34 @@ class ToyFlavor {
toy_set_1_column_2,
toy_set_2_column_1,
toy_set_2_column_2,
toy_x,
toy_xor_a,
toy_xor_b,
toy_xor_c,
toy_table_xor_a,
toy_table_xor_b,
toy_table_xor_c,
toy_q_xor,
toy_q_xor_table,
two_column_perm,
toy_x_shift)
lookup_xor,
lookup_xor_counts)

RefVector<DataType> get_wires()
{
return { toy_first, toy_q_tuple_set, toy_set_1_column_1, toy_set_1_column_2, toy_set_2_column_1,
toy_set_2_column_2, toy_x, two_column_perm, toy_x_shift };
return { toy_first, toy_q_tuple_set, toy_set_1_column_1, toy_set_1_column_2, toy_set_2_column_1,
toy_set_2_column_2, toy_xor_a, toy_xor_b, toy_xor_c, toy_table_xor_a,
toy_table_xor_b, toy_table_xor_c, toy_q_xor, toy_q_xor_table, two_column_perm,
lookup_xor, lookup_xor_counts };
};
RefVector<DataType> get_unshifted()
{
return { toy_first, toy_q_tuple_set, toy_set_1_column_1, toy_set_1_column_2, toy_set_2_column_1,
toy_set_2_column_2, toy_x, two_column_perm };
return { toy_first, toy_q_tuple_set, toy_set_1_column_1, toy_set_1_column_2, toy_set_2_column_1,
toy_set_2_column_2, toy_xor_a, toy_xor_b, toy_xor_c, toy_table_xor_a,
toy_table_xor_b, toy_table_xor_c, toy_q_xor, toy_q_xor_table, two_column_perm,
lookup_xor, lookup_xor_counts };
};
RefVector<DataType> get_to_be_shifted() { return { toy_x }; };
RefVector<DataType> get_shifted() { return { toy_x_shift }; };
RefVector<DataType> get_to_be_shifted() { return {}; };
RefVector<DataType> get_shifted() { return {}; };
};

public:
Expand All @@ -126,7 +149,7 @@ class ToyFlavor {
using Base = ProvingKey_<PrecomputedEntities<Polynomial>, WitnessEntities<Polynomial>>;
using Base::Base;

RefVector<DataType> get_to_be_shifted() { return { toy_x }; };
RefVector<DataType> get_to_be_shifted() { return {}; };

// The plookup wires that store plookup read data.
std::array<PolynomialHandle, 0> get_table_column_wires() { return {}; };
Expand Down Expand Up @@ -169,6 +192,8 @@ class ToyFlavor {
}
};

using RowPolynomials = AllEntities<FF>;

class PartiallyEvaluatedMultivariates : public AllEntities<Polynomial> {
public:
PartiallyEvaluatedMultivariates() = default;
Expand Down Expand Up @@ -206,8 +231,17 @@ class ToyFlavor {
Base::toy_set_1_column_2 = "TOY_SET_1_COLUMN_2";
Base::toy_set_2_column_1 = "TOY_SET_2_COLUMN_1";
Base::toy_set_2_column_2 = "TOY_SET_2_COLUMN_2";
Base::toy_x = "TOY_X";
Base::toy_xor_a = "TOY_XOR_A";
Base::toy_xor_b = "TOY_XOR_B";
Base::toy_xor_c = "TOY_XOR_C";
Base::toy_table_xor_a = "TOY_TABLE_XOR_A";
Base::toy_table_xor_b = "TOY_TABLE_XOR_B";
Base::toy_table_xor_c = "TOY_TABLE_XOR_C";
Base::toy_q_xor = "TOY_Q_XOR";
Base::toy_q_xor_table = "TOY_Q_XOR_TABLE";
Base::two_column_perm = "TWO_COLUMN_PERM";
Base::lookup_xor = "LOOKUP_XOR";
Base::lookup_xor_counts = "LOOKUP_XOR_COUNTS";
};
};

Expand All @@ -231,8 +265,17 @@ class ToyFlavor {
Commitment toy_set_1_column_2;
Commitment toy_set_2_column_1;
Commitment toy_set_2_column_2;
Commitment toy_x;
Commitment toy_xor_a;
Commitment toy_xor_b;
Commitment toy_xor_c;
Commitment toy_table_xor_a;
Commitment toy_table_xor_b;
Commitment toy_table_xor_c;
Commitment toy_q_xor;
Commitment toy_q_xor_table;
Commitment two_column_perm;
Commitment lookup_xor;
Commitment lookup_xor_counts;

std::vector<barretenberg::Univariate<FF, BATCHED_RELATION_PARTIAL_LENGTH>> sumcheck_univariates;
std::array<FF, NUM_ALL_ENTITIES> sumcheck_evaluations;
Expand All @@ -257,8 +300,17 @@ class ToyFlavor {
toy_set_1_column_2 = deserialize_from_buffer<Commitment>(Transcript::proof_data, num_bytes_read);
toy_set_2_column_1 = deserialize_from_buffer<Commitment>(Transcript::proof_data, num_bytes_read);
toy_set_2_column_2 = deserialize_from_buffer<Commitment>(Transcript::proof_data, num_bytes_read);
toy_x = deserialize_from_buffer<Commitment>(Transcript::proof_data, num_bytes_read);
toy_xor_a = deserialize_from_buffer<Commitment>(Transcript::proof_data, num_bytes_read);
toy_xor_b = deserialize_from_buffer<Commitment>(Transcript::proof_data, num_bytes_read);
toy_xor_c = deserialize_from_buffer<Commitment>(Transcript::proof_data, num_bytes_read);
toy_table_xor_a = deserialize_from_buffer<Commitment>(Transcript::proof_data, num_bytes_read);
toy_table_xor_b = deserialize_from_buffer<Commitment>(Transcript::proof_data, num_bytes_read);
toy_table_xor_c = deserialize_from_buffer<Commitment>(Transcript::proof_data, num_bytes_read);
toy_q_xor = deserialize_from_buffer<Commitment>(Transcript::proof_data, num_bytes_read);
toy_q_xor_table = deserialize_from_buffer<Commitment>(Transcript::proof_data, num_bytes_read);
two_column_perm = deserialize_from_buffer<Commitment>(Transcript::proof_data, num_bytes_read);
lookup_xor = deserialize_from_buffer<Commitment>(Transcript::proof_data, num_bytes_read);
lookup_xor_counts = deserialize_from_buffer<Commitment>(Transcript::proof_data, num_bytes_read);

for (size_t i = 0; i < log_n; ++i) {
sumcheck_univariates.emplace_back(
Expand Down Expand Up @@ -287,8 +339,17 @@ class ToyFlavor {
serialize_to_buffer<Commitment>(toy_set_1_column_2, Transcript::proof_data);
serialize_to_buffer<Commitment>(toy_set_2_column_1, Transcript::proof_data);
serialize_to_buffer<Commitment>(toy_set_2_column_2, Transcript::proof_data);
serialize_to_buffer<Commitment>(toy_x, Transcript::proof_data);
serialize_to_buffer<Commitment>(toy_xor_a, Transcript::proof_data);
serialize_to_buffer<Commitment>(toy_xor_b, Transcript::proof_data);
serialize_to_buffer<Commitment>(toy_xor_c, Transcript::proof_data);
serialize_to_buffer<Commitment>(toy_table_xor_a, Transcript::proof_data);
serialize_to_buffer<Commitment>(toy_table_xor_b, Transcript::proof_data);
serialize_to_buffer<Commitment>(toy_table_xor_c, Transcript::proof_data);
serialize_to_buffer<Commitment>(toy_q_xor, Transcript::proof_data);
serialize_to_buffer<Commitment>(toy_q_xor_table, Transcript::proof_data);
serialize_to_buffer<Commitment>(two_column_perm, Transcript::proof_data);
serialize_to_buffer<Commitment>(lookup_xor, Transcript::proof_data);
serialize_to_buffer<Commitment>(lookup_xor_counts, Transcript::proof_data);

for (size_t i = 0; i < log_n; ++i) {
serialize_to_buffer(sumcheck_univariates[i], Transcript::proof_data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ void compute_logderivative_inverse(Polynomials& polynomials, auto& relation_para
constexpr size_t WRITE_TERMS = Relation::WRITE_TERMS;

auto lookup_relation = Relation();

// SELF: we get the inverse polynomial from the lookup relation template
// - > The numerator in any of these will be 1, therefore we have to make an addition to the denominator for the row
// -> Rather what we do is add all of the denominators in the inverse polynomial, then we do a batch inversion so
// that the whole polynomial consists of inversions QUESTION: the doc comment seems to imply that the inverse is
Maddiaa0 marked this conversation as resolved.
Show resolved Hide resolved
// really a PRODUCT of some set of inverse (∏ from this icon) ANS: It IS ! we perform the denominator accumulation
// for EACH of our READ TERMS
// - Are these read terms each of the columns in the lookup?
// for EACH of our WRITE TERMS
// - Are these write terms each of the columns the contribute to the lookuped columns?

// I am not 100% sure how to contribute to the table vs how to read from the table
auto& inverse_polynomial = lookup_relation.template get_inverse_polynomial(polynomials);
for (size_t i = 0; i < circuit_size; ++i) {
auto row = polynomials.get_row(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "barretenberg/ecc/curves/bn254/fr.hpp"
#include "barretenberg/honk/proof_system/logderivative_library.hpp"
#include "barretenberg/proof_system/circuit_builder/circuit_builder_base.hpp"
#include "barretenberg/relations/generic_lookup/generic_lookup_relation.hpp"
#include "barretenberg/relations/generic_permutation/generic_permutation_relation.hpp"

#include "barretenberg/flavor/generated/AvmMini_flavor.hpp"
Expand Down Expand Up @@ -59,12 +60,12 @@ template <typename FF> struct AvmMiniFullRow {
FF avmMini_mem_idx_b{};
FF avmMini_mem_idx_c{};
FF avmMini_last{};
FF memTrace_m_val_shift{};
FF avmMini_internal_return_ptr_shift{};
FF avmMini_pc_shift{};
FF memTrace_m_addr_shift{};
FF memTrace_m_tag_shift{};
FF memTrace_m_val_shift{};
FF memTrace_m_rw_shift{};
FF avmMini_internal_return_ptr_shift{};
FF avmMini_pc_shift{};
};

class AvmMiniCircuitBuilder {
Expand Down Expand Up @@ -136,12 +137,12 @@ class AvmMiniCircuitBuilder {
polys.avmMini_last[i] = rows[i].avmMini_last;
}

polys.memTrace_m_val_shift = Polynomial(polys.memTrace_m_val.shifted());
polys.avmMini_internal_return_ptr_shift = Polynomial(polys.avmMini_internal_return_ptr.shifted());
polys.avmMini_pc_shift = Polynomial(polys.avmMini_pc.shifted());
polys.memTrace_m_addr_shift = Polynomial(polys.memTrace_m_addr.shifted());
polys.memTrace_m_tag_shift = Polynomial(polys.memTrace_m_tag.shifted());
polys.memTrace_m_val_shift = Polynomial(polys.memTrace_m_val.shifted());
polys.memTrace_m_rw_shift = Polynomial(polys.memTrace_m_rw.shifted());
polys.avmMini_internal_return_ptr_shift = Polynomial(polys.avmMini_internal_return_ptr.shifted());
polys.avmMini_pc_shift = Polynomial(polys.avmMini_pc.shifted());

return polys;
}
Expand Down Expand Up @@ -179,14 +180,14 @@ class AvmMiniCircuitBuilder {
return true;
};

if (!evaluate_relation.template operator()<AvmMini_vm::mem_trace<FF>>(
"mem_trace", AvmMini_vm::get_relation_label_mem_trace)) {
return false;
}
if (!evaluate_relation.template operator()<AvmMini_vm::avm_mini<FF>>("avm_mini",
AvmMini_vm::get_relation_label_avm_mini)) {
return false;
}
if (!evaluate_relation.template operator()<AvmMini_vm::mem_trace<FF>>(
"mem_trace", AvmMini_vm::get_relation_label_mem_trace)) {
return false;
}

return true;
}
Expand Down
Loading