Skip to content

Commit

Permalink
chore(avm): move proving key to avm files
Browse files Browse the repository at this point in the history
  • Loading branch information
fcarreiro committed Sep 2, 2024
1 parent 6a2614a commit c018767
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 63 deletions.
46 changes: 0 additions & 46 deletions barretenberg/cpp/src/barretenberg/flavor/flavor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,52 +141,6 @@ template <typename FF, typename CommitmentKey_> class ProvingKey_ {
this->num_public_inputs = num_public_inputs;
};
};
template <typename PrecomputedPolynomials, typename WitnessPolynomials, typename CommitmentKey_>
class ProvingKeyAvm_ : public PrecomputedPolynomials, public WitnessPolynomials {
public:
using Polynomial = typename PrecomputedPolynomials::DataType;
using FF = typename Polynomial::FF;

size_t circuit_size;
bool contains_recursive_proof;
AggregationObjectPubInputIndices recursive_proof_public_input_indices;
bb::EvaluationDomain<FF> evaluation_domain;
std::shared_ptr<CommitmentKey_> commitment_key;

// Offset off the public inputs from the start of the execution trace
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());
}
// This order matters! must match get_unshifted in entity classes
auto get_all() { return concatenate(get_precomputed_polynomials(), get_witness_polynomials()); }
auto get_witness_polynomials() { return WitnessPolynomials::get_all(); }
auto get_precomputed_polynomials() { return PrecomputedPolynomials::get_all(); }
auto get_selectors() { return PrecomputedPolynomials::get_selectors(); }
ProvingKeyAvm_() = default;
ProvingKeyAvm_(const size_t circuit_size, const size_t num_public_inputs)
{
this->commitment_key = std::make_shared<CommitmentKey_>(circuit_size + 1);
this->evaluation_domain = bb::EvaluationDomain<FF>(circuit_size, circuit_size);
this->circuit_size = circuit_size;
this->log_circuit_size = numeric::get_msb(circuit_size);
this->num_public_inputs = num_public_inputs;
// Allocate memory for precomputed polynomials
for (auto& poly : PrecomputedPolynomials::get_all()) {
poly = Polynomial(circuit_size);
}
// Allocate memory for witness polynomials
for (auto& poly : WitnessPolynomials::get_all()) {
poly = Polynomial(circuit_size);
}
};
};

