Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Cyr committed Nov 12, 2024
1 parent 224c5fc commit 9f0a018
Show file tree
Hide file tree
Showing 20 changed files with 601 additions and 355 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name: PR Testing

on:
pull_request:
merge_group:
push:
branches:
- master
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ namespace nil {
}
// Opcode is not presented in RW lookup table. We just take it from json
// // std::cout << opcode << std::endl;
memory_size_before = memory.size();
if(opcode == "STOP") {
// 0x00 -- no RW operations
} else if(opcode == "ADD") {
Expand Down Expand Up @@ -460,7 +461,7 @@ namespace nil {

}
_rw_operations.push_back(stack_rw_operation(call_id, stack_next.size()-1, rw_counter++, true, stack_next[stack_next.size()-1]));

memory_size_before = memory_next.size();
} else if(opcode == "MSTORE") {
// 0x52
_rw_operations.push_back(stack_rw_operation(call_id, stack.size()-1, rw_counter++, false, stack[stack.size()-1]));
Expand All @@ -473,6 +474,7 @@ namespace nil {
for( std::size_t i = 0; i < 32; i++){
_rw_operations.push_back(memory_rw_operation(call_id, addr + i, rw_counter++, true, bytes[i]));
}
memory_size_before = memory_next.size();
} else if(opcode == "MSTORE8") {
// 0x53
_rw_operations.push_back(stack_rw_operation(call_id, stack.size()-1, rw_counter++, false, stack[stack.size()-1]));
Expand Down Expand Up @@ -944,7 +946,6 @@ namespace nil {
BOOST_ASSERT(false);
}
_zkevm_states.push_back(state);
memory_size_before = memory.size();
stack = stack_next;
memory = memory_next;
storage = storage_next;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,48 @@ namespace nil {
zkevm_opcode_tester(){}
void push_opcode(const zkevm_opcode opcode, const std::vector<std::uint8_t> &additional_input = {}){
std::cout << "PC opcode map[" << bytecode.size() << "] = " << opcodes.size() << " opcode = " << opcode_to_string(opcode) << std::endl;
std::uint8_t opcode_number = opcode_to_number(opcode);
bool is_push = (opcode_number >= 0x60) && (opcode_number <= 0x7f);

pc_opcode_map[bytecode.size()] = opcodes.size();
opcodes.push_back({opcode, word_from_byte_buffer(additional_input)});
bytecode.push_back(opcode_to_number(opcode));
bytecode.insert(bytecode.end(), additional_input.begin(), additional_input.end() );
bytecode.push_back(opcode_number);
if( is_push) {
std::uint8_t x = opcode_number - 0x5f;
for( std::size_t i = 0; i < x - additional_input.size(); i++){
bytecode.push_back(0);
}
bytecode.insert(bytecode.end(), additional_input.begin(), additional_input.end() );
BOOST_ASSERT(additional_input.size() <= x);
} else {
if( additional_input.size() != 0)
std::cout << "WRONG opcode input " << opcode
<< " " << std::hex << std::size_t(opcode_number) << std::dec
<< " additional input size = " << additional_input.size()
<< std::endl;
BOOST_ASSERT(additional_input.size() == 0);
}
}

void push_opcode(const zkevm_opcode opcode, zkevm_word_type additional_input){
std::cout << "PC opcode map[" << bytecode.size() << "] = " << opcodes.size() << " opcode = " << opcode_to_string(opcode) << std::endl;
std::uint8_t opcode_number = opcode_to_number(opcode);
std::uint8_t x = 0;
bool is_push = (opcode_number >= 0x60) && (opcode_number <= 0x7f);
auto bytes = w_to_8(additional_input);

pc_opcode_map[bytecode.size()] = opcodes.size();
opcodes.push_back({opcode, additional_input});
bytecode.push_back(opcode_number);
if( is_push) {
x = opcode_number - 0x5f;
}
for( std::size_t i = 0; i < 32 - x; i++){
BOOST_ASSERT(bytes[i] == 0);
}
for( std::size_t i = 32 - x; i < 32; i++){
bytecode.push_back(bytes[i]);
}
}

const std::vector<std::uint8_t> &get_bytecode() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,51 @@ namespace nil {
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));
_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::AND) {
// 0x16
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 result = a & b;
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, true, result));
stack.push_back(result);
pc++;
gas -= 3;
} else if(opcode == zkevm_opcode::OR) {
// 0x17
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 result = a | b;
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, true, result));
stack.push_back(result);
pc++;
gas -= 3;
} else if(opcode == zkevm_opcode::XOR) {
// 0x18
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 result = a ^ b;
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, true, result));
stack.push_back(result);
pc++;
gas -= 3;
} 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 @@ -384,63 +384,92 @@ namespace nil {
auto B_128 = chunks16_to_chunks128_reversed<TYPE>(b_chunks);
auto N_128 = chunks16_to_chunks128_reversed<TYPE>(N_chunks);
auto Res_128 = chunks16_to_chunks128_reversed<TYPE>(res);
// if constexpr (stage == GenerationStage::CONSTRAINTS) {
// constrain(current_state.pc_next() - current_state.pc(5) -
// 1); // PC transition
// constrain(current_state.gas(5) - current_state.gas_next() -
// 8); // GAS transition
// constrain(current_state.stack_size(5) - current_state.stack_size_next() -
// 2); // stack_size transition
// constrain(current_state.memory_size(5) -
// current_state.memory_size_next()); // memory_size transition
// constrain(current_state.rw_counter_next() - current_state.rw_counter(5) -
// 4); // rw_counter transition
// std::vector<TYPE> tmp;
// tmp = {TYPE(rw_op_to_num(rw_operation_type::stack)),
// 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(1),
// TYPE(0), // is_write
// A_128.first,
// A_128.second};
// lookup(tmp, "zkevm_rw");
// tmp = {TYPE(rw_op_to_num(rw_operation_type::stack)),
// current_state.call_id(1),
// current_state.stack_size(1) - 2,
// TYPE(0), // storage_key_hi
// TYPE(0), // storage_key_lo
// TYPE(0), // field
// current_state.rw_counter(1) + 1,
// TYPE(0), // is_write
// B_128.first,
// B_128.second};
// lookup(tmp, "zkevm_rw");
// tmp = {TYPE(rw_op_to_num(rw_operation_type::stack)),
// current_state.call_id(2),
// current_state.stack_size(2) - 3,
// TYPE(0), // storage_key_hi
// TYPE(0), // storage_key_lo
// TYPE(0), // field
// current_state.rw_counter(2) + 2,
// TYPE(0), // is_write
// N_128.first,
// N_128.second};
// lookup(tmp, "zkevm_rw");
// tmp = {TYPE(rw_op_to_num(rw_operation_type::stack)),
// current_state.call_id(5),
// current_state.stack_size(5) - 3,
// TYPE(0), // storage_key_hi
// TYPE(0), // storage_key_lo
// TYPE(0), // field
// current_state.rw_counter(5) + 3,
// TYPE(1), // is_write
// Res_128.first,
// Res_128.second};
// lookup(tmp, "zkevm_rw");
// }

TYPE A0, A1, B0, B1, N0, N1, Res0, Res1;
if constexpr (stage == GenerationStage::ASSIGNMENT) {
A0 = A_128.first;
A1 = A_128.second;
B0 = B_128.first;
B1 = B_128.second;
N0 = N_128.first;
N1 = N_128.second;
Res0 = Res_128.first;
Res1 = Res_128.second;
}
allocate(A0, 34, 3);
allocate(A1, 34, 1);
allocate(B0, 35, 3);
allocate(B1, 35, 1);
allocate(N0, 36, 3);
allocate(N1, 36, 1);
allocate(Res0, 37, 3);
allocate(Res1, 37, 1);

constrain(A0 - A_128.first);
constrain(A1 - A_128.second);
constrain(B0 - B_128.first);
constrain(B1 - B_128.second);
constrain(N0 - N_128.first);
constrain(N1 - N_128.second);
constrain(Res0 - Res_128.first);
constrain(Res1 - Res_128.second);
if constexpr (stage == GenerationStage::CONSTRAINTS) {
constrain(current_state.pc_next() - current_state.pc(4) -
1); // PC transition
constrain(current_state.gas(4) - current_state.gas_next() -
8); // GAS transition
constrain(current_state.stack_size(4) - current_state.stack_size_next() -
2); // stack_size transition
constrain(current_state.memory_size(4) -
current_state.memory_size_next()); // memory_size transition
constrain(current_state.rw_counter_next() - current_state.rw_counter(4) -
4); // rw_counter transition
std::vector<TYPE> tmp;
tmp = {TYPE(rw_op_to_num(rw_operation_type::stack)),
current_state.call_id(2),
current_state.stack_size(2) - 1,
TYPE(0), // storage_key_hi
TYPE(0), // storage_key_lo
TYPE(0), // field
current_state.rw_counter(2),
TYPE(0), // is_write
A0,
A1};
lookup(tmp, "zkevm_rw");
tmp = {TYPE(rw_op_to_num(rw_operation_type::stack)),
current_state.call_id(1),
current_state.stack_size(1) - 2,
TYPE(0), // storage_key_hi
TYPE(0), // storage_key_lo
TYPE(0), // field
current_state.rw_counter(1) + 1,
TYPE(0), // is_write
B0,
B1};
lookup(tmp, "zkevm_rw");
tmp = {TYPE(rw_op_to_num(rw_operation_type::stack)),
current_state.call_id(2),
current_state.stack_size(2) - 3,
TYPE(0), // storage_key_hi
TYPE(0), // storage_key_lo
TYPE(0), // field
current_state.rw_counter(2) + 2,
TYPE(0), // is_write
N0,
N1};
lookup(tmp, "zkevm_rw");
tmp = {TYPE(rw_op_to_num(rw_operation_type::stack)),
current_state.call_id(2),
current_state.stack_size(2) - 3,
TYPE(0), // storage_key_hi
TYPE(0), // storage_key_lo
TYPE(0), // field
current_state.rw_counter(2) + 3,
TYPE(1), // is_write
Res0,
Res1};
lookup(tmp, "zkevm_rw");
}
}
};

Expand All @@ -454,16 +483,15 @@ namespace nil {
&current_state) {
zkevm_addmod_bbf<FieldType, GenerationStage::ASSIGNMENT> bbf_obj(context,
current_state);
}
virtual void fill_context(
typename generic_component<FieldType,
GenerationStage::CONSTRAINTS>::context_type &context,
const opcode_input_type<FieldType, GenerationStage::CONSTRAINTS>
} virtual void fill_context(
typename generic_component<FieldType, GenerationStage::CONSTRAINTS>::context_type
&context,
const opcode_input_type<FieldType, GenerationStage::CONSTRAINTS>
&current_state) {
zkevm_addmod_bbf<FieldType, GenerationStage::CONSTRAINTS> bbf_obj(
context, current_state);
}
virtual std::size_t rows_amount() override { return 6; }
virtual std::size_t rows_amount() override { return 5; }
};
} // namespace bbf
} // namespace blueprint
Expand Down
Loading

0 comments on commit 9f0a018

Please sign in to comment.