Skip to content

Commit

Permalink
SAR and NOT added #95
Browse files Browse the repository at this point in the history
  • Loading branch information
ETatuzova committed Nov 26, 2024
1 parent 5dad982 commit 724ace0
Show file tree
Hide file tree
Showing 8 changed files with 970 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ namespace nil {
const std::map<std::size_t, std::uint8_t> memory;
const std::map<zkevm_word_type, zkevm_word_type> storage;

for( std::size_t ind = 0; ind < tester.get_opcodes().size(); ind++ ){
auto opcode = tester.get_opcodes()[ind].first;
auto additional_input = tester.get_opcodes()[ind].second;
while(true){
auto [opcode,additional_input] = tester.get_opcode_by_pc(pc);

zkevm_state state; // TODO:optimize
state.tx_hash = 0; // * change it
Expand All @@ -82,7 +81,7 @@ namespace nil {
state.rw_counter = rw_counter;
state.bytecode_hash = _bytecodes.get_data()[current_buffer_id].second;
state.additional_input = additional_input;
state.tx_finish = (ind == tester.get_opcodes().size() - 1);
//state.tx_finish = (ind == tester.get_opcodes().size() - 1);
state.stack_size = stack.size();
state.memory_size = memory.size();
state.stack_slice = stack;
Expand Down Expand Up @@ -305,6 +304,16 @@ namespace nil {
stack.push_back(result);
pc++;
gas -= 3;
} else if(opcode == zkevm_opcode::NOT) {
// 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 result = zkevm_word_type(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_cppui_modular257) - a;;
_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::SHL) {
// 0x1b
zkevm_word_type b = stack.back();
Expand Down Expand Up @@ -334,6 +343,28 @@ namespace nil {
stack.push_back(result);
pc++;
gas -= 3;
}else if(opcode == zkevm_opcode::SAR) {
//0x1d
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 input_a = stack.back();
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, input_a));
auto is_negative = [](zkevm_word_type x) {
return (integral_type(x) > zkevm_modulus / 2 - 1);};
auto negate_word = [](zkevm_word_type x) {
return zkevm_word_type(zkevm_modulus - integral_type(x));};
auto abs_word = [&is_negative, &negate_word](zkevm_word_type x) {
return is_negative(x) ? negate_word(x) : x;};
zkevm_word_type a = abs_word(input_a);
int shift = (integral_type(b) < 256) ? int(integral_type(b)) : 256;
integral_type r_integral = integral_type(a) >> shift;
zkevm_word_type result = is_negative(a) ? ((r_integral == 0)? zkevm_word_type(zkevm_modulus-1) : negate_word(zkevm_word_type(r_integral))) : zkevm_word_type(r_integral);
_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::PUSH0){
// 0x5f
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, true, additional_input));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,62 @@ namespace nil {
generic_component<FieldType,stage>(context_object, false)
{
// NOT checked by current test
// 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); // GAS transition
// constrain(current_state.stack_size(0) - current_state.stack_size_next()); // stack_size transition
// constrain(current_state.memory_size(0) - current_state.memory_size_next()); // memory_size transition
// constrain(current_state.rw_counter_next() - current_state.rw_counter(0) - 2); // rw_counter transition
// }
std::vector<TYPE> A(16);
std::vector<TYPE> R(16);

if constexpr( stage == GenerationStage::ASSIGNMENT ){
auto a = w_to_16(current_state.stack_top());
for( std::size_t i = 0; i < 16; i++ ){
A[i] = a[i];
R[i] = 0xFFFF - a[i];
}
}
for( std::size_t i = 0; i < 16; i++ ){
allocate(A[i], i, 0);
allocate(R[i], i + 16, 0);
}
for( std::size_t i = 0; i < 16; i++ ){
constrain(R[i] + A[i] - 0xFFFF);
}

auto A_128 = chunks16_to_chunks128<TYPE>(A);
auto R_128 = chunks16_to_chunks128<TYPE>(R);
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); // GAS transition
constrain(current_state.stack_size(0) - current_state.stack_size_next()); // stack_size transition
constrain(current_state.memory_size(0) - current_state.memory_size_next()); // memory_size transition
constrain(current_state.rw_counter_next() - current_state.rw_counter(0) - 2); // 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) - 1,
TYPE(0),// storage_key_hi
TYPE(0),// storage_key_lo
TYPE(0),// field
current_state.rw_counter(1) + 1,
TYPE(1),// is_write
R_128.first,
R_128.second
};
lookup(tmp, "zkevm_rw");
} else {
std::cout << "Assignment implemented" << std::endl;
}
}
};

Expand Down
Loading

0 comments on commit 724ace0

Please sign in to comment.