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: bb logup proving support #54

Merged
merged 6 commits into from
Apr 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
9 changes: 6 additions & 3 deletions bberg/src/circuit_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ pub trait CircuitBuilder {
name: &str,
relations: &[String],
permutations: &[String],
fixed: &[String],
shifted: &[String],
all_cols_without_inverses: &[String],
all_cols: &[String],
to_be_shifted: &[String],
all_cols_with_shifts: &[String],
);
}
Expand Down Expand Up @@ -62,6 +63,7 @@ impl CircuitBuilder for BBFiles {
name: &str,
relations: &[String],
permutations: &[String],
all_cols_without_inverses: &[String],
all_cols: &[String],
to_be_shifted: &[String],
all_cols_with_shifts: &[String],
Expand Down Expand Up @@ -97,7 +99,8 @@ impl CircuitBuilder for BBFiles {
};

// Apply transformations
let compute_polys_assignemnt = map_with_newline(all_cols, compute_polys_transformation);
let compute_polys_assignemnt =
map_with_newline(all_cols_without_inverses, compute_polys_transformation);
let all_poly_shifts = map_with_newline(to_be_shifted, all_polys_transformation);
let check_circuit_for_each_relation =
map_with_newline(relations, check_circuit_transformation);
Expand Down
4 changes: 2 additions & 2 deletions bberg/src/composer_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void {name}Composer::compute_witness(CircuitConstructor& circuit)
compute_witness(circuit_constructor);
compute_commitment_key(circuit_constructor.get_circuit_subgroup_size());

{name}Prover output_state(proving_key, commitment_key);
{name}Prover output_state(proving_key, proving_key->commitment_key);

return output_state;
}}
Expand Down Expand Up @@ -167,7 +167,7 @@ class {name}Composer {{

void compute_commitment_key(size_t circuit_size)
{{
commitment_key = std::make_shared<CommitmentKey>(circuit_size);
proving_key->commitment_key = std::make_shared<CommitmentKey>(circuit_size);
}};
}};

