Skip to content

Commit c294f91

Browse files
authored
feat(AVM)!: Enable tx=>execution lookup (#16455)
Enables the lookup now that it's passing bulk test, and reduces two columns in the tx trace for context ids
2 parents 286fbfa + a6d5b2f commit c294f91

File tree

10 files changed

+676
-507
lines changed

10 files changed

+676
-507
lines changed

barretenberg/cpp/pil/vm2/context.pil

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ namespace execution;
4444
// ==== Gas ====
4545
pol commit l2_gas_limit;
4646
pol commit da_gas_limit;
47+
48+
pol commit prev_l2_gas_used;
49+
pol commit prev_da_gas_used;
50+
4751
pol commit l2_gas_used;
4852
pol commit da_gas_used;
4953

@@ -114,16 +118,23 @@ namespace execution;
114118

115119
// next_context_id increments with each invocation of an external call or new enqueued call
116120
pol commit next_context_id; // Can be replaced by clk
121+
122+
// We always start enqueued calls with fresh context ids, so the next context id must start at context_id + 1
123+
#[ENQUEUED_CALL_START_NEXT_CTX_ID]
124+
enqueued_call_start * (context_id + 1 - next_context_id) = 0;
125+
117126
// The initial next_context_id = 2, in row = 1
118127
#[INCR_NEXT_CONTEXT_ID]
119128
NOT_LAST_EXEC * (next_context_id' - (next_context_id + sel_enter_call)) = 0;
120129

121130
// nested_exit_call = 1 ==> context_id' = parent_id
122131
// sel_enter_call = 1 ==> context_id' = next_context_id
123132
// otherwise = 0 ==> context_id' = context_id
124-
#[CONTEXT_ID_CALL_NEXT_ROW]
133+
#[CONTEXT_ID_NEXT_ROW]
125134
NOT_LAST_EXEC * DEFAULT_CTX_ROW * (context_id' - context_id) = 0;
135+
#[CONTEXT_ID_EXT_CALL]
126136
NOT_LAST_EXEC * sel_enter_call * (context_id' - next_context_id) = 0;
137+
#[CONTEXT_ID_NESTED_EXIT]
127138
NOT_LAST_EXEC * nested_exit_call * (context_id' - parent_id) = 0;
128139

129140
// nested_exit_call = 1 ==> constraints come from lookup
@@ -429,9 +440,6 @@ namespace execution;
429440
// nested_exit_call = 1 <==> prev_gas_used' = parent_gas_used + gas_used
430441
// sel_enter_call = 1 <==> prev_gas_used' = 0
431442
// otherwise = 0 <==> prev_gas_used' = gas_used
432-
pol commit prev_l2_gas_used;
433-
pol commit prev_da_gas_used;
434-
435443
#[L2_GAS_USED_CONTINUITY]
436444
NOT_LAST_EXEC * DEFAULT_CTX_ROW * (l2_gas_used - prev_l2_gas_used') = 0;
437445
#[L2_GAS_USED_ZERO_AFTER_CALL]

barretenberg/cpp/pil/vm2/tx.pil

Lines changed: 106 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -201,16 +201,6 @@ namespace tx;
201201
pol commit should_process_call_request;
202202
should_process_call_request = is_public_call_request * (1 - is_padded);
203203

204-
// When sending a call to execution, next context id is incremented by one, since we use next_context_id for the call.
205-
pol commit next_context_id_sent_to_enqueued_call;
206-
// When coming back from a call, the next context id may have incremented an amount we can't predict from the tx trace.
207-
pol commit next_context_id_from_enqueued_call;
208-
209-
// When issuing a call, context id is used for the call, making the next context id it plus one.
210-
should_process_call_request * ((next_context_id + 1) - next_context_id_sent_to_enqueued_call) = 0;
211-
// When comming back from the call, the newly available context id is the next_context_id that comes from the end of the call.
212-
should_process_call_request * (next_context_id_from_enqueued_call - next_context_id') = 0;
213-
214204
pol commit is_teardown_phase; // TODO: Constrain
215205
is_teardown_phase * (1 - is_teardown_phase) = 0;
216206

@@ -229,116 +219,114 @@ namespace tx;
229219

230220
// Public Call Requests are dispatch to the execution trace
231221
// We match the values to the start of an enqueued call in the execution trace
232-
// Commented out for now since we dont have enqueued call flags in execution set
233-
// #[DISPATCH_EXEC_START]
234-
// should_process_call_request {
235-
// next_context_id,
236-
// next_context_id_sent_to_enqueued_call,
237-
// discard,
238-
// msg_sender,
239-
// contract_addr,
240-
// fee,
241-
// is_static,
242-
// // Tree State
243-
// prev_note_hash_tree_root,
244-
// prev_note_hash_tree_size,
245-
// prev_num_note_hashes_emitted,
246-
// prev_nullifier_tree_size,
247-
// prev_num_nullifiers_emitted,
248-
// prev_public_data_tree_root,
249-
// prev_public_data_tree_size,
250-
// prev_written_public_data_slots_tree_root,
251-
// prev_written_public_data_slots_tree_size,
252-
// l1_l2_tree_root,
253-
// // Side Effect States
254-
// prev_num_unencrypted_logs,
255-
// prev_num_l2_to_l1_messages,
256-
// // Gas Info
257-
// prev_l2_gas_used_sent_to_enqueued_call,
258-
// prev_da_gas_used_sent_to_enqueued_call,
259-
// l2_gas_limit,
260-
// da_gas_limit
261-
// } in
262-
// execution.enqueued_call_start {
263-
// execution.context_id,
264-
// execution.next_context_id,
265-
// execution.discard,
266-
// execution.msg_sender,
267-
// execution.contract_address,
268-
// execution.transaction_fee,
269-
// execution.is_static,
270-
// // Tree State
271-
// execution.prev_note_hash_tree_root,
272-
// execution.prev_note_hash_tree_size,
273-
// execution.prev_num_note_hashes_emitted,
274-
// execution.prev_nullifier_tree_size,
275-
// execution.prev_num_nullifiers_emitted,
276-
// execution.prev_public_data_tree_root,
277-
// execution.prev_public_data_tree_size,
278-
// execution.prev_written_public_data_slots_tree_root,
279-
// execution.prev_written_public_data_slots_tree_size,
280-
// execution.l1_l2_tree_root,
281-
// // Side Effect States
282-
// execution.prev_num_unencrypted_logs,
283-
// execution.prev_num_l2_to_l1_messages,
284-
// // Gas Info
285-
// execution.prev_l2_gas_used_sent_to_enqueued_call,
286-
// execution.prev_da_gas_used_sent_to_enqueued_call,
287-
// execution.l2_gas_limit,
288-
// execution.da_gas_limit
289-
// };
222+
#[DISPATCH_EXEC_START]
223+
should_process_call_request {
224+
next_context_id,
225+
discard,
226+
msg_sender,
227+
contract_addr,
228+
fee,
229+
is_static,
230+
// Tree State
231+
prev_note_hash_tree_root,
232+
prev_note_hash_tree_size,
233+
prev_num_note_hashes_emitted,
234+
prev_nullifier_tree_root,
235+
prev_nullifier_tree_size,
236+
prev_num_nullifiers_emitted,
237+
prev_public_data_tree_root,
238+
prev_public_data_tree_size,
239+
prev_written_public_data_slots_tree_root,
240+
prev_written_public_data_slots_tree_size,
241+
l1_l2_tree_root,
242+
// Side Effect States
243+
prev_num_unencrypted_logs,
244+
prev_num_l2_to_l1_messages,
245+
// Gas Info
246+
prev_l2_gas_used_sent_to_enqueued_call,
247+
prev_da_gas_used_sent_to_enqueued_call,
248+
l2_gas_limit,
249+
da_gas_limit
250+
} in
251+
execution.enqueued_call_start {
252+
execution.context_id, // next_context_id must be constrained in the execution trace on enqueued_call_start
253+
execution.discard,
254+
execution.msg_sender,
255+
execution.contract_address,
256+
execution.transaction_fee,
257+
execution.is_static,
258+
// Tree State
259+
execution.prev_note_hash_tree_root,
260+
execution.prev_note_hash_tree_size,
261+
execution.prev_num_note_hashes_emitted,
262+
execution.prev_nullifier_tree_root,
263+
execution.prev_nullifier_tree_size,
264+
execution.prev_num_nullifiers_emitted,
265+
execution.prev_public_data_tree_root,
266+
execution.prev_public_data_tree_size,
267+
execution.prev_written_public_data_slots_tree_root,
268+
execution.prev_written_public_data_slots_tree_size,
269+
execution.l1_l2_tree_root,
270+
// Side Effect States
271+
execution.prev_num_unencrypted_logs,
272+
execution.prev_num_l2_to_l1_messages,
273+
// Gas Info
274+
execution.prev_l2_gas_used,
275+
execution.prev_da_gas_used,
276+
execution.l2_gas_limit,
277+
execution.da_gas_limit
278+
};
290279

291280
// We retrieve the return values at the end of an enqueued call in the execution trace
292-
// Commented out for now since we dont have enqueued call flags in execution set
293-
//#[DISPATCH_EXEC_GET_REVERT]
294-
//should_process_call_request {
295-
// next_context_id,
296-
// next_context_id_from_enqueued_call,
297-
// reverted,
298-
// discard,
299-
// // Tree State
300-
// next_note_hash_tree_root,
301-
// next_note_hash_tree_size,
302-
// next_num_note_hashes_emitted,
303-
// next_nullifier_tree_root,
304-
// next_nullifier_tree_size,
305-
// next_num_nullifiers_emitted,
306-
// next_public_data_tree_root,
307-
// next_public_data_tree_size,
308-
// next_written_public_data_slots_tree_root,
309-
// next_written_public_data_slots_tree_size,
310-
// l1_l2_tree_root,
311-
// // Side Effect States
312-
// next_num_unencrypted_logs,
313-
// next_num_l2_to_l1_messages,
314-
// // Gas Info
315-
// next_l2_gas_used_sent_to_enqueued_call,
316-
// next_da_gas_used_sent_to_enqueued_call
317-
//} in
318-
//execution.enqueued_call_end {
319-
// execution.context_id,
320-
// execution.next_context_id,
321-
// execution.sel_error,
322-
// execution.discard,
323-
// // Tree State
324-
// execution.note_hash_tree_root,
325-
// execution.note_hash_tree_size,
326-
// execution.num_note_hashes_emitted,
327-
// execution.nullifier_tree_root,
328-
// execution.nullifier_tree_size,
329-
// execution.num_nullifiers_emitted,
330-
// execution.public_data_tree_root,
331-
// execution.public_data_tree_size,
332-
// execution.written_public_data_slots_tree_root,
333-
// execution.written_public_data_slots_tree_size,
334-
// execution.l1_l2_tree_root,
335-
// // Side Effect States
336-
// execution.num_unencrypted_logs,
337-
// execution.num_l2_to_l1_messages,
338-
// // Gas Info
339-
// execution.l2_gas_used_sent_to_enqueued_call,
340-
// execution.da_gas_used_sent_to_enqueued_call
341-
//};
281+
#[DISPATCH_EXEC_END]
282+
should_process_call_request {
283+
next_context_id,
284+
next_context_id', // The context id of the next row must be the one after the execution of the enqueued call
285+
reverted,
286+
discard,
287+
// Tree State
288+
next_note_hash_tree_root,
289+
next_note_hash_tree_size,
290+
next_num_note_hashes_emitted,
291+
next_nullifier_tree_root,
292+
next_nullifier_tree_size,
293+
next_num_nullifiers_emitted,
294+
next_public_data_tree_root,
295+
next_public_data_tree_size,
296+
next_written_public_data_slots_tree_root,
297+
next_written_public_data_slots_tree_size,
298+
l1_l2_tree_root,
299+
// Side Effect States
300+
next_num_unencrypted_logs,
301+
next_num_l2_to_l1_messages,
302+
// Gas Info
303+
next_l2_gas_used_sent_to_enqueued_call,
304+
next_da_gas_used_sent_to_enqueued_call
305+
} in
306+
execution.enqueued_call_end {
307+
execution.context_id,
308+
execution.next_context_id,
309+
execution.sel_error,
310+
execution.discard,
311+
// Tree State
312+
execution.note_hash_tree_root,
313+
execution.note_hash_tree_size,
314+
execution.num_note_hashes_emitted,
315+
execution.nullifier_tree_root,
316+
execution.nullifier_tree_size,
317+
execution.num_nullifiers_emitted,
318+
execution.public_data_tree_root,
319+
execution.public_data_tree_size,
320+
execution.written_public_data_slots_tree_root,
321+
execution.written_public_data_slots_tree_size,
322+
execution.l1_l2_tree_root,
323+
// Side Effect States
324+
execution.num_unencrypted_logs,
325+
execution.num_l2_to_l1_messages,
326+
// Gas Info
327+
execution.l2_gas_used,
328+
execution.da_gas_used
329+
};
342330

343331
// === PRIVATE SIDE EFFECT INSERTIONS ===
344332
pol commit sel_revertible_append_note_hash;

barretenberg/cpp/src/barretenberg/vm2/constraining/relations/context.test.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,5 +509,77 @@ TEST(ContextConstrainingTest, BytecodeIdPropagation)
509509
"BYTECODE_ID_NEXT_ROW"); // Should fail constraint
510510
}
511511

512+
TEST(ContextConstrainingTest, ContextIdPropagation)
513+
{
514+
TestTraceContainer trace({
515+
{
516+
{ C::precomputed_first_row, 1 },
517+
},
518+
{
519+
{ C::execution_sel, 1 },
520+
{ C::execution_enqueued_call_start, 1 },
521+
{ C::execution_context_id, 1 },
522+
{ C::execution_next_context_id, 2 },
523+
{ C::execution_sel_enter_call, 1 },
524+
},
525+
{
526+
{ C::execution_sel, 1 },
527+
{ C::execution_context_id, 2 },
528+
{ C::execution_next_context_id, 3 },
529+
{ C::execution_sel_exit_call, 1 },
530+
{ C::execution_nested_exit_call, 1 },
531+
{ C::execution_parent_id, 1 },
532+
},
533+
{
534+
{ C::execution_sel, 1 },
535+
{ C::execution_context_id, 1 },
536+
{ C::execution_next_context_id, 3 },
537+
},
538+
{
539+
{ C::execution_sel, 1 },
540+
{ C::execution_context_id, 1 },
541+
{ C::execution_next_context_id, 3 },
542+
},
543+
});
544+
check_relation<context>(trace,
545+
context::SR_ENQUEUED_CALL_START_NEXT_CTX_ID,
546+
context::SR_INCR_NEXT_CONTEXT_ID,
547+
context::SR_CONTEXT_ID_NEXT_ROW,
548+
context::SR_CONTEXT_ID_EXT_CALL,
549+
context::SR_CONTEXT_ID_NESTED_EXIT);
550+
551+
// Negative test: next context id should be context id + 1 on enqueued call start
552+
trace.set(C::execution_next_context_id, 1, 3);
553+
EXPECT_THROW_WITH_MESSAGE(check_relation<context>(trace, context::SR_ENQUEUED_CALL_START_NEXT_CTX_ID),
554+
"ENQUEUED_CALL_START_NEXT_CTX_ID");
555+
trace.set(C::execution_next_context_id, 1, 2);
556+
557+
// Negative test: next context id should increase on external call
558+
trace.set(C::execution_next_context_id, 2, 2);
559+
EXPECT_THROW_WITH_MESSAGE(check_relation<context>(trace, context::SR_INCR_NEXT_CONTEXT_ID), "INCR_NEXT_CONTEXT_ID");
560+
trace.set(C::execution_next_context_id, 2, 3);
561+
562+
// Negative test: next context id should be propagated
563+
trace.set(C::execution_next_context_id, 4, 4);
564+
EXPECT_THROW_WITH_MESSAGE(check_relation<context>(trace, context::SR_INCR_NEXT_CONTEXT_ID), "INCR_NEXT_CONTEXT_ID");
565+
trace.set(C::execution_next_context_id, 4, 3);
566+
567+
// Negative test: context id should be propagated
568+
trace.set(C::execution_context_id, 4, 2);
569+
EXPECT_THROW_WITH_MESSAGE(check_relation<context>(trace, context::SR_CONTEXT_ID_NEXT_ROW), "CONTEXT_ID_NEXT_ROW");
570+
trace.set(C::execution_context_id, 4, 1);
571+
572+
// Negative test: context id should be next context id when entering call
573+
trace.set(C::execution_context_id, 2, 1);
574+
EXPECT_THROW_WITH_MESSAGE(check_relation<context>(trace, context::SR_CONTEXT_ID_EXT_CALL), "CONTEXT_ID_EXT_CALL");
575+
trace.set(C::execution_context_id, 2, 2);
576+
577+
// Negative test: context id should be restored on exit
578+
trace.set(C::execution_context_id, 3, 2);
579+
EXPECT_THROW_WITH_MESSAGE(check_relation<context>(trace, context::SR_CONTEXT_ID_NESTED_EXIT),
580+
"CONTEXT_ID_NESTED_EXIT");
581+
trace.set(C::execution_context_id, 3, 1);
582+
}
583+
512584
} // namespace
513585
} // namespace bb::avm2::constraining

0 commit comments

Comments
 (0)