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>;
46
47TEST(L1ToL2MessageExistsConstrainingTest, PositiveExists)
48{
49 TestTraceContainer trace({
50 { { C::execution_sel_execute_l1_to_l2_message_exists, 1 },
51 { C::execution_register_0_, /*msg_hash=*/42 },
52 { C::execution_register_1_, /*leaf_index=*/27 },
53 { C::execution_register_2_, /*dst=*/1 },
54 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
55 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::U64) },
56 { C::execution_mem_tag_reg_2_, static_cast<uint8_t>(MemoryTag::U1) },
57 { C::execution_l1_to_l2_msg_leaf_in_range, 1 },
58 { C::execution_l1_to_l2_msg_tree_leaf_count, static_cast<uint64_t>(L1_TO_L2_MSG_TREE_LEAF_COUNT) },
59 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_L1_TO_L2_MESSAGE_EXISTS } },
60 });
61 check_relation<l1_to_l2_message_exists>(trace);
62}
63
64TEST(L1ToL2MessageExistsConstrainingTest, OutOfRange)
65{
66 uint64_t leaf_index = L1_TO_L2_MSG_TREE_LEAF_COUNT + 1;
67 TestTraceContainer trace({
68 { { C::execution_sel_execute_l1_to_l2_message_exists, 1 },
69 { C::execution_register_0_, /*msg_hash=*/42 },
70 { C::execution_register_1_, /*leaf_index=*/leaf_index },
71 { C::execution_register_2_, /*dst=*/0 },
72 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
73 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::U64) },
74 { C::execution_mem_tag_reg_2_, static_cast<uint8_t>(MemoryTag::U1) },
75 { C::execution_l1_to_l2_msg_leaf_in_range, 0 },
76 { C::execution_l1_to_l2_msg_tree_leaf_count, static_cast<uint64_t>(L1_TO_L2_MSG_TREE_LEAF_COUNT) },
77 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_L1_TO_L2_MESSAGE_EXISTS } },
78 });
79
80 check_relation<l1_to_l2_message_exists>(trace);
81
82 // Negative test: exists must be false
83 trace.set(C::execution_register_2_, 0, 1);
84 EXPECT_THROW_WITH_MESSAGE(check_relation<l1_to_l2_message_exists>(trace),
87}
88
89TEST(L1ToL2MessageExistsConstrainingTest, NegativeInvalidOutputTag)
90{
91 TestTraceContainer trace({ {
92 { C::execution_sel_execute_l1_to_l2_message_exists, 1 },
93 { C::execution_register_0_, /*msg_hash=*/42 },
94 { C::execution_register_1_, /*leaf_index=*/27 },
95 { C::execution_register_2_, /*dst=*/1 },
96 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
97 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::U64) },
98 { C::execution_mem_tag_reg_2_, static_cast<uint8_t>(MemoryTag::U8) },
99 } });
101 check_relation<l1_to_l2_message_exists>(trace, l1_to_l2_message_exists::SR_L1_TO_L2_MSG_EXISTS_U1_OUTPUT_TAG),
103}
104
105TEST(L1ToL2MessageExistsConstrainingTest, NegativeL1ToL2MessageExistsSuccess)
106{
107 TestTraceContainer trace({ {
108 { C::execution_sel_execute_l1_to_l2_message_exists, 1 },
109 { C::execution_sel_opcode_error, 1 },
110 } });
111
114}
115
116TEST(L1ToL2MessageExistsConstrainingTest, Interactions)
117{
118 NiceMock<MockMerkleCheck> merkle_check;
119 NiceMock<MockFieldGreaterThan> field_gt;
120 NiceMock<MockRangeCheck> range_check;
121
122 EventEmitter<GreaterThanEvent> greater_than_event_emitter;
123 GreaterThan greater_than(field_gt, range_check, greater_than_event_emitter);
124 EventEmitter<L1ToL2MessageTreeCheckEvent> l1_to_l2_message_tree_check_event_emitter;
125 L1ToL2MessageTreeCheck l1_to_l2_message_tree_check(merkle_check, l1_to_l2_message_tree_check_event_emitter);
126
127 FF requested_msg_hash = 42;
128 FF actual_leaf_value = 43;
129
130 uint64_t leaf_index = 27;
131
132 AppendOnlyTreeSnapshot l1_to_l2_message_tree_snapshot = AppendOnlyTreeSnapshot{
133 .root = 42,
134 .next_available_leaf_index = 128,
135 };
136
137 greater_than.gt(L1_TO_L2_MSG_TREE_LEAF_COUNT, leaf_index);
138 l1_to_l2_message_tree_check.exists(
139 requested_msg_hash, actual_leaf_value, leaf_index, {}, l1_to_l2_message_tree_snapshot);
140
141 TestTraceContainer trace({ {
142 { C::execution_sel_execute_l1_to_l2_message_exists, 1 },
143 { C::execution_register_0_, requested_msg_hash },
144 { C::execution_register_1_, leaf_index },
145 { C::execution_register_2_, /*result=*/0 },
146 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
147 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::U64) },
148 { C::execution_mem_tag_reg_2_, static_cast<uint8_t>(MemoryTag::U1) },
149 { C::execution_l1_to_l2_msg_leaf_in_range, 1 },
150 { C::execution_sel_opcode_error, 0 },
151 { C::execution_l1_to_l2_msg_tree_leaf_count, static_cast<uint64_t>(L1_TO_L2_MSG_TREE_LEAF_COUNT) },
152 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_L1_TO_L2_MESSAGE_EXISTS },
153 { C::execution_l1_l2_tree_root, l1_to_l2_message_tree_snapshot.root },
154 } });
155
156 L1ToL2MessageTreeCheckTraceBuilder l1_to_l2_message_tree_check_trace_builder;
157 l1_to_l2_message_tree_check_trace_builder.process(l1_to_l2_message_tree_check_event_emitter.dump_events(), trace);
158
159 GreaterThanTraceBuilder greater_than_trace_builder;
160 greater_than_trace_builder.process(greater_than_event_emitter.dump_events(), trace);
161
162 check_relation<l1_to_l2_message_exists>(trace);
163
164 check_interaction<ExecutionTraceBuilder,
167}
168
169} // namespace
170} // namespace bb::avm2::constraining
GreaterThan greater_than
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessageRegex)
Definition assert.hpp:224
FieldGreaterThan field_gt
MerkleCheck merkle_check
RangeCheck range_check
static std::string get_subrelation_label(size_t index)
Definition execution.hpp:80
static constexpr size_t SR_INFALLIBLE_OPCODES_SUCCESS
Definition execution.hpp:77
static constexpr size_t SR_L1_TO_L2_MSG_EXISTS_OUT_OF_RANGE_FALSE
static constexpr size_t SR_L1_TO_L2_MSG_EXISTS_U1_OUTPUT_TAG
static std::string get_subrelation_label(size_t index)
bool gt(const FF &a, const FF &b) override
Computes whether a > b for field elements, delegating to the ff_gt gadget.
Definition gt.cpp:15
void set(Column col, uint32_t row, const FF &value, bool use_atomic_limbs=false)
TestTraceContainer trace
void check_interaction(tracegen::TestTraceContainer &trace)
TEST(AvmFixedVKTests, FixedVKCommitments)
Test that the fixed VK commitments agree with the ones computed from precomputed columns.
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
NoopEventEmitter< GreaterThanEvent > greater_than_event_emitter
NiceMock< MockExecution > execution