From c4392943bb461becf81e50eadc44038d0fe2da97 Mon Sep 17 00:00:00 2001 From: Facundo Date: Mon, 10 Jun 2024 12:10:02 +0100 Subject: [PATCH] generate row deserialization (#68) Used for serialization to CSV. --- bberg/src/circuit_builder.rs | 56 +++++++++++++++++++++++++++++++++++ bberg/src/flavor_builder.rs | 4 +-- bberg/src/relation_builder.rs | 6 +++- bberg/src/vm_builder.rs | 2 ++ bberg_pil_cli/Cargo.toml | 2 +- 5 files changed, 66 insertions(+), 4 deletions(-) diff --git a/bberg/src/circuit_builder.rs b/bberg/src/circuit_builder.rs index 7c99854e37..1f93f0367e 100644 --- a/bberg/src/circuit_builder.rs +++ b/bberg/src/circuit_builder.rs @@ -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: &[String]); } fn circuit_hpp_includes(name: &str, relations: &[String], permutations: &[String]) -> String { @@ -158,6 +160,8 @@ namespace bb {{ {row_with_all_included}; +template std::ostream& operator<<(std::ostream& os, {name}FullRow const& row); + class {name}CircuitBuilder {{ public: using Flavor = bb::{name}Flavor; @@ -254,6 +258,58 @@ class {name}CircuitBuilder {{ &circuit_hpp, ); } + + fn create_circuit_builder_cpp(&mut self, name: &str, all_cols: &[String]) { + let names_list = map_with_newline(all_cols, |name: &String| format!("\"{}\",", name)); + let stream_all_relations = map_with_newline(all_cols, |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 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 std::vector {name}FullRow::names() {{ + return {{ + {names_list} + \"\" + }}; +}} + +template std::ostream& operator<<(std::ostream& os, {name}FullRow const& row) {{ + return os {stream_all_relations} + \"\"; +}} + +// Explicit template instantiation. +template std::ostream& operator<<(std::ostream& os, AvmFullRow const& row); +template std::vector AvmFullRow::names(); + +}} // namespace bb" + ); + + self.write_file( + &self.circuit, + &format!("{}_circuit_builder.cpp", snake_case(name)), + &circuit_cpp, + ); + } } fn get_lookup_check_closure() -> String { diff --git a/bberg/src/flavor_builder.rs b/bberg/src/flavor_builder.rs index 5066a61911..dab4058bdf 100644 --- a/bberg/src/flavor_builder.rs +++ b/bberg/src/flavor_builder.rs @@ -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\" diff --git a/bberg/src/relation_builder.rs b/bberg/src/relation_builder.rs index 6855f21813..90a8ad03d3 100644 --- a/bberg/src/relation_builder.rs +++ b/bberg/src/relation_builder.rs @@ -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 struct {name}Row {{ \n{}\n }}", + "template struct {name}Row {{ + {} + + [[maybe_unused]] static std::vector names(); + }}", all_annotated, ) } diff --git a/bberg/src/vm_builder.rs b/bberg/src/vm_builder.rs index 2b32e22f93..8aee9d02d5 100644 --- a/bberg/src/vm_builder.rs +++ b/bberg/src/vm_builder.rs @@ -115,6 +115,8 @@ pub(crate) fn analyzed_to_cpp( &all_cols_with_shifts, ); + bb_files.create_circuit_builder_cpp(file_name, &all_cols); + // ----------------------- Create the flavor file ----------------------- bb_files.create_flavor_hpp( file_name, diff --git a/bberg_pil_cli/Cargo.toml b/bberg_pil_cli/Cargo.toml index 78bf533024..3232d97205 100644 --- a/bberg_pil_cli/Cargo.toml +++ b/bberg_pil_cli/Cargo.toml @@ -6,7 +6,7 @@ authors = ["Maddiaa"] edition = "2021" [[bin]] -name = "bberg_pil" +name = "bb_pil" path = "src/main.rs" [dependencies]