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<MockEmitPublicLog> emit_public_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,
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)
628 auto slot = MemoryValue::from<FF>(42);
629 auto contract_address = MemoryValue::from<AztecAddress>(
address);
631 EXPECT_CALL(context, get_memory());
633 EXPECT_CALL(memory,
get(slot_addr)).WillOnce(ReturnRef(
slot));
634 EXPECT_CALL(memory,
get(contract_address_addr)).WillOnce(ReturnRef(contract_address));
637 EXPECT_CALL(memory, set(
dst_addr, MemoryValue::from<FF>(7)));
638 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
643TEST_F(ExecutionSimulationTest, SStore)
648 auto slot = MemoryValue::from<FF>(42);
649 auto value = MemoryValue::from<FF>(7);
650 TreeStates tree_state = {};
651 EXPECT_CALL(context, get_memory());
653 EXPECT_CALL(memory,
get(slot_addr)).WillOnce(ReturnRef(
slot));
654 EXPECT_CALL(memory,
get(value_addr)).WillOnce(ReturnRef(
value));
655 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(
address));
657 EXPECT_CALL(
merkle_db, get_tree_state).WillOnce(Return(tree_state));
658 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 1 }));
660 EXPECT_CALL(context, get_is_static).WillOnce(Return(
false));
664 execution.sstore(context, value_addr, slot_addr);
667TEST_F(ExecutionSimulationTest, SStoreDuringStaticCall)
672 auto slot = MemoryValue::from<FF>(42);
673 auto value = MemoryValue::from<FF>(7);
674 EXPECT_CALL(context, get_memory());
676 EXPECT_CALL(memory,
get(slot_addr)).WillOnce(ReturnRef(
slot));
677 EXPECT_CALL(memory,
get(value_addr)).WillOnce(ReturnRef(
value));
678 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(
address));
680 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 1 }));
682 EXPECT_CALL(context, get_is_static).WillOnce(Return(
true));
686TEST_F(ExecutionSimulationTest, SStoreLimitReached)
691 auto slot = MemoryValue::from<FF>(42);
692 auto value = MemoryValue::from<FF>(7);
693 TreeStates tree_state = {};
695 EXPECT_CALL(context, get_memory());
697 EXPECT_CALL(memory,
get(slot_addr)).WillOnce(ReturnRef(
slot));
698 EXPECT_CALL(memory,
get(value_addr)).WillOnce(ReturnRef(
value));
699 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(
address));
701 EXPECT_CALL(
merkle_db, get_tree_state).WillOnce(Return(tree_state));
702 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 1 }));
704 EXPECT_CALL(context, get_is_static).WillOnce(Return(
false));
707 "SSTORE: Maximum number of data writes reached");
710TEST_F(ExecutionSimulationTest, SStoreLimitReachedSquashed)
715 auto slot = MemoryValue::from<FF>(42);
716 auto value = MemoryValue::from<FF>(7);
717 TreeStates tree_state = {};
719 EXPECT_CALL(context, get_memory());
721 EXPECT_CALL(memory,
get(slot_addr)).WillOnce(ReturnRef(
slot));
722 EXPECT_CALL(memory,
get(value_addr)).WillOnce(ReturnRef(
value));
723 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(
address));
726 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
728 EXPECT_CALL(context, get_is_static).WillOnce(Return(
false));
732 execution.sstore(context, value_addr, slot_addr);
735TEST_F(ExecutionSimulationTest, NoteHashExists)
741 auto unique_note_hash = MemoryValue::from<FF>(42);
742 auto leaf_index = MemoryValue::from<uint64_t>(7);
744 EXPECT_CALL(context, get_memory());
745 EXPECT_CALL(memory,
get(unique_note_hash_addr)).WillOnce(ReturnRef(unique_note_hash));
746 EXPECT_CALL(memory,
get(leaf_index_addr)).WillOnce(ReturnRef(leaf_index));
748 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
752 EXPECT_CALL(
merkle_db, note_hash_exists(leaf_index.as<uint64_t>(), unique_note_hash.as<
FF>()))
753 .WillOnce(Return(
true));
755 EXPECT_CALL(memory, set(
dst_addr, MemoryValue::from<uint1_t>(1)));
757 execution.note_hash_exists(context, unique_note_hash_addr, leaf_index_addr,
dst_addr);
760TEST_F(ExecutionSimulationTest, NoteHashExistsOutOfRange)
766 auto unique_note_hash = MemoryValue::from<FF>(42);
769 EXPECT_CALL(context, get_memory());
770 EXPECT_CALL(memory,
get(unique_note_hash_addr)).WillOnce(ReturnRef(unique_note_hash));
771 EXPECT_CALL(memory,
get(leaf_index_addr)).WillOnce(ReturnRef(leaf_index));
773 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
777 EXPECT_CALL(memory, set(
dst_addr, MemoryValue::from<uint1_t>(0)));
779 execution.note_hash_exists(context, unique_note_hash_addr, leaf_index_addr,
dst_addr);
782TEST_F(ExecutionSimulationTest, EmitNoteHash)
786 auto note_hash = MemoryValue::from<FF>(42);
788 TreeStates tree_state = {};
790 EXPECT_CALL(context, get_memory());
791 EXPECT_CALL(memory,
get(note_hash_addr)).WillOnce(ReturnRef(
note_hash));
792 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(
address));
794 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
796 EXPECT_CALL(context, get_is_static).WillOnce(Return(
false));
797 EXPECT_CALL(
merkle_db, get_tree_state).WillOnce(Return(tree_state));
800 execution.emit_note_hash(context, note_hash_addr);
803TEST_F(ExecutionSimulationTest, EmitNoteHashDuringStaticCall)
807 auto note_hash = MemoryValue::from<FF>(42);
810 EXPECT_CALL(context, get_memory());
811 EXPECT_CALL(memory,
get(note_hash_addr)).WillOnce(ReturnRef(
note_hash));
812 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(
address));
814 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
816 EXPECT_CALL(context, get_is_static).WillOnce(Return(
true));
820TEST_F(ExecutionSimulationTest, EmitNoteHashLimitReached)
824 auto note_hash = MemoryValue::from<FF>(42);
826 TreeStates tree_state = {};
829 EXPECT_CALL(context, get_memory());
830 EXPECT_CALL(memory,
get(note_hash_addr)).WillOnce(ReturnRef(
note_hash));
831 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(
address));
833 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
835 EXPECT_CALL(context, get_is_static).WillOnce(Return(
false));
836 EXPECT_CALL(
merkle_db, get_tree_state).WillOnce(Return(tree_state));
839 "EMITNOTEHASH: Maximum number of note hashes reached");
842TEST_F(ExecutionSimulationTest, L1ToL2MessageExists)
848 auto msg_hash = MemoryValue::from<FF>(42);
849 auto leaf_index = MemoryValue::from<uint64_t>(7);
851 EXPECT_CALL(context, get_memory());
852 EXPECT_CALL(memory,
get(msg_hash_addr)).WillOnce(ReturnRef(msg_hash));
853 EXPECT_CALL(memory,
get(leaf_index_addr)).WillOnce(ReturnRef(leaf_index));
855 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
859 EXPECT_CALL(
merkle_db, l1_to_l2_msg_exists(leaf_index.as<uint64_t>(), msg_hash.as<
FF>())).WillOnce(Return(
true));
861 EXPECT_CALL(memory, set(
dst_addr, MemoryValue::from<uint1_t>(1)));
863 execution.l1_to_l2_message_exists(context, msg_hash_addr, leaf_index_addr,
dst_addr);
866TEST_F(ExecutionSimulationTest, L1ToL2MessageExistsOutOfRange)
872 auto msg_hash = MemoryValue::from<FF>(42);
875 EXPECT_CALL(context, get_memory());
876 EXPECT_CALL(memory,
get(msg_hash_addr)).WillOnce(ReturnRef(msg_hash));
877 EXPECT_CALL(memory,
get(leaf_index_addr)).WillOnce(ReturnRef(leaf_index));
879 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
883 EXPECT_CALL(memory, set(
dst_addr, MemoryValue::from<uint1_t>(0)));
885 execution.l1_to_l2_message_exists(context, msg_hash_addr, leaf_index_addr,
dst_addr);
888TEST_F(ExecutionSimulationTest, NullifierExists)
893 auto nullifier = MemoryValue::from<FF>(42);
895 EXPECT_CALL(context, get_memory());
896 EXPECT_CALL(memory,
get(nullifier_offset)).WillOnce(ReturnRef(nullifier));
898 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
900 EXPECT_CALL(
merkle_db, siloed_nullifier_exists(nullifier.as_ff())).WillOnce(Return(
true));
902 EXPECT_CALL(memory, set(exists_offset, MemoryValue::from<uint1_t>(1)));
904 execution.nullifier_exists(context, nullifier_offset, exists_offset);
907TEST_F(ExecutionSimulationTest, EmitNullifier)
911 auto nullifier = MemoryValue::from<FF>(42);
913 TreeStates tree_state = {};
915 EXPECT_CALL(context, get_memory());
916 EXPECT_CALL(memory,
get(nullifier_addr)).WillOnce(ReturnRef(nullifier));
917 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(
address));
919 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
921 EXPECT_CALL(context, get_is_static).WillOnce(Return(
false));
922 EXPECT_CALL(
merkle_db, get_tree_state).WillOnce(Return(tree_state));
923 EXPECT_CALL(
merkle_db, nullifier_write(
address, nullifier.as_ff())).WillOnce(Return());
925 execution.emit_nullifier(context, nullifier_addr);
928TEST_F(ExecutionSimulationTest, EmitNullifierDuringStaticCall)
932 auto nullifier = MemoryValue::from<FF>(42);
935 EXPECT_CALL(context, get_memory());
936 EXPECT_CALL(memory,
get(nullifier_addr)).WillOnce(ReturnRef(nullifier));
937 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(
address));
939 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
941 EXPECT_CALL(context, get_is_static).WillOnce(Return(
true));
945TEST_F(ExecutionSimulationTest, EmitNullifierLimitReached)
949 auto nullifier = MemoryValue::from<FF>(42);
951 TreeStates tree_state = {};
954 EXPECT_CALL(context, get_memory());
955 EXPECT_CALL(memory,
get(nullifier_addr)).WillOnce(ReturnRef(nullifier));
956 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(
address));
958 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
960 EXPECT_CALL(context, get_is_static).WillOnce(Return(
false));
961 EXPECT_CALL(
merkle_db, get_tree_state).WillOnce(Return(tree_state));
964 "EMITNULLIFIER: Maximum number of nullifiers reached");
967TEST_F(ExecutionSimulationTest, EmitNullifierCollision)
971 auto nullifier = MemoryValue::from<FF>(42);
973 TreeStates tree_state = {};
975 EXPECT_CALL(context, get_memory());
976 EXPECT_CALL(memory,
get(nullifier_addr)).WillOnce(ReturnRef(nullifier));
977 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(
address));
979 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
981 EXPECT_CALL(context, get_is_static).WillOnce(Return(
false));
982 EXPECT_CALL(
merkle_db, get_tree_state).WillOnce(Return(tree_state));
984 .WillOnce(Throw(NullifierCollisionException(
"Nullifier collision")));
989TEST_F(ExecutionSimulationTest, Set)
995 EXPECT_CALL(context, get_memory());
996 EXPECT_CALL(alu, truncate(
value,
static_cast<MemoryTag>(
dst_tag))).WillOnce(Return(MemoryValue::from<uint8_t>(7)));
997 EXPECT_CALL(memory, set(
dst_addr, MemoryValue::from<uint8_t>(7)));
998 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
1003TEST_F(ExecutionSimulationTest, Cast)
1010 EXPECT_CALL(context, get_memory()).WillOnce(ReturnRef(memory));
1011 EXPECT_CALL(memory,
get(src_addr)).WillOnce(ReturnRef(
value));
1014 .WillOnce(Return(MemoryValue::from<uint1_t>(1)));
1015 EXPECT_CALL(memory, set(
dst_addr, MemoryValue::from<uint1_t>(1)));
1016 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
1026 EXPECT_CALL(context, get_memory());
1027 EXPECT_CALL(gas_tracker, consume_gas);
1033TEST_F(ExecutionSimulationTest, EccAdd)
1043 MemoryValue p_x = MemoryValue::from<FF>(
FF(
"0x04c95d1b26d63d46918a156cae92db1bcbc4072a27ec81dc82ea959abdbcf16a"));
1044 MemoryValue p_y = MemoryValue::from<FF>(
FF(
"0x035b6dd9e63c1370462c74775765d07fc21fd1093cc988149d3aa763bb3dbb60"));
1047 MemoryValue q_x = MemoryValue::from<FF>(
FF(
"0x009242167ec31949c00cbe441cd36757607406e87844fa2c8c4364a4403e66d7"));
1048 MemoryValue q_y = MemoryValue::from<FF>(
FF(
"0x0fe3016d64cfa8045609f375284b6b739b5fa282e4cbb75cc7f1687ecc7420e3"));
1053 EXPECT_CALL(context, get_memory()).WillRepeatedly(ReturnRef(memory));
1054 EXPECT_CALL(Const(memory),
get(p_x_addr)).WillOnce(ReturnRef(p_x));
1055 EXPECT_CALL(memory,
get(p_y_addr)).WillOnce(ReturnRef(p_y));
1056 EXPECT_CALL(memory,
get(p_is_inf_addr)).WillOnce(ReturnRef(zero));
1057 EXPECT_CALL(memory,
get(q_x_addr)).WillOnce(ReturnRef(q_x));
1058 EXPECT_CALL(memory,
get(q_y_addr)).WillOnce(ReturnRef(q_y));
1059 EXPECT_CALL(memory,
get(q_is_inf_addr)).WillOnce(ReturnRef(zero));
1061 EXPECT_CALL(gas_tracker, consume_gas);
1064 EXPECT_CALL(ecc, add(_, _, _,
dst_addr));
1067 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);
1070TEST_F(ExecutionSimulationTest, ToRadixBE)
1079 MemoryValue radix = MemoryValue::from<uint32_t>(16);
1081 MemoryValue::from<uint8_t>(0x55),
1082 MemoryValue::from<uint8_t>(0x00) };
1083 MemoryValue num_limbs = MemoryValue::from<uint32_t>(3);
1085 uint32_t num_p_limbs = 64;
1087 EXPECT_CALL(context, get_memory()).WillOnce(ReturnRef(memory));
1088 EXPECT_CALL(memory,
get(value_addr)).WillOnce(ReturnRef(
value));
1089 EXPECT_CALL(memory,
get(radix_addr)).WillOnce(ReturnRef(radix));
1090 EXPECT_CALL(memory,
get(num_limbs_addr)).WillOnce(ReturnRef(num_limbs));
1091 EXPECT_CALL(memory,
get(is_output_bits_addr)).WillOnce(ReturnRef(
is_output_bits));
1093 EXPECT_CALL(greater_than,
gt(radix.as<uint32_t>(), 256)).WillOnce(Return(
false));
1094 EXPECT_CALL(greater_than,
gt(num_limbs.as<uint32_t>(), num_p_limbs)).WillOnce(Return(
false));
1096 EXPECT_CALL(gas_tracker, consume_gas);
1097 EXPECT_CALL(to_radix, to_be_radix);
1099 execution.to_radix_be(context, value_addr, radix_addr, num_limbs_addr, is_output_bits_addr,
dst_addr);
1102TEST_F(ExecutionSimulationTest, EmitPublicLog)
1106 MemoryValue log_size = MemoryValue::from<uint32_t>(10);
1109 EXPECT_CALL(context, get_memory());
1110 EXPECT_CALL(memory,
get(log_size_offset)).WillOnce(ReturnRef(log_size));
1112 EXPECT_CALL(context, get_address).WillOnce(ReturnRef(
address));
1114 EXPECT_CALL(emit_public_log, emit_public_log(_, _,
address, log_offset, log_size.as<uint32_t>()));
1116 EXPECT_CALL(gas_tracker, consume_gas(Gas{ log_size.as<uint32_t>(), log_size.as<uint32_t>() }));
1118 execution.emit_public_log(context, log_size_offset, log_offset);
1121TEST_F(ExecutionSimulationTest, SendL2ToL1Msg)
1130 auto content_value = MemoryValue::from<FF>(content);
1132 ScopedL2ToL1Message dummy_msg = { .message = { .recipient =
recipient_address, .content = content },
1133 .contract_address = contract_address };
1134 TrackedSideEffects side_effects_states;
1136 side_effects_states.l2_to_l1_messages.push_back(dummy_msg);
1138 TrackedSideEffects side_effects_states_after = side_effects_states;
1139 side_effects_states_after.l2_to_l1_messages.push_back(dummy_msg);
1141 EXPECT_CALL(context, get_memory());
1142 EXPECT_CALL(context, get_address).WillOnce(ReturnRef(contract_address));
1143 EXPECT_CALL(context, get_side_effect_tracker);
1145 EXPECT_CALL(memory,
get(recipient_addr)).WillOnce(ReturnRef(recipient));
1146 EXPECT_CALL(memory,
get(content_addr)).WillOnce(ReturnRef(content_value));
1148 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
1152 EXPECT_CALL(context, get_is_static).WillOnce(Return(
false));
1154 EXPECT_CALL(
side_effect_tracker, get_side_effects()).WillOnce(ReturnRef(side_effects_states));
1156 .WillOnce(Return());
1158 execution.send_l2_to_l1_msg(context, recipient_addr, content_addr);
1161TEST_F(ExecutionSimulationTest, SendL2ToL1MsgTooLargeRecipient)
1167 auto content = MemoryValue::from<FF>(27);
1169 TrackedSideEffects side_effects_states;
1171 EXPECT_CALL(context, get_memory());
1173 EXPECT_CALL(memory,
get(recipient_addr)).WillOnce(ReturnRef(recipient));
1174 EXPECT_CALL(memory,
get(content_addr)).WillOnce(ReturnRef(content));
1176 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
1181 "SENDL2TOL1MSG: Recipient address is too large");
1184TEST_F(ExecutionSimulationTest, SendL2ToL1MsgStaticCall)
1189 auto recipient = MemoryValue::from<FF>(42);
1190 auto content = MemoryValue::from<FF>(27);
1192 TrackedSideEffects side_effects_states;
1194 EXPECT_CALL(context, get_memory());
1196 EXPECT_CALL(memory,
get(recipient_addr)).WillOnce(ReturnRef(recipient));
1197 EXPECT_CALL(memory,
get(content_addr)).WillOnce(ReturnRef(content));
1199 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
1203 EXPECT_CALL(context, get_is_static).WillOnce(Return(
true));
1206 "Static call cannot update the state");
1209TEST_F(ExecutionSimulationTest, SendL2ToL1MsgLimitReached)
1214 auto recipient = MemoryValue::from<FF>(42);
1215 auto content = MemoryValue::from<FF>(27);
1217 TrackedSideEffects side_effects_states;
1219 side_effects_states.l2_to_l1_messages.push_back(
1220 ScopedL2ToL1Message{ .message = { .recipient =
EthAddress(0x12345678), .content = 0x12345678 },
1221 .contract_address = 0x12345678 });
1224 EXPECT_CALL(context, get_memory());
1225 EXPECT_CALL(context, get_side_effect_tracker);
1227 EXPECT_CALL(memory,
get(recipient_addr)).WillOnce(ReturnRef(recipient));
1228 EXPECT_CALL(memory,
get(content_addr)).WillOnce(ReturnRef(content));
1230 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
1234 EXPECT_CALL(context, get_is_static).WillOnce(Return(
false));
1236 EXPECT_CALL(
side_effect_tracker, get_side_effects()).WillOnce(ReturnRef(side_effects_states));
1239 "SENDL2TOL1MSG: Maximum number of L2 to L1 messages reached");
1242TEST_F(ExecutionSimulationTest, Sha256Compression)
1248 EXPECT_CALL(context, get_memory());
1249 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
1250 EXPECT_CALL(sha256, compression(_, state_address, input_address,
dst_address));
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessageRegex)
#define NOTE_HASH_TREE_LEAF_COUNT
#define MAX_ETH_ADDRESS_VALUE
#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
static TaggedValue from(T value)
Native Poseidon2 hash function implementation.
Applies the Poseidon2 permutation function from https://eprint.iacr.org/2023/323.
ExecutionIdManager execution_id_manager
StrictMock< MockContext > context
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.
AVM range check gadget for witness generation.
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