Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions barretenberg/cpp/pil/vm2/context.pil
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ namespace execution;
pol commit contract_address;
pol commit bytecode_id;
pol commit transaction_fee;
// Constrained boolean by tx trace (for enqueued call) and #[NEXT_IS_STATIC] for nested
// Constrained boolean by tx trace for enqueued call, #[IS_STATIC_NEXT_ROW] during normal execution,
// IS_STATIC_IF_STATIC_CALL+IS_STATIC_IF_CALL_FROM_STATIC_CONTEXT for nested calls,
// and CTX_STACK_CALL for returns or failures.
pol commit is_static;

pol commit parent_calldata_addr;
Expand Down Expand Up @@ -193,7 +195,12 @@ namespace execution;
// otherwise = 0 ==> is_static' = is_static
#[IS_STATIC_NEXT_ROW]
NOT_LAST_EXEC * DEFAULT_CTX_ROW * (is_static' - is_static) = 0;
NOT_LAST_EXEC * sel_enter_call * (is_static' - sel_execute_static_call) = 0;
// An external call from a non-static context only creates a nested static context if the opcode is STATICCALL.
Comment thread
dbanks12 marked this conversation as resolved.
#[IS_STATIC_IF_STATIC_CALL]
NOT_LAST_EXEC * sel_enter_call * (1 - is_static) * (is_static' - sel_execute_static_call) = 0;
// An external call from a static context always creates a nested static context.
#[IS_STATIC_IF_CALL_FROM_STATIC_CONTEXT]
NOT_LAST_EXEC * sel_enter_call * is_static * (is_static' - 1) = 0;

// nested_exit_call = 1 ==> constraints come from lookup
// sel_enter_call = 1 ==> parent_calldata_addr' = rop[4] (resolved operand 5 from execution trace)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,25 @@ TEST(ContextConstrainingTest, ContextSwitchingCallReturn)
{ C::execution_context_id, 1 },
{ C::execution_next_context_id, 2 },
{ C::execution_bytecode_id, top_bytecode_id },
{ C::execution_is_static, 0 }, // Non-static context
{ C::execution_parent_l2_gas_limit, 2000 },
{ C::execution_parent_da_gas_limit, 4000 },
{ C::execution_parent_l2_gas_used, 500 },
{ C::execution_parent_da_gas_used, 1500 },
{ C::execution_enqueued_call_start, 1 },
},
// CALL
{
{ C::execution_sel, 1 },
{ C::execution_pc, 1 },
{ C::execution_next_pc, 2 },
{ C::execution_sel_execute_call, 1 },
{ C::execution_sel_execute_static_call, 0 }, // Regular CALL, not STATICCALL
{ C::execution_sel_enter_call, 1 },
{ C::execution_context_id, 1 },
{ C::execution_next_context_id, 2 },
{ C::execution_bytecode_id, top_bytecode_id }, // Same as previous row (propagated)
{ C::execution_is_static, 0 }, // Still non-static
{ C::execution_rop_4_, /*cd offset=*/10 },
{ C::execution_register_2_, /*contract address=*/0xdeadbeef },
{ C::execution_register_3_, /*cd size=*/1 },
Expand All @@ -96,6 +100,7 @@ TEST(ContextConstrainingTest, ContextSwitchingCallReturn)
{ C::execution_has_parent_ctx, 1 },
{ C::execution_contract_address, 0xdeadbeef },
{ C::execution_bytecode_id, nested_bytecode_id }, // New bytecode_id on entering new context
{ C::execution_is_static, 0 }, // Remains non-static after regular CALL
{ C::execution_parent_calldata_addr, 10 },
{ C::execution_parent_calldata_size, 1 },
},
Expand Down Expand Up @@ -633,6 +638,137 @@ TEST(ContextConstrainingTest, BytecodeIdPropagation)
"BYTECODE_ID_NEXT_ROW"); // Should fail constraint
}

TEST(ContextConstrainingTest, IsStaticRegularCallFromNonStaticContext)
{
// Non-static context making a regular CALL - should remain non-static
TestTraceContainer trace({
{ { C::precomputed_first_row, 1 } },
{
{ C::execution_sel, 1 },
{ C::execution_context_id, 1 },
{ C::execution_next_context_id, 2 },
{ C::execution_is_static, 0 }, // Non-static context
{ C::execution_sel_enter_call, 1 },
{ C::execution_sel_execute_call, 1 }, // Regular CALL
{ C::execution_sel_execute_static_call, 0 },
},
{
{ C::execution_sel, 1 },
{ C::execution_context_id, 2 },
{ C::execution_next_context_id, 3 },
{ C::execution_is_static, 0 }, // Should remain non-static
},
});
check_relation<context>(
trace, context::SR_IS_STATIC_IF_STATIC_CALL, context::SR_IS_STATIC_IF_CALL_FROM_STATIC_CONTEXT);

// Negative test: change is_static
// regular call from non-static context cannot become static
trace.set(C::execution_is_static, 2, 1);
EXPECT_THROW_WITH_MESSAGE(check_relation<context>(trace, context::SR_IS_STATIC_IF_STATIC_CALL),
"IS_STATIC_IF_STATIC_CALL");

// reset is_static
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover? Not needed if this is the end of the test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I try to always "reset" mutated things like this in our cpp tests. Sometimes I do a few of them back-to-back. So this is just me trying to be consistent.

trace.set(C::execution_is_static, 2, 0);
}

TEST(ContextConstrainingTest, IsStaticStaticCallFromNonStaticContext)
{
// Non-static context making a STATICCALL - should become static
TestTraceContainer trace({
{ { C::precomputed_first_row, 1 } },
{
{ C::execution_sel, 1 },
{ C::execution_context_id, 1 },
{ C::execution_next_context_id, 2 },
{ C::execution_is_static, 0 }, // Non-static context
{ C::execution_sel_enter_call, 1 },
{ C::execution_sel_execute_call, 0 },
{ C::execution_sel_execute_static_call, 1 }, // STATICCALL
},
{
{ C::execution_sel, 1 },
{ C::execution_context_id, 2 },
{ C::execution_next_context_id, 3 },
{ C::execution_is_static, 1 }, // Should become static
},
});
check_relation<context>(
trace, context::SR_IS_STATIC_IF_STATIC_CALL, context::SR_IS_STATIC_IF_CALL_FROM_STATIC_CONTEXT);

// Negative test: change is_static
// static call from non-static context MUST become static
trace.set(C::execution_is_static, 2, 0);
EXPECT_THROW_WITH_MESSAGE(check_relation<context>(trace, context::SR_IS_STATIC_IF_STATIC_CALL),
"IS_STATIC_IF_STATIC_CALL");

// reset is_static
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

trace.set(C::execution_is_static, 2, 1);
}

TEST(ContextConstrainingTest, IsStaticCallFromStaticContext)
{
// Static context making any call - must remain static
TestTraceContainer trace({
{ { C::precomputed_first_row, 1 } },
{
{ C::execution_sel, 1 },
{ C::execution_context_id, 1 },
{ C::execution_next_context_id, 2 },
{ C::execution_is_static, 1 }, // Static context
{ C::execution_sel_enter_call, 1 },
{ C::execution_sel_execute_call, 1 }, // Regular CALL
{ C::execution_sel_execute_static_call, 0 },
},
{
{ C::execution_sel, 1 },
{ C::execution_context_id, 2 },
{ C::execution_next_context_id, 3 },
{ C::execution_is_static, 1 }, // Must remain static
},
});
check_relation<context>(
trace, context::SR_IS_STATIC_IF_STATIC_CALL, context::SR_IS_STATIC_IF_CALL_FROM_STATIC_CONTEXT);

// Negative test: change is_static
// static call from static context MUST remain static
trace.set(C::execution_is_static, 2, 0);
EXPECT_THROW_WITH_MESSAGE(check_relation<context>(trace, context::SR_IS_STATIC_IF_CALL_FROM_STATIC_CONTEXT),
"IS_STATIC_IF_CALL_FROM_STATIC_CONTEXT");

// reset is_static
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

trace.set(C::execution_is_static, 2, 1);
}

TEST(ContextConstrainingTest, IsStaticPropagationWithoutCalls)
{
// is_static propagation without calls
TestTraceContainer trace({
{ { C::precomputed_first_row, 1 } },
{
{ C::execution_sel, 1 },
{ C::execution_context_id, 1 },
{ C::execution_next_context_id, 1 },
{ C::execution_is_static, 1 }, // Static context
},
{
{ C::execution_sel, 1 },
{ C::execution_context_id, 1 },
{ C::execution_next_context_id, 1 },
{ C::execution_is_static, 1 }, // Should propagate
},
});
check_relation<context>(trace, context::SR_IS_STATIC_NEXT_ROW);

// Negative test: change is_static
// staticness must propagate without calls
trace.set(C::execution_is_static, 2, 0);
EXPECT_THROW_WITH_MESSAGE(check_relation<context>(trace, context::SR_IS_STATIC_NEXT_ROW), "IS_STATIC_NEXT_ROW");

// reset is_static
trace.set(C::execution_is_static, 2, 1);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

}

TEST(ContextConstrainingTest, ContextIdPropagation)
{
TestTraceContainer trace({
Expand Down
Loading
Loading