Skip to content

Commit

Permalink
zkevm circuit fix #300
Browse files Browse the repository at this point in the history
  • Loading branch information
ETatuzova committed Feb 13, 2025
1 parent 8fc02ce commit 8b0390b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ namespace nil {
auto B_128 = chunks16_to_chunks128<TYPE>(B);
auto R_128 = chunks16_to_chunks128<TYPE>(R);
if constexpr( stage == GenerationStage::CONSTRAINTS ){
constrain(current_state.pc_next() - current_state.pc(2) - 1); // PC transition
constrain(current_state.pc_next() - current_state.pc(1) - 1); // PC transition
constrain(current_state.gas(1) - current_state.gas_next() - 5); // GAS transition
constrain(current_state.stack_size(1) - current_state.stack_size_next() - 1); // stack_size transition
constrain(current_state.memory_size(1) - current_state.memory_size_next()); // memory_size transition
Expand Down
22 changes: 16 additions & 6 deletions crypto3/libs/blueprint/include/nil/blueprint/zkevm_bbf/zkevm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,15 +441,25 @@ namespace nil {

opcode_impls[current_opcode]->fill_context(fresh_ct, opcode_state_vars);
auto opcode_constraints = fresh_ct.get_constraints();
//std::cout << "Current opcode " << current_opcode << std::endl;
for( const auto &constr_list: opcode_constraints){
for( const auto &local_row: constr_list.first){
for( auto [constraint, name]: constr_list.second){
std::size_t real_row = std::ceil(float(current_opcode_bare_rows_amount) / 2) * 2 - local_row - current_opcode_bare_rows_amount % 2;
opcode_constraints_aggregator[{current_opcode, real_row}].push_back(constraint);
std::size_t real_row = 0;
auto C = constraint;
if( local_row > std::ceil(float(current_opcode_bare_rows_amount) / 2) * 2 - current_opcode_bare_rows_amount % 2 ){
// For constraints on the next opcode state-only
auto C1 = C.rotate(1);
BOOST_ASSERT(C1);
C = *C1;
} else {
real_row = std::ceil(float(current_opcode_bare_rows_amount) / 2) * 2 - current_opcode_bare_rows_amount % 2 - local_row;
}
opcode_constraints_aggregator[{current_opcode, real_row}].push_back(C);
if(opcode_constraints_aggregator[{current_opcode, real_row}].size() > max_opcode_row_constraints){
max_opcode_row_constraints = opcode_constraints_aggregator[{current_opcode, real_row}].size();
}
//std::cout << "\t" << local_row << "=>" << real_row << ": " << constraint << std::endl;
//std::cout << "\t" << local_row << "=>" << real_row << ": " << C << std::endl;
}
//std::cout << std::endl;
}
Expand All @@ -472,7 +482,7 @@ namespace nil {
std::cout << "Accumulate constraints " << max_opcode_row_constraints << std::endl;
for( std::size_t i = 0; i < max_opcode_row_constraints; i++ ){
TYPE acc_constraint;
// std::cout << "\tConstraint " << i << std::endl;
//std::cout << "\tConstraint " << i << std::endl;
for( auto &[pair, constraints]: opcode_constraints_aggregator ){
if( constraints.size() <= i) continue;
acc_constraint += context_object.relativize(zkevm_opcode_row_selectors[pair], -1) * constraints[i];
Expand All @@ -482,7 +492,7 @@ namespace nil {
relative_mc.push_back(acc_constraint);
//std::cout << "\t" << acc_constraint << std::endl;
}
//relative_mc.push_back(context_object.relativize(zkevm_opcode_row_selectors[{zkevm_opcode::PUSH1, 1}], -1));

std::cout << "Accumulate lookup constraints " << std::endl;
std::map<std::string, std::vector<std::vector<TYPE>>> acc_lookup_constraints;
for( auto &[key, exprs]:opcode_lookup_constraints_aggregator){
Expand All @@ -493,7 +503,7 @@ namespace nil {
for( std::size_t i = 0; i < exprs.size(); i++ ) {
acc_lookup_constraints[table_name][i].resize(exprs[i].size());
for( std::size_t j = 0; j < exprs[i].size(); j++ ){
// std::cout << "\t\t" << exprs[i][j] << std::endl;
//std::cout << "\t\t" << exprs[i][j] << std::endl;
acc_lookup_constraints[table_name][i][j] += context_object.relativize(
zkevm_opcode_row_selectors[{local_opcode, local_row}], -1
) * exprs[i][j];
Expand Down

0 comments on commit 8b0390b

Please sign in to comment.