Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
private_execution_steps.cpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Complete, auditors: [Sergei], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
13#include <libdeflate.h>
14
15namespace bb {
16
20std::vector<uint8_t> compress(const std::vector<uint8_t>& input)
21{
22 auto compressor =
23 std::unique_ptr<libdeflate_compressor, void (*)(libdeflate_compressor*)>{ libdeflate_alloc_compressor(6),
24 libdeflate_free_compressor };
25
26 // Worst case size for gzip compression
27 size_t max_compressed_size = libdeflate_gzip_compress_bound(compressor.get(), input.size());
28 std::vector<uint8_t> compressed(max_compressed_size);
29
30 size_t actual_compressed_size =
31 libdeflate_gzip_compress(compressor.get(), input.data(), input.size(), compressed.data(), compressed.size());
32
33 if (actual_compressed_size == 0) {
34 THROW std::runtime_error("Failed to compress data");
35 }
36
37 compressed.resize(actual_compressed_size);
38 return compressed;
39}
40
44std::vector<uint8_t> decompress(const void* bytes, size_t size)
45{
46 std::vector<uint8_t> content;
47 // initial size guess
48 content.resize(1024ULL * 128ULL);
49 for (;;) {
50 auto decompressor = std::unique_ptr<libdeflate_decompressor, void (*)(libdeflate_decompressor*)>{
51 libdeflate_alloc_decompressor(), libdeflate_free_decompressor
52 };
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) {
57 // need a bigger buffer
58 content.resize(content.size() * 2);
59 continue;
60 }
61 if (decompress_result == LIBDEFLATE_BAD_DATA) {
62 THROW std::invalid_argument("bad gzip data in bb main");
63 }
64 content.resize(actual_size);
65 break;
66 }
67 return content;
68}
69
73template <typename T> T unpack_from_file(const std::filesystem::path& filename)
74{
75 std::ifstream fin;
76 fin.open(filename, std::ios::ate | std::ios::binary);
77 if (!fin.is_open()) {
78 THROW std::invalid_argument("file not found");
79 }
80 if (fin.tellg() == -1) {
81 THROW std::invalid_argument("something went wrong");
82 }
83
84 size_t fsize = static_cast<size_t>(fin.tellg());
85 fin.seekg(0, std::ios_base::beg);
86
87 T result;
88 std::string encoded_data(fsize, '\0');
89 fin.read(encoded_data.data(), static_cast<std::streamsize>(fsize));
91 msgpack::unpack(encoded_data.data(), fsize, offset).get().convert(result);
92 if (offset != fsize) {
93 THROW std::invalid_argument("msgpack input has trailing data (" + std::to_string(fsize - offset) +
94 " extra bytes)");
95 }
96 return result;
97}
98
99// TODO(#7371) we should not have so many levels of serialization here.
101{
102 BB_BENCH();
103 return unpack_from_file<std::vector<PrivateExecutionStepRaw>>(input_path);
104}
105
106// TODO(#7371) we should not have so many levels of serialization here.
112
113// TODO(#7371) we should not have so many levels of serialization here.
115 const std::filesystem::path& input_path)
116{
117 BB_BENCH();
118 auto raw_steps = load(input_path);
119 parallel_for(raw_steps.size(), [&](size_t i) {
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());
122 });
123 return raw_steps;
124}
125
127{
129 // Read with msgpack
131 msgpack::unpack(reinterpret_cast<const char*>(buf.data()), buf.size(), offset).get().convert(raw_steps);
132 if (offset != buf.size()) {
133 THROW std::invalid_argument("msgpack input has trailing data (" + std::to_string(buf.size() - offset) +
134 " extra bytes)");
135 }
136 // Unlike load_and_decompress, we don't need to decompress the bytecode and witness fields
137 return raw_steps;
138}
139
141{
142 BB_BENCH();
143
144 // Preallocate space to write into diretly as push_back would not be thread safe
145 folding_stack.resize(steps.size());
146 precomputed_vks.resize(steps.size());
147 function_names.resize(steps.size());
148 kinds.resize(steps.size());
149
150 // Parse each step's bytecode/witness in parallel (thread-safe with msgpack format)
151 parallel_for(steps.size(), [&](size_t i) {
152 PrivateExecutionStepRaw step = std::move(steps[i]);
153
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));
156
157 folding_stack[i] = { std::move(constraints), std::move(witness) };
158 if (step.vk.empty()) {
159 // For backwards compatibility, but it affects performance and correctness.
160 precomputed_vks[i] = {};
161 } else {
162 precomputed_vks[i] = std::move(step.vk);
163 }
164 function_names[i] = std::move(step.function_name);
165 kinds[i] = step.kind;
166 });
167}
168
169std::shared_ptr<Chonk> PrivateExecutionSteps::accumulate()
170{
171 auto step_processor = ChonkStepProcessor(kinds);
172
173 for (auto& vk : precomputed_vks) {
174 if (vk.empty()) {
175 info("DEPRECATED: Precomputed VKs expected for the given circuits.");
176 break;
177 }
178 }
179 for (size_t i = 0; i < folding_stack.size(); ++i) {
180 step_processor.process_step({ .name = std::move(function_names[i]),
181 .program = std::move(folding_stack[i]),
182 .precomputed_vk = std::move(precomputed_vks[i]),
183 .kind = kinds[i] });
184 }
185
186 return step_processor.get_ivc();
187}
188
189void PrivateExecutionStepRaw::compress_and_save(std::vector<PrivateExecutionStepRaw>&& steps,
190 const std::filesystem::path& output_path)
191{
192 // First, compress the bytecode and witness fields of each step
193 for (PrivateExecutionStepRaw& step : steps) {
194 step.bytecode = compress(step.bytecode);
195 step.witness = compress(step.witness);
196 }
197
198 // Serialize to msgpack
199 std::stringstream ss;
200 msgpack::pack(ss, steps);
201 std::string packed_data = ss.str();
202
203 // Write to file
204 std::ofstream file(output_path, std::ios::binary);
205 if (!file) {
206 THROW std::runtime_error("Failed to open file for writing: " + output_path.string());
207 }
208 file.write(packed_data.data(), static_cast<std::streamsize>(packed_data.size()));
209 file.close();
210}
211} // namespace bb
#define BB_BENCH()
Definition bb_bench.hpp:268
#define info(...)
Definition log.hpp:93
ssize_t offset
Definition engine.cpp:62
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
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)
Definition thread.cpp:112
VerifierCommitmentKey< Curve > vk
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::string to_string(bb::avm2::ValueTag tag)
This is the msgpack encoding of the objects returned by the following typescript: const stepToStruct ...
static std::vector< PrivateExecutionStepRaw > load_and_decompress(const std::filesystem::path &input_path)
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.
#define THROW
VectorField result