Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
emit_unencrypted_log.test.cpp
Go to the documentation of this file.
2
3#include <gmock/gmock.h>
4#include <gtest/gtest.h>
5
11
12namespace bb::avm2::simulation {
13
14using ::testing::_;
15using ::testing::ElementsAre;
16using ::testing::Return;
17using ::testing::ReturnRef;
18using ::testing::StrictMock;
19
20TEST(EmitUnencryptedLogTest, Basic)
21{
22 StrictMock<MockMemory> memory;
23 StrictMock<MockContext> context;
24 StrictMock<MockGreaterThan> greater_than;
25 StrictMock<MockExecutionIdManager> execution_id_manager;
27
28 AztecAddress address = 0xdeadbeef;
29 MemoryAddress log_offset = 27;
30 uint32_t log_size = 2;
31 uint64_t end_log_address = 28;
32 SideEffectStates side_effect_states = { .numUnencryptedLogs = 0 };
33 SideEffectStates next_side_effect_states = { .numUnencryptedLogs = 1 };
34
36
37 EXPECT_CALL(execution_id_manager, get_execution_id()).WillOnce(Return(1));
38 EXPECT_CALL(greater_than, gt(log_size, PUBLIC_LOG_SIZE_IN_FIELDS)).WillOnce(Return(false));
39 EXPECT_CALL(greater_than, gt(end_log_address, AVM_HIGHEST_MEM_ADDRESS)).WillOnce(Return(false));
40
41 EXPECT_CALL(context, get_side_effect_states()).WillOnce(ReturnRef(side_effect_states));
42 EXPECT_CALL(context, set_side_effect_states(next_side_effect_states));
43
44 EXPECT_CALL(memory, get(_)).WillRepeatedly([](MemoryAddress address) -> const MemoryValue& {
45 static thread_local MemoryValue value;
46 value = MemoryValue::from<FF>(FF(address));
47 return value;
48 });
49
50 EXPECT_CALL(memory, get_space_id()).WillOnce(Return(57));
51 EXPECT_CALL(context, get_is_static()).WillOnce(Return(false));
52
53 emit_unencrypted_log.emit_unencrypted_log(memory, context, address, log_offset, log_size);
54
55 EmitUnencryptedLogWriteEvent expect_event = {
56 .execution_clk = 1,
57 .contract_address = address,
58 .space_id = 57,
59 .log_address = log_offset,
60 .log_size = log_size,
61 .prev_num_unencrypted_logs = side_effect_states.numUnencryptedLogs,
62 .next_num_unencrypted_logs = next_side_effect_states.numUnencryptedLogs,
63 .is_static = false,
64 .values = { MemoryValue::from<FF>(FF(log_offset)), MemoryValue::from<FF>(FF(log_offset + 1)) },
65 .error_too_large = false,
66 .error_memory_out_of_bounds = false,
67 .error_too_many_logs = false,
68 .error_tag_mismatch = false,
69 };
70
71 EXPECT_THAT(event_emitter.dump_events(), ElementsAre(expect_event));
72}
73
74TEST(EmitUnencryptedLogTest, NegativeTooLarge)
75{
76 StrictMock<MockMemory> memory;
77 StrictMock<MockContext> context;
78 StrictMock<MockGreaterThan> greater_than;
79 StrictMock<MockExecutionIdManager> execution_id_manager;
81
82 AztecAddress address = 0xdeadbeef;
83 MemoryAddress log_offset = 27;
84 uint32_t log_size = PUBLIC_LOG_SIZE_IN_FIELDS + 1;
85 uint64_t end_log_address = log_offset + log_size - 1;
86 SideEffectStates side_effect_states = { .numUnencryptedLogs = 0 };
87 SideEffectStates next_side_effect_states = { .numUnencryptedLogs = 0 };
88
90
91 EXPECT_CALL(execution_id_manager, get_execution_id()).WillOnce(Return(1));
92 EXPECT_CALL(greater_than, gt(log_size, PUBLIC_LOG_SIZE_IN_FIELDS)).WillOnce(Return(true));
93 EXPECT_CALL(greater_than, gt(end_log_address, AVM_HIGHEST_MEM_ADDRESS)).WillOnce(Return(false));
94
95 EXPECT_CALL(context, get_side_effect_states()).WillOnce(ReturnRef(side_effect_states));
96 EXPECT_CALL(context, set_side_effect_states(next_side_effect_states));
97
98 EXPECT_CALL(memory, get(_)).WillRepeatedly([](MemoryAddress address) -> const MemoryValue& {
99 static thread_local MemoryValue value;
100 value = MemoryValue::from<FF>(FF(address));
101 return value;
102 });
103
104 EXPECT_CALL(memory, get_space_id()).WillOnce(Return(57));
105 EXPECT_CALL(context, get_is_static()).WillOnce(Return(false));
106
107 EXPECT_THROW(emit_unencrypted_log.emit_unencrypted_log(memory, context, address, log_offset, log_size),
109
110 std::vector<MemoryValue> expected_values;
111
112 for (uint32_t i = 0; i < PUBLIC_LOG_SIZE_IN_FIELDS; ++i) {
113 expected_values.push_back(MemoryValue::from<FF>(FF(log_offset + i)));
114 }
115
116 EmitUnencryptedLogWriteEvent expect_event = {
117 .execution_clk = 1,
118 .contract_address = address,
119 .space_id = 57,
120 .log_address = log_offset,
121 .log_size = log_size,
122 .prev_num_unencrypted_logs = side_effect_states.numUnencryptedLogs,
123 .next_num_unencrypted_logs = next_side_effect_states.numUnencryptedLogs,
124 .is_static = false,
125 .values = expected_values,
126 .error_too_large = true,
127 .error_memory_out_of_bounds = false,
128 .error_too_many_logs = false,
129 .error_tag_mismatch = false,
130 };
131
132 EXPECT_THAT(event_emitter.dump_events(), ElementsAre(expect_event));
133}
134
135TEST(EmitUnencryptedLogTest, NegativeMemoryOutOfBounds)
136{
137 StrictMock<MockMemory> memory;
138 StrictMock<MockContext> context;
139 StrictMock<MockGreaterThan> greater_than;
140 StrictMock<MockExecutionIdManager> execution_id_manager;
142
143 AztecAddress address = 0xdeadbeef;
145 uint32_t log_size = 2;
146 uint64_t end_log_address = static_cast<uint64_t>(log_offset) + log_size - 1;
147 SideEffectStates side_effect_states = { .numUnencryptedLogs = 0 };
148 SideEffectStates next_side_effect_states = { .numUnencryptedLogs = 0 };
149
151
152 EXPECT_CALL(execution_id_manager, get_execution_id()).WillOnce(Return(1));
153 EXPECT_CALL(greater_than, gt(log_size, PUBLIC_LOG_SIZE_IN_FIELDS)).WillOnce(Return(false));
154 EXPECT_CALL(greater_than, gt(end_log_address, AVM_HIGHEST_MEM_ADDRESS)).WillOnce(Return(true));
155
156 EXPECT_CALL(context, get_side_effect_states()).WillOnce(ReturnRef(side_effect_states));
157 EXPECT_CALL(context, set_side_effect_states(next_side_effect_states));
158
159 EXPECT_CALL(memory, get_space_id()).WillOnce(Return(57));
160 EXPECT_CALL(context, get_is_static()).WillOnce(Return(false));
161
162 EXPECT_THROW(emit_unencrypted_log.emit_unencrypted_log(memory, context, address, log_offset, log_size),
164
165 EmitUnencryptedLogWriteEvent expect_event = {
166 .execution_clk = 1,
167 .contract_address = address,
168 .space_id = 57,
169 .log_address = log_offset,
170 .log_size = log_size,
171 .prev_num_unencrypted_logs = side_effect_states.numUnencryptedLogs,
172 .next_num_unencrypted_logs = next_side_effect_states.numUnencryptedLogs,
173 .is_static = false,
174 .values = {},
175 .error_too_large = false,
176 .error_memory_out_of_bounds = true,
177 .error_too_many_logs = false,
178 .error_tag_mismatch = false,
179 };
180
181 EXPECT_THAT(event_emitter.dump_events(), ElementsAre(expect_event));
182}
183
184TEST(EmitUnencryptedLogTest, NegativeTooManyLogs)
185{
186 StrictMock<MockMemory> memory;
187 StrictMock<MockContext> context;
188 StrictMock<MockGreaterThan> greater_than;
189 StrictMock<MockExecutionIdManager> execution_id_manager;
191
192 AztecAddress address = 0xdeadbeef;
193 MemoryAddress log_offset = 27;
194 uint32_t log_size = 2;
195 uint64_t end_log_address = 28;
197 SideEffectStates next_side_effect_states = { .numUnencryptedLogs = MAX_PUBLIC_LOGS_PER_TX };
198
200
201 EXPECT_CALL(execution_id_manager, get_execution_id()).WillOnce(Return(1));
202 EXPECT_CALL(greater_than, gt(log_size, PUBLIC_LOG_SIZE_IN_FIELDS)).WillOnce(Return(false));
203 EXPECT_CALL(greater_than, gt(end_log_address, AVM_HIGHEST_MEM_ADDRESS)).WillOnce(Return(false));
204
205 EXPECT_CALL(context, get_side_effect_states()).WillOnce(ReturnRef(side_effect_states));
206 EXPECT_CALL(context, set_side_effect_states(next_side_effect_states));
207
208 EXPECT_CALL(memory, get(_)).WillRepeatedly([](MemoryAddress address) -> const MemoryValue& {
209 static thread_local MemoryValue value;
210 value = MemoryValue::from<FF>(FF(address));
211 return value;
212 });
213
214 EXPECT_CALL(memory, get_space_id()).WillOnce(Return(57));
215 EXPECT_CALL(context, get_is_static()).WillOnce(Return(false));
216
217 EXPECT_THROW(emit_unencrypted_log.emit_unencrypted_log(memory, context, address, log_offset, log_size),
219
220 EmitUnencryptedLogWriteEvent expect_event = {
221 .execution_clk = 1,
222 .contract_address = address,
223 .space_id = 57,
224 .log_address = log_offset,
225 .log_size = log_size,
226 .prev_num_unencrypted_logs = side_effect_states.numUnencryptedLogs,
227 .next_num_unencrypted_logs = next_side_effect_states.numUnencryptedLogs,
228 .is_static = false,
229 .values = { MemoryValue::from<FF>(FF(log_offset)), MemoryValue::from<FF>(FF(log_offset + 1)) },
230 .error_too_large = false,
231 .error_memory_out_of_bounds = false,
232 .error_too_many_logs = true,
233 .error_tag_mismatch = false,
234 };
235
236 EXPECT_THAT(event_emitter.dump_events(), ElementsAre(expect_event));
237}
238
239TEST(EmitUnencryptedLogTest, NegativeTagMismatch)
240{
241 StrictMock<MockMemory> memory;
242 StrictMock<MockContext> context;
243 StrictMock<MockGreaterThan> greater_than;
244 StrictMock<MockExecutionIdManager> execution_id_manager;
246
247 AztecAddress address = 0xdeadbeef;
248 MemoryAddress log_offset = 27;
249 uint32_t log_size = 2;
250 uint64_t end_log_address = 28;
251 SideEffectStates side_effect_states = { .numUnencryptedLogs = 0 };
252 SideEffectStates next_side_effect_states = { .numUnencryptedLogs = 0 };
253
255
256 EXPECT_CALL(execution_id_manager, get_execution_id()).WillOnce(Return(1));
257 EXPECT_CALL(greater_than, gt(log_size, PUBLIC_LOG_SIZE_IN_FIELDS)).WillOnce(Return(false));
258 EXPECT_CALL(greater_than, gt(end_log_address, AVM_HIGHEST_MEM_ADDRESS)).WillOnce(Return(false));
259
260 EXPECT_CALL(context, get_side_effect_states()).WillOnce(ReturnRef(side_effect_states));
261 EXPECT_CALL(context, set_side_effect_states(next_side_effect_states));
262
263 EXPECT_CALL(memory, get(_)).WillRepeatedly([](MemoryAddress address) -> const MemoryValue& {
264 static thread_local MemoryValue value;
265 value = MemoryValue::from<uint32_t>(address);
266 return value;
267 });
268
269 EXPECT_CALL(memory, get_space_id()).WillOnce(Return(57));
270 EXPECT_CALL(context, get_is_static()).WillOnce(Return(false));
271
272 EXPECT_THROW(emit_unencrypted_log.emit_unencrypted_log(memory, context, address, log_offset, log_size),
274
275 EmitUnencryptedLogWriteEvent expect_event = {
276 .execution_clk = 1,
277 .contract_address = address,
278 .space_id = 57,
279 .log_address = log_offset,
280 .log_size = log_size,
281 .prev_num_unencrypted_logs = side_effect_states.numUnencryptedLogs,
282 .next_num_unencrypted_logs = next_side_effect_states.numUnencryptedLogs,
283 .is_static = false,
284 .values = { MemoryValue::from<uint32_t>(log_offset), MemoryValue::from<uint32_t>(log_offset + 1) },
285 .error_too_large = false,
286 .error_memory_out_of_bounds = false,
287 .error_too_many_logs = false,
288 .error_tag_mismatch = true,
289 };
290
291 EXPECT_THAT(event_emitter.dump_events(), ElementsAre(expect_event));
292}
293
294TEST(EmitUnencryptedLogTest, NegativeStatic)
295{
296 StrictMock<MockMemory> memory;
297 StrictMock<MockContext> context;
298 StrictMock<MockGreaterThan> greater_than;
299 StrictMock<MockExecutionIdManager> execution_id_manager;
301
302 AztecAddress address = 0xdeadbeef;
303 MemoryAddress log_offset = 27;
304 uint32_t log_size = 2;
305 uint64_t end_log_address = 28;
306 SideEffectStates side_effect_states = { .numUnencryptedLogs = 0 };
307 SideEffectStates next_side_effect_states = { .numUnencryptedLogs = 0 };
308
310
311 EXPECT_CALL(execution_id_manager, get_execution_id()).WillOnce(Return(1));
312 EXPECT_CALL(greater_than, gt(log_size, PUBLIC_LOG_SIZE_IN_FIELDS)).WillOnce(Return(false));
313 EXPECT_CALL(greater_than, gt(end_log_address, AVM_HIGHEST_MEM_ADDRESS)).WillOnce(Return(false));
314
315 EXPECT_CALL(context, get_side_effect_states()).WillOnce(ReturnRef(side_effect_states));
316 EXPECT_CALL(context, set_side_effect_states(next_side_effect_states));
317
318 EXPECT_CALL(memory, get(_)).WillRepeatedly([](MemoryAddress address) -> const MemoryValue& {
319 static thread_local MemoryValue value;
320 value = MemoryValue::from<FF>(FF(address));
321 return value;
322 });
323
324 EXPECT_CALL(memory, get_space_id()).WillOnce(Return(57));
325 EXPECT_CALL(context, get_is_static()).WillOnce(Return(true));
326
327 EXPECT_THROW(emit_unencrypted_log.emit_unencrypted_log(memory, context, address, log_offset, log_size),
329
330 EmitUnencryptedLogWriteEvent expect_event = {
331 .execution_clk = 1,
332 .contract_address = address,
333 .space_id = 57,
334 .log_address = log_offset,
335 .log_size = log_size,
336 .prev_num_unencrypted_logs = side_effect_states.numUnencryptedLogs,
337 .next_num_unencrypted_logs = next_side_effect_states.numUnencryptedLogs,
338 .is_static = true,
339 .values = { MemoryValue::from<FF>(FF(log_offset)), MemoryValue::from<FF>(FF(log_offset + 1)) },
340 .error_too_large = false,
341 .error_memory_out_of_bounds = false,
342 .error_too_many_logs = false,
343 .error_tag_mismatch = false,
344 };
345
346 EXPECT_THAT(event_emitter.dump_events(), ElementsAre(expect_event));
347}
348
349TEST(EmitUnencryptedLogTest, CheckpointListener)
350{
351 StrictMock<MockMemory> memory;
352 StrictMock<MockContext> context;
353 StrictMock<MockGreaterThan> greater_than;
354 StrictMock<MockExecutionIdManager> execution_id_manager;
357
358 emit_unencrypted_log.on_checkpoint_created();
359 emit_unencrypted_log.on_checkpoint_committed();
360 emit_unencrypted_log.on_checkpoint_reverted();
361 EXPECT_THAT(event_emitter.get_events().size(), 3);
362 EXPECT_THAT(event_emitter.dump_events(),
366}
367
368} // namespace bb::avm2::simulation
#define MAX_PUBLIC_LOGS_PER_TX
#define AVM_HIGHEST_MEM_ADDRESS
#define PUBLIC_LOG_SIZE_IN_FIELDS
ExecutionIdManager execution_id_manager
EventEmitter< DataCopyEvent > event_emitter
TEST(EmitUnencryptedLogTest, Basic)
uint32_t MemoryAddress
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13