From 602f5a6f0a020b949760ad8105c5825a77a84573 Mon Sep 17 00:00:00 2001 From: codygunton Date: Thu, 6 Apr 2023 21:45:49 +0000 Subject: [PATCH] WIP Instantiate passes. --- .../standard_honk_composer_helper.cpp | 12 ++--- .../standard_honk_composer_helper.hpp | 11 ++-- .../honk/composer/standard_honk_composer.hpp | 14 ++--- .../standard_circuit_constructor.hpp | 1 - .../composer/composer_helper_lib.hpp | 15 +++--- .../composer/composer_helper_lib.test.cpp | 53 +++++++++++++++++++ .../proof_system/flavor/flavor.hpp | 5 +- 7 files changed, 83 insertions(+), 28 deletions(-) create mode 100644 cpp/src/barretenberg/proof_system/composer/composer_helper_lib.test.cpp 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 2bbf1c596d..f7f52720e7 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 @@ -1,6 +1,7 @@ #include "standard_honk_composer_helper.hpp" #include "barretenberg/polynomials/polynomial.hpp" #include "barretenberg/honk/flavor/flavor.hpp" +#include "barretenberg/proof_system/flavor/flavor.hpp" #include "barretenberg/honk/pcs/commitment_key.hpp" #include "barretenberg/numeric/bitop/get_msb.hpp" @@ -22,13 +23,12 @@ namespace proof_system::honk { * @param num_reserved_gates The number of reserved gates. * @return Pointer to the initialized proving key updated with selector polynomials. * */ -template -std::shared_ptr StandardHonkComposerHelper::compute_proving_key_base( +std::shared_ptr StandardHonkComposerHelper::compute_proving_key_base( const CircuitConstructor& constructor, const size_t minimum_circuit_size, const size_t num_randomized_gates) { // Initialize circuit_proving_key // TODO(#229)(Kesha): replace composer types. - circuit_proving_key = initialize_proving_key( + circuit_proving_key = initialize_proving_key( constructor, crs_factory_.get(), minimum_circuit_size, num_randomized_gates, ComposerType::STANDARD_HONK); // Compute lagrange selectors construct_lagrange_selector_forms(constructor, circuit_proving_key.get()); @@ -155,17 +155,17 @@ StandardVerifier StandardHonkComposerHelper::create_verifier return output_state; } -template +// template template // TODO(Cody): this file should be generic with regard to flavor/arithmetization/whatever. -StandardProver StandardHonkComposerHelper::create_prover( +StandardProver StandardHonkComposerHelper::create_prover( const CircuitConstructor& circuit_constructor) { compute_proving_key(circuit_constructor); compute_witness(circuit_constructor); size_t num_sumcheck_rounds(circuit_proving_key->log_circuit_size); - auto manifest = Flavor::create_manifest(circuit_constructor.public_inputs.size(), num_sumcheck_rounds); + // auto manifest = Flavor::create_manifest(circuit_constructor.public_inputs.size(), num_sumcheck_rounds); StandardProver output_state(std::move(wire_polynomials), circuit_proving_key); // TODO(Cody): This should be more generic 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 5b033996d5..8ff6cc39a6 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,14 +12,15 @@ #include "barretenberg/proof_system/arithmetization/gate_data.hpp" #include "barretenberg/proof_system/composer/composer_helper_lib.hpp" #include "barretenberg/proof_system/composer/permutation_helper.hpp" - +#include "barretenberg/proof_system/flavor/flavor.hpp" #include namespace proof_system::honk { -// TODO(Kesha): change initializations to specify this parameter -// Cody: What does this mean? -template class StandardHonkComposerHelper { +class StandardHonkComposerHelper { public: + using Flavor = flavor::Standard; + using CircuitConstructor = typename Flavor::CircuitConstructor; + static constexpr size_t NUM_RANDOMIZED_GATES = 2; // equal to the number of multilinear evaluations leaked static constexpr size_t num_wires = CircuitConstructor::num_wires; std::shared_ptr circuit_proving_key; @@ -57,7 +58,7 @@ template class StandardHonkComposerHelper { StandardVerifier create_verifier(const CircuitConstructor& circuit_constructor); - template 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 // Cody: Where should this go? In the flavor (or whatever that becomes)? diff --git a/cpp/src/barretenberg/honk/composer/standard_honk_composer.hpp b/cpp/src/barretenberg/honk/composer/standard_honk_composer.hpp index d99033e67d..870ab5d219 100644 --- a/cpp/src/barretenberg/honk/composer/standard_honk_composer.hpp +++ b/cpp/src/barretenberg/honk/composer/standard_honk_composer.hpp @@ -17,22 +17,22 @@ namespace proof_system::honk { */ class StandardHonkComposer { public: - static constexpr ComposerType type = ComposerType::STANDARD_HONK; - 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; + static constexpr ComposerType type = ComposerType::STANDARD_HONK; // TODO(Cody): Get rid of this. static constexpr size_t UINT_LOG2_BASE = 2; // An instantiation of the circuit constructor that only depends on arithmetization, not on the proof system - StandardCircuitConstructor circuit_constructor; + CircuitConstructor circuit_constructor; // Composer helper contains all proof-related material that is separate from circuit creation such as: // 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; - static constexpr size_t num_wires = StandardCircuitConstructor::num_wires; + static constexpr size_t num_wires = CircuitConstructor::num_wires; /**Standard methods*/ @@ -180,7 +180,7 @@ 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); }; + StandardProver create_prover() { return composer_helper.create_prover(circuit_constructor); }; size_t& num_gates; std::vector& variables; diff --git a/cpp/src/barretenberg/proof_system/circuit_constructors/standard_circuit_constructor.hpp b/cpp/src/barretenberg/proof_system/circuit_constructors/standard_circuit_constructor.hpp index b627b17a9b..421d150e08 100644 --- a/cpp/src/barretenberg/proof_system/circuit_constructors/standard_circuit_constructor.hpp +++ b/cpp/src/barretenberg/proof_system/circuit_constructors/standard_circuit_constructor.hpp @@ -1,7 +1,6 @@ #pragma once #include #include "circuit_constructor_base.hpp" -#include "barretenberg/proof_system/flavor/flavor.hpp" #include "barretenberg/proof_system/types/composer_type.hpp" namespace proof_system { diff --git a/cpp/src/barretenberg/proof_system/composer/composer_helper_lib.hpp b/cpp/src/barretenberg/proof_system/composer/composer_helper_lib.hpp index c31dfe13b9..7a07fcaa93 100644 --- a/cpp/src/barretenberg/proof_system/composer/composer_helper_lib.hpp +++ b/cpp/src/barretenberg/proof_system/composer/composer_helper_lib.hpp @@ -14,12 +14,13 @@ namespace proof_system { * @param composer_type The type of composer we are using * @return std::shared_ptr */ -template -std::shared_ptr initialize_proving_key(const CircuitConstructor& circuit_constructor, - ReferenceStringFactory* crs_factory, - const size_t minimum_circuit_size, - const size_t num_randomized_gates, - ComposerType composer_type) +template +std::shared_ptr initialize_proving_key( + const typename Flavor::CircuitConstructor& circuit_constructor, + ReferenceStringFactory* crs_factory, + const size_t minimum_circuit_size, + const size_t num_randomized_gates, + ComposerType composer_type) { const size_t num_gates = circuit_constructor.num_gates; std::span public_inputs = circuit_constructor.public_inputs; @@ -32,7 +33,7 @@ std::shared_ptr initialize_proving_key(const CircuitConstruc auto crs = crs_factory->get_prover_crs(subgroup_size + 1); - return std::make_shared(subgroup_size, num_public_inputs, crs, composer_type); + return std::make_shared(subgroup_size, num_public_inputs, crs, composer_type); } /** diff --git a/cpp/src/barretenberg/proof_system/composer/composer_helper_lib.test.cpp b/cpp/src/barretenberg/proof_system/composer/composer_helper_lib.test.cpp new file mode 100644 index 0000000000..77a78e4c40 --- /dev/null +++ b/cpp/src/barretenberg/proof_system/composer/composer_helper_lib.test.cpp @@ -0,0 +1,53 @@ +#include +#include "barretenberg/proof_system/composer/composer_helper_lib.hpp" +#include "barretenberg/proof_system/types/composer_type.hpp" + +namespace proof_system::test_composer_lib { + +struct MockCircuitConstructor { + static constexpr size_t num_gates = 8; + static size_t get_circuit_subgroup_size(const size_t in) { return in; }; + const std::vector _public_inputs{ 0, 1, 2, 3 }; + const std::span public_inputs{ _public_inputs }; +}; + +struct MockFlavor { + using CircuitConstructor = MockCircuitConstructor; + + struct ProvingKey { + const size_t num_gates; + const size_t num_inputs; + std::shared_ptr crs; + const ComposerType type; + ProvingKey(const size_t num_gates, + const size_t num_inputs, + std::shared_ptr const& crs, + ComposerType type) + : num_gates(num_gates) + , num_inputs(num_inputs) + , crs(crs) + , type(type) + {} + }; +}; + +using Flavor = MockFlavor; + +TEST(ComposerLib, Instantiate) +{ + typename Flavor::CircuitConstructor circuit_constructor; + EXPECT_EQ(circuit_constructor.get_circuit_subgroup_size(15), 15); + + // Flavor flavor; + + ReferenceStringFactory crs_factory; + + auto pk = initialize_proving_key(circuit_constructor, + &crs_factory, + /*minimum_circuit_size=*/2, + /*num_randomized_gates=*/2, + ComposerType::STANDARD); + EXPECT_EQ(pk->num_gates, 14); + EXPECT_EQ(pk->num_inputs, 4); +} +} // namespace proof_system::test_composer_lib diff --git a/cpp/src/barretenberg/proof_system/flavor/flavor.hpp b/cpp/src/barretenberg/proof_system/flavor/flavor.hpp index 8b4b98f5a6..66f8c9d776 100644 --- a/cpp/src/barretenberg/proof_system/flavor/flavor.hpp +++ b/cpp/src/barretenberg/proof_system/flavor/flavor.hpp @@ -6,6 +6,7 @@ #include "barretenberg/honk/sumcheck/polynomials/univariate.hpp" #include "barretenberg/ecc/curves/bn254/g1.hpp" #include "barretenberg/polynomials/polynomial.hpp" +#include "barretenberg/proof_system/circuit_constructors/standard_circuit_constructor.hpp" // could be shared, but will it? namespace proof_system::honk::flavor { @@ -25,10 +26,10 @@ template class Data { }; class Standard { + public: + using CircuitConstructor = proof_system::StandardCircuitConstructor; static constexpr size_t NUM_ALL_ENTITIES = 18; static constexpr size_t NUM_PRECOMPUTED_ENTITIES = 13; - - public: using FF = barretenberg::fr; using Polynomial = barretenberg::Polynomial; using PolynomialView = std::span;