Skip to content

Commit

Permalink
chore: sync bb master (#17)
Browse files Browse the repository at this point in the history
* fix: resync master

* fix: reenable opt
  • Loading branch information
Maddiaa0 authored Nov 21, 2023
1 parent 4635736 commit 9015bef
Show file tree
Hide file tree
Showing 10 changed files with 416 additions and 578 deletions.
24 changes: 0 additions & 24 deletions bberg/src/arith_builder.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1 @@
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);
}
}
21 changes: 10 additions & 11 deletions bberg/src/circuit_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ 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::flavor_builder::FlavorBuilder;
Expand All @@ -26,13 +25,13 @@ pub(crate) fn analyzed_to_cpp<F: FieldElement>(
// 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
let fixed_names = fixed
.iter()
.map(|(name, _)| (*name).to_owned())
.map(|(name, _)| (*name.replace(".", "_")).to_owned())
.collect::<Vec<_>>();
let _witness_names = witness
let witness_names = witness
.iter()
.map(|(name, _)| (*name).to_owned())
.map(|(name, _)| (*name.replace(".", "_")).to_owned())
.collect::<Vec<_>>();

// Inlining step to remove the intermediate poly definitions
Expand All @@ -41,7 +40,7 @@ pub(crate) fn analyzed_to_cpp<F: FieldElement>(
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) =
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();

Expand All @@ -56,27 +55,27 @@ pub(crate) fn analyzed_to_cpp<F: FieldElement>(
&all_cols_with_shifts,
);

// ----------------------- Create the arithmetization file -----------------------
bb_files.create_arith_hpp(file_name, num_cols);

// ----------------------- 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);
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, &all_cols);
bb_files.create_verifier_cpp(file_name, &witness_names);
bb_files.create_verifier_hpp(file_name);

// ----------------------- Create the Prover files -----------------------
Expand Down
85 changes: 39 additions & 46 deletions bberg/src/composer_builder.rs
Original file line number Diff line number Diff line change
@@ -1,58 +1,58 @@
use crate::file_writer::BBFiles;

pub trait ComposerBuilder {
fn create_composer_cpp(&mut self, name: &str);
fn create_composer_cpp(&mut self, name: &str, all_cols: &[String]);
fn create_composer_hpp(&mut self, name: &str);
}

