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::Return;
51
53using C = Column;
54using sload = bb::avm2::sload<FF>;
56
57TEST(SLoadConstrainingTest, PositiveTest)
58{
59 TestTraceContainer trace({
60 { { C::execution_sel_execute_sload, 1 },
61 { C::execution_register_0_, /*slot=*/42 },
62 { C::execution_register_1_, /*dst=*/27 },
63 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
64 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::FF) },
65 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_SLOAD } },
66 });
67 check_relation<sload>(trace);
68}
69
70TEST(SLoadConstrainingTest, NegativeInvalidOutputTag)
71{
72 TestTraceContainer trace({
73 { { C::execution_sel_execute_sload, 1 },
74 { C::execution_register_0_, /*slot=*/42 },
75 { C::execution_register_1_, /*dst=*/27 },
76 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
77 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::U32) },
78 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_SLOAD } },
79 });
80 EXPECT_THROW_WITH_MESSAGE(check_relation<sload>(trace), "SLOAD_FF_OUTPUT_TAG");
81}
82
83TEST(SLoadConstrainingTest, NegativeSloadSuccess)
84{
85 TestTraceContainer trace({
86 { { C::execution_sel_execute_sload, 1 },
87 { C::execution_register_0_, /*slot=*/42 },
88 { C::execution_register_1_, /*dst=*/27 },
89 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
90 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::FF) },
91 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_SLOAD },
92 { C::execution_sel_opcode_error, 1 } },
93 });
94
95 check_relation<sload>(trace);
97 "INFALLIBLE_OPCODES_SUCCESS");
98}
99
100TEST(SLoadConstrainingTest, Interactions)
101{
102 NiceMock<MockPoseidon2> poseidon2;
103 NiceMock<MockFieldGreaterThan> field_gt;
104 NiceMock<MockMerkleCheck> merkle_check;
105 NiceMock<MockExecutionIdManager> execution_id_manager;
106 NiceMock<MockWrittenPublicDataSlotsTreeCheck> written_public_data_slots_tree_check;
107 NiceMock<MockLowLevelMerkleDB> low_level_merkle_db;
108 NiceMock<MockNullifierTreeCheck> nullifier_tree_check;
109 NiceMock<MockNoteHashTreeCheck> note_hash_tree_check;
110 NiceMock<MockL1ToL2MessageTreeCheck> l1_to_l2_message_tree_check;
111
112 EventEmitter<PublicDataTreeCheckEvent> public_data_tree_check_event_emitter;
113 PublicDataTreeCheck public_data_tree_check(
114 poseidon2, merkle_check, field_gt, execution_id_manager, public_data_tree_check_event_emitter);
115
116 FF slot = 42;
118
119 MerkleDB merkle_db(low_level_merkle_db,
120 public_data_tree_check,
121 nullifier_tree_check,
122 note_hash_tree_check,
123 written_public_data_slots_tree_check,
124 l1_to_l2_message_tree_check);
125
126 TreeSnapshots trees;
127 trees.public_data_tree.root = 42;
128 EXPECT_CALL(low_level_merkle_db, get_tree_roots()).WillRepeatedly(Return(trees));
129
130 FF value = merkle_db.storage_read(contract_address, slot);
131
132 TestTraceContainer trace({
133 { { C::execution_sel_execute_sload, 1 },
134 { C::execution_register_0_, slot },
135 { C::execution_register_1_, value },
136 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
137 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::FF) },
138 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_SLOAD },
139 { C::execution_contract_address, contract_address },
140 { C::execution_prev_public_data_tree_root, trees.public_data_tree.root } },
141 });
142
143 PublicDataTreeTraceBuilder public_data_tree_trace_builder;
144 public_data_tree_trace_builder.process(public_data_tree_check_event_emitter.dump_events(), trace);
145
146 check_relation<sload>(trace);
147 check_interaction<ExecutionTraceBuilder, lookup_sload_storage_read_settings>(trace);
148}
149
150} // namespace
151} // namespace bb::avm2::constraining
FieldGreaterThan field_gt
#define AVM_EXEC_OP_ID_SLOAD
StrictMock< MockHighLevelMerkleDB > merkle_db
static constexpr size_t SR_INFALLIBLE_OPCODES_SUCCESS
Definition execution.hpp:76
ExecutionIdManager execution_id_manager
TestTraceContainer trace
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessage)
Definition macros.hpp:7
TEST(TxExecutionConstrainingTest, WriteTreeValue)
Definition tx.test.cpp:441
std::variant< PublicDataTreeReadWriteEvent, CheckPointEventType > PublicDataTreeCheckEvent
crypto::Poseidon2< crypto::Poseidon2Bn254ScalarFieldParams > poseidon2
NiceMock< MockExecution > execution
NiceMock< MockWrittenPublicDataSlotsTreeCheck > written_public_data_slots_tree_check