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// AUTO-GENERATED — DO NOT EDIT.
2//
3// Generated by the `cpp_codegen` tests in `acvm-repo/acir/src/lib.rs`.
4// To regenerate, run:
5//
6// NOIR_CODEGEN_OVERWRITE=1 cargo test -p acir cpp_codegen
7//
8#pragma once
9
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 if (!is_optional && it->second->type == msgpack::type::NIL) {
43 throw_or_abort("nil value for required field: " + struct_name + "::" + field_name);
44 }
45 try {
46 it->second->convert(field);
47 } catch (const msgpack::type_error&) {
48 std::cerr << *it->second << std::endl;
49 throw_or_abort("error converting into field " + struct_name + "::" + field_name);
50 }
51 } else if (!is_optional) {
52 throw_or_abort("missing field: " + struct_name + "::" + field_name);
53 }
54 }
55
56 template <typename T>
57 static void conv_fld_from_array(msgpack::object_array const& array,
58 std::string const& struct_name,
59 std::string const& field_name,
60 T& field,
61 uint32_t index)
62 {
63 if (index >= array.size) {
64 throw_or_abort("index out of bounds: " + struct_name + "::" + field_name + " at " + std::to_string(index));
65 }
66 auto element = array.ptr[index];
67 if (element.type == msgpack::type::NIL) {
68 throw_or_abort("nil value for required field: " + struct_name + "::" + field_name);
69 }
70 try {
71 element.convert(field);
72 } catch (const msgpack::type_error&) {
73 std::cerr << element << std::endl;
74 throw_or_abort("error converting into field " + struct_name + "::" + field_name);
75 }
76 }
77
81 template <typename T>
82 static void convert_or_throw(msgpack::object const& val,
83 std::string const& struct_name,
84 std::string const& field_name,
85 T& field)
86 {
87 try {
88 val.convert(field);
89 } catch (const msgpack::type_error&) {
90 std::cerr << val << std::endl;
91 throw_or_abort("error converting into field " + struct_name + "::" + field_name);
92 }
93 }
94
100 static bool is_int_keyed_map(msgpack::object const& o)
101 {
102 return o.type == msgpack::type::MAP && o.via.map.size > 0 &&
103 o.via.map.ptr[0].key.type == msgpack::type::POSITIVE_INTEGER;
104 }
105
112 template <typename Dispatch>
113 static void int_map_dispatch(msgpack::object const& o, std::string const& name, Dispatch&& dispatch)
114 {
115 for (uint32_t i = 0; i < o.via.map.size; ++i) {
116 uint8_t tag;
117 try {
118 o.via.map.ptr[i].key.convert(tag);
119 } catch (const msgpack::type_error&) {
120 std::cerr << o.via.map.ptr[i].key << std::endl;
121 throw_or_abort("expected u8 tag in int-keyed map for " + name);
122 }
123 dispatch(tag, o.via.map.ptr[i].val);
124 }
125 }
126
138 static void check_size(uint32_t actual, std::string const& name, uint32_t active, uint32_t reserved)
139 {
140 uint32_t max_size = active + reserved;
141 if (actual > max_size) {
142 throw_or_abort(name + " has " + std::to_string(actual) + " entries but at most " +
143 std::to_string(max_size) + " are expected (" + std::to_string(active) + " active + " +
144 std::to_string(reserved) +
145 " reserved); opt into `#[tagged(allow_unknown_tags)]` on the Rust type to accept extras");
146 }
147 }
148};
149} // namespace Witnesses
150
151namespace Witnesses {
152
153struct Witness {
154 uint32_t value;
155
156 friend bool operator==(const Witness&, const Witness&);
157
158 bool operator<(Witness const& rhs) const { return value < rhs.value; }
159 void msgpack_pack(auto& packer) const { packer.pack(value); }
160
161 void msgpack_unpack(msgpack::object const& o)
162 {
163 try {
164 o.convert(value);
165 } catch (const msgpack::type_error&) {
166 std::cerr << o << std::endl;
167 throw_or_abort("error converting into newtype 'Witness'");
168 }
169 }
170};
171
174
175 friend bool operator==(const WitnessMap&, const WitnessMap&);
176
177 void msgpack_pack(auto& packer) const { packer.pack(value); }
178
179 void msgpack_unpack(msgpack::object const& o)
180 {
181 try {
182 o.convert(value);
183 } catch (const msgpack::type_error&) {
184 std::cerr << o << std::endl;
185 throw_or_abort("error converting into newtype 'WitnessMap'");
186 }
187 }
188};
189
190struct StackItem {
191 uint32_t index;
193
194 friend bool operator==(const StackItem&, const StackItem&);
195
196 void msgpack_pack(auto& packer) const
197 {
198 packer.pack_array(2);
199 packer.pack(index);
200 packer.pack(witness);
201 }
202
203 void msgpack_unpack(msgpack::object const& o)
204 {
205 std::string name = "StackItem";
206 if (o.type == msgpack::type::MAP) {
208 Helpers::int_map_dispatch(o, name, [&](uint8_t tag, msgpack::object const& val) {
209 switch (tag) {
210 case 0:
211 Helpers::convert_or_throw(val, name, "index", index);
212 break;
213 case 1:
214 Helpers::convert_or_throw(val, name, "witness", witness);
215 break;
216 default:
217 std::cerr << val << std::endl;
218 throw_or_abort("unknown tag for StackItem: " + std::to_string(tag));
219 }
220 });
221 } else {
222 Helpers::check_size(o.via.map.size, name, 2, 0);
223 auto kvmap = Helpers::make_kvmap(o, name);
224 Helpers::conv_fld_from_kvmap(kvmap, name, "index", index, false);
225 Helpers::conv_fld_from_kvmap(kvmap, name, "witness", witness, false);
226 }
227 } else if (o.type == msgpack::type::ARRAY) {
228 auto array = o.via.array;
229 Helpers::check_size(array.size, name, 2, 0);
230 Helpers::conv_fld_from_array(array, name, "index", index, 0);
231 Helpers::conv_fld_from_array(array, name, "witness", witness, 1);
232 } else {
233 throw_or_abort("expected MAP or ARRAY for " + name);
234 }
235 }
236};
237
239 std::vector<Witnesses::StackItem> stack;
240
241 friend bool operator==(const WitnessStack&, const WitnessStack&);
242
243 void msgpack_pack(auto& packer) const
244 {
245 packer.pack_array(1);
246 packer.pack(stack);
247 }
248
249 void msgpack_unpack(msgpack::object const& o)
250 {
251 std::string name = "WitnessStack";
252 if (o.type == msgpack::type::MAP) {
254 Helpers::int_map_dispatch(o, name, [&](uint8_t tag, msgpack::object const& val) {
255 switch (tag) {
256 case 0:
257 Helpers::convert_or_throw(val, name, "stack", stack);
258 break;
259 default:
260 std::cerr << val << std::endl;
261 throw_or_abort("unknown tag for WitnessStack: " + std::to_string(tag));
262 }
263 });
264 } else {
265 Helpers::check_size(o.via.map.size, name, 1, 0);
266 auto kvmap = Helpers::make_kvmap(o, name);
267 Helpers::conv_fld_from_kvmap(kvmap, name, "stack", stack, false);
268 }
269 } else if (o.type == msgpack::type::ARRAY) {
270 auto array = o.via.array;
271 Helpers::check_size(array.size, name, 1, 0);
272 Helpers::conv_fld_from_array(array, name, "stack", stack, 0);
273 } else {
274 throw_or_abort("expected MAP or ARRAY for " + name);
275 }
276 }
277};
278
279} // end of namespace Witnesses
280
281namespace Witnesses {
282
283inline bool operator==(const StackItem& lhs, const StackItem& rhs)
284{
285 if (!(lhs.index == rhs.index)) {
286 return false;
287 }
288 if (!(lhs.witness == rhs.witness)) {
289 return false;
290 }
291 return true;
292}
293
294} // end of namespace Witnesses
295
296template <>
297template <typename Serializer>
299{
300 serializer.increase_container_depth();
301 serde::Serializable<decltype(obj.index)>::serialize(obj.index, serializer);
302 serde::Serializable<decltype(obj.witness)>::serialize(obj.witness, serializer);
303 serializer.decrease_container_depth();
304}
305
306template <>
307template <typename Deserializer>
309{
310 deserializer.increase_container_depth();
312 obj.index = serde::Deserializable<decltype(obj.index)>::deserialize(deserializer);
313 obj.witness = serde::Deserializable<decltype(obj.witness)>::deserialize(deserializer);
314 deserializer.decrease_container_depth();
315 return obj;
316}
317
318namespace Witnesses {
319
320inline bool operator==(const Witness& lhs, const Witness& rhs)
321{
322 if (!(lhs.value == rhs.value)) {
323 return false;
324 }
325 return true;
326}
327
328} // end of namespace Witnesses
329
330template <>
331template <typename Serializer>
333{
334 serializer.increase_container_depth();
335 serde::Serializable<decltype(obj.value)>::serialize(obj.value, serializer);
336 serializer.decrease_container_depth();
337}
338
339template <>
340template <typename Deserializer>
342{
343 deserializer.increase_container_depth();
345 obj.value = serde::Deserializable<decltype(obj.value)>::deserialize(deserializer);
346 deserializer.decrease_container_depth();
347 return obj;
348}
349
350namespace Witnesses {
351
352inline bool operator==(const WitnessMap& lhs, const WitnessMap& rhs)
353{
354 if (!(lhs.value == rhs.value)) {
355 return false;
356 }
357 return true;
358}
359
360} // end of namespace Witnesses
361
362template <>
363template <typename Serializer>
365{
366 serializer.increase_container_depth();
367 serde::Serializable<decltype(obj.value)>::serialize(obj.value, serializer);
368 serializer.decrease_container_depth();
369}
370
371template <>
372template <typename Deserializer>
374{
375 deserializer.increase_container_depth();
377 obj.value = serde::Deserializable<decltype(obj.value)>::deserialize(deserializer);
378 deserializer.decrease_container_depth();
379 return obj;
380}
381
382namespace Witnesses {
383
384inline bool operator==(const WitnessStack& lhs, const WitnessStack& rhs)
385{
386 if (!(lhs.stack == rhs.stack)) {
387 return false;
388 }
389 return true;
390}
391
392} // end of namespace Witnesses
393
394template <>
395template <typename Serializer>
397{
398 serializer.increase_container_depth();
399 serde::Serializable<decltype(obj.stack)>::serialize(obj.stack, serializer);
400 serializer.decrease_container_depth();
401}
402
403template <>
404template <typename Deserializer>
406{
407 deserializer.increase_container_depth();
409 obj.stack = serde::Deserializable<decltype(obj.stack)>::deserialize(deserializer);
410 deserializer.decrease_container_depth();
411 return obj;
412}
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 check_size(uint32_t actual, std::string const &name, uint32_t active, uint32_t reserved)
static bool is_int_keyed_map(msgpack::object const &o)
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 convert_or_throw(msgpack::object const &val, std::string const &struct_name, std::string const &field_name, T &field)
static void int_map_dispatch(msgpack::object const &o, std::string const &name, Dispatch &&dispatch)
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
void msgpack_unpack(msgpack::object const &o)
void msgpack_pack(auto &packer) const
bool operator<(Witness const &rhs) const
friend bool operator==(const Witness &, const Witness &)
void msgpack_unpack(msgpack::object const &o)
void msgpack_pack(auto &packer) const
friend bool operator==(const WitnessMap &, const WitnessMap &)
std::map< Witnesses::Witness, std::vector< uint8_t > > value
void msgpack_unpack(msgpack::object const &o)
void msgpack_pack(auto &packer) const
std::vector< Witnesses::StackItem > stack
friend bool operator==(const WitnessStack &, const WitnessStack &)
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)