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
30
31namespace bb::avm2::constraining {
32namespace {
33
34using tracegen::ExecutionTraceBuilder;
35using tracegen::IndexedTreeCheckTraceBuilder;
36using tracegen::PublicDataTreeTraceBuilder;
37using tracegen::TestTraceContainer;
38
40using simulation::EventEmitter;
41using simulation::IndexedTreeCheck;
43using simulation::MockExecutionIdManager;
44using simulation::MockFieldGreaterThan;
45using simulation::MockMerkleCheck;
46using simulation::MockPoseidon2;
47using simulation::PublicDataTreeCheck;
52using simulation::WrittenPublicDataSlotsTreeCheck;
53
54using testing::_;
55using testing::NiceMock;
56
58using C = Column;
59using sstore = bb::avm2::sstore<FF>;
62
63TEST(SStoreConstrainingTest, PositiveTest)
64{
65 TestTraceContainer trace({
66 { { C::execution_sel_execute_sstore, 1 },
67 { C::execution_sel_gas_sstore, 1 },
68 { C::execution_dynamic_da_gas_factor, 1 },
69 { C::execution_register_0_, /*value=*/27 },
70 { C::execution_register_1_, /*slot=*/42 },
71 { C::execution_prev_written_public_data_slots_tree_size, 5 },
72 { C::execution_max_data_writes_reached, 0 },
73 { C::execution_remaining_data_writes_inv,
75 { C::execution_sel_write_public_data, 1 },
76 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_SSTORE } },
77 });
78 check_relation<sstore>(trace);
79}
80
81TEST(SStoreConstrainingTest, NegativeDynamicL2GasIsZero)
82{
83 TestTraceContainer trace({ {
84 { C::execution_sel_execute_sstore, 1 },
85 { C::execution_dynamic_l2_gas_factor, 1 },
86 } });
87 EXPECT_THROW_WITH_MESSAGE(check_relation<execution>(trace, execution::SR_DYN_L2_GAS_IS_ZERO), "DYN_L2_GAS_IS_ZERO");
88}
89
90TEST(SStoreConstrainingTest, MaxDataWritesReached)
91{
92 TestTraceContainer trace({
93 {
94 { C::execution_sel_execute_sstore, 1 },
95 { C::execution_prev_written_public_data_slots_tree_size,
97 { C::execution_remaining_data_writes_inv, 0 },
98 { C::execution_max_data_writes_reached, 1 },
99 },
100 });
101 check_relation<sstore>(trace, sstore::SR_SSTORE_MAX_DATA_WRITES_REACHED);
102
103 trace.set(C::execution_max_data_writes_reached, 0, 0);
104
106 "SSTORE_MAX_DATA_WRITES_REACHED");
107}
108
109TEST(SStoreConstrainingTest, OpcodeError)
110{
111 TestTraceContainer trace({
112 {
113 { C::execution_sel_execute_sstore, 1 },
114 { C::execution_dynamic_da_gas_factor, 1 },
115 { C::execution_max_data_writes_reached, 1 },
116 { C::execution_sel_opcode_error, 1 },
117 },
118 {
119 { C::execution_sel_execute_sstore, 1 },
120 { C::execution_dynamic_da_gas_factor, 0 },
121 { C::execution_max_data_writes_reached, 0 },
122 { C::execution_is_static, 1 },
123 { C::execution_sel_opcode_error, 1 },
124 },
125 {
126 { C::execution_sel_execute_sstore, 1 },
127 { C::execution_dynamic_da_gas_factor, 0 },
128 { C::execution_max_data_writes_reached, 1 },
129 { C::execution_sel_opcode_error, 0 },
130 },
131 });
132 check_relation<sstore>(trace, sstore::SR_OPCODE_ERROR_IF_OVERFLOW_OR_STATIC);
133
134 trace.set(C::execution_dynamic_da_gas_factor, 0, 0);
135
137 "OPCODE_ERROR_IF_OVERFLOW_OR_STATIC");
138
139 trace.set(C::execution_dynamic_da_gas_factor, 0, 1);
140
141 trace.set(C::execution_is_static, 1, 0);
142
144 "OPCODE_ERROR_IF_OVERFLOW_OR_STATIC");
145}
146
147TEST(SStoreConstrainingTest, TreeStateNotChangedOnError)
148{
149 TestTraceContainer trace({ {
150 { C::execution_sel_execute_sstore, 1 },
151 { C::execution_prev_public_data_tree_root, 27 },
152 { C::execution_prev_public_data_tree_size, 5 },
153 { C::execution_prev_written_public_data_slots_tree_root, 28 },
154 { C::execution_prev_written_public_data_slots_tree_size, 6 },
155 { C::execution_public_data_tree_root, 27 },
156 { C::execution_public_data_tree_size, 5 },
157 { C::execution_written_public_data_slots_tree_root, 28 },
158 { C::execution_written_public_data_slots_tree_size, 6 },
159 { C::execution_sel_opcode_error, 1 },
160 } });
161
162 check_relation<sstore>(trace,
167
168 // Negative test: written slots tree root must be the same
169 trace.set(C::execution_written_public_data_slots_tree_root, 0, 29);
171 "SSTORE_WRITTEN_SLOTS_ROOT_NOT_CHANGED");
172
173 // Negative test: written slots tree size must be the same
174 trace.set(C::execution_written_public_data_slots_tree_size, 0, 7);
176 "SSTORE_WRITTEN_SLOTS_SIZE_NOT_CHANGED");
177
178 // Negative test: public data tree root must be the same
179 trace.set(C::execution_public_data_tree_root, 0, 29);
181 "SSTORE_PUBLIC_DATA_TREE_ROOT_NOT_CHANGED");
182
183 // Negative test: public data tree size must be the same
184 trace.set(C::execution_public_data_tree_size, 0, 7);
186 "SSTORE_PUBLIC_DATA_TREE_SIZE_NOT_CHANGED");
187}
188
189// Test that ghost rows (sel_execute_sstore=0) cannot set sel_write_public_data=1
190// This verifies the fix: sel_write_public_data * (1 - sel_execute_sstore) = 0
191TEST(SStoreConstrainingTest, NegativeGhostRowStorageWrite_RelationsOnly)
192{
193 // Try to create a ghost row (sel_execute_sstore=0) with sel_write_public_data=1
194 TestTraceContainer trace({
195 {
196 { C::execution_sel_execute_sstore, 0 }, // Ghost row: sstore not executing
197 { C::execution_sel_write_public_data, 1 }, // Try to fire storage write anyway
198 { C::execution_register_0_, /*value=*/999 }, // Arbitrary value
199 { C::execution_register_1_, /*slot=*/666 }, // Arbitrary slot
200 { C::execution_contract_address, 0xDEADBEEF }, // Arbitrary address
201 { C::execution_sel_opcode_error, 0 },
202 },
203 });
204
205 // The fix: sel_write_public_data = sel_execute_sstore * (1 - sel_opcode_error)
206 // When sel_execute_sstore=0 and sel_write_public_data=1: 1 * (1-0) = 1 != 0 -> FAILS
207 EXPECT_THROW_WITH_MESSAGE(check_relation<sstore>(trace), "SEL_WRITE_PUBLIC_DATA_IS_EXECUTE_AND_NOT_ERROR");
208}
209
210TEST(SStoreConstrainingTest, Interactions)
211{
212 NiceMock<MockPoseidon2> poseidon2;
213 NiceMock<MockFieldGreaterThan> field_gt;
214 NiceMock<MockMerkleCheck> merkle_check;
215 NiceMock<MockExecutionIdManager> execution_id_manager;
216
217 EventEmitter<IndexedTreeCheckEvent> indexed_tree_check_emitter;
218 IndexedTreeCheck indexed_tree_check(
219 poseidon2, merkle_check, field_gt, DOM_SEP__WRITTEN_SLOTS_MERKLE, indexed_tree_check_emitter);
220
221 WrittenPublicDataSlotsTreeCheck written_public_data_slots_tree_check(indexed_tree_check,
223
224 EventEmitter<PublicDataTreeCheckEvent> public_data_tree_check_event_emitter;
225 PublicDataTreeCheck public_data_tree_check(
226 poseidon2, merkle_check, field_gt, execution_id_manager, public_data_tree_check_event_emitter);
227
228 FF slot = 42;
229 AztecAddress contract_address = 1;
230 FF leaf_slot = unconstrained_compute_leaf_slot(contract_address, slot);
231 FF value = 27;
232
234 uint64_t low_leaf_index = 30;
235 std::vector<FF> low_leaf_sibling_path = { 1, 2, 3, 4, 5 };
236
237 AppendOnlyTreeSnapshot public_data_tree_before = AppendOnlyTreeSnapshot{
238 .root = 42,
239 .next_available_leaf_index = 128,
240 };
241 AppendOnlyTreeSnapshot written_slots_tree_before = written_public_data_slots_tree_check.get_snapshot();
242
243 EXPECT_CALL(poseidon2, hash(_)).WillRepeatedly([](const std::vector<FF>& inputs) {
245 });
246 EXPECT_CALL(field_gt, ff_gt(_, _)).WillRepeatedly([](const FF& a, const FF& b) {
247 return static_cast<uint256_t>(a) > static_cast<uint256_t>(b);
248 });
249
250 EXPECT_CALL(merkle_check, write)
251 .WillRepeatedly([]([[maybe_unused]] uint64_t domain_separator,
252 [[maybe_unused]] FF current_leaf,
253 FF new_leaf,
254 uint64_t leaf_index,
255 std::span<const FF> sibling_path,
256 [[maybe_unused]] FF prev_root) {
257 return unconstrained_root_from_path(DOM_SEP__WRITTEN_SLOTS_MERKLE, new_leaf, leaf_index, sibling_path);
258 });
259
260 written_public_data_slots_tree_check.contains(contract_address, slot);
261
262 auto public_data_tree_after = public_data_tree_check.write(slot,
263 contract_address,
264 value,
265 low_leaf,
266 low_leaf_index,
267 low_leaf_sibling_path,
268 public_data_tree_before,
269 {},
270 false);
271 written_public_data_slots_tree_check.insert(contract_address, slot);
272 auto written_slots_tree_after = written_public_data_slots_tree_check.get_snapshot();
273
274 TestTraceContainer trace({
275 {
276 { C::execution_sel_execute_sstore, 1 },
277 { C::execution_contract_address, contract_address },
278 { C::execution_sel_gas_sstore, 1 },
279 { C::execution_written_slots_tree_height, AVM_WRITTEN_PUBLIC_DATA_SLOTS_TREE_HEIGHT },
280 { C::execution_written_slots_merkle_separator, DOM_SEP__WRITTEN_SLOTS_MERKLE },
281 { C::execution_written_slots_tree_siloing_separator, DOM_SEP__PUBLIC_LEAF_SLOT },
282 { C::execution_dynamic_da_gas_factor, 1 },
283 { C::execution_register_0_, value },
284 { C::execution_register_1_, slot },
285 { C::execution_max_data_writes_reached, 0 },
286 { C::execution_remaining_data_writes_inv,
288 written_slots_tree_before.next_available_leaf_index)
289 .invert() },
290 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_SSTORE },
291 { C::execution_sel_write_public_data, 1 },
292 { C::execution_prev_public_data_tree_root, public_data_tree_before.root },
293 { C::execution_prev_public_data_tree_size, public_data_tree_before.next_available_leaf_index },
294 { C::execution_public_data_tree_root, public_data_tree_after.root },
295 { C::execution_public_data_tree_size, public_data_tree_after.next_available_leaf_index },
296 { C::execution_prev_written_public_data_slots_tree_root, written_slots_tree_before.root },
297 { C::execution_prev_written_public_data_slots_tree_size,
298 written_slots_tree_before.next_available_leaf_index },
299 { C::execution_written_public_data_slots_tree_root, written_slots_tree_after.root },
300 { C::execution_written_public_data_slots_tree_size, written_slots_tree_after.next_available_leaf_index },
301 },
302 });
303
304 PublicDataTreeTraceBuilder public_data_tree_trace_builder;
305 public_data_tree_trace_builder.process(public_data_tree_check_event_emitter.dump_events(), trace);
306
307 IndexedTreeCheckTraceBuilder written_slots_tree_trace_builder;
308 written_slots_tree_trace_builder.process(indexed_tree_check_emitter.dump_events(), trace);
309
310 check_relation<sstore>(trace);
311 check_interaction<ExecutionTraceBuilder,
314 check_multipermutation_interaction<PublicDataTreeTraceBuilder,
317}
318
319// Ghost row injection attack test.
320// Verifies that the fix (sel_write_public_data * (1 - sel_execute_sstore) = 0) prevents
321// a malicious prover from injecting arbitrary storage writes via ghost sstore rows.
322//
323// Attack vector (now blocked):
324// 1. Create ghost sstore row (sel_execute_sstore=0, sel_write_public_data=1)
325// 2. Populate public_data_check trace with legitimate rows via simulation
326// 3. Align clk values so the STORAGE_WRITE permutation matches
327// 4. Without the fix, the permutation would pass and arbitrary writes would be possible
328TEST(SStoreConstrainingTest, NegativeFullAttackWithAllTraces)
329{
330 NiceMock<MockPoseidon2> poseidon2;
331 NiceMock<MockFieldGreaterThan> field_gt;
332 NiceMock<MockMerkleCheck> merkle_check;
333 NiceMock<MockExecutionIdManager> execution_id_manager;
334
335 EventEmitter<IndexedTreeCheckEvent> indexed_tree_check_emitter;
336 IndexedTreeCheck indexed_tree_check(
337 poseidon2, merkle_check, field_gt, DOM_SEP__WRITTEN_SLOTS_MERKLE, indexed_tree_check_emitter);
338 WrittenPublicDataSlotsTreeCheck written_public_data_slots_tree_check(indexed_tree_check,
340
341 EventEmitter<PublicDataTreeCheckEvent> public_data_tree_check_event_emitter;
342 PublicDataTreeCheck public_data_tree_check(
343 poseidon2, merkle_check, field_gt, execution_id_manager, public_data_tree_check_event_emitter);
344
345 // Attacker-controlled values
346 FF slot = 666;
347 AztecAddress contract_address = 0xDEADBEEF;
348 FF leaf_slot = unconstrained_compute_leaf_slot(contract_address, slot);
349 FF value = 999;
350
352 uint64_t low_leaf_index = 30;
353 std::vector<FF> low_leaf_sibling_path = { 1, 2, 3, 4, 5 };
354
355 AppendOnlyTreeSnapshot public_data_tree_before = AppendOnlyTreeSnapshot{
356 .root = 42,
357 .next_available_leaf_index = 128,
358 };
359 AppendOnlyTreeSnapshot written_slots_tree_before = written_public_data_slots_tree_check.get_snapshot();
360
361 EXPECT_CALL(poseidon2, hash(_)).WillRepeatedly([](const std::vector<FF>& inputs) {
363 });
364 EXPECT_CALL(field_gt, ff_gt(_, _)).WillRepeatedly([](const FF& a, const FF& b) {
365 return static_cast<uint256_t>(a) > static_cast<uint256_t>(b);
366 });
367 EXPECT_CALL(merkle_check, write)
368 .WillRepeatedly([]([[maybe_unused]] uint64_t domain_separator,
369 [[maybe_unused]] FF current_leaf,
370 FF new_leaf,
371 uint64_t leaf_index,
372 std::span<const FF> sibling_path,
373 [[maybe_unused]] FF prev_root) {
374 return unconstrained_root_from_path(DOM_SEP__WRITTEN_SLOTS_MERKLE, new_leaf, leaf_index, sibling_path);
375 });
376
377 // Generate cryptographically valid events via simulation (same as legitimate operation)
378 written_public_data_slots_tree_check.contains(contract_address, slot);
379 auto public_data_tree_after = public_data_tree_check.write(slot,
380 contract_address,
381 value,
382 low_leaf,
383 low_leaf_index,
384 low_leaf_sibling_path,
385 public_data_tree_before,
386 {},
387 false);
388 written_public_data_slots_tree_check.insert(contract_address, slot);
389 auto written_slots_tree_after = written_public_data_slots_tree_check.get_snapshot();
390
391 // Build trace with legitimate public_data_check rows
392 TestTraceContainer trace;
393 PublicDataTreeTraceBuilder public_data_tree_trace_builder;
394 public_data_tree_trace_builder.process(public_data_tree_check_event_emitter.dump_events(), trace);
395
396 IndexedTreeCheckTraceBuilder written_slots_tree_trace_builder;
397 written_slots_tree_trace_builder.process(indexed_tree_check_emitter.dump_events(), trace);
398
399 // Inject ghost sstore at row 0 where precomputed_idx matches public_data_check.clk.
400 // The mock execution_id_manager returns 0, so public_data_check.clk=0.
401 // Ghost row: sel_execute_sstore=0 but sel_write_public_data=1
402 trace.set(
403 0,
404 std::vector<std::pair<Column, FF>>{
405 { C::execution_clk, 0 },
406 { C::precomputed_first_row, 1 },
407 { C::execution_sel_execute_sstore, 0 },
408 { C::execution_sel_write_public_data, 1 },
409 { C::execution_contract_address, contract_address },
410 { C::execution_register_0_, value },
411 { C::execution_register_1_, slot },
412 { C::execution_sel_opcode_error, 0 },
413 { C::execution_discard, 0 },
414 { C::execution_prev_public_data_tree_root, public_data_tree_before.root },
415 { C::execution_prev_public_data_tree_size, public_data_tree_before.next_available_leaf_index },
416 { C::execution_public_data_tree_root, public_data_tree_after.root },
417 { C::execution_public_data_tree_size, public_data_tree_after.next_available_leaf_index },
418 { C::execution_prev_written_public_data_slots_tree_root, written_slots_tree_before.root },
419 { C::execution_prev_written_public_data_slots_tree_size,
420 written_slots_tree_before.next_available_leaf_index },
421 { C::execution_written_public_data_slots_tree_root, written_slots_tree_after.root },
422 { C::execution_written_public_data_slots_tree_size, written_slots_tree_after.next_available_leaf_index },
423 });
424
425 // The fix blocks ghost rows: sel_write_public_data = sel_execute_sstore * (1 - sel_opcode_error)
426 // When sel_execute_sstore=0 and sel_write_public_data=1: 1 * 1 = 1 != 0
427 EXPECT_THROW_WITH_MESSAGE(check_relation<sstore>(trace), "SEL_WRITE_PUBLIC_DATA_IS_EXECUTE_AND_NOT_ERROR");
428}
429
430} // namespace
431} // namespace bb::avm2::constraining
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessageRegex)
Definition assert.hpp:193
#define AVM_EXEC_OP_ID_SSTORE
#define DOM_SEP__WRITTEN_SLOTS_MERKLE
#define AVM_WRITTEN_PUBLIC_DATA_SLOTS_TREE_HEIGHT
#define AVM_WRITTEN_PUBLIC_DATA_SLOTS_TREE_INITIAL_SIZE
#define DOM_SEP__PUBLIC_LEAF_SLOT
#define MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX
FieldGreaterThan field_gt
MerkleCheck merkle_check
IndexedTreeCheck indexed_tree_check
static constexpr size_t SR_DYN_L2_GAS_IS_ZERO
Definition execution.hpp:53
static constexpr size_t SR_SSTORE_WRITTEN_SLOTS_SIZE_NOT_CHANGED
Definition sstore.hpp:42
static constexpr size_t SR_OPCODE_ERROR_IF_OVERFLOW_OR_STATIC
Definition sstore.hpp:39
static constexpr size_t SR_SSTORE_MAX_DATA_WRITES_REACHED
Definition sstore.hpp:38
static constexpr size_t SR_SSTORE_WRITTEN_SLOTS_ROOT_NOT_CHANGED
Definition sstore.hpp:41
static constexpr size_t SR_SSTORE_PUBLIC_DATA_TREE_SIZE_NOT_CHANGED
Definition sstore.hpp:44
static constexpr size_t SR_SSTORE_PUBLIC_DATA_TREE_ROOT_NOT_CHANGED
Definition sstore.hpp:43
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
IndexedTreeLeafData low_leaf
AvmProvingInputs inputs
void check_multipermutation_interaction(tracegen::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.
std::variant< PublicDataTreeReadWriteEvent, CheckPointEventType > PublicDataTreeCheckEvent
crypto::Poseidon2< crypto::Poseidon2Bn254ScalarFieldParams > poseidon2
IndexedLeaf< PublicDataLeafValue > PublicDataTreeLeafPreimage
std::variant< IndexedTreeReadWriteEvent, CheckPointEventType > IndexedTreeCheckEvent
::bb::crypto::merkle_tree::PublicDataLeafValue PublicDataLeafValue
Definition db.hpp:38
WrittenPublicDataSlotsTree build_public_data_slots_tree()
FF unconstrained_root_from_path(uint64_t domain_separator, const FF &leaf_value, const uint64_t leaf_index, std::span< const FF > path)
Definition merkle.cpp:12
FF unconstrained_compute_leaf_slot(const AztecAddress &contract_address, const FF &slot)
Definition merkle.cpp:30
lookup_settings< lookup_execution_check_written_storage_slot_settings_ > lookup_execution_check_written_storage_slot_settings
permutation_settings< perm_tx_balance_update_settings_ > perm_tx_balance_update_settings
Definition perms_tx.hpp:200
AvmFlavorSettings::FF FF
Definition field.hpp:10
permutation_settings< perm_sstore_storage_write_settings_ > perm_sstore_storage_write_settings
lookup_settings< lookup_sstore_record_written_storage_slot_settings_ > lookup_sstore_record_written_storage_slot_settings
void write(B &buf, field2< base_field, Params > const &value)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
constexpr field invert() const noexcept
NiceMock< MockExecution > execution
NiceMock< MockWrittenPublicDataSlotsTreeCheck > written_public_data_slots_tree_check