Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
acir_to_constraint_buf.cpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Complete, auditors: [Federico], commit: 2094fd1467dd9a94803b2c5007cf60ac357aa7d2 }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
8
9#include <cstddef>
10#include <cstdint>
11#include <map>
12#include <tuple>
13#include <utility>
14
25
26namespace acir_format {
27
28using namespace bb;
29
31
32template <class... Ts> struct overloaded : Ts... {
33 using Ts::operator()...;
34};
35
36bb::fr from_buffer_with_bound_checks(const std::vector<uint8_t>& buffer)
37{
38 BB_ASSERT_EQ(buffer.size(), 32U, "acir_format::from_buffer_with_bound_checks: buffer size must be 32 bytes.");
39 return fr::serialize_from_buffer(buffer.data());
40}
41
43{
44 WitnessOrConstant<bb::fr> result = std::visit(overloaded{ [](const Acir::FunctionInput::Witness& e) {
46 .index = e.value.value,
47 .value = bb::fr::zero(),
48 .is_constant = false,
49 };
50 },
53 .index = bb::stdlib::IS_CONSTANT,
54 .value = from_buffer_with_bound_checks(e.value),
55 .is_constant = true,
56 };
57 } },
58 input.value);
59 return result;
60}
61
63{
65 "acir_format::get_witness_from_function_input: input must be a Witness variant. An error here means "
66 "there was a serialization error.");
67
68 return std::get<Acir::FunctionInput::Witness>(input.value).value.value;
69}
70
71void update_max_witness_index(const uint32_t witness_idx, AcirFormat& af)
72{
73 if (witness_idx != stdlib::IS_CONSTANT) {
74 af.max_witness_index = std::max(af.max_witness_index, witness_idx);
75 }
76}
77
79{
80 // Process multiplication terms: each term has two witness indices
81 for (const auto& mul_term : expr.mul_terms) {
84 }
85
86 // Process linear combinations: each term has one witness index
87 for (const auto& linear_term : expr.linear_combinations) {
89 }
90}
91
93{
94 auto update_max_witness_index_from_function_input = [&](const Acir::FunctionInput& input) {
97 }
98 };
99
100 auto update_max_witness_index_from_witness = [&](const Acir::Witness& witness) {
101 update_max_witness_index(witness.value, af);
102 };
103
104 std::visit(
106 [&](const Acir::Opcode::AssertZero& arg) { update_max_witness_index_from_expression(arg.value, af); },
107 [&](const Acir::Opcode::BlackBoxFuncCall& arg) {
108 std::visit(overloaded{ [&](const Acir::BlackBoxFuncCall::AND& bb_arg) {
109 update_max_witness_index_from_function_input(bb_arg.lhs);
110 update_max_witness_index_from_function_input(bb_arg.rhs);
111 update_max_witness_index_from_witness(bb_arg.output);
112 },
113 [&](const Acir::BlackBoxFuncCall::XOR& bb_arg) {
114 update_max_witness_index_from_function_input(bb_arg.lhs);
115 update_max_witness_index_from_function_input(bb_arg.rhs);
116 update_max_witness_index_from_witness(bb_arg.output);
117 },
118 [&](const Acir::BlackBoxFuncCall::RANGE& bb_arg) {
119 update_max_witness_index_from_function_input(bb_arg.input);
120 },
121 [&](const Acir::BlackBoxFuncCall::AES128Encrypt& bb_arg) {
122 for (const auto& input : bb_arg.inputs) {
123 update_max_witness_index_from_function_input(input);
124 }
125 for (const auto& input : *bb_arg.iv) {
126 update_max_witness_index_from_function_input(input);
127 }
128 for (const auto& input : *bb_arg.key) {
129 update_max_witness_index_from_function_input(input);
130 }
131 for (const auto& output : bb_arg.outputs) {
132 update_max_witness_index_from_witness(output);
133 }
134 },
136 for (const auto& input : *bb_arg.inputs) {
137 update_max_witness_index_from_function_input(input);
138 }
139 for (const auto& input : *bb_arg.hash_values) {
140 update_max_witness_index_from_function_input(input);
141 }
142 for (const auto& output : *bb_arg.outputs) {
143 update_max_witness_index_from_witness(output);
144 }
145 },
146 [&](const Acir::BlackBoxFuncCall::Blake2s& bb_arg) {
147 for (const auto& input : bb_arg.inputs) {
148 update_max_witness_index_from_function_input(input);
149 }
150 for (const auto& output : *bb_arg.outputs) {
151 update_max_witness_index_from_witness(output);
152 }
153 },
154 [&](const Acir::BlackBoxFuncCall::Blake3& bb_arg) {
155 for (const auto& input : bb_arg.inputs) {
156 update_max_witness_index_from_function_input(input);
157 }
158 for (const auto& output : *bb_arg.outputs) {
159 update_max_witness_index_from_witness(output);
160 }
161 },
162 [&](const Acir::BlackBoxFuncCall::EcdsaSecp256k1& bb_arg) {
163 for (const auto& input : *bb_arg.public_key_x) {
164 update_max_witness_index_from_function_input(input);
165 }
166 for (const auto& input : *bb_arg.public_key_y) {
167 update_max_witness_index_from_function_input(input);
168 }
169 for (const auto& input : *bb_arg.signature) {
170 update_max_witness_index_from_function_input(input);
171 }
172 for (const auto& input : *bb_arg.hashed_message) {
173 update_max_witness_index_from_function_input(input);
174 }
175 update_max_witness_index_from_function_input(bb_arg.predicate);
176 update_max_witness_index_from_witness(bb_arg.output);
177 },
178 [&](const Acir::BlackBoxFuncCall::EcdsaSecp256r1& bb_arg) {
179 for (const auto& input : *bb_arg.public_key_x) {
180 update_max_witness_index_from_function_input(input);
181 }
182 for (const auto& input : *bb_arg.public_key_y) {
183 update_max_witness_index_from_function_input(input);
184 }
185 for (const auto& input : *bb_arg.signature) {
186 update_max_witness_index_from_function_input(input);
187 }
188 for (const auto& input : *bb_arg.hashed_message) {
189 update_max_witness_index_from_function_input(input);
190 }
191 update_max_witness_index_from_function_input(bb_arg.predicate);
192 update_max_witness_index_from_witness(bb_arg.output);
193 },
194 [&](const Acir::BlackBoxFuncCall::MultiScalarMul& bb_arg) {
195 for (const auto& input : bb_arg.points) {
196 update_max_witness_index_from_function_input(input);
197 }
198 for (const auto& input : bb_arg.scalars) {
199 update_max_witness_index_from_function_input(input);
200 }
201 update_max_witness_index_from_function_input(bb_arg.predicate);
202 for (const auto& output : *bb_arg.outputs) {
203 update_max_witness_index_from_witness(output);
204 }
205 },
207 for (const auto& input : *bb_arg.input1) {
208 update_max_witness_index_from_function_input(input);
209 }
210 for (const auto& input : *bb_arg.input2) {
211 update_max_witness_index_from_function_input(input);
212 }
213 update_max_witness_index_from_function_input(bb_arg.predicate);
214 for (const auto& output : *bb_arg.outputs) {
215 update_max_witness_index_from_witness(output);
216 }
217 },
218 [&](const Acir::BlackBoxFuncCall::Keccakf1600& bb_arg) {
219 for (const auto& input : *bb_arg.inputs) {
220 update_max_witness_index_from_function_input(input);
221 }
222 for (const auto& output : *bb_arg.outputs) {
223 update_max_witness_index_from_witness(output);
224 }
225 },
227 for (const auto& input : bb_arg.verification_key) {
228 update_max_witness_index_from_function_input(input);
229 }
230 for (const auto& input : bb_arg.proof) {
231 update_max_witness_index_from_function_input(input);
232 }
233 for (const auto& input : bb_arg.public_inputs) {
234 update_max_witness_index_from_function_input(input);
235 }
236 update_max_witness_index_from_function_input(bb_arg.key_hash);
237 update_max_witness_index_from_function_input(bb_arg.predicate);
238 },
240 for (const auto& input : bb_arg.inputs) {
241 update_max_witness_index_from_function_input(input);
242 }
243 for (const auto& output : bb_arg.outputs) {
244 update_max_witness_index_from_witness(output);
245 }
246 } },
247 arg.value.value);
248 },
249 [&](const Acir::Opcode::MemoryInit& arg) {
250 for (const auto& init : arg.init) {
251 update_max_witness_index_from_witness(init);
252 }
253 },
254 [&](const Acir::Opcode::MemoryOp& arg) {
257 update_max_witness_index_from_expression(arg.op.operation, af);
258 },
259 [&](const Acir::Opcode::BrilligCall& arg) {
260 for (const auto& input : arg.inputs) {
261 std::visit(overloaded{
262 [&](const Acir::BrilligInputs::Single& e) {
264 },
265 [&](const Acir::BrilligInputs::Array& e) {
266 for (const auto& expr : e.value) {
268 }
269 },
271 // MemoryArray does not contain witnesses directly, so nothing to do here.
272 },
273 },
274 input.value);
275 }
276 for (const auto& output : arg.outputs) {
277 std::visit(overloaded{
278 [&](const Acir::BrilligOutputs::Simple& e) {
279 update_max_witness_index_from_witness(e.value);
280 },
281 [&](const Acir::BrilligOutputs::Array& e) {
282 for (const auto& witness : e.value) {
283 update_max_witness_index_from_witness(witness);
284 }
285 },
286 },
287 output.value);
288 }
290 },
291 [&](const Acir::Opcode::Call&) {
292 bb::assert_failure("acir_format::update_max_witness_index_from_opcode: Call opcode is not supported.");
293 },
294 },
295 opcode.value);
296}
297
299
300template <typename T>
301T deserialize_msgpack_compact(std::vector<uint8_t>&& buf, std::function<T(msgpack::object const&)> decode_msgpack)
302{
303 BB_ASSERT(!buf.empty(), "deserialize_msgpack_compact: buffer is empty");
304
305 // Expect format marker for msgpack or msgpack-compact
306 const uint8_t FORMAT_MSGPACK = 2;
307 const uint8_t FORMAT_MSGPACK_COMPACT = 3;
308 uint8_t format_u8 = buf[0];
309 BB_ASSERT(format_u8 == FORMAT_MSGPACK || format_u8 == FORMAT_MSGPACK_COMPACT,
310 "deserialize_msgpack_compact: expected msgpack format marker (2 or 3), got " + std::to_string(format_u8));
311
312 // Skip the format marker to get the data.
313 const char* buffer = &reinterpret_cast<const char*>(buf.data())[1];
314 size_t size = buf.size() - 1;
315
316 auto oh = msgpack::unpack(buffer, size);
317 auto o = oh.get();
318
319 // Expect ARRAY type for msgpack-compact format
320 BB_ASSERT(o.type == msgpack::type::ARRAY,
321 "deserialize_msgpack_compact: expected ARRAY type, got " + std::to_string(o.type));
322
323 return decode_msgpack(o);
324}
325
327{
329 circuit.opcodes.size(), UINT32_MAX, "acir_format::circuit_serde_to_acir_format: too many opcodes in circuit.");
330
331 AcirFormat af;
332 af.num_acir_opcodes = static_cast<uint32_t>(circuit.opcodes.size());
333 af.public_inputs = join({
335 [&](const Acir::Witness& e) {
336 update_max_witness_index(e.value, af);
337 return e.value;
338 }),
340 [&](const Acir::Witness& e) {
341 update_max_witness_index(e.value, af);
342 return e.value;
343 }),
344 });
345 // Map to a pair of: BlockConstraint, and list of opcodes associated with that BlockConstraint
346 // Block constraints are built as we process the opcodes, so we store them in this map and we add them to the
347 // AcirFormat struct at the end
348 // NOTE: We want to deterministically visit this map, so unordered_map should not be used.
350
351 for (size_t i = 0; i < circuit.opcodes.size(); ++i) {
352 const auto& gate = circuit.opcodes[i];
354 std::visit(
356 [&](const Acir::Opcode::AssertZero& arg) { assert_zero_to_quad_constraints(arg, af, i); },
358 [&](const Acir::Opcode::MemoryInit& arg) {
359 auto block = memory_init_to_block_constraint(arg);
360 uint32_t block_id = arg.block_id.value;
361 block_id_to_block_constraint[block_id] = { block, /*opcode_indices=*/{ i } };
362 },
363 [&](const Acir::Opcode::MemoryOp& arg) {
364 auto block = block_id_to_block_constraint.find(arg.block_id.value);
365 if (block == block_id_to_block_constraint.end()) {
366 bb::assert_failure("acir_format::circuit_serde_to_acir_format: unitialized MemoryOp.");
367 }
368 add_memory_op_to_block_constraint(arg, block->second.first);
369 block->second.second.push_back(i);
370 },
371 [&](const Acir::Opcode::BrilligCall&) {},
372 [&](const Acir::Opcode::Call&) {
373 bb::assert_failure("acir_format::circuit_serde_to_acir_format: Call opcode is not supported.");
374 },
375 },
376 gate.value);
377 }
378 // Add the block constraints to the AcirFormat struct
379 for (const auto& [_, block] : block_id_to_block_constraint) {
380 af.block_constraints.push_back(block.first);
381 af.original_opcode_indices.block_constraints.push_back(block.second);
382 }
383
384 return af;
385}
386
388{
389 // We need to deserialize into Acir::Program first because the buffer returned by Noir has this structure
390 auto program = deserialize_msgpack_compact<Acir::ProgramWithoutBrillig>(
392 Acir::ProgramWithoutBrillig program_wob;
393 try {
394 // Deserialize into a partial structure that ignores the Brillig parts,
395 // so that new opcodes can be added without breaking Barretenberg.
396 o.convert(program_wob);
397 } catch (const msgpack::type_error&) {
398 std::cerr << o << std::endl;
400 "acir_format::circuit_buf_to_acir_format: failed to convert msgpack data to Program");
401 }
402 return program_wob;
403 });
404 BB_ASSERT_EQ(program.functions.size(), 1U, "circuit_buf_to_acir_format: expected single function in ACIR program");
405
406 return circuit_serde_to_acir_format(program.functions[0]);
407}
408
410{
411 // We need to deserialize into WitnessStack first because the buffer returned by Noir has this structure
412 auto witness_stack = deserialize_msgpack_compact<Witnesses::WitnessStack>(std::move(buf), [](auto o) {
413 Witnesses::WitnessStack witness_stack;
414 try {
415 o.convert(witness_stack);
416 } catch (const msgpack::type_error&) {
417 std::cerr << o << std::endl;
419 "acir_format::witness_buf_to_witness_vector: failed to convert msgpack data to WitnessStack");
420 }
421 return witness_stack;
422 });
423 BB_ASSERT_EQ(witness_stack.stack.size(),
424 1U,
425 "acir_format::witness_buf_to_witness_vector: expected single WitnessMap in WitnessStack");
426
427 return witness_map_to_witness_vector(witness_stack.stack[0].witness);
428}
429
431{
432 // Note that the WitnessMap is in increasing order of witness indices because the comparator for the Acir::Witness
433 // is defined in terms of the witness index.
434
435 WitnessVector witness_vector;
436 for (size_t index = 0; const auto& e : witness_map.value) {
437 // ACIR uses a sparse format for WitnessMap where unused witness indices may be left unassigned.
438 // To ensure that witnesses sit at the correct indices in the `WitnessVector`, we fill any indices
439 // which do not exist within the `WitnessMap` with the random values. We use random values instead of zero
440 // because unassigned witnesses indices are not supposed to be used in any constraint, so filling them with a
441 // random value helps catching bugs.
442 while (index < e.first.value) {
443 witness_vector.emplace_back(fr::random_element());
444 index++;
445 }
446 witness_vector.emplace_back(from_buffer_with_bound_checks(e.second));
447 index++;
448 }
449
450 return witness_vector;
451}
452
454
456 std::map<uint32_t, bb::fr>& linear_terms)
457{
458 // Lambda to add next linear term from linear_terms to the mul_quad_ gate and erase it from linear_terms
459 auto add_linear_term_and_erase = [](uint32_t& idx, fr& scaling, std::map<uint32_t, fr>& linear_terms) {
461 idx, bb::stdlib::IS_CONSTANT, "Attempting to override a non-constant witness index in mul_quad_ gate");
462 idx = linear_terms.begin()->first;
463 scaling += linear_terms.begin()->second;
464 linear_terms.erase(idx);
465 };
466
468 // We cannot precompute the exact number of gates that will result from the expression. Therefore, we reserve the
469 // maximum number of gates that could ever be needed: one per multiplication term plus one per linear term. The real
470 // number of gates will in general be lower than this.
471 BB_ASSERT_LTE(arg.mul_terms.size(),
472 SIZE_MAX - linear_terms.size(),
473 "split_into_mul_quad_gates: overflow when reserving space for mul_quad_ gates.");
474 result.reserve(arg.mul_terms.size() + linear_terms.size());
475
476 // Step 1. Add multiplication terms and linear terms with the same witness index
477 for (const auto& mul_term : arg.mul_terms) {
478 result.emplace_back(mul_quad_<fr>{
479 .a = std::get<1>(mul_term).value,
480 .b = std::get<2>(mul_term).value,
481 .c = bb::stdlib::IS_CONSTANT,
482 .d = bb::stdlib::IS_CONSTANT,
483 .mul_scaling = from_buffer_with_bound_checks(std::get<0>(mul_term)),
484 .a_scaling = fr::zero(),
485 .b_scaling = fr::zero(),
486 .c_scaling = fr::zero(),
487 .d_scaling = fr::zero(),
488 .const_scaling = fr::zero(),
489 });
490
491 // Add linear terms corresponding to the witnesses involved in the multiplication term
492 auto& mul_quad = result.back();
493 if (linear_terms.contains(mul_quad.a)) {
494 mul_quad.a_scaling += linear_terms.at(mul_quad.a);
495 linear_terms.erase(mul_quad.a); // Remove it as the linear term for a has been processed
496 }
497 if (linear_terms.contains(mul_quad.b)) {
498 // Note that we enter here only if b is different from a
499 mul_quad.b_scaling += linear_terms.at(mul_quad.b);
500 linear_terms.erase(mul_quad.b); // Remove it as the linear term for b has been processed
501 }
502 }
503
504 // Step 2. Add linear terms to existing gates
505 bool is_first_gate = true;
506 for (auto& mul_quad : result) {
507 if (!linear_terms.empty()) {
508 add_linear_term_and_erase(mul_quad.c, mul_quad.c_scaling, linear_terms);
509 }
510
511 if (is_first_gate) {
512 // First gate contains the constant term and uses all four wires
513 mul_quad.const_scaling = from_buffer_with_bound_checks(arg.q_c);
514 if (!linear_terms.empty()) {
515 add_linear_term_and_erase(mul_quad.d, mul_quad.d_scaling, linear_terms);
516 }
517 is_first_gate = false;
518 }
519 }
520
521 // Step 3. Add remaining linear terms
522 while (!linear_terms.empty()) {
523 // We need to create new mul_quad_ gates to accomodate the remaining linear terms
524 mul_quad_<fr> mul_quad = {
525 .a = bb::stdlib::IS_CONSTANT,
526 .b = bb::stdlib::IS_CONSTANT,
527 .c = bb::stdlib::IS_CONSTANT,
528 .d = bb::stdlib::IS_CONSTANT,
529 .mul_scaling = fr::zero(),
530 .a_scaling = fr::zero(),
531 .b_scaling = fr::zero(),
532 .c_scaling = fr::zero(),
533 .d_scaling = fr::zero(),
534 .const_scaling = fr::zero(),
535 };
536 if (!linear_terms.empty()) {
537 add_linear_term_and_erase(mul_quad.a, mul_quad.a_scaling, linear_terms);
538 }
539 if (!linear_terms.empty()) {
540 add_linear_term_and_erase(mul_quad.b, mul_quad.b_scaling, linear_terms);
541 }
542 if (!linear_terms.empty()) {
543 add_linear_term_and_erase(mul_quad.c, mul_quad.c_scaling, linear_terms);
544 }
545 if (is_first_gate) {
546 // First gate contains the constant term and uses all four wires
548 if (!linear_terms.empty()) {
549 add_linear_term_and_erase(mul_quad.d, mul_quad.d_scaling, linear_terms);
550 }
551 is_first_gate = false;
552 }
553
554 result.emplace_back(mul_quad);
555 }
556
557 BB_ASSERT(!result.empty(),
558 "split_into_mul_quad_gates: resulted in zero gates. This means that there is an expression with no "
559 "multiplication terms and no linear terms.");
560 result.shrink_to_fit();
561
562 return result;
563}
564
566{
567 // Lambda to detect zero gates
568 auto is_zero_gate = [](const mul_quad_<fr>& gate) {
569 return ((gate.mul_scaling == fr(0)) && (gate.a_scaling == fr(0)) && (gate.b_scaling == fr(0)) &&
570 (gate.c_scaling == fr(0)) && (gate.d_scaling == fr(0)) && (gate.const_scaling == fr(0)));
571 };
572
573 auto linear_terms = process_linear_terms(arg.value);
574 bool is_single_gate = is_single_arithmetic_gate(arg.value, linear_terms);
575 std::vector<mul_quad_<fr>> mul_quads = split_into_mul_quad_gates(arg.value, linear_terms);
576
577 if (is_single_gate) {
578 BB_ASSERT_EQ(mul_quads.size(), 1U, "acir_format::assert_zero_to_quad_constraints: expected a single gate.");
579 auto mul_quad = mul_quads[0];
580
581 af.quad_constraints.push_back(mul_quad);
582 af.original_opcode_indices.quad_constraints.push_back(opcode_index);
583 } else {
584 BB_ASSERT_GT(mul_quads.size(),
585 1U,
586 "acir_format::assert_zero_to_quad_constraints: expected multiple gates but found one.");
587 af.big_quad_constraints.push_back(BigQuadConstraint(mul_quads));
588 af.original_opcode_indices.big_quad_constraints.push_back(opcode_index);
589 }
590
591 for (auto const& mul_quad : mul_quads) {
592 BB_ASSERT(!is_zero_gate(mul_quad),
593 "acir_format::assert_zero_to_quad_constraints: produced an arithmetic zero gate.");
594 }
595}
596
598 AcirFormat& af,
599 size_t opcode_index)
600{
601 auto to_witness_or_constant = [](const Acir::FunctionInput& e) { return parse_input(e); };
602 auto to_witness = [](const Acir::Witness& e) { return e.value; };
603 auto to_witness_from_input = [](const Acir::FunctionInput& e) { return get_witness_from_function_input(e); };
604
605 std::visit(
606 overloaded{ [&](const Acir::BlackBoxFuncCall::AND& arg) {
608 .a = parse_input(arg.lhs),
609 .b = parse_input(arg.rhs),
610 .result = to_witness(arg.output),
611 .num_bits = arg.num_bits,
612 .is_xor_gate = false,
613 });
614 af.original_opcode_indices.logic_constraints.push_back(opcode_index);
615 },
616 [&](const Acir::BlackBoxFuncCall::XOR& arg) {
618 .a = parse_input(arg.lhs),
619 .b = parse_input(arg.rhs),
620 .result = to_witness(arg.output),
621 .num_bits = arg.num_bits,
622 .is_xor_gate = true,
623 });
624 af.original_opcode_indices.logic_constraints.push_back(opcode_index);
625 },
626 [&](const Acir::BlackBoxFuncCall::RANGE& arg) {
629 .num_bits = arg.num_bits,
630 });
631 af.original_opcode_indices.range_constraints.push_back(opcode_index);
632 },
635 .inputs = transform::map(arg.inputs, to_witness_or_constant),
636 .iv = transform::map(*arg.iv, to_witness_or_constant),
637 .key = transform::map(*arg.key, to_witness_or_constant),
638 .outputs = transform::map(arg.outputs, to_witness),
639 });
640 af.original_opcode_indices.aes128_constraints.push_back(opcode_index);
641 },
644 .inputs = transform::map(*arg.inputs, to_witness_or_constant),
645 .hash_values = transform::map(*arg.hash_values, to_witness_or_constant),
646 .result = transform::map(*arg.outputs, to_witness),
647 });
648 af.original_opcode_indices.sha256_compression.push_back(opcode_index);
649 },
650 [&](const Acir::BlackBoxFuncCall::Blake2s& arg) {
652 .inputs = transform::map(arg.inputs, to_witness_or_constant),
653 .result = transform::map(*arg.outputs, to_witness),
654 });
655 af.original_opcode_indices.blake2s_constraints.push_back(opcode_index);
656 },
657 [&](const Acir::BlackBoxFuncCall::Blake3& arg) {
659 .inputs = transform::map(arg.inputs, to_witness_or_constant),
660 .result = transform::map(*arg.outputs, to_witness),
661 });
662 af.original_opcode_indices.blake3_constraints.push_back(opcode_index);
663 },
667 .hashed_message = transform::map(*arg.hashed_message, to_witness_from_input),
668 .signature = transform::map(*arg.signature, to_witness_from_input),
669 .pub_x_indices = transform::map(*arg.public_key_x, to_witness_from_input),
670 .pub_y_indices = transform::map(*arg.public_key_y, to_witness_from_input),
671 .predicate = parse_input(arg.predicate),
672 .result = to_witness(arg.output),
673 });
674 af.original_opcode_indices.ecdsa_k1_constraints.push_back(opcode_index);
675 },
679 .hashed_message = transform::map(*arg.hashed_message, to_witness_from_input),
680 .signature = transform::map(*arg.signature, to_witness_from_input),
681 .pub_x_indices = transform::map(*arg.public_key_x, to_witness_from_input),
682 .pub_y_indices = transform::map(*arg.public_key_y, to_witness_from_input),
683 .predicate = parse_input(arg.predicate),
684 .result = to_witness(arg.output),
685 });
686 af.original_opcode_indices.ecdsa_r1_constraints.push_back(opcode_index);
687 },
690 .points = transform::map(arg.points, to_witness_or_constant),
691 .scalars = transform::map(arg.scalars, to_witness_or_constant),
692 .predicate = parse_input(arg.predicate),
693 .out_point_x = to_witness((*arg.outputs)[0]),
694 .out_point_y = to_witness((*arg.outputs)[1]),
695 .out_point_is_infinite = to_witness((*arg.outputs)[2]),
696 });
697 af.original_opcode_indices.multi_scalar_mul_constraints.push_back(opcode_index);
698 },
700 af.ec_add_constraints.push_back(EcAdd{
701 .input1_x = parse_input((*arg.input1)[0]),
702 .input1_y = parse_input((*arg.input1)[1]),
703 .input1_infinite = parse_input((*arg.input1)[2]),
704 .input2_x = parse_input((*arg.input2)[0]),
705 .input2_y = parse_input((*arg.input2)[1]),
706 .input2_infinite = parse_input((*arg.input2)[2]),
707 .predicate = parse_input(arg.predicate),
708 .result_x = to_witness((*arg.outputs)[0]),
709 .result_y = to_witness((*arg.outputs)[1]),
710 .result_infinite = to_witness((*arg.outputs)[2]),
711 });
712 af.original_opcode_indices.ec_add_constraints.push_back(opcode_index);
713 },
715 af.keccak_permutations.push_back(Keccakf1600{
716 .state = transform::map(*arg.inputs, to_witness_or_constant),
717 .result = transform::map(*arg.outputs, to_witness),
718 });
719 af.original_opcode_indices.keccak_permutations.push_back(opcode_index);
720 },
722 auto predicate = parse_input(arg.predicate);
723 if (predicate.is_constant && predicate.value.is_zero()) {
724 // No constraint if the recursion is disabled
725 return;
726 }
727 auto c = RecursionConstraint{
728 .key = transform::map(arg.verification_key, to_witness_from_input),
729 .proof = transform::map(arg.proof, to_witness_from_input),
730 .public_inputs = transform::map(arg.public_inputs, to_witness_from_input),
731 .key_hash = get_witness_from_function_input(arg.key_hash),
732 .proof_type = arg.proof_type,
733 .predicate = predicate,
734 };
735
736 // Add the recursion constraint to the appropriate container based on proof type
737 switch (c.proof_type) {
738 case HONK_ZK:
739 case HONK:
740 case ROLLUP_HONK:
741 case ROOT_ROLLUP_HONK:
742 af.honk_recursion_constraints.push_back(c);
743 af.original_opcode_indices.honk_recursion_constraints.push_back(opcode_index);
744 break;
745 case OINK:
746 case HN:
747 case HN_TAIL:
748 case HN_FINAL:
749 af.hn_recursion_constraints.push_back(c);
750 af.original_opcode_indices.hn_recursion_constraints.push_back(opcode_index);
751 break;
752 case AVM:
753 af.avm_recursion_constraints.push_back(c);
754 af.original_opcode_indices.avm_recursion_constraints.push_back(opcode_index);
755 break;
756 case CHONK:
757 af.chonk_recursion_constraints.push_back(c);
758 af.original_opcode_indices.chonk_recursion_constraints.push_back(opcode_index);
759 break;
760 default:
762 "acir_format::handle_black_box_fun_call: Invalid PROOF_TYPE in RecursionConstraint.");
763 }
764 },
767 .state = transform::map(arg.inputs, to_witness_or_constant),
768 .result = transform::map(arg.outputs, to_witness),
769 });
770 af.original_opcode_indices.poseidon2_constraints.push_back(opcode_index);
771 } },
772 arg.value.value);
773}
774
776{
777 // Noir doesn't distinguish between ROM and RAM table. Therefore, we initialize every table as a ROM table, and
778 // then we make it a RAM table if there is at least one write operation
779 BlockConstraint block{
780 .init = {},
781 .trace = {},
782 .type = BlockType::ROM,
783 .calldata_id = CallDataType::None,
784 };
785
786 for (const auto& init : mem_init.init) {
787 block.init.push_back(init.value);
788 }
789
790 // Databus is only supported for Goblin, non Goblin builders will treat call_data and return_data as normal
791 // array.
793 uint32_t calldata_id = std::get<Acir::BlockType::CallData>(mem_init.block_type.value).value;
794 BB_ASSERT(calldata_id == 0 || calldata_id == 1, "acir_format::handle_memory_init: Unsupported calldata id");
795
796 block.type = BlockType::CallData;
797 block.calldata_id = calldata_id == 0 ? CallDataType::Primary : CallDataType::Secondary;
799 block.type = BlockType::ReturnData;
800 }
801
802 return block;
803}
804
806{
807 // Lambda to convert an Acir::Expression to a witness index
808 auto acir_expression_to_witness_or_constant = [](const Acir::Expression& expr) {
809 // Noir gives us witnesses or constants for read/write operations. We use the following assertions to ensure
810 // that the data coming from Noir is in the correct form.
811 BB_ASSERT(expr.mul_terms.empty(), "MemoryOp should not have multiplication terms");
812 BB_ASSERT_LTE(expr.linear_combinations.size(), 1U, "MemoryOp should have at most one linear term");
813
814 const fr a_scaling = expr.linear_combinations.size() == 1
815 ? from_buffer_with_bound_checks(std::get<0>(expr.linear_combinations[0]))
816 : fr::zero();
817 const fr constant_term = from_buffer_with_bound_checks(expr.q_c);
818
819 bool is_witness = a_scaling == fr::one() && constant_term == fr::zero();
820 bool is_constant = a_scaling == fr::zero();
821 BB_ASSERT(is_witness || is_constant, "MemoryOp expression must be a witness or a constant");
822
824 .index = is_witness ? std::get<1>(expr.linear_combinations[0]).value : bb::stdlib::IS_CONSTANT,
825 .value = is_constant ? constant_term : fr::zero(),
826 .is_constant = is_constant,
827 };
828 };
829
830 // Lambda to determine whether a memory operation is a read or write operation
831 auto is_read_operation = [](const Acir::Expression& expr) {
832 BB_ASSERT(expr.mul_terms.empty(), "MemoryOp expression should not have multiplication terms");
833 BB_ASSERT(expr.linear_combinations.empty(), "MemoryOp expression should not have linear terms");
834
835 const fr const_term = from_buffer_with_bound_checks(expr.q_c);
836
837 BB_ASSERT((const_term == fr::one()) || (const_term == fr::zero()),
838 "MemoryOp expression should be either zero or one");
839
840 // A read operation is given by a zero Expression
841 return const_term == fr::zero();
842 };
843
844 AccessType access_type = is_read_operation(mem_op.op.operation) ? AccessType::Read : AccessType::Write;
845 if (access_type == AccessType::Write) {
846 // We are not allowed to write on the databus
848 // Mark the table as a RAM table
849 block.type = BlockType::RAM;
850 }
851
852 // Update the ranges of the index using the array length
853 WitnessOrConstant<bb::fr> index = acir_expression_to_witness_or_constant(mem_op.op.index);
854 WitnessOrConstant<bb::fr> value = acir_expression_to_witness_or_constant(mem_op.op.value);
855
856 MemOp acir_mem_op = MemOp{ .access_type = access_type, .index = index, .value = value };
857 block.trace.push_back(acir_mem_op);
858}
859
861{
862 static constexpr size_t NUM_WIRES = 4; // Equal to the number of wires in the arithmetization
863
864 // If there are more than 4 distinct witnesses in the linear terms, then we need multiple arithmetic gates
865 if (linear_terms.size() > NUM_WIRES) {
866 return false;
867 }
868
869 if (arg.mul_terms.size() > 1) {
870 // If there is more than one multiplication gate, then we need multiple arithmetic gates
871 return false;
872 }
873
874 if (arg.mul_terms.size() == 1) {
875 // In this case we have two witnesses coming from the multiplication term plus the linear terms.
876 // We proceed as follows:
877 // 0. Start from the assumption that all witnesses (from linear terms and multiplication) are distinct
878 // 1. Check if the lhs and rhs witness in the multiplication are already contained in the linear terms
879 // 2. Check if the lhs witness and the rhs witness are equal
880 // 2.a If they are distinct, update the total number of witnesses to be added to wires according to result
881 // of the check at step 1: each distinct witness already in the linear terms subtracts one from the
882 // total
883 // 2.b If they are equal, update the total number of witnesses to be added to wires according to result of
884 // the check at step 1: if the witness is already in the linear terms, it removes one from the total
885
886 // Number of witnesses to be put in wires if the witnesses from the linear terms and the multiplication term are
887 // all different
888 size_t num_witnesses_to_be_put_in_wires = 2 + linear_terms.size();
889
890 uint32_t witness_idx_lhs = std::get<1>(arg.mul_terms[0]).value;
891 uint32_t witness_idx_rhs = std::get<2>(arg.mul_terms[0]).value;
892
893 bool lhs_is_distinct_from_linear_terms = !linear_terms.contains(witness_idx_lhs);
894 bool rhs_is_distinct_from_linear_terms = !linear_terms.contains(witness_idx_rhs);
895
896 if (witness_idx_lhs != witness_idx_rhs) {
897 num_witnesses_to_be_put_in_wires -= lhs_is_distinct_from_linear_terms ? 0U : 1U;
898 num_witnesses_to_be_put_in_wires -= rhs_is_distinct_from_linear_terms ? 0U : 1U;
899 } else {
900 num_witnesses_to_be_put_in_wires -= lhs_is_distinct_from_linear_terms ? 0U : 1U;
901 }
902
903 return num_witnesses_to_be_put_in_wires <= NUM_WIRES;
904 }
905
906 return linear_terms.size() <= NUM_WIRES;
907}
908
910{
911 std::map<uint32_t, bb::fr> linear_terms;
912 for (const auto& linear_term : expr.linear_combinations) {
913 fr selector_value = from_buffer_with_bound_checks(std::get<0>(linear_term));
914 uint32_t witness_idx = std::get<1>(linear_term).value;
915 if (linear_terms.contains(witness_idx)) {
916 linear_terms[witness_idx] += selector_value; // Accumulate coefficients for duplicate witnesses
917 } else {
918 linear_terms[witness_idx] = selector_value;
919 }
920 }
921 return linear_terms;
922}
923
924} // namespace acir_format
#define BB_ASSERT(expression,...)
Definition assert.hpp:70
#define BB_ASSERT_GT(left, right,...)
Definition assert.hpp:113
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:83
#define BB_ASSERT_LTE(left, right,...)
Definition assert.hpp:158
#define BB_ASSERT_LT(left, right,...)
Definition assert.hpp:143
Constraint representing a polynomial of degree 1 or 2 that does not fit into a standard UltraHonk ari...
uint8_t const * buf
Definition data_store.hpp:9
std::unique_ptr< uint8_t[]> buffer
Definition engine.cpp:50
const auto init
Definition fr.bench.cpp:135
void add_memory_op_to_block_constraint(Acir::Opcode::MemoryOp const &mem_op, BlockConstraint &block)
Process memory operation, either read or write, and update the BlockConstraint type accordingly.
AcirFormat circuit_serde_to_acir_format(Acir::Circuit const &circuit)
Convert an Acir::Circuit into an AcirFormat by processing all the opcodes.
WitnessOrConstant< bb::fr > parse_input(const Acir::FunctionInput &input)
Parse an Acir::FunctionInput (which can either be a witness or a constant) into a WitnessOrConstant.
void update_max_witness_index_from_opcode(Acir::Opcode const &opcode, AcirFormat &af)
Update the max witness index by processing all the witness indices contained in the Acir::Opcode.
uint32_t get_witness_from_function_input(const Acir::FunctionInput &input)
Extract the witness index from an Acir::FunctionInput representing a witness.
void update_max_witness_index_from_expression(Acir::Expression const &expr, AcirFormat &af)
Update max_witness_index by processing all witnesses in an Acir::Expression.
WitnessVector witness_buf_to_witness_vector(std::vector< uint8_t > &&buf)
Convert a buffer representing a witness vector into Barretenberg's internal WitnessVector format.
std::vector< mul_quad_< fr > > split_into_mul_quad_gates(Acir::Expression const &arg, std::map< uint32_t, bb::fr > &linear_terms)
========= ACIR OPCODE HANDLERS ========= ///
void update_max_witness_index(const uint32_t witness_idx, AcirFormat &af)
Update the max_witness_index.
WitnessVector witness_map_to_witness_vector(Witnesses::WitnessMap const &witness_map)
Convert from the ACIR-native WitnessMap format to Barretenberg's internal WitnessVector format.
T deserialize_msgpack_compact(std::vector< uint8_t > &&buf, std::function< T(msgpack::object const &)> decode_msgpack)
========= BYTES TO BARRETENBERG'S REPRESENTATION ========= ///
std::vector< bb::fr > WitnessVector
bool is_single_arithmetic_gate(Acir::Expression const &arg, const std::map< uint32_t, bb::fr > &linear_terms)
Given an Acir::Expression and its processed linear terms, determine whether it can be represented by ...
BlockConstraint memory_init_to_block_constraint(Acir::Opcode::MemoryInit const &mem_init)
========= MEMORY OPERATIONS ========== ///
AcirFormat circuit_buf_to_acir_format(std::vector< uint8_t > &&buf)
Convert a buffer representing a circuit into Barretenberg's internal AcirFormat representation.
bb::fr from_buffer_with_bound_checks(const std::vector< uint8_t > &buffer)
========= HELPERS ========= ///
void assert_zero_to_quad_constraints(Acir::Opcode::AssertZero const &arg, AcirFormat &af, size_t opcode_index)
Single entrypoint for processing arithmetic (AssertZero) opcodes.
void add_blackbox_func_call_to_acir_format(Acir::Opcode::BlackBoxFuncCall const &arg, AcirFormat &af, size_t opcode_index)
std::map< uint32_t, bb::fr > process_linear_terms(Acir::Expression const &expr)
========= ACIR OPCODE HANDLERS ========= ///
Cont< OutElem > map(Cont< InElem, Args... > const &in, F &&op)
Definition map.hpp:15
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
field< Bn254FrParams > fr
Definition fr.hpp:155
void assert_failure(std::string const &err)
Definition assert.cpp:11
C join(std::initializer_list< C > to_join)
Definition container.hpp:26
@ SECP256K1
Definition types.hpp:10
@ SECP256R1
Definition types.hpp:10
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::string to_string(bb::avm2::ValueTag tag)
std::variant< AES128Encrypt, AND, XOR, RANGE, Blake2s, Blake3, EcdsaSecp256k1, EcdsaSecp256r1, MultiScalarMul, EmbeddedCurveAdd, Keccakf1600, RecursiveAggregation, Poseidon2Permutation, Sha256Compression > value
Definition acir.hpp:3447
std::variant< Memory, CallData, ReturnData > value
Definition acir.hpp:3749
Acir::PublicInputs return_values
Definition acir.hpp:4793
std::vector< Acir::Opcode > opcodes
Definition acir.hpp:4790
Acir::PublicInputs public_parameters
Definition acir.hpp:4792
std::vector< std::tuple< std::vector< uint8_t >, Acir::Witness > > linear_combinations
Definition acir.hpp:3833
std::vector< uint8_t > q_c
Definition acir.hpp:3834
std::vector< std::tuple< std::vector< uint8_t >, Acir::Witness, Acir::Witness > > mul_terms
Definition acir.hpp:3832
std::variant< Constant, Witness > value
Definition acir.hpp:2837
Acir::Expression value
Definition acir.hpp:4140
Acir::Expression operation
Definition acir.hpp:4138
Acir::Expression index
Definition acir.hpp:4139
Acir::Expression value
Definition acir.hpp:4174
Acir::BlackBoxFuncCall value
Definition acir.hpp:4192
std::vector< Acir::Witness > init
Definition acir.hpp:4241
Acir::BlockType block_type
Definition acir.hpp:4242
std::variant< AssertZero, BlackBoxFuncCall, MemoryOp, MemoryInit, BrilligCall, Call > value
Definition acir.hpp:4349
std::vector< Acir::Witness > value
Definition acir.hpp:4770
std::map< Witnesses::Witness, std::vector< uint8_t > > value
std::vector< WitnessOrConstant< bb::fr > > inputs
Barretenberg's representation of ACIR constraints.
std::vector< MultiScalarMul > multi_scalar_mul_constraints
std::vector< Blake2sConstraint > blake2s_constraints
std::vector< Sha256Compression > sha256_compression
std::vector< Poseidon2Constraint > poseidon2_constraints
std::vector< LogicConstraint > logic_constraints
std::vector< EcAdd > ec_add_constraints
std::vector< QuadConstraint > quad_constraints
std::vector< Keccakf1600 > keccak_permutations
std::vector< RecursionConstraint > honk_recursion_constraints
std::vector< Blake3Constraint > blake3_constraints
std::vector< EcdsaConstraint > ecdsa_r1_constraints
std::vector< RangeConstraint > range_constraints
std::vector< BigQuadConstraint > big_quad_constraints
std::vector< AES128Constraint > aes128_constraints
AcirFormatOriginalOpcodeIndices original_opcode_indices
std::vector< BlockConstraint > block_constraints
std::vector< EcdsaConstraint > ecdsa_k1_constraints
std::vector< RecursionConstraint > hn_recursion_constraints
std::vector< uint32_t > public_inputs
std::vector< RecursionConstraint > avm_recursion_constraints
std::vector< RecursionConstraint > chonk_recursion_constraints
std::vector< std::vector< size_t > > block_constraints
std::vector< WitnessOrConstant< bb::fr > > inputs
std::vector< WitnessOrConstant< bb::fr > > inputs
Struct holding the data required to add memory constraints to a circuit.
std::vector< uint32_t > init
Constraints for addition of two points on the Grumpkin curve.
WitnessOrConstant< bb::fr > input1_x
std::array< WitnessOrConstant< bb::fr >, 25 > state
Logic constraint representation in ACIR format.
WitnessOrConstant< fr > a
Memory operation. Index and value store the index of the memory location, and value is the value to b...
std::vector< WitnessOrConstant< bb::fr > > points
std::vector< WitnessOrConstant< bb::fr > > state
RecursionConstraint struct contains information required to recursively verify a proof.
std::array< WitnessOrConstant< bb::fr >, 16 > inputs
========= HELPERS ========= ///
static constexpr field one()
static field random_element(numeric::RNG *engine=nullptr) noexcept
static field serialize_from_buffer(const uint8_t *buffer)
static constexpr field zero()