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 (!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 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}
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();
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 // Determine whether we can fuse this addition operation into the previous gate in the block
378 bool can_fuse_into_previous_gate =
379 block.size() > 0 && /* a previous gate exists in the block */
380 block.w_r()[block.size() - 1] == in.x1 && /* output x coord of previous gate is input of this one */
381 block.w_o()[block.size() - 1] == in.y1; /* output y coord of previous gate is input of this one */
382
383 if (can_fuse_into_previous_gate) {
384 block.q_1().set(block.size() - 1, in.sign_coefficient); // set q_sign of previous gate
385 block.q_elliptic().set(block.size() - 1, 1); // set q_ecc of previous gate to 1
386 } else {
387 block.populate_wires(this->zero_idx(), in.x1, in.y1, this->zero_idx());
388 block.q_3().emplace_back(0);
389 block.q_4().emplace_back(0);
390 block.q_1().emplace_back(in.sign_coefficient);
391
392 block.q_2().emplace_back(0);
393 block.q_m().emplace_back(0);
394 block.q_c().emplace_back(0);
395 block.set_gate_selector(1);
396 check_selector_length_consistency();
397 this->increment_num_gates();
398 }
399 // Create the unconstrained gate with the output of the doubling to be read into by the previous gate via shifts
400 create_unconstrained_gate(block, in.x2, in.x3, in.y3, in.y2);
401}
402
419template <typename ExecutionTrace>
421{
422 this->assert_valid_variables({ in.x1, in.x3, in.y1, in.y3 });
423
424 auto& block = blocks.elliptic;
425
426 // Determine whether we can fuse this doubling operation into the previous gate in the block
427 bool can_fuse_into_previous_gate =
428 block.size() > 0 && /* a previous gate exists in the block */
429 block.w_r()[block.size() - 1] == in.x1 && /* output x coord of previous gate is input of this one */
430 block.w_o()[block.size() - 1] == in.y1; /* output y coord of previous gate is input of this one */
431
432 // If possible, update the previous gate to be the first gate in the pair, otherwise create a new gate
433 if (can_fuse_into_previous_gate) {
434 block.q_elliptic().set(block.size() - 1, 1); // set q_ecc of previous gate to 1
435 block.q_m().set(block.size() - 1, 1); // set q_m (q_is_double) of previous gate to 1
436 } else {
437 block.populate_wires(this->zero_idx(), in.x1, in.y1, this->zero_idx());
438 block.q_m().emplace_back(1);
439 block.q_1().emplace_back(0);
440 block.q_2().emplace_back(0);
441 block.q_3().emplace_back(0);
442 block.q_c().emplace_back(0);
443 block.q_4().emplace_back(0);
444 block.set_gate_selector(1);
445 check_selector_length_consistency();
446 this->increment_num_gates();
447 }
448 // Create the unconstrained gate with the output of the doubling to be read into by the previous gate via shifts
449 create_unconstrained_gate(block, this->zero_idx(), in.x3, in.y3, this->zero_idx());
451
458template <typename ExecutionTrace>
459void UltraCircuitBuilder_<ExecutionTrace>::fix_witness(const uint32_t witness_index, const FF& witness_value)
460{
461 this->assert_valid_variables({ witness_index });
462
463 blocks.arithmetic.populate_wires(witness_index, this->zero_idx(), this->zero_idx(), this->zero_idx());
464 blocks.arithmetic.q_m().emplace_back(0);
465 blocks.arithmetic.q_1().emplace_back(1);
466 blocks.arithmetic.q_2().emplace_back(0);
467 blocks.arithmetic.q_3().emplace_back(0);
468 blocks.arithmetic.q_c().emplace_back(-witness_value);
469 blocks.arithmetic.q_4().emplace_back(0);
470 blocks.arithmetic.set_gate_selector(1);
471 check_selector_length_consistency();
472 this->increment_num_gates();
473}
474
475template <typename ExecutionTrace>
477{
478 if (constant_variable_indices.contains(variable)) {
479 return constant_variable_indices.at(variable);
480 } else {
481 uint32_t variable_index = this->add_variable(variable);
482 fix_witness(variable_index, variable);
483 constant_variable_indices.insert({ variable, variable_index });
484 return variable_index;
485 }
486}
496template <typename ExecutionTrace>
498{
499 for (plookup::BasicTable& table : lookup_tables) {
500 if (table.id == id) {
501 return table;
502 }
503 }
504 // Table doesn't exist! So try to create it.
505 lookup_tables.emplace_back(plookup::create_basic_table(id, lookup_tables.size()));
506 return lookup_tables.back();
507}
508
536template <typename ExecutionTrace>
538 const plookup::MultiTableId& id,
539 const plookup::ReadData<FF>& read_values,
540 const uint32_t key_a_index,
541 std::optional<uint32_t> key_b_index)
542{
543 using plookup::ColumnIdx;
544
545 const auto& multi_table = plookup::get_multitable(id);
546 const size_t num_lookups = read_values[ColumnIdx::C1].size();
548
549 for (size_t i = 0; i < num_lookups; ++i) {
550 const bool is_first_lookup = (i == 0);
551 const bool is_last_lookup = (i == num_lookups - 1);
552
553 // Get basic lookup table; construct and add to builder.lookup_tables if not already present
554 plookup::BasicTable& table = get_table(multi_table.basic_table_ids[i]);
555 table.lookup_gates.emplace_back(read_values.lookup_entries[i]);
556
557 // Create witness variables: first lookup reuses user's input indices, subsequent create new variables
558 const auto first_idx = is_first_lookup ? key_a_index : this->add_variable(read_values[ColumnIdx::C1][i]);
559 const auto second_idx = (is_first_lookup && key_b_index.has_value())
560 ? *key_b_index
561 : this->add_variable(read_values[ColumnIdx::C2][i]);
562 const auto third_idx = this->add_variable(read_values[ColumnIdx::C3][i]);
563
564 read_data[ColumnIdx::C1].push_back(first_idx);
565 read_data[ColumnIdx::C2].push_back(second_idx);
566 read_data[ColumnIdx::C3].push_back(third_idx);
567 this->assert_valid_variables({ first_idx, second_idx, third_idx });
568
569 // Populate lookup gate: wire values and selectors
570 blocks.lookup.populate_wires(first_idx, second_idx, third_idx, this->zero_idx());
571 blocks.lookup.set_gate_selector(1); // mark as lookup gate
572 blocks.lookup.q_3().emplace_back(FF(table.table_index)); // unique table identifier
573 // Step size coefficients: zero for last lookup (no next accumulator), negative step sizes otherwise
574 blocks.lookup.q_2().emplace_back(is_last_lookup ? 0 : -multi_table.column_1_step_sizes[i + 1]);
575 blocks.lookup.q_m().emplace_back(is_last_lookup ? 0 : -multi_table.column_2_step_sizes[i + 1]);
576 blocks.lookup.q_c().emplace_back(is_last_lookup ? 0 : -multi_table.column_3_step_sizes[i + 1]);
577 blocks.lookup.q_1().emplace_back(0); // unused
578 blocks.lookup.q_4().emplace_back(0); // unused
579
580 check_selector_length_consistency();
581 this->increment_num_gates();
582 }
583 return read_data;
584}
585
589template <typename ExecutionTrace>
591 const uint64_t target_range)
592{
593 RangeList result;
594 const auto range_tag = get_new_tag();
595 const auto tau_tag = get_new_tag();
596 set_tau_transposition(range_tag, tau_tag);
597 result.target_range = target_range;
598 result.range_tag = range_tag;
599 result.tau_tag = tau_tag;
600
601 uint64_t num_multiples_of_three = (target_range / DEFAULT_PLOOKUP_RANGE_STEP_SIZE);
602 // allocate the minimum number of variable indices required for the range constraint. this function is only called
603 // when we are creating a range constraint on a witness index, which is responsible for the extra + 1. (note that
604 // the below loop goes from 0 to `num_multiples_of_three` inclusive.)
605 result.variable_indices.reserve(static_cast<uint32_t>(num_multiples_of_three + 3));
606 for (uint64_t i = 0; i <= num_multiples_of_three; ++i) {
607 const uint32_t index = this->add_variable(fr(i * DEFAULT_PLOOKUP_RANGE_STEP_SIZE));
608 result.variable_indices.emplace_back(index);
609 assign_tag(index, result.range_tag);
611 // `target_range` may not be divisible by 3, so we explicitly add it also.
612 {
613 const uint32_t index = this->add_variable(fr(target_range));
614 result.variable_indices.emplace_back(index);
615 assign_tag(index, result.range_tag);
616 }
617 // Need this because these variables will not appear in the witness otherwise
618 create_unconstrained_gates(result.variable_indices);
619
620 return result;
621}
622
623template <typename ExecutionTrace>
625 const uint32_t variable_index, const uint64_t num_bits, const uint64_t target_range_bitnum, std::string const& msg)
626{
627 this->assert_valid_variables({ variable_index });
628 // make sure `num_bits` satisfies the correct bounds
629 BB_ASSERT_GT(num_bits, 0U);
630 BB_ASSERT_GTE(MAX_NUM_BITS_RANGE_CONSTRAINT, num_bits);
631
632 uint256_t val = (uint256_t)(this->get_variable(variable_index));
633
634 // If the value is out of range, set the CircuitBuilder error to the given msg.
635 if (val.get_msb() >= num_bits && !this->failed()) {
636 this->failure(msg);
637 }
638
639 // compute limb structure
640 const uint64_t sublimb_mask = (1ULL << target_range_bitnum) - 1;
641
642 std::vector<uint64_t> sublimbs;
643 std::vector<uint32_t> sublimb_indices;
644
645 const bool has_remainder_bits = (num_bits % target_range_bitnum != 0);
646 const uint64_t num_limbs = (num_bits / target_range_bitnum) + has_remainder_bits;
647 const uint64_t last_limb_size = num_bits - ((num_bits / target_range_bitnum) * target_range_bitnum);
648 const uint64_t last_limb_range = ((uint64_t)1 << last_limb_size) - 1;
649
650 // extract limbs from the value
651 uint256_t accumulator = val;
652 for (size_t i = 0; i < num_limbs; ++i) {
653 sublimbs.push_back(accumulator.data[0] & sublimb_mask);
654 accumulator = accumulator >> target_range_bitnum;
655 }
656 // set the correct range constraint on each limb. note that when there are remainder bits, the last limb must be
657 // constrained to a smaller range.
658 const size_t num_full_limbs = has_remainder_bits ? sublimbs.size() - 1 : sublimbs.size();
659 for (size_t i = 0; i < num_full_limbs; ++i) {
660 const auto limb_idx = this->add_variable(bb::fr(sublimbs[i]));
661 sublimb_indices.emplace_back(limb_idx);
662 create_small_range_constraint(limb_idx, sublimb_mask);
663 }
664 if (has_remainder_bits) {
665 const auto limb_idx = this->add_variable(bb::fr(sublimbs.back()));
666 sublimb_indices.emplace_back(limb_idx);
667 create_small_range_constraint(limb_idx, last_limb_range);
668 }
669
670 // Prove that the limbs reconstruct the original value by processing limbs in groups of 3.
671 // We constrain: value = sum_{j=0}^{num_limbs-1} limb[j] * 2^(j * target_range_bitnum)
672 //
673 // Each iteration subtracts 3 limbs' contributions from an accumulator (starting at `val`),
674 // and constrains that the accumulator updates correctly via an arithmetic gate.
675 const uint64_t num_limb_triples = (num_limbs / 3) + ((num_limbs % 3) != 0);
676 // `leftovers` is the number of real limbs in the final triple (1, 2, or 3).
677 const uint64_t leftovers = (num_limbs % 3) == 0 ? 3 : (num_limbs % 3);
678
679 accumulator = val;
680 uint32_t accumulator_idx = variable_index;
681 // loop goes from `i = 0` to `num_limb_triples`, but some special case must be taken for the last triple (`i ==
682 // num_limb_triples - 1`), hence some conditional logic.
683 for (size_t i = 0; i < num_limb_triples; ++i) {
684 // `real_limbs` which limb positions in this triple contain actual limbs vs zero-padding.
685 // When `i == num_limb_triples - 1`, some positions may be unused if `num_limbs` isn't divisible by 3.
686 const bool real_limbs[3]{
687 !(i == (num_limb_triples - 1) && (leftovers < 1)),
688 !(i == (num_limb_triples - 1) && (leftovers < 2)),
689 !(i == (num_limb_triples - 1) && (leftovers < 3)),
690 };
691
692 // The witness values of the 3 limbs in this triple (0 for padding positions).
693 const uint64_t round_sublimbs[3]{
694 real_limbs[0] ? sublimbs[3 * i] : 0,
695 real_limbs[1] ? sublimbs[3 * i + 1] : 0,
696 real_limbs[2] ? sublimbs[3 * i + 2] : 0,
697 };
698 // The witnesss indices of the current 3 limbs (zero_idx for padding positions).
699 const uint32_t new_limbs[3]{
700 real_limbs[0] ? sublimb_indices[3 * i] : this->zero_idx(),
701 real_limbs[1] ? sublimb_indices[3 * i + 1] : this->zero_idx(),
702 real_limbs[2] ? sublimb_indices[3 * i + 2] : this->zero_idx(),
703 };
704 // Bit-shifts for each limb: limb[3*i+k] contributes at bit position (3*i+k) * target_range_bitnum.
705 const uint64_t shifts[3]{
706 target_range_bitnum * (3 * i),
707 target_range_bitnum * (3 * i + 1),
708 target_range_bitnum * (3 * i + 2),
709 };
710 // Compute the new accumulator after subtracting this triple's contribution.
711 // After the final iteration, accumulator should be 0.
712 uint256_t new_accumulator = accumulator - (uint256_t(round_sublimbs[0]) << shifts[0]) -
713 (uint256_t(round_sublimbs[1]) << shifts[1]) -
714 (uint256_t(round_sublimbs[2]) << shifts[2]);
715
716 // This `big_add_gate` has differing behavior depending on whether or not `i == num_limb_triples - 1`.
717 // If `i != num_limb_triples - 1`, then the constraint will be limb[0]*2^shift[0] + limb[1]*2^shift[1] +
718 // limb[2]*2^shift[2] - acc = new_accumulator (the last argument to `create_big_add_gate` is `true`, means the
719 // sum is w_4-shift, which will be the witness corresponding to what is currently `new_accumulator`.).
720 // If `i == num_limb_triples - 1`, then the last argument to `create_big_add_gate` is false, so the constraint
721 // is limb[0]*2^shift[0] + limb[1]*2^shift[1] + limb[2]*2^shift[2] - acc = 0.
722 //
723 // N.B. When `num_bits` is small, we only have remainder bits. This last constraint, checking the correctness of
724 // the limb-decomposition, ensures that the variable is not orphaned. (See the warning in
725 // `create_small_range_constraint`.)
726 create_big_add_gate(
727 {
728 new_limbs[0],
729 new_limbs[1],
730 new_limbs[2],
731 accumulator_idx,
732 uint256_t(1) << shifts[0],
733 uint256_t(1) << shifts[1],
734 uint256_t(1) << shifts[2],
735 -1,
736 0,
737 },
738 (i != num_limb_triples - 1));
739 if (i != num_limb_triples - 1) {
740 accumulator_idx = this->add_variable(fr(new_accumulator));
741 accumulator = new_accumulator;
742 }
743 }
744 return sublimb_indices;
745}
746
747template <typename ExecutionTrace>
749 const uint64_t target_range,
750 std::string const msg)
751{
752 // make sure `target_range` is not too big.
753 BB_ASSERT_GTE(MAX_SMALL_RANGE_CONSTRAINT_VAL, target_range);
754 const bool is_out_of_range = (uint256_t(this->get_variable(variable_index)).data[0] > target_range);
755 if (is_out_of_range && !this->failed()) {
756 this->failure(msg);
757 }
758 if (range_lists.count(target_range) == 0) {
759 range_lists.insert({ target_range, create_range_list(target_range) });
760 }
761 // The tag of `variable_index` is `DEFAULT_TAG` if it has never been range-constrained and a non-trivial value
762 // otherwise.
763 const auto existing_tag = this->real_variable_tags[this->real_variable_index[variable_index]];
764 auto& list = range_lists[target_range];
765
766 // If the variable's tag matches the target range list's tag, do nothing; the variable has _already_ been
767 // constrained to this exact range (i.e., `create_new_range_constraint(variable_index, target_range)` has already
768 // been called).
769 if (existing_tag == list.range_tag) {
770 return;
771 }
772 // If the variable is 'untagged' (i.e., it has the dummy tag), assign it the appropriate tag, which amounts to
773 // setting the range-constraint.
774 if (existing_tag == DEFAULT_TAG) {
775 assign_tag(variable_index, list.range_tag);
776 list.variable_indices.emplace_back(variable_index);
777 return;
778 }
779 // Otherwise, find the range for which the variable has already been tagged.
780 bool found_tag = false;
781 for (const auto& r : range_lists) {
782 if (r.second.range_tag == existing_tag) {
783 found_tag = true;
784 if (r.first < target_range) {
785 // The variable already has a more restrictive range check, so do nothing.
786 return;
787 }
788 // The range constraint we are trying to impose is more restrictive than the existing range
789 // constraint. It would be difficult to remove an existing range check. Instead, arithmetically copy the
790 // variable and apply a range check to new variable. We do _not_ simply create a
791 // copy-constraint, because that would copy the tag, which exactly corresponds to the old (less
792 // restrictive) range constraint. Instead, we use an arithmetic gate to constrain the value of
793 // the new variable and set the tag (a.k.a. range-constraint) via a new call to
794 // `create_new_range_constraint`.
795 const uint32_t copied_witness = this->add_variable(this->get_variable(variable_index));
796 create_add_gate({ .a = variable_index,
797 .b = copied_witness,
798 .c = this->zero_idx(),
799 .a_scaling = 1,
800 .b_scaling = -1,
801 .c_scaling = 0,
802 .const_scaling = 0 });
803 // Recurse with new witness that has no tag attached.
804 create_small_range_constraint(copied_witness, target_range, msg);
805 return;
806 }
807 }
808 // should never occur
809 BB_ASSERT(found_tag);
810}
811
812template <typename ExecutionTrace> void UltraCircuitBuilder_<ExecutionTrace>::process_range_list(RangeList& list)
813{
814 this->assert_valid_variables(list.variable_indices);
815
816 BB_ASSERT_GT(list.variable_indices.size(), 0U);
817
818 // replace witness-index in variable_indices with the corresponding real-variable-index i.e., if a copy constraint
819 // has been applied on a variable after it was range constrained, this makes sure the indices in list point to the
820 // updated index in the range list so the set equivalence does not fail
821 for (uint32_t& x : list.variable_indices) {
822 x = this->real_variable_index[x];
823 }
824 // Sort `variable_indices` and remove duplicate witness indices to prevent the sorted list set size being wrong!
825 std::sort(list.variable_indices.begin(), list.variable_indices.end());
826 auto back_iterator = std::unique(list.variable_indices.begin(), list.variable_indices.end());
827 list.variable_indices.erase(back_iterator, list.variable_indices.end());
828
829 // Extract the values of each (real) variable into a list to be sorted (in the sense of the range/plookup-style
830 // argument).
831 std::vector<uint32_t> sorted_list;
832 sorted_list.reserve(list.variable_indices.size());
833 for (const auto variable_index : list.variable_indices) {
834 // note that `field_element` is < 32 bits as the corresponding witness has a non-trivial range-constraint.
835 const auto& field_element = this->get_variable(variable_index);
836 const uint32_t shrinked_value = (uint32_t)field_element.from_montgomery_form().data[0];
837 sorted_list.emplace_back(shrinked_value);
838 }
839
840#ifdef NO_PAR_ALGOS
841 std::sort(sorted_list.begin(), sorted_list.end());
842#else
843 std::sort(std::execution::par_unseq, sorted_list.begin(), sorted_list.end());
844#endif
845 // list must be padded to a multipe of 4 and larger than 4 (gate_width)
846 constexpr size_t gate_width = NUM_WIRES;
847 size_t padding = (gate_width - (list.variable_indices.size() % gate_width)) % gate_width;
848
849 std::vector<uint32_t> indices;
850 indices.reserve(padding + sorted_list.size());
851
852 if (list.variable_indices.size() <= gate_width) {
853 padding += gate_width;
854 }
855 for (size_t i = 0; i < padding; ++i) {
856 indices.emplace_back(this->zero_idx());
857 }
858 // tag the elements in the sorted_list to apply the multiset-equality check implicit in range-constraints.
859 for (const auto sorted_value : sorted_list) {
860 const uint32_t index = this->add_variable(fr(sorted_value));
861 assign_tag(index, list.tau_tag);
862 indices.emplace_back(index);
863 }
864 // constrain the _sorted_ list: starts at 0, ends at `target_range`, consecutive differences in {0, 1, 2, 3}.
865 create_sort_constraint_with_edges(indices, 0, list.target_range);
866}
867
868template <typename ExecutionTrace> void UltraCircuitBuilder_<ExecutionTrace>::process_range_lists()
869{
870 for (auto& i : range_lists) {
871 process_range_list(i.second);
872 }
873}
874
875template <typename ExecutionTrace>
876void UltraCircuitBuilder_<ExecutionTrace>::enforce_small_deltas(const std::vector<uint32_t>& variable_indices)
877{
878 constexpr size_t gate_width = NUM_WIRES;
879 BB_ASSERT_EQ(variable_indices.size() % gate_width, 0U);
880 this->assert_valid_variables(variable_indices);
881
882 for (size_t i = 0; i < variable_indices.size(); i += gate_width) {
883 blocks.delta_range.populate_wires(
884 variable_indices[i], variable_indices[i + 1], variable_indices[i + 2], variable_indices[i + 3]);
885
886 this->increment_num_gates();
887 blocks.delta_range.q_m().emplace_back(0);
888 blocks.delta_range.q_1().emplace_back(0);
889 blocks.delta_range.q_2().emplace_back(0);
890 blocks.delta_range.q_3().emplace_back(0);
891 blocks.delta_range.q_c().emplace_back(0);
892 blocks.delta_range.q_4().emplace_back(0);
893 blocks.delta_range.set_gate_selector(1);
894 check_selector_length_consistency();
895 }
896 // dummy gate needed because of widget's check of next row
897 create_unconstrained_gate(blocks.delta_range,
898 variable_indices[variable_indices.size() - 1],
899 this->zero_idx(),
900 this->zero_idx(),
901 this->zero_idx());
902}
903
904// useful to put variables in the witness that aren't already used - e.g. the dummy variables of the range constraint in
905// multiples of four
906template <typename ExecutionTrace>
907void UltraCircuitBuilder_<ExecutionTrace>::create_unconstrained_gates(const std::vector<uint32_t>& variable_index)
908{
909 std::vector<uint32_t> padded_list = variable_index;
910 constexpr size_t gate_width = NUM_WIRES;
911 const uint64_t padding = (gate_width - (padded_list.size() % gate_width)) % gate_width;
912 for (uint64_t i = 0; i < padding; ++i) {
913 padded_list.emplace_back(this->zero_idx());
914 }
915 this->assert_valid_variables(variable_index);
916 this->assert_valid_variables(padded_list);
917
918 for (size_t i = 0; i < padded_list.size(); i += gate_width) {
919 create_unconstrained_gate(
920 blocks.arithmetic, padded_list[i], padded_list[i + 1], padded_list[i + 2], padded_list[i + 3]);
921 }
922}
923
924template <typename ExecutionTrace>
926 const std::vector<uint32_t>& variable_indices, const FF& start, const FF& end)
927{
928 // Convenient to assume size is at least 8 (gate_width = 4) for separate gates for start and end conditions
929 constexpr size_t gate_width = NUM_WIRES;
930 BB_ASSERT_EQ(variable_indices.size() % gate_width, 0U);
931 BB_ASSERT_GT(variable_indices.size(), gate_width);
932 this->assert_valid_variables(variable_indices);
933 // 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`
934 // to be in {0, 1, 2, 3}.
935 auto& block = blocks.delta_range;
936
937 // Add an arithmetic gate to ensure the first input is equal to the start value of the range being checked
938 create_add_gate({ variable_indices[0], this->zero_idx(), this->zero_idx(), 1, 0, 0, -start });
939
940 // enforce delta range relation for all rows (there are `variabe_indices.size() / gate_width`). note that there are
941 // at least two rows.
942 for (size_t i = 0; i < variable_indices.size(); i += gate_width) {
943
944 block.populate_wires(
945 variable_indices[i], variable_indices[i + 1], variable_indices[i + 2], variable_indices[i + 3]);
946 this->increment_num_gates();
947 block.q_m().emplace_back(0);
948 block.q_1().emplace_back(0);
949 block.q_2().emplace_back(0);
950 block.q_3().emplace_back(0);
951 block.q_c().emplace_back(0);
952 block.q_4().emplace_back(0);
953 block.set_gate_selector(1);
954 check_selector_length_consistency();
955 }
956
957 // 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}).
958 // Therefore, we repeat the last element in an unconstrained gate.
959 create_unconstrained_gate(
960 block, variable_indices[variable_indices.size() - 1], this->zero_idx(), this->zero_idx(), this->zero_idx());
961 // arithmetic gate to constrain that `variable_indices[last] == end`, i.e., verify the boundary condition.
962 create_add_gate(
963 { variable_indices[variable_indices.size() - 1], this->zero_idx(), this->zero_idx(), 1, 0, 0, -end });
964}
965
988template <typename ExecutionTrace>
990{
991 auto& block = blocks.memory;
992 block.set_gate_selector(type == MEMORY_SELECTORS::MEM_NONE ? 0 : 1);
993 switch (type) {
994 case MEMORY_SELECTORS::ROM_CONSISTENCY_CHECK: {
995 // Memory read gate used with the sorted list of memory reads.
996 // Apply sorted memory read checks with the following additional check:
997 // 1. Assert that if index field across two gates does not change, the value field does not change.
998 // Used for ROM reads and RAM reads across write/read boundaries
999 block.q_1().emplace_back(1);
1000 block.q_2().emplace_back(1);
1001 block.q_3().emplace_back(0);
1002 block.q_4().emplace_back(0);
1003 block.q_m().emplace_back(0);
1004 block.q_c().emplace_back(0);
1005 check_selector_length_consistency();
1006 break;
1007 }
1008 case MEMORY_SELECTORS::RAM_CONSISTENCY_CHECK: {
1009 // Memory read gate used with the sorted list of memory reads.
1010 // 1. Validate adjacent index values across 2 gates increases by 0 or 1
1011 // 2. Validate record computation (r = read_write_flag + index * \eta + \timestamp * \eta^2 + value * \eta^3)
1012 // 3. If adjacent index values across 2 gates does not change, and the next gate's read_write_flag is set to
1013 // 'read', validate adjacent values do not change Used for ROM reads and RAM reads across read/write boundaries
1014 block.q_1().emplace_back(0);
1015 block.q_2().emplace_back(0);
1016 block.q_3().emplace_back(1);
1017 block.q_4().emplace_back(0);
1018 block.q_m().emplace_back(0);
1019 block.q_c().emplace_back(0);
1020 check_selector_length_consistency();
1021 break;
1022 }
1023 case MEMORY_SELECTORS::RAM_TIMESTAMP_CHECK: {
1024 // For two adjacent RAM entries that share the same index, validate the timestamp value is monotonically
1025 // increasing
1026 block.q_1().emplace_back(1);
1027 block.q_2().emplace_back(0);
1028 block.q_3().emplace_back(0);
1029 block.q_4().emplace_back(1);
1030 block.q_m().emplace_back(0);
1031 block.q_c().emplace_back(0);
1032 check_selector_length_consistency();
1033 break;
1034 }
1035 case MEMORY_SELECTORS::ROM_READ: {
1036 // Memory read gate for reading memory cells. Also used for the _initialization_ of ROM memory cells.
1037 // Validates record witness computation (r = read_write_flag + index * \eta + timestamp * \eta^2 + value *
1038 // \eta^3)
1039 block.q_1().emplace_back(1);
1040 block.q_2().emplace_back(0);
1041 block.q_3().emplace_back(0);
1042 block.q_4().emplace_back(0);
1043 block.q_m().emplace_back(1); // validate record witness is correctly computed
1044 block.q_c().emplace_back(0); // read/write flag stored in q_c
1045 check_selector_length_consistency();
1046 break;
1047 }
1048 case MEMORY_SELECTORS::RAM_READ: {
1049 // Memory read gate for reading memory cells.
1050 // Validates record witness computation (r = read_write_flag + index * \eta + timestamp * \eta^2 + value *
1051 // \eta^3)
1052 block.q_1().emplace_back(1);
1053 block.q_2().emplace_back(0);
1054 block.q_3().emplace_back(0);
1055 block.q_4().emplace_back(0);
1056 block.q_m().emplace_back(1); // validate record witness is correctly computed
1057 block.q_c().emplace_back(0); // read/write flag stored in q_c
1058 check_selector_length_consistency();
1059 break;
1060 }
1061 case MEMORY_SELECTORS::RAM_WRITE: {
1062 // Memory read gate for writing memory cells.
1063 // Validates record witness computation (r = read_write_flag + index * \eta + timestamp * \eta^2 + value *
1064 // \eta^3)
1065 block.q_1().emplace_back(1);
1066 block.q_2().emplace_back(0);
1067 block.q_3().emplace_back(0);
1068 block.q_4().emplace_back(0);
1069 block.q_m().emplace_back(1); // validate record witness is correctly computed
1070 block.q_c().emplace_back(1); // read/write flag stored in q_c
1071 check_selector_length_consistency();
1072 break;
1073 }
1074 default: {
1075 block.q_1().emplace_back(0);
1076 block.q_2().emplace_back(0);
1077 block.q_3().emplace_back(0);
1078 block.q_4().emplace_back(0);
1079 block.q_m().emplace_back(0);
1080 block.q_c().emplace_back(0);
1081 check_selector_length_consistency();
1082 break;
1083 }
1084 }
1085}
1086
1110template <typename ExecutionTrace>
1112{
1113 auto& block = blocks.nnf;
1114 block.set_gate_selector(type == NNF_SELECTORS::NNF_NONE ? 0 : 1);
1115 switch (type) {
1116 case NNF_SELECTORS::LIMB_ACCUMULATE_1: {
1117 block.q_1().emplace_back(0);
1118 block.q_2().emplace_back(0);
1119 block.q_3().emplace_back(1);
1120 block.q_4().emplace_back(1);
1121 block.q_m().emplace_back(0);
1122 block.q_c().emplace_back(0);
1123 check_selector_length_consistency();
1124 break;
1125 }
1126 case NNF_SELECTORS::LIMB_ACCUMULATE_2: {
1127 block.q_1().emplace_back(0);
1128 block.q_2().emplace_back(0);
1129 block.q_3().emplace_back(1);
1130 block.q_4().emplace_back(0);
1131 block.q_m().emplace_back(1);
1132 block.q_c().emplace_back(0);
1133 check_selector_length_consistency();
1134 break;
1135 }
1136 case NNF_SELECTORS::NON_NATIVE_FIELD_1: {
1137 block.q_1().emplace_back(0);
1138 block.q_2().emplace_back(1);
1139 block.q_3().emplace_back(1);
1140 block.q_4().emplace_back(0);
1141 block.q_m().emplace_back(0);
1142 block.q_c().emplace_back(0);
1143 check_selector_length_consistency();
1144 break;
1145 }
1146 case NNF_SELECTORS::NON_NATIVE_FIELD_2: {
1147 block.q_1().emplace_back(0);
1148 block.q_2().emplace_back(1);
1149 block.q_3().emplace_back(0);
1150 block.q_4().emplace_back(1);
1151 block.q_m().emplace_back(0);
1152 block.q_c().emplace_back(0);
1153 check_selector_length_consistency();
1154 break;
1155 }
1156 case NNF_SELECTORS::NON_NATIVE_FIELD_3: {
1157 block.q_1().emplace_back(0);
1158 block.q_2().emplace_back(1);
1159 block.q_3().emplace_back(0);
1160 block.q_4().emplace_back(0);
1161 block.q_m().emplace_back(1);
1162 block.q_c().emplace_back(0);
1163 check_selector_length_consistency();
1164 break;
1165 }
1166 default: {
1167 block.q_1().emplace_back(0);
1168 block.q_2().emplace_back(0);
1169 block.q_3().emplace_back(0);
1170 block.q_4().emplace_back(0);
1171 block.q_m().emplace_back(0);
1172 block.q_c().emplace_back(0);
1173 check_selector_length_consistency();
1174 break;
1175 }
1176 }
1177}
1178
1189template <typename ExecutionTrace>
1191 const uint32_t hi_idx,
1192 const size_t lo_limb_bits,
1193 const size_t hi_limb_bits,
1194 std::string const& msg)
1195{
1196 // Validate limbs are <= 70 bits. If limbs are larger we require more witnesses and cannot use our limb accumulation
1197 // custom gate
1198 BB_ASSERT_LTE(lo_limb_bits, 14U * 5U);
1199 BB_ASSERT_LTE(hi_limb_bits, 14U * 5U);
1200
1201 // If the value is larger than the range, we log the error in builder
1202 const bool is_lo_out_of_range = (uint256_t(this->get_variable(lo_idx)) >= (uint256_t(1) << lo_limb_bits));
1203 if (is_lo_out_of_range && !this->failed()) {
1204 this->failure(msg + ": lo limb.");
1205 }
1206 const bool is_hi_out_of_range = (uint256_t(this->get_variable(hi_idx)) >= (uint256_t(1) << hi_limb_bits));
1207 if (is_hi_out_of_range && !this->failed()) {
1208 this->failure(msg + ": hi limb.");
1209 }
1210
1211 // Sometimes we try to use limbs that are too large. It's easier to catch this issue here
1212 const auto get_sublimbs = [&](const uint32_t& limb_idx, const std::array<uint64_t, 5>& sublimb_masks) {
1213 const uint256_t limb = this->get_variable(limb_idx);
1214 // we can use constant 2^14 - 1 mask here. If the sublimb value exceeds the expected value then witness will
1215 // fail the range check below
1216 // We also use zero_idx to substitute variables that should be zero
1217 constexpr uint256_t MAX_SUBLIMB_MASK = (uint256_t(1) << 14) - 1;
1218 std::array<uint32_t, 5> sublimb_indices;
1219 sublimb_indices[0] = sublimb_masks[0] != 0 ? this->add_variable(fr(limb & MAX_SUBLIMB_MASK)) : this->zero_idx();
1220 sublimb_indices[1] =
1221 sublimb_masks[1] != 0 ? this->add_variable(fr((limb >> 14) & MAX_SUBLIMB_MASK)) : this->zero_idx();
1222 sublimb_indices[2] =
1223 sublimb_masks[2] != 0 ? this->add_variable(fr((limb >> 28) & MAX_SUBLIMB_MASK)) : this->zero_idx();
1224 sublimb_indices[3] =
1225 sublimb_masks[3] != 0 ? this->add_variable(fr((limb >> 42) & MAX_SUBLIMB_MASK)) : this->zero_idx();
1226 sublimb_indices[4] =
1227 sublimb_masks[4] != 0 ? this->add_variable(fr((limb >> 56) & MAX_SUBLIMB_MASK)) : this->zero_idx();
1228 return sublimb_indices;
1229 };
1230
1231 const auto get_limb_masks = [](size_t limb_bits) {
1232 std::array<uint64_t, 5> sublimb_masks;
1233 sublimb_masks[0] = limb_bits >= 14 ? 14 : limb_bits;
1234 sublimb_masks[1] = limb_bits >= 28 ? 14 : (limb_bits > 14 ? limb_bits - 14 : 0);
1235 sublimb_masks[2] = limb_bits >= 42 ? 14 : (limb_bits > 28 ? limb_bits - 28 : 0);
1236 sublimb_masks[3] = limb_bits >= 56 ? 14 : (limb_bits > 42 ? limb_bits - 42 : 0);
1237 sublimb_masks[4] = (limb_bits > 56 ? limb_bits - 56 : 0);
1238
1239 for (auto& mask : sublimb_masks) {
1240 mask = (1ULL << mask) - 1ULL;
1241 }
1242 return sublimb_masks;
1243 };
1244
1245 const auto lo_masks = get_limb_masks(lo_limb_bits);
1246 const auto hi_masks = get_limb_masks(hi_limb_bits);
1247 const std::array<uint32_t, 5> lo_sublimbs = get_sublimbs(lo_idx, lo_masks);
1248 const std::array<uint32_t, 5> hi_sublimbs = get_sublimbs(hi_idx, hi_masks);
1249
1250 blocks.nnf.populate_wires(lo_sublimbs[0], lo_sublimbs[1], lo_sublimbs[2], lo_idx);
1251 blocks.nnf.populate_wires(lo_sublimbs[3], lo_sublimbs[4], hi_sublimbs[0], hi_sublimbs[1]);
1252 blocks.nnf.populate_wires(hi_sublimbs[2], hi_sublimbs[3], hi_sublimbs[4], hi_idx);
1253
1254 apply_nnf_selectors(NNF_SELECTORS::LIMB_ACCUMULATE_1);
1255 apply_nnf_selectors(NNF_SELECTORS::LIMB_ACCUMULATE_2);
1256 apply_nnf_selectors(NNF_SELECTORS::NNF_NONE);
1257 this->increment_num_gates(3);
1258
1259 for (size_t i = 0; i < 5; i++) {
1260 if (lo_masks[i] != 0) {
1261 create_small_range_constraint(
1262 lo_sublimbs[i], lo_masks[i], "ultra_circuit_builder: sublimb of low too large");
1263 }
1264 if (hi_masks[i] != 0) {
1265 create_small_range_constraint(
1266 hi_sublimbs[i], hi_masks[i], "ultra_circuit_builder: sublimb of hi too large");
1267 }
1268 }
1269};
1270
1286template <typename ExecutionTrace>
1289{
1290 const auto [a0, a1, a2, a3] = std::array{ this->get_variable(input.a[0]),
1291 this->get_variable(input.a[1]),
1292 this->get_variable(input.a[2]),
1293 this->get_variable(input.a[3]) };
1294 const auto [b0, b1, b2, b3] = std::array{ this->get_variable(input.b[0]),
1295 this->get_variable(input.b[1]),
1296 this->get_variable(input.b[2]),
1297 this->get_variable(input.b[3]) };
1298 const auto [q0, q1, q2, q3] = std::array{ this->get_variable(input.q[0]),
1299 this->get_variable(input.q[1]),
1300 this->get_variable(input.q[2]),
1301 this->get_variable(input.q[3]) };
1302 const auto [r0, r1, r2, r3] = std::array{ this->get_variable(input.r[0]),
1303 this->get_variable(input.r[1]),
1304 this->get_variable(input.r[2]),
1305 this->get_variable(input.r[3]) };
1306 const auto& p_neg = input.neg_modulus;
1307
1308 constexpr FF LIMB_SHIFT = uint256_t(1) << DEFAULT_NON_NATIVE_FIELD_LIMB_BITS;
1309 constexpr FF LIMB_RSHIFT = FF(1) / FF(uint256_t(1) << DEFAULT_NON_NATIVE_FIELD_LIMB_BITS);
1310 constexpr FF LIMB_RSHIFT_2 = FF(1) / FF(uint256_t(1) << (2 * DEFAULT_NON_NATIVE_FIELD_LIMB_BITS));
1311
1312 // lo_0 = (a0·b0 - r0) + (a1·b0 + a0·b1)·2^L
1313 FF lo_0 = (a0 * b0 - r0) + (a1 * b0 + a0 * b1) * LIMB_SHIFT;
1314 // lo_1 = (lo_0 + q0·p0' + (q1·p0' + q0·p1' - r1)·2^L) / 2^2L
1315 FF lo_1 = (lo_0 + q0 * p_neg[0] + (q1 * p_neg[0] + q0 * p_neg[1] - r1) * LIMB_SHIFT) * LIMB_RSHIFT_2;
1316
1317 // hi_0 = (a2·b0 + a0·b2) + (a0·b3 + a3·b0 - r3)·2^L
1318 FF hi_0 = (a2 * b0 + a0 * b2) + (a0 * b3 + a3 * b0 - r3) * LIMB_SHIFT;
1319 // hi_1 = hi_0 + (a1·b1 - r2) + (a1·b2 + a2·b1)·2^L
1320 FF hi_1 = hi_0 + (a1 * b1 - r2) + (a1 * b2 + a2 * b1) * LIMB_SHIFT;
1321 // hi_2 = hi_1 + lo_1 + q2·p0' + (q3·p0' + q2·p1')·2^L
1322 FF hi_2 = hi_1 + lo_1 + q2 * p_neg[0] + (q3 * p_neg[0] + q2 * p_neg[1]) * LIMB_SHIFT;
1323 // hi_3 = (hi_2 + q0·p2' + q1·p1' + (q0·p3' + q1·p2')·2^L) / 2^2L
1324 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;
1325
1326 const uint32_t lo_0_idx = this->add_variable(lo_0);
1327 const uint32_t lo_1_idx = this->add_variable(lo_1);
1328 const uint32_t hi_0_idx = this->add_variable(hi_0);
1329 const uint32_t hi_1_idx = this->add_variable(hi_1);
1330 const uint32_t hi_2_idx = this->add_variable(hi_2);
1331 const uint32_t hi_3_idx = this->add_variable(hi_3);
1332
1333 // Gate 1: big_add_gate to validate lo_1
1334 // (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
1335 // This constraint requires two rows in the trace: an arithmetic gate plus an unconstrained arithmetic gate
1336 // containing lo_0 in wire 4 so that the previous gate can access it via shifts. (We cannot use the next nnf gate
1337 // for this purpose since our trace is sorted by gate type).
1338 create_big_add_gate({ input.q[0],
1339 input.q[1],
1340 input.r[1],
1341 lo_1_idx,
1342 input.neg_modulus[0] + input.neg_modulus[1] * LIMB_SHIFT,
1343 input.neg_modulus[0] * LIMB_SHIFT,
1344 -LIMB_SHIFT,
1345 -LIMB_SHIFT.sqr(),
1346 0 },
1347 /*include_next_gate_w_4*/ true);
1348 // Gate 2: unconstrained gate to provide lo_0 via w_4_shift for gate 1
1349 create_unconstrained_gate(blocks.arithmetic, this->zero_idx(), this->zero_idx(), this->zero_idx(), lo_0_idx);
1350
1351 //
1352 // a = (a3 || a2 || a1 || a0) = (a3 * 2^b + a2) * 2^b + (a1 * 2^b + a0)
1353 // b = (b3 || b2 || b1 || b0) = (b3 * 2^b + b2) * 2^b + (b1 * 2^b + b0)
1354 //
1355 // Gate 3: NNF gate to check if lo_0 was computed correctly
1356 // The gate structure for the nnf gates is as follows:
1357 //
1358 // | a1 | b1 | r0 | lo_0 | <-- Gate 3: check lo_0
1359 // | a0 | b0 | a3 | b3 |
1360 // | a2 | b2 | r3 | hi_0 |
1361 // | a1 | b1 | r2 | hi_1 |
1362 //
1363 // Constraint: lo_0 = (a1 * b0 + a0 * b1) * 2^b + (a0 * b0) - r0
1364 // w4 = (w1 * w'2 + w'1 * w2) * 2^b + (w'1 * w'2) - w3
1365 //
1366 blocks.nnf.populate_wires(input.a[1], input.b[1], input.r[0], lo_0_idx);
1367 apply_nnf_selectors(NNF_SELECTORS::NON_NATIVE_FIELD_1);
1368 this->increment_num_gates();
1369
1370 //
1371 // Gate 4: NNF gate to check if hi_0 was computed correctly
1372 //
1373 // | a1 | b1 | r0 | lo_0 |
1374 // | a0 | b0 | a3 | b3 | <-- Gate 4: check hi_0
1375 // | a2 | b2 | r3 | hi_0 |
1376 // | a1 | b1 | r2 | hi_1 |
1377 //
1378 // Constraint: hi_0 = (a0 * b3 + a3 * b0 - r3) * 2^b + (a0 * b2 + a2 * b0)
1379 // w'4 = (w1 * w4 + w2 * w3 - w'3) * 2^b + (w1 * w'2 + w'1 * w2)
1380 //
1381 blocks.nnf.populate_wires(input.a[0], input.b[0], input.a[3], input.b[3]);
1382 apply_nnf_selectors(NNF_SELECTORS::NON_NATIVE_FIELD_2);
1383 this->increment_num_gates();
1384
1385 //
1386 // Gate 5: NNF gate to check if hi_1 was computed correctly
1387 //
1388 // | a1 | b1 | r0 | lo_0 |
1389 // | a0 | b0 | a3 | b3 |
1390 // | a2 | b2 | r3 | hi_0 | <-- Gate 5: check hi_1
1391 // | a1 | b1 | r2 | hi_1 |
1392 //
1393 // Constraint: hi_1 = hi_0 + (a2 * b1 + a1 * b2) * 2^b + (a1 * b1) - r2
1394 // w'4 = w4 + (w1 * w'2 + w'1 * w2) * 2^b + (w'1 * w'2) - w'3
1395 //
1396 blocks.nnf.populate_wires(input.a[2], input.b[2], input.r[3], hi_0_idx);
1397 apply_nnf_selectors(NNF_SELECTORS::NON_NATIVE_FIELD_3);
1398 this->increment_num_gates();
1399
1400 //
1401 // Gate 6: NNF gate with no constraints (q_nnf=0, truly unconstrained)
1402 // Provides values a[1], b[1], r[2], hi_1 to Gate 5 via shifts (w'1, w'2, w'3, w'4)
1403 //
1404 blocks.nnf.populate_wires(input.a[1], input.b[1], input.r[2], hi_1_idx);
1405 apply_nnf_selectors(NNF_SELECTORS::NNF_NONE);
1406 this->increment_num_gates();
1407
1408 //
1409 // Gate 7: big_add_gate to validate hi_2
1410 //
1411 // hi_2 - hi_1 - lo_1 - q[2](p[1].2^b + p[0]) - q[3](p[0].2^b) = 0
1412 //
1413 create_big_add_gate(
1414 {
1415 input.q[2],
1416 input.q[3],
1417 lo_1_idx,
1418 hi_1_idx,
1419 -input.neg_modulus[1] * LIMB_SHIFT - input.neg_modulus[0],
1420 -input.neg_modulus[0] * LIMB_SHIFT,
1421 -1,
1422 -1,
1423 0,
1424 },
1425 /*include_next_gate_w_4*/ true);
1426
1427 //
1428 // Gate 8: big_add_gate to validate hi_3 (provides hi_2 in w_4 for gate 7)
1429 //
1430 // hi_3 - (hi_2 - q[0](p[3].2^b + p[2]) - q[1](p[2].2^b + p[1])).2^-2b = 0
1431 //
1432 create_big_add_gate({
1433 hi_3_idx,
1434 input.q[0],
1435 input.q[1],
1436 hi_2_idx,
1437 -1,
1438 input.neg_modulus[3] * LIMB_RSHIFT + input.neg_modulus[2] * LIMB_RSHIFT_2,
1439 input.neg_modulus[2] * LIMB_RSHIFT + input.neg_modulus[1] * LIMB_RSHIFT_2,
1440 LIMB_RSHIFT_2,
1441 0,
1442 });
1443
1444 return std::array<uint32_t, 2>{ lo_1_idx, hi_3_idx };
1445}
1446
1454{
1455 for (size_t i = 0; i < cached_partial_non_native_field_multiplications.size(); ++i) {
1456 auto& c = cached_partial_non_native_field_multiplications[i];
1457 for (size_t j = 0; j < c.a.size(); ++j) {
1458 c.a[j] = this->real_variable_index[c.a[j]];
1459 c.b[j] = this->real_variable_index[c.b[j]];
1460 }
1461 }
1462 cached_partial_non_native_field_multiplication::deduplicate(cached_partial_non_native_field_multiplications, this);
1463
1464 // iterate over the cached items and create constraints
1465 for (const auto& input : cached_partial_non_native_field_multiplications) {
1466
1467 blocks.nnf.populate_wires(input.a[1], input.b[1], this->zero_idx(), input.lo_0);
1468 apply_nnf_selectors(NNF_SELECTORS::NON_NATIVE_FIELD_1);
1469 this->increment_num_gates();
1470
1471 blocks.nnf.populate_wires(input.a[0], input.b[0], input.a[3], input.b[3]);
1472 apply_nnf_selectors(NNF_SELECTORS::NON_NATIVE_FIELD_2);
1473 this->increment_num_gates();
1474
1475 blocks.nnf.populate_wires(input.a[2], input.b[2], this->zero_idx(), input.hi_0);
1476 apply_nnf_selectors(NNF_SELECTORS::NON_NATIVE_FIELD_3);
1477 this->increment_num_gates();
1478
1479 blocks.nnf.populate_wires(input.a[1], input.b[1], this->zero_idx(), input.hi_1);
1480 apply_nnf_selectors(NNF_SELECTORS::NNF_NONE);
1481 this->increment_num_gates();
1482 }
1483}
1484
1491template <typename ExecutionTrace>
1494{
1496 this->get_variable(input.a[0]),
1497 this->get_variable(input.a[1]),
1498 this->get_variable(input.a[2]),
1499 this->get_variable(input.a[3]),
1500 };
1502 this->get_variable(input.b[0]),
1503 this->get_variable(input.b[1]),
1504 this->get_variable(input.b[2]),
1505 this->get_variable(input.b[3]),
1506 };
1507
1508 constexpr FF LIMB_SHIFT = uint256_t(1) << DEFAULT_NON_NATIVE_FIELD_LIMB_BITS;
1509
1510 FF lo_0 = a[0] * b[0] + ((a[1] * b[0] + a[0] * b[1]) * LIMB_SHIFT);
1511 FF hi_0 = a[2] * b[0] + a[0] * b[2] + ((a[0] * b[3] + a[3] * b[0]) * LIMB_SHIFT);
1512 FF hi_1 = hi_0 + a[1] * b[1] + ((a[1] * b[2] + a[2] * b[1]) * LIMB_SHIFT);
1513
1514 const uint32_t lo_0_idx = this->add_variable(lo_0);
1515 const uint32_t hi_0_idx = this->add_variable(hi_0);
1516 const uint32_t hi_1_idx = this->add_variable(hi_1);
1517
1518 // Add witnesses into the multiplication cache (duplicates removed during circuit finalization)
1520 .a = input.a,
1521 .b = input.b,
1522 .lo_0 = lo_0_idx,
1523 .hi_0 = hi_0_idx,
1524 .hi_1 = hi_1_idx,
1525 };
1526 cached_partial_non_native_field_multiplications.emplace_back(cache_entry);
1527 return std::array<uint32_t, 2>{ lo_0_idx, hi_1_idx };
1528}
1529
1535template <typename ExecutionTrace>
1538{
1539 const uint32_t& x_0 = std::get<0>(limb0).first;
1540 const uint32_t& x_1 = std::get<0>(limb1).first;
1541 const uint32_t& x_2 = std::get<0>(limb2).first;
1542 const uint32_t& x_3 = std::get<0>(limb3).first;
1543 const uint32_t& x_p = std::get<0>(limbp);
1544
1545 const FF& x_mulconst0 = std::get<0>(limb0).second;
1546 const FF& x_mulconst1 = std::get<0>(limb1).second;
1547 const FF& x_mulconst2 = std::get<0>(limb2).second;
1548 const FF& x_mulconst3 = std::get<0>(limb3).second;
1549
1550 const uint32_t& y_0 = std::get<1>(limb0).first;
1551 const uint32_t& y_1 = std::get<1>(limb1).first;
1552 const uint32_t& y_2 = std::get<1>(limb2).first;
1553 const uint32_t& y_3 = std::get<1>(limb3).first;
1554 const uint32_t& y_p = std::get<1>(limbp);
1555
1556 const FF& y_mulconst0 = std::get<1>(limb0).second;
1557 const FF& y_mulconst1 = std::get<1>(limb1).second;
1558 const FF& y_mulconst2 = std::get<1>(limb2).second;
1559 const FF& y_mulconst3 = std::get<1>(limb3).second;
1560
1561 // constant additive terms
1562 const FF& addconst0 = std::get<2>(limb0);
1563 const FF& addconst1 = std::get<2>(limb1);
1564 const FF& addconst2 = std::get<2>(limb2);
1565 const FF& addconst3 = std::get<2>(limb3);
1566 const FF& addconstp = std::get<2>(limbp);
1567
1568 // get value of result limbs
1569 const FF z_0value = (this->get_variable(x_0) * x_mulconst0) + (this->get_variable(y_0) * y_mulconst0) + addconst0;
1570 const FF z_1value = (this->get_variable(x_1) * x_mulconst1) + (this->get_variable(y_1) * y_mulconst1) + addconst1;
1571 const FF z_2value = (this->get_variable(x_2) * x_mulconst2) + (this->get_variable(y_2) * y_mulconst2) + addconst2;
1572 const FF z_3value = (this->get_variable(x_3) * x_mulconst3) + (this->get_variable(y_3) * y_mulconst3) + addconst3;
1573 const FF z_pvalue = this->get_variable(x_p) + this->get_variable(y_p) + addconstp;
1574
1575 const uint32_t z_0 = this->add_variable(z_0value);
1576 const uint32_t z_1 = this->add_variable(z_1value);
1577 const uint32_t z_2 = this->add_variable(z_2value);
1578 const uint32_t z_3 = this->add_variable(z_3value);
1579 const uint32_t z_p = this->add_variable(z_pvalue);
1580
1602 auto& block = blocks.arithmetic;
1603 block.populate_wires(y_p, x_0, y_0, x_p);
1604 block.populate_wires(z_p, x_1, y_1, z_0);
1605 block.populate_wires(x_2, y_2, z_2, z_1);
1606 block.populate_wires(x_3, y_3, z_3, this->zero_idx());
1607
1608 // When q_arith == 3, w_4_shift is scaled by 2 (see ArithmeticRelation for details). Therefore, for consistency we
1609 // also scale each linear term by this factor of 2 so that the constraint is effectively:
1610 // (q_l * w_1) + (q_r * w_2) + (q_o * w_3) + (q_4 * w_4) + q_c + w_4_shift = 0
1611 const FF linear_term_scale_factor = 2;
1612 block.q_m().emplace_back(addconstp);
1613 block.q_1().emplace_back(0);
1614 block.q_2().emplace_back(-x_mulconst0 * linear_term_scale_factor);
1615 block.q_3().emplace_back(-y_mulconst0 * linear_term_scale_factor);
1616 block.q_4().emplace_back(0);
1617 block.q_c().emplace_back(-addconst0 * linear_term_scale_factor);
1618 block.set_gate_selector(3);
1619
1620 block.q_m().emplace_back(0);
1621 block.q_1().emplace_back(0);
1622 block.q_2().emplace_back(-x_mulconst1);
1623 block.q_3().emplace_back(-y_mulconst1);
1624 block.q_4().emplace_back(0);
1625 block.q_c().emplace_back(-addconst1);
1626 block.set_gate_selector(2);
1627
1628 block.q_m().emplace_back(0);
1629 block.q_1().emplace_back(-x_mulconst2);
1630 block.q_2().emplace_back(-y_mulconst2);
1631 block.q_3().emplace_back(1);
1632 block.q_4().emplace_back(0);
1633 block.q_c().emplace_back(-addconst2);
1634 block.set_gate_selector(1);
1635
1636 block.q_m().emplace_back(0);
1637 block.q_1().emplace_back(-x_mulconst3);
1638 block.q_2().emplace_back(-y_mulconst3);
1639 block.q_3().emplace_back(1);
1640 block.q_4().emplace_back(0);
1641 block.q_c().emplace_back(-addconst3);
1642 block.set_gate_selector(1);
1643
1644 check_selector_length_consistency();
1645
1646 this->increment_num_gates(4);
1648 z_0, z_1, z_2, z_3, z_p,
1649 };
1650}
1651
1657template <typename ExecutionTrace>
1660{
1661 const uint32_t& x_0 = std::get<0>(limb0).first;
1662 const uint32_t& x_1 = std::get<0>(limb1).first;
1663 const uint32_t& x_2 = std::get<0>(limb2).first;
1664 const uint32_t& x_3 = std::get<0>(limb3).first;
1665 const uint32_t& x_p = std::get<0>(limbp);
1666
1667 const FF& x_mulconst0 = std::get<0>(limb0).second;
1668 const FF& x_mulconst1 = std::get<0>(limb1).second;
1669 const FF& x_mulconst2 = std::get<0>(limb2).second;
1670 const FF& x_mulconst3 = std::get<0>(limb3).second;
1671
1672 const uint32_t& y_0 = std::get<1>(limb0).first;
1673 const uint32_t& y_1 = std::get<1>(limb1).first;
1674 const uint32_t& y_2 = std::get<1>(limb2).first;
1675 const uint32_t& y_3 = std::get<1>(limb3).first;
1676 const uint32_t& y_p = std::get<1>(limbp);
1677
1678 const FF& y_mulconst0 = std::get<1>(limb0).second;
1679 const FF& y_mulconst1 = std::get<1>(limb1).second;
1680 const FF& y_mulconst2 = std::get<1>(limb2).second;
1681 const FF& y_mulconst3 = std::get<1>(limb3).second;
1682
1683 // constant additive terms
1684 const FF& addconst0 = std::get<2>(limb0);
1685 const FF& addconst1 = std::get<2>(limb1);
1686 const FF& addconst2 = std::get<2>(limb2);
1687 const FF& addconst3 = std::get<2>(limb3);
1688 const FF& addconstp = std::get<2>(limbp);
1689
1690 // get value of result limbs
1691 const FF z_0value = (this->get_variable(x_0) * x_mulconst0) - (this->get_variable(y_0) * y_mulconst0) + addconst0;
1692 const FF z_1value = (this->get_variable(x_1) * x_mulconst1) - (this->get_variable(y_1) * y_mulconst1) + addconst1;
1693 const FF z_2value = (this->get_variable(x_2) * x_mulconst2) - (this->get_variable(y_2) * y_mulconst2) + addconst2;
1694 const FF z_3value = (this->get_variable(x_3) * x_mulconst3) - (this->get_variable(y_3) * y_mulconst3) + addconst3;
1695 const FF z_pvalue = this->get_variable(x_p) - this->get_variable(y_p) + addconstp;
1696
1697 const uint32_t z_0 = this->add_variable(z_0value);
1698 const uint32_t z_1 = this->add_variable(z_1value);
1699 const uint32_t z_2 = this->add_variable(z_2value);
1700 const uint32_t z_3 = this->add_variable(z_3value);
1701 const uint32_t z_p = this->add_variable(z_pvalue);
1702
1727 auto& block = blocks.arithmetic;
1728 block.populate_wires(y_p, x_0, y_0, z_p);
1729 block.populate_wires(x_p, x_1, y_1, z_0);
1730 block.populate_wires(x_2, y_2, z_2, z_1);
1731 block.populate_wires(x_3, y_3, z_3, this->zero_idx());
1732
1733 // When q_arith == 3, w_4_shift is scaled by 2 (see ArithmeticRelation for details). Therefore, for consistency we
1734 // also scale each linear term by this factor of 2 so that the constraint is effectively:
1735 // (q_l * w_1) + (q_r * w_2) + (q_o * w_3) + (q_4 * w_4) + q_c + w_4_shift = 0
1736 const FF linear_term_scale_factor = 2;
1737 block.q_m().emplace_back(-addconstp);
1738 block.q_1().emplace_back(0);
1739 block.q_2().emplace_back(-x_mulconst0 * linear_term_scale_factor);
1740 block.q_3().emplace_back(y_mulconst0 * linear_term_scale_factor);
1741 block.q_4().emplace_back(0);
1742 block.q_c().emplace_back(-addconst0 * linear_term_scale_factor);
1743 block.set_gate_selector(3);
1744
1745 block.q_m().emplace_back(0);
1746 block.q_1().emplace_back(0);
1747 block.q_2().emplace_back(-x_mulconst1);
1748 block.q_3().emplace_back(y_mulconst1);
1749 block.q_4().emplace_back(0);
1750 block.q_c().emplace_back(-addconst1);
1751 block.set_gate_selector(2);
1752
1753 block.q_m().emplace_back(0);
1754 block.q_1().emplace_back(-x_mulconst2);
1755 block.q_2().emplace_back(y_mulconst2);
1756 block.q_3().emplace_back(1);
1757 block.q_4().emplace_back(0);
1758 block.q_c().emplace_back(-addconst2);
1759 block.set_gate_selector(1);
1760
1761 block.q_m().emplace_back(0);
1762 block.q_1().emplace_back(-x_mulconst3);
1763 block.q_2().emplace_back(y_mulconst3);
1764 block.q_3().emplace_back(1);
1765 block.q_4().emplace_back(0);
1766 block.q_c().emplace_back(-addconst3);
1767 block.set_gate_selector(1);
1768
1769 check_selector_length_consistency();
1770
1771 this->increment_num_gates(4);
1773 z_0, z_1, z_2, z_3, z_p,
1774 };
1775}
1776
1786template <typename ExecutionTrace>
1788{
1789 return this->rom_ram_logic.create_ROM_array(array_size);
1790}
1791
1801template <typename ExecutionTrace>
1803{
1804 return this->rom_ram_logic.create_RAM_array(array_size);
1805}
1806
1814template <typename ExecutionTrace>
1816 const size_t index_value,
1817 const uint32_t value_witness)
1818{
1819 this->rom_ram_logic.init_RAM_element(this, ram_id, index_value, value_witness);
1820}
1821
1822template <typename ExecutionTrace>
1823uint32_t UltraCircuitBuilder_<ExecutionTrace>::read_RAM_array(const size_t ram_id, const uint32_t index_witness)
1824{
1825 return this->rom_ram_logic.read_RAM_array(this, ram_id, index_witness);
1826}
1827
1828template <typename ExecutionTrace>
1830 const uint32_t index_witness,
1831 const uint32_t value_witness)
1832{
1833 this->rom_ram_logic.write_RAM_array(this, ram_id, index_witness, value_witness);
1834}
1835
1851template <typename ExecutionTrace>
1853 const size_t index_value,
1854 const uint32_t value_witness)
1855{
1856 this->rom_ram_logic.set_ROM_element(this, rom_id, index_value, value_witness);
1857}
1858
1866template <typename ExecutionTrace>
1868 const size_t index_value,
1869 const std::array<uint32_t, 2>& value_witnesses)
1870{
1871 this->rom_ram_logic.set_ROM_element_pair(this, rom_id, index_value, value_witnesses);
1872}
1873
1881template <typename ExecutionTrace>
1882uint32_t UltraCircuitBuilder_<ExecutionTrace>::read_ROM_array(const size_t rom_id, const uint32_t index_witness)
1883{
1884 return this->rom_ram_logic.read_ROM_array(this, rom_id, index_witness);
1885}
1886
1894template <typename ExecutionTrace>
1895std::array<uint32_t, 2> UltraCircuitBuilder_<ExecutionTrace>::read_ROM_array_pair(const size_t rom_id,
1896 const uint32_t index_witness)
1897{
1898 return this->rom_ram_logic.read_ROM_array_pair(this, rom_id, index_witness);
1899}
1900
1904template <typename FF>
1906{
1907 auto& block = this->blocks.poseidon2_external;
1908 block.populate_wires(in.a, in.b, in.c, in.d);
1909 block.q_m().emplace_back(0);
1913 block.q_c().emplace_back(0);
1915 block.set_gate_selector(1);
1916 this->check_selector_length_consistency();
1917 this->increment_num_gates();
1918}
1919
1923template <typename FF>
1925{
1926 auto& block = this->blocks.poseidon2_internal;
1927 block.populate_wires(in.a, in.b, in.c, in.d);
1928 block.q_m().emplace_back(0);
1930 block.q_2().emplace_back(0);
1931 block.q_3().emplace_back(0);
1932 block.q_c().emplace_back(0);
1933 block.q_4().emplace_back(0);
1934 block.set_gate_selector(1);
1935 this->check_selector_length_consistency();
1936 this->increment_num_gates();
1937}
1938
1945template <typename ExecutionTrace> msgpack::sbuffer UltraCircuitBuilder_<ExecutionTrace>::export_circuit()
1946{
1947 // You should not name `zero` by yourself
1948 // but it will be rewritten anyway
1949 auto first_zero_idx = this->get_first_variable_in_class(this->zero_idx());
1950 if (!this->variable_names.contains(first_zero_idx)) {
1951 this->set_variable_name(this->zero_idx(), "zero");
1952 } else {
1953 this->variable_names[first_zero_idx] = "zero";
1954 }
1955 using base = CircuitBuilderBase<FF>;
1957
1958 std::array<uint64_t, 4> modulus = {
1959 FF::Params::modulus_0, FF::Params::modulus_1, FF::Params::modulus_2, FF::Params::modulus_3
1960 };
1961 std::stringstream buf;
1962 buf << std::hex << std::setfill('0') << std::setw(16) << modulus[3] << std::setw(16) << modulus[2] << std::setw(16)
1963 << modulus[1] << std::setw(16) << modulus[0];
1964
1965 cir.modulus = buf.str();
1966
1967 for (uint32_t i = 0; i < this->num_public_inputs(); i++) {
1968 cir.public_inps.push_back(this->real_variable_index[this->public_inputs()[i]]);
1969 }
1970
1971 for (auto& tup : base::variable_names) {
1972 cir.vars_of_interest.insert({ this->real_variable_index[tup.first], tup.second });
1973 }
1974
1975 for (const auto& var : this->get_variables()) {
1976 cir.variables.push_back(var);
1977 }
1978
1979 FF curve_b;
1980 if constexpr (FF::modulus == bb::fq::modulus) {
1981 curve_b = bb::g1::curve_b;
1982 } else if constexpr (FF::modulus == grumpkin::fq::modulus) {
1983 curve_b = grumpkin::g1::curve_b;
1984 } else {
1985 curve_b = 0;
1986 }
1987
1988 for (auto& block : blocks.get()) {
1989 std::vector<std::vector<FF>> block_selectors;
1991 for (size_t idx = 0; idx < block.size(); ++idx) {
1992 std::vector<FF> tmp_sel = { block.q_m()[idx],
1993 block.q_1()[idx],
1994 block.q_2()[idx],
1995 block.q_3()[idx],
1996 block.q_4()[idx],
1997 block.q_c()[idx],
1998 block.q_arith()[idx],
1999 block.q_delta_range()[idx],
2000 block.q_elliptic()[idx],
2001 block.q_memory()[idx],
2002 block.q_nnf()[idx],
2003 block.q_lookup()[idx],
2004 curve_b };
2005
2006 std::vector<uint32_t> tmp_w = {
2007 this->real_variable_index[block.w_l()[idx]],
2008 this->real_variable_index[block.w_r()[idx]],
2009 this->real_variable_index[block.w_o()[idx]],
2010 this->real_variable_index[block.w_4()[idx]],
2011 };
2012
2013 if (idx < block.size() - 1) {
2014 tmp_w.push_back(block.w_l()[idx + 1]);
2015 tmp_w.push_back(block.w_r()[idx + 1]);
2016 tmp_w.push_back(block.w_o()[idx + 1]);
2017 tmp_w.push_back(block.w_4()[idx + 1]);
2018 } else {
2019 tmp_w.push_back(0);
2020 tmp_w.push_back(0);
2021 tmp_w.push_back(0);
2022 tmp_w.push_back(0);
2023 }
2024
2025 block_selectors.push_back(tmp_sel);
2026 block_wires.push_back(tmp_w);
2027 }
2028 cir.selectors.push_back(block_selectors);
2029 cir.wires.push_back(block_wires);
2030 }
2031
2032 cir.real_variable_index = this->real_variable_index;
2033
2034 for (const auto& table : this->lookup_tables) {
2035 const FF table_index(table.table_index);
2036 info("Table no: ", table.table_index);
2037 std::vector<std::vector<FF>> tmp_table;
2038 for (size_t i = 0; i < table.size(); ++i) {
2039 tmp_table.push_back({ table.column_1[i], table.column_2[i], table.column_3[i] });
2040 }
2041 cir.lookup_tables.push_back(tmp_table);
2042 }
2043
2044 cir.real_variable_tags = this->real_variable_tags;
2045
2046 for (const auto& list : range_lists) {
2047 cir.range_tags[list.second.range_tag] = list.first;
2048 }
2049
2050 for (auto& rom_table : this->rom_ram_logic.rom_arrays) {
2051 std::sort(rom_table.records.begin(), rom_table.records.end());
2052
2054 table.reserve(rom_table.records.size());
2055 for (const auto& rom_entry : rom_table.records) {
2056 table.push_back({
2057 this->real_variable_index[rom_entry.index_witness],
2058 this->real_variable_index[rom_entry.value_column1_witness],
2059 this->real_variable_index[rom_entry.value_column2_witness],
2060 });
2061 }
2062 cir.rom_records.push_back(table);
2063 cir.rom_states.push_back(rom_table.state);
2064 }
2065
2066 for (auto& ram_table : this->rom_ram_logic.ram_arrays) {
2067 std::sort(ram_table.records.begin(), ram_table.records.end());
2068
2070 table.reserve(ram_table.records.size());
2071 for (const auto& ram_entry : ram_table.records) {
2072 table.push_back({ this->real_variable_index[ram_entry.index_witness],
2073 this->real_variable_index[ram_entry.value_witness],
2074 this->real_variable_index[ram_entry.timestamp_witness],
2075 ram_entry.access_type });
2076 }
2077 cir.ram_records.push_back(table);
2078 cir.ram_states.push_back(ram_table.state);
2079 }
2080
2081 cir.circuit_finalized = this->circuit_finalized;
2082
2083 msgpack::sbuffer buffer;
2084 msgpack::pack(buffer, cir);
2085 return buffer;
2086}
2087
2090
2091} // namespace bb
#define BB_ASSERT(expression,...)
Definition assert.hpp:80
#define BB_ASSERT_GTE(left, right,...)
Definition assert.hpp:138
#define BB_ASSERT_GT(left, right,...)
Definition assert.hpp:123
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:93
#define BB_ASSERT_LTE(left, right,...)
Definition assert.hpp:168
bb::field< bb::Bn254FrParams > FF
Definition field.cpp:22
#define BB_BENCH_NAME(name)
Definition bb_bench.hpp:219
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....
msgpack::sbuffer export_circuit() override
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....
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 create_ecc_add_gate(const ecc_add_gate_< FF > &in)
Create an elliptic curve addition gate.
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
void info(Args... args)
Definition log.hpp:89
FF a
FF b
uint8_t const * buf
Definition data_store.hpp:9
uint8_t buffer[RANDOM_BUFFER_SIZE]
Definition engine.cpp:34
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:174
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