Skip to content

Commit

Permalink
refactor: bye bye shared ptrs for ultra/goblin ultra proving_keys :) (#…
Browse files Browse the repository at this point in the history
…5407)

Partially addresses
#4885.

Removes shared_ptr usage for ultra/goblin_ultra proving key.

Shouldn't affect the benchmarks much, but for some reason it seems to
have decreased slightly.

```
--------------------------------------------------------------------------------
Benchmark                      Time             CPU   Iterations UserCounters...
--------------------------------------------------------------------------------
ClientIVCBench/Full/6      22099 ms        17172 ms            1 Decider::construct_proof=1 Decider::construct_proof(t)=754.682M ECCVMComposer::compute_commitment_key=1 ECCVMComposer::compute_commitment_key(t)=3.65834M ECCVMComposer::compute_witness=1 ECCVMComposer::compute_witness(t)=130.21M ECCVMComposer::create_prover=1 ECCVMComposer::create_prover(t)=149.963M ECCVMComposer::create_proving_key=1 ECCVMComposer::create_proving_key(t)=15.8747M ECCVMProver::construct_proof=1 ECCVMProver::construct_proof(t)=1.75623G Goblin::merge=11 Goblin::merge(t)=142.793M GoblinTranslatorCircuitBuilder::constructor=1 GoblinTranslatorCircuitBuilder::constructor(t)=58.4782M GoblinTranslatorProver=1 GoblinTranslatorProver(t)=144.954M GoblinTranslatorProver::construct_proof=1 GoblinTranslatorProver::construct_proof(t)=962.221M ProtoGalaxyProver_::accumulator_update_round=10 ProtoGalaxyProver_::accumulator_update_round(t)=294.093M ProtoGalaxyProver_::combiner_quotient_round=10 ProtoGalaxyProver_::combiner_quotient_round(t)=5.93984G ProtoGalaxyProver_::perturbator_round=10 ProtoGalaxyProver_::perturbator_round(t)=1.29776G ProtoGalaxyProver_::preparation_round=10 ProtoGalaxyProver_::preparation_round(t)=4.1465G ProtogalaxyProver::fold_instances=10 ProtogalaxyProver::fold_instances(t)=11.6782G ProverInstance(Circuit&)=11 ProverInstance(Circuit&)(t)=1.97873G batch_mul_with_endomorphism=30 batch_mul_with_endomorphism(t)=565.226M commit=426 commit(t)=4.01591G compute_combiner=10 compute_combiner(t)=5.93777G compute_perturbator=9 compute_perturbator(t)=1.29745G compute_univariate=48 compute_univariate(t)=1.42388G construct_circuits=6 construct_circuits(t)=4.44634G
Benchmarking lock deleted.
client_ivc_bench.json                                                            100% 3999   115.8KB/s   00:00    
function                                        ms     % sum
construct_circuits(t)                         4446    20.33%
ProverInstance(Circuit&)(t)                   1979     9.05%
ProtogalaxyProver::fold_instances(t)         11678    53.40%
Decider::construct_proof(t)                    755     3.45%
ECCVMComposer::create_prover(t)                150     0.69%
ECCVMProver::construct_proof(t)               1756     8.03%
GoblinTranslatorProver::construct_proof(t)     962     4.40%
Goblin::merge(t)                               143     0.65%

Total time accounted for: 21869ms/22099ms = 98.96%

Major contributors:
function                                        ms    % sum
commit(t)                                     4016   18.36%
compute_combiner(t)                           5938   27.15%
compute_perturbator(t)                        1297    5.93%
compute_univariate(t)                         1424    6.51%

Breakdown of ProtogalaxyProver::fold_instances:
ProtoGalaxyProver_::preparation_round(t)           4147    35.51%
ProtoGalaxyProver_::perturbator_round(t)           1298    11.11%
ProtoGalaxyProver_::combiner_quotient_round(t)     5940    50.86%
ProtoGalaxyProver_::accumulator_update_round(t)     294     2.52%
```

Please read [contributing guidelines](CONTRIBUTING.md) and remove this
line.
  • Loading branch information
lucasxia01 authored Apr 1, 2024
1 parent f9be2f2 commit b94d0db
Show file tree
Hide file tree
Showing 29 changed files with 234 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "barretenberg/benchmark/ultra_bench/mock_circuits.hpp"
#include "barretenberg/common/op_count_google_bench.hpp"
#include "barretenberg/stdlib_circuit_builders/ultra_circuit_builder.hpp"
#include "barretenberg/ultra_honk/oink_prover.hpp"
#include "barretenberg/ultra_honk/ultra_prover.hpp"

