forked from powdr-labs/powdr
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from AztecProtocol/jm/new-interface
BBerg code generated: new interface to pass a trace to circuit_builder
- Loading branch information
Showing
7 changed files
with
291 additions
and
292 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.