Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

324 memory expansion integration to opcodes #332

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ namespace nil {
std::size_t gas = initial_gas;
std::vector<zkevm_word_type> stack;
zkevm_word_type bytecode_hash = _bytecodes.get_data()[current_buffer_id].second;
const std::map<std::size_t, std::uint8_t> memory;
std::map<std::size_t, std::uint8_t> memory;
const std::map<zkevm_word_type, zkevm_word_type> storage;

while(true){
Expand Down Expand Up @@ -501,10 +501,20 @@ namespace nil {
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, length));

for( std::size_t i = 0; i < length; i++){
_rw_operations.push_back(memory_rw_operation(call_id, stack.size(), rw_counter++, true, 0)); // placeholder
}

std::size_t minimum_word_size = (length + 31) / 32;
std::size_t next_mem = std::max(destOffset + length, state.memory_size);
std::size_t memory_expansion = memory_expansion_cost(next_mem, state.memory_size);

std::size_t next_memory_size = (memory_size_word_util(next_mem))*32;
//placeholder values to mimic memory expansion
for (std::size_t i = 0; i < next_memory_size; ++i) {
memory[i] = static_cast<std::uint8_t>(i);
}

gas-=3; //static gas
gas -= 3 * minimum_word_size + memory_expansion; //dynamic gas
pc++;
Expand Down Expand Up @@ -727,6 +737,169 @@ namespace nil {
stack.push_back(additional_input);
gas -= 3;
pc += 33;
} else if(opcode == zkevm_opcode::LOG0){
// 0xA0
auto offset = std::size_t(stack.back());
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, offset));

auto length = std::size_t(stack.back());
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, length));

for( std::size_t i = 0; i < length; i++){
_rw_operations.push_back(memory_rw_operation(call_id, stack.size(), rw_counter++, true, 0)); // placeholder
}

std::size_t next_mem = std::max(offset + length, state.memory_size);
std::size_t memory_expansion = memory_expansion_cost(next_mem, state.memory_size);

std::size_t next_memory_size = (memory_size_word_util(next_mem))*32;

//placeholder values to mimic memory expansion
for (std::size_t i = 0; i < next_memory_size; ++i) {
memory[i] = static_cast<std::uint8_t>(i);
}
gas-=375; //static gas
gas -= 8 * length + memory_expansion; //dynamic gas
pc++;
} else if(opcode == zkevm_opcode::LOG1){
// 0xA1
auto offset = std::size_t(stack.back());
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, offset));

auto length = std::size_t(stack.back());
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, length));

auto topic1 = stack.back();
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, topic1));


for( std::size_t i = 0; i < length; i++){
_rw_operations.push_back(memory_rw_operation(call_id, stack.size(), rw_counter++, true, 0)); // placeholder
}

std::size_t next_mem = std::max(offset + length, state.memory_size);
std::size_t memory_expansion = memory_expansion_cost(next_mem, state.memory_size);
std::size_t next_memory_size = (memory_size_word_util(next_mem))*32;
//placeholder values to mimic memory expansion
for (std::size_t i = 0; i < next_memory_size; ++i) {
memory[i] = static_cast<std::uint8_t>(i);
}
gas-=375; //static gas
gas -= 375 + 8 * length + memory_expansion; //dynamic gas
pc++;
} else if(opcode == zkevm_opcode::LOG2){
// 0xA2
auto offset = std::size_t(stack.back());
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, offset));

auto length = std::size_t(stack.back());
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, length));

auto topic1 = stack.back();
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, topic1));

auto topic2 = stack.back();
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, topic2));

for( std::size_t i = 0; i < length; i++){
_rw_operations.push_back(memory_rw_operation(call_id, stack.size(), rw_counter++, true, 0)); // placeholder
}

std::size_t next_mem = std::max(offset + length, state.memory_size);
std::size_t memory_expansion = memory_expansion_cost(next_mem, state.memory_size);
std::size_t next_memory_size = (memory_size_word_util(next_mem))*32;
//placeholder values to mimic memory expansion
for (std::size_t i = 0; i < next_memory_size; ++i) {
memory[i] = static_cast<std::uint8_t>(i);
}
gas-=375; //static gas
gas -= 375 * 2 + 8 * length + memory_expansion; //dynamic gas
pc++;
} else if(opcode == zkevm_opcode::LOG3){
// 0xA3
auto offset = std::size_t(stack.back());
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, offset));

auto length = std::size_t(stack.back());
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, length));

auto topic1 = stack.back();
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, topic1));

auto topic2 = stack.back();
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, topic2));

auto topic3 = stack.back();
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, topic3));

for( std::size_t i = 0; i < length; i++){
_rw_operations.push_back(memory_rw_operation(call_id, stack.size(), rw_counter++, true, 0)); // placeholder
}

