Skip to content

Commit

Permalink
feat: bb logup proving support (#54)
Browse files Browse the repository at this point in the history
* feat: circuit builder does not need to compute inverses, prover does so

* fix: integration cleanup

* chore: sweep

* chore: clippy and fmt
  • Loading branch information
Maddiaa0 authored Apr 9, 2024
1 parent 10267b9 commit 133cb68
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 65 deletions.
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
106 changes: 76 additions & 30 deletions bberg/src/flavor_builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
file_writer::BBFiles,
permutation_builder::{get_inverses_from_permutations, Permutation},
utils::{get_relations_imports, map_with_newline, snake_case},
};

Expand All @@ -10,7 +9,7 @@ pub trait FlavorBuilder {
&mut self,
name: &str,
relation_file_names: &[String],
permutations: &[Permutation],
lookups: &[String],
fixed: &[String],
witness: &[String],
all_cols: &[String],
Expand All @@ -26,27 +25,23 @@ impl FlavorBuilder for BBFiles {
&mut self,
name: &str,
relation_file_names: &[String],
permutations: &[Permutation],
lookups: &[String],
fixed: &[String],
witness: &[String],
all_cols: &[String],
to_be_shifted: &[String],
shifted: &[String],
all_cols_and_shifts: &[String],
) {
// TODO: move elsewhere and rename
let inverses = get_inverses_from_permutations(permutations);

let first_poly = &witness[0];
let includes = flavor_includes(&snake_case(name), relation_file_names, &inverses);
let includes = flavor_includes(&snake_case(name), relation_file_names, lookups);
let num_precomputed = fixed.len();
let num_witness = witness.len();
let num_all = all_cols_and_shifts.len();

// Top of file boilerplate
let class_aliases = create_class_aliases();
let relation_definitions =
create_relation_definitions(name, relation_file_names, permutations);
let relation_definitions = create_relation_definitions(name, relation_file_names, lookups);
let container_size_definitions =
container_size_definitions(num_precomputed, num_witness, num_all);

Expand All @@ -56,7 +51,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, 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 @@ -116,8 +112,8 @@ class {name}Flavor {{
}

/// Imports located at the top of the flavor files
fn flavor_includes(name: &str, relation_file_names: &[String], permutations: &[String]) -> String {
let relation_imports = get_relations_imports(name, relation_file_names, permutations);
fn flavor_includes(name: &str, relation_file_names: &[String], lookups: &[String]) -> String {
let relation_imports = get_relations_imports(name, relation_file_names, lookups);

format!(
"
Expand Down Expand Up @@ -150,13 +146,17 @@ fn create_relations_tuple(master_name: &str, relation_file_names: &[String]) ->
}

/// Creates comma separated relations tuple file
/// TODO(md): maybe need the filename in here too if we scope these
fn create_permutations_tuple(permutations: &[Permutation]) -> String {
permutations
.iter()
.map(|perm| format!("{}_relation<FF>", perm.attribute.clone().unwrap()))
.collect::<Vec<_>>()
.join(", ")
fn create_lookups_tuple(lookups: &[String]) -> Option<String> {
if lookups.is_empty() {
return None;
}
Some(
lookups
.iter()
.map(|lookup| format!("{}_relation<FF>", lookup.clone()))
.collect::<Vec<_>>()
.join(", "),
)
}

/// Create Class Aliases
Expand Down Expand Up @@ -190,17 +190,23 @@ fn create_class_aliases() -> &'static str {
fn create_relation_definitions(
name: &str,
relation_file_names: &[String],
permutations: &[Permutation],
lookups: &[String],
) -> 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: Option<String> = create_lookups_tuple(lookups);

// We only include the grand product relations if we are given 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}");
if let Some(lookups) = comma_sep_lookups {
all_relations = all_relations + &format!(", {lookups}");
grand_product_relations = format!("using GrandProductRelations = std::tuple<{lookups}>;");
}

format!("
{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 +215,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 +290,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 +300,6 @@ fn create_witness_entities(witness: &[String]) -> String {
{pointer_view}
{wires}
{sorted_polys}
}};
"
)
Expand Down Expand Up @@ -333,8 +337,14 @@ fn create_all_entities(
)
}

fn create_proving_and_verification_key(to_be_shifted: &[String]) -> String {
fn create_proving_and_verification_key(
flavor_name: &str,
lookups: &[String],
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, lookups);

format!("
public:
Expand All @@ -346,8 +356,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 +386,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 +441,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 +480,6 @@ fn create_commitment_labels(all_ents: &[String]) -> String {
private:
using Base = AllEntities<std::string>;
public:
CommitmentLabels() : AllEntities<std::string>()
{{
Expand All @@ -463,6 +490,25 @@ fn create_commitment_labels(all_ents: &[String]) -> String {
)
}

fn create_compute_logderivative_inverses(flavor_name: &str, lookups: &[String]) -> String {
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(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

0 comments on commit 133cb68

Please sign in to comment.