using namespace benchmark;
Expand Down Expand Up @@ -44,15 +45,16 @@ BB_PROFILE static void test_round_inner(State& state, GoblinUltraProver& prover,
BB_REPORT_OP_COUNT_BENCH_CANCEL();
}
};

time_if_index(PREAMBLE, [&] { prover.oink_prover.execute_preamble_round(); });
time_if_index(WIRE_COMMITMENTS, [&] { prover.oink_prover.execute_wire_commitments_round(); });
time_if_index(SORTED_LIST_ACCUMULATOR, [&] { prover.oink_prover.execute_sorted_list_accumulator_round(); });
time_if_index(LOG_DERIVATIVE_INVERSE, [&] { prover.oink_prover.execute_log_derivative_inverse_round(); });
time_if_index(GRAND_PRODUCT_COMPUTATION, [&] { prover.oink_prover.execute_grand_product_computation_round(); });
time_if_index(GENERATE_ALPHAS, [&] { prover.instance->alphas = prover.oink_prover.generate_alphas_round(); });
OinkProver<GoblinUltraFlavor> oink_prover(prover.instance->proving_key, prover.transcript);
time_if_index(PREAMBLE, [&] { oink_prover.execute_preamble_round(); });
time_if_index(WIRE_COMMITMENTS, [&] { oink_prover.execute_wire_commitments_round(); });
time_if_index(SORTED_LIST_ACCUMULATOR, [&] { oink_prover.execute_sorted_list_accumulator_round(); });
time_if_index(LOG_DERIVATIVE_INVERSE, [&] { oink_prover.execute_log_derivative_inverse_round(); });
time_if_index(GRAND_PRODUCT_COMPUTATION, [&] { oink_prover.execute_grand_product_computation_round(); });
time_if_index(GENERATE_ALPHAS, [&] { prover.instance->alphas = oink_prover.generate_alphas_round(); });
// we need to get the relation_parameters and prover_polynomials from the oink_prover
prover.instance->relation_parameters = prover.oink_prover.relation_parameters;
prover.instance->proving_key = std::move(oink_prover.proving_key);
prover.instance->relation_parameters = oink_prover.relation_parameters;
prover.instance->prover_polynomials = GoblinUltraFlavor::ProverPolynomials(prover.instance->proving_key);
time_if_index(RELATION_CHECK, [&] { prover.execute_relation_check_rounds(); });
time_if_index(ZEROMORPH, [&] { prover.execute_zeromorph_rounds(); });
Expand Down
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->proving_key->log_circuit_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 All @@ -49,5 +49,5 @@ TEST_F(MockKernelTest, PinFoldingKernelSizes)
{ func_fold_proof, ivc.vks.func_vk },
kernel_acc);
auto instance = std::make_shared<ClientIVC::ProverInstance>(kernel_circuit);
EXPECT_EQ(instance->proving_key->log_circuit_size, 17);
EXPECT_EQ(instance->proving_key.log_circuit_size, 17);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
namespace bb {

template <class Flavor>
void ExecutionTrace_<Flavor>::populate(Builder& builder,
const std::shared_ptr<typename Flavor::ProvingKey>& proving_key)
void ExecutionTrace_<Flavor>::populate(Builder& builder, typename Flavor::ProvingKey& proving_key)
{
// Construct wire polynomials, selector polynomials, and copy cycles from raw circuit data
auto trace_data = construct_trace_data(builder, proving_key->circuit_size);
auto trace_data = construct_trace_data(builder, proving_key.circuit_size);

add_wires_and_selectors_to_proving_key(trace_data, builder, proving_key);

Expand All @@ -23,46 +22,48 @@ void ExecutionTrace_<Flavor>::populate(Builder& builder,
}

// Compute the permutation argument polynomials (sigma/id) and add them to proving key
compute_permutation_argument_polynomials<Flavor>(builder, proving_key.get(), trace_data.copy_cycles);
compute_permutation_argument_polynomials<Flavor>(builder, &proving_key, trace_data.copy_cycles);
}

template <class Flavor>
void ExecutionTrace_<Flavor>::add_wires_and_selectors_to_proving_key(
TraceData& trace_data, Builder& builder, const std::shared_ptr<typename Flavor::ProvingKey>& proving_key)
void ExecutionTrace_<Flavor>::add_wires_and_selectors_to_proving_key(TraceData& trace_data,
Builder& builder,
typename Flavor::ProvingKey& proving_key)
{
if constexpr (IsHonkFlavor<Flavor>) {
for (auto [pkey_wire, trace_wire] : zip_view(proving_key->get_wires(), trace_data.wires)) {
for (auto [pkey_wire, trace_wire] : zip_view(proving_key.get_wires(), trace_data.wires)) {
pkey_wire = trace_wire.share();
}
for (auto [pkey_selector, trace_selector] : zip_view(proving_key->get_selectors(), trace_data.selectors)) {
for (auto [pkey_selector, trace_selector] : zip_view(proving_key.get_selectors(), trace_data.selectors)) {
pkey_selector = trace_selector.share();
}
proving_key->pub_inputs_offset = trace_data.pub_inputs_offset;
proving_key.pub_inputs_offset = trace_data.pub_inputs_offset;
} else if constexpr (IsPlonkFlavor<Flavor>) {
for (size_t idx = 0; idx < trace_data.wires.size(); ++idx) {
std::string wire_tag = "w_" + std::to_string(idx + 1) + "_lagrange";
proving_key->polynomial_store.put(wire_tag, std::move(trace_data.wires[idx]));
proving_key.polynomial_store.put(wire_tag, std::move(trace_data.wires[idx]));
}
for (size_t idx = 0; idx < trace_data.selectors.size(); ++idx) {
proving_key->polynomial_store.put(builder.selector_names[idx] + "_lagrange",
std::move(trace_data.selectors[idx]));
proving_key.polynomial_store.put(builder.selector_names[idx] + "_lagrange",
std::move(trace_data.selectors[idx]));
}
}
}

template <class Flavor>
void ExecutionTrace_<Flavor>::add_memory_records_to_proving_key(
TraceData& trace_data, Builder& builder, const std::shared_ptr<typename Flavor::ProvingKey>& proving_key)
void ExecutionTrace_<Flavor>::add_memory_records_to_proving_key(TraceData& trace_data,
Builder& builder,
typename Flavor::ProvingKey& proving_key)
requires IsUltraPlonkOrHonk<Flavor>
{
ASSERT(proving_key->memory_read_records.empty() && proving_key->memory_write_records.empty());
ASSERT(proving_key.memory_read_records.empty() && proving_key.memory_write_records.empty());

// Update indices of RAM/ROM reads/writes based on where block containing these gates sits in the trace
for (auto& index : builder.memory_read_records) {
proving_key->memory_read_records.emplace_back(index + trace_data.ram_rom_offset);
proving_key.memory_read_records.emplace_back(index + trace_data.ram_rom_offset);
}
for (auto& index : builder.memory_write_records) {
proving_key->memory_write_records.emplace_back(index + trace_data.ram_rom_offset);
proving_key.memory_write_records.emplace_back(index + trace_data.ram_rom_offset);
}
}

Expand Down Expand Up @@ -135,32 +136,32 @@ template <class Flavor> void ExecutionTrace_<Flavor>::populate_public_inputs_blo
}

template <class Flavor>
void ExecutionTrace_<Flavor>::add_ecc_op_wires_to_proving_key(
Builder& builder, const std::shared_ptr<typename Flavor::ProvingKey>& proving_key)
void ExecutionTrace_<Flavor>::add_ecc_op_wires_to_proving_key(Builder& builder,
typename Flavor::ProvingKey& proving_key)
requires IsGoblinFlavor<Flavor>
{
// Initialize the ecc op wire polynomials to zero on the whole domain
std::array<Polynomial, NUM_WIRES> op_wire_polynomials;
for (auto& poly : op_wire_polynomials) {
poly = Polynomial{ proving_key->circuit_size };
poly = Polynomial{ proving_key.circuit_size };
}
Polynomial ecc_op_selector{ proving_key->circuit_size };
Polynomial ecc_op_selector{ proving_key.circuit_size };

// Copy the ecc op data from the conventional wires into the op wires over the range of ecc op gates
const size_t op_wire_offset = Flavor::has_zero_row ? 1 : 0;
for (auto [ecc_op_wire, wire] : zip_view(op_wire_polynomials, proving_key->get_wires())) {
for (auto [ecc_op_wire, wire] : zip_view(op_wire_polynomials, proving_key.get_wires())) {
for (size_t i = 0; i < builder.blocks.ecc_op.size(); ++i) {
size_t idx = i + op_wire_offset;
ecc_op_wire[idx] = wire[idx];
ecc_op_selector[idx] = 1; // construct the selector as the indicator on the ecc op block
}
}

proving_key->ecc_op_wire_1 = op_wire_polynomials[0].share();
proving_key->ecc_op_wire_2 = op_wire_polynomials[1].share();
proving_key->ecc_op_wire_3 = op_wire_polynomials[2].share();
proving_key->ecc_op_wire_4 = op_wire_polynomials[3].share();
proving_key->lagrange_ecc_op = ecc_op_selector.share();
proving_key.ecc_op_wire_1 = op_wire_polynomials[0].share();
proving_key.ecc_op_wire_2 = op_wire_polynomials[1].share();
proving_key.ecc_op_wire_3 = op_wire_polynomials[2].share();
proving_key.ecc_op_wire_4 = op_wire_polynomials[3].share();
proving_key.lagrange_ecc_op = ecc_op_selector.share();
}

template class ExecutionTrace_<UltraFlavor>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ template <class Flavor> class ExecutionTrace_ {
*
* @param builder
*/
static void populate(Builder& builder, const std::shared_ptr<ProvingKey>&);
static void populate(Builder& builder, ProvingKey&);

private:
/**
Expand All @@ -54,7 +54,7 @@ template <class Flavor> class ExecutionTrace_ {
*/
static void add_wires_and_selectors_to_proving_key(TraceData& trace_data,
Builder& builder,
const std::shared_ptr<typename Flavor::ProvingKey>& proving_key);
typename Flavor::ProvingKey& proving_key);

/**
* @brief Add the memory records indicating which rows correspond to RAM/ROM reads/writes
Expand All @@ -70,7 +70,7 @@ template <class Flavor> class ExecutionTrace_ {
*/
static void add_memory_records_to_proving_key(TraceData& trace_data,
Builder& builder,
const std::shared_ptr<typename Flavor::ProvingKey>& proving_key)
typename Flavor::ProvingKey& proving_key)
requires IsUltraPlonkOrHonk<Flavor>;

/**
Expand Down Expand Up @@ -98,8 +98,7 @@ template <class Flavor> class ExecutionTrace_ {
* @param builder
* @param proving_key
*/
static void add_ecc_op_wires_to_proving_key(Builder& builder,
const std::shared_ptr<typename Flavor::ProvingKey>& proving_key)
static void add_ecc_op_wires_to_proving_key(Builder& builder, typename Flavor::ProvingKey& proving_key)
requires IsGoblinFlavor<Flavor>;
};

Expand Down
13 changes: 0 additions & 13 deletions barretenberg/cpp/src/barretenberg/flavor/flavor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,19 +165,6 @@ class VerificationKey_ : public PrecomputedCommitments {
this->log_circuit_size = numeric::get_msb(circuit_size);
this->num_public_inputs = num_public_inputs;
};

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->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);
}
}
};

