Skip to content

Commit

Permalink
Changes in include files got from crypto3-zk #49
Browse files Browse the repository at this point in the history
  • Loading branch information
ETatuzova committed Mar 3, 2024
1 parent e198ca9 commit ac41d57
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 15 deletions.
25 changes: 19 additions & 6 deletions include/nil/crypto3/zk/commitments/detail/polynomial/basic_fri.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ namespace nil {
if (i != fri_params.step_list.size() - 1)
precommitment = precommit<FRI>(f, fri_params.D[t], fri_params.step_list[i + 1]);
}

fs.push_back(f);
math::polynomial<typename FRI::field_type::value_type> final_polynomial;
if constexpr (std::is_same<math::polynomial_dfs<typename FRI::field_type::value_type>, PolynomialType>::value) {
Expand Down Expand Up @@ -707,8 +707,16 @@ namespace nil {
}

std::array<int, FRI::lambda> challenges;
for (std::size_t query_id = 0; query_id < FRI::lambda; ++query_id)
challenges[query_id] = transcript.template int_challenge<std::uint64_t>();
for (std::size_t query_id = 0; query_id < FRI::lambda; ++query_id){
typename FRI::field_type::value_type x = transcript.template challenge<typename FRI::field_type>();
x = x.pow((FRI::field_type::modulus - 1)/fri_params.D[0]->size());
challenges[query_id] = 0;
for( challenges[query_id] = 0; challenges[query_id] < fri_params.D[0]->size(); challenges[query_id]++ ){
if( fri_params.D[0]->get_domain_element(challenges[query_id]) == x ){
break;
}
}
}

// TODO: Refactor this part a bit, maybe we can move some values? Maybe divide into a few functions?
parallel_for(0, FRI::lambda,
Expand Down Expand Up @@ -865,9 +873,14 @@ namespace nil {

std::size_t domain_size = fri_params.D[0]->size();
std::size_t coset_size = 1 << fri_params.step_list[0];
std::uint64_t x_index = (transcript.template int_challenge<std::uint64_t>()) % domain_size;
typename FRI::field_type::value_type x = fri_params.D[0]->get_domain_element(x_index);

typename FRI::field_type::value_type x_challenge = transcript.template challenge<typename FRI::field_type>();
typename FRI::field_type::value_type x = x_challenge.pow((FRI::field_type::modulus - 1)/domain_size);
std::uint64_t x_index = 0;
for( x_index = 0; x_index < domain_size; x_index++ ){
if( fri_params.D[0]->get_domain_element(x_index) == x ){
break;
}
}
std::vector<std::array<typename FRI::field_type::value_type, FRI::m>> s;
std::vector<std::array<std::size_t, FRI::m>> s_indices;
std::tie(s, s_indices) = calculate_s<FRI>(x, x_index, fri_params.step_list[0], fri_params.D[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,14 @@ namespace nil {
typedef math::term<variable_type> term_type;
typedef math::binary_arithmetic_operation<variable_type> binary_operation_type;
typedef math::pow_operation<variable_type> pow_operation_type;

typedef std::vector<std::size_t> public_input_sizes_type;
protected:
gates_container_type _gates;
copy_constraints_container_type _copy_constraints;
lookup_gates_container_type _lookup_gates;
lookup_tables_type _lookup_tables;
// If empty, then check full column
public_input_sizes_type _public_input_sizes;
public:
typedef FieldType field_type;

Expand All @@ -83,15 +85,36 @@ namespace nil {
plonk_constraint_system(const gates_container_type &gates,
const copy_constraints_container_type &copy_constraints,
const lookup_gates_container_type &lookup_gates = {},
const lookup_tables_type &lookup_tables = {}
const lookup_tables_type &lookup_tables = {},
const public_input_sizes_type public_input_sizes = {}
) :
_gates(gates),
_copy_constraints(copy_constraints),
_lookup_gates(lookup_gates),
_lookup_tables(lookup_tables)
_lookup_tables(lookup_tables),
_public_input_sizes(public_input_sizes)
{
}

std::size_t full_public_input_size() const {
std::size_t sum = 0;
for(std::size_t i = 0; i < _public_input_sizes.size(); ++i) {
sum += _public_input_sizes[i];
}
return sum;
}

std::size_t public_input_size(std::size_t i) const {
if(i >= _public_input_sizes.size()){
return 0;
}
return _public_input_sizes[i];
}

std::size_t public_input_sizes_num() const {
return _public_input_sizes.size();
}

std::size_t num_gates() const {
return _gates.size();
}
Expand Down Expand Up @@ -217,7 +240,8 @@ namespace nil {

bool operator==(const plonk_constraint_system<FieldType> &other) const {
return (this->_gates == other._gates) && (this->_copy_constraints == other._copy_constraints) &&
(this->_lookup_gates == other._lookup_gates) && (this->_lookup_tables == other._lookup_tables);
(this->_lookup_gates == other._lookup_gates) && (this->_lookup_tables == other._lookup_tables) &&
(this->_public_input_sizes == other._public_input_sizes);
}
};
} // namespace snark
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,19 @@ namespace nil {
using commitment_scheme_type = typename ParamsType::commitment_scheme_type;
using commitments_type = public_commitments_type;
using verification_key_type = verification_key;
using transcript_hash_type = typename ParamsType::transcript_hash_type;
using table_description_type = plonk_table_description<FieldType>;

// marshalled
public_commitments_type commitments;
columns_rotations_type columns_rotations;

std::size_t rows_amount;
std::size_t usable_rows_amount;
std::size_t witness_columns;
std::size_t public_input_columns;
std::size_t constant_columns;
std::size_t selector_columns;

// not marshalled. They can be derived from other fields.
polynomial_dfs_type lagrange_0;
Expand All @@ -134,14 +140,20 @@ namespace nil {
std::vector<std::set<int>> col_rotations,
std::size_t rows,
std::size_t usable_rows,
std::size_t witness_columns,
std::size_t public_input_columns,
std::size_t constant_columns,
std::size_t selector_columns,
std::uint32_t max_gates_degree,
verification_key vk
): commitments(commts),
columns_rotations(col_rotations), rows_amount(rows), usable_rows_amount(usable_rows),
lagrange_0(D->size() - 1, D->size(), FieldType::value_type::zero()),
Z(std::vector<typename FieldType::value_type>(rows + 1, FieldType::value_type::zero())),
basic_domain(D),
max_gates_degree(max_gates_degree), vk(vk)
max_gates_degree(max_gates_degree), vk(vk),
witness_columns(witness_columns), public_input_columns(public_input_columns),
constant_columns(constant_columns), selector_columns(selector_columns)
{
// Z is polynomial -1, 0,..., 0, 1
Z[0] = -FieldType::value_type::one();
Expand All @@ -157,13 +169,19 @@ namespace nil {
std::vector<std::set<int>> col_rotations,
std::size_t rows,
std::size_t usable_rows,
std::size_t witness_columns,
std::size_t public_input_columns,
std::size_t constant_columns,
std::size_t selector_columns,
std::uint32_t max_gates_degree,
verification_key vk
): commitments(commts),
columns_rotations(col_rotations), rows_amount(rows), usable_rows_amount(usable_rows),
lagrange_0(rows - 1, rows, FieldType::value_type::zero()),
Z(std::vector<typename FieldType::value_type>(rows + 1, FieldType::value_type::zero())),
max_gates_degree(max_gates_degree), vk(vk)
max_gates_degree(max_gates_degree), vk(vk),
witness_columns(witness_columns), public_input_columns(public_input_columns),
constant_columns(constant_columns), selector_columns(selector_columns)
{
// Z is polynomial -1, 0,..., 0, 1
Z[0] = -FieldType::value_type::one();
Expand All @@ -186,7 +204,11 @@ namespace nil {
lagrange_0 == rhs.lagrange_0 &&
Z == rhs.Z &&
max_gates_degree == rhs.max_gates_degree &&
vk == rhs.vk;
vk == rhs.vk &&
witness_columns == rhs.witness_columns &&
public_input_columns == rhs.public_input_columns &&
constant_columns == rhs.constant_columns &&
selector_columns == rhs.selector_columns;
}
bool operator!=(const common_data_type &rhs) const {
return !(rhs == *this);
Expand Down Expand Up @@ -508,7 +530,13 @@ namespace nil {
typename preprocessed_data_type::verification_key vk = {constraint_system_with_params_hash, public_commitments.fixed_values};
typename preprocessed_data_type::common_data_type common_data (
std::move(public_commitments), std::move(c_rotations),
N_rows, table_description.usable_rows_amount, max_gates_degree, vk
table_description.rows_amount,
table_description.usable_rows_amount,
table_description.witness_columns,
table_description.public_input_columns,
table_description.constant_columns,
table_description.selector_columns,
max_gates_degree, vk
);

transcript_type transcript(std::vector<std::uint8_t>({}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,17 @@ namespace nil {
auto numerator = challenge.pow(preprocessed_public_data.common_data.rows_amount) - FieldType::value_type::one();
numerator /= typename FieldType::value_type(preprocessed_public_data.common_data.rows_amount);

// If public input sizes are set, all sizes should be set.
if(constraint_system.public_input_sizes_num() != 0 && constraint_system.public_input_sizes_num() != table_description.public_input_columns){
return false;
}

for( std::size_t i = 0; i < public_input.size(); ++i ){
typename FieldType::value_type value = FieldType::value_type::zero();
std::size_t max_size = constraint_system.public_input_sizes_num() == 0 ? public_input[i].size() : constraint_system.public_input_size(i);
max_size = std::min(max_size, public_input[i].size());
auto omega_pow = FieldType::value_type::one();
for( std::size_t j = 0; j < public_input[i].size(); ++j ){
for( std::size_t j = 0; j < max_size; ++j ){
value += (public_input[i][j] * omega_pow) / (challenge - omega_pow);
omega_pow = omega_pow * omega;
}
Expand Down

0 comments on commit ac41d57

Please sign in to comment.