Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
get_bytecode.cpp
Go to the documentation of this file.
1#include "get_bytecode.hpp"
3#include "base64.hpp"
4#include <string>
5#include <vector>
6
7#ifndef __wasm__
8#include <fstream>
9#include <libdeflate.h>
10#include <nlohmann/json.hpp>
11#include <stdexcept>
12#endif
13
14namespace {
15
16std::vector<uint8_t> gzip_decompress([[maybe_unused]] const std::vector<uint8_t>& compressed)
17{
18#ifdef __wasm__
19 throw_or_abort("gzip_decompress not supported in WASM");
20#else
21 std::vector<uint8_t> decompressed;
22 decompressed.resize(1024ULL * 128ULL); // Initial size guess
23
24 for (;;) {
25 auto decompressor = std::unique_ptr<libdeflate_decompressor, void (*)(libdeflate_decompressor*)>{
26 libdeflate_alloc_decompressor(), libdeflate_free_decompressor
27 };
28 size_t actual_size = 0;
29 libdeflate_result result = libdeflate_gzip_decompress(decompressor.get(),
30 compressed.data(),
31 compressed.size(),
32 decompressed.data(),
33 decompressed.size(),
34 &actual_size);
35
36 if (result == LIBDEFLATE_INSUFFICIENT_SPACE) {
37 decompressed.resize(decompressed.size() * 2);
38 continue;
39 }
40 if (result == LIBDEFLATE_BAD_DATA) {
41 throw std::runtime_error("Invalid gzip data");
42 }
43 decompressed.resize(actual_size);
44 break;
45 }
46 return decompressed;
47#endif
48}
49} // namespace
50
51std::vector<uint8_t> decode_bytecode(const std::string& base64_bytecode)
52{
53 // Decode base64 and decompress using libdeflate for gzip
54 std::string decoded = base64_decode(base64_bytecode, false);
55 std::vector<uint8_t> gzipped(decoded.begin(), decoded.end());
56 return gzip_decompress(gzipped);
57}
58
59std::vector<uint8_t> get_bytecode_from_json([[maybe_unused]] const std::string& json_path)
60{
61#ifdef __wasm__
62 throw_or_abort("get_bytecode_from_json not supported in WASM");
63#else
64 std::ifstream json_file(json_path);
65 if (!json_file.is_open()) {
66 throw std::runtime_error("Failed to open JSON file: " + json_path);
67 }
68
69 nlohmann::json json_data = nlohmann::json::parse(json_file);
70 std::string base64_bytecode = json_data["bytecode"];
71
72 return decode_bytecode(base64_bytecode);
73#endif
74}
75
76std::vector<uint8_t> gunzip([[maybe_unused]] const std::string& path)
77{
78#ifdef __wasm__
79 throw_or_abort("gunzip not supported in WASM");
80#else
81 std::ifstream file(path, std::ios::binary);
82 if (!file.is_open()) {
83 throw std::runtime_error("Failed to open file: " + path);
84 }
85
86 std::vector<uint8_t> compressed((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
87 return gzip_decompress(compressed);
88#endif
89}
90
91std::vector<uint8_t> get_bytecode([[maybe_unused]] const std::string& bytecodePath)
92{
93#ifdef __wasm__
94 throw_or_abort("get_bytecode not supported in WASM");
95#else
96 if (bytecodePath == "-") {
98 }
99 std::filesystem::path filePath = bytecodePath;
100 if (filePath.extension() == ".json") {
101 // Try reading json files as if they are a Nargo build artifact
102 return get_bytecode_from_json(bytecodePath);
103 }
104
105 // For other extensions, assume file is a raw ACIR program
106 return gunzip(bytecodePath);
107#endif
108}
std::string base64_decode(std::string const &s, bool remove_linebreaks)
Definition base64.cpp:249
std::vector< uint8_t > get_bytecode_from_json(const std::string &json_path)
std::vector< uint8_t > decode_bytecode(const std::string &base64_bytecode)
std::vector< uint8_t > get_bytecode(const std::string &bytecodePath)
std::vector< uint8_t > gunzip(const std::string &path)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
void throw_or_abort(std::string const &err)