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::MockIndexedTreeCheck;
40using simulation::MockL1ToL2MessageTreeCheck;
41using simulation::MockLowLevelMerkleDB;
42using simulation::MockMerkleCheck;
43using simulation::MockNoteHashTreeCheck;
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_, /*contract_address=*/1 },
63 { C::execution_register_2_, /*value=*/27 },
64 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
65 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::FF) },
66 { C::execution_mem_tag_reg_2_, static_cast<uint8_t>(MemoryTag::FF) },
67 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_SLOAD } },
68 });
69 check_relation<sload>(trace);
70}
71
72TEST(SLoadConstrainingTest, NegativeInvalidOutputTag)
73{
74 TestTraceContainer trace({
75 { { C::execution_sel_execute_sload, 1 },
76 { C::execution_register_0_, /*slot=*/42 },
77 { C::execution_register_1_, /*contract_address=*/1 },
78 { C::execution_register_2_, /*value=*/27 },
79 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
80 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::FF) },
81 { C::execution_mem_tag_reg_2_, static_cast<uint8_t>(MemoryTag::U32) },
82 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_SLOAD } },
83 });
84 EXPECT_THROW_WITH_MESSAGE(check_relation<sload>(trace), "SLOAD_FF_OUTPUT_TAG");
85}
86
87TEST(SLoadConstrainingTest, NegativeSloadSuccess)
88{
89 TestTraceContainer trace({
90 { { C::execution_sel_execute_sload, 1 },
91 { C::execution_register_0_, /*slot=*/42 },
92 { C::execution_register_1_, /*contract_address=*/1 },
93 { C::execution_register_2_, /*value=*/27 },
94 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
95 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::FF) },
96 { C::execution_mem_tag_reg_2_, static_cast<uint8_t>(MemoryTag::FF) },
97 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_SLOAD },
98 { C::execution_sel_opcode_error, 1 } },
99 });
100
101 check_relation<sload>(trace);
103 "INFALLIBLE_OPCODES_SUCCESS");
104}
105
106TEST(SLoadConstrainingTest, Interactions)
107{
108 NiceMock<MockPoseidon2> poseidon2;
109 NiceMock<MockFieldGreaterThan> field_gt;
110 NiceMock<MockMerkleCheck> merkle_check;
111 NiceMock<MockExecutionIdManager> execution_id_manager;
112 NiceMock<MockWrittenPublicDataSlotsTreeCheck> written_public_data_slots_tree_check;
113 NiceMock<MockLowLevelMerkleDB> low_level_merkle_db;
114 NiceMock<MockIndexedTreeCheck> indexed_tree_check;
115 NiceMock<MockNoteHashTreeCheck> note_hash_tree_check;
116 NiceMock<MockL1ToL2MessageTreeCheck> l1_to_l2_message_tree_check;
117
118 EventEmitter<PublicDataTreeCheckEvent> public_data_tree_check_event_emitter;
119 PublicDataTreeCheck public_data_tree_check(
120 poseidon2, merkle_check, field_gt, execution_id_manager, public_data_tree_check_event_emitter);
121
122 FF slot = 42;
123 AztecAddress contract_address = 1;
124
125 MerkleDB merkle_db(low_level_merkle_db,
126 public_data_tree_check,
127 indexed_tree_check,
128 note_hash_tree_check,
130 l1_to_l2_message_tree_check);
131
132 TreeSnapshots trees;
133 trees.public_data_tree.root = 42;
134 EXPECT_CALL(low_level_merkle_db, get_tree_roots()).WillRepeatedly(Return(trees));
135
136 FF value = merkle_db.storage_read(contract_address, slot);
137
138 TestTraceContainer trace({
139 { { C::execution_sel_execute_sload, 1 },
140 { C::execution_register_0_, slot },
141 { C::execution_register_1_, FF(contract_address) },
142 { C::execution_register_2_, value },
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::FF) },
145 { C::execution_mem_tag_reg_2_, static_cast<uint8_t>(MemoryTag::FF) },
146 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_SLOAD },
147 { C::execution_prev_public_data_tree_root, trees.public_data_tree.root } },
148 });
149
150 PublicDataTreeTraceBuilder public_data_tree_trace_builder;
151 public_data_tree_trace_builder.process(public_data_tree_check_event_emitter.dump_events(), trace);
152
153 check_relation<sload>(trace);
154 check_interaction<ExecutionTraceBuilder, lookup_sload_storage_read_settings>(trace);
155}
156
157} // namespace
158} // namespace bb::avm2::constraining
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessageRegex)
Definition assert.hpp:193
#define AVM_EXEC_OP_ID_SLOAD
FieldGreaterThan field_gt
MerkleCheck merkle_check
IndexedTreeCheck indexed_tree_check
StrictMock< MockHighLevelMerkleDB > merkle_db
static constexpr size_t SR_INFALLIBLE_OPCODES_SUCCESS
Definition execution.hpp:78
Native Poseidon2 hash function implementation.
Definition poseidon2.hpp:22
ExecutionIdManager execution_id_manager
TestTraceContainer trace
TEST(AvmFixedVKTests, FixedVKCommitments)
Test that the fixed VK commitments agree with the ones computed from precomputed columns.
std::variant< PublicDataTreeReadWriteEvent, CheckPointEventType > PublicDataTreeCheckEvent
crypto::Poseidon2< crypto::Poseidon2Bn254ScalarFieldParams > poseidon2
AvmFlavorSettings::FF FF
Definition field.hpp:10
NiceMock< MockExecution > execution
NiceMock< MockWrittenPublicDataSlotsTreeCheck > written_public_data_slots_tree_check