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

211 zk padding #212

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 18 additions & 3 deletions include/nil/crypto3/zk/snark/arithmetization/plonk/assignment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ namespace nil {
const ColumnType& operator[](std::uint32_t index) const {
if (index < ArithmetizationParams::witness_columns)
return _witnesses[index];
throw std::out_of_range("Public table index out of range.");
throw std::out_of_range("Public table index out of range.");
}

constexpr std::uint32_t size() const {
Expand All @@ -98,6 +98,11 @@ namespace nil {
friend std::uint32_t basic_padding<FieldType, ArithmetizationParams, ColumnType>(
plonk_table<FieldType, ArithmetizationParams, ColumnType> &table);

friend std::uint32_t zk_padding<FieldType, ArithmetizationParams, ColumnType>(
plonk_table<FieldType, ArithmetizationParams, ColumnType> &table,
typename nil::crypto3::random::algebraic_engine<FieldType> alg_rnd
);

friend struct nil::blueprint::assignment<plonk_constraint_system<FieldType,
ArithmetizationParams>>;
};
Expand Down Expand Up @@ -200,7 +205,7 @@ namespace nil {
if (index < selectors_amount()) {
return selector(index);
}
throw std::out_of_range("Public table index out of range.");
throw std::out_of_range("Public table index out of range.");
}

constexpr std::uint32_t size() const {
Expand All @@ -218,6 +223,11 @@ namespace nil {
friend std::uint32_t basic_padding<FieldType, ArithmetizationParams, ColumnType>(
plonk_table<FieldType, ArithmetizationParams, ColumnType> &table);

friend std::uint32_t zk_padding<FieldType, ArithmetizationParams, ColumnType>(
plonk_table<FieldType, ArithmetizationParams, ColumnType> &table,
typename nil::crypto3::random::algebraic_engine<FieldType> alg_rnd
);

friend struct nil::blueprint::assignment<plonk_constraint_system<FieldType,
ArithmetizationParams>>;
};
Expand Down Expand Up @@ -292,7 +302,7 @@ namespace nil {
index -= _private_table.size();
if (index < _public_table.size())
return _public_table[index];
throw std::out_of_range("Private table index out of range.");
throw std::out_of_range("Private table index out of range.");
}

const private_table_type& private_table() const {
Expand Down Expand Up @@ -371,6 +381,11 @@ namespace nil {

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

friend std::uint32_t zk_padding<FieldType, ArithmetizationParams, ColumnType>(
plonk_table &table,
typename nil::crypto3::random::algebraic_engine<FieldType> alg_rnd
);
};

template<typename FieldType, typename ArithmetizationParams>
Expand Down
65 changes: 65 additions & 0 deletions include/nil/crypto3/zk/snark/arithmetization/plonk/padding.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
#ifndef CRYPTO3_ZK_PLONK_PADDING_HPP
#define CRYPTO3_ZK_PLONK_PADDING_HPP

#include <nil/crypto3/random/algebraic_random_device.hpp>
#include <nil/crypto3/random/algebraic_engine.hpp>

namespace nil {
namespace crypto3 {
namespace zk {
Expand Down Expand Up @@ -76,6 +79,68 @@ namespace nil {
return padded_rows_amount;
}


template<typename FieldType, typename ArithmetizationParams, typename ColumnType>
std::uint32_t zk_padding(
plonk_table<FieldType, ArithmetizationParams, ColumnType> &table,
typename nil::crypto3::random::algebraic_engine<FieldType> alg_rnd = nil::crypto3::random::algebraic_engine<FieldType>()
) {
std::uint32_t usable_rows_amount = table.rows_amount();

std::uint32_t padded_rows_amount = std::pow(2, std::ceil(std::log2(usable_rows_amount)));
if (padded_rows_amount == usable_rows_amount)
padded_rows_amount *= 2;

if (padded_rows_amount < 8)
padded_rows_amount = 8;

//std::cout << "usable_rows_amount = " << usable_rows_amount << std::endl;
//std::cout << "padded_rows_amount = " << padded_rows_amount << std::endl;

for (std::uint32_t w_index = 0; w_index < table._private_table.witnesses_amount(); w_index++) {
table._private_table._witnesses[w_index].resize(usable_rows_amount, FieldType::value_type::zero());
}

for (std::uint32_t pi_index = 0; pi_index < table._public_table.public_inputs_amount(); pi_index++) {
table._public_table._public_inputs[pi_index].resize(usable_rows_amount, FieldType::value_type::zero());
}

for (std::uint32_t c_index = 0; c_index < table._public_table.constants_amount(); c_index++) {
table._public_table._constants[c_index].resize(usable_rows_amount, FieldType::value_type::zero());
}

for (std::uint32_t s_index = 0; s_index < table._public_table.selectors_amount(); s_index++) {
table._public_table._selectors[s_index].resize(usable_rows_amount, FieldType::value_type::zero());
}


for (std::uint32_t w_index = 0; w_index < table._private_table.witnesses_amount(); w_index++) {
table._private_table._witnesses[w_index].resize(padded_rows_amount);
for(std::size_t i = usable_rows_amount; i < padded_rows_amount; i++) {
table._private_table._witnesses[w_index][i] = alg_rnd();
}
}

for (std::uint32_t pi_index = 0; pi_index < table._public_table.public_inputs_amount(); pi_index++) {
table._public_table._public_inputs[pi_index].resize(padded_rows_amount, FieldType::value_type::zero());
}

for (std::uint32_t c_index = 0; c_index < table._public_table.constants_amount(); c_index++) {
table._public_table._constants[c_index].resize(padded_rows_amount);
for(std::size_t i = usable_rows_amount; i < padded_rows_amount; i++) {
table._public_table._constants[c_index][i] = alg_rnd();
}
}

for (std::uint32_t s_index = 0; s_index < table._public_table.selectors_amount(); s_index++) {
table._public_table._selectors[s_index].resize(padded_rows_amount);
for(std::size_t i = usable_rows_amount; i < padded_rows_amount; i++) {
table._public_table._selectors[s_index][i] = alg_rnd();
}
}

return padded_rows_amount;
}
} // namespace snark
} // namespace zk
} // namespace crypto3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ namespace nil {
&column_polynomials,
std::shared_ptr<math::evaluation_domain<FieldType>> original_domain,
std::uint32_t max_gates_degree,
const polynomial_dfs_type &mask_polynomial,
transcript_type& transcript) {
PROFILE_PLACEHOLDER_SCOPE("gate_argument_time");

Expand All @@ -146,7 +147,7 @@ namespace nil {
degree_limits.push_back(max_degree / 2);
extended_domain_sizes.push_back(max_domain_size / 2);

std::vector<math::expression<polynomial_dfs_variable_type>> expressions(extended_domain_sizes.size());
std::vector<math::expression<polynomial_dfs_variable_type>> expressions(extended_domain_sizes.size());

auto theta_acc = FieldType::value_type::one();

Expand All @@ -161,7 +162,7 @@ namespace nil {
const auto& gates = constraint_system.gates();

for (const auto& gate: gates) {
std::vector<math::expression<polynomial_dfs_variable_type>> gate_results(extended_domain_sizes.size());
std::vector<math::expression<polynomial_dfs_variable_type>> gate_results(extended_domain_sizes.size());

for (const auto& constraint : gate.constraints) {
auto next_term = converter.convert(constraint) * value_type_to_polynomial_dfs(theta_acc);
Expand Down Expand Up @@ -201,17 +202,19 @@ namespace nil {
expressions[i], [&assignments=variable_values, domain_size=extended_domain_sizes[i]](const polynomial_dfs_variable_type &var) {
return assignments[var];
});

F[0] += evaluator.evaluate();
}

F[0] *= mask_polynomial;
return F;
}

static inline std::array<typename FieldType::value_type, argument_size>
verify_eval(const std::vector<plonk_gate<FieldType, plonk_constraint<FieldType>>> &gates,
typename policy_type::evaluation_map &evaluations,
const typename FieldType::value_type &challenge,
typename FieldType::value_type mask_value,
transcript_type &transcript) {
typename FieldType::value_type theta = transcript.template challenge<FieldType>();

Expand All @@ -236,6 +239,7 @@ namespace nil {
F[0] += gate_result;
}

F[0] *= mask_value;
return F;
}
};
Expand Down
Loading