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
139 static std::shared_ptr<VerificationKey> get_verification_key(ClientCircuit& builder_in,
140 bool is_hiding_kernel = false)
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
149
150 if (is_hiding_kernel) {
152 return std::make_shared<VerificationKey>(prover_instance->get_precomputed());
153 }
154 auto prover_instance = std::make_shared<Chonk::ProverInstance>(builder);
155 return std::make_shared<VerificationKey>(prover_instance->get_precomputed());
156 }
157
164 ClientCircuit create_next_circuit(Chonk& ivc,
165 size_t log2_num_gates = 0,
166 size_t num_public_inputs = 0,
167 bool check_circuit_sizes = false)
168 {
169 const bool is_kernel = is_kernel_flags[circuit_counter++];
170 const bool use_large_circuit = large_first_app && (circuit_counter == 1); // first circuit is size 2^19
171 // Check if this is one of the trailing kernels (reset, tail, hiding)
172 const bool is_trailing_kernel = (ivc.num_circuits_accumulated >= ivc.get_num_circuits() - NUM_TRAILING_KERNELS);
173
174 ClientCircuit circuit{ ivc.goblin.op_queue };
175 // if the number of gates is specified we just add a number of arithmetic gates
176 if (log2_num_gates != 0) {
177 MockCircuits::construct_arithmetic_circuit(circuit, log2_num_gates, /* include_public_inputs= */ false);
178 // Add some public inputs
179 for (size_t i = 0; i < num_public_inputs; ++i) {
180 circuit.add_public_variable(typename Flavor::FF(13634816 + i)); // arbitrary number
181 }
182 } else {
183 // If the number of gates is not specified we create a structured mock circuit
184 if (is_kernel) {
185 // For trailing kernels (reset, tail, hiding), skip the expensive mock kernel logic to match real Noir
186 // flows. These kernels are simpler and mainly contain the completion logic added by Chonk.
187 if (!is_trailing_kernel) {
188 GoblinMockCircuits::construct_mock_folding_kernel(circuit); // construct mock base logic
189 }
190 mock_databus.populate_kernel_databus(circuit); // populate databus inputs/outputs
191 } else {
192 GoblinMockCircuits::construct_mock_app_circuit(circuit, use_large_circuit); // construct mock app
193 mock_databus.populate_app_databus(circuit); // populate databus outputs
194 }
195 }
196
197 if (is_kernel) {
199 } else {
201 }
202
203 if (check_circuit_sizes) {
204 auto prover_instance = std::make_shared<Chonk::ProverInstance>(circuit);
205 size_t log2_dyadic_size = prover_instance->log_dyadic_size();
206 if (log2_num_gates != 0) {
207 if (is_kernel) {
208 // There are various possibilities here, so we provide a bound
210 log2_dyadic_size,
211 19UL,
212 "Log number of gates in a kernel with fixed number of arithmetic gates has exceeded bound.");
213 vinfo("Log number of gates in a kernel with fixed number of arithmetic gates is: ",
214 log2_dyadic_size);
215 } else {
216 // The offset is due to the fact that finalization adds a certain number of gates
217 size_t LOG2_OFFSET = 2;
218 BB_ASSERT_LTE(log2_dyadic_size,
219 log2_num_gates + LOG2_OFFSET,
220 "Log number of arithemtic gates produced is different from the one requested.");
221 }
222 } else {
223 if (is_kernel) {
224 // Trailing kernels (reset, tail, hiding) are simpler than regular kernels
225 if (is_trailing_kernel) {
226 // Trailing kernels should be significantly smaller, with hiding kernel < 2^16
227 BB_ASSERT_LTE(log2_dyadic_size,
228 17UL,
229 "Trailing kernel circuit size has exceeded expected bound (should be <= 2^16).");
230 vinfo("Log number of gates in a trailing kernel circuit is: ", log2_dyadic_size);
231 } else {
232 BB_ASSERT_EQ(log2_dyadic_size,
233 18UL,
234 "There has been a change in the number of gates of a mock kernel circuit.");
235 }
236 } else {
237 BB_ASSERT_EQ(log2_dyadic_size,
238 use_large_circuit ? 19UL : 17UL,
239 "There has been a change in the of gates generated for a mock app circuit.");
240 }
241 }
242 }
243 return circuit;
244 }
245
250 Chonk& ivc, TestSettings settings = {}, bool check_circuit_size = false)
251 {
252 // If this is a mock hiding kernel, remove the settings and use a default (non-structured) trace
253 const bool is_hiding_kernel = ivc.num_circuits_accumulated == ivc.get_num_circuits() - 1;
254 if (is_hiding_kernel) {
255 settings = TestSettings{};
256 }
257 auto circuit =
258 create_next_circuit(ivc, settings.log2_num_gates, settings.num_public_inputs, check_circuit_size);
259 return { circuit, get_verification_key(circuit, is_hiding_kernel) };
260 }
261
262 void construct_and_accumulate_next_circuit(Chonk& ivc, TestSettings settings = {}, bool check_circuit_sizes = false)
263 {
264 auto [circuit, vk] = create_next_circuit_and_vk(ivc, settings, check_circuit_sizes);
265 ivc.accumulate(circuit, vk);
266 }
267
271 void tamper_with_databus() { mock_databus.tamper_with_app_return_data(); }
272};
273
274} // 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:39
void complete_kernel_circuit_logic(ClientCircuit &circuit)
Append logic to complete a kernel circuit.
Definition chonk.cpp:292
size_t num_circuits_accumulated
Definition chonk.hpp:157
size_t get_num_circuits() const
Definition chonk.hpp:187
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:564
MegaCircuitBuilder ClientCircuit
Definition chonk.hpp:53
Goblin goblin
Definition chonk.hpp:181
typename Curve::ScalarField FF
std::shared_ptr< OpQueue > op_queue
Definition goblin.hpp:59
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