Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proof-producer: support zkevm bbf components #137

Draft
wants to merge 19 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crypto3/libs/blueprint/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ set_target_properties(${CMAKE_WORKSPACE_NAME}_${CURRENT_PROJECT_NAME} PROPERTIES
EXPORT_NAME ${CURRENT_PROJECT_NAME})

target_include_directories(${CMAKE_WORKSPACE_NAME}_${CURRENT_PROJECT_NAME} INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../../test_tools/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/include>)

Expand Down
91 changes: 86 additions & 5 deletions crypto3/libs/blueprint/include/nil/blueprint/bbf/generic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@

#include <nil/crypto3/zk/math/expression.hpp>
#include <nil/crypto3/zk/snark/arithmetization/plonk/constraint_system.hpp>
#include <nil/crypto3/zk/snark/arithmetization/plonk/assignment.hpp>
// #include <nil/crypto3/zk/snark/arithmetization/plonk/copy_constraint.hpp> // NB: part of the previous include

#include <nil/blueprint/blueprint/plonk/assignment.hpp>
#include <nil/blueprint/blueprint/plonk/circuit.hpp>
#include <nil/blueprint/component.hpp>
//#include <nil/blueprint/manifest.hpp>
#include <nil/blueprint/gate_id.hpp>
Expand All @@ -54,7 +53,7 @@ namespace nil {

template<typename FieldType>
class basic_context {
using assignment_type = assignment<crypto3::zk::snark::plonk_constraint_system<FieldType>>;
using assignment_type = nil::crypto3::zk::snark::plonk_assignment_table<FieldType>;
using assignment_description_type = nil::crypto3::zk::snark::plonk_table_description<FieldType>;

public:
Expand Down Expand Up @@ -175,7 +174,7 @@ namespace nil {

template<typename FieldType>
class context<FieldType, GenerationStage::ASSIGNMENT> : public basic_context<FieldType> { // assignment-specific definition
using assignment_type = assignment<crypto3::zk::snark::plonk_constraint_system<FieldType>>;
using assignment_type = nil::crypto3::zk::snark::plonk_assignment_table<FieldType>;
using assignment_description_type = nil::crypto3::zk::snark::plonk_table_description<FieldType>;
using plonk_copy_constraint = crypto3::zk::snark::plonk_copy_constraint<FieldType>;
using lookup_constraint_type = std::pair<std::string,std::vector<typename FieldType::value_type>>;
Expand Down Expand Up @@ -289,7 +288,7 @@ namespace nil {
using basic_context<FieldType>::col_map;
using basic_context<FieldType>::add_rows_to_description;

using assignment_type = assignment<crypto3::zk::snark::plonk_constraint_system<FieldType>>;
using assignment_type = nil::crypto3::zk::snark::plonk_assignment_table<FieldType>;
using assignment_description_type = nil::crypto3::zk::snark::plonk_table_description<FieldType>;

using basic_context<FieldType>::row_shift;
Expand Down Expand Up @@ -413,6 +412,24 @@ namespace nil {
add_constraint(C_rel, get_row(row));
}

void relative_constrain(TYPE C_rel, std::size_t start_row, std::size_t end_row) {
if (!is_relative(C_rel)) {
std::stringstream ss;
ss << "Constraint " << C_rel << " has absolute variables, cannot constrain.";
throw std::logic_error(ss.str());
}
add_constraint(C_rel, get_row(start_row), get_row(end_row));
}

void relative_constrain(TYPE C_rel, std::vector<std::size_t> rows) {
if (!is_relative(C_rel)) {
std::stringstream ss;
ss << "Constraint " << C_rel << " has absolute variables, cannot constrain.";
throw std::logic_error(ss.str());
}
add_constraint(C_rel, rows);
}

void lookup(std::vector<TYPE> &C, std::string table_name) {
std::set<std::size_t> base_rows = {};

Expand Down Expand Up @@ -470,6 +487,30 @@ namespace nil {
add_lookup_constraint(table_name, C, row);
}

// accesible only at GenerationStage::CONSTRAINTS !
void relative_lookup(std::vector<TYPE> &C, std::string table_name, std::size_t start_row, std::size_t end_row) {
for(const TYPE c_part : C) {
if (!is_relative(c_part)) {
std::stringstream ss;
ss << "Constraint " << c_part << " has absolute variables, cannot constrain.";
throw std::logic_error(ss.str());
}
}
add_lookup_constraint(table_name, C, start_row, end_row);
}

// accesible only at GenerationStage::CONSTRAINTS !
void relative_lookup(std::vector<TYPE> &C, std::string table_name, std::vector<std::size_t> rows) {
for(const TYPE c_part : C) {
if (!is_relative(c_part)) {
std::stringstream ss;
ss << "Constraint " << c_part << " has absolute variables, cannot constrain.";
throw std::logic_error(ss.str());
}
}
add_lookup_constraint(table_name, C, rows);
}

void lookup_table(std::string name, std::vector<std::size_t> W, std::size_t from_row, std::size_t num_rows) {
if (lookup_tables->find(name) != lookup_tables->end()) {
BOOST_LOG_TRIVIAL(error) << "Double declaration of dynamic lookup table '" << name << "'!\n";
Expand Down Expand Up @@ -602,6 +643,26 @@ namespace nil {
constraints->at(C_id).second.set_row(stored_row);
}

void add_constraint(TYPE &C_rel, std::size_t start_row, std::size_t end_row) {
std::size_t stored_start_row = start_row - (is_fresh ? row_shift : 0);
std::size_t stored_end_row = end_row - (is_fresh ? row_shift : 0);
constraint_id_type C_id = constraint_id_type(C_rel);
if (constraints->find(C_id) == constraints->end()) {
constraints->insert({C_id, {C_rel, row_selector<>(desc.rows_amount)}});
}
constraints->at(C_id).second.set_interval(stored_start_row, stored_end_row);
}

void add_constraint(TYPE &C_rel, std::vector<std::size_t> rows) {
constraint_id_type C_id = constraint_id_type(C_rel);
if (constraints->find(C_id) == constraints->end()) {
constraints->insert({C_id, {C_rel, row_selector<>(desc.rows_amount)}});
}
for( auto row: rows ){
constraints->at(C_id).second.set_row(get_row(row) - (is_fresh ? row_shift : 0));
}
}

void add_lookup_constraint(std::string table_name, std::vector<TYPE> &C_rel, std::size_t row) {
std::size_t stored_row = row - (is_fresh ? row_shift : 0);
constraint_id_type C_id = constraint_id_type(C_rel);
Expand All @@ -611,6 +672,26 @@ namespace nil {
lookup_constraints->at({table_name,C_id}).second.set_row(stored_row);
}

void add_lookup_constraint(std::string table_name, std::vector<TYPE> &C_rel, std::size_t start_row, std::size_t end_row) {
std::size_t stored_start_row = start_row - (is_fresh ? row_shift : 0);
std::size_t stored_end_row = end_row - (is_fresh ? row_shift : 0);
constraint_id_type C_id = constraint_id_type(C_rel);
if (lookup_constraints->find({table_name,C_id}) == lookup_constraints->end()) {
lookup_constraints->insert({{table_name,C_id}, {C_rel, row_selector<>(desc.rows_amount)}});
}
lookup_constraints->at({table_name,C_id}).second.set_interval(stored_start_row, stored_end_row);
}

void add_lookup_constraint(std::string table_name, std::vector<TYPE> &C_rel, std::vector<std::size_t> rows) {
constraint_id_type C_id = constraint_id_type(C_rel);
if (lookup_constraints->find({table_name,C_id}) == lookup_constraints->end()) {
lookup_constraints->insert({{table_name,C_id}, {C_rel, row_selector<>(desc.rows_amount)}});
}
for( auto row: rows ){
lookup_constraints->at({table_name,C_id}).second.set_row(row - (is_fresh ? row_shift : 0));
}
}

// Assignment description will be used when resetting the context.
assignment_description_type desc;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ namespace nil {
res = R;
};
};

} // namespace bbf
} // namespace blueprint
} // namespace nil
Expand Down
156 changes: 156 additions & 0 deletions crypto3/libs/blueprint/include/nil/blueprint/bbf/opcode_poc.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2024 Elena Tatuzova <[email protected]>
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//---------------------------------------------------------------------------//
// @file Declaration of interfaces for PLONK BBF opcode_poc component class
//---------------------------------------------------------------------------//

#pragma once

#include <functional>

#include <nil/crypto3/zk/snark/arithmetization/plonk/constraint_system.hpp>

#include <nil/blueprint/blueprint/plonk/assignment.hpp>
#include <nil/blueprint/blueprint/plonk/circuit.hpp>
#include <nil/blueprint/component.hpp>
#include <nil/blueprint/bbf/generic.hpp>

namespace nil {
namespace blueprint {
namespace bbf {
template<typename FieldType, GenerationStage stage>
class dummy_block: public generic_component<FieldType, stage> {
using typename generic_component<FieldType, stage>::context_type;
using generic_component<FieldType, stage>::allocate;
using generic_component<FieldType, stage>::constrain;
public:
using typename generic_component<FieldType,stage>::TYPE;
std::size_t block_size;
dummy_block(context_type &context_object, std::size_t _block_size):
generic_component<FieldType,stage>(context_object)
{
block_size = _block_size;
std::vector<TYPE> A(block_size);
for( std::size_t i = 0; i < block_size; i++){
if( stage == GenerationStage::ASSIGNMENT ){
A[i] = i;
// std::cout << "\t" << i << std::endl;
}
allocate(A[i], 0, i);
constrain(A[i] - i);
}
}
};

template<typename FieldType, GenerationStage stage>
class opcode_poc : public generic_component<FieldType, stage> {
using typename generic_component<FieldType, stage>::context_type;
using generic_component<FieldType, stage>::allocate;
using generic_component<FieldType, stage>::constrain;

public:
using typename generic_component<FieldType,stage>::TYPE;
using input_type = typename std::conditional<stage==GenerationStage::ASSIGNMENT, std::vector<std::uint8_t>, std::nullptr_t>::type;
public:
static nil::crypto3::zk::snark::plonk_table_description<FieldType> get_table_description(std::size_t max_rows_amount){
nil::crypto3::zk::snark::plonk_table_description<FieldType> desc(11, 1, 0, 10);
desc.usable_rows_amount = max_rows_amount;
return desc;
}

opcode_poc(context_type &context_object, const input_type &input, std::size_t max_rows) :
generic_component<FieldType,stage>(context_object) {

std::vector<std::uint8_t> block_list;
std::vector<std::array<TYPE, 5>> block_selector(max_rows);
std::vector<std::array<TYPE, 5>> block_row_selector(max_rows);
std::array<std::unordered_map<row_selector<>, std::vector<TYPE>>, 5> block_constraints;

if constexpr (stage == GenerationStage::ASSIGNMENT) {
std::cout << "Opcode POC assignment" << std::endl;
block_list = input;
std::size_t current_row = 0;
for( std::size_t i = 0; i < block_list.size(); i++){
for( std::size_t j = 0; j < block_list[i];j++, current_row++ ){
block_selector[current_row][block_list[i] - 1] = 1;
block_row_selector[current_row][j] = 1;
}
}
} else {
std::cout << "Opcode POC constraints" << std::endl;
for( std::uint8_t i = 0; i < 5; i++){
block_list.push_back(i+1);
}
}

std::size_t curr_row = 0;
for( std::size_t i = 0; i < block_list.size(); i++ ){
std::vector<std::size_t> block_area = {0};
context_type fresh_ct = context_object.fresh_subcontext(block_area, curr_row, curr_row + block_list[i]);
dummy_block<FieldType, stage> block(fresh_ct, block_list[i]);
if constexpr (stage == GenerationStage::ASSIGNMENT) {
curr_row += block.block_size;
//std::cout << "curr_row = " << curr_row << std::endl;
} else {
block_constraints[i] = fresh_ct.get_constraints();
// std::cout << "Block type " << i << std::endl;
// for( auto &constr_list: block_constraints[i]){
// for( auto &constr: constr_list.first){
// std::cout << "\t" << constr << ": ";
// for( auto row: constr_list.second){
// std::cout << row << " ";
// }
// std::cout << std::endl;
// }
// }
}
}
for( std::size_t i = 0; i < max_rows-1; i++){
for( std::size_t j = 0; j < 5; j++ ){
allocate(block_selector[i][j], j+1, i);
allocate(block_row_selector[i][j], j+6, i);
}
for( std::size_t block_type = 0; block_type < 5; block_type++ ){
for( std::size_t block_row = 0; block_row < block_type + 1; block_row ++){
if constexpr (stage == GenerationStage::CONSTRAINTS) {
TYPE pair_selector = block_selector[i][block_type] * block_row_selector[i][block_row];
//std::cout << "Pair_selector = " << pair_selector << std::endl;
TYPE pair_selector_relative = context_object.relativize(pair_selector, -i);
//std::cout << "Pair_selector_relative = " << pair_selector_relative << std::endl;
for( auto & constr_list: block_constraints[block_type] ){
if( !constr_list.first.is_set(block_row) ) continue;
for( auto &constr: constr_list.second ){
//std::cout << pair_selector_relative * constr << std::endl;
context_object.relative_constrain(pair_selector_relative * constr, i);
}
}
}
}
}
}
};
};
} // namespace bbf
} // namespace blueprint
} // namespace nil

Loading
Loading