Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove the VerificationKey from ProverInstance #4908

Merged
merged 6 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,8 @@ 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);
auto verifier_instance = composer.create_verifier_instance(instance);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the recursive verifiers (except PG which doesnt have inner and outer), get_outer_composer uses a constexpr if clause to figure out the right type for the composer. Before, this was enough, as we were computing the verfication key through the composer. The most simple way to get the right type for teh verification key here is to create a VerifierInstance to limit the scope of this PR, as we are planning to refactor the recursive verfiers anyways.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a "TODO" just in case?

auto verifier = composer.create_verifier(verifier_instance->verification_key);
auto proof = prover.construct_proof();
bool verified = verifier.verify_proof(proof);

Expand All @@ -220,7 +222,8 @@ template <typename BuilderType> class GoblinRecursiveVerifierTest : public testi

/**
* @brief Construct a verifier circuit for a proof whose data has been tampered with. Expect failure
* TODO(bberg #656): For now we get a "bad" proof by arbitrarily tampering with bits in a valid proof. It would be
* TODO(bberg #656): For now we get a "bad" proof by arbitrarily tampering with bits in a valid proof. It would
be
* much nicer to explicitly change meaningful components, e.g. such that one of the multilinear evaluations is
* wrong. This is difficult now but should be straightforward if the proof is a struct.
*/
Expand All @@ -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,8 @@ 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);
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 All @@ -149,7 +147,8 @@ template <typename BuilderType> class RecursiveVerifierTest : public testing::Te
}

/**
* @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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tried running format.sh? This doesn't seem to be in line with our formatting, but I'm not sure

*
*/
static void test_recursive_verification()
Expand All @@ -164,9 +163,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 +176,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 +195,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 All @@ -204,7 +206,8 @@ template <typename BuilderType> class RecursiveVerifierTest : public testing::Te

/**
* @brief Construct a verifier circuit for a proof whose data has been tampered with. Expect failure
* TODO(bberg #656): For now we get a "bad" proof by arbitrarily tampering with bits in a valid proof. It would be
* TODO(bberg #656): For now we get a "bad" proof by arbitrarily tampering with bits in a valid proof. It would
be
* much nicer to explicitly change meaningful components, e.g. such that one of the multilinear evaluations is
* wrong. This is difficult now but should be straightforward if the proof is a struct.
*/
Expand All @@ -226,9 +229,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 @@ -92,7 +92,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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the verification key was removed from the prover, should we keep the field?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's correct, we don't need to, removed

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
Loading