Skip to content

Commit

Permalink
something doesn't work
Browse files Browse the repository at this point in the history
  • Loading branch information
maramihali committed Dec 7, 2023
1 parent 828ab91 commit 2583f8f
Show file tree
Hide file tree
Showing 14 changed files with 275 additions and 254 deletions.
23 changes: 23 additions & 0 deletions barretenberg/cpp/src/barretenberg/flavor/goblin_ultra.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,29 @@ class GoblinUltra {
}
};

/**
* @brief An owning container of polynomials.
* @warning When this was introduced it broke some of our design principles.
* - Execution trace builders don't handle "polynomials" because the interpretation of the execution trace
* columns as polynomials is a detail of the proving system, and trace builders are (sometimes in practice,
* always in principle) reusable for different proving protocols (e.g., Plonk and Honk).
* - Polynomial storage is handled by key classes. Polynomials aren't moved, but are accessed elsewhere by
* std::spans.
*
* We will consider revising this data model: TODO(https://github.com/AztecProtocol/barretenberg/issues/743)
*/
class AllPolynomials : public AllEntities<Polynomial> {
public:
[[nodiscard]] AllValues get_row(const size_t row_idx) const
{
AllValues result;
for (auto [result_field, polynomial] : zip_view(result.get_all(), this->get_all())) {
result_field = polynomial[row_idx];
}
return result;
}
};

/**
* @brief A container for the witness commitments.
*/
Expand Down
23 changes: 23 additions & 0 deletions barretenberg/cpp/src/barretenberg/flavor/ultra.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,29 @@ class Ultra {
}
};

/**
* @brief An owning container of polynomials.
* @warning When this was introduced it broke some of our design principles.
* - Execution trace builders don't handle "polynomials" because the interpretation of the execution trace
* columns as polynomials is a detail of the proving system, and trace builders are (sometimes in practice,
* always in principle) reusable for different proving protocols (e.g., Plonk and Honk).
* - Polynomial storage is handled by key classes. Polynomials aren't moved, but are accessed elsewhere by
* std::spans.
*
* We will consider revising this data model: TODO(https://github.com/AztecProtocol/barretenberg/issues/743)
*/
class AllPolynomials : public AllEntities<Polynomial> {
public:
[[nodiscard]] AllValues get_row(const size_t row_idx) const
{
AllValues result;
for (auto [result_field, polynomial] : zip_view(result.get_all(), this->get_all())) {
result_field = polynomial[row_idx];
}
return result;
}
};

