Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
serialize.hpp File Reference
#include "barretenberg/common/log.hpp"
#include "barretenberg/common/net.hpp"
#include "barretenberg/serialize/msgpack_apply.hpp"
#include <array>
#include <cassert>
#include <iostream>
#include <map>
#include <memory>
#include <optional>
#include <type_traits>
#include <vector>

Go to the source code of this file.

Namespaces

namespace  serialize
 
namespace  std
 STL namespace.
 

Concepts

concept  IntegralOrEnum
 
concept  std::HasGetAll
 

Typedefs

using uint128_t = unsigned __int128
 
using in_buf32 = uint8_t const *
 
using out_buf32 = uint8_t *
 
using in_buf64 = uint8_t const *
 
using out_buf64 = uint8_t *
 
using in_buf128 = uint8_t const *
 
using out_buf128 = uint8_t *
 
using in_str_buf = uint8_t const *
 
using out_str_buf = uint8_t **
 
using in_ptr = void *const *
 
using out_ptr = void **
 

Functions

void serialize::read (auto &it, msgpack_concepts::HasMsgPack auto &obj)
 Automatically derived read for any object that defines .msgpack() (implicitly defined by MSGPACK_FIELDS).
 
void serialize::write (auto &buf, const msgpack_concepts::HasMsgPack auto &obj)
 Automatically derived write for any object that defines .msgpack() (implicitly defined by MSGPACK_FIELDS).
 
void serialize::read (uint8_t const *&it, uint8_t &value)
 
void serialize::write (uint8_t *&it, uint8_t value)
 
void serialize::read (uint8_t const *&it, bool &value)
 
void serialize::write (uint8_t *&it, bool value)
 
void serialize::read (uint8_t const *&it, uint16_t &value)
 
void serialize::write (uint8_t *&it, uint16_t value)
 
void serialize::read (uint8_t const *&it, uint32_t &value)
 
void serialize::write (uint8_t *&it, uint32_t value)
 
void serialize::read (uint8_t const *&it, uint64_t &value)
 
void serialize::write (uint8_t *&it, uint64_t value)
 
void serialize::read (uint8_t const *&it, uint128_t &value)
 
void serialize::write (uint8_t *&it, uint128_t value)
 
void serialize::read (std::vector< uint8_t > const &buf, std::integral auto &value)
 
void serialize::write (std::vector< uint8_t > &buf, const std::integral auto &value)
 
void serialize::read (std::istream &is, std::integral auto &value)
 
void serialize::write (std::ostream &os, const std::integral auto &value)
 
void std::read (auto &buf, std::integral auto &value)
 
void std::write (auto &buf, std::integral auto value)
 
template<size_t N>
void std::read (uint8_t const *&it, std::array< uint8_t, N > &value)
 
template<size_t N>
void std::write (uint8_t *&buf, std::array< uint8_t, N > const &value)
 
void std::read (uint8_t const *&it, std::vector< uint8_t > &value)
 
void std::write (uint8_t *&buf, std::vector< uint8_t > const &value)
 
void std::read (std::istream &is, std::vector< uint8_t > &value)
 
void std::write (std::ostream &os, std::vector< uint8_t > const &value)
 
template<size_t N>
void std::write (std::vector< uint8_t > &buf, std::array< uint8_t, N > const &value)
 
template<size_t N>
void std::write (std::ostream &os, std::array< uint8_t, N > const &value)
 
template<typename B , typename T , size_t N>
void std::read (B &it, std::array< T, N > &value)
 
template<typename B , typename T , size_t N>
void std::write (B &buf, std::array< T, N > const &value)
 
template<typename B , typename T , typename A >
void std::read (B &it, std::vector< T, A > &value)
 
template<typename B , typename T , typename A >
void std::write (B &buf, std::vector< T, A > const &value)
 
template<typename B >
void std::read (B &it, std::string &value)
 
template<typename B >
void std::write (B &buf, std::string const &value)
 
template<typename B , typename T , typename U >
void std::read (B &it, std::pair< T, U > &value)
 
template<typename B , typename T , typename U >
void std::write (B &buf, std::pair< T, U > const &value)
 
template<typename B , typename T >
void std::read (B &it, std::shared_ptr< T > &value_ptr)
 
template<typename B , typename T >
void std::write (B &buf, std::shared_ptr< T > const &value_ptr)
 
template<typename B , typename T , typename U >
void std::read (B &it, std::map< T, U > &value)
 
template<typename B , typename T , typename U >
void std::write (B &buf, std::map< T, U > const &value)
 
template<typename B , typename T >
void std::read (B &it, std::optional< T > &opt_value)
 
