Skip to content

Commit 3836e72

Browse files
committed
codecopy
1 parent fb169c0 commit 3836e72

File tree

5 files changed

+344
-133
lines changed

5 files changed

+344
-133
lines changed

crypto3/libs/blueprint/include/nil/blueprint/zkevm_bbf/input_generators/basic_evm.hpp

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,33 +1528,36 @@ namespace nil {
15281528
virtual void codecopy() {
15291529
// 6M memory bytes gives 60M+ gas cost
15301530
// max gas cost is 36M for now, might go up to 60M
1531-
constexpr static const std::size_t max_dest_offset = 8388608; // 2^23
1532-
// max contract size is 24,576 bytes, so offset and length need to fit in
1533-
// first chunk
1531+
constexpr static const std::size_t max_dest_offset = 1 << 22;
1532+
constexpr static const std::size_t max_length = 1 << 22;
1533+
// max contract size is 24,576 bytes, so offset need to fit in first chunk
15341534
constexpr static const std::size_t max_offset = 65536;
1535-
constexpr static const std::size_t max_length = 65536;
1536-
std::size_t dst = std::size_t(stack.back()); stack.pop_back();
1537-
std::size_t src = std::size_t(stack.back()); stack.pop_back();
1538-
std::size_t length = std::size_t(stack.back()); stack.pop_back();
15391535

1540-
bool overflow = (dst > max_dest_offset) ||
1541-
(src > max_offset) ||
1542-
(length > max_length);
1536+
auto dst = stack.back(); stack.pop_back();
1537+
auto src = stack.back(); stack.pop_back();
1538+
auto lgt = stack.back(); stack.pop_back();
15431539

1544-
std::size_t minimum_word_size = (length + 31) / 32;
1545-
std::size_t next_mem = std::max(dst + length, memory.size());
1546-
std::size_t memory_expansion = memory_expansion_cost(next_mem, memory.size());
1547-
std::size_t next_memory_size = memory_size_word_util(next_mem) * 32;
1540+
bool overflow = (dst > max_dest_offset) ||
1541+
(lgt > max_length);
15481542

15491543

15501544
if (overflow) {
1551-
memory.resize(memory.size() - 1);
1552-
increase_gas(1);
1545+
decrease_gas(gas + 1);
1546+
memory.clear();
15531547
}
15541548
else {
1555-
if( memory.size() < dst + length) memory.resize(next_memory_size);
1549+
std::size_t distance = std::size_t(dst);
1550+
std::size_t length = std::size_t(lgt);
1551+
std::size_t source = src < max_offset ? std::size_t(src) : max_offset;
1552+
1553+
std::size_t minimum_word_size = (length + 31) / 32;
1554+
std::size_t next_mem = std::max(distance + length, memory.size());
1555+
std::size_t memory_expansion = memory_expansion_cost(next_mem, memory.size());
1556+
std::size_t next_memory_size = memory_size_word_util(next_mem) * 32;
1557+
1558+
if( memory.size() < distance + length) memory.resize(next_memory_size);
15561559
for( std::size_t i = 0; i < length; i++){
1557-
memory[dst+i] = src + i < bytecode.size()? bytecode[src+i]: 0;
1560+
memory[distance+i] = source + i < bytecode.size()? bytecode[source+i]: 0;
15581561
}
15591562
decrease_gas(3 + 3 * minimum_word_size + memory_expansion); //dynamic gas
15601563
}

crypto3/libs/blueprint/include/nil/blueprint/zkevm_bbf/input_generators/basic_input_generator.hpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -989,28 +989,33 @@ namespace nil {
989989
}
990990

991991
virtual void codecopy() override{
992-
std::size_t dst = std::size_t(stack[stack.size() - 1]);
993-
std::size_t src = std::size_t(stack[stack.size() - 2]);
994-
std::size_t length = std::size_t(stack[stack.size() - 3]);
992+
constexpr static const std::size_t max_offset = 65536;
993+
auto dst = stack[stack.size() - 1];
994+
auto src = stack[stack.size() - 2];
995+
auto lgt = stack[stack.size() - 3];
995996

996997
_zkevm_states.back().load_stack(stack, 3);
997998
append_stack_reads(3);
998999

9991000
zkevm_basic_evm::codecopy();
1000-
if (memory.size() % 32 == 0) {
1001+
if (memory.size() != 0) {
1002+
std::size_t distance = std::size_t(dst);
1003+
std::size_t length = std::size_t(lgt);
1004+
std::size_t source = src < max_offset ? std::size_t(src) : max_offset;
1005+
10011006
copy_event cpy = codecopy_copy_event(
1002-
bytecode_hash,
1003-
src,
1004-
call_id,
1005-
dst,
1006-
rw_counter,
1007-
length
1008-
);
1007+
bytecode_hash,
1008+
source,
1009+
call_id,
1010+
distance,
1011+
rw_counter,
1012+
length
1013+
);
10091014
for( std::size_t i = 0; i < length; i++){
10101015
_short_rw_operations.push_back(memory_rw_operation(
1011-
call_id, dst + i, rw_counter++, true, memory[dst + i]
1016+
call_id, distance + i, rw_counter++, true, memory[distance + i]
10121017
));
1013-
cpy.push_byte(memory[dst+i]);
1018+
cpy.push_byte(memory[distance+i]);
10141019
}
10151020
if( length > 0 ) _copy_events.push_back(cpy);
10161021
}

0 commit comments

Comments
 (0)