Skip to content

Commit

Permalink
feat: makes the code output more granular, rather than creating one m…
Browse files Browse the repository at this point in the history
…onolithic relation (#22)

* fix: remove duplicated replacements

* hide: public references ( little hacky )

* feat: change up to support multiple relation files

* feat: complete codegen

* fix: rm dbg

* feat: single views macro

* fix: variable length relation gen

* fix: refvector flavor changes

* fix: update
  • Loading branch information
Maddiaa0 authored Nov 30, 2023
1 parent e8ba49b commit 1a10dba
Show file tree
Hide file tree
Showing 12 changed files with 379 additions and 293 deletions.
1 change: 0 additions & 1 deletion bberg/src/arith_builder.rs

This file was deleted.

3 changes: 1 addition & 2 deletions bberg/src/bberg_codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ impl BBergCodegen {
witness: &[(String, Vec<F>)],
bname: Option<String>,
) -> Vec<u8> {
let bberg_files = analyzed_to_cpp(pil, fixed, witness, bname);
bberg_files.write();
analyzed_to_cpp(pil, fixed, witness, bname);

Vec::new()
}
Expand Down
53 changes: 45 additions & 8 deletions bberg/src/circuit_builder.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
use crate::file_writer::BBFiles;
use crate::{
file_writer::BBFiles, relation_builder::create_row_type, utils::get_relations_imports,
};

pub trait CircuitBuilder {
fn create_circuit_builder_hpp(&mut self, name: &str, fixed: &[String], shifted: &[String]);
fn create_circuit_builder_hpp(
&mut self,
name: &str,
relations: &[String],
fixed: &[String],
shifted: &[String],
all_cols_with_shifts: &[String],
);
}

fn circuit_hpp_includes(name: &str) -> String {
fn circuit_hpp_includes(name: &str, relations: &[String]) -> String {
let relation_imports = get_relations_imports(name, relations);
format!(
"
// AUTOGENERATED FILE
Expand All @@ -15,7 +25,7 @@ fn circuit_hpp_includes(name: &str) -> String {
#include \"barretenberg/proof_system/circuit_builder/circuit_builder_base.hpp\"
#include \"barretenberg/flavor/generated/{name}_flavor.hpp\"
#include \"barretenberg/relations/generated/{name}.hpp\"
{relation_imports}
"
)
}
Expand All @@ -26,10 +36,14 @@ impl CircuitBuilder for BBFiles {
fn create_circuit_builder_hpp(
&mut self,
name: &str,
relations: &[String],
all_cols: &[String],
to_be_shifted: &[String],
all_cols_with_shifts: &[String],
) {
let includes = circuit_hpp_includes(name);
let includes = circuit_hpp_includes(name, relations);

let row_with_all_included = create_row_type(&format!("{name}Full"), all_cols_with_shifts);

let num_polys = all_cols.len();
let num_cols = all_cols.len() + to_be_shifted.len();
Expand All @@ -46,18 +60,34 @@ impl CircuitBuilder for BBFiles {
.collect::<Vec<String>>()
.join("\n");

let check_circuit_for_each_relation = relations
.iter()
.map(|relation_name| {
format!(
"if (!evaluate_relation.template operator()<{name}_vm::{relation_name}<FF>>(\"{relation_name}\")) {{
return false;
}}",
name = name,
relation_name = relation_name
)
})
.collect::<Vec<String>>()
.join("\n");

let circuit_hpp = format!("
{includes}
using namespace barretenberg;
namespace proof_system {{
{row_with_all_included};
class {name}CircuitBuilder {{
public:
using Flavor = proof_system::honk::flavor::{name}Flavor;
using FF = Flavor::FF;
using Row = {name}_vm::Row<FF>;
using Row = {name}FullRow<FF>;
// TODO: template
using Polynomial = Flavor::Polynomial;
Expand Down Expand Up @@ -117,7 +147,9 @@ class {name}CircuitBuilder {{
return true;
}};
return evaluate_relation.template operator()<{name}_vm::{name}<FF>>(\"{name}\");
{check_circuit_for_each_relation}
return true;
}}
Expand All @@ -135,6 +167,11 @@ class {name}CircuitBuilder {{
}};
}}
");
self.circuit_hpp = Some(circuit_hpp);

self.write_file(
&self.circuit,
&format!("{}_circuit_builder.hpp", name),
&circuit_hpp,
);
}
}
13 changes: 11 additions & 2 deletions bberg/src/composer_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ std::shared_ptr<Flavor::VerificationKey> {name}Composer::compute_verification_ke
}}
");
self.composer_cpp = Some(composer_cpp);
self.write_file(
&self.composer,
&format!("{}_composer.cpp", name),
&composer_cpp,
);
}

fn create_composer_hpp(&mut self, name: &str) {
Expand Down Expand Up @@ -172,7 +176,12 @@ class {name}Composer {{
}} // namespace proof_system::honk
"
);
self.composer_hpp = Some(composer_hpp);

self.write_file(
&self.composer,
&format!("{}_composer.hpp", name),
&composer_hpp,
);
}
}

Expand Down
58 changes: 1 addition & 57 deletions bberg/src/file_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,6 @@ use std::fs::File;
use std::io::Write;

pub struct BBFiles {
pub relation_hpp: Option<String>,
pub flavor_hpp: Option<String>,
// circuit
pub circuit_hpp: Option<String>,
// composer
pub composer_cpp: Option<String>,
pub composer_hpp: Option<String>,

// prover
pub prover_cpp: Option<String>,
pub prover_hpp: Option<String>,

// verifier
pub verifier_cpp: Option<String>,
pub verifier_hpp: Option<String>,

// Relative paths
pub file_name: String,
pub base: String,
Expand Down Expand Up @@ -54,15 +38,6 @@ impl BBFiles {

Self {
file_name,
relation_hpp: None,
flavor_hpp: None,
circuit_hpp: None,
composer_cpp: None,
composer_hpp: None,
prover_cpp: None,
prover_hpp: None,
verifier_cpp: None,
verifier_hpp: None,

base,
rel,
Expand All @@ -74,38 +49,7 @@ impl BBFiles {
}
}

pub fn write(&self) {
// Helper macro codegen using the classes' write_file method
macro_rules! write_file {
($location:expr, $extension:expr, $content:expr) => {
self.write_file(
&$location,
&format!("{}{}", self.file_name, $extension),
&$content.clone().unwrap(),
);
};
}
write_file!(self.rel, ".hpp", self.relation_hpp);

// Circuit
write_file!(self.circuit, "_circuit_builder.hpp", self.circuit_hpp);

write_file!(self.flavor, "_flavor.hpp", self.flavor_hpp);

// Composer
write_file!(self.composer, "_composer.hpp", self.composer_hpp);
write_file!(self.composer, "_composer.cpp", self.composer_cpp);

// Prover
write_file!(self.prover, "_prover.hpp", self.prover_hpp);
write_file!(self.prover, "_prover.cpp", self.prover_cpp);

// Verifier
write_file!(self.prover, "_verifier.hpp", self.verifier_hpp);
write_file!(self.prover, "_verifier.cpp", self.verifier_cpp);
}

fn write_file(&self, folder: &str, filename: &str, contents: &String) {
pub fn write_file(&self, folder: &str, filename: &str, contents: &String) {
// attempt to create dir
let base_path = format!("{}/{}", self.base, folder);
let _ = std::fs::create_dir_all(&base_path);
Expand Down
Loading

0 comments on commit 1a10dba

Please sign in to comment.