/**
* @brief Base verification key class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ std::shared_ptr<Flavor::ProvingKey> AvmComposer::compute_proving_key(CircuitCons
proving_key = std::make_shared<Flavor::ProvingKey>(subgroup_size, 0);
}

proving_key->contains_recursive_proof = false;

return proving_key;
}

Expand Down
19 changes: 19 additions & 0 deletions barretenberg/cpp/src/barretenberg/vm/avm/generated/flavor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2264,4 +2264,23 @@ AvmFlavor::PartiallyEvaluatedMultivariates::PartiallyEvaluatedMultivariates(cons
}
}

AvmFlavor::ProvingKey::ProvingKey(const size_t circuit_size, const size_t num_public_inputs)
: circuit_size(circuit_size)
, evaluation_domain(bb::EvaluationDomain<FF>(circuit_size, circuit_size))
, commitment_key(std::make_shared<CommitmentKey>(circuit_size + 1))
{
// TODO: These come from PrecomputedEntitiesBase, ideal we'd just call that class's constructor.
this->log_circuit_size = numeric::get_msb(circuit_size);
this->num_public_inputs = num_public_inputs;

// Allocate memory for precomputed polynomials
for (auto& poly : PrecomputedEntities<Polynomial>::get_all()) {
poly = Polynomial(circuit_size);
}
// Allocate memory for witness polynomials
for (auto& poly : WitnessEntities<Polynomial>::get_all()) {
poly = Polynomial(circuit_size);
}
};

} // namespace bb
34 changes: 28 additions & 6 deletions barretenberg/cpp/src/barretenberg/vm/avm/generated/flavor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,35 @@ class AvmFlavor {
auto get_precomputed() { return PrecomputedEntities<DataType>::get_all(); }
};

class ProvingKey
: public ProvingKeyAvm_<PrecomputedEntities<Polynomial>, WitnessEntities<Polynomial>, CommitmentKey> {
class ProvingKey : public PrecomputedEntities<Polynomial>, public WitnessEntities<Polynomial> {
public:
// Expose constructors on the base class
using Base = ProvingKeyAvm_<PrecomputedEntities<Polynomial>, WitnessEntities<Polynomial>, CommitmentKey>;
using Base::Base;
auto get_to_be_shifted() { return AvmFlavor::get_to_be_shifted<DataType>(*this); }
using FF = typename Polynomial::FF;

ProvingKey() = default;
ProvingKey(const size_t circuit_size, const size_t num_public_inputs);

size_t circuit_size;
bb::EvaluationDomain<FF> evaluation_domain;
std::shared_ptr<CommitmentKey> commitment_key;

// Offset off the public inputs from the start of the execution trace
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(PrecomputedEntities<Polynomial>::get_labels(),
WitnessEntities<Polynomial>::get_labels());
}
auto get_witness_polynomials() { return WitnessEntities<Polynomial>::get_all(); }
auto get_precomputed_polynomials() { return PrecomputedEntities<Polynomial>::get_all(); }
auto get_selectors() { return PrecomputedEntities<Polynomial>::get_all(); }
auto get_to_be_shifted() { return AvmFlavor::get_to_be_shifted<Polynomial>(*this); }
// This order matters! must match get_unshifted in entity classes
auto get_all() { return concatenate(get_precomputed_polynomials(), get_witness_polynomials()); }
};

class VerificationKey : public VerificationKey_<PrecomputedEntities<Commitment>, VerifierCommitmentKey> {
Expand Down
2 changes: 0 additions & 2 deletions bb-pilcom/bb-pil-backend/templates/composer.cpp.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ std::shared_ptr<Flavor::ProvingKey> {{name}}Composer::compute_proving_key(Circui
proving_key = std::make_shared<Flavor::ProvingKey>(subgroup_size, 0);
}

proving_key->contains_recursive_proof = false;

return proving_key;
}

Expand Down
19 changes: 19 additions & 0 deletions bb-pilcom/bb-pil-backend/templates/flavor.cpp.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,23 @@ void {{name}}Flavor::Transcript::serialize_full_transcript() {
}
}

AvmFlavor::ProvingKey::ProvingKey(const size_t circuit_size, const size_t num_public_inputs)
: circuit_size(circuit_size)
, evaluation_domain(bb::EvaluationDomain<FF>(circuit_size, circuit_size))
, commitment_key(std::make_shared<CommitmentKey>(circuit_size + 1))
{
// TODO: These come from PrecomputedEntitiesBase, ideal we'd just call that class's constructor.
this->log_circuit_size = numeric::get_msb(circuit_size);
this->num_public_inputs = num_public_inputs;

// Allocate memory for precomputed polynomials
for (auto& poly : PrecomputedEntities<Polynomial>::get_all()) {
poly = Polynomial(circuit_size);
}
// Allocate memory for witness polynomials
for (auto& poly : WitnessEntities<Polynomial>::get_all()) {
poly = Polynomial(circuit_size);
}
};

} // namespace bb
35 changes: 28 additions & 7 deletions bb-pilcom/bb-pil-backend/templates/flavor.hpp.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,35 @@ class {{name}}Flavor {
auto get_precomputed() { return PrecomputedEntities<DataType>::get_all(); }
};

class ProvingKey : public ProvingKeyAvm_<PrecomputedEntities<Polynomial>, WitnessEntities<Polynomial>, CommitmentKey> {
class ProvingKey : public PrecomputedEntities<Polynomial>, public WitnessEntities<Polynomial> {
public:
// Expose constructors on the base class
using Base = ProvingKeyAvm_<PrecomputedEntities<Polynomial>, WitnessEntities<Polynomial>, CommitmentKey>;
using Base::Base;
auto get_to_be_shifted() {
return {{name}}Flavor::get_to_be_shifted<DataType>(*this);
}
using FF = typename Polynomial::FF;

ProvingKey() = default;
ProvingKey(const size_t circuit_size, const size_t num_public_inputs);

size_t circuit_size;
bb::EvaluationDomain<FF> evaluation_domain;
std::shared_ptr<CommitmentKey> commitment_key;

// Offset off the public inputs from the start of the execution trace
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(PrecomputedEntities<Polynomial>::get_labels(),
WitnessEntities<Polynomial>::get_labels());
}
auto get_witness_polynomials() { return WitnessEntities<Polynomial>::get_all(); }
auto get_precomputed_polynomials() { return PrecomputedEntities<Polynomial>::get_all(); }
auto get_selectors() { return PrecomputedEntities<Polynomial>::get_all(); }
auto get_to_be_shifted() { return AvmFlavor::get_to_be_shifted<Polynomial>(*this); }
// This order matters! must match get_unshifted in entity classes
auto get_all() { return concatenate(get_precomputed_polynomials(), get_witness_polynomials()); }
};

class VerificationKey : public VerificationKey_<PrecomputedEntities<Commitment>, VerifierCommitmentKey> {
Expand Down

0 comments on commit c018767

Please sign in to comment.