Skip to content

Commit

Permalink
Lookups to bytecode circuits added, test assigner redesigned #325
Browse files Browse the repository at this point in the history
  • Loading branch information
ETatuzova committed Oct 1, 2024
1 parent 6f51905 commit 57a0459
Show file tree
Hide file tree
Showing 46 changed files with 1,266 additions and 1,062 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,6 @@ namespace nil {
if (!constraint_result.is_zero()) {
std::cout << "Constraint " << j << " from gate " << i << " on row " << selector_row
<< " is not satisfied." << std::endl;
for(std::size_t k = 0; k < assignments.rows_amount(); k++){
std::cout << gates[i].constraints[j].evaluate(k, assignments) << " ";
}
std::cout << std::endl;
std::cout << "Constraint: " << gates[i].constraints[j] << std::endl;
std::cout << "Constraint result: " << constraint_result << std::endl;
Expand Down
14 changes: 2 additions & 12 deletions libs/blueprint/include/nil/blueprint/zkevm/operations/add_sub.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,9 @@ namespace nil {
}

void generate_assignments(zkevm_table_type &zkevm_table, const zkevm_machine_interface &machine) override {
zkevm_stack stack = machine.stack;
using word_type = typename zkevm_stack::word_type;
word_type a = stack.pop();
word_type b = stack.pop();
word_type a = machine.stack_top();
word_type b = machine.stack_top(1);
word_type result = is_add ? a + b : a - b;
// TODO: after memory logic would become more complicated here
if (!is_add) {
Expand Down Expand Up @@ -165,15 +164,6 @@ namespace nil {
}
carry = (carry + a_chunks[3 * (carry_amount - 1)] + b_chunks[3 * (carry_amount - 1)]) >= two_16;
assignment.witness(witness_cols[a_chunks.size() + carry_amount - 1], curr_row + 2) = carry;

// stack.push(b);
if (is_add) {
stack.push(result);
//stack.push(a);
} else {
stack.push(a);
//stack.push(result);
}
}

std::size_t rows_amount() override {
Expand Down
13 changes: 3 additions & 10 deletions libs/blueprint/include/nil/blueprint/zkevm/operations/addmod.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,9 @@ namespace nil {
using integral_type = boost::multiprecision::number<
boost::multiprecision::backends::cpp_int_modular_backend<257>>;

zkevm_stack stack = machine.stack;

word_type a = stack.pop();
word_type b = stack.pop();
word_type N = stack.pop();
word_type a = machine.stack_top();
word_type b = machine.stack_top(1);
word_type N = machine.stack_top(2);

integral_type s_integral = integral_type(a) + integral_type(b);
int is_overflow = (s_integral >= zkevm_modulus);
Expand Down Expand Up @@ -441,11 +439,6 @@ namespace nil {
for (std::size_t i = 0; i < chunk_amount; i++) {
assignment.witness(witness_cols[i + chunk_amount], curr_row + 4) = q_out_chunks[i];
}

// stack.push(N);
// stack.push(b);
// stack.push(a);
stack.push(result);
}

std::size_t rows_amount() override {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,11 @@ namespace nil {
}

void generate_assignments(zkevm_table_type &zkevm_table, const zkevm_machine_interface &machine) override {
zkevm_stack stack = machine.stack;
using word_type = typename zkevm_stack::word_type;
using integral_type = typename BlueprintFieldType::integral_type;

word_type a = stack.pop();
word_type b = stack.pop();
word_type a = machine.stack_top();
word_type b = machine.stack_top(1);

word_type result;
switch(bit_operation) {
Expand All @@ -136,12 +135,6 @@ namespace nil {
assignment.witness(witness_cols[2*i], curr_row + 2) = integral_type(r_chunks[i].data) % 256;
assignment.witness(witness_cols[2*i+1], curr_row + 2) = integral_type(r_chunks[i].data) / 256;
}

/*
stack.push(b);
stack.push(a);
*/
stack.push(result);
}

std::size_t rows_amount() override {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,12 @@ namespace nil {
}

void generate_assignments(zkevm_table_type &zkevm_table, const zkevm_machine_interface &machine) override {
zkevm_stack stack = machine.stack;
using word_type = typename zkevm_stack::word_type;
using integral_type = boost::multiprecision::number<
boost::multiprecision::backends::cpp_int_modular_backend<257>>;

word_type i = stack.pop();
word_type x = stack.pop();
word_type i = machine.stack_top();
word_type x = machine.stack_top(1);
int shift = (integral_type(i) < 32) ? int(integral_type(i)) : 32;
word_type result = word_type((integral_type(x) << ((8*shift) + 1)) >> (31*8 + 1));
// +1 because integral type is 257 bits long
Expand Down Expand Up @@ -182,10 +181,6 @@ namespace nil {
assignment.witness(witness_cols[chunk_amount + 5], curr_row) = xp;
assignment.witness(witness_cols[chunk_amount + 6], curr_row) = xpp;
assignment.witness(witness_cols[chunk_amount + 7], curr_row) = static_cast<value_type>(integral_type(result));

// stack.push(x);
// stack.push(i);
stack.push(result);
}

std::size_t rows_amount() override {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ namespace nil {
}

void generate_assignments(zkevm_table_type &zkevm_table, const zkevm_machine_interface &machine) override {
zkevm_stack stack = machine.stack;
std::cout << "Generate assignments and gates for CALLDATALOAD" << std::endl;
stack.push(0);
}

std::size_t rows_amount() override {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ namespace nil {
}

void generate_assignments(zkevm_table_type &zkevm_table, const zkevm_machine_interface &machine) override {
zkevm_stack stack = machine.stack;
std::cout << "Generate assignments and gates for CALLDATASIZE" << std::endl;
stack.push(0);
}

std::size_t rows_amount() override {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ namespace nil {
}

void generate_assignments(zkevm_table_type &zkevm_table, const zkevm_machine_interface &machine) override {
zkevm_stack stack = machine.stack;
std::cout << "Generate assignments and gates for CALLVALUE" << std::endl;
stack.push(0);
}

std::size_t rows_amount() override {
Expand Down
9 changes: 2 additions & 7 deletions libs/blueprint/include/nil/blueprint/zkevm/operations/cmp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ namespace nil {
}

void generate_assignments(zkevm_table_type &zkevm_table, const zkevm_machine_interface &machine) override {
zkevm_stack stack = machine.stack;
using word_type = typename zkevm_stack::word_type;
using integral_type = boost::multiprecision::number<
boost::multiprecision::backends::cpp_int_modular_backend<257>>;
Expand All @@ -183,8 +182,8 @@ namespace nil {
return (integral_type(x) > zkevm_modulus/2 - 1);
};

word_type x = stack.pop();
word_type y = stack.pop();
word_type x = machine.stack_top();
word_type y = machine.stack_top(1);
word_type r;

if ((cmp_operation == C_LT) || (cmp_operation == C_SLT)) {
Expand Down Expand Up @@ -273,10 +272,6 @@ namespace nil {
carry = (carry + a_chunks[3 * (carry_amount - 1)] + b_chunks[3 * (carry_amount - 1)]) >= two_16;
BOOST_ASSERT(carry == r);
assignment.witness(witness_cols[chunk_amount + carry_amount - 1], curr_row + 2) = carry;

//stack.push(y);
//stack.push(x);
stack.push(result);
}

std::size_t rows_amount() override {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,8 @@ namespace nil {

void generate_assignments(zkevm_table_type &zkevm_table, const zkevm_machine_interface &machine) override {
using word_type = typename zkevm_stack::word_type;
zkevm_stack stack = machine.stack;
word_type a = stack.pop();
word_type b = stack.pop();
word_type a = machine.stack_top();
word_type b = machine.stack_top(1);
using integral_type = boost::multiprecision::number<
boost::multiprecision::backends::cpp_int_modular_backend<257>>;
integral_type r_integral = b != 0u ? integral_type(a) / integral_type(b) : 0u;
Expand Down Expand Up @@ -378,14 +377,11 @@ namespace nil {
}
}

// stack.push(b);
// stack.push(a);
// TODO: Just for testing purposes. May be removed or commented.
if( is_div)
BOOST_ASSERT(result == std::get<0>(eth_div(a,b)));
else
BOOST_ASSERT(result == std::get<1>(eth_div(a,b)));
stack.push(result);
}

std::size_t rows_amount() override {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ namespace nil {
}

void generate_assignments(zkevm_table_type &zkevm_table, const zkevm_machine_interface &machine) override{
zkevm_stack stack = machine.stack;
std::cout << "Generate assignments for DUPx opcodes" << std::endl;
stack.push(0);
}

std::size_t rows_amount() override {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,12 @@ namespace nil {
// cost + d3 = gas + g*2^32 <=> g = [cost > gas]
constraints.push_back({position, cost + d30_var + 65536*d31_var - gas_var - g_var*(circuit_integral_type(1) << 32)});

constraints.push_back({position, (si_var - 1)*(so_var - 1)*(g_var - 1)});
//constraints.push_back({position, (si_var - 1)*(so_var - 1)*(g_var - 1)});
return {{gate_class::MIDDLE_OP, {constraints, {}}}};
}

void generate_assignments(zkevm_table_type &zkevm_table, const zkevm_machine_interface &machine,
zkevm_word_type opcode_num) {

zkevm_stack stack = machine.stack;
using word_type = typename zkevm_stack::word_type;
using integral_type = boost::multiprecision::number<boost::multiprecision::backends::cpp_int_modular_backend<257>>;
using circuit_integral_type = typename BlueprintFieldType::integral_type;
Expand All @@ -143,8 +141,8 @@ namespace nil {
n = static_cast<std::size_t>(opcode / 2),
col = n % 44,
row = n / 44;
value_type gas = machine.gas,
stack_size = machine.stack.size();
value_type gas = machine.gas(),
stack_size = machine.stack_size();

auto opcode_mnemo = zkevm_table.get_opcodes_info().get_opcode_from_number(static_cast<std::size_t>(opcode));
std::size_t cost = zkevm_table.get_opcodes_info().get_opcode_cost(opcode_mnemo),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,8 @@ namespace nil {
}

void generate_assignments(zkevm_table_type &zkevm_table, const zkevm_machine_interface &machine) override {
zkevm_stack stack = machine.stack;
using word_type = typename zkevm_stack::word_type;
word_type a = stack.pop();
word_type a = machine.stack_top();
const std::vector<value_type> chunks = zkevm_word_to_field_element<BlueprintFieldType>(a);
const std::vector<std::size_t> &witness_cols = zkevm_table.get_opcode_cols();
assignment_type &assignment = zkevm_table.get_assignment();
Expand All @@ -104,8 +103,6 @@ namespace nil {
const value_type chunk_sum = std::accumulate(chunks.begin(), chunks.end(), value_type::zero());
assignment.witness(witness_cols[2*chunk_amount], curr_row) =
chunk_sum == 0 ? value_type::zero() : value_type::one() * chunk_sum.inversed();
//stack.push(a);
stack.push(word_type(a == 0u));
}

std::size_t rows_amount() override {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ namespace nil {
const std::size_t curr_row = zkevm_table.get_current_row();
auto witness_cols = zkevm_table.get_opcode_cols();

zkevm_word_type dest = machine.stack.top();
zkevm_word_type dest = machine.stack_top();
std::cout << "JUMP assign destination = " << dest << std::endl;
assignment.witness(witness_cols[0], curr_row) = w_lo<BlueprintFieldType>(dest);
}
Expand Down Expand Up @@ -134,7 +134,6 @@ namespace nil {
}

void generate_assignments(zkevm_table_type &zkevm_table, const zkevm_machine_interface &machine) override {
zkevm_stack stack = machine.stack;
std::cout << "Generate assignments and gates for JUMPDEST" << std::endl;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ namespace nil {
}

void generate_assignments(zkevm_table_type &zkevm_table, const zkevm_machine_interface &machine) override {
zkevm_word_type dest = machine.stack.top();
zkevm_word_type condition = machine.stack.top(1);
zkevm_word_type dest = machine.stack_top();
zkevm_word_type condition = machine.stack_top(1);
const std::vector<value_type> chunks = zkevm_word_to_field_element<BlueprintFieldType>(condition);

assignment_type &assignment = zkevm_table.get_assignment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ namespace nil {
}

void generate_assignments(zkevm_table_type &zkevm_table, const zkevm_machine_interface &machine) override {
zkevm_stack stack = machine.stack;
std::cout << "Generate assignments and gates for MLOAD" << std::endl;
stack.pop();
}

virtual constraint_type gas_transition(const zkevm_circuit_type &zkevm_circuit) override {
Expand Down
8 changes: 2 additions & 6 deletions libs/blueprint/include/nil/blueprint/zkevm/operations/mul.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,9 @@ namespace nil {
}

void generate_assignments(zkevm_table_type &zkevm_table, const zkevm_machine_interface &machine) override {
zkevm_stack stack = machine.stack;
using word_type = typename zkevm_stack::word_type;
word_type a = stack.pop();
word_type b = stack.pop();
word_type a = machine.stack_top();
word_type b = machine.stack_top(1);
word_type result = a * b;
const std::vector<value_type> a_chunks = zkevm_word_to_field_element<BlueprintFieldType>(a);
const std::vector<value_type> b_chunks = zkevm_word_to_field_element<BlueprintFieldType>(b);
Expand Down Expand Up @@ -210,9 +209,6 @@ namespace nil {
}
assignment.witness(witness_cols[chunk_amount], curr_row + 2) = c_2;
assignment.witness(witness_cols[1 + chunk_amount], curr_row + 2) = c_4;
//stack.push(b);
//stack.push(a);
stack.push(result);
}

std::size_t rows_amount() override {
Expand Down
13 changes: 3 additions & 10 deletions libs/blueprint/include/nil/blueprint/zkevm/operations/mulmod.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,11 +458,9 @@ namespace nil {
using integral_type = boost::multiprecision::number<boost::multiprecision::backends::cpp_int_modular_backend<257>>;
using extended_integral_type = boost::multiprecision::number<boost::multiprecision::backends::cpp_int_modular_backend<512>>;

zkevm_stack stack = machine.stack;

word_type input_a = stack.pop();
word_type b = stack.pop();
word_type N = stack.pop();
word_type input_a = machine.stack_top();
word_type b = machine.stack_top(1);
word_type N = machine.stack_top(2);

word_type a = N != 0u ? input_a : 0;

Expand Down Expand Up @@ -653,11 +651,6 @@ namespace nil {
assignment.witness(witness_cols[4 + chunk_amount], curr_row + 7) = c_2;
assignment.witness(witness_cols[9 + chunk_amount], curr_row + 7) = c_4;
assignment.witness(witness_cols[14 + chunk_amount], curr_row + 7) = c_6;

// stack.push(N);
// stack.push(b);
// stack.push(input_a);
stack.push(result);
}

std::size_t rows_amount() override {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,10 @@ namespace nil {
}

void generate_assignments(zkevm_table_type &zkevm_table, const zkevm_machine_interface &machine) override {
zkevm_stack stack = machine.stack;
using word_type = typename zkevm_stack::word_type;
using integral_type = boost::multiprecision::number<
boost::multiprecision::backends::cpp_int_modular_backend<257>>;
word_type a = stack.pop();
word_type a = machine.stack_top();
word_type result = word_type((~integral_type(a)) % zkevm_modulus);

const std::vector<value_type> a_chunks = zkevm_word_to_field_element<BlueprintFieldType>(a);
Expand All @@ -151,8 +150,6 @@ namespace nil {
}
carry = (carry + a_chunks[3 * (carry_amount - 1)] + b_chunks[3 * (carry_amount - 1)]) >= two_16;
assignment.witness(witness_cols[a_chunks.size() + carry_amount - 1], curr_row + 1) = carry;

stack.push(result);
}

std::size_t rows_amount() override {
Expand Down
2 changes: 0 additions & 2 deletions libs/blueprint/include/nil/blueprint/zkevm/operations/pop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ namespace nil {
}

void generate_assignments(zkevm_table_type &zkevm_table, const zkevm_machine_interface &machine) override {
zkevm_stack stack = machine.stack;
std::cout << "Generate assignments and gates for POP" << std::endl;
stack.pop();
}

std::size_t rows_amount() override {
Expand Down
Loading

0 comments on commit 57a0459

Please sign in to comment.