Skip to content

Commit 4607e72

Browse files
committed
feat(avm)!: Context id constraining in the tx trace
Tracks context ids properly in the tx trace, and adds context id emission to tx_context events in simulation
1 parent 4087b0c commit 4607e72

File tree

12 files changed

+716
-490
lines changed

12 files changed

+716
-490
lines changed

barretenberg/cpp/pil/vm2/tx.pil

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

204-
pol commit context_id; // TODO: Constrain
205-
pol commit next_context_id; // TODO: Constrain
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+
206214
pol commit is_teardown_phase; // TODO: Constrain
207215
is_teardown_phase * (1 - is_teardown_phase) = 0;
208216

@@ -224,8 +232,8 @@ namespace tx;
224232
// Commented out for now since we dont have enqueued call flags in execution set
225233
// #[DISPATCH_EXEC_START]
226234
// should_process_call_request {
227-
// context_id,
228235
// next_context_id,
236+
// next_context_id_sent_to_enqueued_call,
229237
// discard,
230238
// msg_sender,
231239
// contract_addr,
@@ -284,8 +292,8 @@ namespace tx;
284292
// Commented out for now since we dont have enqueued call flags in execution set
285293
//#[DISPATCH_EXEC_GET_REVERT]
286294
//should_process_call_request {
287-
// context_id,
288295
// next_context_id,
296+
// next_context_id_from_enqueued_call,
289297
// reverted,
290298
// discard,
291299
// // Tree State

barretenberg/cpp/pil/vm2/tx_context.pil

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,16 @@ namespace tx;
396396
precomputed.clk,
397397
public_inputs.cols[0]
398398
};
399+
400+
// ===== EXECUTION CONTEXT ID =====
401+
402+
// The next available context id. When peforming an enqueued call, this is the context id used by it.
403+
pol commit next_context_id;
404+
// Initial next context id is 1
405+
#[NEXT_CONTEXT_ID_INITIAL_VALUE]
406+
start_tx * (1 - next_context_id) = 0;
407+
408+
// We normally propagate the next context id, unless a call is issued, where the execution trace
409+
// controls the next row's next context id.
410+
#[NEXT_CONTEXT_ID_CONTINUITY]
411+
NOT_LAST * (1 - should_process_call_request) * (next_context_id' - next_context_id) = 0;

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

Lines changed: 485 additions & 372 deletions
Large diffs are not rendered by default.

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ TEST(TxContextConstrainingTest, InitialStateChecks)
359359
{ C::tx_next_l2_gas_used, start_gas_used.l2Gas },
360360
{ C::tx_next_da_gas_used, start_gas_used.daGas },
361361
{ C::tx_setup_phase_value, static_cast<uint8_t>(TransactionPhase::SETUP) },
362+
{ C::tx_next_context_id, 1 },
362363
},
363364
});
364365

@@ -480,5 +481,44 @@ TEST(TxContextConstrainingTest, EndStateChecks)
480481
lookup_tx_context_restore_state_on_revert_settings>(trace);
481482
}
482483

484+
TEST(TxContextConstrainingTest, NegativeContextIdChecks)
485+
{
486+
TestTraceContainer trace({
487+
{
488+
// Row 0
489+
{ C::precomputed_first_row, 1 },
490+
},
491+
{
492+
// Row 1
493+
{ C::tx_sel, 1 },
494+
{ C::tx_start_tx, 1 },
495+
{ C::tx_next_context_id, 1 },
496+
},
497+
{
498+
// Row 2
499+
{ C::tx_sel, 1 },
500+
{ C::tx_next_context_id, 1 },
501+
{ C::tx_should_process_call_request, 1 },
502+
},
503+
{
504+
// Row 3
505+
{ C::tx_sel, 1 },
506+
{ C::tx_next_context_id, 5 },
507+
},
508+
});
509+
check_relation<tx_context>(
510+
trace, tx_context::SR_NEXT_CONTEXT_ID_CONTINUITY, tx_context::SR_NEXT_CONTEXT_ID_INITIAL_VALUE);
511+
512+
// Negative test: initial context id should be 1
513+
trace.set(C::tx_next_context_id, 1, 2);
514+
EXPECT_THROW_WITH_MESSAGE(check_relation<tx_context>(trace, tx_context::SR_NEXT_CONTEXT_ID_INITIAL_VALUE),
515+
"NEXT_CONTEXT_ID_INITIAL_VALUE");
516+
517+
// Negative test: continuity check
518+
trace.set(C::tx_next_context_id, 2, 42);
519+
EXPECT_THROW_WITH_MESSAGE(check_relation<tx_context>(trace, tx_context::SR_NEXT_CONTEXT_ID_CONTINUITY),
520+
"NEXT_CONTEXT_ID_CONTINUITY");
521+
}
522+
483523
} // namespace
484524
} // namespace bb::avm2::constraining

barretenberg/cpp/src/barretenberg/vm2/generated/columns.hpp

Lines changed: 5 additions & 5 deletions
Large diffs are not rendered by default.

barretenberg/cpp/src/barretenberg/vm2/generated/flavor_variables.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ namespace bb::avm2 {
129129

130130
struct AvmFlavorVariables {
131131
static constexpr size_t NUM_PRECOMPUTED_ENTITIES = 133;
132-
static constexpr size_t NUM_WITNESS_ENTITIES = 2921;
133-
static constexpr size_t NUM_SHIFTED_ENTITIES = 314;
132+
static constexpr size_t NUM_WITNESS_ENTITIES = 2922;
133+
static constexpr size_t NUM_SHIFTED_ENTITIES = 315;
134134
static constexpr size_t NUM_WIRES = NUM_WITNESS_ENTITIES + NUM_PRECOMPUTED_ENTITIES;
135-
static constexpr size_t NUM_ALL_ENTITIES = 3368;
135+
static constexpr size_t NUM_ALL_ENTITIES = 3370;
136136

137137
// Need to be templated for recursive verifier
138138
template <typename FF_>

0 commit comments

Comments
 (0)