3#include <gmock/gmock.h>
4#include <gtest/gtest.h>
46using ::testing::NiceMock;
47using ::testing::Return;
48using ::testing::ReturnRef;
49using ::testing::StrictMock;
50using ::testing::Throw;
53class TestingExecution :
public Execution {
55 using Execution::Execution;
57 void set_gas_tracker(GasTrackerInterface& gas_tracker) { this->testing_gas_tracker = &gas_tracker; }
59 GasTrackerInterface& get_gas_tracker()
override {
return *testing_gas_tracker; }
62 GasTrackerInterface* testing_gas_tracker;
65class ExecutionSimulationTest :
public ::testing::Test {
67 ExecutionSimulationTest()
69 ON_CALL(
context, get_memory).WillByDefault(ReturnRef(
memory));
70 ON_CALL(
context, get_bytecode_manager).WillByDefault(ReturnRef(bytecode_manager));
74 StrictMock<MockAlu> alu;
75 StrictMock<MockBitwise>
bitwise;
76 StrictMock<MockMemory>
memory;
77 StrictMock<MockExecutionComponentsProvider> execution_components;
78 StrictMock<MockContext>
context;
80 StrictMock<MockInternalCallStackManager> internal_call_stack_manager;
81 StrictMock<MockKeccakF1600> keccakf1600;
82 StrictMock<MockGetContractInstance> get_contract_instance;
83 EventEmitter<ExecutionEvent> execution_event_emitter;
84 EventEmitter<ContextStackEvent> context_stack_event_emitter;
88 StrictMock<MockGasTracker> gas_tracker;
89 StrictMock<MockHighLevelMerkleDB>
merkle_db;
90 StrictMock<MockGreaterThan> greater_than;
92 StrictMock<MockEcc> ecc;
93 StrictMock<MockToRadix> to_radix;
94 StrictMock<MockEmitUnencryptedLog> emit_unencrypted_log;
95 StrictMock<MockBytecodeManager> bytecode_manager;
96 StrictMock<MockSha256>
sha256;
97 TestingExecution
execution = TestingExecution(alu,
104 execution_components,
108 execution_event_emitter,
109 context_stack_event_emitter,
112 get_contract_instance,
113 emit_unencrypted_log,
120TEST_F(ExecutionSimulationTest, Add)
125 EXPECT_CALL(context, get_memory);
126 EXPECT_CALL(memory, get).Times(2).WillOnce(ReturnRef(
a)).WillOnce(ReturnRef(
b));
127 EXPECT_CALL(alu, add(
a,
b)).WillOnce(Return(MemoryValue::from<uint32_t>(9)));
128 EXPECT_CALL(memory, set(6, MemoryValue::from<uint32_t>(9)));
129 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
134TEST_F(ExecutionSimulationTest, Sub)
139 EXPECT_CALL(context, get_memory);
140 EXPECT_CALL(memory, get).Times(2).WillOnce(ReturnRef(
a)).WillOnce(ReturnRef(
b));
141 EXPECT_CALL(alu, sub(
a,
b)).WillOnce(Return(MemoryValue::from<uint64_t>(2)));
142 EXPECT_CALL(memory, set(3, MemoryValue::from<uint64_t>(2)));
143 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
148TEST_F(ExecutionSimulationTest, Mul)
151 auto a = MemoryValue::from<uint128_t>(max);
152 auto b = MemoryValue::from<uint128_t>(max - 3);
154 EXPECT_CALL(context, get_memory);
155 EXPECT_CALL(memory, get).Times(2).WillOnce(ReturnRef(
a)).WillOnce(ReturnRef(
b));
156 EXPECT_CALL(alu, mul(
a,
b)).WillOnce(Return(MemoryValue::from<uint128_t>(4)));
157 EXPECT_CALL(memory, set(3, MemoryValue::from<uint128_t>(4)));
158 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
163TEST_F(ExecutionSimulationTest, Div)
165 auto a = MemoryValue::from<uint128_t>(6);
166 auto b = MemoryValue::from<uint128_t>(3);
168 EXPECT_CALL(context, get_memory);
169 EXPECT_CALL(memory, get).Times(2).WillOnce(ReturnRef(
a)).WillOnce(ReturnRef(
b));
170 EXPECT_CALL(alu, div(
a,
b)).WillOnce(Return(MemoryValue::from<uint128_t>(2)));
171 EXPECT_CALL(memory, set(3, MemoryValue::from<uint128_t>(2)));
172 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
177TEST_F(ExecutionSimulationTest, FDiv)
179 auto a = MemoryValue::from<FF>(FF::modulus - 4);
180 auto b = MemoryValue::from<FF>(2);
182 EXPECT_CALL(context, get_memory);
183 EXPECT_CALL(memory, get).Times(2).WillOnce(ReturnRef(
a)).WillOnce(ReturnRef(
b));
184 EXPECT_CALL(alu, fdiv(
a,
b)).WillOnce(Return(MemoryValue::from<FF>(FF::modulus - 2)));
185 EXPECT_CALL(memory, set(3, MemoryValue::from<FF>(FF::modulus - 2)));
186 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
191TEST_F(ExecutionSimulationTest, Shl)
193 auto a = MemoryValue::from<uint32_t>(64);
194 auto b = MemoryValue::from<uint32_t>(2);
196 EXPECT_CALL(context, get_memory);
197 EXPECT_CALL(memory, get).Times(2).WillOnce(ReturnRef(
a)).WillOnce(ReturnRef(
b));
198 EXPECT_CALL(alu,
shl(
a,
b)).WillOnce(Return(MemoryValue::from<uint32_t>(256)));
199 EXPECT_CALL(memory, set(3, MemoryValue::from<uint32_t>(256)));
200 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
205TEST_F(ExecutionSimulationTest, Shr)
207 auto a = MemoryValue::from<uint64_t>(64);
208 auto b = MemoryValue::from<uint64_t>(2);
210 EXPECT_CALL(context, get_memory);
211 EXPECT_CALL(memory, get).Times(2).WillOnce(ReturnRef(
a)).WillOnce(ReturnRef(
b));
212 EXPECT_CALL(alu,
shr(
a,
b)).WillOnce(Return(MemoryValue::from<uint64_t>(16)));
213 EXPECT_CALL(memory, set(3, MemoryValue::from<uint64_t>(16)));
214 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
221TEST_F(ExecutionSimulationTest, Call)
226 MemoryValue nested_address_value = MemoryValue::from<FF>(nested_address);
227 MemoryValue l2_gas_allocated = MemoryValue::from<uint32_t>(6);
228 MemoryValue da_gas_allocated = MemoryValue::from<uint32_t>(7);
229 MemoryValue cd_size = MemoryValue::from<uint32_t>(8);
230 AppendOnlyTreeSnapshot written_public_data_slots_tree_snapshot = AppendOnlyTreeSnapshot{
232 .nextAvailableLeafIndex = 10,
234 TreeStates tree_states = TreeStates {
238 .nextAvailableLeafIndex = 9,
245 .nextAvailableLeafIndex = 6,
249 .l1ToL2MessageTree = {
252 .nextAvailableLeafIndex = 3,
259 .nextAvailableLeafIndex = 1,
265 SideEffectStates side_effect_states = SideEffectStates{ .numUnencryptedLogs = 1, .numL2ToL1Messages = 2 };
267 EXPECT_CALL(gas_tracker, compute_gas_limit_for_call(Gas{ 6, 7 })).WillOnce(Return(Gas{ 2, 3 }));
268 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
271 EXPECT_CALL(context, get_context_id);
272 EXPECT_CALL(context, get_parent_id);
273 EXPECT_CALL(context, get_bytecode_manager).WillOnce(ReturnRef(bytecode_manager));
274 EXPECT_CALL(bytecode_manager, try_get_bytecode_id);
275 EXPECT_CALL(context, get_next_pc);
276 EXPECT_CALL(context, get_is_static).WillRepeatedly(Return(
false));
277 EXPECT_CALL(context, get_msg_sender).WillOnce(ReturnRef(parent_address));
278 EXPECT_CALL(context, get_transaction_fee).WillOnce(ReturnRef(zero));
279 EXPECT_CALL(context, get_parent_cd_addr);
280 EXPECT_CALL(context, get_parent_cd_size);
281 EXPECT_CALL(context, get_parent_gas_used);
282 EXPECT_CALL(context, get_parent_gas_limit);
283 EXPECT_CALL(context, get_written_public_data_slots_tree_snapshot)
284 .WillOnce(Return(written_public_data_slots_tree_snapshot));
285 EXPECT_CALL(context, get_side_effect_states).WillRepeatedly(ReturnRef(side_effect_states));
289 EXPECT_CALL(
merkle_db, get_tree_state).WillOnce(Return(tree_states));
291 EXPECT_CALL(context, get_memory);
292 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(parent_address));
293 EXPECT_CALL(memory,
get(1)).WillOnce(ReturnRef(l2_gas_allocated));
294 EXPECT_CALL(memory,
get(2)).WillOnce(ReturnRef(da_gas_allocated));
295 EXPECT_CALL(memory,
get(3)).WillOnce(ReturnRef(nested_address_value));
296 EXPECT_CALL(memory,
get(4)).WillOnce(ReturnRef(cd_size));
299 ON_CALL(*nested_context, halted())
300 .WillByDefault(Return(
true));
303 make_nested_context(nested_address,
313 .WillOnce(Return(
std::move(nested_context)));
324TEST_F(ExecutionSimulationTest, ExternalCallStaticnessPropagation)
330 MemoryValue nested_address_value = MemoryValue::from<FF>(nested_address);
331 MemoryValue l2_gas_allocated = MemoryValue::from<uint32_t>(6);
332 MemoryValue da_gas_allocated = MemoryValue::from<uint32_t>(7);
333 MemoryValue cd_size = MemoryValue::from<uint32_t>(8);
334 AppendOnlyTreeSnapshot written_public_data_slots_tree_snapshot = AppendOnlyTreeSnapshot{
336 .nextAvailableLeafIndex = 10,
338 TreeStates tree_states =
339 TreeStates{ .noteHashTree = { .tree = { .root = 10, .nextAvailableLeafIndex = 9 }, .counter = 8 },
340 .nullifierTree = { .tree = { .root = 7, .nextAvailableLeafIndex = 6 }, .counter = 5 },
341 .l1ToL2MessageTree = { .tree = { .root = 4, .nextAvailableLeafIndex = 3 }, .counter = 0 },
342 .publicDataTree = { .tree = { .root = 2, .nextAvailableLeafIndex = 1 }, .counter = 1 } };
343 SideEffectStates side_effect_states = SideEffectStates{ .numUnencryptedLogs = 1, .numL2ToL1Messages = 2 };
345 auto setup_context_expectations = [&](
bool parent_is_static) {
346 EXPECT_CALL(gas_tracker, compute_gas_limit_for_call(Gas{ 6, 7 })).WillOnce(Return(Gas{ 2, 3 }));
347 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
348 EXPECT_CALL(context, get_context_id);
349 EXPECT_CALL(context, get_parent_id);
350 EXPECT_CALL(context, get_bytecode_manager).WillOnce(ReturnRef(bytecode_manager));
351 EXPECT_CALL(bytecode_manager, try_get_bytecode_id);
352 EXPECT_CALL(context, get_next_pc);
353 EXPECT_CALL(context, get_is_static).WillRepeatedly(Return(parent_is_static));
354 EXPECT_CALL(context, get_msg_sender).WillOnce(ReturnRef(parent_address));
355 EXPECT_CALL(context, get_transaction_fee).WillOnce(ReturnRef(zero));
356 EXPECT_CALL(context, get_parent_cd_addr);
357 EXPECT_CALL(context, get_parent_cd_size);
358 EXPECT_CALL(context, get_parent_gas_used);
359 EXPECT_CALL(context, get_parent_gas_limit);
360 EXPECT_CALL(context, get_written_public_data_slots_tree_snapshot)
361 .WillOnce(Return(written_public_data_slots_tree_snapshot));
362 EXPECT_CALL(context, get_side_effect_states).WillRepeatedly(ReturnRef(side_effect_states));
364 EXPECT_CALL(
merkle_db, get_tree_state).WillOnce(Return(tree_states));
365 EXPECT_CALL(context, get_memory);
366 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(parent_address));
367 EXPECT_CALL(memory,
get(1)).WillOnce(ReturnRef(l2_gas_allocated));
368 EXPECT_CALL(memory,
get(2)).WillOnce(ReturnRef(da_gas_allocated));
369 EXPECT_CALL(memory,
get(3)).WillOnce(ReturnRef(nested_address_value));
370 EXPECT_CALL(memory,
get(4)).WillOnce(ReturnRef(cd_size));
373 auto create_nested_context = []() {
375 ON_CALL(*nested, halted()).WillByDefault(Return(
true));
380 setup_context_expectations(
false);
382 make_nested_context(nested_address,
392 .WillOnce(Return(create_nested_context()));
396 setup_context_expectations(
false);
398 make_nested_context(nested_address,
408 .WillOnce(Return(create_nested_context()));
409 execution.static_call(context, 1, 2, 3, 4, 5);
412 setup_context_expectations(
true);
414 make_nested_context(nested_address,
424 .WillOnce(Return(create_nested_context()));
428 setup_context_expectations(
true);
430 make_nested_context(nested_address,
440 .WillOnce(Return(create_nested_context()));
441 execution.static_call(context, 1, 2, 3, 4, 5);
444TEST_F(ExecutionSimulationTest, InternalCall)
446 uint32_t return_pc = 500;
447 uint32_t pc_loc = 11;
449 NiceMock<MockInternalCallStackManager> internal_call_stack_manager;
450 ON_CALL(context, get_internal_call_stack_manager).WillByDefault(ReturnRef(internal_call_stack_manager));
454 EXPECT_CALL(context, get_internal_call_stack_manager());
456 EXPECT_CALL(context, get_next_pc()).WillOnce(Return(return_pc));
457 EXPECT_CALL(internal_call_stack_manager, push(return_pc));
459 EXPECT_CALL(context, set_next_pc(pc_loc));
460 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
462 execution.internal_call(context, pc_loc);
466 EXPECT_CALL(context, get_internal_call_stack_manager());
468 EXPECT_CALL(internal_call_stack_manager, pop()).WillOnce(Return(return_pc));
470 EXPECT_CALL(context, set_next_pc(return_pc));
471 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
476TEST_F(ExecutionSimulationTest, GetEnvVarAddress)
479 EXPECT_CALL(context, get_address).WillOnce(ReturnRef(addr));
480 EXPECT_CALL(context, get_memory);
481 EXPECT_CALL(memory, set(1, MemoryValue::from<FF>(addr)));
482 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
487TEST_F(ExecutionSimulationTest, GetEnvVarChainId)
489 GlobalVariables globals;
491 EXPECT_CALL(context, get_globals).WillOnce(ReturnRef(globals));
492 EXPECT_CALL(context, get_memory);
493 EXPECT_CALL(memory, set(1, MemoryValue::from<FF>(1)));
494 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
499TEST_F(ExecutionSimulationTest, GetEnvVarIsStaticCall)
501 EXPECT_CALL(context, get_is_static).WillOnce(Return(
true));
502 EXPECT_CALL(context, get_memory);
503 EXPECT_CALL(memory, set(1, MemoryValue::from<uint1_t>(1)));
504 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
509TEST_F(ExecutionSimulationTest, GetEnvVarInvalidEnum)
511 EXPECT_CALL(context, get_memory);
512 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
514 EXPECT_THROW(
execution.get_env_var(context, 1, 255), std::runtime_error);
520TEST_F(ExecutionSimulationTest, Jump)
522 EXPECT_CALL(context, set_next_pc(120));
523 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
528TEST_F(ExecutionSimulationTest, SuccessCopyTrue)
530 EXPECT_CALL(context, get_memory);
531 EXPECT_CALL(context, get_last_success).WillOnce(Return(
true));
532 EXPECT_CALL(memory, set(10, MemoryValue::from<uint1_t>(1)));
533 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
538TEST_F(ExecutionSimulationTest, SuccessCopyFalse)
540 EXPECT_CALL(context, get_memory);
541 EXPECT_CALL(context, get_last_success).WillOnce(Return(
false));
542 EXPECT_CALL(memory, set(10, MemoryValue::from<uint1_t>(0)));
543 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
548TEST_F(ExecutionSimulationTest, RdSize)
550 EXPECT_CALL(context, get_memory);
551 EXPECT_CALL(context, get_last_rd_size).WillOnce(Return(42));
552 EXPECT_CALL(memory, set(10, MemoryValue::from<uint32_t>(42)));
553 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
558TEST_F(ExecutionSimulationTest, DebugLogEnabled)
564 uint16_t message_size = 5;
565 bool is_debug_logging_enabled =
true;
569 MemoryValue::from<FF>(
'H'),
570 MemoryValue::from<FF>(
'e'),
571 MemoryValue::from<FF>(
'l'),
572 MemoryValue::from<FF>(
'l'),
573 MemoryValue::from<FF>(
'o'),
579 MemoryValue fields_size = MemoryValue::from<uint32_t>(2);
581 EXPECT_CALL(context, get_memory);
582 EXPECT_CALL(memory,
get(fields_size_offset)).WillOnce(ReturnRef(fields_size));
583 EXPECT_CALL(memory,
get(message_offset + 0)).WillOnce(ReturnRef(message_data[0]));
584 EXPECT_CALL(memory,
get(message_offset + 1)).WillOnce(ReturnRef(message_data[1]));
585 EXPECT_CALL(memory,
get(message_offset + 2)).WillOnce(ReturnRef(message_data[2]));
586 EXPECT_CALL(memory,
get(message_offset + 3)).WillOnce(ReturnRef(message_data[3]));
587 EXPECT_CALL(memory,
get(message_offset + 4)).WillOnce(ReturnRef(message_data[4]));
588 EXPECT_CALL(memory,
get(fields_offset + 0)).WillOnce(ReturnRef(field1));
589 EXPECT_CALL(memory,
get(fields_offset + 1)).WillOnce(ReturnRef(
field2));
590 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
593 context, message_offset, fields_offset, fields_size_offset, message_size, is_debug_logging_enabled);
596TEST_F(ExecutionSimulationTest, DebugLogDisabled)
602 uint16_t message_size = 1UL << 15;
603 bool is_debug_logging_enabled =
false;
606 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
609 context, message_offset, fields_offset, fields_size_offset, message_size, is_debug_logging_enabled);
612TEST_F(ExecutionSimulationTest, DebugLogMessageTruncation)
618 uint16_t message_size = 150;
619 bool is_debug_logging_enabled =
true;
622 MemoryValue fields_size = MemoryValue::from<uint32_t>(0);
624 EXPECT_CALL(context, get_memory);
625 EXPECT_CALL(memory,
get(fields_size_offset)).WillOnce(ReturnRef(fields_size));
628 for (uint32_t i = 0; i < 100; ++i) {
629 MemoryValue char_val = MemoryValue::from<FF>(
'A' + (i % 26));
630 EXPECT_CALL(memory,
get(message_offset + i)).WillOnce(ReturnRef(char_val));
633 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
636 context, message_offset, fields_offset, fields_size_offset, message_size, is_debug_logging_enabled);
639TEST_F(ExecutionSimulationTest, DebugLogExceptionHandling)
645 uint16_t message_size = 5;
646 bool is_debug_logging_enabled =
true;
649 EXPECT_CALL(context, get_memory);
650 EXPECT_CALL(memory,
get(fields_size_offset)).WillOnce(Throw(std::runtime_error(
"Memory access error")));
651 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
655 context, message_offset, fields_offset, fields_size_offset, message_size, is_debug_logging_enabled));
658TEST_F(ExecutionSimulationTest, Sload)
663 auto slot = MemoryValue::from<FF>(42);
665 EXPECT_CALL(context, get_memory);
667 EXPECT_CALL(memory,
get(slot_addr)).WillOnce(ReturnRef(
slot));
668 EXPECT_CALL(context, get_address).WillOnce(ReturnRef(address));
669 EXPECT_CALL(
merkle_db, storage_read(address,
slot.as<
FF>())).WillOnce(Return(7));
671 EXPECT_CALL(memory, set(
dst_addr, MemoryValue::from<FF>(7)));
672 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
677TEST_F(ExecutionSimulationTest, SStore)
682 auto slot = MemoryValue::from<FF>(42);
683 auto value = MemoryValue::from<FF>(7);
684 TreeStates tree_state = {};
685 EXPECT_CALL(context, get_memory);
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 }));
694 EXPECT_CALL(context, get_is_static).WillOnce(Return(
false));
698 execution.sstore(context, value_addr, slot_addr);
701TEST_F(ExecutionSimulationTest, SStoreDuringStaticCall)
706 auto slot = MemoryValue::from<FF>(42);
707 auto value = MemoryValue::from<FF>(7);
708 EXPECT_CALL(context, get_memory);
710 EXPECT_CALL(memory,
get(slot_addr)).WillOnce(ReturnRef(
slot));
711 EXPECT_CALL(memory,
get(value_addr)).WillOnce(ReturnRef(
value));
712 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(address));
713 EXPECT_CALL(
merkle_db, was_storage_written(address,
slot.as<
FF>())).WillOnce(Return(
false));
714 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 1 }));
716 EXPECT_CALL(context, get_is_static).WillOnce(Return(
true));
718 "SSTORE: Cannot write to storage in static context");
721TEST_F(ExecutionSimulationTest, SStoreLimitReached)
726 auto slot = MemoryValue::from<FF>(42);
727 auto value = MemoryValue::from<FF>(7);
728 TreeStates tree_state = {};
730 EXPECT_CALL(context, get_memory);
732 EXPECT_CALL(memory,
get(slot_addr)).WillOnce(ReturnRef(
slot));
733 EXPECT_CALL(memory,
get(value_addr)).WillOnce(ReturnRef(
value));
734 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(address));
735 EXPECT_CALL(
merkle_db, was_storage_written(address,
slot.as<
FF>())).WillOnce(Return(
false));
736 EXPECT_CALL(
merkle_db, get_tree_state).WillOnce(Return(tree_state));
737 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 1 }));
739 EXPECT_CALL(context, get_is_static).WillOnce(Return(
false));
742 "SSTORE: Maximum number of data writes reached");
745TEST_F(ExecutionSimulationTest, SStoreLimitReachedSquashed)
750 auto slot = MemoryValue::from<FF>(42);
751 auto value = MemoryValue::from<FF>(7);
752 TreeStates tree_state = {};
754 EXPECT_CALL(context, get_memory);
756 EXPECT_CALL(memory,
get(slot_addr)).WillOnce(ReturnRef(
slot));
757 EXPECT_CALL(memory,
get(value_addr)).WillOnce(ReturnRef(
value));
758 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(address));
760 EXPECT_CALL(
merkle_db, was_storage_written(address,
slot.as<
FF>())).WillOnce(Return(
true));
761 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
763 EXPECT_CALL(context, get_is_static).WillOnce(Return(
false));
767 execution.sstore(context, value_addr, slot_addr);
770TEST_F(ExecutionSimulationTest, NoteHashExists)
776 auto unique_note_hash = MemoryValue::from<FF>(42);
777 auto leaf_index = MemoryValue::from<uint64_t>(7);
779 EXPECT_CALL(context, get_memory);
780 EXPECT_CALL(memory,
get(unique_note_hash_addr)).WillOnce(ReturnRef(unique_note_hash));
781 EXPECT_CALL(memory,
get(leaf_index_addr)).WillOnce(ReturnRef(leaf_index));
783 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
787 EXPECT_CALL(
merkle_db, note_hash_exists(leaf_index.as<uint64_t>(), unique_note_hash.as<
FF>()))
788 .WillOnce(Return(
true));
790 EXPECT_CALL(memory, set(
dst_addr, MemoryValue::from<uint1_t>(1)));
792 execution.note_hash_exists(context, unique_note_hash_addr, leaf_index_addr,
dst_addr);
795TEST_F(ExecutionSimulationTest, NoteHashExistsOutOfRange)
801 auto unique_note_hash = MemoryValue::from<FF>(42);
804 EXPECT_CALL(context, get_memory);
805 EXPECT_CALL(memory,
get(unique_note_hash_addr)).WillOnce(ReturnRef(unique_note_hash));
806 EXPECT_CALL(memory,
get(leaf_index_addr)).WillOnce(ReturnRef(leaf_index));
808 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
812 EXPECT_CALL(memory, set(
dst_addr, MemoryValue::from<uint1_t>(0)));
814 execution.note_hash_exists(context, unique_note_hash_addr, leaf_index_addr,
dst_addr);
817TEST_F(ExecutionSimulationTest, EmitNoteHash)
821 auto note_hash = MemoryValue::from<FF>(42);
823 TreeStates tree_state = {};
825 EXPECT_CALL(context, get_memory);
826 EXPECT_CALL(memory,
get(note_hash_addr)).WillOnce(ReturnRef(note_hash));
827 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(address));
829 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
831 EXPECT_CALL(context, get_is_static).WillOnce(Return(
false));
832 EXPECT_CALL(
merkle_db, get_tree_state).WillOnce(Return(tree_state));
833 EXPECT_CALL(
merkle_db, note_hash_write(address, note_hash.as<
FF>()));
835 execution.emit_note_hash(context, note_hash_addr);
838TEST_F(ExecutionSimulationTest, EmitNoteHashDuringStaticCall)
842 auto note_hash = MemoryValue::from<FF>(42);
845 EXPECT_CALL(context, get_memory);
846 EXPECT_CALL(memory,
get(note_hash_addr)).WillOnce(ReturnRef(note_hash));
847 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(address));
849 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
851 EXPECT_CALL(context, get_is_static).WillOnce(Return(
true));
853 "EMITNOTEHASH: Cannot emit note hash in static context");
856TEST_F(ExecutionSimulationTest, EmitNoteHashLimitReached)
860 auto note_hash = MemoryValue::from<FF>(42);
862 TreeStates tree_state = {};
865 EXPECT_CALL(context, get_memory);
866 EXPECT_CALL(memory,
get(note_hash_addr)).WillOnce(ReturnRef(note_hash));
867 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(address));
869 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
871 EXPECT_CALL(context, get_is_static).WillOnce(Return(
false));
872 EXPECT_CALL(
merkle_db, get_tree_state).WillOnce(Return(tree_state));
875 "EMITNOTEHASH: Maximum number of note hashes reached");
878TEST_F(ExecutionSimulationTest, L1ToL2MessageExists)
884 auto msg_hash = MemoryValue::from<FF>(42);
885 auto leaf_index = MemoryValue::from<uint64_t>(7);
887 EXPECT_CALL(context, get_memory);
888 EXPECT_CALL(memory,
get(msg_hash_addr)).WillOnce(ReturnRef(msg_hash));
889 EXPECT_CALL(memory,
get(leaf_index_addr)).WillOnce(ReturnRef(leaf_index));
891 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
895 EXPECT_CALL(
merkle_db, l1_to_l2_msg_exists(leaf_index.as<uint64_t>(), msg_hash.as<
FF>())).WillOnce(Return(
true));
897 EXPECT_CALL(memory, set(
dst_addr, MemoryValue::from<uint1_t>(1)));
899 execution.l1_to_l2_message_exists(context, msg_hash_addr, leaf_index_addr,
dst_addr);
902TEST_F(ExecutionSimulationTest, L1ToL2MessageExistsOutOfRange)
908 auto msg_hash = MemoryValue::from<FF>(42);
911 EXPECT_CALL(context, get_memory);
912 EXPECT_CALL(memory,
get(msg_hash_addr)).WillOnce(ReturnRef(msg_hash));
913 EXPECT_CALL(memory,
get(leaf_index_addr)).WillOnce(ReturnRef(leaf_index));
915 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
919 EXPECT_CALL(memory, set(
dst_addr, MemoryValue::from<uint1_t>(0)));
921 execution.l1_to_l2_message_exists(context, msg_hash_addr, leaf_index_addr,
dst_addr);
924TEST_F(ExecutionSimulationTest, NullifierExists)
930 auto nullifier = MemoryValue::from<FF>(42);
931 auto address = MemoryValue::from<FF>(7);
933 EXPECT_CALL(context, get_memory);
934 EXPECT_CALL(memory,
get(nullifier_offset)).WillOnce(ReturnRef(
nullifier));
935 EXPECT_CALL(memory,
get(address_offset)).WillOnce(ReturnRef(address));
937 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
939 EXPECT_CALL(
merkle_db, nullifier_exists(address.as_ff(),
nullifier.as_ff())).WillOnce(Return(
true));
941 EXPECT_CALL(memory, set(exists_offset, MemoryValue::from<uint1_t>(1)));
943 execution.nullifier_exists(context, nullifier_offset, address_offset, exists_offset);
946TEST_F(ExecutionSimulationTest, EmitNullifier)
950 auto nullifier = MemoryValue::from<FF>(42);
952 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));
962 EXPECT_CALL(
merkle_db, nullifier_write(address,
nullifier.as_ff())).WillOnce(Return(
true));
964 execution.emit_nullifier(context, nullifier_addr);
967TEST_F(ExecutionSimulationTest, EmitNullifierDuringStaticCall)
971 auto nullifier = MemoryValue::from<FF>(42);
974 EXPECT_CALL(context, get_memory);
975 EXPECT_CALL(memory,
get(nullifier_addr)).WillOnce(ReturnRef(
nullifier));
976 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(address));
978 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
980 EXPECT_CALL(context, get_is_static).WillOnce(Return(
true));
982 "EMITNULLIFIER: Cannot emit nullifier in static context");
985TEST_F(ExecutionSimulationTest, EmitNullifierLimitReached)
989 auto nullifier = MemoryValue::from<FF>(42);
991 TreeStates tree_state = {};
994 EXPECT_CALL(context, get_memory);
995 EXPECT_CALL(memory,
get(nullifier_addr)).WillOnce(ReturnRef(
nullifier));
996 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(address));
998 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
1000 EXPECT_CALL(context, get_is_static).WillOnce(Return(
false));
1001 EXPECT_CALL(
merkle_db, get_tree_state).WillOnce(Return(tree_state));
1004 "EMITNULLIFIER: Maximum number of nullifiers reached");
1007TEST_F(ExecutionSimulationTest, EmitNullifierCollision)
1011 auto nullifier = MemoryValue::from<FF>(42);
1013 TreeStates tree_state = {};
1015 EXPECT_CALL(context, get_memory);
1016 EXPECT_CALL(memory,
get(nullifier_addr)).WillOnce(ReturnRef(
nullifier));
1017 EXPECT_CALL(context, get_address).WillRepeatedly(ReturnRef(address));
1019 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
1021 EXPECT_CALL(context, get_is_static).WillOnce(Return(
false));
1022 EXPECT_CALL(
merkle_db, get_tree_state).WillOnce(Return(tree_state));
1028TEST_F(ExecutionSimulationTest, Set)
1034 EXPECT_CALL(context, get_memory);
1035 EXPECT_CALL(alu, truncate(
value,
static_cast<MemoryTag>(
dst_tag))).WillOnce(Return(MemoryValue::from<uint8_t>(7)));
1036 EXPECT_CALL(memory, set(
dst_addr, MemoryValue::from<uint8_t>(7)));
1037 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
1042TEST_F(ExecutionSimulationTest, Cast)
1049 EXPECT_CALL(context, get_memory).WillOnce(ReturnRef(memory));
1050 EXPECT_CALL(memory,
get(src_addr)).WillOnce(ReturnRef(
value));
1053 .WillOnce(Return(MemoryValue::from<uint1_t>(1)));
1054 EXPECT_CALL(memory, set(
dst_addr, MemoryValue::from<uint1_t>(1)));
1055 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
1065 EXPECT_CALL(context, get_memory);
1066 EXPECT_CALL(gas_tracker, consume_gas);
1067 EXPECT_CALL(
poseidon2, permutation(_, src_address, dst_address));
1069 execution.poseidon2_permutation(context, src_address, dst_address);
1072TEST_F(ExecutionSimulationTest, EccAdd)
1082 MemoryValue p_x = MemoryValue::from<FF>(
FF(
"0x04c95d1b26d63d46918a156cae92db1bcbc4072a27ec81dc82ea959abdbcf16a"));
1083 MemoryValue p_y = MemoryValue::from<FF>(
FF(
"0x035b6dd9e63c1370462c74775765d07fc21fd1093cc988149d3aa763bb3dbb60"));
1086 MemoryValue q_x = MemoryValue::from<FF>(
FF(
"0x009242167ec31949c00cbe441cd36757607406e87844fa2c8c4364a4403e66d7"));
1087 MemoryValue q_y = MemoryValue::from<FF>(
FF(
"0x0fe3016d64cfa8045609f375284b6b739b5fa282e4cbb75cc7f1687ecc7420e3"));
1092 EXPECT_CALL(context, get_memory).WillRepeatedly(ReturnRef(memory));
1093 EXPECT_CALL(Const(memory),
get(p_x_addr)).WillOnce(ReturnRef(p_x));
1094 EXPECT_CALL(memory,
get(p_y_addr)).WillOnce(ReturnRef(p_y));
1095 EXPECT_CALL(memory,
get(p_is_inf_addr)).WillOnce(ReturnRef(zero));
1096 EXPECT_CALL(memory,
get(q_x_addr)).WillOnce(ReturnRef(q_x));
1097 EXPECT_CALL(memory,
get(q_y_addr)).WillOnce(ReturnRef(q_y));
1098 EXPECT_CALL(memory,
get(q_is_inf_addr)).WillOnce(ReturnRef(zero));
1100 EXPECT_CALL(gas_tracker, consume_gas);
1103 EXPECT_CALL(ecc, add(_, _, _,
dst_addr));
1106 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);
1109TEST_F(ExecutionSimulationTest, ToRadixBE)
1118 MemoryValue radix = MemoryValue::from<uint32_t>(16);
1120 MemoryValue::from<uint8_t>(0x55),
1121 MemoryValue::from<uint8_t>(0x00) };
1122 MemoryValue num_limbs = MemoryValue::from<uint32_t>(3);
1123 MemoryValue is_output_bits = MemoryValue::from<uint1_t>(
false);
1124 uint32_t num_p_limbs = 64;
1126 EXPECT_CALL(context, get_memory).WillOnce(ReturnRef(memory));
1127 EXPECT_CALL(memory,
get(value_addr)).WillOnce(ReturnRef(
value));
1128 EXPECT_CALL(memory,
get(radix_addr)).WillOnce(ReturnRef(radix));
1129 EXPECT_CALL(memory,
get(num_limbs_addr)).WillOnce(ReturnRef(num_limbs));
1130 EXPECT_CALL(memory,
get(is_output_bits_addr)).WillOnce(ReturnRef(is_output_bits));
1132 EXPECT_CALL(greater_than,
gt(radix.as<uint32_t>(), 256)).WillOnce(Return(
false));
1133 EXPECT_CALL(greater_than,
gt(num_limbs.as<uint32_t>(), num_p_limbs)).WillOnce(Return(
false));
1135 EXPECT_CALL(gas_tracker, consume_gas);
1136 EXPECT_CALL(to_radix, to_be_radix);
1138 execution.to_radix_be(context, value_addr, radix_addr, num_limbs_addr, is_output_bits_addr,
dst_addr);
1141TEST_F(ExecutionSimulationTest, EmitUnencryptedLog)
1145 MemoryValue first_field = MemoryValue::from<FF>(42);
1146 MemoryValue log_size = MemoryValue::from<uint32_t>(10);
1149 EXPECT_CALL(context, get_memory);
1150 EXPECT_CALL(memory,
get(log_offset)).WillOnce(ReturnRef(first_field));
1151 EXPECT_CALL(memory,
get(log_size_offset)).WillOnce(ReturnRef(log_size));
1153 EXPECT_CALL(context, get_address).WillOnce(ReturnRef(address));
1155 EXPECT_CALL(emit_unencrypted_log, emit_unencrypted_log(_, _, address, log_offset, log_size.as<uint32_t>()));
1157 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, log_size.as<uint32_t>() }));
1159 execution.emit_unencrypted_log(context, log_offset, log_size_offset);
1162TEST_F(ExecutionSimulationTest, SendL2ToL1Msg)
1167 auto recipient = MemoryValue::from<FF>(42);
1168 auto content = MemoryValue::from<FF>(27);
1170 SideEffectStates side_effects_states = {};
1172 SideEffectStates side_effects_states_after = side_effects_states;
1173 side_effects_states_after.numL2ToL1Messages++;
1175 EXPECT_CALL(context, get_memory);
1177 EXPECT_CALL(memory,
get(recipient_addr)).WillOnce(ReturnRef(recipient));
1178 EXPECT_CALL(memory,
get(content_addr)).WillOnce(ReturnRef(content));
1180 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
1182 EXPECT_CALL(context, get_is_static).WillOnce(Return(
false));
1184 EXPECT_CALL(context, get_side_effect_states).WillOnce(ReturnRef(side_effects_states));
1185 EXPECT_CALL(context, set_side_effect_states(side_effects_states_after));
1187 execution.send_l2_to_l1_msg(context, recipient_addr, content_addr);
1190TEST_F(ExecutionSimulationTest, SendL2ToL1MsgStaticCall)
1195 auto recipient = MemoryValue::from<FF>(42);
1196 auto content = MemoryValue::from<FF>(27);
1198 SideEffectStates side_effects_states = {};
1201 EXPECT_CALL(context, get_memory);
1203 EXPECT_CALL(memory,
get(recipient_addr)).WillOnce(ReturnRef(recipient));
1204 EXPECT_CALL(memory,
get(content_addr)).WillOnce(ReturnRef(content));
1206 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
1208 EXPECT_CALL(context, get_is_static).WillOnce(Return(
true));
1210 EXPECT_CALL(context, get_side_effect_states).WillOnce(ReturnRef(side_effects_states));
1213 "SENDL2TOL1MSG: Cannot send L2 to L1 message in static context");
1216TEST_F(ExecutionSimulationTest, SendL2ToL1MsgLimitReached)
1221 auto recipient = MemoryValue::from<FF>(42);
1222 auto content = MemoryValue::from<FF>(27);
1224 SideEffectStates side_effects_states = {};
1227 EXPECT_CALL(context, get_memory);
1229 EXPECT_CALL(memory,
get(recipient_addr)).WillOnce(ReturnRef(recipient));
1230 EXPECT_CALL(memory,
get(content_addr)).WillOnce(ReturnRef(content));
1232 EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
1234 EXPECT_CALL(context, get_is_static).WillOnce(Return(
false));
1236 EXPECT_CALL(context, get_side_effect_states).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));
1252 execution.sha256_compression(context, dst_address, state_address, input_address);
#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
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.
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessage)
StandardAffinePoint< AvmFlavorSettings::EmbeddedCurve::AffineElement > EmbeddedCurvePoint
uint256_t get_tag_max_value(ValueTag tag)
Sha256Hash sha256(const ByteContainer &input)
TEST_F(IPATest, ChallengesAreZero)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
unsigned __int128 uint128_t
bb::crypto::Poseidon2< bb::crypto::Poseidon2Bn254ScalarFieldParams > poseidon2
NiceMock< MockContextProvider > context_provider
NiceMock< MockExecution > execution