Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
storage_write.test.cpp
Go to the documentation of this file.
1#include <gmock/gmock.h>
2#include <gtest/gtest.h>
3
4#include <cstdint>
5
28
29namespace bb::avm2::constraining {
30namespace {
31
32using tracegen::ExecutionTraceBuilder;
33using tracegen::PublicDataTreeTraceBuilder;
34using tracegen::TestTraceContainer;
35using tracegen::WrittenPublicDataSlotsTreeCheckTraceBuilder;
36
38using simulation::EventEmitter;
39using simulation::MockExecutionIdManager;
40using simulation::MockFieldGreaterThan;
41using simulation::MockMerkleCheck;
42using simulation::MockPoseidon2;
43using simulation::PublicDataTreeCheck;
48using simulation::WrittenPublicDataSlotsTreeCheck;
49using simulation::WrittenPublicDataSlotsTreeCheckEvent;
50
51using testing::_;
52using testing::NiceMock;
53
55using C = Column;
56using sstore = bb::avm2::sstore<FF>;
59
60TEST(SStoreConstrainingTest, PositiveTest)
61{
62 TestTraceContainer trace({
63 { { C::execution_sel_execute_sstore, 1 },
64 { C::execution_sel_gas_sstore, 1 },
65 { C::execution_dynamic_da_gas_factor, 1 },
66 { C::execution_register_0_, /*value=*/27 },
67 { C::execution_register_1_, /*slot=*/42 },
68 { C::execution_prev_written_public_data_slots_tree_size, 5 },
69 { C::execution_max_data_writes_reached, 0 },
70 { C::execution_remaining_data_writes_inv,
72 { C::execution_sel_write_public_data, 1 },
73 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_SSTORE } },
74 });
75 check_relation<sstore>(trace);
76}
77
78TEST(SStoreConstrainingTest, NegativeDynamicL2GasIsZero)
79{
80 TestTraceContainer trace({ {
81 { C::execution_sel_execute_sstore, 1 },
82 { C::execution_dynamic_l2_gas_factor, 1 },
83 } });
85 "SSTORE_DYN_L2_GAS_IS_ZERO");
86}
87
88TEST(SStoreConstrainingTest, MaxDataWritesReached)
89{
90 TestTraceContainer trace({
91 {
92 { C::execution_sel_execute_sstore, 1 },
93 { C::execution_prev_written_public_data_slots_tree_size,
95 { C::execution_remaining_data_writes_inv, 0 },
96 { C::execution_max_data_writes_reached, 1 },
97 },
98 });
99 check_relation<sstore>(trace, sstore::SR_SSTORE_MAX_DATA_WRITES_REACHED);
100
101 trace.set(C::execution_max_data_writes_reached, 0, 0);
102
104 "SSTORE_MAX_DATA_WRITES_REACHED");
105}
106
107TEST(SStoreConstrainingTest, OpcodeError)
108{
109 TestTraceContainer trace({
110 {
111 { C::execution_sel_execute_sstore, 1 },
112 { C::execution_dynamic_da_gas_factor, 1 },
113 { C::execution_max_data_writes_reached, 1 },
114 { C::execution_sel_opcode_error, 1 },
115 },
116 {
117 { C::execution_sel_execute_sstore, 1 },
118 { C::execution_dynamic_da_gas_factor, 0 },
119 { C::execution_max_data_writes_reached, 0 },
120 { C::execution_is_static, 1 },
121 { C::execution_sel_opcode_error, 1 },
122 },
123 {
124 { C::execution_sel_execute_sstore, 1 },
125 { C::execution_dynamic_da_gas_factor, 0 },
126 { C::execution_max_data_writes_reached, 1 },
127 { C::execution_sel_opcode_error, 0 },
128 },
129 });
131
132 trace.set(C::execution_dynamic_da_gas_factor, 0, 0);
133
135 "OPCODE_ERROR_IF_OVERFLOW_OR_STATIC");
136
137 trace.set(C::execution_dynamic_da_gas_factor, 0, 1);
138
139 trace.set(C::execution_is_static, 1, 0);
140
142 "OPCODE_ERROR_IF_OVERFLOW_OR_STATIC");
143}
144
145TEST(SStoreConstrainingTest, TreeStateNotChangedOnError)
146{
147 TestTraceContainer trace({ {
148 { C::execution_sel_execute_sstore, 1 },
149 { C::execution_prev_public_data_tree_root, 27 },
150 { C::execution_prev_public_data_tree_size, 5 },
151 { C::execution_prev_written_public_data_slots_tree_root, 28 },
152 { C::execution_prev_written_public_data_slots_tree_size, 6 },
153 { C::execution_public_data_tree_root, 27 },
154 { C::execution_public_data_tree_size, 5 },
155 { C::execution_written_public_data_slots_tree_root, 28 },
156 { C::execution_written_public_data_slots_tree_size, 6 },
157 { C::execution_sel_opcode_error, 1 },
158 } });
159
160 check_relation<sstore>(trace,
165
166 // Negative test: written slots tree root must be the same
167 trace.set(C::execution_written_public_data_slots_tree_root, 0, 29);
169 "SSTORE_WRITTEN_SLOTS_ROOT_NOT_CHANGED");
170
171 // Negative test: written slots tree size must be the same
172 trace.set(C::execution_written_public_data_slots_tree_size, 0, 7);
174 "SSTORE_WRITTEN_SLOTS_SIZE_NOT_CHANGED");
175
176 // Negative test: public data tree root must be the same
177 trace.set(C::execution_public_data_tree_root, 0, 29);
179 "SSTORE_PUBLIC_DATA_TREE_ROOT_NOT_CHANGED");
180
181 // Negative test: public data tree size must be the same
182 trace.set(C::execution_public_data_tree_size, 0, 7);
184 "SSTORE_PUBLIC_DATA_TREE_SIZE_NOT_CHANGED");
185}
186
187TEST(SStoreConstrainingTest, Interactions)
188{
189 NiceMock<MockPoseidon2> poseidon2;
190 NiceMock<MockFieldGreaterThan> field_gt;
191 NiceMock<MockMerkleCheck> merkle_check;
192 NiceMock<MockExecutionIdManager> execution_id_manager;
193
194 EventEmitter<WrittenPublicDataSlotsTreeCheckEvent> written_public_data_slots_emitter;
195 WrittenPublicDataSlotsTreeCheck written_public_data_slots_tree_check(
196 poseidon2, merkle_check, field_gt, build_public_data_slots_tree(), written_public_data_slots_emitter);
197
198 EventEmitter<PublicDataTreeCheckEvent> public_data_tree_check_event_emitter;
199 PublicDataTreeCheck public_data_tree_check(
200 poseidon2, merkle_check, field_gt, execution_id_manager, public_data_tree_check_event_emitter);
201
202 FF slot = 42;
205 FF value = 27;
206
208 uint64_t low_leaf_index = 30;
209 std::vector<FF> low_leaf_sibling_path = { 1, 2, 3, 4, 5 };
210
211 AppendOnlyTreeSnapshot public_data_tree_before = AppendOnlyTreeSnapshot{
212 .root = 42,
213 .nextAvailableLeafIndex = 128,
214 };
215 AppendOnlyTreeSnapshot written_slots_tree_before = written_public_data_slots_tree_check.snapshot();
216
217 EXPECT_CALL(poseidon2, hash(_)).WillRepeatedly([](const std::vector<FF>& inputs) {
218 return RawPoseidon2::hash(inputs);
219 });
220 EXPECT_CALL(field_gt, ff_gt(_, _)).WillRepeatedly([](const FF& a, const FF& b) {
221 return static_cast<uint256_t>(a) > static_cast<uint256_t>(b);
222 });
223
224 EXPECT_CALL(merkle_check, write)
225 .WillRepeatedly([]([[maybe_unused]] FF current_leaf,
226 FF new_leaf,
227 uint64_t leaf_index,
228 std::span<const FF> sibling_path,
229 [[maybe_unused]] FF prev_root) {
230 return unconstrained_root_from_path(new_leaf, leaf_index, sibling_path);
231 });
232
234
235 auto public_data_tree_after = public_data_tree_check.write(slot,
237 value,
238 low_leaf,
239 low_leaf_index,
240 low_leaf_sibling_path,
241 public_data_tree_before,
242 {},
243 false);
245 auto written_slots_tree_after = written_public_data_slots_tree_check.snapshot();
246
247 TestTraceContainer trace({
248 {
249 { C::execution_sel_execute_sstore, 1 },
250 { C::execution_contract_address, contract_address },
251 { C::execution_sel_gas_sstore, 1 },
252 { C::execution_dynamic_da_gas_factor, 1 },
253 { C::execution_register_0_, value },
254 { C::execution_register_1_, slot },
255 { C::execution_max_data_writes_reached, 0 },
256 { C::execution_remaining_data_writes_inv,
258 written_slots_tree_before.nextAvailableLeafIndex)
259 .invert() },
260 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_SSTORE },
261 { C::execution_sel_write_public_data, 1 },
262 { C::execution_prev_public_data_tree_root, public_data_tree_before.root },
263 { C::execution_prev_public_data_tree_size, public_data_tree_before.nextAvailableLeafIndex },
264 { C::execution_public_data_tree_root, public_data_tree_after.root },
265 { C::execution_public_data_tree_size, public_data_tree_after.nextAvailableLeafIndex },
266 { C::execution_prev_written_public_data_slots_tree_root, written_slots_tree_before.root },
267 { C::execution_prev_written_public_data_slots_tree_size, written_slots_tree_before.nextAvailableLeafIndex },
268 { C::execution_written_public_data_slots_tree_root, written_slots_tree_after.root },
269 { C::execution_written_public_data_slots_tree_size, written_slots_tree_after.nextAvailableLeafIndex },
270 },
271 });
272
273 PublicDataTreeTraceBuilder public_data_tree_trace_builder;
274 public_data_tree_trace_builder.process(public_data_tree_check_event_emitter.dump_events(), trace);
275
276 WrittenPublicDataSlotsTreeCheckTraceBuilder written_slots_tree_trace_builder;
277 written_slots_tree_trace_builder.process(written_public_data_slots_emitter.dump_events(), trace);
278
279 check_relation<sstore>(trace);
280 check_interaction<ExecutionTraceBuilder,
284}
285
286} // namespace
287} // namespace bb::avm2::constraining
#define AVM_EXEC_OP_ID_SSTORE
#define AVM_WRITTEN_PUBLIC_DATA_SLOTS_TREE_INITIAL_SIZE
#define MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX
static constexpr size_t SR_SSTORE_DYN_L2_GAS_IS_ZERO
static constexpr size_t SR_SSTORE_WRITTEN_SLOTS_SIZE_NOT_CHANGED
Definition sstore.hpp:60
static constexpr size_t SR_OPCODE_ERROR_IF_OVERFLOW_OR_STATIC
Definition sstore.hpp:58
static constexpr size_t SR_SSTORE_MAX_DATA_WRITES_REACHED
Definition sstore.hpp:57
static constexpr size_t SR_SSTORE_WRITTEN_SLOTS_ROOT_NOT_CHANGED
Definition sstore.hpp:59
static constexpr size_t SR_SSTORE_PUBLIC_DATA_TREE_SIZE_NOT_CHANGED
Definition sstore.hpp:62
static constexpr size_t SR_SSTORE_PUBLIC_DATA_TREE_ROOT_NOT_CHANGED
Definition sstore.hpp:61
void set(Column col, uint32_t row, const FF &value)
static FF hash(const std::vector< FF > &input)
Hashes a vector of field elements.
ExecutionIdManager execution_id_manager
TestTraceContainer trace
FF a
FF b
NullifierTreeLeafPreimage low_leaf
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessage)
Definition macros.hpp:7
void hash(State &state) noexcept
void check_interaction(tracegen::TestTraceContainer &trace)
AvmFlavorSettings::FF FF
TEST(TxExecutionConstrainingTest, WriteTreeValue)
Definition tx.test.cpp:508
std::variant< PublicDataTreeReadWriteEvent, CheckPointEventType > PublicDataTreeCheckEvent
IndexedLeaf< PublicDataLeafValue > PublicDataTreeLeafPreimage
crypto::Poseidon2< crypto::Poseidon2Bn254ScalarFieldParams > poseidon2
FF unconstrained_root_from_path(const FF &leaf_value, const uint64_t leaf_index, std::span< const FF > path)
Definition merkle.cpp:12
::bb::crypto::merkle_tree::PublicDataLeafValue PublicDataLeafValue
WrittenPublicDataSlotsTree build_public_data_slots_tree()
FF unconstrained_compute_leaf_slot(const AztecAddress &contract_address, const FF &slot)
Definition merkle.cpp:26
lookup_settings< lookup_execution_check_written_storage_slot_settings_ > lookup_execution_check_written_storage_slot_settings
lookup_settings< lookup_sstore_record_written_storage_slot_settings_ > lookup_sstore_record_written_storage_slot_settings
lookup_settings< lookup_sstore_storage_write_settings_ > lookup_sstore_storage_write_settings
typename Flavor::FF FF
void write(B &buf, field2< base_field, Params > const &value)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
NiceMock< MockFieldGreaterThan > field_gt
NiceMock< MockExecution > execution
NiceMock< MockWrittenPublicDataSlotsTreeCheck > written_public_data_slots_tree_check