Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
mock_circuit_producer.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
7#pragma once
8
14
15using namespace bb;
16
17namespace {
18
25class MockDatabusProducer {
26 private:
27 using ClientCircuit = Chonk::ClientCircuit;
28 using Flavor = MegaFlavor;
29 using FF = Flavor::FF;
30 using BusDataArray = std::vector<FF>;
31
32 static constexpr size_t BUS_ARRAY_SIZE = 3; // arbitrary length of mock bus inputs
33 BusDataArray app_return_data;
34 BusDataArray kernel_return_data;
35
36 FF dummy_return_val = 1; // use simple return val for easier test debugging
37
38 BusDataArray generate_random_bus_array()
39 {
40 BusDataArray result;
41 for (size_t i = 0; i < BUS_ARRAY_SIZE; ++i) {
42 result.emplace_back(dummy_return_val);
43 }
44 dummy_return_val += 1;
45 return result;
46 }
47
48 public:
52 void populate_app_databus(ClientCircuit& circuit)
53 {
54 app_return_data = generate_random_bus_array();
55 for (auto& val : app_return_data) {
56 circuit.add_public_return_data(circuit.add_variable(val));
57 }
58 };
59
64 void populate_kernel_databus(ClientCircuit& circuit)
65 {
66 // Populate calldata from previous kernel return data (if it exists)
67 for (auto& val : kernel_return_data) {
68 circuit.add_public_calldata(circuit.add_variable(val));
69 }
70 // Populate secondary_calldata from app return data (if it exists), then clear the app return data
71 for (auto& val : app_return_data) {
72 circuit.add_public_secondary_calldata(circuit.add_variable(val));
73 }
74 app_return_data.clear();
75
76 // Mock the return data for the present kernel circuit
77 kernel_return_data = generate_random_bus_array();
78 for (auto& val : kernel_return_data) {
79 circuit.add_public_return_data(circuit.add_variable(val));
80 }
81 };
82
87 void tamper_with_app_return_data() { app_return_data.emplace_back(17); }
88};
89
94struct TestSettings {
95 // number of public inputs to manually add to circuits, by default this would be 0 because we use the
96 // MockDatabusProducer to test public inputs handling
97 size_t num_public_inputs = 0;
98 // by default we will create more complex apps and kernel with various types of gates but in case we want to
99 // specifically test overflow behaviour or unstructured circuits we can manually construct simple circuits with a
100 // specified number of gates
101 size_t log2_num_gates = 0;
102};
103
112class PrivateFunctionExecutionMockCircuitProducer {
113 using ClientCircuit = Chonk::ClientCircuit;
114 using Flavor = MegaFlavor;
116
117 size_t circuit_counter = 0;
118 std::vector<bool> is_kernel_flags;
119
120 MockDatabusProducer mock_databus;
121 bool large_first_app = true;
122 constexpr static size_t NUM_TRAILING_KERNELS = 3; // reset, tail, hiding
123
124 public:
125 size_t total_num_circuits = 0;
126
127 PrivateFunctionExecutionMockCircuitProducer(size_t num_app_circuits, bool large_first_app = true)
128 : large_first_app(large_first_app)
129 , total_num_circuits(num_app_circuits * 2 +
130 NUM_TRAILING_KERNELS) /*One kernel per app, plus a fixed number of final kernels*/
131 {
132 // Set flags indicating which circuits are kernels vs apps
133 is_kernel_flags.resize(total_num_circuits, true);
134 for (size_t i = 0; i < num_app_circuits; ++i) {
135 is_kernel_flags[2 * i] = false; // every other circuit is an app
136 }
137 }
138
143 static std::shared_ptr<VerificationKey> get_verification_key(ClientCircuit& builder_in)
144 {
145 // This is a workaround to ensure that the circuit is finalized before we create the verification key
146 // In practice, this should not be needed as the circuit will be finalized when it is accumulated into the IVC
147 // but this is a workaround for the test setup.
149
150 // Deepcopy the opqueue to avoid modifying the original one when finalising the circuit
153 std::shared_ptr<VerificationKey> vk = std::make_shared<VerificationKey>(prover_instance->get_precomputed());
154 return vk;
155 }
156
163 ClientCircuit create_next_circuit(Chonk& ivc,
164 size_t log2_num_gates = 0,
165 size_t num_public_inputs = 0,
166 bool check_circuit_sizes = false)
167 {
168 const bool is_kernel = is_kernel_flags[circuit_counter++];
169 const bool use_large_circuit = large_first_app && (circuit_counter == 1); // first circuit is size 2^19
170 // Check if this is one of the trailing kernels (reset, tail, hiding)
171 const bool is_trailing_kernel = (ivc.num_circuits_accumulated >= ivc.get_num_circuits() - NUM_TRAILING_KERNELS);
172
173 ClientCircuit circuit{ ivc.goblin.op_queue };
174 // if the number of gates is specified we just add a number of arithmetic gates
175 if (log2_num_gates != 0) {
176 MockCircuits::construct_arithmetic_circuit(circuit, log2_num_gates, /* include_public_inputs= */ false);
177 // Add some public inputs
178 for (size_t i = 0; i < num_public_inputs; ++i) {
179 circuit.add_public_variable(typename Flavor::FF(13634816 + i)); // arbitrary number
180 }
181 } else {
182 // If the number of gates is not specified we create a structured mock circuit
183 if (is_kernel) {
184 // For trailing kernels (reset, tail, hiding), skip the expensive mock kernel logic to match real Noir
185 // flows. These kernels are simpler and mainly contain the completion logic added by Chonk.
186 if (!is_trailing_kernel) {
187 GoblinMockCircuits::construct_mock_folding_kernel(circuit); // construct mock base logic
188 }
189 mock_databus.populate_kernel_databus(circuit); // populate databus inputs/outputs
190 } else {
191 GoblinMockCircuits::construct_mock_app_circuit(circuit, use_large_circuit); // construct mock app
192 mock_databus.populate_app_databus(circuit); // populate databus outputs
193 }
194 }
195
196 if (is_kernel) {
198 } else {
200 }
201
202 if (check_circuit_sizes) {
203 auto prover_instance = std::make_shared<Chonk::ProverInstance>(circuit);
204 size_t log2_dyadic_size = numeric::get_msb(prover_instance->get_metadata().dyadic_size);
205 if (log2_num_gates != 0) {
206 if (is_kernel) {
207 // There are various possibilities here, so we provide a bound
209 log2_dyadic_size,
210 19UL,
211 "Log number of gates in a kernel with fixed number of arithmetic gates has exceeded bound.");
212 vinfo("Log number of gates in a kernel with fixed number of arithmetic gates is: ",
213 log2_dyadic_size);
214 } else {
215 // The offset is due to the fact that finalization adds a certain number of gates
216 size_t LOG2_OFFSET = 2;
217 BB_ASSERT_LTE(log2_dyadic_size,
218 log2_num_gates + LOG2_OFFSET,
219 "Log number of arithemtic gates produced is different from the one requested.");
220 }
221 } else {
222 if (is_kernel) {
223 // Trailing kernels (reset, tail, hiding) are simpler than regular kernels
224 if (is_trailing_kernel) {
225 // Trailing kernels should be significantly smaller, with hiding kernel < 2^16
226 BB_ASSERT_LTE(log2_dyadic_size,
227 16UL,
228 "Trailing kernel circuit size has exceeded expected bound (should be <= 2^16).");
229 vinfo("Log number of gates in a trailing kernel circuit is: ", log2_dyadic_size);
230 } else {
231 BB_ASSERT_EQ(log2_dyadic_size,
232 18UL,
233 "There has been a change in the number of gates of a mock kernel circuit.");
234 }
235 } else {
236 BB_ASSERT_EQ(log2_dyadic_size,
237 use_large_circuit ? 19UL : 17UL,
238 "There has been a change in the of gates generated for a mock app circuit.");
239 }
240 }
241 }
242 return circuit;
243 }
244
249 Chonk& ivc, TestSettings settings = {}, bool check_circuit_size = false)
250 {
251 // If this is a mock hiding kernel, remove the settings and use a default (non-structured) trace
252 if (ivc.num_circuits_accumulated == ivc.get_num_circuits() - 1) {
253 settings = TestSettings{};
254 }
255 auto circuit =
256 create_next_circuit(ivc, settings.log2_num_gates, settings.num_public_inputs, check_circuit_size);
257 return { circuit, get_verification_key(circuit) };
258 }
259
260 void construct_and_accumulate_next_circuit(Chonk& ivc, TestSettings settings = {}, bool check_circuit_sizes = false)
261 {
262 auto [circuit, vk] = create_next_circuit_and_vk(ivc, settings, check_circuit_sizes);
263 ivc.accumulate(circuit, vk);
264 }
265
269 void tamper_with_databus() { mock_databus.tamper_with_app_return_data(); }
270};
271
272} // namespace
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:77
#define BB_ASSERT_LTE(left, right,...)
Definition assert.hpp:152
The IVC scheme used by the aztec client for private function execution.
Definition chonk.hpp:38
void complete_kernel_circuit_logic(ClientCircuit &circuit)
Append logic to complete a kernel circuit.
Definition chonk.cpp:221
size_t num_circuits_accumulated
Definition chonk.hpp:260
size_t get_num_circuits() const
Definition chonk.hpp:284
void accumulate(ClientCircuit &circuit, const std::shared_ptr< MegaVerificationKey > &precomputed_vk) override
Perform prover work for accumulation (e.g. HN folding, merge proving)
Definition chonk.cpp:361
MegaCircuitBuilder ClientCircuit
Definition chonk.hpp:52
Goblin goblin
Definition chonk.hpp:282
The verification key is responsible for storing the commitments to the precomputed (non-witnessk) pol...
typename Curve::ScalarField FF
std::shared_ptr< OpQueue > op_queue
Definition goblin.hpp:48
static void construct_mock_app_circuit(MegaBuilder &builder, bool large=false)
Populate a builder with some arbitrary but nontrivial constraints.
static void construct_mock_folding_kernel(MegaBuilder &builder)
Construct a mock kernel circuit.
std::shared_ptr< ECCOpQueue > op_queue
Curve::ScalarField FF
static void construct_arithmetic_circuit(Builder &builder, const size_t target_log2_dyadic_size=4, bool include_public_inputs=true)
Populate a builder with a specified number of arithmetic gates; includes a PI.
static void add_default(Builder &builder)
Add default public inputs when they are not present.
#define vinfo(...)
Definition log.hpp:80
AluTraceBuilder builder
Definition alu.test.cpp:124
UltraKeccakFlavor::VerificationKey VerificationKey
constexpr T get_msb(const T in)
Definition get_msb.hpp:47
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
::testing::Types< BN254Settings, GrumpkinSettings > TestSettings
VerifierCommitmentKey< Curve > vk
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13