barretenberg
Loading...
Searching...
No Matches
goblin_ultra_circuit_builder.hpp
1#pragma once
2#include "barretenberg/polynomials/polynomial.hpp"
3#include "barretenberg/proof_system/arithmetization/arithmetization.hpp"
4#include "barretenberg/proof_system/op_queue/ecc_op_queue.hpp"
5#include "barretenberg/proof_system/plookup_tables/plookup_tables.hpp"
6#include "barretenberg/proof_system/plookup_tables/types.hpp"
7#include "barretenberg/proof_system/types/merkle_hash_type.hpp"
8#include "barretenberg/proof_system/types/pedersen_commitment_type.hpp"
9#include "ultra_circuit_builder.hpp"
10#include <optional>
11
12namespace proof_system {
13
14using namespace barretenberg;
15
16template <typename FF> class GoblinUltraCircuitBuilder_ : public UltraCircuitBuilder_<arithmetization::UltraHonk<FF>> {
17 public:
18 static constexpr std::string_view NAME_STRING = "GoblinUltraArithmetization";
19 static constexpr CircuitType CIRCUIT_TYPE = CircuitType::ULTRA;
20 static constexpr size_t DEFAULT_NON_NATIVE_FIELD_LIMB_BITS =
21 UltraCircuitBuilder_<arithmetization::UltraHonk<FF>>::DEFAULT_NON_NATIVE_FIELD_LIMB_BITS;
22
23 size_t num_vars_added_in_constructor = 0; // needed in constructing circuit from acir
24
25 size_t num_ecc_op_gates = 0; // number of ecc op "gates" (rows); these are placed at the start of the circuit
26
27 // Stores record of ecc operations and performs corresponding native operations internally
28 std::shared_ptr<ECCOpQueue> op_queue;
29
30 // Indices for constant variables corresponding to ECCOpQueue op codes
31 uint32_t null_op_idx;
32 uint32_t add_accum_op_idx;
33 uint32_t mul_accum_op_idx;
34 uint32_t equality_op_idx;
35
36 using WireVector = std::vector<uint32_t, ContainerSlabAllocator<uint32_t>>;
37 using SelectorVector = std::vector<FF, ContainerSlabAllocator<FF>>;
38
39 // Wires storing ecc op queue data; values are indices into the variables array
40 std::array<WireVector, arithmetization::UltraHonk<FF>::NUM_WIRES> ecc_op_wires;
41
42 WireVector& ecc_op_wire_1() { return std::get<0>(ecc_op_wires); };
43 WireVector& ecc_op_wire_2() { return std::get<1>(ecc_op_wires); };
44 WireVector& ecc_op_wire_3() { return std::get<2>(ecc_op_wires); };
45 WireVector& ecc_op_wire_4() { return std::get<3>(ecc_op_wires); };
46
47 const WireVector& ecc_op_wire_1() const { return std::get<0>(ecc_op_wires); };
48 const WireVector& ecc_op_wire_2() const { return std::get<1>(ecc_op_wires); };
49 const WireVector& ecc_op_wire_3() const { return std::get<2>(ecc_op_wires); };
50 const WireVector& ecc_op_wire_4() const { return std::get<3>(ecc_op_wires); };
51
52 SelectorVector& q_busread() { return this->selectors.q_busread(); };
53 SelectorVector& q_poseidon2_external() { return this->selectors.q_poseidon2_external(); };
54 SelectorVector& q_poseidon2_internal() { return this->selectors.q_poseidon2_internal(); };
55
56 const SelectorVector& q_busread() const { return this->selectors.q_busread(); };
57 const SelectorVector& q_poseidon2_external() const { return this->selectors.q_poseidon2_external(); };
58 const SelectorVector& q_poseidon2_internal() const { return this->selectors.q_poseidon2_internal(); };
59
60 // DataBus call/return data arrays
61 std::vector<uint32_t> public_calldata;
62 std::vector<uint32_t> calldata_read_counts;
63 std::vector<uint32_t> public_return_data;
64
65 // Functions for adding ECC op queue "gates"
67 ecc_op_tuple queue_ecc_mul_accum(const g1::affine_element& point, const FF& scalar);
69
70 private:
71 void populate_ecc_op_wires(const ecc_op_tuple& in);
72 ecc_op_tuple decompose_ecc_operands(uint32_t op, const g1::affine_element& point, const FF& scalar = FF::zero());
73
74 public:
75 GoblinUltraCircuitBuilder_(const size_t size_hint = 0,
76 std::shared_ptr<ECCOpQueue> op_queue_in = std::make_shared<ECCOpQueue>())
78 , op_queue(op_queue_in)
79 {
80 // Set indices to constants corresponding to Goblin ECC op codes
81 null_op_idx = this->zero_idx;
82 add_accum_op_idx = this->put_constant_variable(FF(EccOpCode::ADD_ACCUM));
83 mul_accum_op_idx = this->put_constant_variable(FF(EccOpCode::MUL_ACCUM));
84 equality_op_idx = this->put_constant_variable(FF(EccOpCode::EQUALITY));
85 num_vars_added_in_constructor = this->variables.size();
86 };
87 GoblinUltraCircuitBuilder_(std::shared_ptr<ECCOpQueue> op_queue_in)
88 : GoblinUltraCircuitBuilder_(0, op_queue_in)
89 {}
90
91 void finalize_circuit();
93
94 size_t get_num_constant_gates() const override { return 0; }
95
106 size_t get_num_gates() const override
107 {
109 return num_ultra_gates + num_ecc_op_gates;
110 }
111
116 virtual void print_num_gates() const override
117 {
118 size_t count = 0;
119 size_t rangecount = 0;
120 size_t romcount = 0;
121 size_t ramcount = 0;
122 size_t nnfcount = 0;
124 count, rangecount, romcount, ramcount, nnfcount);
125
126 size_t total = count + romcount + ramcount + rangecount + num_ecc_op_gates;
127 std::cout << "gates = " << total << " (arith " << count << ", rom " << romcount << ", ram " << ramcount
128 << ", range " << rangecount << ", non native field gates " << nnfcount << ", goblin ecc op gates "
129 << num_ecc_op_gates << "), pubinp = " << this->public_inputs.size() << std::endl;
130 }
131
137 void set_public_calldata(const uint32_t witness_index)
138 {
139 for (const uint32_t calldata : public_calldata) {
140 if (calldata == witness_index) {
141 if (!this->failed()) {
142 this->failure("Attempted to redundantly set a public calldata!");
143 }
144 return;
145 }
146 }
147 public_calldata.emplace_back(witness_index);
148 }
149 void create_poseidon2_external_gate(const poseidon2_external_gate_<FF>& in);
150 void create_poseidon2_internal_gate(const poseidon2_internal_gate_<FF>& in);
151
152 FF compute_poseidon2_external_identity(FF q_poseidon2_external_value,
153 FF q_1_value,
154 FF q_2_value,
155 FF q_3_value,
156 FF q_4_value,
157 FF w_1_value,
158 FF w_2_value,
159 FF w_3_value,
160 FF w_4_value,
161 FF w_1_shifted_value,
162 FF w_2_shifted_value,
163 FF w_3_shifted_value,
164 FF w_4_shifted_value,
165 FF alpha_base,
166 FF alpha) const;
167
168 FF compute_poseidon2_internal_identity(FF q_poseidon2_internal_value,
169 FF q_1_value,
170 FF w_1_value,
171 FF w_2_value,
172 FF w_3_value,
173 FF w_4_value,
174 FF w_1_shifted_value,
175 FF w_2_shifted_value,
176 FF w_3_shifted_value,
177 FF w_4_shifted_value,
178 FF alpha_base,
179 FF alpha) const;
180
181 bool check_circuit();
182};
183extern template class GoblinUltraCircuitBuilder_<barretenberg::fr>;
184using GoblinUltraCircuitBuilder = GoblinUltraCircuitBuilder_<barretenberg::fr>;
185} // namespace proof_system
Definition: affine_element.hpp:11
Definition: goblin_ultra_circuit_builder.hpp:16
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...
Definition: goblin_ultra_circuit_builder.cpp:26
ecc_op_tuple queue_ecc_eq()
Add point equality gates based on the current value of the accumulator internal to the op queue and a...
Definition: goblin_ultra_circuit_builder.cpp:162
ecc_op_tuple queue_ecc_mul_accum(const g1::affine_element &point, const FF &scalar)
Add gates for point mul-then-accumulate and add the raw operation data to the op queue.
Definition: goblin_ultra_circuit_builder.cpp:143
size_t get_num_gates() const override
Get the final number of gates in a circuit, which consists of the sum of: 1) Current number number of...
Definition: goblin_ultra_circuit_builder.hpp:106
void set_public_calldata(const uint32_t witness_index)
Definition: goblin_ultra_circuit_builder.hpp:137
ecc_op_tuple queue_ecc_add_accum(const g1::affine_element &point)
Add gates for simple point addition (no mul) and add the raw operation data to the op queue.
Definition: goblin_ultra_circuit_builder.cpp:122
virtual void print_num_gates() const override
Print the number and composition of gates in the circuit.
Definition: goblin_ultra_circuit_builder.hpp:116
Definition: ultra_circuit_builder.hpp:31
void get_num_gates_split_into_components(size_t &count, size_t &rangecount, size_t &romcount, size_t &ramcount, size_t &nnfcount) const
Get the final number of gates in a circuit, which consists of the sum of: 1) Current number number of...
Definition: ultra_circuit_builder.hpp:801
constexpr_utils defines some helper methods that perform some stl-equivalent operations but in a cons...
Definition: constexpr_utils.hpp:16
Definition: gate_data.hpp:64
Definition: gate_data.hpp:136
Definition: gate_data.hpp:144