4#include <gmock/gmock.h>
5#include <gtest/gtest.h>
22using ::testing::ElementsAre;
23using ::testing::Return;
24using ::testing::ReturnRef;
25using ::testing::StrictMock;
27class DataCopySimulationTest :
public ::testing::Test {
29 DataCopySimulationTest()
31 ON_CALL(context, get_memory()).WillByDefault(ReturnRef(mem));
34 EXPECT_CALL(context, get_memory());
35 EXPECT_CALL(execution_id_manager, get_execution_id());
36 EXPECT_CALL(context, get_context_id());
44 DataCopy
data_copy = DataCopy(execution_id_manager, gt, event_emitter);
49class NestedCdCopySimulationTest :
public DataCopySimulationTest {
51 NestedCdCopySimulationTest()
59 EXPECT_CALL(
context, get_parent_id());
60 EXPECT_CALL(
context, has_parent()).WillRepeatedly(Return(
true));
64 MemoryValue::from<FF>(4), MemoryValue::from<FF>(5), MemoryValue::from<FF>(6),
65 MemoryValue::from<FF>(7), MemoryValue::from<FF>(8) };
71TEST_F(NestedCdCopySimulationTest, CdZero)
74 uint32_t cd_copy_size = 0;
80 EXPECT_TRUE(c.as_ff().is_zero());
83TEST_F(NestedCdCopySimulationTest, CdCopyAll)
86 uint32_t cd_copy_size =
static_cast<uint32_t
>(
calldata.size());
88 EXPECT_CALL(context, get_calldata(
cd_offset, cd_copy_size)).WillOnce(Return(calldata));
94 std::vector<FF> calldata_in_memory;
95 for (uint32_t i = 0; i < cd_copy_size; ++i) {
97 calldata_in_memory.emplace_back(c.as_ff());
99 EXPECT_THAT(calldata_in_memory, ElementsAre(1, 2, 3, 4, 5, 6, 7, 8));
102TEST_F(NestedCdCopySimulationTest, CdCopyPartial)
105 uint32_t cd_copy_size = 2;
107 EXPECT_CALL(context, get_calldata(
cd_offset, cd_copy_size))
109 MemoryValue::from<FF>(2) }));
114 std::vector<FF> calldata_in_memory;
115 for (uint32_t i = 0; i < cd_copy_size; ++i) {
117 calldata_in_memory.emplace_back(c.as_ff());
119 EXPECT_THAT(calldata_in_memory, ElementsAre(1, 2));
122TEST_F(NestedCdCopySimulationTest, CdFullWithPadding)
125 uint32_t cd_copy_size = 10;
129 EXPECT_CALL(context, get_calldata(
cd_offset, effective_reads)).WillOnce(Return(calldata));
134 std::vector<FF> calldata_in_memory;
135 for (uint32_t i = 0; i < cd_copy_size; ++i) {
137 calldata_in_memory.emplace_back(c.as_ff());
139 EXPECT_THAT(calldata_in_memory, ElementsAre(1, 2, 3, 4, 5, 6, 7, 8, 0, 0));
142TEST_F(NestedCdCopySimulationTest, CdPartialWithPadding)
145 uint32_t cd_copy_size = 4;
148 uint32_t effective_reads = 2;
151 MemoryValue::from<FF>(7),
152 MemoryValue::from<FF>(8),
155 EXPECT_CALL(context, get_calldata(
cd_offset, effective_reads)).WillOnce(Return(expected_calldata));
160 std::vector<FF> calldata_in_memory;
161 for (uint32_t i = 0; i < cd_copy_size; ++i) {
163 calldata_in_memory.emplace_back(c.as_ff());
165 EXPECT_THAT(calldata_in_memory, ElementsAre(7, 8, 0, 0));
168class RdCopySimulationTest :
public DataCopySimulationTest {
170 RdCopySimulationTest()
173 EXPECT_CALL(context, get_last_rd_addr()).WillRepeatedly(Return(child_rd_addr));
174 EXPECT_CALL(context, get_last_rd_size()).WillRepeatedly(Return(child_rd_size));
175 EXPECT_CALL(context, get_last_child_id()).WillRepeatedly(Return(child_context_id));
176 EXPECT_CALL(context, has_parent()).WillRepeatedly(Return(
true));
177 EXPECT_CALL(context, get_last_child_id()).WillRepeatedly(Return(2));
180 MemoryValue::from<FF>(9), MemoryValue::from<FF>(10), MemoryValue::from<FF>(11), MemoryValue::from<FF>(12)
187TEST_F(RdCopySimulationTest, RdZero)
190 uint32_t rd_copy_size = 0;
191 uint32_t rd_offset = 0;
196 EXPECT_TRUE(c.as_ff().is_zero());
199TEST_F(RdCopySimulationTest, RdCopyAll)
202 uint32_t rd_copy_size =
static_cast<uint32_t
>(
returndata.size());
203 uint32_t rd_offset = 0;
205 EXPECT_CALL(context, get_returndata(rd_offset, rd_copy_size)).WillOnce(Return(
returndata));
210 std::vector<FF> returndata_in_memory;
211 for (uint32_t i = 0; i < rd_copy_size; ++i) {
213 returndata_in_memory.emplace_back(c.as_ff());
215 EXPECT_THAT(returndata_in_memory, ElementsAre(9, 10, 11, 12));
218TEST_F(RdCopySimulationTest, RdCopyPartial)
221 uint32_t rd_copy_size = 2;
222 uint32_t rd_offset = 1;
224 EXPECT_CALL(context, get_returndata(rd_offset, rd_copy_size))
226 MemoryValue::from<FF>(11) }));
231 std::vector<FF> returndata_in_memory;
232 for (uint32_t i = 0; i < rd_copy_size; ++i) {
234 returndata_in_memory.emplace_back(c.as_ff());
236 EXPECT_THAT(returndata_in_memory, ElementsAre(10, 11));
239TEST_F(RdCopySimulationTest, RdFullWithPadding)
242 uint32_t rd_copy_size = 10;
243 uint32_t rd_offset = 0;
247 EXPECT_CALL(context, get_returndata(rd_offset, effective_reads)).WillOnce(Return(
returndata));
252 std::vector<FF> returndata_in_memory;
253 for (uint32_t i = 0; i < rd_copy_size; ++i) {
255 returndata_in_memory.emplace_back(c.as_ff());
257 EXPECT_THAT(returndata_in_memory, ElementsAre(9, 10, 11, 12, 0, 0, 0, 0, 0, 0));
260TEST_F(RdCopySimulationTest, RdPartialWithPadding)
263 uint32_t rd_copy_size = 4;
264 uint32_t rd_offset = 2;
266 uint32_t effective_reads = 2;
269 MemoryValue::from<FF>(11),
270 MemoryValue::from<FF>(12),
273 EXPECT_CALL(context, get_returndata(rd_offset, effective_reads)).WillOnce(Return(expected_returndata));
278 std::vector<FF> returndata_in_memory;
279 for (uint32_t i = 0; i < rd_copy_size; ++i) {
281 returndata_in_memory.emplace_back(c.as_ff());
283 EXPECT_THAT(returndata_in_memory, ElementsAre(11, 12, 0, 0));
287class SrcOutOfRangeCdCopySimulationTest :
public DataCopySimulationTest {
289 SrcOutOfRangeCdCopySimulationTest()
295 EXPECT_CALL(context, get_parent_cd_addr()).WillRepeatedly(Return(parent_cd_addr));
296 EXPECT_CALL(context, get_parent_cd_size()).WillRepeatedly(Return(parent_cd_size));
297 EXPECT_CALL(context, get_parent_id());
298 EXPECT_CALL(context, has_parent()).WillRepeatedly(Return(
true));
302 MemoryValue::from<FF>(2),
303 MemoryValue::from<FF>(3) };
310TEST_F(SrcOutOfRangeCdCopySimulationTest, SrcOutOfRangeClampedWithPadding)
312 uint32_t cd_copy_size = 5;
318 uint32_t effective_reads = 3;
325 std::vector<FF> result;
326 for (uint32_t i = 0; i < cd_copy_size; ++i) {
330 EXPECT_THAT(result, ElementsAre(1, 2, 3, 0, 0));
333class SrcOutOfRangeRdCopySimulationTest :
public DataCopySimulationTest {
335 SrcOutOfRangeRdCopySimulationTest()
337 EXPECT_CALL(context, get_last_rd_addr()).WillRepeatedly(Return(child_rd_addr));
338 EXPECT_CALL(context, get_last_rd_size()).WillRepeatedly(Return(child_rd_size));
339 EXPECT_CALL(context, get_last_child_id()).WillRepeatedly(Return(2));
340 EXPECT_CALL(context, has_parent()).WillRepeatedly(Return(
true));
348TEST_F(SrcOutOfRangeRdCopySimulationTest, SrcOutOfRangeClampedWithPadding)
350 uint32_t rd_copy_size = 4;
351 uint32_t rd_offset = 0;
356 uint32_t effective_reads = 2;
358 EXPECT_CALL(context, get_returndata(rd_offset, effective_reads)).WillOnce(Return(
returndata));
363 std::vector<FF> result;
364 for (uint32_t i = 0; i < rd_copy_size; ++i) {
367 EXPECT_THAT(result, ElementsAre(10, 20, 0, 0));
371TEST_F(SrcOutOfRangeCdCopySimulationTest, SrcOutOfRangeWithOffset)
373 uint32_t cd_copy_size = 4;
379 uint32_t effective_reads = 2;
382 EXPECT_CALL(context, get_calldata(
cd_offset, effective_reads)).WillOnce(Return(expected));
386 std::vector<FF> result;
387 for (uint32_t i = 0; i < cd_copy_size; ++i) {
391 EXPECT_THAT(result, ElementsAre(2, 3, 0, 0));
395TEST_F(SrcOutOfRangeRdCopySimulationTest, SrcOutOfRangeClampedBelowOffset)
397 uint32_t rd_copy_size = 3;
398 uint32_t rd_offset = 5;
407 std::vector<FF> result;
408 for (uint32_t i = 0; i < rd_copy_size; ++i) {
411 EXPECT_THAT(result, ElementsAre(0, 0, 0));
#define AVM_HIGHEST_MEM_ADDRESS
static TaggedValue from(T value)
void rd_copy(ContextInterface &context, uint32_t copy_size, uint32_t offset, MemoryAddress dst_addr) override
Copies returndata from the last executed context to the dst_addr.
void cd_copy(ContextInterface &context, uint32_t copy_size, uint32_t offset, MemoryAddress dst_addr) override
Writes calldata into dst_addr. There is slight difference in how enqueued and nested contexts are han...
const MemoryValue & get(MemoryAddress index) const override
ExecutionIdManager execution_id_manager
EventEmitter< DataCopyEvent > event_emitter
StrictMock< MockContext > context
AVM range check gadget for witness generation.
TEST_F(IPATest, ChallengesAreZero)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
std::vector< MemoryValue > calldata
uint32_t child_context_id
std::vector< MemoryValue > returndata
uint32_t parent_cd_size_in_range