From 351eab6fb1e237eb09ae023082b138dca971475a Mon Sep 17 00:00:00 2001 From: ledwards2225 Date: Thu, 2 Nov 2023 17:11:51 +0000 Subject: [PATCH] define pad directly in arith to avoid virtual --- .../arithmetization/arithmetization.hpp | 59 +++++++++++++--- .../goblin_ultra_circuit_builder.hpp | 6 -- .../circuit_builder/ultra_circuit_builder.cpp | 70 +++++++++---------- .../circuit_builder/ultra_circuit_builder.hpp | 8 --- 4 files changed, 83 insertions(+), 60 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/proof_system/arithmetization/arithmetization.hpp b/barretenberg/cpp/src/barretenberg/proof_system/arithmetization/arithmetization.hpp index 6697cee6aae7..c6dffdeaa3a1 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/arithmetization/arithmetization.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/arithmetization/arithmetization.hpp @@ -71,8 +71,10 @@ template class Ultra { using FF = FF_; using SelectorType = std::vector>; - std::vector selectors; + private: + std::array selectors; + public: SelectorType& q_m() { return selectors[0]; }; SelectorType& q_c() { return selectors[1]; }; SelectorType& q_1() { return selectors[2]; }; @@ -85,19 +87,22 @@ template class Ultra { SelectorType& q_aux() { return selectors[9]; }; SelectorType& q_lookup_type() { return selectors[10]; }; - Ultra(size_t num_selectors = NUM_SELECTORS) - : selectors(num_selectors) - {} - const auto& get() const { return selectors; }; void reserve(size_t size_hint) { - for (auto& p : selectors) { - p.reserve(size_hint); + for (auto& vec : selectors) { + vec.reserve(size_hint); } } + /** + * @brief Add zeros to all selectors which are not part of the conventional Ultra arithmetization + * @details Does nothing for this class since this IS the conventional Ultra arithmetization + * + */ + void pad_additional(){}; + // Note: These are needed for Plonk only (for poly storage in a std::map). Must be in same order as above struct. inline static const std::vector selector_names = { "q_m", "q_c", "q_1", "q_2", "q_3", "q_4", "q_arith", "q_sort", @@ -110,17 +115,49 @@ template class Ultra { * * @tparam FF_ */ -template class UltraHonk : public Ultra { +template class UltraHonk { public: + static constexpr size_t NUM_WIRES = 4; static constexpr size_t NUM_SELECTORS = 12; using FF = FF_; using SelectorType = std::vector>; + private: + std::array selectors; + + public: + SelectorType& q_m() { return selectors[0]; }; + SelectorType& q_c() { return selectors[1]; }; + SelectorType& q_1() { return selectors[2]; }; + SelectorType& q_2() { return selectors[3]; }; + SelectorType& q_3() { return selectors[4]; }; + SelectorType& q_4() { return selectors[5]; }; + SelectorType& q_arith() { return selectors[6]; }; + SelectorType& q_sort() { return selectors[7]; }; + SelectorType& q_elliptic() { return selectors[8]; }; + SelectorType& q_aux() { return selectors[9]; }; + SelectorType& q_lookup_type() { return selectors[10]; }; SelectorType& q_busread() { return this->selectors[11]; }; - UltraHonk() - : Ultra(NUM_SELECTORS) - {} + const auto& get() const { return selectors; }; + + void reserve(size_t size_hint) + { + for (auto& vec : selectors) { + vec.reserve(size_hint); + } + } + + /** + * @brief Add zeros to all selectors which are not part of the conventional Ultra arithmetization + * @details Facilitates reuse of Ultra gate construction functions in arithmetizations which extend the conventional + * Ultra arithmetization + * + */ + void pad_additional() { q_busread().emplace_back(0); }; + + // Note: Unused. Needed only for consistency with Ultra arith (which is used by Plonk) + inline static const std::vector selector_names = {}; }; class GoblinTranslator { diff --git a/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/goblin_ultra_circuit_builder.hpp b/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/goblin_ultra_circuit_builder.hpp index 1b7c6e6eb158..f4532a318954 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/goblin_ultra_circuit_builder.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/goblin_ultra_circuit_builder.hpp @@ -77,12 +77,6 @@ template class GoblinUltraCircuitBuilder_ : public UltraCircuitBui void finalize_circuit(); void add_gates_to_ensure_all_polys_are_non_zero(); - /** - * @brief Utility for adding zeros to selectors which are not part of the conventional Ultra arithmetization - * - */ - void pad_additional_selectors() override { q_busread.emplace_back(0); }; - size_t get_num_constant_gates() const override { return 0; } /** diff --git a/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/ultra_circuit_builder.cpp b/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/ultra_circuit_builder.cpp index cd4a01881858..da96bd1cf861 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/ultra_circuit_builder.cpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/ultra_circuit_builder.cpp @@ -79,7 +79,7 @@ void UltraCircuitBuilder_::add_gates_to_ensure_all_polys_are_no q_lookup_type.emplace_back(0); q_elliptic.emplace_back(1); q_aux.emplace_back(1); - pad_additional_selectors(); + selectors.pad_additional(); ++this->num_gates; // Some relations depend on wire shifts so we add another gate with @@ -139,7 +139,7 @@ void UltraCircuitBuilder_::create_add_gate(const add_triple_num_gates; } @@ -171,7 +171,7 @@ void UltraCircuitBuilder_::create_big_add_gate(const add_quad_< q_lookup_type.emplace_back(0); q_elliptic.emplace_back(0); q_aux.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); ++this->num_gates; } @@ -265,7 +265,7 @@ void UltraCircuitBuilder_::create_big_mul_gate(const mul_quad_< q_lookup_type.emplace_back(0); q_elliptic.emplace_back(0); q_aux.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); ++this->num_gates; } @@ -291,7 +291,7 @@ void UltraCircuitBuilder_::create_balanced_add_gate(const add_q q_lookup_type.emplace_back(0); q_elliptic.emplace_back(0); q_aux.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); ++this->num_gates; // Why 3? TODO: return to this // The purpose of this gate is to do enable lazy 32-bit addition. @@ -333,7 +333,7 @@ void UltraCircuitBuilder_::create_mul_gate(const mul_triple_num_gates; } /** @@ -362,7 +362,7 @@ void UltraCircuitBuilder_::create_bool_gate(const uint32_t vari q_lookup_type.emplace_back(0); q_elliptic.emplace_back(0); q_aux.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); ++this->num_gates; } @@ -393,7 +393,7 @@ void UltraCircuitBuilder_::create_poly_gate(const poly_triple_< q_lookup_type.emplace_back(0); q_elliptic.emplace_back(0); q_aux.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); ++this->num_gates; } @@ -447,7 +447,7 @@ void UltraCircuitBuilder_::create_ecc_add_gate(const ecc_add_ga q_lookup_type.emplace_back(0); q_elliptic.emplace_back(1); q_aux.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); ++this->num_gates; } w_l.emplace_back(in.x2); @@ -465,7 +465,7 @@ void UltraCircuitBuilder_::create_ecc_add_gate(const ecc_add_ga q_lookup_type.emplace_back(0); q_elliptic.emplace_back(0); q_aux.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); ++this->num_gates; } @@ -511,7 +511,7 @@ void UltraCircuitBuilder_::create_ecc_dbl_gate(const ecc_dbl_ga q_sort.emplace_back(0); q_lookup_type.emplace_back(0); q_aux.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); ++this->num_gates; } @@ -530,7 +530,7 @@ void UltraCircuitBuilder_::create_ecc_dbl_gate(const ecc_dbl_ga q_lookup_type.emplace_back(0); q_elliptic.emplace_back(0); q_aux.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); ++this->num_gates; } @@ -560,7 +560,7 @@ void UltraCircuitBuilder_::fix_witness(const uint32_t witness_i q_lookup_type.emplace_back(0); q_elliptic.emplace_back(0); q_aux.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); ++this->num_gates; } @@ -635,7 +635,7 @@ plookup::ReadData UltraCircuitBuilder_::create_gates_ q_sort.emplace_back(0); q_elliptic.emplace_back(0); q_aux.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); ++this->num_gates; } return read_data; @@ -944,7 +944,7 @@ void UltraCircuitBuilder_::create_sort_constraint(const std::ve q_elliptic.emplace_back(0); q_lookup_type.emplace_back(0); q_aux.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); } // dummy gate needed because of sort widget's check of next row w_l.emplace_back(variable_index[variable_index.size() - 1]); @@ -963,7 +963,7 @@ void UltraCircuitBuilder_::create_sort_constraint(const std::ve q_elliptic.emplace_back(0); q_lookup_type.emplace_back(0); q_aux.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); } // useful to put variables in the witness that aren't already used - e.g. the dummy variables of the range constraint in @@ -997,7 +997,7 @@ void UltraCircuitBuilder_::create_dummy_constraints(const std:: q_elliptic.emplace_back(0); q_lookup_type.emplace_back(0); q_aux.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); } } @@ -1028,7 +1028,7 @@ void UltraCircuitBuilder_::create_sort_constraint_with_edges( q_elliptic.emplace_back(0); q_lookup_type.emplace_back(0); q_aux.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); // enforce range check for middle rows for (size_t i = gate_width; i < variable_index.size() - gate_width; i += gate_width) { @@ -1048,7 +1048,7 @@ void UltraCircuitBuilder_::create_sort_constraint_with_edges( q_elliptic.emplace_back(0); q_lookup_type.emplace_back(0); q_aux.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); } // enforce range checks of last row and ending at end if (variable_index.size() > gate_width) { @@ -1068,7 +1068,7 @@ void UltraCircuitBuilder_::create_sort_constraint_with_edges( q_elliptic.emplace_back(0); q_lookup_type.emplace_back(0); q_aux.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); } // dummy gate needed because of sort widget's check of next row @@ -1089,7 +1089,7 @@ void UltraCircuitBuilder_::create_sort_constraint_with_edges( q_elliptic.emplace_back(0); q_lookup_type.emplace_back(0); q_aux.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); } // range constraint a value by decomposing it into limbs whose size should be the default range constraint size @@ -1205,7 +1205,7 @@ void UltraCircuitBuilder_::apply_aux_selectors(const AUX_SELECT q_m.emplace_back(0); q_c.emplace_back(0); q_arith.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); break; } case AUX_SELECTORS::LIMB_ACCUMULATE_2: { @@ -1216,7 +1216,7 @@ void UltraCircuitBuilder_::apply_aux_selectors(const AUX_SELECT q_m.emplace_back(1); q_c.emplace_back(0); q_arith.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); break; } case AUX_SELECTORS::NON_NATIVE_FIELD_1: { @@ -1227,7 +1227,7 @@ void UltraCircuitBuilder_::apply_aux_selectors(const AUX_SELECT q_m.emplace_back(0); q_c.emplace_back(0); q_arith.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); break; } case AUX_SELECTORS::NON_NATIVE_FIELD_2: { @@ -1238,7 +1238,7 @@ void UltraCircuitBuilder_::apply_aux_selectors(const AUX_SELECT q_m.emplace_back(0); q_c.emplace_back(0); q_arith.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); break; } case AUX_SELECTORS::NON_NATIVE_FIELD_3: { @@ -1249,7 +1249,7 @@ void UltraCircuitBuilder_::apply_aux_selectors(const AUX_SELECT q_m.emplace_back(1); q_c.emplace_back(0); q_arith.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); break; } case AUX_SELECTORS::ROM_CONSISTENCY_CHECK: { @@ -1264,7 +1264,7 @@ void UltraCircuitBuilder_::apply_aux_selectors(const AUX_SELECT q_m.emplace_back(0); q_c.emplace_back(0); q_arith.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); break; } case AUX_SELECTORS::RAM_CONSISTENCY_CHECK: { @@ -1280,7 +1280,7 @@ void UltraCircuitBuilder_::apply_aux_selectors(const AUX_SELECT q_m.emplace_back(0); q_c.emplace_back(0); q_arith.emplace_back(1); - pad_additional_selectors(); + selectors.pad_additional(); break; } case AUX_SELECTORS::RAM_TIMESTAMP_CHECK: { @@ -1293,7 +1293,7 @@ void UltraCircuitBuilder_::apply_aux_selectors(const AUX_SELECT q_m.emplace_back(0); q_c.emplace_back(0); q_arith.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); break; } case AUX_SELECTORS::ROM_READ: { @@ -1307,7 +1307,7 @@ void UltraCircuitBuilder_::apply_aux_selectors(const AUX_SELECT q_m.emplace_back(1); // validate record witness is correctly computed q_c.emplace_back(0); // read/write flag stored in q_c q_arith.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); break; } case AUX_SELECTORS::RAM_READ: { @@ -1321,7 +1321,7 @@ void UltraCircuitBuilder_::apply_aux_selectors(const AUX_SELECT q_m.emplace_back(1); // validate record witness is correctly computed q_c.emplace_back(0); // read/write flag stored in q_c q_arith.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); break; } case AUX_SELECTORS::RAM_WRITE: { @@ -1335,7 +1335,7 @@ void UltraCircuitBuilder_::apply_aux_selectors(const AUX_SELECT q_m.emplace_back(1); // validate record witness is correctly computed q_c.emplace_back(1); // read/write flag stored in q_c q_arith.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); break; } default: { @@ -1346,7 +1346,7 @@ void UltraCircuitBuilder_::apply_aux_selectors(const AUX_SELECT q_m.emplace_back(0); q_c.emplace_back(0); q_arith.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); break; } } @@ -1875,7 +1875,7 @@ std::array UltraCircuitBuilder_::evaluate_non_nati q_lookup_type.emplace_back(0); q_elliptic.emplace_back(0); q_aux.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); } this->num_gates += 4; @@ -1997,7 +1997,7 @@ std::array UltraCircuitBuilder_::evaluate_non_nati q_lookup_type.emplace_back(0); q_elliptic.emplace_back(0); q_aux.emplace_back(0); - pad_additional_selectors(); + selectors.pad_additional(); } this->num_gates += 4; diff --git a/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/ultra_circuit_builder.hpp b/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/ultra_circuit_builder.hpp index 3d36484a288a..ae3b25ab436e 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/ultra_circuit_builder.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/ultra_circuit_builder.hpp @@ -641,14 +641,6 @@ class UltraCircuitBuilder_ : public CircuitBuilderBase& in) override; void create_big_add_gate(const add_quad_& in, const bool use_next_gate_w_4 = false);