77#include " barretenberg/vm2/constraining/flavor_settings.hpp"
88#include " barretenberg/vm2/constraining/testing/check_relation.hpp"
99#include " barretenberg/vm2/generated/relations/execution.hpp"
10+ #include " barretenberg/vm2/simulation/gadgets/field_gt.hpp"
11+ #include " barretenberg/vm2/simulation/testing/mock_range_check.hpp"
1012#include " barretenberg/vm2/testing/macros.hpp"
1113#include " barretenberg/vm2/testing/public_inputs_builder.hpp"
1214#include " barretenberg/vm2/tracegen/execution_trace.hpp"
15+ #include " barretenberg/vm2/tracegen/field_gt_trace.hpp"
1316#include " barretenberg/vm2/tracegen/precomputed_trace.hpp"
1417#include " barretenberg/vm2/tracegen/public_inputs_trace.hpp"
1518#include " barretenberg/vm2/tracegen/test_trace_container.hpp"
1619
1720namespace bb ::avm2::constraining {
1821namespace {
1922
23+ using simulation::EventEmitter;
24+ using simulation::FieldGreaterThan;
25+ using simulation::FieldGreaterThanEvent;
26+ using simulation::MockRangeCheck;
27+
2028using tracegen::ExecutionTraceBuilder;
2129using tracegen::PublicInputsTraceBuilder;
2230using tracegen::TestTraceContainer;
2331
2432using testing::PublicInputsBuilder;
33+ using tracegen::FieldGreaterThanTraceBuilder;
2534
2635using FF = AvmFlavorSettings::FF;
2736using C = Column;
@@ -32,6 +41,7 @@ TEST(SendL2ToL1MsgConstrainingTest, Positive)
3241 uint64_t prev_num_l2_to_l1_msgs = MAX_L2_TO_L1_MSGS_PER_TX - 1 ;
3342 TestTraceContainer trace ({ {
3443 { C::execution_sel_execute_send_l2_to_l1_msg, 1 },
44+ { C::execution_max_eth_address_value, FF (MAX_ETH_ADDRESS_VALUE) },
3545 { C::execution_register_0_, /* recipient=*/ 42 },
3646 { C::execution_register_1_, /* content=*/ 27 },
3747 { C::execution_mem_tag_reg_0_, static_cast <uint8_t >(MemoryTag::FF) },
@@ -54,6 +64,7 @@ TEST(SendL2ToL1MsgConstrainingTest, LimitReached)
5464 uint64_t prev_num_l2_to_l1_msgs = MAX_L2_TO_L1_MSGS_PER_TX;
5565 TestTraceContainer trace ({ {
5666 { C::execution_sel_execute_send_l2_to_l1_msg, 1 },
67+ { C::execution_max_eth_address_value, FF (MAX_ETH_ADDRESS_VALUE) },
5768 { C::execution_register_0_, /* recipient=*/ 42 },
5869 { C::execution_register_1_, /* content=*/ 27 },
5970 { C::execution_mem_tag_reg_0_, static_cast <uint8_t >(MemoryTag::FF) },
@@ -88,6 +99,7 @@ TEST(SendL2ToL1MsgConstrainingTest, Discard)
8899 uint64_t prev_num_l2_to_l1_msgs = 0 ;
89100 TestTraceContainer trace ({ {
90101 { C::execution_sel_execute_send_l2_to_l1_msg, 1 },
102+ { C::execution_max_eth_address_value, FF (MAX_ETH_ADDRESS_VALUE) },
91103 { C::execution_register_0_, /* recipient=*/ 42 },
92104 { C::execution_register_1_, /* content=*/ 27 },
93105 { C::execution_mem_tag_reg_0_, static_cast <uint8_t >(MemoryTag::FF) },
@@ -119,6 +131,12 @@ TEST(SendL2ToL1MsgConstrainingTest, Interactions)
119131 AztecAddress address = 0xdeadbeef ;
120132 AvmAccumulatedData accumulated_data = {};
121133
134+ MockRangeCheck mock_range_check;
135+ EventEmitter<FieldGreaterThanEvent> ff_gt_event_emitter;
136+ FieldGreaterThan field_gt (mock_range_check, ff_gt_event_emitter);
137+
138+ ASSERT_FALSE (field_gt.ff_gt (recipient, FF (MAX_ETH_ADDRESS_VALUE)));
139+
122140 accumulated_data.l2_to_l1_msgs [0 ] = {
123141 .message =
124142 L2ToL1Message{
@@ -135,6 +153,7 @@ TEST(SendL2ToL1MsgConstrainingTest, Interactions)
135153
136154 TestTraceContainer trace ({ {
137155 { C::execution_sel_execute_send_l2_to_l1_msg, 1 },
156+ { C::execution_max_eth_address_value, FF (MAX_ETH_ADDRESS_VALUE) },
138157 { C::execution_register_0_, recipient },
139158 { C::execution_register_1_, content },
140159 { C::execution_mem_tag_reg_0_, static_cast <uint8_t >(MemoryTag::FF) },
@@ -150,6 +169,9 @@ TEST(SendL2ToL1MsgConstrainingTest, Interactions)
150169 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_SENDL2TOL1MSG },
151170 } });
152171
172+ FieldGreaterThanTraceBuilder field_gt_builder;
173+ field_gt_builder.process (ff_gt_event_emitter.dump_events (), trace);
174+
153175 PublicInputsTraceBuilder public_inputs_builder;
154176 public_inputs_builder.process_public_inputs (trace, public_inputs);
155177 public_inputs_builder.process_public_inputs_aux_precomputed (trace);
@@ -158,13 +180,34 @@ TEST(SendL2ToL1MsgConstrainingTest, Interactions)
158180 precomputed_builder.process_misc (trace, trace.get_num_rows ());
159181
160182 check_relation<send_l2_to_l1_msg>(trace);
161- check_interaction<ExecutionTraceBuilder, lookup_send_l2_to_l1_msg_write_l2_to_l1_msg_settings>(trace);
183+ check_interaction<ExecutionTraceBuilder,
184+ lookup_send_l2_to_l1_msg_write_l2_to_l1_msg_settings,
185+ lookup_send_l2_to_l1_msg_recipient_check_settings>(trace);
186+ }
187+
188+ TEST (SendL2ToL1MsgConstrainingTest, NegativeShouldErrorIfRecipientTooLarge)
189+ {
190+ TestTraceContainer trace ({ {
191+ { C::execution_sel_execute_send_l2_to_l1_msg, 1 },
192+ { C::execution_max_eth_address_value, FF (MAX_ETH_ADDRESS_VALUE) },
193+ { C::execution_too_large_recipient_error, 1 },
194+ { C::execution_sel_l2_to_l1_msg_limit_error, 0 },
195+ { C::execution_is_static, 0 },
196+ { C::execution_sel_opcode_error, 1 },
197+ } });
198+ check_relation<send_l2_to_l1_msg>(trace, send_l2_to_l1_msg::SR_OPCODE_ERROR);
199+
200+ // Negative test: sel_opcode_error must be on
201+ trace.set (C::execution_sel_opcode_error, 0 , 0 );
202+ EXPECT_THROW_WITH_MESSAGE (check_relation<send_l2_to_l1_msg>(trace, send_l2_to_l1_msg::SR_OPCODE_ERROR),
203+ " OPCODE_ERROR" );
162204}
163205
164206TEST (SendL2ToL1MsgConstrainingTest, NegativeShouldErrorIfStatic)
165207{
166208 TestTraceContainer trace ({ {
167209 { C::execution_sel_execute_send_l2_to_l1_msg, 1 },
210+ { C::execution_max_eth_address_value, FF (MAX_ETH_ADDRESS_VALUE) },
168211 { C::execution_sel_l2_to_l1_msg_limit_error, 0 },
169212 { C::execution_is_static, 1 },
170213 { C::execution_sel_opcode_error, 1 },
@@ -182,6 +225,7 @@ TEST(SendL2ToL1MsgConstrainingTest, NegativeShouldNotWriteIfDiscard)
182225{
183226 TestTraceContainer trace ({ {
184227 { C::execution_sel_execute_send_l2_to_l1_msg, 1 },
228+ { C::execution_max_eth_address_value, FF (MAX_ETH_ADDRESS_VALUE) },
185229 { C::execution_sel_opcode_error, 0 },
186230 { C::execution_discard, 1 },
187231 { C::execution_sel_write_l2_to_l1_msg, 0 },
@@ -199,6 +243,7 @@ TEST(SendL2ToL1MsgConstrainingTest, NegativeShouldNumL2ToL1MessagesIncrease)
199243{
200244 TestTraceContainer trace ({ {
201245 { C::execution_sel_execute_send_l2_to_l1_msg, 1 },
246+ { C::execution_max_eth_address_value, FF (MAX_ETH_ADDRESS_VALUE) },
202247 { C::execution_sel_opcode_error, 0 },
203248 { C::execution_prev_num_l2_to_l1_messages, 0 },
204249 { C::execution_num_l2_to_l1_messages, 1 },
0 commit comments