-
Notifications
You must be signed in to change notification settings - Fork 130
msgpack: friendly binary serialization format #374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 61 commits
Commits
Show all changes
97 commits
Select commit
Hold shift + click to select a range
f5378ee
Regenerate pedersen lookup tables if they're empty
suyash67 6b4eaa9
re-init generator tables if they're empty.
suyash67 e884272
feat(nullifier_tree): make empty nullifier tree leaves hash be 0 (#360)
Maddiaa0 42b7f17
More generators for aztec3.
suyash67 16df5ac
update js vk (because we now use UP for merkle hashing)
suyash67 274c862
Helpers for ECDSA in A3 (#364)
suyash67 7ed6df4
feat(nullifier_tree): make empty nullifier tree leaves hash be 0 (#360)
Maddiaa0 27f5f8b
Change pedersen hash c_bind to use `pedersen_hash::lookup`.
suyash67 2a72649
feat: add msgpack-c submodule
ludamad 1ae2066
Give up on msgpack c_master
ludamad db7c2bb
Working hacky msgpack test
ludamad 8a3adc8
Interim work
ludamad b475c3f
Interim work
ludamad 5769a0c
Getting rid of memory hacks
ludamad 22a2c5d
fix: memory leaks
ludamad e110c27
Start of demoing cbinds
ludamad 0077d20
Align with other methods
ludamad a81f909
chore: Remove need to return from msgpack method
ludamad 4abc221
Iterate example
ludamad 8f4fb51
fix: Hack around generator issues
ludamad d4901fb
feat: iterate on msgpack in bb
ludamad b76b7b2
fix: fork msgpack for greater checks
ludamad 4f685b3
Refactor
ludamad 5662640
cleanup
ludamad 53e0e1b
Update turbo_circuit_constructor.cpp
ludamad 2322e82
chore: continued cleanup
ludamad 70aff20
chore: continued cleanup
ludamad 3a22e06
chore: continued cleanup
ludamad 8b5e224
Refactor
ludamad 0ebf8f5
Refactor
ludamad 47c4a1f
fix: ci
ludamad 3fb25dc
feat(wasm): hacks to make work in a fno-exceptions wasm environment
ludamad 493c678
feat(wasm): bump msgpack-c
ludamad b35018f
feat(msgpack): first 'complex' object bound
ludamad a234981
More wasm fixes. Was breaking throw() declaration
ludamad 403992e
Fix field serialization
ludamad 489c492
refactoring
ludamad 69b73c0
Update CMakeLists.txt
ludamad 7e9d3fa
Remove // TODO redundant with msgpack
ludamad e70396a
Merge remote-tracking branch 'origin/adam/msgpack-aztec3' into adam/m…
ludamad 1d350c9
Refactor to use macro
ludamad bf1630f
Refactor to use macro
ludamad f903ab1
fix printing bug
ludamad 0615923
fix: fieldd msgpack endianness fix
ludamad 8d37c48
Merge aztec3 branch
ludamad c3d90da
fix: remove shared ptr reference
ludamad 9aafc34
doc
ludamad 716ff15
Add static checking for MSGPACK usage
ludamad bc792d0
Revert log.hpp change
ludamad 73c4b64
Update struct_map_impl.hpp
ludamad 276a9a5
Revert
ludamad 91cb331
remote_build fix
ludamad 50551e6
Keep trying to init submodules
ludamad 3dbe446
Keep trying to init submodules
ludamad 9ca6b53
Bump
ludamad 76ae948
Add missing init_submodules
ludamad 37122e7
Msgpack test fix
ludamad 66e22e6
Msgpack test fix
ludamad 54b655d
Msgpack test fix
ludamad 432ba96
Msgpack test fix
ludamad ea1b6de
Update polynomial_store.test.cpp
ludamad 1afc7f9
Merge remote-tracking branch 'origin/master' into adam/msgpack-aztec3
ludamad 129c9aa
Merge remote-tracking branch 'origin/master' into adam/msgpack-aztec3
ludamad 38fb021
Merge master
ludamad de02925
Update msgpack error
ludamad 0018e93
Better abort distinguishing
ludamad 4f94d55
fix: join split VK hash
ludamad 5c569c7
Serialization updates
ludamad b32bd48
Fix circuits build
ludamad 4a4e899
Try to make circuits test work again
ludamad 5818f3a
Try to make circuits test work again
ludamad 4ad083e
Try to make circuits test work again
ludamad cec5613
fix: initialization warning
ludamad 930b283
fix: prefer default constructor for field, related cleanup
ludamad e9c4d6e
Grand rename
ludamad f5c4f4b
chore: remove unused funcs
ludamad 4cc89e5
Revert fields constructor change for now
ludamad 264225c
chore: Revert .circleci changes
ludamad 0e0df3c
chore: Revert foundation removal
ludamad e5d8fe5
Revert .gitmodules
ludamad 88f79c7
Update affine_element.hpp
ludamad a50c338
Update element.hpp
ludamad cb01cf9
Revert header optimizations
ludamad 17e61fa
Revert init line
ludamad 47b4ea9
Update polynomial_store.test.cpp
ludamad 33aceca
Revert header optimization
ludamad cfc5a2d
Merge remote-tracking branch 'origin/adam/msgpack-aztec3' into adam/m…
ludamad af1e97e
Update raw_pointer.hpp
ludamad e3c8f9d
Update raw_pointer.hpp
ludamad 712ca1c
Update func_traits.hpp documentation
ludamad d5ae7b4
Document msgpack methods in field_impl.hpp
ludamad e3d0368
Update msgpack.hpp
ludamad 6c16964
Update cbind.hpp
ludamad 4e7f48c
Update msgpack.hpp
ludamad 4533702
Update msgpack.hpp
ludamad 3ff51bb
Update schema_impl.hpp
ludamad 579cc77
Update g1.hpp
ludamad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #!/bin/bash | ||
| # For a given repository, init any required submodules. | ||
| set -euo pipefail | ||
|
|
||
| REPOSITORY=$1 | ||
|
|
||
| BUILD_DIR=$(query_manifest buildDir $REPOSITORY) | ||
| if [ -d "$BUILD_DIR" ]; then | ||
| echo "Initialising any submodules under $REPOSITORY buildDir: $BUILD_DIR" | ||
| git submodule update --init --recursive $BUILD_DIR && exit || echo "No submodules under buildDir" | ||
| fi | ||
|
|
||
| SUBMODULE_PATH=$(query_manifest submodulePath $REPOSITORY) | ||
| if [ -n "$SUBMODULE_PATH" ]; then | ||
| # TODO: Needs to actually init all dependency submodules as well. | ||
| echo "Initialising submodule: $SUBMODULE_PATH" | ||
| git submodule update --init --recursive $SUBMODULE_PATH | ||
| fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| [submodule "foundation"] | ||
| path = foundation | ||
| url = git@github.com:AztecProtocol/foundation.git | ||
| [submodule "cpp/src/msgpack-c"] | ||
| path = cpp/src/msgpack-c | ||
| url = https://github.com/AztecProtocol/msgpack-c |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| #pragma once | ||
| /* Minimal header for msgpack. This should be included, *not* msgpack.hpp directly. | ||
| That should be treated as an implementation-only header. | ||
|
|
||
| ## Overview | ||
|
|
||
| The Msgpack module allows for efficient serialization and deserialization of data structures. It can be applied to map-like objects, array-like objects, and custom serialization/deserialization logic. | ||
|
|
||
| ## Binding objects | ||
|
|
||
| Marking structs/classes with their fields for msgpack allows you to pack and unpack the class. | ||
|
|
||
| 1. All objects bound should have a default constructor | ||
| 2. Objects can be tightly packed as binary (see field_impl.hpp), array-like, or map-like. See below | ||
| 3. You should list all fields of a class in the below methods, or use the custom method. | ||
|
|
||
| ### Map-like Objects | ||
|
|
||
| To make objects serializable as a map-like format, define the `msgpack` method in your class as follows: | ||
|
|
||
| ```cpp | ||
| void msgpack(auto ar) { | ||
| ar(NVP(composer_type, circuit_size, num_public_inputs, commitments, contains_recursive_proof, recursive_proof_public_input_indices)); | ||
| } | ||
| or | ||
| MSGPACK(composer_type, circuit_size, num_public_inputs, commitments, contains_recursive_proof, recursive_proof_public_input_indices); | ||
| ``` | ||
|
|
||
| This approach assumes 1. all members are default constructible 2. you give it all members 3. all members are writable references | ||
|
|
||
| This method maps the object's properties (e.g., `composer_type`, `circuit_size`, etc.) to their respective keys in the serialized data. | ||
|
|
||
| ### Array-like Objects | ||
|
|
||
| For array-like objects, define the `msgpack` method similarly: | ||
|
|
||
| ```cpp | ||
|
ludamad marked this conversation as resolved.
Outdated
|
||
| void msgpack(auto ar) { | ||
| ar(NVP(composer_type, circuit_size, num_public_inputs, commitments, contains_recursive_proof, recursive_proof_public_input_indices)); | ||
| } | ||
| ``` | ||
|
|
||
| This will serialize the object's properties as an array, rather than a map. | ||
|
|
||
| ### Custom Serialization and Deserialization | ||
|
|
||
| For custom serialization and deserialization, define `msgpack_pack` and `msgpack_unpack` methods in your class: | ||
|
|
||
| ```cpp | ||
| // For serialization | ||
| template <class Params> void field<Params>::msgpack_pack(auto& packer) const | ||
| { | ||
| auto adjusted = from_montgomery_form(); | ||
| uint64_t bin_data[4] = { | ||
| htonll(adjusted.data[3]), htonll(adjusted.data[2]), htonll(adjusted.data[1]), htonll(adjusted.data[0]) | ||
| }; | ||
| packer.pack_bin(sizeof(bin_data)); | ||
| packer.pack_bin_body((const char*)bin_data, sizeof(bin_data)); | ||
| } | ||
|
|
||
| // For deserialization | ||
| template <class Params> void field<Params>::msgpack_unpack(auto o) | ||
| { | ||
| msgpack::read_bin64(o, data, 4); | ||
| uint64_t reversed[] = {data[3], data[2], data[1], data[0]}; | ||
| for (int i = 0; i < 4; i++) { | ||
| data[i] = reversed[i]; | ||
| } | ||
| *this = to_montgomery_form(); | ||
| } | ||
| ``` | ||
|
|
||
| These methods allow you to implement custom logic for the serialization and deserialization processes. | ||
|
|
||
|
|
||
| ## Packing/Unpacking | ||
|
|
||
| Only when actually using msgpack to write or read data, include "barretenberg/common/msgpack_impl.hpp". | ||
| You can then use msgpack library features to serialize and deserialize C++ objects. | ||
|
|
||
| e.g. packing | ||
| ``` | ||
| // Create a buffer to store the encoded data | ||
| msgpack::sbuffer buffer; | ||
| msgpack::pack(buffer, obj); | ||
|
|
||
| uint8_t* output = (uint8_t*)aligned_alloc(64, buffer.size()); | ||
| memcpy(output, buffer.data(), buffer.size()); | ||
| // Convert the buffer data to a string and return it | ||
| return { output, buffer.size() }; | ||
| ``` | ||
|
|
||
| e.g. unpacking | ||
|
|
||
| ``` | ||
| msgpack::unpack((const char*)encoded_data, encoded_data_size).get().convert(*value); | ||
| ``` | ||
|
|
||
| */ | ||
| #include <type_traits> | ||
| #include "msgpack/name_value_pair_macro.hpp" | ||
| #include "msgpack/concepts.hpp" | ||
|
|
||
| // Helper for above documented syntax | ||
| #define MSGPACK(...) \ | ||
| void msgpack(auto pack_fn) { \ | ||
| pack_fn(NVP(__VA_ARGS__)); \ | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| #pragma once | ||
|
|
||
| struct DoNothing { | ||
| void operator()(auto...){} | ||
| }; | ||
| namespace msgpack { | ||
| template<typename T> | ||
| concept HasMsgPack = requires(T t, DoNothing nop) { t.msgpack(nop); }; | ||
| template<typename T> | ||
| concept HasMsgPackPack = requires(T t, DoNothing nop) { t.msgpack_pack(nop); }; | ||
| } |
34 changes: 34 additions & 0 deletions
34
cpp/src/barretenberg/common/msgpack/name_value_pair_macro.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| #pragma once | ||
|
|
||
| /* start of #define NVP: | ||
| * expands to name-value pairs (NVPs), e.g. NVP(x,y,z) -> "x", x, "y", y", "z", z | ||
| * used in msgpack serialization. */ | ||
|
|
||
| // hacky counting of variadic macro params: | ||
| #define VA_NARGS_IMPL(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, N, ...) N | ||
| #define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1) | ||
|
|
||
| // name-value pair expansion for variables | ||
| // used in msgpack map expansion | ||
| // n<=3 case | ||
| #define _NVP1(x) #x, x | ||
| #define _NVP2(x, y) #x, x, #y, y | ||
| #define _NVP3(x, y, z) #x, x, #y, y, #z, z | ||
| // n>3 cases | ||
| #define _NVP4(x, ...) _NVP1(x), _NVP3(__VA_ARGS__) | ||
| #define _NVP5(x, ...) _NVP1(x), _NVP4(__VA_ARGS__) | ||
| #define _NVP6(x, ...) _NVP1(x), _NVP5(__VA_ARGS__) | ||
| #define _NVP7(x, ...) _NVP1(x), _NVP6(__VA_ARGS__) | ||
| #define _NVP8(x, ...) _NVP1(x), _NVP7(__VA_ARGS__) | ||
| #define _NVP9(x, ...) _NVP1(x), _NVP8(__VA_ARGS__) | ||
| #define _NVP10(x, ...) _NVP1(x), _NVP9(__VA_ARGS__) | ||
| #define _NVP11(x, ...) _NVP1(x), _NVP10(__VA_ARGS__) | ||
| #define _NVP12(x, ...) _NVP1(x), _NVP11(__VA_ARGS__) | ||
| #define _NVP13(x, ...) _NVP1(x), _NVP12(__VA_ARGS__) | ||
| #define _NVP14(x, ...) _NVP1(x), _NVP13(__VA_ARGS__) | ||
| #define _NVP15(x, ...) _NVP1(x), _NVP14(__VA_ARGS__) | ||
|
|
||
| #define CONCAT(a, b) a ## b | ||
| #define _NVP_N(n) CONCAT(_NVP, n) | ||
| #define NVP(...) _NVP_N(VA_NARGS(__VA_ARGS__))(__VA_ARGS__) | ||
| // end of #define NVP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| #pragma once | ||
| // Note: heavy header due to serialization logic, don't include if msgpack.hpp will do | ||
| #include <string> | ||
| #include <iostream> | ||
| #include <iomanip> | ||
| #include <sstream> | ||
| #include <cassert> | ||
| #define MSGPACK_NO_BOOST | ||
| #include <msgpack.hpp> | ||
|
ludamad marked this conversation as resolved.
Outdated
|
||
| #include "concepts.hpp" | ||
|
|
||
| namespace msgpack { | ||
| template <typename Tuple, std::size_t... Is> auto drop_keys_impl(Tuple&& tuple, std::index_sequence<Is...>) | ||
| { | ||
| // Expand 0 to n/2 to 1 to n+1 (increments of 2) | ||
|
ludamad marked this conversation as resolved.
Outdated
|
||
| // Use it to filter the tuple | ||
| return std::tie(std::get<Is * 2 + 1>(std::forward<Tuple>(tuple))...); | ||
| } | ||
|
|
||
| template <typename... Args> auto drop_keys(std::tuple<Args...>&& tuple) | ||
| { | ||
| static_assert(sizeof...(Args) % 2 == 0, "Tuple must contain an even number of elements"); | ||
| // Compile time sequence of integers from 0 to n/2 | ||
| auto compile_time_0_to_n_div_2 = std::make_index_sequence<sizeof...(Args) / 2>{}; | ||
| return drop_keys_impl(tuple, compile_time_0_to_n_div_2); | ||
| } | ||
| } // namespace msgpack | ||
|
|
||
| namespace msgpack { | ||
| template <typename T, typename... Args> | ||
| concept MsgpackConstructible = requires(T object, Args... args) { | ||
| T{args...}; | ||
| }; | ||
| } | ||
|
|
||
| namespace msgpack::adaptor { | ||
| // reads structs with msgpack() method from a JSON-like dictionary | ||
| template <HasMsgPack T> struct convert<T> { | ||
| msgpack::object const& operator()(msgpack::object const& o, T& v) const | ||
| { | ||
| static_assert(std::is_default_constructible_v<T>, "MSGPACK requires default-constructible types"); | ||
|
ludamad marked this conversation as resolved.
Outdated
|
||
| v.msgpack([&](auto&... args) { | ||
| auto static_checker = [&](auto&... value_args) { | ||
| static_assert(msgpack::MsgpackConstructible<T, decltype(value_args)...>, | ||
| "MSGPACK requires a constructor that can take the types listed in MSGPACK. " | ||
| "Type or arg count mismatch, or member initializer constructor not available."); | ||
| }; | ||
| std::apply(static_checker, drop_keys(std::tie(args...))); | ||
| msgpack::type::define_map<decltype(args)...>{args...}.msgpack_unpack(o); | ||
| }); | ||
| return o; | ||
| } | ||
| }; | ||
|
|
||
| // converts structs with msgpack() method from a JSON-like dictionary | ||
| template <HasMsgPack T> struct pack<T> { | ||
| template <typename Stream> packer<Stream>& operator()(msgpack::packer<Stream>& o, T const& v) const | ||
| { | ||
| static_assert(std::is_default_constructible_v<T>, "T requires a default-constructor for MSGPACK"); | ||
| const_cast<T&>(v).msgpack([&](auto&... args) { | ||
| auto static_checker = [&](auto &... value_args) { | ||
| static_assert(msgpack::MsgpackConstructible<T, decltype(value_args)...>, | ||
| "T requires a constructor that can take the types listed in MSGPACK. " | ||
| "Type or arg count mismatch, or member initializer constructor not available."); | ||
| }; | ||
| std::apply(static_checker, drop_keys(std::tie(args...))); | ||
| msgpack::type::define_map<decltype(args)...>{args...}.msgpack_pack(o); }); | ||
| return o; | ||
| } | ||
| }; | ||
|
|
||
| } // namespace msgpack::adaptor | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| #pragma once | ||
| // Note: Meant to only be included in compilation units that need msgpack | ||
| #define MSGPACK_NO_BOOST | ||
| #include <msgpack.hpp> | ||
| #include <variant> | ||
|
|
||
| namespace msgpack::adaptor { | ||
| // writes std::variant to msgpack format (TODO should we read std::variant?) | ||
| template<typename... T> | ||
| struct pack<std::variant< T...>> { | ||
| auto &operator()(auto& o, std::variant<T...> const &variant) const { | ||
| std::visit([&o](auto &&arg) { msgpack::pack(o, arg); }, variant); | ||
| return o; | ||
| } | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| #pragma once | ||
| // Note: Meant to only be included in compilation units that need msgpack | ||
| #include "msgpack/struct_map_impl.hpp" | ||
| #include "msgpack/variant_impl.hpp" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.