Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
execution.test.cpp
Go to the documentation of this file.
2
3#include <gmock/gmock.h>
4#include <gtest/gtest.h>
5
6#include <memory>
7
46
47namespace bb::avm2::simulation {
48namespace {
49
50using ::testing::_;
51using ::testing::NiceMock;
52using ::testing::Return;
53using ::testing::ReturnRef;
54using ::testing::StrictMock;
55using ::testing::Throw;
56
57// TODO(fcarreiro): This is a hack to get the gas tracker for testing.
58class TestingExecution : public Execution {
59 public:
60 using Execution::Execution;
61
62 void set_gas_tracker(GasTrackerInterface& gas_tracker) { this->testing_gas_tracker = &gas_tracker; }
63
64 GasTrackerInterface& get_gas_tracker() override { return *testing_gas_tracker; }
65
66 private:
67 GasTrackerInterface* testing_gas_tracker;
68};
69
70class ExecutionSimulationTest : public ::testing::Test {
71 protected:
72 ExecutionSimulationTest()
73 {
74 ON_CALL(context, get_memory()).WillByDefault(ReturnRef(memory));
75 ON_CALL(context, get_bytecode_manager).WillByDefault(ReturnRef(bytecode_manager));
76 ON_CALL(context, get_side_effect_tracker).WillByDefault(ReturnRef(side_effect_tracker));
77 execution.set_gas_tracker(gas_tracker);
78 }
79
80 StrictMock<MockAlu> alu;
81 StrictMock<MockBitwise> bitwise;
82 StrictMock<MockMemory> memory;
83 StrictMock<MockExecutionComponentsProvider> execution_components;
84 StrictMock<MockContext> context;
85 StrictMock<MockDataCopy> data_copy;
86 StrictMock<MockInternalCallStackManager> internal_call_stack_manager;
87 StrictMock<MockKeccakF1600> keccakf1600;
88 StrictMock<MockGetContractInstance> get_contract_instance;
89 EventEmitter<ExecutionEvent> execution_event_emitter;
90 EventEmitter<ContextStackEvent> context_stack_event_emitter;
91 InstructionInfoDB instruction_info_db; // Using the real thing.
92 StrictMock<MockContextProvider> context_provider;
93 StrictMock<MockExecutionIdManager> execution_id_manager;
94 StrictMock<MockGasTracker> gas_tracker;
95 StrictMock<MockHighLevelMerkleDB> merkle_db;
96 StrictMock<MockGreaterThan> greater_than;
97 StrictMock<MockPoseidon2> poseidon2;
98 StrictMock<MockEcc> ecc;
99 StrictMock<MockToRadix> to_radix;
100 StrictMock<MockEmitUnencryptedLog> emit_unencrypted_log;
101 StrictMock<MockBytecodeManager> bytecode_manager;
102 StrictMock<MockSha256> sha256;
103 StrictMock<MockDebugLog> debug_log;
104 StrictMock<MockSideEffectTracker> side_effect_tracker;
105 StrictMock<MockCallStackMetadataCollector> call_stack_metadata_collector;
106 TestingExecution execution = TestingExecution(alu,
107 bitwise,
108 data_copy,
109 poseidon2,
110 ecc,
111 to_radix,
112 sha256,
113 execution_components,
117 execution_event_emitter,
118 context_stack_event_emitter,
119 keccakf1600,
121 get_contract_instance,
122 emit_unencrypted_log,
123 debug_log,
124 merkle_db,
126};
127
128// NOTE: MemoryAddresses x, y used in the below tests like: execution.fn(context, x, y, ..) are just unchecked arbitrary
129// addresses. We test the MemoryValues and destination addresses.
130
131TEST_F(ExecutionSimulationTest, Add)
132{
133 MemoryValue a = MemoryValue::from<uint32_t>(4);
134 MemoryValue b = MemoryValue::from<uint32_t>(5);
135
136 EXPECT_CALL(context, get_memory());
137 EXPECT_CALL(memory, get).Times(2).WillOnce(ReturnRef(a)).WillOnce(ReturnRef(b));
138 EXPECT_CALL(alu, add(a, b)).WillOnce(Return(MemoryValue::from<uint32_t>(9)));
139 EXPECT_CALL(memory, set(6, MemoryValue::from<uint32_t>(9)));
140 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
141
142 execution.add(context, 4, 5, 6);
143}
144
145TEST_F(ExecutionSimulationTest, Sub)
146{
147 MemoryValue a = MemoryValue::from<uint64_t>(5);
148 MemoryValue b = MemoryValue::from<uint64_t>(3);
149
150 EXPECT_CALL(context, get_memory());
151 EXPECT_CALL(memory, get).Times(2).WillOnce(ReturnRef(a)).WillOnce(ReturnRef(b));
152 EXPECT_CALL(alu, sub(a, b)).WillOnce(Return(MemoryValue::from<uint64_t>(2)));
153 EXPECT_CALL(memory, set(3, MemoryValue::from<uint64_t>(2)));
154 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
155
156 execution.sub(context, 1, 2, 3);
157}
158
159TEST_F(ExecutionSimulationTest, Mul)
160{
162 auto a = MemoryValue::from<uint128_t>(max);
163 auto b = MemoryValue::from<uint128_t>(max - 3);
164
165 EXPECT_CALL(context, get_memory());
166 EXPECT_CALL(memory, get).Times(2).WillOnce(ReturnRef(a)).WillOnce(ReturnRef(b));
167 EXPECT_CALL(alu, mul(a, b)).WillOnce(Return(MemoryValue::from<uint128_t>(4)));
168 EXPECT_CALL(memory, set(3, MemoryValue::from<uint128_t>(4)));
169 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
170
171 execution.mul(context, 1, 2, 3);
172}
173
174TEST_F(ExecutionSimulationTest, Div)
175{
176 auto a = MemoryValue::from<uint128_t>(6);
177 auto b = MemoryValue::from<uint128_t>(3);
178
179 EXPECT_CALL(context, get_memory());
180 EXPECT_CALL(memory, get).Times(2).WillOnce(ReturnRef(a)).WillOnce(ReturnRef(b));
181 EXPECT_CALL(alu, div(a, b)).WillOnce(Return(MemoryValue::from<uint128_t>(2)));
182 EXPECT_CALL(memory, set(3, MemoryValue::from<uint128_t>(2)));
183 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
184
185 execution.div(context, 1, 2, 3);
186}
187
188TEST_F(ExecutionSimulationTest, FDiv)
189{
190 auto a = MemoryValue::from<FF>(FF::modulus - 4);
191 auto b = MemoryValue::from<FF>(2);
192
193 EXPECT_CALL(context, get_memory());
194 EXPECT_CALL(memory, get).Times(2).WillOnce(ReturnRef(a)).WillOnce(ReturnRef(b));
195 EXPECT_CALL(alu, fdiv(a, b)).WillOnce(Return(MemoryValue::from<FF>(FF::modulus - 2)));
196 EXPECT_CALL(memory, set(3, MemoryValue::from<FF>(FF::modulus - 2)));
197 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
198
199 execution.fdiv(context, 1, 2, 3);
200}
201
202TEST_F(ExecutionSimulationTest, Shl)
203{
204 auto a = MemoryValue::from<uint32_t>(64);
205 auto b = MemoryValue::from<uint32_t>(2);
206
207 EXPECT_CALL(context, get_memory());
208 EXPECT_CALL(memory, get).Times(2).WillOnce(ReturnRef(a)).WillOnce(ReturnRef(b));
209 EXPECT_CALL(alu, shl(a, b)).WillOnce(Return(MemoryValue::from<uint32_t>(256)));
210 EXPECT_CALL(memory, set(3, MemoryValue::from<uint32_t>(256)));
211 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
212
213 execution.shl(context, 1, 2, 3);
214}
215
216TEST_F(ExecutionSimulationTest, Shr)
217{
218 auto a = MemoryValue::from<uint64_t>(64);
219 auto b = MemoryValue::from<uint64_t>(2);
220
221 EXPECT_CALL(context, get_memory());
222 EXPECT_CALL(memory, get).Times(2).WillOnce(ReturnRef(a)).WillOnce(ReturnRef(b));
223 EXPECT_CALL(alu, shr(a, b)).WillOnce(Return(MemoryValue::from<uint64_t>(16)));
224 EXPECT_CALL(memory, set(3, MemoryValue::from<uint64_t>(16)));
225 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
226
227 execution.shr(context, 1, 2, 3);
228}
229
230// TODO(MW): Add alu tests here for other ops
231
232TEST_F(ExecutionSimulationTest, Call)
233{
234 FF zero = 0;
235 AztecAddress parent_address = 0xdeadbeef;
236 AztecAddress nested_address = 0xc0ffee;
237 uint32_t parent_pc = 100;
238 MemoryValue nested_address_value = MemoryValue::from<FF>(nested_address);
239 MemoryValue l2_gas_allocated = MemoryValue::from<uint32_t>(6);
240 MemoryValue da_gas_allocated = MemoryValue::from<uint32_t>(7);
241 MemoryValue cd_size = MemoryValue::from<uint32_t>(8);
242 AppendOnlyTreeSnapshot written_public_data_slots_tree_snapshot = AppendOnlyTreeSnapshot{
243 .root = 0x12345678,
244 .next_available_leaf_index = 10,
245 };
246 TreeStates tree_states = TreeStates {
247 .note_hash_tree = {
248 .tree = {
249 .root = 10,
250 .next_available_leaf_index = 9,
251 },
252 .counter = 8,
253 },
254 .nullifier_tree = {
255 .tree = {
256 .root = 7,
257 .next_available_leaf_index = 6,
258 },
259 .counter = 5,
260 },
261 .l1_to_l2_message_tree = {
262 .tree = {
263 .root = 4,
264 .next_available_leaf_index = 3,
265 },
266 .counter = 0,
267 },
268 .public_data_tree = {
269 .tree = {
270 .root = 2,
271 .next_available_leaf_index = 1,
272 },
273 .counter = 1,
274 }
275 };
276 TrackedSideEffects side_effect_states = {
277 .l2_to_l1_messages = { {
278 .message = { .recipient = EthAddress(0x12345678), .content = 0x12345678 },
279 .contract_address = parent_address,
280 },
281 {
282 .message = { .recipient = EthAddress(0x333333), .content = 0x12345678 },
283 .contract_address = parent_address,
284 } },
285 .public_logs = PublicLogs{ { { { 4 }, parent_address } } },
286 };
287
288 EXPECT_CALL(gas_tracker, compute_gas_limit_for_call(Gas{ 6, 7 })).WillOnce(Return(Gas{ 2, 3 }));
289 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
290
291 // Call stack metadata collector
292 EXPECT_CALL(context, get_pc).WillOnce(Return(parent_pc));
293 EXPECT_CALL(call_stack_metadata_collector, notify_enter_call(nested_address, parent_pc, _, false, Gas{ 2, 3 }));
294
295 // Context snapshotting
296 EXPECT_CALL(context, get_context_id);
297 EXPECT_CALL(context, get_parent_id);
298 EXPECT_CALL(context, get_bytecode_manager).WillOnce(ReturnRef(bytecode_manager));
299 EXPECT_CALL(bytecode_manager, get_retrieved_bytecode_id).WillOnce(Return(FF(1)));
300 EXPECT_CALL(context, get_next_pc);
301 EXPECT_CALL(context, get_is_static).WillRepeatedly(Return(false));
302 EXPECT_CALL(context, get_msg_sender).WillOnce(ReturnRef(parent_address));
303 EXPECT_CALL(context, get_transaction_fee).WillOnce(ReturnRef(zero));
304 EXPECT_CALL(context, get_parent_cd_addr);
305 EXPECT_CALL(context, get_parent_cd_size);
306 EXPECT_CALL(context, get_parent_gas_used);
307 EXPECT_CALL(context, get_parent_gas_limit);
308 EXPECT_CALL(context, get_written_public_data_slots_tree_snapshot)
309 .WillOnce(Return(written_public_data_slots_tree_snapshot));
310 EXPECT_CALL(context, get_side_effect_tracker);
311 EXPECT_CALL(side_effect_tracker, get_side_effects()).WillRepeatedly(ReturnRef(side_effect_states));
312
313 EXPECT_CALL(context, get_phase).WillOnce(Return(TransactionPhase::APP_LOGIC));
314
315 EXPECT_CALL(merkle_db, get_tree_state).WillOnce(Return(tree_states));
316
317 EXPECT_CALL(context, get_memory());
318 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(parent_address));
319 EXPECT_CALL(memory, get(1)).WillOnce(ReturnRef(l2_gas_allocated)); // l2_gas_offset
320 EXPECT_CALL(memory, get(2)).WillOnce(ReturnRef(da_gas_allocated)); // da_gas_offset
321 EXPECT_CALL(memory, get(3)).WillOnce(ReturnRef(nested_address_value)); // contract_address
322 EXPECT_CALL(memory, get(4)).WillOnce(ReturnRef(cd_size)); // cd_size
323
324 auto nested_context = std::make_unique<NiceMock<MockContext>>();
325 ON_CALL(*nested_context, halted())
326 .WillByDefault(Return(true)); // We just want the recursive call to return immediately.
327 // Call stack metadata collector
328 EXPECT_CALL(*nested_context, get_address).WillOnce(ReturnRef(nested_address));
329 EXPECT_CALL(*nested_context, get_is_static).WillOnce(Return(false));
330 EXPECT_CALL(*nested_context, get_gas_limit).WillOnce(Return(Gas{ 2, 3 }));
331
332 EXPECT_CALL(
334 make_nested_context(nested_address, parent_address, _, _, _, _, _, Gas{ 2, 3 }, TransactionPhase::APP_LOGIC))
335 .WillOnce(Return(std::move(nested_context)));
336
337 execution.call(context,
338 /*l2_gas_offset=*/1,
339 /*da_gas_offset=*/2,
340 /*addr=*/3,
341 /*cd_size_offset=*/4,
342 /*cd_offset=*/5);
343}
344
345// Test staticness propagation for external calls (CALL vs STATICCALL)
346TEST_F(ExecutionSimulationTest, ExternalCallStaticnessPropagation)
347{
348 // Common test data setup
349 FF zero = 0;
350 AztecAddress parent_address = 0xdeadbeef;
351 AztecAddress nested_address = 0xc0ffee;
352 MemoryValue nested_address_value = MemoryValue::from<FF>(nested_address);
353 MemoryValue l2_gas_allocated = MemoryValue::from<uint32_t>(6);
354 MemoryValue da_gas_allocated = MemoryValue::from<uint32_t>(7);
355 MemoryValue cd_size = MemoryValue::from<uint32_t>(8);
356 AppendOnlyTreeSnapshot written_public_data_slots_tree_snapshot = AppendOnlyTreeSnapshot{
357 .root = 0x12345678,
358 .next_available_leaf_index = 10,
359 };
360 TreeStates tree_states =
361 TreeStates{ .note_hash_tree = { .tree = { .root = 10, .next_available_leaf_index = 9 }, .counter = 8 },
362 .nullifier_tree = { .tree = { .root = 7, .next_available_leaf_index = 6 }, .counter = 5 },
363 .l1_to_l2_message_tree = { .tree = { .root = 4, .next_available_leaf_index = 3 }, .counter = 0 },
364 .public_data_tree = { .tree = { .root = 2, .next_available_leaf_index = 1 }, .counter = 1 } };
365 TrackedSideEffects side_effect_states = {
366 .l2_to_l1_messages = { {
367 .message = { .recipient = EthAddress(0x12345678), .content = 0x12345678 },
368 .contract_address = parent_address,
369 },
370 {
371 .message = { .recipient = EthAddress(0x333333), .content = 0x12345678 },
372 .contract_address = parent_address,
373 } },
374 .public_logs = PublicLogs{ { { { 4 }, parent_address } } },
375 };
376
377 auto setup_context_expectations = [&](bool parent_is_static) {
378 EXPECT_CALL(call_stack_metadata_collector, notify_enter_call);
379 EXPECT_CALL(gas_tracker, compute_gas_limit_for_call(Gas{ 6, 7 })).WillOnce(Return(Gas{ 2, 3 }));
380 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
381 EXPECT_CALL(context, get_pc).WillOnce(Return(100));
382 EXPECT_CALL(context, get_context_id);
383 EXPECT_CALL(context, get_parent_id);
384 EXPECT_CALL(context, get_bytecode_manager).WillOnce(ReturnRef(bytecode_manager));
385 EXPECT_CALL(bytecode_manager, get_retrieved_bytecode_id).WillOnce(Return(FF(1)));
386 EXPECT_CALL(context, get_next_pc);
387 EXPECT_CALL(context, get_is_static).WillRepeatedly(Return(parent_is_static));
388 EXPECT_CALL(context, get_msg_sender).WillOnce(ReturnRef(parent_address));
389 EXPECT_CALL(context, get_transaction_fee).WillOnce(ReturnRef(zero));
390 EXPECT_CALL(context, get_parent_cd_addr);
391 EXPECT_CALL(context, get_parent_cd_size);
392 EXPECT_CALL(context, get_parent_gas_used);
393 EXPECT_CALL(context, get_parent_gas_limit);
394 EXPECT_CALL(context, get_written_public_data_slots_tree_snapshot)
395 .WillOnce(Return(written_public_data_slots_tree_snapshot));
396 EXPECT_CALL(context, get_side_effect_tracker);
397 EXPECT_CALL(side_effect_tracker, get_side_effects()).WillRepeatedly(ReturnRef(side_effect_states));
398 EXPECT_CALL(context, get_phase).WillOnce(Return(TransactionPhase::APP_LOGIC));
399 EXPECT_CALL(merkle_db, get_tree_state).WillOnce(Return(tree_states));
400 EXPECT_CALL(context, get_memory());
401 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(parent_address));
402 EXPECT_CALL(memory, get(1)).WillOnce(ReturnRef(l2_gas_allocated));
403 EXPECT_CALL(memory, get(2)).WillOnce(ReturnRef(da_gas_allocated));
404 EXPECT_CALL(memory, get(3)).WillOnce(ReturnRef(nested_address_value));
405 EXPECT_CALL(memory, get(4)).WillOnce(ReturnRef(cd_size));
406 };
407
408 auto create_nested_context = [&]() {
410 ON_CALL(*nested, halted()).WillByDefault(Return(true));
411 // Call stack metadata collector
412 EXPECT_CALL(*nested, get_address).WillOnce(ReturnRef(nested_address));
413 EXPECT_CALL(*nested, get_is_static).WillOnce(Return(false));
414 EXPECT_CALL(*nested, get_gas_limit).WillOnce(Return(Gas{ 2, 3 }));
415 return nested;
416 };
417
418 // Test Case 1: Non-static context + CALL -> nested context is non-static
419 setup_context_expectations(false);
420 EXPECT_CALL(context_provider,
421 make_nested_context(nested_address,
422 parent_address,
423 _,
424 _,
425 _,
426 _,
427 /*is_static=*/false,
428 Gas{ 2, 3 },
430 .WillOnce(Return(create_nested_context()));
431 execution.call(context, 1, 2, 3, 4, 5);
432
433 // Test Case 2: Non-static context + STATICCALL -> nested context is static
434 setup_context_expectations(false);
435 EXPECT_CALL(context_provider,
436 make_nested_context(nested_address,
437 parent_address,
438 _,
439 _,
440 _,
441 _,
442 /*is_static=*/true,
443 Gas{ 2, 3 },
445 .WillOnce(Return(create_nested_context()));
446 execution.static_call(context, 1, 2, 3, 4, 5);
447
448 // Test Case 3: Static context + CALL -> nested context remains static
449 setup_context_expectations(true);
450 EXPECT_CALL(context_provider,
451 make_nested_context(nested_address,
452 parent_address,
453 _,
454 _,
455 _,
456 _,
457 /*is_static=*/true,
458 Gas{ 2, 3 },
460 .WillOnce(Return(create_nested_context()));
461 execution.call(context, 1, 2, 3, 4, 5);
462
463 // Test Case 4: Static context + STATICCALL -> nested context remains static
464 setup_context_expectations(true);
465 EXPECT_CALL(context_provider,
466 make_nested_context(nested_address,
467 parent_address,
468 _,
469 _,
470 _,
471 _,
472 /*is_static=*/true,
473 Gas{ 2, 3 },
475 .WillOnce(Return(create_nested_context()));
476 execution.static_call(context, 1, 2, 3, 4, 5);
477}
478
479TEST_F(ExecutionSimulationTest, InternalCall)
480{
481 uint32_t pc = 100; // This is the pc of the current call.
482 uint32_t return_pc = 500; // This is next pc that we should return to after the internal call.
483 uint32_t pc_loc = 11; // This is the pc of the internal call
484
485 NiceMock<MockInternalCallStackManager> internal_call_stack_manager;
486 ON_CALL(context, get_internal_call_stack_manager).WillByDefault(ReturnRef(internal_call_stack_manager));
487
488 // ==== Internal Call
489 // Get manager
490 EXPECT_CALL(context, get_internal_call_stack_manager());
491 // Store the return pc (i.e. context.get_next_pc())
492 EXPECT_CALL(context, get_pc()).WillOnce(Return(pc));
493 EXPECT_CALL(context, get_next_pc()).WillOnce(Return(return_pc));
494 EXPECT_CALL(internal_call_stack_manager, push(pc, return_pc));
495 // Set next pc to the parameter pc_loc
496 EXPECT_CALL(context, set_next_pc(pc_loc));
497 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
498
499 execution.internal_call(context, pc_loc);
500
501 // ==== Internal Return
502 // Get manager
503 EXPECT_CALL(context, get_internal_call_stack_manager());
504 // Pop the return pc from the stack
505 EXPECT_CALL(internal_call_stack_manager, pop()).WillOnce(Return(return_pc));
506 // Set the next pc to the return pc
507 EXPECT_CALL(context, set_next_pc(return_pc));
508 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
509
510 execution.internal_return(context);
511}
512
513TEST_F(ExecutionSimulationTest, GetEnvVarAddress)
514{
515 AztecAddress addr = 0xdeadbeef;
516 EXPECT_CALL(context, get_address).WillOnce(ReturnRef(addr));
517 EXPECT_CALL(context, get_memory());
518 EXPECT_CALL(memory, set(1, MemoryValue::from<FF>(addr)));
519 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
520
521 execution.get_env_var(context, 1, static_cast<uint8_t>(EnvironmentVariable::ADDRESS));
522}
523
524TEST_F(ExecutionSimulationTest, GetEnvVarChainId)
525{
526 GlobalVariables globals;
527 globals.chain_id = 1;
528 EXPECT_CALL(context, get_globals).WillOnce(ReturnRef(globals));
529 EXPECT_CALL(context, get_memory());
530 EXPECT_CALL(memory, set(1, MemoryValue::from<FF>(1)));
531 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
532
533 execution.get_env_var(context, 1, static_cast<uint8_t>(EnvironmentVariable::CHAINID));
534}
535
536TEST_F(ExecutionSimulationTest, GetEnvVarIsStaticCall)
537{
538 EXPECT_CALL(context, get_is_static).WillOnce(Return(true));
539 EXPECT_CALL(context, get_memory());
540 EXPECT_CALL(memory, set(1, MemoryValue::from<uint1_t>(1)));
541 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
542
543 execution.get_env_var(context, 1, static_cast<uint8_t>(EnvironmentVariable::ISSTATICCALL));
544}
545
546TEST_F(ExecutionSimulationTest, GetEnvVarInvalidEnum)
547{
548 EXPECT_CALL(context, get_memory());
549 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
550
551 EXPECT_THROW(execution.get_env_var(context, 1, 255), std::runtime_error);
552}
553
554// Trivial test at the moment.
555// TODO: Attempt to have infra to call execution.execute() with a JUMP and a second instruction
556// and check the pc value for the second instruction is correct.
557TEST_F(ExecutionSimulationTest, Jump)
558{
559 EXPECT_CALL(context, set_next_pc(120));
560 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
561
562 execution.jump(context, 120);
563}
564
565TEST_F(ExecutionSimulationTest, SuccessCopyTrue)
566{
567 EXPECT_CALL(context, get_memory());
568 EXPECT_CALL(context, get_last_success).WillOnce(Return(true));
569 EXPECT_CALL(memory, set(10, MemoryValue::from<uint1_t>(1)));
570 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
571
572 execution.success_copy(context, 10);
573}
574
575TEST_F(ExecutionSimulationTest, SuccessCopyFalse)
576{
577 EXPECT_CALL(context, get_memory());
578 EXPECT_CALL(context, get_last_success).WillOnce(Return(false));
579 EXPECT_CALL(memory, set(10, MemoryValue::from<uint1_t>(0)));
580 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
581
582 execution.success_copy(context, 10);
583}
584
585TEST_F(ExecutionSimulationTest, RdSize)
586{
587 EXPECT_CALL(context, get_memory());
588 EXPECT_CALL(context, get_last_rd_size).WillOnce(Return(42));
589 EXPECT_CALL(memory, set(10, MemoryValue::from<uint32_t>(42)));
590 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
591
592 execution.rd_size(context, /*dst_addr=*/10);
593}
594
595TEST_F(ExecutionSimulationTest, DebugLog)
596{
597 // Setup test data
598 MemoryAddress level_offset = 50;
599 MemoryAddress message_offset = 100;
600 MemoryAddress fields_offset = 200;
601 MemoryAddress fields_size_offset = 300;
602 uint16_t message_size = 5;
603 AztecAddress address = 0xdeadbeef;
604
605 EXPECT_CALL(context, get_memory());
606 EXPECT_CALL(context, get_address).WillOnce(ReturnRef(address));
607 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
608 EXPECT_CALL(debug_log,
609 debug_log(_, address, level_offset, message_offset, message_size, fields_offset, fields_size_offset));
610
611 execution.debug_log(context, level_offset, message_offset, fields_offset, fields_size_offset, message_size);
612}
613
614TEST_F(ExecutionSimulationTest, Sload)
615{
616 MemoryAddress slot_addr = 27;
618 AztecAddress address = 0xdeadbeef;
619 auto slot = MemoryValue::from<FF>(42);
620
621 EXPECT_CALL(context, get_memory());
622
623 EXPECT_CALL(memory, get(slot_addr)).WillOnce(ReturnRef(slot));
624 EXPECT_CALL(context, get_address).WillOnce(ReturnRef(address));
625 EXPECT_CALL(merkle_db, storage_read(address, slot.as<FF>())).WillOnce(Return(7));
626
627 EXPECT_CALL(memory, set(dst_addr, MemoryValue::from<FF>(7)));
628 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
629
630 execution.sload(context, slot_addr, dst_addr);
631}
632
633TEST_F(ExecutionSimulationTest, SStore)
634{
635 MemoryAddress slot_addr = 27;
636 MemoryAddress value_addr = 10;
637 AztecAddress address = 0xdeadbeef;
638 auto slot = MemoryValue::from<FF>(42);
639 auto value = MemoryValue::from<FF>(7);
640 TreeStates tree_state = {};
641 EXPECT_CALL(context, get_memory());
642
643 EXPECT_CALL(memory, get(slot_addr)).WillOnce(ReturnRef(slot));
644 EXPECT_CALL(memory, get(value_addr)).WillOnce(ReturnRef(value));
645 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(address));
646 EXPECT_CALL(merkle_db, was_storage_written(address, slot.as<FF>())).WillOnce(Return(false));
647 EXPECT_CALL(merkle_db, get_tree_state).WillOnce(Return(tree_state));
648 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 1 }));
649
650 EXPECT_CALL(context, get_is_static).WillOnce(Return(false));
651
652 EXPECT_CALL(merkle_db, storage_write(address, slot.as<FF>(), value.as<FF>(), false));
653
654 execution.sstore(context, value_addr, slot_addr);
655}
656
657TEST_F(ExecutionSimulationTest, SStoreDuringStaticCall)
658{
659 MemoryAddress slot_addr = 27;
660 MemoryAddress value_addr = 10;
661 AztecAddress address = 0xdeadbeef;
662 auto slot = MemoryValue::from<FF>(42);
663 auto value = MemoryValue::from<FF>(7);
664 EXPECT_CALL(context, get_memory());
665
666 EXPECT_CALL(memory, get(slot_addr)).WillOnce(ReturnRef(slot));
667 EXPECT_CALL(memory, get(value_addr)).WillOnce(ReturnRef(value));
668 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(address));
669 EXPECT_CALL(merkle_db, was_storage_written(address, slot.as<FF>())).WillOnce(Return(false));
670 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 1 }));
671
672 EXPECT_CALL(context, get_is_static).WillOnce(Return(true));
673 EXPECT_THROW_WITH_MESSAGE(execution.sstore(context, value_addr, slot_addr), "Static call cannot update the state");
674}
675
676TEST_F(ExecutionSimulationTest, SStoreLimitReached)
677{
678 MemoryAddress slot_addr = 27;
679 MemoryAddress value_addr = 10;
680 AztecAddress address = 0xdeadbeef;
681 auto slot = MemoryValue::from<FF>(42);
682 auto value = MemoryValue::from<FF>(7);
683 TreeStates tree_state = {};
684 tree_state.public_data_tree.counter = MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX;
685 EXPECT_CALL(context, get_memory());
686
687 EXPECT_CALL(memory, get(slot_addr)).WillOnce(ReturnRef(slot));
688 EXPECT_CALL(memory, get(value_addr)).WillOnce(ReturnRef(value));
689 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(address));
690 EXPECT_CALL(merkle_db, was_storage_written(address, slot.as<FF>())).WillOnce(Return(false));
691 EXPECT_CALL(merkle_db, get_tree_state).WillOnce(Return(tree_state));
692 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 1 }));
693
694 EXPECT_CALL(context, get_is_static).WillOnce(Return(false));
695
696 EXPECT_THROW_WITH_MESSAGE(execution.sstore(context, value_addr, slot_addr),
697 "SSTORE: Maximum number of data writes reached");
698}
699
700TEST_F(ExecutionSimulationTest, SStoreLimitReachedSquashed)
701{
702 MemoryAddress slot_addr = 27;
703 MemoryAddress value_addr = 10;
704 AztecAddress address = 0xdeadbeef;
705 auto slot = MemoryValue::from<FF>(42);
706 auto value = MemoryValue::from<FF>(7);
707 TreeStates tree_state = {};
708 tree_state.public_data_tree.counter = MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX;
709 EXPECT_CALL(context, get_memory());
710
711 EXPECT_CALL(memory, get(slot_addr)).WillOnce(ReturnRef(slot));
712 EXPECT_CALL(memory, get(value_addr)).WillOnce(ReturnRef(value));
713 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(address));
714 // has been written before, so it does not count against the limit.
715 EXPECT_CALL(merkle_db, was_storage_written(address, slot.as<FF>())).WillOnce(Return(true));
716 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
717
718 EXPECT_CALL(context, get_is_static).WillOnce(Return(false));
719
720 EXPECT_CALL(merkle_db, storage_write(address, slot.as<FF>(), value.as<FF>(), false));
721
722 execution.sstore(context, value_addr, slot_addr);
723}
724
725TEST_F(ExecutionSimulationTest, NoteHashExists)
726{
727 MemoryAddress unique_note_hash_addr = 10;
728 MemoryAddress leaf_index_addr = 11;
730
731 auto unique_note_hash = MemoryValue::from<FF>(42);
732 auto leaf_index = MemoryValue::from<uint64_t>(7);
733
734 EXPECT_CALL(context, get_memory());
735 EXPECT_CALL(memory, get(unique_note_hash_addr)).WillOnce(ReturnRef(unique_note_hash));
736 EXPECT_CALL(memory, get(leaf_index_addr)).WillOnce(ReturnRef(leaf_index));
737
738 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
739
740 EXPECT_CALL(greater_than, gt(NOTE_HASH_TREE_LEAF_COUNT, leaf_index.as<uint64_t>())).WillOnce(Return(true));
741
742 EXPECT_CALL(merkle_db, note_hash_exists(leaf_index.as<uint64_t>(), unique_note_hash.as<FF>()))
743 .WillOnce(Return(true));
744
745 EXPECT_CALL(memory, set(dst_addr, MemoryValue::from<uint1_t>(1)));
746
747 execution.note_hash_exists(context, unique_note_hash_addr, leaf_index_addr, dst_addr);
748}
749
750TEST_F(ExecutionSimulationTest, NoteHashExistsOutOfRange)
751{
752 MemoryAddress unique_note_hash_addr = 10;
753 MemoryAddress leaf_index_addr = 11;
755
756 auto unique_note_hash = MemoryValue::from<FF>(42);
757 auto leaf_index = MemoryValue::from<uint64_t>(NOTE_HASH_TREE_LEAF_COUNT + 1);
758
759 EXPECT_CALL(context, get_memory());
760 EXPECT_CALL(memory, get(unique_note_hash_addr)).WillOnce(ReturnRef(unique_note_hash));
761 EXPECT_CALL(memory, get(leaf_index_addr)).WillOnce(ReturnRef(leaf_index));
762
763 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
764
765 EXPECT_CALL(greater_than, gt(NOTE_HASH_TREE_LEAF_COUNT, leaf_index.as<uint64_t>())).WillOnce(Return(false));
766
767 EXPECT_CALL(memory, set(dst_addr, MemoryValue::from<uint1_t>(0)));
768
769 execution.note_hash_exists(context, unique_note_hash_addr, leaf_index_addr, dst_addr);
770}
771
772TEST_F(ExecutionSimulationTest, EmitNoteHash)
773{
774 MemoryAddress note_hash_addr = 10;
775
776 auto note_hash = MemoryValue::from<FF>(42);
777 AztecAddress address = 0xdeadbeef;
778 TreeStates tree_state = {};
779
780 EXPECT_CALL(context, get_memory());
781 EXPECT_CALL(memory, get(note_hash_addr)).WillOnce(ReturnRef(note_hash));
782 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(address));
783
784 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
785
786 EXPECT_CALL(context, get_is_static).WillOnce(Return(false));
787 EXPECT_CALL(merkle_db, get_tree_state).WillOnce(Return(tree_state));
788 EXPECT_CALL(merkle_db, note_hash_write(address, note_hash.as<FF>()));
789
790 execution.emit_note_hash(context, note_hash_addr);
791}
792
793TEST_F(ExecutionSimulationTest, EmitNoteHashDuringStaticCall)
794{
795 MemoryAddress note_hash_addr = 10;
796
797 auto note_hash = MemoryValue::from<FF>(42);
798 AztecAddress address = 0xdeadbeef;
799
800 EXPECT_CALL(context, get_memory());
801 EXPECT_CALL(memory, get(note_hash_addr)).WillOnce(ReturnRef(note_hash));
802 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(address));
803
804 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
805
806 EXPECT_CALL(context, get_is_static).WillOnce(Return(true));
807 EXPECT_THROW_WITH_MESSAGE(execution.emit_note_hash(context, note_hash_addr), "Static call cannot update the state");
808}
809
810TEST_F(ExecutionSimulationTest, EmitNoteHashLimitReached)
811{
812 MemoryAddress note_hash_addr = 10;
813
814 auto note_hash = MemoryValue::from<FF>(42);
815 AztecAddress address = 0xdeadbeef;
816 TreeStates tree_state = {};
817 tree_state.note_hash_tree.counter = MAX_NOTE_HASHES_PER_TX;
818
819 EXPECT_CALL(context, get_memory());
820 EXPECT_CALL(memory, get(note_hash_addr)).WillOnce(ReturnRef(note_hash));
821 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(address));
822
823 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
824
825 EXPECT_CALL(context, get_is_static).WillOnce(Return(false));
826 EXPECT_CALL(merkle_db, get_tree_state).WillOnce(Return(tree_state));
827
828 EXPECT_THROW_WITH_MESSAGE(execution.emit_note_hash(context, note_hash_addr),
829 "EMITNOTEHASH: Maximum number of note hashes reached");
830}
831
832TEST_F(ExecutionSimulationTest, L1ToL2MessageExists)
833{
834 MemoryAddress msg_hash_addr = 10;
835 MemoryAddress leaf_index_addr = 11;
837
838 auto msg_hash = MemoryValue::from<FF>(42);
839 auto leaf_index = MemoryValue::from<uint64_t>(7);
840
841 EXPECT_CALL(context, get_memory());
842 EXPECT_CALL(memory, get(msg_hash_addr)).WillOnce(ReturnRef(msg_hash));
843 EXPECT_CALL(memory, get(leaf_index_addr)).WillOnce(ReturnRef(leaf_index));
844
845 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
846
847 EXPECT_CALL(greater_than, gt(L1_TO_L2_MSG_TREE_LEAF_COUNT, leaf_index.as<uint64_t>())).WillOnce(Return(true));
848
849 EXPECT_CALL(merkle_db, l1_to_l2_msg_exists(leaf_index.as<uint64_t>(), msg_hash.as<FF>())).WillOnce(Return(true));
850
851 EXPECT_CALL(memory, set(dst_addr, MemoryValue::from<uint1_t>(1)));
852
853 execution.l1_to_l2_message_exists(context, msg_hash_addr, leaf_index_addr, dst_addr);
854}
855
856TEST_F(ExecutionSimulationTest, L1ToL2MessageExistsOutOfRange)
857{
858 MemoryAddress msg_hash_addr = 10;
859 MemoryAddress leaf_index_addr = 11;
861
862 auto msg_hash = MemoryValue::from<FF>(42);
863 auto leaf_index = MemoryValue::from<uint64_t>(L1_TO_L2_MSG_TREE_LEAF_COUNT + 1);
864
865 EXPECT_CALL(context, get_memory());
866 EXPECT_CALL(memory, get(msg_hash_addr)).WillOnce(ReturnRef(msg_hash));
867 EXPECT_CALL(memory, get(leaf_index_addr)).WillOnce(ReturnRef(leaf_index));
868
869 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
870
871 EXPECT_CALL(greater_than, gt(L1_TO_L2_MSG_TREE_LEAF_COUNT, leaf_index.as<uint64_t>())).WillOnce(Return(false));
872
873 EXPECT_CALL(memory, set(dst_addr, MemoryValue::from<uint1_t>(0)));
874
875 execution.l1_to_l2_message_exists(context, msg_hash_addr, leaf_index_addr, dst_addr);
876}
877
878TEST_F(ExecutionSimulationTest, NullifierExists)
879{
880 MemoryAddress nullifier_offset = 10;
881 MemoryAddress address_offset = 11;
882 MemoryAddress exists_offset = 12;
883
884 auto nullifier = MemoryValue::from<FF>(42);
885 auto address = MemoryValue::from<FF>(7);
886
887 EXPECT_CALL(context, get_memory());
888 EXPECT_CALL(memory, get(nullifier_offset)).WillOnce(ReturnRef(nullifier));
889 EXPECT_CALL(memory, get(address_offset)).WillOnce(ReturnRef(address));
890
891 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
892
893 EXPECT_CALL(merkle_db, nullifier_exists(address.as_ff(), nullifier.as_ff())).WillOnce(Return(true));
894
895 EXPECT_CALL(memory, set(exists_offset, MemoryValue::from<uint1_t>(1)));
896
897 execution.nullifier_exists(context, nullifier_offset, address_offset, exists_offset);
898}
899
900TEST_F(ExecutionSimulationTest, EmitNullifier)
901{
902 MemoryAddress nullifier_addr = 10;
903
904 auto nullifier = MemoryValue::from<FF>(42);
905 AztecAddress address = 0xdeadbeef;
906 TreeStates tree_state = {};
907
908 EXPECT_CALL(context, get_memory());
909 EXPECT_CALL(memory, get(nullifier_addr)).WillOnce(ReturnRef(nullifier));
910 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(address));
911
912 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
913
914 EXPECT_CALL(context, get_is_static).WillOnce(Return(false));
915 EXPECT_CALL(merkle_db, get_tree_state).WillOnce(Return(tree_state));
916 EXPECT_CALL(merkle_db, nullifier_write(address, nullifier.as_ff())).WillOnce(Return()); // success
917
918 execution.emit_nullifier(context, nullifier_addr);
919}
920
921TEST_F(ExecutionSimulationTest, EmitNullifierDuringStaticCall)
922{
923 MemoryAddress nullifier_addr = 10;
924
925 auto nullifier = MemoryValue::from<FF>(42);
926 AztecAddress address = 0xdeadbeef;
927
928 EXPECT_CALL(context, get_memory());
929 EXPECT_CALL(memory, get(nullifier_addr)).WillOnce(ReturnRef(nullifier));
930 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(address));
931
932 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
933
934 EXPECT_CALL(context, get_is_static).WillOnce(Return(true));
935 EXPECT_THROW_WITH_MESSAGE(execution.emit_nullifier(context, nullifier_addr), "Static call cannot update the state");
936}
937
938TEST_F(ExecutionSimulationTest, EmitNullifierLimitReached)
939{
940 MemoryAddress nullifier_addr = 10;
941
942 auto nullifier = MemoryValue::from<FF>(42);
943 AztecAddress address = 0xdeadbeef;
944 TreeStates tree_state = {};
945 tree_state.nullifier_tree.counter = MAX_NULLIFIERS_PER_TX;
946
947 EXPECT_CALL(context, get_memory());
948 EXPECT_CALL(memory, get(nullifier_addr)).WillOnce(ReturnRef(nullifier));
949 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(address));
950
951 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
952
953 EXPECT_CALL(context, get_is_static).WillOnce(Return(false));
954 EXPECT_CALL(merkle_db, get_tree_state).WillOnce(Return(tree_state));
955
956 EXPECT_THROW_WITH_MESSAGE(execution.emit_nullifier(context, nullifier_addr),
957 "EMITNULLIFIER: Maximum number of nullifiers reached");
958}
959
960TEST_F(ExecutionSimulationTest, EmitNullifierCollision)
961{
962 MemoryAddress nullifier_addr = 10;
963
964 auto nullifier = MemoryValue::from<FF>(42);
965 AztecAddress address = 0xdeadbeef;
966 TreeStates tree_state = {};
967
968 EXPECT_CALL(context, get_memory());
969 EXPECT_CALL(memory, get(nullifier_addr)).WillOnce(ReturnRef(nullifier));
970 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(address));
971
972 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
973
974 EXPECT_CALL(context, get_is_static).WillOnce(Return(false));
975 EXPECT_CALL(merkle_db, get_tree_state).WillOnce(Return(tree_state));
976 EXPECT_CALL(merkle_db, nullifier_write(address, nullifier.as<FF>()))
977 .WillOnce(Throw(NullifierCollisionException("Nullifier collision"))); // collision
978
979 EXPECT_THROW_WITH_MESSAGE(execution.emit_nullifier(context, nullifier_addr), "EMITNULLIFIER: Nullifier collision");
980}
981
982TEST_F(ExecutionSimulationTest, Set)
983{
985 uint8_t dst_tag = static_cast<uint8_t>(MemoryTag::U8);
986 FF value = 7;
987
988 EXPECT_CALL(context, get_memory());
989 EXPECT_CALL(alu, truncate(value, static_cast<MemoryTag>(dst_tag))).WillOnce(Return(MemoryValue::from<uint8_t>(7)));
990 EXPECT_CALL(memory, set(dst_addr, MemoryValue::from<uint8_t>(7)));
991 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
992
993 execution.set(context, dst_addr, dst_tag, value);
994}
995
996TEST_F(ExecutionSimulationTest, Cast)
997{
998 MemoryAddress src_addr = 9;
1000 uint8_t dst_tag = static_cast<uint8_t>(MemoryTag::U1);
1001 MemoryValue value = MemoryValue::from<uint64_t>(7);
1002
1003 EXPECT_CALL(context, get_memory()).WillOnce(ReturnRef(memory));
1004 EXPECT_CALL(memory, get(src_addr)).WillOnce(ReturnRef(value));
1005
1006 EXPECT_CALL(alu, truncate(value.as_ff(), static_cast<MemoryTag>(dst_tag)))
1007 .WillOnce(Return(MemoryValue::from<uint1_t>(1)));
1008 EXPECT_CALL(memory, set(dst_addr, MemoryValue::from<uint1_t>(1)));
1009 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
1010
1011 execution.cast(context, src_addr, dst_addr, dst_tag);
1012}
1013
1014TEST_F(ExecutionSimulationTest, Poseidon2Perm)
1015{
1018
1019 EXPECT_CALL(context, get_memory());
1020 EXPECT_CALL(gas_tracker, consume_gas);
1021 EXPECT_CALL(poseidon2, permutation(_, src_address, dst_address));
1022
1023 execution.poseidon2_permutation(context, src_address, dst_address);
1024}
1025
1026TEST_F(ExecutionSimulationTest, EccAdd)
1027{
1028 MemoryAddress p_x_addr = 10;
1029 MemoryAddress p_y_addr = 15;
1030 MemoryAddress p_is_inf_addr = 25;
1031 MemoryAddress q_x_addr = 20;
1032 MemoryAddress q_y_addr = 30;
1033 MemoryAddress q_is_inf_addr = 35;
1035
1036 MemoryValue p_x = MemoryValue::from<FF>(FF("0x04c95d1b26d63d46918a156cae92db1bcbc4072a27ec81dc82ea959abdbcf16a"));
1037 MemoryValue p_y = MemoryValue::from<FF>(FF("0x035b6dd9e63c1370462c74775765d07fc21fd1093cc988149d3aa763bb3dbb60"));
1038 EmbeddedCurvePoint p(p_x.as_ff(), p_y, false);
1039
1040 MemoryValue q_x = MemoryValue::from<FF>(FF("0x009242167ec31949c00cbe441cd36757607406e87844fa2c8c4364a4403e66d7"));
1041 MemoryValue q_y = MemoryValue::from<FF>(FF("0x0fe3016d64cfa8045609f375284b6b739b5fa282e4cbb75cc7f1687ecc7420e3"));
1042 EmbeddedCurvePoint q(q_x.as_ff(), q_y.as_ff(), false);
1043
1044 // Mock the context and memory interactions
1045 MemoryValue zero = MemoryValue::from<uint1_t>(0);
1046 EXPECT_CALL(context, get_memory()).WillRepeatedly(ReturnRef(memory));
1047 EXPECT_CALL(Const(memory), get(p_x_addr)).WillOnce(ReturnRef(p_x));
1048 EXPECT_CALL(memory, get(p_y_addr)).WillOnce(ReturnRef(p_y));
1049 EXPECT_CALL(memory, get(p_is_inf_addr)).WillOnce(ReturnRef(zero)); // p is not infinity
1050 EXPECT_CALL(memory, get(q_x_addr)).WillOnce(ReturnRef(q_x));
1051 EXPECT_CALL(memory, get(q_y_addr)).WillOnce(ReturnRef(q_y));
1052 EXPECT_CALL(memory, get(q_is_inf_addr)).WillOnce(ReturnRef(zero)); // q is not infinity
1053
1054 EXPECT_CALL(gas_tracker, consume_gas);
1055
1056 // Mock the ECC add operation
1057 EXPECT_CALL(ecc, add(_, _, _, dst_addr));
1058
1059 // Execute the ECC add operation
1060 execution.ecc_add(context, p_x_addr, p_y_addr, p_is_inf_addr, q_x_addr, q_y_addr, q_is_inf_addr, dst_addr);
1061}
1062
1063TEST_F(ExecutionSimulationTest, ToRadixBE)
1064{
1065 MemoryAddress value_addr = 5;
1066 MemoryAddress radix_addr = 6;
1067 MemoryAddress num_limbs_addr = 7;
1068 MemoryAddress is_output_bits_addr = 8;
1069
1071 MemoryValue value = MemoryValue::from<FF>(42069);
1072 MemoryValue radix = MemoryValue::from<uint32_t>(16);
1073 std::vector<MemoryValue> be_limbs = { MemoryValue::from<uint8_t>(0xa4),
1074 MemoryValue::from<uint8_t>(0x55),
1075 MemoryValue::from<uint8_t>(0x00) };
1076 MemoryValue num_limbs = MemoryValue::from<uint32_t>(3);
1077 MemoryValue is_output_bits = MemoryValue::from<uint1_t>(false);
1078 uint32_t num_p_limbs = 64;
1079
1080 EXPECT_CALL(context, get_memory()).WillOnce(ReturnRef(memory));
1081 EXPECT_CALL(memory, get(value_addr)).WillOnce(ReturnRef(value));
1082 EXPECT_CALL(memory, get(radix_addr)).WillOnce(ReturnRef(radix));
1083 EXPECT_CALL(memory, get(num_limbs_addr)).WillOnce(ReturnRef(num_limbs));
1084 EXPECT_CALL(memory, get(is_output_bits_addr)).WillOnce(ReturnRef(is_output_bits));
1085
1086 EXPECT_CALL(greater_than, gt(radix.as<uint32_t>(), /*max_radix/*/ 256)).WillOnce(Return(false));
1087 EXPECT_CALL(greater_than, gt(num_limbs.as<uint32_t>(), num_p_limbs)).WillOnce(Return(false));
1088
1089 EXPECT_CALL(gas_tracker, consume_gas);
1090 EXPECT_CALL(to_radix, to_be_radix);
1091
1092 execution.to_radix_be(context, value_addr, radix_addr, num_limbs_addr, is_output_bits_addr, dst_addr);
1093}
1094
1095TEST_F(ExecutionSimulationTest, EmitUnencryptedLog)
1096{
1097 MemoryAddress log_offset = 10;
1098 MemoryAddress log_size_offset = 20;
1099 MemoryValue log_size = MemoryValue::from<uint32_t>(10);
1100 AztecAddress address = 0xdeadbeef;
1101
1102 EXPECT_CALL(context, get_memory());
1103 EXPECT_CALL(memory, get(log_size_offset)).WillOnce(ReturnRef(log_size));
1104
1105 EXPECT_CALL(context, get_address).WillOnce(ReturnRef(address));
1106
1107 EXPECT_CALL(emit_unencrypted_log, emit_unencrypted_log(_, _, address, log_offset, log_size.as<uint32_t>()));
1108
1109 EXPECT_CALL(gas_tracker, consume_gas(Gas{ log_size.as<uint32_t>(), log_size.as<uint32_t>() }));
1110
1111 execution.emit_unencrypted_log(context, log_size_offset, log_offset);
1112}
1113
1114TEST_F(ExecutionSimulationTest, SendL2ToL1Msg)
1115{
1116 AztecAddress contract_address = 0xc0ffee;
1117 EthAddress recipient_address = EthAddress(0x12345678);
1118 FF content = 0x999;
1119 MemoryAddress recipient_addr = 10;
1120 MemoryAddress content_addr = 11;
1121
1122 auto recipient = MemoryValue::from<FF>(recipient_address);
1123 auto content_value = MemoryValue::from<FF>(content);
1124
1125 ScopedL2ToL1Message dummy_msg = { .message = { .recipient = recipient_address, .content = content },
1126 .contract_address = contract_address };
1127 TrackedSideEffects side_effects_states;
1128 for (int i = 0; i < MAX_L2_TO_L1_MSGS_PER_TX - 1; i++) {
1129 side_effects_states.l2_to_l1_messages.push_back(dummy_msg);
1130 }
1131 TrackedSideEffects side_effects_states_after = side_effects_states;
1132 side_effects_states_after.l2_to_l1_messages.push_back(dummy_msg);
1133
1134 EXPECT_CALL(context, get_memory());
1135 EXPECT_CALL(context, get_address).WillOnce(ReturnRef(contract_address));
1136 EXPECT_CALL(context, get_side_effect_tracker);
1137
1138 EXPECT_CALL(memory, get(recipient_addr)).WillOnce(ReturnRef(recipient));
1139 EXPECT_CALL(memory, get(content_addr)).WillOnce(ReturnRef(content_value));
1140
1141 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
1142
1143 EXPECT_CALL(context, get_is_static).WillOnce(Return(false));
1144
1145 EXPECT_CALL(side_effect_tracker, get_side_effects()).WillOnce(ReturnRef(side_effects_states));
1146 EXPECT_CALL(side_effect_tracker, add_l2_to_l1_message(contract_address, recipient_address, content))
1147 .WillOnce(Return());
1148
1149 execution.send_l2_to_l1_msg(context, recipient_addr, content_addr);
1150}
1151
1152TEST_F(ExecutionSimulationTest, SendL2ToL1MsgStaticCall)
1153{
1154 MemoryAddress recipient_addr = 10;
1155 MemoryAddress content_addr = 11;
1156
1157 auto recipient = MemoryValue::from<FF>(42);
1158 auto content = MemoryValue::from<FF>(27);
1159
1160 TrackedSideEffects side_effects_states;
1161
1162 EXPECT_CALL(context, get_memory());
1163
1164 EXPECT_CALL(memory, get(recipient_addr)).WillOnce(ReturnRef(recipient));
1165 EXPECT_CALL(memory, get(content_addr)).WillOnce(ReturnRef(content));
1166
1167 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
1168
1169 EXPECT_CALL(context, get_is_static).WillOnce(Return(true));
1170
1171 EXPECT_THROW_WITH_MESSAGE(execution.send_l2_to_l1_msg(context, recipient_addr, content_addr),
1172 "Static call cannot update the state");
1173}
1174
1175TEST_F(ExecutionSimulationTest, SendL2ToL1MsgLimitReached)
1176{
1177 MemoryAddress recipient_addr = 10;
1178 MemoryAddress content_addr = 11;
1179
1180 auto recipient = MemoryValue::from<FF>(42);
1181 auto content = MemoryValue::from<FF>(27);
1182
1183 TrackedSideEffects side_effects_states;
1184 for (int i = 0; i < MAX_L2_TO_L1_MSGS_PER_TX; i++) {
1185 side_effects_states.l2_to_l1_messages.push_back(
1186 ScopedL2ToL1Message{ .message = { .recipient = EthAddress(0x12345678), .content = 0x12345678 },
1187 .contract_address = 0x12345678 });
1188 }
1189
1190 EXPECT_CALL(context, get_memory());
1191 EXPECT_CALL(context, get_side_effect_tracker);
1192
1193 EXPECT_CALL(memory, get(recipient_addr)).WillOnce(ReturnRef(recipient));
1194 EXPECT_CALL(memory, get(content_addr)).WillOnce(ReturnRef(content));
1195
1196 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
1197
1198 EXPECT_CALL(context, get_is_static).WillOnce(Return(false));
1199
1200 EXPECT_CALL(side_effect_tracker, get_side_effects()).WillOnce(ReturnRef(side_effects_states));
1201
1202 EXPECT_THROW_WITH_MESSAGE(execution.send_l2_to_l1_msg(context, recipient_addr, content_addr),
1203 "SENDL2TOL1MSG: Maximum number of L2 to L1 messages reached");
1204}
1205
1206TEST_F(ExecutionSimulationTest, Sha256Compression)
1207{
1208 MemoryAddress state_address = 10;
1209 MemoryAddress input_address = 20;
1211
1212 EXPECT_CALL(context, get_memory());
1213 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
1214 EXPECT_CALL(sha256, compression(_, state_address, input_address, dst_address));
1215
1216 execution.sha256_compression(context, dst_address, state_address, input_address);
1217}
1218
1219} // namespace
1220
1221} // namespace bb::avm2::simulation
GreaterThan greater_than
MemoryTag dst_tag
#define NOTE_HASH_TREE_LEAF_COUNT
#define L1_TO_L2_MSG_TREE_LEAF_COUNT
#define MAX_L2_TO_L1_MSGS_PER_TX
#define MAX_NOTE_HASHES_PER_TX
#define MAX_NULLIFIERS_PER_TX
#define MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX
StrictMock< MockHighLevelMerkleDB > merkle_db
Applies the Poseidon2 permutation function from https://eprint.iacr.org/2023/323.
ExecutionIdManager execution_id_manager
uint32_t dst_addr
GreaterThan gt
StrictMock< MockContext > context
FF a
FF b
void debug_log(Args &&... args)
Compile-time debug logging helper.
Definition fuzzer.hpp:127
InstructionInfoDB instruction_info_db
smt_circuit::STerm shr(smt_circuit::STerm v0, smt_circuit::STerm v1, smt_solver::Solver *solver)
Right shift operation.
Definition helpers.cpp:40
smt_circuit::STerm shl(smt_circuit::STerm v0, smt_circuit::STerm v1, smt_solver::Solver *solver)
Left shift operation without truncation.
Definition helpers.cpp:34
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessage)
Definition macros.hpp:7
TaggedValue MemoryValue
StandardAffinePoint< AvmFlavorSettings::EmbeddedCurve::AffineElement > EmbeddedCurvePoint
Definition field.hpp:12
uint256_t get_tag_max_value(ValueTag tag)
uint32_t MemoryAddress
AvmFlavorSettings::FF FF
Definition field.hpp:10
Sha256Hash sha256(const ByteContainer &input)
SHA-256 hash function (FIPS 180-4)
Definition sha256.cpp:150
TEST_F(IPATest, ChallengesAreZero)
Definition ipa.test.cpp:185
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
unsigned __int128 uint128_t
Definition serialize.hpp:44
bb::crypto::Poseidon2< bb::crypto::Poseidon2Bn254ScalarFieldParams > poseidon2
Bitwise bitwise
DataCopy data_copy
MemoryStore memory
static constexpr uint256_t modulus
SideEffectTracker side_effect_tracker
NiceMock< MockContextProvider > context_provider
NiceMock< MockExecution > execution
NoopCallStackMetadataCollector call_stack_metadata_collector