From b2a3972d59d243cd2c7e15d5b061a5ed82ad777a Mon Sep 17 00:00:00 2001 From: maramihali Date: Wed, 31 May 2023 14:12:42 +0000 Subject: [PATCH] wip --- .../standard_honk_composer_helper.cpp | 41 ++++++++++++------- .../standard_honk_composer_helper.hpp | 37 +++++++++-------- .../honk/composer/standard_honk_composer.hpp | 37 +++++++++-------- .../composer/standard_honk_composer.test.cpp | 9 ++++ .../barretenberg/honk/proof_system/prover.hpp | 2 - .../honk/proof_system/ultra_prover.hpp | 1 - .../proof_system/flavor/flavor.hpp | 5 +++ .../primitives/composers/composers_fwd.hpp | 6 +-- 8 files changed, 81 insertions(+), 57 deletions(-) diff --git a/cpp/src/barretenberg/honk/composer/composer_helper/standard_honk_composer_helper.cpp b/cpp/src/barretenberg/honk/composer/composer_helper/standard_honk_composer_helper.cpp index 681c54e56e..ce7f00ae9e 100644 --- a/cpp/src/barretenberg/honk/composer/composer_helper/standard_honk_composer_helper.cpp +++ b/cpp/src/barretenberg/honk/composer/composer_helper/standard_honk_composer_helper.cpp @@ -23,7 +23,8 @@ namespace proof_system::honk { * @param num_reserved_gates The number of reserved gates. * @return Pointer to the initialized proving key updated with selector polynomials. * */ -std::shared_ptr StandardHonkComposerHelper::compute_proving_key_base( +template +std::shared_ptr StandardHonkComposerHelper_::compute_proving_key_base( const CircuitConstructor& constructor, const size_t minimum_circuit_size, const size_t num_randomized_gates) { // Initialize proving_key @@ -42,12 +43,13 @@ std::shared_ptr StandardHonkComposerHelp * (2) sets the polynomial manifest using the data from proving key. */ -std::shared_ptr StandardHonkComposerHelper::compute_verification_key_base( - std::shared_ptr const& proving_key) +template +std::shared_ptr StandardHonkComposerHelper_::compute_verification_key_base( + std::shared_ptr const& proving_key) { auto key = std::make_shared( proving_key->circuit_size, proving_key->num_public_inputs, proving_key->composer_type); - auto commitment_key = PCSParams::CommitmentKey(proving_key->circuit_size, "../srs_db/ignition"); + auto commitment_key = typename PCSParams::CommitmentKey(proving_key->circuit_size, "../srs_db/ignition"); // Compute and store commitments to all precomputed polynomials key->q_m = commitment_key.commit(proving_key->q_m); @@ -76,8 +78,9 @@ std::shared_ptr StandardHonkCompose * * @tparam Program settings needed to establish if w_4 is being used. * */ -void StandardHonkComposerHelper::compute_witness(const CircuitConstructor& circuit_constructor, - const size_t minimum_circuit_size) +template +void StandardHonkComposerHelper_::compute_witness(const CircuitConstructor& circuit_constructor, + const size_t minimum_circuit_size) { if (computed_witness) { return; @@ -99,14 +102,15 @@ void StandardHonkComposerHelper::compute_witness(const CircuitConstructor& circu * @return Proving key with saved computed polynomials. * */ -std::shared_ptr StandardHonkComposerHelper::compute_proving_key( +template +std::shared_ptr StandardHonkComposerHelper_::compute_proving_key( const CircuitConstructor& circuit_constructor) { if (proving_key) { return proving_key; } // Compute q_l, q_r, q_o, etc polynomials - StandardHonkComposerHelper::compute_proving_key_base( + StandardHonkComposerHelper_::compute_proving_key_base( circuit_constructor, /*minimum_circuit_size=*/0, NUM_RESERVED_GATES); // Compute sigma polynomials (we should update that late) @@ -123,7 +127,8 @@ std::shared_ptr StandardHonkComposerHelp * * @return Pointer to created circuit verification key. * */ -std::shared_ptr StandardHonkComposerHelper::compute_verification_key( +template +std::shared_ptr StandardHonkComposerHelper_::compute_verification_key( const CircuitConstructor& circuit_constructor) { if (verification_key) { @@ -133,36 +138,42 @@ std::shared_ptr StandardHonkCompose compute_proving_key(circuit_constructor); } - verification_key = StandardHonkComposerHelper::compute_verification_key_base(proving_key); + verification_key = StandardHonkComposerHelper_::compute_verification_key_base(proving_key); verification_key->composer_type = proving_key->composer_type; return verification_key; } -StandardVerifier StandardHonkComposerHelper::create_verifier(const CircuitConstructor& circuit_constructor) +template +StandardVerifier_ StandardHonkComposerHelper_::create_verifier( + const CircuitConstructor& circuit_constructor) { compute_verification_key(circuit_constructor); StandardVerifier output_state(verification_key); auto pcs_verification_key = - std::make_unique(verification_key->circuit_size, "../srs_db/ignition"); + std::make_unique(verification_key->circuit_size, "../srs_db/ignition"); output_state.pcs_verification_key = std::move(pcs_verification_key); return output_state; } -StandardProver StandardHonkComposerHelper::create_prover(const CircuitConstructor& circuit_constructor) +template +StandardProver_ StandardHonkComposerHelper_::create_prover( + const CircuitConstructor& circuit_constructor) { compute_proving_key(circuit_constructor); compute_witness(circuit_constructor); StandardProver output_state(proving_key); auto pcs_commitment_key = - std::make_unique(proving_key->circuit_size, "../srs_db/ignition"); + std::make_unique(proving_key->circuit_size, "../srs_db/ignition"); output_state.pcs_commitment_key = std::move(pcs_commitment_key); return output_state; } -} // namespace proof_system::honk +template class StandardHonkComposerHelper_; + +} // namespace proof_system::honk \ No newline at end of file diff --git a/cpp/src/barretenberg/honk/composer/composer_helper/standard_honk_composer_helper.hpp b/cpp/src/barretenberg/honk/composer/composer_helper/standard_honk_composer_helper.hpp index 14bd534db0..936b1ffbf7 100644 --- a/cpp/src/barretenberg/honk/composer/composer_helper/standard_honk_composer_helper.hpp +++ b/cpp/src/barretenberg/honk/composer/composer_helper/standard_honk_composer_helper.hpp @@ -12,13 +12,12 @@ #include "barretenberg/honk/flavor/standard.hpp" namespace proof_system::honk { -class StandardHonkComposerHelper { +template class StandardHonkComposerHelper_ { public: - using Flavor = flavor::Standard; - using PCSParams = Flavor::PCSParams; - using CircuitConstructor = Flavor::CircuitConstructor; - using ProvingKey = Flavor::ProvingKey; - using VerificationKey = Flavor::VerificationKey; + using PCSParams = typename Flavor::PCSParams; + using CircuitConstructor = typename Flavor::CircuitConstructor; + using ProvingKey = typename Flavor::ProvingKey; + using VerificationKey = typename Flavor::VerificationKey; static constexpr size_t NUM_RESERVED_GATES = 2; // equal to the number of multilinear evaluations leaked static constexpr size_t NUM_WIRES = CircuitConstructor::NUM_WIRES; @@ -29,33 +28,33 @@ class StandardHonkComposerHelper { std::shared_ptr crs_factory_; bool computed_witness = false; // TODO(Luke): use make_shared - StandardHonkComposerHelper() - : StandardHonkComposerHelper(std::shared_ptr( + StandardHonkComposerHelper_() + : StandardHonkComposerHelper_(std::shared_ptr( new proof_system::FileReferenceStringFactory("../srs_db/ignition"))) {} - StandardHonkComposerHelper(std::shared_ptr crs_factory) + StandardHonkComposerHelper_(std::shared_ptr crs_factory) : crs_factory_(std::move(crs_factory)) {} - StandardHonkComposerHelper(std::unique_ptr&& crs_factory) + StandardHonkComposerHelper_(std::unique_ptr&& crs_factory) : crs_factory_(std::move(crs_factory)) {} - StandardHonkComposerHelper(std::shared_ptr p_key, std::shared_ptr v_key) + StandardHonkComposerHelper_(std::shared_ptr p_key, std::shared_ptr v_key) : proving_key(std::move(p_key)) , verification_key(std::move(v_key)) {} - StandardHonkComposerHelper(StandardHonkComposerHelper&& other) noexcept = default; - StandardHonkComposerHelper(const StandardHonkComposerHelper& other) = delete; - StandardHonkComposerHelper& operator=(StandardHonkComposerHelper&& other) noexcept = default; - StandardHonkComposerHelper& operator=(const StandardHonkComposerHelper& other) = delete; - ~StandardHonkComposerHelper() = default; + StandardHonkComposerHelper_(StandardHonkComposerHelper_&& other) noexcept = default; + StandardHonkComposerHelper_(const StandardHonkComposerHelper_& other) = delete; + StandardHonkComposerHelper_& operator=(StandardHonkComposerHelper_&& other) noexcept = default; + StandardHonkComposerHelper_& operator=(const StandardHonkComposerHelper_& other) = delete; + ~StandardHonkComposerHelper_() = default; std::shared_ptr compute_proving_key(const CircuitConstructor& circuit_constructor); std::shared_ptr compute_verification_key(const CircuitConstructor& circuit_constructor); - StandardVerifier create_verifier(const CircuitConstructor& circuit_constructor); + StandardVerifier_ create_verifier(const CircuitConstructor& circuit_constructor); - StandardProver create_prover(const CircuitConstructor& circuit_constructor); + StandardProver_ create_prover(const CircuitConstructor& circuit_constructor); // TODO(#216)(Adrian): Seems error prone to provide the number of randomized gates std::shared_ptr compute_proving_key_base(const CircuitConstructor& circuit_constructor, @@ -68,5 +67,7 @@ class StandardHonkComposerHelper { void compute_witness(const CircuitConstructor& circuit_constructor, const size_t minimum_circuit_size = 0); }; +extern template class StandardHonkComposerHelper_; +using StandardHonkComposerHelper = StandardHonkComposerHelper_; } // namespace proof_system::honk diff --git a/cpp/src/barretenberg/honk/composer/standard_honk_composer.hpp b/cpp/src/barretenberg/honk/composer/standard_honk_composer.hpp index ea60cb6a2a..2666cd276f 100644 --- a/cpp/src/barretenberg/honk/composer/standard_honk_composer.hpp +++ b/cpp/src/barretenberg/honk/composer/standard_honk_composer.hpp @@ -13,13 +13,12 @@ namespace proof_system::honk { * @details However, it has a lot of its logic separated into subclasses and simply proxies the calls. * */ -class StandardHonkComposer { +template class StandardHonkComposer_ { public: // TODO(#426): This doesn't belong here static constexpr merkle::HashType merkle_hash_type = merkle::HashType::LOOKUP_PEDERSEN; static constexpr pedersen::CommitmentType commitment_type = pedersen::CommitmentType::FIXED_BASE_PEDERSEN; - using Flavor = flavor::Standard; using CircuitConstructor = StandardCircuitConstructor; using ProvingKey = typename Flavor::ProvingKey; using VerificationKey = typename Flavor::VerificationKey; @@ -32,7 +31,7 @@ class StandardHonkComposer { // 1) Proving and verification keys // 2) CRS // 3) Converting variables to witness vectors/polynomials - StandardHonkComposerHelper composer_helper; + StandardHonkComposerHelper_ composer_helper; // Leaving it in for now just in case bool contains_recursive_proof = false; @@ -40,24 +39,24 @@ class StandardHonkComposer { /**Standard methods*/ - StandardHonkComposer(const size_t size_hint = 0) + StandardHonkComposer_(const size_t size_hint = 0) : circuit_constructor(size_hint) , num_gates(circuit_constructor.num_gates) , variables(circuit_constructor.variables){}; - StandardHonkComposer(std::string const& crs_path, const size_t size_hint = 0) - : StandardHonkComposer( + StandardHonkComposer_(std::string const& crs_path, const size_t size_hint = 0) + : StandardHonkComposer_( std::unique_ptr(new proof_system::FileReferenceStringFactory(crs_path)), size_hint){}; - StandardHonkComposer(std::shared_ptr const& crs_factory, const size_t size_hint = 0) + StandardHonkComposer_(std::shared_ptr const& crs_factory, const size_t size_hint = 0) : circuit_constructor(size_hint) , composer_helper(crs_factory) , num_gates(circuit_constructor.num_gates) , variables(circuit_constructor.variables) {} - StandardHonkComposer(std::unique_ptr&& crs_factory, const size_t size_hint = 0) + StandardHonkComposer_(std::unique_ptr&& crs_factory, const size_t size_hint = 0) : circuit_constructor(size_hint) , composer_helper(std::move(crs_factory)) , num_gates(circuit_constructor.num_gates) @@ -65,22 +64,22 @@ class StandardHonkComposer { {} - StandardHonkComposer(std::shared_ptr const& p_key, - std::shared_ptr const& v_key, - size_t size_hint = 0) + StandardHonkComposer_(std::shared_ptr const& p_key, + std::shared_ptr const& v_key, + size_t size_hint = 0) : circuit_constructor(size_hint) , composer_helper(p_key, v_key) , num_gates(circuit_constructor.num_gates) , variables(circuit_constructor.variables) {} - StandardHonkComposer(const StandardHonkComposer& other) = delete; - StandardHonkComposer(StandardHonkComposer&& other) = default; - StandardHonkComposer& operator=(const StandardHonkComposer& other) = delete; + StandardHonkComposer_(const StandardHonkComposer_& other) = delete; + StandardHonkComposer_(StandardHonkComposer_&& other) = default; + StandardHonkComposer_& operator=(const StandardHonkComposer_& other) = delete; // TODO(#230)(Cody): This constructor started to be implicitly deleted when I added `n` and `variables` members. // This is a temporary measure until we can rewrite Plonk and all tests using circuit builder methods in place of - // composer methods, where appropriate. StandardHonkComposer& operator=(StandardHonkComposer&& other) = default; - ~StandardHonkComposer() = default; + // composer methods, where appropriate. StandardHonkComposer_& operator=(StandardHonkComposer_&& other) = default; + ~StandardHonkComposer_() = default; size_t get_num_gates() const { return circuit_constructor.get_num_gates(); } @@ -183,8 +182,8 @@ class StandardHonkComposer { void compute_witness() { composer_helper.compute_witness(circuit_constructor); }; - StandardVerifier create_verifier() { return composer_helper.create_verifier(circuit_constructor); } - StandardProver create_prover() { return composer_helper.create_prover(circuit_constructor); }; + StandardVerifier_ create_verifier() { return composer_helper.create_verifier(circuit_constructor); } + StandardProver_ create_prover() { return composer_helper.create_prover(circuit_constructor); }; size_t& num_gates; std::vector& variables; @@ -192,4 +191,6 @@ class StandardHonkComposer { const std::string& err() const { return circuit_constructor.err(); }; void failure(std::string msg) { circuit_constructor.failure(msg); } }; +template class StandardHonkComposer_; +using StandardHonkComposer = StandardHonkComposer_; } // namespace proof_system::honk diff --git a/cpp/src/barretenberg/honk/composer/standard_honk_composer.test.cpp b/cpp/src/barretenberg/honk/composer/standard_honk_composer.test.cpp index a13e0df167..e2f3c6777f 100644 --- a/cpp/src/barretenberg/honk/composer/standard_honk_composer.test.cpp +++ b/cpp/src/barretenberg/honk/composer/standard_honk_composer.test.cpp @@ -27,6 +27,7 @@ namespace test_standard_honk_composer { */ TEST(StandardHonkComposer, SigmaIDCorrectness) { + using StandardHonkComposer = StandardHonkComposer_; auto test_permutation = [](StandardHonkComposer& composer) { auto proving_key = composer.compute_proving_key(); const auto n = proving_key->circuit_size; @@ -140,6 +141,7 @@ TEST(StandardHonkComposer, SigmaIDCorrectness) */ TEST(StandardHonkComposer, LagrangeCorrectness) { + using StandardHonkComposer = StandardHonkComposer_; // Create a composer and a dummy circuit with a few gates StandardHonkComposer composer = StandardHonkComposer(); fr a = fr::one(); @@ -189,6 +191,8 @@ TEST(StandardHonkComposer, LagrangeCorrectness) */ TEST(StandardHonkComposer, AssertEquals) { + using StandardHonkComposer = StandardHonkComposer_; + /** * @brief A function that creates a simple circuit with repeated gates, leading to large permutation cycles * @@ -283,6 +287,8 @@ TEST(StandardHonkComposer, AssertEquals) TEST(StandardHonkComposer, VerificationKeyCreation) { + using StandardHonkComposer = StandardHonkComposer_; + // Create a composer and a dummy circuit with a few gates StandardHonkComposer composer = StandardHonkComposer(); fr a = fr::one(); @@ -306,6 +312,7 @@ TEST(StandardHonkComposer, VerificationKeyCreation) TEST(StandardHonkComposer, BaseCase) { + using StandardHonkComposer = StandardHonkComposer_; auto composer = StandardHonkComposer(); fr a = 1; composer.circuit_constructor.add_variable(a); @@ -319,6 +326,7 @@ TEST(StandardHonkComposer, BaseCase) TEST(StandardHonkComposer, TwoGates) { + using StandardHonkComposer = StandardHonkComposer_; auto run_test = [](bool expect_verified) { auto composer = StandardHonkComposer(); @@ -353,6 +361,7 @@ TEST(StandardHonkComposer, TwoGates) TEST(StandardHonkComposer, SumcheckEvaluations) { + using StandardHonkComposer = StandardHonkComposer_; auto run_test = [](bool expected_result) { auto composer = StandardHonkComposer(); fr a = fr::one(); diff --git a/cpp/src/barretenberg/honk/proof_system/prover.hpp b/cpp/src/barretenberg/honk/proof_system/prover.hpp index 80b577c767..e4abda3a8e 100644 --- a/cpp/src/barretenberg/honk/proof_system/prover.hpp +++ b/cpp/src/barretenberg/honk/proof_system/prover.hpp @@ -14,8 +14,6 @@ namespace proof_system::honk { // We won't compile this class with honk::flavor::Ultra, but we will like want to compile it (at least for testing) // with a flavor that uses the curve Grumpkin, or a flavor that does/does not have zk, etc. -template concept StandardFlavor = IsAnyOf; - template class StandardProver_ { using FF = typename Flavor::FF; diff --git a/cpp/src/barretenberg/honk/proof_system/ultra_prover.hpp b/cpp/src/barretenberg/honk/proof_system/ultra_prover.hpp index f225e831f1..c2635fcd23 100644 --- a/cpp/src/barretenberg/honk/proof_system/ultra_prover.hpp +++ b/cpp/src/barretenberg/honk/proof_system/ultra_prover.hpp @@ -12,7 +12,6 @@ namespace proof_system::honk { // We won't compile this class with honk::flavor::Standard, but we will like want to compile it (at least for testing) // with a flavor that uses the curve Grumpkin, or a flavor that does/does not have zk, etc. -template concept UltraFlavor = IsAnyOf; template class UltraProver_ { using FF = typename Flavor::FF; diff --git a/cpp/src/barretenberg/proof_system/flavor/flavor.hpp b/cpp/src/barretenberg/proof_system/flavor/flavor.hpp index a476c362ed..686b15ca26 100644 --- a/cpp/src/barretenberg/proof_system/flavor/flavor.hpp +++ b/cpp/src/barretenberg/proof_system/flavor/flavor.hpp @@ -324,5 +324,10 @@ concept IsPlonkFlavor = IsAnyOf concept IsHonkFlavor = IsAnyOf; + +template concept StandardFlavor = IsAnyOf; + +template concept UltraFlavor = IsAnyOf; + // clang-format on } // namespace proof_system diff --git a/cpp/src/barretenberg/stdlib/primitives/composers/composers_fwd.hpp b/cpp/src/barretenberg/stdlib/primitives/composers/composers_fwd.hpp index 692e228883..b390b57bb8 100644 --- a/cpp/src/barretenberg/stdlib/primitives/composers/composers_fwd.hpp +++ b/cpp/src/barretenberg/stdlib/primitives/composers/composers_fwd.hpp @@ -8,9 +8,9 @@ class UltraPlonkComposer; class StandardPlonkComposer; } // namespace proof_system::plonk -namespace proof_system::honk { -class StandardHonkComposer; -} // namespace proof_system::honk +// namespace proof_system::honk { +// using StandardHonkComposer = StandardHonkComposer_; +// } // namespace proof_system::honk namespace proof_system { class StandardCircuitConstructor; class TurboCircuitConstructor;