From 6dc4e604d53e38b880143318e522a29e12ace200 Mon Sep 17 00:00:00 2001 From: Maddiaa0 <47148561+Maddiaa0@users.noreply.github.com> Date: Thu, 9 May 2024 17:41:09 +0000 Subject: [PATCH] fix: wasm compatible futures gen --- bberg/src/circuit_builder.rs | 46 ++++++++++++++++++++++++++++++++--- bberg/src/verifier_builder.rs | 13 +++++++--- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/bberg/src/circuit_builder.rs b/bberg/src/circuit_builder.rs index 04da4e8992..595f21dd1b 100644 --- a/bberg/src/circuit_builder.rs +++ b/bberg/src/circuit_builder.rs @@ -25,7 +25,9 @@ fn circuit_hpp_includes(name: &str, relations: &[String], permutations: &[String #pragma once #include +#ifndef __wasm__ #include +#endif #include \"barretenberg/common/constexpr_utils.hpp\" #include \"barretenberg/common/throw_or_abort.hpp\" @@ -88,7 +90,6 @@ impl CircuitBuilder for BBFiles { "auto {relation_name} = [=]() {{ return evaluate_relation.template operator()<{name}_vm::{relation_name}>(\"{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 @@ -100,11 +101,28 @@ impl CircuitBuilder for BBFiles { "auto {lookup_name} = [=]() {{ return evaluate_logderivative.template operator()<{lookup_name}_relation>(\"{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); @@ -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()) @@ -125,6 +151,7 @@ impl CircuitBuilder for BBFiles { "".to_owned() }; + let circuit_hpp = format!(" {includes} @@ -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> 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) {{ @@ -191,6 +224,11 @@ class {name}CircuitBuilder {{ return false; }} }} +#else + {serial_relations} + {serial_lookups} + +#endif return true; }} diff --git a/bberg/src/verifier_builder.rs b/bberg/src/verifier_builder.rs index 203eb84025..fcbc42248c 100644 --- a/bberg/src/verifier_builder.rs +++ b/bberg/src/verifier_builder.rs @@ -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; }} @@ -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 points, std::vector challenges) {{ - Polynomial polynomial(points); + [[maybe_unused]] inline FF evaluate_public_input_column(std::vector points, const size_t circuit_size, std::vector 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 new_points(circuit_size, 0); + std::copy(points.begin(), points.end(), new_points.data()); + + Polynomial polynomial(new_points); return polynomial.evaluate_mle(challenges); }} " @@ -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\" " ) }