Skip to content

Commit

Permalink
chore(avm): less code in prover and verifier (#7302)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 authored Jul 3, 2024
1 parent ccfa69c commit f401a9a
Show file tree
Hide file tree
Showing 7 changed files with 332 additions and 3,271 deletions.
2,131 changes: 212 additions & 1,919 deletions barretenberg/cpp/src/barretenberg/vm/generated/avm_flavor.hpp

Large diffs are not rendered by default.

761 changes: 5 additions & 756 deletions barretenberg/cpp/src/barretenberg/vm/generated/avm_prover.cpp

Large diffs are not rendered by default.

534 changes: 3 additions & 531 deletions barretenberg/cpp/src/barretenberg/vm/generated/avm_verifier.cpp

Large diffs are not rendered by default.

128 changes: 97 additions & 31 deletions bb-pilcom/bb-pil-backend/src/flavor_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub trait FlavorBuilder {
lookups: &[String],
fixed: &[String],
witness: &[String],
witness_without_inverses: &[String],
all_cols: &[String],
to_be_shifted: &[String],
shifted: &[String],
Expand All @@ -28,6 +29,7 @@ impl FlavorBuilder for BBFiles {
lookups: &[String],
fixed: &[String],
witness: &[String],
witness_without_inverses: &[String],
all_cols: &[String],
to_be_shifted: &[String],
shifted: &[String],
Expand All @@ -47,9 +49,9 @@ impl FlavorBuilder for BBFiles {

// Entities classes
let precomputed_entities = create_precomputed_entities(fixed);
let witness_entities = create_witness_entities(witness);
let all_entities =
create_all_entities(all_cols, to_be_shifted, shifted, all_cols_and_shifts);
let witness_entities =
create_witness_entities(witness_without_inverses, lookups, shifted, to_be_shifted);
let all_entities = create_all_entities();

let proving_and_verification_key =
create_proving_and_verification_key(name, lookups, to_be_shifted);
Expand Down Expand Up @@ -197,16 +199,12 @@ fn create_relation_definitions(
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 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 Down Expand Up @@ -286,52 +284,120 @@ fn create_precomputed_entities(fixed: &[String]) -> String {
)
}

fn create_witness_entities(witness: &[String]) -> String {
let pointer_view = create_flavor_members(witness);

let wires = return_ref_vector("get_wires", witness);
// Note(md): this is witnesses WITHOUT inverses or shifts
fn create_wire_entities(witness: &[String]) -> String {
let flavor_members = create_flavor_members(witness);

format!(
"
template <typename DataType>
class WitnessEntities {{
class WireEntities {{
public:
{flavor_members}
}};
"
)
}

{pointer_view}
// Note(md): this is witnesses and in future grand products
fn create_derived_witnesses(inverses: &[String]) -> String {
let flavor_members = create_flavor_members(inverses);

{wires}
format!(
"
template <typename DataType>
struct DerivedWitnessEntities {{
{flavor_members}
}};
"
)
}

/// Creates container of all witness entities and shifts
fn create_all_entities(
all_cols: &[String],
to_be_shifted: &[String],
fn create_shifted_entities(shifted: &[String]) -> String {
let flavor_members = create_flavor_members(shifted);

format!(
"
template <typename DataType>
class ShiftedEntities {{
public:
{flavor_members}
}};
"
)
}

fn create_to_be_shifted(to_be_shifted: &[String]) -> String {
let entities_transformation = |name: &String| format!("entities.{name},");
let entities_list = map_with_newline(to_be_shifted, entities_transformation);

format!(
"
template <typename DataType, typename PrecomputedAndWitnessEntitiesSuperset>
static auto get_to_be_shifted(PrecomputedAndWitnessEntitiesSuperset& entities) {{
return RefArray{{
{entities_list}
}};
}}
"
)
}

fn create_witness_entities(
witness: &[String],
inverses: &[String],
shifted: &[String],
all_cols_and_shifts: &[String],
to_be_shifted: &[String],
) -> String {
let all_entities_flavor_members = create_flavor_members(all_cols_and_shifts);

let wires = return_ref_vector("get_wires", all_cols_and_shifts);
let get_unshifted = return_ref_vector("get_unshifted", all_cols);
let get_to_be_shifted = return_ref_vector("get_to_be_shifted", to_be_shifted);
let get_shifted = return_ref_vector("get_shifted", shifted);
let wire_entities = create_wire_entities(witness);
let derived_witnesses = create_derived_witnesses(inverses);
let shifted_entities = create_shifted_entities(shifted);
let to_be_shifted = create_to_be_shifted(to_be_shifted);

format!(
"
{wire_entities}
{derived_witnesses}
{shifted_entities}
{to_be_shifted}
template <typename DataType>
class AllEntities {{
class WitnessEntities: public WireEntities<DataType>, public DerivedWitnessEntities<DataType> {{
public:
DEFINE_COMPOUND_GET_ALL(WireEntities<DataType>, DerivedWitnessEntities<DataType>)
auto get_wires() {{ return WireEntities<DataType>::get_all(); }};
}};
"
)
}

{all_entities_flavor_members}
/// Creates container of all witness entities and shifts
fn create_all_entities() -> String {
format!(
"
template <typename DataType>
class AllEntities: public PrecomputedEntities<DataType>,
public WitnessEntities<DataType>,
public ShiftedEntities<DataType> {{
public:
AllEntities()
: PrecomputedEntities<DataType>{{}}
, WitnessEntities<DataType>{{}}
, ShiftedEntities<DataType>{{}}
{{}}
DEFINE_COMPOUND_GET_ALL(PrecomputedEntities<DataType>, WitnessEntities<DataType>, ShiftedEntities<DataType>)
{wires}
{get_unshifted}
{get_to_be_shifted}
{get_shifted}
auto get_unshifted(){{
return concatenate(PrecomputedEntities<DataType>::get_all(), WitnessEntities<DataType>::get_all());
}}
auto get_to_be_shifted(){{ return AvmFlavor::get_to_be_shifted<DataType>(*this); }}
auto get_shifted() {{ return ShiftedEntities<DataType>::get_all(); }}
auto get_precomputed() {{ return PrecomputedEntities<DataType>::get_all(); }}
}};
"
)
Expand Down
31 changes: 9 additions & 22 deletions bb-pilcom/bb-pil-backend/src/prover_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ use crate::utils::{map_with_newline, snake_case};
pub trait ProverBuilder {
fn create_prover_hpp(&mut self, name: &str);

fn create_prover_cpp(
&mut self,
name: &str,
commitment_polys: &[String],
lookup_names: &[String],
);
fn create_prover_cpp(&mut self, name: &str, lookup_names: &[String]);
}

impl ProverBuilder for BBFiles {
Expand Down Expand Up @@ -83,15 +78,10 @@ impl ProverBuilder for BBFiles {
/// Create the prover cpp file
///
/// Committed polys are included as we manually unroll all commitments, as we do not commit to everything
fn create_prover_cpp(
&mut self,
name: &str,
commitment_polys: &[String],
lookup_names: &[String],
) {
fn create_prover_cpp(&mut self, name: &str, lookup_names: &[String]) {
let include_str = includes_cpp(&snake_case(name));

let polynomial_commitment_phase = create_commitments_phase(commitment_polys);
let polynomial_commitment_phase = create_commitments_phase();

let (call_log_derivative_phase, log_derivative_inverse_phase): (String, String) =
if lookup_names.is_empty() {
Expand Down Expand Up @@ -292,18 +282,15 @@ fn send_to_verifier_transform(name: &String) -> String {
format!("transcript->send_to_verifier(commitment_labels.{name}, witness_commitments.{name});")
}

fn create_commitments_phase(polys_to_commit_to: &[String]) -> String {
let all_commit_operations = map_with_newline(polys_to_commit_to, commitment_transform);
let send_to_verifier_operations =
map_with_newline(polys_to_commit_to, send_to_verifier_transform);

fn create_commitments_phase() -> String {
format!(
"
// Commit to all polynomials (apart from logderivative inverse polynomials, which are committed to in the later logderivative phase)
{all_commit_operations}
// Send all commitments to the verifier
{send_to_verifier_operations}
auto wire_polys = prover_polynomials.get_wires();
auto labels = commitment_labels.get_wires();
for (size_t idx = 0; idx < wire_polys.size(); ++idx) {{
transcript->send_to_verifier(labels[idx], commitment_key->commit(wire_polys[idx]));
}}
"
)
}
Expand Down
8 changes: 3 additions & 5 deletions bb-pilcom/bb-pil-backend/src/verifier_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ pub trait VerifierBuilder {
fn create_verifier_cpp(
&mut self,
name: &str,
witness: &[String],
inverses: &[String],
public_cols: &[(String, usize)],
);
Expand All @@ -19,7 +18,6 @@ impl VerifierBuilder for BBFiles {
fn create_verifier_cpp(
&mut self,
name: &str,
witness: &[String],
inverses: &[String],
public_cols: &[(String, usize)],
) {
Expand All @@ -30,8 +28,6 @@ impl VerifierBuilder for BBFiles {
"commitments.{n} = transcript->template receive_from_prover<Commitment>(commitment_labels.{n});"
)
};
let wire_commitments = map_with_newline(witness, wire_transformation);

let has_public_input_columns = !public_cols.is_empty();
let has_inverses = !inverses.is_empty();

Expand Down Expand Up @@ -151,7 +147,9 @@ impl VerifierBuilder for BBFiles {
}}
// Get commitments to VM wires
{wire_commitments}
for (auto [comm, label] : zip_view(commitments.get_wires(), commitment_labels.get_wires())) {{
comm = transcript->template receive_from_prover<Commitment>(label);
}}
{get_inverse_challenges}
Expand Down
10 changes: 3 additions & 7 deletions bb-pilcom/bb-pil-backend/src/vm_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ pub fn analyzed_to_cpp<F: FieldElement>(
&inverses,
&fixed,
&witness,
&witnesses_without_inverses,
&all_cols,
&to_be_shifted,
&shifted,
Expand All @@ -147,16 +148,11 @@ pub fn analyzed_to_cpp<F: FieldElement>(
bb_files.create_composer_hpp(file_name);

// ----------------------- Create the Verifier files -----------------------
bb_files.create_verifier_cpp(
file_name,
&witnesses_without_inverses,
&inverses,
&public_inputs,
);
bb_files.create_verifier_cpp(file_name, &inverses, &public_inputs);
bb_files.create_verifier_hpp(file_name, &public_inputs);

// ----------------------- Create the Prover files -----------------------
bb_files.create_prover_cpp(file_name, &witnesses_without_inverses, &inverses);
bb_files.create_prover_cpp(file_name, &inverses);
bb_files.create_prover_hpp(file_name);
}

Expand Down

0 comments on commit f401a9a

Please sign in to comment.