3#include <gmock/gmock.h>
4#include <gtest/gtest.h>
51using ::testing::NiceMock;
52using ::testing::Return;
53using ::testing::ReturnRef;
54using ::testing::StrictMock;
55using ::testing::Throw;
58class TestingExecution :
public Execution {
60 using Execution::Execution;
62 void set_gas_tracker(GasTrackerInterface& gas_tracker) { this->testing_gas_tracker = &gas_tracker; }
64 GasTrackerInterface& get_gas_tracker()
override {
return *testing_gas_tracker; }
67 GasTrackerInterface* testing_gas_tracker;
70class ExecutionSimulationTest :
public ::testing::Test {
72 ExecutionSimulationTest()
74 ON_CALL(
context, get_memory()).WillByDefault(ReturnRef(
memory));
75 ON_CALL(
context, get_bytecode_manager).WillByDefault(ReturnRef(bytecode_manager));
80 StrictMock<MockAlu> alu;
81 StrictMock<MockBitwise>
bitwise;
82 StrictMock<MockMemory>
memory;
83 StrictMock<MockExecutionComponentsProvider> execution_components;
84 StrictMock<MockContext>
context;
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;
94 StrictMock<MockGasTracker> gas_tracker;
95 StrictMock<MockHighLevelMerkleDB>
merkle_db;
98 StrictMock<MockEcc> ecc;
99 StrictMock<MockToRadix> to_radix;
100 StrictMock<MockEmitUnencryptedLog> emit_unencrypted_log;
101 StrictMock<MockBytecodeManager> bytecode_manager;
102 StrictMock<MockSha256>
sha256;
106 TestingExecution
execution = TestingExecution(alu,
113 execution_components,
117 execution_event_emitter,
118 context_stack_event_emitter,
121 get_contract_instance,
122 emit_unencrypted_log,
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 }));
145TEST_F(ExecutionSimulationTest, Sub)
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 }));
159TEST_F(ExecutionSimulationTest, Mul)
162 auto a = MemoryValue::from<uint128_t>(max);
163 auto b = MemoryValue::from<uint128_t>(max - 3);
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 }));
174TEST_F(ExecutionSimulationTest, Div)
176 auto a = MemoryValue::from<uint128_t>(6);
177 auto b = MemoryValue::from<uint128_t>(3);
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 }));
188TEST_F(ExecutionSimulationTest, FDiv)
191 auto b = MemoryValue::from<FF>(2);
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 }));
202TEST_F(ExecutionSimulationTest, Shl)
204 auto a = MemoryValue::from<uint32_t>(64);
205 auto b = MemoryValue::from<uint32_t>(2);
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 }));
216TEST_F(ExecutionSimulationTest, Shr)
218 auto a = MemoryValue::from<uint64_t>(64);
219 auto b = MemoryValue::from<uint64_t>(2);
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 }));
232TEST_F(ExecutionSimulationTest, Call)
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{
244 .next_available_leaf_index = 10,
246 TreeStates tree_states = TreeStates {
250 .next_available_leaf_index = 9,
257 .next_available_leaf_index = 6,
261 .l1_to_l2_message_tree = {
264 .next_available_leaf_index = 3,
268 .public_data_tree = {
271 .next_available_leaf_index = 1,
276 TrackedSideEffects side_effect_states = {
277 .l2_to_l1_messages = { {
278 .message = { .recipient =
EthAddress(0x12345678), .content = 0x12345678 },
279 .contract_address = parent_address,
282 .message = { .recipient =
EthAddress(0x333333), .content = 0x12345678 },
283 .contract_address = parent_address,
285 .public_logs = PublicLogs{ { { { 4 }, parent_address } } },
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 }));
292 EXPECT_CALL(context, get_pc).WillOnce(Return(parent_pc));
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_internal_call_stack_manager).WillRepeatedly(ReturnRef(internal_call_stack_manager));
309 EXPECT_CALL(internal_call_stack_manager, get_call_id);
310 EXPECT_CALL(internal_call_stack_manager, get_return_call_id);
311 EXPECT_CALL(internal_call_stack_manager, get_next_call_id);
312 EXPECT_CALL(context, get_written_public_data_slots_tree_snapshot)
313 .WillOnce(Return(written_public_data_slots_tree_snapshot));
314 EXPECT_CALL(context, get_side_effect_tracker);
315 EXPECT_CALL(
side_effect_tracker, get_side_effects()).WillRepeatedly(ReturnRef(side_effect_states));
319 EXPECT_CALL(
merkle_db, get_tree_state).WillOnce(Return(tree_states));
321 EXPECT_CALL(context, get_memory());
322 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(parent_address));
323 EXPECT_CALL(memory,
get(1)).WillOnce(ReturnRef(l2_gas_allocated));
324 EXPECT_CALL(memory,
get(2)).WillOnce(ReturnRef(da_gas_allocated));
325 EXPECT_CALL(memory,
get(3)).WillOnce(ReturnRef(nested_address_value));
326 EXPECT_CALL(memory,
get(4)).WillOnce(ReturnRef(cd_size));
329 ON_CALL(*nested_context, halted())
330 .WillByDefault(Return(
true));
332 EXPECT_CALL(*nested_context, get_address).WillOnce(ReturnRef(nested_address));
333 EXPECT_CALL(*nested_context, get_is_static).WillOnce(Return(
false));
334 EXPECT_CALL(*nested_context, get_gas_limit).WillOnce(Return(Gas{ 2, 3 }));
339 .WillOnce(Return(
std::move(nested_context)));
350TEST_F(ExecutionSimulationTest, ExternalCallStaticnessPropagation)
356 MemoryValue nested_address_value = MemoryValue::from<FF>(nested_address);
357 MemoryValue l2_gas_allocated = MemoryValue::from<uint32_t>(6);
358 MemoryValue da_gas_allocated = MemoryValue::from<uint32_t>(7);
359 MemoryValue cd_size = MemoryValue::from<uint32_t>(8);
360 AppendOnlyTreeSnapshot written_public_data_slots_tree_snapshot = AppendOnlyTreeSnapshot{
362 .next_available_leaf_index = 10,
364 TreeStates tree_states =
365 TreeStates{ .note_hash_tree = { .tree = { .root = 10, .next_available_leaf_index = 9 }, .counter = 8 },
366 .nullifier_tree = { .tree = { .root = 7, .next_available_leaf_index = 6 }, .counter = 5 },
367 .l1_to_l2_message_tree = { .tree = { .root = 4, .next_available_leaf_index = 3 }, .counter = 0 },
368 .public_data_tree = { .tree = { .root = 2, .next_available_leaf_index = 1 }, .counter = 1 } };
369 TrackedSideEffects side_effect_states = {
370 .l2_to_l1_messages = { {
371 .message = { .recipient =
EthAddress(0x12345678), .content = 0x12345678 },
372 .contract_address = parent_address,
375 .message = { .recipient =
EthAddress(0x333333), .content = 0x12345678 },
376 .contract_address = parent_address,
378 .public_logs = PublicLogs{ { { { 4 }, parent_address } } },
381 auto setup_context_expectations = [&](
bool parent_is_static) {
383 EXPECT_CALL(gas_tracker, compute_gas_limit_for_call(Gas{ 6, 7 })).WillOnce(Return(Gas{ 2, 3 }));
384 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
385 EXPECT_CALL(context, get_pc).WillOnce(Return(100));
386 EXPECT_CALL(context, get_context_id);
387 EXPECT_CALL(context, get_parent_id);
388 EXPECT_CALL(context, get_bytecode_manager).WillOnce(ReturnRef(bytecode_manager));
389 EXPECT_CALL(bytecode_manager, get_retrieved_bytecode_id).WillOnce(Return(
FF(1)));
390 EXPECT_CALL(context, get_next_pc);
391 EXPECT_CALL(context, get_is_static).WillRepeatedly(Return(parent_is_static));
392 EXPECT_CALL(context, get_msg_sender).WillOnce(ReturnRef(parent_address));
393 EXPECT_CALL(context, get_transaction_fee).WillOnce(ReturnRef(zero));
394 EXPECT_CALL(context, get_parent_cd_addr);
395 EXPECT_CALL(context, get_parent_cd_size);
396 EXPECT_CALL(context, get_parent_gas_used);
397 EXPECT_CALL(context, get_parent_gas_limit);
398 EXPECT_CALL(context, get_internal_call_stack_manager).WillRepeatedly(ReturnRef(internal_call_stack_manager));
399 EXPECT_CALL(internal_call_stack_manager, get_call_id);
400 EXPECT_CALL(internal_call_stack_manager, get_return_call_id);
401 EXPECT_CALL(internal_call_stack_manager, get_next_call_id);
402 EXPECT_CALL(context, get_written_public_data_slots_tree_snapshot)
403 .WillOnce(Return(written_public_data_slots_tree_snapshot));
404 EXPECT_CALL(context, get_side_effect_tracker);
405 EXPECT_CALL(
side_effect_tracker, get_side_effects()).WillRepeatedly(ReturnRef(side_effect_states));
407 EXPECT_CALL(
merkle_db, get_tree_state).WillOnce(Return(tree_states));
408 EXPECT_CALL(context, get_memory());
409 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(parent_address));
410 EXPECT_CALL(memory,
get(1)).WillOnce(ReturnRef(l2_gas_allocated));
411 EXPECT_CALL(memory,
get(2)).WillOnce(ReturnRef(da_gas_allocated));
412 EXPECT_CALL(memory,
get(3)).WillOnce(ReturnRef(nested_address_value));
413 EXPECT_CALL(memory,
get(4)).WillOnce(ReturnRef(cd_size));
416 auto create_nested_context = [&]() {
418 ON_CALL(*nested, halted()).WillByDefault(Return(
true));
420 EXPECT_CALL(*nested, get_address).WillOnce(ReturnRef(nested_address));
421 EXPECT_CALL(*nested, get_is_static).WillOnce(Return(
false));
422 EXPECT_CALL(*nested, get_gas_limit).WillOnce(Return(Gas{ 2, 3 }));
427 setup_context_expectations(
false);
429 make_nested_context(nested_address,
438 .WillOnce(Return(create_nested_context()));
442 setup_context_expectations(
false);
444 make_nested_context(nested_address,
453 .WillOnce(Return(create_nested_context()));
454 execution.static_call(context, 1, 2, 3, 4, 5);
457 setup_context_expectations(
true);
459 make_nested_context(nested_address,
468 .WillOnce(Return(create_nested_context()));
472 setup_context_expectations(
true);
474 make_nested_context(nested_address,
483 .WillOnce(Return(create_nested_context()));
484 execution.static_call(context, 1, 2, 3, 4, 5);
487TEST_F(ExecutionSimulationTest, InternalCall)
493 NiceMock<MockInternalCallStackManager> internal_call_stack_manager;
494 ON_CALL(context, get_internal_call_stack_manager).WillByDefault(ReturnRef(internal_call_stack_manager));
498 EXPECT_CALL(context, get_internal_call_stack_manager());
500 EXPECT_CALL(context, get_pc()).WillOnce(Return(pc));
501 EXPECT_CALL(context, get_next_pc()).WillOnce(Return(return_pc));
502 EXPECT_CALL(internal_call_stack_manager, push(pc, return_pc));
504 EXPECT_CALL(context, set_next_pc(pc_loc));
505 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
507 execution.internal_call(context, pc_loc);
511 EXPECT_CALL(context, get_internal_call_stack_manager());
513 EXPECT_CALL(internal_call_stack_manager, pop()).WillOnce(Return(return_pc));
515 EXPECT_CALL(context, set_next_pc(return_pc));
516 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
521TEST_F(ExecutionSimulationTest, GetEnvVarAddress)
524 EXPECT_CALL(context, get_address).WillOnce(ReturnRef(addr));
525 EXPECT_CALL(context, get_memory());
526 EXPECT_CALL(memory, set(1, MemoryValue::from<FF>(addr)));
527 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
532TEST_F(ExecutionSimulationTest, GetEnvVarChainId)
534 GlobalVariables globals;
535 globals.chain_id = 1;
536 EXPECT_CALL(context, get_globals).WillOnce(ReturnRef(globals));
537 EXPECT_CALL(context, get_memory());
538 EXPECT_CALL(memory, set(1, MemoryValue::from<FF>(1)));
539 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
544TEST_F(ExecutionSimulationTest, GetEnvVarIsStaticCall)
546 EXPECT_CALL(context, get_is_static).WillOnce(Return(
true));
547 EXPECT_CALL(context, get_memory());
548 EXPECT_CALL(memory, set(1, MemoryValue::from<uint1_t>(1)));
549 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
554TEST_F(ExecutionSimulationTest, GetEnvVarInvalidEnum)
556 EXPECT_CALL(context, get_memory());
557 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
559 EXPECT_THROW(
execution.get_env_var(context, 1, 255), std::runtime_error);
565TEST_F(ExecutionSimulationTest, Jump)
567 EXPECT_CALL(context, set_next_pc(120));
568 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
573TEST_F(ExecutionSimulationTest, SuccessCopyTrue)
575 EXPECT_CALL(context, get_memory());
576 EXPECT_CALL(context, get_last_success).WillOnce(Return(
true));
577 EXPECT_CALL(memory, set(10, MemoryValue::from<uint1_t>(1)));
578 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
583TEST_F(ExecutionSimulationTest, SuccessCopyFalse)
585 EXPECT_CALL(context, get_memory());
586 EXPECT_CALL(context, get_last_success).WillOnce(Return(
false));
587 EXPECT_CALL(memory, set(10, MemoryValue::from<uint1_t>(0)));
588 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
593TEST_F(ExecutionSimulationTest, RdSize)
595 EXPECT_CALL(context, get_memory());
596 EXPECT_CALL(context, get_last_rd_size).WillOnce(Return(42));
597 EXPECT_CALL(memory, set(10, MemoryValue::from<uint32_t>(42)));
598 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
603TEST_F(ExecutionSimulationTest, DebugLog)
613 EXPECT_CALL(context, get_memory());
614 EXPECT_CALL(context, get_address).WillOnce(ReturnRef(
address));
615 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
622TEST_F(ExecutionSimulationTest, Sload)
627 auto slot = MemoryValue::from<FF>(42);
629 EXPECT_CALL(context, get_memory());
631 EXPECT_CALL(memory,
get(slot_addr)).WillOnce(ReturnRef(
slot));
632 EXPECT_CALL(context, get_address).WillOnce(ReturnRef(
address));
635 EXPECT_CALL(memory, set(
dst_addr, MemoryValue::from<FF>(7)));
636 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
641TEST_F(ExecutionSimulationTest, SStore)
646 auto slot = MemoryValue::from<FF>(42);
647 auto value = MemoryValue::from<FF>(7);
648 TreeStates tree_state = {};
649 EXPECT_CALL(context, get_memory());
651 EXPECT_CALL(memory,
get(slot_addr)).WillOnce(ReturnRef(
slot));
652 EXPECT_CALL(memory,
get(value_addr)).WillOnce(ReturnRef(
value));
653 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(
address));
655 EXPECT_CALL(
merkle_db, get_tree_state).WillOnce(Return(tree_state));
656 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 1 }));
658 EXPECT_CALL(context, get_is_static).WillOnce(Return(
false));
662 execution.sstore(context, value_addr, slot_addr);
665TEST_F(ExecutionSimulationTest, SStoreDuringStaticCall)
670 auto slot = MemoryValue::from<FF>(42);
671 auto value = MemoryValue::from<FF>(7);
672 EXPECT_CALL(context, get_memory());
674 EXPECT_CALL(memory,
get(slot_addr)).WillOnce(ReturnRef(
slot));
675 EXPECT_CALL(memory,
get(value_addr)).WillOnce(ReturnRef(
value));
676 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(
address));
678 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 1 }));
680 EXPECT_CALL(context, get_is_static).WillOnce(Return(
true));
684TEST_F(ExecutionSimulationTest, SStoreLimitReached)
689 auto slot = MemoryValue::from<FF>(42);
690 auto value = MemoryValue::from<FF>(7);
691 TreeStates tree_state = {};
693 EXPECT_CALL(context, get_memory());
695 EXPECT_CALL(memory,
get(slot_addr)).WillOnce(ReturnRef(
slot));
696 EXPECT_CALL(memory,
get(value_addr)).WillOnce(ReturnRef(
value));
697 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(
address));
699 EXPECT_CALL(
merkle_db, get_tree_state).WillOnce(Return(tree_state));
700 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 1 }));
702 EXPECT_CALL(context, get_is_static).WillOnce(Return(
false));
705 "SSTORE: Maximum number of data writes reached");
708TEST_F(ExecutionSimulationTest, SStoreLimitReachedSquashed)
713 auto slot = MemoryValue::from<FF>(42);
714 auto value = MemoryValue::from<FF>(7);
715 TreeStates tree_state = {};
717 EXPECT_CALL(context, get_memory());
719 EXPECT_CALL(memory,
get(slot_addr)).WillOnce(ReturnRef(
slot));
720 EXPECT_CALL(memory,
get(value_addr)).WillOnce(ReturnRef(
value));
721 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(
address));
724 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
726 EXPECT_CALL(context, get_is_static).WillOnce(Return(
false));
730 execution.sstore(context, value_addr, slot_addr);
733TEST_F(ExecutionSimulationTest, NoteHashExists)
739 auto unique_note_hash = MemoryValue::from<FF>(42);
740 auto leaf_index = MemoryValue::from<uint64_t>(7);
742 EXPECT_CALL(context, get_memory());
743 EXPECT_CALL(memory,
get(unique_note_hash_addr)).WillOnce(ReturnRef(unique_note_hash));
744 EXPECT_CALL(memory,
get(leaf_index_addr)).WillOnce(ReturnRef(leaf_index));
746 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
750 EXPECT_CALL(
merkle_db, note_hash_exists(leaf_index.as<uint64_t>(), unique_note_hash.as<
FF>()))
751 .WillOnce(Return(
true));
753 EXPECT_CALL(memory, set(
dst_addr, MemoryValue::from<uint1_t>(1)));
755 execution.note_hash_exists(context, unique_note_hash_addr, leaf_index_addr,
dst_addr);
758TEST_F(ExecutionSimulationTest, NoteHashExistsOutOfRange)
764 auto unique_note_hash = MemoryValue::from<FF>(42);
767 EXPECT_CALL(context, get_memory());
768 EXPECT_CALL(memory,
get(unique_note_hash_addr)).WillOnce(ReturnRef(unique_note_hash));
769 EXPECT_CALL(memory,
get(leaf_index_addr)).WillOnce(ReturnRef(leaf_index));
771 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
775 EXPECT_CALL(memory, set(
dst_addr, MemoryValue::from<uint1_t>(0)));
777 execution.note_hash_exists(context, unique_note_hash_addr, leaf_index_addr,
dst_addr);
780TEST_F(ExecutionSimulationTest, EmitNoteHash)
784 auto note_hash = MemoryValue::from<FF>(42);
786 TreeStates tree_state = {};
788 EXPECT_CALL(context, get_memory());
789 EXPECT_CALL(memory,
get(note_hash_addr)).WillOnce(ReturnRef(
note_hash));
790 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(
address));
792 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
794 EXPECT_CALL(context, get_is_static).WillOnce(Return(
false));
795 EXPECT_CALL(
merkle_db, get_tree_state).WillOnce(Return(tree_state));
798 execution.emit_note_hash(context, note_hash_addr);
801TEST_F(ExecutionSimulationTest, EmitNoteHashDuringStaticCall)
805 auto note_hash = MemoryValue::from<FF>(42);
808 EXPECT_CALL(context, get_memory());
809 EXPECT_CALL(memory,
get(note_hash_addr)).WillOnce(ReturnRef(
note_hash));
810 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(
address));
812 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
814 EXPECT_CALL(context, get_is_static).WillOnce(Return(
true));
818TEST_F(ExecutionSimulationTest, EmitNoteHashLimitReached)
822 auto note_hash = MemoryValue::from<FF>(42);
824 TreeStates tree_state = {};
827 EXPECT_CALL(context, get_memory());
828 EXPECT_CALL(memory,
get(note_hash_addr)).WillOnce(ReturnRef(
note_hash));
829 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(
address));
831 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
833 EXPECT_CALL(context, get_is_static).WillOnce(Return(
false));
834 EXPECT_CALL(
merkle_db, get_tree_state).WillOnce(Return(tree_state));
837 "EMITNOTEHASH: Maximum number of note hashes reached");
840TEST_F(ExecutionSimulationTest, L1ToL2MessageExists)
846 auto msg_hash = MemoryValue::from<FF>(42);
847 auto leaf_index = MemoryValue::from<uint64_t>(7);
849 EXPECT_CALL(context, get_memory());
850 EXPECT_CALL(memory,
get(msg_hash_addr)).WillOnce(ReturnRef(msg_hash));
851 EXPECT_CALL(memory,
get(leaf_index_addr)).WillOnce(ReturnRef(leaf_index));
853 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
857 EXPECT_CALL(
merkle_db, l1_to_l2_msg_exists(leaf_index.as<uint64_t>(), msg_hash.as<
FF>())).WillOnce(Return(
true));
859 EXPECT_CALL(memory, set(
dst_addr, MemoryValue::from<uint1_t>(1)));
861 execution.l1_to_l2_message_exists(context, msg_hash_addr, leaf_index_addr,
dst_addr);
864TEST_F(ExecutionSimulationTest, L1ToL2MessageExistsOutOfRange)
870 auto msg_hash = MemoryValue::from<FF>(42);
873 EXPECT_CALL(context, get_memory());
874 EXPECT_CALL(memory,
get(msg_hash_addr)).WillOnce(ReturnRef(msg_hash));
875 EXPECT_CALL(memory,
get(leaf_index_addr)).WillOnce(ReturnRef(leaf_index));
877 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
881 EXPECT_CALL(memory, set(
dst_addr, MemoryValue::from<uint1_t>(0)));
883 execution.l1_to_l2_message_exists(context, msg_hash_addr, leaf_index_addr,
dst_addr);
886TEST_F(ExecutionSimulationTest, NullifierExists)
892 auto nullifier = MemoryValue::from<FF>(42);
893 auto address = MemoryValue::from<FF>(7);
895 EXPECT_CALL(context, get_memory());
896 EXPECT_CALL(memory,
get(nullifier_offset)).WillOnce(ReturnRef(
nullifier));
897 EXPECT_CALL(memory,
get(address_offset)).WillOnce(ReturnRef(
address));
899 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
903 EXPECT_CALL(memory, set(exists_offset, MemoryValue::from<uint1_t>(1)));
905 execution.nullifier_exists(context, nullifier_offset, address_offset, exists_offset);
908TEST_F(ExecutionSimulationTest, EmitNullifier)
912 auto nullifier = MemoryValue::from<FF>(42);
914 TreeStates tree_state = {};
916 EXPECT_CALL(context, get_memory());
917 EXPECT_CALL(memory,
get(nullifier_addr)).WillOnce(ReturnRef(
nullifier));
918 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(
address));
920 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
922 EXPECT_CALL(context, get_is_static).WillOnce(Return(
false));
923 EXPECT_CALL(
merkle_db, get_tree_state).WillOnce(Return(tree_state));
926 execution.emit_nullifier(context, nullifier_addr);
929TEST_F(ExecutionSimulationTest, EmitNullifierDuringStaticCall)
933 auto nullifier = MemoryValue::from<FF>(42);
936 EXPECT_CALL(context, get_memory());
937 EXPECT_CALL(memory,
get(nullifier_addr)).WillOnce(ReturnRef(
nullifier));
938 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(
address));
940 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
942 EXPECT_CALL(context, get_is_static).WillOnce(Return(
true));
946TEST_F(ExecutionSimulationTest, EmitNullifierLimitReached)
950 auto nullifier = MemoryValue::from<FF>(42);
952 TreeStates tree_state = {};
955 EXPECT_CALL(context, get_memory());
956 EXPECT_CALL(memory,
get(nullifier_addr)).WillOnce(ReturnRef(
nullifier));
957 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(
address));
959 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
961 EXPECT_CALL(context, get_is_static).WillOnce(Return(
false));
962 EXPECT_CALL(
merkle_db, get_tree_state).WillOnce(Return(tree_state));
965 "EMITNULLIFIER: Maximum number of nullifiers reached");
968TEST_F(ExecutionSimulationTest, EmitNullifierCollision)
972 auto nullifier = MemoryValue::from<FF>(42);
974 TreeStates tree_state = {};
976 EXPECT_CALL(context, get_memory());
977 EXPECT_CALL(memory,
get(nullifier_addr)).WillOnce(ReturnRef(
nullifier));
978 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(
address));
980 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
982 EXPECT_CALL(context, get_is_static).WillOnce(Return(
false));
983 EXPECT_CALL(
merkle_db, get_tree_state).WillOnce(Return(tree_state));
985 .WillOnce(Throw(NullifierCollisionException(
"Nullifier collision")));
990TEST_F(ExecutionSimulationTest, Set)
996 EXPECT_CALL(context, get_memory());
997 EXPECT_CALL(alu, truncate(
value,
static_cast<MemoryTag>(
dst_tag))).WillOnce(Return(MemoryValue::from<uint8_t>(7)));
998 EXPECT_CALL(memory, set(
dst_addr, MemoryValue::from<uint8_t>(7)));
999 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
1004TEST_F(ExecutionSimulationTest, Cast)
1011 EXPECT_CALL(context, get_memory()).WillOnce(ReturnRef(memory));
1012 EXPECT_CALL(memory,
get(src_addr)).WillOnce(ReturnRef(
value));
1015 .WillOnce(Return(MemoryValue::from<uint1_t>(1)));
1016 EXPECT_CALL(memory, set(
dst_addr, MemoryValue::from<uint1_t>(1)));
1017 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
1027 EXPECT_CALL(context, get_memory());
1028 EXPECT_CALL(gas_tracker, consume_gas);
1034TEST_F(ExecutionSimulationTest, EccAdd)
1044 MemoryValue p_x = MemoryValue::from<FF>(
FF(
"0x04c95d1b26d63d46918a156cae92db1bcbc4072a27ec81dc82ea959abdbcf16a"));
1045 MemoryValue p_y = MemoryValue::from<FF>(
FF(
"0x035b6dd9e63c1370462c74775765d07fc21fd1093cc988149d3aa763bb3dbb60"));
1048 MemoryValue q_x = MemoryValue::from<FF>(
FF(
"0x009242167ec31949c00cbe441cd36757607406e87844fa2c8c4364a4403e66d7"));
1049 MemoryValue q_y = MemoryValue::from<FF>(
FF(
"0x0fe3016d64cfa8045609f375284b6b739b5fa282e4cbb75cc7f1687ecc7420e3"));
1054 EXPECT_CALL(context, get_memory()).WillRepeatedly(ReturnRef(memory));
1055 EXPECT_CALL(Const(memory),
get(p_x_addr)).WillOnce(ReturnRef(p_x));
1056 EXPECT_CALL(memory,
get(p_y_addr)).WillOnce(ReturnRef(p_y));
1057 EXPECT_CALL(memory,
get(p_is_inf_addr)).WillOnce(ReturnRef(zero));
1058 EXPECT_CALL(memory,
get(q_x_addr)).WillOnce(ReturnRef(q_x));
1059 EXPECT_CALL(memory,
get(q_y_addr)).WillOnce(ReturnRef(q_y));
1060 EXPECT_CALL(memory,
get(q_is_inf_addr)).WillOnce(ReturnRef(zero));
1062 EXPECT_CALL(gas_tracker, consume_gas);
1065 EXPECT_CALL(ecc, add(_, _, _,
dst_addr));
1068 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);
1071TEST_F(ExecutionSimulationTest, ToRadixBE)
1080 MemoryValue radix = MemoryValue::from<uint32_t>(16);
1082 MemoryValue::from<uint8_t>(0x55),
1083 MemoryValue::from<uint8_t>(0x00) };
1084 MemoryValue num_limbs = MemoryValue::from<uint32_t>(3);
1086 uint32_t num_p_limbs = 64;
1088 EXPECT_CALL(context, get_memory()).WillOnce(ReturnRef(memory));
1089 EXPECT_CALL(memory,
get(value_addr)).WillOnce(ReturnRef(
value));
1090 EXPECT_CALL(memory,
get(radix_addr)).WillOnce(ReturnRef(radix));
1091 EXPECT_CALL(memory,
get(num_limbs_addr)).WillOnce(ReturnRef(num_limbs));
1092 EXPECT_CALL(memory,
get(is_output_bits_addr)).WillOnce(ReturnRef(
is_output_bits));
1094 EXPECT_CALL(greater_than,
gt(radix.as<uint32_t>(), 256)).WillOnce(Return(
false));
1095 EXPECT_CALL(greater_than,
gt(num_limbs.as<uint32_t>(), num_p_limbs)).WillOnce(Return(
false));
1097 EXPECT_CALL(gas_tracker, consume_gas);
1098 EXPECT_CALL(to_radix, to_be_radix);
1100 execution.to_radix_be(context, value_addr, radix_addr, num_limbs_addr, is_output_bits_addr,
dst_addr);
1103TEST_F(ExecutionSimulationTest, EmitUnencryptedLog)
1107 MemoryValue log_size = MemoryValue::from<uint32_t>(10);
1110 EXPECT_CALL(context, get_memory());
1111 EXPECT_CALL(memory,
get(log_size_offset)).WillOnce(ReturnRef(log_size));
1113 EXPECT_CALL(context, get_address).WillOnce(ReturnRef(
address));
1115 EXPECT_CALL(emit_unencrypted_log, emit_unencrypted_log(_, _,
address, log_offset, log_size.as<uint32_t>()));
1117 EXPECT_CALL(gas_tracker, consume_gas(Gas{ log_size.as<uint32_t>(), log_size.as<uint32_t>() }));
1119 execution.emit_unencrypted_log(context, log_size_offset, log_offset);
1122TEST_F(ExecutionSimulationTest, SendL2ToL1Msg)
1131 auto content_value = MemoryValue::from<FF>(
content);
1135 TrackedSideEffects side_effects_states;
1137 side_effects_states.l2_to_l1_messages.push_back(dummy_msg);
1139 TrackedSideEffects side_effects_states_after = side_effects_states;
1140 side_effects_states_after.l2_to_l1_messages.push_back(dummy_msg);
1142 EXPECT_CALL(context, get_memory());
1144 EXPECT_CALL(context, get_side_effect_tracker);
1146 EXPECT_CALL(memory,
get(recipient_addr)).WillOnce(ReturnRef(
recipient));
1147 EXPECT_CALL(memory,
get(content_addr)).WillOnce(ReturnRef(content_value));
1149 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
1151 EXPECT_CALL(context, get_is_static).WillOnce(Return(
false));
1153 EXPECT_CALL(
side_effect_tracker, get_side_effects()).WillOnce(ReturnRef(side_effects_states));
1155 .WillOnce(Return());
1157 execution.send_l2_to_l1_msg(context, recipient_addr, content_addr);
1160TEST_F(ExecutionSimulationTest, SendL2ToL1MsgStaticCall)
1165 auto recipient = MemoryValue::from<FF>(42);
1166 auto content = MemoryValue::from<FF>(27);
1168 TrackedSideEffects side_effects_states;
1170 EXPECT_CALL(context, get_memory());
1172 EXPECT_CALL(memory,
get(recipient_addr)).WillOnce(ReturnRef(
recipient));
1173 EXPECT_CALL(memory,
get(content_addr)).WillOnce(ReturnRef(
content));
1175 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
1177 EXPECT_CALL(context, get_is_static).WillOnce(Return(
true));
1180 "Static call cannot update the state");
1183TEST_F(ExecutionSimulationTest, SendL2ToL1MsgLimitReached)
1188 auto recipient = MemoryValue::from<FF>(42);
1189 auto content = MemoryValue::from<FF>(27);
1191 TrackedSideEffects side_effects_states;
1193 side_effects_states.l2_to_l1_messages.push_back(
1194 ScopedL2ToL1Message{ .message = { .recipient =
EthAddress(0x12345678), .content = 0x12345678 },
1195 .contract_address = 0x12345678 });
1198 EXPECT_CALL(context, get_memory());
1199 EXPECT_CALL(context, get_side_effect_tracker);
1201 EXPECT_CALL(memory,
get(recipient_addr)).WillOnce(ReturnRef(
recipient));
1202 EXPECT_CALL(memory,
get(content_addr)).WillOnce(ReturnRef(
content));
1204 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
1206 EXPECT_CALL(context, get_is_static).WillOnce(Return(
false));
1208 EXPECT_CALL(
side_effect_tracker, get_side_effects()).WillOnce(ReturnRef(side_effects_states));
1211 "SENDL2TOL1MSG: Maximum number of L2 to L1 messages reached");
1214TEST_F(ExecutionSimulationTest, Sha256Compression)
1220 EXPECT_CALL(context, get_memory());
1221 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
1222 EXPECT_CALL(sha256, compression(_, state_address, input_address,
dst_address));
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessageRegex)
#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
StrictMock< MockContext > context
AztecAddress contract_address
void debug_log(Args &&... args)
Compile-time debug logging helper.
InstructionInfoDB instruction_info_db
smt_circuit::STerm shr(smt_circuit::STerm v0, smt_circuit::STerm v1, smt_solver::Solver *solver)
Right shift operation.
smt_circuit::STerm shl(smt_circuit::STerm v0, smt_circuit::STerm v1, smt_solver::Solver *solver)
Left shift operation without truncation.
StandardAffinePoint< AvmFlavorSettings::EmbeddedCurve::AffineElement > EmbeddedCurvePoint
uint256_t get_tag_max_value(ValueTag tag)
TEST_F(IPATest, ChallengesAreZero)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
unsigned __int128 uint128_t
bb::crypto::Poseidon2< bb::crypto::Poseidon2Bn254ScalarFieldParams > poseidon2
static constexpr uint256_t modulus
SideEffectTracker side_effect_tracker
NiceMock< MockContextProvider > context_provider
NiceMock< MockExecution > execution
NoopCallStackMetadataCollector call_stack_metadata_collector