13#include <libdeflate.h>
20std::vector<uint8_t>
compress(
const std::vector<uint8_t>& input)
23 std::unique_ptr<libdeflate_compressor, void (*)(libdeflate_compressor*)>{ libdeflate_alloc_compressor(6),
24 libdeflate_free_compressor };
27 size_t max_compressed_size = libdeflate_gzip_compress_bound(compressor.get(), input.size());
28 std::vector<uint8_t> compressed(max_compressed_size);
30 size_t actual_compressed_size =
31 libdeflate_gzip_compress(compressor.get(), input.data(), input.size(), compressed.data(), compressed.size());
33 if (actual_compressed_size == 0) {
34 THROW std::runtime_error(
"Failed to compress data");
37 compressed.resize(actual_compressed_size);
44std::vector<uint8_t>
decompress(
const void* bytes,
size_t size)
46 std::vector<uint8_t> content;
48 content.resize(1024ULL * 128ULL);
50 auto decompressor = std::unique_ptr<libdeflate_decompressor, void (*)(libdeflate_decompressor*)>{
51 libdeflate_alloc_decompressor(), libdeflate_free_decompressor
53 size_t actual_size = 0;
54 libdeflate_result decompress_result =
55 libdeflate_gzip_decompress(decompressor.get(), bytes, size, content.data(), content.size(), &actual_size);
56 if (decompress_result == LIBDEFLATE_INSUFFICIENT_SPACE) {
58 content.resize(content.size() * 2);
61 if (decompress_result == LIBDEFLATE_BAD_DATA) {
62 THROW std::invalid_argument(
"bad gzip data in bb main");
64 content.resize(actual_size);
76 fin.open(filename, std::ios::ate | std::ios::binary);
78 THROW std::invalid_argument(
"file not found");
80 if (fin.tellg() == -1) {
81 THROW std::invalid_argument(
"something went wrong");
84 size_t fsize =
static_cast<size_t>(fin.tellg());
85 fin.seekg(0, std::ios_base::beg);
88 std::string encoded_data(fsize,
'\0');
91 msgpack::unpack(encoded_data.data(), fsize,
offset).get().convert(
result);
103 return unpack_from_file<std::vector<PrivateExecutionStepRaw>>(input_path);
115 const std::filesystem::path& input_path)
118 auto raw_steps =
load(input_path);
120 raw_steps[i].bytecode = decompress(raw_steps[i].bytecode.data(), raw_steps[i].bytecode.size());
121 raw_steps[i].witness = decompress(raw_steps[i].witness.data(), raw_steps[i].witness.size());
131 msgpack::unpack(
reinterpret_cast<const char*
>(buf.data()), buf.size(),
offset).get().convert(raw_steps);
132 if (
offset != buf.size()) {
148 kinds.resize(steps.size());
152 PrivateExecutionStepRaw step = std::move(steps[i]);
154 acir_format::AcirFormat constraints = acir_format::circuit_buf_to_mega_acir_format(std::move(step.bytecode));
155 acir_format::WitnessVector witness = acir_format::witness_buf_to_witness_vector(std::move(step.witness));
157 folding_stack[i] = { std::move(constraints), std::move(witness) };
158 if (step.vk.empty()) {
165 kinds[i] = step.kind;
173 for (
auto&
vk : precomputed_vks) {
175 info(
"DEPRECATED: Precomputed VKs expected for the given circuits.");
179 for (
size_t i = 0; i < folding_stack.size(); ++i) {
180 step_processor.process_step({ .name =
std::move(function_names[i]),
182 .precomputed_vk =
std::move(precomputed_vks[i]),
186 return step_processor.get_ivc();
190 const std::filesystem::path& output_path)
194 step.bytecode =
compress(step.bytecode);
195 step.witness =
compress(step.witness);
199 std::stringstream ss;
200 msgpack::pack(ss, steps);
201 std::string packed_data = ss.str();
204 std::ofstream file(output_path, std::ios::binary);
206 THROW std::runtime_error(
"Failed to open file for writing: " + output_path.string());
208 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
std::string to_string(bb::avm2::ValueTag tag)
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)
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::vector< uint8_t > > precomputed_vks
Serialized precomputed VKs (performance)
std::vector< std::string > function_names
Function names for logging.
std::vector< CircuitKind > kinds
Per-step CircuitKind.