Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
uint_128_t_adaptor.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <msgpack.hpp>
4
8
9namespace msgpack::adaptor {
10
11// Pack function for uint128_t
12template <> struct pack<uint128_t> {
13 template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, uint128_t const& v) const
14 {
15 // Convert to string and pack as a string.
16 // TODO(fcarreiro): Consider the bin format for 16 bytes of data for speed.
17 std::string str = format(v);
18 o.pack_str(static_cast<uint32_t>(str.size()));
19 o.pack_str_body(str.c_str(), static_cast<uint32_t>(str.size()));
20 return o;
21 }
22};
23
24template <> struct convert<uint128_t> {
25 msgpack::object const& operator()(msgpack::object const& o, uint128_t& v) const
26 {
27 if (o.type == msgpack::type::POSITIVE_INTEGER) {
28 v = static_cast<uint128_t>(o.via.u64);
29 } else if (o.type == msgpack::type::STR) {
30 // When the bigint is too large to fit in a u64, msgpackr will serialize it as a digits string.
31 // Configured on the TS side with largeBigIntToString: true.
32 uint128_t result = 0;
33 // 2**128 is 39 digits long in base 10.
34 if (o.via.str.size > 39) {
35 throw_or_abort("uint128_t deserialization failed: string too long");
36 }
37
38 for (size_t i = 0; i < o.via.str.size; ++i) {
39 char c = o.via.str.ptr[i];
40 if (c < '0' || c > '9') {
41 throw_or_abort("uint128_t deserialization failed: Non-digit character in input");
42 }
43
44 result = result * 10 + (static_cast<uint128_t>(c - '0'));
45 }
46
47 v = result;
48 } else {
49 throw_or_abort("Invalid type for uint128_t deserialization");
50 }
51 return o;
52 }
53};
54
55} // namespace msgpack::adaptor
std::string format(Args... args)
Definition log.hpp:22
constexpr std::array< uint8_t, S > convert(const std::string_view &in)
unsigned __int128 uint128_t
Definition serialize.hpp:44
msgpack::object const & operator()(msgpack::object const &o, uint128_t &v) const
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, uint128_t const &v) const
void throw_or_abort(std::string const &err)