Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
send_l2_to_l1_msg.test.cpp
Go to the documentation of this file.
1#include <gmock/gmock.h>
2#include <gtest/gtest.h>
3
4#include <cstdint>
5
16
17namespace bb::avm2::constraining {
18namespace {
19
20using tracegen::ExecutionTraceBuilder;
21using tracegen::PublicInputsTraceBuilder;
22using tracegen::TestTraceContainer;
23
24using testing::PublicInputsBuilder;
25
27using C = Column;
28using send_l2_to_l1_msg = bb::avm2::send_l2_to_l1_msg<FF>;
29
30TEST(SendL2ToL1MsgConstrainingTest, Positive)
31{
32 uint64_t prev_num_l2_to_l1_msgs = MAX_L2_TO_L1_MSGS_PER_TX - 1;
33 TestTraceContainer trace({ {
34 { C::execution_sel_execute_send_l2_to_l1_msg, 1 },
35 { C::execution_register_0_, /*recipient=*/42 },
36 { C::execution_register_1_, /*content=*/27 },
37 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
38 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::FF) },
39 { C::execution_remaining_l2_to_l1_msgs_inv, FF(MAX_L2_TO_L1_MSGS_PER_TX - prev_num_l2_to_l1_msgs).invert() },
40 { C::execution_sel_write_l2_to_l1_msg, 1 },
41 { C::execution_sel_opcode_error, 0 },
42 { C::execution_public_inputs_index,
44 { C::execution_prev_num_l2_to_l1_messages, prev_num_l2_to_l1_msgs },
45 { C::execution_num_l2_to_l1_messages, prev_num_l2_to_l1_msgs + 1 },
46 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_SENDL2TOL1MSG },
47
48 } });
49 check_relation<send_l2_to_l1_msg>(trace);
50}
51
52TEST(SendL2ToL1MsgConstrainingTest, LimitReached)
53{
54 uint64_t prev_num_l2_to_l1_msgs = MAX_L2_TO_L1_MSGS_PER_TX;
55 TestTraceContainer trace({ {
56 { C::execution_sel_execute_send_l2_to_l1_msg, 1 },
57 { C::execution_register_0_, /*recipient=*/42 },
58 { C::execution_register_1_, /*content=*/27 },
59 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
60 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::FF) },
61 { C::execution_sel_l2_to_l1_msg_limit_error, 1 },
62 { C::execution_remaining_l2_to_l1_msgs_inv, 0 },
63 { C::execution_sel_write_l2_to_l1_msg, 0 },
64 { C::execution_sel_opcode_error, 1 },
65 { C::execution_public_inputs_index,
67 { C::execution_prev_num_l2_to_l1_messages, prev_num_l2_to_l1_msgs },
68 { C::execution_num_l2_to_l1_messages, prev_num_l2_to_l1_msgs },
69 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_SENDL2TOL1MSG },
70 } });
71 check_relation<send_l2_to_l1_msg>(trace);
72
73 // Negative test: sel_opcode_error must be on
74 trace.set(C::execution_sel_opcode_error, 0, 0);
75 EXPECT_THROW_WITH_MESSAGE(check_relation<send_l2_to_l1_msg>(trace, send_l2_to_l1_msg::SR_OPCODE_ERROR),
76 "OPCODE_ERROR");
77 trace.set(C::execution_sel_opcode_error, 0, 1);
78
79 // Negative test: num l2 to l1 messages must be the same
80 trace.set(C::execution_num_l2_to_l1_messages, 0, prev_num_l2_to_l1_msgs + 1);
81 EXPECT_THROW_WITH_MESSAGE(check_relation<send_l2_to_l1_msg>(
83 "EMIT_L2_TO_L1_MSG_NUM_L2_TO_L1_MSGS_EMITTED_INCREASE");
84}
85
86TEST(SendL2ToL1MsgConstrainingTest, Discard)
87{
88 uint64_t prev_num_l2_to_l1_msgs = 0;
89 TestTraceContainer trace({ {
90 { C::execution_sel_execute_send_l2_to_l1_msg, 1 },
91 { C::execution_register_0_, /*recipient=*/42 },
92 { C::execution_register_1_, /*content=*/27 },
93 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
94 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::FF) },
95 { C::execution_remaining_l2_to_l1_msgs_inv, FF(MAX_L2_TO_L1_MSGS_PER_TX - prev_num_l2_to_l1_msgs).invert() },
96 { C::execution_sel_write_l2_to_l1_msg, 0 },
97 { C::execution_sel_opcode_error, 0 },
98 { C::execution_public_inputs_index,
100 { C::execution_discard, 1 },
101 { C::execution_prev_num_l2_to_l1_messages, prev_num_l2_to_l1_msgs },
102 { C::execution_num_l2_to_l1_messages, prev_num_l2_to_l1_msgs + 1 },
103 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_SENDL2TOL1MSG },
104 } });
105 check_relation<send_l2_to_l1_msg>(trace);
106
107 // Negative test: num l2 to l1 messages should increase when discarding
108 trace.set(C::execution_num_l2_to_l1_messages, 0, prev_num_l2_to_l1_msgs);
109 EXPECT_THROW_WITH_MESSAGE(check_relation<send_l2_to_l1_msg>(
111 "EMIT_L2_TO_L1_MSG_NUM_L2_TO_L1_MSGS_EMITTED_INCREASE");
112}
113
114TEST(SendL2ToL1MsgConstrainingTest, Interactions)
115{
116 uint64_t prev_num_l2_to_l1_msgs = 0;
117 FF recipient = 42;
118 FF content = 27;
119 AztecAddress address = 0xdeadbeef;
120 AvmAccumulatedData accumulated_data = {};
121
122 accumulated_data.l2ToL1Msgs[0] = {
123 .message =
124 L2ToL1Message{
125 .recipient = recipient,
126 .content = content,
127 },
128 .contractAddress = address,
129 };
130 AvmAccumulatedDataArrayLengths array_lengths = { .l2ToL1Msgs = 1 };
131 auto public_inputs = PublicInputsBuilder()
132 .set_accumulated_data(accumulated_data)
133 .set_accumulated_data_array_lengths(array_lengths)
134 .build();
135
136 TestTraceContainer trace({ {
137 { C::execution_sel_execute_send_l2_to_l1_msg, 1 },
138 { C::execution_register_0_, recipient },
139 { C::execution_register_1_, content },
140 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
141 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::FF) },
142 { C::execution_remaining_l2_to_l1_msgs_inv, FF(MAX_L2_TO_L1_MSGS_PER_TX - prev_num_l2_to_l1_msgs).invert() },
143 { C::execution_sel_write_l2_to_l1_msg, 1 },
144 { C::execution_sel_opcode_error, 0 },
145 { C::execution_public_inputs_index,
147 { C::execution_contract_address, address },
148 { C::execution_prev_num_l2_to_l1_messages, prev_num_l2_to_l1_msgs },
149 { C::execution_num_l2_to_l1_messages, prev_num_l2_to_l1_msgs + 1 },
150 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_SENDL2TOL1MSG },
151 } });
152
153 PublicInputsTraceBuilder public_inputs_builder;
154 public_inputs_builder.process_public_inputs(trace, public_inputs);
155 public_inputs_builder.process_public_inputs_aux_precomputed(trace);
156
157 tracegen::PrecomputedTraceBuilder precomputed_builder;
159
160 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);
162}
163
164TEST(SendL2ToL1MsgConstrainingTest, NegativeShouldErrorIfStatic)
165{
166 TestTraceContainer trace({ {
167 { C::execution_sel_execute_send_l2_to_l1_msg, 1 },
168 { C::execution_sel_l2_to_l1_msg_limit_error, 0 },
169 { C::execution_is_static, 1 },
170 { C::execution_sel_opcode_error, 1 },
171
172 } });
173 check_relation<send_l2_to_l1_msg>(trace, send_l2_to_l1_msg::SR_OPCODE_ERROR);
174
175 // Negative test: sel_opcode_error must be on
176 trace.set(C::execution_sel_opcode_error, 0, 0);
177 EXPECT_THROW_WITH_MESSAGE(check_relation<send_l2_to_l1_msg>(trace, send_l2_to_l1_msg::SR_OPCODE_ERROR),
178 "OPCODE_ERROR");
179}
180
181TEST(SendL2ToL1MsgConstrainingTest, NegativeShouldNotWriteIfDiscard)
182{
183 TestTraceContainer trace({ {
184 { C::execution_sel_execute_send_l2_to_l1_msg, 1 },
185 { C::execution_sel_opcode_error, 0 },
186 { C::execution_discard, 1 },
187 { C::execution_sel_write_l2_to_l1_msg, 0 },
188 } });
189 check_relation<send_l2_to_l1_msg>(trace, send_l2_to_l1_msg::SR_SEND_L2_TO_L1_MSG_CONDITION);
190
191 // Negative test: sel_write_l2_to_l1_msg must be off
192 trace.set(C::execution_sel_write_l2_to_l1_msg, 0, 1);
194 check_relation<send_l2_to_l1_msg>(trace, send_l2_to_l1_msg::SR_SEND_L2_TO_L1_MSG_CONDITION),
195 "SEND_L2_TO_L1_MSG_CONDITION");
196}
197
198TEST(SendL2ToL1MsgConstrainingTest, NegativeShouldNumL2ToL1MessagesIncrease)
199{
200 TestTraceContainer trace({ {
201 { C::execution_sel_execute_send_l2_to_l1_msg, 1 },
202 { C::execution_sel_opcode_error, 0 },
203 { C::execution_prev_num_l2_to_l1_messages, 0 },
204 { C::execution_num_l2_to_l1_messages, 1 },
205 } });
206 check_relation<send_l2_to_l1_msg>(trace,
208
209 // Negative test: num_l2_to_l1_messages must increase
210 trace.set(C::execution_prev_num_l2_to_l1_messages, 0, 1);
211 EXPECT_THROW_WITH_MESSAGE(check_relation<send_l2_to_l1_msg>(
213 "EMIT_L2_TO_L1_MSG_NUM_L2_TO_L1_MSGS_EMITTED_INCREASE");
214}
215
216} // namespace
217} // namespace bb::avm2::constraining
#define AVM_PUBLIC_INPUTS_AVM_ACCUMULATED_DATA_L2_TO_L1_MSGS_ROW_IDX
#define MAX_L2_TO_L1_MSGS_PER_TX
#define AVM_EXEC_OP_ID_SENDL2TOL1MSG
static constexpr size_t SR_OPCODE_ERROR
static constexpr size_t SR_EMIT_L2_TO_L1_MSG_NUM_L2_TO_L1_MSGS_EMITTED_INCREASE
static constexpr size_t SR_SEND_L2_TO_L1_MSG_CONDITION
void set(Column col, uint32_t row, const FF &value)
PrecomputedTraceBuilder precomputed_builder
Definition alu.test.cpp:119
TestTraceContainer trace
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessage)
Definition macros.hpp:7
AvmFlavorSettings::FF FF
TEST(TxExecutionConstrainingTest, WriteTreeValue)
Definition tx.test.cpp:508
typename Flavor::FF FF