Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(honk): template honk composers by flavor #488

Merged
merged 10 commits into from
Jun 12, 2023
Merged
5 changes: 2 additions & 3 deletions cpp/src/barretenberg/benchmark/honk_bench/honk.bench.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#include "barretenberg/ecc/curves/bn254/fr.hpp"
#include "barretenberg/honk/proof_system/prover.hpp"
#include "barretenberg/honk/proof_system/verifier.hpp"
#include <benchmark/benchmark.h>
#include <cstddef>
#include "barretenberg/honk/composer/standard_honk_composer.hpp"
#include "barretenberg/stdlib/primitives/field/field.hpp"
#include "barretenberg/stdlib/primitives/composers/composers.hpp"
#include "barretenberg/stdlib/primitives/composers/composers_fwd.hpp"

using namespace benchmark;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#include "barretenberg/crypto/ecdsa/ecdsa.hpp"
#include "barretenberg/ecc/curves/bn254/fr.hpp"
#include "barretenberg/honk/proof_system/ultra_prover.hpp"
#include "barretenberg/honk/proof_system/ultra_verifier.hpp"
#include <benchmark/benchmark.h>
#include <cstddef>
#include "barretenberg/honk/composer/ultra_honk_composer.hpp"
#include "barretenberg/stdlib/primitives/composers/composers_fwd.hpp"
#include "barretenberg/stdlib/primitives/composers/composers.hpp"
#include "barretenberg/stdlib/encryption/ecdsa/ecdsa.hpp"
#include "barretenberg/stdlib/hash/keccak/keccak.hpp"
#include "barretenberg/stdlib/primitives/curves/secp256k1.hpp"
Expand Down
1 change: 1 addition & 0 deletions cpp/src/barretenberg/dsl/acir_format/acir_format.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "acir_format.hpp"
#include "barretenberg/common/log.hpp"
#include "barretenberg/stdlib/primitives/composers/composers.hpp"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this suddenly needed?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and elsewhere, we'd rather be more specific than include this one header because it's implicitly including a lot of other stuff: all composers and circuit constructors.


