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

feat: added prove_output_all flow for honk #6869

Merged
merged 7 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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,
Copy link
Contributor

Choose a reason for hiding this comment

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

Rather than "all" will you use "vk" or something like this? Unclear at a glance what "all" means, and there's no real benefit to using a short function name here IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

all means the vk in binary, vk as fields, proof in binary, proof as fields, so it's not exactly easily written in a function name

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
Loading