Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
bincode.hpp
Go to the documentation of this file.
1// Copyright (c) Facebook, Inc. and its affiliates
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4#pragma once
5
6#include <cstdint>
7#include <limits>
8
9#include "binary.hpp"
10#include "serde.hpp"
11
12// Maximum length supported in practice (e.g. Java).
13constexpr size_t BINCODE_MAX_LENGTH = (1ULL << 31) - 1;
14
15namespace serde {
16
17class BincodeSerializer : public BinarySerializer<BincodeSerializer> {
19
20 public:
22 : Parent(SIZE_MAX)
23 {}
24
25 void serialize_f32(float value);
26 void serialize_f64(double value);
27 void serialize_len(size_t value);
28 void serialize_variant_index(uint32_t value);
29
30 static constexpr bool enforce_strict_map_ordering = false;
31};
32
33class BincodeDeserializer : public BinaryDeserializer<BincodeDeserializer> {
35
36 public:
37 BincodeDeserializer(std::vector<uint8_t> bytes)
38 : Parent(std::move(bytes), SIZE_MAX)
39 {}
40
41 float deserialize_f32();
42 double deserialize_f64();
43 size_t deserialize_len();
45
46 static constexpr bool enforce_strict_map_ordering = false;
47};
48
49// Native floats and doubles must be IEEE-754 values of the expected size.
50static_assert(std::numeric_limits<float>::is_iec559);
51static_assert(std::numeric_limits<double>::is_iec559);
52static_assert(sizeof(float) == sizeof(uint32_t));
53static_assert(sizeof(double) == sizeof(uint64_t));
54
56{
57 Parent::serialize_u32(*reinterpret_cast<uint32_t*>(&value));
58}
59
61{
62 Parent::serialize_u64(*reinterpret_cast<uint64_t*>(&value));
63}
64
66{
68 throw_or_abort("Length is too large");
69 }
71}
72
77
79{
81 return *reinterpret_cast<float*>(&value);
82}
83
85{
87 return *reinterpret_cast<double*>(&value);
88}
89
91{
92 auto value = (size_t)Parent::deserialize_u64();
94 throw_or_abort("Length is too large");
95 }
96 return (size_t)value;
97}
98
103
104} // end of namespace serde
constexpr size_t BINCODE_MAX_LENGTH
Definition bincode.hpp:13
BincodeDeserializer(std::vector< uint8_t > bytes)
Definition bincode.hpp:37
static constexpr bool enforce_strict_map_ordering
Definition bincode.hpp:46
uint32_t deserialize_variant_index()
Definition bincode.hpp:99
void serialize_f32(float value)
Definition bincode.hpp:55
void serialize_variant_index(uint32_t value)
Definition bincode.hpp:73
void serialize_f64(double value)
Definition bincode.hpp:60
void serialize_len(size_t value)
Definition bincode.hpp:65
static constexpr bool enforce_strict_map_ordering
Definition bincode.hpp:30
STL namespace.
void throw_or_abort(std::string const &err)