Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
execution.test.cpp
Go to the documentation of this file.
1#include <gmock/gmock.h>
2#include <gtest/gtest.h>
3
4#include <cstdint>
5
18
19namespace bb::avm2::constraining {
20namespace {
21
22using tracegen::TestTraceContainer;
24using C = Column;
26using addressing = bb::avm2::addressing<FF>;
27using gas = bb::avm2::gas<FF>;
28using registers = bb::avm2::registers<FF>;
29
30TEST(ExecutionConstrainingTest, EmptyRow)
31{
32 check_relation<execution>(testing::empty_trace());
33}
34
35// DO NOT SUBMIT: add full flow tests
36// TEST(ExecutionConstrainingTest, Basic)
37// {
38// // clang-format off
39// TestTraceContainer trace({
40// {{ C::execution_sel, 1 }, { C::execution_pc, 0 }},
41// {{ C::execution_sel, 1 }, { C::execution_pc, 20 }, { C::execution_last, 1 }}
42// });
43// // clang-format on
44
45// check_relation<execution>(trace);
46// }
47
48TEST(ExecutionConstrainingTest, Continuity)
49{
50 // clang-format off
51 TestTraceContainer trace({
52 {{ C::precomputed_first_row, 1 }},
53 {{ C::execution_sel, 1 }},
54 {{ C::execution_sel, 1 }},
55 {{ C::execution_sel, 1 }, { C::execution_enqueued_call_end, 1 }},
56 });
57 // clang-format on
58
59 check_relation<execution>(trace, execution::SR_TRACE_CONTINUITY);
60
61 // Negative test: remove enqueued call end
62 trace.set(C::execution_enqueued_call_end, 3, 0);
63 EXPECT_THROW_WITH_MESSAGE(check_relation<execution>(trace, execution::SR_TRACE_CONTINUITY),
65}
66
67TEST(ExecutionConstrainingTest, ContinuityBrokenFirstRow)
68{
69 // clang-format off
70 TestTraceContainer trace({
71 {{ C::execution_sel, 0 }}, // End of trace!
72 {{ C::execution_sel, 1 }},
73 {{ C::execution_sel, 1 }},
74 {{ C::execution_sel, 1 }, { C::execution_enqueued_call_end, 1 }},
75 });
76 // clang-format on
77
78 EXPECT_THROW_WITH_MESSAGE(check_relation<execution>(trace, execution::SR_TRACE_CONTINUITY),
80}
81
82TEST(ExecutionConstrainingTest, ContinuityBrokenInMiddle)
83{
84 // clang-format off
85 TestTraceContainer trace({
86 {{ C::execution_sel, 1 }},
87 {{ C::execution_sel, 0 }}, // End of trace!
88 {{ C::execution_sel, 1 }},
89 {{ C::execution_sel, 1 }, { C::execution_enqueued_call_end, 1 }},
90 });
91 // clang-format on
92
93 EXPECT_THROW_WITH_MESSAGE(check_relation<execution>(trace, execution::SR_TRACE_CONTINUITY),
95}
96
97TEST(ExecutionConstrainingTest, TreeStateNotChanged)
98{
99 TestTraceContainer trace({
100 {
101 { C::precomputed_first_row, 1 },
102 },
103 {
104 { C::execution_sel, 1 },
105 { C::execution_prev_note_hash_tree_root, 10 },
106 { C::execution_prev_note_hash_tree_size, 9 },
107 { C::execution_prev_num_note_hashes_emitted, 8 },
108 { C::execution_prev_nullifier_tree_root, 7 },
109 { C::execution_prev_nullifier_tree_size, 6 },
110 { C::execution_prev_num_nullifiers_emitted, 5 },
111 { C::execution_prev_public_data_tree_root, 4 },
112 { C::execution_prev_public_data_tree_size, 3 },
113 { C::execution_prev_written_public_data_slots_tree_root, 2 },
114 { C::execution_prev_written_public_data_slots_tree_size, 1 },
115 { C::execution_prev_retrieved_bytecodes_tree_root, 12 },
116 { C::execution_prev_retrieved_bytecodes_tree_size, 13 },
117 { C::execution_note_hash_tree_root, 10 },
118 { C::execution_note_hash_tree_size, 9 },
119 { C::execution_num_note_hashes_emitted, 8 },
120 { C::execution_nullifier_tree_root, 7 },
121 { C::execution_nullifier_tree_size, 6 },
122 { C::execution_num_nullifiers_emitted, 5 },
123 { C::execution_public_data_tree_root, 4 },
124 { C::execution_public_data_tree_size, 3 },
125 { C::execution_written_public_data_slots_tree_root, 2 },
126 { C::execution_written_public_data_slots_tree_size, 1 },
127 { C::execution_retrieved_bytecodes_tree_root, 12 },
128 { C::execution_retrieved_bytecodes_tree_size, 13 },
129 },
130 });
131
132 check_relation<execution>(trace,
145
146 // Negative test: change note hash tree root
147 trace.set(C::execution_note_hash_tree_root, 1, 100);
150
151 // Negative test: change note hash tree size
152 trace.set(C::execution_note_hash_tree_size, 1, 100);
155
156 // Negative test: change num note hashes emitted
157 trace.set(C::execution_num_note_hashes_emitted, 1, 100);
160
161 // Negative test: change nullifier tree root
162 trace.set(C::execution_nullifier_tree_root, 1, 100);
165
166 // Negative test: change nullifier tree size
167 trace.set(C::execution_nullifier_tree_size, 1, 100);
170
171 // Negative test: change num nullifiers emitted
172 trace.set(C::execution_prev_num_nullifiers_emitted, 1, 100);
175
176 // Negative test: change public data tree root
177 trace.set(C::execution_public_data_tree_root, 1, 100);
180
181 // Negative test: change public data tree size
182 trace.set(C::execution_public_data_tree_size, 1, 100);
185
186 // Negative test: change written public data slots tree root
187 trace.set(C::execution_written_public_data_slots_tree_root, 1, 100);
191
192 // Negative test: change written public data slots tree size
193 trace.set(C::execution_written_public_data_slots_tree_size, 1, 100);
197
198 // Negative test: change retrieved bytecodes tree root
199 trace.set(C::execution_retrieved_bytecodes_tree_root, 1, 100);
201 check_relation<execution>(trace, execution::SR_RETRIEVED_BYTECODES_TREE_ROOT_NOT_CHANGED),
203
204 // Negative test: change retrieved bytecodes tree size
205 trace.set(C::execution_retrieved_bytecodes_tree_size, 1, 100);
207 check_relation<execution>(trace, execution::SR_RETRIEVED_BYTECODES_TREE_SIZE_NOT_CHANGED),
209}
210
211TEST(ExecutionConstrainingTest, SideEffectStateNotChanged)
212{
213 TestTraceContainer trace({
214 {
215 { C::precomputed_first_row, 1 },
216 },
217 {
218 { C::execution_sel, 1 },
219 { C::execution_prev_num_public_log_fields, 10 },
220 { C::execution_prev_num_l2_to_l1_messages, 11 },
221 { C::execution_num_public_log_fields, 10 },
222 { C::execution_num_l2_to_l1_messages, 11 },
223 },
224 });
225
226 check_relation<execution>(
228
229 // Negative test: change num public logs
230 trace.set(C::execution_num_public_log_fields, 1, 100);
233
234 // Negative test: change num l2 to l1 messages
235 trace.set(C::execution_num_l2_to_l1_messages, 1, 100);
238}
239
240TEST(ExecutionConstrainingTest, NoFetchingNoInstrFetchError)
241{
242 // sel_bytecode_retrieval_success == 0 => sel_instruction_fetching_failure == 0
243 TestTraceContainer trace({
244 { { C::execution_sel_bytecode_retrieval_success, 0 }, { C::execution_sel_instruction_fetching_failure, 0 } },
245 });
246
247 check_relation<execution>(trace, execution::SR_NO_FETCHING_NO_INSTR_FETCH_ERROR);
248
249 // Negative test: sel_bytecode_retrieval_success == 0 but sel_instruction_fetching_failure == 1
250 trace.set(C::execution_sel_instruction_fetching_failure, 0, 1);
253
254 // Positive test: sel_bytecode_retrieval_success == 1 allows sel_instruction_fetching_failure == 1
255 trace.set(C::execution_sel_bytecode_retrieval_success, 0, 1);
256 check_relation<execution>(trace, execution::SR_NO_FETCHING_NO_INSTR_FETCH_ERROR);
257}
258
259TEST(ExecutionConstrainingTest, NoAddressingErrorIfNotResolving)
260{
261 // sel_instruction_fetching_success == 0 => sel_addressing_error == 0
262 // (SEL_RESOLVE_ADDRESS is an alias for sel_instruction_fetching_success)
263 TestTraceContainer trace({
264 { { C::execution_sel_instruction_fetching_success, 0 }, { C::execution_sel_addressing_error, 0 } },
265 });
266
267 check_relation<addressing>(trace, addressing::SR_NO_ADDRESSING_ERROR_IF_NOT_RESOLVING);
268
269 // Negative test: sel_instruction_fetching_success == 0 but sel_addressing_error == 1
270 trace.set(C::execution_sel_addressing_error, 0, 1);
273
274 // Positive test: sel_instruction_fetching_success == 1 allows sel_addressing_error == 1
275 trace.set(C::execution_sel_instruction_fetching_success, 0, 1);
276 check_relation<addressing>(trace, addressing::SR_NO_ADDRESSING_ERROR_IF_NOT_RESOLVING);
277}
278
279TEST(ExecutionConstrainingTest, NoRegisterReadErrorIfNotReading)
280{
281 // sel_read_registers == 0 => sel_register_read_error == 0
282 // Via #[REGISTER_READ_TAG_CHECK]: when sel_read_registers == 0, BATCHED_TAGS_DIFF_X_REG == 0,
283 // which forces sel_register_read_error == 0.
284 TestTraceContainer trace({
285 { { C::execution_sel_read_registers, 0 }, { C::execution_sel_register_read_error, 0 } },
286 });
287
288 check_relation<registers>(trace, registers::SR_REGISTER_READ_TAG_CHECK);
289
290 // Negative test: sel_read_registers == 0 but sel_register_read_error == 1
291 trace.set(C::execution_sel_register_read_error, 0, 1);
294}
295
296TEST(ExecutionConstrainingTest, NoOogIfNoGasCheck)
297{
298 // sel_check_gas == 0 => sel_out_of_gas == 0
299 TestTraceContainer trace({
300 { { C::execution_sel_check_gas, 0 }, { C::execution_sel_out_of_gas, 0 } },
301 });
302
303 check_relation<gas>(trace, gas::SR_NO_OOG_IF_NO_GAS_CHECK);
304
305 // Negative test: sel_check_gas == 0 but sel_out_of_gas == 1
306 trace.set(C::execution_sel_out_of_gas, 0, 1);
309
310 // Positive test: sel_check_gas == 1 allows sel_out_of_gas == 1
311 trace.set(C::execution_sel_check_gas, 0, 1);
312 check_relation<gas>(trace, gas::SR_NO_OOG_IF_NO_GAS_CHECK);
313}
314
315TEST(ExecutionConstrainingTest, NoOpcodeErrorIfNotExecuting)
316{
317 // sel_execute_opcode == 0 => sel_opcode_error == 0
318 TestTraceContainer trace({
319 { { C::execution_sel_execute_opcode, 0 }, { C::execution_sel_opcode_error, 0 } },
320 });
321
322 check_relation<execution>(trace, execution::SR_NO_OPCODE_ERROR_IF_NOT_EXECUTING);
323
324 // Negative test: sel_execute_opcode == 0 but sel_opcode_error == 1
325 trace.set(C::execution_sel_opcode_error, 0, 1);
328
329 // Positive test: sel_execute_opcode == 1 allows sel_opcode_error == 1
330 trace.set(C::execution_sel_execute_opcode, 0, 1);
331 check_relation<execution>(trace, execution::SR_NO_OPCODE_ERROR_IF_NOT_EXECUTING);
332}
333
334TEST(ExecutionConstrainingTest, SubtraceIdDecomposition)
335{
339
340 TestTraceContainer trace;
341 const uint8_t enum_length = static_cast<uint8_t>(SubtraceSel::MAX) + 1;
342
343 for (uint8_t i = 0; i < enum_length; i++) {
344 SubtraceSel subtrace_sel = static_cast<SubtraceSel>(i);
345 const auto subtrace_id = get_subtrace_id(subtrace_sel);
346 const auto subtrace_selector = get_subtrace_selector(subtrace_sel);
347
348 trace.set(i,
349 { {
350 { subtrace_selector, 1 },
351 { C::execution_subtrace_id, subtrace_id },
352 { C::execution_sel_execute_opcode, 1 },
353 } });
354 }
355
356 check_relation<execution>(trace, execution::SR_SUBTRACE_ID_DECOMPOSITION);
357
358 for (uint8_t i = 0; i < enum_length; i++) {
359 const auto subtrace_selector = get_subtrace_selector(static_cast<SubtraceSel>(i));
360
361 // Negative test: de-activate the selector
362 trace.set(subtrace_selector, i, 0);
365
366 // Negative test: activate the wrong selector
367 const auto wrong_selector = get_subtrace_selector(static_cast<SubtraceSel>((i + 1) % enum_length));
368 trace.set(wrong_selector, i, 1);
371 // De-activate the wrong selector
372 trace.set(wrong_selector, i, 0);
373
374 // Re-activate the correct selector
375 trace.set(subtrace_selector, i, 1);
376
377 // Ensure we have a correct trace for the next iteration
378 check_relation<execution>(trace, execution::SR_SUBTRACE_ID_DECOMPOSITION);
379 }
380}
381
382} // namespace
383} // namespace bb::avm2::constraining
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessageRegex)
Definition assert.hpp:224
static std::string get_subrelation_label(size_t index)
static constexpr size_t SR_NO_ADDRESSING_ERROR_IF_NOT_RESOLVING
static constexpr size_t SR_NUM_L2_TO_L1_MESSAGES_NOT_CHANGED
Definition execution.hpp:74
static constexpr size_t SR_RETRIEVED_BYTECODES_TREE_ROOT_NOT_CHANGED
Definition execution.hpp:75
static constexpr size_t SR_WRITTEN_PUBLIC_DATA_SLOTS_TREE_SIZE_NOT_CHANGED
Definition execution.hpp:66
static constexpr size_t SR_NULLIFIER_TREE_SIZE_NOT_CHANGED
Definition execution.hpp:71
static std::string get_subrelation_label(size_t index)
Definition execution.hpp:80
static constexpr size_t SR_NO_OPCODE_ERROR_IF_NOT_EXECUTING
Definition execution.hpp:78
static constexpr size_t SR_NUM_NULLIFIERS_EMITTED_NOT_CHANGED
Definition execution.hpp:72
static constexpr size_t SR_PUBLIC_DATA_TREE_SIZE_NOT_CHANGED
Definition execution.hpp:64
static constexpr size_t SR_NOTE_HASH_TREE_ROOT_NOT_CHANGED
Definition execution.hpp:67
static constexpr size_t SR_TRACE_CONTINUITY
Definition execution.hpp:43
static constexpr size_t SR_NO_FETCHING_NO_INSTR_FETCH_ERROR
Definition execution.hpp:47
static constexpr size_t SR_SUBTRACE_ID_DECOMPOSITION
Definition execution.hpp:53
static constexpr size_t SR_NULLIFIER_TREE_ROOT_NOT_CHANGED
Definition execution.hpp:70
static constexpr size_t SR_NUM_PUBLIC_LOGS_NOT_CHANGED
Definition execution.hpp:73
static constexpr size_t SR_PUBLIC_DATA_TREE_ROOT_NOT_CHANGED
Definition execution.hpp:63
static constexpr size_t SR_WRITTEN_PUBLIC_DATA_SLOTS_TREE_ROOT_NOT_CHANGED
Definition execution.hpp:65
static constexpr size_t SR_NOTE_HASH_TREE_SIZE_NOT_CHANGED
Definition execution.hpp:68
static constexpr size_t SR_RETRIEVED_BYTECODES_TREE_SIZE_NOT_CHANGED
Definition execution.hpp:76
static constexpr size_t SR_NUM_NOTE_HASHES_EMITTED_NOT_CHANGED
Definition execution.hpp:69
static std::string get_subrelation_label(size_t index)
Definition gas.hpp:39
static constexpr size_t SR_NO_OOG_IF_NO_GAS_CHECK
Definition gas.hpp:37
static constexpr size_t SR_REGISTER_READ_TAG_CHECK
Definition registers.hpp:41
static std::string get_subrelation_label(size_t index)
Definition registers.hpp:43
void set(Column col, uint32_t row, const FF &value, bool use_atomic_limbs=false)
TestTraceContainer trace
TEST(AvmFixedVKTests, FixedVKCommitments)
Test that the fixed VK commitments agree with the ones computed from precomputed columns.
TestTraceContainer empty_trace()
Definition fixtures.cpp:156
Column get_subtrace_selector(SubtraceSel subtrace_sel)
Get the column selector for a given subtrace selector.
FF get_subtrace_id(SubtraceSel subtrace_sel)
Get the subtrace ID for a given subtrace enum.
NiceMock< MockExecution > execution