Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
fcarreiro committed Jun 10, 2024
1 parent dbc902f commit 416291b
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 4 deletions.
58 changes: 58 additions & 0 deletions bberg/src/circuit_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub trait CircuitBuilder {
to_be_shifted: &[String],
all_cols_with_shifts: &[String],
);

fn create_circuit_builder_cpp(&mut self, name: &str, all_cols_with_shifts: &[String]);
}

fn circuit_hpp_includes(name: &str, relations: &[String], permutations: &[String]) -> String {
Expand Down Expand Up @@ -158,6 +160,8 @@ namespace bb {{
{row_with_all_included};
template <typename FF> std::ostream& operator<<(std::ostream& os, {name}FullRow<FF> const& row);
class {name}CircuitBuilder {{
public:
using Flavor = bb::{name}Flavor;
Expand Down Expand Up @@ -254,6 +258,60 @@ class {name}CircuitBuilder {{
&circuit_hpp,
);
}

fn create_circuit_builder_cpp(&mut self, name: &str, all_cols_with_shifts: &[String]) {
let names_list = map_with_newline(all_cols_with_shifts, |name: &String| {
format!("\"{}\",", name)
});
let stream_all_relations = map_with_newline(all_cols_with_shifts, |name: &String| {
format!("<< field_to_string(row.{}) << \",\"", name)
});
let snake_name = snake_case(name);

let circuit_cpp = format!(
"
#include \"barretenberg/vm/generated/{snake_name}_circuit_builder.hpp\"
namespace bb {{
namespace {{
template <typename FF> std::string field_to_string(const FF& ff)
{{
std::ostringstream os;
os << ff;
std::string raw = os.str();
auto first_not_zero = raw.find_first_not_of('0', 2);
std::string result = \"0x\" + (first_not_zero != std::string::npos ? raw.substr(first_not_zero) : \"0\");
return result;
}}
}} // namespace
template <typename FF> std::vector<std::string> {name}FullRow<FF>::names() {{
return {{
{names_list}
\"\"
}};
}}
template <typename FF> std::ostream& operator<<(std::ostream& os, {name}FullRow<FF> const& row) {{
return os {stream_all_relations}
\"\";
}}
// Explicit template instantiation.
template std::ostream& operator<<(std::ostream& os, AvmFullRow<bb::AvmFlavor::FF> const& row);
template std::vector<std::string> AvmFullRow<bb::AvmFlavor::FF>::names();
}} // namespace bb"
);

self.write_file(
&self.circuit,
&format!("{}_circuit_builder.cpp", snake_case(name)),
&circuit_cpp,
);
}
}

fn get_lookup_check_closure() -> String {
Expand Down
4 changes: 2 additions & 2 deletions bberg/src/flavor_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ fn flavor_includes(name: &str, relation_file_names: &[String], lookups: &[String
let relation_imports = get_relations_imports(name, relation_file_names, lookups);

format!(
"
#pragma once
"#pragma once
#include \"barretenberg/commitment_schemes/kzg/kzg.hpp\"
#include \"barretenberg/ecc/curves/bn254/g1.hpp\"
#include \"barretenberg/flavor/relation_definitions.hpp\"
Expand Down
6 changes: 5 additions & 1 deletion bberg/src/relation_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,11 @@ pub(crate) fn create_row_type(name: &str, all_rows: &[String]) -> String {
let all_annotated = map_with_newline(all_rows, row_transformation);

format!(
"template <typename FF> struct {name}Row {{ \n{}\n }}",
"template <typename FF> struct {name}Row {{
{}
[[maybe_unused]] static std::vector<std::string> names();
}}",
all_annotated,
)
}
Expand Down
2 changes: 2 additions & 0 deletions bberg/src/vm_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ pub(crate) fn analyzed_to_cpp<F: FieldElement>(
&all_cols_with_shifts,
);

bb_files.create_circuit_builder_cpp(file_name, &all_cols_with_shifts);

// ----------------------- Create the flavor file -----------------------
bb_files.create_flavor_hpp(
file_name,
Expand Down
2 changes: 1 addition & 1 deletion bberg_pil_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ authors = ["Maddiaa"]
edition = "2021"

[[bin]]
name = "bberg_pil"
name = "bb_pil"
path = "src/main.rs"

[dependencies]
Expand Down

0 comments on commit 416291b

Please sign in to comment.