/**
* @brief A container for storing the partially evaluated multivariates produced by sumcheck.
*/
Expand Down
22 changes: 10 additions & 12 deletions barretenberg/cpp/src/barretenberg/honk/utils/testing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,50 @@ namespace proof_system::honk {
* function returns an array of data pointed to by the ProverPolynomials.
*/
template <typename Flavor>
std::pair<std::array<barretenberg::Polynomial<typename Flavor::FF>, Flavor::NUM_ALL_ENTITIES>,
typename Flavor::ProverPolynomials>
get_sequential_prover_polynomials(const size_t log_circuit_size, const size_t starting_value)
std::pair<typename Flavor::AllPolynomials, typename Flavor::ProverPolynomials> get_sequential_prover_polynomials(
const size_t log_circuit_size, const size_t starting_value)
{
using FF = typename Flavor::FF;
using ProverPolynomials = typename Flavor::ProverPolynomials;
using Polynomial = typename Flavor::Polynomial;

std::array<barretenberg::Polynomial<typename Flavor::FF>, Flavor::NUM_ALL_ENTITIES> storage;
typename Flavor::AllPolynomials storage;
size_t circuit_size = 1 << log_circuit_size;
size_t value_idx = starting_value;
for (auto& polynomial : storage) {
for (auto& polynomial : storage.get_all()) {
polynomial = Polynomial(circuit_size);
for (auto& value : polynomial) {
value = FF(value_idx++);
}
}

ProverPolynomials prover_polynomials;
for (auto [prover_poly, storage_poly] : zip_view(prover_polynomials.get_all(), storage)) {
for (auto [prover_poly, storage_poly] : zip_view(prover_polynomials.get_all(), storage.get_all())) {
prover_poly = storage_poly;
}

return std::pair(std::move(storage), prover_polynomials);
}

template <typename Flavor>
std::pair<std::array<barretenberg::Polynomial<typename Flavor::FF>, Flavor::NUM_ALL_ENTITIES>,
typename Flavor::ProverPolynomials>
get_zero_prover_polynomials(const size_t log_circuit_size)
std::pair<typename Flavor::AllPolynomials, typename Flavor::ProverPolynomials> get_zero_prover_polynomials(
const size_t log_circuit_size)
{
using FF = typename Flavor::FF;
using ProverPolynomials = typename Flavor::ProverPolynomials;
using Polynomial = typename Flavor::Polynomial;

std::array<barretenberg::Polynomial<typename Flavor::FF>, Flavor::NUM_ALL_ENTITIES> storage;
typename Flavor::AllPolynomials storage;
size_t circuit_size = 1 << log_circuit_size;
for (auto& polynomial : storage) {
for (auto& polynomial : storage.get_all()) {
polynomial = Polynomial(circuit_size);
for (auto& value : polynomial) {
value = FF(0);
}
}

ProverPolynomials prover_polynomials;
for (auto [prover_poly, storage_poly] : zip_view(prover_polynomials.get_all(), storage)) {
for (auto [prover_poly, storage_poly] : zip_view(prover_polynomials.get_all(), storage.get_all())) {
prover_poly = storage_poly;
}

Expand Down
4 changes: 2 additions & 2 deletions barretenberg/cpp/src/barretenberg/honk/utils/testing.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ TEST(HonkTestingUtils, ProverPolynomials)
auto [storage, prover_polynomials] =
proof_system::honk::get_sequential_prover_polynomials<Flavor>(/*log_circuit_size=*/2, /*starting_value=*/0);
auto& first_polynomial = prover_polynomials.get_all()[0];
EXPECT_EQ(storage[0][0], first_polynomial[0]);
EXPECT_EQ(storage[0][1], first_polynomial[1]);
EXPECT_EQ(storage.get_all()[0][0], first_polynomial[0]);
EXPECT_EQ(storage.get_all()[0][1], first_polynomial[1]);
};

} // namespace barretenberg::test_testing_utils
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ TEST(Protogalaxy, CombinerOn2Instances)
// relation.
if (is_random_input) {
std::vector<std::shared_ptr<ProverInstance>> instance_data(NUM_INSTANCES);
std::array<std::array<Polynomial, Flavor::NUM_ALL_ENTITIES>, NUM_INSTANCES> storage_arrays;
std::array<typename Flavor::AllPolynomials, NUM_INSTANCES> storage_arrays;
ProtoGalaxyProver prover;
std::vector<FF> pow_betas = { FF(1), FF(2) };

Expand Down Expand Up @@ -73,7 +73,7 @@ TEST(Protogalaxy, CombinerOn2Instances)
EXPECT_EQ(result, expected_result);
} else {
std::vector<std::shared_ptr<ProverInstance>> instance_data(NUM_INSTANCES);
std::array<std::array<Polynomial, Flavor::NUM_ALL_ENTITIES>, NUM_INSTANCES> storage_arrays;
std::array<typename Flavor::AllPolynomials, NUM_INSTANCES> storage_arrays;
ProtoGalaxyProver prover;
std::vector<FF> pow_betas = { FF(1), FF(2) };

Expand Down Expand Up @@ -166,7 +166,7 @@ TEST(Protogalaxy, CombinerOn4Instances)

auto run_test = [&]() {
std::vector<std::shared_ptr<ProverInstance>> instance_data(NUM_INSTANCES);
std::array<std::array<Polynomial, Flavor::NUM_ALL_ENTITIES>, NUM_INSTANCES> storage_arrays;
std::array<typename Flavor::AllPolynomials, NUM_INSTANCES> storage_arrays;
ProtoGalaxyProver prover;
std::vector<FF> pow_betas = { FF(1), FF(2) };

Expand Down
129 changes: 129 additions & 0 deletions barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_prover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,135 @@ FoldingResult<typename ProverInstances::Flavor> ProtoGalaxyProver_<ProverInstanc

return res;
}
template <class ProverInstances>
std::shared_ptr<typename ProverInstances::Instance> ProtoGalaxyProver_<ProverInstances>::compute_next_accumulator(
ProverInstances& instances,
Univariate<FF, ProverInstances::BATCHED_EXTENDED_LENGTH, ProverInstances::NUM>& combiner_quotient,
const FF& challenge,
const FF& compressed_perturbator)
{
auto combiner_quotient_at_challenge = combiner_quotient.evaluate(challenge);

// Given the challenge \gamma, compute Z(\gamma) and {L_0(\gamma),L_1(\gamma)}
// TODO(https://github.com/AztecProtocol/barretenberg/issues/764): Generalize the vanishing polynomial formula
// and the computation of Lagrange basis for k instances
auto vanishing_polynomial_at_challenge = challenge * (challenge - FF(1));
std::vector<FF> lagranges{ FF(1) - challenge, challenge };

auto next_accumulator = std::make_shared<Instance>();

// Compute the next target sum and send the next folding parameters to the verifier
auto next_target_sum =
compressed_perturbator * lagranges[0] + vanishing_polynomial_at_challenge * combiner_quotient_at_challenge;
next_accumulator->folding_parameters = { instances.next_gate_challenges, next_target_sum };
transcript->send_to_verifier("next_target_sum", next_target_sum);
for (size_t idx = 0; idx < instances.next_gate_challenges.size(); idx++) {
transcript->send_to_verifier("next_gate_challenge_" + std::to_string(idx), instances.next_gate_challenges[idx]);
}

// Allocate space, initialised to 0, for the prover polynomials of the next accumulator
AllPolynomials storage;
for (auto& polynomial : storage.get_all()) {
polynomial = typename Flavor::Polynomial(instances[0]->instance_size);
for (auto& value : polynomial) {
value = FF(0);
}
}
ProverPolynomials acc_prover_polynomials;
size_t poly_idx = 0;
auto prover_polynomial_pointers = acc_prover_polynomials.get_all();
for (auto& polynomial : storage.get_all()) {
prover_polynomial_pointers[poly_idx] = polynomial;
poly_idx++;
}

// Fold the prover polynomials
auto acc_poly_views = acc_prover_polynomials.get_all();
for (size_t inst_idx = 0; inst_idx < ProverInstances::NUM; inst_idx++) {
auto inst_poly_views = instances[inst_idx]->prover_polynomials.get_all();
for (auto [acc_poly_view, inst_poly_view] : zip_view(acc_poly_views, inst_poly_views)) {
for (size_t poly_idx = 0; poly_idx < inst_poly_view.size(); poly_idx++) {
(acc_poly_view)[poly_idx] += (inst_poly_view)[poly_idx] * lagranges[inst_idx];
}
}
}
next_accumulator->prover_polynomials = acc_prover_polynomials;

// Fold the witness commtiments and send them to the verifier
auto witness_labels = next_accumulator->commitment_labels.get_witness();
size_t comm_idx = 0;
for (auto& acc_comm : next_accumulator->witness_commitments.get_all()) {
acc_comm = Commitment::infinity();
size_t inst_idx = 0;
for (auto& instance : instances) {
acc_comm = acc_comm + instance->witness_commitments.get_all()[comm_idx] * lagranges[inst_idx];
inst_idx++;
}
transcript->send_to_verifier("next_" + witness_labels[comm_idx], acc_comm);
comm_idx++;
}

// Fold public data ϕ from all instances to produce ϕ* and add it to the transcript. As part of the folding
// 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);
size_t el_idx = 0;
for (auto& el : next_accumulator->public_inputs) {
size_t inst = 0;
for (auto& instance : instances) {
el += instance->public_inputs[el_idx] * lagranges[inst];
inst++;
}
transcript->send_to_verifier("next_public_input_" + std::to_string(el_idx), el);
el_idx++;
}

// Evaluate the combined batching challenge α univariate at challenge to obtain next α and send it to the
// verifier
next_accumulator->alpha = instances.alpha.evaluate(challenge);
transcript->send_to_verifier("next_alpha", next_accumulator->alpha);

// Evaluate each relation parameter univariate at challenge to obtain the folded relation parameters and send to
// the verifier
auto& combined_relation_parameters = instances.relation_parameters;
auto folded_relation_parameters = proof_system::RelationParameters<FF>{
combined_relation_parameters.eta.evaluate(challenge),
combined_relation_parameters.beta.evaluate(challenge),
combined_relation_parameters.gamma.evaluate(challenge),
combined_relation_parameters.public_input_delta.evaluate(challenge),
combined_relation_parameters.lookup_grand_product_delta.evaluate(challenge),
};
// TODO(https://github.com/AztecProtocol/barretenberg/issues/805): Add the relation parameters to the transcript
// together.
transcript->send_to_verifier("next_eta", folded_relation_parameters.eta);
transcript->send_to_verifier("next_beta", folded_relation_parameters.beta);
transcript->send_to_verifier("next_gamma", folded_relation_parameters.gamma);
transcript->send_to_verifier("next_public_input_delta", folded_relation_parameters.public_input_delta);
transcript->send_to_verifier("next_lookup_grand_product_delta",
folded_relation_parameters.lookup_grand_product_delta);
next_accumulator->relation_parameters = folded_relation_parameters;

// Fold the verification key and send it to the verifier as this is part of ϕ as well
auto acc_vk = std::make_shared<VerificationKey>(instances[0]->prover_polynomials.get_polynomial_size(),
instances[0]->public_inputs.size());
auto labels = next_accumulator->commitment_labels.get_precomputed();
size_t vk_idx = 0;
for (auto& vk : acc_vk->get_all()) {
size_t inst = 0;
vk = Commitment::infinity();
for (auto& instance : instances) {
vk = vk + (instance->verification_key->get_all()[vk_idx]) * lagranges[inst];
inst++;
}
transcript->send_to_verifier("next_" + labels[vk_idx], vk);
vk_idx++;
}
next_accumulator->verification_key = acc_vk;

return next_accumulator;
}

template class ProtoGalaxyProver_<ProverInstances_<honk::flavor::Ultra, 2>>;
template class ProtoGalaxyProver_<ProverInstances_<honk::flavor::GoblinUltra, 2>>;
} // namespace proof_system::honk
Loading

0 comments on commit 2583f8f

Please sign in to comment.