1#include <gmock/gmock.h>
2#include <gtest/gtest.h>
6#include "barretenberg/aztec/aztec_constants.hpp"
34using tracegen::ExecutionTraceBuilder;
35using tracegen::IndexedTreeCheckTraceBuilder;
36using tracegen::PublicDataTreeTraceBuilder;
37using tracegen::TestTraceContainer;
40using simulation::EventEmitter;
41using simulation::IndexedTreeCheck;
43using simulation::MockExecutionIdManager;
44using simulation::MockFieldGreaterThan;
45using simulation::MockMerkleCheck;
46using simulation::MockPoseidon2;
47using simulation::PublicDataTreeCheck;
52using simulation::WrittenPublicDataSlotsTreeCheck;
55using testing::NiceMock;
63TEST(SStoreConstrainingTest, PositiveTest)
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_, 27 },
70 { C::execution_register_1_, 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,
74 FF(MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX + AVM_WRITTEN_PUBLIC_DATA_SLOTS_TREE_INITIAL_SIZE - 5).invert() },
75 { C::execution_sel_write_public_data, 1 },
76 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_SSTORE } },
78 check_relation<sstore>(trace);
81TEST(SStoreConstrainingTest, NegativeDynamicL2GasIsZero)
83 TestTraceContainer
trace({ {
84 { C::execution_sel_execute_sstore, 1 },
85 { C::execution_dynamic_l2_gas_factor, 1 },
91TEST(SStoreConstrainingTest, MaxDataWritesReached)
93 TestTraceContainer
trace({
95 { C::execution_sel_execute_sstore, 1 },
96 { C::execution_prev_written_public_data_slots_tree_size,
97 MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX + AVM_WRITTEN_PUBLIC_DATA_SLOTS_TREE_INITIAL_SIZE },
98 { C::execution_remaining_data_writes_inv, 0 },
99 { C::execution_max_data_writes_reached, 1 },
104 trace.
set(C::execution_max_data_writes_reached, 0, 0);
110TEST(SStoreConstrainingTest, OpcodeError)
112 TestTraceContainer
trace({
114 { C::execution_sel_execute_sstore, 1 },
115 { C::execution_dynamic_da_gas_factor, 1 },
116 { C::execution_max_data_writes_reached, 1 },
117 { C::execution_sel_opcode_error, 1 },
120 { C::execution_sel_execute_sstore, 1 },
121 { C::execution_dynamic_da_gas_factor, 0 },
122 { C::execution_max_data_writes_reached, 0 },
123 { C::execution_is_static, 1 },
124 { C::execution_sel_opcode_error, 1 },
127 { C::execution_sel_execute_sstore, 1 },
128 { C::execution_dynamic_da_gas_factor, 0 },
129 { C::execution_max_data_writes_reached, 1 },
130 { C::execution_sel_opcode_error, 0 },
135 trace.
set(C::execution_dynamic_da_gas_factor, 0, 0);
140 trace.
set(C::execution_dynamic_da_gas_factor, 0, 1);
142 trace.
set(C::execution_is_static, 1, 0);
148TEST(SStoreConstrainingTest, TreeStateNotChangedOnError)
150 TestTraceContainer
trace({ {
151 { C::execution_sel_execute_sstore, 1 },
152 { C::execution_prev_public_data_tree_root, 27 },
153 { C::execution_prev_public_data_tree_size, 5 },
154 { C::execution_prev_written_public_data_slots_tree_root, 28 },
155 { C::execution_prev_written_public_data_slots_tree_size, 6 },
156 { C::execution_public_data_tree_root, 27 },
157 { C::execution_public_data_tree_size, 5 },
158 { C::execution_written_public_data_slots_tree_root, 28 },
159 { C::execution_written_public_data_slots_tree_size, 6 },
160 { C::execution_sel_opcode_error, 1 },
163 check_relation<sstore>(trace,
170 trace.
set(C::execution_written_public_data_slots_tree_root, 0, 29);
175 trace.
set(C::execution_written_public_data_slots_tree_size, 0, 7);
180 trace.
set(C::execution_public_data_tree_root, 0, 29);
185 trace.
set(C::execution_public_data_tree_size, 0, 7);
192TEST(SStoreConstrainingTest, NegativeGhostRowStorageWrite_RelationsOnly)
195 TestTraceContainer
trace({
197 { C::execution_sel_execute_sstore, 0 },
198 { C::execution_sel_write_public_data, 1 },
199 { C::execution_register_0_, 999 },
200 { C::execution_register_1_, 666 },
201 { C::execution_contract_address, 0xDEADBEEF },
202 { C::execution_sel_opcode_error, 0 },
212TEST(SStoreConstrainingTest, Interactions)
215 NiceMock<MockFieldGreaterThan>
field_gt;
219 EventEmitter<IndexedTreeCheckEvent> indexed_tree_check_emitter;
221 poseidon2, merkle_check,
field_gt, DOM_SEP__WRITTEN_SLOTS_MERKLE, indexed_tree_check_emitter);
226 EventEmitter<PublicDataTreeCheckEvent> public_data_tree_check_event_emitter;
227 PublicDataTreeCheck public_data_tree_check(
236 uint64_t low_leaf_index = 30;
237 std::vector<FF> low_leaf_sibling_path = { 1, 2, 3, 4, 5 };
239 AppendOnlyTreeSnapshot public_data_tree_before = AppendOnlyTreeSnapshot{
241 .next_available_leaf_index = 128,
245 EXPECT_CALL(
poseidon2, hash(_)).WillRepeatedly([](
const std::vector<FF>&
inputs) {
248 EXPECT_CALL(
field_gt, ff_gt(_, _)).WillRepeatedly([](
const FF&
a,
const FF&
b) {
252 EXPECT_CALL(merkle_check,
write)
253 .WillRepeatedly([]([[maybe_unused]] uint64_t domain_separator,
254 [[maybe_unused]]
FF current_leaf,
258 [[maybe_unused]]
FF prev_root) {
264 auto public_data_tree_after = public_data_tree_check.write(
slot,
269 low_leaf_sibling_path,
270 public_data_tree_before,
276 TestTraceContainer
trace({
278 { C::execution_sel_execute_sstore, 1 },
279 { C::execution_contract_address, contract_address },
280 { C::execution_sel_gas_sstore, 1 },
281 { C::execution_written_slots_tree_height, AVM_WRITTEN_PUBLIC_DATA_SLOTS_TREE_HEIGHT },
282 { C::execution_written_slots_merkle_separator, DOM_SEP__WRITTEN_SLOTS_MERKLE },
283 { C::execution_written_slots_tree_siloing_separator, DOM_SEP__PUBLIC_LEAF_SLOT },
284 { C::execution_dynamic_da_gas_factor, 1 },
285 { C::execution_register_0_,
value },
286 { C::execution_register_1_,
slot },
287 { C::execution_max_data_writes_reached, 0 },
288 { C::execution_remaining_data_writes_inv,
289 FF(MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX + AVM_WRITTEN_PUBLIC_DATA_SLOTS_TREE_INITIAL_SIZE -
290 written_slots_tree_before.next_available_leaf_index)
292 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_SSTORE },
293 { C::execution_sel_write_public_data, 1 },
294 { C::execution_prev_public_data_tree_root, public_data_tree_before.root },
295 { C::execution_prev_public_data_tree_size, public_data_tree_before.next_available_leaf_index },
296 { C::execution_public_data_tree_root, public_data_tree_after.root },
297 { C::execution_public_data_tree_size, public_data_tree_after.next_available_leaf_index },
298 { C::execution_prev_written_public_data_slots_tree_root, written_slots_tree_before.root },
299 { C::execution_prev_written_public_data_slots_tree_size,
300 written_slots_tree_before.next_available_leaf_index },
301 { C::execution_written_public_data_slots_tree_root, written_slots_tree_after.root },
302 { C::execution_written_public_data_slots_tree_size, written_slots_tree_after.next_available_leaf_index },
306 PublicDataTreeTraceBuilder public_data_tree_trace_builder;
307 public_data_tree_trace_builder.process(public_data_tree_check_event_emitter.dump_events(),
trace);
309 IndexedTreeCheckTraceBuilder written_slots_tree_trace_builder;
310 written_slots_tree_trace_builder.process(indexed_tree_check_emitter.dump_events(),
trace);
312 check_relation<sstore>(trace);
330TEST(SStoreConstrainingTest, NegativeFullAttackWithAllTraces)
333 NiceMock<MockFieldGreaterThan>
field_gt;
337 EventEmitter<IndexedTreeCheckEvent> indexed_tree_check_emitter;
339 poseidon2, merkle_check,
field_gt, DOM_SEP__WRITTEN_SLOTS_MERKLE, indexed_tree_check_emitter);
343 EventEmitter<PublicDataTreeCheckEvent> public_data_tree_check_event_emitter;
344 PublicDataTreeCheck public_data_tree_check(
354 uint64_t low_leaf_index = 30;
355 std::vector<FF> low_leaf_sibling_path = { 1, 2, 3, 4, 5 };
357 AppendOnlyTreeSnapshot public_data_tree_before = AppendOnlyTreeSnapshot{
359 .next_available_leaf_index = 128,
363 EXPECT_CALL(
poseidon2, hash(_)).WillRepeatedly([](
const std::vector<FF>&
inputs) {
366 EXPECT_CALL(
field_gt, ff_gt(_, _)).WillRepeatedly([](
const FF&
a,
const FF&
b) {
369 EXPECT_CALL(merkle_check,
write)
370 .WillRepeatedly([]([[maybe_unused]] uint64_t domain_separator,
371 [[maybe_unused]]
FF current_leaf,
375 [[maybe_unused]]
FF prev_root) {
381 auto public_data_tree_after = public_data_tree_check.write(
slot,
386 low_leaf_sibling_path,
387 public_data_tree_before,
394 TestTraceContainer
trace;
395 PublicDataTreeTraceBuilder public_data_tree_trace_builder;
396 public_data_tree_trace_builder.process(public_data_tree_check_event_emitter.dump_events(),
trace);
398 IndexedTreeCheckTraceBuilder written_slots_tree_trace_builder;
399 written_slots_tree_trace_builder.process(indexed_tree_check_emitter.dump_events(),
trace);
407 { C::execution_clk, 0 },
408 { C::precomputed_first_row, 1 },
409 { C::execution_sel_execute_sstore, 0 },
410 { C::execution_sel_write_public_data, 1 },
411 { C::execution_contract_address, contract_address },
412 { C::execution_register_0_,
value },
413 { C::execution_register_1_,
slot },
414 { C::execution_sel_opcode_error, 0 },
415 { C::execution_discard, 0 },
416 { C::execution_prev_public_data_tree_root, public_data_tree_before.root },
417 { C::execution_prev_public_data_tree_size, public_data_tree_before.next_available_leaf_index },
418 { C::execution_public_data_tree_root, public_data_tree_after.root },
419 { C::execution_public_data_tree_size, public_data_tree_after.next_available_leaf_index },
420 { C::execution_prev_written_public_data_slots_tree_root, written_slots_tree_before.root },
421 { C::execution_prev_written_public_data_slots_tree_size,
422 written_slots_tree_before.next_available_leaf_index },
423 { C::execution_written_public_data_slots_tree_root, written_slots_tree_after.root },
424 { C::execution_written_public_data_slots_tree_size, written_slots_tree_after.next_available_leaf_index },
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessageRegex)
FieldGreaterThan field_gt
IndexedTreeCheck indexed_tree_check
static constexpr size_t SR_DYN_L2_GAS_IS_ZERO
static std::string get_subrelation_label(size_t index)
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_SEL_WRITE_PUBLIC_DATA_IS_EXECUTE_AND_NOT_ERROR
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 std::string get_subrelation_label(size_t index)
static constexpr size_t SR_SSTORE_PUBLIC_DATA_TREE_ROOT_NOT_CHANGED
void set(Column col, uint32_t row, const FF &value, bool use_atomic_limbs=false)
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
std::variant< IndexedTreeReadWriteEvent, CheckPointEventType > IndexedTreeCheckEvent
::bb::crypto::merkle_tree::PublicDataLeafValue PublicDataLeafValue
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)
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