Skip to content

Commit

Permalink
feat: copy constructors for builders (AztecProtocol#3635)
Browse files Browse the repository at this point in the history
This work removes all reference data members from the circuit builders
so that they can be default copy constructed.
  • Loading branch information
ledwards2225 authored Dec 12, 2023
1 parent fd6eefe commit b82b0c5
Show file tree
Hide file tree
Showing 12 changed files with 1,058 additions and 914 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ void UltraComposer::compute_witness(CircuitBuilder& circuit_constructor)
// Note: the remaining NUM_RESERVED_GATES indices are padded with zeros within `compute_witness_base` (called
// next).
for (size_t i = filled_gates; i < total_num_gates; ++i) {
circuit_constructor.w_l.emplace_back(circuit_constructor.zero_idx);
circuit_constructor.w_r.emplace_back(circuit_constructor.zero_idx);
circuit_constructor.w_o.emplace_back(circuit_constructor.zero_idx);
circuit_constructor.w_4.emplace_back(circuit_constructor.zero_idx);
circuit_constructor.w_l().emplace_back(circuit_constructor.zero_idx);
circuit_constructor.w_r().emplace_back(circuit_constructor.zero_idx);
circuit_constructor.w_o().emplace_back(circuit_constructor.zero_idx);
circuit_constructor.w_4().emplace_back(circuit_constructor.zero_idx);
}

auto wire_polynomial_evaluations = construct_wire_polynomials_base<Flavor>(circuit_constructor, subgroup_size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ template <typename FF_> class Standard {
SelectorType& q_3() { return selectors[3]; };
SelectorType& q_c() { return selectors[4]; };

const SelectorType& q_m() const { return selectors[0]; };
const SelectorType& q_1() const { return selectors[1]; };
const SelectorType& q_2() const { return selectors[2]; };
const SelectorType& q_3() const { return selectors[3]; };
const SelectorType& q_c() const { return selectors[4]; };

Standard()
: selectors(NUM_SELECTORS)
{}
Expand Down Expand Up @@ -87,6 +93,18 @@ template <typename FF_> class Ultra {
SelectorType& q_aux() { return selectors[9]; };
SelectorType& q_lookup_type() { return selectors[10]; };

const SelectorType& q_m() const { return selectors[0]; };
const SelectorType& q_c() const { return selectors[1]; };
const SelectorType& q_1() const { return selectors[2]; };
const SelectorType& q_2() const { return selectors[3]; };
const SelectorType& q_3() const { return selectors[4]; };
const SelectorType& q_4() const { return selectors[5]; };
const SelectorType& q_arith() const { return selectors[6]; };
const SelectorType& q_sort() const { return selectors[7]; };
const SelectorType& q_elliptic() const { return selectors[8]; };
const SelectorType& q_aux() const { return selectors[9]; };
const SelectorType& q_lookup_type() const { return selectors[10]; };

const auto& get() const { return selectors; };

void reserve(size_t size_hint)
Expand Down Expand Up @@ -141,6 +159,21 @@ template <typename FF_> class UltraHonk {
SelectorType& q_poseidon2_external() { return this->selectors[12]; };
SelectorType& q_poseidon2_internal() { return this->selectors[13]; };

const SelectorType& q_m() const { return selectors[0]; };
const SelectorType& q_c() const { return selectors[1]; };
const SelectorType& q_1() const { return selectors[2]; };
const SelectorType& q_2() const { return selectors[3]; };
const SelectorType& q_3() const { return selectors[4]; };
const SelectorType& q_4() const { return selectors[5]; };
const SelectorType& q_arith() const { return selectors[6]; };
const SelectorType& q_sort() const { return selectors[7]; };
const SelectorType& q_elliptic() const { return selectors[8]; };
const SelectorType& q_aux() const { return selectors[9]; };
const SelectorType& q_lookup_type() const { return selectors[10]; };
const SelectorType& q_busread() const { return selectors[11]; };
const SelectorType& q_poseidon2_external() const { return this->selectors[12]; };
const SelectorType& q_poseidon2_internal() const { return this->selectors[13]; };

const auto& get() const { return selectors; };

void reserve(size_t size_hint)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,72 +43,72 @@ template <typename FF> void GoblinUltraCircuitBuilder_<FF>::add_gates_to_ensure_
}

// Construct gate corresponding to a single calldata read
size_t read_idx = 1; // index into calldata array at which we want to read
this->w_l.emplace_back(public_calldata[read_idx]); // populate with value of calldata at read index
this->w_r.emplace_back(this->add_variable(FF(read_idx))); // populate with read index as witness
calldata_read_counts[read_idx]++; // increment read count at read index
q_busread().emplace_back(1); // read selector on
size_t read_idx = 1; // index into calldata array at which we want to read
this->w_l().emplace_back(public_calldata[read_idx]); // populate with value of calldata at read index
this->w_r().emplace_back(this->add_variable(FF(read_idx))); // populate with read index as witness
calldata_read_counts[read_idx]++; // increment read count at read index
q_busread().emplace_back(1); // read selector on

// populate all other components with zero
this->w_o.emplace_back(this->zero_idx);
this->w_4.emplace_back(this->zero_idx);
this->q_m.emplace_back(0);
this->q_1.emplace_back(0);
this->q_2.emplace_back(0);
this->q_3.emplace_back(0);
this->q_c.emplace_back(0);
this->q_sort.emplace_back(0);
this->q_arith.emplace_back(0);
this->q_4.emplace_back(0);
this->q_lookup_type.emplace_back(0);
this->q_elliptic.emplace_back(0);
this->q_aux.emplace_back(0);
this->q_poseidon2_external.emplace_back(0);
this->q_poseidon2_internal.emplace_back(0);
this->w_o().emplace_back(this->zero_idx);
this->w_4().emplace_back(this->zero_idx);
this->q_m().emplace_back(0);
this->q_1().emplace_back(0);
this->q_2().emplace_back(0);
this->q_3().emplace_back(0);
this->q_c().emplace_back(0);
this->q_sort().emplace_back(0);
this->q_arith().emplace_back(0);
this->q_4().emplace_back(0);
this->q_lookup_type().emplace_back(0);
this->q_elliptic().emplace_back(0);
this->q_aux().emplace_back(0);
this->q_poseidon2_external().emplace_back(0);
this->q_poseidon2_internal().emplace_back(0);

++this->num_gates;

// mock gates that use poseidon selectors, with all zeros as input
this->w_l.emplace_back(this->zero_idx);
this->w_r.emplace_back(this->zero_idx);
this->w_o.emplace_back(this->zero_idx);
this->w_4.emplace_back(this->zero_idx);
this->q_m.emplace_back(0);
this->q_1.emplace_back(0);
this->q_2.emplace_back(0);
this->q_3.emplace_back(0);
this->q_c.emplace_back(0);
this->q_arith.emplace_back(0);
this->q_4.emplace_back(0);
this->q_sort.emplace_back(0);
this->q_lookup_type.emplace_back(0);
this->q_elliptic.emplace_back(0);
this->q_aux.emplace_back(0);
this->w_l().emplace_back(this->zero_idx);
this->w_r().emplace_back(this->zero_idx);
this->w_o().emplace_back(this->zero_idx);
this->w_4().emplace_back(this->zero_idx);
this->q_m().emplace_back(0);
this->q_1().emplace_back(0);
this->q_2().emplace_back(0);
this->q_3().emplace_back(0);
this->q_c().emplace_back(0);
this->q_arith().emplace_back(0);
this->q_4().emplace_back(0);
this->q_sort().emplace_back(0);
this->q_lookup_type().emplace_back(0);
this->q_elliptic().emplace_back(0);
this->q_aux().emplace_back(0);
this->q_busread().emplace_back(0);
this->q_poseidon2_external.emplace_back(1);
this->q_poseidon2_internal.emplace_back(1);
this->q_poseidon2_external().emplace_back(1);
this->q_poseidon2_internal().emplace_back(1);

++this->num_gates;

// second gate that stores the output of all zeros of the poseidon gates
this->w_l.emplace_back(this->zero_idx);
this->w_r.emplace_back(this->zero_idx);
this->w_o.emplace_back(this->zero_idx);
this->w_4.emplace_back(this->zero_idx);
this->q_m.emplace_back(0);
this->q_1.emplace_back(0);
this->q_2.emplace_back(0);
this->q_3.emplace_back(0);
this->q_c.emplace_back(0);
this->q_arith.emplace_back(0);
this->q_4.emplace_back(0);
this->q_sort.emplace_back(0);
this->q_lookup_type.emplace_back(0);
this->q_elliptic.emplace_back(0);
this->q_aux.emplace_back(0);
this->w_l().emplace_back(this->zero_idx);
this->w_r().emplace_back(this->zero_idx);
this->w_o().emplace_back(this->zero_idx);
this->w_4().emplace_back(this->zero_idx);
this->q_m().emplace_back(0);
this->q_1().emplace_back(0);
this->q_2().emplace_back(0);
this->q_3().emplace_back(0);
this->q_c().emplace_back(0);
this->q_arith().emplace_back(0);
this->q_4().emplace_back(0);
this->q_sort().emplace_back(0);
this->q_lookup_type().emplace_back(0);
this->q_elliptic().emplace_back(0);
this->q_aux().emplace_back(0);
this->q_busread().emplace_back(0);
this->q_poseidon2_external.emplace_back(0);
this->q_poseidon2_internal.emplace_back(0);
this->q_poseidon2_external().emplace_back(0);
this->q_poseidon2_internal().emplace_back(0);

++this->num_gates;
}
Expand Down Expand Up @@ -233,64 +233,64 @@ ecc_op_tuple GoblinUltraCircuitBuilder_<FF>::decompose_ecc_operands(uint32_t op_
*/
template <typename FF> void GoblinUltraCircuitBuilder_<FF>::populate_ecc_op_wires(const ecc_op_tuple& in)
{
ecc_op_wire_1.emplace_back(in.op);
ecc_op_wire_2.emplace_back(in.x_lo);
ecc_op_wire_3.emplace_back(in.x_hi);
ecc_op_wire_4.emplace_back(in.y_lo);
ecc_op_wire_1().emplace_back(in.op);
ecc_op_wire_2().emplace_back(in.x_lo);
ecc_op_wire_3().emplace_back(in.x_hi);
ecc_op_wire_4().emplace_back(in.y_lo);

ecc_op_wire_1.emplace_back(this->zero_idx);
ecc_op_wire_2.emplace_back(in.y_hi);
ecc_op_wire_3.emplace_back(in.z_1);
ecc_op_wire_4.emplace_back(in.z_2);
ecc_op_wire_1().emplace_back(this->zero_idx);
ecc_op_wire_2().emplace_back(in.y_hi);
ecc_op_wire_3().emplace_back(in.z_1);
ecc_op_wire_4().emplace_back(in.z_2);

num_ecc_op_gates += 2;
};

template <typename FF>
void GoblinUltraCircuitBuilder_<FF>::create_poseidon2_external_gate(const poseidon2_external_gate_<FF>& in)
{
this->w_l.emplace_back(in.a);
this->w_r.emplace_back(in.b);
this->w_o.emplace_back(in.c);
this->w_4.emplace_back(in.d);
this->q_m.emplace_back(0);
this->q_1.emplace_back(Poseidon2Bn254ScalarFieldParams::round_constants[in.round_idx][0]);
this->q_2.emplace_back(Poseidon2Bn254ScalarFieldParams::round_constants[in.round_idx][1]);
this->q_3.emplace_back(Poseidon2Bn254ScalarFieldParams::round_constants[in.round_idx][2]);
this->q_c.emplace_back(0);
this->q_arith.emplace_back(0);
this->q_4.emplace_back(Poseidon2Bn254ScalarFieldParams::round_constants[in.round_idx][3]);
this->q_sort.emplace_back(0);
this->q_lookup_type.emplace_back(0);
this->q_elliptic.emplace_back(0);
this->q_aux.emplace_back(0);
this->w_l().emplace_back(in.a);
this->w_r().emplace_back(in.b);
this->w_o().emplace_back(in.c);
this->w_4().emplace_back(in.d);
this->q_m().emplace_back(0);
this->q_1().emplace_back(Poseidon2Bn254ScalarFieldParams::round_constants[in.round_idx][0]);
this->q_2().emplace_back(Poseidon2Bn254ScalarFieldParams::round_constants[in.round_idx][1]);
this->q_3().emplace_back(Poseidon2Bn254ScalarFieldParams::round_constants[in.round_idx][2]);
this->q_c().emplace_back(0);
this->q_arith().emplace_back(0);
this->q_4().emplace_back(Poseidon2Bn254ScalarFieldParams::round_constants[in.round_idx][3]);
this->q_sort().emplace_back(0);
this->q_lookup_type().emplace_back(0);
this->q_elliptic().emplace_back(0);
this->q_aux().emplace_back(0);
this->q_busread().emplace_back(0);
this->q_poseidon2_external.emplace_back(1);
this->q_poseidon2_internal.emplace_back(0);
this->q_poseidon2_external().emplace_back(1);
this->q_poseidon2_internal().emplace_back(0);
++this->num_gates;
}

template <typename FF>
void GoblinUltraCircuitBuilder_<FF>::create_poseidon2_internal_gate(const poseidon2_internal_gate_<FF>& in)
{
this->w_l.emplace_back(in.a);
this->w_r.emplace_back(in.b);
this->w_o.emplace_back(in.c);
this->w_4.emplace_back(in.d);
this->q_m.emplace_back(0);
this->q_1.emplace_back(Poseidon2Bn254ScalarFieldParams::round_constants[in.round_idx][0]);
this->q_2.emplace_back(0);
this->q_3.emplace_back(0);
this->q_c.emplace_back(0);
this->q_arith.emplace_back(0);
this->q_4.emplace_back(0);
this->q_sort.emplace_back(0);
this->q_lookup_type.emplace_back(0);
this->q_elliptic.emplace_back(0);
this->q_aux.emplace_back(0);
this->w_l().emplace_back(in.a);
this->w_r().emplace_back(in.b);
this->w_o().emplace_back(in.c);
this->w_4().emplace_back(in.d);
this->q_m().emplace_back(0);
this->q_1().emplace_back(Poseidon2Bn254ScalarFieldParams::round_constants[in.round_idx][0]);
this->q_2().emplace_back(0);
this->q_3().emplace_back(0);
this->q_c().emplace_back(0);
this->q_arith().emplace_back(0);
this->q_4().emplace_back(0);
this->q_sort().emplace_back(0);
this->q_lookup_type().emplace_back(0);
this->q_elliptic().emplace_back(0);
this->q_aux().emplace_back(0);
this->q_busread().emplace_back(0);
this->q_poseidon2_external.emplace_back(0);
this->q_poseidon2_internal.emplace_back(1);
this->q_poseidon2_external().emplace_back(0);
this->q_poseidon2_internal().emplace_back(1);
++this->num_gates;
}

Expand Down Expand Up @@ -420,25 +420,25 @@ template <typename FF> bool GoblinUltraCircuitBuilder_<FF>::check_circuit()
FF w_3_value;
FF w_4_value;
// Get the values of selectors and wires and update tag products along the way
q_poseidon2_external_value = this->q_poseidon2_external[i];
q_poseidon2_internal_value = this->q_poseidon2_internal[i];
q_1_value = this->q_1[i];
q_2_value = this->q_2[i];
q_3_value = this->q_3[i];
q_4_value = this->q_4[i];
w_1_value = this->get_variable(this->w_l[i]);
w_2_value = this->get_variable(this->w_r[i]);
w_3_value = this->get_variable(this->w_o[i]);
w_4_value = this->get_variable(this->w_4[i]);
q_poseidon2_external_value = this->q_poseidon2_external()[i];
q_poseidon2_internal_value = this->q_poseidon2_internal()[i];
q_1_value = this->q_1()[i];
q_2_value = this->q_2()[i];
q_3_value = this->q_3()[i];
q_4_value = this->q_4()[i];
w_1_value = this->get_variable(this->w_l()[i]);
w_2_value = this->get_variable(this->w_r()[i]);
w_3_value = this->get_variable(this->w_o()[i]);
w_4_value = this->get_variable(this->w_4()[i]);
FF w_1_shifted_value;
FF w_2_shifted_value;
FF w_3_shifted_value;
FF w_4_shifted_value;
if (i < (this->num_gates - 1)) {
w_1_shifted_value = this->get_variable(this->w_l[i + 1]);
w_2_shifted_value = this->get_variable(this->w_r[i + 1]);
w_3_shifted_value = this->get_variable(this->w_o[i + 1]);
w_4_shifted_value = this->get_variable(this->w_4[i + 1]);
w_1_shifted_value = this->get_variable(this->w_l()[i + 1]);
w_2_shifted_value = this->get_variable(this->w_r()[i + 1]);
w_3_shifted_value = this->get_variable(this->w_o()[i + 1]);
w_4_shifted_value = this->get_variable(this->w_4()[i + 1]);
} else {
w_1_shifted_value = FF::zero();
w_2_shifted_value = FF::zero();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,23 @@ template <typename FF> class GoblinUltraCircuitBuilder_ : public UltraCircuitBui
// Wires storing ecc op queue data; values are indices into the variables array
std::array<WireVector, arithmetization::UltraHonk<FF>::NUM_WIRES> ecc_op_wires;

WireVector& ecc_op_wire_1 = std::get<0>(ecc_op_wires);
WireVector& ecc_op_wire_2 = std::get<1>(ecc_op_wires);
WireVector& ecc_op_wire_3 = std::get<2>(ecc_op_wires);
WireVector& ecc_op_wire_4 = std::get<3>(ecc_op_wires);
WireVector& ecc_op_wire_1() { return std::get<0>(ecc_op_wires); };
WireVector& ecc_op_wire_2() { return std::get<1>(ecc_op_wires); };
WireVector& ecc_op_wire_3() { return std::get<2>(ecc_op_wires); };
WireVector& ecc_op_wire_4() { return std::get<3>(ecc_op_wires); };

const WireVector& ecc_op_wire_1() const { return std::get<0>(ecc_op_wires); };
const WireVector& ecc_op_wire_2() const { return std::get<1>(ecc_op_wires); };
const WireVector& ecc_op_wire_3() const { return std::get<2>(ecc_op_wires); };
const WireVector& ecc_op_wire_4() const { return std::get<3>(ecc_op_wires); };

SelectorVector& q_busread() { return this->selectors.q_busread(); };
SelectorVector& q_poseidon2_external = this->selectors.q_poseidon2_external();
SelectorVector& q_poseidon2_internal = this->selectors.q_poseidon2_internal();
SelectorVector& q_poseidon2_external() { return this->selectors.q_poseidon2_external(); };
SelectorVector& q_poseidon2_internal() { return this->selectors.q_poseidon2_internal(); };

const SelectorVector& q_busread() const { return this->selectors.q_busread(); };
const SelectorVector& q_poseidon2_external() const { return this->selectors.q_poseidon2_external(); };
const SelectorVector& q_poseidon2_internal() const { return this->selectors.q_poseidon2_internal(); };

// DataBus call/return data arrays
std::vector<uint32_t> public_calldata;
Expand Down
Loading

0 comments on commit b82b0c5

Please sign in to comment.