Skip to content

Commit

Permalink
feat: update to protogalaxy interfaces (#2498)
Browse files Browse the repository at this point in the history
This PR resolves
[#725](AztecProtocol/barretenberg#725) and
introduces `ProverInstances` and `VerifierInstances` classes responsible
of managing the Instances objects and determining the number of
instances to be folded.
  • Loading branch information
maramihali authored Sep 25, 2023
1 parent 868cceb commit 9a3d265
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 98 deletions.
28 changes: 0 additions & 28 deletions barretenberg/cpp/src/barretenberg/honk/composer/ultra_composer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,7 @@ UltraVerifier_<Flavor> UltraComposer_<Flavor>::create_verifier(std::shared_ptr<I
return output_state;
}

template <UltraFlavor Flavor>
ProtoGalaxyProver_<Flavor> UltraComposer_<Flavor>::create_folding_prover(
std::vector<std::shared_ptr<Instance>> instances)
{
uint32_t idx = 0;
for (const auto& inst : instances) {
inst->index = idx;
idx++;
}
ProtoGalaxyProver_<Flavor> output_state(instances);

return output_state;
}

template <UltraFlavor Flavor>
ProtoGalaxyVerifier_<Flavor> UltraComposer_<Flavor>::create_folding_verifier(
std::vector<std::shared_ptr<Instance>> instances)
{
std::vector<std::shared_ptr<VerificationKey>> vks;
for (const auto& inst : instances) {
vks.emplace_back(inst->compute_verification_key());
}
ProtoGalaxyVerifier_<Flavor> output_state(vks);

return output_state;
}

template class UltraComposer_<honk::flavor::Ultra>;
template class UltraComposer_<honk::flavor::UltraGrumpkin>;
template class UltraComposer_<honk::flavor::GoblinUltra>;

} // namespace proof_system::honk
25 changes: 22 additions & 3 deletions barretenberg/cpp/src/barretenberg/honk/composer/ultra_composer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ template <UltraFlavor Flavor> class UltraComposer_ {
using VerifierCommitmentKey = typename Flavor::VerifierCommitmentKey;
using Instance = ProverInstance_<Flavor>;

static constexpr size_t NUM_FOLDING = 2;
using ProverInstances = ProverInstances_<Flavor, NUM_FOLDING>;
using VerifierInstances = VerifierInstances_<Flavor, NUM_FOLDING>;

// offset due to placing zero wires at the start of execution trace
static constexpr size_t num_zero_rows = Flavor::has_zero_row ? 1 : 0;

static constexpr std::string_view NAME_STRING = "UltraHonk";
static constexpr size_t NUM_WIRES = CircuitBuilder::NUM_WIRES;
std::shared_ptr<ProvingKey> proving_key;
Expand Down Expand Up @@ -69,8 +72,24 @@ template <UltraFlavor Flavor> class UltraComposer_ {
UltraProver_<Flavor> create_prover(std::shared_ptr<Instance>);
UltraVerifier_<Flavor> create_verifier(std::shared_ptr<Instance>);

ProtoGalaxyProver_<Flavor> create_folding_prover(std::vector<std::shared_ptr<Instance>>);
ProtoGalaxyVerifier_<Flavor> create_folding_verifier(std::vector<std::shared_ptr<Instance>>);
ProtoGalaxyProver_<ProverInstances> create_folding_prover(std::vector<std::shared_ptr<Instance>> instances)
{
ProverInstances insts(instances);
ProtoGalaxyProver_<ProverInstances> output_state(insts);

return output_state;
};
ProtoGalaxyVerifier_<VerifierInstances> create_folding_verifier(std::vector<std::shared_ptr<Instance>> instances)
{
std::vector<std::shared_ptr<VerificationKey>> vks;
for (const auto& inst : instances) {
vks.emplace_back(inst->compute_verification_key());
}
VerifierInstances insts(vks);
ProtoGalaxyVerifier_<VerifierInstances> output_state(insts);

return output_state;
};
};
extern template class UltraComposer_<honk::flavor::Ultra>;
// TODO: the UltraGrumpkin flavor still works on BN254 because plookup needs to be templated to be able to construct
Expand Down
48 changes: 48 additions & 0 deletions barretenberg/cpp/src/barretenberg/honk/instance/instances.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once
#include "barretenberg/honk/instance/prover_instance.hpp"
#include "barretenberg/honk/instance/verifier_instance.hpp"
namespace proof_system::honk {

template <typename Flavor_, size_t NUM_> struct ProverInstances_ {
using Flavor = Flavor_;
using Instance = ProverInstance_<Flavor>;
using ArrayType = std::array<std::shared_ptr<Instance>, NUM_>;

public:
static constexpr size_t NUM = NUM_;
ArrayType _data;
Instance const& operator[](size_t idx) const { return _data[idx]; }
typename ArrayType::iterator begin() { return _data.begin(); };
typename ArrayType::iterator end() { return _data.end(); };
ProverInstances_(std::vector<std::shared_ptr<Instance>> data)
{
ASSERT(data.size() == NUM);
for (size_t idx = 0; idx < data.size(); idx++) {
_data[idx] = std::move(data[idx]);
}
};
};

template <typename Flavor_, size_t NUM_> struct VerifierInstances_ {
using Flavor = Flavor_;
using VerificationKey = typename Flavor::VerificationKey;
using Instance = VerifierInstance_<Flavor>;
using ArrayType = std::array<Instance, NUM_>;

public:
static constexpr size_t NUM = NUM_;
ArrayType _data;
Instance const& operator[](size_t idx) const { return _data[idx]; }
typename ArrayType::iterator begin() { return _data.begin(); };
typename ArrayType::iterator end() { return _data.end(); };
VerifierInstances_(std::vector<std::shared_ptr<VerificationKey>> vks)
{
ASSERT(vks.size() == NUM);
for (size_t idx = 0; idx < vks.size(); idx++) {
Instance inst;
inst.verification_key = std::move(vks[idx]);
_data[idx] = inst;
}
};
};
} // namespace proof_system::honk
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ template <class Flavor> class ProverInstance_ {
proof_system::RelationParameters<FF> relation_parameters;
std::vector<uint32_t> recursive_proof_public_input_indices;
FoldingParameters folding_params;
// Used by the prover for domain separation in the transcript
uint32_t index;

ProverInstance_(Circuit& circuit)
{
Expand Down Expand Up @@ -101,6 +99,4 @@ extern template class ProverInstance_<honk::flavor::Ultra>;
extern template class ProverInstance_<honk::flavor::UltraGrumpkin>;
extern template class ProverInstance_<honk::flavor::GoblinUltra>;

using ProverInstance = ProverInstance_<honk::flavor::Ultra>;

} // namespace proof_system::honk
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@ template <class Flavor> class VerifierInstance_ {
size_t circuit_size;
RelationParameters<FF> relation_parameters;
FoldingParameters folding_params;
size_t index;
};
} // namespace proof_system::honk
Original file line number Diff line number Diff line change
@@ -1,54 +1,52 @@
#include "protogalaxy_prover.hpp"
#include "barretenberg/proof_system/flavor/flavor.hpp"
namespace proof_system::honk {
template <class Flavor>
ProtoGalaxyProver_<Flavor>::ProtoGalaxyProver_(std::vector<std::shared_ptr<Instance>> insts)
: instances(insts)
{}

/**
* @brief Prior to folding we need to add all the public inputs to the transcript, labelled by their corresponding
* instance index, compute all the instance's polynomials and record the relation parameters involved in computing these
* polynomials in the transcript.
*
*/
template <class Flavor> void ProtoGalaxyProver_<Flavor>::prepare_for_folding()
template <class ProverInstances> void ProtoGalaxyProver_<ProverInstances>::prepare_for_folding()
{
for (const auto& instance : instances) {
// this doesnt work in the current format
auto idx = 0;
for (auto it = instances.begin(); it != instances.end(); it++, idx++) {
auto instance = *it;
instance->initialise_prover_polynomials();

const auto instance_index = std::to_string(instance->index);
auto domain_separator = std::to_string(idx);
const auto circuit_size = static_cast<uint32_t>(instance->proving_key->circuit_size);
const auto num_public_inputs = static_cast<uint32_t>(instance->proving_key->num_public_inputs);

transcript.send_to_verifier(instance_index + "_circuit_size", circuit_size);
transcript.send_to_verifier(instance_index + "_public_input_size", num_public_inputs);
transcript.send_to_verifier(instance_index + "_pub_inputs_offset",
transcript.send_to_verifier(domain_separator + "_circuit_size", circuit_size);
transcript.send_to_verifier(domain_separator + "_public_input_size", num_public_inputs);
transcript.send_to_verifier(domain_separator + "_pub_inputs_offset",
static_cast<uint32_t>(instance->pub_inputs_offset));

for (size_t i = 0; i < instance->proving_key->num_public_inputs; ++i) {
auto public_input_i = instance->public_inputs[i];
transcript.send_to_verifier(instance_index + "_public_input_" + std::to_string(i), public_input_i);
transcript.send_to_verifier(domain_separator + "_public_input_" + std::to_string(i), public_input_i);
}

auto [eta, beta, gamma] =
transcript.get_challenges(instance_index + "_eta", instance_index + "_beta", instance_index + "_gamma");
auto [eta, beta, gamma] = transcript.get_challenges(
domain_separator + "_eta", domain_separator + "_beta", domain_separator + "_gamma");
instance->compute_sorted_accumulator_polynomials(eta);
instance->compute_grand_product_polynomials(beta, gamma);
}
}

// TODO(#689): implement this function
template <class Flavor> ProverFoldingResult<Flavor> ProtoGalaxyProver_<Flavor>::fold_instances()
template <class ProverInstances>
ProverFoldingResult<typename ProverInstances::Flavor> ProtoGalaxyProver_<ProverInstances>::fold_instances()
{
prepare_for_folding();
ProverFoldingResult<Flavor> res;
res.folding_data = transcript.proof_data;
return res;
}

template class ProtoGalaxyProver_<honk::flavor::Ultra>;
template class ProtoGalaxyProver_<honk::flavor::UltraGrumpkin>;
template class ProtoGalaxyProver_<honk::flavor::GoblinUltra>;

template class ProtoGalaxyProver_<ProverInstances_<honk::flavor::Ultra, 2>>;
template class ProtoGalaxyProver_<ProverInstances_<honk::flavor::UltraGrumpkin, 2>>;
template class ProtoGalaxyProver_<ProverInstances_<honk::flavor::GoblinUltra, 2>>;
} // namespace proof_system::honk
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,30 @@
#include "barretenberg/honk/flavor/goblin_ultra.hpp"
#include "barretenberg/honk/flavor/ultra.hpp"
#include "barretenberg/honk/flavor/ultra_grumpkin.hpp"
#include "barretenberg/honk/instance/prover_instance.hpp"
#include "barretenberg/honk/instance/instances.hpp"
#include "barretenberg/honk/proof_system/folding_result.hpp"
#include "barretenberg/proof_system/flavor/flavor.hpp"
namespace proof_system::honk {
template <class Flavor> class ProtoGalaxyProver_ {
template <class ProverInstances> class ProtoGalaxyProver_ {
public:
using Flavor = typename ProverInstances::Flavor;
using FF = typename Flavor::FF;
using Instance = ProverInstance_<Flavor>;
using ProverPolynomials = typename Flavor::ProverPolynomials;

std::vector<std::shared_ptr<Instance>> instances;
ProverInstances instances;

ProverTranscript<FF> transcript;

explicit ProtoGalaxyProver_(std::vector<std::shared_ptr<Instance>>);
ProtoGalaxyProver_(ProverInstances insts)
: instances(insts){};
~ProtoGalaxyProver_() = default;

void prepare_for_folding();

ProverFoldingResult<Flavor> fold_instances();
};

extern template class ProtoGalaxyProver_<honk::flavor::Ultra>;
extern template class ProtoGalaxyProver_<honk::flavor::UltraGrumpkin>;
extern template class ProtoGalaxyProver_<honk::flavor::GoblinUltra>;
extern template class ProtoGalaxyProver_<ProverInstances_<honk::flavor::Ultra, 2>>;
extern template class ProtoGalaxyProver_<ProverInstances_<honk::flavor::UltraGrumpkin, 2>>;
extern template class ProtoGalaxyProver_<ProverInstances_<honk::flavor::GoblinUltra, 2>>;
} // namespace proof_system::honk
Original file line number Diff line number Diff line change
@@ -1,36 +1,28 @@
#include "protogalaxy_verifier.hpp"
#include "barretenberg/honk/utils/grand_product_delta.hpp"
namespace proof_system::honk {
template <class Flavor>
ProtoGalaxyVerifier_<Flavor>::ProtoGalaxyVerifier_(std::vector<std::shared_ptr<VerificationKey>> vks)
{
// TODO(https://github.com/AztecProtocol/barretenberg/issues/391): simplify code with C++23 features
uint32_t idx = 0;
for (const auto& vk : vks) {
VerifierInstance inst;
inst.verification_key = std::move(vk);
inst.index = idx;
verifier_instances.emplace_back(inst);
idx++;
}
}

template <class Flavor>
VerifierFoldingResult<Flavor> ProtoGalaxyVerifier_<Flavor>::fold_public_parameters(std::vector<uint8_t> fold_data)
template <class VerifierInstances>
VerifierFoldingResult<typename VerifierInstances::Flavor> ProtoGalaxyVerifier_<
VerifierInstances>::fold_public_parameters(std::vector<uint8_t> fold_data)
{
transcript = VerifierTranscript<FF>{ fold_data };
for (auto& inst : verifier_instances) {
auto idx = std::to_string(inst.index);
inst.circuit_size = transcript.template receive_from_prover<uint32_t>(idx + "_circuit_size");
inst.public_input_size = transcript.template receive_from_prover<uint32_t>(idx + "_public_input_size");
inst.pub_inputs_offset = transcript.template receive_from_prover<uint32_t>(idx + "_pub_inputs_offset");
auto index = 0;
for (auto it = verifier_instances.begin(); it != verifier_instances.end(); it++, index++) {
auto inst = *it;
auto domain_separator = std::to_string(index);
inst.circuit_size = transcript.template receive_from_prover<uint32_t>(domain_separator + "_circuit_size");
inst.public_input_size =
transcript.template receive_from_prover<uint32_t>(domain_separator + "_public_input_size");
inst.pub_inputs_offset =
transcript.template receive_from_prover<uint32_t>(domain_separator + "_pub_inputs_offset");

for (size_t i = 0; i < inst.public_input_size; ++i) {
auto public_input_i =
transcript.template receive_from_prover<FF>(idx + "_public_input_" + std::to_string(i));
transcript.template receive_from_prover<FF>(domain_separator + "_public_input_" + std::to_string(i));
inst.public_inputs.emplace_back(public_input_i);
}
auto [eta, beta, gamma] = transcript.get_challenges(idx + "_eta", idx + "_beta", idx + "_gamma");
auto [eta, beta, gamma] = transcript.get_challenges(
domain_separator + "_eta", domain_separator + "_beta", domain_separator + "_gamma");
const FF public_input_delta = compute_public_input_delta<Flavor>(
inst.public_inputs, beta, gamma, inst.circuit_size, inst.pub_inputs_offset);
const FF lookup_grand_product_delta = compute_lookup_grand_product_delta<FF>(beta, gamma, inst.circuit_size);
Expand All @@ -43,7 +35,7 @@ VerifierFoldingResult<Flavor> ProtoGalaxyVerifier_<Flavor>::fold_public_paramete
return res;
}

template class ProtoGalaxyVerifier_<honk::flavor::Ultra>;
template class ProtoGalaxyVerifier_<honk::flavor::UltraGrumpkin>;
template class ProtoGalaxyVerifier_<honk::flavor::GoblinUltra>;
template class ProtoGalaxyVerifier_<VerifierInstances_<honk::flavor::Ultra, 2>>;
template class ProtoGalaxyVerifier_<VerifierInstances_<honk::flavor::UltraGrumpkin, 2>>;
template class ProtoGalaxyVerifier_<VerifierInstances_<honk::flavor::GoblinUltra, 2>>;
} // namespace proof_system::honk
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@
#include "barretenberg/honk/flavor/goblin_ultra.hpp"
#include "barretenberg/honk/flavor/ultra.hpp"
#include "barretenberg/honk/flavor/ultra_grumpkin.hpp"
#include "barretenberg/honk/instance/verifier_instance.hpp"
#include "barretenberg/honk/instance/instances.hpp"
#include "barretenberg/honk/proof_system/folding_result.hpp"
#include "barretenberg/honk/transcript/transcript.hpp"
#include "barretenberg/proof_system/flavor/flavor.hpp"

namespace proof_system::honk {
template <class Flavor> class ProtoGalaxyVerifier_ {
template <class VerifierInstances> class ProtoGalaxyVerifier_ {
public:
using Flavor = typename VerifierInstances::Flavor;
using FF = typename Flavor::FF;
using Instance = typename VerifierInstances::Instance;
using VerificationKey = typename Flavor::VerificationKey;
using VerifierInstance = VerifierInstance_<Flavor>;
std::vector<VerifierInstance> verifier_instances;
VerifierInstances verifier_instances;
VerifierTranscript<FF> transcript;

explicit ProtoGalaxyVerifier_(std::vector<std::shared_ptr<VerificationKey>> vks);
// should the PG verifier be given the VerifierInstances, nah this makes sense yo me
ProtoGalaxyVerifier_(VerifierInstances insts)
: verifier_instances(insts){};
~ProtoGalaxyVerifier_() = default;
VerifierFoldingResult<Flavor> fold_public_parameters(std::vector<uint8_t> fold_data);
};

extern template class ProtoGalaxyVerifier_<honk::flavor::Ultra>;
extern template class ProtoGalaxyVerifier_<honk::flavor::UltraGrumpkin>;
extern template class ProtoGalaxyVerifier_<honk::flavor::GoblinUltra>;
extern template class ProtoGalaxyVerifier_<VerifierInstances_<honk::flavor::Ultra, 2>>;
extern template class ProtoGalaxyVerifier_<VerifierInstances_<honk::flavor::UltraGrumpkin, 2>>;
extern template class ProtoGalaxyVerifier_<VerifierInstances_<honk::flavor::GoblinUltra, 2>>;
} // namespace proof_system::honk
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ TEST_F(UltraTranscriptTests, VerifierManifestConsistency)

TEST_F(UltraTranscriptTests, FoldingManifestTest)
{
using Flavor = flavor::Ultra;
auto builder_one = proof_system::UltraCircuitBuilder();
auto a = 2;
auto b = 3;
Expand All @@ -206,7 +207,7 @@ TEST_F(UltraTranscriptTests, FoldingManifestTest)
auto instance_one = composer.create_instance(builder_one);
auto instance_two = composer.create_instance(builder_two);

std::vector<std::shared_ptr<ProverInstance>> insts;
std::vector<std::shared_ptr<ProverInstance_<Flavor>>> insts;
insts.emplace_back(instance_one);
insts.emplace_back(instance_two);
auto prover = composer.create_folding_prover(insts);
Expand Down

0 comments on commit 9a3d265

Please sign in to comment.