barretenberg
Loading...
Searching...
No Matches
src
barretenberg
serialize
msgpack.hpp
1
#pragma once
2
/* Minimal header for declaring msgpack fields.
3
This should be included as "barretenberg/serialize/msgpack.hpp" unless a translation wants
4
to use msgpack for bindings, then "barretenberg/serialize/cbind.hpp" should be included.
5
6
## Overview
7
8
The Msgpack module allows for efficient serialization and deserialization of data structures. It can be applied to
9
map-like objects, array-like objects, and custom serialization/deserialization logic.
10
11
## Binding objects
12
13
Marking structs/classes with their fields for msgpack allows you to pack and unpack the class.
14
15
1. All objects bound should have a default constructor
16
2. Objects can be tightly packed as binary (see field_impl.hpp), array-like, or map-like. See below
17
3. You should list all fields of a class in the below methods, or use the custom method.
18
19
### Typical Objects
20
21
To make objects serializable as a map-like format, define the `msgpack` method in your class as follows:
22
23
```cpp
24
void msgpack(auto ar) {
25
ar(NVP(circuit_type, circuit_size, num_public_inputs, commitments, contains_recursive_proof,
26
recursive_proof_public_input_indices));
27
}
28
or
29
MSGPACK_FIELDS(circuit_type, circuit_size, num_public_inputs, commitments, contains_recursive_proof,
30
recursive_proof_public_input_indices);
31
```
32
33
This approach assumes 1. all members are default constructible 2. you give it all members 3. all members are writable
34
references
35
36
This method maps the object's properties (e.g., `circuit_type`, `circuit_size`, etc.) to their respective keys in the
37
serialized data.
38
39
40
### Custom Serialization and Deserialization
41
42
For custom serialization and deserialization, define `msgpack_pack` and `msgpack_unpack` methods in your class:
43
44
```cpp
45
// For serialization
46
template <class Params> void field<Params>::msgpack_pack(auto& packer) const
47
{
48
auto adjusted = from_montgomery_form();
49
uint64_t bin_data[4] = {
50
htonll(adjusted.data[3]), htonll(adjusted.data[2]), htonll(adjusted.data[1]), htonll(adjusted.data[0])
51
};
52
packer.pack_bin(sizeof(bin_data));
53
packer.pack_bin_body((const char*)bin_data, sizeof(bin_data));
54
}
55
56
// For deserialization
57
template <class Params> void field<Params>::msgpack_unpack(auto o)
58
{
59
msgpack::read_bin64(o, data, 4);
60
uint64_t reversed[] = {data[3], data[2], data[1], data[0]};
61
for (int i = 0; i < 4; i++) {
62
data[i] = reversed[i];
63
}
64
*this = to_montgomery_form();
65
}
66
```
67
68
These methods allow you to implement custom logic for the serialization and deserialization processes.
69
70
71
## Packing/Unpacking
72
73
Only when actually using msgpack to write or read data, include "barretenberg/serialize/cbind.hpp".
74
You can then use msgpack library features to serialize and deserialize C++ objects.
75
76
e.g. packing
77
```
78
// Create a buffer to store the encoded data
79
msgpack::sbuffer buffer;
80
msgpack::pack(buffer, obj);
81
82
uint8_t* output = (uint8_t*)aligned_alloc(64, buffer.size());
83
memcpy(output, buffer.data(), buffer.size());
84
// Convert the buffer data to a string and return it
85
return { output, buffer.size() };
86
```
87
88
e.g. unpacking
89
90
```
91
msgpack::unpack((const char*)encoded_data, encoded_data_size).get().convert(*value);
92
```
93
*/
94
#include "msgpack_impl/concepts.hpp"
95
#include "msgpack_impl/name_value_pair_macro.hpp"
96
#include <type_traits>
97
98
// Helper for above documented syntax
99
// Define a macro that takes any amount of parameters and expands to a msgpack method definition
100
// __VA__ARGS__ expands to the parmeters, comma separated.
101
#define MSGPACK_FIELDS(...) \
102
void msgpack(auto pack_fn) \
103
{ \
104
pack_fn(NVP(__VA_ARGS__)); \
105
}
Generated by
1.9.6