diff --git a/barretenberg/cpp/CMakePresets.json b/barretenberg/cpp/CMakePresets.json index b80ee58f10b2..c30f1c99e064 100644 --- a/barretenberg/cpp/CMakePresets.json +++ b/barretenberg/cpp/CMakePresets.json @@ -317,6 +317,7 @@ "FUZZING": "ON", "FUZZING_SHOW_INFORMATION": "ON", "DISABLE_AZTEC_VM": "ON", + "AVM_TRANSPILER_LIB": "", "ENABLE_ASAN": "ON", "DISABLE_ASM": "ON", "CMAKE_BUILD_TYPE": "RelWithAssert" diff --git a/barretenberg/cpp/bootstrap.sh b/barretenberg/cpp/bootstrap.sh index b1eda7e6219e..1fb0f525a40d 100755 --- a/barretenberg/cpp/bootstrap.sh +++ b/barretenberg/cpp/bootstrap.sh @@ -185,9 +185,12 @@ function build_smt_verification { cmake --preset smt-verification cvc5_cmake_hash=$(cache_content_hash ^barretenberg/cpp/src/barretenberg/smt_verification/CMakeLists.txt) - if ! cache_download barretenberg-cvc5-$cvc5_cmake_hash.zst; then - cmake --build build-smt --target cvc5 - cache_upload barretenberg-cvc5-$cvc5_cmake_hash.zst build-smt/_deps/cvc5 + if cache_download barretenberg-cvc5-$cvc5_cmake_hash.zst; then + # Restore machine-dependent paths after downloading cache + find build-smt/_deps/cvc5 -type f -name "*.cmake" -exec sed -i "s|/workspace|$(pwd)|g" {} \; + else + cmake --build build-smt --target cvc5 + cache_upload barretenberg-cvc5-$cvc5_cmake_hash.zst build-smt/_deps/cvc5 fi cmake --build build-smt --target smt_verification_tests diff --git a/barretenberg/cpp/src/barretenberg/boomerang_value_detection/graph.cpp b/barretenberg/cpp/src/barretenberg/boomerang_value_detection/graph.cpp index 39734b25bfbc..33d527bb5e6f 100644 --- a/barretenberg/cpp/src/barretenberg/boomerang_value_detection/graph.cpp +++ b/barretenberg/cpp/src/barretenberg/boomerang_value_detection/graph.cpp @@ -227,11 +227,11 @@ inline std::vector StaticAnalyzer_::get_sort_const in order to pad a size of indices to gate width. But tool has to ignore these additional variables */ for (const auto& var_idx : row_variables) { - if (var_idx != circuit_builder.zero_idx) { + if (var_idx != circuit_builder.zero_idx()) { gate_variables.emplace_back(var_idx); } } - if (index != block.size() - 1 && block.w_l()[index + 1] != circuit_builder.zero_idx) { + if (index != block.size() - 1 && block.w_l()[index + 1] != circuit_builder.zero_idx()) { gate_variables.emplace_back(block.w_l()[index + 1]); } } @@ -502,10 +502,10 @@ inline std::vector StaticAnalyzer_::get_rom_table_ // By default ROM read gate uses variables (w_1, w_2, w_3, w_4) = (index_witness, vc1_witness, // vc2_witness, record_witness) So we can update all of them gate_variables.emplace_back(index_witness); - if (vc1_witness != circuit_builder.zero_idx) { + if (vc1_witness != circuit_builder.zero_idx()) { gate_variables.emplace_back(vc1_witness); } - if (vc2_witness != circuit_builder.zero_idx) { + if (vc2_witness != circuit_builder.zero_idx()) { gate_variables.emplace_back(vc2_witness); } gate_variables.emplace_back(record_witness); @@ -556,10 +556,10 @@ inline std::vector StaticAnalyzer_::get_ram_table_ // By default RAM read/write gate uses variables (w_1, w_2, w_3, w_4) = (index_witness, // timestamp_witness, value_witness, record_witness) So we can update all of them gate_variables.emplace_back(index_witness); - if (timestamp_witness != circuit_builder.zero_idx) { + if (timestamp_witness != circuit_builder.zero_idx()) { gate_variables.emplace_back(timestamp_witness); } - if (value_witness != circuit_builder.zero_idx) { + if (value_witness != circuit_builder.zero_idx()) { gate_variables.emplace_back(value_witness); } gate_variables.emplace_back(record_witness); @@ -617,7 +617,7 @@ inline std::vector StaticAnalyzer_::get_eccop_part std::vector second_row_variables; auto w1 = blk.w_l()[index]; // get opcode of operation, because function get_ecc_op_idx returns type // uint32_t and it adds as w1 - if (w1 != circuit_builder.zero_idx) { + if (w1 != circuit_builder.zero_idx()) { // this is opcode and start of the UltraOp element first_row_variables.insert( first_row_variables.end(), @@ -809,7 +809,7 @@ void StaticAnalyzer_::connect_all_variables_in_vector(const variables_vector.end(), std::back_inserter(filtered_variables_vector), [&](uint32_t variable_index) { - return variable_index != circuit_builder.zero_idx && + return variable_index != circuit_builder.zero_idx() && this->check_is_not_constant_variable(variable_index); }); // Remove duplicates @@ -975,7 +975,7 @@ template inline size_t StaticAnalyzer_::process_current_decompose_chain(size_t index) { auto& arithmetic_block = circuit_builder.blocks.arithmetic; - auto zero_idx = circuit_builder.zero_idx; + auto zero_idx = circuit_builder.zero_idx(); size_t current_index = index; std::vector accumulators_indices; while (true) { diff --git a/barretenberg/cpp/src/barretenberg/boomerang_value_detection/graph_description.test.cpp b/barretenberg/cpp/src/barretenberg/boomerang_value_detection/graph_description.test.cpp index a9b3cedf5426..42c41a75fb46 100644 --- a/barretenberg/cpp/src/barretenberg/boomerang_value_detection/graph_description.test.cpp +++ b/barretenberg/cpp/src/barretenberg/boomerang_value_detection/graph_description.test.cpp @@ -662,7 +662,7 @@ TEST(boomerang_ultra_circuit_constructor, composed_range_constraint) auto e = fr(d); auto a_idx = circuit_constructor.add_variable(fr(e)); circuit_constructor.create_add_gate( - { a_idx, circuit_constructor.zero_idx, circuit_constructor.zero_idx, 1, 0, 0, -fr(e) }); + { a_idx, circuit_constructor.zero_idx(), circuit_constructor.zero_idx(), 1, 0, 0, -fr(e) }); circuit_constructor.decompose_into_default_range(a_idx, 134); StaticAnalyzer graph = StaticAnalyzer(circuit_constructor); diff --git a/barretenberg/cpp/src/barretenberg/boomerang_value_detection/variable_gates_count.test.cpp b/barretenberg/cpp/src/barretenberg/boomerang_value_detection/variable_gates_count.test.cpp index a60dae316f1e..5d1095846e6c 100644 --- a/barretenberg/cpp/src/barretenberg/boomerang_value_detection/variable_gates_count.test.cpp +++ b/barretenberg/cpp/src/barretenberg/boomerang_value_detection/variable_gates_count.test.cpp @@ -20,7 +20,7 @@ TEST(boomerang_ultra_circuit_constructor, test_variable_gates_count_for_decompos auto e = fr(d); auto a_idx = circuit_constructor.add_variable(fr(e)); circuit_constructor.create_add_gate( - { a_idx, circuit_constructor.zero_idx, circuit_constructor.zero_idx, 1, 0, 0, -fr(e) }); + { a_idx, circuit_constructor.zero_idx(), circuit_constructor.zero_idx(), 1, 0, 0, -fr(e) }); circuit_constructor.decompose_into_default_range(a_idx, 134); StaticAnalyzer graph = StaticAnalyzer(circuit_constructor); @@ -36,7 +36,7 @@ TEST(boomerang_ultra_circuit_constructor, test_variable_gates_count_for_decompos auto e = fr(d); auto a_idx = circuit_constructor.add_variable(fr(e)); circuit_constructor.create_add_gate( - { a_idx, circuit_constructor.zero_idx, circuit_constructor.zero_idx, 1, 0, 0, -fr(e) }); + { a_idx, circuit_constructor.zero_idx(), circuit_constructor.zero_idx(), 1, 0, 0, -fr(e) }); circuit_constructor.decompose_into_default_range(a_idx, 42); StaticAnalyzer graph = StaticAnalyzer(circuit_constructor); @@ -78,9 +78,9 @@ TEST(boomerang_ultra_circuit_constructor, test_variable_gates_count_for_two_deco auto a1_idx = circuit_constructor.add_variable(fr(e1)); auto a2_idx = circuit_constructor.add_variable(fr(e2)); circuit_constructor.create_add_gate( - { a1_idx, circuit_constructor.zero_idx, circuit_constructor.zero_idx, 1, 0, 0, -fr(e1) }); + { a1_idx, circuit_constructor.zero_idx(), circuit_constructor.zero_idx(), 1, 0, 0, -fr(e1) }); circuit_constructor.create_add_gate( - { a2_idx, circuit_constructor.zero_idx, circuit_constructor.zero_idx, 1, 0, 0, -fr(e2) }); + { a2_idx, circuit_constructor.zero_idx(), circuit_constructor.zero_idx(), 1, 0, 0, -fr(e2) }); circuit_constructor.decompose_into_default_range(a1_idx, 42); circuit_constructor.decompose_into_default_range(a2_idx, 42); @@ -122,7 +122,7 @@ TEST(boomerang_ultra_circuit_constructor, test_decompose_for_6_bit_number) auto e = fr(d); auto a_idx = circuit_constructor.add_variable(fr(d)); circuit_constructor.create_add_gate( - { a_idx, circuit_constructor.zero_idx, circuit_constructor.zero_idx, 1, 0, 0, -fr(e) }); + { a_idx, circuit_constructor.zero_idx(), circuit_constructor.zero_idx(), 1, 0, 0, -fr(e) }); circuit_constructor.decompose_into_default_range(a_idx, 6); StaticAnalyzer graph = StaticAnalyzer(circuit_constructor); diff --git a/barretenberg/cpp/src/barretenberg/circuit_checker/ultra_circuit_builder.test.cpp b/barretenberg/cpp/src/barretenberg/circuit_checker/ultra_circuit_builder.test.cpp index a4fdfd62fa56..9eaf8f2b4ec7 100644 --- a/barretenberg/cpp/src/barretenberg/circuit_checker/ultra_circuit_builder.test.cpp +++ b/barretenberg/cpp/src/barretenberg/circuit_checker/ultra_circuit_builder.test.cpp @@ -112,8 +112,8 @@ TEST(UltraCircuitBuilder, BadLookupFailure) // Erroneously set a non-zero wire value to zero in one of the lookup gates for (auto& wire_3_witness_idx : builder.blocks.lookup.w_o()) { - if (wire_3_witness_idx != builder.zero_idx) { - wire_3_witness_idx = builder.zero_idx; + if (wire_3_witness_idx != builder.zero_idx()) { + wire_3_witness_idx = builder.zero_idx(); break; } } @@ -210,8 +210,8 @@ TEST(UltraCircuitBuilder, NonTrivialTagPermutation) auto c_idx = builder.add_variable(b); auto d_idx = builder.add_variable(a); - builder.create_add_gate({ a_idx, b_idx, builder.zero_idx, fr::one(), fr::one(), fr::zero(), fr::zero() }); - builder.create_add_gate({ c_idx, d_idx, builder.zero_idx, fr::one(), fr::one(), fr::zero(), fr::zero() }); + builder.create_add_gate({ a_idx, b_idx, builder.zero_idx(), fr::one(), fr::one(), fr::zero(), fr::zero() }); + builder.create_add_gate({ c_idx, d_idx, builder.zero_idx(), fr::one(), fr::one(), fr::zero(), fr::zero() }); builder.create_tag(1, 2); builder.create_tag(2, 1); @@ -256,9 +256,9 @@ TEST(UltraCircuitBuilder, NonTrivialTagPermutationAndCycles) builder.assign_tag(e_idx, 2); builder.assign_tag(g_idx, 2); - builder.create_add_gate({ b_idx, a_idx, builder.zero_idx, fr::one(), fr::neg_one(), fr::zero(), fr::zero() }); - builder.create_add_gate({ c_idx, g_idx, builder.zero_idx, fr::one(), -fr::one(), fr::zero(), fr::zero() }); - builder.create_add_gate({ e_idx, f_idx, builder.zero_idx, fr::one(), -fr::one(), fr::zero(), fr::zero() }); + builder.create_add_gate({ b_idx, a_idx, builder.zero_idx(), fr::one(), fr::neg_one(), fr::zero(), fr::zero() }); + builder.create_add_gate({ c_idx, g_idx, builder.zero_idx(), fr::one(), -fr::one(), fr::zero(), fr::zero() }); + builder.create_add_gate({ e_idx, f_idx, builder.zero_idx(), fr::one(), -fr::one(), fr::zero(), fr::zero() }); bool result = CircuitChecker::check(builder); EXPECT_EQ(result, true); @@ -278,8 +278,8 @@ TEST(UltraCircuitBuilder, BadTagPermutation) auto c_idx = builder.add_variable(b); auto d_idx = builder.add_variable(a + 1); - builder.create_add_gate({ a_idx, b_idx, builder.zero_idx, 1, 1, 0, 0 }); - builder.create_add_gate({ c_idx, d_idx, builder.zero_idx, 1, 1, 0, -1 }); + builder.create_add_gate({ a_idx, b_idx, builder.zero_idx(), 1, 1, 0, 0 }); + builder.create_add_gate({ c_idx, d_idx, builder.zero_idx(), 1, 1, 0, -1 }); bool result = CircuitChecker::check(builder); EXPECT_EQ(result, true); @@ -431,7 +431,7 @@ TEST(UltraCircuitBuilder, RangeConstraint) builder.create_new_range_constraint(indices[i], 3); } // auto ind = {a_idx,b_idx,c_idx,d_idx,e_idx,f_idx,g_idx,h_idx}; - builder.create_dummy_constraints(indices); + builder.create_unconstrained_gates(indices); bool result = CircuitChecker::check(builder); EXPECT_EQ(result, true); } @@ -452,7 +452,7 @@ TEST(UltraCircuitBuilder, RangeConstraint) for (size_t i = 0; i < indices.size(); i++) { builder.create_new_range_constraint(indices[i], 128); } - builder.create_dummy_constraints(indices); + builder.create_unconstrained_gates(indices); bool result = CircuitChecker::check(builder); EXPECT_EQ(result, true); } @@ -463,7 +463,7 @@ TEST(UltraCircuitBuilder, RangeConstraint) for (size_t i = 0; i < indices.size(); i++) { builder.create_new_range_constraint(indices[i], 79); } - builder.create_dummy_constraints(indices); + builder.create_unconstrained_gates(indices); bool result = CircuitChecker::check(builder); EXPECT_EQ(result, false); } @@ -474,7 +474,7 @@ TEST(UltraCircuitBuilder, RangeConstraint) for (size_t i = 0; i < indices.size(); i++) { builder.create_new_range_constraint(indices[i], 79); } - builder.create_dummy_constraints(indices); + builder.create_unconstrained_gates(indices); bool result = CircuitChecker::check(builder); EXPECT_EQ(result, false); } @@ -488,10 +488,10 @@ TEST(UltraCircuitBuilder, RangeWithGates) builder.create_new_range_constraint(idx[i], 8); } - builder.create_add_gate({ idx[0], idx[1], builder.zero_idx, fr::one(), fr::one(), fr::zero(), -3 }); - builder.create_add_gate({ idx[2], idx[3], builder.zero_idx, fr::one(), fr::one(), fr::zero(), -7 }); - builder.create_add_gate({ idx[4], idx[5], builder.zero_idx, fr::one(), fr::one(), fr::zero(), -11 }); - builder.create_add_gate({ idx[6], idx[7], builder.zero_idx, fr::one(), fr::one(), fr::zero(), -15 }); + builder.create_add_gate({ idx[0], idx[1], builder.zero_idx(), fr::one(), fr::one(), fr::zero(), -3 }); + builder.create_add_gate({ idx[2], idx[3], builder.zero_idx(), fr::one(), fr::one(), fr::zero(), -7 }); + builder.create_add_gate({ idx[4], idx[5], builder.zero_idx(), fr::one(), fr::one(), fr::zero(), -11 }); + builder.create_add_gate({ idx[6], idx[7], builder.zero_idx(), fr::one(), fr::one(), fr::zero(), -15 }); bool result = CircuitChecker::check(builder); EXPECT_EQ(result, true); } @@ -504,10 +504,10 @@ TEST(UltraCircuitBuilder, RangeWithGatesWhereRangeIsNotAPowerOfTwo) builder.create_new_range_constraint(idx[i], 12); } - builder.create_add_gate({ idx[0], idx[1], builder.zero_idx, fr::one(), fr::one(), fr::zero(), -3 }); - builder.create_add_gate({ idx[2], idx[3], builder.zero_idx, fr::one(), fr::one(), fr::zero(), -7 }); - builder.create_add_gate({ idx[4], idx[5], builder.zero_idx, fr::one(), fr::one(), fr::zero(), -11 }); - builder.create_add_gate({ idx[6], idx[7], builder.zero_idx, fr::one(), fr::one(), fr::zero(), -15 }); + builder.create_add_gate({ idx[0], idx[1], builder.zero_idx(), fr::one(), fr::one(), fr::zero(), -3 }); + builder.create_add_gate({ idx[2], idx[3], builder.zero_idx(), fr::one(), fr::one(), fr::zero(), -7 }); + builder.create_add_gate({ idx[4], idx[5], builder.zero_idx(), fr::one(), fr::one(), fr::zero(), -11 }); + builder.create_add_gate({ idx[6], idx[7], builder.zero_idx(), fr::one(), fr::one(), fr::zero(), -15 }); bool result = CircuitChecker::check(builder); EXPECT_EQ(result, true); } @@ -565,7 +565,7 @@ TEST(UltraCircuitBuilder, ComposedRangeConstraint) auto d = uint256_t(c).slice(0, 133); auto e = fr(d); auto a_idx = builder.add_variable(fr(e)); - builder.create_add_gate({ a_idx, builder.zero_idx, builder.zero_idx, 1, 0, 0, -fr(e) }); + builder.create_add_gate({ a_idx, builder.zero_idx(), builder.zero_idx(), 1, 0, 0, -fr(e) }); builder.decompose_into_default_range(a_idx, 134); // odd num bits - divisible by 3 @@ -573,7 +573,7 @@ TEST(UltraCircuitBuilder, ComposedRangeConstraint) auto d_1 = uint256_t(c_1).slice(0, 126); auto e_1 = fr(d_1); auto a_idx_1 = builder.add_variable(fr(e_1)); - builder.create_add_gate({ a_idx_1, builder.zero_idx, builder.zero_idx, 1, 0, 0, -fr(e_1) }); + builder.create_add_gate({ a_idx_1, builder.zero_idx(), builder.zero_idx(), 1, 0, 0, -fr(e_1) }); builder.decompose_into_default_range(a_idx_1, 127); bool result = CircuitChecker::check(builder); @@ -825,9 +825,9 @@ TEST(UltraCircuitBuilder, RamSimple) // Use the result in a simple arithmetic gate builder.create_big_add_gate({ a_idx, - builder.zero_idx, - builder.zero_idx, - builder.zero_idx, + builder.zero_idx(), + builder.zero_idx(), + builder.zero_idx(), -1, 0, 0, @@ -886,9 +886,9 @@ TEST(UltraCircuitBuilder, Ram) true); builder.create_big_add_gate( { - builder.zero_idx, - builder.zero_idx, - builder.zero_idx, + builder.zero_idx(), + builder.zero_idx(), + builder.zero_idx(), e_idx, 0, 0, @@ -952,9 +952,9 @@ TEST(UltraCircuitBuilder, CheckCircuitShowcase) uint32_t b = builder.add_variable(0xbeef); // Let's create 2 gates that will bind these 2 variables to be one these two values builder.create_poly_gate( - { a, a, builder.zero_idx, fr(1), -fr(0xdead) - fr(0xbeef), 0, 0, fr(0xdead) * fr(0xbeef) }); + { a, a, builder.zero_idx(), fr(1), -fr(0xdead) - fr(0xbeef), 0, 0, fr(0xdead) * fr(0xbeef) }); builder.create_poly_gate( - { b, b, builder.zero_idx, fr(1), -fr(0xdead) - fr(0xbeef), 0, 0, fr(0xdead) * fr(0xbeef) }); + { b, b, builder.zero_idx(), fr(1), -fr(0xdead) - fr(0xbeef), 0, 0, fr(0xdead) * fr(0xbeef) }); // We can check if this works EXPECT_EQ(CircuitChecker::check(builder), true); diff --git a/barretenberg/cpp/src/barretenberg/client_ivc/client_ivc.cpp b/barretenberg/cpp/src/barretenberg/client_ivc/client_ivc.cpp index cc85c86a5857..1d07ead885d8 100644 --- a/barretenberg/cpp/src/barretenberg/client_ivc/client_ivc.cpp +++ b/barretenberg/cpp/src/barretenberg/client_ivc/client_ivc.cpp @@ -82,7 +82,7 @@ std::shared_ptr ClientIVC::perform_oink_re OinkRecursiveVerifier verifier{ &circuit, verifier_instance, transcript }; verifier.verify_proof(proof); - verifier_instance->target_sum = StdlibFF::from_witness_index(&circuit, circuit.zero_idx); + verifier_instance->target_sum = StdlibFF::from_witness_index(&circuit, circuit.zero_idx()); // Get the gate challenges for sumcheck/combiner computation verifier_instance->gate_challenges = transcript->template get_powers_of_challenge("gate_challenge", CONST_PG_LOG_N); diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp index 5a3883e06969..8ca8bf7d1da0 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp @@ -661,7 +661,7 @@ template <> UltraCircuitBuilder create_circuit(AcirProgram& program, const Progr AcirFormat& constraints = program.constraints; WitnessVector& witness = program.witness; - Builder builder{ metadata.size_hint, witness, constraints.public_inputs, constraints.varnum, metadata.recursive }; + Builder builder{ metadata.size_hint, witness, constraints.public_inputs, constraints.varnum }; build_constraints(builder, program, metadata); diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_integration.test.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_integration.test.cpp index 58bc68c7b459..afe621936650 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_integration.test.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_integration.test.cpp @@ -85,7 +85,7 @@ class AcirIntegrationTest : public ::testing::Test { val_idx_1, val_idx_2, val_idx_3, - circuit.zero_idx, + circuit.zero_idx(), 1, 1, 1, diff --git a/barretenberg/cpp/src/barretenberg/flavor/mega_flavor.hpp b/barretenberg/cpp/src/barretenberg/flavor/mega_flavor.hpp index 0e7646b3bb13..b6abac34640d 100644 --- a/barretenberg/cpp/src/barretenberg/flavor/mega_flavor.hpp +++ b/barretenberg/cpp/src/barretenberg/flavor/mega_flavor.hpp @@ -179,8 +179,6 @@ class MegaFlavor { databus_id // column 30 // id polynomial, i.e. id_i = i ) - static constexpr CircuitType CIRCUIT_TYPE = CircuitBuilder::CIRCUIT_TYPE; - auto get_non_gate_selectors() { return RefArray{ q_m, q_c, q_l, q_r, q_o, q_4 }; }; auto get_gate_selectors() { diff --git a/barretenberg/cpp/src/barretenberg/flavor/ultra_flavor.hpp b/barretenberg/cpp/src/barretenberg/flavor/ultra_flavor.hpp index eab7d1a85d2b..fff3e8b07efa 100644 --- a/barretenberg/cpp/src/barretenberg/flavor/ultra_flavor.hpp +++ b/barretenberg/cpp/src/barretenberg/flavor/ultra_flavor.hpp @@ -182,8 +182,6 @@ class UltraFlavor { lagrange_first, // column 26 lagrange_last) // column 27 - static constexpr CircuitType CIRCUIT_TYPE = CircuitBuilder::CIRCUIT_TYPE; - auto get_non_gate_selectors() { return RefArray{ q_m, q_c, q_l, q_r, q_o, q_4 }; } auto get_gate_selectors() { diff --git a/barretenberg/cpp/src/barretenberg/honk/execution_trace/mega_execution_trace.hpp b/barretenberg/cpp/src/barretenberg/honk/execution_trace/mega_execution_trace.hpp index 02a32227106b..b085291ca5af 100644 --- a/barretenberg/cpp/src/barretenberg/honk/execution_trace/mega_execution_trace.hpp +++ b/barretenberg/cpp/src/barretenberg/honk/execution_trace/mega_execution_trace.hpp @@ -136,6 +136,11 @@ class MegaTraceBlock : public ExecutionTraceBlock { */ void resize_additional(size_t new_size) { q_busread().resize(new_size); }; + /** + * @brief Default implementation does nothing + */ + virtual void set_gate_selector([[maybe_unused]] const fr& value) {} + private: std::array, 9> zero_selectors; }; @@ -146,6 +151,19 @@ class MegaTraceBusReadBlock : public MegaTraceBlock { public: SelectorType& q_busread() override { return gate_selector; } + void set_gate_selector(const fr& value) override + { + gate_selector.emplace_back(value); + q_lookup_type().emplace_back(0); + q_arith().emplace_back(0); + q_delta_range().emplace_back(0); + q_elliptic().emplace_back(0); + q_memory().emplace_back(0); + q_nnf().emplace_back(0); + q_poseidon2_external().emplace_back(0); + q_poseidon2_internal().emplace_back(0); + } + private: SlabVectorSelector gate_selector; }; @@ -154,6 +172,19 @@ class MegaTraceLookupBlock : public MegaTraceBlock { public: SelectorType& q_lookup_type() override { return gate_selector; } + void set_gate_selector(const fr& value) override + { + q_busread().emplace_back(0); + gate_selector.emplace_back(value); + q_arith().emplace_back(0); + q_delta_range().emplace_back(0); + q_elliptic().emplace_back(0); + q_memory().emplace_back(0); + q_nnf().emplace_back(0); + q_poseidon2_external().emplace_back(0); + q_poseidon2_internal().emplace_back(0); + } + private: SlabVectorSelector gate_selector; }; @@ -162,6 +193,19 @@ class MegaTraceArithmeticBlock : public MegaTraceBlock { public: SelectorType& q_arith() override { return gate_selector; } + void set_gate_selector(const fr& value) override + { + q_busread().emplace_back(0); + q_lookup_type().emplace_back(0); + gate_selector.emplace_back(value); + q_delta_range().emplace_back(0); + q_elliptic().emplace_back(0); + q_memory().emplace_back(0); + q_nnf().emplace_back(0); + q_poseidon2_external().emplace_back(0); + q_poseidon2_internal().emplace_back(0); + } + private: SlabVectorSelector gate_selector; }; @@ -170,6 +214,19 @@ class MegaTraceDeltaRangeBlock : public MegaTraceBlock { public: SelectorType& q_delta_range() override { return gate_selector; } + void set_gate_selector(const fr& value) override + { + q_busread().emplace_back(0); + q_lookup_type().emplace_back(0); + q_arith().emplace_back(0); + gate_selector.emplace_back(value); + q_elliptic().emplace_back(0); + q_memory().emplace_back(0); + q_nnf().emplace_back(0); + q_poseidon2_external().emplace_back(0); + q_poseidon2_internal().emplace_back(0); + } + private: SlabVectorSelector gate_selector; }; @@ -178,6 +235,19 @@ class MegaTraceEllipticBlock : public MegaTraceBlock { public: SelectorType& q_elliptic() override { return gate_selector; } + void set_gate_selector(const fr& value) override + { + q_busread().emplace_back(0); + q_lookup_type().emplace_back(0); + q_arith().emplace_back(0); + q_delta_range().emplace_back(0); + gate_selector.emplace_back(value); + q_memory().emplace_back(0); + q_nnf().emplace_back(0); + q_poseidon2_external().emplace_back(0); + q_poseidon2_internal().emplace_back(0); + } + private: SlabVectorSelector gate_selector; }; @@ -186,6 +256,19 @@ class MegaTraceMemoryBlock : public MegaTraceBlock { public: SelectorType& q_memory() override { return gate_selector; } + void set_gate_selector(const fr& value) override + { + q_busread().emplace_back(0); + q_lookup_type().emplace_back(0); + q_arith().emplace_back(0); + q_delta_range().emplace_back(0); + q_elliptic().emplace_back(0); + gate_selector.emplace_back(value); + q_nnf().emplace_back(0); + q_poseidon2_external().emplace_back(0); + q_poseidon2_internal().emplace_back(0); + } + private: SlabVectorSelector gate_selector; }; @@ -194,6 +277,19 @@ class MegaTraceNonNativeFieldBlock : public MegaTraceBlock { public: SelectorType& q_nnf() override { return gate_selector; } + void set_gate_selector(const fr& value) override + { + q_busread().emplace_back(0); + q_lookup_type().emplace_back(0); + q_arith().emplace_back(0); + q_delta_range().emplace_back(0); + q_elliptic().emplace_back(0); + q_memory().emplace_back(0); + gate_selector.emplace_back(value); + q_poseidon2_external().emplace_back(0); + q_poseidon2_internal().emplace_back(0); + } + private: SlabVectorSelector gate_selector; }; @@ -202,6 +298,19 @@ class MegaTracePoseidon2ExternalBlock : public MegaTraceBlock { public: SelectorType& q_poseidon2_external() override { return gate_selector; } + void set_gate_selector(const fr& value) override + { + q_busread().emplace_back(0); + q_lookup_type().emplace_back(0); + q_arith().emplace_back(0); + q_delta_range().emplace_back(0); + q_elliptic().emplace_back(0); + q_memory().emplace_back(0); + q_nnf().emplace_back(0); + gate_selector.emplace_back(value); + q_poseidon2_internal().emplace_back(0); + } + private: SlabVectorSelector gate_selector; }; @@ -210,6 +319,19 @@ class MegaTracePoseidon2InternalBlock : public MegaTraceBlock { public: SelectorType& q_poseidon2_internal() override { return gate_selector; } + void set_gate_selector(const fr& value) override + { + q_busread().emplace_back(0); + q_lookup_type().emplace_back(0); + q_arith().emplace_back(0); + q_delta_range().emplace_back(0); + q_elliptic().emplace_back(0); + q_memory().emplace_back(0); + q_nnf().emplace_back(0); + q_poseidon2_external().emplace_back(0); + gate_selector.emplace_back(value); + } + private: SlabVectorSelector gate_selector; }; @@ -483,6 +605,4 @@ static constexpr TraceStructure AZTEC_TRACE_STRUCTURE{ .ecc_op = 1000, .poseidon2_internal = 96500, .overflow = 0 }; -template -concept HasAdditionalSelectors = IsAnyOf; } // namespace bb diff --git a/barretenberg/cpp/src/barretenberg/honk/execution_trace/ultra_execution_trace.hpp b/barretenberg/cpp/src/barretenberg/honk/execution_trace/ultra_execution_trace.hpp index c2ea600fc6b0..50eccde40d0c 100644 --- a/barretenberg/cpp/src/barretenberg/honk/execution_trace/ultra_execution_trace.hpp +++ b/barretenberg/cpp/src/barretenberg/honk/execution_trace/ultra_execution_trace.hpp @@ -43,6 +43,11 @@ class UltraTraceBlock : public ExecutionTraceBlock { q_poseidon2_internal() }; } + /** + * @brief Default implementation does nothing + */ + virtual void set_gate_selector([[maybe_unused]] const fr& value) {} + private: std::array, 8> zero_selectors; }; @@ -53,6 +58,18 @@ class UltraTraceLookupBlock : public UltraTraceBlock { public: SelectorType& q_lookup_type() override { return gate_selector; } + void set_gate_selector(const fr& value) override + { + gate_selector.emplace_back(value); + q_arith().emplace_back(0); + q_delta_range().emplace_back(0); + q_elliptic().emplace_back(0); + q_memory().emplace_back(0); + q_nnf().emplace_back(0); + q_poseidon2_external().emplace_back(0); + q_poseidon2_internal().emplace_back(0); + } + private: SlabVectorSelector gate_selector; }; @@ -61,6 +78,18 @@ class UltraTraceArithmeticBlock : public UltraTraceBlock { public: SelectorType& q_arith() override { return gate_selector; } + void set_gate_selector(const fr& value) override + { + q_lookup_type().emplace_back(0); + gate_selector.emplace_back(value); + q_delta_range().emplace_back(0); + q_elliptic().emplace_back(0); + q_memory().emplace_back(0); + q_nnf().emplace_back(0); + q_poseidon2_external().emplace_back(0); + q_poseidon2_internal().emplace_back(0); + } + private: SlabVectorSelector gate_selector; }; @@ -69,6 +98,18 @@ class UltraTraceDeltaRangeBlock : public UltraTraceBlock { public: SelectorType& q_delta_range() override { return gate_selector; } + void set_gate_selector(const fr& value) override + { + q_lookup_type().emplace_back(0); + q_arith().emplace_back(0); + gate_selector.emplace_back(value); + q_elliptic().emplace_back(0); + q_memory().emplace_back(0); + q_nnf().emplace_back(0); + q_poseidon2_external().emplace_back(0); + q_poseidon2_internal().emplace_back(0); + } + private: SlabVectorSelector gate_selector; }; @@ -77,6 +118,18 @@ class UltraTraceEllipticBlock : public UltraTraceBlock { public: SelectorType& q_elliptic() override { return gate_selector; } + void set_gate_selector(const fr& value) override + { + q_lookup_type().emplace_back(0); + q_arith().emplace_back(0); + q_delta_range().emplace_back(0); + gate_selector.emplace_back(value); + q_memory().emplace_back(0); + q_nnf().emplace_back(0); + q_poseidon2_external().emplace_back(0); + q_poseidon2_internal().emplace_back(0); + } + private: SlabVectorSelector gate_selector; }; @@ -85,6 +138,18 @@ class UltraTraceMemoryBlock : public UltraTraceBlock { public: SelectorType& q_memory() override { return gate_selector; } + void set_gate_selector(const fr& value) override + { + q_lookup_type().emplace_back(0); + q_arith().emplace_back(0); + q_delta_range().emplace_back(0); + q_elliptic().emplace_back(0); + gate_selector.emplace_back(value); + q_nnf().emplace_back(0); + q_poseidon2_external().emplace_back(0); + q_poseidon2_internal().emplace_back(0); + } + private: SlabVectorSelector gate_selector; }; @@ -93,6 +158,18 @@ class UltraTraceNonNativeFieldBlock : public UltraTraceBlock { public: SelectorType& q_nnf() override { return gate_selector; } + void set_gate_selector(const fr& value) override + { + q_lookup_type().emplace_back(0); + q_arith().emplace_back(0); + q_delta_range().emplace_back(0); + q_elliptic().emplace_back(0); + q_memory().emplace_back(0); + gate_selector.emplace_back(value); + q_poseidon2_external().emplace_back(0); + q_poseidon2_internal().emplace_back(0); + } + private: SlabVectorSelector gate_selector; }; @@ -101,6 +178,18 @@ class UltraTracePoseidon2ExternalBlock : public UltraTraceBlock { public: SelectorType& q_poseidon2_external() override { return gate_selector; } + void set_gate_selector(const fr& value) override + { + q_lookup_type().emplace_back(0); + q_arith().emplace_back(0); + q_delta_range().emplace_back(0); + q_elliptic().emplace_back(0); + q_memory().emplace_back(0); + q_nnf().emplace_back(0); + gate_selector.emplace_back(value); + q_poseidon2_internal().emplace_back(0); + } + private: SlabVectorSelector gate_selector; }; @@ -109,6 +198,18 @@ class UltraTracePoseidon2InternalBlock : public UltraTraceBlock { public: SelectorType& q_poseidon2_internal() override { return gate_selector; } + void set_gate_selector(const fr& value) override + { + q_lookup_type().emplace_back(0); + q_arith().emplace_back(0); + q_delta_range().emplace_back(0); + q_elliptic().emplace_back(0); + q_memory().emplace_back(0); + q_nnf().emplace_back(0); + q_poseidon2_external().emplace_back(0); + gate_selector.emplace_back(value); + } + private: SlabVectorSelector gate_selector; }; diff --git a/barretenberg/cpp/src/barretenberg/honk/types/merkle_hash_type.hpp b/barretenberg/cpp/src/barretenberg/honk/types/merkle_hash_type.hpp deleted file mode 100644 index ebaf9bcec581..000000000000 --- a/barretenberg/cpp/src/barretenberg/honk/types/merkle_hash_type.hpp +++ /dev/null @@ -1,12 +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 - -namespace bb::merkle { -// TODO(https://github.com/AztecProtocol/barretenberg/issues/426) -enum HashType { FIXED_BASE_PEDERSEN, LOOKUP_PEDERSEN }; -} // namespace bb::merkle diff --git a/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy.test.cpp b/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy.test.cpp index 1c95de44103c..8ff28d4bf9e9 100644 --- a/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy.test.cpp +++ b/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy.test.cpp @@ -392,8 +392,8 @@ template class ProtogalaxyTests : public testing::Test { // Erroneously set a non-zero wire value to zero in one of the lookup gates for (auto& wire_3_witness_idx : builder1.blocks.lookup.w_o()) { - if (wire_3_witness_idx != builder1.zero_idx) { - wire_3_witness_idx = builder1.zero_idx; + if (wire_3_witness_idx != builder1.zero_idx()) { + wire_3_witness_idx = builder1.zero_idx(); break; } } 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 e5805a9bcca4..d356561b2448 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2_permutation.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2_permutation.hpp @@ -77,11 +77,11 @@ template class Poseidon2Permutation { */ static void propagate_current_state_to_next_row(Builder* builder, const State& state, auto& block) { - builder->create_dummy_gate(block, - state[0].get_witness_index(), - state[1].get_witness_index(), - state[2].get_witness_index(), - state[3].get_witness_index()); + builder->create_unconstrained_gate(block, + state[0].get_witness_index(), + state[1].get_witness_index(), + state[2].get_witness_index(), + state[3].get_witness_index()); }; }; 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 ecf039bba53c..7c298be41fc0 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/bigfield/bigfield_impl.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/bigfield/bigfield_impl.hpp @@ -173,8 +173,11 @@ bigfield bigfield::create_from_u512_as_witness(Builder* // NOTE(https://github.com/AztecProtocol/barretenberg/issues/879): Optimisation opportunity to use a single gate // (and remove dummy gate). Currently, dummy gate is necessary for preceeding big add gate as these gates fall in // the arithmetic block. More details on the linked Github issue. - ctx->create_dummy_gate( - ctx->blocks.arithmetic, ctx->zero_idx, ctx->zero_idx, ctx->zero_idx, limb_0.get_normalized_witness_index()); + ctx->create_unconstrained_gate(ctx->blocks.arithmetic, + ctx->zero_idx(), + ctx->zero_idx(), + ctx->zero_idx(), + limb_0.get_normalized_witness_index()); uint64_t num_last_limb_bits = (can_overflow) ? NUM_LIMB_BITS : NUM_LAST_LIMB_BITS; @@ -2079,9 +2082,9 @@ template void bigfield::self_reduce() BB_ASSERT_LT((uint1024_t(1) << maximum_quotient_bits) * uint1024_t(modulus_u512) + DEFAULT_MAXIMUM_REMAINDER, get_maximum_crt_product()); quotient.binary_basis_limbs[0] = Limb(quotient_limb, uint256_t(1) << maximum_quotient_bits); - quotient.binary_basis_limbs[1] = Limb(field_t::from_witness_index(context, context->zero_idx), 0); - quotient.binary_basis_limbs[2] = Limb(field_t::from_witness_index(context, context->zero_idx), 0); - quotient.binary_basis_limbs[3] = Limb(field_t::from_witness_index(context, context->zero_idx), 0); + quotient.binary_basis_limbs[1] = Limb(field_t::from_witness_index(context, context->zero_idx()), 0); + quotient.binary_basis_limbs[2] = Limb(field_t::from_witness_index(context, context->zero_idx()), 0); + quotient.binary_basis_limbs[3] = Limb(field_t::from_witness_index(context, context->zero_idx()), 0); quotient.prime_basis_limb = quotient_limb; // this constructor with can_overflow=false will enforce remainder of size<2^s bigfield remainder = bigfield( @@ -2282,10 +2285,10 @@ void bigfield::unsafe_evaluate_multiply_add(const bigfield& input_le std::array, NUM_LIMBS> remainder_limbs{ field_t::accumulate(limb_0_accumulator), - needs_normalize ? field_t::from_witness_index(ctx, ctx->zero_idx) + needs_normalize ? field_t::from_witness_index(ctx, ctx->zero_idx()) : remainders[0].binary_basis_limbs[1].element, field_t::accumulate(limb_2_accumulator), - needs_normalize ? field_t::from_witness_index(ctx, ctx->zero_idx) + needs_normalize ? field_t::from_witness_index(ctx, ctx->zero_idx()) : remainders[0].binary_basis_limbs[3].element, }; field_t remainder_prime_limb = field_t::accumulate(prime_limb_accumulator); @@ -2579,12 +2582,12 @@ void bigfield::unsafe_evaluate_multiple_multiply_add(const std::vect accumulated_hi = field_t::from_witness_index(ctx, ctx->put_constant_variable(accumulated_hi.get_value())); } - field_t remainder1 = no_remainders ? field_t::from_witness_index(ctx, ctx->zero_idx) + field_t remainder1 = no_remainders ? field_t::from_witness_index(ctx, ctx->zero_idx()) : remainders[0].binary_basis_limbs[1].element; if (remainder1.is_constant()) { remainder1 = field_t::from_witness_index(ctx, ctx->put_constant_variable(remainder1.get_value())); } - field_t remainder3 = no_remainders ? field_t::from_witness_index(ctx, ctx->zero_idx) + field_t remainder3 = no_remainders ? field_t::from_witness_index(ctx, ctx->zero_idx()) : remainders[0].binary_basis_limbs[3].element; if (remainder3.is_constant()) { remainder3 = field_t::from_witness_index(ctx, ctx->put_constant_variable(remainder3.get_value())); diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/biggroup/biggroup.hpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/biggroup/biggroup.hpp index 447b475f5f0b..ef4a3674ca56 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/biggroup/biggroup.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/biggroup/biggroup.hpp @@ -197,7 +197,7 @@ template class element { static element point_at_infinity(Builder* ctx) { - Fr zero = Fr::from_witness_index(ctx, ctx->zero_idx); + Fr zero = Fr::from_witness_index(ctx, ctx->zero_idx()); zero.unset_free_witness_tag(); Fq x_fq(zero, zero); Fq y_fq(zero, zero); diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/biggroup/biggroup_goblin.hpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/biggroup/biggroup_goblin.hpp index 0fe18d7baa92..fa263f7f3283 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/biggroup/biggroup_goblin.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/biggroup/biggroup_goblin.hpp @@ -144,7 +144,7 @@ template class goblin_el static goblin_element point_at_infinity(Builder* ctx) { - Fr zero = Fr::from_witness_index(ctx, ctx->zero_idx); + Fr zero = Fr::from_witness_index(ctx, ctx->zero_idx()); zero.unset_free_witness_tag(); Fq x_fq(zero, zero); Fq y_fq(zero, zero); 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 8c6a2e39bb33..03506b3fd255 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/biggroup/biggroup_nafs.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/biggroup/biggroup_nafs.hpp @@ -109,7 +109,7 @@ Fr element::reconstruct_bigfield_from_wnaf(Builder* builder, // TODO: improve efficiency by creating a constructor that does NOT require us to range constrain // limbs (we already know (sum < 2^{130})) // Convert this value to bigfield element - Fr reconstructed = Fr(sum, field_t::from_witness_index(builder, builder->zero_idx), false); + Fr reconstructed = Fr(sum, field_t::from_witness_index(builder, builder->zero_idx()), false); // Double the final value and add the skew reconstructed = (reconstructed + reconstructed).add_to_lower_limb(positive_skew, uint256_t(1)); return reconstructed; 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 e8d063fec5fc..f15c0ffe114d 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/bool/bool.test.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/bool/bool.test.cpp @@ -175,7 +175,7 @@ template class BoolTest : public ::testing::Test { EXPECT_EQ(builder.get_estimated_num_finalized_gates() - num_gates_start, expected); - builder.create_dummy_constraints(indices); + builder.create_unconstrained_gates(indices); EXPECT_TRUE(CircuitChecker::check(builder)); } diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.cpp index 15e7dce32de2..e9ae510f4a38 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.cpp @@ -553,9 +553,9 @@ template field_t field_t::madd(const field_ field_t result(ctx); result.witness_index = ctx->add_variable(out); ctx->create_big_mul_gate({ - .a = is_constant() ? ctx->zero_idx : witness_index, - .b = to_mul.is_constant() ? ctx->zero_idx : to_mul.witness_index, - .c = to_add.is_constant() ? ctx->zero_idx : to_add.witness_index, + .a = is_constant() ? ctx->zero_idx() : witness_index, + .b = to_mul.is_constant() ? ctx->zero_idx() : to_mul.witness_index, + .c = to_add.is_constant() ? ctx->zero_idx() : to_add.witness_index, .d = result.witness_index, .mul_scaling = mul_scaling, .a_scaling = a_scaling, @@ -612,9 +612,9 @@ template field_t field_t::add_two(const fie // Constrain the result ctx->create_big_mul_gate({ - .a = is_constant() ? ctx->zero_idx : witness_index, - .b = add_b.is_constant() ? ctx->zero_idx : add_b.witness_index, - .c = add_c.is_constant() ? ctx->zero_idx : add_c.witness_index, + .a = is_constant() ? ctx->zero_idx() : witness_index, + .b = add_b.is_constant() ? ctx->zero_idx() : add_b.witness_index, + .c = add_c.is_constant() ? ctx->zero_idx() : add_c.witness_index, .d = result.witness_index, .mul_scaling = bb::fr::zero(), .a_scaling = a_scaling, @@ -662,7 +662,7 @@ template field_t field_t::normalize() const // this.v * a_scaling + result.v * c_scaling + const_scaling = 0 context->create_add_gate({ .a = witness_index, - .b = context->zero_idx, + .b = context->zero_idx(), .c = result.witness_index, .a_scaling = multiplicative_constant, .b_scaling = bb::fr::zero(), @@ -693,8 +693,8 @@ template void field_t::assert_is_zero(std::string co context->create_poly_gate({ .a = witness_index, - .b = context->zero_idx, - .c = context->zero_idx, + .b = context->zero_idx(), + .c = context->zero_idx(), .q_m = bb::fr::zero(), .q_l = multiplicative_constant, .q_r = bb::fr::zero(), @@ -738,7 +738,7 @@ template void field_t::assert_is_not_zero(std::strin context->create_poly_gate({ .a = witness_index, // input value .b = inverse.witness_index, // inverse - .c = context->zero_idx, // no output + .c = context->zero_idx(), // no output .q_m = multiplicative_constant, // a * b * mul_const .q_l = bb::fr::zero(), // a * 0 .q_r = additive_constant, // b * mul_const @@ -948,7 +948,7 @@ template void field_t::assert_equal(const field_t& r // single `add` gate constraining a - b = 0 ctx->create_add_gate({ .a = lhs.witness_index, .b = rhs.witness_index, - .c = ctx->zero_idx, + .c = ctx->zero_idx(), .a_scaling = lhs.multiplicative_constant, .b_scaling = -rhs.multiplicative_constant, .c_scaling = 0, @@ -1094,10 +1094,10 @@ void field_t::evaluate_linear_identity( bb::fr const_scaling = a.additive_constant + b.additive_constant + c.additive_constant + d.additive_constant; ctx->create_big_add_gate({ - .a = a.is_constant() ? ctx->zero_idx : a.witness_index, - .b = b.is_constant() ? ctx->zero_idx : b.witness_index, - .c = c.is_constant() ? ctx->zero_idx : c.witness_index, - .d = d.is_constant() ? ctx->zero_idx : d.witness_index, + .a = a.is_constant() ? ctx->zero_idx() : a.witness_index, + .b = b.is_constant() ? ctx->zero_idx() : b.witness_index, + .c = c.is_constant() ? ctx->zero_idx() : c.witness_index, + .d = d.is_constant() ? ctx->zero_idx() : d.witness_index, .a_scaling = a.multiplicative_constant, .b_scaling = b.multiplicative_constant, .c_scaling = c.multiplicative_constant, @@ -1135,10 +1135,10 @@ void field_t::evaluate_polynomial_identity( bb::fr const_scaling = a.additive_constant * b.additive_constant + c.additive_constant + d.additive_constant; ctx->create_big_mul_gate({ - .a = a.is_constant() ? ctx->zero_idx : a.witness_index, - .b = b.is_constant() ? ctx->zero_idx : b.witness_index, - .c = c.is_constant() ? ctx->zero_idx : c.witness_index, - .d = d.is_constant() ? ctx->zero_idx : d.witness_index, + .a = a.is_constant() ? ctx->zero_idx() : a.witness_index, + .b = b.is_constant() ? ctx->zero_idx() : b.witness_index, + .c = c.is_constant() ? ctx->zero_idx() : c.witness_index, + .d = d.is_constant() ? ctx->zero_idx() : d.witness_index, .mul_scaling = mul_scaling, .a_scaling = a_scaling, .b_scaling = b_scaling, @@ -1195,7 +1195,7 @@ template field_t field_t::accumulate(const // Pad the accumulator with zeroes so that its size is a multiple of 3. const size_t num_padding_wires = (num_elements % 3) == 0 ? 0 : 3 - (num_elements % 3); for (size_t i = 0; i < num_padding_wires; ++i) { - accumulator.emplace_back(field_t::from_witness_index(ctx, ctx->zero_idx)); + accumulator.emplace_back(field_t::from_witness_index(ctx, ctx->zero_idx())); } num_elements = accumulator.size(); const size_t num_gates = (num_elements / 3); 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 f9be2d2fe3b4..31bb84e6926a 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.test.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.test.cpp @@ -314,7 +314,7 @@ template class stdlib_field : public testing::Test { builder.create_add_gate({ .a = x.witness_index, .b = y.witness_index, - .c = builder.zero_idx, + .c = builder.zero_idx(), .a_scaling = 0, .b_scaling = 1, .c_scaling = 0, @@ -325,7 +325,7 @@ template class stdlib_field : public testing::Test { builder.create_add_gate({ .a = x.witness_index, .b = y.witness_index, - .c = builder.zero_idx, + .c = builder.zero_idx(), .a_scaling = 0, .b_scaling = 1, .c_scaling = 0, diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field_conversion.hpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field_conversion.hpp index b729921f8929..8f060306b3c9 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field_conversion.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field_conversion.hpp @@ -67,7 +67,7 @@ template inline T convert_challenge(const fr::from_witness_index(builder, builder->zero_idx)); + return T(challenge, fr::from_witness_index(builder, builder->zero_idx())); } } diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field_conversion.test.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field_conversion.test.cpp index ac63e969e5ba..79b16b2be929 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field_conversion.test.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field_conversion.test.cpp @@ -188,7 +188,7 @@ TYPED_TEST(stdlib_field_conversion, DeserializePointAtInfinity) { using Builder = TypeParam; Builder builder; - const fr zero(fr::from_witness_index(&builder, builder.zero_idx)); + const fr zero(fr::from_witness_index(&builder, builder.zero_idx())); { std::vector> zeros(4, zero); diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field_utils.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field_utils.cpp index 6bc99a3ecbc1..99905fcba0de 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field_utils.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field_utils.cpp @@ -69,7 +69,7 @@ std::pair, field_t> split_unique(const field_t::from_witness_index(ctx, ctx->zero_idx); + auto zero = field_t::from_witness_index(ctx, ctx->zero_idx()); field_t::evaluate_linear_identity(lo, hi * shift, -field, zero); // Set the origin tag for the limbs 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 0c42c7b14a73..5053b0a3f251 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 @@ -22,7 +22,7 @@ void ProtogalaxyRecursiveVerifier_::run_oink_verifier_on_each_ if (!key->is_complete) { OinkRecursiveVerifier_ oink_verifier{ builder, key, transcript, domain_separator + '_' }; oink_verifier.verify(); - key->target_sum = FF::from_witness_index(builder, builder->zero_idx); + key->target_sum = FF::from_witness_index(builder, builder->zero_idx()); // Get the gate challenges for sumcheck/combiner computation key->gate_challenges = transcript->template get_powers_of_challenge(domain_separator + "_gate_challenge", CONST_PG_LOG_N); diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/circuit_builder_base.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/circuit_builder_base.hpp index 89d5c2623bd9..bba26c5b4121 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/circuit_builder_base.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/circuit_builder_base.hpp @@ -22,7 +22,7 @@ static constexpr uint32_t DUMMY_TAG = 0; template class CircuitBuilderBase { public: using FF = FF_; - using EmbeddedCurve = std::conditional_t, curve::BN254, curve::Grumpkin>; + using EmbeddedCurve = curve::Grumpkin; private: // A container for all of the witness values used by the circuit @@ -32,17 +32,36 @@ template class CircuitBuilderBase { bool public_inputs_finalized_ = false; // Addition of new public inputs disallowed after this is set to true. - public: - size_t num_gates = 0; // true if we have dummy witnesses (in the write_vk case) bool has_dummy_witnesses = false; - std::unordered_map variable_names; - // index of next variable in equivalence class (=REAL_VARIABLE if you're last) std::vector next_var_index; // index of previous variable in equivalence class (=FIRST if you're in a cycle alone) std::vector prev_var_index; + + bool _failed = false; + std::string _err; + + static constexpr uint32_t REAL_VARIABLE = UINT32_MAX - 1; + static constexpr uint32_t FIRST_VARIABLE_IN_CLASS = UINT32_MAX - 2; + + // Index at which we store a witness constrained to be equal to 0 + uint32_t _zero_idx = 0; + + protected: + std::unordered_map variable_names; + + void set_zero_idx(uint32_t value) { _zero_idx = value; } + + public: + size_t num_gates = 0; + + // The permutation on variable tags. See + // https://github.com/AztecProtocol/plonk-with-lookups-private/blob/new-stuff/GenPermuations.pdf + // DOCTODO(#231): replace with the relevant wiki link. + std::map tau; + // The "real_variable_index" acts as a map from a "witness index" (e.g. the one stored by a stdlib object) to an // index into the variables array. This extra layer of indirection is used to support copy constraints by allowing, // for example, two witnesses with differing witness indices to have the same "real variable index" and thus the @@ -51,23 +70,8 @@ template class CircuitBuilderBase { std::vector real_variable_index; std::vector real_variable_tags; uint32_t current_tag = DUMMY_TAG; - // The permutation on variable tags. See - // https://github.com/AztecProtocol/plonk-with-lookups-private/blob/new-stuff/GenPermuations.pdf - // DOCTODO(#231): replace with the relevant wiki link. - std::map tau; - - // We know from the CLI arguments during proving whether a circuit should use a prover which produces - // proofs that are friendly to verify in a circuit themselves. A verifier does not need a full circuit - // description and should be able to verify a proof with just the verification key and the proof. - // This field exists to later set the same field in the verification key, and make sure - // that we are using the correct prover/verifier. - bool is_recursive_circuit = false; - - bool _failed = false; - std::string _err; - static constexpr uint32_t REAL_VARIABLE = UINT32_MAX - 1; - static constexpr uint32_t FIRST_VARIABLE_IN_CLASS = UINT32_MAX - 2; + public: CircuitBuilderBase(size_t size_hint = 0, bool has_dummy_witnesses = false); CircuitBuilderBase(const CircuitBuilderBase& other) = default; @@ -82,10 +86,9 @@ template class CircuitBuilderBase { virtual size_t get_estimated_num_finalized_gates() const; virtual void print_num_estimated_finalized_gates() const; virtual size_t get_num_variables() const; - // TODO(#216)(Adrian): Feels wrong to let the zero_idx be changed. - uint32_t zero_idx = 0; - // TODO(https://github.com/AztecProtocol/barretenberg/issues/1546): Do we need `one_idx`? - uint32_t one_idx = 1; + + // Non-owning getter for the index at which a fixed witness 0 is stored + uint32_t zero_idx() const { return _zero_idx; } virtual void create_add_gate(const add_triple_& in) = 0; virtual void create_mul_gate(const mul_triple_& in) = 0; @@ -192,15 +195,6 @@ template class CircuitBuilderBase { */ virtual void set_variable_name(uint32_t index, const std::string& name); - /** - * After assert_equal() merge two class names if present. - * Preserves the first name in class. - * - * @param index Index of the variable you have previously named and used in assert_equal. - * - */ - virtual void update_variable_names(uint32_t index); - /** * Export the existing circuit as msgpack compatible buffer. * @@ -244,7 +238,6 @@ template class CircuitBuilderBase { bool failed() const; const std::string& err() const; - void set_err(std::string msg); void failure(std::string msg); }; @@ -557,7 +550,7 @@ template struct CircuitSchemaInternal { * * * - * The (subgroup.size() * program_width) elements of sigma_mappings are of the form: + * The (subgroup.size() * num_wires) elements of sigma_mappings are of the form: * { * row_index: j, // iterates over all rows in the subgroup * column_index: i, // l,r,o,4 diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/circuit_builder_base_impl.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/circuit_builder_base_impl.hpp index 98a78ded2e19..23f8765786ba 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/circuit_builder_base_impl.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/circuit_builder_base_impl.hpp @@ -91,6 +91,7 @@ template uint32_t CircuitBuilderBase::add_variable(const FF& return index; } +// AUDITTODO: is this used? template void CircuitBuilderBase::set_variable_name(uint32_t index, const std::string& name) { ASSERT_DEBUG(variables.size() > index); @@ -103,31 +104,6 @@ template void CircuitBuilderBase::set_variable_name(uint32_t variable_names.insert({ first_idx, name }); } -template void CircuitBuilderBase::update_variable_names(uint32_t index) -{ - uint32_t first_idx = get_first_variable_in_class(index); - - uint32_t cur_idx = next_var_index[first_idx]; - while (cur_idx != REAL_VARIABLE && !variable_names.contains(cur_idx)) { - cur_idx = next_var_index[cur_idx]; - } - - if (variable_names.contains(first_idx)) { - if (cur_idx != REAL_VARIABLE) { - variable_names.extract(cur_idx); - } - return; - } - - if (cur_idx != REAL_VARIABLE) { - std::string var_name = variable_names.find(cur_idx)->second; - variable_names.erase(cur_idx); - variable_names.insert({ first_idx, var_name }); - return; - } - failure("No previously assigned names found"); -} - template size_t CircuitBuilderBase::get_circuit_subgroup_size(const size_t num_gates) const { auto log2_n = static_cast(numeric::get_msb(num_gates)); @@ -227,11 +203,6 @@ template const std::string& CircuitBuilderBase::err() const return _err; } -template void CircuitBuilderBase::set_err(std::string msg) -{ - _err = std::move(msg); -} - template void CircuitBuilderBase::failure(std::string msg) { #ifndef FUZZING_DISABLE_WARNINGS @@ -241,6 +212,6 @@ template void CircuitBuilderBase::failure(std::string msg) } #endif _failed = true; - set_err(std::move(msg)); + _err = msg; } } // namespace bb diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_circuit_builder.cpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_circuit_builder.cpp index c0665a23f226..6784c05f267e 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_circuit_builder.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_circuit_builder.cpp @@ -178,8 +178,8 @@ ecc_op_tuple MegaCircuitBuilder_::populate_ecc_op_wires(const UltraOp& ultra op_tuple.z_2 = this->add_variable(ultra_op.z_2); // Set the indices for the op values for each of the two rows - uint32_t op_val_idx_1 = op_tuple.op; // genuine op code value - uint32_t op_val_idx_2 = this->zero_idx; // second row value always set to 0 + uint32_t op_val_idx_1 = op_tuple.op; // genuine op code value + uint32_t op_val_idx_2 = this->zero_idx(); // second row value always set to 0 // If this is a random operation, the op values are randomized if (ultra_op.op_code.is_random_op) { op_val_idx_1 = this->add_variable(ultra_op.op_code.random_value_1); @@ -224,7 +224,7 @@ template void MegaCircuitBuilder_::queue_ecc_random_op() template void MegaCircuitBuilder_::set_goblin_ecc_op_code_constant_variables() { - null_op_idx = this->zero_idx; // constant 0 is is associated with the zero index + null_op_idx = this->zero_idx(); // constant 0 is is associated with the zero index add_accum_op_idx = this->put_constant_variable(FF(EccOpCode{ .add = true }.value())); mul_accum_op_idx = this->put_constant_variable(FF(EccOpCode{ .mul = true }.value())); equality_op_idx = this->put_constant_variable(FF(EccOpCode{ .eq = true, .reset = true }.value())); @@ -268,7 +268,7 @@ template void MegaCircuitBuilder_::create_databus_read_gate(const databus_lookup_gate_& in, const BusId bus_idx) { auto& block = this->blocks.busread; - block.populate_wires(in.value, in.index, this->zero_idx, this->zero_idx); + block.populate_wires(in.value, in.index, this->zero_idx(), this->zero_idx()); apply_databus_selectors(bus_idx); this->check_selector_length_consistency(); @@ -298,18 +298,10 @@ template void MegaCircuitBuilder_::apply_databus_selectors(con break; } } - block.q_busread().emplace_back(1); + block.q_4().emplace_back(0); block.q_m().emplace_back(0); block.q_c().emplace_back(0); - block.q_delta_range().emplace_back(0); - block.q_arith().emplace_back(0); - block.q_4().emplace_back(0); - block.q_lookup_type().emplace_back(0); - block.q_elliptic().emplace_back(0); - block.q_memory().emplace_back(0); - block.q_nnf().emplace_back(0); - block.q_poseidon2_external().emplace_back(0); - block.q_poseidon2_internal().emplace_back(0); + block.set_gate_selector(1); } template class MegaCircuitBuilder_; diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp index 477fa3188fb9..9250710e31ee 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp @@ -21,7 +21,6 @@ template class MegaCircuitBuilder_ : public UltraCircuitBuilder_::DEFAULT_NON_NATIVE_FIELD_LIMB_BITS; @@ -143,22 +142,6 @@ template class MegaCircuitBuilder_ : public UltraCircuitBuilder_ builder; // instantiate new builder - - size_t num_gates_prior = builder.get_estimated_num_finalized_gates(); - builder.add_ultra_and_mega_gates_to_ensure_all_polys_are_non_zero(); - size_t num_gates_post = builder.get_estimated_num_finalized_gates(); // accounts for finalization gates - - return num_gates_post - num_gates_prior; - } - /**x * @brief Print the number and composition of gates in the circuit * diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mock_circuits.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mock_circuits.hpp index 13d92a5cd8e3..e5b6c0c36e59 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mock_circuits.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mock_circuits.hpp @@ -120,7 +120,7 @@ class MockCircuits { val_idx_1, val_idx_2, val_idx_3, - builder.zero_idx, + builder.zero_idx(), 1, 1, 1, diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/rom_ram_logic.cpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/rom_ram_logic.cpp index f03cea32b9bf..1eba7490c1cb 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/rom_ram_logic.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/rom_ram_logic.cpp @@ -30,20 +30,20 @@ void RomRamLogic_::set_ROM_element(CircuitBuilder* builder, BB_ASSERT_GT(rom_arrays.size(), rom_id); RomTranscript& rom_array = rom_arrays[rom_id]; const uint32_t index_witness = - (index_value == 0) ? builder->zero_idx : builder->put_constant_variable((uint64_t)index_value); + (index_value == 0) ? builder->zero_idx() : builder->put_constant_variable((uint64_t)index_value); BB_ASSERT_GT(rom_array.state.size(), index_value); BB_ASSERT_EQ(rom_array.state[index_value][0], UNINITIALIZED_MEMORY_RECORD); RomRecord new_record{ .index_witness = index_witness, .value_column1_witness = value_witness, - .value_column2_witness = builder->zero_idx, + .value_column2_witness = builder->zero_idx(), .index = static_cast(index_value), .record_witness = 0, .gate_index = 0, }; rom_array.state[index_value][0] = value_witness; - rom_array.state[index_value][1] = builder->zero_idx; + rom_array.state[index_value][1] = builder->zero_idx(); create_ROM_gate(builder, new_record); rom_array.records.emplace_back(new_record); } @@ -88,7 +88,7 @@ uint32_t RomRamLogic_::read_ROM_array(CircuitBuilder* builder, RomRecord new_record{ .index_witness = index_witness, .value_column1_witness = value_witness, - .value_column2_witness = builder->zero_idx, + .value_column2_witness = builder->zero_idx(), .index = index, .record_witness = 0, .gate_index = 0, @@ -173,7 +173,8 @@ void RomRamLogic_::process_ROM_array(CircuitBuilder* builder, co // Make sure that every cell has been initialized for (size_t i = 0; i < rom_array.state.size(); ++i) { if (rom_array.state[i][0] == UNINITIALIZED_MEMORY_RECORD) { - set_ROM_element_pair(builder, rom_id, static_cast(i), { builder->zero_idx, builder->zero_idx }); + set_ROM_element_pair( + builder, rom_id, static_cast(i), { builder->zero_idx(), builder->zero_idx() }); } } @@ -229,14 +230,14 @@ void RomRamLogic_::process_ROM_array(CircuitBuilder* builder, co // TODO(https://github.com/AztecProtocol/barretenberg/issues/879): This was formerly a single arithmetic gate. A // dummy gate has been added to allow the previous gate to access the required wire data via shifts, allowing the // arithmetic gate to occur out of sequence. - builder->create_dummy_gate( - builder->blocks.memory, max_index, builder->zero_idx, builder->zero_idx, builder->zero_idx); + builder->create_unconstrained_gate( + builder->blocks.memory, max_index, builder->zero_idx(), builder->zero_idx(), builder->zero_idx()); builder->create_big_add_gate( { max_index, - builder->zero_idx, - builder->zero_idx, - builder->zero_idx, + builder->zero_idx(), + builder->zero_idx(), + builder->zero_idx(), 1, 0, 0, @@ -274,7 +275,7 @@ void RomRamLogic_::init_RAM_element(CircuitBuilder* builder, BB_ASSERT_GT(ram_arrays.size(), ram_id); RamTranscript& ram_array = ram_arrays[ram_id]; const uint32_t index_witness = - (index_value == 0) ? builder->zero_idx : builder->put_constant_variable((uint64_t)index_value); + (index_value == 0) ? builder->zero_idx() : builder->put_constant_variable((uint64_t)index_value); BB_ASSERT_GT(ram_array.state.size(), index_value); BB_ASSERT_EQ(ram_array.state[index_value], UNINITIALIZED_MEMORY_RECORD); RamRecord new_record{ .index_witness = index_witness, @@ -401,18 +402,18 @@ void RomRamLogic_::create_final_sorted_RAM_gate(CircuitBuilder* // Create a final gate with all selectors zero; wire values are accessed by the previous RAM gate via // shifted wires - builder->create_dummy_gate(builder->blocks.memory, - record.index_witness, - record.timestamp_witness, - record.value_witness, - record.record_witness); + builder->create_unconstrained_gate(builder->blocks.memory, + record.index_witness, + record.timestamp_witness, + record.value_witness, + record.record_witness); // Create an add gate ensuring the final index is consistent with the size of the RAM array builder->create_big_add_gate({ record.index_witness, - builder->zero_idx, - builder->zero_idx, - builder->zero_idx, + builder->zero_idx(), + builder->zero_idx(), + builder->zero_idx(), 1, 0, 0, @@ -436,7 +437,7 @@ void RomRamLogic_::process_RAM_array(CircuitBuilder* builder, co // different public iputs will produce different circuit constraints. for (size_t i = 0; i < ram_array.state.size(); ++i) { if (ram_array.state[i] == UNINITIALIZED_MEMORY_RECORD) { - init_RAM_element(builder, ram_id, static_cast(i), builder->zero_idx); + init_RAM_element(builder, ram_id, static_cast(i), builder->zero_idx()); } } @@ -531,7 +532,7 @@ void RomRamLogic_::process_RAM_array(CircuitBuilder* builder, co builder->apply_memory_selectors(CircuitBuilder::MEMORY_SELECTORS::RAM_TIMESTAMP_CHECK); builder->blocks.memory.populate_wires( - current.index_witness, current.timestamp_witness, timestamp_delta_witness, builder->zero_idx); + current.index_witness, current.timestamp_witness, timestamp_delta_witness, builder->zero_idx()); ++builder->num_gates; @@ -543,8 +544,8 @@ void RomRamLogic_::process_RAM_array(CircuitBuilder* builder, co // add the index/timestamp values of the last sorted record in an empty add gate. // (the previous gate will access the wires on this gate and requires them to be those of the last record) const auto& last = sorted_ram_records[ram_array.records.size() - 1]; - builder->create_dummy_gate( - builder->blocks.memory, last.index_witness, last.timestamp_witness, builder->zero_idx, builder->zero_idx); + builder->create_unconstrained_gate( + builder->blocks.memory, last.index_witness, last.timestamp_witness, builder->zero_idx(), builder->zero_idx()); // Step 3: validate difference in timestamps is monotonically increasing. i.e. is <= maximum timestamp const size_t max_timestamp = ram_array.access_count - 1; diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/ultra_circuit_builder.cpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/ultra_circuit_builder.cpp index db185ea232f1..e1bca3403ca6 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/ultra_circuit_builder.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/ultra_circuit_builder.cpp @@ -5,8 +5,6 @@ // ===================== /** - * @file ultra_circuit_builder.cpp - * @author Luke (ledwards2225) and Kesha (Rumata888) * @brief This file contains the implementation of field-agnostic UltraCircuitBuilder class that defines the logic * of ultra-style circuits and is intended for the use in UltraHonk * @@ -83,123 +81,74 @@ template void UltraCircuitBuilder_::add_gates_to_ensure_all_polys_are_non_zero() { // q_m, q_1, q_2, q_3, q_4 - blocks.arithmetic.populate_wires(this->zero_idx, this->zero_idx, this->zero_idx, this->zero_idx); + blocks.arithmetic.populate_wires(this->zero_idx(), this->zero_idx(), this->zero_idx(), this->zero_idx()); blocks.arithmetic.q_m().emplace_back(1); blocks.arithmetic.q_1().emplace_back(1); blocks.arithmetic.q_2().emplace_back(1); blocks.arithmetic.q_3().emplace_back(1); blocks.arithmetic.q_4().emplace_back(1); blocks.arithmetic.q_c().emplace_back(0); - blocks.arithmetic.q_delta_range().emplace_back(0); - blocks.arithmetic.q_arith().emplace_back(0); - blocks.arithmetic.q_lookup_type().emplace_back(0); - blocks.arithmetic.q_elliptic().emplace_back(0); - blocks.arithmetic.q_memory().emplace_back(0); - blocks.arithmetic.q_nnf().emplace_back(0); - blocks.arithmetic.q_poseidon2_external().emplace_back(0); - blocks.arithmetic.q_poseidon2_internal().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - blocks.arithmetic.pad_additional(); - } + blocks.arithmetic.set_gate_selector(0); check_selector_length_consistency(); ++this->num_gates; // q_delta_range - blocks.delta_range.populate_wires(this->zero_idx, this->zero_idx, this->zero_idx, this->zero_idx); + blocks.delta_range.populate_wires(this->zero_idx(), this->zero_idx(), this->zero_idx(), this->zero_idx()); blocks.delta_range.q_m().emplace_back(0); blocks.delta_range.q_1().emplace_back(0); blocks.delta_range.q_2().emplace_back(0); blocks.delta_range.q_3().emplace_back(0); blocks.delta_range.q_4().emplace_back(0); blocks.delta_range.q_c().emplace_back(0); - blocks.delta_range.q_delta_range().emplace_back(1); - blocks.delta_range.q_arith().emplace_back(0); - blocks.delta_range.q_lookup_type().emplace_back(0); - blocks.delta_range.q_elliptic().emplace_back(0); - blocks.delta_range.q_memory().emplace_back(0); - blocks.delta_range.q_nnf().emplace_back(0); - blocks.delta_range.q_poseidon2_external().emplace_back(0); - blocks.delta_range.q_poseidon2_internal().emplace_back(0); - - if constexpr (HasAdditionalSelectors) { - blocks.delta_range.pad_additional(); - } + blocks.delta_range.set_gate_selector(1); + check_selector_length_consistency(); ++this->num_gates; - create_dummy_gate(blocks.delta_range, this->zero_idx, this->zero_idx, this->zero_idx, this->zero_idx); + create_unconstrained_gate( + blocks.delta_range, this->zero_idx(), this->zero_idx(), this->zero_idx(), this->zero_idx()); // q_elliptic - blocks.elliptic.populate_wires(this->zero_idx, this->zero_idx, this->zero_idx, this->zero_idx); + blocks.elliptic.populate_wires(this->zero_idx(), this->zero_idx(), this->zero_idx(), this->zero_idx()); blocks.elliptic.q_m().emplace_back(0); blocks.elliptic.q_1().emplace_back(0); blocks.elliptic.q_2().emplace_back(0); blocks.elliptic.q_3().emplace_back(0); blocks.elliptic.q_4().emplace_back(0); blocks.elliptic.q_c().emplace_back(0); - blocks.elliptic.q_delta_range().emplace_back(0); - blocks.elliptic.q_arith().emplace_back(0); - blocks.elliptic.q_lookup_type().emplace_back(0); - blocks.elliptic.q_elliptic().emplace_back(1); - blocks.elliptic.q_memory().emplace_back(0); - blocks.elliptic.q_nnf().emplace_back(0); - blocks.elliptic.q_poseidon2_external().emplace_back(0); - blocks.elliptic.q_poseidon2_internal().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - blocks.elliptic.pad_additional(); - } + blocks.elliptic.set_gate_selector(1); check_selector_length_consistency(); ++this->num_gates; - create_dummy_gate(blocks.elliptic, this->zero_idx, this->zero_idx, this->zero_idx, this->zero_idx); + create_unconstrained_gate(blocks.elliptic, this->zero_idx(), this->zero_idx(), this->zero_idx(), this->zero_idx()); // q_memory - blocks.memory.populate_wires(this->zero_idx, this->zero_idx, this->zero_idx, this->zero_idx); + blocks.memory.populate_wires(this->zero_idx(), this->zero_idx(), this->zero_idx(), this->zero_idx()); blocks.memory.q_m().emplace_back(0); blocks.memory.q_1().emplace_back(0); blocks.memory.q_2().emplace_back(0); blocks.memory.q_3().emplace_back(0); blocks.memory.q_4().emplace_back(0); blocks.memory.q_c().emplace_back(0); - blocks.memory.q_delta_range().emplace_back(0); - blocks.memory.q_arith().emplace_back(0); - blocks.memory.q_lookup_type().emplace_back(0); - blocks.memory.q_elliptic().emplace_back(0); - blocks.memory.q_memory().emplace_back(1); - blocks.memory.q_nnf().emplace_back(0); - blocks.memory.q_poseidon2_external().emplace_back(0); - blocks.memory.q_poseidon2_internal().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - blocks.memory.pad_additional(); - } + blocks.memory.set_gate_selector(1); check_selector_length_consistency(); ++this->num_gates; - create_dummy_gate(blocks.memory, this->zero_idx, this->zero_idx, this->zero_idx, this->zero_idx); + create_unconstrained_gate(blocks.memory, this->zero_idx(), this->zero_idx(), this->zero_idx(), this->zero_idx()); // q_nnf - blocks.nnf.populate_wires(this->zero_idx, this->zero_idx, this->zero_idx, this->zero_idx); + blocks.nnf.populate_wires(this->zero_idx(), this->zero_idx(), this->zero_idx(), this->zero_idx()); blocks.nnf.q_m().emplace_back(0); blocks.nnf.q_1().emplace_back(0); blocks.nnf.q_2().emplace_back(0); blocks.nnf.q_3().emplace_back(0); blocks.nnf.q_4().emplace_back(0); blocks.nnf.q_c().emplace_back(0); - blocks.nnf.q_delta_range().emplace_back(0); - blocks.nnf.q_arith().emplace_back(0); - blocks.nnf.q_lookup_type().emplace_back(0); - blocks.nnf.q_elliptic().emplace_back(0); - blocks.nnf.q_memory().emplace_back(0); - blocks.nnf.q_nnf().emplace_back(1); - blocks.nnf.q_poseidon2_external().emplace_back(0); - blocks.nnf.q_poseidon2_internal().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - blocks.nnf.pad_additional(); - } + blocks.nnf.set_gate_selector(1); check_selector_length_consistency(); ++this->num_gates; - create_dummy_gate(blocks.nnf, this->zero_idx, this->zero_idx, this->zero_idx, this->zero_idx); + create_unconstrained_gate(blocks.nnf, this->zero_idx(), this->zero_idx(), this->zero_idx(), this->zero_idx()); // Add nonzero values in w_4 and q_c (q_4*w_4 + q_c --> 1*1 - 1 = 0) - this->one_idx = put_constant_variable(FF::one()); - create_big_add_gate({ this->zero_idx, this->zero_idx, this->zero_idx, this->one_idx, 0, 0, 0, 1, -1 }); + uint32_t one_idx = put_constant_variable(FF::one()); + create_big_add_gate({ this->zero_idx(), this->zero_idx(), this->zero_idx(), one_idx, 0, 0, 0, 1, -1 }); // Take care of all polys related to lookups (q_lookup, tables, sorted, etc) // by doing a dummy lookup with a special table. @@ -232,54 +181,36 @@ void UltraCircuitBuilder_::add_gates_to_ensure_all_polys_are_non } // mock a poseidon external gate, with all zeros as input - blocks.poseidon2_external.populate_wires(this->zero_idx, this->zero_idx, this->zero_idx, this->zero_idx); + blocks.poseidon2_external.populate_wires(this->zero_idx(), this->zero_idx(), this->zero_idx(), this->zero_idx()); blocks.poseidon2_external.q_m().emplace_back(0); blocks.poseidon2_external.q_1().emplace_back(0); blocks.poseidon2_external.q_2().emplace_back(0); blocks.poseidon2_external.q_3().emplace_back(0); blocks.poseidon2_external.q_c().emplace_back(0); - blocks.poseidon2_external.q_arith().emplace_back(0); blocks.poseidon2_external.q_4().emplace_back(0); - blocks.poseidon2_external.q_delta_range().emplace_back(0); - blocks.poseidon2_external.q_lookup_type().emplace_back(0); - blocks.poseidon2_external.q_elliptic().emplace_back(0); - blocks.poseidon2_external.q_memory().emplace_back(0); - blocks.poseidon2_external.q_nnf().emplace_back(0); - blocks.poseidon2_external.q_poseidon2_external().emplace_back(1); - blocks.poseidon2_external.q_poseidon2_internal().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - blocks.poseidon2_external.pad_additional(); - } + blocks.poseidon2_external.set_gate_selector(1); check_selector_length_consistency(); ++this->num_gates; - // dummy gate to be read into by previous poseidon external gate via shifts - this->create_dummy_gate(blocks.poseidon2_external, this->zero_idx, this->zero_idx, this->zero_idx, this->zero_idx); + // unconstrained gate to be read into by previous poseidon external gate via shifts + create_unconstrained_gate( + blocks.poseidon2_external, this->zero_idx(), this->zero_idx(), this->zero_idx(), this->zero_idx()); // mock a poseidon internal gate, with all zeros as input - blocks.poseidon2_internal.populate_wires(this->zero_idx, this->zero_idx, this->zero_idx, this->zero_idx); + blocks.poseidon2_internal.populate_wires(this->zero_idx(), this->zero_idx(), this->zero_idx(), this->zero_idx()); blocks.poseidon2_internal.q_m().emplace_back(0); blocks.poseidon2_internal.q_1().emplace_back(0); blocks.poseidon2_internal.q_2().emplace_back(0); blocks.poseidon2_internal.q_3().emplace_back(0); blocks.poseidon2_internal.q_c().emplace_back(0); - blocks.poseidon2_internal.q_arith().emplace_back(0); blocks.poseidon2_internal.q_4().emplace_back(0); - blocks.poseidon2_internal.q_delta_range().emplace_back(0); - blocks.poseidon2_internal.q_lookup_type().emplace_back(0); - blocks.poseidon2_internal.q_elliptic().emplace_back(0); - blocks.poseidon2_internal.q_memory().emplace_back(0); - blocks.poseidon2_internal.q_nnf().emplace_back(0); - blocks.poseidon2_internal.q_poseidon2_external().emplace_back(0); - blocks.poseidon2_internal.q_poseidon2_internal().emplace_back(1); - if constexpr (HasAdditionalSelectors) { - blocks.poseidon2_internal.pad_additional(); - } + blocks.poseidon2_internal.set_gate_selector(1); check_selector_length_consistency(); ++this->num_gates; // dummy gate to be read into by previous poseidon internal gate via shifts - create_dummy_gate(blocks.poseidon2_internal, this->zero_idx, this->zero_idx, this->zero_idx, this->zero_idx); + create_unconstrained_gate( + blocks.poseidon2_internal, this->zero_idx(), this->zero_idx(), this->zero_idx(), this->zero_idx()); } /** @@ -294,24 +225,14 @@ template void UltraCircuitBuilder_::cr { this->assert_valid_variables({ in.a, in.b, in.c }); - blocks.arithmetic.populate_wires(in.a, in.b, in.c, this->zero_idx); + blocks.arithmetic.populate_wires(in.a, in.b, in.c, this->zero_idx()); blocks.arithmetic.q_m().emplace_back(0); blocks.arithmetic.q_1().emplace_back(in.a_scaling); blocks.arithmetic.q_2().emplace_back(in.b_scaling); blocks.arithmetic.q_3().emplace_back(in.c_scaling); blocks.arithmetic.q_c().emplace_back(in.const_scaling); - blocks.arithmetic.q_arith().emplace_back(1); blocks.arithmetic.q_4().emplace_back(0); - blocks.arithmetic.q_delta_range().emplace_back(0); - blocks.arithmetic.q_lookup_type().emplace_back(0); - blocks.arithmetic.q_elliptic().emplace_back(0); - blocks.arithmetic.q_memory().emplace_back(0); - blocks.arithmetic.q_nnf().emplace_back(0); - blocks.arithmetic.q_poseidon2_external().emplace_back(0); - blocks.arithmetic.q_poseidon2_internal().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - blocks.arithmetic.pad_additional(); - } + blocks.arithmetic.set_gate_selector(1); check_selector_length_consistency(); ++this->num_gates; } @@ -335,18 +256,8 @@ void UltraCircuitBuilder_::create_big_mul_add_gate(const mul_qua blocks.arithmetic.q_2().emplace_back(in.b_scaling); blocks.arithmetic.q_3().emplace_back(in.c_scaling); blocks.arithmetic.q_c().emplace_back(in.const_scaling); - blocks.arithmetic.q_arith().emplace_back(include_next_gate_w_4 ? 2 : 1); blocks.arithmetic.q_4().emplace_back(in.d_scaling); - blocks.arithmetic.q_delta_range().emplace_back(0); - blocks.arithmetic.q_lookup_type().emplace_back(0); - blocks.arithmetic.q_elliptic().emplace_back(0); - blocks.arithmetic.q_memory().emplace_back(0); - blocks.arithmetic.q_nnf().emplace_back(0); - blocks.arithmetic.q_poseidon2_external().emplace_back(0); - blocks.arithmetic.q_poseidon2_internal().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - blocks.arithmetic.pad_additional(); - } + blocks.arithmetic.set_gate_selector(include_next_gate_w_4 ? 2 : 1); check_selector_length_consistency(); ++this->num_gates; } @@ -370,87 +281,12 @@ void UltraCircuitBuilder_::create_big_add_gate(const add_quad_) { - blocks.arithmetic.pad_additional(); - } + blocks.arithmetic.set_gate_selector(include_next_gate_w_4 ? 2 : 1); check_selector_length_consistency(); ++this->num_gates; } -/** - * @brief A legacy method that was used to extract a bit from c-4d by using gate selectors in the - * Turboplonk, but is simulated here for ultraplonk - * - * @param in Structure with variables and witness selector values - */ -template -void UltraCircuitBuilder_::create_big_add_gate_with_bit_extraction(const add_quad_& in) -{ - // This method is an artifact of a turbo plonk feature that implicitly extracts - // a high or low bit from a base-4 quad and adds it into the arithmetic gate relationship. - // This has been removed in the plookup composer due to it's infrequent use not being worth the extra - // cost incurred by the prover for the extra field muls required. - - // We have wires a, b, c, d, where - // a + b + c + d + 6 * (extracted bit) = 0 - // (extracted bit) is the high bit pulled from c - 4d - - this->assert_valid_variables({ in.a, in.b, in.c, in.d }); - - const uint256_t quad = this->get_variable(in.c) - this->get_variable(in.d) * 4; - const auto lo_bit = quad & uint256_t(1); - const auto hi_bit = (quad & uint256_t(2)) >> 1; - const auto lo_idx = this->add_variable(lo_bit); - const auto hi_idx = this->add_variable(hi_bit); - // lo + hi * 2 - c + 4 * d = 0 - create_big_add_gate({ - lo_idx, - hi_idx, - in.c, - in.d, - 1, - 2, - -1, - 4, - 0, - }); - - // create temporary variable t = in.a * in.a_scaling + 6 * hi_bit - const auto t = this->get_variable(in.a) * in.a_scaling + FF(hi_bit) * 6; - const auto t_idx = this->add_variable(t); - create_big_add_gate({ - in.a, - hi_idx, - t_idx, - this->zero_idx, - in.a_scaling, - 6, - -1, - 0, - 0, - }); - // (t = a + 6 * hi_bit) + b + c + d = 0 - create_big_add_gate({ - t_idx, - in.b, - in.c, - in.d, - 1, - in.b_scaling, - in.c_scaling, - in.d_scaling, - in.const_scaling, - }); -} /** * @brief Create a basic multiplication gate q_m * a * b + q_1 * a + q_2 * b + q_3 * c + q_4 * d + q_c = 0 (q_arith = 1) * @@ -467,55 +303,12 @@ void UltraCircuitBuilder_::create_big_mul_gate(const mul_quad_) { - blocks.arithmetic.pad_additional(); - } + blocks.arithmetic.set_gate_selector(1); check_selector_length_consistency(); ++this->num_gates; } -// Creates a width-4 addition gate, where the fourth witness must be a boolean. -// Can be used to normalize a 32-bit addition -template -void UltraCircuitBuilder_::create_balanced_add_gate(const add_quad_& in) -{ - this->assert_valid_variables({ in.a, in.b, in.c, in.d }); - - blocks.arithmetic.populate_wires(in.a, in.b, in.c, in.d); - blocks.arithmetic.q_m().emplace_back(0); - blocks.arithmetic.q_1().emplace_back(in.a_scaling); - blocks.arithmetic.q_2().emplace_back(in.b_scaling); - blocks.arithmetic.q_3().emplace_back(in.c_scaling); - blocks.arithmetic.q_c().emplace_back(in.const_scaling); - blocks.arithmetic.q_arith().emplace_back(1); - blocks.arithmetic.q_4().emplace_back(in.d_scaling); - blocks.arithmetic.q_delta_range().emplace_back(0); - blocks.arithmetic.q_lookup_type().emplace_back(0); - blocks.arithmetic.q_elliptic().emplace_back(0); - blocks.arithmetic.q_memory().emplace_back(0); - blocks.arithmetic.q_nnf().emplace_back(0); - blocks.arithmetic.q_poseidon2_external().emplace_back(0); - blocks.arithmetic.q_poseidon2_internal().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - blocks.arithmetic.pad_additional(); - } - check_selector_length_consistency(); - ++this->num_gates; - - // Range constrain the 4-th wire to {0, 1}. Since the inputs being added never exceed (2^x - 1) - // during uintx arithmetic, we can safely use a 1-bit range check here. In other words, we do not - // allow lazy uintx addition. - create_new_range_constraint(in.d, 1); -} /** * @brief Create a multiplication gate with q_m * a * b + q_3 * c + q_const = 0 * @@ -527,24 +320,14 @@ template void UltraCircuitBuilder_::cr { this->assert_valid_variables({ in.a, in.b, in.c }); - blocks.arithmetic.populate_wires(in.a, in.b, in.c, this->zero_idx); + blocks.arithmetic.populate_wires(in.a, in.b, in.c, this->zero_idx()); blocks.arithmetic.q_m().emplace_back(in.mul_scaling); blocks.arithmetic.q_1().emplace_back(0); blocks.arithmetic.q_2().emplace_back(0); blocks.arithmetic.q_3().emplace_back(in.c_scaling); blocks.arithmetic.q_c().emplace_back(in.const_scaling); - blocks.arithmetic.q_arith().emplace_back(1); blocks.arithmetic.q_4().emplace_back(0); - blocks.arithmetic.q_delta_range().emplace_back(0); - blocks.arithmetic.q_lookup_type().emplace_back(0); - blocks.arithmetic.q_elliptic().emplace_back(0); - blocks.arithmetic.q_memory().emplace_back(0); - blocks.arithmetic.q_nnf().emplace_back(0); - blocks.arithmetic.q_poseidon2_external().emplace_back(0); - blocks.arithmetic.q_poseidon2_internal().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - blocks.arithmetic.pad_additional(); - } + blocks.arithmetic.set_gate_selector(1); check_selector_length_consistency(); ++this->num_gates; } @@ -558,25 +341,14 @@ void UltraCircuitBuilder_::create_bool_gate(const uint32_t varia { this->assert_valid_variables({ variable_index }); - blocks.arithmetic.populate_wires(variable_index, variable_index, this->zero_idx, this->zero_idx); + blocks.arithmetic.populate_wires(variable_index, variable_index, this->zero_idx(), this->zero_idx()); blocks.arithmetic.q_m().emplace_back(1); blocks.arithmetic.q_1().emplace_back(-1); blocks.arithmetic.q_2().emplace_back(0); blocks.arithmetic.q_3().emplace_back(0); blocks.arithmetic.q_c().emplace_back(0); - blocks.arithmetic.q_delta_range().emplace_back(0); - - blocks.arithmetic.q_arith().emplace_back(1); blocks.arithmetic.q_4().emplace_back(0); - blocks.arithmetic.q_lookup_type().emplace_back(0); - blocks.arithmetic.q_elliptic().emplace_back(0); - blocks.arithmetic.q_memory().emplace_back(0); - blocks.arithmetic.q_nnf().emplace_back(0); - blocks.arithmetic.q_poseidon2_external().emplace_back(0); - blocks.arithmetic.q_poseidon2_internal().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - blocks.arithmetic.pad_additional(); - } + blocks.arithmetic.set_gate_selector(1); check_selector_length_consistency(); ++this->num_gates; } @@ -592,25 +364,14 @@ void UltraCircuitBuilder_::create_poly_gate(const poly_triple_assert_valid_variables({ in.a, in.b, in.c }); - blocks.arithmetic.populate_wires(in.a, in.b, in.c, this->zero_idx); + blocks.arithmetic.populate_wires(in.a, in.b, in.c, this->zero_idx()); blocks.arithmetic.q_m().emplace_back(in.q_m); blocks.arithmetic.q_1().emplace_back(in.q_l); blocks.arithmetic.q_2().emplace_back(in.q_r); blocks.arithmetic.q_3().emplace_back(in.q_o); blocks.arithmetic.q_c().emplace_back(in.q_c); - blocks.arithmetic.q_delta_range().emplace_back(0); - - blocks.arithmetic.q_arith().emplace_back(1); blocks.arithmetic.q_4().emplace_back(0); - blocks.arithmetic.q_lookup_type().emplace_back(0); - blocks.arithmetic.q_elliptic().emplace_back(0); - blocks.arithmetic.q_memory().emplace_back(0); - blocks.arithmetic.q_nnf().emplace_back(0); - blocks.arithmetic.q_poseidon2_external().emplace_back(0); - blocks.arithmetic.q_poseidon2_internal().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - blocks.arithmetic.pad_additional(); - } + blocks.arithmetic.set_gate_selector(1); check_selector_length_consistency(); ++this->num_gates; } @@ -651,33 +412,29 @@ void UltraCircuitBuilder_::create_ecc_add_gate(const ecc_add_gat can_fuse_into_previous_gate = can_fuse_into_previous_gate && (block.q_m()[block.size() - 1] == 0); } + // AUDITTODO: use this instead. + [[maybe_unused]] bool can_fuse = + block.size() > 0 && /* a previous gate exists in the block */ + block.w_r()[block.size() - 1] == in.x1 && /* and output x coord of previous gate is input of this one */ + block.w_o()[block.size() - 1] == in.y1; /* and output y coord of previous gate is input of this one */ + if (can_fuse_into_previous_gate) { block.q_1().set(block.size() - 1, in.sign_coefficient); block.q_elliptic().set(block.size() - 1, 1); } else { - block.populate_wires(this->zero_idx, in.x1, in.y1, this->zero_idx); + block.populate_wires(this->zero_idx(), in.x1, in.y1, this->zero_idx()); block.q_3().emplace_back(0); block.q_4().emplace_back(0); block.q_1().emplace_back(in.sign_coefficient); - block.q_arith().emplace_back(0); block.q_2().emplace_back(0); block.q_m().emplace_back(0); block.q_c().emplace_back(0); - block.q_delta_range().emplace_back(0); - block.q_lookup_type().emplace_back(0); - block.q_elliptic().emplace_back(1); - block.q_memory().emplace_back(0); - block.q_nnf().emplace_back(0); - block.q_poseidon2_external().emplace_back(0); - block.q_poseidon2_internal().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - block.pad_additional(); - } + block.set_gate_selector(1); check_selector_length_consistency(); ++this->num_gates; } - create_dummy_gate(block, in.x2, in.x3, in.y3, in.y2); + create_unconstrained_gate(block, in.x2, in.x3, in.y3, in.y2); } /** @@ -712,32 +469,28 @@ void UltraCircuitBuilder_::create_ecc_dbl_gate(const ecc_dbl_gat can_fuse_into_previous_gate = can_fuse_into_previous_gate && (block.q_nnf()[block.size() - 1] == 0); } + // AUDITTODO: use this instead. + [[maybe_unused]] bool can_fuse = + block.size() > 0 && /* a previous gate exists in the block */ + block.w_r()[block.size() - 1] == in.x1 && /* and output x coord of previous gate is input of this one */ + block.w_o()[block.size() - 1] == in.y1; /* and output y coord of previous gate is input of this one */ + if (can_fuse_into_previous_gate) { block.q_elliptic().set(block.size() - 1, 1); block.q_m().set(block.size() - 1, 1); } else { - block.populate_wires(this->zero_idx, in.x1, in.y1, this->zero_idx); - block.q_elliptic().emplace_back(1); + block.populate_wires(this->zero_idx(), in.x1, in.y1, this->zero_idx()); block.q_m().emplace_back(1); block.q_1().emplace_back(0); block.q_2().emplace_back(0); block.q_3().emplace_back(0); block.q_c().emplace_back(0); - block.q_arith().emplace_back(0); block.q_4().emplace_back(0); - block.q_delta_range().emplace_back(0); - block.q_lookup_type().emplace_back(0); - block.q_memory().emplace_back(0); - block.q_nnf().emplace_back(0); - block.q_poseidon2_external().emplace_back(0); - block.q_poseidon2_internal().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - block.pad_additional(); - } + block.set_gate_selector(1); check_selector_length_consistency(); ++this->num_gates; } - create_dummy_gate(block, this->zero_idx, in.x3, in.y3, this->zero_idx); + create_unconstrained_gate(block, this->zero_idx(), in.x3, in.y3, this->zero_idx()); } /** @@ -751,24 +504,14 @@ void UltraCircuitBuilder_::fix_witness(const uint32_t witness_in { this->assert_valid_variables({ witness_index }); - blocks.arithmetic.populate_wires(witness_index, this->zero_idx, this->zero_idx, this->zero_idx); + blocks.arithmetic.populate_wires(witness_index, this->zero_idx(), this->zero_idx(), this->zero_idx()); blocks.arithmetic.q_m().emplace_back(0); blocks.arithmetic.q_1().emplace_back(1); blocks.arithmetic.q_2().emplace_back(0); blocks.arithmetic.q_3().emplace_back(0); blocks.arithmetic.q_c().emplace_back(-witness_value); - blocks.arithmetic.q_arith().emplace_back(1); blocks.arithmetic.q_4().emplace_back(0); - blocks.arithmetic.q_delta_range().emplace_back(0); - blocks.arithmetic.q_lookup_type().emplace_back(0); - blocks.arithmetic.q_elliptic().emplace_back(0); - blocks.arithmetic.q_memory().emplace_back(0); - blocks.arithmetic.q_nnf().emplace_back(0); - blocks.arithmetic.q_poseidon2_external().emplace_back(0); - blocks.arithmetic.q_poseidon2_internal().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - blocks.arithmetic.pad_additional(); - } + blocks.arithmetic.set_gate_selector(1); check_selector_length_consistency(); ++this->num_gates; } @@ -838,24 +581,14 @@ plookup::ReadData UltraCircuitBuilder_::create_gates_f read_data[plookup::ColumnIdx::C3].push_back(third_idx); this->assert_valid_variables({ first_idx, second_idx, third_idx }); - blocks.lookup.q_lookup_type().emplace_back(FF(1)); blocks.lookup.q_3().emplace_back(FF(table.table_index)); - blocks.lookup.populate_wires(first_idx, second_idx, third_idx, this->zero_idx); + blocks.lookup.populate_wires(first_idx, second_idx, third_idx, this->zero_idx()); blocks.lookup.q_1().emplace_back(0); blocks.lookup.q_2().emplace_back((i == (num_lookups - 1) ? 0 : -multi_table.column_1_step_sizes[i + 1])); blocks.lookup.q_m().emplace_back((i == (num_lookups - 1) ? 0 : -multi_table.column_2_step_sizes[i + 1])); blocks.lookup.q_c().emplace_back((i == (num_lookups - 1) ? 0 : -multi_table.column_3_step_sizes[i + 1])); - blocks.lookup.q_arith().emplace_back(0); blocks.lookup.q_4().emplace_back(0); - blocks.lookup.q_delta_range().emplace_back(0); - blocks.lookup.q_elliptic().emplace_back(0); - blocks.lookup.q_memory().emplace_back(0); - blocks.lookup.q_nnf().emplace_back(0); - blocks.lookup.q_poseidon2_external().emplace_back(0); - blocks.lookup.q_poseidon2_internal().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - blocks.lookup.pad_additional(); - } + blocks.lookup.set_gate_selector(1); check_selector_length_consistency(); ++this->num_gates; } @@ -892,7 +625,7 @@ typename UltraCircuitBuilder_::RangeList UltraCircuitBuilder_ UltraCircuitBuilder_::decompose_into_defau real_limbs[2] ? sublimbs[3 * i + 2] : 0, }; const uint32_t new_limbs[3]{ - real_limbs[0] ? sublimb_indices[3 * i] : this->zero_idx, - real_limbs[1] ? sublimb_indices[3 * i + 1] : this->zero_idx, - real_limbs[2] ? sublimb_indices[3 * i + 2] : this->zero_idx, + real_limbs[0] ? sublimb_indices[3 * i] : this->zero_idx(), + real_limbs[1] ? sublimb_indices[3 * i + 1] : this->zero_idx(), + real_limbs[2] ? sublimb_indices[3 * i + 2] : this->zero_idx(), }; const uint64_t shifts[3]{ target_range_bitnum * (3 * i), @@ -1049,7 +782,7 @@ void UltraCircuitBuilder_::create_new_range_constraint(const uin const uint32_t copied_witness = this->add_variable(this->get_variable(variable_index)); create_add_gate({ .a = variable_index, .b = copied_witness, - .c = this->zero_idx, + .c = this->zero_idx(), .a_scaling = 1, .b_scaling = -1, .c_scaling = 0, @@ -1111,7 +844,7 @@ template void UltraCircuitBuilder_::pr padding += gate_width; } for (size_t i = 0; i < padding; ++i) { - indices.emplace_back(this->zero_idx); + indices.emplace_back(this->zero_idx()); } for (const auto sorted_value : sorted_list) { const uint32_t index = this->add_variable(sorted_value); @@ -1158,41 +891,34 @@ void UltraCircuitBuilder_::create_sort_constraint(const std::vec blocks.delta_range.q_2().emplace_back(0); blocks.delta_range.q_3().emplace_back(0); blocks.delta_range.q_c().emplace_back(0); - blocks.delta_range.q_arith().emplace_back(0); blocks.delta_range.q_4().emplace_back(0); - blocks.delta_range.q_delta_range().emplace_back(1); - blocks.delta_range.q_elliptic().emplace_back(0); - blocks.delta_range.q_lookup_type().emplace_back(0); - blocks.delta_range.q_memory().emplace_back(0); - blocks.delta_range.q_nnf().emplace_back(0); - blocks.delta_range.q_poseidon2_external().emplace_back(0); - blocks.delta_range.q_poseidon2_internal().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - blocks.delta_range.pad_additional(); - } + blocks.delta_range.set_gate_selector(1); check_selector_length_consistency(); } // dummy gate needed because of sort widget's check of next row - create_dummy_gate( - blocks.delta_range, variable_index[variable_index.size() - 1], this->zero_idx, this->zero_idx, this->zero_idx); + create_unconstrained_gate(blocks.delta_range, + variable_index[variable_index.size() - 1], + this->zero_idx(), + this->zero_idx(), + this->zero_idx()); } // useful to put variables in the witness that aren't already used - e.g. the dummy variables of the range constraint in // multiples of four template -void UltraCircuitBuilder_::create_dummy_constraints(const std::vector& variable_index) +void UltraCircuitBuilder_::create_unconstrained_gates(const std::vector& variable_index) { std::vector padded_list = variable_index; constexpr size_t gate_width = NUM_WIRES; const uint64_t padding = (gate_width - (padded_list.size() % gate_width)) % gate_width; for (uint64_t i = 0; i < padding; ++i) { - padded_list.emplace_back(this->zero_idx); + padded_list.emplace_back(this->zero_idx()); } this->assert_valid_variables(variable_index); this->assert_valid_variables(padded_list); for (size_t i = 0; i < padded_list.size(); i += gate_width) { - create_dummy_gate( + create_unconstrained_gate( blocks.arithmetic, padded_list[i], padded_list[i + 1], padded_list[i + 2], padded_list[i + 3]); } } @@ -1211,7 +937,7 @@ void UltraCircuitBuilder_::create_sort_constraint_with_edges( auto& block = blocks.delta_range; // Add an arithmetic gate to ensure the first input is equal to the start value of the range being checked - create_add_gate({ variable_index[0], this->zero_idx, this->zero_idx, 1, 0, 0, -start }); + create_add_gate({ variable_index[0], this->zero_idx(), this->zero_idx(), 1, 0, 0, -start }); // enforce range check for all but the final row for (size_t i = 0; i < variable_index.size() - gate_width; i += gate_width) { @@ -1223,18 +949,8 @@ void UltraCircuitBuilder_::create_sort_constraint_with_edges( block.q_2().emplace_back(0); block.q_3().emplace_back(0); block.q_c().emplace_back(0); - block.q_arith().emplace_back(0); block.q_4().emplace_back(0); - block.q_delta_range().emplace_back(1); - block.q_elliptic().emplace_back(0); - block.q_lookup_type().emplace_back(0); - block.q_memory().emplace_back(0); - block.q_nnf().emplace_back(0); - block.q_poseidon2_external().emplace_back(0); - block.q_poseidon2_internal().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - block.pad_additional(); - } + block.set_gate_selector(1); check_selector_length_consistency(); } // enforce range checks of last row and ending at end @@ -1249,18 +965,8 @@ void UltraCircuitBuilder_::create_sort_constraint_with_edges( block.q_2().emplace_back(0); block.q_3().emplace_back(0); block.q_c().emplace_back(0); - block.q_arith().emplace_back(0); block.q_4().emplace_back(0); - block.q_delta_range().emplace_back(1); - block.q_elliptic().emplace_back(0); - block.q_lookup_type().emplace_back(0); - block.q_memory().emplace_back(0); - block.q_nnf().emplace_back(0); - block.q_poseidon2_external().emplace_back(0); - block.q_poseidon2_internal().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - block.pad_additional(); - } + block.set_gate_selector(1); check_selector_length_consistency(); } @@ -1268,78 +974,9 @@ void UltraCircuitBuilder_::create_sort_constraint_with_edges( // (and remove dummy gate). This used to be a single gate before trace sorting based on gate types. The dummy gate // has been added to allow the previous gate to access the required wire data via shifts, allowing the arithmetic // gate to occur out of sequence. More details on the linked Github issue. - create_dummy_gate(block, variable_index[variable_index.size() - 1], this->zero_idx, this->zero_idx, this->zero_idx); - create_add_gate({ variable_index[variable_index.size() - 1], this->zero_idx, this->zero_idx, 1, 0, 0, -end }); -} - -// range constraint a value by decomposing it into limbs whose size should be the default range constraint size - -template -std::vector UltraCircuitBuilder_::decompose_into_default_range_better_for_oddlimbnum( - const uint32_t variable_index, const size_t num_bits, std::string const& msg) -{ - std::vector sums; - const size_t limb_num = (size_t)num_bits / DEFAULT_PLOOKUP_RANGE_BITNUM; - const size_t last_limb_size = num_bits - (limb_num * DEFAULT_PLOOKUP_RANGE_BITNUM); - if (limb_num < 3) { - std::cerr - << "number of bits in range must be an integer multipe of DEFAULT_PLOOKUP_RANGE_BITNUM of size at least 3" - << std::endl; - return sums; - } - - const uint256_t val = (uint256_t)(this->get_variable(variable_index)); - // check witness value is indeed in range (commented out cause interferes with negative tests) - // ASSERT(val < ((uint256_t)1 << num_bits) - 1); // Q:ask Zac what happens with wrapping when converting scalar - // field to uint256 ASSERT(limb_num % 3 == 0); // TODO: write version of method that doesn't need this - std::vector val_limbs; - std::vector val_slices; - for (size_t i = 0; i < limb_num; i++) { - val_slices.emplace_back( - FF(val.slice(DEFAULT_PLOOKUP_RANGE_BITNUM * i, DEFAULT_PLOOKUP_RANGE_BITNUM * (i + 1) - 1))); - val_limbs.emplace_back(this->add_variable(val_slices[i])); - create_new_range_constraint(val_limbs[i], DEFAULT_PLOOKUP_RANGE_SIZE); - } - - uint64_t last_limb_range = ((uint64_t)1 << last_limb_size) - 1; - FF last_slice(0); - uint32_t last_limb(this->zero_idx); - size_t total_limb_num = limb_num; - if (last_limb_size > 0) { - val_slices.emplace_back(FF(val.slice(num_bits - last_limb_size, num_bits))); - val_limbs.emplace_back(this->add_variable(last_slice)); - create_new_range_constraint(last_limb, last_limb_range); - total_limb_num++; - } - // pad slices and limbs in case they are not 2 mod 3 - if (total_limb_num % 3 == 1) { - val_limbs.emplace_back(this->zero_idx); // TODO: check this is zero - val_slices.emplace_back(0); - total_limb_num++; - } - FF shift = FF(1 << DEFAULT_PLOOKUP_RANGE_BITNUM); - FF second_shift = shift * shift; - sums.emplace_back(this->add_variable(val_slices[0] + shift * val_slices[1] + second_shift * val_slices[2])); - create_big_add_gate({ val_limbs[0], val_limbs[1], val_limbs[2], sums[0], 1, shift, second_shift, -1, 0 }); - FF cur_shift = (shift * second_shift); - FF cur_second_shift = cur_shift * shift; - for (size_t i = 3; i < total_limb_num; i = i + 2) { - sums.emplace_back(this->add_variable(this->get_variable(sums[sums.size() - 1]) + cur_shift * val_slices[i] + - cur_second_shift * val_slices[i + 1])); - create_big_add_gate({ sums[sums.size() - 2], - val_limbs[i], - val_limbs[i + 1], - sums[sums.size() - 1], - 1, - cur_shift, - cur_second_shift, - -1, - 0 }); - cur_shift *= second_shift; - cur_second_shift *= second_shift; - } - this->assert_equal(sums[sums.size() - 1], variable_index, msg); - return sums; + create_unconstrained_gate( + block, variable_index[variable_index.size() - 1], this->zero_idx(), this->zero_idx(), this->zero_idx()); + create_add_gate({ variable_index[variable_index.size() - 1], this->zero_idx(), this->zero_idx(), 1, 0, 0, -end }); } /** @@ -1368,15 +1005,7 @@ template void UltraCircuitBuilder_::apply_memory_selectors(const MEMORY_SELECTORS type) { auto& block = blocks.memory; - block.q_memory().emplace_back(type == MEMORY_SELECTORS::MEM_NONE ? 0 : 1); - // Set to zero the selectors that are not enabled for this gate - block.q_arith().emplace_back(0); - block.q_delta_range().emplace_back(0); - block.q_lookup_type().emplace_back(0); - block.q_elliptic().emplace_back(0); - block.q_nnf().emplace_back(0); - block.q_poseidon2_external().emplace_back(0); - block.q_poseidon2_internal().emplace_back(0); + block.set_gate_selector(type == MEMORY_SELECTORS::MEM_NONE ? 0 : 1); switch (type) { case MEMORY_SELECTORS::ROM_CONSISTENCY_CHECK: { // Memory read gate used with the sorted list of memory reads. @@ -1389,9 +1018,6 @@ void UltraCircuitBuilder_::apply_memory_selectors(const MEMORY_S block.q_4().emplace_back(0); block.q_m().emplace_back(0); block.q_c().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - block.pad_additional(); - } check_selector_length_consistency(); break; } @@ -1407,9 +1033,6 @@ void UltraCircuitBuilder_::apply_memory_selectors(const MEMORY_S block.q_4().emplace_back(0); block.q_m().emplace_back(0); block.q_c().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - block.pad_additional(); - } check_selector_length_consistency(); break; } @@ -1422,9 +1045,6 @@ void UltraCircuitBuilder_::apply_memory_selectors(const MEMORY_S block.q_4().emplace_back(1); block.q_m().emplace_back(0); block.q_c().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - block.pad_additional(); - } check_selector_length_consistency(); break; } @@ -1438,9 +1058,6 @@ void UltraCircuitBuilder_::apply_memory_selectors(const MEMORY_S block.q_4().emplace_back(0); block.q_m().emplace_back(1); // validate record witness is correctly computed block.q_c().emplace_back(0); // read/write flag stored in q_c - if constexpr (HasAdditionalSelectors) { - block.pad_additional(); - } check_selector_length_consistency(); break; } @@ -1454,9 +1071,6 @@ void UltraCircuitBuilder_::apply_memory_selectors(const MEMORY_S block.q_4().emplace_back(0); block.q_m().emplace_back(1); // validate record witness is correctly computed block.q_c().emplace_back(0); // read/write flag stored in q_c - if constexpr (HasAdditionalSelectors) { - block.pad_additional(); - } check_selector_length_consistency(); break; } @@ -1470,9 +1084,6 @@ void UltraCircuitBuilder_::apply_memory_selectors(const MEMORY_S block.q_4().emplace_back(0); block.q_m().emplace_back(1); // validate record witness is correctly computed block.q_c().emplace_back(1); // read/write flag stored in q_c - if constexpr (HasAdditionalSelectors) { - block.pad_additional(); - } check_selector_length_consistency(); break; } @@ -1483,9 +1094,6 @@ void UltraCircuitBuilder_::apply_memory_selectors(const MEMORY_S block.q_4().emplace_back(0); block.q_m().emplace_back(0); block.q_c().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - block.pad_additional(); - } check_selector_length_consistency(); break; } @@ -1519,15 +1127,7 @@ template void UltraCircuitBuilder_::apply_nnf_selectors(const NNF_SELECTORS type) { auto& block = blocks.nnf; - block.q_nnf().emplace_back(type == NNF_SELECTORS::NNF_NONE ? 0 : 1); - // Set to zero the selectors that are not enabled for this gate - block.q_arith().emplace_back(0); - block.q_delta_range().emplace_back(0); - block.q_lookup_type().emplace_back(0); - block.q_elliptic().emplace_back(0); - block.q_memory().emplace_back(0); - block.q_poseidon2_external().emplace_back(0); - block.q_poseidon2_internal().emplace_back(0); + block.set_gate_selector(type == NNF_SELECTORS::NNF_NONE ? 0 : 1); switch (type) { case NNF_SELECTORS::LIMB_ACCUMULATE_1: { block.q_1().emplace_back(0); @@ -1536,9 +1136,6 @@ void UltraCircuitBuilder_::apply_nnf_selectors(const NNF_SELECTO block.q_4().emplace_back(1); block.q_m().emplace_back(0); block.q_c().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - block.pad_additional(); - } check_selector_length_consistency(); break; } @@ -1549,9 +1146,6 @@ void UltraCircuitBuilder_::apply_nnf_selectors(const NNF_SELECTO block.q_4().emplace_back(0); block.q_m().emplace_back(1); block.q_c().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - block.pad_additional(); - } check_selector_length_consistency(); break; } @@ -1562,9 +1156,6 @@ void UltraCircuitBuilder_::apply_nnf_selectors(const NNF_SELECTO block.q_4().emplace_back(0); block.q_m().emplace_back(0); block.q_c().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - block.pad_additional(); - } check_selector_length_consistency(); break; } @@ -1575,9 +1166,6 @@ void UltraCircuitBuilder_::apply_nnf_selectors(const NNF_SELECTO block.q_4().emplace_back(1); block.q_m().emplace_back(0); block.q_c().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - block.pad_additional(); - } check_selector_length_consistency(); break; } @@ -1588,9 +1176,6 @@ void UltraCircuitBuilder_::apply_nnf_selectors(const NNF_SELECTO block.q_4().emplace_back(0); block.q_m().emplace_back(1); block.q_c().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - block.pad_additional(); - } check_selector_length_consistency(); break; } @@ -1601,9 +1186,6 @@ void UltraCircuitBuilder_::apply_nnf_selectors(const NNF_SELECTO block.q_4().emplace_back(0); block.q_m().emplace_back(0); block.q_c().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - block.pad_additional(); - } check_selector_length_consistency(); break; } @@ -1650,15 +1232,15 @@ void UltraCircuitBuilder_::range_constrain_two_limbs(const uint3 // We also use zero_idx to substitute variables that should be zero constexpr uint256_t MAX_SUBLIMB_MASK = (uint256_t(1) << 14) - 1; std::array sublimb_indices; - sublimb_indices[0] = sublimb_masks[0] != 0 ? this->add_variable(limb & MAX_SUBLIMB_MASK) : this->zero_idx; + sublimb_indices[0] = sublimb_masks[0] != 0 ? this->add_variable(limb & MAX_SUBLIMB_MASK) : this->zero_idx(); sublimb_indices[1] = - sublimb_masks[1] != 0 ? this->add_variable((limb >> 14) & MAX_SUBLIMB_MASK) : this->zero_idx; + sublimb_masks[1] != 0 ? this->add_variable((limb >> 14) & MAX_SUBLIMB_MASK) : this->zero_idx(); sublimb_indices[2] = - sublimb_masks[2] != 0 ? this->add_variable((limb >> 28) & MAX_SUBLIMB_MASK) : this->zero_idx; + sublimb_masks[2] != 0 ? this->add_variable((limb >> 28) & MAX_SUBLIMB_MASK) : this->zero_idx(); sublimb_indices[3] = - sublimb_masks[3] != 0 ? this->add_variable((limb >> 42) & MAX_SUBLIMB_MASK) : this->zero_idx; + sublimb_masks[3] != 0 ? this->add_variable((limb >> 42) & MAX_SUBLIMB_MASK) : this->zero_idx(); sublimb_indices[4] = - sublimb_masks[4] != 0 ? this->add_variable((limb >> 56) & MAX_SUBLIMB_MASK) : this->zero_idx; + sublimb_masks[4] != 0 ? this->add_variable((limb >> 56) & MAX_SUBLIMB_MASK) : this->zero_idx(); return sublimb_indices; }; @@ -1817,7 +1399,7 @@ std::array UltraCircuitBuilder_::evaluate_non_nativ -LIMB_SHIFT.sqr(), 0 }, true); - create_dummy_gate(blocks.arithmetic, this->zero_idx, this->zero_idx, this->zero_idx, lo_0_idx); + create_unconstrained_gate(blocks.arithmetic, this->zero_idx(), this->zero_idx(), this->zero_idx(), lo_0_idx); // // a = (a3 || a2 || a1 || a0) = (a3 * 2^b + a2) * 2^b + (a1 * 2^b + a0) // b = (b3 || b2 || b1 || b0) = (b3 * 2^b + b2) * 2^b + (b1 * 2^b + b0) @@ -1925,7 +1507,7 @@ template void UltraCircuitBuilder_::po // Update the public inputs block for (const auto& idx : this->public_inputs()) { // first two wires get a copy of the public inputs - blocks.pub_inputs.populate_wires(idx, idx, this->zero_idx, this->zero_idx); + blocks.pub_inputs.populate_wires(idx, idx, this->zero_idx(), this->zero_idx()); for (auto& selector : this->blocks.pub_inputs.get_selectors()) { selector.emplace_back(0); } @@ -1951,7 +1533,7 @@ template void UltraCircuitBuilder_::pr // iterate over the cached items and create constraints for (const auto& input : cached_partial_non_native_field_multiplications) { - blocks.nnf.populate_wires(input.a[1], input.b[1], this->zero_idx, input.lo_0); + blocks.nnf.populate_wires(input.a[1], input.b[1], this->zero_idx(), input.lo_0); apply_nnf_selectors(NNF_SELECTORS::NON_NATIVE_FIELD_1); ++this->num_gates; @@ -1959,11 +1541,11 @@ template void UltraCircuitBuilder_::pr apply_nnf_selectors(NNF_SELECTORS::NON_NATIVE_FIELD_2); ++this->num_gates; - blocks.nnf.populate_wires(input.a[2], input.b[2], this->zero_idx, input.hi_0); + blocks.nnf.populate_wires(input.a[2], input.b[2], this->zero_idx(), input.hi_0); apply_nnf_selectors(NNF_SELECTORS::NON_NATIVE_FIELD_3); ++this->num_gates; - blocks.nnf.populate_wires(input.a[1], input.b[1], this->zero_idx, input.hi_1); + blocks.nnf.populate_wires(input.a[1], input.b[1], this->zero_idx(), input.hi_1); apply_nnf_selectors(NNF_SELECTORS::NNF_NONE); ++this->num_gates; } @@ -2096,7 +1678,7 @@ std::array UltraCircuitBuilder_::evaluate_non_nativ block.populate_wires(y_p, x_0, y_0, x_p); block.populate_wires(z_p, x_1, y_1, z_0); block.populate_wires(x_2, y_2, z_2, z_1); - block.populate_wires(x_3, y_3, z_3, this->zero_idx); + block.populate_wires(x_3, y_3, z_3, this->zero_idx()); block.q_m().emplace_back(addconstp); block.q_1().emplace_back(0); @@ -2105,7 +1687,7 @@ std::array UltraCircuitBuilder_::evaluate_non_nativ block.q_3().emplace_back(-y_mulconst0 * 2); // z_0 - (x_0 * -xmulconst0) - (y_0 * ymulconst0) = 0 => z_0 = x_0 + y_0 block.q_4().emplace_back(0); block.q_c().emplace_back(-addconst0 * 2); - block.q_arith().emplace_back(3); + block.set_gate_selector(3); block.q_m().emplace_back(0); block.q_1().emplace_back(0); @@ -2113,7 +1695,7 @@ std::array UltraCircuitBuilder_::evaluate_non_nativ block.q_3().emplace_back(-y_mulconst1); block.q_4().emplace_back(0); block.q_c().emplace_back(-addconst1); - block.q_arith().emplace_back(2); + block.set_gate_selector(2); block.q_m().emplace_back(0); block.q_1().emplace_back(-x_mulconst2); @@ -2121,7 +1703,7 @@ std::array UltraCircuitBuilder_::evaluate_non_nativ block.q_3().emplace_back(1); block.q_4().emplace_back(0); block.q_c().emplace_back(-addconst2); - block.q_arith().emplace_back(1); + block.set_gate_selector(1); block.q_m().emplace_back(0); block.q_1().emplace_back(-x_mulconst3); @@ -2129,20 +1711,8 @@ std::array UltraCircuitBuilder_::evaluate_non_nativ block.q_3().emplace_back(1); block.q_4().emplace_back(0); block.q_c().emplace_back(-addconst3); - block.q_arith().emplace_back(1); - - for (size_t i = 0; i < 4; ++i) { - block.q_delta_range().emplace_back(0); - block.q_lookup_type().emplace_back(0); - block.q_elliptic().emplace_back(0); - block.q_memory().emplace_back(0); - block.q_nnf().emplace_back(0); - block.q_poseidon2_external().emplace_back(0); - block.q_poseidon2_internal().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - block.pad_additional(); - } - } + block.set_gate_selector(1); + check_selector_length_consistency(); this->num_gates += 4; @@ -2220,7 +1790,7 @@ std::array UltraCircuitBuilder_::evaluate_non_nativ block.populate_wires(y_p, x_0, y_0, z_p); block.populate_wires(x_p, x_1, y_1, z_0); block.populate_wires(x_2, y_2, z_2, z_1); - block.populate_wires(x_3, y_3, z_3, this->zero_idx); + block.populate_wires(x_3, y_3, z_3, this->zero_idx()); block.q_m().emplace_back(-addconstp); block.q_1().emplace_back(0); @@ -2228,7 +1798,7 @@ std::array UltraCircuitBuilder_::evaluate_non_nativ block.q_3().emplace_back(y_mulconst0 * 2); // z_0 + (x_0 * -xmulconst0) + (y_0 * ymulconst0) = 0 => z_0 = x_0 - y_0 block.q_4().emplace_back(0); block.q_c().emplace_back(-addconst0 * 2); - block.q_arith().emplace_back(3); + block.set_gate_selector(3); block.q_m().emplace_back(0); block.q_1().emplace_back(0); @@ -2236,7 +1806,7 @@ std::array UltraCircuitBuilder_::evaluate_non_nativ block.q_3().emplace_back(y_mulconst1); block.q_4().emplace_back(0); block.q_c().emplace_back(-addconst1); - block.q_arith().emplace_back(2); + block.set_gate_selector(2); block.q_m().emplace_back(0); block.q_1().emplace_back(-x_mulconst2); @@ -2244,7 +1814,7 @@ std::array UltraCircuitBuilder_::evaluate_non_nativ block.q_3().emplace_back(1); block.q_4().emplace_back(0); block.q_c().emplace_back(-addconst2); - block.q_arith().emplace_back(1); + block.set_gate_selector(1); block.q_m().emplace_back(0); block.q_1().emplace_back(-x_mulconst3); @@ -2252,20 +1822,8 @@ std::array UltraCircuitBuilder_::evaluate_non_nativ block.q_3().emplace_back(1); block.q_4().emplace_back(0); block.q_c().emplace_back(-addconst3); - block.q_arith().emplace_back(1); - - for (size_t i = 0; i < 4; ++i) { - block.q_delta_range().emplace_back(0); - block.q_lookup_type().emplace_back(0); - block.q_elliptic().emplace_back(0); - block.q_memory().emplace_back(0); - block.q_nnf().emplace_back(0); - block.q_poseidon2_external().emplace_back(0); - block.q_poseidon2_internal().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - block.pad_additional(); - } - } + block.set_gate_selector(1); + check_selector_length_consistency(); this->num_gates += 4; @@ -2412,18 +1970,8 @@ void UltraCircuitBuilder_::create_poseidon2_external_gate(const poseidon2_ex block.q_2().emplace_back(crypto::Poseidon2Bn254ScalarFieldParams::round_constants[in.round_idx][1]); block.q_3().emplace_back(crypto::Poseidon2Bn254ScalarFieldParams::round_constants[in.round_idx][2]); block.q_c().emplace_back(0); - block.q_arith().emplace_back(0); block.q_4().emplace_back(crypto::Poseidon2Bn254ScalarFieldParams::round_constants[in.round_idx][3]); - block.q_delta_range().emplace_back(0); - block.q_lookup_type().emplace_back(0); - block.q_elliptic().emplace_back(0); - block.q_memory().emplace_back(0); - block.q_nnf().emplace_back(0); - block.q_poseidon2_external().emplace_back(1); - block.q_poseidon2_internal().emplace_back(0); - if constexpr (HasAdditionalSelectors) { - block.pad_additional(); - } + block.set_gate_selector(1); this->check_selector_length_consistency(); ++this->num_gates; } @@ -2441,18 +1989,8 @@ void UltraCircuitBuilder_::create_poseidon2_internal_gate(const poseidon2_in block.q_2().emplace_back(0); block.q_3().emplace_back(0); block.q_c().emplace_back(0); - block.q_arith().emplace_back(0); block.q_4().emplace_back(0); - block.q_delta_range().emplace_back(0); - block.q_lookup_type().emplace_back(0); - block.q_elliptic().emplace_back(0); - block.q_memory().emplace_back(0); - block.q_nnf().emplace_back(0); - block.q_poseidon2_external().emplace_back(0); - block.q_poseidon2_internal().emplace_back(1); - if constexpr (HasAdditionalSelectors) { - block.pad_additional(); - } + block.set_gate_selector(1); this->check_selector_length_consistency(); ++this->num_gates; } @@ -2467,9 +2005,9 @@ template msgpack::sbuffer UltraCircuitBuilder_get_first_variable_in_class(this->zero_idx); + auto first_zero_idx = this->get_first_variable_in_class(this->zero_idx()); if (!this->variable_names.contains(first_zero_idx)) { - this->set_variable_name(this->zero_idx, "zero"); + this->set_variable_name(this->zero_idx(), "zero"); } else { this->variable_names[first_zero_idx] = "zero"; } @@ -2608,7 +2146,5 @@ template msgpack::sbuffer UltraCircuitBuilder_; template class UltraCircuitBuilder_; -// To enable this we need to template plookup -// template class UltraCircuitBuilder_; } // namespace bb diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/ultra_circuit_builder.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/ultra_circuit_builder.hpp index 27c51ae5763e..bc4b06c862e5 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/ultra_circuit_builder.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/ultra_circuit_builder.hpp @@ -9,11 +9,9 @@ #include "barretenberg/honk/execution_trace/mega_execution_trace.hpp" #include "barretenberg/honk/execution_trace/ultra_execution_trace.hpp" #include "barretenberg/honk/types/circuit_type.hpp" -#include "barretenberg/honk/types/merkle_hash_type.hpp" #include "barretenberg/stdlib_circuit_builders/plookup_tables/plookup_tables.hpp" #include "barretenberg/stdlib_circuit_builders/plookup_tables/types.hpp" -// TODO(md): note that this has now been added #include "circuit_builder_base.hpp" #include "rom_ram_logic.hpp" #include @@ -46,13 +44,8 @@ class UltraCircuitBuilder_ : public CircuitBuilderBase; static constexpr size_t NUM_WIRES = ExecutionTrace::NUM_WIRES; - // Keeping NUM_WIRES, at least temporarily, for backward compatibility - static constexpr size_t program_width = ExecutionTrace::NUM_WIRES; static constexpr std::string_view NAME_STRING = "UltraCircuitBuilder"; - static constexpr CircuitType CIRCUIT_TYPE = CircuitType::ULTRA; - static constexpr merkle::HashType merkle_hash_type = merkle::HashType::LOOKUP_PEDERSEN; - static constexpr size_t UINT_LOG2_BASE = 6; // DOCTODO: explain what this is, or rename. // The plookup range proof requires work linear in range size, thus cannot be used directly for // large ranges such as 2^64. For such ranges the element will be decomposed into smaller // chuncks according to the parameter below @@ -60,11 +53,6 @@ class UltraCircuitBuilder_ : public CircuitBuilderBase range_lists; // DOCTODO: explain this. // Witnesses that can be in one gate, but that's intentional (used in boomerang catcher) - std::vector used_witnesses; + std::vector used_witnesses; // AUDITTODO: isolate these boomerang details? // Witnesses that appear in finalize method (used in boomerang catcher). Need to check // that all variables from some connected component were created after finalize method was called std::unordered_set finalize_witnesses; @@ -233,12 +213,14 @@ class UltraCircuitBuilder_ : public CircuitBuilderBase(size_hint) { - this->zero_idx = put_constant_variable(FF::zero()); + this->set_zero_idx(put_constant_variable(FF::zero())); this->tau.insert({ DUMMY_TAG, DUMMY_TAG }); // TODO(luke): explain this }; + /** * @brief Constructor from data generated from ACIR * @@ -256,8 +238,7 @@ class UltraCircuitBuilder_ : public CircuitBuilderBase& public_inputs, - size_t varnum, - bool recursive = false) + size_t varnum) : CircuitBuilderBase(size_hint, witness_values.empty()) { for (size_t idx = 0; idx < varnum; ++idx) { @@ -272,55 +253,15 @@ class UltraCircuitBuilder_ : public CircuitBuilderBasezero_idx = put_constant_variable(FF::zero()); + this->set_zero_idx(put_constant_variable(FF::zero())); this->tau.insert({ DUMMY_TAG, DUMMY_TAG }); // TODO(luke): explain this - - this->is_recursive_circuit = recursive; }; UltraCircuitBuilder_(const UltraCircuitBuilder_& other) = default; - UltraCircuitBuilder_(UltraCircuitBuilder_&& other) noexcept - : CircuitBuilderBase(std::move(other)) - , blocks(other.blocks) - , constant_variable_indices(other.constant_variable_indices) - , lookup_tables(other.lookup_tables) - , rom_ram_logic(other.rom_ram_logic) - , memory_read_records(other.memory_read_records) - , memory_write_records(other.memory_write_records) - , range_lists(other.range_lists) - , cached_partial_non_native_field_multiplications(other.cached_partial_non_native_field_multiplications) - , circuit_finalized(other.circuit_finalized) - , ipa_proof(other.ipa_proof) {}; + UltraCircuitBuilder_(UltraCircuitBuilder_&& other) = default; UltraCircuitBuilder_& operator=(const UltraCircuitBuilder_& other) = default; - UltraCircuitBuilder_& operator=(UltraCircuitBuilder_&& other) noexcept - { - CircuitBuilderBase::operator=(std::move(other)); - blocks = other.blocks; - constant_variable_indices = other.constant_variable_indices; - - lookup_tables = other.lookup_tables; - range_lists = other.range_lists; - rom_ram_logic = other.rom_ram_logic; - memory_read_records = other.memory_read_records; - memory_write_records = other.memory_write_records; - cached_partial_non_native_field_multiplications = other.cached_partial_non_native_field_multiplications; - circuit_finalized = other.circuit_finalized; - ipa_proof = other.ipa_proof; - return *this; - }; + UltraCircuitBuilder_& operator=(UltraCircuitBuilder_&& other) = default; ~UltraCircuitBuilder_() override = default; - bool operator==(const UltraCircuitBuilder_& other) const - { - - return blocks == other.blocks && constant_variable_indices == other.constant_variable_indices && - lookup_tables == other.lookup_tables && memory_read_records == other.memory_read_records && - memory_write_records == other.memory_write_records && range_lists == other.range_lists && - cached_partial_non_native_field_multiplications == - other.cached_partial_non_native_field_multiplications && - used_witnesses == other.used_witnesses && rom_ram_logic == other.rom_ram_logic && - // Compare the base class - CircuitBuilderBase::operator==(other); - } /** * @brief Debug helper method for ensuring all selectors have the same size * @details Each gate construction method manually appends values to the selectors. Failing to update one of the @@ -351,9 +292,7 @@ class UltraCircuitBuilder_ : public CircuitBuilderBase& in) override; void create_big_mul_add_gate(const mul_quad_& in, const bool use_next_gate_w_4 = false); void create_big_add_gate(const add_quad_& in, const bool use_next_gate_w_4 = false); - void create_big_add_gate_with_bit_extraction(const add_quad_& in); void create_big_mul_gate(const mul_quad_& in); - void create_balanced_add_gate(const add_quad_& in); void create_mul_gate(const mul_triple_& in) override; void create_bool_gate(const uint32_t a) override; @@ -422,6 +361,11 @@ class UltraCircuitBuilder_ : public CircuitBuilderBasenum_gates; // each ROM gate adds +1 extra gate due to the rom reads being copied to a sorted list set @@ -540,22 +484,6 @@ class UltraCircuitBuilder_ : public CircuitBuilderBase builder; // instantiate new builder - - size_t num_gates_prior = builder.get_estimated_num_finalized_gates(); - builder.add_gates_to_ensure_all_polys_are_non_zero(); - size_t num_gates_post = builder.get_estimated_num_finalized_gates(); // accounts for finalization gates - - return num_gates_post - num_gates_prior; - } - /** * @brief Get combined size of all tables used in circuit * @@ -712,10 +640,6 @@ class UltraCircuitBuilder_ : public CircuitBuilderBase decompose_into_default_range_better_for_oddlimbnum( - const uint32_t variable_index, - const size_t num_bits, - std::string const& msg = "decompose_into_default_range_better_for_oddlimbnum"); /** * @brief Create a gate with no constraints but with possibly non-trivial wire values @@ -725,7 +649,7 @@ class UltraCircuitBuilder_ : public CircuitBuilderBase) { - block.pad_additional(); - } + block.set_gate_selector(0); // all selectors zero + check_selector_length_consistency(); ++this->num_gates; } - void create_dummy_constraints(const std::vector& variable_index); + void create_unconstrained_gates(const std::vector& variable_index); void create_sort_constraint(const std::vector& variable_index); void create_sort_constraint_with_edges(const std::vector& variable_index, const FF&, const FF&); void assign_tag(const uint32_t variable_index, const uint32_t tag) @@ -818,7 +732,6 @@ class UltraCircuitBuilder_ : public CircuitBuilderBase& in); void create_poseidon2_internal_gate(const poseidon2_internal_gate_& in); diff --git a/barretenberg/cpp/src/barretenberg/translator_vm/translator_circuit_builder.cpp b/barretenberg/cpp/src/barretenberg/translator_vm/translator_circuit_builder.cpp index ad5ccd586249..9b1976f81553 100644 --- a/barretenberg/cpp/src/barretenberg/translator_vm/translator_circuit_builder.cpp +++ b/barretenberg/cpp/src/barretenberg/translator_vm/translator_circuit_builder.cpp @@ -410,7 +410,7 @@ void TranslatorCircuitBuilder::populate_wires_from_ultra_op(const UltraOp& ultra op_wire.push_back(add_variable(ultra_op.op_code.value())); // Similarly to the ColumnPolynomials in the merge protocol, the op_wire is 0 at every second index for a // genuine op - op_wire.push_back(zero_idx); + op_wire.push_back(zero_idx()); } insert_pair_into_wire(WireIds::X_LOW_Y_HI, ultra_op.x_lo, ultra_op.y_hi); @@ -532,8 +532,8 @@ void TranslatorCircuitBuilder::feed_ecc_op_queue_into_circuit(const std::shared_ // Although only the first index needs to be zero, we add two zeros to maintain consistency since each actual // UltraOp populates two polynomial indices. for (auto& wire : wires) { - wire.push_back(zero_idx); - wire.push_back(zero_idx); + wire.push_back(zero_idx()); + wire.push_back(zero_idx()); } num_gates += 2; @@ -542,8 +542,8 @@ void TranslatorCircuitBuilder::feed_ecc_op_queue_into_circuit(const std::shared_ populate_wires_from_ultra_op(ultra_op); // Populate the other wires with zeros for (size_t i = WireIds::Y_LOW_Z_2 + 1; i < wires.size(); i++) { - wires[i].push_back(zero_idx); - wires[i].push_back(zero_idx); + wires[i].push_back(zero_idx()); + wires[i].push_back(zero_idx()); } num_gates += 2; }; @@ -598,8 +598,8 @@ void TranslatorCircuitBuilder::feed_ecc_op_queue_into_circuit(const std::shared_ // previous row are well-formed and that the accumulator value is correctly propagated throughout // the entire no-op range for both even and odd indices. for (size_t j = 0; j < ACCUMULATORS_BINARY_LIMBS_0; j++) { - wires[j].push_back(zero_idx); - wires[j].push_back(zero_idx); + wires[j].push_back(zero_idx()); + wires[j].push_back(zero_idx()); } size_t idx = 0; for (size_t j = ACCUMULATORS_BINARY_LIMBS_0; j < ACCUMULATORS_BINARY_LIMBS_3 + 1; j++) { @@ -608,8 +608,8 @@ void TranslatorCircuitBuilder::feed_ecc_op_queue_into_circuit(const std::shared_ idx++; } for (size_t j = ACCUMULATORS_BINARY_LIMBS_3 + 1; j < TOTAL_COUNT; j++) { - wires[j].push_back(zero_idx); - wires[j].push_back(zero_idx); + wires[j].push_back(zero_idx()); + wires[j].push_back(zero_idx()); } num_gates += 2; continue; diff --git a/barretenberg/cpp/src/barretenberg/translator_vm/translator_circuit_builder.hpp b/barretenberg/cpp/src/barretenberg/translator_vm/translator_circuit_builder.hpp index 3767d42e953f..c8a57d598512 100644 --- a/barretenberg/cpp/src/barretenberg/translator_vm/translator_circuit_builder.hpp +++ b/barretenberg/cpp/src/barretenberg/translator_vm/translator_circuit_builder.hpp @@ -339,7 +339,7 @@ class TranslatorCircuitBuilder : public CircuitBuilderBase { , evaluation_input_x(evaluation_input_x_) , avm_mode(avm_mode_) { - this->zero_idx = add_variable(Fr::zero()); + this->set_zero_idx(add_variable(Fr::zero())); }; /** diff --git a/barretenberg/cpp/src/barretenberg/ultra_honk/relation_correctness.test.cpp b/barretenberg/cpp/src/barretenberg/ultra_honk/relation_correctness.test.cpp index 8bfe212fc65b..ccc21fa53ae4 100644 --- a/barretenberg/cpp/src/barretenberg/ultra_honk/relation_correctness.test.cpp +++ b/barretenberg/cpp/src/barretenberg/ultra_honk/relation_correctness.test.cpp @@ -50,7 +50,7 @@ template void create_some_add_gates(auto& circuit_builder) FF e = a + b + c + d; uint32_t e_idx = circuit_builder.add_variable(e); - uint32_t zero_idx = circuit_builder.zero_idx; + uint32_t zero_idx = circuit_builder.zero_idx(); circuit_builder.create_big_add_gate({ a_idx, b_idx, c_idx, d_idx, -1, -1, -1, -1, 0 }, true); // use next row circuit_builder.create_big_add_gate({ zero_idx, zero_idx, zero_idx, e_idx, 0, 0, 0, 0, 0 }, false); } @@ -126,9 +126,9 @@ template void create_some_RAM_gates(auto& circuit_builder) circuit_builder.create_big_add_gate({ a_idx, b_idx, c_idx, d_idx, -1, -1, -1, -1, 0 }, true); circuit_builder.create_big_add_gate( { - circuit_builder.zero_idx, - circuit_builder.zero_idx, - circuit_builder.zero_idx, + circuit_builder.zero_idx(), + circuit_builder.zero_idx(), + circuit_builder.zero_idx(), e_idx, 0, 0, diff --git a/barretenberg/cpp/src/barretenberg/ultra_honk/sumcheck.test.cpp b/barretenberg/cpp/src/barretenberg/ultra_honk/sumcheck.test.cpp index 16c002dcb608..9fed84db2c33 100644 --- a/barretenberg/cpp/src/barretenberg/ultra_honk/sumcheck.test.cpp +++ b/barretenberg/cpp/src/barretenberg/ultra_honk/sumcheck.test.cpp @@ -55,7 +55,7 @@ TEST_F(SumcheckTestsRealCircuit, Ultra) FF e = a + b + c + d; uint32_t e_idx = builder.add_variable(e); - uint32_t zero_idx = builder.zero_idx; + uint32_t zero_idx = builder.zero_idx(); builder.create_big_add_gate({ a_idx, b_idx, c_idx, d_idx, -1, -1, -1, -1, 0 }, true); // use next row builder.create_big_add_gate({ zero_idx, zero_idx, zero_idx, e_idx, 0, 0, 0, 0, 0 }, false); @@ -134,9 +134,9 @@ TEST_F(SumcheckTestsRealCircuit, Ultra) builder.create_big_add_gate({ a_idx, b_idx, c_idx, d_idx, -1, -1, -1, -1, 0 }, true); builder.create_big_add_gate( { - builder.zero_idx, - builder.zero_idx, - builder.zero_idx, + builder.zero_idx(), + builder.zero_idx(), + builder.zero_idx(), e_idx, 0, 0, diff --git a/barretenberg/cpp/src/barretenberg/ultra_honk/ultra_honk.test.cpp b/barretenberg/cpp/src/barretenberg/ultra_honk/ultra_honk.test.cpp index 299a172a06cf..7fadb20768e5 100644 --- a/barretenberg/cpp/src/barretenberg/ultra_honk/ultra_honk.test.cpp +++ b/barretenberg/cpp/src/barretenberg/ultra_honk/ultra_honk.test.cpp @@ -420,9 +420,9 @@ TYPED_TEST(UltraHonkTests, NonTrivialTagPermutation) auto d_idx = circuit_builder.add_variable(a); circuit_builder.create_add_gate( - { a_idx, b_idx, circuit_builder.zero_idx, fr::one(), fr::one(), fr::zero(), fr::zero() }); + { a_idx, b_idx, circuit_builder.zero_idx(), fr::one(), fr::one(), fr::zero(), fr::zero() }); circuit_builder.create_add_gate( - { c_idx, d_idx, circuit_builder.zero_idx, fr::one(), fr::one(), fr::zero(), fr::zero() }); + { c_idx, d_idx, circuit_builder.zero_idx(), fr::one(), fr::one(), fr::zero(), fr::zero() }); circuit_builder.create_tag(1, 2); circuit_builder.create_tag(2, 1); @@ -464,11 +464,11 @@ TYPED_TEST(UltraHonkTests, NonTrivialTagPermutationAndCycles) circuit_builder.assign_tag(g_idx, 2); circuit_builder.create_add_gate( - { b_idx, a_idx, circuit_builder.zero_idx, fr::one(), fr::neg_one(), fr::zero(), fr::zero() }); + { b_idx, a_idx, circuit_builder.zero_idx(), fr::one(), fr::neg_one(), fr::zero(), fr::zero() }); circuit_builder.create_add_gate( - { c_idx, g_idx, circuit_builder.zero_idx, fr::one(), -fr::one(), fr::zero(), fr::zero() }); + { c_idx, g_idx, circuit_builder.zero_idx(), fr::one(), -fr::one(), fr::zero(), fr::zero() }); circuit_builder.create_add_gate( - { e_idx, f_idx, circuit_builder.zero_idx, fr::one(), -fr::one(), fr::zero(), fr::zero() }); + { e_idx, f_idx, circuit_builder.zero_idx(), fr::one(), -fr::one(), fr::zero(), fr::zero() }); TestFixture::set_default_pairing_points_and_ipa_claim_and_proof(circuit_builder); TestFixture::prove_and_verify(circuit_builder, /*expected_result=*/true); @@ -486,8 +486,8 @@ TYPED_TEST(UltraHonkTests, BadTagPermutation) auto c_idx = circuit_builder.add_variable(b); auto d_idx = circuit_builder.add_variable(a + 1); - circuit_builder.create_add_gate({ a_idx, b_idx, circuit_builder.zero_idx, 1, 1, 0, 0 }); - circuit_builder.create_add_gate({ c_idx, d_idx, circuit_builder.zero_idx, 1, 1, 0, -1 }); + circuit_builder.create_add_gate({ a_idx, b_idx, circuit_builder.zero_idx(), 1, 1, 0, 0 }); + circuit_builder.create_add_gate({ c_idx, d_idx, circuit_builder.zero_idx(), 1, 1, 0, -1 }); circuit_builder.create_tag(1, 2); circuit_builder.create_tag(2, 1); @@ -511,8 +511,8 @@ TYPED_TEST(UltraHonkTests, BadTagPermutation) auto c_idx = circuit_builder.add_variable(b); auto d_idx = circuit_builder.add_variable(a + 1); - circuit_builder.create_add_gate({ a_idx, b_idx, circuit_builder.zero_idx, 1, 1, 0, 0 }); - circuit_builder.create_add_gate({ c_idx, d_idx, circuit_builder.zero_idx, 1, 1, 0, -1 }); + circuit_builder.create_add_gate({ a_idx, b_idx, circuit_builder.zero_idx(), 1, 1, 0, 0 }); + circuit_builder.create_add_gate({ c_idx, d_idx, circuit_builder.zero_idx(), 1, 1, 0, -1 }); TestFixture::set_default_pairing_points_and_ipa_claim_and_proof(circuit_builder); TestFixture::prove_and_verify(circuit_builder, /*expected_result=*/true); @@ -664,7 +664,7 @@ TYPED_TEST(UltraHonkTests, RangeConstraint) circuit_builder.create_new_range_constraint(indices[i], 3); } // auto ind = {a_idx,b_idx,c_idx,d_idx,e_idx,f_idx,g_idx,h_idx}; - circuit_builder.create_dummy_constraints(indices); + circuit_builder.create_unconstrained_gates(indices); TestFixture::set_default_pairing_points_and_ipa_claim_and_proof(circuit_builder); @@ -689,7 +689,7 @@ TYPED_TEST(UltraHonkTests, RangeConstraint) for (size_t i = 0; i < indices.size(); i++) { circuit_builder.create_new_range_constraint(indices[i], 128); } - circuit_builder.create_dummy_constraints(indices); + circuit_builder.create_unconstrained_gates(indices); TestFixture::set_default_pairing_points_and_ipa_claim_and_proof(circuit_builder); @@ -702,7 +702,7 @@ TYPED_TEST(UltraHonkTests, RangeConstraint) for (size_t i = 0; i < indices.size(); i++) { circuit_builder.create_new_range_constraint(indices[i], 79); } - circuit_builder.create_dummy_constraints(indices); + circuit_builder.create_unconstrained_gates(indices); TestFixture::set_default_pairing_points_and_ipa_claim_and_proof(circuit_builder); @@ -715,7 +715,7 @@ TYPED_TEST(UltraHonkTests, RangeConstraint) for (size_t i = 0; i < indices.size(); i++) { circuit_builder.create_new_range_constraint(indices[i], 79); } - circuit_builder.create_dummy_constraints(indices); + circuit_builder.create_unconstrained_gates(indices); TestFixture::set_default_pairing_points_and_ipa_claim_and_proof(circuit_builder); @@ -731,12 +731,14 @@ TYPED_TEST(UltraHonkTests, RangeWithGates) circuit_builder.create_new_range_constraint(idx[i], 8); } - circuit_builder.create_add_gate({ idx[0], idx[1], circuit_builder.zero_idx, fr::one(), fr::one(), fr::zero(), -3 }); - circuit_builder.create_add_gate({ idx[2], idx[3], circuit_builder.zero_idx, fr::one(), fr::one(), fr::zero(), -7 }); circuit_builder.create_add_gate( - { idx[4], idx[5], circuit_builder.zero_idx, fr::one(), fr::one(), fr::zero(), -11 }); + { idx[0], idx[1], circuit_builder.zero_idx(), fr::one(), fr::one(), fr::zero(), -3 }); circuit_builder.create_add_gate( - { idx[6], idx[7], circuit_builder.zero_idx, fr::one(), fr::one(), fr::zero(), -15 }); + { idx[2], idx[3], circuit_builder.zero_idx(), fr::one(), fr::one(), fr::zero(), -7 }); + circuit_builder.create_add_gate( + { idx[4], idx[5], circuit_builder.zero_idx(), fr::one(), fr::one(), fr::zero(), -11 }); + circuit_builder.create_add_gate( + { idx[6], idx[7], circuit_builder.zero_idx(), fr::one(), fr::one(), fr::zero(), -15 }); TestFixture::set_default_pairing_points_and_ipa_claim_and_proof(circuit_builder); @@ -751,12 +753,14 @@ TYPED_TEST(UltraHonkTests, RangeWithGatesWhereRangeIsNotAPowerOfTwo) circuit_builder.create_new_range_constraint(idx[i], 12); } - circuit_builder.create_add_gate({ idx[0], idx[1], circuit_builder.zero_idx, fr::one(), fr::one(), fr::zero(), -3 }); - circuit_builder.create_add_gate({ idx[2], idx[3], circuit_builder.zero_idx, fr::one(), fr::one(), fr::zero(), -7 }); circuit_builder.create_add_gate( - { idx[4], idx[5], circuit_builder.zero_idx, fr::one(), fr::one(), fr::zero(), -11 }); + { idx[0], idx[1], circuit_builder.zero_idx(), fr::one(), fr::one(), fr::zero(), -3 }); + circuit_builder.create_add_gate( + { idx[2], idx[3], circuit_builder.zero_idx(), fr::one(), fr::one(), fr::zero(), -7 }); + circuit_builder.create_add_gate( + { idx[4], idx[5], circuit_builder.zero_idx(), fr::one(), fr::one(), fr::zero(), -11 }); circuit_builder.create_add_gate( - { idx[6], idx[7], circuit_builder.zero_idx, fr::one(), fr::one(), fr::zero(), -15 }); + { idx[6], idx[7], circuit_builder.zero_idx(), fr::one(), fr::one(), fr::zero(), -15 }); TestFixture::set_default_pairing_points_and_ipa_claim_and_proof(circuit_builder); @@ -819,7 +823,7 @@ TYPED_TEST(UltraHonkTests, ComposedRangeConstraint) auto d = uint256_t(c).slice(0, 133); auto e = fr(d); auto a_idx = circuit_builder.add_variable(fr(e)); - circuit_builder.create_add_gate({ a_idx, circuit_builder.zero_idx, circuit_builder.zero_idx, 1, 0, 0, -fr(e) }); + circuit_builder.create_add_gate({ a_idx, circuit_builder.zero_idx(), circuit_builder.zero_idx(), 1, 0, 0, -fr(e) }); circuit_builder.decompose_into_default_range(a_idx, 134); TestFixture::set_default_pairing_points_and_ipa_claim_and_proof(circuit_builder); @@ -984,9 +988,9 @@ TYPED_TEST(UltraHonkTests, Ram) true); circuit_builder.create_big_add_gate( { - circuit_builder.zero_idx, - circuit_builder.zero_idx, - circuit_builder.zero_idx, + circuit_builder.zero_idx(), + circuit_builder.zero_idx(), + circuit_builder.zero_idx(), e_idx, 0, 0,