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 is_kernel_flags.resize(total_num_circuits, true);
128 for (size_t i = 0; i < num_app_circuits; ++i) {
129 is_kernel_flags[2 * i] = false; // every other circuit is an app
130 }
131 }
132
137 static std::shared_ptr<VerificationKey> get_verification_key(ClientCircuit& builder_in)
138 {
139 // This is a workaround to ensure that the circuit is finalized before we create the verification key
140 // In practice, this should not be needed as the circuit will be finalized when it is accumulated into the IVC
141 // but this is a workaround for the test setup.
143
144 // Deepcopy the opqueue to avoid modifying the original one when finalising the circuit
147 std::shared_ptr<VerificationKey> vk = std::make_shared<VerificationKey>(prover_instance->get_precomputed());
148 return vk;
149 }
150
157 ClientCircuit create_next_circuit(Chonk& ivc,
158 size_t log2_num_gates = 0,
159 size_t num_public_inputs = 0,
160 bool check_circuit_sizes = false)
161 {
162 const bool is_kernel = is_kernel_flags[circuit_counter++];
163 const bool use_large_circuit = large_first_app && (circuit_counter == 1); // first circuit is size 2^19
164 // Check if this is one of the trailing kernels (reset, tail, hiding)
165 const bool is_trailing_kernel = (ivc.num_circuits_accumulated >= ivc.get_num_circuits() - NUM_TRAILING_KERNELS);
166
167 ClientCircuit circuit{ ivc.goblin.op_queue };
168 // if the number of gates is specified we just add a number of arithmetic gates
169 if (log2_num_gates != 0) {
170 MockCircuits::construct_arithmetic_circuit(circuit, log2_num_gates, /* include_public_inputs= */ false);
171 // Add some public inputs
172 for (size_t i = 0; i < num_public_inputs; ++i) {
173 circuit.add_public_variable(typename Flavor::FF(13634816 + i)); // arbitrary number
174 }
175 } else {
176 // If the number of gates is not specified we create a structured mock circuit
177 if (is_kernel) {
178 // For trailing kernels (reset, tail, hiding), skip the expensive mock kernel logic to match real Noir
179 // flows. These kernels are simpler and mainly contain the completion logic added by Chonk.
180 if (!is_trailing_kernel) {
181 GoblinMockCircuits::construct_mock_folding_kernel(circuit); // construct mock base logic
182 }
183 mock_databus.populate_kernel_databus(circuit); // populate databus inputs/outputs
184 } else {
185 GoblinMockCircuits::construct_mock_app_circuit(circuit, use_large_circuit); // construct mock app
186 mock_databus.populate_app_databus(circuit); // populate databus outputs
187 }
188 }
189
190 if (is_kernel) {
192 } else {
194 }
195
196 if (check_circuit_sizes) {
197 auto prover_instance = std::make_shared<Chonk::ProverInstance>(circuit);
198 size_t log2_dyadic_size = numeric::get_msb(prover_instance->get_metadata().dyadic_size);
199 if (log2_num_gates != 0) {
200 if (is_kernel) {
201 // There are various possibilities here, so we provide a bound
203 log2_dyadic_size,
204 19UL,
205 "Log number of gates in a kernel with fixed number of arithmetic gates has exceeded bound.");
206 vinfo("Log number of gates in a kernel with fixed number of arithmetic gates is: ",
207 log2_dyadic_size);
208 } else {
209 // The offset is due to the fact that finalization adds a certain number of gates
210 size_t LOG2_OFFSET = 2;
211 BB_ASSERT_LTE(log2_dyadic_size,
212 log2_num_gates + LOG2_OFFSET,
213 "Log number of arithemtic gates produced is different from the one requested.");
214 }
215 } else {
216 if (is_kernel) {
217 // Trailing kernels (reset, tail, hiding) are simpler than regular kernels
218 if (is_trailing_kernel) {
219 // Trailing kernels should be significantly smaller, with hiding kernel < 2^16
220 BB_ASSERT_LTE(log2_dyadic_size,
221 16UL,
222 "Trailing kernel circuit size has exceeded expected bound (should be <= 2^16).");
223 vinfo("Log number of gates in a trailing kernel circuit is: ", log2_dyadic_size);
224 } else {
225 BB_ASSERT_EQ(log2_dyadic_size,
226 18UL,
227 "There has been a change in the number of gates of a mock kernel circuit.");
228 }
229 } else {
230 BB_ASSERT_EQ(log2_dyadic_size,
231 use_large_circuit ? 19UL : 17UL,
232 "There has been a change in the of gates generated for a mock app circuit.");
233 }
234 }
235 }
236 return circuit;
237 }
238
243 Chonk& ivc, TestSettings settings = {}, bool check_circuit_size = false)
244 {
245 // If this is a mock hiding kernel, remove the settings and use a default (non-structured) trace
246 if (ivc.num_circuits_accumulated == ivc.get_num_circuits() - 1) {
247 settings = TestSettings{};
248 }
249 auto circuit =
250 create_next_circuit(ivc, settings.log2_num_gates, settings.num_public_inputs, check_circuit_size);
251 return { circuit, get_verification_key(circuit) };
252 }
253
254 void construct_and_accumulate_next_circuit(Chonk& ivc, TestSettings settings = {}, bool check_circuit_sizes = false)
255 {
256 auto [circuit, vk] = create_next_circuit_and_vk(ivc, settings, check_circuit_sizes);
257 ivc.accumulate(circuit, vk);
258 }
259
263 void tamper_with_databus() { mock_databus.tamper_with_app_return_data(); }
264};
265
266} // namespace
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:93
#define BB_ASSERT_LTE(left, right,...)
Definition assert.hpp:168
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:246
size_t num_circuits_accumulated
Definition chonk.hpp:134
size_t get_num_circuits() const
Definition chonk.hpp:160
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:397
MegaCircuitBuilder ClientCircuit
Definition chonk.hpp:52
Goblin goblin
Definition chonk.hpp:158
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
NativeVerificationKey_< PrecomputedEntities< Commitment >, Codec, HashFunction, CommitmentKey > VerificationKey
The verification key is responsible for storing the commitments to the precomputed (non-witness) poly...
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:141
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
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