Skip to content

Commit

Permalink
Merge branch 'avm' into md/merge-upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 authored Nov 15, 2023
2 parents c1341ca + 95e4676 commit 1934a47
Show file tree
Hide file tree
Showing 10 changed files with 367 additions and 413 deletions.
25 changes: 25 additions & 0 deletions bberg/src/arith_builder.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
80 changes: 25 additions & 55 deletions bberg/src/circuit_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F: FieldElement>(
analyzed: &Analyzed<F>,
fixed: &[(String, Vec<F>)],
witness: &[(String, Vec<F>)],
bname: Option<String>,
) -> 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());

Expand All @@ -41,14 +40,14 @@ pub(crate) fn analyzed_to_cpp<F: FieldElement>(
.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);
Expand All @@ -64,7 +63,7 @@ pub(crate) fn analyzed_to_cpp<F: FieldElement>(
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,
Expand All @@ -73,13 +72,13 @@ pub(crate) fn analyzed_to_cpp<F: FieldElement>(
);

// ----------------------- 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,
Expand All @@ -88,47 +87,18 @@ pub(crate) fn analyzed_to_cpp<F: FieldElement>(
);

// ----------------------- 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<F: FieldElement>(
Expand Down
79 changes: 45 additions & 34 deletions bberg/src/composer_builder.rs
Original file line number Diff line number Diff line change
@@ -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}
Expand Down Expand Up @@ -106,27 +101,14 @@ std::shared_ptr<typename Flavor::VerificationKey> {name}Composer_<Flavor>::compu
template class {name}Composer_<honk::flavor::{name}Flavor>;
}}
")
}

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}
Expand Down Expand Up @@ -195,5 +177,34 @@ using {name}Composer = {name}Composer_<honk::flavor::{name}Flavor>;
}} // 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\"
"
)
}
28 changes: 0 additions & 28 deletions bberg/src/file_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit 1934a47

Please sign in to comment.