Expand Down
110 changes: 100 additions & 10 deletions bberg/src/flavor_builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
file_writer::BBFiles,
lookup_builder::{get_inverses_from_lookups, Lookup},
permutation_builder::{get_inverses_from_permutations, Permutation},
utils::{get_relations_imports, map_with_newline, snake_case},
};
Expand All @@ -11,6 +12,7 @@ pub trait FlavorBuilder {
name: &str,
relation_file_names: &[String],
permutations: &[Permutation],
lookups: &[Lookup],
fixed: &[String],
witness: &[String],
all_cols: &[String],
Expand All @@ -27,6 +29,7 @@ impl FlavorBuilder for BBFiles {
name: &str,
relation_file_names: &[String],
permutations: &[Permutation],
lookups: &[Lookup],
fixed: &[String],
witness: &[String],
all_cols: &[String],
Expand All @@ -35,7 +38,15 @@ impl FlavorBuilder for BBFiles {
all_cols_and_shifts: &[String],
) {
// TODO: move elsewhere and rename
Maddiaa0 marked this conversation as resolved.
Show resolved Hide resolved
let inverses = get_inverses_from_permutations(permutations);
let permutation_inverses = get_inverses_from_permutations(permutations);
let lookup_inverses = get_inverses_from_lookups(lookups);

// Inverses from both permutations and lookups
let inverses: Vec<String> = permutation_inverses
.iter()
.chain(lookup_inverses.iter())
.cloned()
.collect();

let first_poly = &witness[0];
let includes = flavor_includes(&snake_case(name), relation_file_names, &inverses);
Expand All @@ -46,7 +57,7 @@ impl FlavorBuilder for BBFiles {
// Top of file boilerplate
let class_aliases = create_class_aliases();
let relation_definitions =
create_relation_definitions(name, relation_file_names, permutations);
create_relation_definitions(name, relation_file_names, permutations, lookups);
let container_size_definitions =
container_size_definitions(num_precomputed, num_witness, num_all);

Expand All @@ -56,7 +67,8 @@ impl FlavorBuilder for BBFiles {
let all_entities =
create_all_entities(all_cols, to_be_shifted, shifted, all_cols_and_shifts);

let proving_and_verification_key = create_proving_and_verification_key(to_be_shifted);
let proving_and_verification_key =
create_proving_and_verification_key(name, permutations, lookups, to_be_shifted);
let polynomial_views = create_polynomial_views(first_poly);

let commitment_labels_class = create_commitment_labels(all_cols);
Expand Down Expand Up @@ -159,6 +171,14 @@ fn create_permutations_tuple(permutations: &[Permutation]) -> String {
.join(", ")
}

fn create_lookups_tuple(lookups: &[Lookup]) -> String {
lookups
.iter()
.map(|lookup| format!("{}_relation<FF>", lookup.attribute.clone().unwrap()))
.collect::<Vec<_>>()
.join(", ")
}

/// Create Class Aliases
///
/// Contains boilerplate defining key characteristics of the flavor class
Expand Down Expand Up @@ -191,16 +211,29 @@ fn create_relation_definitions(
name: &str,
relation_file_names: &[String],
permutations: &[Permutation],
lookups: &[Lookup],
) -> String {
// Relations tuple = ns::relation_name_0, ns::relation_name_1, ... ns::relation_name_n (comma speratated)
let comma_sep_relations = create_relations_tuple(name, relation_file_names);
let comma_sep_perms: String = create_permutations_tuple(permutations);
let comma_sep_lookups: String = create_lookups_tuple(lookups);

let mut grand_product_relations = String::new();
let mut all_relations = comma_sep_relations.to_string();
if !permutations.is_empty() {
all_relations = all_relations + &format!(", {comma_sep_perms}");
grand_product_relations = grand_product_relations + &comma_sep_perms.to_string();
}

if !lookups.is_empty() {
all_relations = all_relations + &format!(", {comma_sep_lookups}");
grand_product_relations =
grand_product_relations.to_owned() + &format!(", {comma_sep_lookups}");
}

format!("
using GrandProductRelations = std::tuple<{grand_product_relations}>;

using Relations = std::tuple<{all_relations}>;

static constexpr size_t MAX_PARTIAL_RELATION_LENGTH = compute_max_partial_relation_length<Relations>();
Expand All @@ -209,7 +242,7 @@ fn create_relation_definitions(
// random polynomial e.g. For \\sum(x) [A(x) * B(x) + C(x)] * PowZeta(X), relation length = 2 and random relation
// length = 3
static constexpr size_t BATCHED_RELATION_PARTIAL_LENGTH = MAX_PARTIAL_RELATION_LENGTH + 1;
static constexpr size_t NUM_RELATIONS = std::tuple_size<Relations>::value;
static constexpr size_t NUM_RELATIONS = std::tuple_size_v<Relations>;

template <size_t NUM_INSTANCES>
using ProtogalaxyTupleOfTuplesOfUnivariates =
Expand Down Expand Up @@ -284,7 +317,6 @@ fn create_witness_entities(witness: &[String]) -> String {
let pointer_view = create_flavor_members(witness);

let wires = return_ref_vector("get_wires", witness);
let sorted_polys = return_ref_vector("get_sorted_polynomials", &[]);

format!(
"
Expand All @@ -295,7 +327,6 @@ fn create_witness_entities(witness: &[String]) -> String {
{pointer_view}

{wires}
{sorted_polys}
}};
"
)
Expand Down Expand Up @@ -333,8 +364,15 @@ fn create_all_entities(
)
}

fn create_proving_and_verification_key(to_be_shifted: &[String]) -> String {
fn create_proving_and_verification_key(
flavor_name: &str,
permutations: &[Permutation],
lookups: &[Lookup],
to_be_shifted: &[String],
) -> String {
let get_to_be_shifted = return_ref_vector("get_to_be_shifted", to_be_shifted);
let compute_logderivative_inverses =
create_compute_logderivative_inverses(flavor_name, permutations, lookups);

format!("
public:
Expand All @@ -346,8 +384,7 @@ fn create_proving_and_verification_key(to_be_shifted: &[String]) -> String {

{get_to_be_shifted}

// The plookup wires that store plookup read data.
std::array<PolynomialHandle, 0> get_table_column_wires() {{ return {{}}; }};
{compute_logderivative_inverses}
}};

using VerificationKey = VerificationKey_<PrecomputedEntities<Commitment>, VerifierCommitmentKey>;
Expand Down Expand Up @@ -377,6 +414,19 @@ fn create_polynomial_views(first_poly: &String) -> String {
ProverPolynomials(ProverPolynomials&& o) noexcept = default;
ProverPolynomials& operator=(ProverPolynomials&& o) noexcept = default;
~ProverPolynomials() = default;

ProverPolynomials(ProvingKey& proving_key)
{{
for (auto [prover_poly, key_poly] : zip_view(this->get_unshifted(), proving_key.get_all())) {{
ASSERT(flavor_get_label(*this, prover_poly) == flavor_get_label(proving_key, key_poly));
prover_poly = key_poly.share();
}}
for (auto [prover_poly, key_poly] : zip_view(this->get_shifted(), proving_key.get_to_be_shifted())) {{
ASSERT(flavor_get_label(*this, prover_poly) == (flavor_get_label(proving_key, key_poly) + \"_shift\"));
prover_poly = key_poly.shifted();
}}
}}

[[nodiscard]] size_t get_polynomial_size() const {{ return {first_poly}.size(); }}
/**
* @brief Returns the evaluations of all prover polynomials at one point on the boolean hypercube, which
Expand Down Expand Up @@ -419,6 +469,12 @@ fn create_polynomial_views(first_poly: &String) -> String {
*/
using ExtendedEdges = ProverUnivariates<MAX_PARTIAL_RELATION_LENGTH>;

/**
* @brief A container for the witness commitments.
*
*/
using WitnessCommitments = WitnessEntities<Commitment>;

")
}

Expand Down Expand Up @@ -452,7 +508,6 @@ fn create_commitment_labels(all_ents: &[String]) -> String {
private:
using Base = AllEntities<std::string>;


public:
CommitmentLabels() : AllEntities<std::string>()
{{
Expand All @@ -463,6 +518,41 @@ fn create_commitment_labels(all_ents: &[String]) -> String {
)
}

fn create_compute_logderivative_inverses(
flavor_name: &str,
permutations: &[Permutation],
lookups: &[Lookup],
) -> String {
let mut all_perm_and_lookups = Vec::new();
all_perm_and_lookups.extend(
permutations
.iter()
.map(|perm| perm.attribute.clone().unwrap()),
);
all_perm_and_lookups.extend(
lookups
.iter()
.map(|lookup| lookup.attribute.clone().unwrap()),
);

let compute_inverse_transformation = |lookup_name: &String| {
format!("bb::compute_logderivative_inverse<{flavor_name}Flavor, {lookup_name}_relation<FF>>(prover_polynomials, relation_parameters, this->circuit_size);")
};

let compute_inverses = map_with_newline(&all_perm_and_lookups, compute_inverse_transformation);

format!(
"
void compute_logderivative_inverses(const RelationParameters<FF>& relation_parameters)
{{
ProverPolynomials prover_polynomials = ProverPolynomials(*this);

{compute_inverses}
}}
"
)
}

fn create_key_dereference(fixed: &[String]) -> String {
let deref_transformation = |name: &String| format!("{name} = verification_key->{name};");

Expand Down
2 changes: 1 addition & 1 deletion bberg/src/lookup_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ fn create_lookup_settings_file(lookup: &Lookup) -> String {
let lookup_tuple_size = columns_per_set;

// NOTE: hardcoded until optimizations required
let inverse_degree = 2;
let inverse_degree = 4;
let read_term_degree = 0;
let write_term_degree = 0;
let read_term_types = "{0}";
Expand Down
Loading
Loading