Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opt: convert relation checks to futures #59

Merged
merged 11 commits into from
May 13, 2024
68 changes: 62 additions & 6 deletions bberg/src/circuit_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ fn circuit_hpp_includes(name: &str, relations: &[String], permutations: &[String
// AUTOGENERATED FILE
#pragma once

#include <vector>
#ifndef __wasm__
#include <future>
#endif

#include \"barretenberg/common/constexpr_utils.hpp\"
#include \"barretenberg/common/throw_or_abort.hpp\"
#include \"barretenberg/ecc/curves/bn254/fr.hpp\"
Expand Down Expand Up @@ -82,22 +87,42 @@ impl CircuitBuilder for BBFiles {
|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}\", {name}_vm::get_relation_label_{relation_name})) {{
return false;
}}",
"auto {relation_name} = [=]() {{
return evaluate_relation.template operator()<{name}_vm::{relation_name}<FF>>(\"{relation_name}\", {name}_vm::get_relation_label_{relation_name});
}};
",
name = name,
relation_name = relation_name
)
};
let check_lookup_transformation = |lookup_name: &String| {
let lookup_name_upper = lookup_name.to_uppercase();
format!(
"if (!evaluate_logderivative.template operator()<{lookup_name}_relation<FF>>(\"{lookup_name_upper}\")) {{
return false;
}}"
"auto {lookup_name} = [=]() {{
return evaluate_logderivative.template operator()<{lookup_name}_relation<FF>>(\"{lookup_name_upper}\");
}};
"
)
};

// 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 @@ -106,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 @@ -118,6 +151,7 @@ impl CircuitBuilder for BBFiles {
"".to_owned()
};


let circuit_hpp = format!("
{includes}

Expand Down Expand Up @@ -174,6 +208,28 @@ class {name}CircuitBuilder {{

{check_circuit_for_each_lookup}

#ifndef __wasm__

// Evaluate check circuit closures as futures
std::vector<std::future<bool>> relation_futures;

{emplace_future_relations}
{emplace_future_lookups}


// Wait for lookup evaluations to complete
for (auto& future : relation_futures) {{
int result = future.get();
if (!result) {{
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(https://github.com/AztecProtocol/aztec-packages/issues/6361): 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 @@ -253,6 +259,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
Loading