Skip to content

Commit

Permalink
Some comments added, copy constraint changed #305
Browse files Browse the repository at this point in the history
  • Loading branch information
ETatuzova committed Apr 8, 2024
1 parent abb2657 commit eced385
Show file tree
Hide file tree
Showing 13 changed files with 473 additions and 348 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,14 @@ namespace nil {
x_index %= fri_params.D[t - 1]->size();
x = fri_params.D[t - 1]->get_domain_element(x_index);
x = x * x;

// Last step
// Assume that FRI rounds continues with step == 1
// x_index % (domain_size / 2) -- index in the next round
// fri_params.D[t-1]->size()/4 -- half of the next domain size
// Then next round values will be written in the straight order if next round index < next domain size - 1
// Otherwise, they will be written in the reverse order.

std::size_t ind = (x_index %(fri_params.D[t-1]->size()/2) < fri_params.D[t-1]->size()/4)? 0: 1;
round_proofs[i].y.resize(1);
round_proofs[i].y[0][ind] = final_polynomial.evaluate(x);
Expand All @@ -915,7 +923,7 @@ namespace nil {
proof.final_polynomial = std::move(final_polynomial);
proof.query_proofs = std::move(query_proofs);

return proof;//typename FRI::proof_type{fri_roots, final_polynomial, query_proofs};
return proof;
}

template<typename FRI>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ namespace nil {

integral_type mask =
(GrindingBits > 0 ?
((integral_type(2) << GrindingBits - 1) - 1) << (FieldType::modulus_bits - GrindingBits)
((integral_type(1) << GrindingBits) - 1) << (FieldType::modulus_bits - GrindingBits)
: 0);

while( true ) {
Expand All @@ -119,7 +119,7 @@ namespace nil {
transcript(proof_of_work);
integral_type mask =
(GrindingBits > 0 ?
((integral_type(2) << GrindingBits - 1) - 1) << (FieldType::modulus_bits - GrindingBits)
((integral_type(1) << GrindingBits) - 1) << (FieldType::modulus_bits - GrindingBits)
: 0);

integral_type result = integral_type(transcript.template challenge<FieldType>().data);
Expand Down
281 changes: 2 additions & 279 deletions include/nil/crypto3/zk/commitments/polynomial/kzg.hpp

Large diffs are not rendered by default.

348 changes: 348 additions & 0 deletions include/nil/crypto3/zk/commitments/polynomial/kzg_v2.hpp

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions include/nil/crypto3/zk/commitments/polynomial/lpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,8 @@ namespace nil {

// List of unique eval points set. [id=>points]
std::size_t total_points = points.size();
for( std::size_t i = 0; i < _batch_fixed.size(); i++){
if( _batch_fixed[i]){ total_points++; break; }
}
if (std::any_of(_batch_fixed.begin(), _batch_fixed.end(), [](auto i){return i.second != false;})) total_points++;

typename std::vector<typename field_type::value_type> U(total_points);
// V is product of (x - eval_point) polynomial for each eval_point
typename std::vector<math::polynomial<value_type>> V(total_points);
Expand Down
40 changes: 0 additions & 40 deletions include/nil/crypto3/zk/commitments/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
namespace nil {
namespace crypto3 {
namespace zk {

BOOST_TTI_HAS_TYPE(commitment_type)
BOOST_TTI_HAS_TYPE(proof_type)

Expand Down Expand Up @@ -98,45 +97,6 @@ namespace nil {
constexpr static const bool value = !std::is_same<no, decltype(test<T>(nullptr))>::value;
};

template<typename T>
class has_available_static_member_function_is_kzg {
struct no { };

protected:
template<typename C>
static void test(std::nullptr_t) {
struct t {
using C::is_kzg;
};
}

template<typename>
static no test(...);

public:
constexpr static const bool value = !std::is_same<no, decltype(test<T>(nullptr))>::value;
};

template<typename T>
class has_available_static_member_function_is_lpc {
struct no { };

protected:
template<typename C>

static void test(std::nullptr_t) {
struct t {
using C::is_lpc;
};
}

template<typename>
static no test(...);

public:
constexpr static const bool value = !std::is_same<no, decltype(test<T>(nullptr))>::value;
};

template<typename T>
struct is_commitment {
using commitment_type = typename member_type_commitment_type<T>::type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ namespace nil {
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;
typedef FieldType field_type;

protected:
gates_container_type _gates;
Expand All @@ -77,7 +78,6 @@ namespace nil {
// If empty, then check full column
public_input_sizes_type _public_input_sizes;
public:
typedef FieldType field_type;

plonk_constraint_system() {
}
Expand Down Expand Up @@ -106,7 +106,7 @@ namespace nil {
result.insert(variable_type(var0.index, 0, true, var0.type));
result.insert(variable_type(var1.index, 0, true, var1.type));
}
return result;
return std::move(result);
}

std::size_t public_input_total_size() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,74 @@ namespace nil {
namespace snark {

template<typename FieldType>
using plonk_copy_constraint = std::pair<plonk_variable<typename FieldType::value_type>, plonk_variable<typename FieldType::value_type>>;
struct plonk_copy_constraint {
plonk_copy_constraint() = default;
plonk_copy_constraint(const plonk_copy_constraint<FieldType> &other){
initialize(other.first, other.second);
}
plonk_copy_constraint(
const plonk_variable<typename FieldType::value_type> &_first,
const plonk_variable<typename FieldType::value_type> &_second
){
initialize(_first, _second);
}
plonk_variable<typename FieldType::value_type> first;
plonk_variable<typename FieldType::value_type> second;
bool operator==(const plonk_copy_constraint<FieldType> &other){
return ((first == other.first ) && (second == other.second));
}
protected:
void initialize(
const plonk_variable<typename FieldType::value_type> &_first,
const plonk_variable<typename FieldType::value_type> &_second
){
BOOST_ASSERT(_first.relative == false);
BOOST_ASSERT(_second.relative == false);
if(_first.type == _second.type){
if(_first.index < _second.index){
first = plonk_variable<typename FieldType::value_type>(_first);
second = plonk_variable<typename FieldType::value_type>(_second);
} else if (_first.index > _second.index){
first = plonk_variable<typename FieldType::value_type>(_second);
second = plonk_variable<typename FieldType::value_type>(_first);
} else if (_first.rotation < _second.rotation){
first = plonk_variable<typename FieldType::value_type>(_first);
second = plonk_variable<typename FieldType::value_type>(_second);
} else if (_first.rotation > _second.rotation){
first = plonk_variable<typename FieldType::value_type>(_second);
second = plonk_variable<typename FieldType::value_type>(_first);
} else {
BOOST_ASSERT_MSG(false, "Copy constraint with equal variables");
}
return;
}
if( _first.type == plonk_variable<typename FieldType::value_type>::column_type::witness){
first = plonk_variable<typename FieldType::value_type>(_first);
second = plonk_variable<typename FieldType::value_type>(_second);
} else if (
_first.type == plonk_variable<typename FieldType::value_type>::column_type::public_input &&
_second.type != plonk_variable<typename FieldType::value_type>::column_type::witness
){
first = plonk_variable<typename FieldType::value_type>(_first);
second = plonk_variable<typename FieldType::value_type>(_second);
} else if(
_first.type == plonk_variable<typename FieldType::value_type>::column_type::constant &&
_second.type == plonk_variable<typename FieldType::value_type>::column_type::selector
){
first = plonk_variable<typename FieldType::value_type>(_first);
second = plonk_variable<typename FieldType::value_type>(_second);
} else {
first = plonk_variable<typename FieldType::value_type>(_second);
second = plonk_variable<typename FieldType::value_type>(_first);
}
return;
}
};

template<typename FieldType>
bool operator==(const plonk_copy_constraint<FieldType> &lhs, const plonk_copy_constraint<FieldType> &rhs) {
return ((lhs.first == rhs.first ) && (lhs.second == rhs.second)) ||
((lhs.first == rhs.second) && (lhs.second == rhs.first ));
template <typename FieldType>
bool operator==(const plonk_copy_constraint<FieldType> &a, const plonk_copy_constraint<FieldType> &b) {
return a.first == b.first && a.second == b.second;
}

} // namespace snark
} // namespace zk
} // namespace crypto3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,14 @@ namespace nil {
lookup_chunk = 0;
lookup_part = 0;
}
// +1 because lookup input is multiplied by selector
lookup_chunk += max_constraint_degree + 1;
lookup_part++;
}
}
for (const auto& table : constraint_system.lookup_tables()) {
for( const auto &lookup_options: table.lookup_options ){
// +3 because now any lookup option is lookup_column * lookup_selector * (1-q_last-q_blind) -- three polynomials degree rows_amount-1
if( lookup_chunk + 3 >= max_quotient_chunks ){
lookup_parts.push_back(lookup_part);
lookup_chunk = 0;
Expand Down Expand Up @@ -229,10 +231,6 @@ namespace nil {
math::polynomial_dfs<typename FieldType::value_type> V_L_shifted =
math::polynomial_shift(V_L, 1, basic_domain->m);

std::vector<math::polynomial_dfs<typename FieldType::value_type>> lookup_parts;
for( std::size_t i = 0; i < part_sizes.size(); i++){
}

std::array<math::polynomial_dfs<typename FieldType::value_type>, argument_size> F_dfs;

F_dfs[0] = preprocessed_data.common_data.lagrange_0 * (one_polynomial - V_L);
Expand Down Expand Up @@ -346,14 +344,14 @@ namespace nil {

// We don't use lookup_value after this line.
lookup_value_ptr.reset(nullptr);
return result;
return std::move(result);
}

std::vector<math::polynomial_dfs<typename FieldType::value_type>> compute_hs(
const std::vector<math::polynomial_dfs<typename FieldType::value_type>>& sorted,
const typename FieldType::value_type& beta,
const typename FieldType::value_type& gamma,
std::vector<std::size_t> lookup_part_sizes
const std::vector<std::size_t> &lookup_part_sizes
) {
auto one = FieldType::value_type::one();

Expand All @@ -375,7 +373,7 @@ namespace nil {
}
}
BOOST_ASSERT(h_multipliers.size() == 0);
return result;
return std::move(result);
}

math::polynomial_dfs<typename FieldType::value_type> compute_V_L(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#ifndef CRYPTO3_ZK_PLONK_PLACEHOLDER_PROOF_HPP
#define CRYPTO3_ZK_PLONK_PLACEHOLDER_PROOF_HPP

#include <cstddef>
#include <map>

namespace nil {
Expand Down
6 changes: 2 additions & 4 deletions test/systems/plonk/placeholder/placeholder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,11 @@
#include <nil/crypto3/algebra/curves/vesta.hpp>
#include <nil/crypto3/algebra/fields/arithmetic_params/vesta.hpp>
#include <nil/crypto3/algebra/random_element.hpp>

/*
#include <nil/crypto3/algebra/curves/alt_bn128.hpp>
#include <nil/crypto3/algebra/pairing/alt_bn128.hpp>
#include <nil/crypto3/algebra/fields/arithmetic_params/alt_bn128.hpp>
*/

#include <nil/crypto3/algebra/curves/mnt4.hpp>
#include <nil/crypto3/algebra/pairing/mnt4.hpp>
#include <nil/crypto3/algebra/fields/arithmetic_params/mnt4.hpp>
Expand Down Expand Up @@ -89,6 +87,7 @@
#include <nil/crypto3/zk/commitments/polynomial/fri.hpp>
#include <nil/crypto3/zk/commitments/polynomial/lpc.hpp>
#include <nil/crypto3/zk/commitments/polynomial/kzg.hpp>
#include <nil/crypto3/zk/commitments/polynomial/kzg_v2.hpp>
#include <nil/crypto3/zk/commitments/batched_commitment.hpp>

#include "circuits.hpp"
Expand Down Expand Up @@ -1510,8 +1509,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit2_kzg_v2)
selector_columns_t,
usable_rows_t,
true>
/*
, placeholder_kzg_test_fixture<
/* , placeholder_kzg_test_fixture<
algebra::curves::alt_bn128_254,
hashes::keccak_1600<256>,
hashes::keccak_1600<256>,
Expand Down
38 changes: 35 additions & 3 deletions test/systems/plonk/plonk_constraint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,24 @@
#include <nil/crypto3/random/algebraic_engine.hpp>

#include <nil/crypto3/zk/snark/arithmetization/plonk/constraint.hpp>
#include <nil/crypto3/zk/snark/arithmetization/plonk/copy_constraint.hpp>
#include <nil/crypto3/zk/snark/arithmetization/plonk/variable.hpp>
#include <nil/crypto3/zk/snark/arithmetization/plonk/params.hpp>
#include <nil/crypto3/zk/snark/arithmetization/plonk/assignment.hpp>

using namespace nil::crypto3;

BOOST_AUTO_TEST_SUITE(plonk_constraint_test_suite)

BOOST_AUTO_TEST_CASE(plonk_constraint_basic_test) {

// setup
using curve_type = algebra::curves::pallas;
using FieldType = typename curve_type::base_field_type;

using var = zk::snark::plonk_variable<typename FieldType::value_type>;

using constraint_type = zk::snark::plonk_constraint<FieldType>;
using copy_constraint_type = zk::snark::plonk_copy_constraint<FieldType>;

BOOST_AUTO_TEST_CASE(plonk_constraint_basic_test) {
constraint_type constraint = var(0, 0) + var(1, 0) - var(2, 0);
constraint_type constraint1 = var(0, 0) + var(1, 0) - 2;
constraint_type constraint2 = 2 - (var(0, 0) + var(1, 0));
Expand Down Expand Up @@ -111,4 +111,36 @@ BOOST_AUTO_TEST_CASE(plonk_constraint_basic_test) {
BOOST_CHECK((witness_columns[0][0] - witness_columns[0][0]) == constraint9.evaluate(0, assignment));
}

BOOST_AUTO_TEST_CASE(plonk_copy_constraint_constructor_test) {
var w0(0, 0, false, var::column_type::witness);
var w0_1(0, 1, false, var::column_type::witness);
var w1(1, 0, false, var::column_type::witness);
var w1_1(1, 0, false, var::column_type::witness);
var p0(0,0, false, var::column_type::public_input);
var c0(0,0, false, var::column_type::constant);
var s0(0,0, false, var::column_type::selector);
var w(0, 0, true);

// copy_constraint_type copy_constraint(w0, w0); // Fails with assersion
copy_constraint_type cp0({w0, w0_1}); BOOST_ASSERT(cp0.first == w0 && cp0.second == w0_1);
copy_constraint_type cp1({w0_1, w0}); BOOST_ASSERT(cp1.first == w0 && cp1.second == w0_1);
copy_constraint_type cp2({w0, w1}); BOOST_ASSERT(cp2.first == w0 && cp2.second == w1);
copy_constraint_type cp3({w1, w0}); BOOST_ASSERT(cp3.first == w0 && cp3.second == w1);
copy_constraint_type cp4({w0_1, w1}); BOOST_ASSERT(cp4.first == w0_1 && cp4.second == w1);
copy_constraint_type cp5({w1, w0_1}); BOOST_ASSERT(cp5.first == w0_1 && cp5.second == w1);
copy_constraint_type cp6({w0, p0}); BOOST_ASSERT(cp6.first == w0 && cp6.second == p0);
copy_constraint_type cp7({p0, w0}); BOOST_ASSERT(cp7.first == w0 && cp7.second == p0);
copy_constraint_type cp8({w0, c0}); BOOST_ASSERT(cp8.first == w0 && cp8.second == c0);
copy_constraint_type cp9({c0, w0}); BOOST_ASSERT(cp9.first == w0 && cp9.second == c0);
copy_constraint_type cp10({w0, s0}); BOOST_ASSERT(cp10.first == w0 && cp10.second == s0);
copy_constraint_type cp11({s0, w0}); BOOST_ASSERT(cp11.first == w0 && cp11.second == s0);
copy_constraint_type cp12({p0, c0}); BOOST_ASSERT(cp12.first == p0 && cp12.second == c0);
copy_constraint_type cp13({c0, p0}); BOOST_ASSERT(cp13.first == p0 && cp13.second == c0);
copy_constraint_type cp14({p0, s0}); BOOST_ASSERT(cp14.first == p0 && cp14.second == s0);
copy_constraint_type cp15({s0, p0}); BOOST_ASSERT(cp15.first == p0 && cp15.second == s0);
copy_constraint_type cp16({c0, s0}); BOOST_ASSERT(cp16.first == c0 && cp16.second == s0);
copy_constraint_type cp17({s0, c0}); BOOST_ASSERT(cp17.first == c0 && cp17.second == s0);
// copy_constraint_type cp18({w0, w}); // Fails with assertion
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit eced385

Please sign in to comment.