Skip to content

Commit

Permalink
Merge pull request #21 from AztecProtocol/jm/new-interface
Browse files Browse the repository at this point in the history
BBerg code generated: new interface to pass a trace to circuit_builder
  • Loading branch information
jeanmon authored Nov 23, 2023
2 parents 553590b + b07215e commit e8ba49b
Show file tree
Hide file tree
Showing 7 changed files with 291 additions and 292 deletions.
2 changes: 1 addition & 1 deletion bberg/src/bberg_codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::io;

use number::{BigInt, Bn254Field, DegreeType, FieldElement};

use crate::circuit_builder::analyzed_to_cpp;
use crate::vm_builder::analyzed_to_cpp;

// TODO: there will need to be multiple files that are generated, one for each relation

Expand Down
272 changes: 135 additions & 137 deletions bberg/src/circuit_builder.rs
Original file line number Diff line number Diff line change
@@ -1,142 +1,140 @@
use ast::analyzed::Analyzed;

use itertools::Itertools;
use number::FieldElement;
use pil_analyzer::pil_analyzer::inline_intermediate_polynomials;

use crate::composer_builder::ComposerBuilder;
use crate::file_writer::BBFiles;
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::VerifierBuilder;

pub(crate) fn analyzed_to_cpp<F: FieldElement>(
analyzed: &Analyzed<F>,
fixed: &[(String, Vec<F>)],
witness: &[(String, Vec<F>)],
name: Option<String>,
) -> BBFiles {
let file_name: &str = &name.unwrap_or("Example".to_owned());

let mut bb_files = BBFiles::default(file_name.to_owned());

// Collect all column names and determine if they need a shift or not

// TODO: currently we provide shifts for both the fixed and witness columns, in the long term we need to work out what needs a shift and what doesn't
let fixed_names = fixed
.iter()
.map(|(name, _)| (*name.replace(".", "_")).to_owned())
.collect::<Vec<_>>();
let witness_names = witness
.iter()
.map(|(name, _)| (*name.replace(".", "_")).to_owned())
.collect::<Vec<_>>();

// Inlining step to remove the intermediate poly definitions
let analyzed_identities = inline_intermediate_polynomials(analyzed);

let (subrelations, identities, mut collected_shifts) = create_identities(&analyzed_identities);
let shifted_polys: Vec<String> = collected_shifts.drain().collect_vec();

let (all_cols, unshifted, to_be_shifted, shifted, all_cols_with_shifts) =
get_all_col_names(fixed, witness, &shifted_polys);
let num_cols = all_cols_with_shifts.len();

let row_type = create_row_type(&all_cols_with_shifts);

// ----------------------- Create the relation file -----------------------
bb_files.create_relation_hpp(
file_name,
&subrelations,
&identities,
&row_type,
&all_cols_with_shifts,
);

// ----------------------- Create the trace builder file -----------------------
bb_files.create_trace_builder_hpp(file_name, &all_cols, &to_be_shifted);

// ----------------------- Create the flavor file -----------------------
bb_files.create_flavor_hpp(
file_name,
&subrelations,
&fixed_names,
&witness_names,
&all_cols,
&to_be_shifted,
&shifted,
// &shifted,
);

// ----------------------- Create the composer files -----------------------
bb_files.create_composer_cpp(file_name, &all_cols);
bb_files.create_composer_hpp(file_name);

// ----------------------- Create the Verifier files -----------------------
bb_files.create_verifier_cpp(file_name, &witness_names);
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

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

fn get_all_col_names<F: FieldElement>(
fixed: &[(String, Vec<F>)],
witness: &[(String, Vec<F>)],
to_be_shifted: &[String],
) -> (
Vec<String>,
Vec<String>,
Vec<String>,
Vec<String>,
Vec<String>,
) {
let fixed_names: Vec<String> = fixed
.iter()
.map(|(name, _)| {
let n = name.replace('.', "_");
n.to_owned()
})
.collect();
let witness_names: Vec<String> = witness
.iter()
.map(|(name, _)| {
let n = name.replace('.', "_");
n.to_owned()
})
.collect();

let shifted: Vec<String> = to_be_shifted
.iter()
.map(|name| format!("{}_shift", *name))
.collect();

let all_cols: Vec<String> = [fixed_names.clone(), witness_names.clone()]
.into_iter()
.flatten()
.collect();

let unshifted: Vec<String> = [fixed_names.clone(), witness_names.clone()]
.into_iter()
.flatten()
.filter(|name| !shifted.contains(name))
.collect();

let with_shifts: Vec<String> = [fixed_names, witness_names, shifted.clone()]
.into_iter()
.flatten()
.collect();

(
all_cols,
unshifted,
to_be_shifted.to_vec(),
shifted,
with_shifts,
fn circuit_hpp_includes(name: &str) -> String {
format!(
"
// AUTOGENERATED FILE
#pragma once
#include \"barretenberg/common/throw_or_abort.hpp\"
#include \"barretenberg/ecc/curves/bn254/fr.hpp\"
#include \"barretenberg/proof_system/circuit_builder/circuit_builder_base.hpp\"
#include \"barretenberg/flavor/generated/{name}_flavor.hpp\"
#include \"barretenberg/relations/generated/{name}.hpp\"
"
)
}

impl CircuitBuilder for BBFiles {
// Create circuit builder
// Generate some code that can read a commits.bin and constants.bin into data structures that bberg understands
fn create_circuit_builder_hpp(
&mut self,
name: &str,
all_cols: &[String],
to_be_shifted: &[String],
) {
let includes = circuit_hpp_includes(name);

let num_polys = all_cols.len();
let num_cols = all_cols.len() + to_be_shifted.len();

let compute_polys_assignemnt = all_cols
.iter()
.map(|name| format!("polys.{name}[i] = rows[i].{name};",))
.collect::<Vec<String>>()
.join("\n");

let all_poly_shifts = &to_be_shifted
.iter()
.map(|name| format!("polys.{name}_shift = Polynomial(polys.{name}.shifted());"))
.collect::<Vec<String>>()
.join("\n");

let circuit_hpp = format!("
{includes}
using namespace barretenberg;
namespace proof_system {{
class {name}CircuitBuilder {{
public:
using Flavor = proof_system::honk::flavor::{name}Flavor;
using FF = Flavor::FF;
using Row = {name}_vm::Row<FF>;
// TODO: template
using Polynomial = Flavor::Polynomial;
using AllPolynomials = Flavor::AllPolynomials;
static constexpr size_t num_fixed_columns = {num_cols};
static constexpr size_t num_polys = {num_polys};
std::vector<Row> rows;
void set_trace(std::vector<Row>&& trace) {{ rows = std::move(trace); }}
AllPolynomials compute_polynomials() {{
const auto num_rows = get_circuit_subgroup_size();
AllPolynomials polys;
// Allocate mem for each column
for (auto* poly : polys.pointer_view()) {{
*poly = Polynomial(num_rows);
}}
for (size_t i = 0; i < rows.size(); i++) {{
{compute_polys_assignemnt}
}}
{all_poly_shifts }
return polys;
}}
[[maybe_unused]] bool check_circuit()
{{
auto polys = compute_polynomials();
const size_t num_rows = polys.get_polynomial_size();
const auto evaluate_relation = [&]<typename Relation>(const std::string& relation_name) {{
typename Relation::SumcheckArrayOfValuesOverSubrelations result;
for (auto& r : result) {{
r = 0;
}}
constexpr size_t NUM_SUBRELATIONS = result.size();
for (size_t i = 0; i < num_rows; ++i) {{
Relation::accumulate(result, polys.get_row(i), {{}}, 1);
bool x = true;
for (size_t j = 0; j < NUM_SUBRELATIONS; ++j) {{
if (result[j] != 0) {{
throw_or_abort(
format(\"Relation \", relation_name, \", subrelation index \", j, \" failed at row \", i));
x = false;
}}
}}
if (!x) {{
return false;
}}
}}
return true;
}};
return evaluate_relation.template operator()<{name}_vm::{name}<FF>>(\"{name}\");
}}
[[nodiscard]] size_t get_num_gates() const {{ return rows.size(); }}
[[nodiscard]] size_t get_circuit_subgroup_size() const
{{
const size_t num_rows = get_num_gates();
const auto num_rows_log2 = static_cast<size_t>(numeric::get_msb64(num_rows));
size_t num_rows_pow2 = 1UL << (num_rows_log2 + (1UL << num_rows_log2 == num_rows ? 0 : 1));
return num_rows_pow2;
}}
}};
}}
");
self.circuit_hpp = Some(circuit_hpp);
}
}
6 changes: 3 additions & 3 deletions bberg/src/composer_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ namespace proof_system::honk {{
class {name}Composer {{
public:
using Flavor = honk::flavor::{name}Flavor;
using CircuitConstructor = {name}TraceBuilder;
using CircuitConstructor = {name}CircuitBuilder;
using ProvingKey = Flavor::ProvingKey;
using VerificationKey = Flavor::VerificationKey;
using PCS = Flavor::PCS;
Expand Down Expand Up @@ -181,7 +181,7 @@ fn cpp_includes(name: &str) -> String {
"
#include \"./{name}_composer.hpp\"
#include \"barretenberg/vm/generated/{name}_verifier.hpp\"
#include \"barretenberg/proof_system/circuit_builder/generated/{name}_trace.hpp\"
#include \"barretenberg/proof_system/circuit_builder/generated/{name}_circuit_builder.hpp\"
#include \"barretenberg/proof_system/composer/composer_lib.hpp\"
#include \"barretenberg/proof_system/composer/permutation_lib.hpp\"
"
Expand All @@ -195,7 +195,7 @@ pub fn hpp_includes(name: &str) -> String {
#include \"barretenberg/vm/generated/{name}_prover.hpp\"
#include \"barretenberg/vm/generated/{name}_verifier.hpp\"
#include \"barretenberg/proof_system/circuit_builder/generated/{name}_trace.hpp\"
#include \"barretenberg/proof_system/circuit_builder/generated/{name}_circuit_builder.hpp\"
#include \"barretenberg/proof_system/composer/composer_lib.hpp\"
#include \"barretenberg/srs/global_crs.hpp\"
"
Expand Down
18 changes: 9 additions & 9 deletions bberg/src/file_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::io::Write;
pub struct BBFiles {
pub relation_hpp: Option<String>,
pub flavor_hpp: Option<String>,
// trace
pub trace_hpp: Option<String>,
// circuit
pub circuit_hpp: Option<String>,
// composer
pub composer_cpp: Option<String>,
pub composer_hpp: Option<String>,
Expand All @@ -23,7 +23,7 @@ pub struct BBFiles {
pub base: String,
pub rel: String,
pub arith: String,
pub trace: String,
pub circuit: String,
pub flavor: String,
pub composer: String,
pub prover: String, // path for both prover and verifier files
Expand All @@ -39,15 +39,15 @@ impl BBFiles {
base: Option<String>,
rel: Option<String>,
arith: Option<String>,
trace: Option<String>,
circuit: Option<String>,
flavor: Option<String>,
composer: Option<String>,
prover: Option<String>,
) -> Self {
let base = base.unwrap_or("src/barretenberg".to_owned());
let rel = rel.unwrap_or("relations/generated".to_owned());
let arith = arith.unwrap_or("proof_system/arithmetization/generated".to_owned());
let trace = trace.unwrap_or("proof_system/circuit_builder/generated".to_owned());
let circuit = circuit.unwrap_or("proof_system/circuit_builder/generated".to_owned());
let flavor = flavor.unwrap_or("flavor/generated".to_owned());
let composer = composer.unwrap_or("vm/generated".to_owned());
let prover = prover.unwrap_or("vm/generated".to_owned());
Expand All @@ -56,7 +56,7 @@ impl BBFiles {
file_name,
relation_hpp: None,
flavor_hpp: None,
trace_hpp: None,
circuit_hpp: None,
composer_cpp: None,
composer_hpp: None,
prover_cpp: None,
Expand All @@ -67,7 +67,7 @@ impl BBFiles {
base,
rel,
arith,
trace,
circuit,
flavor,
composer,
prover,
Expand All @@ -87,8 +87,8 @@ impl BBFiles {
}
write_file!(self.rel, ".hpp", self.relation_hpp);

// Trace
write_file!(self.trace, "_trace.hpp", self.trace_hpp);
// Circuit
write_file!(self.circuit, "_circuit_builder.hpp", self.circuit_hpp);

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

Expand Down
4 changes: 2 additions & 2 deletions bberg/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
mod arith_builder;
pub mod bberg_codegen;
pub mod circuit_builder;
mod circuit_builder;
mod composer_builder;
mod file_writer;
mod flavor_builder;
mod prover_builder;
mod relation_builder;
mod trace_builder;
mod verifier_builder;
pub mod vm_builder;
Loading

0 comments on commit e8ba49b

Please sign in to comment.