Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 40 additions & 20 deletions bberg/src/circuit_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use pil_analyzer::pil_analyzer::inline_intermediate_polynomials;

use crate::prover_builder::{prover_builder_cpp, prover_builder_hpp};
use crate::verifier_builder::{verifier_builder_cpp, verifier_builder_hpp};
use crate::FILE_NAME;
use crate::{
composer_builder::{composer_builder_cpp, composer_builder_hpp},
flavor_builder,
Expand All @@ -32,7 +33,6 @@ pub struct BBFiles {
pub flavor_hpp: Option<String>,
// trace
pub trace_hpp: Option<String>,
pub trace_cpp: Option<String>,
// composer
pub composer_cpp: Option<String>,
pub composer_hpp: Option<String>,
Expand Down Expand Up @@ -85,7 +85,6 @@ impl BBFiles {
arith_hpp: None,
flavor_hpp: None,
trace_hpp: None,
trace_cpp: None,
composer_cpp: None,
composer_hpp: None,
prover_cpp: None,
Expand All @@ -108,7 +107,6 @@ impl BBFiles {
relation_hpp: String,
arith_hpp: String,
trace_hpp: String,
trace_cpp: String,
flavor_hpp: String,
composer_cpp: String,
composer_hpp: String,
Expand All @@ -124,7 +122,6 @@ impl BBFiles {
self.composer_hpp = Some(composer_hpp);

self.trace_hpp = Some(trace_hpp);
self.trace_cpp = Some(trace_cpp);

self.verifier_cpp = Some(verifier_cpp);
self.verifier_hpp = Some(verifier_hpp);
Expand All @@ -149,7 +146,6 @@ impl BBFiles {

// Trace
write_file!(self.trace, "_trace.hpp", self.trace_hpp);
write_file!(self.trace, "_trace.cpp", self.trace_cpp);

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

Expand Down Expand Up @@ -183,7 +179,8 @@ pub(crate) fn analyzed_to_cpp<F: FieldElement>(
fixed: &[(&str, Vec<F>)],
witness: &[(&str, Vec<F>)],
) -> BBFiles {
let file_name: &str = "BrilligVM";
let file_name: &str = FILE_NAME;

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

// Collect all column names and determine if they need a shift or not
Expand All @@ -198,10 +195,27 @@ pub(crate) fn analyzed_to_cpp<F: FieldElement>(
.map(|(name, _)| (*name).to_owned())
.collect::<Vec<_>>();

println!("Fixed: {:?}", fixed_names);
println!("Witness: {:?}", witness_names);
let first_col = fixed
.iter()
.find(|col_name| col_name.0.contains("FIRST"))
.expect("PIL file must contain a fixed column named FIRST")
.0
.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(".", "_");

// 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 (subrelations, identities, mut collected_shifts) =
create_identities(&first_col, &last_col, &analyzed_identities);
let shifted_polys: Vec<String> = collected_shifts.drain().collect_vec();
dbg!(shifted_polys.clone());

Expand All @@ -224,15 +238,13 @@ pub(crate) fn analyzed_to_cpp<F: FieldElement>(
let arith_hpp = create_arith_boilerplate_file(file_name, num_cols);

// ----------------------- Create the read from powdr columns file -----------------------
let trace_cpp =
bb_files.create_trace_builder_cpp(file_name, &fixed_names, &witness_names, &to_be_shifted);
let trace_hpp = 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(
file_name,
&subrelations,
&unshifted,
&all_cols,
&to_be_shifted,
// &shifted,
);
Expand All @@ -253,7 +265,6 @@ pub(crate) fn analyzed_to_cpp<F: FieldElement>(
relation_hpp,
arith_hpp,
trace_hpp,
trace_cpp,
flavor_hpp,
composer_cpp,
composer_hpp,
Expand Down Expand Up @@ -493,13 +504,14 @@ fn get_cols_in_identity_macro(all_rows_and_shifts: &[String]) -> String {
}

fn create_identity<T: FieldElement>(
last_col: &str,
expression: &SelectedExpressions<Expression<T>>,
collected_shifts: &mut HashSet<String>,
) -> Option<BBIdentity> {
// We want to read the types of operators and then create the appropiate code

if let Some(expr) = &expression.selector {
let x = craft_expression(expr, collected_shifts);
let x = craft_expression(last_col, expr, collected_shifts);
println!("{:?}", x);
Some(x)
} else {
Expand All @@ -508,7 +520,12 @@ fn create_identity<T: FieldElement>(
}

// TODO: replace the preamble with a macro so the code looks nicer
fn create_subrelation(index: usize, preamble: String, identity: &mut BBIdentity) -> String {
fn create_subrelation(
first_col: &str,
index: usize,
preamble: String,
identity: &mut BBIdentity,
) -> String {
// \\\
let id = &identity.1;

Expand All @@ -520,13 +537,14 @@ fn create_subrelation(index: usize, preamble: String, identity: &mut BBIdentity)

auto tmp = {id};
tmp *= scaling_factor;
tmp *= (-main_FIRST+ FF(1)); // Temp to switch off
tmp *= (-{first_col} + FF(1)); // Temp to switch off
std::get<{index}>(evals) += tmp;
}}",
)
}

fn craft_expression<T: FieldElement>(
last_col: &str,
expr: &Expression<T>,
collected_shifts: &mut HashSet<String>,
) -> BBIdentity {
Expand All @@ -543,14 +561,14 @@ fn craft_expression<T: FieldElement>(
poly_name = format!("{}_shift", poly_name);

// TODO(HORRIBLE): TEMP, add in a relation that turns off shifts on the last row
poly_name = format!("{poly_name} * (-main_LAST + FF(1))");
poly_name = format!("{poly_name} * (-{} + FF(1))", last_col);
degree += 1;
}
(degree, poly_name)
}
Expression::BinaryOperation(lhe, op, rhe) => {
let (ld, lhs) = craft_expression(lhe, collected_shifts);
let (rd, rhs) = craft_expression(rhe, collected_shifts);
let (ld, lhs) = craft_expression(last_col, lhe, collected_shifts);
let (rd, rhs) = craft_expression(last_col, rhe, collected_shifts);

// dbg!(&lhe);
let degree = std::cmp::max(ld, rd);
Expand All @@ -576,7 +594,7 @@ fn craft_expression<T: FieldElement>(
// }
Expression::UnaryOperation(operator, expression) => match operator {
AlgebraicUnaryOperator::Minus => {
let (d, e) = craft_expression(expression, collected_shifts);
let (d, e) = craft_expression(last_col, expression, collected_shifts);
(d, format!("-{}", e))
}
_ => unimplemented!("{:?}", expr),
Expand All @@ -591,6 +609,8 @@ type BBIdentity = (DegreeType, String);

/// Todo, eventually these will need to be siloed based on the file name they are in
fn create_identities<F: FieldElement>(
first_col: &str,
last_col: &str,
identities: &Vec<Identity<Expression<F>>>,
) -> (Vec<String>, Vec<BBIdentity>, HashSet<String>) {
// We only want the expressions for now
Expand All @@ -617,8 +637,8 @@ fn create_identities<F: FieldElement>(
);
// TODO: deal with unwrap

let mut identity = create_identity(expression, &mut collected_shifts).unwrap();
let subrelation = create_subrelation(i, relation_boilerplate, &mut identity);
let mut identity = create_identity(last_col, expression, &mut collected_shifts).unwrap();
let subrelation = create_subrelation(first_col, i, relation_boilerplate, &mut identity);

identities.push(identity);

Expand Down
2 changes: 2 additions & 0 deletions bberg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ pub mod prover_builder;
pub mod relation_builder;
pub mod trace_builder;
pub mod verifier_builder;

const FILE_NAME: &str = "Fib";
132 changes: 2 additions & 130 deletions bberg/src/trace_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,61 +50,13 @@ fn trace_hpp_includes(name: &str) -> String {
#include \"barretenberg/proof_system/arithmetization/arithmetization.hpp\"
#include \"barretenberg/proof_system/circuit_builder/circuit_builder_base.hpp\"

#include \"./{name}_trace.cpp\"
#include \"barretenberg/honk/flavor/generated/{name}_flavor.hpp\"
#include \"barretenberg/proof_system/arithmetization/generated/{name}_arith.hpp\"
#include \"barretenberg/proof_system/relations/generated/{name}.hpp\"
"
)
}

fn build_shifts(fixed: &[String]) -> String {
let shift_assign: Vec<String> = fixed
.iter()
.map(|name| format!("row.{name}_shift = rows[(i) % rows.size()].{name};"))
.collect();

format!(
"
for (size_t i = 1; i < rows.size(); ++i) {{
Row& row = rows[i-1];
{}

}}
",
shift_assign.join("\n")
)
}

fn build_empty_row(all_cols: &[String]) -> String {
// The empty row turns off all constraints when the ISLAST flag is set
// We must check that this column exists, and return an error to the user if it is not found
let is_last = all_cols.iter().find(|name| name.contains("ISLAST"));
if is_last.is_none() {
// TODO: make error
panic!("ISLAST column not found in witness");
}
let is_last = is_last.unwrap();

let initialize = all_cols
.iter()
.filter(|name| !name.contains("ISLAST")) // filter out islast
.map(|name| format!("empty_row.{name} = fr(0);"))
.collect::<Vec<_>>()
.join("\n");

format!(
"
auto empty_row = Row{{}};
empty_row.{is_last} = fr(1);
{initialize}
rows.push_back(empty_row);


"
)
}

impl TraceBuilder for BBFiles {
// Create trace builder
// Generate some code that can read a commits.bin and constants.bin into data structures that bberg understands
Expand All @@ -113,7 +65,7 @@ impl TraceBuilder for BBFiles {
name: &str,
fixed: &[String],
witness: &[String],
to_be_shifted: &[String],
_to_be_shifted: &[String],
) -> String {
// We are assuming that the order of the columns in the trace file is the same as the order in the witness file
let includes = trace_cpp_includes(&self.rel, name);
Expand Down Expand Up @@ -157,28 +109,6 @@ impl TraceBuilder for BBFiles {
.collect::<Vec<String>>()
.join("\n");

let fixed_rows = fixed
.iter()
.map(|name| {
let n = name.replace('.', "_");
format!("current_row.{n} = read_field(constant_file);")
})
.collect::<Vec<_>>()
.join("\n");

let wit_rows = &witness_name
.iter()
.map(|n| {
format!(
"
current_row.{n} = read_field(commited_file);"
)
})
.collect::<Vec<_>>()
.join("\n");

let construct_shifts = build_shifts(to_be_shifted);

// NOTE: can we assume that the witness filename etc will stay the same?
let read_from_file_boilerplate = format!(
"
Expand All @@ -190,57 +120,6 @@ using namespace barretenberg;
namespace proof_system {{

{row_import}
inline fr read_field(std::ifstream& file)
{{
uint8_t buffer[32];
file.read(reinterpret_cast<char*>(buffer), 32);

// swap it to big endian ???? TODO: create utility
for (int n = 0, m = 31; n < m; ++n, --m) {{
std::swap(buffer[n], buffer[m]);
}}

return fr::serialize_from_buffer(buffer);
}}

inline std::vector<Row> read_both_file_into_cols(
std::string const& commited_filename,
std::string const& constants_filename
) {{
std::vector<Row> rows;

// open both files
std::ifstream commited_file(commited_filename, std::ios::binary);
if (!commited_file) {{
std::cout << \"Error opening commited file\" << std::endl;
return {{}};
}}

std::ifstream constant_file(constants_filename, std::ios::binary);
if (!constant_file) {{
std::cout << \"Error opening constant file\" << std::endl;
return {{}};
}}

// We are assuming that the two files are the same length
while (commited_file) {{
Row current_row = {{}};

{fixed_rows}
{wit_rows}

rows.push_back(current_row);
}}

// remove the last row - TODO: BUG!
rows.pop_back();

// Build out shifts from collected rows
{construct_shifts}


return rows;
}}

}}
"
Expand Down Expand Up @@ -294,11 +173,7 @@ class {name}TraceBuilder {{
static constexpr size_t num_polys = {num_polys};
std::vector<Row> rows;


[[maybe_unused]] void build_circuit() {{
rows = read_both_file_into_cols(\"../commits.bin\", \"../constants.bin\");
}}

[[maybe_unused]] void build_circuit();

AllPolynomials compute_polynomials() {{
const auto num_rows = get_circuit_subgroup_size();
Expand All @@ -319,9 +194,6 @@ class {name}TraceBuilder {{
}}

[[maybe_unused]] bool check_circuit() {{
// Get the rows from file
build_circuit();

auto polys = compute_polynomials();
const size_t num_rows = polys[0].size();

Expand Down
Loading