Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
maramihali committed Feb 19, 2024
1 parent 2188c57 commit ffaddfc
Show file tree
Hide file tree
Showing 14 changed files with 190 additions and 272 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,33 @@ class IvcBench : public benchmark::Fixture {
Builder initial_function_circuit{ ivc.goblin.op_queue };
GoblinMockCircuits::construct_mock_function_circuit(initial_function_circuit);
ivc.initialize(initial_function_circuit);
auto kernel_acc = std::make_shared<ClientIVC::VerifierInstance>();
kernel_acc->verification_key = ivc.vks[0];
auto kernel_verifeir_accumulator = std::make_shared<ClientIVC::VerifierInstance>();
kernel_verifeir_accumulator->verification_key = ivc.vks[0];

Builder function_circuit{ ivc.goblin.op_queue };
GoblinMockCircuits::construct_mock_function_circuit(function_circuit);
auto function_fold_proof = ivc.accumulate(function_circuit);
FoldOutput function_fold_output = { function_fold_proof, ivc.vks[1] };

Builder kernel_circuit{ ivc.goblin.op_queue };
kernel_acc =
GoblinMockCircuits::construct_mock_folding_kernel(kernel_circuit, function_fold_output, {}, kernel_acc);
kernel_verifeir_accumulator = GoblinMockCircuits::construct_mock_folding_kernel(
kernel_circuit, function_fold_output, {}, kernel_verifeir_accumulator);
auto kernel_fold_proof = ivc.accumulate(kernel_circuit);
FoldOutput kernel_fold_output = { kernel_fold_proof, ivc.vks[2] };
auto NUM_CIRCUITS = static_cast<size_t>(state.range(0));
// Subtract one to account for the "initialization" round above
NUM_CIRCUITS -= 1;
for (size_t circuit_idx = 0; circuit_idx < NUM_CIRCUITS; ++circuit_idx) {
// Accumulate function circuit
Builder function_circuit{ ivc.goblin.op_queue };
GoblinMockCircuits::construct_mock_function_circuit(function_circuit);
auto function_fold_proof = ivc.accumulate(function_circuit);
function_fold_output = { function_fold_proof, ivc.vks[1] };

// Accumulate kernel circuit
Builder kernel_circuit{ ivc.goblin.op_queue };
kernel_acc = GoblinMockCircuits::construct_mock_folding_kernel(
kernel_circuit, function_fold_output, kernel_fold_output, kernel_acc);
kernel_verifeir_accumulator = GoblinMockCircuits::construct_mock_folding_kernel(
kernel_circuit, function_fold_output, kernel_fold_output, kernel_verifeir_accumulator);

kernel_fold_proof = ivc.accumulate(kernel_circuit);
kernel_fold_output = { kernel_fold_proof, ivc.vks[3] };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,40 @@ template <typename Composer>
void _bench_round(::benchmark::State& state,
void (*F)(ProtoGalaxyProver_<ProverInstances_<typename Composer::Flavor, 2>>&))
{
// using Flavor = typename Composer::Flavor;
// using Instance = ProverInstance_<Flavor>;
// using Builder = typename Flavor::CircuitBuilder;
static_cast<void>(state);
static_cast<void>(F);
// bb::srs::init_crs_factory("../srs_db/ignition");
// auto log2_num_gates = static_cast<size_t>(state.range(0));
// auto composer = Composer();

// const auto construct_instance = [&]() {
// Builder builder;
// if constexpr (std::same_as<Flavor, GoblinUltraFlavor>) {
// GoblinMockCircuits::construct_arithmetic_circuit(builder, log2_num_gates);
// } else {
// static_assert(std::same_as<Flavor, UltraFlavor>);
// bb::mock_proofs::generate_basic_arithmetic_circuit(builder, log2_num_gates);
// }
// return composer.create_prover_instance(builder);
// };

// std::shared_ptr<Instance> instance_1 = construct_instance();
// std::shared_ptr<Instance> instance_2 = construct_instance();

// auto folding_prover = composer.create_folding_prover({ instance_1, instance_2 });

// // prepare the prover state
// folding_prover.state.accumulator = instance_1;
// folding_prover.state.deltas.resize(log2_num_gates);
// std::fill_n(folding_prover.state.deltas.begin(), log2_num_gates, 0);
// folding_prover.state.perturbator = Flavor::Polynomial::random(1 << log2_num_gates);
// folding_prover.transcript = Flavor::Transcript::prover_init_empty();
// folding_prover.preparation_round();

// for (auto _ : state) {
// F(folding_prover);
using Flavor = typename Composer::Flavor;
using Builder = typename Flavor::CircuitBuilder;

bb::srs::init_crs_factory("../srs_db/ignition");
auto log2_num_gates = static_cast<size_t>(state.range(0));
auto composer = Composer();

const auto construct_instance = [&]() {
Builder builder;
if constexpr (std::same_as<Flavor, GoblinUltraFlavor>) {
GoblinMockCircuits::construct_arithmetic_circuit(builder, log2_num_gates);
} else {
static_assert(std::same_as<Flavor, UltraFlavor>);
bb::mock_proofs::generate_basic_arithmetic_circuit(builder, log2_num_gates);
}
return composer.create_prover_instance(builder);
};

auto prover_instance_1 = construct_instance();
auto prover_instance_2 = construct_instance();

auto folding_prover = composer.create_folding_prover({ prover_instance_1, prover_instance_2 });

// prepare the prover state
folding_prover.state.accumulator = prover_instance_1;
folding_prover.state.deltas.resize(log2_num_gates);
std::fill_n(folding_prover.state.deltas.begin(), log2_num_gates, 0);
folding_prover.state.perturbator = Flavor::Polynomial::random(1 << log2_num_gates);
folding_prover.transcript = Flavor::Transcript::prover_init_empty();
folding_prover.preparation_round();

for (auto _ : state) {
F(folding_prover);
}
}

void bench_round_ultra(::benchmark::State& state, void (*F)(ProtoGalaxyProver_<ProverInstances_<UltraFlavor, 2>>&))
Expand All @@ -60,25 +59,23 @@ void bench_round_goblin_ultra(::benchmark::State& state,
_bench_round<GoblinUltraComposer>(state, F);
}

// BENCHMARK_CAPTURE(bench_round_ultra, preparation, [](auto& prover) { prover.preparation_round(); })
// -> DenseRange(14, 20) -> Unit(kMillisecond);
// BENCHMARK_CAPTURE(bench_round_ultra, perturbator, [](auto& prover) { prover.perturbator_round(); })
// -> DenseRange(14, 20) -> Unit(kMillisecond);
// BENCHMARK_CAPTURE(bench_round_ultra, combiner_quotient, [](auto& prover) { prover.combiner_quotient_round(); })
// -> DenseRange(14, 20) -> Unit(kMillisecond);
// BENCHMARK_CAPTURE(bench_round_ultra, accumulator_update, [](auto& prover) { prover.accumulator_update_round(); })
// -> DenseRange(14, 20) -> Unit(kMillisecond);

// BENCHMARK_CAPTURE(bench_round_goblin_ultra, preparation, [](auto& prover) { prover.preparation_round(); })
// -> DenseRange(14, 20) -> Unit(kMillisecond);
// BENCHMARK_CAPTURE(bench_round_goblin_ultra, perturbator, [](auto& prover) { prover.perturbator_round(); })
// -> DenseRange(14, 20) -> Unit(kMillisecond);
// BENCHMARK_CAPTURE(bench_round_goblin_ultra, combiner_quotient, [](auto& prover) { prover.combiner_quotient_round();
// })
// -> DenseRange(14, 20) -> Unit(kMillisecond);
// BENCHMARK_CAPTURE(bench_round_goblin_ultra, accumulator_update, [](auto& prover) { prover.accumulator_update_round();
// })
// -> DenseRange(14, 20) -> Unit(kMillisecond);
BENCHMARK_CAPTURE(bench_round_ultra, preparation, [](auto& prover) { prover.preparation_round(); })
-> DenseRange(14, 20) -> Unit(kMillisecond);
BENCHMARK_CAPTURE(bench_round_ultra, perturbator, [](auto& prover) { prover.perturbator_round(); })
-> DenseRange(14, 20) -> Unit(kMillisecond);
BENCHMARK_CAPTURE(bench_round_ultra, combiner_quotient, [](auto& prover) { prover.combiner_quotient_round(); })
-> DenseRange(14, 20) -> Unit(kMillisecond);
BENCHMARK_CAPTURE(bench_round_ultra, accumulator_update, [](auto& prover) { prover.accumulator_update_round(); })
-> DenseRange(14, 20) -> Unit(kMillisecond);

BENCHMARK_CAPTURE(bench_round_goblin_ultra, preparation, [](auto& prover) { prover.preparation_round(); })
-> DenseRange(14, 20) -> Unit(kMillisecond);
BENCHMARK_CAPTURE(bench_round_goblin_ultra, perturbator, [](auto& prover) { prover.perturbator_round(); })
-> DenseRange(14, 20) -> Unit(kMillisecond);
BENCHMARK_CAPTURE(bench_round_goblin_ultra, combiner_quotient, [](auto& prover) { prover.combiner_quotient_round(); })
-> DenseRange(14, 20) -> Unit(kMillisecond);
BENCHMARK_CAPTURE(bench_round_goblin_ultra, accumulator_update, [](auto& prover) { prover.accumulator_update_round(); })
-> DenseRange(14, 20) -> Unit(kMillisecond);

} // namespace bb

Expand Down
48 changes: 26 additions & 22 deletions barretenberg/cpp/src/barretenberg/client_ivc/client_ivc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ bool ClientIVC::verify(Proof& proof, const std::vector<ClientIVC::VerifierAccumu
Composer composer;
auto folding_verifier = composer.create_folding_verifier({ verifier_instances[0], verifier_instances[1] });
auto verifier_accumulator = folding_verifier.verify_folding_proof(proof.fold_proof);
// NOTE: Use of member accumulator here will go away with removal of vkey from ProverInstance

auto decider_verifier = composer.create_decider_verifier(verifier_accumulator);
bool decision = decider_verifier.verify_proof(proof.decider_proof);
return goblin_verified && decision;
Expand All @@ -82,52 +82,56 @@ HonkProof ClientIVC::decider_prove() const
}

/**
* @brief Precompute the array of verification keys by simulating folding. There will be 4 different verification keys.
* @brief Precompute the array of verification keys by simulating folding. There will be 4 different verification keys:
* initial function verification key (without recursive merge verifier), subsequent function verification key (with
* recursive merge verifier), initial kernel verification key (with recursive merge verifier appended, no previous
* kernel to fold), "full" kernel verification key( two recursive folding verifiers and merge verifier).
*
*/
void ClientIVC::precompute_folding_verification_keys()
{
Composer composer;
ClientCircuit initial_function_circuit{ goblin.op_queue };
GoblinMockCircuits::construct_mock_function_circuit(initial_function_circuit);

// Accumulate three circuits to generate two folding proofs for input to folding kernel
ClientCircuit circuit_1{ goblin.op_queue };
GoblinMockCircuits::construct_mock_function_circuit(circuit_1);

initialize(circuit_1);

// Initialise both the first prover and verifier accumulator from the inital function circuit
initialize(initial_function_circuit);
composer.compute_commitment_key(prover_fold_output.accumulator->instance_size);

vks[0] = composer.compute_verification_key(prover_fold_output.accumulator);
auto verifier_acc = std::make_shared<VerifierInstance>();
verifier_acc->verification_key = vks[0];

ClientCircuit circuit_2{ goblin.op_queue };
GoblinMockCircuits::construct_mock_function_circuit(circuit_2);
auto fold_proof_1 = accumulate(circuit_2);
// Accumulate the next function circuit
ClientCircuit function_circuit{ goblin.op_queue };
GoblinMockCircuits::construct_mock_function_circuit(function_circuit);
auto function_fold_proof = accumulate(function_circuit);

// Create its verification key (we have called accumulate so it includes the recursive merge verifier)
vks[1] = composer.compute_verification_key(prover_instance);

FoldOutput kernel_accum;
// Construct kernel circuit
// Create the initial kernel iteration and precompute its verification key
ClientCircuit kernel_circuit{ goblin.op_queue };
auto new_acc =
GoblinMockCircuits::construct_mock_folding_kernel(kernel_circuit, { fold_proof_1, vks[1] }, {}, verifier_acc);
auto fold_proof_3 = accumulate(kernel_circuit);
vks[2] = composer.compute_verification_key(prover_instance); // first iteration of a kernel is smaller
auto new_acc = GoblinMockCircuits::construct_mock_folding_kernel(
kernel_circuit, { function_fold_proof, vks[1] }, {}, verifier_acc);
auto kernel_fold_proof = accumulate(kernel_circuit);
vks[2] = composer.compute_verification_key(prover_instance);

// Create another mock function circuit to run the full kernel
ClientCircuit circuit_4{ goblin.op_queue };
GoblinMockCircuits::construct_mock_function_circuit(circuit_4);
auto fold_proof_4 = accumulate(circuit_4);
function_fold_proof = accumulate(circuit_4);

// Create the fullk ernel circuit and compute verification key
ClientCircuit new_kernel_circuit = GoblinUltraCircuitBuilder{ goblin.op_queue };
auto new_new_acc = GoblinMockCircuits::construct_mock_folding_kernel(
new_kernel_circuit, { fold_proof_4, vks[1] }, { fold_proof_3, vks[2] }, new_acc);

auto fold_proof_5 = accumulate(new_kernel_circuit);
new_kernel_circuit, { function_fold_proof, vks[1] }, { kernel_fold_proof, vks[2] }, new_acc);
kernel_fold_proof = accumulate(new_kernel_circuit);

vks[3] = composer.compute_verification_key(prover_instance);
auto kernel_inst = std::make_shared<VerifierInstance>();
kernel_inst->verification_key = vks[3];

// Clean the ivc state
goblin.op_queue = std::make_shared<Goblin::OpQueue>();
goblin.merge_proof_exists = false;
GoblinMockCircuits::perform_op_queue_interactions_for_mock_first_circuit(goblin.op_queue);
Expand Down
11 changes: 2 additions & 9 deletions barretenberg/cpp/src/barretenberg/client_ivc/client_ivc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ class ClientIVC {
Goblin::Proof goblin_proof;
};

struct FoldOutput {
FoldProof fold_proof;
std::shared_ptr<VerificationKey> inst_vk;
};

private:
using ProverFoldOutput = FoldingResult<GoblinUltraFlavor>;
using Composer = GoblinUltraComposer;
Expand All @@ -46,7 +41,8 @@ class ClientIVC {
ProverFoldOutput prover_fold_output;
ProverAccumulator prover_accumulator;

// keep the instance or instances around if we're folding more of them so we can compute the verification key
// Note: We need to save the last instance that was folded in order to compute its verification key, this will not
// be needed in the real IVC as they are provided as inputs
std::shared_ptr<ProverInstance> prover_instance;

std::array<std::shared_ptr<VerificationKey>, 4> vks;
Expand All @@ -65,9 +61,6 @@ class ClientIVC {

void decider_prove_and_verify(const VerifierAccumulator&) const;

VerifierAccumulator get_verifier_accumulator();
std::shared_ptr<VerifierInstance> get_verifier_instance() const;

void precompute_folding_verification_keys();
};
} // namespace bb
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ TEST_F(ClientIVCTests, Full)

// Constuct four proofs: merge, eccvm, translator, decider
auto proof = ivc.prove();
auto inst = std::make_shared<VerifierInstance>();
inst->verification_key = kernel_vk;
auto inst = std::make_shared<VerifierInstance>(kernel_vk);
// Verify all four proofs
EXPECT_TRUE(ivc.verify(proof, { kernel_acc, inst }));
};
1 change: 1 addition & 0 deletions barretenberg/cpp/src/barretenberg/goblin/mock_circuits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ class GoblinMockCircuits {
stdlib::generate_ecdsa_verification_test_circuit(builder, NUM_ECDSA_VERIFICATIONS);
stdlib::generate_sha256_test_circuit(builder, NUM_SHA_HASHES);

// Init
if (kernel.fold_proof.empty()) {
FoldingRecursiveVerifier verifier_1{ &builder, prev_kernel_accum, { func.inst_vk } };
auto fctn_verifier_accum = verifier_1.verify_folding_proof(func.fold_proof);
Expand Down
48 changes: 0 additions & 48 deletions barretenberg/cpp/src/barretenberg/protogalaxy/decider_verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,58 +25,11 @@ DeciderVerifier_<Flavor>::DeciderVerifier_()
*/
template <typename Flavor> bool DeciderVerifier_<Flavor>::verify_proof(const HonkProof& proof)
{
// using FF = typename Flavor::FF;
// using Commitment = typename Flavor::Commitment;
using Curve = typename Flavor::Curve;
using ZeroMorph = ZeroMorphVerifier_<Curve>;
// using Instance = VerifierInstance_<Flavor>;
using VerifierCommitments = typename Flavor::VerifierCommitments;

// static constexpr size_t NUM_SUBRELATIONS = Flavor::NUM_SUBRELATIONS;
transcript = std::make_shared<Transcript>(proof);
// auto inst = std::make_unique<Instance>();

// inst->instance_size = transcript->template receive_from_prover<uint32_t>("instance_size");
// inst->log_instance_size = static_cast<size_t>(numeric::get_msb(inst->instance_size));
// inst->public_input_size = transcript->template receive_from_prover<uint32_t>("public_input_size");

// for (size_t i = 0; i < inst->public_input_size; ++i) {
// auto public_input_i = transcript->template receive_from_prover<FF>("public_input_" + std::to_string(i));
// inst->public_inputs.emplace_back(public_input_i);
// }

// auto eta = transcript->template receive_from_prover<FF>("eta");
// auto beta = transcript->template receive_from_prover<FF>("beta");
// auto gamma = transcript->template receive_from_prover<FF>("gamma");
// auto public_input_delta = transcript->template receive_from_prover<FF>("public_input_delta");
// auto lookup_grand_product_delta = transcript->template receive_from_prover<FF>("lookup_grand_product_delta");
// inst->relation_parameters =
// RelationParameters<FF>{ eta, beta, gamma, public_input_delta, lookup_grand_product_delta };

// for (size_t idx = 0; idx < NUM_SUBRELATIONS - 1; idx++) {

// inst->alphas[idx] = transcript->template receive_from_prover<FF>("alpha" + std::to_string(idx));
// }

// inst->target_sum = transcript->template receive_from_prover<FF>("target_sum");

// inst->gate_challenges = std::vector<FF>(inst->log_instance_size);
// for (size_t idx = 0; idx < inst->log_instance_size; idx++) {
// inst->gate_challenges[idx] =
// transcript->template receive_from_prover<FF>("gate_challenge_" + std::to_string(idx));
// }
// auto comm_view = inst->witness_commitments.get_all();
// auto witness_labels = inst->commitment_labels.get_witness();
// for (size_t idx = 0; idx < witness_labels.size(); idx++) {
// comm_view[idx] = transcript->template receive_from_prover<Commitment>(witness_labels[idx]);
// }

// inst->verification_key = std::make_shared<VerificationKey>(inst->instance_size, inst->public_input_size);
// auto vk_view = inst->verification_key->get_all();
// auto vk_labels = inst->commitment_labels.get_precomputed();
// for (size_t idx = 0; idx < vk_labels.size(); idx++) {
// vk_view[idx] = transcript->template receive_from_prover<Commitment>(vk_labels[idx]);
// }

VerifierCommitments commitments{ accumulator->verification_key, accumulator->witness_commitments };

Expand All @@ -87,7 +40,6 @@ template <typename Flavor> bool DeciderVerifier_<Flavor>::verify_proof(const Hon

// If Sumcheck did not verify, return false
if (sumcheck_verified.has_value() && !sumcheck_verified.value()) {
info("here");
return false;
}

Expand Down
Loading

0 comments on commit ffaddfc

Please sign in to comment.