// Because of how Gemini is written, is importat to put the polynomials out in this order.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ TEST_F(MockCircuitsPinning, FunctionSizes)
GoblinMockCircuits::construct_mock_function_circuit(app_circuit, large);
auto instance = std::make_shared<ProverInstance>(app_circuit);
if (large) {
EXPECT_EQ(instance->proving_key->log_circuit_size, 19);
EXPECT_EQ(instance->proving_key.log_circuit_size, 19);
} else {
EXPECT_EQ(instance->proving_key->log_circuit_size, 17);
EXPECT_EQ(instance->proving_key.log_circuit_size, 17);
};
};
run_test(true);
Expand All @@ -51,9 +51,9 @@ TEST_F(MockCircuitsPinning, RecursionKernelSizes)

auto instance = std::make_shared<ProverInstance>(kernel_circuit);
if (large) {
EXPECT_EQ(instance->proving_key->log_circuit_size, 17);
EXPECT_EQ(instance->proving_key.log_circuit_size, 17);
} else {
EXPECT_EQ(instance->proving_key->log_circuit_size, 17);
EXPECT_EQ(instance->proving_key.log_circuit_size, 17);
};
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ std::shared_ptr<plonk::proving_key> StandardComposer::compute_proving_key(Circui
subgroup_size, circuit_constructor.public_inputs.size(), crs, CircuitType::STANDARD);