std::size_t next_mem = std::max(offset + length, state.memory_size);
std::size_t memory_expansion = memory_expansion_cost(next_mem, state.memory_size);
std::size_t next_memory_size = (memory_size_word_util(next_mem))*32;
//placeholder values to mimic memory expansion
for (std::size_t i = 0; i < next_memory_size; ++i) {
memory[i] = static_cast<std::uint8_t>(i);
}
gas-=375; //static gas
gas -= 375 * 3 + 8 * length + memory_expansion; //dynamic gas
pc++;
} else if(opcode == zkevm_opcode::LOG4){
// 0xA4
auto offset = std::size_t(stack.back());
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, offset));

auto length = std::size_t(stack.back());
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, length));

auto topic1 = stack.back();
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, topic1));

auto topic2 = stack.back();
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, topic2));

auto topic3 = stack.back();
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, topic3));

auto topic4 = stack.back();
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, topic4));

for( std::size_t i = 0; i < length; i++){
_rw_operations.push_back(memory_rw_operation(call_id, stack.size(), rw_counter++, true, 0)); // placeholder
}

std::size_t next_mem = std::max(offset + length, state.memory_size);
std::size_t memory_expansion = memory_expansion_cost(next_mem, state.memory_size);
std::size_t next_memory_size = (memory_size_word_util(next_mem))*32;
//placeholder values to mimic memory expansion
for (std::size_t i = 0; i < next_memory_size; ++i) {
memory[i] = static_cast<std::uint8_t>(i);
}
gas-=375; //static gas
gas -= 375 * 4 + 8 * length + memory_expansion; //dynamic gas
pc++;
} else {
std::cout << "Opcode tester machine doesn't contain " << opcode << " implementation" << std::endl;
BOOST_ASSERT(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,16 @@ namespace nil {
using Memory_Cost = typename bbf::memory_cost<FieldType, stage>;

TYPE destOffset, offset, length, current_mem, next_mem,
memory_expansion, S;
memory_expansion_cost, memory_expansion_size, S;

if constexpr (stage == GenerationStage::ASSIGNMENT) {
destOffset = w_lo<FieldType>(current_state.stack_top());
offset = w_lo<FieldType>(current_state.stack_top(1));
length = w_lo<FieldType>(current_state.stack_top(2));
current_mem = current_state.memory_size;
next_mem = std::max(destOffset + length, current_mem);
next_mem = length.is_zero()
? current_mem
: std::max(destOffset + length, current_mem);
S = next_mem > current_mem;
}
allocate(destOffset, 32, 0);
Expand All @@ -80,9 +82,10 @@ namespace nil {
(1 - S) * (next_mem - current_mem));

std::vector<std::size_t> word_size_lookup_area = {32, 33, 34};
allocate(memory_expansion, 35, 1);
allocate(memory_expansion_cost, 35, 1);
allocate(memory_expansion_size, 36, 1);
std::vector<std::size_t> memory_cost_lookup_area = {42, 43, 44,
45, 46,47};
45, 46, 47};

context_type word_size_ct =
context_object.subcontext(word_size_lookup_area, 1, 1);
Expand All @@ -95,24 +98,26 @@ namespace nil {
Memory_Cost current_memory =
Memory_Cost(current_memory_ct, current_mem);
Memory_Cost next_memory = Memory_Cost(next_memory_ct, next_mem);
memory_expansion = next_memory.cost - current_memory.cost;

Word_Size word = Word_Size(word_size_ct, length);
memory_expansion_cost = next_memory.cost - current_memory.cost;
memory_expansion_size =
(next_memory.word_size - current_memory.word_size) * 32;
Word_Size minimum_word = Word_Size(word_size_ct, length);

if constexpr (stage == GenerationStage::CONSTRAINTS) {
constrain(current_state.pc_next() - current_state.pc(0) -
1); // PC transition
constrain(current_state.gas(0) - current_state.gas_next() - 3 -
3 * word.size - memory_expansion); // GAS transition
3 * minimum_word.size -
memory_expansion_cost); // GAS transition
constrain(current_state.stack_size(0) -
current_state.stack_size_next() -
3); // stack_size transition
constrain(
current_state.memory_size(0) -
current_state.memory_size_next()); // memory_size transition
constrain(current_state.memory_size_next() -
current_state.memory_size(0) -
memory_expansion_size); // memory_size transition
constrain(current_state.rw_counter_next() -
current_state.rw_counter(0) -
3); // rw_counter transition
current_state.rw_counter(0) - 3 -
length); // rw_counter transition
std::vector<TYPE> tmp;
tmp = {TYPE(rw_op_to_num(rw_operation_type::stack)),
current_state.call_id(0),
Expand Down
Loading