Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
context.test.cpp
Go to the documentation of this file.
1#include <gmock/gmock.h>
2#include <gtest/gtest.h>
3
4#include <cstdint>
5
15
16namespace bb::avm2::constraining {
17namespace {
18
19using tracegen::TestTraceContainer;
21using C = Column;
23
24TEST(ContextConstrainingTest, EmptyRow)
25{
26 check_relation<context>(testing::empty_trace());
27}
28
29// This test currently does a lot, consider splitting up the various exit call conditions
30TEST(ContextConstrainingTest, ContextSwitchingCallReturn)
31{
32 constexpr uint32_t top_bytecode_id = 0x12345678;
33 constexpr uint32_t nested_bytecode_id = 0x456789ab;
34
35 TestTraceContainer trace(
36 { {
37 { C::execution_next_context_id, 0 },
38 { C::precomputed_first_row, 1 },
39 // Context Stack Rows
40 { C::context_stack_sel, 1 },
41 { C::context_stack_entered_context_id, 2 },
42 { C::context_stack_context_id, 1 },
43 { C::context_stack_parent_id, 0 },
44 { C::context_stack_next_pc, 2 },
45 { C::context_stack_msg_sender, 0 },
46 { C::context_stack_contract_address, 0 },
47 { C::context_stack_bytecode_id, top_bytecode_id },
48 { C::context_stack_is_static, 0 },
49 { C::context_stack_parent_calldata_addr, 0 },
50 { C::context_stack_parent_calldata_size, 0 },
51 { C::context_stack_parent_l2_gas_limit, 2000 },
52 { C::context_stack_parent_da_gas_limit, 4000 },
53 { C::context_stack_parent_l2_gas_used, 500 },
54 { C::context_stack_parent_da_gas_used, 1500 },
55 },
56 // First Row of execution
57 {
58 { C::execution_sel, 1 },
59 { C::execution_pc, 0 },
60 { C::execution_next_pc, 1 },
61 { C::execution_context_id, 1 },
62 { C::execution_next_context_id, 2 },
63 { C::execution_bytecode_id, top_bytecode_id },
64 { C::execution_is_static, 0 }, // Non-static context
65 { C::execution_parent_l2_gas_limit, 2000 },
66 { C::execution_parent_da_gas_limit, 4000 },
67 { C::execution_parent_l2_gas_used, 500 },
68 { C::execution_parent_da_gas_used, 1500 },
69 { C::execution_enqueued_call_start, 1 },
70 },
71 // CALL
72 {
73 { C::execution_sel, 1 },
74 { C::execution_pc, 1 },
75 { C::execution_next_pc, 2 },
76 { C::execution_sel_execute_call, 1 },
77 { C::execution_sel_execute_static_call, 0 }, // Regular CALL, not STATICCALL
78 { C::execution_sel_enter_call, 1 },
79 { C::execution_context_id, 1 },
80 { C::execution_next_context_id, 2 },
81 { C::execution_bytecode_id, top_bytecode_id }, // Same as previous row (propagated)
82 { C::execution_is_static, 0 }, // Still non-static
83 { C::execution_rop_4_, /*cd offset=*/10 },
84 { C::execution_register_2_, /*contract address=*/0xdeadbeef },
85 { C::execution_register_3_, /*cd size=*/1 },
86 { C::execution_parent_l2_gas_limit, 2000 },
87 { C::execution_parent_da_gas_limit, 4000 },
88 { C::execution_parent_l2_gas_used, 500 },
89 { C::execution_parent_da_gas_used, 1500 },
90 },
91 // First Row in new context
92 {
93 { C::execution_sel, 1 },
94 { C::execution_pc, 0 }, // pc=0 because it is after a CALL
95 { C::execution_next_pc, 20 },
96 { C::execution_context_id, 2 }, // Previous row next_context_id
97 { C::execution_next_context_id, 3 }, // Incremented due to previous call
98 { C::execution_parent_id, 1 }, // Previous row context id
99 { C::execution_is_parent_id_inv, 1 },
100 { C::execution_has_parent_ctx, 1 },
101 { C::execution_contract_address, 0xdeadbeef },
102 { C::execution_bytecode_id, nested_bytecode_id }, // New bytecode_id on entering new context
103 { C::execution_is_static, 0 }, // Remains non-static after regular CALL
104 { C::execution_parent_calldata_addr, 10 },
105 { C::execution_parent_calldata_size, 1 },
106 },
107 // Return Row
108 {
109 { C::execution_sel, 1 },
110 { C::execution_pc, 20 },
111 { C::execution_next_pc, 30 },
112 { C::execution_sel_execute_return, 1 },
113 { C::execution_rop_0_, 500 }, // Return data size offset
114 { C::execution_rop_1_, 600 }, // Return data offset
115 { C::execution_register_0_, 200 }, // Return data size
116 { C::execution_sel_exit_call, 1 },
117 { C::execution_nested_exit_call, 1 },
118 { C::execution_nested_return, 1 },
119 { C::execution_context_id, 2 },
120 { C::execution_next_context_id, 3 },
121 { C::execution_parent_id, 1 },
122 { C::execution_is_parent_id_inv, 1 },
123 { C::execution_has_parent_ctx, 1 },
124 { C::execution_contract_address, 0xdeadbeef },
125 { C::execution_bytecode_id, nested_bytecode_id }, // Propagated within same context
126 { C::execution_parent_calldata_addr, 10 },
127 { C::execution_parent_calldata_size, 1 },
128 },
129 {
130 { C::execution_sel, 1 },
131 { C::execution_next_context_id, 3 },
132 { C::execution_context_id, 1 },
133 { C::execution_parent_id, 0 },
134 { C::execution_last_child_id, 2 }, // Previous context id
135 { C::execution_pc, 2 }, // Based on next_pc of CALL step
136 { C::execution_msg_sender, 0 },
137 { C::execution_contract_address, 0 },
138 { C::execution_bytecode_id, top_bytecode_id }, // Restored from context stack
139 { C::execution_is_static, 0 },
140 { C::execution_parent_calldata_addr, 0 },
141 { C::execution_parent_calldata_size, 0 },
142 { C::execution_last_child_returndata_size, 200 }, // Return data size
143 { C::execution_last_child_returndata_addr, 600 }, // Return data offset
144 { C::execution_parent_l2_gas_limit, 2000 },
145 { C::execution_parent_da_gas_limit, 4000 },
146 { C::execution_parent_l2_gas_used, 500 },
147 { C::execution_parent_da_gas_used, 1500 },
148 },
149 {
150 { C::execution_sel, 0 },
151 } });
152
153 check_relation<context>(trace);
154
155 check_interaction<tracegen::ExecutionTraceBuilder,
159}
160
161TEST(ContextConstrainingTest, ContextSwitchingExceptionalHalt)
162{
163 constexpr uint32_t top_bytecode_id = 0x12345678;
164 constexpr uint32_t nested_bytecode_id = 0x456789ab;
165
166 TestTraceContainer trace(
167 { {
168 { C::execution_next_context_id, 0 },
169 { C::precomputed_first_row, 1 },
170 // Context Stack Rows
171 { C::context_stack_sel, 1 },
172 { C::context_stack_entered_context_id, 2 },
173 { C::context_stack_context_id, 1 },
174 { C::context_stack_parent_id, 0 },
175 { C::context_stack_next_pc, 2 },
176 { C::context_stack_msg_sender, 0 },
177 { C::context_stack_contract_address, 0 },
178 { C::context_stack_bytecode_id, top_bytecode_id },
179 { C::context_stack_is_static, 0 },
180 { C::context_stack_parent_calldata_addr, 0 },
181 { C::context_stack_parent_calldata_size, 0 },
182 { C::context_stack_parent_l2_gas_limit, 2000 },
183 { C::context_stack_parent_da_gas_limit, 4000 },
184 { C::context_stack_parent_l2_gas_used, 500 },
185 { C::context_stack_parent_da_gas_used, 1500 },
186 },
187 // First Row of execution
188 {
189 { C::execution_sel, 1 },
190 { C::execution_pc, 0 },
191 { C::execution_next_pc, 1 },
192 { C::execution_context_id, 1 },
193 { C::execution_next_context_id, 2 },
194 { C::execution_bytecode_id, top_bytecode_id },
195 { C::execution_parent_l2_gas_limit, 2000 },
196 { C::execution_parent_da_gas_limit, 4000 },
197 { C::execution_parent_l2_gas_used, 500 },
198 { C::execution_parent_da_gas_used, 1500 },
199 { C::execution_enqueued_call_start, 1 },
200 },
201 // CALL
202 {
203 { C::execution_sel, 1 },
204 { C::execution_pc, 1 },
205 { C::execution_next_pc, 2 },
206 { C::execution_sel_execute_call, 1 },
207 { C::execution_sel_enter_call, 1 },
208 { C::execution_context_id, 1 },
209 { C::execution_next_context_id, 2 },
210 { C::execution_bytecode_id, top_bytecode_id }, // Same as previous row (propagated)
211 { C::execution_rop_4_, /*cd offset=*/10 },
212 { C::execution_register_2_, /*contract address=*/0xdeadbeef },
213 { C::execution_register_3_, /*cd size=*/1 },
214 { C::execution_parent_l2_gas_limit, 2000 },
215 { C::execution_parent_da_gas_limit, 4000 },
216 { C::execution_parent_l2_gas_used, 500 },
217 { C::execution_parent_da_gas_used, 1500 },
218 },
219 // First Row in new context
220 {
221 { C::execution_sel, 1 },
222 { C::execution_pc, 0 }, // pc=0 because it is after a CALL
223 { C::execution_next_pc, 20 },
224 { C::execution_context_id, 2 }, // Previous row next_context_id
225 { C::execution_next_context_id, 3 }, // Incremented due to previous call
226 { C::execution_parent_id, 1 }, // Previous row context id
227 { C::execution_is_parent_id_inv, 1 },
228 { C::execution_has_parent_ctx, 1 },
229 { C::execution_contract_address, 0xdeadbeef },
230 { C::execution_bytecode_id, nested_bytecode_id }, // New bytecode_id on entering new context
231 { C::execution_parent_calldata_addr, 10 },
232 { C::execution_parent_calldata_size, 1 },
233 },
234 // Exceptional Halt Row
235 {
236 { C::execution_sel, 1 },
237 { C::execution_pc, 20 },
238 { C::execution_next_pc, 30 },
239 { C::execution_sel_execute_return, 1 },
240 { C::execution_rop_0_, 500 }, // Return data size offset
241 { C::execution_rop_1_, 600 }, // Return data offset
242 { C::execution_register_0_, 200 }, // Return data size
243 { C::execution_sel_exit_call, 1 },
244 { C::execution_nested_exit_call, 1 },
245 { C::execution_sel_error, 1 }, // Exceptional Halt
246 { C::execution_context_id, 2 },
247 { C::execution_next_context_id, 3 },
248 { C::execution_parent_id, 1 },
249 { C::execution_is_parent_id_inv, 1 },
250 { C::execution_has_parent_ctx, 1 },
251 { C::execution_contract_address, 0xdeadbeef },
252 { C::execution_bytecode_id, nested_bytecode_id }, // Propagated within same context
253 { C::execution_parent_calldata_addr, 10 },
254 { C::execution_parent_calldata_size, 1 },
255 },
256 {
257 { C::execution_sel, 1 },
258 { C::execution_next_context_id, 3 },
259 { C::execution_context_id, 1 },
260 { C::execution_parent_id, 0 },
261 { C::execution_last_child_id, 2 }, // Previous context id
262 { C::execution_pc, 2 }, // Based on next_pc of CALL step
263 { C::execution_next_pc, 3 },
264 { C::execution_msg_sender, 0 },
265 { C::execution_contract_address, 0 },
266 { C::execution_bytecode_id, top_bytecode_id }, // Restored from context stack
267 { C::execution_is_static, 0 },
268 { C::execution_parent_calldata_addr, 0 },
269 { C::execution_parent_calldata_size, 0 },
270 { C::execution_last_child_returndata_size, 0 }, // Return data size reset
271 { C::execution_last_child_returndata_addr, 0 }, // Return data offset reset
272 { C::execution_parent_l2_gas_limit, 2000 },
273 { C::execution_parent_da_gas_limit, 4000 },
274 { C::execution_parent_l2_gas_used, 500 },
275 { C::execution_parent_da_gas_used, 1500 },
276 },
277 {
278 { C::execution_sel, 0 },
279 } });
280
281 check_relation<context>(trace);
282
283 check_interaction<tracegen::ExecutionTraceBuilder,
287}
288
289TEST(ContextConstrainingTest, GasNextRow)
290{
291 TestTraceContainer trace({ { { C::precomputed_first_row, 1 } },
292 {
293 // First Row of execution
294 { C::execution_sel, 1 },
295 { C::execution_l2_gas_limit, 1000 },
296 { C::execution_da_gas_limit, 2000 },
297 { C::execution_parent_l2_gas_limit, 2000 },
298 { C::execution_parent_da_gas_limit, 4000 },
299 { C::execution_parent_l2_gas_used, 500 },
300 { C::execution_parent_da_gas_used, 1500 },
301 },
302 {
303 // CALL
304 { C::execution_sel, 1 },
305 { C::execution_sel_enter_call, 1 },
306 { C::execution_l2_gas_used, 200 },
307 { C::execution_da_gas_used, 300 },
308 { C::execution_l2_gas_limit, 1000 },
309 { C::execution_da_gas_limit, 2000 },
310 { C::execution_parent_l2_gas_limit, 2000 },
311 { C::execution_parent_da_gas_limit, 4000 },
312 { C::execution_parent_l2_gas_used, 500 },
313 { C::execution_parent_da_gas_used, 1500 },
314 },
315 {
316 // Return
317 { C::execution_sel, 1 },
318 { C::execution_sel_exit_call, 1 },
319 { C::execution_nested_exit_call, 1 },
320 { C::execution_parent_l2_gas_limit, 1000 },
321 { C::execution_parent_da_gas_limit, 2000 },
322 { C::execution_parent_l2_gas_used, 200 },
323 { C::execution_parent_da_gas_used, 300 },
324 },
325 {
326 // After return
327 { C::execution_sel, 1 },
328 { C::execution_l2_gas_limit, 1000 },
329 { C::execution_da_gas_limit, 2000 },
330 { C::execution_parent_l2_gas_limit, 2000 },
331 { C::execution_parent_da_gas_limit, 4000 },
332 },
333 {
334 { C::execution_sel, 0 },
335 } });
336
337 check_relation<context>(trace,
350
351 // Negative test: after return, restore wrong limits
352 trace.set(C::execution_l2_gas_limit, 4, 1001);
354 "L2_GAS_LIMIT_RESTORE_ON_EXIT");
355 trace.set(C::execution_da_gas_limit, 4, 2001);
357 "DA_GAS_LIMIT_RESTORE_ON_EXIT");
358
359 // Negative test: inside a nested call, store wrong parent limit and used
360 trace.set(C::execution_parent_l2_gas_limit, 3, 2001);
362 "PARENT_L2_GAS_LIMIT_STORE_ON_ENTER");
363 trace.set(C::execution_parent_da_gas_limit, 3, 4001);
365 "PARENT_DA_GAS_LIMIT_STORE_ON_ENTER");
366 trace.set(C::execution_parent_l2_gas_used, 3, 201);
368 "PARENT_L2_GAS_USED_STORE_ON_ENTER");
369 trace.set(C::execution_parent_da_gas_used, 3, 301);
371 "PARENT_DA_GAS_USED_STORE_ON_ENTER");
372
373 // Negative test: when no calls have been made, limits, parent limits, and parent used shouldn't change
374 trace.set(C::execution_l2_gas_limit, 2, 1001);
375 EXPECT_THROW_WITH_MESSAGE(check_relation<context>(trace, context::SR_L2_GAS_LIMIT_NEXT_ROW),
376 "L2_GAS_LIMIT_NEXT_ROW");
377 trace.set(C::execution_da_gas_limit, 2, 2001);
378 EXPECT_THROW_WITH_MESSAGE(check_relation<context>(trace, context::SR_DA_GAS_LIMIT_NEXT_ROW),
379 "DA_GAS_LIMIT_NEXT_ROW");
380
381 trace.set(C::execution_parent_l2_gas_limit, 2, 2001);
383 "PARENT_L2_GAS_LIMIT_NEXT_ROW");
384 trace.set(C::execution_parent_da_gas_limit, 2, 4001);
386 "PARENT_DA_GAS_LIMIT_NEXT_ROW");
387
388 trace.set(C::execution_parent_l2_gas_used, 2, 501);
390 "PARENT_L2_GAS_USED_NEXT_ROW");
391 trace.set(C::execution_parent_da_gas_used, 2, 1501);
393 "PARENT_DA_GAS_USED_NEXT_ROW");
394}
395
396TEST(ContextConstrainingTest, GasUsedContinuity)
397{
398 TestTraceContainer trace({ { { C::precomputed_first_row, 1 } },
399 {
400 // First Row of execution
401 { C::execution_sel, 1 },
402 { C::execution_l2_gas_used, 100 },
403 { C::execution_da_gas_used, 200 },
404 },
405 {
406 // CALL
407 { C::execution_sel, 1 },
408 { C::execution_sel_enter_call, 1 },
409 { C::execution_l2_gas_used, 110 },
410 { C::execution_da_gas_used, 200 },
411 { C::execution_prev_l2_gas_used, 100 },
412 { C::execution_prev_da_gas_used, 200 },
413 },
414 {
415 // Return
416 { C::execution_sel, 1 },
417 { C::execution_sel_exit_call, 1 },
418 { C::execution_nested_exit_call, 1 },
419 { C::execution_l2_gas_used, 50 },
420 { C::execution_da_gas_used, 60 },
421 { C::execution_parent_l2_gas_used, 110 },
422 { C::execution_parent_da_gas_used, 200 },
423 { C::execution_prev_l2_gas_used, 0 },
424 { C::execution_prev_da_gas_used, 0 },
425 },
426 {
427 // After return
428 { C::execution_sel, 1 },
429 { C::execution_l2_gas_used, 170 },
430 { C::execution_da_gas_used, 260 },
431 { C::execution_prev_l2_gas_used, 160 }, // 110 + 50
432 { C::execution_prev_da_gas_used, 260 }, // 200 + 60
433 },
434 {
435 { C::execution_sel, 0 },
436 } });
437
438 check_relation<context>(trace,
445
446 // Negative test: after return, ingest a wrong value
447 trace.set(C::execution_prev_l2_gas_used, 4, 110);
448
450 "L2_GAS_USED_INGEST_AFTER_EXIT");
451
452 trace.set(C::execution_prev_da_gas_used, 4, 60);
454 "DA_GAS_USED_INGEST_AFTER_EXIT");
455
456 // Negative test: inside a nested call, start with non-zero gas used
457 trace.set(C::execution_prev_l2_gas_used, 3, 110);
459 "L2_GAS_USED_ZERO_AFTER_CALL");
460
461 trace.set(C::execution_prev_da_gas_used, 3, 200);
463 "DA_GAS_USED_ZERO_AFTER_CALL");
464
465 // Negative test: when no calls are made, prev gas used should be gas used of the previous row
466 trace.set(C::execution_prev_l2_gas_used, 2, 0);
468 "L2_GAS_USED_CONTINUITY");
469
470 trace.set(C::execution_prev_da_gas_used, 2, 0);
472 "DA_GAS_USED_CONTINUITY");
473}
474
475TEST(ContextConstrainingTest, TreeStateContinuity)
476{
477 TestTraceContainer trace({ { { C::precomputed_first_row, 1 } },
478 {
479 // First Row of execution
480 { C::execution_sel, 1 },
481 { C::execution_note_hash_tree_root, 10 },
482 { C::execution_note_hash_tree_size, 9 },
483 { C::execution_num_note_hashes_emitted, 8 },
484 { C::execution_nullifier_tree_root, 7 },
485 { C::execution_nullifier_tree_size, 6 },
486 { C::execution_num_nullifiers_emitted, 5 },
487 { C::execution_public_data_tree_root, 4 },
488 { C::execution_public_data_tree_size, 3 },
489 { C::execution_written_public_data_slots_tree_root, 2 },
490 { C::execution_written_public_data_slots_tree_size, 1 },
491 { C::execution_l1_l2_tree_root, 27 },
492 { C::execution_retrieved_bytecodes_tree_root, 26 },
493 { C::execution_retrieved_bytecodes_tree_size, 25 },
494 },
495 {
496 // Second row of execution
497 { C::execution_sel, 1 },
498 { C::execution_prev_note_hash_tree_root, 10 },
499 { C::execution_prev_note_hash_tree_size, 9 },
500 { C::execution_prev_num_note_hashes_emitted, 8 },
501 { C::execution_prev_nullifier_tree_root, 7 },
502 { C::execution_prev_nullifier_tree_size, 6 },
503 { C::execution_prev_num_nullifiers_emitted, 5 },
504 { C::execution_prev_public_data_tree_root, 4 },
505 { C::execution_prev_public_data_tree_size, 3 },
506 { C::execution_prev_written_public_data_slots_tree_root, 2 },
507 { C::execution_prev_written_public_data_slots_tree_size, 1 },
508 { C::execution_l1_l2_tree_root, 27 },
509 { C::execution_prev_retrieved_bytecodes_tree_root, 26 },
510 { C::execution_prev_retrieved_bytecodes_tree_size, 25 },
511 { C::execution_enqueued_call_end, 1 },
512 { C::execution_sel_exit_call, 1 },
513 },
514 {
515 // Third row of execution
516 { C::execution_sel, 1 },
517 { C::execution_prev_note_hash_tree_root, 100 },
518 { C::execution_prev_note_hash_tree_size, 90 },
519 { C::execution_prev_num_note_hashes_emitted, 80 },
520 { C::execution_prev_nullifier_tree_root, 70 },
521 { C::execution_prev_nullifier_tree_size, 60 },
522 { C::execution_prev_num_nullifiers_emitted, 50 },
523 { C::execution_prev_public_data_tree_root, 40 },
524 { C::execution_prev_public_data_tree_size, 30 },
525 { C::execution_prev_written_public_data_slots_tree_root, 20 },
526 { C::execution_prev_written_public_data_slots_tree_size, 10 },
527 { C::execution_l1_l2_tree_root, 27 },
528 { C::execution_prev_retrieved_bytecodes_tree_root, 260 },
529 { C::execution_prev_retrieved_bytecodes_tree_size, 250 },
530 } });
531
532 check_relation<context>(trace,
546
547 // Negative test: change note hash tree root
548 trace.set(C::execution_prev_note_hash_tree_root, 2, 100);
550 "NOTE_HASH_TREE_ROOT_CONTINUITY");
551
552 // Negative test: change note hash tree size
553 trace.set(C::execution_prev_note_hash_tree_size, 2, 100);
555 "NOTE_HASH_TREE_SIZE_CONTINUITY");
556
557 // Negative test: change num note hashes emitted
558 trace.set(C::execution_prev_num_note_hashes_emitted, 2, 10);
560 "NUM_NOTE_HASHES_EMITTED_CONTINUITY");
561
562 // Negative test: change nullifier tree root
563 trace.set(C::execution_prev_nullifier_tree_root, 2, 100);
565 "NULLIFIER_TREE_ROOT_CONTINUITY");
566
567 // Negative test: change nullifier tree size
568 trace.set(C::execution_prev_nullifier_tree_size, 2, 100);
570 "NULLIFIER_TREE_SIZE_CONTINUITY");
571
572 // Negative test: change num nullifiers emitted
573 trace.set(C::execution_prev_num_nullifiers_emitted, 2, 100);
575 "NUM_NULLIFIERS_EMITTED_CONTINUITY");
576
577 // Negative test: change public data tree root
578 trace.set(C::execution_prev_public_data_tree_root, 2, 100);
580 "PUBLIC_DATA_TREE_ROOT_CONTINUITY");
581
582 // Negative test: change public data tree size
583 trace.set(C::execution_prev_public_data_tree_size, 2, 100);
585 "PUBLIC_DATA_TREE_SIZE_CONTINUITY");
586
587 // Negative test: change written public data slots tree root
588 trace.set(C::execution_prev_written_public_data_slots_tree_root, 2, 100);
591 "WRITTEN_PUBLIC_DATA_SLOTS_TREE_ROOT_CONTINUITY");
592
593 // Negative test: change written public data slots tree size
594 trace.set(C::execution_prev_written_public_data_slots_tree_size, 2, 100);
597 "WRITTEN_PUBLIC_DATA_SLOTS_TREE_SIZE_CONTINUITY");
598
599 // Negative test: change l1 l2 tree root
600 trace.set(C::execution_l1_l2_tree_root, 2, 100);
602 "L1_L2_TREE_ROOT_CONTINUITY");
603
604 // Negative test: change retrieved bytecodes tree root
605 trace.set(C::execution_prev_retrieved_bytecodes_tree_root, 2, 100);
607 "RETRIEVED_BYTECODES_TREE_ROOT_CONTINUITY");
608
609 // Negative test: change retrieved bytecodes tree size
610 trace.set(C::execution_prev_retrieved_bytecodes_tree_size, 2, 100);
612 "RETRIEVED_BYTECODES_TREE_SIZE_CONTINUITY");
613}
614
615TEST(ContextConstrainingTest, SideEffectStateContinuity)
616{
617 TestTraceContainer trace({
618 { { C::precomputed_first_row, 1 } },
619 {
620 // First Row of execution
621 { C::execution_sel, 1 },
622 { C::execution_num_unencrypted_log_fields, 10 },
623 { C::execution_num_l2_to_l1_messages, 11 },
624 },
625 {
626 // Second row of execution
627 { C::execution_sel, 1 },
628 { C::execution_prev_num_unencrypted_log_fields, 10 },
629 { C::execution_prev_num_l2_to_l1_messages, 11 },
630 },
631 });
632
633 check_relation<context>(
635
636 // Negative test: change num unencrypted logs
637 trace.set(C::execution_prev_num_unencrypted_log_fields, 2, 100);
639 "NUM_UNENCRYPTED_LOGS_CONTINUITY");
640
641 // Negative test: change num l2 to l1 messages
642 trace.set(C::execution_prev_num_l2_to_l1_messages, 2, 100);
644 "NUM_L2_TO_L1_MESSAGES_CONTINUITY");
645}
646
647TEST(ContextConstrainingTest, BytecodeIdPropagation)
648{
649 TestTraceContainer trace({ // First row - setup
650 {
651 { C::precomputed_first_row, 1 },
652 { C::execution_sel, 1 },
653 { C::execution_context_id, 1 },
654 { C::execution_next_context_id, 1 },
655 { C::execution_bytecode_id, 42 }, // Initial bytecode_id
656 },
657 // Second row - should propagate bytecode_id
658 {
659 { C::execution_sel, 1 },
660 { C::execution_context_id, 1 },
661 { C::execution_next_context_id, 1 },
662 { C::execution_bytecode_id, 42 }, // Same bytecode_id (propagated)
663 } });
664
665 check_relation<context>(trace);
666 // mutate the bytecode_id and confirm that it is a violation
667 trace.set(C::execution_bytecode_id, 1, 99);
668 EXPECT_THROW_WITH_MESSAGE(check_relation<context>(trace, context::SR_BYTECODE_ID_NEXT_ROW),
669 "BYTECODE_ID_NEXT_ROW"); // Should fail constraint
670}
671
672TEST(ContextConstrainingTest, IsStaticRegularCallFromNonStaticContext)
673{
674 // Non-static context making a regular CALL - should remain non-static
675 TestTraceContainer trace({
676 { { C::precomputed_first_row, 1 } },
677 {
678 { C::execution_sel, 1 },
679 { C::execution_context_id, 1 },
680 { C::execution_next_context_id, 2 },
681 { C::execution_is_static, 0 }, // Non-static context
682 { C::execution_sel_enter_call, 1 },
683 { C::execution_sel_execute_call, 1 }, // Regular CALL
684 { C::execution_sel_execute_static_call, 0 },
685 },
686 {
687 { C::execution_sel, 1 },
688 { C::execution_context_id, 2 },
689 { C::execution_next_context_id, 3 },
690 { C::execution_is_static, 0 }, // Should remain non-static
691 },
692 });
693 check_relation<context>(
695
696 // Negative test: change is_static
697 // regular call from non-static context cannot become static
698 trace.set(C::execution_is_static, 2, 1);
700 "IS_STATIC_IF_STATIC_CALL");
701
702 // reset is_static
703 trace.set(C::execution_is_static, 2, 0);
704}
705
706TEST(ContextConstrainingTest, IsStaticStaticCallFromNonStaticContext)
707{
708 // Non-static context making a STATICCALL - should become static
709 TestTraceContainer trace({
710 { { C::precomputed_first_row, 1 } },
711 {
712 { C::execution_sel, 1 },
713 { C::execution_context_id, 1 },
714 { C::execution_next_context_id, 2 },
715 { C::execution_is_static, 0 }, // Non-static context
716 { C::execution_sel_enter_call, 1 },
717 { C::execution_sel_execute_call, 0 },
718 { C::execution_sel_execute_static_call, 1 }, // STATICCALL
719 },
720 {
721 { C::execution_sel, 1 },
722 { C::execution_context_id, 2 },
723 { C::execution_next_context_id, 3 },
724 { C::execution_is_static, 1 }, // Should become static
725 },
726 });
727 check_relation<context>(
729
730 // Negative test: change is_static
731 // static call from non-static context MUST become static
732 trace.set(C::execution_is_static, 2, 0);
734 "IS_STATIC_IF_STATIC_CALL");
735
736 // reset is_static
737 trace.set(C::execution_is_static, 2, 1);
738}
739
740TEST(ContextConstrainingTest, IsStaticCallFromStaticContext)
741{
742 // Static context making any call - must remain static
743 TestTraceContainer trace({
744 { { C::precomputed_first_row, 1 } },
745 {
746 { C::execution_sel, 1 },
747 { C::execution_context_id, 1 },
748 { C::execution_next_context_id, 2 },
749 { C::execution_is_static, 1 }, // Static context
750 { C::execution_sel_enter_call, 1 },
751 { C::execution_sel_execute_call, 1 }, // Regular CALL
752 { C::execution_sel_execute_static_call, 0 },
753 },
754 {
755 { C::execution_sel, 1 },
756 { C::execution_context_id, 2 },
757 { C::execution_next_context_id, 3 },
758 { C::execution_is_static, 1 }, // Must remain static
759 },
760 });
761 check_relation<context>(
763
764 // Negative test: change is_static
765 // static call from static context MUST remain static
766 trace.set(C::execution_is_static, 2, 0);
768 "IS_STATIC_IF_CALL_FROM_STATIC_CONTEXT");
769
770 // reset is_static
771 trace.set(C::execution_is_static, 2, 1);
772}
773
774TEST(ContextConstrainingTest, IsStaticPropagationWithoutCalls)
775{
776 // is_static propagation without calls
777 TestTraceContainer trace({
778 { { C::precomputed_first_row, 1 } },
779 {
780 { C::execution_sel, 1 },
781 { C::execution_context_id, 1 },
782 { C::execution_next_context_id, 1 },
783 { C::execution_is_static, 1 }, // Static context
784 },
785 {
786 { C::execution_sel, 1 },
787 { C::execution_context_id, 1 },
788 { C::execution_next_context_id, 1 },
789 { C::execution_is_static, 1 }, // Should propagate
790 },
791 });
792 check_relation<context>(trace, context::SR_IS_STATIC_NEXT_ROW);
793
794 // Negative test: change is_static
795 // staticness must propagate without calls
796 trace.set(C::execution_is_static, 2, 0);
797 EXPECT_THROW_WITH_MESSAGE(check_relation<context>(trace, context::SR_IS_STATIC_NEXT_ROW), "IS_STATIC_NEXT_ROW");
798
799 // reset is_static
800 trace.set(C::execution_is_static, 2, 1);
801}
802
803TEST(ContextConstrainingTest, ContextIdPropagation)
804{
805 TestTraceContainer trace({
806 {
807 { C::precomputed_first_row, 1 },
808 },
809 {
810 { C::execution_sel, 1 },
811 { C::execution_enqueued_call_start, 1 },
812 { C::execution_context_id, 1 },
813 { C::execution_next_context_id, 2 },
814 { C::execution_sel_enter_call, 1 },
815 },
816 {
817 { C::execution_sel, 1 },
818 { C::execution_context_id, 2 },
819 { C::execution_next_context_id, 3 },
820 { C::execution_sel_exit_call, 1 },
821 { C::execution_nested_exit_call, 1 },
822 { C::execution_parent_id, 1 },
823 },
824 {
825 { C::execution_sel, 1 },
826 { C::execution_context_id, 1 },
827 { C::execution_next_context_id, 3 },
828 },
829 {
830 { C::execution_sel, 1 },
831 { C::execution_context_id, 1 },
832 { C::execution_next_context_id, 3 },
833 },
834 });
835 check_relation<context>(trace,
841
842 // Negative test: next context id should be context id + 1 on enqueued call start
843 trace.set(C::execution_next_context_id, 1, 3);
845 "ENQUEUED_CALL_START_NEXT_CTX_ID");
846 trace.set(C::execution_next_context_id, 1, 2);
847
848 // Negative test: next context id should increase on external call
849 trace.set(C::execution_next_context_id, 2, 2);
850 EXPECT_THROW_WITH_MESSAGE(check_relation<context>(trace, context::SR_INCR_NEXT_CONTEXT_ID), "INCR_NEXT_CONTEXT_ID");
851 trace.set(C::execution_next_context_id, 2, 3);
852
853 // Negative test: next context id should be propagated
854 trace.set(C::execution_next_context_id, 4, 4);
855 EXPECT_THROW_WITH_MESSAGE(check_relation<context>(trace, context::SR_INCR_NEXT_CONTEXT_ID), "INCR_NEXT_CONTEXT_ID");
856 trace.set(C::execution_next_context_id, 4, 3);
857
858 // Negative test: context id should be propagated
859 trace.set(C::execution_context_id, 4, 2);
860 EXPECT_THROW_WITH_MESSAGE(check_relation<context>(trace, context::SR_CONTEXT_ID_NEXT_ROW), "CONTEXT_ID_NEXT_ROW");
861 trace.set(C::execution_context_id, 4, 1);
862
863 // Negative test: context id should be next context id when entering call
864 trace.set(C::execution_context_id, 2, 1);
865 EXPECT_THROW_WITH_MESSAGE(check_relation<context>(trace, context::SR_CONTEXT_ID_EXT_CALL), "CONTEXT_ID_EXT_CALL");
866 trace.set(C::execution_context_id, 2, 2);
867
868 // Negative test: context id should be restored on exit
869 trace.set(C::execution_context_id, 3, 2);
871 "CONTEXT_ID_NESTED_EXIT");
872 trace.set(C::execution_context_id, 3, 1);
873}
874
875} // namespace
876} // namespace bb::avm2::constraining
static constexpr size_t SR_PARENT_DA_GAS_USED_NEXT_ROW
Definition context.hpp:83
static constexpr size_t SR_INCR_NEXT_CONTEXT_ID
Definition context.hpp:43
static constexpr size_t SR_L1_L2_TREE_ROOT_CONTINUITY
Definition context.hpp:103
static constexpr size_t SR_DA_GAS_USED_INGEST_AFTER_EXIT
Definition context.hpp:92
static constexpr size_t SR_L2_GAS_USED_ZERO_AFTER_CALL
Definition context.hpp:88
static constexpr size_t SR_PARENT_L2_GAS_LIMIT_STORE_ON_ENTER
Definition context.hpp:78
static constexpr size_t SR_PARENT_L2_GAS_USED_STORE_ON_ENTER
Definition context.hpp:82
static constexpr size_t SR_NOTE_HASH_TREE_SIZE_CONTINUITY
Definition context.hpp:94
static constexpr size_t SR_IS_STATIC_IF_CALL_FROM_STATIC_CONTEXT
Definition context.hpp:57
static constexpr size_t SR_RETRIEVED_BYTECODES_TREE_ROOT_CONTINUITY
Definition context.hpp:85
static constexpr size_t SR_WRITTEN_PUBLIC_DATA_SLOTS_TREE_ROOT_CONTINUITY
Definition context.hpp:101
static constexpr size_t SR_L2_GAS_LIMIT_RESTORE_ON_EXIT
Definition context.hpp:74
static constexpr size_t SR_DA_GAS_USED_CONTINUITY
Definition context.hpp:90
static constexpr size_t SR_L2_GAS_USED_INGEST_AFTER_EXIT
Definition context.hpp:89
static constexpr size_t SR_PUBLIC_DATA_TREE_SIZE_CONTINUITY
Definition context.hpp:100
static constexpr size_t SR_PARENT_DA_GAS_USED_STORE_ON_ENTER
Definition context.hpp:84
static constexpr size_t SR_NUM_NOTE_HASHES_EMITTED_CONTINUITY
Definition context.hpp:95
static constexpr size_t SR_PARENT_L2_GAS_LIMIT_NEXT_ROW
Definition context.hpp:77
static constexpr size_t SR_NULLIFIER_TREE_SIZE_CONTINUITY
Definition context.hpp:97
static constexpr size_t SR_CONTEXT_ID_EXT_CALL
Definition context.hpp:45
static constexpr size_t SR_DA_GAS_USED_ZERO_AFTER_CALL
Definition context.hpp:91
static constexpr size_t SR_NUM_L2_TO_L1_MESSAGES_CONTINUITY
Definition context.hpp:105
static constexpr size_t SR_ENQUEUED_CALL_START_NEXT_CTX_ID
Definition context.hpp:42
static constexpr size_t SR_PARENT_DA_GAS_LIMIT_STORE_ON_ENTER
Definition context.hpp:80
static constexpr size_t SR_NOTE_HASH_TREE_ROOT_CONTINUITY
Definition context.hpp:93
static constexpr size_t SR_CONTEXT_ID_NESTED_EXIT
Definition context.hpp:46
static constexpr size_t SR_WRITTEN_PUBLIC_DATA_SLOTS_TREE_SIZE_CONTINUITY
Definition context.hpp:102
static constexpr size_t SR_BYTECODE_ID_NEXT_ROW
Definition context.hpp:53
static constexpr size_t SR_CONTEXT_ID_NEXT_ROW
Definition context.hpp:44
static constexpr size_t SR_RETRIEVED_BYTECODES_TREE_SIZE_CONTINUITY
Definition context.hpp:86
static constexpr size_t SR_PUBLIC_DATA_TREE_ROOT_CONTINUITY
Definition context.hpp:99
static constexpr size_t SR_NUM_NULLIFIERS_EMITTED_CONTINUITY
Definition context.hpp:98
static constexpr size_t SR_L2_GAS_LIMIT_NEXT_ROW
Definition context.hpp:73
static constexpr size_t SR_NUM_UNENCRYPTED_LOGS_CONTINUITY
Definition context.hpp:104
static constexpr size_t SR_DA_GAS_LIMIT_RESTORE_ON_EXIT
Definition context.hpp:76
static constexpr size_t SR_DA_GAS_LIMIT_NEXT_ROW
Definition context.hpp:75
static constexpr size_t SR_PARENT_DA_GAS_LIMIT_NEXT_ROW
Definition context.hpp:79
static constexpr size_t SR_IS_STATIC_IF_STATIC_CALL
Definition context.hpp:56
static constexpr size_t SR_IS_STATIC_NEXT_ROW
Definition context.hpp:55
static constexpr size_t SR_NULLIFIER_TREE_ROOT_CONTINUITY
Definition context.hpp:96
static constexpr size_t SR_PARENT_L2_GAS_USED_NEXT_ROW
Definition context.hpp:81
static constexpr size_t SR_L2_GAS_USED_CONTINUITY
Definition context.hpp:87
void set(Column col, uint32_t row, const FF &value)
TestTraceContainer trace
StrictMock< MockContext > context
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessage)
Definition macros.hpp:7
void check_interaction(tracegen::TestTraceContainer &trace)
TEST(TxExecutionConstrainingTest, WriteTreeValue)
Definition tx.test.cpp:441
TestTraceContainer empty_trace()
Definition fixtures.cpp:153
lookup_settings< lookup_context_ctx_stack_rollback_settings_ > lookup_context_ctx_stack_rollback_settings
lookup_settings< lookup_context_ctx_stack_return_settings_ > lookup_context_ctx_stack_return_settings
lookup_settings< lookup_context_ctx_stack_call_settings_ > lookup_context_ctx_stack_call_settings