template<typename B , HasGetAll T>
void std::write (B &buf, T const &value)
 
template<typename B , typename T >
void std::write (B &buf, std::optional< T > const &opt_value)
 
template<typename T , typename B >
from_buffer (B const &buffer, size_t offset=0)
 
template<typename T >
std::vector< uint8_t > to_buffer (T const &value)
 
template<typename T >
uint8_t * to_heap_buffer (T const &value)
 
template<typename T >
std::vector< T > many_from_buffer (std::vector< uint8_t > const &buffer)
 
template<bool include_size = false, typename T >
std::vector< uint8_t > to_buffer (std::vector< T > const &value)
 
void serialize::_read_msgpack_field (auto &it, auto &field)
 Helper method for better error reporting. Clang does not give the best errors for "auto..." arguments.
 
void serialize::_write_msgpack_field (auto &it, const auto &field)
 Helper method for better error reporting. Clang does not give the best errors for "auto..." arguments.
 

Typedef Documentation

◆ in_buf128

using in_buf128 = uint8_t const*

Definition at line 488 of file serialize.hpp.

◆ in_buf32

using in_buf32 = uint8_t const*

Definition at line 484 of file serialize.hpp.

◆ in_buf64

using in_buf64 = uint8_t const*

Definition at line 486 of file serialize.hpp.

◆ in_ptr

using in_ptr = void* const*

Definition at line 496 of file serialize.hpp.

◆ in_str_buf

using in_str_buf = uint8_t const*

Definition at line 492 of file serialize.hpp.

◆ out_buf128

using out_buf128 = uint8_t*

Definition at line 489 of file serialize.hpp.

◆ out_buf32

using out_buf32 = uint8_t*

Definition at line 485 of file serialize.hpp.

◆ out_buf64

using out_buf64 = uint8_t*

Definition at line 487 of file serialize.hpp.

◆ out_ptr

using out_ptr = void**

Definition at line 497 of file serialize.hpp.

◆ out_str_buf

using out_str_buf = uint8_t**

Definition at line 493 of file serialize.hpp.

◆ uint128_t

using uint128_t = unsigned __int128

This is a non-msgpack flat buffer serialization library. It is currently used alongside msgpack, with hope to eventually move to msgpack. It enables the reading and writing of big-endian formatted integers and various standard library types to and from the following supported types:

  • uint8_t*
  • std::vector<uint8_t>
  • std::ostream / std::istream

To support custom types, free functions taking the following form should be defined alongside the custom type:

  • template <typename B> inline void read(B& it, my_custom_type& value)
  • template <typename B> inline void write(B& it, my_custom_type const& value) They should be implemented in terms of lower level read/write functions. Be aware that if B is a uint8_t*, it will be advanced appropriately during reads and writes.

For understanding, given integers do not belong in any namespace, they have been defined inside the serialize namespace. It may sometimes be necessary to specify a using serialize::read or using serialize::write to find them. This is prefereable to polluting the global namespace which comes with its own issues.

Standard library types are defined inside the std namespace, so they can be discovered using argument dependent lookup. Placing them inside the serialize namespace was an option, but would mean integers and std types would need to have the serialize namespace specified, but custom types would not. By leveraging ADL we can avoid needing to specify the serialize namespace in almost all cases.

A few helpers are defined at global namespace:

  • from_buffer<T>
  • many_from_buffer<T>
  • to_buffer

Definition at line 44 of file serialize.hpp.

Function Documentation

◆ from_buffer()

template<typename T , typename B >
T from_buffer ( B const &  buffer,
size_t  offset = 0 
)

Definition at line 416 of file serialize.hpp.

◆ many_from_buffer()

template<typename T >
std::vector< T > many_from_buffer ( std::vector< uint8_t > const &  buffer)

Definition at line 458 of file serialize.hpp.

◆ to_buffer() [1/2]

template<bool include_size = false, typename T >
std::vector< uint8_t > to_buffer ( std::vector< T > const &  value)

Definition at line 469 of file serialize.hpp.

◆ to_buffer() [2/2]

template<typename T >
std::vector< uint8_t > to_buffer ( T const &  value)

Definition at line 425 of file serialize.hpp.

◆ to_heap_buffer()

template<typename T >
uint8_t * to_heap_buffer ( T const &  value)

Serializes the given value, such that it is byte length prefixed, for deserialization on the calling side. This is used for variable length outputs, whereby the caller needs to discover the length so the memory can be appropriately sliced. It can result in the (expected) oddity of e.g. a vector<uint8_t> being "multiple prefixed": e.g. [heap buffer length][value length][value bytes].

Definition at line 440 of file serialize.hpp.