Skip to content

Commit

Permalink
fix: remove FIRST and LAST requirement (#16)
Browse files Browse the repository at this point in the history
* fix: remove first requirement

* fix: remove last requirement

* fix

* fix
  • Loading branch information
Maddiaa0 authored Nov 15, 2023
1 parent b0ebac8 commit 4635736
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 73 deletions.
17 changes: 1 addition & 16 deletions bberg/src/circuit_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,10 @@ pub(crate) fn analyzed_to_cpp<F: FieldElement>(
.map(|(name, _)| (*name).to_owned())
.collect::<Vec<_>>();

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(&first_col, &last_col, &analyzed_identities);
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) =
Expand Down
34 changes: 8 additions & 26 deletions bberg/src/relation_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,13 @@ 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(last_col, expr, collected_shifts);
let x = craft_expression(expr, collected_shifts);
println!("{:?}", x);
Some(x)
} else {
Expand All @@ -197,31 +196,22 @@ fn create_identity<T: FieldElement>(
}

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

// TODO: TEMP HACK: Part of the main_FIRST hack below - to switch off constraints on the first row
identity.0 += 1;
format!(
"//Contribution {index}
{{\n{preamble}
auto tmp = {id};
tmp *= scaling_factor;
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 @@ -237,14 +227,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} * (-{} + FF(1))", last_col);
poly_name = format!("{poly_name}");
degree += 1;
}
(degree, poly_name)
}
Expression::BinaryOperation(lhe, op, rhe) => {
let (ld, lhs) = craft_expression(last_col, lhe, collected_shifts);
let (rd, rhs) = craft_expression(last_col, rhe, collected_shifts);
let (ld, lhs) = craft_expression(lhe, collected_shifts);
let (rd, rhs) = craft_expression(rhe, collected_shifts);

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

/// Todo, eventually these will need to be siloed based on the file name they are in
pub(crate) 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 @@ -310,8 +298,8 @@ pub(crate) fn create_identities<F: FieldElement>(
);
// TODO: deal with unwrap

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

identities.push(identity);

Expand All @@ -321,9 +309,3 @@ pub(crate) fn create_identities<F: FieldElement>(
// Returning both for now
(subrelations, identities, collected_shifts)
}

//
// Row check_row = { .main_FIRST = 1, .main__block_enforcer_last_step = 1, .main_XIsZero = 1 };
// rows.push_back(check_row);
//
//
36 changes: 5 additions & 31 deletions bberg/src/trace_builder.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,7 @@
use crate::file_writer::BBFiles;

pub trait TraceBuilder {
fn create_trace_builder_hpp(
&mut self,
name: &str,
fixed: &[String],
shifted: &[String],
) -> String;
}

fn trace_cpp_includes(relation_path: &str, name: &str) -> String {
let boilerplate = r#"
#include "barretenberg/ecc/curves/bn254/fr.hpp"
#include <cstdint>
#include <fstream>
#include <iostream>
#include <string>
#include <sys/types.h>
#include <vector>
#include "barretenberg/proof_system/arithmetization/arithmetization.hpp"
"#
.to_owned();

format!(
"
{boilerplate}
#include \"barretenberg/{relation_path}/{name}.hpp\"
#include \"barretenberg/proof_system/arithmetization/generated/{name}_arith.hpp\"
"
)
fn create_trace_builder_hpp(&mut self, name: &str, fixed: &[String], shifted: &[String]);
}

fn trace_hpp_includes(name: &str) -> String {
Expand Down Expand Up @@ -57,7 +30,7 @@ impl TraceBuilder for BBFiles {
name: &str,
all_cols: &[String],
to_be_shifted: &[String],
) -> String {
) {
let includes = trace_hpp_includes(name);

let num_polys = all_cols.len();
Expand All @@ -75,7 +48,7 @@ impl TraceBuilder for BBFiles {
.collect::<Vec<String>>()
.join("\n");

format!("
let trace_hpp = format!("
{includes}
using namespace barretenberg;
Expand Down Expand Up @@ -159,6 +132,7 @@ class {name}TraceBuilder {{
}};
}}
")
");
self.trace_hpp = Some(trace_hpp);
}
}

0 comments on commit 4635736

Please sign in to comment.