Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
api_acir.cpp
Go to the documentation of this file.
8#ifndef __wasm__
9#include <iostream>
10#endif
11
12namespace bb {
13
14void acir_roundtrip(const std::filesystem::path& bytecode_path, const std::filesystem::path& output_path)
15{
16 auto buf = get_bytecode(bytecode_path.string());
17
18 BB_ASSERT(!buf.empty(), "acir_roundtrip: bytecode buffer is empty");
19 const uint8_t fmt = buf[0];
20 BB_ASSERT(fmt == 2 || fmt == 3,
21 "acir_roundtrip: expected msgpack format marker (2 or 3), got " + std::to_string(fmt));
22
23 // Deserialize from msgpack to Acir::Program (including Brillig/unconstrained_functions)
24 const char* data = reinterpret_cast<const char*>(buf.data() + 1);
25 size_t data_size = buf.size() - 1;
26 auto oh = msgpack::unpack(data, data_size);
27 auto o = oh.get();
28 BB_ASSERT(o.type == msgpack::type::ARRAY, "acir_roundtrip: expected ARRAY, got " + std::to_string(o.type));
29
30 Acir::Program program;
31#ifndef __wasm__
32 try {
33 o.convert(program);
34 } catch (const msgpack::type_error& e) {
35 std::cerr << "acir_roundtrip: failed to deserialize Program: " << e.what() << '\n';
36 throw;
37 }
38#else
39 o.convert(program);
40#endif
41
42 // Re-serialize to msgpack bytes
43 msgpack::sbuffer sbuf;
44 msgpack::packer<msgpack::sbuffer> packer(sbuf);
45 packer.pack(program);
46
47 // Build output: format marker byte followed by the msgpack payload
48 std::vector<uint8_t> raw_bytes;
49 raw_bytes.push_back(fmt);
50 raw_bytes.insert(raw_bytes.end(), sbuf.data(), sbuf.data() + sbuf.size());
51
52 write_file(output_path.string(), raw_bytes);
53 vinfo("acir_roundtrip: wrote roundtripped bytecode to ", output_path);
54}
55
56} // namespace bb
#define BB_ASSERT(expression,...)
Definition assert.hpp:70
#define vinfo(...)
Definition log.hpp:94
const std::vector< MemoryValue > data
uint8_t const * buf
Definition data_store.hpp:9
std::vector< uint8_t > get_bytecode(const std::string &bytecodePath)
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
void acir_roundtrip(const std::filesystem::path &bytecode_path, const std::filesystem::path &output_path)
Deserialize an ACIR program from bytecode (msgpack), re-serialize it, and write the result to an outp...
Definition api_acir.cpp:14
void write_file(const std::string &filename, std::span< const uint8_t > data)
Definition file_io.hpp:99
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::string to_string(bb::avm2::ValueTag tag)