Skip to content

Commit

Permalink
fix: wasm compatible futures gen
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 committed May 9, 2024
1 parent 7d9ec8c commit 6dc4e60
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
46 changes: 42 additions & 4 deletions bberg/src/circuit_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ fn circuit_hpp_includes(name: &str, relations: &[String], permutations: &[String
#pragma once
#include <vector>
#ifndef __wasm__
#include <future>
#endif
#include \"barretenberg/common/constexpr_utils.hpp\"
#include \"barretenberg/common/throw_or_abort.hpp\"
Expand Down Expand Up @@ -88,7 +90,6 @@ impl CircuitBuilder for BBFiles {
"auto {relation_name} = [=]() {{
return evaluate_relation.template operator()<{name}_vm::{relation_name}<FF>>(\"{relation_name}\", {name}_vm::get_relation_label_{relation_name});
}};
relation_futures.emplace_back(std::async(std::launch::async, {relation_name}));
",
name = name,
relation_name = relation_name
Expand All @@ -100,11 +101,28 @@ impl CircuitBuilder for BBFiles {
"auto {lookup_name} = [=]() {{
return evaluate_logderivative.template operator()<{lookup_name}_relation<FF>>(\"{lookup_name_upper}\");
}};
relation_futures.emplace_back(std::async(std::launch::async, {lookup_name}));
"
)
};

// When we are running natively, we want check circuit to run as futures; however, futures are not supported in wasm, so we must provide an
// alternative codepath that will execute the closures in serial
let emplace_future_transformation = |relation_name: &String| {
format!(
"
relation_futures.emplace_back(std::async(std::launch::async, {relation_name}));
"
)
};

let execute_serial_transformation = |relation_name: &String| {
format!(
"
{relation_name}();
"
)
};

// Apply transformations
let compute_polys_assignemnt =
map_with_newline(all_cols_without_inverses, compute_polys_transformation);
Expand All @@ -113,6 +131,14 @@ impl CircuitBuilder for BBFiles {
map_with_newline(relations, check_circuit_transformation);
let check_circuit_for_each_lookup =
map_with_newline(permutations, check_lookup_transformation);

// With futures
let emplace_future_relations = map_with_newline(relations, emplace_future_transformation);
let emplace_future_lookups = map_with_newline(permutations, emplace_future_transformation);

// With threads
let serial_relations = map_with_newline(relations, execute_serial_transformation);
let serial_lookups = map_with_newline(permutations, execute_serial_transformation);

let (params, lookup_check_closure) = if !permutations.is_empty() {
(get_params(), get_lookup_check_closure())
Expand All @@ -125,6 +151,7 @@ impl CircuitBuilder for BBFiles {
"".to_owned()
};


let circuit_hpp = format!("
{includes}
Expand Down Expand Up @@ -177,12 +204,18 @@ class {name}CircuitBuilder {{
{lookup_check_closure}
{check_circuit_for_each_relation}
{check_circuit_for_each_lookup}
#ifndef __wasm__
// Evaluate check circuit closures as futures
std::vector<std::future<bool>> relation_futures;
{check_circuit_for_each_relation}
{emplace_future_relations}
{emplace_future_lookups}
{check_circuit_for_each_lookup}
// Wait for lookup evaluations to complete
for (auto& future : relation_futures) {{
Expand All @@ -191,6 +224,11 @@ class {name}CircuitBuilder {{
return false;
}}
}}
#else
{serial_relations}
{serial_lookups}
#endif
return true;
}}
Expand Down
13 changes: 10 additions & 3 deletions bberg/src/verifier_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl VerifierBuilder for BBFiles {
let public_inputs_column = public_cols[0].clone(); // asserted to be 1 for the meantime, this will be generalized when required
let inputs_check = format!(
"
FF public_column_evaluation = evaluate_public_input_column(public_inputs, multivariate_challenge);
FF public_column_evaluation = evaluate_public_input_column(public_inputs, circuit_size, multivariate_challenge);
if (public_column_evaluation != claimed_evaluations.{public_inputs_column}) {{
return false;
}}
Expand All @@ -68,8 +68,14 @@ impl VerifierBuilder for BBFiles {
using FF = {name}Flavor::FF;
// Evaluate the given public input column over the multivariate challenge points
[[maybe_unused]] FF evaluate_public_input_column(std::vector<FF> points, std::vector<FF> challenges) {{
Polynomial<FF> polynomial(points);
[[maybe_unused]] inline FF evaluate_public_input_column(std::vector<FF> points, const size_t circuit_size, std::vector<FF> challenges) {{
// TODO: we pad the points to the circuit size in order to get the correct evaluation
// This is not efficient, and will not be valid in production
std::vector<FF> new_points(circuit_size, 0);
std::copy(points.begin(), points.end(), new_points.data());
Polynomial<FF> polynomial(new_points);
return polynomial.evaluate_mle(challenges);
}}
"
Expand Down Expand Up @@ -254,6 +260,7 @@ fn include_hpp(name: &str) -> String {
#include \"barretenberg/plonk/proof_system/types/proof.hpp\"
#include \"barretenberg/sumcheck/sumcheck.hpp\"
#include \"barretenberg/vm/generated/{name}_flavor.hpp\"
#include \"barretenberg/vm/avm_trace/constants.hpp\"
"
)
}
Expand Down

0 comments on commit 6dc4e60

Please sign in to comment.