Skip to content

Commit

Permalink
feat: added prove_output_all flow for honk (#6869)
Browse files Browse the repository at this point in the history
Adds new flow, prove_honk_output_all that is needed for rollup circuits.
  • Loading branch information
lucasxia01 authored Jun 20, 2024
1 parent edd6d3f commit 7bd7c66
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
76 changes: 75 additions & 1 deletion barretenberg/cpp/src/barretenberg/bb/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ template <IsUltraFlavor Flavor> bool verify_honk(const std::string& proof_path,
}

/**
* @brief Writes a verification key for an ACIR circuit to a file
* @brief Writes a Honk verification key for an ACIR circuit to a file
*
* Communication:
* - stdout: The verification key is written to stdout as a byte array
Expand Down Expand Up @@ -965,6 +965,74 @@ void prove_output_all(const std::string& bytecodePath, const std::string& witnes
vinfo("vk as fields written to: ", vkFieldsOutputPath);
}

/**
* @brief Creates a Honk proof for an ACIR circuit, outputs the proof and verification key in binary and 'field' format
*
* Communication:
* - Filesystem: The proof is written to the path specified by outputPath
*
* @param bytecodePath Path to the file containing the serialized circuit
* @param witnessPath Path to the file containing the serialized witness
* @param outputPath Directory into which we write the proof and verification key data
*/
template <IsUltraFlavor Flavor>
void prove_honk_output_all(const std::string& bytecodePath,
const std::string& witnessPath,
const std::string& outputPath)
{
using Builder = Flavor::CircuitBuilder;
using Prover = UltraProver_<Flavor>;
using VerificationKey = Flavor::VerificationKey;

bool honk_recursion = false;
if constexpr (IsAnyOf<Flavor, UltraFlavor>) {
honk_recursion = true;
}

auto constraint_system = get_constraint_system(bytecodePath, honk_recursion);
auto witness = get_witness(witnessPath);

auto builder = acir_format::create_circuit<Builder>(constraint_system, 0, witness, honk_recursion);

auto num_extra_gates = builder.get_num_gates_added_to_ensure_nonzero_polynomials();
size_t srs_size = builder.get_circuit_subgroup_size(builder.get_total_circuit_size() + num_extra_gates);
init_bn254_crs(srs_size);

// Construct Honk proof
Prover prover{ builder };
auto proof = prover.construct_proof();

// We have been given a directory, we will write the proof and verification key
// into the directory in both 'binary' and 'fields' formats
std::string vkOutputPath = outputPath + "/vk";
std::string proofPath = outputPath + "/proof";
std::string vkFieldsOutputPath = outputPath + "/vk_fields.json";
std::string proofFieldsPath = outputPath + "/proof_fields.json";

VerificationKey vk(
prover.instance->proving_key); // uses a partial form of the proving key which only has precomputed entities

// Write the 'binary' proof
write_file(proofPath, to_buffer</*include_size=*/true>(proof));
vinfo("binary proof written to: ", proofPath);

// Write the proof as fields
std::string proofJson = to_json(proof);
write_file(proofFieldsPath, { proofJson.begin(), proofJson.end() });
vinfo("proof as fields written to: ", proofFieldsPath);

// Write the vk as binary
auto serialized_vk = to_buffer(vk);
write_file(vkOutputPath, serialized_vk);
vinfo("vk written to: ", vkOutputPath);

// Write the vk as fields
std::vector<bb::fr> vk_data = vk.to_field_elements();
auto vk_json = honk_vk_to_json(vk_data);
write_file(vkFieldsOutputPath, { vk_json.begin(), vk_json.end() });
vinfo("vk as fields written to: ", vkFieldsOutputPath);
}

bool flag_present(std::vector<std::string>& args, const std::string& flag)
{
return std::find(args.begin(), args.end(), flag) != args.end();
Expand Down Expand Up @@ -1027,6 +1095,12 @@ int main(int argc, char* argv[])
} else if (command == "prove_output_all") {
std::string output_path = get_option(args, "-o", "./proofs");
prove_output_all(bytecode_path, witness_path, output_path);
} else if (command == "prove_ultra_honk_output_all") {
std::string output_path = get_option(args, "-o", "./proofs");
prove_honk_output_all<UltraFlavor>(bytecode_path, witness_path, output_path);
} else if (command == "prove_mega_honk_output_all") {
std::string output_path = get_option(args, "-o", "./proofs");
prove_honk_output_all<MegaFlavor>(bytecode_path, witness_path, output_path);
} else if (command == "client_ivc_prove_output_all") {
std::string output_path = get_option(args, "-o", "./proofs");
client_ivc_prove_output_all(bytecode_path, witness_path, output_path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class MockCircuits {
*/
template <typename Builder> static void add_arithmetic_gates(Builder& builder, const size_t num_gates = 4)
{
// For good measure, include a gate with some public inputs
for (size_t i = 0; i < num_gates; ++i) {
FF a = FF::random_element(&engine);
FF b = FF::random_element(&engine);
Expand Down

0 comments on commit 7bd7c66

Please sign in to comment.