Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
tx_data.cpp
Go to the documentation of this file.
2
15
16#include <optional>
17#include <random>
18
19namespace {
20
21void mutate_enqueued_calls(std::vector<PublicCallRequestWithCalldata>& enqueued_calls,
22 std::vector<AztecAddress>& contract_addresses,
23 std::mt19937_64& rng)
24{
25 auto mutate_fn = [&](PublicCallRequestWithCalldata& call, std::mt19937_64& rng) {
26 bb::avm2::fuzzer::mutate_public_call_request(call, contract_addresses, rng);
27 };
28
29 auto gen_fn = [&](std::mt19937_64& rng) {
30 return bb::avm2::fuzzer::generate_public_call_request(contract_addresses, rng);
31 };
32
33 mutate_vec<PublicCallRequestWithCalldata>(enqueued_calls, rng, mutate_fn, gen_fn, BASIC_VEC_MUTATION_CONFIGURATION);
34};
35
36void mutate_teardown(std::optional<PublicCallRequestWithCalldata>& teardown_call,
37 std::vector<AztecAddress>& contract_addresses,
38 std::mt19937_64& rng)
39{
40 if (!teardown_call.has_value()) {
41 // Nothing to mutate, generate a new one
42 teardown_call = bb::avm2::fuzzer::generate_public_call_request(contract_addresses, rng);
43 return;
44 }
45
46 // If we already have a teardown call, there's a 1 in 10 chance we discard it
47 bool discard = std::uniform_int_distribution<int>(0, 9)(rng) == 0;
48 if (discard) {
49 fuzz_info("Discarding teardown enqueued call");
50 teardown_call = std::nullopt;
51 } else {
52 // Mutate existing teardown call
53 bb::avm2::fuzzer::mutate_public_call_request(teardown_call.value(), contract_addresses, rng);
54 }
55}
56
57} // namespace
58
59namespace bb::avm2::fuzzer {
60
61// Gas bounds for mutation
62constexpr uint32_t MIN_GAS = 1000;
63constexpr uint32_t MAX_GAS = 10000000;
64
65// Fee bounds for mutation
66constexpr uint128_t MIN_FEE = 1;
67constexpr uint128_t MAX_FEE = 1000;
68
74
75void mutate_tx(Tx& tx, std::vector<AztecAddress>& contract_addresses, std::mt19937_64& rng)
76{
77 auto choice = TX_MUTATION_CONFIGURATION.select(rng);
78
79 switch (choice) {
81 // Mutate setup enqueued calls
82 fuzz_info("Mutating setup enqueued calls: ", tx.setup_enqueued_calls.size());
83 mutate_enqueued_calls(tx.setup_enqueued_calls, contract_addresses, rng);
84 break;
86 // Mutate app logic enqueued calls
87 fuzz_info("Mutating app logic enqueued calls: ", tx.app_logic_enqueued_calls.size());
88 mutate_enqueued_calls(tx.app_logic_enqueued_calls, contract_addresses, rng);
89 break;
91 // Mutate teardown enqueued call
92 fuzz_info("Mutating teardown enqueued call");
93 mutate_teardown(tx.teardown_enqueued_call, contract_addresses, rng);
94 break;
96 // Mutate non-revertible accumulated data
97 fuzz_info("Mutating non-revertible accumulated data");
98 mutate_non_revertible_accumulated_data(tx.non_revertible_accumulated_data, rng);
99 break;
101 // Mutate revertible accumulated data
102 fuzz_info("Mutating revertible accumulated data");
103 mutate_revertible_accumulated_data(tx.revertible_accumulated_data, rng);
104 break;
105
106 // case 2:
107 // // Mutate gas_settings
108 // mutate_gas_settings(tx.gas_settings, rng);
109 // break;
110 // case 3:
111 // // Mutate effective_gas_fees
112 // mutate_gas_fees(tx.effective_gas_fees, rng);
113 // break;
114 // case 4:
115 // // Mutate Deployment data
116 // break;
117 // case 8:
118 // // Mutate gas_used_by_private
119 // break;
120 // case 9:
121 // // Mutate fee_payer
122 // break;
123 //}
124 }
125}
126
128{
129 auto choice = std::uniform_int_distribution<uint8_t>(0, 3)(rng);
130
131 switch (choice) {
132 case 0:
133 // Pick a Gas Limit between [0, AVM_MAX_PROCESSABLE_L2_GAS]
134 // fixme: probably should not mutate both l2_gas and da_gas to max in one go
137 break;
138 case 1:
139 // Mutate teardown_gas_limits
140 gas_settings.teardown_gas_limits.l2_gas =
142 gas_settings.teardown_gas_limits.da_gas =
144 break;
145 case 2:
146 // Mutate max_fees_per_gas
147 // mutate_gas_fees(gas_settings.max_fees_per_gas, rng);
148 break;
149 case 3:
150 // Mutate max_priority_fees_per_gas
151 // mutate_gas_fees(gas_settings.max_priority_fees_per_gas, rng);
152 break;
153 }
154}
155
157{
158 auto choice = std::uniform_int_distribution<uint8_t>(0, 2)(rng);
159
160 switch (choice) {
161 case 0:
162 // Mutate l2_gas
164 break;
165 case 1:
166 // Mutate da_gas
168 break;
169 case 2:
170 // Set both to same value
172 break;
173 }
174}
175
177{
178 auto choice = std::uniform_int_distribution<uint8_t>(0, 3)(rng);
179
180 switch (choice) {
181 case 0:
182 // Mutate fee_per_da_gas
184 break;
185 case 1:
186 // Mutate fee_per_l2_gas
188 break;
189 case 2:
190 // Set both to zero
191 fees.fee_per_da_gas = 0;
192 fees.fee_per_l2_gas = 0;
193 break;
194 case 3:
195 // Set both to same non-zero value
197 break;
198 }
199}
200
202 std::vector<FuzzerData>& enqueued_calls,
203 std::mt19937_64& rng,
204 size_t max_size)
205{
206 auto choice = std::uniform_int_distribution<uint8_t>(0, 1)(rng);
207 switch (choice) {
208 case 0: {
209 fuzz_info("Adding a new enqueued call");
210 // Add a new enqueued call
211 if (enqueued_calls.size() < max_size) {
212 FuzzerData new_enqueued_call = generate_fuzzer_data(rng, context);
213 enqueued_calls.push_back(new_enqueued_call);
214 }
215 break;
216 }
217 case 1: {
218 // Mutate an existing enqueued call
219 fuzz_info("Mutating an existing enqueued call");
220 if (!enqueued_calls.empty()) {
221 size_t idx = std::uniform_int_distribution<size_t>(0, enqueued_calls.size() - 1)(rng);
222 fuzz_info("Mutating enqueued call at index: ", idx);
223 mutate_fuzzer_data(enqueued_calls[idx], rng, context);
224 add_default_instruction_block_if_empty(enqueued_calls[idx], rng, context);
225 }
226 break;
227 }
228 // case 2: {
229 // // Remove an existing enqueued call
230 // vinfo("Removing an existing enqueued call");
231 // if (!enqueued_calls.empty()) {
232 // size_t idx = std::uniform_int_distribution<size_t>(0, enqueued_calls.size() - 1)(rng);
233 // enqueued_calls.erase(enqueued_calls.begin() + static_cast<std::ptrdiff_t>(idx));
234 // }
235 // break;
236 // }
237 }
238}
239
240} // namespace bb::avm2::fuzzer
#define fuzz_info(...)
Definition constants.hpp:51
#define MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX
#define AVM_EMITUNENCRYPTEDLOG_BASE_DA_GAS
#define AVM_EMITNULLIFIER_BASE_DA_GAS
#define MAX_L2_TO_L1_MSGS_PER_TX
#define AVM_SENDL2TOL1MSG_BASE_DA_GAS
#define MAX_NOTE_HASHES_PER_TX
#define AVM_EMITNOTEHASH_BASE_DA_GAS
#define MAX_NULLIFIERS_PER_TX
#define AVM_SSTORE_DYN_DA_GAS
#define AVM_MAX_PROCESSABLE_L2_GAS
#define PUBLIC_LOGS_LENGTH
T select(std::mt19937_64 &rng) const
constexpr VecMutationConfig BASIC_VEC_MUTATION_CONFIGURATION
void mutate_tx(Tx &tx, std::vector< AztecAddress > &contract_addresses, std::mt19937_64 &rng)
Definition tx_data.cpp:75
void mutate_gas_settings(GasSettings &gas_settings, std::mt19937_64 &rng)
Definition tx_data.cpp:127
void mutate_gas(Gas &gas, std::mt19937_64 &rng)
Definition tx_data.cpp:156
constexpr uint32_t MIN_GAS
Definition tx_data.cpp:62
void mutate_non_revertible_accumulated_data(AccumulatedData &data, std::mt19937_64 &rng)
FuzzerData generate_fuzzer_data(std::mt19937_64 &rng, const FuzzerContext &context)
constexpr uint128_t MIN_FEE
Definition tx_data.cpp:66
PublicCallRequestWithCalldata generate_public_call_request(std::vector< AztecAddress > &contract_addresses, std::mt19937_64 &rng)
constexpr uint128_t MAX_FEE
Definition tx_data.cpp:67
void mutate_fuzzer_data_vec(const FuzzerContext &context, std::vector< FuzzerData > &enqueued_calls, std::mt19937_64 &rng, size_t max_size)
Definition tx_data.cpp:201
void add_default_instruction_block_if_empty(FuzzerData &fuzzer_data, std::mt19937_64 &rng, const FuzzerContext &context)
void mutate_fuzzer_data(FuzzerData &fuzzer_data, std::mt19937_64 &rng, const FuzzerContext &context)
constexpr uint32_t AVM_MAX_PROCESSABLE_DA_GAS
Definition tx_data.cpp:69
void mutate_gas_fees(GasFees &fees, std::mt19937_64 &rng)
Definition tx_data.cpp:176
void mutate_public_call_request(PublicCallRequestWithCalldata &request, std::vector< AztecAddress > &contract_addresses, std::mt19937_64 &rng)
constexpr uint32_t MAX_GAS
Definition tx_data.cpp:63
void mutate_revertible_accumulated_data(AccumulatedData &data, std::mt19937_64 &rng)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
unsigned __int128 uint128_t
Definition serialize.hpp:44
describes the data which will be used for fuzzing Should contain instructions, calldata,...
uint128_t fee_per_l2_gas
uint128_t fee_per_da_gas
constexpr TxMutationConfig TX_MUTATION_CONFIGURATION
Definition tx_data.hpp:22