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