Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
witness_stack.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#pragma once
8
9#include "bincode.hpp"
10#include "msgpack.hpp"
11#include "serde.hpp"
12
13namespace Witnesses {
14struct Helpers {
15 static std::map<std::string, msgpack::object const*> make_kvmap(msgpack::object const& o, std::string const& name)
16 {
17 if (o.type != msgpack::type::MAP) {
18 std::cerr << o << std::endl;
19 throw_or_abort("expected MAP for " + name);
20 }
22 for (uint32_t i = 0; i < o.via.map.size; ++i) {
23 if (o.via.map.ptr[i].key.type != msgpack::type::STR) {
24 std::cerr << o << std::endl;
25 throw_or_abort("expected STR for keys of " + name);
26 }
27 kvmap.emplace(std::string(o.via.map.ptr[i].key.via.str.ptr, o.via.map.ptr[i].key.via.str.size),
28 &o.via.map.ptr[i].val);
29 }
30 return kvmap;
31 }
32 template <typename T>
34 std::string const& struct_name,
35 std::string const& field_name,
36 T& field,
37 bool is_optional)
38 {
39 auto it = kvmap.find(field_name);
40 if (it != kvmap.end()) {
41 try {
42 it->second->convert(field);
43 } catch (const msgpack::type_error&) {
44 std::cerr << *it->second << std::endl;
45 throw_or_abort("error converting into field " + struct_name + "::" + field_name);
46 }
47 } else if (!is_optional) {
48 throw_or_abort("missing field: " + struct_name + "::" + field_name);
49 }
50 }
51};
52} // namespace Witnesses
53
54namespace Witnesses {
55
56struct Witness {
57 uint32_t value;
58
59 friend bool operator==(const Witness&, const Witness&);
60 std::vector<uint8_t> bincodeSerialize() const;
61 static Witness bincodeDeserialize(std::vector<uint8_t>);
62
63 bool operator<(Witness const& rhs) const { return value < rhs.value; }
64 void msgpack_pack(auto& packer) const { packer.pack(value); }
65
66 void msgpack_unpack(msgpack::object const& o)
67 {
68 try {
69 o.convert(value);
70 } catch (const msgpack::type_error&) {
71 std::cerr << o << std::endl;
72 throw_or_abort("error converting into newtype 'Witness'");
73 }
74 }
75};
76
77struct WitnessMap {
78 std::map<Witnesses::Witness, std::string> value;
79
80 friend bool operator==(const WitnessMap&, const WitnessMap&);
81 std::vector<uint8_t> bincodeSerialize() const;
82 static WitnessMap bincodeDeserialize(std::vector<uint8_t>);
83
84 void msgpack_pack(auto& packer) const { packer.pack(value); }
85
86 void msgpack_unpack(msgpack::object const& o)
87 {
88 try {
89 o.convert(value);
90 } catch (const msgpack::type_error&) {
91 std::cerr << o << std::endl;
92 throw_or_abort("error converting into newtype 'WitnessMap'");
93 }
94 }
95};
96
97struct StackItem {
98 uint32_t index;
100
101 friend bool operator==(const StackItem&, const StackItem&);
102 std::vector<uint8_t> bincodeSerialize() const;
103 static StackItem bincodeDeserialize(std::vector<uint8_t>);
104
105 void msgpack_pack(auto& packer) const
106 {
107 packer.pack_map(2);
108 packer.pack(std::make_pair("index", index));
109 packer.pack(std::make_pair("witness", witness));
110 }
111
112 void msgpack_unpack(msgpack::object const& o)
113 {
114 auto name = "StackItem";
115 auto kvmap = Helpers::make_kvmap(o, name);
116 Helpers::conv_fld_from_kvmap(kvmap, name, "index", index, false);
117 Helpers::conv_fld_from_kvmap(kvmap, name, "witness", witness, false);
118 }
119};
120
122 std::vector<Witnesses::StackItem> stack;
123
124 friend bool operator==(const WitnessStack&, const WitnessStack&);
125 std::vector<uint8_t> bincodeSerialize() const;
126 static WitnessStack bincodeDeserialize(std::vector<uint8_t>);
127
128 void msgpack_pack(auto& packer) const
129 {
130 packer.pack_map(1);
131 packer.pack(std::make_pair("stack", stack));
132 }
133
134 void msgpack_unpack(msgpack::object const& o)
135 {
136 auto name = "WitnessStack";
137 auto kvmap = Helpers::make_kvmap(o, name);
138 Helpers::conv_fld_from_kvmap(kvmap, name, "stack", stack, false);
139 }
140};
141
142} // end of namespace Witnesses
143
144namespace Witnesses {
145
146inline bool operator==(const StackItem& lhs, const StackItem& rhs)
147{
148 if (!(lhs.index == rhs.index)) {
149 return false;
150 }
151 if (!(lhs.witness == rhs.witness)) {
152 return false;
153 }
154 return true;
155}
156
157inline std::vector<uint8_t> StackItem::bincodeSerialize() const
158{
159 auto serializer = serde::BincodeSerializer();
161 return std::move(serializer).bytes();
162}
163
164inline StackItem StackItem::bincodeDeserialize(std::vector<uint8_t> input)
165{
166 auto deserializer = serde::BincodeDeserializer(input);
168 if (deserializer.get_buffer_offset() < input.size()) {
169 throw_or_abort("Some input bytes were not read");
170 }
171 return value;
172}
173
174} // end of namespace Witnesses
175
176template <>
177template <typename Serializer>
179{
180 serializer.increase_container_depth();
181 serde::Serializable<decltype(obj.index)>::serialize(obj.index, serializer);
182 serde::Serializable<decltype(obj.witness)>::serialize(obj.witness, serializer);
183 serializer.decrease_container_depth();
184}
185
186template <>
187template <typename Deserializer>
189{
190 deserializer.increase_container_depth();
192 obj.index = serde::Deserializable<decltype(obj.index)>::deserialize(deserializer);
193 obj.witness = serde::Deserializable<decltype(obj.witness)>::deserialize(deserializer);
194 deserializer.decrease_container_depth();
195 return obj;
196}
197
198namespace Witnesses {
199
200inline bool operator==(const Witness& lhs, const Witness& rhs)
201{
202 if (!(lhs.value == rhs.value)) {
203 return false;
204 }
205 return true;
206}
207
208inline std::vector<uint8_t> Witness::bincodeSerialize() const
209{
210 auto serializer = serde::BincodeSerializer();
212 return std::move(serializer).bytes();
213}
214
215inline Witness Witness::bincodeDeserialize(std::vector<uint8_t> input)
216{
217 auto deserializer = serde::BincodeDeserializer(input);
219 if (deserializer.get_buffer_offset() < input.size()) {
220 throw_or_abort("Some input bytes were not read");
221 }
222 return value;
223}
224
225} // end of namespace Witnesses
226
227template <>
228template <typename Serializer>
230{
231 serializer.increase_container_depth();
232 serde::Serializable<decltype(obj.value)>::serialize(obj.value, serializer);
233 serializer.decrease_container_depth();
234}
235
236template <>
237template <typename Deserializer>
239{
240 deserializer.increase_container_depth();
242 obj.value = serde::Deserializable<decltype(obj.value)>::deserialize(deserializer);
243 deserializer.decrease_container_depth();
244 return obj;
245}
246
247namespace Witnesses {
248
249inline bool operator==(const WitnessMap& lhs, const WitnessMap& rhs)
250{
251 if (!(lhs.value == rhs.value)) {
252 return false;
253 }
254 return true;
255}
256
257inline std::vector<uint8_t> WitnessMap::bincodeSerialize() const
258{
259 auto serializer = serde::BincodeSerializer();
261 return std::move(serializer).bytes();
262}
263
264inline WitnessMap WitnessMap::bincodeDeserialize(std::vector<uint8_t> input)
265{
266 auto deserializer = serde::BincodeDeserializer(input);
268 if (deserializer.get_buffer_offset() < input.size()) {
269 throw_or_abort("Some input bytes were not read");
270 }
271 return value;
272}
273
274} // end of namespace Witnesses
275
276template <>
277template <typename Serializer>
279{
280 serializer.increase_container_depth();
281 serde::Serializable<decltype(obj.value)>::serialize(obj.value, serializer);
282 serializer.decrease_container_depth();
283}
284
285template <>
286template <typename Deserializer>
288{
289 deserializer.increase_container_depth();
291 obj.value = serde::Deserializable<decltype(obj.value)>::deserialize(deserializer);
292 deserializer.decrease_container_depth();
293 return obj;
294}
295
296namespace Witnesses {
297
298inline bool operator==(const WitnessStack& lhs, const WitnessStack& rhs)
299{
300 if (!(lhs.stack == rhs.stack)) {
301 return false;
302 }
303 return true;
304}
305
306inline std::vector<uint8_t> WitnessStack::bincodeSerialize() const
307{
308 auto serializer = serde::BincodeSerializer();
310 return std::move(serializer).bytes();
311}
312
313inline WitnessStack WitnessStack::bincodeDeserialize(std::vector<uint8_t> input)
314{
315 auto deserializer = serde::BincodeDeserializer(input);
317 if (deserializer.get_buffer_offset() < input.size()) {
318 throw_or_abort("Some input bytes were not read");
319 }
320 return value;
321}
322
323} // end of namespace Witnesses
324
325template <>
326template <typename Serializer>
328{
329 serializer.increase_container_depth();
330 serde::Serializable<decltype(obj.stack)>::serialize(obj.stack, serializer);
331 serializer.decrease_container_depth();
332}
333
334template <>
335template <typename Deserializer>
337{
338 deserializer.increase_container_depth();
340 obj.stack = serde::Deserializable<decltype(obj.stack)>::deserialize(deserializer);
341 deserializer.decrease_container_depth();
342 return obj;
343}
bool operator==(const StackItem &lhs, const StackItem &rhs)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
static std::map< std::string, msgpack::object const * > make_kvmap(msgpack::object const &o, std::string const &name)
static void conv_fld_from_kvmap(std::map< std::string, msgpack::object const * > const &kvmap, std::string const &struct_name, std::string const &field_name, T &field, bool is_optional)
void msgpack_pack(auto &packer) const
friend bool operator==(const StackItem &, const StackItem &)
Witnesses::WitnessMap witness
std::vector< uint8_t > bincodeSerialize() const
void msgpack_unpack(msgpack::object const &o)
static StackItem bincodeDeserialize(std::vector< uint8_t >)
std::vector< uint8_t > bincodeSerialize() const
void msgpack_pack(auto &packer) const
bool operator<(Witness const &rhs) const
friend bool operator==(const Witness &, const Witness &)
static Witness bincodeDeserialize(std::vector< uint8_t >)
void msgpack_unpack(msgpack::object const &o)
void msgpack_pack(auto &packer) const
friend bool operator==(const WitnessMap &, const WitnessMap &)
std::vector< uint8_t > bincodeSerialize() const
static WitnessMap bincodeDeserialize(std::vector< uint8_t >)
void msgpack_unpack(msgpack::object const &o)
std::map< Witnesses::Witness, std::string > value
static WitnessStack bincodeDeserialize(std::vector< uint8_t >)
void msgpack_pack(auto &packer) const
std::vector< Witnesses::StackItem > stack
friend bool operator==(const WitnessStack &, const WitnessStack &)
std::vector< uint8_t > bincodeSerialize() const
void msgpack_unpack(msgpack::object const &o)
static T deserialize(Deserializer &deserializer)
static void serialize(const T &value, Serializer &serializer)
void throw_or_abort(std::string const &err)