Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
storage_load.test.cpp
Go to the documentation of this file.
1#include <gmock/gmock.h>
2#include <gtest/gtest.h>
3
4#include <cstdint>
5
27
28namespace bb::avm2::constraining {
29namespace {
30
31using tracegen::ExecutionTraceBuilder;
32using tracegen::PublicDataTreeTraceBuilder;
33using tracegen::TestTraceContainer;
34
35using simulation::EventEmitter;
36using simulation::MerkleDB;
37using simulation::MockExecutionIdManager;
38using simulation::MockFieldGreaterThan;
39using simulation::MockL1ToL2MessageTreeCheck;
40using simulation::MockLowLevelMerkleDB;
41using simulation::MockMerkleCheck;
42using simulation::MockNoteHashTreeCheck;
43using simulation::MockNullifierTreeCheck;
44using simulation::MockPoseidon2;
45using simulation::MockWrittenPublicDataSlotsTreeCheck;
46using simulation::PublicDataTreeCheck;
48
49using testing::NiceMock;
50using testing::ReturnRef;
51
53using C = Column;
54using sload = bb::avm2::sload<FF>;
55
56TEST(SLoadConstrainingTest, PositiveTest)
57{
58 TestTraceContainer trace({
59 { { C::execution_sel_execute_sload, 1 },
60 { C::execution_register_0_, /*slot=*/42 },
61 { C::execution_register_1_, /*dst=*/27 },
62 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
63 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::FF) },
64 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_SLOAD } },
65 });
66 check_relation<sload>(trace);
67}
68
69TEST(SLoadConstrainingTest, NegativeInvalidOutputTag)
70{
71 TestTraceContainer trace({
72 { { C::execution_sel_execute_sload, 1 },
73 { C::execution_register_0_, /*slot=*/42 },
74 { C::execution_register_1_, /*dst=*/27 },
75 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
76 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::U32) },
77 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_SLOAD } },
78 });
79 EXPECT_THROW_WITH_MESSAGE(check_relation<sload>(trace), "SLOAD_FF_OUTPUT_TAG");
80}
81
82TEST(SLoadConstrainingTest, NegativeSloadSuccess)
83{
84 TestTraceContainer trace({
85 { { C::execution_sel_execute_sload, 1 },
86 { C::execution_register_0_, /*slot=*/42 },
87 { C::execution_register_1_, /*dst=*/27 },
88 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
89 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::FF) },
90 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_SLOAD },
91 { C::execution_sel_opcode_error, 1 } },
92 });
93 EXPECT_THROW_WITH_MESSAGE(check_relation<sload>(trace), "SLOAD_SUCCESS");
94}
95
96TEST(SLoadConstrainingTest, Interactions)
97{
98 NiceMock<MockPoseidon2> poseidon2;
99 NiceMock<MockFieldGreaterThan> field_gt;
100 NiceMock<MockMerkleCheck> merkle_check;
101 NiceMock<MockExecutionIdManager> execution_id_manager;
102 NiceMock<MockWrittenPublicDataSlotsTreeCheck> written_public_data_slots_tree_check;
103 NiceMock<MockLowLevelMerkleDB> low_level_merkle_db;
104 NiceMock<MockNullifierTreeCheck> nullifier_tree_check;
105 NiceMock<MockNoteHashTreeCheck> note_hash_tree_check;
106 NiceMock<MockL1ToL2MessageTreeCheck> l1_to_l2_message_tree_check;
107
108 EventEmitter<PublicDataTreeCheckEvent> public_data_tree_check_event_emitter;
109 PublicDataTreeCheck public_data_tree_check(
110 poseidon2, merkle_check, field_gt, execution_id_manager, public_data_tree_check_event_emitter);
111
112 FF slot = 42;
114
115 MerkleDB merkle_db(low_level_merkle_db,
116 public_data_tree_check,
117 nullifier_tree_check,
118 note_hash_tree_check,
119 written_public_data_slots_tree_check,
120 l1_to_l2_message_tree_check);
121
122 TreeSnapshots trees;
123 trees.publicDataTree.root = 42;
124 EXPECT_CALL(low_level_merkle_db, get_tree_roots()).WillRepeatedly(ReturnRef(trees));
125
126 FF value = merkle_db.storage_read(contract_address, slot);
127
128 TestTraceContainer trace({
129 { { C::execution_sel_execute_sload, 1 },
130 { C::execution_register_0_, slot },
131 { C::execution_register_1_, value },
132 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
133 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::FF) },
134 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_SLOAD },
135 { C::execution_contract_address, contract_address },
136 { C::execution_prev_public_data_tree_root, trees.publicDataTree.root } },
137 });
138
139 PublicDataTreeTraceBuilder public_data_tree_trace_builder;
140 public_data_tree_trace_builder.process(public_data_tree_check_event_emitter.dump_events(), trace);
141
142 check_relation<sload>(trace);
143 check_interaction<ExecutionTraceBuilder, lookup_sload_storage_read_settings>(trace);
144}
145
146} // namespace
147} // namespace bb::avm2::constraining
#define AVM_EXEC_OP_ID_SLOAD
StrictMock< MockHighLevelMerkleDB > merkle_db
ExecutionIdManager execution_id_manager
TestTraceContainer trace
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessage)
Definition macros.hpp:7
TEST(TxExecutionConstrainingTest, WriteTreeValue)
Definition tx.test.cpp:508
std::variant< PublicDataTreeReadWriteEvent, CheckPointEventType > PublicDataTreeCheckEvent
crypto::Poseidon2< crypto::Poseidon2Bn254ScalarFieldParams > poseidon2
typename Flavor::FF FF
NiceMock< MockFieldGreaterThan > field_gt
NiceMock< MockWrittenPublicDataSlotsTreeCheck > written_public_data_slots_tree_check