Skip to content

Commit

Permalink
feat: create and apply map_with_newline utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 committed Dec 5, 2023
1 parent 53c08a0 commit 14418f0
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 111 deletions.
35 changes: 15 additions & 20 deletions bberg/src/circuit_builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
file_writer::BBFiles, relation_builder::create_row_type, utils::get_relations_imports,
file_writer::BBFiles, relation_builder::create_row_type, utils::{get_relations_imports, map_with_newline},
};

pub trait CircuitBuilder {
Expand Down Expand Up @@ -48,31 +48,26 @@ impl CircuitBuilder for BBFiles {
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 check_circuit_for_each_relation = relations
.iter()
.map(|relation_name| {
// Declare mapping transformations
let compute_polys_transformation = |name: &String| format!(
"polys.{name}[i] = rows[i].{name};"
);
let all_polys_transformation = |name: &String| format!(
"polys.{name}_shift = Polynomial(polys.{name}.shifted());"
);
let check_circuit_transformation = |relation_name: &String|
format!(
"if (!evaluate_relation.template operator()<{name}_vm::{relation_name}<FF>>(\"{relation_name}\")) {{
return false;
}}",
name = name,
relation_name = relation_name
)
})
.collect::<Vec<String>>()
.join("\n");
);

// Apply transformations
let compute_polys_assignemnt = map_with_newline(all_cols, compute_polys_transformation);
let all_poly_shifts = map_with_newline(to_be_shifted, all_polys_transformation);
let check_circuit_for_each_relation = map_with_newline(relations, check_circuit_transformation);