namespace acir_format {

Expand Down
1 change: 1 addition & 0 deletions cpp/src/barretenberg/dsl/acir_format/acir_format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "pedersen.hpp"
#include "hash_to_field.hpp"
#include "barretenberg/dsl/types.hpp"
#include "barretenberg/stdlib/primitives/composers/composers_fwd.hpp"

namespace acir_format {

Expand Down
1 change: 1 addition & 0 deletions cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "barretenberg/common/streams.hpp"
#include "barretenberg/serialize/test_helper.hpp"
#include "ecdsa_secp256k1.hpp"
#include "barretenberg/stdlib/primitives/composers/composers.hpp"

TEST(acir_format, test_a_single_constraint_no_pub_inputs)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,10 @@ typename Curve::Element pippenger_without_endomorphism_basis_points(typename Cur

// Explicit instantiation
// BN254
template void generate_pippenger_point_table<curve::BN254>(curve::BN254::AffineElement* points,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It turned out to not be necessary to explicitly instantiate this function (with or without extern, for either curve).

curve::BN254::AffineElement* table,
size_t num_points);

template uint32_t construct_addition_chains<curve::BN254>(affine_product_runtime_state<curve::BN254>& state,
bool empty_bucket_counts = true);

Expand Down Expand Up @@ -1004,6 +1008,10 @@ template curve::BN254::Element pippenger_without_endomorphism_basis_points<curve
pippenger_runtime_state<curve::BN254>& state);

// Grumpkin
template void generate_pippenger_point_table<curve::Grumpkin>(curve::Grumpkin::AffineElement* points,
curve::Grumpkin::AffineElement* table,
size_t num_points);

template uint32_t construct_addition_chains<curve::Grumpkin>(affine_product_runtime_state<curve::Grumpkin>& state,
bool empty_bucket_counts = true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ typename Curve::Element pippenger_without_endomorphism_basis_points(typename Cur

// Explicit instantiation
// BN254

extern template void generate_pippenger_point_table<curve::BN254>(curve::BN254::AffineElement* points,
curve::BN254::AffineElement* table,
size_t num_points);

extern template uint32_t construct_addition_chains<curve::BN254>(affine_product_runtime_state<curve::BN254>& state,
bool empty_bucket_counts = true);

Expand Down Expand Up @@ -221,6 +226,10 @@ extern template curve::BN254::Element pippenger_without_endomorphism_basis_point

// Grumpkin

extern template void generate_pippenger_point_table<curve::Grumpkin>(curve::Grumpkin::AffineElement* points,
curve::Grumpkin::AffineElement* table,
size_t num_points);

extern template uint32_t construct_addition_chains<curve::Grumpkin>(
affine_product_runtime_state<curve::Grumpkin>& state, bool empty_bucket_counts = true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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::ProvingKey> StandardHonkComposerHelper::compute_proving_key_base(
template <StandardFlavor Flavor>
std::shared_ptr<typename Flavor::ProvingKey> StandardHonkComposerHelper_<Flavor>::compute_proving_key_base(
const CircuitConstructor& constructor, const size_t minimum_circuit_size, const size_t num_randomized_gates)
{
// Initialize proving_key
Expand All @@ -45,8 +46,9 @@ std::shared_ptr<StandardHonkComposerHelper::ProvingKey> StandardHonkComposerHelp
*
* @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 <StandardFlavor Flavor>
void StandardHonkComposerHelper_<Flavor>::compute_witness(const CircuitConstructor& circuit_constructor,
const size_t minimum_circuit_size)
{
if (computed_witness) {
return;
Expand All @@ -67,14 +69,16 @@ void StandardHonkComposerHelper::compute_witness(const CircuitConstructor& circu
*
* @return Proving key with saved computed polynomials.
* */
std::shared_ptr<StandardHonkComposerHelper::ProvingKey> StandardHonkComposerHelper::compute_proving_key(

template <StandardFlavor Flavor>
std::shared_ptr<typename Flavor::ProvingKey> StandardHonkComposerHelper_<Flavor>::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)
Expand All @@ -91,14 +95,15 @@ std::shared_ptr<StandardHonkComposerHelper::ProvingKey> StandardHonkComposerHelp
*
* @return Pointer to created circuit verification key.
* */
std::shared_ptr<StandardHonkComposerHelper::VerificationKey> StandardHonkComposerHelper::compute_verification_key(
template <StandardFlavor Flavor>
std::shared_ptr<typename Flavor::VerificationKey> StandardHonkComposerHelper_<Flavor>::compute_verification_key(
const CircuitConstructor& circuit_constructor)
{
if (verification_key) {
return verification_key;
}

verification_key = std::make_shared<VerificationKey>(
verification_key = std::make_shared<typename Flavor::VerificationKey>(
proving_key->circuit_size, proving_key->num_public_inputs, proving_key->composer_type);

// Compute and store commitments to all precomputed polynomials
Expand All @@ -121,28 +126,35 @@ std::shared_ptr<StandardHonkComposerHelper::VerificationKey> StandardHonkCompose
return verification_key;
}

StandardVerifier StandardHonkComposerHelper::create_verifier(const CircuitConstructor& circuit_constructor)
template <StandardFlavor Flavor>
StandardVerifier_<Flavor> StandardHonkComposerHelper_<Flavor>::create_verifier(
const CircuitConstructor& circuit_constructor)
{
compute_verification_key(circuit_constructor);
StandardVerifier output_state(verification_key);
StandardVerifier_<Flavor> output_state(verification_key);

auto pcs_verification_key =
std::make_unique<PCSParams::VerificationKey>(verification_key->circuit_size, crs_factory_);
std::make_unique<typename PCSParams::VerificationKey>(verification_key->circuit_size, crs_factory_);

output_state.pcs_verification_key = std::move(pcs_verification_key);

return output_state;
}

StandardProver StandardHonkComposerHelper::create_prover(const CircuitConstructor& circuit_constructor)
template <StandardFlavor Flavor>
StandardProver_<Flavor> StandardHonkComposerHelper_<Flavor>::create_prover(
const CircuitConstructor& circuit_constructor)
{
compute_proving_key(circuit_constructor);
compute_witness(circuit_constructor);

compute_commitment_key(proving_key->circuit_size, crs_factory_);

StandardProver output_state(proving_key, commitment_key);
StandardProver_<Flavor> output_state(proving_key, commitment_key);

return output_state;
}
} // namespace proof_system::honk
template class StandardHonkComposerHelper_<honk::flavor::Standard>;
template class StandardHonkComposerHelper_<honk::flavor::StandardGrumpkin>;

} // namespace proof_system::honk
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
#include "barretenberg/proof_system/composer/permutation_helper.hpp"

#include "barretenberg/honk/flavor/standard.hpp"
#include "barretenberg/honk/flavor/standard_grumpkin.hpp"

namespace proof_system::honk {
class StandardHonkComposerHelper {
template <StandardFlavor Flavor> class StandardHonkComposerHelper_ {
public:
using Flavor = flavor::Standard;
using PCSParams = Flavor::PCSParams;
using CircuitConstructor = Flavor::CircuitConstructor;
using ProvingKey = Flavor::ProvingKey;
using VerificationKey = Flavor::VerificationKey;
using PCSCommitmentKey = PCSParams::CommitmentKey;
using PCSParams = typename Flavor::PCSParams;
using CircuitConstructor = typename Flavor::CircuitConstructor;
using ProvingKey = typename Flavor::ProvingKey;
using VerificationKey = typename Flavor::VerificationKey;
using PCSCommitmentKey = typename PCSParams::CommitmentKey;

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;
Expand All @@ -34,33 +34,33 @@ class StandardHonkComposerHelper {

bool computed_witness = false;
// TODO(Luke): use make_shared
StandardHonkComposerHelper()
: StandardHonkComposerHelper(
StandardHonkComposerHelper_()
: StandardHonkComposerHelper_(
std::shared_ptr<srs::factories::CrsFactory>(new srs::factories::FileCrsFactory("../srs_db/ignition")))
{}
StandardHonkComposerHelper(std::shared_ptr<srs::factories::CrsFactory> crs_factory)
StandardHonkComposerHelper_(std::shared_ptr<srs::factories::CrsFactory> crs_factory)
: crs_factory_(std::move(crs_factory))
{}

StandardHonkComposerHelper(std::unique_ptr<srs::factories::CrsFactory>&& crs_factory)
StandardHonkComposerHelper_(std::unique_ptr<srs::factories::CrsFactory>&& crs_factory)
: crs_factory_(std::move(crs_factory))
{}
StandardHonkComposerHelper(std::shared_ptr<ProvingKey> p_key, std::shared_ptr<VerificationKey> v_key)
StandardHonkComposerHelper_(std::shared_ptr<ProvingKey> p_key, std::shared_ptr<VerificationKey> 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<ProvingKey> compute_proving_key(const CircuitConstructor& circuit_constructor);
std::shared_ptr<VerificationKey> compute_verification_key(const CircuitConstructor& circuit_constructor);

StandardVerifier create_verifier(const CircuitConstructor& circuit_constructor);
StandardVerifier_<Flavor> create_verifier(const CircuitConstructor& circuit_constructor);

StandardProver create_prover(const CircuitConstructor& circuit_constructor);
StandardProver_<Flavor> create_prover(const CircuitConstructor& circuit_constructor);

// TODO(#216)(Adrian): Seems error prone to provide the number of randomized gates
std::shared_ptr<ProvingKey> compute_proving_key_base(const CircuitConstructor& circuit_constructor,
Expand All @@ -71,8 +71,11 @@ class StandardHonkComposerHelper {

void compute_commitment_key(size_t circuit_size, std::shared_ptr<srs::factories::CrsFactory> crs_factory)
{
commitment_key = std::make_shared<PCSParams::CommitmentKey>(circuit_size, crs_factory_);
commitment_key = std::make_shared<typename PCSParams::CommitmentKey>(circuit_size, crs_factory_);
};
};
extern template class StandardHonkComposerHelper_<honk::flavor::Standard>;
extern template class StandardHonkComposerHelper_<honk::flavor::StandardGrumpkin>;
using StandardHonkComposerHelper = StandardHonkComposerHelper_<honk::flavor::Standard>;

} // namespace proof_system::honk
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ namespace proof_system::honk {
* @brief Compute witness polynomials
*
*/
void UltraHonkComposerHelper::compute_witness(CircuitConstructor& circuit_constructor)
template <UltraFlavor Flavor>
void UltraHonkComposerHelper_<Flavor>::compute_witness(CircuitConstructor& circuit_constructor)
{
if (computed_witness) {
return;
Expand Down Expand Up @@ -155,7 +156,8 @@ void UltraHonkComposerHelper::compute_witness(CircuitConstructor& circuit_constr
computed_witness = true;
}

UltraProver UltraHonkComposerHelper::create_prover(CircuitConstructor& circuit_constructor)
template <UltraFlavor Flavor>
UltraProver_<Flavor> UltraHonkComposerHelper_<Flavor>::create_prover(CircuitConstructor& circuit_constructor)
{
finalize_circuit(circuit_constructor);

Expand All @@ -164,7 +166,7 @@ UltraProver UltraHonkComposerHelper::create_prover(CircuitConstructor& circuit_c

compute_commitment_key(proving_key->circuit_size, crs_factory_);

UltraProver output_state(proving_key, commitment_key);
UltraProver_<Flavor> output_state(proving_key, commitment_key);

return output_state;
}
Expand All @@ -175,11 +177,12 @@ UltraProver UltraHonkComposerHelper::create_prover(CircuitConstructor& circuit_c
*
* @return The verifier.
* */
UltraVerifier UltraHonkComposerHelper::create_verifier(const CircuitConstructor& circuit_constructor)
template <UltraFlavor Flavor>
UltraVerifier_<Flavor> UltraHonkComposerHelper_<Flavor>::create_verifier(const CircuitConstructor& circuit_constructor)
{
auto verification_key = compute_verification_key(circuit_constructor);

UltraVerifier output_state(verification_key);
UltraVerifier_<Flavor> output_state(verification_key);

auto pcs_verification_key = std::make_unique<PCSVerificationKey>(verification_key->circuit_size, crs_factory_);

Expand All @@ -188,7 +191,8 @@ UltraVerifier UltraHonkComposerHelper::create_verifier(const CircuitConstructor&
return output_state;
}

std::shared_ptr<UltraHonkComposerHelper::Flavor::ProvingKey> UltraHonkComposerHelper::compute_proving_key(
template <UltraFlavor Flavor>
std::shared_ptr<typename Flavor::ProvingKey> UltraHonkComposerHelper_<Flavor>::compute_proving_key(
const CircuitConstructor& circuit_constructor)
{
if (proving_key) {
Expand Down Expand Up @@ -310,7 +314,8 @@ std::shared_ptr<UltraHonkComposerHelper::Flavor::ProvingKey> UltraHonkComposerHe
*
* @return Pointer to created circuit verification key.
* */
std::shared_ptr<UltraHonkComposerHelper::VerificationKey> UltraHonkComposerHelper::compute_verification_key(
template <UltraFlavor Flavor>
std::shared_ptr<typename Flavor::VerificationKey> UltraHonkComposerHelper_<Flavor>::compute_verification_key(
const CircuitConstructor& circuit_constructor)
{
if (verification_key) {
Expand All @@ -321,7 +326,7 @@ std::shared_ptr<UltraHonkComposerHelper::VerificationKey> UltraHonkComposerHelpe
compute_proving_key(circuit_constructor);
}

verification_key = std::make_shared<UltraHonkComposerHelper::VerificationKey>(
verification_key = std::make_shared<typename Flavor::VerificationKey>(
proving_key->circuit_size, proving_key->num_public_inputs, proving_key->composer_type);

// Compute and store commitments to all precomputed polynomials
Expand Down Expand Up @@ -360,5 +365,7 @@ std::shared_ptr<UltraHonkComposerHelper::VerificationKey> UltraHonkComposerHelpe

return verification_key;
}
template class UltraHonkComposerHelper_<honk::flavor::Ultra>;
template class UltraHonkComposerHelper_<honk::flavor::UltraGrumpkin>;

} // namespace proof_system::honk
Loading