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