Skip to content

Commit

Permalink
Mod_ops test added, lookup constraints amount decreased #95
Browse files Browse the repository at this point in the history
  • Loading branch information
ETatuzova committed Nov 11, 2024
1 parent 578312c commit 4505f99
Show file tree
Hide file tree
Showing 7 changed files with 373 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,38 @@ namespace nil {
stack.push_back(additional_input);
gas -= 3;
pc += 33;
} else if(opcode == zkevm_opcode::ADDMOD) {
// 0x08
zkevm_word_type a = stack.back();
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, a));
zkevm_word_type b = stack.back();
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, b));
zkevm_word_type modulus = stack.back();
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, modulus));
zkevm_word_type result = integral_type(modulus) == 0? 0 :(a + b) % modulus;
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, true, result));
stack.push_back(result);
pc++;
gas -= 8;
} else if(opcode == zkevm_opcode::MULMOD) {
// 0x09
zkevm_word_type a = stack.back();
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, a));
zkevm_word_type b = stack.back();
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, b));
zkevm_word_type modulus = stack.back();
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, modulus));
zkevm_word_type result = integral_type(modulus) == 0? 0 : (a * b) % modulus;
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, true, modulus));
stack.push_back(result);
pc++;
gas -= 8;
} else {
BOOST_ASSERT(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ namespace nil {
generic_component<FieldType,stage>(context_object, false)
{
TYPE addr;
TYPE addr1;
TYPE addr_mod;
TYPE addr_mod31;
TYPE addr_words;
Expand All @@ -67,6 +68,7 @@ namespace nil {
bytes[i] = TYPE(b);
}
addr = address;
addr1 = address;
// addr + 31 -- maximum address
// memory_size -- previous memory_size
// if ( memory_size < addr + 31 ) then
Expand Down Expand Up @@ -97,11 +99,13 @@ namespace nil {
std::cout << "\tMEMORY SIZE NOT CHANGED " << addr_words << " <= " << memory_words << std::endl;
}
}
for( std::size_t i = 0; i < 32; i++ ){
for( std::size_t i = 0; i < 16; i++ ){
allocate(bytes[i], i+16, 0);
allocate(bytes[i+16], i+16, 1);
}

allocate(addr, 0, 0);
allocate(addr1, 0, 1);
allocate(addr_mod, 1, 0);
allocate(addr_mod31 , 2, 0);
allocate(addr_words, 3, 0);
Expand All @@ -121,50 +125,66 @@ namespace nil {
TYPE old_memory_gas_cost = 3 * memory_words; // TODO: check and test with large memory consumption
TYPE new_memory_gas_cost = 3 * addr_words; // TODO: check and test with large memory consumption

constrain(current_state.pc_next() - current_state.pc(0) - 1); // PC transition
constrain(current_state.pc_next() - current_state.pc(1) - 1); // PC transition
//constrain(current_state.gas(0) - current_state.gas_next() - 3 - is_memory_size_changed * (new_memory_gas_cost - old_memory_gas_cost)); // GAS transition
constrain(current_state.stack_size(0) - current_state.stack_size_next()); // stack_size transition
constrain(current_state.stack_size(1) - current_state.stack_size_next()); // stack_size transition
//constrain(new_memory_size - current_state.memory_size_next()); // memory_size transition
constrain(current_state.rw_counter_next() - current_state.rw_counter(0) - 34); // rw_counter transition
constrain(current_state.rw_counter_next() - current_state.rw_counter(1) - 34); // rw_counter transition

auto V_128 = chunks8_to_chunks128<TYPE>(bytes);
std::vector<TYPE> tmp;
tmp = {
TYPE(rw_op_to_num(rw_operation_type::stack)),
current_state.call_id(0),
current_state.stack_size(0) - 1,
current_state.call_id(1),
current_state.stack_size(1) - 1,
TYPE(0), // storage_key_hi
TYPE(0), // storage_key_lo
TYPE(0), // field
current_state.rw_counter(0),
current_state.rw_counter(1),
TYPE(0), // is_write
TYPE(0), // hi bytes are 0
addr // addr is smaller than maximum contract size
};
lookup(tmp, "zkevm_rw");
for( std::size_t i = 0; i < 32; i++ ){
tmp = {
TYPE(rw_op_to_num(rw_operation_type::memory)),
current_state.call_id(0),
addr + i,
TYPE(0), // storage_key_hi
TYPE(0), // storage_key_lo
TYPE(0), // field
current_state.rw_counter(0) + i + 1,
TYPE(0), // is_write
TYPE(0), // hi bytes are 0
bytes[i] // addr is smaller than maximum contract size
};
lookup(tmp, "zkevm_rw");
if( i < 16 ){
tmp = {
TYPE(rw_op_to_num(rw_operation_type::memory)),
current_state.call_id(0),
addr + i,
TYPE(0), // storage_key_hi
TYPE(0), // storage_key_lo
TYPE(0), // field
current_state.rw_counter(0) + i + 1,
TYPE(0), // is_write
TYPE(0), // hi bytes are 0
bytes[i] // addr is smaller than maximum contract size
};
lookup(tmp, "zkevm_rw");
} else {
tmp = {
TYPE(rw_op_to_num(rw_operation_type::memory)),
current_state.call_id(1),
addr1 + i,
TYPE(0), // storage_key_hi
TYPE(0), // storage_key_lo
TYPE(0), // field
current_state.rw_counter(1) + i + 1,
TYPE(0), // is_write
TYPE(0), // hi bytes are 0
bytes[i] // addr is smaller than maximum contract size
};
lookup(tmp, "zkevm_rw");
}
}
tmp = {
TYPE(rw_op_to_num(rw_operation_type::stack)),
current_state.call_id(0),
current_state.stack_size(0) - 1,
current_state.call_id(1),
current_state.stack_size(1) - 1,
TYPE(0), // storage_key_hi
TYPE(0), // storage_key_lo
TYPE(0), // field
current_state.rw_counter(0)+33,
current_state.rw_counter(1)+33,
TYPE(1), // is_write
V_128.first, // hi bytes are 0
V_128.second // addr is smaller than maximum contract size
Expand Down Expand Up @@ -192,7 +212,7 @@ namespace nil {
zkevm_mload_bbf<FieldType, GenerationStage::CONSTRAINTS> bbf_obj(context, current_state);
}
virtual std::size_t rows_amount() override {
return 1;
return 2;
}
};
} // namespace bbf
Expand Down
Loading

0 comments on commit 4505f99

Please sign in to comment.