diff --git a/bberg/src/arith_builder.rs b/bberg/src/arith_builder.rs new file mode 100644 index 0000000000..365faa8d70 --- /dev/null +++ b/bberg/src/arith_builder.rs @@ -0,0 +1,25 @@ +use crate::file_writer::BBFiles; + +pub trait ArithmetizationBuilder { + fn create_arith_hpp(&mut self, name: &str, num_cols: usize); +} + +impl ArithmetizationBuilder for BBFiles { + // We have no selectors so we can easily create a boilerplate file + fn create_arith_hpp(&mut self, name: &str, num_cols: usize) { + let arith = format!( + " +#pragma once +#include \"barretenberg/proof_system/arithmetization/arithmetization.hpp\" +namespace arithmetization {{ + class {name}Arithmetization : public Arithmetization<{num_cols}, 0> {{ + public: + using FF = barretenberg::fr; + struct Selectors {{}}; + }}; +}} // namespace arithmetization +" + ); + self.arith_hpp = Some(arith); + } +} diff --git a/bberg/src/circuit_builder.rs b/bberg/src/circuit_builder.rs index d2f5bf036c..0605c90e71 100644 --- a/bberg/src/circuit_builder.rs +++ b/bberg/src/circuit_builder.rs @@ -4,23 +4,22 @@ use itertools::Itertools; use number::FieldElement; use pil_analyzer::pil_analyzer::inline_intermediate_polynomials; +use crate::arith_builder::ArithmetizationBuilder; +use crate::composer_builder::ComposerBuilder; use crate::file_writer::BBFiles; -use crate::prover_builder::{prover_builder_cpp, prover_builder_hpp}; -use crate::relation_builder::{create_identities, create_relation_hpp, create_row_type}; +use crate::flavor_builder::FlavorBuilder; +use crate::prover_builder::ProverBuilder; +use crate::relation_builder::{create_identities, create_row_type, RelationBuilder}; use crate::trace_builder::TraceBuilder; -use crate::verifier_builder::{verifier_builder_cpp, verifier_builder_hpp}; -use crate::{ - composer_builder::{composer_builder_cpp, composer_builder_hpp}, - flavor_builder, -}; +use crate::verifier_builder::VerifierBuilder; pub(crate) fn analyzed_to_cpp( analyzed: &Analyzed, fixed: &[(&str, Vec)], witness: &[(&str, Vec)], - bname: Option, + name: Option, ) -> BBFiles { - let file_name: &str = &bname.unwrap_or("Example".to_owned()); + let file_name: &str = &name.unwrap_or("Example".to_owned()); let mut bb_files = BBFiles::default(file_name.to_owned()); @@ -41,14 +40,14 @@ pub(crate) fn analyzed_to_cpp( .find(|col_name| col_name.0.contains("FIRST")) .expect("PIL file must contain a fixed column named FIRST") .0 - .replace(".", "_"); + .replace('.', "_"); let last_col = fixed .iter() .find(|col_name| col_name.0.contains("LAST")) .expect("PIL file must contain a fixed column named LAST") .0 - .replace(".", "_"); + .replace('.', "_"); // Inlining step to remove the intermediate poly definitions let analyzed_identities = inline_intermediate_polynomials(analyzed); @@ -64,7 +63,7 @@ pub(crate) fn analyzed_to_cpp( let row_type = create_row_type(&all_cols_with_shifts); // ----------------------- Create the relation file ----------------------- - let relation_hpp = create_relation_hpp( + bb_files.create_relation_hpp( file_name, &subrelations, &identities, @@ -73,13 +72,13 @@ pub(crate) fn analyzed_to_cpp( ); // ----------------------- Create the arithmetization file ----------------------- - let arith_hpp = create_arith_boilerplate_file(file_name, num_cols); + bb_files.create_arith_hpp(file_name, num_cols); - // ----------------------- Create the read from powdr columns file ----------------------- - let trace_hpp = bb_files.create_trace_builder_hpp(file_name, &all_cols, &to_be_shifted); + // ----------------------- Create the trace builder file ----------------------- + bb_files.create_trace_builder_hpp(file_name, &all_cols, &to_be_shifted); // ----------------------- Create the flavor file ----------------------- - let flavor_hpp = flavor_builder::create_flavor_hpp( + bb_files.create_flavor_hpp( file_name, &subrelations, &all_cols, @@ -88,47 +87,18 @@ pub(crate) fn analyzed_to_cpp( ); // ----------------------- Create the composer files ----------------------- - let composer_cpp = composer_builder_cpp(file_name); - let composer_hpp = composer_builder_hpp(file_name); - - // ----------------------- Create the prover files ----------------------- - let verifier_cpp = verifier_builder_cpp(file_name, &all_cols); - let verifier_hpp = verifier_builder_hpp(file_name); - - // ----------------------- Create the verifier files ----------------------- - let prover_cpp = prover_builder_cpp(file_name, &unshifted, &to_be_shifted); - let prover_hpp = prover_builder_hpp(file_name); - - bb_files.add_files( - relation_hpp, - arith_hpp, - trace_hpp, - flavor_hpp, - composer_cpp, - composer_hpp, - verifier_cpp, - verifier_hpp, - prover_cpp, - prover_hpp, - ); - bb_files -} + bb_files.create_composer_cpp(file_name); + bb_files.create_composer_hpp(file_name); -// We have no selectors so we can easily create a boilerplate file -fn create_arith_boilerplate_file(name: &str, num_cols: usize) -> String { - format!( - " -#pragma once -#include \"barretenberg/proof_system/arithmetization/arithmetization.hpp\" -namespace arithmetization {{ - class {name}Arithmetization : public Arithmetization<{num_cols}, 0> {{ - public: - using FF = barretenberg::fr; - struct Selectors {{}}; - }}; -}} // namespace arithmetization -" - ) + // ----------------------- Create the Verifier files ----------------------- + bb_files.create_verifier_cpp(file_name, &all_cols); + bb_files.create_verifier_hpp(file_name); + + // ----------------------- Create the Prover files ----------------------- + bb_files.create_prover_cpp(file_name, &unshifted, &to_be_shifted); + bb_files.create_prover_hpp(file_name); + + bb_files } fn get_all_col_names( diff --git a/bberg/src/composer_builder.rs b/bberg/src/composer_builder.rs index 50c384cebc..4ec955fac4 100644 --- a/bberg/src/composer_builder.rs +++ b/bberg/src/composer_builder.rs @@ -1,21 +1,16 @@ -fn cpp_includes(name: &str) -> String { - format!( - " -#include \"./{name}_composer.hpp\" -#include \"barretenberg/honk/proof_system/generated/{name}_verifier.hpp\" -#include \"barretenberg/honk/proof_system/grand_product_library.hpp\" -#include \"barretenberg/proof_system/circuit_builder/generated/{name}_trace.hpp\" -#include \"barretenberg/proof_system/composer/composer_lib.hpp\" -#include \"barretenberg/proof_system/composer/permutation_lib.hpp\" -" - ) +use crate::file_writer::BBFiles; + +pub trait ComposerBuilder { + fn create_composer_cpp(&mut self, name: &str); + fn create_composer_hpp(&mut self, name: &str); } -pub fn composer_builder_cpp(name: &str) -> String { - // Create a composer file, this is used to a prover and verifier for our flavour - let include_str = cpp_includes(name); +impl ComposerBuilder for BBFiles { + fn create_composer_cpp(&mut self, name: &str) { + // Create a composer file, this is used to a prover and verifier for our flavour + let include_str = cpp_includes(name); - format!( + let composer_cpp = format!( " {include_str} @@ -106,27 +101,14 @@ std::shared_ptr {name}Composer_::compu template class {name}Composer_; }} -") -} - -pub fn hpp_includes(name: &str) -> String { - format!( - " -#pragma once - -#include \"barretenberg/honk/proof_system/generated/{name}_prover.hpp\" -#include \"barretenberg/honk/proof_system/generated/{name}_verifier.hpp\" -#include \"barretenberg/proof_system/circuit_builder/generated/{name}_trace.hpp\" -#include \"barretenberg/proof_system/composer/composer_lib.hpp\" -#include \"barretenberg/srs/global_crs.hpp\" - " - ) -} +"); + self.composer_cpp = Some(composer_cpp); + } -pub fn composer_builder_hpp(name: &str) -> String { - let include_str = hpp_includes(name); + fn create_composer_hpp(&mut self, name: &str) { + let include_str = hpp_includes(name); - format!( + let composer_hpp = format!( " {include_str} @@ -195,5 +177,34 @@ using {name}Composer = {name}Composer_; }} // namespace proof_system::honk " + ); + self.composer_hpp = Some(composer_hpp); + } +} + +fn cpp_includes(name: &str) -> String { + format!( + " +#include \"./{name}_composer.hpp\" +#include \"barretenberg/honk/proof_system/generated/{name}_verifier.hpp\" +#include \"barretenberg/honk/proof_system/grand_product_library.hpp\" +#include \"barretenberg/proof_system/circuit_builder/generated/{name}_trace.hpp\" +#include \"barretenberg/proof_system/composer/composer_lib.hpp\" +#include \"barretenberg/proof_system/composer/permutation_lib.hpp\" +" + ) +} + +pub fn hpp_includes(name: &str) -> String { + format!( + " +#pragma once + +#include \"barretenberg/honk/proof_system/generated/{name}_prover.hpp\" +#include \"barretenberg/honk/proof_system/generated/{name}_verifier.hpp\" +#include \"barretenberg/proof_system/circuit_builder/generated/{name}_trace.hpp\" +#include \"barretenberg/proof_system/composer/composer_lib.hpp\" +#include \"barretenberg/srs/global_crs.hpp\" + " ) } diff --git a/bberg/src/file_writer.rs b/bberg/src/file_writer.rs index 97ae6bb3ff..bc7f5d59ee 100644 --- a/bberg/src/file_writer.rs +++ b/bberg/src/file_writer.rs @@ -76,34 +76,6 @@ impl BBFiles { } } - pub fn add_files( - &mut self, - relation_hpp: String, - arith_hpp: String, - trace_hpp: String, - flavor_hpp: String, - composer_cpp: String, - composer_hpp: String, - verifier_cpp: String, - verifier_hpp: String, - prover_cpp: String, - prover_hpp: String, - ) { - self.relation_hpp = Some(relation_hpp); - self.arith_hpp = Some(arith_hpp); - self.flavor_hpp = Some(flavor_hpp); - self.composer_cpp = Some(composer_cpp); - self.composer_hpp = Some(composer_hpp); - - self.trace_hpp = Some(trace_hpp); - - self.verifier_cpp = Some(verifier_cpp); - self.verifier_hpp = Some(verifier_hpp); - - self.prover_cpp = Some(prover_cpp); - self.prover_hpp = Some(prover_hpp); - } - pub fn write(&self) { // Helper macro codegen using the classes' write_file method macro_rules! write_file { diff --git a/bberg/src/flavor_builder.rs b/bberg/src/flavor_builder.rs index f9e357524e..1f2726b9e4 100644 --- a/bberg/src/flavor_builder.rs +++ b/bberg/src/flavor_builder.rs @@ -1,52 +1,63 @@ -/// Build the boilerplate for the flavor file - -pub(crate) fn create_flavor_hpp( - name: &str, - relations: &[String], - all_cols: &[String], - shifted: &[String], - // shifted: &[String], -) -> String { - let includes = flavor_includes(name, relations); - let num_witness = all_cols.len(); - let num_all = num_witness + shifted.len(); - // Note: includes all witness shifts - // TODO: for now we include a shift OF ALL witness wires, however this is not necessarily true - - let precomputed = witness_get(all_cols, 0, false); - let witness_str = create_witness_entities(all_cols); - let all_shift = witness_get(shifted, num_witness, true); - - dbg!(&all_shift); - - let all_entities_get_wires = make_wires_set( - &[all_cols.to_vec(), shifted.to_vec()] - .into_iter() - .flatten() - .collect::>(), - ); - let all_entities_get_unshifted = make_wires_set(all_cols); - let all_entities_get_to_be_shifted = make_wires_set(shifted); - let all_entities_get_shifted = make_wires_set( - &shifted - .iter() - .map(|w| format!("{}_shift", w)) - .collect::>(), +use crate::file_writer::BBFiles; + +pub trait FlavorBuilder { + fn create_flavor_hpp( + &mut self, + name: &str, + relations: &[String], + all_cols: &[String], + shifted: &[String], ); +} - let commitment_labels_class = create_commitment_labels(all_cols); - - let verification_commitments = create_verifier_commitments(); - - // TODO: make this work when we have multiple relation files, for now we just have the one - let relations_tuple = format!("{name}_vm::{name}"); - // let relations_tuple = relations - // .iter() - // .map(|r| format!("{}Relation", r)) - // .collect::>() - // .join(", "); - - format!( +/// Build the boilerplate for the flavor file +impl FlavorBuilder for BBFiles { + fn create_flavor_hpp( + &mut self, + name: &str, + relations: &[String], + all_cols: &[String], + shifted: &[String], + // shifted: &[String], + ) { + let includes = flavor_includes(name, relations); + let num_witness = all_cols.len(); + let num_all = num_witness + shifted.len(); + // Note: includes all witness shifts + // TODO: for now we include a shift OF ALL witness wires, however this is not necessarily true + + let precomputed = witness_get(all_cols, 0, false); + let witness_str = create_witness_entities(all_cols); + let all_shift = witness_get(shifted, num_witness, true); + + let all_entities_get_wires = make_wires_set( + &[all_cols.to_vec(), shifted.to_vec()] + .into_iter() + .flatten() + .collect::>(), + ); + let all_entities_get_unshifted = make_wires_set(all_cols); + let all_entities_get_to_be_shifted = make_wires_set(shifted); + let all_entities_get_shifted = make_wires_set( + &shifted + .iter() + .map(|w| format!("{}_shift", w)) + .collect::>(), + ); + + let commitment_labels_class = create_commitment_labels(all_cols); + + let verification_commitments = create_verifier_commitments(); + + // TODO: make this work when we have multiple relation files, for now we just have the one + let relations_tuple = format!("{name}_vm::{name}"); + // let relations_tuple = relations + // .iter() + // .map(|r| format!("{}Relation", r)) + // .collect::>() + // .join(", "); + + let flavor_hpp = format!( " {includes} @@ -242,7 +253,9 @@ class {name}Flavor : public {name}FlavorBase String { diff --git a/bberg/src/lib.rs b/bberg/src/lib.rs index d6cdb794d4..25bcbf3f98 100644 --- a/bberg/src/lib.rs +++ b/bberg/src/lib.rs @@ -1,3 +1,4 @@ +mod arith_builder; pub mod bberg_codegen; pub mod circuit_builder; mod composer_builder; diff --git a/bberg/src/prover_builder.rs b/bberg/src/prover_builder.rs index 062a7e2683..8ee21e9b91 100644 --- a/bberg/src/prover_builder.rs +++ b/bberg/src/prover_builder.rs @@ -1,22 +1,15 @@ -fn includes_hpp(name: &str) -> String { - format!( - " -#pragma once -#include \"barretenberg/honk/flavor/generated/{name}_flavor.hpp\" -#include \"barretenberg/honk/pcs/gemini/gemini.hpp\" -#include \"barretenberg/honk/pcs/shplonk/shplonk.hpp\" -#include \"barretenberg/honk/sumcheck/sumcheck_output.hpp\" -#include \"barretenberg/honk/transcript/transcript.hpp\" -#include \"barretenberg/plonk/proof_system/types/proof.hpp\" -#include \"barretenberg/proof_system/relations/relation_parameters.hpp\" +use crate::file_writer::BBFiles; - " - ) +pub trait ProverBuilder { + fn create_prover_cpp(&mut self, name: &str, fixed: &[String], to_be_shifted: &[String]); + + fn create_prover_hpp(&mut self, name: &str); } -pub fn prover_builder_hpp(name: &str) -> String { - let include_str = includes_hpp(name); - format!(" +impl ProverBuilder for BBFiles { + fn create_prover_hpp(&mut self, name: &str) { + let include_str = includes_hpp(name); + let prover_hpp = format!(" {include_str} namespace proof_system::honk {{ @@ -89,74 +82,47 @@ pub fn prover_builder_hpp(name: &str) -> String { }} // namespace proof_system::honk - ") -} - -fn includes_cpp(name: &str) -> String { - format!( - " - - #include \"{name}_prover.hpp\" - #include \"barretenberg/honk/pcs/claim.hpp\" - #include \"barretenberg/honk/pcs/commitment_key.hpp\" - #include \"barretenberg/honk/proof_system/lookup_library.hpp\" - #include \"barretenberg/honk/proof_system/permutation_library.hpp\" - #include \"barretenberg/honk/sumcheck/sumcheck.hpp\" - #include \"barretenberg/honk/utils/power_polynomial.hpp\" - #include \"barretenberg/polynomials/polynomial.hpp\" - #include \"barretenberg/polynomials/univariate.hpp\" // will go away - #include \"barretenberg/proof_system/relations/lookup_relation.hpp\" - #include \"barretenberg/proof_system/relations/permutation_relation.hpp\" - #include \"barretenberg/transcript/transcript_wrappers.hpp\" - #include - #include - #include - #include - #include - #include - #include - #include - " - ) -} + "); + self.prover_hpp = Some(prover_hpp); + } -pub fn prover_builder_cpp(name: &str, fixed: &[String], to_be_shifted: &[String]) -> String { - let include_str = includes_cpp(name); + fn create_prover_cpp(&mut self, name: &str, fixed: &[String], to_be_shifted: &[String]) { + let include_str = includes_cpp(name); - // Create the wire assignments, prover_polynomial = key - let fixed_assignments = fixed - .iter() - .map(|name| { - let n = name.replace('.', "_"); - format!("prover_polynomials.{n} = key->{n};", n = n) - }) - .collect::>() - .join("\n"); + // Create the wire assignments, prover_polynomial = key + let fixed_assignments = fixed + .iter() + .map(|name| { + let n = name.replace('.', "_"); + format!("prover_polynomials.{n} = key->{n};", n = n) + }) + .collect::>() + .join("\n"); - let committed_assignments = to_be_shifted - .iter() - .map(|name| { - let n = name.replace('.', "_"); - format!( - " + let committed_assignments = to_be_shifted + .iter() + .map(|name| { + let n = name.replace('.', "_"); + format!( + " prover_polynomials.{n} = key->{n}; prover_polynomials.{n}_shift = key->{n}.shifted(); ", - n = n - ) - }) - .collect::>() - .join("\n"); + n = n + ) + }) + .collect::>() + .join("\n"); - // Lmao yuck - let all_assignments = format!( - " + // Lmao yuck + let all_assignments = format!( + " {fixed_assignments} {committed_assignments} " - ); + ); - format!(" + let prover_cpp = format!(" {include_str} namespace proof_system::honk {{ @@ -416,5 +382,51 @@ prover_polynomials.{n}_shift = key->{n}.shifted(); }} // namespace proof_system::honk - ") + "); + self.prover_cpp = Some(prover_cpp); + } +} + +fn includes_hpp(name: &str) -> String { + format!( + " +#pragma once +#include \"barretenberg/honk/flavor/generated/{name}_flavor.hpp\" +#include \"barretenberg/honk/pcs/gemini/gemini.hpp\" +#include \"barretenberg/honk/pcs/shplonk/shplonk.hpp\" +#include \"barretenberg/honk/sumcheck/sumcheck_output.hpp\" +#include \"barretenberg/honk/transcript/transcript.hpp\" +#include \"barretenberg/plonk/proof_system/types/proof.hpp\" +#include \"barretenberg/proof_system/relations/relation_parameters.hpp\" + + " + ) +} + +fn includes_cpp(name: &str) -> String { + format!( + " + + #include \"{name}_prover.hpp\" + #include \"barretenberg/honk/pcs/claim.hpp\" + #include \"barretenberg/honk/pcs/commitment_key.hpp\" + #include \"barretenberg/honk/proof_system/lookup_library.hpp\" + #include \"barretenberg/honk/proof_system/permutation_library.hpp\" + #include \"barretenberg/honk/sumcheck/sumcheck.hpp\" + #include \"barretenberg/honk/utils/power_polynomial.hpp\" + #include \"barretenberg/polynomials/polynomial.hpp\" + #include \"barretenberg/polynomials/univariate.hpp\" // will go away + #include \"barretenberg/proof_system/relations/lookup_relation.hpp\" + #include \"barretenberg/proof_system/relations/permutation_relation.hpp\" + #include \"barretenberg/transcript/transcript_wrappers.hpp\" + #include + #include + #include + #include + #include + #include + #include + #include + " + ) } diff --git a/bberg/src/relation_builder.rs b/bberg/src/relation_builder.rs index 94704ce6df..0daf8e8295 100644 --- a/bberg/src/relation_builder.rs +++ b/bberg/src/relation_builder.rs @@ -4,29 +4,43 @@ use ast::analyzed::{ IdentityKind, }; use ast::parsed::SelectedExpressions; -use itertools::Itertools; use std::collections::HashSet; use number::{DegreeType, FieldElement}; -// TODO: MOve -> to gen code we need to know the degree of each poly -type BBIdentity = (DegreeType, String); +use crate::file_writer::BBFiles; -pub(crate) fn create_relation_hpp( - name: &str, - sub_relations: &[String], - identities: &[BBIdentity], - row_type: &String, - all_rows_and_shifts: &[String], -) -> String { - let includes = relation_includes(); - let class_boilerplate = relation_class_boilerplate(name, sub_relations, identities); - let export = get_export(name); +pub trait RelationBuilder { + fn create_relation_hpp( + &mut self, + name: &str, + sub_relations: &[String], + identities: &[BBIdentity], + row_type: &String, + all_rows_and_shifts: &[String], + ); +} - let view_macro_preamble = get_cols_in_identity_macro(all_rows_and_shifts); +// TODO: MOve -> to gen code we need to know the degree of each poly +type BBIdentity = (DegreeType, String); - format!( - "{includes} +impl RelationBuilder for BBFiles { + fn create_relation_hpp( + &mut self, + name: &str, + sub_relations: &[String], + identities: &[BBIdentity], + row_type: &String, + all_rows_and_shifts: &[String], + ) { + let includes = relation_includes(); + let class_boilerplate = relation_class_boilerplate(name, sub_relations, identities); + let export = get_export(name); + + let view_macro_preamble = get_cols_in_identity_macro(all_rows_and_shifts); + + let relations = format!( + "{includes} namespace proof_system::{name}_vm {{ {row_type}; @@ -38,7 +52,9 @@ namespace proof_system::{name}_vm {{ {export} }}" - ) + ); + self.relation_hpp = Some(relations); + } } fn relation_class_boilerplate( diff --git a/bberg/src/trace_builder.rs b/bberg/src/trace_builder.rs index a3e987fe11..fdd7f522ec 100644 --- a/bberg/src/trace_builder.rs +++ b/bberg/src/trace_builder.rs @@ -1,14 +1,6 @@ use crate::file_writer::BBFiles; pub trait TraceBuilder { - fn create_trace_builder_cpp( - &mut self, - name: &str, - fixed: &[String], - witness: &[String], - to_be_shifted: &[String], - ) -> String; - fn create_trace_builder_hpp( &mut self, name: &str, @@ -60,76 +52,6 @@ fn trace_hpp_includes(name: &str) -> String { impl TraceBuilder for BBFiles { // Create trace builder // Generate some code that can read a commits.bin and constants.bin into data structures that bberg understands - fn create_trace_builder_cpp( - &mut self, - name: &str, - fixed: &[String], - witness: &[String], - _to_be_shifted: &[String], - ) -> String { - // We are assuming that the order of the columns in the trace file is the same as the order in the witness file - let includes = trace_cpp_includes(&self.rel, name); - let row_import = format!("using Row = {name}_vm::Row;"); - - // NOTE: Both of these are also calculated elsewhere, this is extra work - // TODO: Recalculated! - let _num_cols = fixed.len() + witness.len() * 2; // (2* as shifts) - let fixed_name = fixed - .iter() - .map(|name| { - let n = name.replace('.', "_"); - n.to_string() - }) - .collect::>(); - let witness_name = witness - .iter() - .map(|name| { - let n = name.replace('.', "_"); - n.to_string() - }) - .collect::>(); - - // TODO: remove the ol clones - let all_names = [fixed_name.clone(), witness_name.clone()] - .into_iter() - .flatten() - .collect::>(); - - // let empty_row = build_empty_row(&all_names); - - let _compute_polys_assignemnt = all_names - .iter() - .map(|name| format!("polys.{name}[i] = rows[i].{name};",)) - .collect::>() - .join("\n"); - - let _all_poly_shifts = &witness_name - .iter() - .map(|name| format!("polys.{name}_shift = rows[i].{name}_shift;")) - .collect::>() - .join("\n"); - - // NOTE: can we assume that the witness filename etc will stay the same? - let read_from_file_boilerplate = format!( - " -// AUTOGENERATED FILE -{includes} - -using namespace barretenberg; - -namespace proof_system {{ - -{row_import} - -}} - " - ); - - // TODO: remove return val, make traits for everything - self.trace_hpp = Some(read_from_file_boilerplate.clone()); - read_from_file_boilerplate - } - fn create_trace_builder_hpp( &mut self, name: &str, diff --git a/bberg/src/verifier_builder.rs b/bberg/src/verifier_builder.rs index e1aa2dfe15..d9558b04e8 100644 --- a/bberg/src/verifier_builder.rs +++ b/bberg/src/verifier_builder.rs @@ -1,90 +1,21 @@ -fn include_hpp(name: &str) -> String { - format!( - " -#pragma once -#include \"barretenberg/honk/flavor/generated/{name}_flavor.hpp\" -#include \"barretenberg/honk/sumcheck/sumcheck.hpp\" -#include \"barretenberg/plonk/proof_system/types/proof.hpp\" -" - ) -} - -pub fn verifier_builder_hpp(name: &str) -> String { - let include_str = include_hpp(name); - format!( - " -{include_str} - - namespace proof_system::honk {{ - template class {name}Verifier_ {{ - using FF = typename Flavor::FF; - using Commitment = typename Flavor::Commitment; - using VerificationKey = typename Flavor::VerificationKey; - using VerifierCommitmentKey = typename Flavor::VerifierCommitmentKey; - - public: - explicit {name}Verifier_(std::shared_ptr verifier_key = nullptr); - {name}Verifier_(std::shared_ptr key, - std::map commitments, - std::map pcs_fr_elements, - std::shared_ptr pcs_verification_key, - VerifierTranscript transcript) - : key(std::move(key)) - , commitments(std::move(commitments)) - , pcs_fr_elements(std::move(pcs_fr_elements)) - , pcs_verification_key(std::move(pcs_verification_key)) - , transcript(std::move(transcript)) - {{}} +use crate::file_writer::BBFiles; - {name}Verifier_({name}Verifier_&& other) noexcept; - {name}Verifier_(const {name}Verifier_& other) = delete; - {name}Verifier_& operator=(const {name}Verifier_& other) = delete; - {name}Verifier_& operator=({name}Verifier_&& other) noexcept; - ~{name}Verifier_() = default; - - bool verify_proof(const plonk::proof& proof); - - std::shared_ptr key; - std::map commitments; - std::map pcs_fr_elements; - std::shared_ptr pcs_verification_key; - VerifierTranscript transcript; - }}; - - extern template class {name}Verifier_; - - using {name}Verifier = {name}Verifier_; - - }} // namespace proof_system::honk - - - " - ) -} +pub trait VerifierBuilder { + fn create_verifier_cpp(&mut self, name: &str, all_wires: &[String]); -fn includes_cpp(name: &str) -> String { - format!( - " - #include \"./{name}_verifier.hpp\" - #include \"barretenberg/honk/flavor/generated/{name}_flavor.hpp\" - #include \"barretenberg/honk/pcs/gemini/gemini.hpp\" - #include \"barretenberg/honk/pcs/shplonk/shplonk.hpp\" - #include \"barretenberg/honk/transcript/transcript.hpp\" - #include \"barretenberg/honk/utils/power_polynomial.hpp\" - #include \"barretenberg/numeric/bitop/get_msb.hpp\" - " - ) + fn create_verifier_hpp(&mut self, name: &str); } -pub fn verifier_builder_cpp(name: &str, all_wires: &[String]) -> String { - let include_str = includes_cpp(name); +impl VerifierBuilder for BBFiles { + fn create_verifier_cpp(&mut self, name: &str, all_wires: &[String]) { + let include_str = includes_cpp(name); - let wire_commitments = all_wires.iter().map(|name|{ + let wire_commitments = all_wires.iter().map(|name|{ let n = name.replace('.',"_"); format!("commitments.{n} = transcript.template receive_from_prover(commitment_labels.{n});", n=n) }).collect::>().join("\n"); - format!(" + let ver_cpp = format!(" {include_str} using namespace barretenberg; @@ -241,5 +172,86 @@ pub fn verifier_builder_cpp(name: &str, all_wires: &[String]) -> String { }} // namespace proof_system::honk - ") + "); + self.verifier_cpp = Some(ver_cpp); + } + + fn create_verifier_hpp(&mut self, name: &str) { + let include_str = include_hpp(name); + let ver_hpp = format!( + " +{include_str} + + namespace proof_system::honk {{ + template class {name}Verifier_ {{ + using FF = typename Flavor::FF; + using Commitment = typename Flavor::Commitment; + using VerificationKey = typename Flavor::VerificationKey; + using VerifierCommitmentKey = typename Flavor::VerifierCommitmentKey; + + public: + explicit {name}Verifier_(std::shared_ptr verifier_key = nullptr); + {name}Verifier_(std::shared_ptr key, + std::map commitments, + std::map pcs_fr_elements, + std::shared_ptr pcs_verification_key, + VerifierTranscript transcript) + : key(std::move(key)) + , commitments(std::move(commitments)) + , pcs_fr_elements(std::move(pcs_fr_elements)) + , pcs_verification_key(std::move(pcs_verification_key)) + , transcript(std::move(transcript)) + {{}} + + {name}Verifier_({name}Verifier_&& other) noexcept; + {name}Verifier_(const {name}Verifier_& other) = delete; + {name}Verifier_& operator=(const {name}Verifier_& other) = delete; + {name}Verifier_& operator=({name}Verifier_&& other) noexcept; + ~{name}Verifier_() = default; + + bool verify_proof(const plonk::proof& proof); + + std::shared_ptr key; + std::map commitments; + std::map pcs_fr_elements; + std::shared_ptr pcs_verification_key; + VerifierTranscript transcript; + }}; + + extern template class {name}Verifier_; + + using {name}Verifier = {name}Verifier_; + + }} // namespace proof_system::honk + + + " + ); + self.verifier_hpp = Some(ver_hpp); + } +} + +fn include_hpp(name: &str) -> String { + format!( + " +#pragma once +#include \"barretenberg/honk/flavor/generated/{name}_flavor.hpp\" +#include \"barretenberg/honk/sumcheck/sumcheck.hpp\" +#include \"barretenberg/plonk/proof_system/types/proof.hpp\" +" + ) +} + +fn includes_cpp(name: &str) -> String { + format!( + " + #include \"./{name}_verifier.hpp\" + #include \"barretenberg/honk/flavor/generated/{name}_flavor.hpp\" + #include \"barretenberg/honk/pcs/gemini/gemini.hpp\" + #include \"barretenberg/honk/pcs/shplonk/shplonk.hpp\" + #include \"barretenberg/honk/transcript/transcript.hpp\" + #include \"barretenberg/honk/utils/power_polynomial.hpp\" + #include \"barretenberg/numeric/bitop/get_msb.hpp\" + " + ) }