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
10#include "bincode.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
33 template <typename T>
35 std::string const& struct_name,
36 std::string const& field_name,
37 T& field,
38 bool is_optional)
39 {
40 auto it = kvmap.find(field_name);
41 if (it != kvmap.end()) {
42 try {
43 it->second->convert(field);
44 } catch (const msgpack::type_error&) {
45 std::cerr << *it->second << std::endl;
46 throw_or_abort("error converting into field " + struct_name + "::" + field_name);
47 }
48 } else if (!is_optional) {
49 throw_or_abort("missing field: " + struct_name + "::" + field_name);
50 }
51 }
52
53 template <typename T>
54 static void conv_fld_from_array(msgpack::object_array const& array,
55 std::string const& struct_name,
56 std::string const& field_name,
57 T& field,
58 uint32_t index)
59 {
60 if (index >= array.size) {
61 throw_or_abort("index out of bounds: " + struct_name + "::" + field_name + " at " + std::to_string(index));
62 }
63 auto element = array.ptr[index];
64 try {
65 element.convert(field);
66 } catch (const msgpack::type_error&) {
67 std::cerr << element << std::endl;
68 throw_or_abort("error converting into field " + struct_name + "::" + field_name);
69 }
70 }
71};
72} // namespace Witnesses
73
74namespace Witnesses {
75
76struct Witness {
77 uint32_t value;
78
79 friend bool operator==(const Witness&, const Witness&);
80 std::vector<uint8_t> bincodeSerialize() const;
81 static Witness bincodeDeserialize(std::vector<uint8_t>);
82
83 bool operator<(Witness const& rhs) const { return value < rhs.value; }
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 'Witness'");
93 }
94 }
95};
96
97struct WitnessMap {
99
100 friend bool operator==(const WitnessMap&, const WitnessMap&);
101 std::vector<uint8_t> bincodeSerialize() const;
102 static WitnessMap bincodeDeserialize(std::vector<uint8_t>);
103
104 void msgpack_pack(auto& packer) const { packer.pack(value); }
105
106 void msgpack_unpack(msgpack::object const& o)
107 {
108 try {
109 o.convert(value);
110 } catch (const msgpack::type_error&) {
111 std::cerr << o << std::endl;
112 throw_or_abort("error converting into newtype 'WitnessMap'");
113 }
114 }
115};
116
117struct StackItem {
118 uint32_t index;
120
121 friend bool operator==(const StackItem&, const StackItem&);
122 std::vector<uint8_t> bincodeSerialize() const;
123 static StackItem bincodeDeserialize(std::vector<uint8_t>);
124
125 void msgpack_pack(auto& packer) const
126 {
127 packer.pack_map(2);
128 packer.pack(std::make_pair("index", index));
129 packer.pack(std::make_pair("witness", witness));
130 }
131
132 void msgpack_unpack(msgpack::object const& o)
133 {
134 std::string name = "StackItem";
135 if (o.type == msgpack::type::MAP) {
136 auto kvmap = Helpers::make_kvmap(o, name);
137 Helpers::conv_fld_from_kvmap(kvmap, name, "index", index, false);
138 Helpers::conv_fld_from_kvmap(kvmap, name, "witness", witness, false);
139 } else if (o.type == msgpack::type::ARRAY) {
140 auto array = o.via.array;
141 Helpers::conv_fld_from_array(array, name, "index", index, 0);
142 Helpers::conv_fld_from_array(array, name, "witness", witness, 1);
143 } else {
144 throw_or_abort("expected MAP or ARRAY for " + name);
145 }
146 }
147};
148
150 std::vector<Witnesses::StackItem> stack;
151
152 friend bool operator==(const WitnessStack&, const WitnessStack&);
153 std::vector<uint8_t> bincodeSerialize() const;
154 static WitnessStack bincodeDeserialize(std::vector<uint8_t>);
155
156 void msgpack_pack(auto& packer) const
157 {
158 packer.pack_map(1);
159 packer.pack(std::make_pair("stack", stack));
160 }
161
162 void msgpack_unpack(msgpack::object const& o)
163 {
164 std::string name = "WitnessStack";
165 if (o.type == msgpack::type::MAP) {
166 auto kvmap = Helpers::make_kvmap(o, name);
167 Helpers::conv_fld_from_kvmap(kvmap, name, "stack", stack, false);
168 } else if (o.type == msgpack::type::ARRAY) {
169 auto array = o.via.array;
170 Helpers::conv_fld_from_array(array, name, "stack", stack, 0);
171 } else {
172 throw_or_abort("expected MAP or ARRAY for " + name);
173 }
174 }
175};
176
177} // end of namespace Witnesses
178
179namespace Witnesses {
180
181inline bool operator==(const StackItem& lhs, const StackItem& rhs)
182{
183 if (!(lhs.index == rhs.index)) {
184 return false;
185 }
186 if (!(lhs.witness == rhs.witness)) {
187 return false;
188 }
189 return true;
190}
191
192inline std::vector<uint8_t> StackItem::bincodeSerialize() const
193{
194 auto serializer = serde::BincodeSerializer();
196 return std::move(serializer).bytes();
197}
198
199inline StackItem StackItem::bincodeDeserialize(std::vector<uint8_t> input)
200{
201 auto deserializer = serde::BincodeDeserializer(input);
203 if (deserializer.get_buffer_offset() < input.size()) {
204 throw_or_abort("Some input bytes were not read");
205 }
206 return value;
207}
208
209} // end of namespace Witnesses
210
211template <>
212template <typename Serializer>
214{
215 serializer.increase_container_depth();
216 serde::Serializable<decltype(obj.index)>::serialize(obj.index, serializer);
217 serde::Serializable<decltype(obj.witness)>::serialize(obj.witness, serializer);
218 serializer.decrease_container_depth();
219}
220
221template <>
222template <typename Deserializer>
224{
225 deserializer.increase_container_depth();
227 obj.index = serde::Deserializable<decltype(obj.index)>::deserialize(deserializer);
228 obj.witness = serde::Deserializable<decltype(obj.witness)>::deserialize(deserializer);
229 deserializer.decrease_container_depth();
230 return obj;
231}
232
233namespace Witnesses {
234
235inline bool operator==(const Witness& lhs, const Witness& rhs)
236{
237 if (!(lhs.value == rhs.value)) {
238 return false;
239 }
240 return true;
241}
242
243inline std::vector<uint8_t> Witness::bincodeSerialize() const
244{
245 auto serializer = serde::BincodeSerializer();
247 return std::move(serializer).bytes();
248}
249
250inline Witness Witness::bincodeDeserialize(std::vector<uint8_t> input)
251{
252 auto deserializer = serde::BincodeDeserializer(input);
254 if (deserializer.get_buffer_offset() < input.size()) {
255 throw_or_abort("Some input bytes were not read");
256 }
257 return value;
258}
259
260} // end of namespace Witnesses
261
262template <>
263template <typename Serializer>
265{
266 serializer.increase_container_depth();
267 serde::Serializable<decltype(obj.value)>::serialize(obj.value, serializer);
268 serializer.decrease_container_depth();
269}
270
271template <>
272template <typename Deserializer>
274{
275 deserializer.increase_container_depth();
277 obj.value = serde::Deserializable<decltype(obj.value)>::deserialize(deserializer);
278 deserializer.decrease_container_depth();
279 return obj;
280}
281
282namespace Witnesses {
283
284inline bool operator==(const WitnessMap& lhs, const WitnessMap& rhs)
285{
286 if (!(lhs.value == rhs.value)) {
287 return false;
288 }
289 return true;
290}
291
292inline std::vector<uint8_t> WitnessMap::bincodeSerialize() const
293{
294 auto serializer = serde::BincodeSerializer();
296 return std::move(serializer).bytes();
297}
298
299inline WitnessMap WitnessMap::bincodeDeserialize(std::vector<uint8_t> input)
300{
301 auto deserializer = serde::BincodeDeserializer(input);
303 if (deserializer.get_buffer_offset() < input.size()) {
304 throw_or_abort("Some input bytes were not read");
305 }
306 return value;
307}
308
309} // end of namespace Witnesses
310
311template <>
312template <typename Serializer>
314{
315 serializer.increase_container_depth();
316 serde::Serializable<decltype(obj.value)>::serialize(obj.value, serializer);
317 serializer.decrease_container_depth();
318}
319
320template <>
321template <typename Deserializer>
323{
324 deserializer.increase_container_depth();
326 obj.value = serde::Deserializable<decltype(obj.value)>::deserialize(deserializer);
327 deserializer.decrease_container_depth();
328 return obj;
329}
330
331namespace Witnesses {
332
333inline bool operator==(const WitnessStack& lhs, const WitnessStack& rhs)
334{
335 if (!(lhs.stack == rhs.stack)) {
336 return false;
337 }
338 return true;
339}
340
341inline std::vector<uint8_t> WitnessStack::bincodeSerialize() const
342{
343 auto serializer = serde::BincodeSerializer();
345 return std::move(serializer).bytes();
346}
347
348inline WitnessStack WitnessStack::bincodeDeserialize(std::vector<uint8_t> input)
349{
350 auto deserializer = serde::BincodeDeserializer(input);
352 if (deserializer.get_buffer_offset() < input.size()) {
353 throw_or_abort("Some input bytes were not read");
354 }
355 return value;
356}
357
358} // end of namespace Witnesses
359
360template <>
361template <typename Serializer>
363{
364 serializer.increase_container_depth();
365 serde::Serializable<decltype(obj.stack)>::serialize(obj.stack, serializer);
366 serializer.decrease_container_depth();
367}
368
369template <>
370template <typename Deserializer>
372{
373 deserializer.increase_container_depth();
375 obj.stack = serde::Deserializable<decltype(obj.stack)>::deserialize(deserializer);
376 deserializer.decrease_container_depth();
377 return obj;
378}
bool operator==(const StackItem &lhs, const StackItem &rhs)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::string to_string(bb::avm2::ValueTag tag)
static std::map< std::string, msgpack::object const * > make_kvmap(msgpack::object const &o, std::string const &name)
static void conv_fld_from_array(msgpack::object_array const &array, std::string const &struct_name, std::string const &field_name, T &field, uint32_t index)
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 >)
std::map< Witnesses::Witness, std::vector< uint8_t > > value
void msgpack_unpack(msgpack::object const &o)
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)