Skip to content

Commit

Permalink
DIV, MOD, SDIV, SMOD #95
Browse files Browse the repository at this point in the history
  • Loading branch information
ETatuzova committed Nov 28, 2024
1 parent bf1e37d commit b06cb1a
Show file tree
Hide file tree
Showing 8 changed files with 786 additions and 254 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -411,10 +411,6 @@ namespace nil {
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, a));
auto n = w_to_8(N)[31];
zkevm_word_type result = N > 31? 0: w_to_8(a)[n];
// std::cout << "BYTE" << std::endl;
// std::cout << std::hex << "\ta = " << a << std::dec << std::endl;
// std::cout << std::hex << "\tN = " << N << std::dec << std::endl;
// std::cout << std::hex << "\tresult = " << result << std::dec << std::endl;
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, true, result));
stack.push_back(result);
pc++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ namespace nil {
auto N = current_state.stack_top();
auto b = w_to_16(current_state.stack_top());
auto a = w_to_16(current_state.stack_top(1));
std::cout << "\tb = " << std::hex << current_state.stack_top() << std::dec << std::endl;
std::cout << "\ta = " << std::hex << current_state.stack_top(1) << std::dec << std::endl;
for( std::size_t i = 0; i < 16; i++){
A[i] = a[i];
B[i] = b[i];
Expand All @@ -80,11 +78,6 @@ namespace nil {
minus_a16_hi = 255 - a16_hi;
minus_a16_lo = 255 - a16_lo;
result = (b_last_bit == 0)? a16_hi: a16_lo;
std::cout << "\ta16 = " << std::hex << a16 << std::endl;
std::cout << "\ta16_hi = " << a16_hi << std::endl;
std::cout << "\ta16_lo = " << a16_lo << std::endl;
std::cout << "\tresult = " << result << std::endl;
std::cout << "\tb_sum = " << b_sum << std::dec << std::endl;
}
for( std::size_t i = 0; i < 16; i++ ){
allocate(A[i], i, 0);
Expand Down Expand Up @@ -120,8 +113,6 @@ namespace nil {
b_sum_constraint += b_hi;
b_bits_composition *= 2;
b_bits_composition += b_last_bit;
std::cout << "\tb_bits_composition = " << std::hex << b_bits_composition << std::dec << std::endl;
std::cout << "\tb_sum_constraint = " << std::hex << b_sum_constraint << std::dec << std::endl;
constrain(b_bits_composition - b_bits_sum * b_lo);
constrain(b_sum_constraint - b_sum);
constrain(b_sum * (b_sum * b_sum_inv - 1));
Expand All @@ -136,8 +127,6 @@ namespace nil {

auto A_128 = chunks16_to_chunks128<TYPE>(A);
auto B_128 = chunks16_to_chunks128<TYPE>(B);
std::cout << "\tA128 = " << std::hex << A_128.first << ", " << A_128.second << std::dec << std::endl;
std::cout << "\tB128 = " << std::hex << B_128.first << ", " << B_128.second << std::dec << std::endl;
if constexpr( stage == GenerationStage::CONSTRAINTS ){
constrain(current_state.pc_next() - current_state.pc(1) - 1); // PC transition
constrain(current_state.gas(1) - current_state.gas_next() - 3); // GAS transition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,17 +326,18 @@ namespace nil {
3); // rw_counter transition
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,
TYPE(0), // storage_key_hi
TYPE(0), // storage_key_lo
TYPE(0), // field
current_state.rw_counter(0),
TYPE(0), // is_write
A0,
A1};
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,
// TYPE(0), // storage_key_hi
// TYPE(0), // storage_key_lo
// TYPE(0), // field
// current_state.rw_counter(0),
// TYPE(0), // is_write
// A0,
// A1};
// lookup(tmp, "zkevm_rw");
/*
tmp = {TYPE(rw_op_to_num(rw_operation_type::stack)),
current_state.call_id(0),
current_state.stack_size(0) - 2,
Expand All @@ -347,6 +348,7 @@ namespace nil {
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),
Expand All @@ -358,8 +360,9 @@ namespace nil {
TYPE(1), // is_write
Res0,
Res1};
lookup(tmp, "zkevm_rw");
lookup(tmp, "zkevm_rw");*/
}

}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ namespace nil {
std::vector<TYPE> q_chunks(chunk_amount);
std::vector<TYPE> v_chunks(chunk_amount);

std::vector<TYPE> indic_1(chunk_amount);
std::vector<TYPE> indic_2(chunk_amount);

TYPE carry[3][carry_amount + 1];

if constexpr (stage == GenerationStage::ASSIGNMENT) {
Expand Down Expand Up @@ -249,11 +252,19 @@ namespace nil {
z = (1 - b0ppp * I1) *
(1 -
sum_part_b * I2); // z is zero if input_b >= 256, otherwise it is 1
two_powers =
(static_cast<unsigned int>(1) << int(integral_type(input_b) % 16));
tp = z * (static_cast<unsigned int>(1) << int(integral_type(input_b) % 16));
b_sum_inverse = b_sum.is_zero() ? 0 : b_sum.inversed();
b_zero = 1 - b_sum_inverse * b_sum;

two_powers = 0;
unsigned int pow = 1;
for (std::size_t i = 0; i < chunk_amount; i++) {
indic_1[i] = (b0p - i).is_zero() ? 0 : (b0p - i).inversed();
indic_2[i] = (b0pp - i).is_zero() ? 0 : (b0pp - i).inversed();
two_powers += (1 - (b0p - i) * indic_1[i]) * pow;
pow *= 2;
}

// note that we don't assign 64-chunks for a/b, as we can build them from
// 16-chunks with constraints under the same logic we only assign the 16 -
// bit
Expand Down Expand Up @@ -352,20 +363,28 @@ namespace nil {
constrain(b_zero * r_chunks[i]);
}

tp = z * two_powers;
allocate(tp, 35, 3);
allocate(z, 36, 3);
allocate(I1, 37, 3);
allocate(I2, 38, 3);
allocate(two_powers, 39, 3);

allocate(b0p, 32, 3);
allocate(b0pp, 33, 3);
allocate(b0ppp, 34, 3);
allocate(sum_part_b, 40, 3);
allocate(tp, 12, 4);
allocate(z, 13, 4);
allocate(I1, 39, 2);
allocate(I2, 40, 2);
allocate(two_powers, 41, 2);

allocate(b0p, 9, 4);
allocate(b0pp, 10, 4);
allocate(b0ppp, 11, 4);
allocate(sum_part_b, 42, 2);
allocate(b_sum, 40, 1);
allocate(b_sum_inverse, 41, 1);

constrain(tp - z * two_powers);
for (std::size_t i = 0; i < chunk_amount; i++) {
allocate(indic_1[i], i + 2 * chunk_amount, 3);
allocate(indic_2[i], i + 2 * chunk_amount, 4);
constrain((b0p - i) * (1 - (b0p - i) * indic_1[i]));
constrain((b0pp - i) * (1 - (b0pp - i) * indic_2[i]));
constrain(b_chunks[i] - tp * (1 - (b0pp - i) * indic_2[i]));
}

constrain(b_sum_inverse * (b_sum_inverse * b_sum - 1));
constrain(b_sum * (b_sum_inverse * b_sum - 1));
constrain(input_b_chunks[0] - b0p - 16 * b0pp - 256 * b0ppp);
Expand All @@ -374,11 +393,11 @@ namespace nil {
constrain(sum_part_b * (1 - sum_part_b * I2));
constrain((z - (1 - b0ppp * I1) * (1 - sum_part_b * I2)));

allocate(first_carryless, 32, 4);
allocate(second_carryless, 35, 4);
allocate(third_carryless, 36, 4);
allocate(c_1_64, 33, 4);
allocate(c_2, 34, 4);
allocate(first_carryless, 39, 0);
allocate(second_carryless, 40, 0);
allocate(third_carryless, 41, 0);
allocate(c_1_64, 42, 0);
allocate(c_2, 43, 0);

allocate(b_64_chunks[3], 7, 4);
allocate(r_64_chunks[3], 8, 4);
Expand Down Expand Up @@ -488,39 +507,43 @@ namespace nil {
B1 = B_128.second;
Res0 = Res_128.first;
Res1 = Res_128.second;
allocate(A0, 39, 0);
allocate(A1, 40, 0);
allocate(B0, 39, 2);
allocate(B1, 40, 2);
allocate(Res0, 41, 0);
allocate(Res1, 41, 2);
allocate(A0, 45, 0);
allocate(A1, 46, 0);
allocate(B0, 45, 2);
allocate(B1, 46, 2);
allocate(Res0, 47, 0);
allocate(Res1, 47, 2);

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() - 3); // GAS transition
constrain(current_state.stack_size(4) - current_state.stack_size_next() - 1); // stack_size transition
constrain(current_state.memory_size(4) - current_state.memory_size_next()); // memory_size transition
constrain(current_state.pc_next() - current_state.pc(4) -
1); // PC transition
constrain(current_state.gas(4) - current_state.gas_next() -
3); // GAS transition
constrain(current_state.stack_size(4) - current_state.stack_size_next() -
1); // 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) -
3); // 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,
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(1),
current_state.rw_counter(2),
TYPE(0), // is_write
B0,
B1};
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,
current_state.call_id(0),
current_state.stack_size(0) - 2,
TYPE(0), // storage_key_hi
TYPE(0), // storage_key_lo
TYPE(0), // field
current_state.rw_counter(1) + 1,
current_state.rw_counter(0) + 1,
TYPE(0), // is_write
A0,
A1};
Expand Down
Loading

0 comments on commit b06cb1a

Please sign in to comment.