Skip to content

Commit

Permalink
refactor: cleanup of prover and verifier instances (#4959)
Browse files Browse the repository at this point in the history
Moves the instance_size, log_instance_size, pub_inputs_offset,
num_public_inputs, public_inputs out of Prover and VerifierInstance and
into the proving key and verification key.

This is a step towards being able to pass only the proving key to the
OinkProver (presumcheck prover)
  • Loading branch information
lucasxia01 authored Mar 7, 2024
1 parent 5cccc78 commit f2fdefd
Show file tree
Hide file tree
Showing 22 changed files with 221 additions and 188 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ TEST_F(MockKernelTest, PinFoldingKernelSizes)
kernel_circuit, { func_fold_proof, ivc.vks.func_vk }, {}, kernel_acc);

auto kernel_fold_proof = ivc.accumulate(kernel_circuit);
EXPECT_EQ(ivc.prover_instance->log_instance_size, 17);
EXPECT_EQ(ivc.prover_instance->proving_key->log_circuit_size, 17);

GoblinUltraCircuitBuilder circuit_3{ ivc.goblin.op_queue };
GoblinMockCircuits::construct_mock_function_circuit(circuit_3);
Expand Down
13 changes: 12 additions & 1 deletion barretenberg/cpp/src/barretenberg/flavor/flavor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ class ProvingKey_ : public PrecomputedPolynomials, public WitnessPolynomials {
bb::EvaluationDomain<FF> evaluation_domain;
std::shared_ptr<CommitmentKey_> commitment_key;

// offset due to placing zero wires at the start of execution trace
// non-zero for Instances constructed from circuits, this concept doesn't exist for accumulated
// instances
size_t pub_inputs_offset = 0;

// The number of public inputs has to be the same for all instances because they are
// folded element by element.
std::vector<FF> public_inputs;

std::vector<std::string> get_labels() const
{
return concatenate(PrecomputedPolynomials::get_labels(), WitnessPolynomials::get_labels());
Expand Down Expand Up @@ -147,6 +156,7 @@ template <typename PrecomputedCommitments, typename VerifierCommitmentKey>
class VerificationKey_ : public PrecomputedCommitments {
public:
std::shared_ptr<VerifierCommitmentKey> pcs_verification_key;
size_t pub_inputs_offset = 0;

VerificationKey_() = default;
VerificationKey_(const size_t circuit_size, const size_t num_public_inputs)
Expand All @@ -158,10 +168,11 @@ class VerificationKey_ : public PrecomputedCommitments {

template <typename ProvingKeyPtr> VerificationKey_(const ProvingKeyPtr& proving_key)
{
this->pcs_verification_key = std::make_shared<VerifierCommitmentKey>();
this->circuit_size = proving_key->circuit_size;
this->log_circuit_size = numeric::get_msb(this->circuit_size);
this->num_public_inputs = proving_key->num_public_inputs;
this->pcs_verification_key = std::make_shared<VerifierCommitmentKey>();
this->pub_inputs_offset = proving_key->pub_inputs_offset;

for (auto [polynomial, commitment] : zip_view(proving_key->get_precomputed_polynomials(), this->get_all())) {
commitment = proving_key->commitment_key->commit(polynomial);
Expand Down
15 changes: 14 additions & 1 deletion barretenberg/cpp/src/barretenberg/flavor/goblin_ultra.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,20 @@ class GoblinUltraFlavor {
* circuits.
* @todo TODO(https://github.com/AztecProtocol/barretenberg/issues/876)
*/
using VerificationKey = VerificationKey_<PrecomputedEntities<Commitment>, VerifierCommitmentKey>;
class VerificationKey : public VerificationKey_<PrecomputedEntities<Commitment>, VerifierCommitmentKey> {
public:
std::vector<FF> public_inputs;

VerificationKey(const size_t circuit_size, const size_t num_public_inputs)
: VerificationKey_(circuit_size, num_public_inputs)
{}

template <typename ProvingKeyPtr>
VerificationKey(const ProvingKeyPtr& proving_key)
: VerificationKey_(proving_key)
, public_inputs(proving_key->public_inputs)
{}
};

/**
* @brief A container for storing the partially evaluated multivariates produced by sumcheck.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ template <typename BuilderType> class GoblinUltraRecursiveFlavor_ {
class VerificationKey
: public VerificationKey_<GoblinUltraFlavor::PrecomputedEntities<Commitment>, VerifierCommitmentKey> {
public:
std::vector<FF> public_inputs;

VerificationKey(const size_t circuit_size, const size_t num_public_inputs)
{
this->circuit_size = circuit_size;
Expand All @@ -118,6 +120,11 @@ template <typename BuilderType> class GoblinUltraRecursiveFlavor_ {
this->circuit_size = native_key->circuit_size;
this->log_circuit_size = numeric::get_msb(this->circuit_size);
this->num_public_inputs = native_key->num_public_inputs;
this->pub_inputs_offset = native_key->pub_inputs_offset;
this->public_inputs = std::vector<FF>(native_key->num_public_inputs);
for (auto [public_input, native_public_input] : zip_view(this->public_inputs, native_key->public_inputs)) {
public_input = FF::from_witness(builder, native_public_input);
}
this->q_m = Commitment::from_witness(builder, native_key->q_m);
this->q_l = Commitment::from_witness(builder, native_key->q_l);
this->q_r = Commitment::from_witness(builder, native_key->q_r);
Expand Down
15 changes: 14 additions & 1 deletion barretenberg/cpp/src/barretenberg/flavor/ultra.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,20 @@ class UltraFlavor {
* that, and split out separate PrecomputedPolynomials/Commitments data for clarity but also for portability of our
* circuits.
*/
using VerificationKey = VerificationKey_<PrecomputedEntities<Commitment>, VerifierCommitmentKey>;
class VerificationKey : public VerificationKey_<PrecomputedEntities<Commitment>, VerifierCommitmentKey> {
public:
std::vector<FF> public_inputs;

VerificationKey(const size_t circuit_size, const size_t num_public_inputs)
: VerificationKey_(circuit_size, num_public_inputs)
{}

template <typename ProvingKeyPtr>
VerificationKey(const ProvingKeyPtr& proving_key)
: VerificationKey_(proving_key)
, public_inputs(proving_key->public_inputs)
{}
};

/**
* @brief A field element for each entity of the flavor. These entities represent the prover polynomials
Expand Down
17 changes: 12 additions & 5 deletions barretenberg/cpp/src/barretenberg/flavor/ultra_recursive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ template <typename BuilderType> class UltraRecursiveFlavor_ {
*/
class VerificationKey : public VerificationKey_<PrecomputedEntities<Commitment>, VerifierCommitmentKey> {
public:
std::vector<FF> public_inputs;

VerificationKey(const size_t circuit_size, const size_t num_public_inputs)
{
this->circuit_size = circuit_size;
Expand All @@ -285,10 +287,15 @@ template <typename BuilderType> class UltraRecursiveFlavor_ {
*/
VerificationKey(CircuitBuilder* builder, const std::shared_ptr<NativeVerificationKey>& native_key)
{
this->pcs_verification_key = native_key->pcs_verification_key;
this->circuit_size = native_key->circuit_size;
this->log_circuit_size = numeric::get_msb(this->circuit_size);
this->num_public_inputs = native_key->num_public_inputs;
this->pcs_verification_key = native_key->pcs_verification_key;
this->pub_inputs_offset = native_key->pub_inputs_offset;
this->public_inputs = std::vector<FF>(native_key->num_public_inputs);
for (auto [public_input, native_public_input] : zip_view(this->public_inputs, native_key->public_inputs)) {
public_input = FF::from_witness(builder, native_public_input);
}
this->q_m = Commitment::from_witness(builder, native_key->q_m);
this->q_l = Commitment::from_witness(builder, native_key->q_l);
this->q_r = Commitment::from_witness(builder, native_key->q_r);
Expand Down Expand Up @@ -318,8 +325,8 @@ template <typename BuilderType> class UltraRecursiveFlavor_ {
};

/**
* @brief A field element for each entity of the flavor. These entities represent the prover polynomials evaluated
* at one point.
* @brief A field element for each entity of the flavor. These entities represent the prover polynomials
* evaluated at one point.
*/
class AllValues : public AllEntities<FF> {
public:
Expand All @@ -330,8 +337,8 @@ template <typename BuilderType> class UltraRecursiveFlavor_ {

/**
* @brief A container for commitment labels.
* @note It's debatable whether this should inherit from AllEntities. since most entries are not strictly needed. It
* has, however, been useful during debugging to have these labels available.
* @note It's debatable whether this should inherit from AllEntities. since most entries are not strictly
* needed. It has, however, been useful during debugging to have these labels available.
*
*/
class CommitmentLabels : public AllEntities<std::string> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ TEST(Protogalaxy, CombinerOn2Instances)
/*log_circuit_size=*/1, idx * 128);
restrict_to_standard_arithmetic_relation(prover_polynomials);
instance->prover_polynomials = std::move(prover_polynomials);
instance->instance_size = 2;
instance->proving_key = std::make_shared<Flavor::ProvingKey>();
instance->proving_key->circuit_size = 2;
instance_data[idx] = instance;
}

Expand Down Expand Up @@ -77,7 +78,8 @@ TEST(Protogalaxy, CombinerOn2Instances)
/*log_circuit_size=*/1);
restrict_to_standard_arithmetic_relation(prover_polynomials);
instance->prover_polynomials = std::move(prover_polynomials);
instance->instance_size = 2;
instance->proving_key = std::make_shared<Flavor::ProvingKey>();
instance->proving_key->circuit_size = 2;
instance_data[idx] = instance;
}

Expand Down Expand Up @@ -167,7 +169,8 @@ TEST(Protogalaxy, CombinerOn4Instances)
auto prover_polynomials = get_zero_prover_polynomials<Flavor>(
/*log_circuit_size=*/1);
instance->prover_polynomials = std::move(prover_polynomials);
instance->instance_size = 2;
instance->proving_key = std::make_shared<Flavor::ProvingKey>();
instance->proving_key->circuit_size = 2;
instance_data[idx] = instance;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ DeciderProver_<Flavor>::DeciderProver_(const std::shared_ptr<Instance>& inst,
template <IsUltraFlavor Flavor> void DeciderProver_<Flavor>::execute_relation_check_rounds()
{
using Sumcheck = SumcheckProver<Flavor>;
auto instance_size = accumulator->instance_size;
auto instance_size = accumulator->proving_key->circuit_size;
auto sumcheck = Sumcheck(instance_size, transcript);
sumcheck_output = sumcheck.prove(accumulator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ template <typename Flavor> bool DeciderVerifier_<Flavor>::verify_proof(const Hon

VerifierCommitments commitments{ accumulator->verification_key, accumulator->witness_commitments };

auto sumcheck = SumcheckVerifier<Flavor>(accumulator->log_instance_size, transcript, accumulator->target_sum);
auto sumcheck =
SumcheckVerifier<Flavor>(accumulator->verification_key->log_circuit_size, transcript, accumulator->target_sum);

auto [multivariate_challenge, claimed_evaluations, sumcheck_verified] =
sumcheck.verify(accumulator->relation_parameters, accumulator->alphas, accumulator->gate_challenges);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ void ProtoGalaxyProver_<ProverInstances>::finalise_and_send_instance(std::shared
{
instance->initialize_prover_polynomials();

const auto instance_size = static_cast<uint32_t>(instance->instance_size);
const auto num_public_inputs = static_cast<uint32_t>(instance->public_inputs.size());
const auto instance_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(domain_separator + "_instance_size", instance_size);
transcript->send_to_verifier(domain_separator + "_public_input_size", num_public_inputs);

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

auto& witness_commitments = instance->witness_commitments;

Expand Down Expand Up @@ -102,7 +102,7 @@ template <class ProverInstances> void ProtoGalaxyProver_<ProverInstances>::prepa
if (!instance->is_accumulator) {
finalise_and_send_instance(instance, domain_separator);
instance->target_sum = 0;
instance->gate_challenges = std::vector<FF>(instance->log_instance_size, 0);
instance->gate_challenges = std::vector<FF>(instance->proving_key->log_circuit_size, 0);
}

idx++;
Expand Down Expand Up @@ -132,8 +132,6 @@ std::shared_ptr<typename ProverInstances::Instance> ProtoGalaxyProver_<ProverIns
// TODO(https://github.com/AztecProtocol/barretenberg/issues/881): bad pattern
auto next_accumulator = std::make_shared<Instance>();
next_accumulator->is_accumulator = true;
next_accumulator->instance_size = instances[0]->instance_size;
next_accumulator->log_instance_size = instances[0]->log_instance_size;
next_accumulator->proving_key = instances[0]->proving_key;

// Compute the next target sum and send the next folding parameters to the verifier
Expand All @@ -146,7 +144,7 @@ std::shared_ptr<typename ProverInstances::Instance> ProtoGalaxyProver_<ProverIns
// Initialize prover polynomials
ProverPolynomials acc_prover_polynomials;
for (auto& polynomial : acc_prover_polynomials.get_all()) {
polynomial = typename Flavor::Polynomial(instances[0]->instance_size);
polynomial = typename Flavor::Polynomial(instances[0]->proving_key->circuit_size);
}

// Fold the prover polynomials
Expand All @@ -164,14 +162,14 @@ std::shared_ptr<typename ProverInstances::Instance> ProtoGalaxyProver_<ProverIns
// verification, the verifier will produce ϕ* as well and check it against what was sent by the prover.

// Fold the public inputs and send to the verifier
next_accumulator->public_inputs = std::vector<FF>(instances[0]->public_inputs.size(), 0);
next_accumulator->proving_key->public_inputs = std::vector<FF>(instances[0]->proving_key->public_inputs.size(), 0);
size_t el_idx = 0;
for (auto& el : next_accumulator->public_inputs) {
for (auto& el : next_accumulator->proving_key->public_inputs) {
size_t inst = 0;
for (auto& instance : instances) {
// TODO(https://github.com/AztecProtocol/barretenberg/issues/830)
if (instance->public_inputs.size() >= next_accumulator->public_inputs.size()) {
el += instance->public_inputs[el_idx] * lagranges[inst];
if (instance->proving_key->num_public_inputs >= next_accumulator->proving_key->num_public_inputs) {
el += instance->proving_key->public_inputs[el_idx] * lagranges[inst];
inst++;
};
}
Expand Down Expand Up @@ -208,14 +206,14 @@ template <class ProverInstances> void ProtoGalaxyProver_<ProverInstances>::pertu
{
state.accumulator = get_accumulator();
FF delta = transcript->template get_challenge<FF>("delta");
state.deltas = compute_round_challenge_pows(state.accumulator->log_instance_size, delta);
state.perturbator = Polynomial<FF>(state.accumulator->log_instance_size + 1); // initialize to all zeros
state.deltas = compute_round_challenge_pows(state.accumulator->proving_key->log_circuit_size, delta);
state.perturbator = Polynomial<FF>(state.accumulator->proving_key->log_circuit_size + 1); // initialize to all zeros
// compute perturbator only if this is not the first round and has an accumulator
if (state.accumulator->is_accumulator) {
state.perturbator = compute_perturbator(state.accumulator, state.deltas);
// Prover doesn't send the constant coefficient of F because this is supposed to be equal to the target sum of
// the accumulator which the folding verifier has from the previous iteration.
for (size_t idx = 1; idx <= state.accumulator->log_instance_size; idx++) {
for (size_t idx = 1; idx <= state.accumulator->proving_key->log_circuit_size; idx++) {
transcript->send_to_verifier("perturbator_" + std::to_string(idx), state.perturbator[idx]);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ template <class ProverInstances_> class ProtoGalaxyProver_ {
ExtendedUnivariateWithRandomization compute_combiner(const ProverInstances& instances, PowPolynomial<FF>& pow_betas)
{
BB_OP_COUNT_TIME();
size_t common_instance_size = instances[0]->instance_size;
size_t common_instance_size = instances[0]->proving_key->circuit_size;
pow_betas.compute_values();
// Determine number of threads for multithreading.
// Note: Multithreading is "on" for every round but we reduce the number of threads from the max available based
Expand Down
Loading

0 comments on commit f2fdefd

Please sign in to comment.