Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion barretenberg/cpp/src/barretenberg/api/api_chonk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ bool ChonkAPI::verify([[maybe_unused]] const Flags& flags,
auto proof_fields = many_from_buffer<fr>(read_file(proof_path));
auto proof = ChonkProof::from_field_elements(proof_fields);

auto vk_buffer = read_file(vk_path);
auto vk_buffer = read_vk_file(vk_path);

auto response = bbapi::ChonkVerify{ .proof = std::move(proof), .vk = std::move(vk_buffer) }.execute();
return response.valid;
Expand Down
4 changes: 2 additions & 2 deletions barretenberg/cpp/src/barretenberg/api/api_ultra_honk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ bool UltraHonkAPI::verify(const Flags& flags,
// Read input files
auto public_inputs = many_from_buffer<uint256_t>(read_file(public_inputs_path));
auto proof = many_from_buffer<uint256_t>(read_file(proof_path));
auto vk_bytes = read_file(vk_path);
auto vk_bytes = read_vk_file(vk_path);

// Convert flags to ProofSystemSettings
bbapi::ProofSystemSettings settings{ .ipa_accumulation = flags.ipa_accumulation,
Expand Down Expand Up @@ -209,7 +209,7 @@ void UltraHonkAPI::write_solidity_verifier(const Flags& flags,
{
BB_BENCH_NAME("UltraHonkAPI::write_solidity_verifier");
// Read VK file
auto vk_bytes = read_file(vk_path);
auto vk_bytes = read_vk_file(vk_path);

// Convert flags to ProofSystemSettings
bbapi::ProofSystemSettings settings{ .ipa_accumulation = flags.ipa_accumulation,
Expand Down
32 changes: 32 additions & 0 deletions barretenberg/cpp/src/barretenberg/api/api_ultra_honk.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,35 @@ TEST_F(ApiUltraHonkTest, GatesWithOpcodesSmokeTest)
// Check that output contains per-opcode information
EXPECT_TRUE(output.find("gates_per_opcode") != std::string::npos);
}

TEST_F(ApiUltraHonkTest, VerifyWithMissingVkGivesActionableError)
{
auto [bytecode_path, witness_path] = create_test_circuit_files(test_dir);

API::Flags flags;
flags.oracle_hash_type = "poseidon2";
flags.write_vk = true;

UltraHonkAPI api;

// Generate proof with vk
auto proof_output_dir = test_dir / "proof";
std::filesystem::create_directories(proof_output_dir);
api.prove(flags, bytecode_path, witness_path, "", proof_output_dir);

// Try to verify with a non-existent vk path
auto nonexistent_vk_path = test_dir / "nonexistent_vk";
try {
api.verify(flags, proof_output_dir / "public_inputs", proof_output_dir / "proof", nonexistent_vk_path);
FAIL() << "Expected an exception to be thrown";
} catch (const std::runtime_error& e) {
std::string error_msg = e.what();
// Check that the error message contains actionable guidance
EXPECT_TRUE(error_msg.find("--write_vk") != std::string::npos)
<< "Error message should mention --write_vk flag. Got: " << error_msg;
EXPECT_TRUE(error_msg.find("bb write_vk") != std::string::npos)
<< "Error message should mention bb write_vk command. Got: " << error_msg;
EXPECT_TRUE(error_msg.find("--vk_path") != std::string::npos)
<< "Error message should mention --vk_path option. Got: " << error_msg;
}
}
20 changes: 20 additions & 0 deletions barretenberg/cpp/src/barretenberg/api/file_io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <cstdint>
#include <cstring>
#include <fcntl.h>
#include <filesystem>
#include <fstream>
#include <ios>
#include <iostream>
Expand Down Expand Up @@ -100,4 +101,23 @@ template <typename Fr> inline std::string field_elements_to_json(const std::vect
ss << "]";
return ss.str();
}

/**
* @brief Read a verification key file with an actionable error message if not found.
*
* @param vk_path Path to the verification key file
* @return std::vector<uint8_t> The verification key bytes
* @throws std::runtime_error with actionable message if vk file not found
*/
inline std::vector<uint8_t> read_vk_file(const std::filesystem::path& vk_path)
{
try {
return read_file(vk_path);
} catch (const std::runtime_error&) {
THROW std::runtime_error("Unable to open file: " + vk_path.string() +
"\nGenerate a vk during proving by running `bb prove` with an additional `--write_vk` "
"flag, or run `bb write_vk` to generate a standalone vk."
"\nIf you already have a vk file, specify its path with `--vk_path <path>`.");
}
}
} // namespace bb
2 changes: 1 addition & 1 deletion barretenberg/cpp/src/barretenberg/vm2/api_avm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ bool avm_verify(const std::filesystem::path& proof_path,
const std::filesystem::path& vk_path)
{
const auto proof = many_from_buffer<fr>(read_file(proof_path));
std::vector<uint8_t> vk_bytes = read_file(vk_path);
std::vector<uint8_t> vk_bytes = read_vk_file(vk_path);
auto public_inputs = avm2::PublicInputs::from(read_file(public_inputs_path));

avm2::AvmAPI avm;
Expand Down