14#include <nlohmann/json.hpp>
18#ifdef ENABLE_AVM_TRANSPILER
20#include <avm_transpiler.h>
30std::vector<uint8_t> extract_bytecode(
const nlohmann::json& function)
32 if (!function.contains(
"bytecode")) {
36 const auto& base64_bytecode = function[
"bytecode"].get<std::string>();
43std::string compute_bytecode_hash(
const std::vector<uint8_t>&
bytecode)
46 std::ostringstream oss;
47 for (
auto byte :
hash) {
48 oss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(
byte);
56std::filesystem::path get_cache_dir()
62 std::filesystem::path cache_dir = std::filesystem::path(home) /
".bb" /
BB_VERSION /
"vk_cache";
63 std::filesystem::create_directories(cache_dir);
70bool is_private_constrained_function(
const nlohmann::json& function)
72 bool is_public =
false;
73 bool is_unconstrained =
false;
76 if (function.contains(
"custom_attributes") && function[
"custom_attributes"].is_array()) {
77 for (
const auto& attr : function[
"custom_attributes"]) {
78 if (attr.is_string() && attr.get<std::string>() ==
"public") {
86 if (function.contains(
"is_unconstrained") && function[
"is_unconstrained"].is_boolean()) {
87 is_unconstrained = function[
"is_unconstrained"].get<
bool>();
90 return !is_public && !is_unconstrained;
96std::vector<uint8_t> get_or_generate_cached_vk(
const std::filesystem::path& cache_dir,
97 const std::string& circuit_name,
98 const std::vector<uint8_t>&
bytecode,
101 std::string hash_str = compute_bytecode_hash(
bytecode);
102 std::filesystem::path vk_cache_path = cache_dir / (hash_str +
".vk");
105 if (!force && std::filesystem::exists(vk_cache_path)) {
106 info(
"Verification key already in cache: ", hash_str);
111 info(
"Generating verification key: ", hash_str);
113 bbapi::ChonkComputeStandaloneVk{ .circuit = { .name = circuit_name, .bytecode =
bytecode } }.execute();
118 return response.bytes;
124void generate_vks_for_functions(
const std::filesystem::path& cache_dir,
133 const size_t num_functions = functions.size();
138 size_t actual_tasks = std::min(num_functions, total_cpus);
139 size_t threads_per_task = std::min(total_cpus,
std::max(
size_t{ 2 }, total_cpus / actual_tasks * 2));
142 std::atomic<size_t> current_function{ 0 };
145 auto worker = [&]() {
151 while ((func_idx = current_function.fetch_add(1)) < num_functions) {
152 auto* function = functions[func_idx];
153 std::string fn_name = (*function)[
"name"].get<std::string>();
156 auto bytecode = extract_bytecode(*function);
159 get_or_generate_cached_vk(cache_dir, fn_name,
bytecode, force);
164 std::vector<std::thread> threads;
165 threads.reserve(actual_tasks);
167 for (
size_t i = 0; i < actual_tasks; ++i) {
168 threads.emplace_back(worker);
172 for (
auto& t : threads) {
177 for (
auto* function : functions) {
178 std::string fn_name = (*function)[
"name"].get<std::string>();
181 auto bytecode = extract_bytecode(*function);
184 std::string hash_str = compute_bytecode_hash(
bytecode);
185 std::filesystem::path vk_cache_path = cache_dir / (hash_str +
".vk");
189 std::string encoded_vk =
base64_encode(vk_data.data(), vk_data.size(),
false);
190 (*function)[
"verification_key"] = encoded_vk;
199bool transpile_artifact([[maybe_unused]]
const std::string& input_path, [[maybe_unused]]
const std::string& output_path)
201#ifdef ENABLE_AVM_TRANSPILER
202 info(
"Transpiling: ", input_path,
" -> ", output_path);
204 auto result = avm_transpile_file(input_path.c_str(), output_path.c_str());
206 if (result.success == 0) {
207 if (result.error_message) {
208 std::string error_msg(result.error_message);
209 if (error_msg ==
"Contract already transpiled") {
211 if (input_path != output_path) {
212 std::filesystem::copy_file(
213 input_path, output_path, std::filesystem::copy_options::overwrite_existing);
216 info(
"Transpilation failed: ", error_msg);
217 avm_free_result(&result);
221 info(
"Transpilation failed");
222 avm_free_result(&result);
227 avm_free_result(&result);
229 info(
"Transpiled: ", input_path,
" -> ", output_path);
231 throw_or_abort(
"AVM Transpiler is not enabled. Please enable it to use bb aztec_process.");
243 if (!std::filesystem::exists(output_path)) {
248 auto cache_dir = get_cache_dir();
249 info(
"Generating verification keys for functions in ", std::filesystem::path(output_path).filename().
string());
250 info(
"Cache directory: ", cache_dir.string());
253 auto artifact_content =
read_file(output_path);
254 std::string artifact_str(artifact_content.begin(), artifact_content.end());
255 auto artifact_json = nlohmann::json::parse(artifact_str);
257 if (!artifact_json.contains(
"functions")) {
258 info(
"Warning: No functions found in artifact");
264 for (
auto& function : artifact_json[
"functions"]) {
265 if (is_private_constrained_function(function)) {
266 private_functions.push_back(&function);
270 if (private_functions.empty()) {
271 info(
"No private constrained functions found");
276 generate_vks_for_functions(cache_dir, private_functions, force);
279 std::ofstream out_file(output_path);
280 out_file << artifact_json.dump(2) <<
std::endl;
283 info(
"Successfully processed: ", input_path,
" -> ", output_path);
289 std::vector<std::string> artifacts;
292 for (
const auto& entry : std::filesystem::recursive_directory_iterator(search_path)) {
293 if (!entry.is_regular_file()) {
297 const auto& path = entry.path();
300 if (path.extension() !=
".json") {
305 std::string path_str = path.string();
306 if (path_str.find(
"/target/") == std::string::npos && path_str.find(
"\\target\\") == std::string::npos) {
311 if (path_str.find(
"/cache/") != std::string::npos || path_str.find(
"\\cache\\") != std::string::npos ||
312 path_str.find(
".function_artifact_") != std::string::npos) {
316 artifacts.push_back(path.string());
326 if (artifacts.empty()) {
327 info(
"No contract artifacts found. Please compile your contracts first with 'nargo compile'.");
331 info(
"Found ", artifacts.size(),
" contract artifact(s) to process");
333 bool all_success =
true;
334 for (
const auto& artifact : artifacts) {
342 info(
"Contract postprocessing complete!");
352 if (!std::filesystem::exists(input_path)) {
357 auto artifact_content =
read_file(input_path);
358 std::string artifact_str(artifact_content.begin(), artifact_content.end());
359 auto artifact_json = nlohmann::json::parse(artifact_str);
361 if (!artifact_json.contains(
"functions")) {
367 auto cache_dir = get_cache_dir();
370 for (
const auto& function : artifact_json[
"functions"]) {
371 if (!is_private_constrained_function(function)) {
375 std::string fn_name = function[
"name"].get<std::string>();
376 auto bytecode = extract_bytecode(function);
377 std::string hash_str = compute_bytecode_hash(
bytecode);
378 std::filesystem::path vk_cache_path = cache_dir / (hash_str +
".vk");
381 std::cout << hash_str <<
":" << vk_cache_path.string() <<
":" << fn_name <<
std::endl;
385 }
catch (
const std::exception& e) {
386 info(
"Error getting cache paths: ", e.what());
std::shared_ptr< Napi::ThreadSafeFunction > bytecode
std::string base64_encode(unsigned char const *bytes_to_encode, size_t in_len, bool url)
Chonk-specific command definitions for the Barretenberg RPC API.
std::vector< uint8_t > decode_bytecode(const std::string &base64_bytecode)
void hash(State &state) noexcept
Sha256Hash sha256(const ByteContainer &input)
SHA-256 hash function (FIPS 180-4)
Entry point for Barretenberg command-line interface.
bool transpile_artifact(const std::string &input_path, const std::string &output_path)
Transpile the artifact file (or copy if transpiler not enabled)
bool process_all_artifacts(const std::string &search_path, bool force)
Process all discovered contract artifacts in a directory tree.
bool get_cache_paths(const std::string &input_path)
Get cache paths for all verification keys in an artifact.
bool process_aztec_artifact(const std::string &input_path, const std::string &output_path, bool force)
Process Aztec contract artifacts: transpile and generate verification keys.
std::vector< std::string > find_contract_artifacts(const std::string &search_path)
Find all contract artifacts in target/ directories.
std::vector< uint8_t > read_file(const std::string &filename, size_t bytes=0)
void set_parallel_for_concurrency(size_t num_cores)
void write_file(const std::string &filename, std::vector< uint8_t > const &data)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
void throw_or_abort(std::string const &err)