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#pragma once
2
8
9using namespace bb;
10
11namespace {
12
19class MockDatabusProducer {
20 private:
21 using ClientCircuit = Chonk::ClientCircuit;
22 using Flavor = MegaFlavor;
23 using FF = Flavor::FF;
24 using BusDataArray = std::vector<FF>;
25
26 static constexpr size_t BUS_ARRAY_SIZE = 3; // arbitrary length of mock bus inputs
27 BusDataArray app_return_data;
28 BusDataArray kernel_return_data;
29
30 FF dummy_return_val = 1; // use simple return val for easier test debugging
31
32 BusDataArray generate_random_bus_array()
33 {
34 BusDataArray result;
35 for (size_t i = 0; i < BUS_ARRAY_SIZE; ++i) {
36 result.emplace_back(dummy_return_val);
37 }
38 dummy_return_val += 1;
39 return result;
40 }
41
42 public:
46 void populate_app_databus(ClientCircuit& circuit)
47 {
48 app_return_data = generate_random_bus_array();
49 for (auto& val : app_return_data) {
50 circuit.add_public_return_data(circuit.add_variable(val));
51 }
52 };
53
58 void populate_kernel_databus(ClientCircuit& circuit)
59 {
60 // Populate calldata from previous kernel return data (if it exists)
61 for (auto& val : kernel_return_data) {
62 circuit.add_public_calldata(circuit.add_variable(val));
63 }
64 // Populate secondary_calldata from app return data (if it exists), then clear the app return data
65 for (auto& val : app_return_data) {
66 circuit.add_public_secondary_calldata(circuit.add_variable(val));
67 }
68 app_return_data.clear();
69
70 // Mock the return data for the present kernel circuit
71 kernel_return_data = generate_random_bus_array();
72 for (auto& val : kernel_return_data) {
73 circuit.add_public_return_data(circuit.add_variable(val));
74 }
75 };
76
81 void tamper_with_app_return_data() { app_return_data.emplace_back(17); }
82};
83
88struct TestSettings {
89 // number of public inputs to manually add to circuits, by default this would be 0 because we use the
90 // MockDatabusProducer to test public inputs handling
91 size_t num_public_inputs = 0;
92 // by default we will create more complex apps and kernel with various types of gates but in case we want to
93 // specifically test overflow behaviour or unstructured circuits we can manually construct simple circuits with a
94 // specified number of gates
95 size_t log2_num_gates = 0;
96};
97
106class PrivateFunctionExecutionMockCircuitProducer {
107 using ClientCircuit = Chonk::ClientCircuit;
108 using Flavor = MegaFlavor;
110
111 size_t circuit_counter = 0;
112 std::vector<bool> is_kernel_flags;
113
114 MockDatabusProducer mock_databus;
115 bool large_first_app = true;
116 constexpr static size_t NUM_TRAILING_KERNELS = 3; // reset, tail, hiding
117
118 public:
119 size_t total_num_circuits = 0;
120
121 PrivateFunctionExecutionMockCircuitProducer(size_t num_app_circuits, bool large_first_app = true)
122 : large_first_app(large_first_app)
123 , total_num_circuits(num_app_circuits * 2 +
124 NUM_TRAILING_KERNELS) /*One kernel per app, plus a fixed number of final kernels*/
125 {
126 // Set flags indicating which circuits are kernels vs apps
127 for (size_t i = 0; i < num_app_circuits; ++i) {
128 is_kernel_flags.emplace_back(false); // every other circuit is an app
129 is_kernel_flags.emplace_back(true); // every other circuit is a kernel
130 }
131 for (size_t i = 0; i < NUM_TRAILING_KERNELS; ++i) {
132 is_kernel_flags.emplace_back(true);
133 }
134 }
135
140 static std::shared_ptr<VerificationKey> get_verification_key(ClientCircuit& builder_in)
141 {
142 // This is a workaround to ensure that the circuit is finalized before we create the verification key
143 // In practice, this should not be needed as the circuit will be finalized when it is accumulated into the IVC
144 // but this is a workaround for the test setup.
146
147 // Deepcopy the opqueue to avoid modifying the original one when finalising the circuit
150 std::shared_ptr<VerificationKey> vk = std::make_shared<VerificationKey>(prover_instance->get_precomputed());
151 return vk;
152 }
153
160 ClientCircuit create_next_circuit(Chonk& ivc,
161 size_t log2_num_gates = 0,
162 size_t num_public_inputs = 0,
163 bool check_circuit_sizes = false)
164 {
165 const bool is_kernel = is_kernel_flags[circuit_counter++];
166 const bool use_large_circuit = large_first_app && (circuit_counter == 1); // first circuit is size 2^19
167 // Check if this is one of the trailing kernels (reset, tail, hiding)
168 const bool is_trailing_kernel = (ivc.num_circuits_accumulated >= ivc.get_num_circuits() - NUM_TRAILING_KERNELS);
169
170 ClientCircuit circuit{ ivc.goblin.op_queue };
171 // if the number of gates is specified we just add a number of arithmetic gates
172 if (log2_num_gates != 0) {
173 MockCircuits::construct_arithmetic_circuit(circuit, log2_num_gates, /* include_public_inputs= */ false);
174 // Add some public inputs
175 for (size_t i = 0; i < num_public_inputs; ++i) {
176 circuit.add_public_variable(typename Flavor::FF(13634816 + i)); // arbitrary number
177 }
178 } else {
179 // If the number of gates is not specified we create a structured mock circuit
180 if (is_kernel) {
181 // For trailing kernels (reset, tail, hiding), skip the expensive mock kernel logic to match real Noir
182 // flows. These kernels are simpler and mainly contain the completion logic added by Chonk.
183 if (!is_trailing_kernel) {
184 GoblinMockCircuits::construct_mock_folding_kernel(circuit); // construct mock base logic
185 }
186 mock_databus.populate_kernel_databus(circuit); // populate databus inputs/outputs
187 } else {
188 GoblinMockCircuits::construct_mock_app_circuit(circuit, use_large_circuit); // construct mock app
189 mock_databus.populate_app_databus(circuit); // populate databus outputs
190 }
191 }
192
193 if (is_kernel) {
195 } else {
197 }
198
199 if (check_circuit_sizes) {
200 auto prover_instance = std::make_shared<Chonk::ProverInstance>(circuit);
201 size_t log2_dyadic_size = prover_instance->log_dyadic_size();
202 if (log2_num_gates != 0) {
203 if (is_kernel) {
204 // There are various possibilities here, so we provide a bound
206 log2_dyadic_size,
207 19UL,
208 "Log number of gates in a kernel with fixed number of arithmetic gates has exceeded bound.");
209 vinfo("Log number of gates in a kernel with fixed number of arithmetic gates is: ",
210 log2_dyadic_size);
211 } else {
212 // The offset is due to the fact that finalization adds a certain number of gates
213 size_t LOG2_OFFSET = 2;
214 BB_ASSERT_LTE(log2_dyadic_size,
215 log2_num_gates + LOG2_OFFSET,
216 "Log number of arithemtic gates produced is different from the one requested.");
217 }
218 } else {
219 if (is_kernel) {
220 // Trailing kernels (reset, tail, hiding) are simpler than regular kernels
221 if (is_trailing_kernel) {
222 // Trailing kernels should be significantly smaller, with hiding kernel < 2^16
223 BB_ASSERT_LTE(log2_dyadic_size,
224 16UL,
225 "Trailing kernel circuit size has exceeded expected bound (should be <= 2^16).");
226 vinfo("Log number of gates in a trailing kernel circuit is: ", log2_dyadic_size);
227 } else {
228 BB_ASSERT_EQ(log2_dyadic_size,
229 18UL,
230 "There has been a change in the number of gates of a mock kernel circuit.");
231 }
232 } else {
233 BB_ASSERT_EQ(log2_dyadic_size,
234 use_large_circuit ? 19UL : 17UL,
235 "There has been a change in the of gates generated for a mock app circuit.");
236 }
237 }
238 }
239 return circuit;
240 }
241
246 Chonk& ivc, TestSettings settings = {}, bool check_circuit_size = false)
247 {
248 // If this is a mock hiding kernel, remove the settings and use a default (non-structured) trace
249 if (ivc.num_circuits_accumulated == ivc.get_num_circuits() - 1) {
250 settings = TestSettings{};
251 }
252 auto circuit =
253 create_next_circuit(ivc, settings.log2_num_gates, settings.num_public_inputs, check_circuit_size);
254 return { circuit, get_verification_key(circuit) };
255 }
256
257 void construct_and_accumulate_next_circuit(Chonk& ivc, TestSettings settings = {}, bool check_circuit_sizes = false)
258 {
259 auto [circuit, vk] = create_next_circuit_and_vk(ivc, settings, check_circuit_sizes);
260 ivc.accumulate(circuit, vk);
261 }
262
266 void tamper_with_databus() { mock_databus.tamper_with_app_return_data(); }
267};
268
269} // namespace
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:83
#define BB_ASSERT_LTE(left, right,...)
Definition assert.hpp:158
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:240
size_t num_circuits_accumulated
Definition chonk.hpp:145
size_t get_num_circuits() const
Definition chonk.hpp:171
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:391
MegaCircuitBuilder ClientCircuit
Definition chonk.hpp:52
Goblin goblin
Definition chonk.hpp:169
typename Curve::ScalarField FF
std::shared_ptr< OpQueue > op_queue
Definition goblin.hpp:54
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
NativeVerificationKey_< PrecomputedEntities< Commitment >, Codec, HashFunction, CommitmentKey > VerificationKey
The verification key stores commitments to the precomputed (non-witness) polynomials used by the veri...
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.
Base Native verification key class.
Definition flavor.hpp:135
static void add_default(Builder &builder)
Add default public inputs when they are not present.
#define vinfo(...)
Definition log.hpp:94
AluTraceBuilder builder
Definition alu.test.cpp:124
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