6#include "barretenberg/bbapi/generated/bb_types.hpp"
16#include <nlohmann/json.hpp>
20#ifdef ENABLE_AVM_TRANSPILER
22#include <avm_transpiler.h>
32std::vector<uint8_t> extract_bytecode(
const nlohmann::json& function)
34 if (!function.contains(
"bytecode")) {
38 const auto& base64_bytecode = function[
"bytecode"].get<std::string>();
45std::string compute_bytecode_hash(
const std::vector<uint8_t>&
bytecode)
48 std::ostringstream oss;
49 for (
auto byte : hash) {
50 oss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(
byte);
58std::filesystem::path get_cache_dir()
64 std::filesystem::path cache_dir = std::filesystem::path(home) /
".bb" /
BB_VERSION /
"vk_cache";
65 std::filesystem::create_directories(cache_dir);
72bool is_private_constrained_function(
const nlohmann::json& function)
74 bool is_public =
false;
75 bool is_unconstrained =
false;
78 if (function.contains(
"custom_attributes") && function[
"custom_attributes"].is_array()) {
79 for (
const auto& attr : function[
"custom_attributes"]) {
80 if (attr.is_string() && attr.get<std::string>() ==
"public") {
88 if (function.contains(
"is_unconstrained") && function[
"is_unconstrained"].is_boolean()) {
89 is_unconstrained = function[
"is_unconstrained"].get<
bool>();
92 return !is_public && !is_unconstrained;
98std::vector<uint8_t> get_or_generate_cached_vk(
const std::filesystem::path& cache_dir,
99 const std::string& circuit_name,
100 const std::vector<uint8_t>&
bytecode,
103 std::string hash_str = compute_bytecode_hash(
bytecode);
104 std::filesystem::path vk_cache_path = cache_dir / (hash_str +
".vk");
107 if (!force && std::filesystem::exists(vk_cache_path)) {
108 info(
"Verification key already in cache: ", hash_str);
113 info(
"Generating verification key: ", hash_str);
114 bbapi::BBApiRequest request;
117 bbapi::wire::ChonkComputeVk{ .circuit = bbapi::wire::CircuitInputNoVK{
118 .name = circuit_name, .bytecode =
bytecode } });
123 return response.bytes;
129void generate_vks_for_functions(
const std::filesystem::path& cache_dir,
138 const size_t num_functions = functions.size();
143 size_t actual_tasks = std::min(num_functions, total_cpus);
144 size_t threads_per_task = std::min(total_cpus,
std::max(
size_t{ 2 }, total_cpus / actual_tasks * 2));
147 std::atomic<size_t> current_function{ 0 };
150 auto worker = [&]() {
156 while ((func_idx = current_function.fetch_add(1)) < num_functions) {
157 auto* function = functions[func_idx];
158 std::string fn_name = (*function)[
"name"].get<std::string>();
161 auto bytecode = extract_bytecode(*function);
164 get_or_generate_cached_vk(cache_dir, fn_name,
bytecode, force);
169 std::vector<std::thread> threads;
170 threads.reserve(actual_tasks);
172 for (
size_t i = 0; i < actual_tasks; ++i) {
173 threads.emplace_back(worker);
177 for (
auto& t : threads) {
182 for (
auto* function : functions) {
183 std::string fn_name = (*function)[
"name"].get<std::string>();
186 auto bytecode = extract_bytecode(*function);
189 std::string hash_str = compute_bytecode_hash(
bytecode);
190 std::filesystem::path vk_cache_path = cache_dir / (hash_str +
".vk");
194 std::string encoded_vk =
base64_encode(vk_data.data(), vk_data.size(),
false);
195 (*function)[
"verification_key"] = encoded_vk;
204bool transpile_artifact([[maybe_unused]]
const std::string& input_path, [[maybe_unused]]
const std::string& output_path)
206#ifdef ENABLE_AVM_TRANSPILER
207 info(
"Transpiling: ", input_path,
" -> ", output_path);
209 auto result = avm_transpile_file(input_path.c_str(), output_path.c_str());
211 if (result.success == 0) {
212 if (result.error_message) {
213 std::string error_msg(result.error_message);
214 if (error_msg ==
"Contract already transpiled") {
216 if (input_path != output_path) {
217 std::filesystem::copy_file(
218 input_path, output_path, std::filesystem::copy_options::overwrite_existing);
221 info(
"Transpilation failed: ", error_msg);
222 avm_free_result(&result);
226 info(
"Transpilation failed");
227 avm_free_result(&result);
232 avm_free_result(&result);
234 info(
"Transpiled: ", input_path,
" -> ", output_path);
236 throw_or_abort(
"AVM Transpiler is not enabled. Please enable it to use bb aztec_process.");
248 if (!std::filesystem::exists(output_path)) {
253 auto cache_dir = get_cache_dir();
254 info(
"Generating verification keys for functions in ", std::filesystem::path(output_path).filename().
string());
255 info(
"Cache directory: ", cache_dir.string());
258 auto artifact_content =
read_file(output_path);
259 std::string artifact_str(artifact_content.begin(), artifact_content.end());
260 auto artifact_json = nlohmann::json::parse(artifact_str);
262 if (!artifact_json.contains(
"functions")) {
263 info(
"Warning: No functions found in artifact");
270 const std::string internal_prefix =
"__aztec_nr_internals__";
271 for (
auto& function : artifact_json[
"functions"]) {
272 auto& name = function[
"name"];
273 if (name.is_string()) {
274 std::string fn_name = name.get<std::string>();
275 if (fn_name.size() >= internal_prefix.size() &&
276 fn_name.compare(0, internal_prefix.size(), internal_prefix) == 0) {
277 name = fn_name.substr(internal_prefix.size());
284 for (
auto& function : artifact_json[
"functions"]) {
285 if (is_private_constrained_function(function)) {
286 private_functions.push_back(&function);
290 if (!private_functions.empty()) {
292 generate_vks_for_functions(cache_dir, private_functions, force);
294 info(
"No private constrained functions found");
298 std::ofstream out_file(output_path);
299 out_file << artifact_json.dump(2) <<
std::endl;
302 info(
"Successfully processed: ", input_path,
" -> ", output_path);
308 std::vector<std::string> artifacts;
311 for (
const auto& entry : std::filesystem::recursive_directory_iterator(search_path)) {
312 if (!entry.is_regular_file()) {
316 const auto& path = entry.path();
319 if (path.extension() !=
".json") {
324 std::string path_str = path.string();
325 if (path_str.find(
"/target/") == std::string::npos && path_str.find(
"\\target\\") == std::string::npos) {
330 if (path_str.find(
"/cache/") != std::string::npos || path_str.find(
"\\cache\\") != std::string::npos ||
331 path_str.find(
".function_artifact_") != std::string::npos) {
335 artifacts.push_back(path.string());
345 if (artifacts.empty()) {
346 info(
"No contract artifacts found in '", search_path,
"'.");
350 info(
"Found ", artifacts.size(),
" contract artifact(s) to process");
352 bool all_success =
true;
353 for (
const auto& artifact : artifacts) {
361 info(
"Contract postprocessing complete!");
371 if (!std::filesystem::exists(input_path)) {
376 auto artifact_content =
read_file(input_path);
377 std::string artifact_str(artifact_content.begin(), artifact_content.end());
378 auto artifact_json = nlohmann::json::parse(artifact_str);
380 if (!artifact_json.contains(
"functions")) {
386 auto cache_dir = get_cache_dir();
389 for (
const auto& function : artifact_json[
"functions"]) {
390 if (!is_private_constrained_function(function)) {
394 std::string fn_name = function[
"name"].get<std::string>();
395 auto bytecode = extract_bytecode(function);
396 std::string hash_str = compute_bytecode_hash(
bytecode);
397 std::filesystem::path vk_cache_path = cache_dir / (hash_str +
".vk");
400 std::cout << hash_str <<
":" << vk_cache_path.string() <<
":" << fn_name <<
std::endl;
404 }
catch (
const std::exception& e) {
405 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)
Non-template handler declarations for the bb service.
Shared type definitions for the Barretenberg RPC API.
std::vector< uint8_t > decode_bytecode(const std::string &base64_bytecode)
wire::ChonkComputeVkResponse handle_chonk_compute_vk(BBApiRequest &, wire::ChonkComputeVk &&cmd)
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::span< const uint8_t > data)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
void throw_or_abort(std::string const &err)