impl ComposerBuilder for BBFiles {
fn create_composer_cpp(&mut self, name: &str) {
fn create_composer_cpp(&mut self, name: &str, all_cols: &[String]) {
// Create a composer file, this is used to a prover and verifier for our flavour
let include_str = cpp_includes(name);

let polys_to_key = all_cols
.iter()
.map(|name| format!("proving_key->{name} = polynomials.{name};", name = name))
.collect::<Vec<String>>()
.join("\n");

let composer_cpp = format!(
"
{include_str}
namespace proof_system::honk {{
template <typename Flavor> void {name}Composer_<Flavor>::compute_witness(CircuitConstructor& circuit)
using Flavor = honk::flavor::{name}Flavor;
void {name}Composer::compute_witness(CircuitConstructor& circuit)
{{
if (computed_witness) {{
return;
}}
auto polynomials = circuit.compute_polynomials();
auto key_wires = proving_key->get_wires();
auto poly_wires = polynomials.get_wires();
for (size_t i = 0; i < key_wires.size(); ++i) {{
std::copy(poly_wires[i].begin(), poly_wires[i].end(), key_wires[i].begin());
}}
{polys_to_key}
computed_witness = true;
}}
template <typename Flavor>
{name}Prover_<Flavor> {name}Composer_<Flavor>::create_prover(CircuitConstructor& circuit_constructor)
{name}Prover {name}Composer::create_prover(CircuitConstructor& circuit_constructor)
{{
compute_proving_key(circuit_constructor);
compute_witness(circuit_constructor);
compute_commitment_key(circuit_constructor.get_circuit_subgroup_size());
{name}Prover_<Flavor> output_state(proving_key, commitment_key);
{name}Prover output_state(proving_key, commitment_key);
return output_state;
}}
template <typename Flavor>
{name}Verifier_<Flavor> {name}Composer_<Flavor>::create_verifier(
{name}Verifier {name}Composer::create_verifier(
CircuitConstructor& circuit_constructor)
{{
auto verification_key = compute_verification_key(circuit_constructor);
{name}Verifier_<Flavor> output_state(verification_key);
{name}Verifier output_state(verification_key);
auto pcs_verification_key = std::make_unique<VerifierCommitmentKey>(verification_key->circuit_size, crs_factory_);
Expand All @@ -61,8 +61,7 @@ template <typename Flavor>
return output_state;
}}
template <typename Flavor>
std::shared_ptr<typename Flavor::ProvingKey> {name}Composer_<Flavor>::compute_proving_key(
std::shared_ptr<Flavor::ProvingKey> {name}Composer::compute_proving_key(
CircuitConstructor& circuit_constructor)
{{
if (proving_key) {{
Expand All @@ -72,16 +71,15 @@ std::shared_ptr<typename Flavor::ProvingKey> {name}Composer_<Flavor>::compute_pr
// Initialize proving_key
{{
const size_t subgroup_size = circuit_constructor.get_circuit_subgroup_size();
proving_key = std::make_shared<typename Flavor::ProvingKey>(subgroup_size, 0);
proving_key = std::make_shared<Flavor::ProvingKey>(subgroup_size, 0);
}}
proving_key->contains_recursive_proof = false;
return proving_key;
}}
template <typename Flavor>
std::shared_ptr<typename Flavor::VerificationKey> {name}Composer_<Flavor>::compute_verification_key(
std::shared_ptr<Flavor::VerificationKey> {name}Composer::compute_verification_key(
CircuitConstructor& circuit_constructor)
{{
if (verification_key) {{
Expand All @@ -93,13 +91,11 @@ std::shared_ptr<typename Flavor::VerificationKey> {name}Composer_<Flavor>::compu
}}
verification_key =
std::make_shared<typename Flavor::VerificationKey>(proving_key->circuit_size, proving_key->num_public_inputs);
std::make_shared<Flavor::VerificationKey>(proving_key->circuit_size, proving_key->num_public_inputs);
return verification_key;
}}
template class {name}Composer_<honk::flavor::{name}Flavor>;
}}
");
self.composer_cpp = Some(composer_cpp);
Expand All @@ -113,14 +109,15 @@ template class {name}Composer_<honk::flavor::{name}Flavor>;
{include_str}
namespace proof_system::honk {{
template <typename Flavor> class {name}Composer_ {{
class {name}Composer {{
public:
using Flavor = honk::flavor::{name}Flavor;
using CircuitConstructor = {name}TraceBuilder;
using ProvingKey = typename Flavor::ProvingKey;
using VerificationKey = typename Flavor::VerificationKey;
using PCS = typename Flavor::PCS;
using CommitmentKey = typename Flavor::CommitmentKey;
using VerifierCommitmentKey = typename Flavor::VerifierCommitmentKey;
using ProvingKey = Flavor::ProvingKey;
using VerificationKey = Flavor::VerificationKey;
using PCS = Flavor::PCS;
using CommitmentKey = Flavor::CommitmentKey;
using VerifierCommitmentKey = Flavor::VerifierCommitmentKey;
// TODO: which of these will we really need
static constexpr std::string_view NAME_STRING = \"{name}\";
Expand All @@ -131,7 +128,7 @@ template <typename Flavor> class {name}Composer_ {{
std::shared_ptr<VerificationKey> verification_key;
// The crs_factory holds the path to the srs and exposes methods to extract the srs elements
std::shared_ptr<barretenberg::srs::factories::CrsFactory<typename Flavor::Curve>> crs_factory_;
std::shared_ptr<barretenberg::srs::factories::CrsFactory<Flavor::Curve>> crs_factory_;
// The commitment key is passed to the prover but also used herein to compute the verfication key commitments
std::shared_ptr<CommitmentKey> commitment_key;
Expand All @@ -140,29 +137,29 @@ template <typename Flavor> class {name}Composer_ {{
bool contains_recursive_proof = false;
bool computed_witness = false;
{name}Composer_() requires(std::same_as<Flavor, honk::flavor::{name}Flavor>)
{name}Composer()
{{
crs_factory_ = barretenberg::srs::get_crs_factory();
}}
{name}Composer_(std::shared_ptr<ProvingKey> p_key, std::shared_ptr<VerificationKey> v_key)
{name}Composer(std::shared_ptr<ProvingKey> p_key, std::shared_ptr<VerificationKey> v_key)
: proving_key(std::move(p_key))
, verification_key(std::move(v_key))
{{}}
{name}Composer_({name}Composer_&& other) noexcept = default;
{name}Composer_({name}Composer_ const& other) noexcept = default;
{name}Composer_& operator=({name}Composer_&& other) noexcept = default;
{name}Composer_& operator=({name}Composer_ const& other) noexcept = default;
~{name}Composer_() = default;
{name}Composer({name}Composer&& other) noexcept = default;
{name}Composer({name}Composer const& other) noexcept = default;
{name}Composer& operator=({name}Composer&& other) noexcept = default;
{name}Composer& operator=({name}Composer const& other) noexcept = default;
~{name}Composer() = default;
std::shared_ptr<ProvingKey> compute_proving_key(CircuitConstructor& circuit_constructor);
std::shared_ptr<VerificationKey> compute_verification_key(CircuitConstructor& circuit_constructor);
void compute_witness(CircuitConstructor& circuit_constructor);
{name}Prover_<Flavor> create_prover(CircuitConstructor& circuit_constructor);
{name}Verifier_<Flavor> create_verifier(CircuitConstructor& circuit_constructor);
{name}Prover create_prover(CircuitConstructor& circuit_constructor);
{name}Verifier create_verifier(CircuitConstructor& circuit_constructor);
void add_table_column_selector_poly_to_proving_key(barretenberg::polynomial& small, const std::string& tag);
Expand All @@ -172,9 +169,6 @@ template <typename Flavor> class {name}Composer_ {{
}};
}};
extern template class {name}Composer_<honk::flavor::{name}Flavor>;
using {name}Composer = {name}Composer_<honk::flavor::{name}Flavor>;
}} // namespace proof_system::honk
"
);
Expand All @@ -186,8 +180,7 @@ 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/vm/generated/{name}_verifier.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\"
Expand All @@ -200,8 +193,8 @@ pub fn hpp_includes(name: &str) -> String {
"
#pragma once
#include \"barretenberg/honk/proof_system/generated/{name}_prover.hpp\"
#include \"barretenberg/honk/proof_system/generated/{name}_verifier.hpp\"
#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/composer/composer_lib.hpp\"
#include \"barretenberg/srs/global_crs.hpp\"
Expand Down
11 changes: 4 additions & 7 deletions bberg/src/file_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::io::Write;

pub struct BBFiles {
pub relation_hpp: Option<String>,
pub arith_hpp: Option<String>,
pub flavor_hpp: Option<String>,
// trace
pub trace_hpp: Option<String>,
Expand Down Expand Up @@ -46,17 +45,16 @@ impl BBFiles {
prover: Option<String>,
) -> Self {
let base = base.unwrap_or("src/barretenberg".to_owned());
let rel = rel.unwrap_or("proof_system/relations/generated".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 flavor = flavor.unwrap_or("honk/flavor/generated".to_owned());
let composer = composer.unwrap_or("honk/composer/generated".to_owned());
let prover = prover.unwrap_or("honk/proof_system/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());

Self {
file_name,
relation_hpp: None,
arith_hpp: None,
flavor_hpp: None,
trace_hpp: None,
composer_cpp: None,
Expand Down Expand Up @@ -88,7 +86,6 @@ impl BBFiles {
};
}
write_file!(self.rel, ".hpp", self.relation_hpp);
write_file!(self.arith, "_arith.hpp", self.arith_hpp);

// Trace
write_file!(self.trace, "_trace.hpp", self.trace_hpp);
Expand Down
Loading

0 comments on commit 9015bef

Please sign in to comment.