diff --git a/bberg/src/circuit_builder.rs b/bberg/src/circuit_builder.rs index 35eec1508b..595f21dd1b 100644 --- a/bberg/src/circuit_builder.rs +++ b/bberg/src/circuit_builder.rs @@ -24,6 +24,11 @@ fn circuit_hpp_includes(name: &str, relations: &[String], permutations: &[String // AUTOGENERATED FILE #pragma once + #include +#ifndef __wasm__ + #include +#endif + #include \"barretenberg/common/constexpr_utils.hpp\" #include \"barretenberg/common/throw_or_abort.hpp\" #include \"barretenberg/ecc/curves/bn254/fr.hpp\" @@ -82,9 +87,10 @@ 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}>(\"{relation_name}\", {name}_vm::get_relation_label_{relation_name})) {{ - return false; - }}", + "auto {relation_name} = [=]() {{ + return evaluate_relation.template operator()<{name}_vm::{relation_name}>(\"{relation_name}\", {name}_vm::get_relation_label_{relation_name}); + }}; + ", name = name, relation_name = relation_name ) @@ -92,12 +98,31 @@ impl CircuitBuilder for BBFiles { let check_lookup_transformation = |lookup_name: &String| { let lookup_name_upper = lookup_name.to_uppercase(); format!( - "if (!evaluate_logderivative.template operator()<{lookup_name}_relation>(\"{lookup_name_upper}\")) {{ - return false; - }}" + "auto {lookup_name} = [=]() {{ + return evaluate_logderivative.template operator()<{lookup_name}_relation>(\"{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); @@ -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()) @@ -118,6 +151,7 @@ impl CircuitBuilder for BBFiles { "".to_owned() }; + let circuit_hpp = format!(" {includes} @@ -174,6 +208,28 @@ class {name}CircuitBuilder {{ {check_circuit_for_each_lookup} +#ifndef __wasm__ + + // Evaluate check circuit closures as futures + std::vector> 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; }} diff --git a/bberg/src/verifier_builder.rs b/bberg/src/verifier_builder.rs index b1880e2e8e..8276104d2d 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(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 new_points(circuit_size, 0); + std::copy(points.begin(), points.end(), new_points.data()); + + Polynomial polynomial(new_points); return polynomial.evaluate_mle(challenges); }} " @@ -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\" " ) }