Skip to content

Commit

Permalink
Merge pull request #104 from NilFoundation/81-blueprint-arch-compatib…
Browse files Browse the repository at this point in the history
…ility

PLONK constraint system and table interfaces updated
  • Loading branch information
nkaskov authored Nov 21, 2022
2 parents 8e5dd88 + 33f31a2 commit d55a528
Show file tree
Hide file tree
Showing 3 changed files with 270 additions and 77 deletions.
237 changes: 176 additions & 61 deletions include/nil/crypto3/zk/snark/arithmetization/plonk/assignment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,125 +27,178 @@
#ifndef CRYPTO3_ZK_PLONK_PLACEHOLDER_TABLE_HPP
#define CRYPTO3_ZK_PLONK_PLACEHOLDER_TABLE_HPP

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

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

namespace nil {
namespace blueprint {
template<typename ArithmetizationType, std::size_t... BlueprintParams>
class assignment;
} // namespace blueprint
namespace crypto3 {
namespace zk {
namespace snark {

template<typename FieldType, typename ArithmetizationParams>
struct plonk_constraint_system;

template<typename FieldType>
using plonk_column = std::vector<typename FieldType::value_type>;

template<typename FieldType, typename ArithmetizationParams, typename ColumnType>
struct plonk_table;

template<typename FieldType, typename ArithmetizationParams, typename ColumnType>
struct plonk_private_table {

using witnesses_container_type = std::array<ColumnType, ArithmetizationParams::witness_columns>;

protected:
std::array<ColumnType, ArithmetizationParams::witness_columns> witness_columns;

witnesses_container_type _witnesses;

public:
plonk_private_table(
std::array<ColumnType, ArithmetizationParams::witness_columns> witness_columns = {}) :
witness_columns(witness_columns) {
witnesses_container_type witness_columns = {}) :
_witnesses(witness_columns) {
}

std::uint32_t witnesses_amount() const {
return _witnesses.size();
}

ColumnType witness(std::size_t index) const {
BOOST_ASSERT(index < ArithmetizationParams::witness_columns);
std::uint32_t witness_column_size(std::uint32_t index) const {
return _witnesses[index].size();
}

return witness_columns[index];
ColumnType witness(std::uint32_t index) const {
assert(index < ArithmetizationParams::witness_columns);
return _witnesses[index];
}

std::array<ColumnType, ArithmetizationParams::witness_columns> witnesses() const {
return witness_columns;
witnesses_container_type witnesses() const {
return _witnesses;
}

ColumnType operator[](std::size_t index) const {
ColumnType operator[](std::uint32_t index) const {
if (index < ArithmetizationParams::witness_columns)
return witness_columns[index];
return _witnesses[index];
index -= ArithmetizationParams::witness_columns;
return {};

}

constexpr std::size_t size() const {
return witness_columns.size();
constexpr std::uint32_t size() const {
return witnesses_amount();
}

friend std::uint32_t basic_padding<FieldType, ArithmetizationParams, ColumnType>(
plonk_table<FieldType, ArithmetizationParams, ColumnType> &table);

friend struct nil::blueprint::assignment<plonk_constraint_system<FieldType,
ArithmetizationParams>>;
};

template<typename FieldType, typename ArithmetizationParams, typename ColumnType>
struct plonk_public_table {

using public_input_container_type = std::array<ColumnType, ArithmetizationParams::public_input_columns>;
using constant_container_type = std::array<ColumnType, ArithmetizationParams::constant_columns>;
using selector_container_type = std::array<ColumnType, ArithmetizationParams::selector_columns>;

protected:
std::array<ColumnType, ArithmetizationParams::public_input_columns> public_input_columns;
std::array<ColumnType, ArithmetizationParams::constant_columns> constant_columns;
std::array<ColumnType, ArithmetizationParams::selector_columns> selector_columns;

public_input_container_type _public_inputs;
constant_container_type _constants;
selector_container_type _selectors;

public:
plonk_public_table(
std::array<ColumnType, ArithmetizationParams::public_input_columns> public_input_columns = {},
std::array<ColumnType, ArithmetizationParams::constant_columns> constant_columns = {},
std::array<ColumnType, ArithmetizationParams::selector_columns> selector_columns = {}) :
public_input_columns(public_input_columns),
constant_columns(constant_columns), selector_columns(selector_columns) {
public_input_container_type public_input_columns = {},
constant_container_type constant_columns = {},
selector_container_type selector_columns = {}) :
_public_inputs(public_input_columns),
_constants(constant_columns),
_selectors(selector_columns) {
}

ColumnType public_input(std::size_t index) const {
BOOST_ASSERT(index < ArithmetizationParams::public_input_columns);
std::uint32_t public_inputs_amount() const {
return _public_inputs.size();
}

return public_input_columns[index];
std::uint32_t public_input_column_size(std::uint32_t index) const {
return _public_inputs[index].size();
}

std::array<ColumnType, ArithmetizationParams::public_input_columns> public_inputs() const {
return public_input_columns;
ColumnType public_input(std::uint32_t index) const {
assert(index < public_inputs_amount());
return _public_inputs[index];
}

std::size_t public_input_size() {
return public_input_columns.size();
public_input_container_type public_inputs() const {
return _public_inputs;
}

ColumnType constant(std::size_t index) const {
BOOST_ASSERT(index < ArithmetizationParams::constant_columns);
std::uint32_t constants_amount() const {
return _constants.size();
}

return constant_columns[index];
std::uint32_t constant_column_size(std::uint32_t index) const {
return _constants[index].size();
}

std::array<ColumnType, ArithmetizationParams::constant_columns> constants() const {
return constant_columns;
ColumnType constant(std::uint32_t index) const {
assert(index < constants_amount());
return _constants[index];
}

std::size_t constant_size() {
return constant_columns.size();
constant_container_type constants() const {
return _constants;
}

ColumnType selector(std::size_t index) const {
BOOST_ASSERT(index < ArithmetizationParams::selector_columns);
constexpr std::uint32_t selectors_amount() const {
return _selectors.size();
}

return selector_columns[index];
std::uint32_t selector_column_size(std::uint32_t index) const {
return _selectors[index].size();
}

std::array<ColumnType, ArithmetizationParams::selector_columns> selectors() const {
return selector_columns;
ColumnType selector(std::uint32_t index) const {
assert(index < selectors_amount());
return _selectors[index];
}

std::size_t selectors_size() {
return selector_columns.size();
selector_container_type selectors() const {
return _selectors;
}

ColumnType operator[](std::size_t index) const {
if (index < ArithmetizationParams::public_input_columns)
return public_input_columns[index];
index -= ArithmetizationParams::public_input_columns;
if (index < ArithmetizationParams::constant_columns)
return constant_columns[index];
index -= ArithmetizationParams::constant_columns;
if (index < ArithmetizationParams::selector_columns) {
return selector_columns[index];
ColumnType operator[](std::uint32_t index) const {
if (index < public_inputs_amount())
return public_input(index);
index -= public_inputs_amount();
if (index < constants_amount())
return constant(index);
index -= constants_amount();
if (index < selectors_amount()) {
return selector(index);
}
index -= ArithmetizationParams::selector_columns;
index -= selectors_amount();
return {};
}

constexpr std::size_t size() const {
return ArithmetizationParams::public_input_columns + ArithmetizationParams::constant_columns +
ArithmetizationParams::selector_columns;
constexpr std::uint32_t size() const {
return public_inputs_amount() +
constants_amount() +
selectors_amount();
}

friend std::uint32_t basic_padding<FieldType, ArithmetizationParams, ColumnType>(
plonk_table<FieldType, ArithmetizationParams, ColumnType> &table);

friend struct nil::blueprint::assignment<plonk_constraint_system<FieldType,
ArithmetizationParams>>;
};

template<typename FieldType, typename ArithmetizationParams, typename ColumnType>
Expand All @@ -165,28 +218,29 @@ namespace nil {
_public_table(public_table) {
}

ColumnType witness(std::size_t index) const {
ColumnType witness(std::uint32_t index) const {
return _private_table.witness(index);
}

ColumnType public_input(std::size_t index) const {
ColumnType public_input(std::uint32_t index) const {
return _public_table.public_input(index);
}

ColumnType constant(std::size_t index) const {
ColumnType constant(std::uint32_t index) const {
return _public_table.constant(index);
}

ColumnType selector(std::size_t index) const {
ColumnType selector(std::uint32_t index) const {
return _public_table.selector(index);
}

ColumnType operator[](std::size_t index) const {
ColumnType operator[](std::uint32_t index) const {
if (index < _private_table.size())
return _private_table[index];
index -= _private_table.size();
if (index < _public_table.size())
return _public_table[index];
return {};
}

private_table_type private_table() const {
Expand All @@ -197,9 +251,70 @@ namespace nil {
return _public_table;
}

std::size_t size() const {
std::uint32_t size() const {
return _private_table.size() + _public_table.size();
}

std::uint32_t witnesses_amount() const {
return _private_table.witnesses_amount();
}

std::uint32_t witness_column_size(std::uint32_t index) const {
return _private_table.witness_column_size(index);
}

std::uint32_t public_inputs_amount() const {
return _public_table.public_inputs_amount();
}

std::uint32_t public_input_column_size(std::uint32_t index) const {
return _public_table.public_input_column_size(index);
}

std::uint32_t constants_amount() const {
return _public_table.constants_amount();
}

std::uint32_t constant_column_size(std::uint32_t index) const {
return _public_table.constant_column_size(index);
}

std::uint32_t selectors_amount() const {
return _public_table.selectors_amount();
}

std::uint32_t selector_column_size(std::uint32_t index) const {
return _public_table.selector_column_size(index);
}

std::uint32_t rows_amount() const {
std::uint32_t rows_amount = 0;

for (std::uint32_t w_index = 0; w_index <
witnesses_amount(); w_index++) {
rows_amount = std::max(rows_amount, witness_column_size(w_index));
}

for (std::uint32_t pi_index = 0; pi_index <
public_inputs_amount(); pi_index++) {
rows_amount = std::max(rows_amount, public_input_column_size(pi_index));
}

for (std::uint32_t c_index = 0; c_index <
constants_amount(); c_index++) {
rows_amount = std::max(rows_amount, constant_column_size(c_index));
}

for (std::uint32_t s_index = 0; s_index <
selectors_amount(); s_index++) {
rows_amount = std::max(rows_amount, selector_column_size(s_index));
}

return rows_amount;
}

friend std::uint32_t basic_padding<FieldType, ArithmetizationParams, ColumnType>(
plonk_table &table);
};

template<typename FieldType, typename ArithmetizationParams>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,24 @@ namespace nil {

template<typename FieldType, typename ArithmetizationParams>
struct plonk_constraint_system {
typedef plonk_gate<FieldType, plonk_constraint<FieldType>> gate_type;
typedef plonk_copy_constraint<FieldType> copy_constraint_type;
typedef plonk_gate<FieldType, plonk_lookup_constraint<FieldType>> lookup_gate_type;

typedef std::vector<gate_type> gates_type;
typedef std::vector<copy_constraint_type> copy_constraints_type;
typedef std::vector<lookup_gate_type> lookup_gates_type;
typedef std::vector<plonk_gate<FieldType, plonk_constraint<FieldType>>> gates_container_type;
typedef std::vector<plonk_copy_constraint<FieldType>> copy_constraints_container_type;
typedef std::vector<plonk_gate<FieldType, plonk_lookup_constraint<FieldType>>> lookup_gates_container_type;

protected:
gates_type _gates;
copy_constraints_type _copy_constraints;
lookup_gates_type _lookup_gates;
gates_container_type _gates;
copy_constraints_container_type _copy_constraints;
lookup_gates_container_type _lookup_gates;

public:
typedef FieldType field_type;

plonk_constraint_system() {
}

plonk_constraint_system(const gates_type &gates,
const copy_constraints_type &copy_constraints,
const lookup_gates_type &lookup_gates) :
plonk_constraint_system(const gates_container_type &gates,
const copy_constraints_container_type &copy_constraints,
const lookup_gates_container_type &lookup_gates) :
_gates(gates),
_copy_constraints(copy_constraints), _lookup_gates(lookup_gates) {
}
Expand All @@ -95,15 +91,15 @@ namespace nil {
// return true;
// }

const gates_type &gates() const {
const gates_container_type &gates() const {
return _gates;
}

const copy_constraints_type &copy_constraints() const {
const copy_constraints_container_type &copy_constraints() const {
return _copy_constraints;
}

const lookup_gates_type &lookup_gates() const {
const lookup_gates_container_type &lookup_gates() const {
return _lookup_gates;
}
};
Expand Down
Loading

0 comments on commit d55a528

Please sign in to comment.