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 } });
84 EXPECT_THROW_WITH_MESSAGE(check_relation<execution>(trace, execution::SR_DYN_L2_GAS_IS_ZERO), "DYN_L2_GAS_IS_ZERO");
85}
86
87TEST(SStoreConstrainingTest, MaxDataWritesReached)
88{
89 TestTraceContainer trace({
90 {
91 { C::execution_sel_execute_sstore, 1 },
92 { C::execution_prev_written_public_data_slots_tree_size,
94 { C::execution_remaining_data_writes_inv, 0 },
95 { C::execution_max_data_writes_reached, 1 },
96 },
97 });
98 check_relation<sstore>(trace, sstore::SR_SSTORE_MAX_DATA_WRITES_REACHED);
99
100 trace.set(C::execution_max_data_writes_reached, 0, 0);
101
103 "SSTORE_MAX_DATA_WRITES_REACHED");
104}
105
106TEST(SStoreConstrainingTest, OpcodeError)
107{
108 TestTraceContainer trace({
109 {
110 { C::execution_sel_execute_sstore, 1 },
111 { C::execution_dynamic_da_gas_factor, 1 },
112 { C::execution_max_data_writes_reached, 1 },
113 { C::execution_sel_opcode_error, 1 },
114 },
115 {
116 { C::execution_sel_execute_sstore, 1 },
117 { C::execution_dynamic_da_gas_factor, 0 },
118 { C::execution_max_data_writes_reached, 0 },
119 { C::execution_is_static, 1 },
120 { C::execution_sel_opcode_error, 1 },
121 },
122 {
123 { C::execution_sel_execute_sstore, 1 },
124 { C::execution_dynamic_da_gas_factor, 0 },
125 { C::execution_max_data_writes_reached, 1 },
126 { C::execution_sel_opcode_error, 0 },
127 },
128 });
129 check_relation<sstore>(trace, sstore::SR_OPCODE_ERROR_IF_OVERFLOW_OR_STATIC);
130
131 trace.set(C::execution_dynamic_da_gas_factor, 0, 0);
132
134 "OPCODE_ERROR_IF_OVERFLOW_OR_STATIC");
135
136 trace.set(C::execution_dynamic_da_gas_factor, 0, 1);
137
138 trace.set(C::execution_is_static, 1, 0);
139
141 "OPCODE_ERROR_IF_OVERFLOW_OR_STATIC");
142}
143
144TEST(SStoreConstrainingTest, TreeStateNotChangedOnError)
145{
146 TestTraceContainer trace({ {
147 { C::execution_sel_execute_sstore, 1 },
148 { C::execution_prev_public_data_tree_root, 27 },
149 { C::execution_prev_public_data_tree_size, 5 },
150 { C::execution_prev_written_public_data_slots_tree_root, 28 },
151 { C::execution_prev_written_public_data_slots_tree_size, 6 },
152 { C::execution_public_data_tree_root, 27 },
153 { C::execution_public_data_tree_size, 5 },
154 { C::execution_written_public_data_slots_tree_root, 28 },
155 { C::execution_written_public_data_slots_tree_size, 6 },
156 { C::execution_sel_opcode_error, 1 },
157 } });
158
159 check_relation<sstore>(trace,
164
165 // Negative test: written slots tree root must be the same
166 trace.set(C::execution_written_public_data_slots_tree_root, 0, 29);
168 "SSTORE_WRITTEN_SLOTS_ROOT_NOT_CHANGED");
169
170 // Negative test: written slots tree size must be the same
171 trace.set(C::execution_written_public_data_slots_tree_size, 0, 7);
173 "SSTORE_WRITTEN_SLOTS_SIZE_NOT_CHANGED");
174
175 // Negative test: public data tree root must be the same
176 trace.set(C::execution_public_data_tree_root, 0, 29);
178 "SSTORE_PUBLIC_DATA_TREE_ROOT_NOT_CHANGED");
179
180 // Negative test: public data tree size must be the same
181 trace.set(C::execution_public_data_tree_size, 0, 7);
183 "SSTORE_PUBLIC_DATA_TREE_SIZE_NOT_CHANGED");
184}
185
186TEST(SStoreConstrainingTest, Interactions)
187{
188 NiceMock<MockPoseidon2> poseidon2;
189 NiceMock<MockFieldGreaterThan> field_gt;
190 NiceMock<MockMerkleCheck> merkle_check;
191 NiceMock<MockExecutionIdManager> execution_id_manager;
192
193 EventEmitter<WrittenPublicDataSlotsTreeCheckEvent> written_public_data_slots_emitter;
194 WrittenPublicDataSlotsTreeCheck written_public_data_slots_tree_check(
195 poseidon2, merkle_check, field_gt, build_public_data_slots_tree(), written_public_data_slots_emitter);
196
197 EventEmitter<PublicDataTreeCheckEvent> public_data_tree_check_event_emitter;
198 PublicDataTreeCheck public_data_tree_check(
199 poseidon2, merkle_check, field_gt, execution_id_manager, public_data_tree_check_event_emitter);
200
201 FF slot = 42;
204 FF value = 27;
205
207 uint64_t low_leaf_index = 30;
208 std::vector<FF> low_leaf_sibling_path = { 1, 2, 3, 4, 5 };
209
210 AppendOnlyTreeSnapshot public_data_tree_before = AppendOnlyTreeSnapshot{
211 .root = 42,
212 .next_available_leaf_index = 128,
213 };
214 AppendOnlyTreeSnapshot written_slots_tree_before = written_public_data_slots_tree_check.get_snapshot();
215
216 EXPECT_CALL(poseidon2, hash(_)).WillRepeatedly([](const std::vector<FF>& inputs) {
218 });
219 EXPECT_CALL(field_gt, ff_gt(_, _)).WillRepeatedly([](const FF& a, const FF& b) {
220 return static_cast<uint256_t>(a) > static_cast<uint256_t>(b);
221 });
222
223 EXPECT_CALL(merkle_check, write)
224 .WillRepeatedly([]([[maybe_unused]] FF current_leaf,
225 FF new_leaf,
226 uint64_t leaf_index,
227 std::span<const FF> sibling_path,
228 [[maybe_unused]] FF prev_root) {
229 return unconstrained_root_from_path(new_leaf, leaf_index, sibling_path);
230 });
231
233
234 auto public_data_tree_after = public_data_tree_check.write(slot,
236 value,
237 low_leaf,
238 low_leaf_index,
239 low_leaf_sibling_path,
240 public_data_tree_before,
241 {},
242 false);
244 auto written_slots_tree_after = written_public_data_slots_tree_check.get_snapshot();
245
246 TestTraceContainer trace({
247 {
248 { C::execution_sel_execute_sstore, 1 },
249 { C::execution_contract_address, contract_address },
250 { C::execution_sel_gas_sstore, 1 },
251 { C::execution_dynamic_da_gas_factor, 1 },
252 { C::execution_register_0_, value },
253 { C::execution_register_1_, slot },
254 { C::execution_max_data_writes_reached, 0 },
255 { C::execution_remaining_data_writes_inv,
257 written_slots_tree_before.next_available_leaf_index)
258 .invert() },
259 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_SSTORE },
260 { C::execution_sel_write_public_data, 1 },
261 { C::execution_prev_public_data_tree_root, public_data_tree_before.root },
262 { C::execution_prev_public_data_tree_size, public_data_tree_before.next_available_leaf_index },
263 { C::execution_public_data_tree_root, public_data_tree_after.root },
264 { C::execution_public_data_tree_size, public_data_tree_after.next_available_leaf_index },
265 { C::execution_prev_written_public_data_slots_tree_root, written_slots_tree_before.root },
266 { C::execution_prev_written_public_data_slots_tree_size,
267 written_slots_tree_before.next_available_leaf_index },
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.next_available_leaf_index },
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,
283 check_multipermutation_interaction<PublicDataTreeTraceBuilder,
286}
287
288} // namespace
289} // namespace bb::avm2::constraining
FieldGreaterThan field_gt
#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_DYN_L2_GAS_IS_ZERO
Definition execution.hpp:51
static constexpr size_t SR_SSTORE_WRITTEN_SLOTS_SIZE_NOT_CHANGED
Definition sstore.hpp:41
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:40
static constexpr size_t SR_SSTORE_PUBLIC_DATA_TREE_SIZE_NOT_CHANGED
Definition sstore.hpp:43
static constexpr size_t SR_SSTORE_PUBLIC_DATA_TREE_ROOT_NOT_CHANGED
Definition sstore.hpp:42
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
AvmProvingInputs inputs
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessage)
Definition macros.hpp:7
void hash(State &state) noexcept
void check_multipermutation_interaction(tracegen::TestTraceContainer &trace)
void check_interaction(tracegen::TestTraceContainer &trace)
TEST(TxExecutionConstrainingTest, WriteTreeValue)
Definition tx.test.cpp:441
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)
Definition merkle.cpp:12
::bb::crypto::merkle_tree::PublicDataLeafValue PublicDataLeafValue
Definition db.hpp:38
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
permutation_settings< perm_tx_balance_update_settings_ > perm_tx_balance_update_settings
Definition perms_tx.hpp:198
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
AvmFlavorSettings::FF FF
Definition field.hpp:10
void write(std::vector< uint8_t > &buf, Chonk::VerificationKey const &vk)
Definition chonk.hpp:366
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