// Construct and add to proving key the wire, selector and copy constraint polynomials
Trace::populate(circuit_constructor, circuit_proving_key);
Trace::populate(circuit_constructor, *circuit_proving_key);

// Make all selectors nonzero
enforce_nonzero_selector_polynomials(circuit_constructor, circuit_proving_key.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ std::shared_ptr<proving_key> UltraComposer::compute_proving_key(CircuitBuilder&
std::make_shared<plonk::proving_key>(subgroup_size, circuit.public_inputs.size(), crs, CircuitType::ULTRA);

// Construct and add to proving key the wire, selector and copy constraint polynomials
Trace::populate(circuit, circuit_proving_key);
Trace::populate(circuit, *circuit_proving_key);

enforce_nonzero_selector_polynomials(circuit, circuit_proving_key.get());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void print_databus_info(auto& prover_instance)
{
info("\nInstance Inspector: Printing databus gate info.");
auto& key = prover_instance->proving_key;
for (size_t idx = 0; idx < prover_instance->proving_key->circuit_size; ++idx) {
for (size_t idx = 0; idx < prover_instance->proving_key.circuit_size; ++idx) {
if (key->q_busread[idx] == 1) {
info("idx = ", idx);
info("q_busread = ", key->q_busread[idx]);
Expand Down
12 changes: 6 additions & 6 deletions barretenberg/cpp/src/barretenberg/protogalaxy/combiner.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +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->proving_key = std::make_shared<Flavor::ProvingKey>();
instance->proving_key->circuit_size = 2;
instance->proving_key = Flavor::ProvingKey();
instance->proving_key.circuit_size = 2;
instance_data[idx] = instance;
}

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

Expand Down Expand Up @@ -170,8 +170,8 @@ TEST(Protogalaxy, CombinerOn4Instances)
auto prover_polynomials = get_zero_prover_polynomials<Flavor>(
/*log_circuit_size=*/1);
instance->prover_polynomials = std::move(prover_polynomials);
instance->proving_key = std::make_shared<Flavor::ProvingKey>();
instance->proving_key->circuit_size = 2;
instance->proving_key = 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 @@ -17,7 +17,7 @@ DeciderProver_<Flavor>::DeciderProver_(const std::shared_ptr<Instance>& inst,
const std::shared_ptr<Transcript>& transcript)
: accumulator(std::move(inst))
, transcript(transcript)
, commitment_key(inst->proving_key->commitment_key)
, commitment_key(inst->proving_key.commitment_key)
{}

/**
Expand All @@ -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->proving_key->circuit_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 @@ -79,7 +79,7 @@ template <typename Flavor> class ProtoGalaxyTests : public testing::Test {

static void check_accumulator_target_sum_manual(std::shared_ptr<ProverInstance>& accumulator, bool expected_result)
{
auto instance_size = accumulator->proving_key->circuit_size;
auto instance_size = accumulator->proving_key.circuit_size;
auto expected_honk_evals = ProtoGalaxyProver::compute_full_honk_evaluations(
accumulator->prover_polynomials, accumulator->alphas, accumulator->relation_parameters);
// Construct pow(\vec{betas*}) as in the paper
Expand Down Expand Up @@ -124,13 +124,13 @@ template <typename Flavor> class ProtoGalaxyTests : public testing::Test {
instance->relation_parameters.beta = FF::random_element();
instance->relation_parameters.gamma = FF::random_element();

instance->proving_key->compute_sorted_accumulator_polynomials(instance->relation_parameters.eta,
instance->relation_parameters.eta_two,
instance->relation_parameters.eta_three);
instance->proving_key.compute_sorted_accumulator_polynomials(instance->relation_parameters.eta,
instance->relation_parameters.eta_two,
instance->relation_parameters.eta_three);
if constexpr (IsGoblinFlavor<Flavor>) {
instance->proving_key->compute_logderivative_inverse(instance->relation_parameters);
instance->proving_key.compute_logderivative_inverse(instance->relation_parameters);
}
instance->proving_key->compute_grand_product_polynomials(instance->relation_parameters);
instance->proving_key.compute_grand_product_polynomials(instance->relation_parameters);
instance->prover_polynomials = ProverPolynomials(instance->proving_key);

for (auto& alpha : instance->alphas) {
Expand Down
Loading

0 comments on commit b94d0db

Please sign in to comment.