11#include <libdeflate.h>
18std::vector<uint8_t>
compress(
const std::vector<uint8_t>& input)
21 std::unique_ptr<libdeflate_compressor, void (*)(libdeflate_compressor*)>{ libdeflate_alloc_compressor(6),
22 libdeflate_free_compressor };
25 size_t max_compressed_size = libdeflate_gzip_compress_bound(compressor.get(), input.size());
26 std::vector<uint8_t> compressed(max_compressed_size);
28 size_t actual_compressed_size =
29 libdeflate_gzip_compress(compressor.get(), input.data(), input.size(), compressed.data(), compressed.size());
31 if (actual_compressed_size == 0) {
32 THROW std::runtime_error(
"Failed to compress data");
35 compressed.resize(actual_compressed_size);
42std::vector<uint8_t>
decompress(
const void* bytes,
size_t size)
46 content.resize(1024ULL * 128ULL);
48 auto decompressor = std::unique_ptr<libdeflate_decompressor, void (*)(libdeflate_decompressor*)>{
49 libdeflate_alloc_decompressor(), libdeflate_free_decompressor
51 size_t actual_size = 0;
52 libdeflate_result decompress_result =
53 libdeflate_gzip_decompress(decompressor.get(), bytes, size,
content.data(),
content.size(), &actual_size);
54 if (decompress_result == LIBDEFLATE_INSUFFICIENT_SPACE) {
59 if (decompress_result == LIBDEFLATE_BAD_DATA) {
60 THROW std::invalid_argument(
"bad gzip data in bb main");
74 fin.open(filename, std::ios::ate | std::ios::binary);
76 THROW std::invalid_argument(
"file not found");
78 if (fin.tellg() == -1) {
79 THROW std::invalid_argument(
"something went wrong");
82 size_t fsize =
static_cast<size_t>(fin.tellg());
83 fin.seekg(0, std::ios_base::beg);
86 std::string encoded_data(fsize,
'\0');
88 msgpack::unpack(encoded_data.data(), fsize).get().convert(result);
96 return unpack_from_file<std::vector<PrivateExecutionStepRaw>>(input_path);
108 const std::filesystem::path& input_path)
111 auto raw_steps =
load(input_path);
113 step.bytecode =
decompress(step.bytecode.data(), step.bytecode.size());
114 step.witness =
decompress(step.witness.data(), step.witness.size());
123 msgpack::unpack(
reinterpret_cast<const char*
>(
buf.data()),
buf.size()).get().convert(raw_steps);
138 for (
size_t i = 0; i < steps.size(); i++) {
147 if (step.
vk.empty()) {
151 precomputed_vks[i] = from_buffer<std::shared_ptr<Chonk::MegaVerificationKey>>(step.
vk);
165 info(
"DEPRECATED: Precomputed VKs expected for the given circuits.");
172 auto circuit = acir_format::create_circuit<MegaCircuitBuilder>(program, metadata);
174 info(
"Chonk: accumulating " + function_name);
177 ivc->accumulate(circuit, precomputed_vk);
184 const std::filesystem::path& output_path)
188 step.bytecode =
compress(step.bytecode);
189 step.witness =
compress(step.witness);
193 std::stringstream ss;
194 msgpack::pack(ss, steps);
195 std::string packed_data = ss.str();
198 std::ofstream file(output_path, std::ios::binary);
200 THROW std::runtime_error(
"Failed to open file for writing: " + output_path.string());
202 file.write(packed_data.data(),
static_cast<std::streamsize>(packed_data.size()));
Entry point for Barretenberg command-line interface.
std::vector< uint8_t > compress(const std::vector< uint8_t > &input)
Save modified ivc-inputs.msgpack when VKs are rewritten.
std::vector< uint8_t > decompress(const void *bytes, size_t size)
Decompress bytecode and witness fields from ivc-inputs.msgpack.
T unpack_from_file(const std::filesystem::path &filename)
Deserialize msgpack data from file.
VerifierCommitmentKey< Curve > vk
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
This is the msgpack encoding of the objects returned by the following typescript: const stepToStruct ...
std::vector< uint8_t > vk
std::vector< uint8_t > bytecode
static void compress_and_save(std::vector< PrivateExecutionStepRaw > &&steps, const std::filesystem::path &output_path)
std::string function_name
static std::vector< PrivateExecutionStepRaw > load_and_decompress(const std::filesystem::path &input_path)
std::vector< uint8_t > witness
static std::vector< PrivateExecutionStepRaw > parse_uncompressed(const std::vector< uint8_t > &buf)
static std::vector< PrivateExecutionStepRaw > load(const std::filesystem::path &input_path)
std::shared_ptr< Chonk > accumulate()
Creates a Chonk instance and accumulates each circuit in the folding stack. Uses precomputed VKs when...
std::vector< std::shared_ptr< Chonk::MegaVerificationKey > > precomputed_vks
Precomputed VKs (performance)
void parse(std::vector< PrivateExecutionStepRaw > &&steps)
Converts PrivateExecutionStepRaw entries (which contain raw bytecode/witness bytes) into structured A...
std::vector< acir_format::AcirProgram > folding_stack
ACIR programs with witnesses.
std::vector< std::string > function_names
Function names for logging.