1#include <gmock/gmock.h>
2#include <gtest/gtest.h>
33using tracegen::ExecutionTraceBuilder;
34using tracegen::IndexedTreeCheckTraceBuilder;
35using tracegen::PublicDataTreeTraceBuilder;
36using tracegen::TestTraceContainer;
39using simulation::EventEmitter;
40using simulation::IndexedTreeCheck;
42using simulation::MockExecutionIdManager;
43using simulation::MockFieldGreaterThan;
44using simulation::MockMerkleCheck;
45using simulation::MockPoseidon2;
46using simulation::PublicDataTreeCheck;
51using simulation::WrittenPublicDataSlotsTreeCheck;
54using testing::NiceMock;
62TEST(SStoreConstrainingTest, PositiveTest)
64 TestTraceContainer
trace({
65 { { C::execution_sel_execute_sstore, 1 },
66 { C::execution_sel_gas_sstore, 1 },
67 { C::execution_dynamic_da_gas_factor, 1 },
68 { C::execution_register_0_, 27 },
69 { C::execution_register_1_, 42 },
70 { C::execution_prev_written_public_data_slots_tree_size, 5 },
71 { C::execution_max_data_writes_reached, 0 },
72 { C::execution_remaining_data_writes_inv,
74 { C::execution_sel_write_public_data, 1 },
77 check_relation<sstore>(trace);
80TEST(SStoreConstrainingTest, NegativeDynamicL2GasIsZero)
82 TestTraceContainer
trace({ {
83 { C::execution_sel_execute_sstore, 1 },
84 { C::execution_dynamic_l2_gas_factor, 1 },
89TEST(SStoreConstrainingTest, MaxDataWritesReached)
91 TestTraceContainer
trace({
93 { C::execution_sel_execute_sstore, 1 },
94 { C::execution_prev_written_public_data_slots_tree_size,
96 { C::execution_remaining_data_writes_inv, 0 },
97 { C::execution_max_data_writes_reached, 1 },
102 trace.
set(C::execution_max_data_writes_reached, 0, 0);
105 "SSTORE_MAX_DATA_WRITES_REACHED");
108TEST(SStoreConstrainingTest, OpcodeError)
110 TestTraceContainer
trace({
112 { C::execution_sel_execute_sstore, 1 },
113 { C::execution_dynamic_da_gas_factor, 1 },
114 { C::execution_max_data_writes_reached, 1 },
115 { C::execution_sel_opcode_error, 1 },
118 { C::execution_sel_execute_sstore, 1 },
119 { C::execution_dynamic_da_gas_factor, 0 },
120 { C::execution_max_data_writes_reached, 0 },
121 { C::execution_is_static, 1 },
122 { C::execution_sel_opcode_error, 1 },
125 { C::execution_sel_execute_sstore, 1 },
126 { C::execution_dynamic_da_gas_factor, 0 },
127 { C::execution_max_data_writes_reached, 1 },
128 { C::execution_sel_opcode_error, 0 },
133 trace.
set(C::execution_dynamic_da_gas_factor, 0, 0);
136 "OPCODE_ERROR_IF_OVERFLOW_OR_STATIC");
138 trace.
set(C::execution_dynamic_da_gas_factor, 0, 1);
140 trace.
set(C::execution_is_static, 1, 0);
143 "OPCODE_ERROR_IF_OVERFLOW_OR_STATIC");
146TEST(SStoreConstrainingTest, TreeStateNotChangedOnError)
148 TestTraceContainer
trace({ {
149 { C::execution_sel_execute_sstore, 1 },
150 { C::execution_prev_public_data_tree_root, 27 },
151 { C::execution_prev_public_data_tree_size, 5 },
152 { C::execution_prev_written_public_data_slots_tree_root, 28 },
153 { C::execution_prev_written_public_data_slots_tree_size, 6 },
154 { C::execution_public_data_tree_root, 27 },
155 { C::execution_public_data_tree_size, 5 },
156 { C::execution_written_public_data_slots_tree_root, 28 },
157 { C::execution_written_public_data_slots_tree_size, 6 },
158 { C::execution_sel_opcode_error, 1 },
161 check_relation<sstore>(trace,
168 trace.
set(C::execution_written_public_data_slots_tree_root, 0, 29);
170 "SSTORE_WRITTEN_SLOTS_ROOT_NOT_CHANGED");
173 trace.
set(C::execution_written_public_data_slots_tree_size, 0, 7);
175 "SSTORE_WRITTEN_SLOTS_SIZE_NOT_CHANGED");
178 trace.
set(C::execution_public_data_tree_root, 0, 29);
180 "SSTORE_PUBLIC_DATA_TREE_ROOT_NOT_CHANGED");
183 trace.
set(C::execution_public_data_tree_size, 0, 7);
185 "SSTORE_PUBLIC_DATA_TREE_SIZE_NOT_CHANGED");
190TEST(SStoreConstrainingTest, NegativeGhostRowStorageWrite_RelationsOnly)
193 TestTraceContainer
trace({
195 { C::execution_sel_execute_sstore, 0 },
196 { C::execution_sel_write_public_data, 1 },
197 { C::execution_register_0_, 999 },
198 { C::execution_register_1_, 666 },
199 { C::execution_contract_address, 0xDEADBEEF },
200 { C::execution_sel_opcode_error, 0 },
209TEST(SStoreConstrainingTest, Interactions)
212 NiceMock<MockFieldGreaterThan>
field_gt;
216 EventEmitter<IndexedTreeCheckEvent> indexed_tree_check_emitter;
222 EventEmitter<PublicDataTreeCheckEvent> public_data_tree_check_event_emitter;
223 PublicDataTreeCheck public_data_tree_check(
232 uint64_t low_leaf_index = 30;
233 std::vector<FF> low_leaf_sibling_path = { 1, 2, 3, 4, 5 };
235 AppendOnlyTreeSnapshot public_data_tree_before = AppendOnlyTreeSnapshot{
237 .next_available_leaf_index = 128,
241 EXPECT_CALL(
poseidon2, hash(_)).WillRepeatedly([](
const std::vector<FF>&
inputs) {
244 EXPECT_CALL(
field_gt, ff_gt(_, _)).WillRepeatedly([](
const FF&
a,
const FF&
b) {
248 EXPECT_CALL(merkle_check,
write)
249 .WillRepeatedly([]([[maybe_unused]]
FF current_leaf,
253 [[maybe_unused]]
FF prev_root) {
259 auto public_data_tree_after = public_data_tree_check.write(
slot,
264 low_leaf_sibling_path,
265 public_data_tree_before,
271 TestTraceContainer
trace({
273 { C::execution_sel_execute_sstore, 1 },
274 { C::execution_contract_address, contract_address },
275 { C::execution_sel_gas_sstore, 1 },
278 { C::execution_dynamic_da_gas_factor, 1 },
279 { C::execution_register_0_,
value },
280 { C::execution_register_1_,
slot },
281 { C::execution_max_data_writes_reached, 0 },
282 { C::execution_remaining_data_writes_inv,
284 written_slots_tree_before.next_available_leaf_index)
287 { C::execution_sel_write_public_data, 1 },
288 { C::execution_prev_public_data_tree_root, public_data_tree_before.root },
289 { C::execution_prev_public_data_tree_size, public_data_tree_before.next_available_leaf_index },
290 { C::execution_public_data_tree_root, public_data_tree_after.root },
291 { C::execution_public_data_tree_size, public_data_tree_after.next_available_leaf_index },
292 { C::execution_prev_written_public_data_slots_tree_root, written_slots_tree_before.root },
293 { C::execution_prev_written_public_data_slots_tree_size,
294 written_slots_tree_before.next_available_leaf_index },
295 { C::execution_written_public_data_slots_tree_root, written_slots_tree_after.root },
296 { C::execution_written_public_data_slots_tree_size, written_slots_tree_after.next_available_leaf_index },
300 PublicDataTreeTraceBuilder public_data_tree_trace_builder;
301 public_data_tree_trace_builder.process(public_data_tree_check_event_emitter.dump_events(),
trace);
303 IndexedTreeCheckTraceBuilder written_slots_tree_trace_builder;
304 written_slots_tree_trace_builder.process(indexed_tree_check_emitter.dump_events(),
trace);
306 check_relation<sstore>(trace);
324TEST(SStoreConstrainingTest, NegativeFullAttackWithAllTraces)
327 NiceMock<MockFieldGreaterThan>
field_gt;
331 EventEmitter<IndexedTreeCheckEvent> indexed_tree_check_emitter;
336 EventEmitter<PublicDataTreeCheckEvent> public_data_tree_check_event_emitter;
337 PublicDataTreeCheck public_data_tree_check(
347 uint64_t low_leaf_index = 30;
348 std::vector<FF> low_leaf_sibling_path = { 1, 2, 3, 4, 5 };
350 AppendOnlyTreeSnapshot public_data_tree_before = AppendOnlyTreeSnapshot{
352 .next_available_leaf_index = 128,
356 EXPECT_CALL(
poseidon2, hash(_)).WillRepeatedly([](
const std::vector<FF>&
inputs) {
359 EXPECT_CALL(
field_gt, ff_gt(_, _)).WillRepeatedly([](
const FF&
a,
const FF&
b) {
362 EXPECT_CALL(merkle_check,
write)
363 .WillRepeatedly([]([[maybe_unused]]
FF current_leaf,
367 [[maybe_unused]]
FF prev_root) {
373 auto public_data_tree_after = public_data_tree_check.write(
slot,
378 low_leaf_sibling_path,
379 public_data_tree_before,
386 TestTraceContainer
trace;
387 PublicDataTreeTraceBuilder public_data_tree_trace_builder;
388 public_data_tree_trace_builder.process(public_data_tree_check_event_emitter.dump_events(),
trace);
390 IndexedTreeCheckTraceBuilder written_slots_tree_trace_builder;
391 written_slots_tree_trace_builder.process(indexed_tree_check_emitter.dump_events(),
trace);
399 { C::execution_clk, 0 },
400 { C::precomputed_first_row, 1 },
401 { C::execution_sel_execute_sstore, 0 },
402 { C::execution_sel_write_public_data, 1 },
403 { C::execution_contract_address, contract_address },
404 { C::execution_register_0_,
value },
405 { C::execution_register_1_,
slot },
406 { C::execution_sel_opcode_error, 0 },
407 { C::execution_discard, 0 },
408 { C::execution_prev_public_data_tree_root, public_data_tree_before.root },
409 { C::execution_prev_public_data_tree_size, public_data_tree_before.next_available_leaf_index },
410 { C::execution_public_data_tree_root, public_data_tree_after.root },
411 { C::execution_public_data_tree_size, public_data_tree_after.next_available_leaf_index },
412 { C::execution_prev_written_public_data_slots_tree_root, written_slots_tree_before.root },
413 { C::execution_prev_written_public_data_slots_tree_size,
414 written_slots_tree_before.next_available_leaf_index },
415 { C::execution_written_public_data_slots_tree_root, written_slots_tree_after.root },
416 { C::execution_written_public_data_slots_tree_size, written_slots_tree_after.next_available_leaf_index },
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessageRegex)
#define AVM_EXEC_OP_ID_SSTORE
#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
IndexedTreeCheck indexed_tree_check
static constexpr size_t SR_DYN_L2_GAS_IS_ZERO
static constexpr size_t SR_SSTORE_WRITTEN_SLOTS_SIZE_NOT_CHANGED
static constexpr size_t SR_OPCODE_ERROR_IF_OVERFLOW_OR_STATIC
static constexpr size_t SR_SSTORE_MAX_DATA_WRITES_REACHED
static constexpr size_t SR_SSTORE_WRITTEN_SLOTS_ROOT_NOT_CHANGED
static constexpr size_t SR_SSTORE_PUBLIC_DATA_TREE_SIZE_NOT_CHANGED
static constexpr size_t SR_SSTORE_PUBLIC_DATA_TREE_ROOT_NOT_CHANGED
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
IndexedTreeLeafData low_leaf
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
FF unconstrained_root_from_path(const FF &leaf_value, const uint64_t leaf_index, std::span< const FF > path)
std::variant< IndexedTreeReadWriteEvent, CheckPointEventType > IndexedTreeCheckEvent
::bb::crypto::merkle_tree::PublicDataLeafValue PublicDataLeafValue
WrittenPublicDataSlotsTree build_public_data_slots_tree()
FF unconstrained_compute_leaf_slot(const AztecAddress &contract_address, const FF &slot)
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
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
constexpr field invert() const noexcept
NiceMock< MockExecution > execution
NiceMock< MockWrittenPublicDataSlotsTreeCheck > written_public_data_slots_tree_check