Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
ultra_circuit_builder.cpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Complete, auditors: [Luke, Raju], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
16#include "rom_ram_logic.hpp"
17
20#include <execution>
21#include <unordered_map>
22#include <unordered_set>
23
24namespace bb {
25
26template <typename ExecutionTrace>
28{
54 if (!this->circuit_finalized) {
55 if (ensure_nonzero) {
56 add_gates_to_ensure_all_polys_are_non_zero();
57 }
58 process_non_native_field_multiplications();
59#ifndef ULTRA_FUZZ
60 this->rom_ram_logic.process_ROM_arrays(this);
61 this->rom_ram_logic.process_RAM_arrays(this);
62 process_range_lists();
63#endif
64 populate_public_inputs_block();
65 this->circuit_finalized = true;
66 } else {
67 // Gates added after first call to finalize will not be processed since finalization is only performed once
68 info("WARNING: Redundant call to finalize_circuit(). Is this intentional?");
69 }
70}
71
76{
77 BB_BENCH_NAME("populate_public_inputs_block");
78
79 // Update the public inputs block
80 for (const auto& idx : this->public_inputs()) {
81 // first two wires get a copy of the public inputs
82 blocks.pub_inputs.populate_wires(idx, idx, this->zero_idx(), this->zero_idx());
83 for (auto& selector : this->blocks.pub_inputs.get_selectors()) {
84 selector.emplace_back(0);
85 }
86 }
87}
88
94// TODO(#423): This function adds valid (but arbitrary) gates to ensure that the circuit which includes
95// them will not result in any zero-polynomials. It also ensures that the first coefficient of the wire
96// polynomials is zero, which is required for them to be shiftable.
97template <typename ExecutionTrace>
99{
100 // q_m, q_1, q_2, q_3, q_4
101 blocks.arithmetic.populate_wires(this->zero_idx(), this->zero_idx(), this->zero_idx(), this->zero_idx());
102 blocks.arithmetic.q_m().emplace_back(1);
103 blocks.arithmetic.q_1().emplace_back(1);
104 blocks.arithmetic.q_2().emplace_back(1);
105 blocks.arithmetic.q_3().emplace_back(1);
106 blocks.arithmetic.q_4().emplace_back(1);
107 blocks.arithmetic.q_c().emplace_back(0);
108 blocks.arithmetic.set_gate_selector(0);
109 check_selector_length_consistency();
110 this->increment_num_gates();
111
112 // q_delta_range
113 blocks.delta_range.populate_wires(this->zero_idx(), this->zero_idx(), this->zero_idx(), this->zero_idx());
114 blocks.delta_range.q_m().emplace_back(0);
115 blocks.delta_range.q_1().emplace_back(0);
116 blocks.delta_range.q_2().emplace_back(0);
117 blocks.delta_range.q_3().emplace_back(0);
118 blocks.delta_range.q_4().emplace_back(0);
119 blocks.delta_range.q_c().emplace_back(0);
120 blocks.delta_range.set_gate_selector(1);
121
122 check_selector_length_consistency();
123 this->increment_num_gates();
124 create_unconstrained_gate(
125 blocks.delta_range, this->zero_idx(), this->zero_idx(), this->zero_idx(), this->zero_idx());
126
127 // q_elliptic
128 blocks.elliptic.populate_wires(this->zero_idx(), this->zero_idx(), this->zero_idx(), this->zero_idx());
129 blocks.elliptic.q_m().emplace_back(0);
130 blocks.elliptic.q_1().emplace_back(0);
131 blocks.elliptic.q_2().emplace_back(0);
132 blocks.elliptic.q_3().emplace_back(0);
133 blocks.elliptic.q_4().emplace_back(0);
134 blocks.elliptic.q_c().emplace_back(0);
135 blocks.elliptic.set_gate_selector(1);
136 check_selector_length_consistency();
137 this->increment_num_gates();
138 create_unconstrained_gate(blocks.elliptic, this->zero_idx(), this->zero_idx(), this->zero_idx(), this->zero_idx());
139
140 // q_memory
141 blocks.memory.populate_wires(this->zero_idx(), this->zero_idx(), this->zero_idx(), this->zero_idx());
142 blocks.memory.q_m().emplace_back(0);
143 blocks.memory.q_1().emplace_back(0);
144 blocks.memory.q_2().emplace_back(0);
145 blocks.memory.q_3().emplace_back(0);
146 blocks.memory.q_4().emplace_back(0);
147 blocks.memory.q_c().emplace_back(0);
148 blocks.memory.set_gate_selector(1);
149 check_selector_length_consistency();
150 this->increment_num_gates();
151 create_unconstrained_gate(blocks.memory, this->zero_idx(), this->zero_idx(), this->zero_idx(), this->zero_idx());
152
153 // q_nnf
154 blocks.nnf.populate_wires(this->zero_idx(), this->zero_idx(), this->zero_idx(), this->zero_idx());
155 blocks.nnf.q_m().emplace_back(0);
156 blocks.nnf.q_1().emplace_back(0);
157 blocks.nnf.q_2().emplace_back(0);
158 blocks.nnf.q_3().emplace_back(0);
159 blocks.nnf.q_4().emplace_back(0);
160 blocks.nnf.q_c().emplace_back(0);
161 blocks.nnf.set_gate_selector(1);
162 check_selector_length_consistency();
163 this->increment_num_gates();
164 create_unconstrained_gate(blocks.nnf, this->zero_idx(), this->zero_idx(), this->zero_idx(), this->zero_idx());
165
166 // Add nonzero values in w_4 and q_c (q_4*w_4 + q_c --> 1*1 - 1 = 0)
167 uint32_t one_idx = put_constant_variable(FF::one());
168 create_big_add_gate({ this->zero_idx(), this->zero_idx(), this->zero_idx(), one_idx, 0, 0, 0, 1, -1 });
169
170 // Take care of all polys related to lookups (q_lookup, tables, sorted, etc)
171 // by doing a dummy lookup with a special table.
172 // Note: the 4th table poly is the table index: this is not the value of the table
173 // type enum but rather the index of the table in the list of all tables utilized
174 // in the circuit. Therefore we naively need two different basic tables (indices 0, 1)
175 // to get a non-zero value in table_4.
176 // The multitable operates on 2-bit values, so the maximum is 3
177 uint32_t left_value = 3;
178 uint32_t right_value = 3;
179
180 FF left_witness_value = fr{ left_value, 0, 0, 0 }.to_montgomery_form();
181 FF right_witness_value = fr{ right_value, 0, 0, 0 }.to_montgomery_form();
182
183 uint32_t left_witness_index = this->add_variable(left_witness_value);
184 uint32_t right_witness_index = this->add_variable(right_witness_value);
185 const auto dummy_accumulators = plookup::get_lookup_accumulators(
186 plookup::MultiTableId::HONK_DUMMY_MULTI, left_witness_value, right_witness_value, true);
187 auto read_data = create_gates_from_plookup_accumulators(
188 plookup::MultiTableId::HONK_DUMMY_MULTI, dummy_accumulators, left_witness_index, right_witness_index);
189
190 update_used_witnesses(left_witness_index);
191 update_used_witnesses(right_witness_index);
192 std::array<std::vector<uint32_t>, 3> parse_read_data{ read_data[plookup::ColumnIdx::C1],
193 read_data[plookup::ColumnIdx::C2],
194 read_data[plookup::ColumnIdx::C3] };
195 for (const auto& column : parse_read_data) {
196 update_used_witnesses(column);
197 update_finalize_witnesses(column);
198 }
199
200 // mock a poseidon external gate, with all zeros as input
201 blocks.poseidon2_external.populate_wires(this->zero_idx(), this->zero_idx(), this->zero_idx(), this->zero_idx());
202 blocks.poseidon2_external.q_m().emplace_back(0);
203 blocks.poseidon2_external.q_1().emplace_back(0);
204 blocks.poseidon2_external.q_2().emplace_back(0);
205 blocks.poseidon2_external.q_3().emplace_back(0);
206 blocks.poseidon2_external.q_c().emplace_back(0);
207 blocks.poseidon2_external.q_4().emplace_back(0);
208 blocks.poseidon2_external.set_gate_selector(1);
209 check_selector_length_consistency();
210 this->increment_num_gates();
211
212 // unconstrained gate to be read into by previous poseidon external gate via shifts
213 create_unconstrained_gate(
214 blocks.poseidon2_external, this->zero_idx(), this->zero_idx(), this->zero_idx(), this->zero_idx());
215
216 // mock a poseidon internal gate, with all zeros as input
217 blocks.poseidon2_internal.populate_wires(this->zero_idx(), this->zero_idx(), this->zero_idx(), this->zero_idx());
218 blocks.poseidon2_internal.q_m().emplace_back(0);
219 blocks.poseidon2_internal.q_1().emplace_back(0);
220 blocks.poseidon2_internal.q_2().emplace_back(0);
221 blocks.poseidon2_internal.q_3().emplace_back(0);
222 blocks.poseidon2_internal.q_c().emplace_back(0);
223 blocks.poseidon2_internal.q_4().emplace_back(0);
224 blocks.poseidon2_internal.set_gate_selector(1);
225 check_selector_length_consistency();
226 this->increment_num_gates();
227
228 // dummy gate to be read into by previous poseidon internal gate via shifts
229 create_unconstrained_gate(
230 blocks.poseidon2_internal, this->zero_idx(), this->zero_idx(), this->zero_idx(), this->zero_idx());
231}
232
241template <typename ExecutionTrace> void UltraCircuitBuilder_<ExecutionTrace>::create_add_gate(const add_triple_<FF>& in)
242{
243 // Delegate to create_big_add_gate with 4th wire set to zero
244 create_big_add_gate({ .a = in.a,
245 .b = in.b,
246 .c = in.c,
247 .d = this->zero_idx(),
248 .a_scaling = in.a_scaling,
249 .b_scaling = in.b_scaling,
250 .c_scaling = in.c_scaling,
251 .d_scaling = 0,
252 .const_scaling = in.const_scaling });
253}
254
263template <typename ExecutionTrace>
265 const bool include_next_gate_w_4)
266{
267 this->assert_valid_variables({ in.a, in.b, in.c, in.d });
268 blocks.arithmetic.populate_wires(in.a, in.b, in.c, in.d);
269 // If include_next_gate_w_4 is true then we set q_arith = 2. In this case, the linear term in the ArithmeticRelation
270 // is scaled by a factor of 2. We compensate here by scaling the quadratic term by 2 to achieve the constraint:
271 // 2 * [q_m * w_1 * w_2 + \sum_{i=1..4} q_i * w_i + q_c + w_4_shift] = 0
272 const FF mul_scaling = include_next_gate_w_4 ? in.mul_scaling * FF(2) : in.mul_scaling;
273 blocks.arithmetic.q_m().emplace_back(mul_scaling);
274 blocks.arithmetic.q_1().emplace_back(in.a_scaling);
275 blocks.arithmetic.q_2().emplace_back(in.b_scaling);
276 blocks.arithmetic.q_3().emplace_back(in.c_scaling);
277 blocks.arithmetic.q_c().emplace_back(in.const_scaling);
278 blocks.arithmetic.q_4().emplace_back(in.d_scaling);
279 blocks.arithmetic.set_gate_selector(include_next_gate_w_4 ? 2 : 1);
280 check_selector_length_consistency();
281 this->increment_num_gates();
282}
283
292template <typename ExecutionTrace>
294 const bool include_next_gate_w_4)
296 this->assert_valid_variables({ in.a, in.b, in.c, in.d });
297 blocks.arithmetic.populate_wires(in.a, in.b, in.c, in.d);
298 blocks.arithmetic.q_m().emplace_back(0);
299 blocks.arithmetic.q_1().emplace_back(in.a_scaling);
300 blocks.arithmetic.q_2().emplace_back(in.b_scaling);
301 blocks.arithmetic.q_3().emplace_back(in.c_scaling);
302 blocks.arithmetic.q_c().emplace_back(in.const_scaling);
303 blocks.arithmetic.q_4().emplace_back(in.d_scaling);
304 blocks.arithmetic.set_gate_selector(include_next_gate_w_4 ? 2 : 1);
305 check_selector_length_consistency();
306 this->increment_num_gates();
307}
308
314template <typename ExecutionTrace>
316{
317 this->assert_valid_variables({ variable_index });
318
319 blocks.arithmetic.populate_wires(variable_index, variable_index, this->zero_idx(), this->zero_idx());
320 blocks.arithmetic.q_m().emplace_back(1);
321 blocks.arithmetic.q_1().emplace_back(-1);
322 blocks.arithmetic.q_2().emplace_back(0);
323 blocks.arithmetic.q_3().emplace_back(0);
324 blocks.arithmetic.q_c().emplace_back(0);
325 blocks.arithmetic.q_4().emplace_back(0);
326 blocks.arithmetic.set_gate_selector(1);
327 check_selector_length_consistency();
328 this->increment_num_gates();
329}
330
337template <typename ExecutionTrace>
339{
340 this->assert_valid_variables({ in.a, in.b, in.c });
341
342 blocks.arithmetic.populate_wires(in.a, in.b, in.c, this->zero_idx());
343 blocks.arithmetic.q_m().emplace_back(in.q_m);
344 blocks.arithmetic.q_1().emplace_back(in.q_l);
345 blocks.arithmetic.q_2().emplace_back(in.q_r);
346 blocks.arithmetic.q_3().emplace_back(in.q_o);
347 blocks.arithmetic.q_c().emplace_back(in.q_c);
348 blocks.arithmetic.q_4().emplace_back(0);
349 blocks.arithmetic.set_gate_selector(1);
350 check_selector_length_consistency();
351 this->increment_num_gates();
352}
353
370template <typename ExecutionTrace>
372{
373 this->assert_valid_variables({ in.x1, in.x2, in.x3, in.y1, in.y2, in.y3 });
374
375 auto& block = blocks.elliptic;
376
377 // Convert bool to field element for the relation: +1 for addition, -1 for subtraction
378 // The elliptic curve relation assumes q_sign² = 1 (see elliptic_relation.hpp)
379 const FF q_sign = in.is_addition ? FF(1) : FF(-1);
380
381 // Determine whether we can fuse this addition operation into the previous gate in the block
382 bool can_fuse_into_previous_gate =
383 block.size() > 0 && /* a previous gate exists in the block */
384 block.w_r()[block.size() - 1] == in.x1 && /* output x coord of previous gate is input of this one */
385 block.w_o()[block.size() - 1] == in.y1; /* output y coord of previous gate is input of this one */
386
387 if (can_fuse_into_previous_gate) {
388 block.q_1().set(block.size() - 1, q_sign); // set q_sign of previous gate
389 block.q_elliptic().set(block.size() - 1, 1); // set q_ecc of previous gate to 1
390 } else {
391 block.populate_wires(this->zero_idx(), in.x1, in.y1, this->zero_idx());
392 block.q_3().emplace_back(0);
393 block.q_4().emplace_back(0);
394 block.q_1().emplace_back(q_sign);
395
396 block.q_2().emplace_back(0);
397 block.q_m().emplace_back(0);
398 block.q_c().emplace_back(0);
399 block.set_gate_selector(1);
400 check_selector_length_consistency();
401 this->increment_num_gates();
402 }
403 // Create the unconstrained gate with the output of the doubling to be read into by the previous gate via shifts
404 create_unconstrained_gate(block, in.x2, in.x3, in.y3, in.y2);
405}
406
423template <typename ExecutionTrace>
425{
426 this->assert_valid_variables({ in.x1, in.x3, in.y1, in.y3 });
428 auto& block = blocks.elliptic;
429
430 // Determine whether we can fuse this doubling operation into the previous gate in the block
431 bool can_fuse_into_previous_gate =
432 block.size() > 0 && /* a previous gate exists in the block */
433 block.w_r()[block.size() - 1] == in.x1 && /* output x coord of previous gate is input of this one */
434 block.w_o()[block.size() - 1] == in.y1; /* output y coord of previous gate is input of this one */
436 // If possible, update the previous gate to be the first gate in the pair, otherwise create a new gate
437 if (can_fuse_into_previous_gate) {
438 block.q_elliptic().set(block.size() - 1, 1); // set q_ecc of previous gate to 1
439 block.q_m().set(block.size() - 1, 1); // set q_m (q_is_double) of previous gate to 1
440 } else {
441 block.populate_wires(this->zero_idx(), in.x1, in.y1, this->zero_idx());
442 block.q_m().emplace_back(1);
443 block.q_1().emplace_back(0);
444 block.q_2().emplace_back(0);
445 block.q_3().emplace_back(0);
446 block.q_c().emplace_back(0);
447 block.q_4().emplace_back(0);
448 block.set_gate_selector(1);
449 check_selector_length_consistency();
450 this->increment_num_gates();
451 }
452 // Create the unconstrained gate with the output of the doubling to be read into by the previous gate via shifts
453 create_unconstrained_gate(block, this->zero_idx(), in.x3, in.y3, this->zero_idx());
454}
455
462template <typename ExecutionTrace>
463void UltraCircuitBuilder_<ExecutionTrace>::fix_witness(const uint32_t witness_index, const FF& witness_value)
464{
465 this->assert_valid_variables({ witness_index });
466
467 // Mark as intentionally single-gate for boomerang detection
468 update_used_witnesses(witness_index);
469
470 blocks.arithmetic.populate_wires(witness_index, this->zero_idx(), this->zero_idx(), this->zero_idx());
471 blocks.arithmetic.q_m().emplace_back(0);
472 blocks.arithmetic.q_1().emplace_back(1);
473 blocks.arithmetic.q_2().emplace_back(0);
474 blocks.arithmetic.q_3().emplace_back(0);
475 blocks.arithmetic.q_c().emplace_back(-witness_value);
476 blocks.arithmetic.q_4().emplace_back(0);
477 blocks.arithmetic.set_gate_selector(1);
478 check_selector_length_consistency();
479 this->increment_num_gates();
480}
481
482template <typename ExecutionTrace>
484{
485 if (constant_variable_indices.contains(variable)) {
486 return constant_variable_indices.at(variable);
487 } else {
488 uint32_t variable_index = this->add_variable(variable);
489 fix_witness(variable_index, variable);
490 constant_variable_indices.insert({ variable, variable_index });
491 return variable_index;
492 }
493}
494
503template <typename ExecutionTrace>
505{
506 for (plookup::BasicTable& table : lookup_tables) {
507 if (table.id == id) {
508 return table;
509 }
510 }
511 // Table doesn't exist! So try to create it.
512 lookup_tables.emplace_back(plookup::create_basic_table(id, lookup_tables.size()));
513 return lookup_tables.back();
514}
515
543template <typename ExecutionTrace>
545 const plookup::MultiTableId& id,
546 const plookup::ReadData<FF>& read_values,
547 const uint32_t key_a_index,
548 std::optional<uint32_t> key_b_index)
549{
550 using plookup::ColumnIdx;
551
552 const auto& multi_table = plookup::get_multitable(id);
553 const size_t num_lookups = read_values[ColumnIdx::C1].size();
555
556 for (size_t i = 0; i < num_lookups; ++i) {
557 const bool is_first_lookup = (i == 0);
558 const bool is_last_lookup = (i == num_lookups - 1);
559
560 // Get basic lookup table; construct and add to builder.lookup_tables if not already present
561 plookup::BasicTable& table = get_table(multi_table.basic_table_ids[i]);
562 table.lookup_gates.emplace_back(read_values.lookup_entries[i]);
564 // Create witness variables: first lookup reuses user's input indices, subsequent create new variables
565 const auto first_idx = is_first_lookup ? key_a_index : this->add_variable(read_values[ColumnIdx::C1][i]);
566 const auto second_idx = (is_first_lookup && key_b_index.has_value())
567 ? *key_b_index
568 : this->add_variable(read_values[ColumnIdx::C2][i]);
569 const auto third_idx = this->add_variable(read_values[ColumnIdx::C3][i]);
570
571 read_data[ColumnIdx::C1].push_back(first_idx);
572 read_data[ColumnIdx::C2].push_back(second_idx);
573 read_data[ColumnIdx::C3].push_back(third_idx);
574 this->assert_valid_variables({ first_idx, second_idx, third_idx });
575
576 // Populate lookup gate: wire values and selectors
577 blocks.lookup.populate_wires(first_idx, second_idx, third_idx, this->zero_idx());
578 blocks.lookup.set_gate_selector(1); // mark as lookup gate
579 blocks.lookup.q_3().emplace_back(FF(table.table_index)); // unique table identifier
580 // Step size coefficients: zero for last lookup (no next accumulator), negative step sizes otherwise
581 blocks.lookup.q_2().emplace_back(is_last_lookup ? 0 : -multi_table.column_1_step_sizes[i + 1]);
582 blocks.lookup.q_m().emplace_back(is_last_lookup ? 0 : -multi_table.column_2_step_sizes[i + 1]);
583 blocks.lookup.q_c().emplace_back(is_last_lookup ? 0 : -multi_table.column_3_step_sizes[i + 1]);
584 blocks.lookup.q_1().emplace_back(0); // unused
585 blocks.lookup.q_4().emplace_back(0); // unused
586
587 check_selector_length_consistency();
588 this->increment_num_gates();
589 }
590 return read_data;
591}
592
596template <typename ExecutionTrace>
598 const uint64_t target_range)
599{
600 RangeList result;
601 const auto range_tag = get_new_tag();
602 const auto tau_tag = get_new_tag();
603 set_tau_transposition(range_tag, tau_tag);
604 result.target_range = target_range;
605 result.range_tag = range_tag;
606 result.tau_tag = tau_tag;
608 uint64_t num_multiples_of_three = (target_range / DEFAULT_PLOOKUP_RANGE_STEP_SIZE);
609 // allocate the minimum number of variable indices required for the range constraint. this function is only called
610 // when we are creating a range constraint on a witness index, which is responsible for the extra + 1. (note that
611 // the below loop goes from 0 to `num_multiples_of_three` inclusive.)
612 result.variable_indices.reserve(static_cast<uint32_t>(num_multiples_of_three + 3));
613 for (uint64_t i = 0; i <= num_multiples_of_three; ++i) {
614 const uint32_t index = this->add_variable(fr(i * DEFAULT_PLOOKUP_RANGE_STEP_SIZE));
615 result.variable_indices.emplace_back(index);
616 assign_tag(index, result.range_tag);
617 }
618 // `target_range` may not be divisible by 3, so we explicitly add it also.
619 {
620 const uint32_t index = this->add_variable(fr(target_range));
621 result.variable_indices.emplace_back(index);
622 assign_tag(index, result.range_tag);
623 }
624 // Need this because these variables will not appear in the witness otherwise
625 create_unconstrained_gates(result.variable_indices);
626
627 return result;
628}
629
630template <typename ExecutionTrace>
632 const uint32_t variable_index, const uint64_t num_bits, const uint64_t target_range_bitnum, std::string const& msg)
633{
634 this->assert_valid_variables({ variable_index });
635 // make sure `num_bits` satisfies the correct bounds
636 BB_ASSERT_GT(num_bits, 0U);
637 BB_ASSERT_GTE(MAX_NUM_BITS_RANGE_CONSTRAINT, num_bits);
638
639 uint256_t val = (uint256_t)(this->get_variable(variable_index));
640
641 // If the value is out of range, set the CircuitBuilder error to the given msg.
642 if (val.get_msb() >= num_bits && !this->failed()) {
643 this->failure(msg);
644 }
645
646 // compute limb structure
647 const uint64_t sublimb_mask = (1ULL << target_range_bitnum) - 1;
648
649 std::vector<uint64_t> sublimbs;
650 std::vector<uint32_t> sublimb_indices;
651
652 const bool has_remainder_bits = (num_bits % target_range_bitnum != 0);
653 const uint64_t num_limbs = (num_bits / target_range_bitnum) + has_remainder_bits;
654 const uint64_t last_limb_size = num_bits - ((num_bits / target_range_bitnum) * target_range_bitnum);
655 const uint64_t last_limb_range = ((uint64_t)1 << last_limb_size) - 1;
656
657 // extract limbs from the value
658 uint256_t accumulator = val;
659 for (size_t i = 0; i < num_limbs; ++i) {
660 sublimbs.push_back(accumulator.data[0] & sublimb_mask);
661 accumulator = accumulator >> target_range_bitnum;
662 }
663 // set the correct range constraint on each limb. note that when there are remainder bits, the last limb must be
664 // constrained to a smaller range.
665 const size_t num_full_limbs = has_remainder_bits ? sublimbs.size() - 1 : sublimbs.size();
666 for (size_t i = 0; i < num_full_limbs; ++i) {
667 const auto limb_idx = this->add_variable(bb::fr(sublimbs[i]));
668 sublimb_indices.emplace_back(limb_idx);
669 create_small_range_constraint(limb_idx, sublimb_mask);
670 }
671 if (has_remainder_bits) {
672 const auto limb_idx = this->add_variable(bb::fr(sublimbs.back()));
673 sublimb_indices.emplace_back(limb_idx);
674 create_small_range_constraint(limb_idx, last_limb_range);
675 }
676
677 // Prove that the limbs reconstruct the original value by processing limbs in groups of 3.
678 // We constrain: value = sum_{j=0}^{num_limbs-1} limb[j] * 2^(j * target_range_bitnum)
679 //
680 // Each iteration subtracts 3 limbs' contributions from an accumulator (starting at `val`),
681 // and constrains that the accumulator updates correctly via an arithmetic gate.
682 const uint64_t num_limb_triples = (num_limbs / 3) + ((num_limbs % 3) != 0);
683 // `leftovers` is the number of real limbs in the final triple (1, 2, or 3).
684 const uint64_t leftovers = (num_limbs % 3) == 0 ? 3 : (num_limbs % 3);
685
686 accumulator = val;
687 uint32_t accumulator_idx = variable_index;
688 // loop goes from `i = 0` to `num_limb_triples`, but some special case must be taken for the last triple (`i ==
689 // num_limb_triples - 1`), hence some conditional logic.
690 for (size_t i = 0; i < num_limb_triples; ++i) {
691 // `real_limbs` which limb positions in this triple contain actual limbs vs zero-padding.
692 // When `i == num_limb_triples - 1`, some positions may be unused if `num_limbs` isn't divisible by 3.
693 const bool real_limbs[3]{
694 !(i == (num_limb_triples - 1) && (leftovers < 1)),
695 !(i == (num_limb_triples - 1) && (leftovers < 2)),
696 !(i == (num_limb_triples - 1) && (leftovers < 3)),
697 };
698
699 // The witness values of the 3 limbs in this triple (0 for padding positions).
700 const uint64_t round_sublimbs[3]{
701 real_limbs[0] ? sublimbs[3 * i] : 0,
702 real_limbs[1] ? sublimbs[3 * i + 1] : 0,
703 real_limbs[2] ? sublimbs[3 * i + 2] : 0,
704 };
705 // The witnesss indices of the current 3 limbs (zero_idx for padding positions).
706 const uint32_t new_limbs[3]{
707 real_limbs[0] ? sublimb_indices[3 * i] : this->zero_idx(),
708 real_limbs[1] ? sublimb_indices[3 * i + 1] : this->zero_idx(),
709 real_limbs[2] ? sublimb_indices[3 * i + 2] : this->zero_idx(),
710 };
711 // Bit-shifts for each limb: limb[3*i+k] contributes at bit position (3*i+k) * target_range_bitnum.
712 const uint64_t shifts[3]{
713 target_range_bitnum * (3 * i),
714 target_range_bitnum * (3 * i + 1),
715 target_range_bitnum * (3 * i + 2),
716 };
717 // Compute the new accumulator after subtracting this triple's contribution.
718 // After the final iteration, accumulator should be 0.
719 uint256_t new_accumulator = accumulator - (uint256_t(round_sublimbs[0]) << shifts[0]) -
720 (uint256_t(round_sublimbs[1]) << shifts[1]) -
721 (uint256_t(round_sublimbs[2]) << shifts[2]);
722
723 // This `big_add_gate` has differing behavior depending on whether or not `i == num_limb_triples - 1`.
724 // If `i != num_limb_triples - 1`, then the constraint will be limb[0]*2^shift[0] + limb[1]*2^shift[1] +
725 // limb[2]*2^shift[2] - acc = new_accumulator (the last argument to `create_big_add_gate` is `true`, means the
726 // sum is w_4-shift, which will be the witness corresponding to what is currently `new_accumulator`.).
727 // If `i == num_limb_triples - 1`, then the last argument to `create_big_add_gate` is false, so the constraint
728 // is limb[0]*2^shift[0] + limb[1]*2^shift[1] + limb[2]*2^shift[2] - acc = 0.
729 //
730 // N.B. When `num_bits` is small, we only have remainder bits. This last constraint, checking the correctness of
731 // the limb-decomposition, ensures that the variable is not orphaned. (See the warning in
732 // `create_small_range_constraint`.)
733 create_big_add_gate(
734 {
735 new_limbs[0],
736 new_limbs[1],
737 new_limbs[2],
738 accumulator_idx,
739 uint256_t(1) << shifts[0],
740 uint256_t(1) << shifts[1],
741 uint256_t(1) << shifts[2],
742 -1,
743 0,
744 },
745 (i != num_limb_triples - 1));
746 if (i != num_limb_triples - 1) {
747 accumulator_idx = this->add_variable(fr(new_accumulator));
748 accumulator = new_accumulator;
749 }
750 }
751 return sublimb_indices;
752}
753
754template <typename ExecutionTrace>
756 const uint64_t target_range,
757 std::string const msg)
758{
759 // make sure `target_range` is not too big.
760 BB_ASSERT_GTE(MAX_SMALL_RANGE_CONSTRAINT_VAL, target_range);
761 const bool is_out_of_range = (uint256_t(this->get_variable(variable_index)).data[0] > target_range);
762 if (is_out_of_range && !this->failed()) {
763 this->failure(msg);
764 }
765 if (range_lists.count(target_range) == 0) {
766 range_lists.insert({ target_range, create_range_list(target_range) });
767 }
768 // The tag of `variable_index` is `DEFAULT_TAG` if it has never been range-constrained and a non-trivial value
769 // otherwise.
770 const auto existing_tag = this->real_variable_tags[this->real_variable_index[variable_index]];
771 auto& list = range_lists[target_range];
772
773 // If the variable's tag matches the target range list's tag, do nothing; the variable has _already_ been
774 // constrained to this exact range (i.e., `create_new_range_constraint(variable_index, target_range)` has already
775 // been called).
776 if (existing_tag == list.range_tag) {
777 return;
778 }
779 // If the variable is 'untagged' (i.e., it has the dummy tag), assign it the appropriate tag, which amounts to
780 // setting the range-constraint.
781 if (existing_tag == DEFAULT_TAG) {
782 assign_tag(variable_index, list.range_tag);
783 list.variable_indices.emplace_back(variable_index);
784 return;
785 }
786 // Otherwise, find the range for which the variable has already been tagged.
787 bool found_tag = false;
788 for (const auto& r : range_lists) {
789 if (r.second.range_tag == existing_tag) {
790 found_tag = true;
791 if (r.first < target_range) {
792 // The variable already has a more restrictive range check, so do nothing.
793 return;
794 }
795 // The range constraint we are trying to impose is more restrictive than the existing range
796 // constraint. It would be difficult to remove an existing range check. Instead, arithmetically copy the
797 // variable and apply a range check to new variable. We do _not_ simply create a
798 // copy-constraint, because that would copy the tag, which exactly corresponds to the old (less
799 // restrictive) range constraint. Instead, we use an arithmetic gate to constrain the value of
800 // the new variable and set the tag (a.k.a. range-constraint) via a new call to
801 // `create_new_range_constraint`.
802 const uint32_t copied_witness = this->add_variable(this->get_variable(variable_index));
803 create_add_gate({ .a = variable_index,
804 .b = copied_witness,
805 .c = this->zero_idx(),
806 .a_scaling = 1,
807 .b_scaling = -1,
808 .c_scaling = 0,
809 .const_scaling = 0 });
810 // Recurse with new witness that has no tag attached.
811 create_small_range_constraint(copied_witness, target_range, msg);
812 return;
813 }
814 }
815 // should never occur
816 BB_ASSERT(found_tag);
817}
818
819template <typename ExecutionTrace> void UltraCircuitBuilder_<ExecutionTrace>::process_range_list(RangeList& list)
820{
821 this->assert_valid_variables(list.variable_indices);
822
823 BB_ASSERT_GT(list.variable_indices.size(), 0U);
824
825 // replace witness-index in variable_indices with the corresponding real-variable-index i.e., if a copy constraint
826 // has been applied on a variable after it was range constrained, this makes sure the indices in list point to the
827 // updated index in the range list so the set equivalence does not fail
828 for (uint32_t& x : list.variable_indices) {
829 x = this->real_variable_index[x];
830 }
831 // Sort `variable_indices` and remove duplicate witness indices to prevent the sorted list set size being wrong!
832 std::sort(list.variable_indices.begin(), list.variable_indices.end());
833 auto back_iterator = std::unique(list.variable_indices.begin(), list.variable_indices.end());
834 list.variable_indices.erase(back_iterator, list.variable_indices.end());
835
836 // Extract the values of each (real) variable into a list to be sorted (in the sense of the range/plookup-style
837 // argument).
838 std::vector<uint32_t> sorted_list;
839 sorted_list.reserve(list.variable_indices.size());
840 for (const auto variable_index : list.variable_indices) {
841 // note that `field_element` is < 32 bits as the corresponding witness has a non-trivial range-constraint.
842 const auto& field_element = this->get_variable(variable_index);
843 const uint32_t shrinked_value = static_cast<uint32_t>(field_element);
844 sorted_list.emplace_back(shrinked_value);
845 }
846
847#ifdef NO_PAR_ALGOS
848 std::sort(sorted_list.begin(), sorted_list.end());
849#else
850 std::sort(std::execution::par_unseq, sorted_list.begin(), sorted_list.end());
851#endif
852 // list must be padded to a multipe of 4 and larger than 4 (gate_width)
853 constexpr size_t gate_width = NUM_WIRES;
854 size_t padding = (gate_width - (list.variable_indices.size() % gate_width)) % gate_width;
855
856 std::vector<uint32_t> indices;
857 indices.reserve(padding + sorted_list.size());
858
859 if (list.variable_indices.size() <= gate_width) {
860 padding += gate_width;
861 }
862 for (size_t i = 0; i < padding; ++i) {
863 indices.emplace_back(this->zero_idx());
864 }
865 // tag the elements in the sorted_list to apply the multiset-equality check implicit in range-constraints.
866 for (const auto sorted_value : sorted_list) {
867 const uint32_t index = this->add_variable(fr(sorted_value));
868 assign_tag(index, list.tau_tag);
869 indices.emplace_back(index);
870 }
871 // constrain the _sorted_ list: starts at 0, ends at `target_range`, consecutive differences in {0, 1, 2, 3}.
872 create_sort_constraint_with_edges(indices, 0, list.target_range);
873}
874
875template <typename ExecutionTrace> void UltraCircuitBuilder_<ExecutionTrace>::process_range_lists()
876{
877 for (auto& i : range_lists) {
878 process_range_list(i.second);
879 }
880}
881
882template <typename ExecutionTrace>
883void UltraCircuitBuilder_<ExecutionTrace>::enforce_small_deltas(const std::vector<uint32_t>& variable_indices)
884{
885 constexpr size_t gate_width = NUM_WIRES;
886 BB_ASSERT_EQ(variable_indices.size() % gate_width, 0U);
887 this->assert_valid_variables(variable_indices);
888
889 for (size_t i = 0; i < variable_indices.size(); i += gate_width) {
890 blocks.delta_range.populate_wires(
891 variable_indices[i], variable_indices[i + 1], variable_indices[i + 2], variable_indices[i + 3]);
892
893 this->increment_num_gates();
894 blocks.delta_range.q_m().emplace_back(0);
895 blocks.delta_range.q_1().emplace_back(0);
896 blocks.delta_range.q_2().emplace_back(0);
897 blocks.delta_range.q_3().emplace_back(0);
898 blocks.delta_range.q_c().emplace_back(0);
899 blocks.delta_range.q_4().emplace_back(0);
900 blocks.delta_range.set_gate_selector(1);
901 check_selector_length_consistency();
902 }
903 // dummy gate needed because of widget's check of next row
904 create_unconstrained_gate(blocks.delta_range,
905 variable_indices[variable_indices.size() - 1],
906 this->zero_idx(),
907 this->zero_idx(),
908 this->zero_idx());
909}
910
911// useful to put variables in the witness that aren't already used - e.g. the dummy variables of the range constraint in
912// multiples of four
913template <typename ExecutionTrace>
914void UltraCircuitBuilder_<ExecutionTrace>::create_unconstrained_gates(const std::vector<uint32_t>& variable_index)
915{
916 std::vector<uint32_t> padded_list = variable_index;
917 constexpr size_t gate_width = NUM_WIRES;
918 const uint64_t padding = (gate_width - (padded_list.size() % gate_width)) % gate_width;
919 for (uint64_t i = 0; i < padding; ++i) {
920 padded_list.emplace_back(this->zero_idx());
921 }
922 this->assert_valid_variables(variable_index);
923 this->assert_valid_variables(padded_list);
924
925 for (size_t i = 0; i < padded_list.size(); i += gate_width) {
926 create_unconstrained_gate(
927 blocks.arithmetic, padded_list[i], padded_list[i + 1], padded_list[i + 2], padded_list[i + 3]);
928 }
929}
930
931template <typename ExecutionTrace>
933 const std::vector<uint32_t>& variable_indices, const FF& start, const FF& end)
934{
935 // Convenient to assume size is at least 8 (gate_width = 4) for separate gates for start and end conditions
936 constexpr size_t gate_width = NUM_WIRES;
937 BB_ASSERT_EQ(variable_indices.size() % gate_width, 0U);
938 BB_ASSERT_GT(variable_indices.size(), gate_width);
939 this->assert_valid_variables(variable_indices);
940 // only work with the delta_range block. this forces: `w_2 - w_1`, `w_3 - w_2`, `w_4 - w_3`, and `w_1_shift - w_4`
941 // to be in {0, 1, 2, 3}.
942 auto& block = blocks.delta_range;
943
944 // Add an arithmetic gate to ensure the first input is equal to the start value of the range being checked
945 create_add_gate({ variable_indices[0], this->zero_idx(), this->zero_idx(), 1, 0, 0, -start });
946
947 // enforce delta range relation for all rows (there are `variabe_indices.size() / gate_width`). note that there are
948 // at least two rows.
949 for (size_t i = 0; i < variable_indices.size(); i += gate_width) {
950
951 block.populate_wires(
952 variable_indices[i], variable_indices[i + 1], variable_indices[i + 2], variable_indices[i + 3]);
953 this->increment_num_gates();
954 block.q_m().emplace_back(0);
955 block.q_1().emplace_back(0);
956 block.q_2().emplace_back(0);
957 block.q_3().emplace_back(0);
958 block.q_c().emplace_back(0);
959 block.q_4().emplace_back(0);
960 block.set_gate_selector(1);
961 check_selector_length_consistency();
962 }
963
964 // the delta_range constraint has to have access to w_1-shift (it checks that w_1-shift - w_4 is in {0, 1, 2, 3}).
965 // Therefore, we repeat the last element in an unconstrained gate.
966 create_unconstrained_gate(
967 block, variable_indices[variable_indices.size() - 1], this->zero_idx(), this->zero_idx(), this->zero_idx());
968 // arithmetic gate to constrain that `variable_indices[last] == end`, i.e., verify the boundary condition.
969 create_add_gate(
970 { variable_indices[variable_indices.size() - 1], this->zero_idx(), this->zero_idx(), 1, 0, 0, -end });
971}
972
995template <typename ExecutionTrace>
997{
998 auto& block = blocks.memory;
999 block.set_gate_selector(type == MEMORY_SELECTORS::MEM_NONE ? 0 : 1);
1000 switch (type) {
1001 case MEMORY_SELECTORS::ROM_CONSISTENCY_CHECK: {
1002 // Memory read gate used with the sorted list of memory reads.
1003 // Apply sorted memory read checks with the following additional check:
1004 // 1. Assert that if index field across two gates does not change, the value field does not change.
1005 // Used for ROM reads and RAM reads across write/read boundaries
1006 block.q_1().emplace_back(1);
1007 block.q_2().emplace_back(1);
1008 block.q_3().emplace_back(0);
1009 block.q_4().emplace_back(0);
1010 block.q_m().emplace_back(0);
1011 block.q_c().emplace_back(0);
1012 check_selector_length_consistency();
1013 break;
1014 }
1015 case MEMORY_SELECTORS::RAM_CONSISTENCY_CHECK: {
1016 // Memory read gate used with the sorted list of memory reads.
1017 // 1. Validate adjacent index values across 2 gates increases by 0 or 1
1018 // 2. Validate record computation (r = read_write_flag + index * \eta + \timestamp * \eta^2 + value * \eta^3)
1019 // 3. If adjacent index values across 2 gates does not change, and the next gate's read_write_flag is set to
1020 // 'read', validate adjacent values do not change Used for ROM reads and RAM reads across read/write boundaries
1021 block.q_1().emplace_back(0);
1022 block.q_2().emplace_back(0);
1023 block.q_3().emplace_back(1);
1024 block.q_4().emplace_back(0);
1025 block.q_m().emplace_back(0);
1026 block.q_c().emplace_back(0);
1027 check_selector_length_consistency();
1028 break;
1029 }
1030 case MEMORY_SELECTORS::RAM_TIMESTAMP_CHECK: {
1031 // For two adjacent RAM entries that share the same index, validate the timestamp value is monotonically
1032 // increasing
1033 block.q_1().emplace_back(1);
1034 block.q_2().emplace_back(0);
1035 block.q_3().emplace_back(0);
1036 block.q_4().emplace_back(1);
1037 block.q_m().emplace_back(0);
1038 block.q_c().emplace_back(0);
1039 check_selector_length_consistency();
1040 break;
1041 }
1042 case MEMORY_SELECTORS::ROM_READ: {
1043 // Memory read gate for reading memory cells. Also used for the _initialization_ of ROM memory cells.
1044 // Validates record witness computation (r = read_write_flag + index * \eta + timestamp * \eta^2 + value *
1045 // \eta^3)
1046 block.q_1().emplace_back(1);
1047 block.q_2().emplace_back(0);
1048 block.q_3().emplace_back(0);
1049 block.q_4().emplace_back(0);
1050 block.q_m().emplace_back(1); // validate record witness is correctly computed
1051 block.q_c().emplace_back(0); // read/write flag stored in q_c
1052 check_selector_length_consistency();
1053 break;
1054 }
1055 case MEMORY_SELECTORS::RAM_READ: {
1056 // Memory read gate for reading memory cells.
1057 // Validates record witness computation (r = read_write_flag + index * \eta + timestamp * \eta^2 + value *
1058 // \eta^3)
1059 block.q_1().emplace_back(1);
1060 block.q_2().emplace_back(0);
1061 block.q_3().emplace_back(0);
1062 block.q_4().emplace_back(0);
1063 block.q_m().emplace_back(1); // validate record witness is correctly computed
1064 block.q_c().emplace_back(0); // read/write flag stored in q_c
1065 check_selector_length_consistency();
1066 break;
1067 }
1068 case MEMORY_SELECTORS::RAM_WRITE: {
1069 // Memory read gate for writing memory cells.
1070 // Validates record witness computation (r = read_write_flag + index * \eta + timestamp * \eta^2 + value *
1071 // \eta^3)
1072 block.q_1().emplace_back(1);
1073 block.q_2().emplace_back(0);
1074 block.q_3().emplace_back(0);
1075 block.q_4().emplace_back(0);
1076 block.q_m().emplace_back(1); // validate record witness is correctly computed
1077 block.q_c().emplace_back(1); // read/write flag stored in q_c
1078 check_selector_length_consistency();
1079 break;
1080 }
1081 default: {
1082 block.q_1().emplace_back(0);
1083 block.q_2().emplace_back(0);
1084 block.q_3().emplace_back(0);
1085 block.q_4().emplace_back(0);
1086 block.q_m().emplace_back(0);
1087 block.q_c().emplace_back(0);
1088 check_selector_length_consistency();
1089 break;
1090 }
1091 }
1092}
1093
1117template <typename ExecutionTrace>
1119{
1120 auto& block = blocks.nnf;
1121 block.set_gate_selector(type == NNF_SELECTORS::NNF_NONE ? 0 : 1);
1122 switch (type) {
1123 case NNF_SELECTORS::LIMB_ACCUMULATE_1: {
1124 block.q_1().emplace_back(0);
1125 block.q_2().emplace_back(0);
1126 block.q_3().emplace_back(1);
1127 block.q_4().emplace_back(1);
1128 block.q_m().emplace_back(0);
1129 block.q_c().emplace_back(0);
1130 check_selector_length_consistency();
1131 break;
1132 }
1133 case NNF_SELECTORS::LIMB_ACCUMULATE_2: {
1134 block.q_1().emplace_back(0);
1135 block.q_2().emplace_back(0);
1136 block.q_3().emplace_back(1);
1137 block.q_4().emplace_back(0);
1138 block.q_m().emplace_back(1);
1139 block.q_c().emplace_back(0);
1140 check_selector_length_consistency();
1141 break;
1142 }
1143 case NNF_SELECTORS::NON_NATIVE_FIELD_1: {
1144 block.q_1().emplace_back(0);
1145 block.q_2().emplace_back(1);
1146 block.q_3().emplace_back(1);
1147 block.q_4().emplace_back(0);
1148 block.q_m().emplace_back(0);
1149 block.q_c().emplace_back(0);
1150 check_selector_length_consistency();
1151 break;
1152 }
1153 case NNF_SELECTORS::NON_NATIVE_FIELD_2: {
1154 block.q_1().emplace_back(0);
1155 block.q_2().emplace_back(1);
1156 block.q_3().emplace_back(0);
1157 block.q_4().emplace_back(1);
1158 block.q_m().emplace_back(0);
1159 block.q_c().emplace_back(0);
1160 check_selector_length_consistency();
1161 break;
1162 }
1163 case NNF_SELECTORS::NON_NATIVE_FIELD_3: {
1164 block.q_1().emplace_back(0);
1165 block.q_2().emplace_back(1);
1166 block.q_3().emplace_back(0);
1167 block.q_4().emplace_back(0);
1168 block.q_m().emplace_back(1);
1169 block.q_c().emplace_back(0);
1170 check_selector_length_consistency();
1171 break;
1172 }
1173 default: {
1174 block.q_1().emplace_back(0);
1175 block.q_2().emplace_back(0);
1176 block.q_3().emplace_back(0);
1177 block.q_4().emplace_back(0);
1178 block.q_m().emplace_back(0);
1179 block.q_c().emplace_back(0);
1180 check_selector_length_consistency();
1181 break;
1182 }
1183 }
1184}
1185
1196template <typename ExecutionTrace>
1198 const uint32_t hi_idx,
1199 const size_t lo_limb_bits,
1200 const size_t hi_limb_bits,
1201 std::string const& msg)
1202{
1203 // Validate limbs are <= 70 bits. If limbs are larger we require more witnesses and cannot use our limb accumulation
1204 // custom gate
1205 BB_ASSERT_LTE(lo_limb_bits, 14U * 5U);
1206 BB_ASSERT_LTE(hi_limb_bits, 14U * 5U);
1207
1208 // If the value is larger than the range, we log the error in builder
1209 const bool is_lo_out_of_range = (uint256_t(this->get_variable(lo_idx)) >= (uint256_t(1) << lo_limb_bits));
1210 if (is_lo_out_of_range && !this->failed()) {
1211 this->failure(msg + ": lo limb.");
1212 }
1213 const bool is_hi_out_of_range = (uint256_t(this->get_variable(hi_idx)) >= (uint256_t(1) << hi_limb_bits));
1214 if (is_hi_out_of_range && !this->failed()) {
1215 this->failure(msg + ": hi limb.");
1216 }
1217
1218 // Sometimes we try to use limbs that are too large. It's easier to catch this issue here
1219 const auto get_sublimbs = [&](const uint32_t& limb_idx, const std::array<uint64_t, 5>& sublimb_masks) {
1220 const uint256_t limb = this->get_variable(limb_idx);
1221 // we can use constant 2^14 - 1 mask here. If the sublimb value exceeds the expected value then witness will
1222 // fail the range check below
1223 // We also use zero_idx to substitute variables that should be zero
1224 constexpr uint256_t MAX_SUBLIMB_MASK = (uint256_t(1) << 14) - 1;
1225 std::array<uint32_t, 5> sublimb_indices;
1226 sublimb_indices[0] = sublimb_masks[0] != 0 ? this->add_variable(fr(limb & MAX_SUBLIMB_MASK)) : this->zero_idx();
1227 sublimb_indices[1] =
1228 sublimb_masks[1] != 0 ? this->add_variable(fr((limb >> 14) & MAX_SUBLIMB_MASK)) : this->zero_idx();
1229 sublimb_indices[2] =
1230 sublimb_masks[2] != 0 ? this->add_variable(fr((limb >> 28) & MAX_SUBLIMB_MASK)) : this->zero_idx();
1231 sublimb_indices[3] =
1232 sublimb_masks[3] != 0 ? this->add_variable(fr((limb >> 42) & MAX_SUBLIMB_MASK)) : this->zero_idx();
1233 sublimb_indices[4] =
1234 sublimb_masks[4] != 0 ? this->add_variable(fr((limb >> 56) & MAX_SUBLIMB_MASK)) : this->zero_idx();
1235 return sublimb_indices;
1236 };
1237
1238 const auto get_limb_masks = [](size_t limb_bits) {
1239 std::array<uint64_t, 5> sublimb_masks;
1240 sublimb_masks[0] = limb_bits >= 14 ? 14 : limb_bits;
1241 sublimb_masks[1] = limb_bits >= 28 ? 14 : (limb_bits > 14 ? limb_bits - 14 : 0);
1242 sublimb_masks[2] = limb_bits >= 42 ? 14 : (limb_bits > 28 ? limb_bits - 28 : 0);
1243 sublimb_masks[3] = limb_bits >= 56 ? 14 : (limb_bits > 42 ? limb_bits - 42 : 0);
1244 sublimb_masks[4] = (limb_bits > 56 ? limb_bits - 56 : 0);
1245
1246 for (auto& mask : sublimb_masks) {
1247 mask = (1ULL << mask) - 1ULL;
1248 }
1249 return sublimb_masks;
1250 };
1251
1252 const auto lo_masks = get_limb_masks(lo_limb_bits);
1253 const auto hi_masks = get_limb_masks(hi_limb_bits);
1254 const std::array<uint32_t, 5> lo_sublimbs = get_sublimbs(lo_idx, lo_masks);
1255 const std::array<uint32_t, 5> hi_sublimbs = get_sublimbs(hi_idx, hi_masks);
1256
1257 blocks.nnf.populate_wires(lo_sublimbs[0], lo_sublimbs[1], lo_sublimbs[2], lo_idx);
1258 blocks.nnf.populate_wires(lo_sublimbs[3], lo_sublimbs[4], hi_sublimbs[0], hi_sublimbs[1]);
1259 blocks.nnf.populate_wires(hi_sublimbs[2], hi_sublimbs[3], hi_sublimbs[4], hi_idx);
1260
1261 apply_nnf_selectors(NNF_SELECTORS::LIMB_ACCUMULATE_1);
1262 apply_nnf_selectors(NNF_SELECTORS::LIMB_ACCUMULATE_2);
1263 apply_nnf_selectors(NNF_SELECTORS::NNF_NONE);
1264 this->increment_num_gates(3);
1265
1266 for (size_t i = 0; i < 5; i++) {
1267 if (lo_masks[i] != 0) {
1268 create_small_range_constraint(
1269 lo_sublimbs[i], lo_masks[i], "ultra_circuit_builder: sublimb of low too large");
1270 }
1271 if (hi_masks[i] != 0) {
1272 create_small_range_constraint(
1273 hi_sublimbs[i], hi_masks[i], "ultra_circuit_builder: sublimb of hi too large");
1274 }
1275 }
1276};
1277
1293template <typename ExecutionTrace>
1296{
1297 const auto [a0, a1, a2, a3] = std::array{ this->get_variable(input.a[0]),
1298 this->get_variable(input.a[1]),
1299 this->get_variable(input.a[2]),
1300 this->get_variable(input.a[3]) };
1301 const auto [b0, b1, b2, b3] = std::array{ this->get_variable(input.b[0]),
1302 this->get_variable(input.b[1]),
1303 this->get_variable(input.b[2]),
1304 this->get_variable(input.b[3]) };
1305 const auto [q0, q1, q2, q3] = std::array{ this->get_variable(input.q[0]),
1306 this->get_variable(input.q[1]),
1307 this->get_variable(input.q[2]),
1308 this->get_variable(input.q[3]) };
1309 const auto [r0, r1, r2, r3] = std::array{ this->get_variable(input.r[0]),
1310 this->get_variable(input.r[1]),
1311 this->get_variable(input.r[2]),
1312 this->get_variable(input.r[3]) };
1313 const auto& p_neg = input.neg_modulus;
1314
1315 constexpr FF LIMB_SHIFT = uint256_t(1) << DEFAULT_NON_NATIVE_FIELD_LIMB_BITS;
1316 constexpr FF LIMB_RSHIFT = FF(1) / FF(uint256_t(1) << DEFAULT_NON_NATIVE_FIELD_LIMB_BITS);
1317 constexpr FF LIMB_RSHIFT_2 = FF(1) / FF(uint256_t(1) << (2 * DEFAULT_NON_NATIVE_FIELD_LIMB_BITS));
1318
1319 // lo_0 = (a0·b0 - r0) + (a1·b0 + a0·b1)·2^L
1320 FF lo_0 = (a0 * b0 - r0) + (a1 * b0 + a0 * b1) * LIMB_SHIFT;
1321 // lo_1 = (lo_0 + q0·p0' + (q1·p0' + q0·p1' - r1)·2^L) / 2^2L
1322 FF lo_1 = (lo_0 + q0 * p_neg[0] + (q1 * p_neg[0] + q0 * p_neg[1] - r1) * LIMB_SHIFT) * LIMB_RSHIFT_2;
1323
1324 // hi_0 = (a2·b0 + a0·b2) + (a0·b3 + a3·b0 - r3)·2^L
1325 FF hi_0 = (a2 * b0 + a0 * b2) + (a0 * b3 + a3 * b0 - r3) * LIMB_SHIFT;
1326 // hi_1 = hi_0 + (a1·b1 - r2) + (a1·b2 + a2·b1)·2^L
1327 FF hi_1 = hi_0 + (a1 * b1 - r2) + (a1 * b2 + a2 * b1) * LIMB_SHIFT;
1328 // hi_2 = hi_1 + lo_1 + q2·p0' + (q3·p0' + q2·p1')·2^L
1329 FF hi_2 = hi_1 + lo_1 + q2 * p_neg[0] + (q3 * p_neg[0] + q2 * p_neg[1]) * LIMB_SHIFT;
1330 // hi_3 = (hi_2 + q0·p2' + q1·p1' + (q0·p3' + q1·p2')·2^L) / 2^2L
1331 FF hi_3 = (hi_2 + q0 * p_neg[2] + q1 * p_neg[1] + (q0 * p_neg[3] + q1 * p_neg[2]) * LIMB_SHIFT) * LIMB_RSHIFT_2;
1332
1333 const uint32_t lo_0_idx = this->add_variable(lo_0);
1334 const uint32_t lo_1_idx = this->add_variable(lo_1);
1335 const uint32_t hi_0_idx = this->add_variable(hi_0);
1336 const uint32_t hi_1_idx = this->add_variable(hi_1);
1337 const uint32_t hi_2_idx = this->add_variable(hi_2);
1338 const uint32_t hi_3_idx = this->add_variable(hi_3);
1339
1340 // Gate 1: big_add_gate to validate lo_1
1341 // (lo_0 + q_0(p_0 + p_1*2^b) + q_1(p_0*2^b) - (r_1)2^b)2^-2b - lo_1 = 0
1342 // This constraint requires two rows in the trace: an arithmetic gate plus an unconstrained arithmetic gate
1343 // containing lo_0 in wire 4 so that the previous gate can access it via shifts. (We cannot use the next nnf gate
1344 // for this purpose since our trace is sorted by gate type).
1345 create_big_add_gate({ input.q[0],
1346 input.q[1],
1347 input.r[1],
1348 lo_1_idx,
1349 input.neg_modulus[0] + input.neg_modulus[1] * LIMB_SHIFT,
1350 input.neg_modulus[0] * LIMB_SHIFT,
1351 -LIMB_SHIFT,
1352 -LIMB_SHIFT.sqr(),
1353 0 },
1354 /*include_next_gate_w_4*/ true);
1355 // Gate 2: unconstrained gate to provide lo_0 via w_4_shift for gate 1
1356 create_unconstrained_gate(blocks.arithmetic, this->zero_idx(), this->zero_idx(), this->zero_idx(), lo_0_idx);
1357
1358 //
1359 // a = (a3 || a2 || a1 || a0) = (a3 * 2^b + a2) * 2^b + (a1 * 2^b + a0)
1360 // b = (b3 || b2 || b1 || b0) = (b3 * 2^b + b2) * 2^b + (b1 * 2^b + b0)
1361 //
1362 // Gate 3: NNF gate to check if lo_0 was computed correctly
1363 // The gate structure for the nnf gates is as follows:
1364 //
1365 // | a1 | b1 | r0 | lo_0 | <-- Gate 3: check lo_0
1366 // | a0 | b0 | a3 | b3 |
1367 // | a2 | b2 | r3 | hi_0 |
1368 // | a1 | b1 | r2 | hi_1 |
1369 //
1370 // Constraint: lo_0 = (a1 * b0 + a0 * b1) * 2^b + (a0 * b0) - r0
1371 // w4 = (w1 * w'2 + w'1 * w2) * 2^b + (w'1 * w'2) - w3
1372 //
1373 blocks.nnf.populate_wires(input.a[1], input.b[1], input.r[0], lo_0_idx);
1374 apply_nnf_selectors(NNF_SELECTORS::NON_NATIVE_FIELD_1);
1375 this->increment_num_gates();
1376
1377 //
1378 // Gate 4: NNF gate to check if hi_0 was computed correctly
1379 //
1380 // | a1 | b1 | r0 | lo_0 |
1381 // | a0 | b0 | a3 | b3 | <-- Gate 4: check hi_0
1382 // | a2 | b2 | r3 | hi_0 |
1383 // | a1 | b1 | r2 | hi_1 |
1384 //
1385 // Constraint: hi_0 = (a0 * b3 + a3 * b0 - r3) * 2^b + (a0 * b2 + a2 * b0)
1386 // w'4 = (w1 * w4 + w2 * w3 - w'3) * 2^b + (w1 * w'2 + w'1 * w2)
1387 //
1388 blocks.nnf.populate_wires(input.a[0], input.b[0], input.a[3], input.b[3]);
1389 apply_nnf_selectors(NNF_SELECTORS::NON_NATIVE_FIELD_2);
1390 this->increment_num_gates();
1391
1392 //
1393 // Gate 5: NNF gate to check if hi_1 was computed correctly
1394 //
1395 // | a1 | b1 | r0 | lo_0 |
1396 // | a0 | b0 | a3 | b3 |
1397 // | a2 | b2 | r3 | hi_0 | <-- Gate 5: check hi_1
1398 // | a1 | b1 | r2 | hi_1 |
1399 //
1400 // Constraint: hi_1 = hi_0 + (a2 * b1 + a1 * b2) * 2^b + (a1 * b1) - r2
1401 // w'4 = w4 + (w1 * w'2 + w'1 * w2) * 2^b + (w'1 * w'2) - w'3
1402 //
1403 blocks.nnf.populate_wires(input.a[2], input.b[2], input.r[3], hi_0_idx);
1404 apply_nnf_selectors(NNF_SELECTORS::NON_NATIVE_FIELD_3);
1405 this->increment_num_gates();
1406
1407 //
1408 // Gate 6: NNF gate with no constraints (q_nnf=0, truly unconstrained)
1409 // Provides values a[1], b[1], r[2], hi_1 to Gate 5 via shifts (w'1, w'2, w'3, w'4)
1410 //
1411 blocks.nnf.populate_wires(input.a[1], input.b[1], input.r[2], hi_1_idx);
1412 apply_nnf_selectors(NNF_SELECTORS::NNF_NONE);
1413 this->increment_num_gates();
1414
1415 //
1416 // Gate 7: big_add_gate to validate hi_2
1417 //
1418 // hi_2 - hi_1 - lo_1 - q[2](p[1].2^b + p[0]) - q[3](p[0].2^b) = 0
1419 //
1420 create_big_add_gate(
1421 {
1422 input.q[2],
1423 input.q[3],
1424 lo_1_idx,
1425 hi_1_idx,
1426 -input.neg_modulus[1] * LIMB_SHIFT - input.neg_modulus[0],
1427 -input.neg_modulus[0] * LIMB_SHIFT,
1428 -1,
1429 -1,
1430 0,
1431 },
1432 /*include_next_gate_w_4*/ true);
1433
1434 //
1435 // Gate 8: big_add_gate to validate hi_3 (provides hi_2 in w_4 for gate 7)
1436 //
1437 // hi_3 - (hi_2 - q[0](p[3].2^b + p[2]) - q[1](p[2].2^b + p[1])).2^-2b = 0
1438 //
1439 create_big_add_gate({
1440 hi_3_idx,
1441 input.q[0],
1442 input.q[1],
1443 hi_2_idx,
1444 -1,
1445 input.neg_modulus[3] * LIMB_RSHIFT + input.neg_modulus[2] * LIMB_RSHIFT_2,
1446 input.neg_modulus[2] * LIMB_RSHIFT + input.neg_modulus[1] * LIMB_RSHIFT_2,
1447 LIMB_RSHIFT_2,
1448 0,
1449 });
1450
1451 return std::array<uint32_t, 2>{ lo_1_idx, hi_3_idx };
1452}
1453
1461{
1462 for (size_t i = 0; i < cached_partial_non_native_field_multiplications.size(); ++i) {
1463 auto& c = cached_partial_non_native_field_multiplications[i];
1464 for (size_t j = 0; j < c.a.size(); ++j) {
1465 c.a[j] = this->real_variable_index[c.a[j]];
1466 c.b[j] = this->real_variable_index[c.b[j]];
1467 }
1468 }
1469 cached_partial_non_native_field_multiplication::deduplicate(cached_partial_non_native_field_multiplications, this);
1470
1471 // iterate over the cached items and create constraints
1472 for (const auto& input : cached_partial_non_native_field_multiplications) {
1473
1474 blocks.nnf.populate_wires(input.a[1], input.b[1], this->zero_idx(), input.lo_0);
1475 apply_nnf_selectors(NNF_SELECTORS::NON_NATIVE_FIELD_1);
1476 this->increment_num_gates();
1477
1478 blocks.nnf.populate_wires(input.a[0], input.b[0], input.a[3], input.b[3]);
1479 apply_nnf_selectors(NNF_SELECTORS::NON_NATIVE_FIELD_2);
1480 this->increment_num_gates();
1481
1482 blocks.nnf.populate_wires(input.a[2], input.b[2], this->zero_idx(), input.hi_0);
1483 apply_nnf_selectors(NNF_SELECTORS::NON_NATIVE_FIELD_3);
1484 this->increment_num_gates();
1485
1486 blocks.nnf.populate_wires(input.a[1], input.b[1], this->zero_idx(), input.hi_1);
1487 apply_nnf_selectors(NNF_SELECTORS::NNF_NONE);
1488 this->increment_num_gates();
1489 }
1490}
1491
1498template <typename ExecutionTrace>
1501{
1503 this->get_variable(input.a[0]),
1504 this->get_variable(input.a[1]),
1505 this->get_variable(input.a[2]),
1506 this->get_variable(input.a[3]),
1507 };
1509 this->get_variable(input.b[0]),
1510 this->get_variable(input.b[1]),
1511 this->get_variable(input.b[2]),
1512 this->get_variable(input.b[3]),
1513 };
1514
1515 constexpr FF LIMB_SHIFT = uint256_t(1) << DEFAULT_NON_NATIVE_FIELD_LIMB_BITS;
1516
1517 FF lo_0 = a[0] * b[0] + ((a[1] * b[0] + a[0] * b[1]) * LIMB_SHIFT);
1518 FF hi_0 = a[2] * b[0] + a[0] * b[2] + ((a[0] * b[3] + a[3] * b[0]) * LIMB_SHIFT);
1519 FF hi_1 = hi_0 + a[1] * b[1] + ((a[1] * b[2] + a[2] * b[1]) * LIMB_SHIFT);
1520
1521 const uint32_t lo_0_idx = this->add_variable(lo_0);
1522 const uint32_t hi_0_idx = this->add_variable(hi_0);
1523 const uint32_t hi_1_idx = this->add_variable(hi_1);
1524
1525 // Add witnesses into the multiplication cache (duplicates removed during circuit finalization)
1527 .a = input.a,
1528 .b = input.b,
1529 .lo_0 = lo_0_idx,
1530 .hi_0 = hi_0_idx,
1531 .hi_1 = hi_1_idx,
1532 };
1533 cached_partial_non_native_field_multiplications.emplace_back(cache_entry);
1534 return std::array<uint32_t, 2>{ lo_0_idx, hi_1_idx };
1535}
1536
1542template <typename ExecutionTrace>
1545{
1546 const uint32_t& x_0 = std::get<0>(limb0).first;
1547 const uint32_t& x_1 = std::get<0>(limb1).first;
1548 const uint32_t& x_2 = std::get<0>(limb2).first;
1549 const uint32_t& x_3 = std::get<0>(limb3).first;
1550 const uint32_t& x_p = std::get<0>(limbp);
1551
1552 const FF& x_mulconst0 = std::get<0>(limb0).second;
1553 const FF& x_mulconst1 = std::get<0>(limb1).second;
1554 const FF& x_mulconst2 = std::get<0>(limb2).second;
1555 const FF& x_mulconst3 = std::get<0>(limb3).second;
1556
1557 const uint32_t& y_0 = std::get<1>(limb0).first;
1558 const uint32_t& y_1 = std::get<1>(limb1).first;
1559 const uint32_t& y_2 = std::get<1>(limb2).first;
1560 const uint32_t& y_3 = std::get<1>(limb3).first;
1561 const uint32_t& y_p = std::get<1>(limbp);
1562
1563 const FF& y_mulconst0 = std::get<1>(limb0).second;
1564 const FF& y_mulconst1 = std::get<1>(limb1).second;
1565 const FF& y_mulconst2 = std::get<1>(limb2).second;
1566 const FF& y_mulconst3 = std::get<1>(limb3).second;
1567
1568 // constant additive terms
1569 const FF& addconst0 = std::get<2>(limb0);
1570 const FF& addconst1 = std::get<2>(limb1);
1571 const FF& addconst2 = std::get<2>(limb2);
1572 const FF& addconst3 = std::get<2>(limb3);
1573 const FF& addconstp = std::get<2>(limbp);
1574
1575 // get value of result limbs
1576 const FF z_0value = (this->get_variable(x_0) * x_mulconst0) + (this->get_variable(y_0) * y_mulconst0) + addconst0;
1577 const FF z_1value = (this->get_variable(x_1) * x_mulconst1) + (this->get_variable(y_1) * y_mulconst1) + addconst1;
1578 const FF z_2value = (this->get_variable(x_2) * x_mulconst2) + (this->get_variable(y_2) * y_mulconst2) + addconst2;
1579 const FF z_3value = (this->get_variable(x_3) * x_mulconst3) + (this->get_variable(y_3) * y_mulconst3) + addconst3;
1580 const FF z_pvalue = this->get_variable(x_p) + this->get_variable(y_p) + addconstp;
1581
1582 const uint32_t z_0 = this->add_variable(z_0value);
1583 const uint32_t z_1 = this->add_variable(z_1value);
1584 const uint32_t z_2 = this->add_variable(z_2value);
1585 const uint32_t z_3 = this->add_variable(z_3value);
1586 const uint32_t z_p = this->add_variable(z_pvalue);
1587
1609 auto& block = blocks.arithmetic;
1610 block.populate_wires(y_p, x_0, y_0, x_p);
1611 block.populate_wires(z_p, x_1, y_1, z_0);
1612 block.populate_wires(x_2, y_2, z_2, z_1);
1613 block.populate_wires(x_3, y_3, z_3, this->zero_idx());
1614
1615 // When q_arith == 3, w_4_shift is scaled by 2 (see ArithmeticRelation for details). Therefore, for consistency we
1616 // also scale each linear term by this factor of 2 so that the constraint is effectively:
1617 // (q_l * w_1) + (q_r * w_2) + (q_o * w_3) + (q_4 * w_4) + q_c + w_4_shift = 0
1618 const FF linear_term_scale_factor = 2;
1619 block.q_m().emplace_back(addconstp);
1620 block.q_1().emplace_back(0);
1621 block.q_2().emplace_back(-x_mulconst0 * linear_term_scale_factor);
1622 block.q_3().emplace_back(-y_mulconst0 * linear_term_scale_factor);
1623 block.q_4().emplace_back(0);
1624 block.q_c().emplace_back(-addconst0 * linear_term_scale_factor);
1625 block.set_gate_selector(3);
1626
1627 block.q_m().emplace_back(0);
1628 block.q_1().emplace_back(0);
1629 block.q_2().emplace_back(-x_mulconst1);
1630 block.q_3().emplace_back(-y_mulconst1);
1631 block.q_4().emplace_back(0);
1632 block.q_c().emplace_back(-addconst1);
1633 block.set_gate_selector(2);
1634
1635 block.q_m().emplace_back(0);
1636 block.q_1().emplace_back(-x_mulconst2);
1637 block.q_2().emplace_back(-y_mulconst2);
1638 block.q_3().emplace_back(1);
1639 block.q_4().emplace_back(0);
1640 block.q_c().emplace_back(-addconst2);
1641 block.set_gate_selector(1);
1642
1643 block.q_m().emplace_back(0);
1644 block.q_1().emplace_back(-x_mulconst3);
1645 block.q_2().emplace_back(-y_mulconst3);
1646 block.q_3().emplace_back(1);
1647 block.q_4().emplace_back(0);
1648 block.q_c().emplace_back(-addconst3);
1649 block.set_gate_selector(1);
1650
1651 check_selector_length_consistency();
1652
1653 this->increment_num_gates(4);
1655 z_0, z_1, z_2, z_3, z_p,
1656 };
1657}
1658
1664template <typename ExecutionTrace>
1667{
1668 const uint32_t& x_0 = std::get<0>(limb0).first;
1669 const uint32_t& x_1 = std::get<0>(limb1).first;
1670 const uint32_t& x_2 = std::get<0>(limb2).first;
1671 const uint32_t& x_3 = std::get<0>(limb3).first;
1672 const uint32_t& x_p = std::get<0>(limbp);
1673
1674 const FF& x_mulconst0 = std::get<0>(limb0).second;
1675 const FF& x_mulconst1 = std::get<0>(limb1).second;
1676 const FF& x_mulconst2 = std::get<0>(limb2).second;
1677 const FF& x_mulconst3 = std::get<0>(limb3).second;
1678
1679 const uint32_t& y_0 = std::get<1>(limb0).first;
1680 const uint32_t& y_1 = std::get<1>(limb1).first;
1681 const uint32_t& y_2 = std::get<1>(limb2).first;
1682 const uint32_t& y_3 = std::get<1>(limb3).first;
1683 const uint32_t& y_p = std::get<1>(limbp);
1684
1685 const FF& y_mulconst0 = std::get<1>(limb0).second;
1686 const FF& y_mulconst1 = std::get<1>(limb1).second;
1687 const FF& y_mulconst2 = std::get<1>(limb2).second;
1688 const FF& y_mulconst3 = std::get<1>(limb3).second;
1689
1690 // constant additive terms
1691 const FF& addconst0 = std::get<2>(limb0);
1692 const FF& addconst1 = std::get<2>(limb1);
1693 const FF& addconst2 = std::get<2>(limb2);
1694 const FF& addconst3 = std::get<2>(limb3);
1695 const FF& addconstp = std::get<2>(limbp);
1696
1697 // get value of result limbs
1698 const FF z_0value = (this->get_variable(x_0) * x_mulconst0) - (this->get_variable(y_0) * y_mulconst0) + addconst0;
1699 const FF z_1value = (this->get_variable(x_1) * x_mulconst1) - (this->get_variable(y_1) * y_mulconst1) + addconst1;
1700 const FF z_2value = (this->get_variable(x_2) * x_mulconst2) - (this->get_variable(y_2) * y_mulconst2) + addconst2;
1701 const FF z_3value = (this->get_variable(x_3) * x_mulconst3) - (this->get_variable(y_3) * y_mulconst3) + addconst3;
1702 const FF z_pvalue = this->get_variable(x_p) - this->get_variable(y_p) + addconstp;
1703
1704 const uint32_t z_0 = this->add_variable(z_0value);
1705 const uint32_t z_1 = this->add_variable(z_1value);
1706 const uint32_t z_2 = this->add_variable(z_2value);
1707 const uint32_t z_3 = this->add_variable(z_3value);
1708 const uint32_t z_p = this->add_variable(z_pvalue);
1709
1734 auto& block = blocks.arithmetic;
1735 block.populate_wires(y_p, x_0, y_0, z_p);
1736 block.populate_wires(x_p, x_1, y_1, z_0);
1737 block.populate_wires(x_2, y_2, z_2, z_1);
1738 block.populate_wires(x_3, y_3, z_3, this->zero_idx());
1739
1740 // When q_arith == 3, w_4_shift is scaled by 2 (see ArithmeticRelation for details). Therefore, for consistency we
1741 // also scale each linear term by this factor of 2 so that the constraint is effectively:
1742 // (q_l * w_1) + (q_r * w_2) + (q_o * w_3) + (q_4 * w_4) + q_c + w_4_shift = 0
1743 const FF linear_term_scale_factor = 2;
1744 block.q_m().emplace_back(-addconstp);
1745 block.q_1().emplace_back(0);
1746 block.q_2().emplace_back(-x_mulconst0 * linear_term_scale_factor);
1747 block.q_3().emplace_back(y_mulconst0 * linear_term_scale_factor);
1748 block.q_4().emplace_back(0);
1749 block.q_c().emplace_back(-addconst0 * linear_term_scale_factor);
1750 block.set_gate_selector(3);
1751
1752 block.q_m().emplace_back(0);
1753 block.q_1().emplace_back(0);
1754 block.q_2().emplace_back(-x_mulconst1);
1755 block.q_3().emplace_back(y_mulconst1);
1756 block.q_4().emplace_back(0);
1757 block.q_c().emplace_back(-addconst1);
1758 block.set_gate_selector(2);
1759
1760 block.q_m().emplace_back(0);
1761 block.q_1().emplace_back(-x_mulconst2);
1762 block.q_2().emplace_back(y_mulconst2);
1763 block.q_3().emplace_back(1);
1764 block.q_4().emplace_back(0);
1765 block.q_c().emplace_back(-addconst2);
1766 block.set_gate_selector(1);
1767
1768 block.q_m().emplace_back(0);
1769 block.q_1().emplace_back(-x_mulconst3);
1770 block.q_2().emplace_back(y_mulconst3);
1771 block.q_3().emplace_back(1);
1772 block.q_4().emplace_back(0);
1773 block.q_c().emplace_back(-addconst3);
1774 block.set_gate_selector(1);
1775
1776 check_selector_length_consistency();
1777
1778 this->increment_num_gates(4);
1780 z_0, z_1, z_2, z_3, z_p,
1781 };
1782}
1783
1793template <typename ExecutionTrace>
1795{
1796 return this->rom_ram_logic.create_ROM_array(array_size);
1797}
1798
1808template <typename ExecutionTrace>
1810{
1811 return this->rom_ram_logic.create_RAM_array(array_size);
1812}
1813
1821template <typename ExecutionTrace>
1823 const size_t index_value,
1824 const uint32_t value_witness)
1825{
1826 this->rom_ram_logic.init_RAM_element(this, ram_id, index_value, value_witness);
1827}
1828
1829template <typename ExecutionTrace>
1830uint32_t UltraCircuitBuilder_<ExecutionTrace>::read_RAM_array(const size_t ram_id, const uint32_t index_witness)
1831{
1832 return this->rom_ram_logic.read_RAM_array(this, ram_id, index_witness);
1833}
1834
1835template <typename ExecutionTrace>
1837 const uint32_t index_witness,
1838 const uint32_t value_witness)
1839{
1840 this->rom_ram_logic.write_RAM_array(this, ram_id, index_witness, value_witness);
1841}
1842
1858template <typename ExecutionTrace>
1860 const size_t index_value,
1861 const uint32_t value_witness)
1862{
1863 this->rom_ram_logic.set_ROM_element(this, rom_id, index_value, value_witness);
1864}
1865
1873template <typename ExecutionTrace>
1875 const size_t index_value,
1876 const std::array<uint32_t, 2>& value_witnesses)
1877{
1878 this->rom_ram_logic.set_ROM_element_pair(this, rom_id, index_value, value_witnesses);
1879}
1880
1888template <typename ExecutionTrace>
1889uint32_t UltraCircuitBuilder_<ExecutionTrace>::read_ROM_array(const size_t rom_id, const uint32_t index_witness)
1890{
1891 return this->rom_ram_logic.read_ROM_array(this, rom_id, index_witness);
1892}
1893
1901template <typename ExecutionTrace>
1902std::array<uint32_t, 2> UltraCircuitBuilder_<ExecutionTrace>::read_ROM_array_pair(const size_t rom_id,
1903 const uint32_t index_witness)
1904{
1905 return this->rom_ram_logic.read_ROM_array_pair(this, rom_id, index_witness);
1906}
1907
1911template <typename FF>
1913{
1914 auto& block = this->blocks.poseidon2_external;
1915 block.populate_wires(in.a, in.b, in.c, in.d);
1916 block.q_m().emplace_back(0);
1920 block.q_c().emplace_back(0);
1922 block.set_gate_selector(1);
1923 this->check_selector_length_consistency();
1924 this->increment_num_gates();
1925}
1926
1930template <typename FF>
1932{
1933 auto& block = this->blocks.poseidon2_internal;
1934 block.populate_wires(in.a, in.b, in.c, in.d);
1935 block.q_m().emplace_back(0);
1937 block.q_2().emplace_back(0);
1938 block.q_3().emplace_back(0);
1939 block.q_c().emplace_back(0);
1940 block.q_4().emplace_back(0);
1941 block.set_gate_selector(1);
1942 this->check_selector_length_consistency();
1943 this->increment_num_gates();
1944}
1945
1952template <typename ExecutionTrace> msgpack::sbuffer UltraCircuitBuilder_<ExecutionTrace>::export_circuit()
1953{
1954 // You should not name `zero` by yourself
1955 // but it will be rewritten anyway
1956 auto first_zero_idx = this->get_first_variable_in_class(this->zero_idx());
1957 if (!this->variable_names.contains(first_zero_idx)) {
1958 this->set_variable_name(this->zero_idx(), "zero");
1959 } else {
1960 this->variable_names[first_zero_idx] = "zero";
1961 }
1962 using base = CircuitBuilderBase<FF>;
1964
1965 std::array<uint64_t, 4> modulus = {
1966 FF::Params::modulus_0, FF::Params::modulus_1, FF::Params::modulus_2, FF::Params::modulus_3
1967 };
1968 std::stringstream buf;
1969 buf << std::hex << std::setfill('0') << std::setw(16) << modulus[3] << std::setw(16) << modulus[2] << std::setw(16)
1970 << modulus[1] << std::setw(16) << modulus[0];
1971
1972 cir.modulus = buf.str();
1973
1974 for (uint32_t i = 0; i < this->num_public_inputs(); i++) {
1975 cir.public_inps.push_back(this->real_variable_index[this->public_inputs()[i]]);
1976 }
1977
1978 for (auto& tup : base::variable_names) {
1979 cir.vars_of_interest.insert({ this->real_variable_index[tup.first], tup.second });
1980 }
1981
1982 for (const auto& var : this->get_variables()) {
1983 cir.variables.push_back(var);
1984 }
1985
1986 FF curve_b;
1987 if constexpr (FF::modulus == bb::fq::modulus) {
1988 curve_b = bb::g1::curve_b;
1989 } else if constexpr (FF::modulus == grumpkin::fq::modulus) {
1990 curve_b = grumpkin::g1::curve_b;
1991 } else {
1992 curve_b = 0;
1993 }
1994
1995 for (auto& block : blocks.get()) {
1996 std::vector<std::vector<FF>> block_selectors;
1998 for (size_t idx = 0; idx < block.size(); ++idx) {
1999 std::vector<FF> tmp_sel = { block.q_m()[idx],
2000 block.q_1()[idx],
2001 block.q_2()[idx],
2002 block.q_3()[idx],
2003 block.q_4()[idx],
2004 block.q_c()[idx],
2005 block.q_arith()[idx],
2006 block.q_delta_range()[idx],
2007 block.q_elliptic()[idx],
2008 block.q_memory()[idx],
2009 block.q_nnf()[idx],
2010 block.q_lookup()[idx],
2011 curve_b };
2012
2013 std::vector<uint32_t> tmp_w = {
2014 this->real_variable_index[block.w_l()[idx]],
2015 this->real_variable_index[block.w_r()[idx]],
2016 this->real_variable_index[block.w_o()[idx]],
2017 this->real_variable_index[block.w_4()[idx]],
2018 };
2019
2020 if (idx < block.size() - 1) {
2021 tmp_w.push_back(block.w_l()[idx + 1]);
2022 tmp_w.push_back(block.w_r()[idx + 1]);
2023 tmp_w.push_back(block.w_o()[idx + 1]);
2024 tmp_w.push_back(block.w_4()[idx + 1]);
2025 } else {
2026 tmp_w.push_back(0);
2027 tmp_w.push_back(0);
2028 tmp_w.push_back(0);
2029 tmp_w.push_back(0);
2030 }
2031
2032 block_selectors.push_back(tmp_sel);
2033 block_wires.push_back(tmp_w);
2034 }
2035 cir.selectors.push_back(block_selectors);
2036 cir.wires.push_back(block_wires);
2037 }
2038
2039 cir.real_variable_index = this->real_variable_index;
2040
2041 for (const auto& table : this->lookup_tables) {
2042 const FF table_index(table.table_index);
2043 info("Table no: ", table.table_index);
2044 std::vector<std::vector<FF>> tmp_table;
2045 for (size_t i = 0; i < table.size(); ++i) {
2046 tmp_table.push_back({ table.column_1[i], table.column_2[i], table.column_3[i] });
2047 }
2048 cir.lookup_tables.push_back(tmp_table);
2049 }
2050
2051 cir.real_variable_tags = this->real_variable_tags;
2052
2053 for (const auto& list : range_lists) {
2054 cir.range_tags[list.second.range_tag] = list.first;
2055 }
2056
2057 for (auto& rom_table : this->rom_ram_logic.rom_arrays) {
2058 std::sort(rom_table.records.begin(), rom_table.records.end());
2059
2061 table.reserve(rom_table.records.size());
2062 for (const auto& rom_entry : rom_table.records) {
2063 table.push_back({
2064 this->real_variable_index[rom_entry.index_witness],
2065 this->real_variable_index[rom_entry.value_column1_witness],
2066 this->real_variable_index[rom_entry.value_column2_witness],
2067 });
2068 }
2069 cir.rom_records.push_back(table);
2070 cir.rom_states.push_back(rom_table.state);
2071 }
2072
2073 for (auto& ram_table : this->rom_ram_logic.ram_arrays) {
2074 std::sort(ram_table.records.begin(), ram_table.records.end());
2075
2077 table.reserve(ram_table.records.size());
2078 for (const auto& ram_entry : ram_table.records) {
2079 table.push_back({ this->real_variable_index[ram_entry.index_witness],
2080 this->real_variable_index[ram_entry.value_witness],
2081 this->real_variable_index[ram_entry.timestamp_witness],
2082 ram_entry.access_type });
2083 }
2084 cir.ram_records.push_back(table);
2085 cir.ram_states.push_back(ram_table.state);
2086 }
2087
2088 cir.circuit_finalized = this->circuit_finalized;
2089
2090 msgpack::sbuffer buffer;
2091 msgpack::pack(buffer, cir);
2092 return buffer;
2093}
2094
2097
2098} // namespace bb
#define BB_ASSERT(expression,...)
Definition assert.hpp:70
#define BB_ASSERT_GTE(left, right,...)
Definition assert.hpp:128
#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
bb::field< bb::Bn254FrParams > FF
Definition field.cpp:24
#define BB_BENCH_NAME(name)
Definition bb_bench.hpp:225
void fix_witness(const uint32_t witness_index, const FF &witness_value)
Add a gate equating a particular witness to a constant, fixing its value.
void init_RAM_element(const size_t ram_id, const size_t index_value, const uint32_t value_witness)
Initialize a RAM cell to equal value_witness
void create_ecc_dbl_gate(const ecc_dbl_gate_< FF > &in)
Create an elliptic curve doubling gate.
void add_gates_to_ensure_all_polys_are_non_zero()
Ensure all polynomials have at least one non-zero coefficient to avoid commiting to the zero-polynomi...
void create_sort_constraint_with_edges(const std::vector< uint32_t > &variable_indices, const FF &start, const FF &end)
Constrain consecutive variable differences to be in {0, 1, 2, 3}, with boundary checks.
void process_range_list(RangeList &list)
void create_poseidon2_internal_gate(const poseidon2_internal_gate_< FF > &in)
Poseidon2 internal round gate, activates the q_poseidon2_internal selector and relation.
size_t create_RAM_array(const size_t array_size)
Create a new updatable memory region.
void create_big_mul_add_gate(const mul_quad_< FF > &in, const bool use_next_gate_w_4=false)
Create a big multiplication-addition gate, where in.a * in.b * in.mul_scaling + in....
void create_small_range_constraint(const uint32_t variable_index, const uint64_t target_range, std::string const msg="create_small_range_constraint")
Range-constraints for small ranges, where the upper bound (target_range) need not be dyadic....
std::tuple< scaled_witness, scaled_witness, FF > add_simple
uint32_t read_RAM_array(const size_t ram_id, const uint32_t index_witness)
void create_unconstrained_gates(const std::vector< uint32_t > &variable_index)
void create_add_gate(const add_triple_< FF > &in)
Create an addition gate, where in.a * in.a_scaling + in.b * in.b_scaling + in.c * in....
void create_big_add_gate(const add_quad_< FF > &in, const bool use_next_gate_w_4=false)
Create a big addition gate, where in.a * in.a_scaling + in.b * in.b_scaling + in.c * in....
void create_ecc_add_gate(const ecc_add_gate_ &in)
Create an elliptic curve addition gate.
typename ExecutionTrace::FF FF
std::array< uint32_t, 5 > evaluate_non_native_field_addition(add_simple limb0, add_simple limb1, add_simple limb2, add_simple limb3, std::tuple< uint32_t, uint32_t, FF > limbp)
Construct gates for non-native field addition.
std::vector< uint32_t > create_limbed_range_constraint(const uint32_t variable_index, const uint64_t num_bits, const uint64_t target_range_bitnum=DEFAULT_PLOOKUP_RANGE_BITNUM, std::string const &msg="create_limbed_range_constraint")
Range-constrain a variable to [0, 2^num_bits - 1] by decomposing into smaller limbs.
size_t create_ROM_array(const size_t array_size)
Create a new read-only memory region (a.k.a. ROM table)
plookup::ReadData< uint32_t > create_gates_from_plookup_accumulators(const plookup::MultiTableId &id, const plookup::ReadData< FF > &read_values, const uint32_t key_a_index, std::optional< uint32_t > key_b_index=std::nullopt)
Create gates from pre-computed accumulator values which simultaneously establish individual basic-tab...
plookup::BasicTable & get_table(const plookup::BasicTableId id)
Get the basic table with provided ID from the set of tables for the present circuit; create it if it ...
void apply_nnf_selectors(const NNF_SELECTORS type)
Enable the nnf gate of particular type.
void finalize_circuit(const bool ensure_nonzero)
void create_poseidon2_external_gate(const poseidon2_external_gate_< FF > &in)
Poseidon2 external round gate, activates the q_poseidon2_external selector and relation.
std::array< uint32_t, 2 > evaluate_non_native_field_multiplication(const non_native_multiplication_witnesses< FF > &input)
Create gates for a full non-native field multiplication identity a * b = q * p + r.
void populate_public_inputs_block()
Copy the public input idx data into the public inputs trace block.
uint32_t read_ROM_array(const size_t rom_id, const uint32_t index_witness)
Read a single element from ROM.
RangeList create_range_list(const uint64_t target_range)
uint32_t put_constant_variable(const FF &variable)
void set_ROM_element(const size_t rom_id, const size_t index_value, const uint32_t value_witness)
Initialize a rom cell to equal value_witness
void enforce_small_deltas(const std::vector< uint32_t > &variable_indices)
Check for a sequence of variables that the neighboring differences are in {0, 1, 2,...
void create_bool_gate(const uint32_t a)
Generate an arithmetic gate equivalent to x^2 - x = 0, which forces x to be 0 or 1.
void write_RAM_array(const size_t ram_id, const uint32_t index_witness, const uint32_t value_witness)
void set_ROM_element_pair(const size_t rom_id, const size_t index_value, const std::array< uint32_t, 2 > &value_witnesses)
Initialize a ROM array element with a pair of witness values.
std::array< uint32_t, 2 > read_ROM_array_pair(const size_t rom_id, const uint32_t index_witness)
Read a pair of elements from ROM.
void range_constrain_two_limbs(const uint32_t lo_idx, const uint32_t hi_idx, const size_t lo_limb_bits=DEFAULT_NON_NATIVE_FIELD_LIMB_BITS, const size_t hi_limb_bits=DEFAULT_NON_NATIVE_FIELD_LIMB_BITS, std::string const &msg="range_constrain_two_limbs")
std::array< uint32_t, 2 > queue_partial_non_native_field_multiplication(const non_native_partial_multiplication_witnesses< FF > &input)
Queue the addition of gates constraining the limb-multiplication part of a non native field mul.
std::array< uint32_t, 5 > evaluate_non_native_field_subtraction(add_simple limb0, add_simple limb1, add_simple limb2, add_simple limb3, std::tuple< uint32_t, uint32_t, FF > limbp)
Construct gates for non-native field subtraction.
void apply_memory_selectors(const MEMORY_SELECTORS type)
Enable the memory gate of particular type.
void process_non_native_field_multiplications()
Iterates over the cached_non_native_field_multiplication objects, removes duplicates,...
void create_arithmetic_gate(const arithmetic_triple_< FF > &in)
A plonk gate with disabled (set to zero) fourth wire. q_m * a * b + q_1 * a + q_2 * b + q_3.
static constexpr Fq curve_b
Definition group.hpp:51
constexpr uint64_t get_msb() const
Container for lookup accumulator values and table reads.
Definition types.hpp:357
std::vector< BasicTable::LookupEntry > lookup_entries
Definition types.hpp:363
#define info(...)
Definition log.hpp:93
FF a
FF b
uint8_t const * buf
Definition data_store.hpp:9
std::unique_ptr< uint8_t[]> buffer
Definition engine.cpp:50
ReadData< bb::fr > get_lookup_accumulators(const MultiTableId id, const fr &key_a, const fr &key_b, const bool is_2_to_1_lookup)
Given a table ID and the key(s) for a key-value lookup, return the lookup accumulators.
@ HONK_DUMMY_MULTI
Definition types.hpp:125
BasicTable create_basic_table(const BasicTableId id, const size_t index)
const MultiTable & get_multitable(const MultiTableId id)
Return the multitable with the provided ID; construct all MultiTables if not constructed already.
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
field< Bn254FrParams > fr
Definition fr.hpp:155
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
Serialized state of a circuit.
std::vector< std::vector< std::vector< FF > > > selectors
std::vector< uint32_t > real_variable_index
std::unordered_map< uint32_t, uint64_t > range_tags
std::unordered_map< uint32_t, std::string > vars_of_interest
std::vector< std::vector< uint32_t > > ram_states
std::vector< std::vector< std::array< uint32_t, 2 > > > rom_states
std::vector< std::vector< std::vector< uint32_t > > > ram_records
std::vector< std::vector< std::vector< uint32_t > > > rom_records
std::vector< std::vector< std::vector< FF > > > lookup_tables
std::vector< uint32_t > real_variable_tags
std::vector< uint32_t > public_inps
std::vector< std::vector< std::vector< uint32_t > > > wires
Used to store instructions to create partial_non_native_field_multiplication gates.
static constexpr std::array< std::array< FF, t >, rounds_f+rounds_p > round_constants
static constexpr uint256_t modulus
BB_INLINE constexpr field to_montgomery_form() const noexcept
A basic table from which we can perform lookups (for example, an xor table)
Definition types.hpp:285
std::vector< LookupEntry > lookup_gates
Definition types.hpp:321
size_t size() const
Definition types.hpp:332
std::vector< bb::fr > column_3
Definition types.hpp:320
std::vector< bb::fr > column_2
Definition types.hpp:319
std::vector< bb::fr > column_1
Definition types.hpp:318