Skip to content

Commit

Permalink
fix: Remove the VerificationKey from ProverInstance (#4908)
Browse files Browse the repository at this point in the history
In PR #4639 I introduced a tiny regression after doing the final master
merge: while the VerificationKey wasn't used anymore in the PG prover
workflow, as the PR aimed, it remained computed (and retrieved in some
places) from the prover instance which nullified the benefits of
precomputing verification keys for benchmarks. This PR addresses this.

# Benchmark

### Before

```
ninja: no work to do.
Benchmarking lock created at ~/BENCHMARK_IN_PROGRESS.
client_ivc_bench                                                                                                    100%   16MB  55.4MB/s   00:00    
2024-03-04T11:23:39+00:00
Running ./client_ivc_bench
Run on (16 X 3606.39 MHz CPU s)
CPU Caches:
  L1 Data 32 KiB (x8)
  L1 Instruction 32 KiB (x8)
  L2 Unified 1024 KiB (x8)
  L3 Unified 36608 KiB (x1)
Load Average: 0.13, 0.72, 0.40
----------------------------------------------------------------
Benchmark                      Time             CPU   Iterations
----------------------------------------------------------------
ClientIVCBench/Full/6      32647 ms        26672 ms            1
Benchmarking lock deleted.
```

### After 
```
client_ivc_bench                                                                                                    100%   16MB  52.0MB/s   00:00    
2024-03-04T11:19:11+00:00
Running ./client_ivc_bench
Run on (16 X 3596.99 MHz CPU s)
CPU Caches:
  L1 Data 32 KiB (x8)
  L1 Instruction 32 KiB (x8)
  L2 Unified 1024 KiB (x8)
  L3 Unified 36608 KiB (x1)
Load Average: 0.00, 0.00, 0.00
----------------------------------------------------------------
Benchmark                      Time             CPU   Iterations
----------------------------------------------------------------
ClientIVCBench/Full/6      29206 ms        24549 ms            1
```
  • Loading branch information
maramihali authored Mar 4, 2024
1 parent 46ee6a8 commit 8619c08
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 38 deletions.
9 changes: 5 additions & 4 deletions barretenberg/cpp/src/barretenberg/client_ivc/client_ivc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,14 @@ HonkProof ClientIVC::decider_prove() const
void ClientIVC::precompute_folding_verification_keys()
{
using VerifierInstance = VerifierInstance_<GoblinUltraFlavor>;
using VerificationKey = Flavor::VerificationKey;

ClientCircuit initial_function_circuit{ goblin.op_queue };
GoblinMockCircuits::construct_mock_function_circuit(initial_function_circuit);

// Initialise both the first prover and verifier accumulator from the inital function circuit
initialize(initial_function_circuit);
vks.first_func_vk = prover_fold_output.accumulator->verification_key;
vks.first_func_vk = std::make_shared<VerificationKey>(prover_fold_output.accumulator->proving_key);
auto initial_verifier_acc = std::make_shared<VerifierInstance>(vks.first_func_vk);

// Accumulate the next function circuit
Expand All @@ -106,14 +107,14 @@ void ClientIVC::precompute_folding_verification_keys()
auto function_fold_proof = accumulate(function_circuit);

// Create its verification key (we have called accumulate so it includes the recursive merge verifier)
vks.func_vk = prover_instance->verification_key;
vks.func_vk = std::make_shared<VerificationKey>(prover_instance->proving_key);

// Create the initial kernel iteration and precompute its verification key
ClientCircuit kernel_circuit{ goblin.op_queue };
auto kernel_acc = GoblinMockCircuits::construct_mock_folding_kernel(
kernel_circuit, { function_fold_proof, vks.func_vk }, {}, initial_verifier_acc);
auto kernel_fold_proof = accumulate(kernel_circuit);
vks.first_kernel_vk = prover_instance->verification_key;
vks.first_kernel_vk = std::make_shared<VerificationKey>(prover_instance->proving_key);

// Create another mock function circuit to run the full kernel
function_circuit = ClientCircuit{ goblin.op_queue };
Expand All @@ -126,7 +127,7 @@ void ClientIVC::precompute_folding_verification_keys()
kernel_circuit, { function_fold_proof, vks.func_vk }, { kernel_fold_proof, vks.first_kernel_vk }, kernel_acc);
kernel_fold_proof = accumulate(kernel_circuit);

vks.kernel_vk = prover_instance->verification_key;
vks.kernel_vk = std::make_shared<VerificationKey>(prover_instance->proving_key);

// Clean the ivc state
goblin.op_queue = std::make_shared<Goblin::OpQueue>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,20 @@ class ClientIVCTests : public ::testing::Test {
*/
TEST_F(ClientIVCTests, Full)
{
using VerificationKey = Flavor::VerificationKey;

ClientIVC ivc;
// Initialize IVC with function circuit
Builder function_circuit = create_mock_circuit(ivc);
ivc.initialize(function_circuit);

auto function_vk = ivc.prover_fold_output.accumulator->verification_key;
auto function_vk = std::make_shared<VerificationKey>(ivc.prover_fold_output.accumulator->proving_key);
auto foo_verifier_instance = std::make_shared<VerifierInstance>(function_vk);
// Accumulate kernel circuit (first kernel mocked as simple circuit since no folding proofs yet)
Builder kernel_circuit = create_mock_circuit(ivc);
FoldProof kernel_fold_proof = ivc.accumulate(kernel_circuit);
// This will have a different verification key because we added the recursive merge verification to the circuit
auto function_vk_with_merge = ivc.prover_instance->verification_key;
auto function_vk_with_merge = std::make_shared<VerificationKey>(ivc.prover_instance->proving_key);
auto kernel_vk = function_vk_with_merge;
auto intermediary_acc = update_accumulator_and_decide_native(
ivc.prover_fold_output.accumulator, kernel_fold_proof, foo_verifier_instance, kernel_vk);
Expand All @@ -137,7 +139,7 @@ TEST_F(ClientIVCTests, Full)
foo_verifier_instance = construct_mock_folding_kernel(
kernel_circuit, kernel_fold_output, function_fold_output, foo_verifier_instance);
FoldProof kernel_fold_proof = ivc.accumulate(kernel_circuit);
kernel_vk = ivc.prover_instance->verification_key;
kernel_vk = std::make_shared<VerificationKey>(ivc.prover_instance->proving_key);

intermediary_acc = update_accumulator_and_decide_native(
ivc.prover_fold_output.accumulator, kernel_fold_proof, intermediary_acc, kernel_vk);
Expand Down
7 changes: 5 additions & 2 deletions barretenberg/cpp/src/barretenberg/goblin/goblin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Goblin {
using RecursiveMergeVerifier = bb::stdlib::recursion::goblin::MergeRecursiveVerifier_<GoblinUltraCircuitBuilder>;
using MergeProver = bb::MergeProver_<GoblinUltraFlavor>;
using MergeVerifier = bb::MergeVerifier_<GoblinUltraFlavor>;
using VerificationKey = GoblinUltraFlavor::VerificationKey;
/**
* @brief Output of goblin::accumulate; an Ultra proof and the corresponding verification key
*
Expand Down Expand Up @@ -105,6 +106,7 @@ class Goblin {
// Construct a Honk proof for the main circuit
GoblinUltraComposer composer;
auto instance = composer.create_prover_instance(circuit_builder);
auto verification_key = std::make_shared<VerificationKey>(instance->proving_key);
auto prover = composer.create_prover(instance);
auto ultra_proof = prover.construct_proof();

Expand All @@ -116,7 +118,7 @@ class Goblin {
merge_proof_exists = true;
}

return { ultra_proof, instance->verification_key };
return { ultra_proof, verification_key };
};

/**
Expand Down Expand Up @@ -231,10 +233,11 @@ class Goblin {
// Construct a Honk proof for the main circuit
GoblinUltraComposer composer;
auto instance = composer.create_prover_instance(circuit_builder);
auto verification_key = std::make_shared<VerificationKey>(instance->proving_key);
auto prover = composer.create_prover(instance);
auto ultra_proof = prover.construct_proof();

accumulator = { ultra_proof, instance->verification_key };
accumulator = { ultra_proof, verification_key };

// TODO(https://github.com/AztecProtocol/barretenberg/issues/811): no merge prover for now since we're not
// mocking the first set of ecc ops
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ template <typename BuilderType> class GoblinRecursiveVerifierTest : public testi
// Compute native verification key
InnerComposer inner_composer;
auto instance = inner_composer.create_prover_instance(inner_circuit);
auto prover = inner_composer.create_prover(instance); // A prerequisite for computing VK
auto verification_key = instance->verification_key;
auto verification_key = std::make_shared<typename InnerFlavor::VerificationKey>(instance->proving_key);
// Instantiate the recursive verifier using the native verification key
RecursiveVerifier verifier{ &outer_circuit, verification_key };

Expand All @@ -166,7 +165,8 @@ template <typename BuilderType> class GoblinRecursiveVerifierTest : public testi
}

/**
* @brief Construct a recursive verification circuit for the proof of an inner circuit then call check_circuit on it
* @brief Construct a recursive verification circuit for the proof of an inner circuit then call check_circuit on
it
*
*/
static void test_recursive_verification()
Expand All @@ -177,12 +177,13 @@ template <typename BuilderType> class GoblinRecursiveVerifierTest : public testi
// Generate a proof over the inner circuit
InnerComposer inner_composer;
auto instance = inner_composer.create_prover_instance(inner_circuit);
auto verification_key = std::make_shared<typename InnerFlavor::VerificationKey>(instance->proving_key);
auto inner_prover = inner_composer.create_prover(instance);
auto inner_proof = inner_prover.construct_proof();

// Create a recursive verification circuit for the proof of the inner circuit
OuterBuilder outer_circuit;
RecursiveVerifier verifier{ &outer_circuit, instance->verification_key };
RecursiveVerifier verifier{ &outer_circuit, verification_key };
auto pairing_points = verifier.verify_proof(inner_proof);
info("Recursive Verifier Goblin: num gates = ", outer_circuit.num_gates);

Expand All @@ -191,7 +192,7 @@ template <typename BuilderType> class GoblinRecursiveVerifierTest : public testi

// Check 1: Perform native verification then perform the pairing on the outputs of the recursive
// verifier and check that the result agrees.
auto native_verifier = inner_composer.create_verifier(instance->verification_key);
auto native_verifier = inner_composer.create_verifier(verification_key);
auto native_result = native_verifier.verify_proof(inner_proof);
auto recursive_result = native_verifier.key->pcs_verification_key->pairing_check(pairing_points[0].get_value(),
pairing_points[1].get_value());
Expand All @@ -210,7 +211,9 @@ template <typename BuilderType> class GoblinRecursiveVerifierTest : public testi
auto composer = get_outer_composer<OuterBuilder>();
auto instance = composer.create_prover_instance(outer_circuit);
auto prover = composer.create_prover(instance);
auto verifier = composer.create_verifier(instance->verification_key);
TODO: // github.com/AztecProtocol/barretenberg/issues/892
auto verifier_instance = composer.create_verifier_instance(instance);
auto verifier = composer.create_verifier(verifier_instance->verification_key);
auto proof = prover.construct_proof();
bool verified = verifier.verify_proof(proof);

Expand Down Expand Up @@ -241,9 +244,12 @@ template <typename BuilderType> class GoblinRecursiveVerifierTest : public testi
inner_prover.transcript->serialize_full_transcript();
inner_proof = inner_prover.export_proof();

// Generate the corresponding inner verification key
auto inner_verification_key = std::make_shared<typename InnerFlavor::VerificationKey>(instance->proving_key);

// Create a recursive verification circuit for the proof of the inner circuit
OuterBuilder outer_circuit;
RecursiveVerifier verifier{ &outer_circuit, instance->verification_key };
RecursiveVerifier verifier{ &outer_circuit, inner_verification_key };
verifier.verify_proof(inner_proof);

// We expect the circuit check to fail due to the bad proof
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ class RecursiveMergeVerifierTest : public testing::Test {
GoblinUltraComposer composer;
auto instance = composer.create_prover_instance(outer_circuit);
auto prover = composer.create_prover(instance);
auto verifier = composer.create_verifier(instance->verification_key);
// TODO: github.com/AztecProtocol/barretenberg/issues/892
auto verifier_instance = composer.create_verifier_instance(instance);
auto verifier = composer.create_verifier(verifier_instance->verification_key);
auto proof = prover.construct_proof();
bool verified = verifier.verify_proof(proof);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ template <typename RecursiveFlavor> class ProtoGalaxyRecursiveTests : public tes
using Curve = bn254<Builder>;
using Commitment = typename NativeFlavor::Commitment;
using FF = typename NativeFlavor::FF;
using VerificationKey = typename NativeFlavor::VerificationKey;

using RecursiveVerifierInstances = ::bb::stdlib::recursion::honk::RecursiveVerifierInstances_<RecursiveFlavor, 2>;
using FoldingRecursiveVerifier = ProtoGalaxyRecursiveVerifier_<RecursiveVerifierInstances>;
Expand Down Expand Up @@ -205,7 +206,8 @@ template <typename RecursiveFlavor> class ProtoGalaxyRecursiveTests : public tes
auto composer = Composer();
auto instance = composer.create_prover_instance(folding_circuit);
auto prover = composer.create_prover(instance);
auto verifier = composer.create_verifier(instance->verification_key);
auto verification_key = std::make_shared<VerificationKey>(instance->proving_key);
auto verifier = composer.create_verifier(verification_key);
auto proof = prover.construct_proof();
bool verified = verifier.verify_proof(proof);

Expand Down Expand Up @@ -295,7 +297,8 @@ template <typename RecursiveFlavor> class ProtoGalaxyRecursiveTests : public tes
auto composer = Composer();
auto instance = composer.create_prover_instance(decider_circuit);
auto prover = composer.create_prover(instance);
auto verifier = composer.create_verifier(instance->verification_key);
auto verification_key = std::make_shared<VerificationKey>(instance->proving_key);
auto verifier = composer.create_verifier(verification_key);
auto proof = prover.construct_proof();
bool verified = verifier.verify_proof(proof);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ template <typename BuilderType> class RecursiveVerifierTest : public testing::Te
using RecursiveFlavor = UltraRecursiveFlavor_<BuilderType>;
using RecursiveVerifier = UltraRecursiveVerifier_<RecursiveFlavor>;
using OuterBuilder = BuilderType;
using VerificationKey = typename RecursiveVerifier::VerificationKey;

// Helper for getting composer for prover/verifier of recursive (outer) circuit
template <typename BuilderT> static auto get_outer_composer()
Expand Down Expand Up @@ -133,8 +132,7 @@ template <typename BuilderType> class RecursiveVerifierTest : public testing::Te
// Compute native verification key
InnerComposer inner_composer;
auto instance = inner_composer.create_prover_instance(inner_circuit);
auto prover = inner_composer.create_prover(instance); // A prerequisite for computing VK
auto verification_key = instance->verification_key;
auto verification_key = std::make_shared<typename InnerFlavor::VerificationKey>(instance->proving_key);
// Instantiate the recursive verifier using the native verification key
RecursiveVerifier verifier{ &outer_circuit, verification_key };

Expand Down Expand Up @@ -164,9 +162,11 @@ template <typename BuilderType> class RecursiveVerifierTest : public testing::Te
auto inner_prover = inner_composer.create_prover(instance);
auto inner_proof = inner_prover.construct_proof();

auto verification_key = std::make_shared<typename InnerFlavor::VerificationKey>(instance->proving_key);

// Create a recursive verification circuit for the proof of the inner circuit
OuterBuilder outer_circuit;
RecursiveVerifier verifier{ &outer_circuit, instance->verification_key };
RecursiveVerifier verifier{ &outer_circuit, verification_key };
auto pairing_points = verifier.verify_proof(inner_proof);
info("Recursive Verifier Ultra: num gates = ", outer_circuit.num_gates);

Expand All @@ -175,7 +175,7 @@ template <typename BuilderType> class RecursiveVerifierTest : public testing::Te

// Check 1: Perform native verification then perform the pairing on the outputs of the recursive
// verifier and check that the result agrees.
auto native_verifier = inner_composer.create_verifier(instance->verification_key);
auto native_verifier = inner_composer.create_verifier(verification_key);
auto native_result = native_verifier.verify_proof(inner_proof);
auto recursive_result = native_verifier.key->pcs_verification_key->pairing_check(pairing_points[0].get_value(),
pairing_points[1].get_value());
Expand All @@ -194,7 +194,8 @@ template <typename BuilderType> class RecursiveVerifierTest : public testing::Te
auto composer = get_outer_composer<OuterBuilder>();
auto instance = composer.create_prover_instance(outer_circuit);
auto prover = composer.create_prover(instance);
auto verifier = composer.create_verifier(instance->verification_key);
auto verifier_instance = composer.create_verifier_instance(instance);
auto verifier = composer.create_verifier(verifier_instance->verification_key);
auto proof = prover.construct_proof();
bool verified = verifier.verify_proof(proof);

Expand Down Expand Up @@ -226,9 +227,11 @@ template <typename BuilderType> class RecursiveVerifierTest : public testing::Te
inner_prover.transcript->serialize_full_transcript();
inner_proof = inner_prover.export_proof();

auto verification_key = std::make_shared<typename InnerFlavor::VerificationKey>(instance->proving_key);

// Create a recursive verification circuit for the proof of the inner circuit
OuterBuilder outer_circuit;
RecursiveVerifier verifier{ &outer_circuit, instance->verification_key };
RecursiveVerifier verifier{ &outer_circuit, verification_key };
verifier.verify_proof(inner_proof);

// We expect the circuit check to fail due to the bad proof
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ template <class Flavor> class ProverInstance_ {
// currently commitment_key needs to be here, and not accessed through the proving key, since sometimes the proving
// key is null during protogalaxy proving (TODO(https://github.com/AztecProtocol/barretenberg/issues/881)?)
std::shared_ptr<CommitmentKey> commitment_key;
std::shared_ptr<VerificationKey> verification_key;

ProverPolynomials prover_polynomials;
WitnessCommitments witness_commitments;
Expand Down Expand Up @@ -92,7 +91,6 @@ template <class Flavor> class ProverInstance_ {

sorted_polynomials = construct_sorted_list_polynomials<Flavor>(circuit, dyadic_circuit_size);

verification_key = std::make_shared<VerificationKey>(proving_key);
commitment_key = proving_key->commitment_key;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ TEST_F(DataBusComposerTests, CallDataRead)
auto instance = composer.create_prover_instance(builder);
// For debugging, use "instance_inspector::print_databus_info(instance)"
auto prover = composer.create_prover(instance);
auto verifier = composer.create_verifier(instance->verification_key);
auto verification_key = std::make_shared<GoblinUltraFlavor::VerificationKey>(instance->proving_key);
auto verifier = composer.create_verifier(verification_key);
auto proof = prover.construct_proof();
bool verified = verifier.verify_proof(proof);
EXPECT_TRUE(verified);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ class GoblinUltraHonkComposerTests : public ::testing::Test {
{
auto instance = composer.create_prover_instance(builder);
auto prover = composer.create_prover(instance);
auto verifier = composer.create_verifier(instance->verification_key);
auto verification_key = std::make_shared<GoblinUltraFlavor::VerificationKey>(instance->proving_key);
auto verifier = composer.create_verifier(verification_key);
auto proof = prover.construct_proof();
bool verified = verifier.verify_proof(proof);

Expand Down
Loading

0 comments on commit 8619c08

Please sign in to comment.