Skip to content

Commit 21d0bf2

Browse files
fix: add actionable error when bb verify cannot find vk file (#19398)
## Summary - Adds a more helpful error message when `bb verify` fails because the vk file doesn't exist - Previously users got a confusing "Unable to open file: ./target/vk" error - Now the error provides actionable guidance on how to generate or specify the vk file ## Test plan - Added `VerifyWithMissingVkGivesActionableError` test that verifies the error message contains: - `--write_vk` flag guidance - `bb write_vk` command guidance - `--vk_path` option guidance Fixes #17726
1 parent feb4094 commit 21d0bf2

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

barretenberg/cpp/src/barretenberg/api/api_chonk.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ bool ChonkAPI::verify([[maybe_unused]] const Flags& flags,
123123
auto proof_fields = many_from_buffer<fr>(read_file(proof_path));
124124
auto proof = ChonkProof::from_field_elements(proof_fields);
125125

126-
auto vk_buffer = read_file(vk_path);
126+
auto vk_buffer = read_vk_file(vk_path);
127127

128128
auto response = bbapi::ChonkVerify{ .proof = std::move(proof), .vk = std::move(vk_buffer) }.execute();
129129
return response.valid;

barretenberg/cpp/src/barretenberg/api/api_ultra_honk.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ bool UltraHonkAPI::verify(const Flags& flags,
101101
// Read input files
102102
auto public_inputs = many_from_buffer<uint256_t>(read_file(public_inputs_path));
103103
auto proof = many_from_buffer<uint256_t>(read_file(proof_path));
104-
auto vk_bytes = read_file(vk_path);
104+
auto vk_bytes = read_vk_file(vk_path);
105105

106106
// Convert flags to ProofSystemSettings
107107
bbapi::ProofSystemSettings settings{ .ipa_accumulation = flags.ipa_accumulation,
@@ -209,7 +209,7 @@ void UltraHonkAPI::write_solidity_verifier(const Flags& flags,
209209
{
210210
BB_BENCH_NAME("UltraHonkAPI::write_solidity_verifier");
211211
// Read VK file
212-
auto vk_bytes = read_file(vk_path);
212+
auto vk_bytes = read_vk_file(vk_path);
213213

214214
// Convert flags to ProofSystemSettings
215215
bbapi::ProofSystemSettings settings{ .ipa_accumulation = flags.ipa_accumulation,

barretenberg/cpp/src/barretenberg/api/api_ultra_honk.test.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,35 @@ TEST_F(ApiUltraHonkTest, GatesWithOpcodesSmokeTest)
233233
// Check that output contains per-opcode information
234234
EXPECT_TRUE(output.find("gates_per_opcode") != std::string::npos);
235235
}
236+
237+
TEST_F(ApiUltraHonkTest, VerifyWithMissingVkGivesActionableError)
238+
{
239+
auto [bytecode_path, witness_path] = create_test_circuit_files(test_dir);
240+
241+
API::Flags flags;
242+
flags.oracle_hash_type = "poseidon2";
243+
flags.write_vk = true;
244+
245+
UltraHonkAPI api;
246+
247+
// Generate proof with vk
248+
auto proof_output_dir = test_dir / "proof";
249+
std::filesystem::create_directories(proof_output_dir);
250+
api.prove(flags, bytecode_path, witness_path, "", proof_output_dir);
251+
252+
// Try to verify with a non-existent vk path
253+
auto nonexistent_vk_path = test_dir / "nonexistent_vk";
254+
try {
255+
api.verify(flags, proof_output_dir / "public_inputs", proof_output_dir / "proof", nonexistent_vk_path);
256+
FAIL() << "Expected an exception to be thrown";
257+
} catch (const std::runtime_error& e) {
258+
std::string error_msg = e.what();
259+
// Check that the error message contains actionable guidance
260+
EXPECT_TRUE(error_msg.find("--write_vk") != std::string::npos)
261+
<< "Error message should mention --write_vk flag. Got: " << error_msg;
262+
EXPECT_TRUE(error_msg.find("bb write_vk") != std::string::npos)
263+
<< "Error message should mention bb write_vk command. Got: " << error_msg;
264+
EXPECT_TRUE(error_msg.find("--vk_path") != std::string::npos)
265+
<< "Error message should mention --vk_path option. Got: " << error_msg;
266+
}
267+
}

barretenberg/cpp/src/barretenberg/api/file_io.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <cstdint>
66
#include <cstring>
77
#include <fcntl.h>
8+
#include <filesystem>
89
#include <fstream>
910
#include <ios>
1011
#include <iostream>
@@ -100,4 +101,23 @@ template <typename Fr> inline std::string field_elements_to_json(const std::vect
100101
ss << "]";
101102
return ss.str();
102103
}
104+
105+
/**
106+
* @brief Read a verification key file with an actionable error message if not found.
107+
*
108+
* @param vk_path Path to the verification key file
109+
* @return std::vector<uint8_t> The verification key bytes
110+
* @throws std::runtime_error with actionable message if vk file not found
111+
*/
112+
inline std::vector<uint8_t> read_vk_file(const std::filesystem::path& vk_path)
113+
{
114+
try {
115+
return read_file(vk_path);
116+
} catch (const std::runtime_error&) {
117+
THROW std::runtime_error("Unable to open file: " + vk_path.string() +
118+
"\nGenerate a vk during proving by running `bb prove` with an additional `--write_vk` "
119+
"flag, or run `bb write_vk` to generate a standalone vk."
120+
"\nIf you already have a vk file, specify its path with `--vk_path <path>`.");
121+
}
122+
}
103123
} // namespace bb

0 commit comments

Comments
 (0)