12#include <libdeflate.h>
19std::vector<uint8_t>
compress(
const std::vector<uint8_t>& input)
22 std::unique_ptr<libdeflate_compressor, void (*)(libdeflate_compressor*)>{ libdeflate_alloc_compressor(6),
23 libdeflate_free_compressor };
26 size_t max_compressed_size = libdeflate_gzip_compress_bound(compressor.get(), input.size());
27 std::vector<uint8_t> compressed(max_compressed_size);
29 size_t actual_compressed_size =
30 libdeflate_gzip_compress(compressor.get(), input.data(), input.size(), compressed.data(), compressed.size());
32 if (actual_compressed_size == 0) {
33 THROW std::runtime_error(
"Failed to compress data");
36 compressed.resize(actual_compressed_size);
43std::vector<uint8_t>
decompress(
const void* bytes,
size_t size)
45 std::vector<uint8_t> content;
47 content.resize(1024ULL * 128ULL);
49 auto decompressor = std::unique_ptr<libdeflate_decompressor, void (*)(libdeflate_decompressor*)>{
50 libdeflate_alloc_decompressor(), libdeflate_free_decompressor
52 size_t actual_size = 0;
53 libdeflate_result decompress_result =
54 libdeflate_gzip_decompress(decompressor.get(), bytes, size, content.data(), content.size(), &actual_size);
55 if (decompress_result == LIBDEFLATE_INSUFFICIENT_SPACE) {
57 content.resize(content.size() * 2);
60 if (decompress_result == LIBDEFLATE_BAD_DATA) {
61 THROW std::invalid_argument(
"bad gzip data in bb main");
63 content.resize(actual_size);
75 fin.open(filename, std::ios::ate | std::ios::binary);
77 THROW std::invalid_argument(
"file not found");
79 if (fin.tellg() == -1) {
80 THROW std::invalid_argument(
"something went wrong");
83 size_t fsize =
static_cast<size_t>(fin.tellg());
84 fin.seekg(0, std::ios_base::beg);
87 std::string encoded_data(fsize,
'\0');
89 msgpack::unpack(encoded_data.data(), fsize).get().convert(result);
97 return unpack_from_file<std::vector<PrivateExecutionStepRaw>>(input_path);
109 const std::filesystem::path& input_path)
112 auto raw_steps =
load(input_path);
114 raw_steps[i].bytecode = decompress(raw_steps[i].bytecode.data(), raw_steps[i].bytecode.size());
115 raw_steps[i].witness = decompress(raw_steps[i].witness.data(), raw_steps[i].witness.size());
124 msgpack::unpack(
reinterpret_cast<const char*
>(
buf.data()),
buf.size()).get().convert(raw_steps);
140 PrivateExecutionStepRaw step = std::move(steps[i]);
142 acir_format::AcirFormat constraints = acir_format::circuit_buf_to_acir_format(std::move(step.bytecode));
143 acir_format::WitnessVector witness = acir_format::witness_buf_to_witness_vector(std::move(step.witness));
145 folding_stack[i] = { std::move(constraints), std::move(witness) };
146 if (step.vk.empty()) {
150 precomputed_vks[i] = from_buffer<std::shared_ptr<Chonk::MegaVerificationKey>>(step.vk);
162 for (
auto&
vk : precomputed_vks) {
164 info(
"DEPRECATED: Precomputed VKs expected for the given circuits.");
169 for (
auto [program, precomputed_vk, function_name] :
zip_view(folding_stack, precomputed_vks, function_names)) {
171 auto circuit = acir_format::create_circuit<MegaCircuitBuilder>(program, metadata);
173 info(
"Chonk: accumulating " + function_name);
176 ivc->accumulate(circuit, precomputed_vk);
183 const std::filesystem::path& output_path)
187 step.bytecode =
compress(step.bytecode);
188 step.witness =
compress(step.witness);
192 std::stringstream ss;
193 msgpack::pack(ss, steps);
194 std::string packed_data = ss.str();
197 std::ofstream file(output_path, std::ios::binary);
199 THROW std::runtime_error(
"Failed to open file for writing: " + output_path.string());
201 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.
void parallel_for(size_t num_iterations, const std::function< void(size_t)> &func)
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 > bytecode
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::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.