Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
msgpack_impl.hpp
Go to the documentation of this file.
1#pragma once
2// Meant to be the main header included by *.cpp files* that use msgpack.
3// Note: heavy header due to serialization logic, don't include if msgpack.hpp will do
4// CBinding helpers that take a function or a lambda and
5// - bind the input as a coded msgpack array of all the arguments (using template metamagic)
6// - bind the return value to an out buffer, where the caller must free the memory
7
8#define MSGPACK_NO_BOOST
18
19#include <cstring>
20#include <type_traits>
21
28{
29 // Create a buffer to store the encoded data
30 msgpack::sbuffer buffer;
31 msgpack::pack(buffer, obj);
32
33 uint8_t* output = static_cast<uint8_t*>(aligned_alloc(64, buffer.size()));
34 memcpy(output, buffer.data(), buffer.size());
35 // Convert the buffer data to a string and return it
36 return { output, buffer.size() };
37}
38
39// This function is intended to bind a function to a MessagePack-formatted input data,
40// perform the function with the unpacked data, then pack the result back into MessagePack format.
41inline void msgpack_cbind_impl(const auto& func, // The function to be applied
42 const uint8_t* input_in, // The input data in MessagePack format
43 size_t input_len_in, // The length of the input data
44 uint8_t** output_out, // The output data in MessagePack format
45 size_t* output_len_out) // The length of the output data
46{
47 using FuncTraits = decltype(get_func_traits<decltype(func)>());
48 // Args: the parameter types of the function as a tuple.
49 typename FuncTraits::Args params;
50
51 // Unpack the input data into the parameter tuple.
52 msgpack::unpack(reinterpret_cast<const char*>(input_in), input_len_in).get().convert(params);
53
54 // Apply the function to the parameters, then encode the result into a MessagePack buffer.
55 auto [output, output_len] = msgpack_encode_buffer(FuncTraits::apply(func, params));
56
57 // Assign the output data and its length to the given output parameters.
58 *output_out = output;
59 *output_len_out = output_len;
60}
61
62// returns a C-style string json of the schema
63inline void msgpack_cbind_schema_impl(auto func, uint8_t** output_out, size_t* output_len_out)
64{
65 (void)func; // unused except for type
66 // Object representation of the cbind
67 auto cbind_obj = get_func_traits<decltype(func)>();
68 std::string schema = msgpack_schema_to_string(cbind_obj);
69 *output_out = static_cast<uint8_t*>(aligned_alloc(64, schema.size() + 1));
70 memcpy(*output_out, schema.c_str(), schema.size() + 1);
71 *output_len_out = schema.size();
72}
73
74// The CBIND_NOSCHEMA macro generates a function named 'cname' that decodes the input arguments from msgpack format,
75// calls the target function, and then encodes the return value back into msgpack format. It should be used over CBIND
76// in cases where we do not want schema generation, such as meta-functions that themselves give information to control
77// how the schema is interpreted.
78#define CBIND_NOSCHEMA(cname, func) \
79 WASM_EXPORT void cname(const uint8_t* input_in, size_t input_len_in, uint8_t** output_out, size_t* output_len_out) \
80 { \
81 msgpack_cbind_impl(func, input_in, input_len_in, output_out, output_len_out); \
82 }
83
84// The CBIND macro is a convenient utility that abstracts away several steps in binding C functions with msgpack
85// serialization. It creates two separate functions:
86// 1. cname function: This decodes the input arguments from msgpack format, calls the target function,
87// and then encodes the return value back into msgpack format.
88// 2. cname##__schema function: This creates a JSON schema of the function's input arguments and return type.
89#define CBIND(cname, func) \
90 CBIND_NOSCHEMA(cname, func) \
91 WASM_EXPORT void cname##__schema(uint8_t** output_out, size_t* output_len_out) \
92 { \
93 msgpack_cbind_schema_impl(func, output_out, output_len_out); \
94 }
uint8_t buffer[RANDOM_BUFFER_SIZE]
Definition engine.cpp:34
void msgpack_cbind_impl(const auto &func, const uint8_t *input_in, size_t input_len_in, uint8_t **output_out, size_t *output_len_out)
void msgpack_cbind_schema_impl(auto func, uint8_t **output_out, size_t *output_len_out)
std::pair< uint8_t *, size_t > msgpack_encode_buffer(auto &&obj)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::string msgpack_schema_to_string(const auto &obj)
Print's an object's derived msgpack schema as a string.