diff --git a/barretenberg/cpp/src/barretenberg/benchmark/CMakeLists.txt b/barretenberg/cpp/src/barretenberg/benchmark/CMakeLists.txt index 145bd1de14ce..b91a5f54f497 100644 --- a/barretenberg/cpp/src/barretenberg/benchmark/CMakeLists.txt +++ b/barretenberg/cpp/src/barretenberg/benchmark/CMakeLists.txt @@ -6,7 +6,6 @@ add_subdirectory(bb_cli_bench) add_subdirectory(client_ivc_bench) add_subdirectory(pippenger_bench) add_subdirectory(plonk_bench) -add_subdirectory(simulator_bench) add_subdirectory(protogalaxy_bench) add_subdirectory(protogalaxy_rounds_bench) add_subdirectory(relations_bench) diff --git a/barretenberg/cpp/src/barretenberg/benchmark/simulator_bench/CMakeLists.txt b/barretenberg/cpp/src/barretenberg/benchmark/simulator_bench/CMakeLists.txt deleted file mode 100644 index caff4f1c2bc1..000000000000 --- a/barretenberg/cpp/src/barretenberg/benchmark/simulator_bench/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ - barretenberg_module(simulator_bench stdlib_honk_verifier stdlib_sha256 stdlib_pedersen_hash) \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/benchmark/simulator_bench/simulator.bench.cpp b/barretenberg/cpp/src/barretenberg/benchmark/simulator_bench/simulator.bench.cpp deleted file mode 100644 index cd19252f6833..000000000000 --- a/barretenberg/cpp/src/barretenberg/benchmark/simulator_bench/simulator.bench.cpp +++ /dev/null @@ -1,137 +0,0 @@ -#include "barretenberg/goblin/goblin.hpp" -#include "barretenberg/goblin/mock_circuits.hpp" -#include "barretenberg/stdlib_circuit_builders/ultra_circuit_builder.hpp" -#include "barretenberg/ultra_honk/ultra_prover.hpp" -#include "barretenberg/ultra_honk/ultra_verifier.hpp" -#include - -using namespace benchmark; -using namespace bb; - -namespace { -template class SimulatorFixture : public benchmark::Fixture { - - public: - using Flavor = typename RecursiveFlavor::NativeFlavor; - using DeciderProvingKey = DeciderProvingKey_; - using Builder = typename Flavor::CircuitBuilder; - using VerificationKey = typename Flavor::VerificationKey; - using CircuitSimulator = typename RecursiveFlavor::CircuitBuilder; - using SimulatingVerifier = stdlib::recursion::honk::UltraRecursiveVerifier_; - using SimulatorAggregationObject = stdlib::recursion::aggregation_state; - - struct VerifierInput { - HonkProof proof; - std::shared_ptr verification_key; - }; - - void SetUp([[maybe_unused]] const ::benchmark::State& state) override - { - bb::srs::init_crs_factory(bb::srs::get_ignition_crs_path()); - } - - /** - * @brief Create a Honk proof (either Ultra or Mega) for a non-trivial circuit. - * - * @param large determines whether the circuit is 2^17 or 2^19 - */ - static VerifierInput create_proof(bool large = false) - { - - auto builder = construct_mock_function_circuit(large); - auto proving_key = std::make_shared(builder); - UltraProver_ prover(proving_key); - auto ultra_proof = prover.construct_proof(); - auto verification_key = std::make_shared(proving_key->proving_key); - return { ultra_proof, verification_key }; - } - - /** - * @brief Populate the builder with non-trivial operations that mock a circuit encountered in practice. - * - * @param large determines whether the circuit is 2^17 or 2^19 - */ - static Builder construct_mock_function_circuit(bool large = false) - { - using InnerCurve = bb::stdlib::bn254; - using fr_ct = InnerCurve::ScalarField; - using point_ct = InnerCurve::AffineElement; - using fr = typename InnerCurve::ScalarFieldNative; - using point = typename InnerCurve::GroupNative::affine_element; - Builder builder; - - // Perform a batch mul which will add some arbitrary goblin-style ECC op gates if the circuit arithmetic is - // goblinisied otherwise it will add the conventional nonnative gates - size_t num_points = 5; - std::vector circuit_points; - std::vector circuit_scalars; - for (size_t i = 0; i < num_points; ++i) { - circuit_points.push_back(point_ct::from_witness(&builder, point::random_element())); - circuit_scalars.push_back(fr_ct::from_witness(&builder, fr::random_element())); - } - point_ct::batch_mul(circuit_points, circuit_scalars); - - // Determine number of times to execute the below operations that constitute the mock circuit logic. Note - // that the circuit size does not scale linearly with number of iterations due to e.g. amortization of lookup - - const size_t NUM_ITERATIONS_LARGE = 12; // results in circuit size 2^19 (502238 gates) - const size_t NUM_ITERATIONS_MEDIUM = 3; // results in circuit size 2^17 (124843 gates) - const size_t NUM_ITERATIONS = large ? NUM_ITERATIONS_LARGE : NUM_ITERATIONS_MEDIUM; - - stdlib::generate_sha256_test_circuit(builder, NUM_ITERATIONS); // min gates: ~39k - stdlib::generate_ecdsa_verification_test_circuit(builder, NUM_ITERATIONS); // min gates: ~41k - stdlib::generate_merkle_membership_test_circuit(builder, NUM_ITERATIONS); // min gates: ~29k - - return builder; - } -}; - -BENCHMARK_TEMPLATE_F(SimulatorFixture, GoblinNative, bb::MegaRecursiveFlavor_) -(benchmark::State& state) -{ - auto verifier_input = SimulatorFixture::create_proof(); - for (auto _ : state) { - UltraVerifier_ ultra_verifier{ verifier_input.verification_key }; - ultra_verifier.verify_proof((verifier_input.proof)); - } -} - -BENCHMARK_TEMPLATE_F(SimulatorFixture, GoblinSimulated, bb::MegaRecursiveFlavor_) -(benchmark::State& state) -{ - auto verifier_input = SimulatorFixture::create_proof(); - for (auto _ : state) { - CircuitSimulator simulator; - SimulatingVerifier ultra_verifier{ &simulator, verifier_input.verification_key }; - ultra_verifier.verify_proof(verifier_input.proof, SimulatorAggregationObject::construct_default(simulator)); - } -} - -BENCHMARK_TEMPLATE_F(SimulatorFixture, UltraNative, bb::UltraRecursiveFlavor_) -(benchmark::State& state) -{ - auto verifier_input = SimulatorFixture::create_proof(); - for (auto _ : state) { - UltraVerifier_ ultra_verifier{ verifier_input.verification_key }; - ultra_verifier.verify_proof((verifier_input.proof)); - } -} - -BENCHMARK_TEMPLATE_F(SimulatorFixture, UltraSimulated, bb::UltraRecursiveFlavor_) -(benchmark::State& state) -{ - auto verifier_input = SimulatorFixture::create_proof(); - for (auto _ : state) { - CircuitSimulator simulator; - SimulatingVerifier ultra_verifier{ &simulator, verifier_input.verification_key }; - ultra_verifier.verify_proof(verifier_input.proof, SimulatorAggregationObject::construct_default(simulator)); - } -} - -BENCHMARK_REGISTER_F(SimulatorFixture, GoblinSimulated)->Unit(benchmark::kMillisecond); -BENCHMARK_REGISTER_F(SimulatorFixture, UltraSimulated)->Unit(benchmark::kMillisecond); -BENCHMARK_REGISTER_F(SimulatorFixture, GoblinNative)->Unit(benchmark::kMillisecond); -BENCHMARK_REGISTER_F(SimulatorFixture, UltraNative)->Unit(benchmark::kMillisecond); - -} // namespace -BENCHMARK_MAIN(); diff --git a/barretenberg/cpp/src/barretenberg/circuit_checker/circuit_checker.hpp b/barretenberg/cpp/src/barretenberg/circuit_checker/circuit_checker.hpp index 5b619776f3af..000438c4348e 100644 --- a/barretenberg/cpp/src/barretenberg/circuit_checker/circuit_checker.hpp +++ b/barretenberg/cpp/src/barretenberg/circuit_checker/circuit_checker.hpp @@ -12,8 +12,7 @@ concept IsCheckable = bb::IsAnyOf, StandardCircuitBuilder_, UltraCircuitBuilder, - MegaCircuitBuilder, - CircuitSimulatorBN254>; + MegaCircuitBuilder>; /** * @brief The unified interface for check circuit functionality implemented in the specialized CircuitChecker classes @@ -28,8 +27,6 @@ class CircuitChecker { return UltraCircuitChecker::check(builder); } else if constexpr (IsStandardBuilder) { return StandardCircuitChecker::check(builder); - } else if constexpr (IsSimulator) { - return SimulatorCircuitChecker::check(builder); } else { return false; } diff --git a/barretenberg/cpp/src/barretenberg/flavor/flavor.hpp b/barretenberg/cpp/src/barretenberg/flavor/flavor.hpp index 77b2e8e4a4cd..e3fedf3049a1 100644 --- a/barretenberg/cpp/src/barretenberg/flavor/flavor.hpp +++ b/barretenberg/cpp/src/barretenberg/flavor/flavor.hpp @@ -400,7 +400,6 @@ template concept IsMegaFlavor = IsAnyOf, MegaRecursiveFlavor_, - MegaRecursiveFlavor_, MegaZKRecursiveFlavor_, MegaZKRecursiveFlavor_>; @@ -413,16 +412,13 @@ concept HasIPAAccumulator = IsAnyOf concept IsRecursiveFlavor = IsAnyOf, UltraRecursiveFlavor_, - UltraRecursiveFlavor_, UltraRollupRecursiveFlavor_, MegaRecursiveFlavor_, MegaRecursiveFlavor_, - MegaRecursiveFlavor_, MegaZKRecursiveFlavor_, MegaZKRecursiveFlavor_, TranslatorRecursiveFlavor_, TranslatorRecursiveFlavor_, - TranslatorRecursiveFlavor_, ECCVMRecursiveFlavor_, AvmRecursiveFlavor_, AvmRecursiveFlavor_, @@ -446,11 +442,9 @@ template concept IsFoldingFlavor = IsAnyOf, UltraRecursiveFlavor_, - UltraRecursiveFlavor_, UltraRollupRecursiveFlavor_, MegaRecursiveFlavor_, MegaRecursiveFlavor_, - MegaRecursiveFlavor_, MegaZKRecursiveFlavor_, MegaZKRecursiveFlavor_>; #else @@ -464,11 +458,9 @@ template concept IsFoldingFlavor = IsAnyOf, UltraRecursiveFlavor_, - UltraRecursiveFlavor_, UltraRollupRecursiveFlavor_, MegaRecursiveFlavor_, MegaRecursiveFlavor_, - MegaRecursiveFlavor_, MegaZKRecursiveFlavor_, MegaZKRecursiveFlavor_>; #endif diff --git a/barretenberg/cpp/src/barretenberg/stdlib/hash/keccak/keccak.cpp b/barretenberg/cpp/src/barretenberg/stdlib/hash/keccak/keccak.cpp index dd834ae24afd..05fffdc002a4 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/hash/keccak/keccak.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/hash/keccak/keccak.cpp @@ -831,10 +831,6 @@ stdlib::byte_array keccak::hash(byte_array_ct& input, const ui return output; }; - if constexpr (IsSimulator) { - return constant_case(); - } - if (ctx == nullptr) { return constant_case(); } @@ -921,7 +917,6 @@ template void generate_keccak_test_circuit(Builder& builder, } } -template class keccak; template class keccak; template class keccak; template void generate_keccak_test_circuit(bb::UltraCircuitBuilder&, size_t); diff --git a/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.cpp b/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.cpp index 3b4d21c0a522..01ebd680622f 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.cpp @@ -47,6 +47,5 @@ template field_t poseidon2::hash_buffer(C& builder, const std } template class poseidon2; template class poseidon2; -template class poseidon2; } // namespace bb::stdlib diff --git a/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.test.cpp b/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.test.cpp index 8ffdbb9c315d..50c0319790ac 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.test.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.test.cpp @@ -147,7 +147,7 @@ template class StdlibPoseidon2 : public testing::Test { } }; -using CircuitTypes = testing::Types; +using CircuitTypes = testing::Types; TYPED_TEST_SUITE(StdlibPoseidon2, CircuitTypes); diff --git a/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2_permutation.cpp b/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2_permutation.cpp index 5b95e84fda46..d0e793209b3f 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2_permutation.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2_permutation.cpp @@ -23,7 +23,6 @@ namespace bb::stdlib { template typename Poseidon2Permutation::State Poseidon2Permutation::permutation( Builder* builder, const typename Poseidon2Permutation::State& input) - requires(!IsSimulator) { // deep copy State current_state(input); @@ -115,95 +114,6 @@ typename Poseidon2Permutation::State Poseidon2Permutation -typename Poseidon2Permutation::State Poseidon2Permutation::permutation( - Builder* builder, const typename Poseidon2Permutation::State& input) - requires IsSimulator -{ - // deep copy - State current_state(input); - - // Apply 1st linear layer - matrix_multiplication_external(builder, current_state); - - // First set of external rounds - constexpr size_t rounds_f_beginning = rounds_f / 2; - for (size_t i = 0; i < rounds_f_beginning; ++i) { - add_round_constants(current_state, round_constants[i]); - apply_sbox(current_state); - matrix_multiplication_external(builder, current_state); - } - - // Internal rounds - const size_t p_end = rounds_f_beginning + rounds_p; - for (size_t i = rounds_f_beginning; i < p_end; ++i) { - current_state[0] += round_constants[i][0]; - apply_single_sbox(current_state[0]); - matrix_multiplication_internal(current_state); - } - - // Remaining external rounds - for (size_t i = p_end; i < NUM_ROUNDS; ++i) { - add_round_constants(current_state, round_constants[i]); - apply_sbox(current_state); - matrix_multiplication_external(builder, current_state); - } - return current_state; -} - -template -void Poseidon2Permutation::add_round_constants( - State& input, const typename Poseidon2Permutation::RoundConstants& rc) - requires IsSimulator - -{ - for (size_t i = 0; i < t; ++i) { - input[i] += rc[i]; - } -} - -template -void Poseidon2Permutation::apply_sbox(State& input) - requires IsSimulator -{ - for (auto& in : input) { - apply_single_sbox(in); - } -} - -template -void Poseidon2Permutation::apply_single_sbox(field_t& input) - requires IsSimulator -{ - // hardcoded assumption that d = 5. should fix this or not make d configurable - auto xx = input.sqr(); - auto xxxx = xx.sqr(); - input *= xxxx; -} - -template -void Poseidon2Permutation::matrix_multiplication_internal(State& input) - requires IsSimulator -{ - // for t = 4 - auto sum = input[0]; - for (size_t i = 1; i < t; ++i) { - sum += input[i]; - } - for (size_t i = 0; i < t; ++i) { - input[i] *= Params::internal_matrix_diagonal[i]; // internal_matrix_diagonal[i]; - input[i] += sum; - } -} - /** * @brief Separate function to do just the first linear layer (equivalent to external matrix mul). * @details We use 6 arithmetic gates to implement: @@ -318,6 +228,5 @@ void Poseidon2Permutation::matrix_multiplication_external( template class Poseidon2Permutation; template class Poseidon2Permutation; -template class Poseidon2Permutation; -} // namespace bb::stdlib \ No newline at end of file +} // namespace bb::stdlib diff --git a/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2_permutation.hpp b/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2_permutation.hpp index 287fd260d256..25122eeae134 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2_permutation.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2_permutation.hpp @@ -50,19 +50,7 @@ template class Poseidon2Permutation { * @param input * @return State */ - static State permutation(Builder* builder, const State& input) - requires(!IsSimulator); - static State permutation(Builder* builder, const State& input) - requires IsSimulator; - - static void add_round_constants(State& input, const RoundConstants& rc) - requires IsSimulator; - static void apply_sbox(State& input) - requires IsSimulator; - static void apply_single_sbox(field_t& input) - requires IsSimulator; - static void matrix_multiplication_internal(State& input) - requires IsSimulator; + static State permutation(Builder* builder, const State& input); /** * @brief Separate function to do just the first linear layer (equivalent to external matrix mul). @@ -80,4 +68,4 @@ template class Poseidon2Permutation { static void matrix_multiplication_external(Builder* builder, State& state); }; -} // namespace bb::stdlib \ No newline at end of file +} // namespace bb::stdlib diff --git a/barretenberg/cpp/src/barretenberg/stdlib/honk_verifier/decider_recursive_verifier.cpp b/barretenberg/cpp/src/barretenberg/stdlib/honk_verifier/decider_recursive_verifier.cpp index b5bb43ee36eb..91eb5d725bfd 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/honk_verifier/decider_recursive_verifier.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/honk_verifier/decider_recursive_verifier.cpp @@ -59,6 +59,4 @@ DeciderRecursiveVerifier_::AggregationObject DeciderRecursiveVerifier_>; template class DeciderRecursiveVerifier_>; -template class DeciderRecursiveVerifier_>; -template class DeciderRecursiveVerifier_>; } // namespace bb::stdlib::recursion::honk diff --git a/barretenberg/cpp/src/barretenberg/stdlib/honk_verifier/oink_recursive_verifier.cpp b/barretenberg/cpp/src/barretenberg/stdlib/honk_verifier/oink_recursive_verifier.cpp index d2b873d3686a..a17f8738368c 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/honk_verifier/oink_recursive_verifier.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/honk_verifier/oink_recursive_verifier.cpp @@ -135,7 +135,5 @@ template class OinkRecursiveVerifier_>; template class OinkRecursiveVerifier_>; template class OinkRecursiveVerifier_>; -template class OinkRecursiveVerifier_>; -template class OinkRecursiveVerifier_>; template class OinkRecursiveVerifier_>; } // namespace bb::stdlib::recursion::honk diff --git a/barretenberg/cpp/src/barretenberg/stdlib/honk_verifier/ultra_recursive_verifier.cpp b/barretenberg/cpp/src/barretenberg/stdlib/honk_verifier/ultra_recursive_verifier.cpp index 0e74b5d9d1d7..c9db30587539 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/honk_verifier/ultra_recursive_verifier.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/honk_verifier/ultra_recursive_verifier.cpp @@ -156,7 +156,5 @@ template class UltraRecursiveVerifier_>; template class UltraRecursiveVerifier_>; template class UltraRecursiveVerifier_>; -template class UltraRecursiveVerifier_>; -template class UltraRecursiveVerifier_>; template class UltraRecursiveVerifier_>; } // namespace bb::stdlib::recursion::honk diff --git a/barretenberg/cpp/src/barretenberg/stdlib/honk_verifier/ultra_recursive_verifier.test.cpp b/barretenberg/cpp/src/barretenberg/stdlib/honk_verifier/ultra_recursive_verifier.test.cpp index ddbb367bcf6a..99b2c58f1f7f 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/honk_verifier/ultra_recursive_verifier.test.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/honk_verifier/ultra_recursive_verifier.test.cpp @@ -239,7 +239,7 @@ template class RecursiveVerifierTest : public testing } // Check 3: Construct and verify a proof of the recursive verifier circuit - if constexpr (!IsSimulator) { + { auto proving_key = std::make_shared(outer_circuit); OuterProver prover(proving_key); auto verification_key = std::make_shared(proving_key->proving_key); @@ -307,8 +307,6 @@ using Flavors = testing::Types, UltraRecursiveFlavor_, UltraRecursiveFlavor_, UltraRollupRecursiveFlavor_, - UltraRecursiveFlavor_, - MegaRecursiveFlavor_, MegaZKRecursiveFlavor_, MegaZKRecursiveFlavor_>; diff --git a/barretenberg/cpp/src/barretenberg/stdlib/plonk_recursion/transcript/transcript.hpp b/barretenberg/cpp/src/barretenberg/stdlib/plonk_recursion/transcript/transcript.hpp index 7646bc4e6660..22f8f404b177 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/plonk_recursion/transcript/transcript.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/plonk_recursion/transcript/transcript.hpp @@ -260,9 +260,7 @@ template class Transcript { field_pt borrow = field_pt::from_witness(context, need_borrow); // directly call `create_new_range_constraint` to avoid creating an arithmetic gate - if constexpr (IsSimulator) { - context->create_range_constraint(borrow.get_value(), 1, "borrow"); - } else if constexpr (HasPlookup) { + if constexpr (HasPlookup) { context->create_new_range_constraint(borrow.get_witness_index(), 1, "borrow"); } else { context->create_range_constraint(borrow.get_witness_index(), 1, "borrow"); diff --git a/barretenberg/cpp/src/barretenberg/stdlib/plonk_recursion/verification_key/verification_key.hpp b/barretenberg/cpp/src/barretenberg/stdlib/plonk_recursion/verification_key/verification_key.hpp index 866d161c82b9..beb8bbaf072e 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/plonk_recursion/verification_key/verification_key.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/plonk_recursion/verification_key/verification_key.hpp @@ -155,9 +155,7 @@ template struct PedersenPreimageB field_pt borrow = field_pt::from_witness(context, need_borrow); // directly call `create_new_range_constraint` to avoid creating an arithmetic gate - if constexpr (IsSimulator) { - context->create_range_constraint(borrow.get_value(), 1, "borrow"); - } else if constexpr (HasPlookup) { + if constexpr (HasPlookup) { context->create_new_range_constraint(borrow.get_witness_index(), 1, "borrow"); } else { context->create_range_constraint(borrow.get_witness_index(), 1, "borrow"); diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/bigfield/bigfield.test.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/bigfield/bigfield.test.cpp index 81949bb40e99..c146bf009d95 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/bigfield/bigfield.test.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/bigfield/bigfield.test.cpp @@ -1221,7 +1221,7 @@ template class stdlib_bigfield : public testing::Test { }; // Define types for which the above tests will be constructed. -using CircuitTypes = testing::Types; +using CircuitTypes = testing::Types; // Define the suite of tests. TYPED_TEST_SUITE(stdlib_bigfield, CircuitTypes); diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/bigfield/bigfield_impl.hpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/bigfield/bigfield_impl.hpp index 7867c1b87af7..78cd3d43fcba 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/bigfield/bigfield_impl.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/bigfield/bigfield_impl.hpp @@ -87,9 +87,7 @@ bigfield::bigfield(const field_t& low_bits_in, mid_index = static_cast((NUM_LIMB_BITS / 2) - 1); // Range constraint returns an array of partial sums, midpoint will happen to hold the big limb // value - if constexpr (!IsSimulator) { - limb_1.witness_index = low_accumulator[mid_index]; - } + limb_1.witness_index = low_accumulator[mid_index]; // We can get the first half bits of low_bits_in from the variables we already created limb_0 = (low_bits_in - (limb_1 * shift_1)); } @@ -127,9 +125,7 @@ bigfield::bigfield(const field_t& low_bits_in, static_cast(num_high_limb_bits), "bigfield: high_bits_in too large."); - if constexpr (!IsSimulator) { - limb_3.witness_index = high_accumulator[static_cast(((num_last_limb_bits + 1) / 2) - 1)]; - } + limb_3.witness_index = high_accumulator[static_cast(((num_last_limb_bits + 1) / 2) - 1)]; limb_2 = (high_bits_in - (limb_3 * shift_1)); } } else { @@ -1158,16 +1154,6 @@ bigfield bigfield::pow(const field_t& exponent) auto* ctx = get_context() ? get_context() : exponent.get_context(); uint256_t exponent_value = exponent.get_value(); - if constexpr (IsSimulator) { - if ((exponent_value >> 32) != static_cast(0)) { - ctx->failure("field_t::pow exponent accumulator incorrect"); - } - constexpr uint256_t MASK_32_BITS = 0xffff'ffff; - auto result = bigfield(ctx, native(get_value()).pow(exponent_value & MASK_32_BITS)); - result.set_origin_tag(OriginTag(get_origin_tag(), exponent.get_origin_tag())); - return result; - } - ASSERT(exponent_value.get_msb() < 32); // Use the constant version that perfoms only the necessary multiplications if the exponent is constant if (exponent.is_constant()) { @@ -2029,12 +2015,6 @@ template void bigfield::assert_less_t { // Warning: this assumes we have run circuit construction at least once in debug mode where large non reduced // constants are allowed via ASSERT - if constexpr (IsSimulator) { - if (get_value() >= static_cast(upper_limit)) { - context->failure("Bigfield assert_less_than failed in simulation."); - } - return; - } if (is_constant()) { ASSERT(get_value() < static_cast(upper_limit)); @@ -2112,52 +2092,46 @@ template void bigfield::assert_equal( { Builder* ctx = this->context ? this->context : other.context; - if constexpr (IsSimulator) { - // TODO(https://github.com/AztecProtocol/barretenberg/issues/677) + if (is_constant() && other.is_constant()) { + std::cerr << "bigfield: calling assert equal on 2 CONSTANT bigfield elements...is this intended?" << std::endl; + ASSERT(get_value() == other.get_value()); // We expect constants to be less than the target modulus + return; + } else if (other.is_constant()) { + // TODO(https://github.com/AztecProtocol/barretenberg/issues/998): Something is fishy here + // evaluate a strict equality - make sure *this is reduced first, or an honest prover + // might not be able to satisfy these constraints. + field_t t0 = (binary_basis_limbs[0].element - other.binary_basis_limbs[0].element); + field_t t1 = (binary_basis_limbs[1].element - other.binary_basis_limbs[1].element); + field_t t2 = (binary_basis_limbs[2].element - other.binary_basis_limbs[2].element); + field_t t3 = (binary_basis_limbs[3].element - other.binary_basis_limbs[3].element); + field_t t4 = (prime_basis_limb - other.prime_basis_limb); + t0.assert_is_zero(); + t1.assert_is_zero(); + t2.assert_is_zero(); + t3.assert_is_zero(); + t4.assert_is_zero(); + return; + } else if (is_constant()) { + other.assert_equal(*this); return; } else { - if (is_constant() && other.is_constant()) { - std::cerr << "bigfield: calling assert equal on 2 CONSTANT bigfield elements...is this intended?" - << std::endl; - ASSERT(get_value() == other.get_value()); // We expect constants to be less than the target modulus - return; - } else if (other.is_constant()) { - // TODO(https://github.com/AztecProtocol/barretenberg/issues/998): Something is fishy here - // evaluate a strict equality - make sure *this is reduced first, or an honest prover - // might not be able to satisfy these constraints. - field_t t0 = (binary_basis_limbs[0].element - other.binary_basis_limbs[0].element); - field_t t1 = (binary_basis_limbs[1].element - other.binary_basis_limbs[1].element); - field_t t2 = (binary_basis_limbs[2].element - other.binary_basis_limbs[2].element); - field_t t3 = (binary_basis_limbs[3].element - other.binary_basis_limbs[3].element); - field_t t4 = (prime_basis_limb - other.prime_basis_limb); - t0.assert_is_zero(); - t1.assert_is_zero(); - t2.assert_is_zero(); - t3.assert_is_zero(); - t4.assert_is_zero(); - return; - } else if (is_constant()) { - other.assert_equal(*this); - return; - } else { - - bigfield diff = *this - other; - const uint512_t diff_val = diff.get_value(); - const uint512_t modulus(target_basis.modulus); - const auto [quotient_512, remainder_512] = (diff_val).divmod(modulus); - if (remainder_512 != 0) { - std::cerr << "bigfield: remainder not zero!" << std::endl; - } - bigfield quotient; + bigfield diff = *this - other; + const uint512_t diff_val = diff.get_value(); + const uint512_t modulus(target_basis.modulus); - const size_t num_quotient_bits = get_quotient_max_bits({ 0 }); - quotient = bigfield(witness_t(ctx, fr(quotient_512.slice(0, NUM_LIMB_BITS * 2).lo)), - witness_t(ctx, fr(quotient_512.slice(NUM_LIMB_BITS * 2, NUM_LIMB_BITS * 4).lo)), - false, - num_quotient_bits); - unsafe_evaluate_multiply_add(diff, { one() }, {}, quotient, { zero() }); + const auto [quotient_512, remainder_512] = (diff_val).divmod(modulus); + if (remainder_512 != 0) { + std::cerr << "bigfield: remainder not zero!" << std::endl; } + bigfield quotient; + + const size_t num_quotient_bits = get_quotient_max_bits({ 0 }); + quotient = bigfield(witness_t(ctx, fr(quotient_512.slice(0, NUM_LIMB_BITS * 2).lo)), + witness_t(ctx, fr(quotient_512.slice(NUM_LIMB_BITS * 2, NUM_LIMB_BITS * 4).lo)), + false, + num_quotient_bits); + unsafe_evaluate_multiply_add(diff, { one() }, {}, quotient, { zero() }); } } diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/biggroup/biggroup.test.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/biggroup/biggroup.test.cpp index 855d970fb951..7c3c97debc90 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/biggroup/biggroup.test.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/biggroup/biggroup.test.cpp @@ -1716,8 +1716,7 @@ template class stdlib_biggroup : public testing::Test { enum UseBigfield { No, Yes }; using TestTypes = testing::Types, UseBigfield::No>, TestType, UseBigfield::Yes>, - TestType, UseBigfield::No>, - TestType, UseBigfield::No>>; + TestType, UseBigfield::No>>; TYPED_TEST_SUITE(stdlib_biggroup, TestTypes); diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/biggroup/biggroup_impl.hpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/biggroup/biggroup_impl.hpp index ad7596d62a35..b75aba7f8d9b 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/biggroup/biggroup_impl.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/biggroup/biggroup_impl.hpp @@ -774,8 +774,6 @@ element element::batch_mul(const std::vector element::batch_mul(const std::vector) { - // TODO(https://github.com/AztecProtocol/barretenberg/issues/663) - auto context = points[0].get_context(); - using element_t = typename G::element; - element_t result = G::one; - result.self_set_infinity(); - for (size_t i = 0; i < points.size(); i++) { - result += (element_t(points[i].get_value()) * scalars[i].get_value()); - } - result = result.normalize(); - auto nonnative_result = from_witness(context, result); - nonnative_result.set_origin_tag(tag); - return nonnative_result; - } else { - // Perform goblinized batched mul if available; supported only for BN254 - if (with_edgecases) { - std::tie(points, scalars) = mask_points(points, scalars); - } - const size_t num_points = points.size(); - ASSERT(scalars.size() == num_points); + // Perform goblinized batched mul if available; supported only for BN254 + if (with_edgecases) { + std::tie(points, scalars) = mask_points(points, scalars); + } + const size_t num_points = points.size(); + ASSERT(scalars.size() == num_points); - batch_lookup_table point_table(points); - const size_t num_rounds = (max_num_bits == 0) ? Fr::modulus.get_msb() + 1 : max_num_bits; + batch_lookup_table point_table(points); + const size_t num_rounds = (max_num_bits == 0) ? Fr::modulus.get_msb() + 1 : max_num_bits; - std::vector> naf_entries; - for (size_t i = 0; i < num_points; ++i) { - naf_entries.emplace_back(compute_naf(scalars[i], max_num_bits)); - } - const auto offset_generators = compute_offset_generators(num_rounds); - element accumulator = - element::chain_add_end(element::chain_add(offset_generators.first, point_table.get_chain_initial_entry())); - - constexpr size_t num_rounds_per_iteration = 4; - size_t num_iterations = num_rounds / num_rounds_per_iteration; - num_iterations += ((num_iterations * num_rounds_per_iteration) == num_rounds) ? 0 : 1; - const size_t num_rounds_per_final_iteration = - (num_rounds - 1) - ((num_iterations - 1) * num_rounds_per_iteration); - for (size_t i = 0; i < num_iterations; ++i) { - - std::vector nafs(num_points); - std::vector to_add; - const size_t inner_num_rounds = - (i != num_iterations - 1) ? num_rounds_per_iteration : num_rounds_per_final_iteration; - for (size_t j = 0; j < inner_num_rounds; ++j) { - for (size_t k = 0; k < num_points; ++k) { - nafs[k] = (naf_entries[k][i * num_rounds_per_iteration + j + 1]); - } - to_add.emplace_back(point_table.get_chain_add_accumulator(nafs)); + std::vector> naf_entries; + for (size_t i = 0; i < num_points; ++i) { + naf_entries.emplace_back(compute_naf(scalars[i], max_num_bits)); + } + const auto offset_generators = compute_offset_generators(num_rounds); + element accumulator = + element::chain_add_end(element::chain_add(offset_generators.first, point_table.get_chain_initial_entry())); + + constexpr size_t num_rounds_per_iteration = 4; + size_t num_iterations = num_rounds / num_rounds_per_iteration; + num_iterations += ((num_iterations * num_rounds_per_iteration) == num_rounds) ? 0 : 1; + const size_t num_rounds_per_final_iteration = (num_rounds - 1) - ((num_iterations - 1) * num_rounds_per_iteration); + for (size_t i = 0; i < num_iterations; ++i) { + + std::vector nafs(num_points); + std::vector to_add; + const size_t inner_num_rounds = + (i != num_iterations - 1) ? num_rounds_per_iteration : num_rounds_per_final_iteration; + for (size_t j = 0; j < inner_num_rounds; ++j) { + for (size_t k = 0; k < num_points; ++k) { + nafs[k] = (naf_entries[k][i * num_rounds_per_iteration + j + 1]); } - accumulator = accumulator.multiple_montgomery_ladder(to_add); - } - for (size_t i = 0; i < num_points; ++i) { - element skew = accumulator - points[i]; - Fq out_x = accumulator.x.conditional_select(skew.x, naf_entries[i][num_rounds]); - Fq out_y = accumulator.y.conditional_select(skew.y, naf_entries[i][num_rounds]); - accumulator = element(out_x, out_y); + to_add.emplace_back(point_table.get_chain_add_accumulator(nafs)); } - accumulator = accumulator - offset_generators.second; - - accumulator.set_origin_tag(tag); - return accumulator; + accumulator = accumulator.multiple_montgomery_ladder(to_add); } + for (size_t i = 0; i < num_points; ++i) { + element skew = accumulator - points[i]; + Fq out_x = accumulator.x.conditional_select(skew.x, naf_entries[i][num_rounds]); + Fq out_y = accumulator.y.conditional_select(skew.y, naf_entries[i][num_rounds]); + accumulator = element(out_x, out_y); + } + accumulator = accumulator - offset_generators.second; + + accumulator.set_origin_tag(tag); + return accumulator; } /** * Implements scalar multiplication operator. diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/biggroup/biggroup_nafs.hpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/biggroup/biggroup_nafs.hpp index 9ef07083fc1f..85cd9d78a531 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/biggroup/biggroup_nafs.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/biggroup/biggroup_nafs.hpp @@ -391,8 +391,6 @@ std::vector> element::compute_wnaf(const Fr& scalar) field_t entry(witness_t(ctx, offset_entry)); if constexpr (HasPlookup) { ctx->create_new_range_constraint(entry.witness_index, 1ULL << (WNAF_SIZE), "biggroup_nafs"); - } else if constexpr (IsSimulator) { - ctx->create_range_constraint(entry.get_value(), WNAF_SIZE, "biggroup_nafs"); } else { ctx->create_range_constraint(entry.witness_index, WNAF_SIZE, "biggroup_nafs"); } @@ -403,8 +401,6 @@ std::vector> element::compute_wnaf(const Fr& scalar) wnaf_entries.emplace_back(witness_t(ctx, skew)); if constexpr (HasPlookup) { ctx->create_new_range_constraint(wnaf_entries[wnaf_entries.size() - 1].witness_index, 1, "biggroup_nafs"); - } else if constexpr (IsSimulator) { - ctx->create_range_constraint(wnaf_entries[wnaf_entries.size() - 1].get_value(), 1, "biggroup_nafs"); } else { ctx->create_range_constraint(wnaf_entries[wnaf_entries.size() - 1].witness_index, 1, "biggroup_nafs"); } @@ -528,10 +524,7 @@ std::vector> element::compute_naf(const Fr& scalar, cons bit.context = ctx; bit.witness_index = witness_t(ctx, true).witness_index; // flip sign bit.witness_bool = true; - if constexpr (IsSimulator) { - ctx->create_range_constraint( - bit.get_value(), 1, "biggroup_nafs: compute_naf extracted too many bits in non-next_entry case"); - } else if constexpr (HasPlookup) { + if constexpr (HasPlookup) { ctx->create_new_range_constraint( bit.witness_index, 1, "biggroup_nafs: compute_naf extracted too many bits in non-next_entry case"); } else { @@ -543,11 +536,7 @@ std::vector> element::compute_naf(const Fr& scalar, cons bool_ct bit(ctx, false); bit.witness_index = witness_t(ctx, false).witness_index; // don't flip sign bit.witness_bool = false; - if constexpr (IsSimulator) { - ctx->create_range_constraint( - bit.get_value(), 1, "biggroup_nafs: compute_naf extracted too many bits in next_entry case"); - } else if constexpr (HasPlookup) { - // TODO(https://github.com/AztecProtocol/barretenberg/issues/665) + if constexpr (HasPlookup) { ctx->create_new_range_constraint( bit.witness_index, 1, "biggroup_nafs: compute_naf extracted too many bits in next_entry case"); } else { diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/bit_array/bit_array.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/bit_array/bit_array.cpp index 6af686f1a000..681f3699f46d 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/bit_array/bit_array.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/bit_array/bit_array.cpp @@ -165,6 +165,5 @@ template std::string bit_array::get_witness_as_strin template class bit_array; template class bit_array; template class bit_array; -template class bit_array; } // namespace bb::stdlib diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/bit_array/bit_array.test.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/bit_array/bit_array.test.cpp index 0df52d116b53..ef38e676ae68 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/bit_array/bit_array.test.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/bit_array/bit_array.test.cpp @@ -25,7 +25,7 @@ auto& engine = numeric::get_debug_randomness(); template class BitArrayTest : public ::testing::Test {}; -using CircuitTypes = ::testing::Types; +using CircuitTypes = ::testing::Types; TYPED_TEST_SUITE(BitArrayTest, CircuitTypes); TYPED_TEST(BitArrayTest, test_uint32_input_output_consistency) diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/bool/bool.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/bool/bool.cpp index 48e12b30c0af..1ebf0c52623d 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/bool/bool.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/bool/bool.cpp @@ -421,9 +421,7 @@ template void bool_t::assert_equal(const bool_t& rhs const bool_t lhs = *this; Builder* ctx = lhs.get_context() ? lhs.get_context() : rhs.get_context(); - if constexpr (IsSimulator) { - ctx->assert_equal(lhs.get_value(), rhs.get_value(), msg); - } else if (lhs.is_constant() && rhs.is_constant()) { + if (lhs.is_constant() && rhs.is_constant()) { ASSERT(lhs.get_value() == rhs.get_value()); } else if (lhs.is_constant()) { // if rhs is inverted, flip the value of the lhs constant @@ -579,6 +577,5 @@ template bool_t bool_t::normalize() const template class bool_t; template class bool_t; template class bool_t; -template class bool_t; } // namespace bb::stdlib diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/bool/bool.test.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/bool/bool.test.cpp index f26754acfebc..d0569aca08bf 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/bool/bool.test.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/bool/bool.test.cpp @@ -21,7 +21,7 @@ auto& engine = numeric::get_debug_randomness(); STANDARD_TESTING_TAGS template class BoolTest : public ::testing::Test {}; -using CircuitTypes = ::testing::Types; +using CircuitTypes = ::testing::Types; TYPED_TEST_SUITE(BoolTest, CircuitTypes); TYPED_TEST(BoolTest, TestBasicOperations) @@ -81,10 +81,8 @@ TYPED_TEST(BoolTest, TestBasicOperations) bool result = CircuitChecker::check(builder); EXPECT_EQ(result, true); - if (!IsSimulator) { - auto gates_after = builder.get_estimated_num_finalized_gates(); - EXPECT_EQ(gates_after - gates_before, 6UL); - } + auto gates_after = builder.get_estimated_num_finalized_gates(); + EXPECT_EQ(gates_after - gates_before, 6UL); } TYPED_TEST(BoolTest, Xor) diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/byte_array/byte_array.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/byte_array/byte_array.cpp index 72002c0edc23..4bb5870d7f0d 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/byte_array/byte_array.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/byte_array/byte_array.cpp @@ -403,6 +403,5 @@ typename byte_array::byte_slice byte_array::split_byte(const s template class byte_array; template class byte_array; template class byte_array; -template class byte_array; -} // namespace bb::stdlib \ No newline at end of file +} // namespace bb::stdlib diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/byte_array/byte_array.test.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/byte_array/byte_array.test.cpp index 43f7034217da..de7616f2c149 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/byte_array/byte_array.test.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/byte_array/byte_array.test.cpp @@ -23,7 +23,7 @@ template class ByteArrayTest : public ::testing::Test {}; template using byte_array_ct = byte_array; -using CircuitTypes = ::testing::Types; +using CircuitTypes = ::testing::Types; STANDARD_TESTING_TAGS diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/circuit_builders/circuit_builders.hpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/circuit_builders/circuit_builders.hpp index 086ac9812a17..723e3119c38f 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/circuit_builders/circuit_builders.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/circuit_builders/circuit_builders.hpp @@ -9,7 +9,6 @@ * instantiate templates. */ #pragma once -#include "barretenberg/stdlib_circuit_builders/circuit_simulator.hpp" #include "barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp" #include "barretenberg/stdlib_circuit_builders/standard_circuit_builder.hpp" #include "barretenberg/stdlib_circuit_builders/ultra_circuit_builder.hpp" @@ -25,6 +24,3 @@ template concept IsMegaBuilder = bb::IsAnyOf; template concept IsNotMegaBuilder = !IsMegaBuilder; - -template -concept IsSimulator = bb::IsAnyOf; diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/circuit_builders/circuit_builders_fwd.hpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/circuit_builders/circuit_builders_fwd.hpp index db3aef501aab..31e1da5777b7 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/circuit_builders/circuit_builders_fwd.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/circuit_builders/circuit_builders_fwd.hpp @@ -34,5 +34,4 @@ template class UltraCircuitBuilder_; using UltraCircuitBuilder = UltraCircuitBuilder_; template class MegaCircuitBuilder_; using MegaCircuitBuilder = MegaCircuitBuilder_>; -class CircuitSimulatorBN254; } // namespace bb diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/array.test.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/array.test.cpp index 32663eccf02f..055caef0df07 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/array.test.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/array.test.cpp @@ -518,9 +518,7 @@ template class stdlib_array : public testing::Test { EXPECT_FALSE(proof_result); // TODO(https://github.com/AztecProtocol/barretenberg/issues/666): - if constexpr (!IsSimulator) { - EXPECT_EQ(error, "Once we've hit the first zero, there must only be zeros thereafter!"); - } + EXPECT_EQ(error, "Once we've hit the first zero, there must only be zeros thereafter!"); } class MockClass { @@ -596,7 +594,7 @@ template class stdlib_array : public testing::Test { } }; -typedef testing::Types CircuitTypes; +typedef testing::Types CircuitTypes; TYPED_TEST_SUITE(stdlib_array, CircuitTypes); @@ -634,12 +632,7 @@ TYPED_TEST(stdlib_array, test_array_push_generic) } TYPED_TEST(stdlib_array, test_array_push_generic_full) { - if constexpr (IsSimulator) { - // TODO(https://github.com/AztecProtocol/barretenberg/issues/666): - GTEST_SKIP() << "Skipped for simulator"; - } else { - TestFixture::test_array_push_generic_full(); - } + TestFixture::test_array_push_generic_full(); } // push array to array (pata) tests TYPED_TEST(stdlib_array, test_pata_large_bench) diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.cpp index 57ac3ad12ad8..a351157c2aeb 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.cpp @@ -26,15 +26,9 @@ template field_t::field_t(const witness_t& value) : context(value.context) { - if constexpr (IsSimulator) { - additive_constant = value.witness; - multiplicative_constant = 1; - witness_index = IS_CONSTANT; - } else { - additive_constant = 0; - multiplicative_constant = 1; - witness_index = value.witness_index; - } + additive_constant = 0; + multiplicative_constant = 1; + witness_index = value.witness_index; } template @@ -373,15 +367,6 @@ template field_t field_t::pow(const field_t auto* ctx = get_context() ? get_context() : exponent.get_context(); uint256_t exponent_value = exponent.get_value(); - if constexpr (IsSimulator) { - if ((exponent_value >> 32) != static_cast(0)) { - ctx->failure("field_t::pow exponent accumulator incorrect"); - } - constexpr uint256_t MASK_32_BITS = 0xffff'ffff; - auto result = field_t(get_value().pow(exponent_value & MASK_32_BITS)); - result.set_origin_tag(OriginTag(get_origin_tag(), exponent.get_origin_tag())); - return result; - } bool exponent_constant = exponent.is_constant(); // TODO(https://github.com/AztecProtocol/barretenberg/issues/446): optimize by allowing smaller exponent @@ -577,38 +562,32 @@ template field_t field_t::normalize() const template void field_t::assert_is_zero(std::string const& msg) const { - if constexpr (IsSimulator) { - if (get_value() != 0) { - context->failure(msg); - } - } else { - if (get_value() != bb::fr(0)) { - context->failure(msg); - } - - if (witness_index == IS_CONSTANT) { - ASSERT(additive_constant == bb::fr(0)); - return; - } + if (get_value() != bb::fr(0)) { + context->failure(msg); + } - // Aim of new gate: this.v * this.mul + this.add == 0 - // I.e.: - // this.v * 0 * [ 0 ] + this.v * [this.mul] + 0 * [ 0 ] + 0 * [ 0 ] + [this.add] == 0 - // this.v * 0 * [q_m] + this.v * [ q_l ] + 0 * [q_r] + 0 * [q_o] + [ q_c ] == 0 - - Builder* ctx = context; - - context->create_poly_gate({ - .a = witness_index, - .b = ctx->zero_idx, - .c = ctx->zero_idx, - .q_m = bb::fr(0), - .q_l = multiplicative_constant, - .q_r = bb::fr(0), - .q_o = bb::fr(0), - .q_c = additive_constant, - }); + if (witness_index == IS_CONSTANT) { + ASSERT(additive_constant == bb::fr(0)); + return; } + + // Aim of new gate: this.v * this.mul + this.add == 0 + // I.e.: + // this.v * 0 * [ 0 ] + this.v * [this.mul] + 0 * [ 0 ] + 0 * [ 0 ] + [this.add] == 0 + // this.v * 0 * [q_m] + this.v * [ q_l ] + 0 * [q_r] + 0 * [q_o] + [ q_c ] == 0 + + Builder* ctx = context; + + context->create_poly_gate({ + .a = witness_index, + .b = ctx->zero_idx, + .c = ctx->zero_idx, + .q_m = bb::fr(0), + .q_l = multiplicative_constant, + .q_r = bb::fr(0), + .q_o = bb::fr(0), + .q_c = additive_constant, + }); } template void field_t::assert_is_not_zero(std::string const& msg) const @@ -618,11 +597,9 @@ template void field_t::assert_is_not_zero(std::strin // We don't return; we continue with the function, for debugging purposes. } - if constexpr (!IsSimulator) { - if (witness_index == IS_CONSTANT) { - ASSERT(additive_constant != bb::fr(0)); - return; - } + if (witness_index == IS_CONSTANT) { + ASSERT(additive_constant != bb::fr(0)); + return; } if (get_value() == 0 && context) { @@ -808,23 +785,19 @@ field_t field_t::conditional_assign(const bool_t& pre template void field_t::create_range_constraint(const size_t num_bits, std::string const& msg) const { - if constexpr (IsSimulator) { - context->create_range_constraint(get_value(), num_bits, msg); + if (num_bits == 0) { + assert_is_zero("0-bit range_constraint on non-zero field_t."); } else { - if (num_bits == 0) { - assert_is_zero("0-bit range_constraint on non-zero field_t."); + if (is_constant()) { + ASSERT(uint256_t(get_value()).get_msb() < num_bits); } else { - if (is_constant()) { - ASSERT(uint256_t(get_value()).get_msb() < num_bits); + if constexpr (HasPlookup) { + context->decompose_into_default_range(normalize().get_witness_index(), + num_bits, + bb::UltraCircuitBuilder::DEFAULT_PLOOKUP_RANGE_BITNUM, + msg); } else { - if constexpr (HasPlookup) { - context->decompose_into_default_range(normalize().get_witness_index(), - num_bits, - bb::UltraCircuitBuilder::DEFAULT_PLOOKUP_RANGE_BITNUM, - msg); - } else { - context->decompose_into_base4_accumulators(normalize().get_witness_index(), num_bits, msg); - } + context->decompose_into_base4_accumulators(normalize().get_witness_index(), num_bits, msg); } } } @@ -842,9 +815,7 @@ template void field_t::assert_equal(const field_t& r const field_t lhs = *this; Builder* ctx = lhs.get_context() ? lhs.get_context() : rhs.get_context(); - if constexpr (IsSimulator) { - ctx->assert_equal(lhs.get_value(), rhs.get_value(), msg); - } else if (lhs.is_constant() && rhs.is_constant()) { + if (lhs.is_constant() && rhs.is_constant()) { ASSERT(lhs.get_value() == rhs.get_value()); } else if (lhs.is_constant()) { field_t right = rhs.normalize(); @@ -1230,30 +1201,21 @@ std::vector> field_t::decompose_into_bits( field_t y_lo = (-sum) + (p_lo + shift); y_lo += shifted_high_limb; - if constexpr (IsSimulator) { - fr sum_lo = shift + p_lo - y_lo.get_value(); - auto sum_nonreduced = - static_cast(sum_lo) + static_cast(shifted_high_limb.get_value()); - if (sum_nonreduced > modulus_minus_one) { - context->failure("Bit decomposition describes non-reduced form of a field element."); - } - } else { - // A carry was necessary if and only if the 128th bit y_lo_hi of y_lo is 0. - auto [y_lo_lo, y_lo_hi, zeros] = y_lo.slice(128, 128); - // This copy constraint, along with the constraints of field_t::slice, imposes that y_lo has bit length 129. - // Since the integer y_lo is at least -2**128+1, which has more than 129 bits in `Fr`, the implicit range - // constraint shows that y_lo is non-negative. - context->assert_equal( - zeros.witness_index, context->zero_idx, "field_t: bit decomposition_fails: high limb non-zero"); - // y_borrow is the boolean "a carry was necessary" - field_t y_borrow = -(y_lo_hi - 1); - // If a carry was necessary, subtract that carry from p_hi - // y_hi = (p_hi - y_borrow) - sum_hi - field_t y_hi = -(shifted_high_limb / shift) + (p_hi); - y_hi -= y_borrow; - // As before, except that now the range constraint is explicit, this shows that y_hi is non-negative. - y_hi.create_range_constraint(128, "field_t: bit decomposition fails: y_hi is too large."); - } + // A carry was necessary if and only if the 128th bit y_lo_hi of y_lo is 0. + auto [y_lo_lo, y_lo_hi, zeros] = y_lo.slice(128, 128); + // This copy constraint, along with the constraints of field_t::slice, imposes that y_lo has bit length 129. + // Since the integer y_lo is at least -2**128+1, which has more than 129 bits in `Fr`, the implicit range + // constraint shows that y_lo is non-negative. + context->assert_equal( + zeros.witness_index, context->zero_idx, "field_t: bit decomposition_fails: high limb non-zero"); + // y_borrow is the boolean "a carry was necessary" + field_t y_borrow = -(y_lo_hi - 1); + // If a carry was necessary, subtract that carry from p_hi + // y_hi = (p_hi - y_borrow) - sum_hi + field_t y_hi = -(shifted_high_limb / shift) + (p_hi); + y_hi -= y_borrow; + // As before, except that now the range constraint is explicit, this shows that y_hi is non-negative. + y_hi.create_range_constraint(128, "field_t: bit decomposition fails: y_hi is too large."); } return result; @@ -1262,6 +1224,5 @@ std::vector> field_t::decompose_into_bits( template class field_t; template class field_t; template class field_t; -template class field_t; } // namespace bb::stdlib diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.hpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.hpp index 7eea8f7b981a..11098abc4164 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.hpp @@ -287,17 +287,7 @@ template class field_t { void assert_is_not_zero(std::string const& msg = "field_t::assert_is_not_zero") const; void assert_is_zero(std::string const& msg = "field_t::assert_is_zero") const; bool is_constant() const { return witness_index == IS_CONSTANT; } - uint32_t set_public() const - { - uint32_t public_input_index = 0; - if constexpr (IsSimulator) { - auto value = normalize().get_value(); - public_input_index = context->set_public_input(value); - } else { - public_input_index = context->set_public_input(normalize().witness_index); - } - return public_input_index; - } + uint32_t set_public() const { return context->set_public_input(normalize().witness_index); } /** * Create a witness form a constant. This way the value of the witness is fixed and public (public, because the diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.test.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.test.cpp index b75c4a0323f3..be88935c7719 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.test.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.test.cpp @@ -163,81 +163,61 @@ template class stdlib_field : public testing::Test { */ static void test_assert_equal() { - if constexpr (IsSimulator) { - auto run_test = [](bool expect_failure) { - Builder simulator; - auto lhs = bb::fr::random_element(); - auto rhs = lhs; - if (expect_failure) { - lhs++; - } - simulator.assert_equal(lhs, rhs, "testing assert equal"); - if (expect_failure) { - ASSERT_TRUE(simulator.failed()); + auto run_test = [](bool constrain, bool true_when_y_val_zero = true) { + Builder builder = Builder(); + field_ct x = witness_ct(&builder, 1); + field_ct y = witness_ct(&builder, 0); + + // With no constraints, the proof verification will pass even though + // we assert x and y are equal. + bool expected_result = true; + + if (constrain) { + /* The fact that we have a passing test in both cases that follow tells us + * that the failure in the first case comes from the additive constraint, + * not from a copy constraint. That failure is because the assert_equal + * below says that 'the value of y was always x'--the value 1 is substituted + * for x when evaluating the gate identity. + */ + if (true_when_y_val_zero) { + // constraint: 0*x + 1*y + 0*0 + 0 == 0 + + builder.create_add_gate({ .a = x.witness_index, + .b = y.witness_index, + .c = builder.zero_idx, + .a_scaling = 0, + .b_scaling = 1, + .c_scaling = 0, + .const_scaling = 0 }); + expected_result = false; } else { - ASSERT_FALSE(simulator.failed()); - } - }; - run_test(true); - run_test(false); - } else { - - auto run_test = [](bool constrain, bool true_when_y_val_zero = true) { - Builder builder = Builder(); - field_ct x = witness_ct(&builder, 1); - field_ct y = witness_ct(&builder, 0); - - // With no constraints, the proof verification will pass even though - // we assert x and y are equal. - bool expected_result = true; - - if (constrain) { - /* The fact that we have a passing test in both cases that follow tells us - * that the failure in the first case comes from the additive constraint, - * not from a copy constraint. That failure is because the assert_equal - * below says that 'the value of y was always x'--the value 1 is substituted - * for x when evaluating the gate identity. - */ - if (true_when_y_val_zero) { - // constraint: 0*x + 1*y + 0*0 + 0 == 0 - - builder.create_add_gate({ .a = x.witness_index, - .b = y.witness_index, - .c = builder.zero_idx, - .a_scaling = 0, - .b_scaling = 1, - .c_scaling = 0, - .const_scaling = 0 }); - expected_result = false; - } else { - // constraint: 0*x + 1*y + 0*0 - 1 == 0 - - builder.create_add_gate({ .a = x.witness_index, - .b = y.witness_index, - .c = builder.zero_idx, - .a_scaling = 0, - .b_scaling = 1, - .c_scaling = 0, - .const_scaling = -1 }); - expected_result = true; - } + // constraint: 0*x + 1*y + 0*0 - 1 == 0 + + builder.create_add_gate({ .a = x.witness_index, + .b = y.witness_index, + .c = builder.zero_idx, + .a_scaling = 0, + .b_scaling = 1, + .c_scaling = 0, + .const_scaling = -1 }); + expected_result = true; } + } - x.assert_equal(y); + x.assert_equal(y); - // both field elements have real value 1 now - EXPECT_EQ(x.get_value(), 1); - EXPECT_EQ(y.get_value(), 1); + // both field elements have real value 1 now + EXPECT_EQ(x.get_value(), 1); + EXPECT_EQ(y.get_value(), 1); - bool result = CircuitChecker::check(builder); + bool result = CircuitChecker::check(builder); - EXPECT_EQ(result, expected_result); - }; + EXPECT_EQ(result, expected_result); + }; - run_test(false); - run_test(true, true); - run_test(true, false); - } + run_test(false); + run_test(true, true); + run_test(true, false); } static void test_add_mul_with_constants() @@ -245,12 +225,10 @@ template class stdlib_field : public testing::Test { Builder builder = Builder(); auto gates_before = builder.get_estimated_num_finalized_gates(); uint64_t expected = fidget(builder); - if constexpr (!IsSimulator) { - auto gates_after = builder.get_estimated_num_finalized_gates(); - auto& block = builder.blocks.arithmetic; - EXPECT_EQ(builder.get_variable(block.w_o()[block.size() - 1]), fr(expected)); - info("Number of gates added", gates_after - gates_before); - } + auto gates_after = builder.get_estimated_num_finalized_gates(); + auto& block = builder.blocks.arithmetic; + EXPECT_EQ(builder.get_variable(block.w_o()[block.size() - 1]), fr(expected)); + info("Number of gates added", gates_after - gates_before); bool result = CircuitChecker::check(builder); EXPECT_EQ(result, true); } @@ -925,11 +903,9 @@ template class stdlib_field : public testing::Test { EXPECT_EQ(first_copy.get_value(), value); EXPECT_EQ(second_copy.get_value(), value); - if (!IsSimulator) { - EXPECT_EQ(value_ct.get_witness_index() + 1, first_copy.get_witness_index()); - EXPECT_EQ(value_ct.get_witness_index() + 2, second_copy.get_witness_index()); - info("num gates = ", builder.get_estimated_num_finalized_gates()); - } + EXPECT_EQ(value_ct.get_witness_index() + 1, first_copy.get_witness_index()); + EXPECT_EQ(value_ct.get_witness_index() + 2, second_copy.get_witness_index()); + info("num gates = ", builder.get_estimated_num_finalized_gates()); bool result = CircuitChecker::check(builder); EXPECT_EQ(result, true); @@ -1133,7 +1109,7 @@ template class stdlib_field : public testing::Test { } }; -using CircuitTypes = testing::Types; +using CircuitTypes = testing::Types; TYPED_TEST_SUITE(stdlib_field, CircuitTypes); diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field_conversion.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field_conversion.cpp index 75b4d5ff66f4..9350907dd624 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field_conversion.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field_conversion.cpp @@ -51,7 +51,4 @@ template fq convert_to_grumpkin_fr(Ult template fq convert_to_grumpkin_fr(MegaCircuitBuilder& builder, const fr& f); -template fq convert_to_grumpkin_fr(CircuitSimulatorBN254& builder, - const fr& f); - } // namespace bb::stdlib::field_conversion diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/group/cycle_group.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/group/cycle_group.cpp index ca40ff998db7..07f1ce471354 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/group/cycle_group.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/group/cycle_group.cpp @@ -1596,8 +1596,7 @@ typename cycle_group::batch_mul_internal_output cycle_group::_ OriginTag tag{}; for (size_t i = 0; i < num_points; ++i) { - // Merge all tags of scalars (we don't have to account for CircuitSimulator in cycle_group yet, because it - // breaks) + // Merge all tags of scalars tag = OriginTag(tag, scalars[i].get_origin_tag()); std::optional> table_id = plookup::fixed_base::table::get_lookup_table_ids_for_point(base_points[i]); @@ -2005,6 +2004,5 @@ template cycle_group cycle_group::operator/ template class cycle_group; template class cycle_group; template class cycle_group; -template class cycle_group; } // namespace bb::stdlib diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/logic/logic.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/logic/logic.cpp index c743c11deb0e..85490299a72f 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/logic/logic.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/logic/logic.cpp @@ -40,90 +40,80 @@ field_t logic::create_logic_constraint( ASSERT(num_bits < 254); ASSERT(num_bits > 0); - if constexpr (IsSimulator) { + if (a.is_constant() && !b.is_constant()) { + Builder* ctx = b.get_context(); uint256_t a_native(a.get_value()); + field_pt a_witness = field_pt::from_witness_index(ctx, ctx->put_constant_variable(a_native)); + return create_logic_constraint(a_witness, b, num_bits, is_xor_gate, get_chunk); + } + if (!a.is_constant() && b.is_constant()) { + Builder* ctx = a.get_context(); uint256_t b_native(b.get_value()); - uint256_t c_native = is_xor_gate ? (a_native ^ b_native) : (a_native & b_native); - return field_t(c_native); - } else { - if (a.is_constant() && !b.is_constant()) { - Builder* ctx = b.get_context(); - uint256_t a_native(a.get_value()); - field_pt a_witness = field_pt::from_witness_index(ctx, ctx->put_constant_variable(a_native)); - return create_logic_constraint(a_witness, b, num_bits, is_xor_gate, get_chunk); - } - if (!a.is_constant() && b.is_constant()) { - Builder* ctx = a.get_context(); - uint256_t b_native(b.get_value()); - field_pt b_witness = field_pt::from_witness_index(ctx, ctx->put_constant_variable(b_native)); - return create_logic_constraint(a, b_witness, num_bits, is_xor_gate, get_chunk); - } - if constexpr (HasPlookup) { - Builder* ctx = a.get_context(); - - const size_t num_chunks = (num_bits / 32) + ((num_bits % 32 == 0) ? 0 : 1); - auto left((uint256_t)a.get_value()); - auto right((uint256_t)b.get_value()); - - field_pt a_accumulator(bb::fr::zero()); - field_pt b_accumulator(bb::fr::zero()); + field_pt b_witness = field_pt::from_witness_index(ctx, ctx->put_constant_variable(b_native)); + return create_logic_constraint(a, b_witness, num_bits, is_xor_gate, get_chunk); + } + if constexpr (HasPlookup) { + Builder* ctx = a.get_context(); - field_pt res(ctx, 0); - for (size_t i = 0; i < num_chunks; ++i) { - size_t chunk_size = (i != num_chunks - 1) ? 32 : num_bits - i * 32; - auto [left_chunk, right_chunk] = get_chunk(left, right, chunk_size); + const size_t num_chunks = (num_bits / 32) + ((num_bits % 32 == 0) ? 0 : 1); + auto left((uint256_t)a.get_value()); + auto right((uint256_t)b.get_value()); - field_pt a_chunk = witness_pt(ctx, left_chunk); - field_pt b_chunk = witness_pt(ctx, right_chunk); - field_pt result_chunk = 0; - if (is_xor_gate) { - result_chunk = stdlib::plookup_read::read_from_2_to_1_table( - plookup::MultiTableId::UINT32_XOR, a_chunk, b_chunk); + field_pt a_accumulator(bb::fr::zero()); + field_pt b_accumulator(bb::fr::zero()); - } else { - result_chunk = stdlib::plookup_read::read_from_2_to_1_table( - plookup::MultiTableId::UINT32_AND, a_chunk, b_chunk); - } + field_pt res(ctx, 0); + for (size_t i = 0; i < num_chunks; ++i) { + size_t chunk_size = (i != num_chunks - 1) ? 32 : num_bits - i * 32; + auto [left_chunk, right_chunk] = get_chunk(left, right, chunk_size); - auto scaling_factor = uint256_t(1) << (32 * i); - a_accumulator += a_chunk * scaling_factor; - b_accumulator += b_chunk * scaling_factor; + field_pt a_chunk = witness_pt(ctx, left_chunk); + field_pt b_chunk = witness_pt(ctx, right_chunk); + field_pt result_chunk = 0; + if (is_xor_gate) { + result_chunk = stdlib::plookup_read::read_from_2_to_1_table( + plookup::MultiTableId::UINT32_XOR, a_chunk, b_chunk); - if (chunk_size != 32) { - ctx->create_range_constraint( - a_chunk.witness_index, chunk_size, "stdlib logic: bad range on final chunk of left operand"); - ctx->create_range_constraint( - b_chunk.witness_index, chunk_size, "stdlib logic: bad range on final chunk of right operand"); - } + } else { + result_chunk = stdlib::plookup_read::read_from_2_to_1_table( + plookup::MultiTableId::UINT32_AND, a_chunk, b_chunk); + } - res += result_chunk * scaling_factor; + auto scaling_factor = uint256_t(1) << (32 * i); + a_accumulator += a_chunk * scaling_factor; + b_accumulator += b_chunk * scaling_factor; - left = left >> 32; - right = right >> 32; + if (chunk_size != 32) { + ctx->create_range_constraint( + a_chunk.witness_index, chunk_size, "stdlib logic: bad range on final chunk of left operand"); + ctx->create_range_constraint( + b_chunk.witness_index, chunk_size, "stdlib logic: bad range on final chunk of right operand"); } - field_pt a_slice = a.slice(static_cast(num_bits - 1), 0)[1]; - field_pt b_slice = b.slice(static_cast(num_bits - 1), 0)[1]; - a_slice.assert_equal(a_accumulator, "stdlib logic: failed to reconstruct left operand"); - b_slice.assert_equal(b_accumulator, "stdlib logic: failed to reconstruct right operand"); - return res; - } else { - // If the builder doesn't have lookups we call the expensive logic constraint gate - // which creates constraints for each bit. We only create constraints up to num_bits. - Builder* ctx = a.get_context(); - field_pt a_slice = a.slice(static_cast(num_bits - 1), 0)[1]; - field_pt b_slice = b.slice(static_cast(num_bits - 1), 0)[1]; - auto accumulator_triple = ctx->create_logic_constraint(a_slice.normalize().get_witness_index(), - b_slice.normalize().get_witness_index(), - num_bits, - is_xor_gate); - auto out_idx = accumulator_triple.out[accumulator_triple.out.size() - 1]; - return field_t::from_witness_index(ctx, out_idx); + res += result_chunk * scaling_factor; + + left = left >> 32; + right = right >> 32; } + field_pt a_slice = a.slice(static_cast(num_bits - 1), 0)[1]; + field_pt b_slice = b.slice(static_cast(num_bits - 1), 0)[1]; + a_slice.assert_equal(a_accumulator, "stdlib logic: failed to reconstruct left operand"); + b_slice.assert_equal(b_accumulator, "stdlib logic: failed to reconstruct right operand"); + + return res; + } else { + // If the builder doesn't have lookups we call the expensive logic constraint gate + // which creates constraints for each bit. We only create constraints up to num_bits. + Builder* ctx = a.get_context(); + field_pt a_slice = a.slice(static_cast(num_bits - 1), 0)[1]; + field_pt b_slice = b.slice(static_cast(num_bits - 1), 0)[1]; + auto accumulator_triple = ctx->create_logic_constraint( + a_slice.normalize().get_witness_index(), b_slice.normalize().get_witness_index(), num_bits, is_xor_gate); + auto out_idx = accumulator_triple.out[accumulator_triple.out.size() - 1]; + return field_t::from_witness_index(ctx, out_idx); } } template class logic; template class logic; template class logic; -template class logic; } // namespace bb::stdlib diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/logic/logic.test.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/logic/logic.test.cpp index 350c93a4f771..a14b3dac22e0 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/logic/logic.test.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/logic/logic.test.cpp @@ -26,7 +26,7 @@ template void ignore_unused(T&) {} // use to ignore unused variables i template class LogicTest : public testing::Test {}; -using CircuitTypes = ::testing::Types; +using CircuitTypes = ::testing::Types; TYPED_TEST_SUITE(LogicTest, CircuitTypes); @@ -91,9 +91,7 @@ TYPED_TEST(LogicTest, TestCorrectLogic) TYPED_TEST(LogicTest, LargeOperands) { STDLIB_TYPE_ALIASES - if constexpr (IsSimulator) { - GTEST_SKIP() << "Skipping this test for the simulator"; - } + auto builder = Builder(); uint256_t mask = (uint256_t(1) << 48) - 1; diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/packed_byte_array/packed_byte_array.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/packed_byte_array/packed_byte_array.cpp index 23576bb8895f..c13e14c6d42c 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/packed_byte_array/packed_byte_array.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/packed_byte_array/packed_byte_array.cpp @@ -271,9 +271,8 @@ template std::string packed_byte_array::get_value() return bytes; } -template class packed_byte_array; template class packed_byte_array; template class packed_byte_array; template class packed_byte_array; -} // namespace bb::stdlib \ No newline at end of file +} // namespace bb::stdlib diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/packed_byte_array/packed_byte_array.test.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/packed_byte_array/packed_byte_array.test.cpp index 00c007e6e7fb..ccfd1a90dd43 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/packed_byte_array/packed_byte_array.test.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/packed_byte_array/packed_byte_array.test.cpp @@ -20,7 +20,7 @@ auto& engine = numeric::get_debug_randomness(); template class PackedByteArrayTest : public ::testing::Test {}; -using CircuitTypes = ::testing::Types; +using CircuitTypes = ::testing::Types; TYPED_TEST_SUITE(PackedByteArrayTest, CircuitTypes); TYPED_TEST(PackedByteArrayTest, string_constructor_and_get_value_consistency) diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/padding_indicator_array/padding_indicator_array.test.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/padding_indicator_array/padding_indicator_array.test.cpp index 14dfbe25a5a9..ddf8461c7124 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/padding_indicator_array/padding_indicator_array.test.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/padding_indicator_array/padding_indicator_array.test.cpp @@ -111,8 +111,7 @@ template class PaddingIndicatorArrayTest : public testing::Test using TestTypes = testing::Types< PaddingTestParams>>::ScalarField, bb::MegaCircuitBuilder>, - PaddingTestParams::ScalarField, bb::UltraCircuitBuilder>, - PaddingTestParams::ScalarField, bb::CircuitSimulatorBN254>>; + PaddingTestParams::ScalarField, bb::UltraCircuitBuilder>>; TYPED_TEST_SUITE(PaddingIndicatorArrayTest, TestTypes); diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/plookup/plookup.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/plookup/plookup.cpp index 2e19dca67c5f..847fdc8b75ef 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/plookup/plookup.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/plookup/plookup.cpp @@ -99,5 +99,4 @@ field_t plookup_read::read_from_1_to_2_table(const MultiTableI template class plookup_read; template class plookup_read; -template class plookup_read; } // namespace bb::stdlib diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/safe_uint/safe_uint.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/safe_uint/safe_uint.cpp index 414a1f1024be..dba3f9e1a925 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/safe_uint/safe_uint.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/safe_uint/safe_uint.cpp @@ -42,9 +42,8 @@ safe_uint_t safe_uint_t::subtract(const safe_uint_t& other, std::string const& description) const { ASSERT(difference_bit_size <= MAX_BIT_NUM); - if constexpr (!IsSimulator) { - ASSERT(!(this->value.is_constant() && other.value.is_constant())); - } + ASSERT(!(this->value.is_constant() && other.value.is_constant())); + field_ct difference_val = this->value - other.value; // Creates the range constraint that difference_val is in [0, (1< difference(difference_val, difference_bit_size, format("subtract: ", description)); @@ -74,10 +73,9 @@ safe_uint_t safe_uint_t::subtract(const safe_uint_t& other, template safe_uint_t safe_uint_t::operator-(const safe_uint_t& other) const { // If both are constants and the operation is an underflow, throw an error since circuit itself underflows - if constexpr (!IsSimulator) { - ASSERT(!(this->value.is_constant() && other.value.is_constant() && - static_cast(value.get_value()) < static_cast(other.value.get_value()))); - } + ASSERT(!(this->value.is_constant() && other.value.is_constant() && + static_cast(value.get_value()) < static_cast(other.value.get_value()))); + field_ct difference_val = this->value - other.value; // safe_uint_t constructor creates a range constraint which checks that `difference_val` is within [0, @@ -117,9 +115,7 @@ safe_uint_t safe_uint_t::divide( std::string const& description, const std::function(uint256_t, uint256_t)>& get_quotient) const { - if constexpr (!IsSimulator) { - ASSERT(this->value.is_constant() == false); - } + ASSERT(this->value.is_constant() == false); ASSERT(quotient_bit_size <= MAX_BIT_NUM); ASSERT(remainder_bit_size <= MAX_BIT_NUM); uint256_t val = this->value.get_value(); @@ -154,9 +150,8 @@ safe_uint_t safe_uint_t::divide( */ template safe_uint_t safe_uint_t::operator/(const safe_uint_t& other) const { - if constexpr (!IsSimulator) { - ASSERT(this->value.is_constant() == false); - } + ASSERT(this->value.is_constant() == false); + uint256_t val = this->value.get_value(); auto [quotient_val, remainder_val] = val.divmod((uint256_t)other.value.get_value()); field_ct quotient_field(witness_t(value.context, quotient_val)); @@ -261,6 +256,5 @@ std::array, 3> safe_uint_t::slice(const uint8_t ms template class safe_uint_t; template class safe_uint_t; template class safe_uint_t; -template class safe_uint_t; } // namespace bb::stdlib diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/safe_uint/safe_uint.test.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/safe_uint/safe_uint.test.cpp index 9e319d8fb4b6..43d9271c0bc9 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/safe_uint/safe_uint.test.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/safe_uint/safe_uint.test.cpp @@ -32,7 +32,7 @@ template void ignore_unused(T&) {} // use to ignore unused variables i template class SafeUintTest : public ::testing::Test {}; -using CircuitTypes = ::testing::Types; +using CircuitTypes = ::testing::Types; TYPED_TEST_SUITE(SafeUintTest, CircuitTypes); STANDARD_TESTING_TAGS diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/plookup/arithmetic.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/plookup/arithmetic.cpp index cab4f4a2d3fc..ff2af22db998 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/plookup/arithmetic.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/plookup/arithmetic.cpp @@ -101,53 +101,48 @@ uint_plookup uint_plookup::operator-(const uin template uint_plookup uint_plookup::operator*(const uint_plookup& other) const { - Builder* ctx = (context == nullptr) ? other.context : context; - if constexpr (IsSimulator) { + if (is_constant() && other.is_constant()) { return uint_plookup(context, (additive_constant * other.additive_constant) & MASK); - } else { - if (is_constant() && other.is_constant()) { - return uint_plookup(context, (additive_constant * other.additive_constant) & MASK); - } - if (is_constant() && !other.is_constant()) { - return other * (*this); - } - - const uint32_t rhs_idx = other.is_constant() ? ctx->zero_idx : other.witness_index; - - const uint256_t lhs = ctx->variables[witness_index]; - const uint256_t rhs = ctx->variables[rhs_idx]; - - const uint256_t product = (lhs * rhs) + (lhs * other.additive_constant) + (rhs * additive_constant); - const uint256_t overflow = product >> width; - const uint256_t remainder = product & MASK; - - const mul_quad_ gate{ - witness_index, - rhs_idx, - ctx->add_variable(remainder), - ctx->add_variable(overflow), - FF::one(), - other.additive_constant, - additive_constant, - FF::neg_one(), - -FF(CIRCUIT_UINT_MAX_PLUS_ONE), - 0, - }; - - ctx->create_big_mul_gate(gate); - - // discard the high bits - ctx->decompose_into_default_range(gate.d, width); - - uint_plookup result(ctx); - result.accumulators = constrain_accumulators(ctx, gate.c); - result.witness_index = gate.c; - result.witness_status = WitnessStatus::OK; - - return result; } + if (is_constant() && !other.is_constant()) { + return other * (*this); + } + + const uint32_t rhs_idx = other.is_constant() ? ctx->zero_idx : other.witness_index; + + const uint256_t lhs = ctx->variables[witness_index]; + const uint256_t rhs = ctx->variables[rhs_idx]; + + const uint256_t product = (lhs * rhs) + (lhs * other.additive_constant) + (rhs * additive_constant); + const uint256_t overflow = product >> width; + const uint256_t remainder = product & MASK; + + const mul_quad_ gate{ + witness_index, + rhs_idx, + ctx->add_variable(remainder), + ctx->add_variable(overflow), + FF::one(), + other.additive_constant, + additive_constant, + FF::neg_one(), + -FF(CIRCUIT_UINT_MAX_PLUS_ONE), + 0, + }; + + ctx->create_big_mul_gate(gate); + + // discard the high bits + ctx->decompose_into_default_range(gate.d, width); + + uint_plookup result(ctx); + result.accumulators = constrain_accumulators(ctx, gate.c); + result.witness_index = gate.c; + result.witness_status = WitnessStatus::OK; + + return result; } template @@ -192,11 +187,7 @@ std::pair, uint_plookup> uint_plo if (other.is_constant() && other.get_value() == 0) { // TODO: should have an actual error handler! const uint32_t one = ctx->add_variable(FF::one()); - if constexpr (IsSimulator) { - ctx->assert_equal_constant(FF::one(), FF::zero(), "plookup_arithmetic: divide by zero!"); - } else { - ctx->assert_equal_constant(one, FF::zero(), "plookup_arithmetic: divide by zero!"); - } + ctx->assert_equal_constant(one, FF::zero(), "plookup_arithmetic: divide by zero!"); } else if (!other.is_constant()) { const bool_t is_divisor_zero = field_t(other).is_zero(); ctx->assert_equal_constant(is_divisor_zero.witness_index, FF::zero(), "plookup_arithmetic: divide by zero!"); @@ -269,15 +260,11 @@ std::pair, uint_plookup> uint_plo } template class uint_plookup; template class uint_plookup; -template class uint_plookup; template class uint_plookup; template class uint_plookup; -template class uint_plookup; template class uint_plookup; template class uint_plookup; -template class uint_plookup; template class uint_plookup; template class uint_plookup; -template class uint_plookup; -} // namespace bb::stdlib \ No newline at end of file +} // namespace bb::stdlib diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/plookup/comparison.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/plookup/comparison.cpp index b9104d18b197..36f65c78942e 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/plookup/comparison.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/plookup/comparison.cpp @@ -81,14 +81,10 @@ template bool_t uint_plookup; template class uint_plookup; -template class uint_plookup; template class uint_plookup; template class uint_plookup; -template class uint_plookup; template class uint_plookup; template class uint_plookup; -template class uint_plookup; template class uint_plookup; template class uint_plookup; -template class uint_plookup; -} // namespace bb::stdlib \ No newline at end of file +} // namespace bb::stdlib diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/plookup/logic.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/plookup/logic.cpp index 539e9268cc0d..69cb2060e7d4 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/plookup/logic.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/plookup/logic.cpp @@ -333,15 +333,11 @@ uint_plookup uint_plookup::logic_operator(cons template class uint_plookup; template class uint_plookup; -template class uint_plookup; template class uint_plookup; template class uint_plookup; -template class uint_plookup; template class uint_plookup; template class uint_plookup; -template class uint_plookup; template class uint_plookup; template class uint_plookup; -template class uint_plookup; -} // namespace bb::stdlib \ No newline at end of file +} // namespace bb::stdlib diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/plookup/uint.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/plookup/uint.cpp index 0f7591fef4a5..4f029160055b 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/plookup/uint.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/plookup/uint.cpp @@ -251,15 +251,11 @@ bool_t uint_plookup::at(const size_t bit_index) const template class uint_plookup; template class uint_plookup; -template class uint_plookup; template class uint_plookup; template class uint_plookup; -template class uint_plookup; template class uint_plookup; template class uint_plookup; -template class uint_plookup; template class uint_plookup; template class uint_plookup; -template class uint_plookup; } // namespace bb::stdlib diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/uint.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/uint.cpp index e06fd8d56630..c5ec9016c3f5 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/uint.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/uint.cpp @@ -42,15 +42,10 @@ uint::uint(const witness_t& witness) : context(witness.context) , witness_status(WitnessStatus::OK) { - if constexpr (IsSimulator) { - additive_constant = witness.witness; - witness_index = IS_CONSTANT; - } else { - additive_constant = 0; - accumulators = constrain_accumulators( - context, witness.witness_index, width, "uint: range constraint fails in constructor of uint from witness"); - witness_index = accumulators[num_accumulators() - 1]; - } + additive_constant = 0; + accumulators = constrain_accumulators( + context, witness.witness_index, width, "uint: range constraint fails in constructor of uint from witness"); + witness_index = accumulators[num_accumulators() - 1]; } template diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/uint.hpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/uint.hpp index a321c8212dba..a39b1d9bc9d0 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/uint.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/uint.hpp @@ -194,20 +194,16 @@ template inline std::ostream& operator<<(std::ostream& } template -using uint8 = typename std::conditional || IsSimulator, - uint_plookup, - uint>::type; +using uint8 = + typename std::conditional, uint_plookup, uint>::type; template -using uint16 = typename std::conditional || IsSimulator, - uint_plookup, - uint>::type; +using uint16 = + typename std::conditional, uint_plookup, uint>::type; template -using uint32 = typename std::conditional || IsSimulator, - uint_plookup, - uint>::type; +using uint32 = + typename std::conditional, uint_plookup, uint>::type; template -using uint64 = typename std::conditional || IsSimulator, - uint_plookup, - uint>::type; +using uint64 = + typename std::conditional, uint_plookup, uint>::type; } // namespace bb::stdlib diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/uint.test.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/uint.test.cpp index 62a289025bae..03401b4ea37e 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/uint.test.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/uint.test.cpp @@ -1738,7 +1738,7 @@ template class stdlib_uint : public testing::Test { } }; -using CircuitTypes = testing::Types; +using CircuitTypes = testing::Types; TYPED_TEST_SUITE(stdlib_uint, CircuitTypes); @@ -1845,11 +1845,7 @@ TYPED_TEST(stdlib_uint, test_divide_special) } TYPED_TEST(stdlib_uint, div_remainder_constraint) { - if constexpr (IsSimulator) { - GTEST_SKIP() << "Doesn't apply to the simulator."; - } else { - TestFixture::div_remainder_constraint(); - } + TestFixture::div_remainder_constraint(); } TYPED_TEST(stdlib_uint, test_and) { diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/witness/witness.hpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/witness/witness.hpp index eeeeec21c909..3c42571feecd 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/witness/witness.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/witness/witness.hpp @@ -21,9 +21,7 @@ template class witness_t { { context = parent_context; witness = in; - if constexpr (!IsSimulator) { - witness_index = context->add_variable(witness); - } + witness_index = context->add_variable(witness); } witness_t(Builder* parent_context, const bool in) @@ -34,28 +32,20 @@ template class witness_t { } else { bb::fr::__copy(bb::fr::zero(), witness); } - if constexpr (!IsSimulator) { - witness_index = context->add_variable(witness); - } + witness_index = context->add_variable(witness); } witness_t(Builder* parent_context, IntegralOrEnum auto const in) { context = parent_context; witness = bb::fr{ static_cast(in), 0, 0, 0 }.to_montgomery_form(); - if constexpr (!IsSimulator) { - witness_index = context->add_variable(witness); - } + witness_index = context->add_variable(witness); } static witness_t create_constant_witness(Builder* parent_context, const bb::fr& in) { witness_t out(parent_context, in); - if constexpr (IsSimulator) { - parent_context->assert_equal_constant(out.witness, in, "Failed to create constant witness."); - } else { - parent_context->assert_equal_constant(out.witness_index, in, "Failed to create constant witness."); - } + parent_context->assert_equal_constant(out.witness_index, in, "Failed to create constant witness."); return out; } @@ -75,9 +65,7 @@ template class public_witness_t : public witness_t { { context = parent_context; bb::fr::__copy(in, witness); - if constexpr (!IsSimulator) { - witness_index = context->add_public_variable(witness); - } + witness_index = context->add_public_variable(witness); } public_witness_t(Builder* parent_context, const bool in) @@ -95,9 +83,7 @@ template class public_witness_t : public witness_t { { context = parent_context; witness = bb::fr{ static_cast(in), 0, 0, 0 }.to_montgomery_form(); - if constexpr (!IsSimulator) { - witness_index = context->add_public_variable(witness); - } + witness_index = context->add_public_variable(witness); } }; diff --git a/barretenberg/cpp/src/barretenberg/stdlib/protogalaxy_verifier/protogalaxy_recursive_verifier.cpp b/barretenberg/cpp/src/barretenberg/stdlib/protogalaxy_verifier/protogalaxy_recursive_verifier.cpp index cc6b60875e68..ea42ddbcdbf4 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/protogalaxy_verifier/protogalaxy_recursive_verifier.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/protogalaxy_verifier/protogalaxy_recursive_verifier.cpp @@ -210,7 +210,5 @@ template class ProtogalaxyRecursiveVerifier_< RecursiveDeciderVerificationKeys_, 2>>; template class ProtogalaxyRecursiveVerifier_< RecursiveDeciderVerificationKeys_, 2>>; -template class ProtogalaxyRecursiveVerifier_< - RecursiveDeciderVerificationKeys_, 2>>; } // namespace bb::stdlib::recursion::honk diff --git a/barretenberg/cpp/src/barretenberg/stdlib/protogalaxy_verifier/protogalaxy_recursive_verifier.test.cpp b/barretenberg/cpp/src/barretenberg/stdlib/protogalaxy_verifier/protogalaxy_recursive_verifier.test.cpp index e2756a3de92c..414b63ba2ec1 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/protogalaxy_verifier/protogalaxy_recursive_verifier.test.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/protogalaxy_verifier/protogalaxy_recursive_verifier.test.cpp @@ -246,7 +246,7 @@ template class ProtogalaxyRecursiveTests : public tes // Check for a failure flag in the recursive verifier circuit - if constexpr (!IsSimulator) { + { // inefficiently check finalized size folding_circuit.finalize_circuit(/* ensure_nonzero= */ true); info("Folding Recursive Verifier: num gates finalized = ", folding_circuit.num_gates); @@ -340,7 +340,7 @@ template class ProtogalaxyRecursiveTests : public tes auto recursive_result = pcs_vkey.pairing_check(pairing_points.P0.get_value(), pairing_points.P1.get_value()); EXPECT_EQ(native_result, recursive_result); - if constexpr (!IsSimulator) { + { auto decider_pk = std::make_shared(decider_circuit); OuterProver prover(decider_pk); auto honk_vk = std::make_shared(decider_pk->proving_key); @@ -463,14 +463,11 @@ template class ProtogalaxyRecursiveTests : public tes verifier_circuit_2.get_estimated_num_finalized_gates()); // The circuit blocks (selectors + wires) fully determine the circuit - check that they are identical - if constexpr (!IsSimulator) { - EXPECT_EQ(verifier_circuit_1.blocks, verifier_circuit_2.blocks); - } + EXPECT_EQ(verifier_circuit_1.blocks, verifier_circuit_2.blocks); } }; -using FlavorTypes = - testing::Types, MegaRecursiveFlavor_>; +using FlavorTypes = testing::Types>; TYPED_TEST_SUITE(ProtogalaxyRecursiveTests, FlavorTypes); TYPED_TEST(ProtogalaxyRecursiveTests, InnerCircuit) diff --git a/barretenberg/cpp/src/barretenberg/stdlib/translator_vm_verifier/translator_recursive_verifier.cpp b/barretenberg/cpp/src/barretenberg/stdlib/translator_vm_verifier/translator_recursive_verifier.cpp index 2a746cd6b81f..63968ee297e9 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/translator_vm_verifier/translator_recursive_verifier.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/translator_vm_verifier/translator_recursive_verifier.cpp @@ -181,6 +181,5 @@ bool TranslatorRecursiveVerifier_::verify_translation( } template class TranslatorRecursiveVerifier_>; template class TranslatorRecursiveVerifier_>; -template class TranslatorRecursiveVerifier_>; } // namespace bb diff --git a/barretenberg/cpp/src/barretenberg/stdlib/translator_vm_verifier/translator_recursive_verifier.test.cpp b/barretenberg/cpp/src/barretenberg/stdlib/translator_vm_verifier/translator_recursive_verifier.test.cpp index f4b5bced856b..1b2afa2cd75f 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/translator_vm_verifier/translator_recursive_verifier.test.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/translator_vm_verifier/translator_recursive_verifier.test.cpp @@ -118,7 +118,7 @@ template class TranslatorRecursiveTests : public ::te EXPECT_EQ(vk_poly.get_value(), native_vk_poly); } - if constexpr (!IsSimulator) { + { auto proving_key = std::make_shared(outer_circuit); OuterProver prover(proving_key); auto verification_key = std::make_shared(proving_key->proving_key); @@ -185,9 +185,8 @@ template class TranslatorRecursiveTests : public ::te }; }; -using FlavorTypes = testing::Types, - TranslatorRecursiveFlavor_, - TranslatorRecursiveFlavor_>; +using FlavorTypes = + testing::Types, TranslatorRecursiveFlavor_>; TYPED_TEST_SUITE(TranslatorRecursiveTests, FlavorTypes); diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/circuit_simulator.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/circuit_simulator.hpp deleted file mode 100644 index 93eab01cf541..000000000000 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/circuit_simulator.hpp +++ /dev/null @@ -1,216 +0,0 @@ -// === AUDIT STATUS === -// internal: { status: not started, auditors: [], date: YYYY-MM-DD } -// external_1: { status: not started, auditors: [], date: YYYY-MM-DD } -// external_2: { status: not started, auditors: [], date: YYYY-MM-DD } -// ===================== - -#pragma once -#include "barretenberg/ecc/curves/bn254/bn254.hpp" -#include "barretenberg/ecc/curves/bn254/fr.hpp" -#include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp" -#include "barretenberg/plonk_honk_shared/execution_trace/gate_data.hpp" -#include "barretenberg/plonk_honk_shared/types/aggregation_object_type.hpp" -#include "barretenberg/plonk_honk_shared/types/circuit_type.hpp" -#include "barretenberg/plonk_honk_shared/types/merkle_hash_type.hpp" -#include "barretenberg/stdlib_circuit_builders/plookup_tables/plookup_tables.hpp" -#include "barretenberg/stdlib_circuit_builders/plookup_tables/types.hpp" -#include - -namespace bb { - -/** - * @brief Simulate circuit construction to quickly get function outputs. - * @details Instantiating the stdlib with this type will give a version of the stdlib where none of the classes store - * witness data. This significantly speeds us the cost of executing stdlib functions. What's more, for even faster - * speeds, we have the option (partially implemented) to completely skip computing most witness constrution, and instead - * proxy out to "native" functions (for instance, bb::stdlib::ecdsa_verify_signature we might reformat - * its inputs and then directly call the function crypto::ecdsa_verify_signature). - * - * The general strategy is to use the "constant" code paths that already exist in the stdlib, but with a circuit - * simulator as its context (rather than, for instance, a nullptr). In cases where this doesn't quite work, we use - * template metaprogramming to provide an alternative implementation. - * - * This means changing the data model in some ways. Since a simulator does not contain a `variables` vector, we cannot - * work with witness indices anymore. In particular, the witness index of every witness instantiated with a simulator - * type will be `IS_CONSTANT`, and the witness is just a wrapper for a value. A stdlib `field_t` instance will now store - * all of its data in the `additive_constant`, and something similar is true for the `uint` classes. Changes are also - * necessary with some basic builder functions, like `assert_equal`, which would take in a pair of witness indices for a - * real builder, but which just takes in a pair of native field elements during simulation. This strategy has a - * relatively small footprint, but it feels possible to improve upon the idioms, reduce the size of the divergence, or - * perhaps organize things more cleanly in a way that avoids the use of compile time `if` statements. - * - */ -// TODO(https://github.com/AztecProtocol/barretenberg/issues/961): Ensure we can execute the simulator in the context of -// ECCVM which is instantiated on Grumpkin -class CircuitSimulatorBN254 { - public: - using FF = bb::fr; - using EmbeddedCurve = std::conditional_t, curve::BN254, curve::Grumpkin>; - static constexpr CircuitType CIRCUIT_TYPE = CircuitType::ULTRA; - static constexpr std::string_view NAME_STRING = "SIMULATOR"; - bool contains_pairing_point_accumulator = false; - static constexpr size_t UINT_LOG2_BASE = 2; // Would be 6 for UltraPlonk - static constexpr size_t DEFAULT_PLOOKUP_RANGE_BITNUM = 1028; - - size_t num_gates = 0; - static constexpr uint32_t zero_idx = 0; // Should agree with what is in circuit builders - std::vector public_inputs; - std::vector used_witnesses; - - inline uint32_t add_variable([[maybe_unused]] const bb::fr index) const { return 1028; } - inline bb::fr get_variable([[maybe_unused]] const uint32_t index) const { return 1028; } - - uint32_t put_constant_variable([[maybe_unused]] const bb::fr& variable) { return 1028; } - uint32_t set_public_input([[maybe_unused]] const uint32_t witness_index) { return 0; } - - uint32_t set_public_input(const bb::fr value) - { - public_inputs.emplace_back(value); - return 0; - } - - void fix_witness([[maybe_unused]] const uint32_t witness_index, [[maybe_unused]] const bb::fr& witness_value){}; - - [[nodiscard]] size_t get_estimated_num_finalized_gates() const { return 0; } - - void create_add_gate([[maybe_unused]] const add_triple_& in){}; - void create_mul_gate([[maybe_unused]] const mul_triple_& in){}; - void create_bool_gate([[maybe_unused]] const uint32_t a){}; - void create_poly_gate([[maybe_unused]] const poly_triple_& in){}; - void create_big_add_gate([[maybe_unused]] const add_quad_& in){}; - void create_big_add_gate_with_bit_extraction([[maybe_unused]] const add_quad_& in){}; - void create_big_mul_gate([[maybe_unused]] const mul_quad_& in){}; - void create_balanced_add_gate([[maybe_unused]] const add_quad_& in){}; - void create_fixed_group_add_gate([[maybe_unused]] const fixed_group_add_quad_& in){}; - void create_fixed_group_add_gate_with_init([[maybe_unused]] const fixed_group_add_quad_& in, - [[maybe_unused]] const fixed_group_init_quad_& init){}; - void create_fixed_group_add_gate_final([[maybe_unused]] const add_quad_& in){}; - void create_ecc_add_gate([[maybe_unused]] const ecc_add_gate_& in){}; - - void create_poseidon2_internal_gate(const poseidon2_internal_gate_& in); - void create_poseidon2_external_gate(const poseidon2_external_gate_& in); - - plookup::ReadData create_gates_from_plookup_accumulators( - [[maybe_unused]] const plookup::MultiTableId& id, - [[maybe_unused]] const plookup::ReadData& read_values, - [[maybe_unused]] const uint32_t key_a_index, - [[maybe_unused]] std::optional key_b_index = std::nullopt) - { - return {}; - }; - - std::vector decompose_into_default_range( - [[maybe_unused]] const uint32_t variable_index, - [[maybe_unused]] const uint64_t num_bits, - [[maybe_unused]] const uint64_t target_range_bitnum = 1028, - [[maybe_unused]] std::string const& msg = "decompose_into_default_range") - { - return {}; - }; - - std::vector decompose_into_default_range_better_for_oddlimbnum( - [[maybe_unused]] const uint32_t variable_index, - [[maybe_unused]] const size_t num_bits, - [[maybe_unused]] std::string const& msg = "decompose_into_default_range_better_for_oddlimbnum") - { - return {}; - }; - void create_dummy_constraints([[maybe_unused]] const std::vector& variable_index){}; - void create_sort_constraint([[maybe_unused]] const std::vector& variable_index){}; - void create_sort_constraint_with_edges([[maybe_unused]] const std::vector& variable_index, - [[maybe_unused]] const FF&, - [[maybe_unused]] const FF&){}; - void assign_tag([[maybe_unused]] const uint32_t variable_index, [[maybe_unused]] const uint32_t tag){}; - - accumulator_triple_ create_and_constraint([[maybe_unused]] const uint32_t a, - [[maybe_unused]] const uint32_t b, - [[maybe_unused]] const size_t num_bits) - { - return { { 1028 }, { 1028 }, { 1028 } }; - }; - accumulator_triple_ create_xor_constraint([[maybe_unused]] const uint32_t a, - [[maybe_unused]] const uint32_t b, - [[maybe_unused]] const size_t num_bits) - { - return { { 1028 }, { 1028 }, { 1028 } }; - }; - - size_t get_num_constant_gates() { return 1028; }; - // maybe this shouldn't be implemented? - - bool create_range_constraint(FF const& elt, - size_t const& num_bits, - std::string const& msg = "create_range_constraint") - { - const bool constraint_holds = static_cast(elt).get_msb() < num_bits; - if (!constraint_holds) { - failure(msg); - } - return constraint_holds; - } - - std::vector decompose_into_base4_accumulators( - [[maybe_unused]] const uint32_t witness_index, - [[maybe_unused]] const size_t num_bits, - [[maybe_unused]] std::string const& msg = "create_range_constraint") - { - return { 1028 }; - }; - - void create_new_range_constraint([[maybe_unused]] const uint32_t variable_index, - [[maybe_unused]] const uint64_t target_range, - [[maybe_unused]] std::string const msg = "create_new_range_constraint"){}; - - void assert_equal(FF left, FF right, std::string const& msg) - { - if (left != right) { - failure(msg); - } - } - - void assert_equal_constant(FF left, FF right, std::string const& msg) { assert_equal(left, right, msg); } - - bool _failed = false; - std::string _err; - - [[nodiscard]] bool failed() const { return _failed; }; - [[nodiscard]] const std::string& err() const { return _err; }; - - void set_err(std::string msg) { _err = std::move(msg); } - void failure(std::string msg) - { - _failed = true; - set_err(std::move(msg)); - } - - [[nodiscard]] bool check_circuit() const { return !_failed; } - - size_t create_ROM_array([[maybe_unused]] const size_t array_size) { return {}; } - - void set_ROM_element_pair([[maybe_unused]] const size_t rom_id, - [[maybe_unused]] const size_t index_value, - [[maybe_unused]] const std::array& value_witnesses) - {} - uint32_t read_ROM_array([[maybe_unused]] const size_t rom_id, [[maybe_unused]] const uint32_t index_witness) - { - return {}; - } - std::array read_ROM_array_pair([[maybe_unused]] const size_t rom_id, - [[maybe_unused]] const uint32_t index_witness) - { - return {}; - } - void create_ecc_dbl_gate([[maybe_unused]] const ecc_dbl_gate_& in){}; - - std::vector get_used_witnesses() { return used_witnesses; } - void update_used_witnesses([[maybe_unused]] uint32_t var_idx){}; - // Public input indices which contain recursive proof information - PairingPointAccumulatorPubInputIndices pairing_point_accumulator_public_input_indices; -}; - -class SimulatorCircuitChecker { - public: - static bool check(const CircuitSimulatorBN254& builder) { return builder.check_circuit(); } -}; - -} // namespace bb