Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
l1_to_l2_message_exists.test.cpp
Go to the documentation of this file.
1#include <gmock/gmock.h>
2#include <gtest/gtest.h>
3
4#include <cstdint>
5
22
23namespace bb::avm2::constraining {
24namespace {
25
26using tracegen::ExecutionTraceBuilder;
27using tracegen::GreaterThanTraceBuilder;
28using tracegen::L1ToL2MessageTreeCheckTraceBuilder;
29using tracegen::TestTraceContainer;
30
31using simulation::EventEmitter;
32using simulation::GreaterThan;
33using simulation::GreaterThanEvent;
34using simulation::L1ToL2MessageTreeCheck;
35using simulation::L1ToL2MessageTreeCheckEvent;
36using simulation::MockFieldGreaterThan;
37using simulation::MockMerkleCheck;
38using simulation::MockRangeCheck;
39
40using testing::NiceMock;
41
43using C = Column;
44using l1_to_l2_message_exists = bb::avm2::l1_to_l2_message_exists<FF>;
45
46TEST(L1ToL2MessageExistsConstrainingTest, PositiveExists)
47{
48 TestTraceContainer trace({
49 { { C::execution_sel_execute_l1_to_l2_message_exists, 1 },
50 { C::execution_register_0_, /*msg_hash=*/42 },
51 { C::execution_register_1_, /*leaf_index=*/27 },
52 { C::execution_register_2_, /*dst=*/1 },
53 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
54 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::U64) },
55 { C::execution_mem_tag_reg_2_, static_cast<uint8_t>(MemoryTag::U1) },
56 { C::execution_l1_to_l2_msg_leaf_in_range, 1 },
57 { C::execution_l1_to_l2_msg_tree_leaf_count, static_cast<uint64_t>(L1_TO_L2_MSG_TREE_LEAF_COUNT) },
58 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_L1_TO_L2_MESSAGE_EXISTS } },
59 });
60 check_relation<l1_to_l2_message_exists>(trace);
61}
62
63TEST(L1ToL2MessageExistsConstrainingTest, OutOfRange)
64{
65 uint64_t leaf_index = L1_TO_L2_MSG_TREE_LEAF_COUNT + 1;
66 TestTraceContainer trace({
67 { { C::execution_sel_execute_l1_to_l2_message_exists, 1 },
68 { C::execution_register_0_, /*msg_hash=*/42 },
69 { C::execution_register_1_, /*leaf_index=*/leaf_index },
70 { C::execution_register_2_, /*dst=*/0 },
71 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
72 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::U64) },
73 { C::execution_mem_tag_reg_2_, static_cast<uint8_t>(MemoryTag::U1) },
74 { C::execution_l1_to_l2_msg_leaf_in_range, 0 },
75 { C::execution_l1_to_l2_msg_tree_leaf_count, static_cast<uint64_t>(L1_TO_L2_MSG_TREE_LEAF_COUNT) },
76 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_L1_TO_L2_MESSAGE_EXISTS } },
77 });
78
79 check_relation<l1_to_l2_message_exists>(trace);
80
81 // Negative test: exists must be false
82 trace.set(C::execution_register_2_, 0, 1);
83 EXPECT_THROW_WITH_MESSAGE(check_relation<l1_to_l2_message_exists>(trace), "L1_TO_L2_MSG_EXISTS_OUT_OF_RANGE_FALSE");
84}
85
86TEST(L1ToL2MessageExistsConstrainingTest, NegativeInvalidOutputTag)
87{
88 TestTraceContainer trace({ {
89 { C::execution_sel_execute_l1_to_l2_message_exists, 1 },
90 { C::execution_register_0_, /*msg_hash=*/42 },
91 { C::execution_register_1_, /*leaf_index=*/27 },
92 { C::execution_register_2_, /*dst=*/1 },
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::U64) },
95 { C::execution_mem_tag_reg_2_, static_cast<uint8_t>(MemoryTag::U8) },
96 } });
98 check_relation<l1_to_l2_message_exists>(trace, l1_to_l2_message_exists::SR_L1_TO_L2_MSG_EXISTS_U1_OUTPUT_TAG),
99 "L1_TO_L2_MSG_EXISTS_U1_OUTPUT_TAG");
100}
101
102TEST(L1ToL2MessageExistsConstrainingTest, NegativeL1ToL2MessageExistsSuccess)
103{
104 TestTraceContainer trace({ {
105 { C::execution_sel_execute_l1_to_l2_message_exists, 1 },
106 { C::execution_sel_opcode_error, 1 },
107 } });
108
110 check_relation<l1_to_l2_message_exists>(trace, l1_to_l2_message_exists::SR_L1_TO_L2_MSG_EXISTS_SUCCESS),
111 "L1_TO_L2_MSG_EXISTS_SUCCESS");
112}
113
114TEST(L1ToL2MessageExistsConstrainingTest, Interactions)
115{
116 NiceMock<MockMerkleCheck> merkle_check;
117 NiceMock<MockFieldGreaterThan> field_gt;
118 NiceMock<MockRangeCheck> range_check;
119
120 EventEmitter<GreaterThanEvent> greater_than_event_emitter;
121 GreaterThan greater_than(field_gt, range_check, greater_than_event_emitter);
122 EventEmitter<L1ToL2MessageTreeCheckEvent> l1_to_l2_message_tree_check_event_emitter;
123 L1ToL2MessageTreeCheck l1_to_l2_message_tree_check(merkle_check, l1_to_l2_message_tree_check_event_emitter);
124
125 FF requested_msg_hash = 42;
126 FF actual_leaf_value = 43;
127
128 uint64_t leaf_index = 27;
129
130 AppendOnlyTreeSnapshot l1_to_l2_message_tree_snapshot = AppendOnlyTreeSnapshot{
131 .root = 42,
132 .nextAvailableLeafIndex = 128,
133 };
134
136 l1_to_l2_message_tree_check.exists(
137 requested_msg_hash, actual_leaf_value, leaf_index, {}, l1_to_l2_message_tree_snapshot);
138
139 TestTraceContainer trace({ {
140 { C::execution_sel_execute_l1_to_l2_message_exists, 1 },
141 { C::execution_register_0_, requested_msg_hash },
142 { C::execution_register_1_, leaf_index },
143 { C::execution_register_2_, /*result=*/0 },
144 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
145 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::U64) },
146 { C::execution_mem_tag_reg_2_, static_cast<uint8_t>(MemoryTag::U1) },
147 { C::execution_l1_to_l2_msg_leaf_in_range, 1 },
148 { C::execution_sel_opcode_error, 0 },
149 { C::execution_l1_to_l2_msg_tree_leaf_count, static_cast<uint64_t>(L1_TO_L2_MSG_TREE_LEAF_COUNT) },
150 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_L1_TO_L2_MESSAGE_EXISTS },
151 { C::execution_l1_l2_tree_root, l1_to_l2_message_tree_snapshot.root },
152 } });
153
154 L1ToL2MessageTreeCheckTraceBuilder l1_to_l2_message_tree_check_trace_builder;
155 l1_to_l2_message_tree_check_trace_builder.process(l1_to_l2_message_tree_check_event_emitter.dump_events(), trace);
156
157 GreaterThanTraceBuilder greater_than_trace_builder;
158 greater_than_trace_builder.process(greater_than_event_emitter.dump_events(), trace);
159
160 check_relation<l1_to_l2_message_exists>(trace);
161
162 check_interaction<ExecutionTraceBuilder,
165}
166
167} // namespace
168} // namespace bb::avm2::constraining
#define L1_TO_L2_MSG_TREE_LEAF_COUNT
#define AVM_EXEC_OP_ID_L1_TO_L2_MESSAGE_EXISTS
static constexpr size_t SR_L1_TO_L2_MSG_EXISTS_U1_OUTPUT_TAG
static constexpr size_t SR_L1_TO_L2_MSG_EXISTS_SUCCESS
void set(Column col, uint32_t row, const FF &value)
RangeCheck range_check
TestTraceContainer trace
testing::StrictMock< MockGreaterThan > greater_than
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessage)
Definition macros.hpp:7
void check_interaction(tracegen::TestTraceContainer &trace)
TEST(TxExecutionConstrainingTest, WriteTreeValue)
Definition tx.test.cpp:402
lookup_settings< lookup_l1_to_l2_message_exists_l1_to_l2_msg_leaf_index_in_range_settings_ > lookup_l1_to_l2_message_exists_l1_to_l2_msg_leaf_index_in_range_settings
lookup_settings< lookup_l1_to_l2_message_exists_l1_to_l2_msg_read_settings_ > lookup_l1_to_l2_message_exists_l1_to_l2_msg_read_settings
typename Flavor::FF FF
FieldGreaterThan field_gt
NoopEventEmitter< GreaterThanEvent > greater_than_event_emitter