let circuit_hpp = format!("
{includes}
Expand Down
18 changes: 11 additions & 7 deletions bberg/src/composer_builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::file_writer::BBFiles;
use crate::{file_writer::BBFiles, utils::map_with_newline};

pub trait ComposerBuilder {
fn create_composer_cpp(&mut self, name: &str, all_cols: &[String]);
Expand All @@ -10,11 +10,7 @@ impl ComposerBuilder for BBFiles {
// 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 map_polys_to_key = create_map_polys_to_key( all_cols);

let composer_cpp = format!(
"
Expand All @@ -31,7 +27,7 @@ void {name}Composer::compute_witness(CircuitConstructor& circuit)
auto polynomials = circuit.compute_polynomials();
{polys_to_key}
{map_polys_to_key}
computed_witness = true;
}}
Expand Down Expand Up @@ -210,3 +206,11 @@ pub fn hpp_includes(name: &str) -> String {
"
)
}

fn create_map_polys_to_key(all_cols: &[String]) -> String {
let map_transformation = |name: &String| format!(
"proving_key->{name} = polynomials.{name};", name = name
);

map_with_newline(all_cols, map_transformation)
}
46 changes: 19 additions & 27 deletions bberg/src/flavor_builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{file_writer::BBFiles, utils::get_relations_imports};
use crate::{file_writer::BBFiles, utils::{get_relations_imports, map_with_newline}};

pub trait FlavorBuilder {
fn create_flavor_hpp(
Expand Down Expand Up @@ -202,7 +202,7 @@ fn container_size_definitions(num_precomputed: usize, num_witness: usize, num_a
fn return_ref_vector(name: &str, columns: &[String]) -> String {
let comma_sep = create_comma_separated(columns);

format!("RefVector<DataType> {name} {{ return {comma_sep}; }};")
format!("RefVector<DataType> {name} {{ return {{ {comma_sep} }}; }};")
}

/// list -> "list[0], list[1], ... list[n-1]"
Expand Down Expand Up @@ -405,11 +405,11 @@ fn create_commitment_labels(all_ents: &[String]) -> String {
}

fn create_key_dereference(fixed: &[String]) -> String {
fixed
.iter()
.map(|name| format!("{name} = verification_key->{name};"))
.collect::<Vec<_>>()
.join("\n")
let deref_transformation = |name: &String| format!(
"{name} = verification_key->{name};"
);

map_with_newline(fixed, deref_transformation)
}

fn create_verifier_commitments(fixed: &[String]) -> String {
Expand All @@ -434,27 +434,19 @@ fn create_verifier_commitments(fixed: &[String]) -> String {
}

fn generate_transcript(witness: &[String]) -> String {
let declarations = witness
.iter()
.map(|f| format!("Commitment {f};"))
.collect::<Vec<_>>()
.join("\n");
let deserialize_wires = witness
.iter()
.map(|name| {
format!(
// Transformations
let declaration_transform = |c: &_| format!("Commitment {c};");
let deserialize_transform = |name: &_| format!(
"{name} = deserialize_from_buffer<Commitment>(BaseTranscript<FF>::proof_data, num_bytes_read);",
)
})
.collect::<Vec<_>>()
.join("\n");
let serialize_wires = witness
.iter()
.map(|name| {
format!("serialize_to_buffer<Commitment>({name}, BaseTranscript<FF>::proof_data);",)
})
.collect::<Vec<_>>()
.join("\n");
);
let serialize_transform = |name: &_| format!(
"serialize_to_buffer<Commitment>({name}, BaseTranscript<FF>::proof_data);"
);

// Perform Transformations
let declarations = map_with_newline(witness, declaration_transform);
let deserialize_wires = map_with_newline(witness, deserialize_transform);
let serialize_wires = map_with_newline(witness, serialize_transform);

format!("
class Transcript : public BaseTranscript<FF> {{
Expand Down
29 changes: 7 additions & 22 deletions bberg/src/prover_builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::file_writer::BBFiles;
use crate::{file_writer::BBFiles, utils::map_with_newline};

pub trait ProverBuilder {
fn create_prover_cpp(&mut self, name: &str, fixed: &[String], to_be_shifted: &[String]);
Expand Down Expand Up @@ -72,32 +72,16 @@ impl ProverBuilder for BBFiles {
let include_str = includes_cpp(name);

// Create the wire assignments, prover_polynomial = key
let fixed_assignments = fixed
.iter()
.map(|name| format!("prover_polynomials.{name} = key->{name};"))
.collect::<Vec<_>>()
.join("\n");

let committed_assignments = to_be_shifted
.iter()
.map(|name| {
let fixed_assignments = map_with_newline(fixed, |name| format!("prover_polynomials.{name} = key->{name};"));
let committed_assignments = map_with_newline(to_be_shifted,
|name| {
format!(
"
prover_polynomials.{name} = key->{name};
prover_polynomials.{name}_shift = key->{name}.shifted();
",
)
})
.collect::<Vec<_>>()
.join("\n");

// Lmao yuck
let all_assignments = format!(
"
{fixed_assignments}
{committed_assignments}
"
);
});

let prover_cpp = format!("
{include_str}
Expand All @@ -120,7 +104,8 @@ prover_polynomials.{name}_shift = key->{name}.shifted();
, commitment_key(commitment_key)
{{
// TODO: take every polynomial and assign it to the key!!
{all_assignments}
{fixed_assignments}
{committed_assignments}
// prover_polynomials.lookup_inverses = key->lookup_inverses;
// key->z_perm = Polynomial(key->circuit_size);
Expand Down
35 changes: 12 additions & 23 deletions bberg/src/relation_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::collections::HashSet;
use number::{DegreeType, FieldElement};

use crate::file_writer::BBFiles;
use crate::utils::map_with_newline;

pub trait RelationBuilder {
fn create_relations(
Expand Down Expand Up @@ -60,14 +61,10 @@ namespace proof_system::{root_name}_vm {{
}

fn create_declare_views(&self, name: &str, all_cols_and_shifts: &[String]) {
let make_view_per_row = all_cols_and_shifts
.iter()
.map(|row_name| {
let name = row_name.replace('.', "_");
format!("[[maybe_unused]] auto {name} = View(new_term.{name}); \\")
})
.collect::<Vec<_>>()
.join("\n");
let view_transformation = |name: &String| format!(
"[[maybe_unused]] auto {name} = View(new_term.{name}); \\"
);
let make_view_per_row = map_with_newline(all_cols_and_shifts, view_transformation);

let declare_views = format!(
"
Expand Down Expand Up @@ -163,25 +160,17 @@ fn relation_includes() -> &'static str {
"#
}

// Yucky that everything is allocated into vecs here
fn create_row_type_items(names: &[String]) -> Vec<String> {
names
.iter()
.map(|name| format!(" FF {} {{}};", name.replace('.', "_")))
.collect::<Vec<_>>()
}

// Each vm will need to have a row which is a combination of all of the witness columns
pub(crate) fn create_row_type(name: &str, all_rows: &[String]) -> String {
let all_annotated = create_row_type_items(all_rows);

let row_type = format!(
"template <typename FF> struct {name}Row {{ \n{}\n }}",
all_annotated.join("\n"),
let row_transformation = |row: &_| format!(
" FF {row} {{row}};"
);
let all_annotated = map_with_newline(all_rows, row_transformation);

println!("{}", row_type);
row_type
format!(
"template <typename FF> struct {name}Row {{ \n{}\n }}",
all_annotated,
)
}

fn create_identity<T: FieldElement>(
Expand Down
22 changes: 15 additions & 7 deletions bberg/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
/// We may have multiple relation files in the generated foler
/// This method will return all of the imports for the relation header files
pub fn get_relations_imports(name: &str, relations: &[String]) -> String {
relations
.iter()
.map(|relation_name| {
format!("#include \"barretenberg/relations/generated/{name}/{relation_name}.hpp\"",)
})
.collect::<Vec<_>>()
.join("\n")
let transformation = |relation_name: &_| format!(
"#include \"barretenberg/relations/generated/{name}/{relation_name}.hpp\""
);

map_with_newline(relations, transformation)
}

/// Sanitize Names
Expand All @@ -28,3 +26,13 @@ pub fn capitalize(s: &String) -> String {
Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
}
}

/// Map With Newline
/// This utility function is used all over the codegen pipeline
/// It takes a list, usually the names of columns in an execution trace and applies a string transformation "op"
/// to each element in the list
pub fn map_with_newline<F>(list: &[String], op: F) -> String
where F: Fn(&String) -> String
{
list.iter().map(op).collect::<Vec<String>>().join("\n")
}
10 changes: 5 additions & 5 deletions bberg/src/verifier_builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::file_writer::BBFiles;
use crate::{file_writer::BBFiles, utils::map_with_newline};

pub trait VerifierBuilder {
fn create_verifier_cpp(&mut self, name: &str, witness: &[String]);
Expand All @@ -10,10 +10,10 @@ impl VerifierBuilder for BBFiles {
fn create_verifier_cpp(&mut self, name: &str, witness: &[String]) {
let include_str = includes_cpp(name);

let wire_commitments = witness.iter().map(|name|{
let n = name.replace('.',"_");
format!("commitments.{n} = transcript.template receive_from_prover<Commitment>(commitment_labels.{n});", n=n)
}).collect::<Vec<String>>().join("\n");
let wire_transformation = |name| format!(
"commitments.{name} = transcript.template receive_from_prover<Commitment>(commitment_labels.{n});"
);
let wire_commitments = map_with_newline(witness, wire_transformation);

let ver_cpp = format!("
{include_str}
Expand Down

0 comments on commit 14418f0

Please sign in to comment.