diff --git a/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_prover.cpp b/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_prover.cpp index d2ca5f4af45..e4a19bdd6cf 100644 --- a/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_prover.cpp +++ b/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_prover.cpp @@ -1,103 +1,132 @@ #include "protogalaxy_prover.hpp" #include "barretenberg/flavor/flavor.hpp" namespace proof_system::honk { +template +void ProtoGalaxyProver_::finalise_and_send_instance(std::shared_ptr instance, + std::string domain_separator) +{ + instance->initialize_prover_polynomials(); + + const auto instance_size = static_cast(instance->instance_size); + const auto num_public_inputs = static_cast(instance->public_inputs.size()); + transcript.send_to_verifier(domain_separator + "_instance_size", instance_size); + transcript.send_to_verifier(domain_separator + "_public_input_size", num_public_inputs); + + for (size_t i = 0; i < instance->public_inputs.size(); ++i) { + auto public_input_i = instance->public_inputs[i]; + transcript.send_to_verifier(domain_separator + "_public_input_" + std::to_string(i), public_input_i); + } + transcript.send_to_verifier(domain_separator + "_pub_inputs_offset", + static_cast(instance->pub_inputs_offset)); + + auto& witness_commitments = instance->witness_commitments; + + // Commit to the first three wire polynomials of the instance + // We only commit to the fourth wire polynomial after adding memory recordss + witness_commitments.w_l = commitment_key->commit(instance->proving_key->w_l); + witness_commitments.w_r = commitment_key->commit(instance->proving_key->w_r); + witness_commitments.w_o = commitment_key->commit(instance->proving_key->w_o); + + auto wire_comms = witness_commitments.get_wires(); + auto commitment_labels = instance->commitment_labels; + auto wire_labels = commitment_labels.get_wires(); + for (size_t idx = 0; idx < 3; ++idx) { + transcript.send_to_verifier(domain_separator + "_" + wire_labels[idx], wire_comms[idx]); + } + + auto eta = transcript.get_challenge(domain_separator + "_eta"); + instance->compute_sorted_accumulator_polynomials(eta); + + // Commit to the sorted withness-table accumulator and the finalized (i.e. with memory records) fourth wire + // polynomial + witness_commitments.sorted_accum = commitment_key->commit(instance->prover_polynomials.sorted_accum); + witness_commitments.w_4 = commitment_key->commit(instance->prover_polynomials.w_4); + + transcript.send_to_verifier(domain_separator + "_" + commitment_labels.sorted_accum, + witness_commitments.sorted_accum); + transcript.send_to_verifier(domain_separator + "_" + commitment_labels.w_4, witness_commitments.w_4); + + auto [beta, gamma] = transcript.get_challenges(domain_separator + "_beta", domain_separator + "_gamma"); + instance->compute_grand_product_polynomials(beta, gamma); + + witness_commitments.z_perm = commitment_key->commit(instance->prover_polynomials.z_perm); + witness_commitments.z_lookup = commitment_key->commit(instance->prover_polynomials.z_lookup); + + transcript.send_to_verifier(domain_separator + "_" + commitment_labels.z_perm, + instance->witness_commitments.z_perm); + transcript.send_to_verifier(domain_separator + "_" + commitment_labels.z_lookup, + instance->witness_commitments.z_lookup); + + instance->alpha = transcript.get_challenge(domain_separator + "_alpha"); + + auto vk_view = instance->verification_key->pointer_view(); + auto labels = instance->commitment_labels.get_precomputed(); + for (size_t idx = 0; idx < labels.size(); idx++) { + transcript.send_to_verifier(domain_separator + "_" + labels[idx], (*vk_view[idx])); + } +} + +template +void ProtoGalaxyProver_::send_accumulator(std::shared_ptr instance, + std::string domain_separator) +{ + const auto instance_size = static_cast(instance->instance_size); + const auto num_public_inputs = static_cast(instance->public_inputs.size()); + transcript.send_to_verifier(domain_separator + "_instance_size", instance_size); + transcript.send_to_verifier(domain_separator + "_public_input_size", num_public_inputs); + + for (size_t i = 0; i < instance->public_inputs.size(); ++i) { + auto public_input_i = instance->public_inputs[i]; + transcript.send_to_verifier(domain_separator + "_public_input_" + std::to_string(i), public_input_i); + } + + transcript.send_to_verifier(domain_separator + "_eta", instance->relation_parameters.eta); + transcript.send_to_verifier(domain_separator + "_beta", instance->relation_parameters.beta); + transcript.send_to_verifier(domain_separator + "_gamma", instance->relation_parameters.gamma); + transcript.send_to_verifier(domain_separator + "_public_input_delta", + instance->relation_parameters.public_input_delta); + transcript.send_to_verifier(domain_separator + "_lookup_grand_product_delta", + instance->relation_parameters.lookup_grand_product_delta); + + transcript.send_to_verifier(domain_separator + "_alpha", instance->alpha); + + auto folding_parameters = instance->folding_parameters; + transcript.send_to_verifier(domain_separator + "_target_sum", folding_parameters.target_sum); + for (size_t idx = 0; idx < folding_parameters.gate_challenges.size(); idx++) { + transcript.send_to_verifier(domain_separator + "_gate_challenge_" + std::to_string(idx), + folding_parameters.gate_challenges[idx]); + } + + auto comm_view = instance->witness_commitments.pointer_view(); + auto witness_labels = instance->commitment_labels.get_witness(); + for (size_t idx = 0; idx < witness_labels.size(); idx++) { + transcript.send_to_verifier(domain_separator + "_" + witness_labels[idx], (*comm_view[idx])); + } + + auto vk_view = instance->verification_key->pointer_view(); + auto vk_labels = instance->commitment_labels.get_precomputed(); + for (size_t idx = 0; idx < vk_labels.size(); idx++) { + transcript.send_to_verifier(domain_separator + "_" + vk_labels[idx], (*vk_view[idx])); + } +} template void ProtoGalaxyProver_::prepare_for_folding() { auto idx = 0; - for (auto it = instances.begin(); it != instances.end(); it++, idx++) { + auto instance = instances[0]; + auto domain_separator = std::to_string(idx); + transcript.send_to_verifier(domain_separator + "is_accumulator", instance->is_accumulator); + if (instance->is_accumulator) { + send_accumulator(instance, domain_separator); + } else { + finalise_and_send_instance(instance, domain_separator); + } + idx++; + + for (auto it = instances.begin() + 1; it != instances.end(); it++, idx++) { auto instance = *it; auto domain_separator = std::to_string(idx); - transcript.send_to_verifier(domain_separator + "is_accumulator", instance->is_accumulator); - - if (!static_cast(instance->is_accumulator)) { - instance->initialize_prover_polynomials(); - } - - const auto instance_size = static_cast(instance->instance_size); - const auto num_public_inputs = static_cast(instance->public_inputs.size()); - transcript.send_to_verifier(domain_separator + "_instance_size", instance_size); - transcript.send_to_verifier(domain_separator + "_public_input_size", num_public_inputs); - - for (size_t i = 0; i < instance->public_inputs.size(); ++i) { - auto public_input_i = instance->public_inputs[i]; - transcript.send_to_verifier(domain_separator + "_public_input_" + std::to_string(i), public_input_i); - } - - if (!static_cast(instance->is_accumulator)) { - transcript.send_to_verifier(domain_separator + "_pub_inputs_offset", - static_cast(instance->pub_inputs_offset)); - - auto& witness_commitments = instance->witness_commitments; - - // Commit to the first three wire polynomials of the instance - // We only commit to the fourth wire polynomial after adding memory recordss - witness_commitments.w_l = commitment_key->commit(instance->proving_key->w_l); - witness_commitments.w_r = commitment_key->commit(instance->proving_key->w_r); - witness_commitments.w_o = commitment_key->commit(instance->proving_key->w_o); - - auto wire_comms = witness_commitments.get_wires(); - auto commitment_labels = instance->commitment_labels; - auto wire_labels = commitment_labels.get_wires(); - for (size_t idx = 0; idx < 3; ++idx) { - transcript.send_to_verifier(domain_separator + "_" + wire_labels[idx], wire_comms[idx]); - } - - auto eta = transcript.get_challenge(domain_separator + "_eta"); - instance->compute_sorted_accumulator_polynomials(eta); - - // Commit to the sorted withness-table accumulator and the finalized (i.e. with memory records) fourth wire - // polynomial - witness_commitments.sorted_accum = commitment_key->commit(instance->prover_polynomials.sorted_accum); - witness_commitments.w_4 = commitment_key->commit(instance->prover_polynomials.w_4); - - transcript.send_to_verifier(domain_separator + "_" + commitment_labels.sorted_accum, - witness_commitments.sorted_accum); - transcript.send_to_verifier(domain_separator + "_" + commitment_labels.w_4, witness_commitments.w_4); - - auto [beta, gamma] = transcript.get_challenges(domain_separator + "_beta", domain_separator + "_gamma"); - instance->compute_grand_product_polynomials(beta, gamma); - - witness_commitments.z_perm = commitment_key->commit(instance->prover_polynomials.z_perm); - witness_commitments.z_lookup = commitment_key->commit(instance->prover_polynomials.z_lookup); - - transcript.send_to_verifier(domain_separator + "_" + commitment_labels.z_perm, - instance->witness_commitments.z_perm); - transcript.send_to_verifier(domain_separator + "_" + commitment_labels.z_lookup, - instance->witness_commitments.z_lookup); - - instance->alpha = transcript.get_challenge(domain_separator + "_alpha"); - - } else { - transcript.send_to_verifier(domain_separator + "_eta", instance->relation_parameters.eta); - transcript.send_to_verifier(domain_separator + "_beta", instance->relation_parameters.beta); - transcript.send_to_verifier(domain_separator + "_gamma", instance->relation_parameters.gamma); - transcript.send_to_verifier(domain_separator + "_public_input_delta", - instance->relation_parameters.public_input_delta); - transcript.send_to_verifier(domain_separator + "_lookup_grand_product_delta", - instance->relation_parameters.lookup_grand_product_delta); - - transcript.send_to_verifier(domain_separator + "_alpha", instance->alpha); - - auto folding_parameters = instance->folding_parameters; - transcript.send_to_verifier(domain_separator + "_target_sum", folding_parameters.target_sum); - for (size_t idx = 0; idx < folding_parameters.gate_challenges.size(); idx++) { - transcript.send_to_verifier(domain_separator + "_gate_challenge_" + std::to_string(idx), - folding_parameters.gate_challenges[idx]); - } - - auto comm_view = instance->witness_commitments.pointer_view(); - auto labels = instance->commitment_labels.get_witness(); - for (size_t idx = 0; idx < labels.size(); idx++) { - transcript.send_to_verifier(domain_separator + "_" + labels[idx], (*comm_view[idx])); - } - } - - auto vk_view = instance->verification_key->pointer_view(); - auto labels = instance->commitment_labels.get_precomputed(); - for (size_t idx = 0; idx < labels.size(); idx++) { - transcript.send_to_verifier(domain_separator + "_" + labels[idx], (*vk_view[idx])); - } + finalise_and_send_instance(instance, domain_separator); } } diff --git a/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_prover.hpp b/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_prover.hpp index bdd29b03f83..9d53b087879 100644 --- a/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_prover.hpp +++ b/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_prover.hpp @@ -63,6 +63,10 @@ template class ProtoGalaxyProver_ { */ void prepare_for_folding(); + void send_accumulator(std::shared_ptr, std::string); + + void finalise_and_send_instance(std::shared_ptr, std::string); + /** * @brief Given a vector \vec{\beta} of values, compute the pow polynomial on these values as defined in the paper. */ diff --git a/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_verifier.cpp b/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_verifier.cpp index 16928326905..f5d218e3c79 100644 --- a/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_verifier.cpp +++ b/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_verifier.cpp @@ -2,94 +2,124 @@ #include "barretenberg/proof_system/library/grand_product_delta.hpp" namespace proof_system::honk { +template +void ProtoGalaxyVerifier_::receive_accumulator(std::shared_ptr inst, + std::string domain_separator) +{ + inst->instance_size = transcript.template receive_from_prover(domain_separator + "_instance_size"); + inst->log_instance_size = static_cast(numeric::get_msb(inst->instance_size)); + inst->public_input_size = + transcript.template receive_from_prover(domain_separator + "_public_input_size"); + + for (size_t i = 0; i < inst->public_input_size; ++i) { + auto public_input_i = + transcript.template receive_from_prover(domain_separator + "_public_input_" + std::to_string(i)); + inst->public_inputs.emplace_back(public_input_i); + } + + auto eta = transcript.template receive_from_prover(domain_separator + "_eta"); + auto beta = transcript.template receive_from_prover(domain_separator + "_beta"); + auto gamma = transcript.template receive_from_prover(domain_separator + "_gamma"); + auto public_input_delta = transcript.template receive_from_prover(domain_separator + "_public_input_delta"); + auto lookup_grand_product_delta = + transcript.template receive_from_prover(domain_separator + "_lookup_grand_product_delta"); + inst->relation_parameters = + RelationParameters{ eta, beta, gamma, public_input_delta, lookup_grand_product_delta }; + inst->alpha = transcript.template receive_from_prover(domain_separator + "_alpha"); + + inst->folding_parameters.target_sum = transcript.template receive_from_prover(domain_separator + "_target_sum"); + + inst->folding_parameters.gate_challenges = std::vector(inst->log_instance_size); + for (size_t idx = 0; idx < inst->log_instance_size; idx++) { + inst->folding_parameters.gate_challenges[idx] = + transcript.template receive_from_prover(domain_separator + "_gate_challenge_" + std::to_string(idx)); + } + auto comm_view = inst->witness_commitments.pointer_view(); + 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(domain_separator + "_" + witness_labels[idx]); + } + + inst->verification_key = std::make_shared(inst->instance_size, inst->public_input_size); + auto vk_view = inst->verification_key->pointer_view(); + 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(domain_separator + "_" + vk_labels[idx]); + } +} + +template +void ProtoGalaxyVerifier_::receive_and_finalise_instance(std::shared_ptr inst, + std::string domain_separator) +{ + inst->instance_size = transcript.template receive_from_prover(domain_separator + "_instance_size"); + inst->log_instance_size = static_cast(numeric::get_msb(inst->instance_size)); + inst->public_input_size = + transcript.template receive_from_prover(domain_separator + "_public_input_size"); + + for (size_t i = 0; i < inst->public_input_size; ++i) { + auto public_input_i = + transcript.template receive_from_prover(domain_separator + "_public_input_" + std::to_string(i)); + inst->public_inputs.emplace_back(public_input_i); + } + + inst->pub_inputs_offset = + transcript.template receive_from_prover(domain_separator + "_pub_inputs_offset"); + + auto labels = inst->commitment_labels; + auto& witness_commitments = inst->witness_commitments; + witness_commitments.w_l = transcript.template receive_from_prover(domain_separator + "_" + labels.w_l); + witness_commitments.w_r = transcript.template receive_from_prover(domain_separator + "_" + labels.w_r); + witness_commitments.w_o = transcript.template receive_from_prover(domain_separator + "_" + labels.w_o); + + auto eta = transcript.get_challenge(domain_separator + "_eta"); + witness_commitments.sorted_accum = + transcript.template receive_from_prover(domain_separator + "_" + labels.sorted_accum); + witness_commitments.w_4 = transcript.template receive_from_prover(domain_separator + "_" + labels.w_4); + + auto [beta, gamma] = transcript.get_challenges(domain_separator + "_beta", domain_separator + "_gamma"); + witness_commitments.z_perm = + transcript.template receive_from_prover(domain_separator + "_" + labels.z_perm); + witness_commitments.z_lookup = + transcript.template receive_from_prover(domain_separator + "_" + labels.z_lookup); + + const FF public_input_delta = compute_public_input_delta( + inst->public_inputs, beta, gamma, inst->instance_size, inst->pub_inputs_offset); + const FF lookup_grand_product_delta = compute_lookup_grand_product_delta(beta, gamma, inst->instance_size); + inst->relation_parameters = + RelationParameters{ eta, beta, gamma, public_input_delta, lookup_grand_product_delta }; + + inst->alpha = transcript.get_challenge(domain_separator + "_alpha"); + + inst->verification_key = std::make_shared(inst->instance_size, inst->public_input_size); + auto vk_view = inst->verification_key->pointer_view(); + auto vk_labels = labels.get_precomputed(); + for (size_t idx = 0; idx < vk_labels.size(); idx++) { + (*vk_view[idx]) = transcript.template receive_from_prover(domain_separator + "_" + vk_labels[idx]); + } +} + +// TODO(#795) template void ProtoGalaxyVerifier_::prepare_for_folding(std::vector fold_data) { transcript = BaseTranscript{ fold_data }; auto index = 0; - for (auto it = instances.begin(); it != instances.end(); it++, index++) { + auto inst = instances[0]; + auto domain_separator = std::to_string(index); + inst->is_accumulator = transcript.template receive_from_prover(domain_separator + "is_accumulator"); + if (inst->is_accumulator) { + receive_accumulator(inst, domain_separator); + } else { + receive_and_finalise_instance(inst, domain_separator); + } + index++; + + for (auto it = instances.begin() + 1; it != instances.end(); it++, index++) { auto inst = *it; auto domain_separator = std::to_string(index); - inst->is_accumulator = transcript.template receive_from_prover(domain_separator + "is_accumulator"); - - inst->instance_size = transcript.template receive_from_prover(domain_separator + "_instance_size"); - inst->log_instance_size = static_cast(numeric::get_msb(inst->instance_size)); - inst->public_input_size = - transcript.template receive_from_prover(domain_separator + "_public_input_size"); - - for (size_t i = 0; i < inst->public_input_size; ++i) { - auto public_input_i = - transcript.template receive_from_prover(domain_separator + "_public_input_" + std::to_string(i)); - inst->public_inputs.emplace_back(public_input_i); - } - - if (static_cast(inst->is_accumulator)) { - auto eta = transcript.template receive_from_prover(domain_separator + "_eta"); - auto beta = transcript.template receive_from_prover(domain_separator + "_beta"); - auto gamma = transcript.template receive_from_prover(domain_separator + "_gamma"); - auto public_input_delta = - transcript.template receive_from_prover(domain_separator + "_public_input_delta"); - auto lookup_grand_product_delta = - transcript.template receive_from_prover(domain_separator + "_lookup_grand_product_delta"); - inst->relation_parameters = - RelationParameters{ eta, beta, gamma, public_input_delta, lookup_grand_product_delta }; - inst->alpha = transcript.template receive_from_prover(domain_separator + "_alpha"); - - inst->folding_parameters.target_sum = - transcript.template receive_from_prover(domain_separator + "_target_sum"); - - inst->folding_parameters.gate_challenges = std::vector(inst->log_instance_size); - for (size_t idx = 0; idx < inst->log_instance_size; idx++) { - inst->folding_parameters.gate_challenges[idx] = transcript.template receive_from_prover( - domain_separator + "_gate_challenge_" + std::to_string(idx)); - } - auto comm_view = inst->witness_commitments.pointer_view(); - auto labels = inst->commitment_labels.get_witness(); - for (size_t idx = 0; idx < labels.size(); idx++) { - (*comm_view[idx]) = - transcript.template receive_from_prover(domain_separator + "_" + labels[idx]); - } - } else { - inst->pub_inputs_offset = - transcript.template receive_from_prover(domain_separator + "_pub_inputs_offset"); - - auto labels = inst->commitment_labels; - auto& witness_commitments = inst->witness_commitments; - witness_commitments.w_l = - transcript.template receive_from_prover(domain_separator + "_" + labels.w_l); - witness_commitments.w_r = - transcript.template receive_from_prover(domain_separator + "_" + labels.w_r); - witness_commitments.w_o = - transcript.template receive_from_prover(domain_separator + "_" + labels.w_o); - - auto eta = transcript.get_challenge(domain_separator + "_eta"); - witness_commitments.sorted_accum = - transcript.template receive_from_prover(domain_separator + "_" + labels.sorted_accum); - witness_commitments.w_4 = - transcript.template receive_from_prover(domain_separator + "_" + labels.w_4); - - auto [beta, gamma] = transcript.get_challenges(domain_separator + "_beta", domain_separator + "_gamma"); - witness_commitments.z_perm = - transcript.template receive_from_prover(domain_separator + "_" + labels.z_perm); - witness_commitments.z_lookup = - transcript.template receive_from_prover(domain_separator + "_" + labels.z_lookup); - - const FF public_input_delta = compute_public_input_delta( - inst->public_inputs, beta, gamma, inst->instance_size, inst->pub_inputs_offset); - const FF lookup_grand_product_delta = - compute_lookup_grand_product_delta(beta, gamma, inst->instance_size); - inst->relation_parameters = - RelationParameters{ eta, beta, gamma, public_input_delta, lookup_grand_product_delta }; - - inst->alpha = transcript.get_challenge(domain_separator + "_alpha"); - } - - inst->verification_key = std::make_shared(inst->instance_size, inst->public_input_size); - auto vk_view = inst->verification_key->pointer_view(); - auto labels = typename Flavor::CommitmentLabels().get_precomputed(); - for (size_t idx = 0; idx < labels.size(); idx++) { - (*vk_view[idx]) = transcript.template receive_from_prover(domain_separator + "_" + labels[idx]); - } + receive_and_finalise_instance(inst, domain_separator); } } diff --git a/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_verifier.hpp b/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_verifier.hpp index 6cc149a87da..86b1c698b4a 100644 --- a/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_verifier.hpp +++ b/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_verifier.hpp @@ -60,7 +60,11 @@ template class ProtoGalaxyVerifier_ { * * @param fold_data The data transmitted via the transcript by the prover. */ - void prepare_for_folding(std::vector fold_data); + void prepare_for_folding(std::vector); + + void receive_accumulator(std::shared_ptr, std::string); + + void receive_and_finalise_instance(std::shared_ptr, std::string); /** * @brief Run the folding protocol on the verifier side to verify the public data ϕ of the new accumulator, received @@ -68,7 +72,7 @@ template class ProtoGalaxyVerifier_ { * * TODO(https://github.com/AztecProtocol/barretenberg/issues/690): finalise the implementation of this function */ - bool verify_folding_proof(std::vector fold_data); + bool verify_folding_proof(std::vector); }; extern template class ProtoGalaxyVerifier_>;