From 2688eb5d1c876cdbff5bb51df3951a323efac9d0 Mon Sep 17 00:00:00 2001 From: Mikhail Komarov Date: Mon, 22 Apr 2024 17:39:16 +0300 Subject: [PATCH] Initial refactoring implemented #318 --- include/nil/crypto3/zk/snark/proof.hpp | 6 +- include/nil/crypto3/zk/snark/proving_key.hpp | 4 +- .../crypto3/zk/snark/routing/as_waksman.hpp | 92 +- .../zk/snark/systems/plonk/pickles/alphas.hpp | 11 +- .../snark/systems/plonk/pickles/constants.hpp | 1 - .../systems/plonk/pickles/constraints.hpp | 23 +- .../zk/snark/systems/plonk/pickles/detail.hpp | 1 + .../plonk/pickles/detail/kimchi_functions.hpp | 17 +- .../systems/plonk/pickles/detail/mapping.hpp | 44 +- .../snark/systems/plonk/pickles/oracles.hpp | 162 +- .../systems/plonk/pickles/permutation.hpp | 10 +- .../zk/snark/systems/plonk/pickles/proof.hpp | 78 +- .../snark/systems/plonk/pickles/verifier.hpp | 241 +-- .../systems/plonk/pickles/verifier_index.hpp | 16 +- ...placeholder_policy.hpp => placeholder.hpp} | 26 +- .../plonk/placeholder/gates_argument.hpp | 4 +- .../plonk/placeholder/lookup_argument.hpp | 373 ++-- .../placeholder/permutation_argument.hpp | 2 +- .../plonk/placeholder/preprocessor.hpp | 6 +- .../systems/plonk/placeholder/prover.hpp | 272 +-- .../systems/plonk/placeholder/verifier.hpp | 254 +-- .../systems/ppzksnark/r1cs_gg_ppzksnark.hpp | 86 +- .../nil/crypto3/zk/snark/verification_key.hpp | 4 +- test/systems/plonk/placeholder/circuits.hpp | 374 ++-- .../systems/plonk/placeholder/performance.cpp | 204 +- .../systems/plonk/placeholder/placeholder.cpp | 1786 +++++++++-------- 26 files changed, 2142 insertions(+), 1955 deletions(-) rename include/nil/crypto3/zk/snark/systems/plonk/{placeholder/detail/placeholder_policy.hpp => placeholder.hpp} (71%) diff --git a/include/nil/crypto3/zk/snark/proof.hpp b/include/nil/crypto3/zk/snark/proof.hpp index 4d88b9685..ad2cbc210 100644 --- a/include/nil/crypto3/zk/snark/proof.hpp +++ b/include/nil/crypto3/zk/snark/proof.hpp @@ -31,9 +31,9 @@ namespace nil { namespace crypto3 { namespace zk { namespace snark { - template - class proof : public zk::proof { - typedef zk::proof policy_type; + template + class proof : public zk::proof { + typedef zk::proof policy_type; public: typedef typename policy_type::scheme_type scheme_type; diff --git a/include/nil/crypto3/zk/snark/proving_key.hpp b/include/nil/crypto3/zk/snark/proving_key.hpp index 510fc0746..335b22371 100644 --- a/include/nil/crypto3/zk/snark/proving_key.hpp +++ b/include/nil/crypto3/zk/snark/proving_key.hpp @@ -29,9 +29,9 @@ namespace nil { namespace crypto3 { namespace zk { namespace snark { - template + template class proving_key { - typedef ZkScheme scheme_type; + typedef ProofSystemType scheme_type; }; } // namespace snark } // namespace zk diff --git a/include/nil/crypto3/zk/snark/routing/as_waksman.hpp b/include/nil/crypto3/zk/snark/routing/as_waksman.hpp index 2783614d7..6c5b5f889 100644 --- a/include/nil/crypto3/zk/snark/routing/as_waksman.hpp +++ b/include/nil/crypto3/zk/snark/routing/as_waksman.hpp @@ -149,7 +149,7 @@ namespace nil { /** * Return the height of the AS-Waksman network's top sub-network. */ - inline std::size_t as_waksman_top_height(std::size_t num_packets) const { + inline std::size_t as_waksman_top_height(std::size_t num_packets) { return num_packets / 2; } @@ -164,8 +164,9 @@ namespace nil { * * If top = true, return the top wire, otherwise return bottom wire. */ - inline std::size_t as_waksman_switch_output(size_t num_packets, std::size_t row_offset, std::size_t row_idx, - bool use_top) const{ + inline std::size_t + as_waksman_switch_output(size_t num_packets, std::size_t row_offset, std::size_t row_idx, + bool use_top) { std::size_t relpos = row_idx - row_offset; assert(relpos % 2 == 0 && relpos + 1 < num_packets); return row_offset + (relpos / 2) + (use_top ? 0 : as_waksman_top_height(num_packets)); @@ -177,8 +178,9 @@ namespace nil { * * This function is analogous to as_waksman_switch_output above. */ - inline std::size_t as_waksman_switch_input(size_t num_packets, std::size_t row_offset, std::size_t row_idx, - bool use_top) { + inline std::size_t + as_waksman_switch_input(size_t num_packets, std::size_t row_offset, std::size_t row_idx, + bool use_top) { /* Due to symmetry, this function equals as_waksman_switch_output. */ return as_waksman_switch_output(num_packets, row_offset, row_idx, use_top); } @@ -200,11 +202,11 @@ namespace nil { * This function fills out neighbors[left] and neighbors[right-1]. */ inline void construct_as_waksman_inner(size_t left, - std::size_t right, - std::size_t lo, - std::size_t hi, - const std::vector &rhs_dests, - as_waksman_topology &neighbors) { + std::size_t right, + std::size_t lo, + std::size_t hi, + const std::vector &rhs_dests, + as_waksman_topology &neighbors) { if (left > right) { return; } @@ -222,7 +224,7 @@ namespace nil { for (std::size_t packet_idx = lo; packet_idx <= hi; ++packet_idx) { neighbors[left][packet_idx].first = neighbors[left][packet_idx].second = packet_idx; neighbors[right][packet_idx].first = neighbors[right][packet_idx].second = - rhs_dests[packet_idx - lo]; + rhs_dests[packet_idx - lo]; } std::vector new_rhs_dests(subnetwork_size, -1); @@ -252,18 +254,18 @@ namespace nil { for (std::size_t row_idx = lo; row_idx < (subnetwork_size % 2 == 1 ? hi : hi + 1); row_idx += 2) { neighbors[left][row_idx].first = neighbors[left][row_idx + 1].second = - as_waksman_switch_output(subnetwork_size, lo, row_idx, true); + as_waksman_switch_output(subnetwork_size, lo, row_idx, true); neighbors[left][row_idx].second = neighbors[left][row_idx + 1].first = - as_waksman_switch_output(subnetwork_size, lo, row_idx, false); + as_waksman_switch_output(subnetwork_size, lo, row_idx, false); new_rhs_dests[as_waksman_switch_input(subnetwork_size, lo, row_idx, true) - lo] = row_idx; new_rhs_dests[as_waksman_switch_input(subnetwork_size, lo, row_idx, false) - lo] = - row_idx + 1; + row_idx + 1; neighbors[right][row_idx].first = neighbors[right][row_idx + 1].second = - rhs_dests[row_idx - lo]; + rhs_dests[row_idx - lo]; neighbors[right][row_idx].second = neighbors[right][row_idx + 1].first = - rhs_dests[row_idx + 1 - lo]; + rhs_dests[row_idx + 1 - lo]; } if (subnetwork_size % 2 == 1) { @@ -302,7 +304,8 @@ namespace nil { as_waksman_topology neighbors(width, std::vector>( - num_packets, std::make_pair(-1, -1))); + num_packets, + std::make_pair(-1, -1))); std::vector rhs_dests(num_packets); for (std::size_t packet_idx = 0; packet_idx < num_packets; ++packet_idx) { @@ -337,8 +340,8 @@ namespace nil { * - the output position for the RHS switches. */ inline bool as_waksman_get_switch_setting_from_top_bottom_decision(size_t row_offset, - std::size_t packet_idx, - bool use_top) { + std::size_t packet_idx, + bool use_top) { const std::size_t row_idx = as_waksman_get_canonical_row_idx(row_offset, packet_idx); return (packet_idx == row_idx) ^ use_top; } @@ -354,8 +357,8 @@ namespace nil { * - the output position for the LHS switches. */ inline bool as_waksman_get_top_bottom_decision_from_switch_setting(size_t row_offset, - std::size_t packet_idx, - bool switch_setting) { + std::size_t packet_idx, + bool switch_setting) { const std::size_t row_idx = as_waksman_get_canonical_row_idx(row_offset, packet_idx); return (row_idx == packet_idx) ^ switch_setting; } @@ -392,12 +395,12 @@ namespace nil { * NOTE: due to offsets, neither pi or piinv are instances of integer_permutation. */ inline void as_waksman_route_inner(size_t left, - std::size_t right, - std::size_t lo, - std::size_t hi, - const math::integer_permutation &permutation, - const math::integer_permutation &permutation_inv, - as_waksman_routing &routing) { + std::size_t right, + std::size_t lo, + std::size_t hi, + const math::integer_permutation &permutation, + const math::integer_permutation &permutation_inv, + as_waksman_routing &routing) { if (left > right) { return; } @@ -462,9 +465,9 @@ namespace nil { * using the lower subnetwork. */ const std::size_t rhs_switch = - as_waksman_get_canonical_row_idx(lo, permutation.get(hi)); + as_waksman_get_canonical_row_idx(lo, permutation.get(hi)); const bool rhs_switch_setting = as_waksman_get_switch_setting_from_top_bottom_decision( - lo, permutation.get(hi), false); + lo, permutation.get(hi), false); routing[right][rhs_switch] = rhs_switch_setting; std::size_t tprime = as_waksman_switch_input(subnetwork_size, lo, rhs_switch, false); new_permutation.set(hi, tprime); @@ -500,9 +503,9 @@ namespace nil { } const bool lhs_switch_setting = routing[left][lhs_switch]; const bool use_top = as_waksman_get_top_bottom_decision_from_switch_setting( - lo, to_route, lhs_switch_setting); + lo, to_route, lhs_switch_setting); const std::size_t t = - as_waksman_switch_output(subnetwork_size, lo, lhs_switch, use_top); + as_waksman_switch_output(subnetwork_size, lo, lhs_switch, use_top); if (permutation.get(to_route) == hi) { /** * We have routed to the straight wire for the odd case, @@ -515,16 +518,16 @@ namespace nil { route_left = true; } else { const std::size_t rhs_switch = - as_waksman_get_canonical_row_idx(lo, permutation.get(to_route)); + as_waksman_get_canonical_row_idx(lo, permutation.get(to_route)); /** * We know that the corresponding switch on the right-hand side * cannot be set, so we set it according to the incoming wire. */ assert(routing[right].find(rhs_switch) == routing[right].end()); routing[right][rhs_switch] = as_waksman_get_switch_setting_from_top_bottom_decision( - lo, permutation.get(to_route), use_top); + lo, permutation.get(to_route), use_top); const std::size_t tprime = - as_waksman_switch_input(subnetwork_size, lo, rhs_switch, use_top); + as_waksman_switch_input(subnetwork_size, lo, rhs_switch, use_top); new_permutation.set(t, tprime); new_permutation_inv.set(tprime, t); @@ -539,13 +542,13 @@ namespace nil { */ const std::size_t rhs_switch = as_waksman_get_canonical_row_idx(lo, to_route); const std::size_t lhs_switch = - as_waksman_get_canonical_row_idx(lo, permutation_inv.get(to_route)); + as_waksman_get_canonical_row_idx(lo, permutation_inv.get(to_route)); assert(routing[right].find(rhs_switch) != routing[right].end()); const bool rhs_switch_setting = routing[right][rhs_switch]; const bool use_top = as_waksman_get_top_bottom_decision_from_switch_setting( - lo, to_route, rhs_switch_setting); + lo, to_route, rhs_switch_setting); const bool lhs_switch_setting = as_waksman_get_switch_setting_from_top_bottom_decision( - lo, permutation_inv.get(to_route), use_top); + lo, permutation_inv.get(to_route), use_top); /* The value on the left-hand side is either the same or not set. */ auto it = routing[left].find(lhs_switch); @@ -554,7 +557,7 @@ namespace nil { const std::size_t t = as_waksman_switch_input(subnetwork_size, lo, rhs_switch, use_top); const std::size_t tprime = - as_waksman_switch_output(subnetwork_size, lo, lhs_switch, use_top); + as_waksman_switch_output(subnetwork_size, lo, lhs_switch, use_top); new_permutation.set(tprime, t); new_permutation_inv.set(t, tprime); @@ -594,8 +597,11 @@ namespace nil { const math::integer_permutation new_permutation_upper = new_permutation.slice(lo, lo + d - 1); const math::integer_permutation new_permutation_lower = new_permutation.slice(lo + d, hi); - const math::integer_permutation new_permutation_inv_upper = new_permutation_inv.slice(lo, lo + d - 1); - const math::integer_permutation new_permutation_inv_lower = new_permutation_inv.slice(lo + d, hi); + const math::integer_permutation new_permutation_inv_upper = new_permutation_inv.slice(lo, + lo + d - + 1); + const math::integer_permutation new_permutation_inv_lower = new_permutation_inv.slice(lo + d, + hi); as_waksman_route_inner(left + 1, right - 1, @@ -620,7 +626,7 @@ namespace nil { } inline bool valid_as_waksman_routing(const math::integer_permutation &permutation, - const as_waksman_routing &routing) { + const as_waksman_routing &routing) { const std::size_t num_packets = permutation.size(); const std::size_t width = as_waksman_num_columns(num_packets); as_waksman_topology neighbors = generate_as_waksman_topology(num_packets); @@ -638,10 +644,10 @@ namespace nil { auto it2 = routing[column_idx].find(packet_idx - 1); assert((it != routing[column_idx].end()) ^ (it2 != routing[column_idx].end())); const bool switch_setting = - (it != routing[column_idx].end() ? it->second : it2->second); + (it != routing[column_idx].end() ? it->second : it2->second); routed_packet_idx = (switch_setting ? neighbors[column_idx][packet_idx].second : - neighbors[column_idx][packet_idx].first); + neighbors[column_idx][packet_idx].first); } nextperm.set(routed_packet_idx, curperm.get(packet_idx)); diff --git a/include/nil/crypto3/zk/snark/systems/plonk/pickles/alphas.hpp b/include/nil/crypto3/zk/snark/systems/plonk/pickles/alphas.hpp index 148668c6a..0ecab6dca 100644 --- a/include/nil/crypto3/zk/snark/systems/plonk/pickles/alphas.hpp +++ b/include/nil/crypto3/zk/snark/systems/plonk/pickles/alphas.hpp @@ -38,7 +38,6 @@ namespace nil { namespace zk { namespace snark { enum argument_type; - template struct Alphas { /// The next power of alpha to use @@ -49,16 +48,16 @@ namespace nil { /// The powers of alpha: 1, alpha, alpha^2, etc. /// If set to [Some], you can't register new constraints. std::vector alphas; - std::unordered_map> mapping; + std::unordered_map> mapping; Alphas() : next_power(0) {} // Create alphas from 0 to next_power - 1 - void register_(argument_type arg, int power){ - if(mapping.find(arg) == mapping.end()){ + void register_(argument_type arg, int power) { + if (mapping.find(arg) == mapping.end()) { mapping[arg] = std::make_pair(next_power, power); } - + next_power += power; } @@ -76,7 +75,7 @@ namespace nil { // Return num alphas std::vector get_alphas(argument_type arg, std::size_t num) { - if(mapping.find(arg) == mapping.end()){ + if (mapping.find(arg) == mapping.end()) { assert(false); } std::pair range = mapping[arg]; diff --git a/include/nil/crypto3/zk/snark/systems/plonk/pickles/constants.hpp b/include/nil/crypto3/zk/snark/systems/plonk/pickles/constants.hpp index d9f0f0809..b2516e318 100644 --- a/include/nil/crypto3/zk/snark/systems/plonk/pickles/constants.hpp +++ b/include/nil/crypto3/zk/snark/systems/plonk/pickles/constants.hpp @@ -33,7 +33,6 @@ namespace nil { namespace crypto3 { namespace zk { namespace snark { - struct kimchi_constant { constexpr static const std::size_t CHALLENGE_LENGTH_IN_LIMBS = 2; constexpr static const std::size_t PERMUTES = 7; diff --git a/include/nil/crypto3/zk/snark/systems/plonk/pickles/constraints.hpp b/include/nil/crypto3/zk/snark/systems/plonk/pickles/constraints.hpp index 54024379d..d00f56cff 100644 --- a/include/nil/crypto3/zk/snark/systems/plonk/pickles/constraints.hpp +++ b/include/nil/crypto3/zk/snark/systems/plonk/pickles/constraints.hpp @@ -9,7 +9,7 @@ namespace nil { namespace zk { namespace snark { template - struct ConstraintSystem{ + struct ConstraintSystem { typedef typename FieldType::value_type value_type; // typedef proof_evaluation_type proof_evaluation_type; @@ -17,20 +17,20 @@ namespace nil { constexpr static const std::size_t ZK_ROWS = 3; constexpr static const std::size_t GENERIC_REGISTERS = 3; - static value_type perm_scalars(std::vector>& e, value_type beta, - value_type& gamma, std::vector& alphas, - value_type& zkp_zeta){ + static value_type perm_scalars(std::vector> &e, value_type beta, + value_type &gamma, std::vector &alphas, + value_type &zkp_zeta) { value_type res = e[1].z * beta * alphas.front() * zkp_zeta; - for(int i = 0; i < std::min(e[0].w.size(), e[0].s.size()); ++i){ + for (int i = 0; i < std::min(e[0].w.size(), e[0].s.size()); ++i) { res *= (gamma + (beta * e[0].s[i]) + e[0].w[i]); } return -res; } - static void generic_gate(std::vector& res, const value_type& alpha_pow, - const std::size_t register_offset, const value_type& generic_zeta, - const std::array& w_zeta){ + static void generic_gate(std::vector &res, const value_type &alpha_pow, + const std::size_t register_offset, const value_type &generic_zeta, + const std::array &w_zeta) { value_type alpha_generic = alpha_pow * generic_zeta; // addition @@ -44,9 +44,10 @@ namespace nil { // constant res.push_back(alpha_generic); } - static std::vector gnrc_scalars(const std::vector& alphas, - const std::array& w_zeta, - const value_type& generic_zeta){ + + static std::vector gnrc_scalars(const std::vector &alphas, + const std::array &w_zeta, + const value_type &generic_zeta) { std::vector res; generic_gate(res, alphas[0], 0, generic_zeta, w_zeta); diff --git a/include/nil/crypto3/zk/snark/systems/plonk/pickles/detail.hpp b/include/nil/crypto3/zk/snark/systems/plonk/pickles/detail.hpp index 63f2ca145..f9774e6fd 100644 --- a/include/nil/crypto3/zk/snark/systems/plonk/pickles/detail.hpp +++ b/include/nil/crypto3/zk/snark/systems/plonk/pickles/detail.hpp @@ -32,6 +32,7 @@ #include #include #include + #include #include diff --git a/include/nil/crypto3/zk/snark/systems/plonk/pickles/detail/kimchi_functions.hpp b/include/nil/crypto3/zk/snark/systems/plonk/pickles/detail/kimchi_functions.hpp index ea4eed912..68b97fc43 100644 --- a/include/nil/crypto3/zk/snark/systems/plonk/pickles/detail/kimchi_functions.hpp +++ b/include/nil/crypto3/zk/snark/systems/plonk/pickles/detail/kimchi_functions.hpp @@ -1,21 +1,22 @@ #ifndef CRYPTO3_ZK_KIMCHI_FUNCTIONS #define CRYPTO3_ZK_KIMCHI_FUNCTIONS -namespace nil{ - namespace crypto3{ - namespace zk{ - namespace snark{ - template +namespace nil { + namespace crypto3 { + namespace zk { + namespace snark { + template struct kimchi_functions { typedef typename CurveType::scalar_field_type scalar_field_type; typedef typename CurveType::base_field_type base_field_type; - static typename scalar_field_type::value_type shift_scalar(const typename scalar_field_type::value_type& x){ + static typename scalar_field_type::value_type + shift_scalar(const typename scalar_field_type::value_type &x) { typename scalar_field_type::value_type two = typename scalar_field_type::value_type(2); typename scalar_field_type::value_type two_pow = two.pow(scalar_field_type::modulus_bits); - if(scalar_field_type::modulus < base_field_type::modulus){ + if (scalar_field_type::modulus < base_field_type::modulus) { return (x - (two_pow + scalar_field_type::value_type::one())) / two; - } else{ + } else { return x - two_pow; } } diff --git a/include/nil/crypto3/zk/snark/systems/plonk/pickles/detail/mapping.hpp b/include/nil/crypto3/zk/snark/systems/plonk/pickles/detail/mapping.hpp index b41f3537f..7d8ec3659 100644 --- a/include/nil/crypto3/zk/snark/systems/plonk/pickles/detail/mapping.hpp +++ b/include/nil/crypto3/zk/snark/systems/plonk/pickles/detail/mapping.hpp @@ -3,12 +3,12 @@ #include -namespace nil{ - namespace crypto3{ - namespace zk{ - namespace snark{ - template - struct group_map{ +namespace nil { + namespace crypto3 { + namespace zk { + namespace snark { + template + struct group_map { typedef typename CurveType::scalar_field_type scalar_field_type; typedef typename CurveType::base_field_type base_field_type; typedef typename CurveType::template g1_type group_type; @@ -22,7 +22,7 @@ namespace nil{ value_type sqrt_neg_three_u_squared; value_type inv_three_u_squared; - static value_type curve_eqn(value_type x){ + static value_type curve_eqn(value_type x) { value_type res = x; res *= x; res += a; @@ -33,49 +33,50 @@ namespace nil{ group_map() { u = value_type(1); - while(true){ + while (true) { fu = curve_eqn(u); - if(!fu.is_zero()){ + if (!fu.is_zero()) { break; - } - else{ + } else { ++u; } } - + value_type three_u_squared = value_type(3) * u.squared(); inv_three_u_squared = three_u_squared.inversed(); sqrt_neg_three_u_squared = (-three_u_squared).sqrt(); - sqrt_neg_three_u_squared_minus_u_over_2 = (sqrt_neg_three_u_squared - u) * (value_type(2)).inversed(); + sqrt_neg_three_u_squared_minus_u_over_2 = + (sqrt_neg_three_u_squared - u) * (value_type(2)).inversed(); } - std::array potential_xs_helper(value_type& t2, value_type& alpha){ - value_type x1 = sqrt_neg_three_u_squared_minus_u_over_2 - t2.squared() * alpha * sqrt_neg_three_u_squared; + std::array potential_xs_helper(value_type &t2, value_type &alpha) { + value_type x1 = sqrt_neg_three_u_squared_minus_u_over_2 - + t2.squared() * alpha * sqrt_neg_three_u_squared; value_type x2 = -u - x1; value_type t2_plus_fu = t2 + fu; value_type x3 = u - t2_plus_fu.squared() * alpha * t2_plus_fu * inv_three_u_squared; return std::array({x1, x2, x3}); } - std::array potential_xs(value_type& t){ + std::array potential_xs(value_type &t) { value_type t2 = t.squared(); value_type alpha = ((t2 + fu) * t2).inversed(); return potential_xs_helper(t2, alpha); } - typename group_type::value_type get_xy(value_type& t){ + typename group_type::value_type get_xy(value_type &t) { std::array xvec = potential_xs(t); - for(auto &x : xvec){ + for (auto &x: xvec) { value_type y = curve_eqn(x).sqrt(); - if(y.squared() == x.pow(3) + a * x + b){ + if (y.squared() == x.pow(3) + a * x + b) { return typename group_type::value_type(x, y); } } return typename group_type::value_type(); } - typename group_type::value_type to_group(value_type t){ + typename group_type::value_type to_group(value_type t) { return get_xy(t); } }; @@ -113,13 +114,14 @@ namespace nil{ return a * endo_coeff + b; } - typename FieldType::value_type value(){ + typename FieldType::value_type value() { return _val; } ScalarChallenge(typename FieldType::value_type _val) : _val(_val) {} ScalarChallenge() = default; + typename FieldType::value_type _val; }; } diff --git a/include/nil/crypto3/zk/snark/systems/plonk/pickles/oracles.hpp b/include/nil/crypto3/zk/snark/systems/plonk/pickles/oracles.hpp index ac8ffac68..5e7f38c67 100644 --- a/include/nil/crypto3/zk/snark/systems/plonk/pickles/oracles.hpp +++ b/include/nil/crypto3/zk/snark/systems/plonk/pickles/oracles.hpp @@ -78,7 +78,7 @@ namespace nil { /// zeta^n and (zeta * omega)^n std::array powers_of_eval_points_for_chunks; /// ? - std::vector < std::tuple < commitment_type, std::vector>>> polys; + std::vector>>> polys; /// pre-computed zeta^n typename scalar_field_type::value_type zeta1; /// The evaluation f(zeta) - t(zeta) * Z_H(zeta) @@ -87,45 +87,46 @@ namespace nil { }; template> - std::vector>> prev_chal_evals( - proof_type proof, - VerifierIndexType index, - std::vector evaluation_points, - std::array powers_of_eval_points_for_chunks){ + std::vector>> + prev_chal_evals( + proof_type proof, + VerifierIndexType index, + std::vector evaluation_points, + std::array powers_of_eval_points_for_chunks) { typedef commitments::kimchi_pedersen commitment_scheme; typedef typename CurveType::scalar_field_type scalar_field_type; // Fr typedef typename CurveType::base_field_type base_field_type; // Fq std::vector>> prev_chal_evals; - for(auto &[chals, comm] : proof.prev_challenges){ + for (auto &[chals, comm]: proof.prev_challenges) { std::size_t b_len = 1 << chals.size(); std::vector b; prev_chal_evals.push_back(std::vector>()); - for(int i = 0; i < evaluation_points.size(); ++i){ + for (int i = 0; i < evaluation_points.size(); ++i) { // prev_chal_evals.back().push_back(std::vector()); - typename scalar_field_type::value_type full = commitment_scheme::b_poly(chals, evaluation_points[i]); - if(index.max_poly_size == b_len){ + typename scalar_field_type::value_type full = commitment_scheme::b_poly(chals, + evaluation_points[i]); + if (index.max_poly_size == b_len) { std::vector vec_full = {full}; prev_chal_evals.back().emplace_back(vec_full); - } - else{ + } else { typename scalar_field_type::value_type betaacc = scalar_field_type::value_type::one(); typename scalar_field_type::value_type diff; - for(std::size_t j = index.max_poly_size; j < b_len; ++j){ + for (std::size_t j = index.max_poly_size; j < b_len; ++j) { typename scalar_field_type::value_type b_j; - if(b.empty()){ + if (b.empty()) { b = commitment_scheme::b_poly_coefficents(chals); } b_j = b[j]; - + diff += betaacc * b[j]; betaacc *= evaluation_points[i]; } std::vector tmp_vec = { - full - (diff * powers_of_eval_points_for_chunks[i]), diff, + full - (diff * powers_of_eval_points_for_chunks[i]), diff, }; prev_chal_evals.back().emplace_back(tmp_vec); @@ -139,8 +140,8 @@ namespace nil { /// This function runs the random oracle argument template> OraclesResult oracles(proof_type proof, - VerifierIndexType index, - typename commitments::kimchi_pedersen::commitment_type p_comm) { + VerifierIndexType index, + typename commitments::kimchi_pedersen::commitment_type p_comm) { typedef commitments::kimchi_pedersen commitment_scheme; typedef typename commitment_scheme::commitment_type commitment_type; typedef typename commitment_scheme::evaluation_type evaluation_type; @@ -160,33 +161,35 @@ namespace nil { fq_sponge.absorb_g(p_comm.unshifted); //~ 3. Absorb the commitments to the registers / witness columns with the Fq-Sponge. - for (auto &commit : proof.commitments.w_comm) { + for (auto &commit: proof.commitments.w_comm) { fq_sponge.absorb_g(commit.unshifted); } std::tuple, - typename CurveType::scalar_field_type::value_type> + typename CurveType::scalar_field_type::value_type> joint_combiner; - if(index.lookup_index_is_used) { - BOOST_ASSERT_MSG(proof.commitments.lookup_is_used, "lookup should be in proof commitments"); + if (index.lookup_index_is_used) { + BOOST_ASSERT_MSG(proof.commitments.lookup_is_used, "lookup should be in proof commitments"); - if(index.lookup_index.runtime_tables_selector_is_used){ - BOOST_ASSERT_MSG(proof.commitments.lookup.runtime_is_used, "lookup runtime should be in proof commitments"); + if (index.lookup_index.runtime_tables_selector_is_used) { + BOOST_ASSERT_MSG(proof.commitments.lookup.runtime_is_used, + "lookup runtime should be in proof commitments"); fq_sponge.absorb_g(proof.commitments.lookup.runtime.unshifted); } ScalarChallenge s; if (index.lookup_index.lookup_used == lookup_verifier_index::lookups_used::Single) { - s = ScalarChallenge(CurveType::scalar_field_type::value_type::zero()); - } - else if (index.lookup_index.lookup_used == lookup_verifier_index::lookups_used::Joint) { + s = ScalarChallenge( + CurveType::scalar_field_type::value_type::zero()); + } else if (index.lookup_index.lookup_used == + lookup_verifier_index::lookups_used::Joint) { s = ScalarChallenge(fq_sponge.challenge()); } joint_combiner = std::make_tuple(s, s.to_field(index.srs.endo_r)); - for(auto &commit : proof.commitments.lookup.sorted){ + for (auto &commit: proof.commitments.lookup.sorted) { fq_sponge.absorb_g(commit.unshifted); } } @@ -209,7 +212,7 @@ namespace nil { // proof.commitments.lookup[i] = fq_sponge.absorb_g(proof.commitments.lookup[i].aggreg.unshifted); // } - if(proof.commitments.lookup_is_used){ + if (proof.commitments.lookup_is_used) { fq_sponge.absorb_g(proof.commitments.lookup.aggreg.unshifted); } @@ -218,19 +221,21 @@ namespace nil { fq_sponge.absorb_g(proof.commitments.z_comm.unshifted); //~ 10. Sample $\alpha'$ with the Fq-Sponge. - ScalarChallenge alpha_chal = ScalarChallenge(fq_sponge.challenge()); + ScalarChallenge alpha_chal = ScalarChallenge( + fq_sponge.challenge()); //~ 11. Derive $\alpha$ from $\alpha'$ using the endomorphism (TODO: details). typename scalar_field_type::value_type alpha = alpha_chal.to_field(index.srs.endo_r); //~ 12. Enforce that the length of the $t$ commitment is of size `PERMUTS`. BOOST_ASSERT_MSG(proof.commitments.t_comm.unshifted.size() == kimchi_constant::PERMUTES, - "IncorrectCommitmentLength(t)"); + "IncorrectCommitmentLength(t)"); //~ 13. Absorb the commitment to the quotient polynomial $t$ into the argument. fq_sponge.absorb_g(proof.commitments.t_comm.unshifted); //~ 14. Sample $\zeta'$ with the Fq-Sponge. - ScalarChallenge zeta_chal = ScalarChallenge(fq_sponge.challenge()); + ScalarChallenge zeta_chal = ScalarChallenge( + fq_sponge.challenge()); //~ 15. Derive $\zeta$ from $\zeta'$ using the endomorphism (TODO: specify). typename scalar_field_type::value_type zeta = zeta_chal.to_field(index.srs.endo_r); @@ -251,21 +256,21 @@ namespace nil { std::vector w; w.reserve(proof.public_input.size()); - if(proof.public_input.size() > 0){ + if (proof.public_input.size() > 0) { w.push_back(scalar_field_type::value_type::one()); } Alphas all_alphas = index.powers_of_alpha; - + all_alphas.instantiate(alpha); - - for(int i = 0; i < proof.public_input.size(); ++i){ + + for (int i = 0; i < proof.public_input.size(); ++i) { w.push_back(w.back() * index.domain.omega); } // compute Lagrange base evaluation denominators std::vector zeta_minus_x; - for (auto &i : w) { + for (auto &i: w) { zeta_minus_x.push_back(zeta - i); } @@ -278,7 +283,7 @@ namespace nil { // ark_ff::fields::batch_inversion::>(&mut zeta_minus_x); // zeta_minus_x = zeta_minus_x.inverse() * Fr::one(); - std::transform(zeta_minus_x.begin(), zeta_minus_x.end(), zeta_minus_x.begin(), [](auto &element){ + std::transform(zeta_minus_x.begin(), zeta_minus_x.end(), zeta_minus_x.begin(), [](auto &element) { return element.inversed(); }); //~ 18. Evaluate the negated public polynomial (if present) at $\zeta$ and $\zeta\omega$. @@ -288,16 +293,17 @@ namespace nil { if (!proof.public_input.empty()) { typename scalar_field_type::value_type tmp; std::size_t iter_size = std::min({proof.public_input.size(), zeta_minus_x.size(), w.size()}); - - for(int i = 0; i < iter_size; ++i){ + + for (int i = 0; i < iter_size; ++i) { tmp -= proof.public_input[i] * zeta_minus_x[i] * w[i]; } - typename scalar_field_type::value_type size_inv = typename scalar_field_type::value_type(index.domain.size()).inversed(); + typename scalar_field_type::value_type size_inv = typename scalar_field_type::value_type( + index.domain.size()).inversed(); p_eval[0].push_back(tmp * (zeta1 - scalar_field_type::value_type::one()) * size_inv); p_eval[1].push_back(tmp * (zetaw.pow(n) - scalar_field_type::value_type::one()) * size_inv); - } else{ + } else { p_eval.resize(2); } @@ -330,8 +336,8 @@ namespace nil { //~ 25. Create a list of all polynomials that have an evaluation proof. std::vector evaluation_points = {zeta, zetaw}; std::array powers_of_eval_points_for_chunks = { - zeta.pow(index.max_poly_size), - zetaw.pow(index.max_poly_size) + zeta.pow(index.max_poly_size), + zetaw.pow(index.max_poly_size) }; // let polys : Vec<(PolyComm, _)> = @@ -343,37 +349,38 @@ namespace nil { std::vector>>> polys; std::vector>> prev_chal_evals_vec = prev_chal_evals( - proof, index, evaluation_points, powers_of_eval_points_for_chunks + proof, index, evaluation_points, powers_of_eval_points_for_chunks ); - for(int i = 0; i < proof.prev_challenges.size(); ++i){ + for (int i = 0; i < proof.prev_challenges.size(); ++i) { polys.emplace_back(std::get<1>(proof.prev_challenges[i]), prev_chal_evals_vec[i]); } std::vector> evals = { - proof.evals[0].combine(powers_of_eval_points_for_chunks[0]), - proof.evals[1].combine(powers_of_eval_points_for_chunks[1]) + proof.evals[0].combine(powers_of_eval_points_for_chunks[0]), + proof.evals[1].combine(powers_of_eval_points_for_chunks[1]) }; //~ 26. Compute the evaluation of $ft(\zeta)$. typename scalar_field_type::value_type zkp = index.zkpm.evaluate(zeta); typename scalar_field_type::value_type zeta1m1 = zeta1 - scalar_field_type::value_type::one(); - std::vector alpha_powers = all_alphas.get_alphas(argument_type::Permutation, kimchi_constant::CONSTRAINTS); + std::vector alpha_powers = all_alphas.get_alphas( + argument_type::Permutation, kimchi_constant::CONSTRAINTS); typename scalar_field_type::value_type alpha0 = alpha_powers[0]; typename scalar_field_type::value_type alpha1 = alpha_powers[1]; typename scalar_field_type::value_type alpha2 = alpha_powers[2]; - typename scalar_field_type::value_type ft_eval0 = (evals[0].w[kimchi_constant::PERMUTES - 1] + gamma) * evals[1].z * alpha0 * zkp; + typename scalar_field_type::value_type ft_eval0 = + (evals[0].w[kimchi_constant::PERMUTES - 1] + gamma) * evals[1].z * alpha0 * zkp; for (size_t i = 0; i < evals[0].s.size(); ++i) { ft_eval0 *= (beta * evals[0].s[i]) + evals[0].w[i] + gamma; } if (!p_eval.empty() && !p_eval[0].empty()) { ft_eval0 -= p_eval[0][0]; - } - else { // ?????????????? + } else { // ?????????????? ft_eval0 -= scalar_field_type::value_type::zero(); } @@ -384,35 +391,42 @@ namespace nil { ft_eval0 -= tmp; - typename scalar_field_type::value_type numerator = ((zeta1m1 * alpha1 * (zeta - index.w)) + - (zeta1m1 * alpha2 * (zeta - scalar_field_type::value_type::one()))) * (scalar_field_type::value_type::one() - evals[0].z); + typename scalar_field_type::value_type numerator = ((zeta1m1 * alpha1 * (zeta - index.w)) + + (zeta1m1 * alpha2 * (zeta - + scalar_field_type::value_type::one()))) * + (scalar_field_type::value_type::one() - + evals[0].z); - typename scalar_field_type::value_type denominator = (zeta - index.w) * (zeta - scalar_field_type::value_type::one()); + typename scalar_field_type::value_type denominator = + (zeta - index.w) * (zeta - scalar_field_type::value_type::one()); denominator = denominator.inversed(); ft_eval0 += numerator * denominator; - Constants cs{alpha, beta, gamma, std::get<1>(joint_combiner), index.endo, index.fr_sponge_params.mds}; + Constants cs{alpha, beta, gamma, std::get<1>(joint_combiner), index.endo, + index.fr_sponge_params.mds}; ft_eval0 -= - PolishToken::evaluate(index.linearization.constant_term, index.domain, zeta, evals, cs); + PolishToken::evaluate(index.linearization.constant_term, index.domain, + zeta, evals, cs); std::vector> es; - for(auto &poly : polys){ + for (auto &poly: polys) { evaluation_type eval(commitment_type(), std::get<1>(poly), -1); es.emplace_back(eval, -1); } es.emplace_back(evaluation_type(commitment_type(), p_eval, -1), -1); - std::vector> ft_eval = {{ft_eval0}, {proof.ft_eval1}}; + std::vector> ft_eval = {{ft_eval0}, + {proof.ft_eval1}}; es.emplace_back(evaluation_type(commitment_type(), ft_eval, -1), -1); std::vector> z; std::vector> generic_selector; std::vector> poseidon_selector; - for(auto &eval : proof.evals){ + for (auto &eval: proof.evals) { z.push_back(eval.z); generic_selector.push_back(eval.generic_selector); poseidon_selector.push_back(eval.poseidon_selector); @@ -421,32 +435,34 @@ namespace nil { es.emplace_back(evaluation_type(commitment_type(), generic_selector, -1), -1); es.emplace_back(evaluation_type(commitment_type(), poseidon_selector, -1), -1); - for(int i = 0; i < proof.evals[0].w.size(); ++i){ - std::vector> w_copy = {proof.evals[0].w[i], proof.evals[1].w[i]}; + for (int i = 0; i < proof.evals[0].w.size(); ++i) { + std::vector> w_copy = {proof.evals[0].w[i], + proof.evals[1].w[i]}; es.emplace_back(evaluation_type(commitment_type(), w_copy, -1), -1); } - for(int i = 0; i < proof.evals[0].s.size(); ++i){ - std::vector> s_copy = {proof.evals[0].s[i], proof.evals[1].s[i]}; + for (int i = 0; i < proof.evals[0].s.size(); ++i) { + std::vector> s_copy = {proof.evals[0].s[i], + proof.evals[1].s[i]}; es.emplace_back(evaluation_type(commitment_type(), s_copy, -1), -1); } typename scalar_field_type::value_type combined_inner_product0 = commitment_scheme::combined_inner_product( - evaluation_points, - v, - u, - es, - index.srs.g.size() + evaluation_points, + v, + u, + es, + index.srs.g.size() ); RandomOracles oracles = { - joint_combiner, beta, gamma, alpha_chal, alpha, zeta, v, u, zeta_chal, v_chal, u_chal + joint_combiner, beta, gamma, alpha_chal, alpha, zeta, v, u, zeta_chal, v_chal, u_chal }; - return OraclesResult{fq_sponge, digest, oracles, - all_alphas, p_eval, powers_of_eval_points_for_chunks, - polys, zeta1, ft_eval0, combined_inner_product0}; + return OraclesResult{fq_sponge, digest, oracles, + all_alphas, p_eval, powers_of_eval_points_for_chunks, + polys, zeta1, ft_eval0, combined_inner_product0}; } } // namespace snark } // namespace zk diff --git a/include/nil/crypto3/zk/snark/systems/plonk/pickles/permutation.hpp b/include/nil/crypto3/zk/snark/systems/plonk/pickles/permutation.hpp index ba7450441..fb5878729 100644 --- a/include/nil/crypto3/zk/snark/systems/plonk/pickles/permutation.hpp +++ b/include/nil/crypto3/zk/snark/systems/plonk/pickles/permutation.hpp @@ -8,9 +8,11 @@ namespace nil { namespace zk { namespace snark { template - typename FieldType::value_type eval_vanishes_on_last_4_rows(math::basic_radix2_domain& domain, - typename FieldType::value_type& x){ - typename FieldType::value_type w4 = domain.get_domain_element(domain.size() - (kimchi_constant::ZK_ROWS + 1)); + typename FieldType::value_type + eval_vanishes_on_last_4_rows(math::basic_radix2_domain &domain, + typename FieldType::value_type &x) { + typename FieldType::value_type w4 = domain.get_domain_element( + domain.size() - (kimchi_constant::ZK_ROWS + 1)); typename FieldType::value_type w3 = domain.omega * w4; typename FieldType::value_type w2 = domain.omega * w3; typename FieldType::value_type w1 = domain.omega * w2; @@ -20,6 +22,6 @@ namespace nil { } // namespace components } // namespace zk } // namespace crypto3 -} +} #endif diff --git a/include/nil/crypto3/zk/snark/systems/plonk/pickles/proof.hpp b/include/nil/crypto3/zk/snark/systems/plonk/pickles/proof.hpp index 47a0be668..07fbe1c08 100644 --- a/include/nil/crypto3/zk/snark/systems/plonk/pickles/proof.hpp +++ b/include/nil/crypto3/zk/snark/systems/plonk/pickles/proof.hpp @@ -46,9 +46,10 @@ namespace nil { value_type runtime; bool runtime_is_used; - lookup_evaluation_type(std::vector& sorted, value_type& aggreg, - value_type& table, value_type& runtime) : sorted(sorted), - aggreg(aggreg), table(table), runtime(runtime) {}; + lookup_evaluation_type(std::vector &sorted, value_type &aggreg, + value_type &table, value_type &runtime) : sorted(sorted), + aggreg(aggreg), table(table), + runtime(runtime) {}; lookup_evaluation_type() = default; }; @@ -66,34 +67,35 @@ namespace nil { value_type generic_selector; value_type poseidon_selector; - base_proof_evaluation_type(std::array& w, - value_type& z, std::array& s, - lookup_evaluation_type &lookup, - value_type& generic_selector, value_type& poseidon_selector) : - w(w), z(z), s(s), lookup(lookup), generic_selector(generic_selector), - poseidon_selector(poseidon_selector) {} + base_proof_evaluation_type(std::array &w, + value_type &z, std::array &s, + lookup_evaluation_type &lookup, + value_type &generic_selector, value_type &poseidon_selector) : + w(w), z(z), s(s), lookup(lookup), generic_selector(generic_selector), + poseidon_selector(poseidon_selector) {} base_proof_evaluation_type() = default; }; - template + template struct proof_evaluation_type : base_proof_evaluation_type { using base_proof_evaluation_type::base_proof_evaluation_type; }; - template - struct proof_evaluation_type> : base_proof_evaluation_type>{ + template + struct proof_evaluation_type> + : base_proof_evaluation_type> { using base_proof_evaluation_type>::base_proof_evaluation_type; - - proof_evaluation_type combine(value_type& pt){ + + proof_evaluation_type combine(value_type &pt) { std::array s_combined; - for(int i = 0; i < s_combined.size(); ++i){ + for (int i = 0; i < s_combined.size(); ++i) { math::polynomial temp_polynomial(this->s[i].begin(), this->s[i].end()); s_combined[i] = temp_polynomial.evaluate(pt); } std::array w_combined; - for(int i = 0; i < w_combined.size(); ++i){ + for (int i = 0; i < w_combined.size(); ++i) { math::polynomial temp_polynomial(this->w[i].begin(), this->w[i].end()); w_combined[i] = temp_polynomial.evaluate(pt); } @@ -101,39 +103,44 @@ namespace nil { math::polynomial temp_polynomial_z(this->z.begin(), this->z.end()); value_type z_combined = temp_polynomial_z.evaluate(pt); - math::polynomial temp_polynomial_gs(this->generic_selector.begin(), this->generic_selector.end()); + math::polynomial temp_polynomial_gs(this->generic_selector.begin(), + this->generic_selector.end()); value_type generic_selector_combined = temp_polynomial_gs.evaluate(pt); - math::polynomial temp_polynomial_ps(this->poseidon_selector.begin(), this->poseidon_selector.end()); + math::polynomial temp_polynomial_ps(this->poseidon_selector.begin(), + this->poseidon_selector.end()); value_type poseidon_selector_combined = temp_polynomial_ps.evaluate(pt); lookup_evaluation_type lookup_combined; - if(this->lookup_is_used){ + if (this->lookup_is_used) { lookup_combined = lookup_evaluation_type(); - math::polynomial temp_polynomial_table(this->lookup.table.begin(), this->lookup.table.end()); + math::polynomial temp_polynomial_table(this->lookup.table.begin(), + this->lookup.table.end()); lookup_combined.table = temp_polynomial_table.evaluate(pt); - math::polynomial temp_polynomial_aggreg(this->lookup.aggreg.begin(), this->lookup.aggreg.end()); + math::polynomial temp_polynomial_aggreg(this->lookup.aggreg.begin(), + this->lookup.aggreg.end()); lookup_combined.aggreg = temp_polynomial_aggreg.evaluate(pt); - for(int i = 0; i < this->lookup.sorted.size(); ++i){ - math::polynomial temp_polynomial_sorted(this->lookup.sorted[i].begin(), this->lookup.sorted[i].end()); + for (int i = 0; i < this->lookup.sorted.size(); ++i) { + math::polynomial temp_polynomial_sorted(this->lookup.sorted[i].begin(), + this->lookup.sorted[i].end()); lookup_combined.sorted[i] = temp_polynomial_sorted.evaluate(pt); } - - if(this->lookup.runtime_is_used){ + + if (this->lookup.runtime_is_used) { math::polynomial temp_polynomial_runtime(this->lookup.runtime.begin(), - this->lookup.runtime.end()); + this->lookup.runtime.end()); lookup_combined.runtime = temp_polynomial_runtime.evaluate(pt); } } - return proof_evaluation_type(w_combined, z_combined, s_combined, lookup_combined, - generic_selector_combined, poseidon_selector_combined); + return proof_evaluation_type(w_combined, z_combined, s_combined, lookup_combined, + generic_selector_combined, poseidon_selector_combined); } }; - + template struct lookup_commitment_type { typedef commitments::kimchi_pedersen commitment_scheme; @@ -144,7 +151,7 @@ namespace nil { commitment_type runtime; bool runtime_is_used; }; - + template struct proof_commitment_type { typedef commitments::kimchi_pedersen commitment_scheme; @@ -160,7 +167,8 @@ namespace nil { bool lookup_is_used; }; - template + template class proof_type { typedef commitments::kimchi_pedersen commitment_scheme; typedef typename commitments::kimchi_pedersen::commitment_type commitment_type; @@ -179,12 +187,8 @@ namespace nil { // public std::vector public_input; // Previous challenges - std::vector< - std::pair< - std::vector, - commitment_type - > - > prev_challenges; + std::vector, + commitment_type>> prev_challenges; }; } // namespace snark } // namespace zk diff --git a/include/nil/crypto3/zk/snark/systems/plonk/pickles/verifier.hpp b/include/nil/crypto3/zk/snark/systems/plonk/pickles/verifier.hpp index 0f558b1cc..29e05752f 100644 --- a/include/nil/crypto3/zk/snark/systems/plonk/pickles/verifier.hpp +++ b/include/nil/crypto3/zk/snark/systems/plonk/pickles/verifier.hpp @@ -49,7 +49,7 @@ namespace nil { namespace snark { template> - struct verifier{ + struct verifier { typedef commitments::kimchi_pedersen commitment_scheme; typedef typename commitment_scheme::commitment_type commitment_type; typedef typename commitment_scheme::evaluation_type evaluation_type; @@ -75,7 +75,9 @@ namespace nil { //~ //~ 1. Commit to the negated public input polynomial. - BOOST_ASSERT_MSG(index.srs.lagrange_bases.find(index.domain.size()) != index.srs.lagrange_bases.end(), "pre-computed committed lagrange bases not found"); + BOOST_ASSERT_MSG( + index.srs.lagrange_bases.find(index.domain.size()) != index.srs.lagrange_bases.end(), + "pre-computed committed lagrange bases not found"); std::vector lgr_comm = index.srs.lagrange_bases[index.domain.size()]; // calculate lgr_comm BOOST_ASSERT(lgr_comm.size() == 512); // ?? std::vector com; @@ -87,14 +89,15 @@ namespace nil { } // std::vector *com_ref = &com; std::vector elm; - for (auto &i : proof.public_input) { + for (auto &i: proof.public_input) { elm.push_back(-i); } commitment_type p_comm = commitment_type::multi_scalar_mul(com, elm); //~ 2. Run the [Fiat-Shamir argument](#fiat-shamir-argument). - OraclesResult oracles_res = oracles(proof, index, p_comm); + OraclesResult oracles_res = oracles( + proof, index, p_comm); // fq_sponge, // oracles, // all_alphas, @@ -112,82 +115,84 @@ namespace nil { // Calculate polynoms in pointers powers_of_eval_points_for_chunks[0] and // powers_of_eval_points_for_chunks[1] std::vector> evals = { - proof.evals[0].combine(oracles_res.powers_of_eval_points_for_chunks[0]), - proof.evals[1].combine(oracles_res.powers_of_eval_points_for_chunks[1]) + proof.evals[0].combine(oracles_res.powers_of_eval_points_for_chunks[0]), + proof.evals[1].combine(oracles_res.powers_of_eval_points_for_chunks[1]) }; //~ 4. Compute the commitment to the linearized polynomial $f$. // permutation typename scalar_field_type::value_type zkp = index.zkpm.evaluate(oracles_res.oracles.zeta); - std::vector alphas = oracles_res.all_alphas.get_alphas(argument_type::Permutation, kimchi_constant::CONSTRAINTS); + std::vector alphas = oracles_res.all_alphas.get_alphas( + argument_type::Permutation, kimchi_constant::CONSTRAINTS); std::vector commitments = {index.sigma_comm[PERMUTES - 1]}; - std::vector scalars = {ConstraintSystem::perm_scalars(evals, oracles_res.oracles.beta, - oracles_res.oracles.gamma, alphas, zkp)}; + std::vector scalars = { + ConstraintSystem::perm_scalars(evals, oracles_res.oracles.beta, + oracles_res.oracles.gamma, alphas, + zkp)}; // generic - + std::vector generic_scalars = - ConstraintSystem::gnrc_scalars(alphas, evals[0].w, evals[0].generic_selector); + ConstraintSystem::gnrc_scalars(alphas, evals[0].w, + evals[0].generic_selector); std::vector generic_com( - index.coefficients_comm.begin(), index.coefficients_comm.begin() + generic_scalars.size()); + index.coefficients_comm.begin(), + index.coefficients_comm.begin() + generic_scalars.size()); BOOST_ASSERT(generic_scalars.size() == generic_com.size()); scalars.insert(scalars.end(), generic_scalars.begin(), generic_scalars.end()); commitments.insert(commitments.end(), generic_com.begin(), generic_com.end()); - + // other gates are implemented using the expression framework { // TODO: Reuse constants from oracles function Constants constants = { - oracles_res.oracles.alpha, - oracles_res.oracles.beta, - oracles_res.oracles.gamma, - std::get<1>(oracles_res.oracles.joint_combiner), - index.endo, - index.fr_sponge_params.mds - }; - - for (auto i : index.linearization.index_term) { + oracles_res.oracles.alpha, + oracles_res.oracles.beta, + oracles_res.oracles.gamma, + std::get<1>(oracles_res.oracles.joint_combiner), + index.endo, + index.fr_sponge_params.mds + }; + + for (auto i: index.linearization.index_term) { auto col = std::get<0>(i); auto tokens = std::get<1>(i); auto scalar = - PolishToken::evaluate(tokens, index.domain, oracles_res.oracles.zeta, evals, constants); + PolishToken::evaluate(tokens, index.domain, + oracles_res.oracles.zeta, evals, + constants); auto l = proof.commitments.lookup; if (col.column == column_type::Witness) { scalars.push_back(scalar); commitments.push_back(proof.commitments.w_comm[col.witness_value]); - } - else if (col.column == column_type::Coefficient) { + } else if (col.column == column_type::Coefficient) { scalars.push_back(scalar); commitments.push_back(index.coefficients_comm[col.coefficient_value]); - } - else if (col.column == column_type::Z) { + } else if (col.column == column_type::Z) { scalars.push_back(scalar); commitments.push_back(proof.commitments.z_comm); - } - else if (col.column == column_type::LookupSorted) { + } else if (col.column == column_type::LookupSorted) { scalars.push_back(scalar); commitments.push_back(l.sorted[col.lookup_sorted_value]); - } - else if (col.column == column_type::LookupAggreg) { + } else if (col.column == column_type::LookupAggreg) { scalars.push_back(scalar); commitments.push_back(l.aggreg); - } - else if (col.column == column_type::LookupKindIndex) { + } else if (col.column == column_type::LookupKindIndex) { if (index.lookup_index_is_used) { // assert("Attempted to use, but no lookup index was given"); } else { scalars.push_back(scalar); - commitments.push_back(index.lookup_index.lookup_selectors[col.lookup_kind_index_value]); + commitments.push_back( + index.lookup_index.lookup_selectors[col.lookup_kind_index_value]); } - } - else if (col.column == column_type::LookupTable) { + } else if (col.column == column_type::LookupTable) { if (index.lookup_index_is_used) { // assert("Attempted to use, but no lookup index was given"); } else { @@ -200,46 +205,35 @@ namespace nil { commitments.push_back(index.lookup_index.lookup_table[k]); } } - } - else if (col.column == column_type::Index) { + } else if (col.column == column_type::Index) { commitment_type c; - if(col.index_value == gate_type::Zero || col.index_value == gate_type::Generic || col.index_value == gate_type::Lookup){ + if (col.index_value == gate_type::Zero || col.index_value == gate_type::Generic || + col.index_value == gate_type::Lookup) { std::cout << "Selector for {:?} not defined\n"; - } - else if(col.index_value == gate_type::CompleteAdd){ + } else if (col.index_value == gate_type::CompleteAdd) { c = index.complete_add_comm; - } - else if(col.index_value == gate_type::VarBaseMul){ + } else if (col.index_value == gate_type::VarBaseMul) { c = index.mul_comm; - } - else if(col.index_value == gate_type::EndoMul){ + } else if (col.index_value == gate_type::EndoMul) { c = index.emul_comm; - } - else if(col.index_value == gate_type::EndoMulScalar){ + } else if (col.index_value == gate_type::EndoMulScalar) { c = index.endomul_scalar_comm; - } - else if(col.index_value == gate_type::Poseidon){ + } else if (col.index_value == gate_type::Poseidon) { c = index.psm_comm; - } - else if(col.index_value == gate_type::ChaCha0){ + } else if (col.index_value == gate_type::ChaCha0) { c = index.chacha_comm[0]; - } - else if(col.index_value == gate_type::ChaCha1){ + } else if (col.index_value == gate_type::ChaCha1) { c = index.chacha_comm[1]; - } - else if(col.index_value == gate_type::ChaCha2){ + } else if (col.index_value == gate_type::ChaCha2) { c = index.chacha_comm[2]; - } - else if(col.index_value == gate_type::ChaChaFinal){ + } else if (col.index_value == gate_type::ChaChaFinal) { c = index.chacha_comm[3]; - } - else if(col.index_value == gate_type::RangeCheck0){ + } else if (col.index_value == gate_type::RangeCheck0) { c = index.range_check_comm[0]; - } - else if(col.index_value == gate_type::RangeCheck1){ + } else if (col.index_value == gate_type::RangeCheck1) { c = index.range_check_comm[1]; } - + scalars.push_back(scalar); commitments.push_back(c); } @@ -249,19 +243,21 @@ namespace nil { // MSM commitment_type f_comm = commitment_type::multi_scalar_mul(commitments, scalars); - //~ 5. Compute the (chuncked) commitment of $ft$ - //~ (see [Maller's optimization](../crypto/plonk/maller_15.html)). - typename scalar_field_type::value_type zeta_to_srs_len = oracles_res.oracles.zeta.pow(index.max_poly_size); + //~ 5. Compute the (chuncked) commitment of $ft$ + //~ (see [Maller's optimization](../crypto/plonk/maller_15.html)). + typename scalar_field_type::value_type zeta_to_srs_len = oracles_res.oracles.zeta.pow( + index.max_poly_size); commitment_type chunked_f_comm = f_comm.chunk_commitment(zeta_to_srs_len); commitment_type chunked_t_comm = proof.commitments.t_comm.chunk_commitment(zeta_to_srs_len); - commitment_type ft_comm = chunked_f_comm - chunked_t_comm.scale(oracles_res.zeta1 - scalar_field_type::value_type::one()); + commitment_type ft_comm = chunked_f_comm - chunked_t_comm.scale( + oracles_res.zeta1 - scalar_field_type::value_type::one()); //~ 6. List the polynomial commitments, and their associated evaluations, //~ that are associated to the aggregated evaluation proof in the proof: std::vector evaluations; //~ - recursion - for (auto i : oracles_res.polys) { + for (auto i: oracles_res.polys) { evaluations.emplace_back(std::get<0>(i), std::get<1>(i), -1); } @@ -269,108 +265,117 @@ namespace nil { evaluations.emplace_back(p_comm, oracles_res.p_eval, -1); //~ - ft commitment (chunks of it) - std::vector> ft_comm_evals = {{oracles_res.ft_eval0}, {proof.ft_eval1}}; + std::vector> ft_comm_evals = {{oracles_res.ft_eval0}, + {proof.ft_eval1}}; evaluations.emplace_back(ft_comm, ft_comm_evals, -1); //~ - permutation commitment std::vector> tmp_evals; - for (auto &i : proof.evals) { + for (auto &i: proof.evals) { tmp_evals.push_back(i.z); } evaluations.emplace_back(proof.commitments.z_comm, tmp_evals, -1); //~ - index commitments that use the coefficients tmp_evals.clear(); - for (auto i : proof.evals) { + for (auto i: proof.evals) { tmp_evals.push_back(i.generic_selector); } evaluations.emplace_back(index.generic_comm, tmp_evals, -1); tmp_evals.clear(); - for (auto i : proof.evals) { + for (auto i: proof.evals) { tmp_evals.push_back(i.poseidon_selector); } evaluations.emplace_back(index.psm_comm, tmp_evals, -1); //~ - witness commitments for (size_t i = 0; i < COLUMNS; ++i) { - std::vector> witness_comm_evals = {proof.evals[0].w[i], proof.evals[1].w[i]}; + std::vector> witness_comm_evals = { + proof.evals[0].w[i], proof.evals[1].w[i]}; evaluations.emplace_back(proof.commitments.w_comm[i], witness_comm_evals, -1); } //~ - sigma commitments for (size_t i = 0; i < PERMUTES - 1; ++i) { - std::vector> sigma_comm_evals = {proof.evals[0].s[i], proof.evals[1].s[i]}; + std::vector> sigma_comm_evals = { + proof.evals[0].s[i], proof.evals[1].s[i]}; evaluations.emplace_back(index.sigma_comm[i], sigma_comm_evals, -1); } - if(index.lookup_index_is_used){ + if (index.lookup_index_is_used) { std::size_t lookup_len = std::min({ - proof.commitments.lookup.sorted.size(), - proof.evals[0].lookup.sorted.size(), - proof.evals[1].lookup.sorted.size(), - }); - - for(int i = 0; i < lookup_len; ++i){ - std::vector> lookup_sorted_comm_evals = {proof.evals[0].lookup.sorted[i], proof.evals[1].lookup.sorted[i]}; + proof.commitments.lookup.sorted.size(), + proof.evals[0].lookup.sorted.size(), + proof.evals[1].lookup.sorted.size(), + }); + + for (int i = 0; i < lookup_len; ++i) { + std::vector> lookup_sorted_comm_evals = { + proof.evals[0].lookup.sorted[i], proof.evals[1].lookup.sorted[i]}; evaluations.emplace_back( - proof.commitments.lookup.sorted[i], - lookup_sorted_comm_evals, - -1 + proof.commitments.lookup.sorted[i], + lookup_sorted_comm_evals, + -1 ); } - std::vector> lookup_aggreg_comm_evals = {proof.evals[0].lookup.aggreg, proof.evals[1].lookup.aggreg}; + std::vector> lookup_aggreg_comm_evals = { + proof.evals[0].lookup.aggreg, proof.evals[1].lookup.aggreg}; evaluations.emplace_back( - proof.commitments.lookup.aggreg, - lookup_aggreg_comm_evals, - -1 + proof.commitments.lookup.aggreg, + lookup_aggreg_comm_evals, + -1 ); commitment_type table_comm = lookup_verifier_index::combine_table( - index.lookup_index.lookup_table, - std::get<1>(oracles_res.oracles.joint_combiner), - std::get<1>(oracles_res.oracles.joint_combiner).pow(index.lookup_index.max_joint_size), - index.lookup_index.table_ids, - proof.commitments.lookup.runtime + index.lookup_index.lookup_table, + std::get<1>(oracles_res.oracles.joint_combiner), + std::get<1>(oracles_res.oracles.joint_combiner).pow( + index.lookup_index.max_joint_size), + index.lookup_index.table_ids, + proof.commitments.lookup.runtime ); - std::vector> lookup_table_comm_evals = {proof.evals[0].lookup.table, proof.evals[1].lookup.table}; + std::vector> lookup_table_comm_evals = { + proof.evals[0].lookup.table, proof.evals[1].lookup.table}; evaluations.emplace_back( - table_comm, - lookup_table_comm_evals, - -1 + table_comm, + lookup_table_comm_evals, + -1 ); - if(index.lookup_index.runtime_tables_selector_is_used){ - std::vector> lookup_runtime_comm_evals = {proof.evals[0].lookup.runtime, proof.evals[1].lookup.runtime}; + if (index.lookup_index.runtime_tables_selector_is_used) { + std::vector> lookup_runtime_comm_evals = { + proof.evals[0].lookup.runtime, proof.evals[1].lookup.runtime}; evaluations.emplace_back( - index.lookup_index.runtime_tables_selector, - lookup_runtime_comm_evals, - -1 + index.lookup_index.runtime_tables_selector, + lookup_runtime_comm_evals, + -1 ); } } // prepare for the opening proof verification - std::vector evaluation_points = {oracles_res.oracles.zeta, - oracles_res.oracles.zeta * index.domain.omega}; + std::vector evaluation_points = { + oracles_res.oracles.zeta, + oracles_res.oracles.zeta * index.domain.omega}; return batchproof_type({ - oracles_res.fq_sponge, - evaluations, - evaluation_points, - oracles_res.oracles.v, - oracles_res.oracles.u, - proof.proof - }); + oracles_res.fq_sponge, + evaluations, + evaluation_points, + oracles_res.oracles.v, + oracles_res.oracles.u, + proof.proof + }); } - - static bool batch_verify(group_map& g_map, - proofs_type& proofs){ + + static bool batch_verify(group_map &g_map, + proofs_type &proofs) { std::vector batch; - + typename commitment_scheme::params_type &srs = std::get<0>(proofs.front()).srs; - for(auto &[index, proof] : proofs){ + for (auto &[index, proof]: proofs) { batch.push_back(to_batch(index, proof)); } @@ -378,8 +383,8 @@ namespace nil { } static bool verify(group_map &g_map, - VerifierIndexType &index, - proof_type &proof){ + VerifierIndexType &index, + proof_type &proof) { proofs_type proofs; proofs.emplace_back(index, proof); diff --git a/include/nil/crypto3/zk/snark/systems/plonk/pickles/verifier_index.hpp b/include/nil/crypto3/zk/snark/systems/plonk/pickles/verifier_index.hpp index 0cacae357..8c6fe2d43 100644 --- a/include/nil/crypto3/zk/snark/systems/plonk/pickles/verifier_index.hpp +++ b/include/nil/crypto3/zk/snark/systems/plonk/pickles/verifier_index.hpp @@ -51,11 +51,11 @@ namespace nil { // hashes::detail::poseidon_constants_kimchi fr_sponge_params; // hashes::detail::poseidon_constants_kimchi fq_sponge_params; template< - typename CurveType, - typename PoseidonKimchiScalarConstants = hashes::detail::poseidon_constants>, - typename PoseidonKimchiBaseConstants = hashes::detail::poseidon_constants>, - std::size_t WiresAmount = kimchi_constant::COLUMNS, - std::size_t Permuts = kimchi_constant::PERMUTES + typename CurveType, + typename PoseidonKimchiScalarConstants = hashes::detail::poseidon_constants>, + typename PoseidonKimchiBaseConstants = hashes::detail::poseidon_constants>, + std::size_t WiresAmount = kimchi_constant::COLUMNS, + std::size_t Permuts = kimchi_constant::PERMUTES > struct verifier_index { typedef commitments::kimchi_pedersen commitment_scheme; @@ -74,7 +74,7 @@ namespace nil { commitment_type generic_comm; commitment_type psm_comm; - + commitment_type complete_add_comm; commitment_type mul_comm; commitment_type emul_comm; @@ -96,8 +96,8 @@ namespace nil { // linearization; // TODO: // Linearization>>> Alphas powers_of_alpha; - PoseidonKimchiScalarConstants fr_sponge_params; - PoseidonKimchiBaseConstants fq_sponge_params; + PoseidonKimchiScalarConstants fr_sponge_params; + PoseidonKimchiBaseConstants fq_sponge_params; verifier_index() : domain(2) {} }; diff --git a/include/nil/crypto3/zk/snark/systems/plonk/placeholder/detail/placeholder_policy.hpp b/include/nil/crypto3/zk/snark/systems/plonk/placeholder.hpp similarity index 71% rename from include/nil/crypto3/zk/snark/systems/plonk/placeholder/detail/placeholder_policy.hpp rename to include/nil/crypto3/zk/snark/systems/plonk/placeholder.hpp index a3a0e2cea..6900d9ba4 100644 --- a/include/nil/crypto3/zk/snark/systems/plonk/placeholder/detail/placeholder_policy.hpp +++ b/include/nil/crypto3/zk/snark/systems/plonk/placeholder.hpp @@ -44,26 +44,24 @@ namespace nil { namespace crypto3 { namespace zk { namespace snark { - namespace detail { - template - struct placeholder_policy { - /******************************** Params ********************************/ + template + struct placeholder { + /******************************** Params ********************************/ - /** - * Below are various template aliases (used for convenience). - */ + /** + * Below are various template aliases (used for convenience). + */ - typedef plonk_constraint_system constraint_system_type; + typedef plonk_constraint_system constraint_system_type; - typedef FieldType field_type; - typedef PlaceholderParams placeholder_params_type; + typedef FieldType field_type; + typedef PlaceholderParams placeholder_params_type; - typedef plonk_assignment_table variable_assignment_type; + typedef plonk_assignment_table variable_assignment_type; - typedef detail::plonk_evaluation_map> evaluation_map; + typedef detail::plonk_evaluation_map> evaluation_map; - }; - } // namespace detail + }; } // namespace snark } // namespace zk } // namespace crypto3 diff --git a/include/nil/crypto3/zk/snark/systems/plonk/placeholder/gates_argument.hpp b/include/nil/crypto3/zk/snark/systems/plonk/placeholder/gates_argument.hpp index cb46911dd..17df2e1ff 100644 --- a/include/nil/crypto3/zk/snark/systems/plonk/placeholder/gates_argument.hpp +++ b/include/nil/crypto3/zk/snark/systems/plonk/placeholder/gates_argument.hpp @@ -46,7 +46,7 @@ #include #include #include -#include +#include #include #include #include @@ -69,7 +69,7 @@ namespace nil { using variable_type = plonk_variable; using polynomial_dfs_variable_type = plonk_variable; - typedef detail::placeholder_policy policy_type; + typedef placeholder policy_type; constexpr static const std::size_t argument_size = 1; diff --git a/include/nil/crypto3/zk/snark/systems/plonk/placeholder/lookup_argument.hpp b/include/nil/crypto3/zk/snark/systems/plonk/placeholder/lookup_argument.hpp index dc1acca60..5403e5d2b 100644 --- a/include/nil/crypto3/zk/snark/systems/plonk/placeholder/lookup_argument.hpp +++ b/include/nil/crypto3/zk/snark/systems/plonk/placeholder/lookup_argument.hpp @@ -44,7 +44,7 @@ #include #include #include -#include +#include #include #include @@ -52,12 +52,11 @@ namespace nil { namespace crypto3 { namespace zk { namespace snark { - template + template std::vector lookup_parts( - const plonk_constraint_system &constraint_system, - std::size_t max_quotient_chunks - ){ - if( max_quotient_chunks == 0 ){ + const plonk_constraint_system &constraint_system, + std::size_t max_quotient_chunks) { + if (max_quotient_chunks == 0) { return {constraint_system.sorted_lookup_columns_number()}; } @@ -69,17 +68,17 @@ namespace nil { std::size_t lookup_chunk = 0; std::size_t lookup_part = 0; std::size_t max_constraint_degree; - for (const auto& gate :constraint_system.lookup_gates()) { - for (const auto& constr : gate.constraints) { + for (const auto &gate: constraint_system.lookup_gates()) { + for (const auto &constr: gate.constraints) { max_constraint_degree = 0; - for (const auto& li : constr.lookup_input) { + for (const auto &li: constr.lookup_input) { std::size_t deg = lookup_visitor.compute_max_degree(li); max_constraint_degree = std::max( - max_constraint_degree, - deg + max_constraint_degree, + deg ); } - if( lookup_chunk + max_constraint_degree + 1>= max_quotient_chunks ){ + if (lookup_chunk + max_constraint_degree + 1 >= max_quotient_chunks) { lookup_parts.push_back(lookup_part); lookup_chunk = 0; lookup_part = 0; @@ -89,10 +88,10 @@ namespace nil { lookup_part++; } } - for (const auto& table : constraint_system.lookup_tables()) { - for( const auto &lookup_options: table.lookup_options ){ + 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 ){ + if (lookup_chunk + 3 >= max_quotient_chunks) { lookup_parts.push_back(lookup_part); lookup_chunk = 0; lookup_part = 0; @@ -118,7 +117,7 @@ namespace nil { static constexpr std::size_t argument_size = 4; - typedef detail::placeholder_policy policy_type; + typedef placeholder policy_type; public: @@ -129,23 +128,18 @@ namespace nil { placeholder_lookup_argument_prover( const plonk_constraint_system - &constraint_system, + &constraint_system, const typename placeholder_public_preprocessor::preprocessed_data_type - &preprocessed_data, + &preprocessed_data, const plonk_polynomial_dfs_table - &plonk_columns, + &plonk_columns, commitment_scheme_type &commitment_scheme, transcript_type &transcript) - : constraint_system(constraint_system) - , preprocessed_data(preprocessed_data) - , plonk_columns(plonk_columns) - , commitment_scheme(commitment_scheme) - , transcript(transcript) - , basic_domain(preprocessed_data.common_data.basic_domain) - , lookup_gates(constraint_system.lookup_gates()) - , lookup_tables(constraint_system.lookup_tables()) - , lookup_chunks(0) - { + : constraint_system(constraint_system), preprocessed_data(preprocessed_data), + plonk_columns(plonk_columns), commitment_scheme(commitment_scheme), + transcript(transcript), basic_domain(preprocessed_data.common_data.basic_domain), + lookup_gates(constraint_system.lookup_gates()), + lookup_tables(constraint_system.lookup_tables()), lookup_chunks(0) { // $/theta = \challenge$ theta = transcript.template challenge(); } @@ -155,58 +149,60 @@ namespace nil { // Construct lookup gates math::polynomial_dfs one_polynomial( - 0, basic_domain->m, FieldType::value_type::one()); + 0, basic_domain->m, FieldType::value_type::one()); math::polynomial_dfs zero_polynomial( - 0, basic_domain->m, FieldType::value_type::zero()); + 0, basic_domain->m, FieldType::value_type::zero()); math::polynomial_dfs mask_assignment = - one_polynomial - preprocessed_data.q_last - preprocessed_data.q_blind; + one_polynomial - preprocessed_data.q_last - preprocessed_data.q_blind; std::unique_ptr>> lookup_value_ptr = - prepare_lookup_value(mask_assignment); - auto& lookup_value = *lookup_value_ptr; + prepare_lookup_value(mask_assignment); + auto &lookup_value = *lookup_value_ptr; std::unique_ptr>> lookup_input_ptr = - prepare_lookup_input(); - auto& lookup_input = *lookup_input_ptr; + prepare_lookup_input(); + auto &lookup_input = *lookup_input_ptr; // 3. Lookup_input and lookup_value are ready // Now sort them! // Reduce value and input: auto reduced_value_ptr = std::make_unique>>(); - auto& reduced_value = *reduced_value_ptr; + auto &reduced_value = *reduced_value_ptr; - for( std::size_t i = 0; i < lookup_value.size(); i++ ){ + for (std::size_t i = 0; i < lookup_value.size(); i++) { reduced_value.push_back(reduce_dfs_polynomial_domain(lookup_value[i], basic_domain->m)); } auto reduced_input_ptr = std::make_unique>>(); - auto& reduced_input = *reduced_input_ptr; + auto &reduced_input = *reduced_input_ptr; - for( std::size_t i = 0; i < lookup_input.size(); i++ ){ + for (std::size_t i = 0; i < lookup_input.size(); i++) { reduced_input.push_back(reduce_dfs_polynomial_domain(lookup_input[i], basic_domain->m)); } // Sort auto sorted = sort_polynomials(reduced_input, reduced_value, basic_domain->m, - preprocessed_data.common_data.desc.usable_rows_amount); + preprocessed_data.common_data.desc.usable_rows_amount); // 4. Commit sorted polys - for( std::size_t i = 0; i < sorted.size(); i++){ + for (std::size_t i = 0; i < sorted.size(); i++) { commitment_scheme.append_to_batch(LOOKUP_BATCH, sorted[i]); } - typename commitment_scheme_type::commitment_type lookup_commitment = commitment_scheme.commit(LOOKUP_BATCH); + typename commitment_scheme_type::commitment_type lookup_commitment = commitment_scheme.commit( + LOOKUP_BATCH); transcript(lookup_commitment); //5. Compute V_L polynomial. - typename FieldType::value_type beta = transcript.template challenge(); + typename FieldType::value_type beta = transcript.template challenge(); typename FieldType::value_type gamma = transcript.template challenge(); - auto part_sizes = lookup_parts(constraint_system, preprocessed_data.common_data.max_quotient_chunks); + auto part_sizes = lookup_parts(constraint_system, + preprocessed_data.common_data.max_quotient_chunks); std::vector lookup_alphas; - for(std::size_t i = 0; i < part_sizes.size() - 1; i++){ + for (std::size_t i = 0; i < part_sizes.size() - 1; i++) { lookup_alphas.push_back(transcript.template challenge()); } math::polynomial_dfs V_L = compute_V_L( - sorted, reduced_input, reduced_value, beta, gamma); + sorted, reduced_input, reduced_value, beta, gamma); // We don't use reduced_input and reduced_value after this line. reduced_input_ptr.reset(nullptr); @@ -214,33 +210,34 @@ namespace nil { commitment_scheme.append_to_batch(PERMUTATION_BATCH, V_L); - BOOST_CHECK(V_L[preprocessed_data.common_data.desc.usable_rows_amount] == FieldType::value_type::one()); + BOOST_CHECK(V_L[preprocessed_data.common_data.desc.usable_rows_amount] == + FieldType::value_type::one()); BOOST_ASSERT(std::accumulate(part_sizes.begin(), part_sizes.end(), 0) == sorted.size()); // Compute gs and hs products for each part std::vector> gs = compute_gs( - std::move(lookup_input_ptr), std::move(lookup_value_ptr), beta, gamma, part_sizes + std::move(lookup_input_ptr), std::move(lookup_value_ptr), beta, gamma, part_sizes ); std::vector> hs = compute_hs( - sorted, beta, gamma, part_sizes + sorted, beta, gamma, part_sizes ); math::polynomial_dfs V_L_shifted = - math::polynomial_shift(V_L, 1, basic_domain->m); + math::polynomial_shift(V_L, 1, basic_domain->m); std::array, argument_size> F_dfs; F_dfs[0] = preprocessed_data.common_data.lagrange_0 * (one_polynomial - V_L); - F_dfs[1] = preprocessed_data.q_last * ( V_L * V_L - V_L ); + F_dfs[1] = preprocessed_data.q_last * (V_L * V_L - V_L); // Polynomial g is waaay too large, saving memory here, by making code very unreadable. //F_dfs[2] = (one_polynomial - (preprocessed_data.q_last + preprocessed_data.q_blind)) * // (V_L_shifted * h - V_L * g); F_dfs[2] = zero_polynomial; - if( part_sizes.size() == 1 ){ + if (part_sizes.size() == 1) { auto &g = gs[0]; auto &h = hs[0]; g *= V_L; @@ -256,12 +253,13 @@ namespace nil { BOOST_ASSERT(part_sizes.size() == gs.size()); BOOST_ASSERT(part_sizes.size() == hs.size()); BOOST_ASSERT(part_sizes.size() == lookup_alphas.size() + 1); - for( std::size_t i = 0; i < lookup_alphas.size(); i ++ ){ + for (std::size_t i = 0; i < lookup_alphas.size(); i++) { auto &g = gs[i]; auto &h = hs[i]; auto reduced_g = reduce_dfs_polynomial_domain(g, basic_domain->m); auto reduced_h = reduce_dfs_polynomial_domain(h, basic_domain->m); - for( std::size_t j = 0; j < preprocessed_data.common_data.desc.usable_rows_amount; j++){ + for (std::size_t j = 0; + j < preprocessed_data.common_data.desc.usable_rows_amount; j++) { current_poly[j] = (previous_poly[j] * reduced_g[j]) / reduced_h[j]; } commitment_scheme.append_to_batch(PERMUTATION_BATCH, current_poly); @@ -279,8 +277,10 @@ namespace nil { F_dfs[3] = zero_polynomial; for (std::size_t i = 1; i < sorted.size(); i++) { - typename FieldType::value_type alpha = transcript.template challenge(); - math::polynomial_dfs sorted_shifted = math::polynomial_shift(sorted[i-1], preprocessed_data.common_data.desc.usable_rows_amount , basic_domain->m); + typename FieldType::value_type alpha = transcript.template challenge(); + math::polynomial_dfs sorted_shifted = math::polynomial_shift(sorted[i - 1], + preprocessed_data.common_data.desc.usable_rows_amount, + basic_domain->m); F_dfs[3] += alpha * preprocessed_data.common_data.lagrange_0 * (sorted[i] - sorted_shifted); } @@ -292,21 +292,20 @@ namespace nil { }*/ return { - std::move(F_dfs), - std::move(lookup_commitment) + std::move(F_dfs), + std::move(lookup_commitment) }; } std::vector> compute_gs( std::unique_ptr>> lookup_input_ptr, std::unique_ptr>> lookup_value_ptr, - const typename FieldType::value_type& beta, - const typename FieldType::value_type& gamma, - std::vector lookup_part_sizes - ) { + const typename FieldType::value_type &beta, + const typename FieldType::value_type &gamma, + std::vector lookup_part_sizes) { std::vector> result; - auto& lookup_value = *lookup_value_ptr; - auto& lookup_input = *lookup_input_ptr; + auto &lookup_value = *lookup_value_ptr; + auto &lookup_input = *lookup_input_ptr; auto g = math::polynomial_dfs::one(); auto one = FieldType::value_type::one(); @@ -315,7 +314,7 @@ namespace nil { std::vector> g_multipliers; for (std::size_t i = 0; i < lookup_input.size(); i++) { g_multipliers.push_back((one + beta) * (gamma + lookup_input[i])); - if( g_multipliers.size() == lookup_part_sizes[current_part] ){ + if (g_multipliers.size() == lookup_part_sizes[current_part]) { g *= math::polynomial_product(std::move(g_multipliers)); result.push_back(g); g_multipliers.clear(); @@ -327,11 +326,11 @@ namespace nil { // We don't use lookup_input after this line. lookup_input_ptr.reset(nullptr); - auto part1 = (one+beta) * gamma; + auto part1 = (one + beta) * gamma; for (std::size_t i = 0; i < lookup_value.size(); i++) { auto lookup_shifted = math::polynomial_shift(lookup_value[i], 1, basic_domain->m); - g_multipliers.push_back( part1 + lookup_value[i] + beta * lookup_shifted); - if( g_multipliers.size() == lookup_part_sizes[current_part] ){ + g_multipliers.push_back(part1 + lookup_value[i] + beta * lookup_shifted); + if (g_multipliers.size() == lookup_part_sizes[current_part]) { g *= math::polynomial_product(std::move(g_multipliers)); result.push_back(g); g_multipliers.clear(); @@ -348,11 +347,10 @@ namespace nil { } std::vector> compute_hs( - const std::vector>& sorted, - const typename FieldType::value_type& beta, - const typename FieldType::value_type& gamma, - const std::vector &lookup_part_sizes - ) { + const std::vector> &sorted, + const typename FieldType::value_type &beta, + const typename FieldType::value_type &gamma, + const std::vector &lookup_part_sizes) { auto one = FieldType::value_type::one(); std::vector> result; @@ -363,7 +361,7 @@ namespace nil { for (std::size_t i = 0; i < sorted.size(); i++) { auto sorted_shifted = math::polynomial_shift(sorted[i], 1, basic_domain->m); h_multipliers.push_back((one + beta) * gamma + sorted[i] + beta * sorted_shifted); - if( h_multipliers.size() == lookup_part_sizes[current_part] ){ + if (h_multipliers.size() == lookup_part_sizes[current_part]) { h = math::polynomial_product(h_multipliers); result.push_back(h); h_multipliers.clear(); @@ -377,55 +375,58 @@ namespace nil { } math::polynomial_dfs compute_V_L( - const std::vector>& sorted, - const std::vector>& reduced_input, - const std::vector>& reduced_value, - const typename FieldType::value_type& beta, - const typename FieldType::value_type& gamma) { + const std::vector> &sorted, + const std::vector> &reduced_input, + const std::vector> &reduced_value, + const typename FieldType::value_type &beta, + const typename FieldType::value_type &gamma) { math::polynomial_dfs V_L( - basic_domain->m-1,basic_domain->m, FieldType::value_type::zero()); + basic_domain->m - 1, basic_domain->m, FieldType::value_type::zero()); V_L[0] = FieldType::value_type::one(); auto one = FieldType::value_type::one(); for (std::size_t k = 1; k <= preprocessed_data.common_data.desc.usable_rows_amount; k++) { - V_L[k] = V_L[k-1]; + V_L[k] = V_L[k - 1]; typename FieldType::value_type g_tmp = (one + beta).pow(reduced_input.size()); for (std::size_t i = 0; i < reduced_input.size(); i++) { - g_tmp *= gamma + reduced_input[i][k-1]; + g_tmp *= gamma + reduced_input[i][k - 1]; } auto part1 = (one + beta) * gamma; for (std::size_t i = 0; i < reduced_value.size(); i++) { - g_tmp *= part1 + reduced_value[i][k-1] + beta * reduced_value[i][k]; + g_tmp *= part1 + reduced_value[i][k - 1] + beta * reduced_value[i][k]; } V_L[k] *= g_tmp; typename FieldType::value_type h_tmp = FieldType::value_type::one(); for (std::size_t i = 0; i < sorted.size(); i++) { - h_tmp *= part1 + sorted[i][k-1] + beta * sorted[i][k]; + h_tmp *= part1 + sorted[i][k - 1] + beta * sorted[i][k]; } V_L[k] *= h_tmp.inversed(); } return V_L; } - std::unique_ptr>> prepare_lookup_value( - const math::polynomial_dfs& mask_assignment) { + std::unique_ptr>> + prepare_lookup_value(const math::polynomial_dfs &mask_assignment) { typename FieldType::value_type theta_acc; // Prepare lookup value auto lookup_value_ptr = std::make_unique>>(); for (std::size_t t_id = 0; t_id < lookup_tables.size(); t_id++) { const plonk_lookup_table &l_table = lookup_tables[t_id]; - const math::polynomial_dfs &lookup_tag = plonk_columns.selector(l_table.tag_index); + const math::polynomial_dfs &lookup_tag = plonk_columns.selector( + l_table.tag_index); for (std::size_t o_id = 0; o_id < l_table.lookup_options.size(); o_id++) { - math::polynomial_dfs v = (typename FieldType::value_type(t_id + 1)) * lookup_tag; + math::polynomial_dfs v = + (typename FieldType::value_type(t_id + 1)) * lookup_tag; theta_acc = theta; for (std::size_t i = 0; i < l_table.columns_number; i++) { - v += theta_acc * lookup_tag * plonk_columns.constant(l_table.lookup_options[o_id][i].index); + v += theta_acc * lookup_tag * + plonk_columns.constant(l_table.lookup_options[o_id][i].index); theta_acc *= theta; } v *= mask_assignment; @@ -435,19 +436,20 @@ namespace nil { return std::move(lookup_value_ptr); } - std::unique_ptr>> prepare_lookup_input() { + std::unique_ptr>> + prepare_lookup_input() { // Copied from gate argument. // TODO: remove code duplication. auto value_type_to_polynomial_dfs = []( - const typename VariableType::assignment_type& coeff) { - return polynomial_dfs_type(0, 1, coeff); - }; + const typename VariableType::assignment_type &coeff) { + return polynomial_dfs_type(0, 1, coeff); + }; math::expression_variable_type_converter converter( - value_type_to_polynomial_dfs); + value_type_to_polynomial_dfs); - auto get_var_value = [&domain=basic_domain, &assignments=plonk_columns] - (const DfsVariableType &var) { + auto get_var_value = [&domain = basic_domain, &assignments = plonk_columns] + (const DfsVariableType &var) { polynomial_dfs_type assignment; switch (var.type) { case DfsVariableType::column_type::witness: @@ -474,13 +476,15 @@ namespace nil { // Prepare lookup input auto lookup_input_ptr = std::make_unique>>(); - for (const auto &gate : lookup_gates) { + for (const auto &gate: lookup_gates) { math::expression expr; - math::polynomial_dfs lookup_selector = plonk_columns.selector(gate.tag_index); - for (const auto &constraint : gate.constraints) { - math::polynomial_dfs l = lookup_selector * (typename FieldType::value_type(constraint.table_id)); + math::polynomial_dfs lookup_selector = plonk_columns.selector( + gate.tag_index); + for (const auto &constraint: gate.constraints) { + math::polynomial_dfs l = + lookup_selector * (typename FieldType::value_type(constraint.table_id)); theta_acc = theta; - for(std::size_t k = 0; k < constraint.lookup_input.size(); k++){ + for (std::size_t k = 0; k < constraint.lookup_input.size(); k++) { expr = converter.convert(constraint.lookup_input[k]); math::cached_expression_evaluator evaluator(expr, get_var_value); @@ -497,11 +501,10 @@ namespace nil { private: math::polynomial_dfs reduce_dfs_polynomial_domain( - const math::polynomial_dfs &polynomial, - const std::size_t &new_domain_size - ) { + const math::polynomial_dfs &polynomial, + const std::size_t &new_domain_size) { math::polynomial_dfs reduced( - new_domain_size - 1, new_domain_size, FieldType::value_type::zero()); + new_domain_size - 1, new_domain_size, FieldType::value_type::zero()); BOOST_ASSERT(new_domain_size <= polynomial.size()); if (polynomial.size() == new_domain_size) { @@ -518,16 +521,16 @@ namespace nil { }; math::polynomial_dfs get_constraint_tag_from_gate_tag_column( - math::polynomial_dfs tag_column, - std::size_t constraints_num, - std::size_t constraint_id, - std::size_t table_id - ){ + math::polynomial_dfs tag_column, + std::size_t constraints_num, + std::size_t constraint_id, + std::size_t table_id) { math::polynomial_dfs result = tag_column; for (std::size_t i = 1; i <= constraints_num; i++) { if (i != constraint_id) { auto tmp = tag_column - typename FieldType::value_type(i); - tmp /= (typename FieldType::value_type(constraint_id) - typename FieldType::value_type(i)); + tmp /= (typename FieldType::value_type(constraint_id) - + typename FieldType::value_type(i)); result *= tmp; } } @@ -537,16 +540,16 @@ namespace nil { } typename FieldType::value_type get_constraint_tag_value_from_gate_tag_value( - typename FieldType::value_type tag_value, - std::size_t constraints_num, - std::size_t constraint_id, - std::size_t table_id - ) { + typename FieldType::value_type tag_value, + std::size_t constraints_num, + std::size_t constraint_id, + std::size_t table_id) { typename FieldType::value_type result = tag_value; for (std::size_t i = 1; i <= constraints_num; i++) { if (i != constraint_id) { auto tmp = tag_value - typename FieldType::value_type(i); - tmp /= typename FieldType::value_type(constraint_id) - typename FieldType::value_type(i); + tmp /= typename FieldType::value_type(constraint_id) - + typename FieldType::value_type(i); result *= tmp; } } @@ -563,16 +566,15 @@ namespace nil { // So similar values in compressed lookup tables vectors repeated values may be only in one column // near each other. std::vector> sort_polynomials( - const std::vector>& reduced_input, - const std::vector>& reduced_value, - std::size_t domain_size, - std::size_t usable_rows_amount - ) { + const std::vector> &reduced_input, + const std::vector> &reduced_value, + std::size_t domain_size, + std::size_t usable_rows_amount) { // Build sorting map std::unordered_map sorting_map; for (std::size_t i = 0; i < reduced_value.size(); i++) { for (std::size_t j = 0; j < usable_rows_amount; j++) { - if(sorting_map.find(reduced_value[i][j]) != sorting_map.end()) + if (sorting_map.find(reduced_value[i][j]) != sorting_map.end()) sorting_map[reduced_value[i][j]]++; else sorting_map[reduced_value[i][j]] = 1; @@ -588,19 +590,21 @@ namespace nil { } math::polynomial_dfs zero_poly( - domain_size-1, domain_size, FieldType::value_type::zero()); + domain_size - 1, domain_size, FieldType::value_type::zero()); std::vector> sorted( - reduced_input.size() + reduced_value.size(), zero_poly + reduced_input.size() + reduced_value.size(), zero_poly ); - std::size_t i1=0; - std::size_t j1=0; + std::size_t i1 = 0; + std::size_t j1 = 0; typename FieldType::value_type prev(0); prev = typename FieldType::value_type(0); - auto append_to_sorted = [usable_rows_amount, &sorted, &i1, &j1] (const typename FieldType::value_type& value) { + auto append_to_sorted = [usable_rows_amount, &sorted, &i1, &j1]( + const typename FieldType::value_type &value) { sorted[i1][j1] = value; j1++; - if (j1 >= usable_rows_amount){ - i1++; j1 = 0; + if (j1 >= usable_rows_amount) { + i1++; + j1 = 0; } }; @@ -628,19 +632,19 @@ namespace nil { } for (std::size_t i = 0; i < sorted.size() - 1; i++) { - sorted[i][usable_rows_amount] = sorted[i+1][0]; + sorted[i][usable_rows_amount] = sorted[i + 1][0]; } return sorted; } const plonk_constraint_system &constraint_system; - const typename placeholder_public_preprocessor::preprocessed_data_type& preprocessed_data; + const typename placeholder_public_preprocessor::preprocessed_data_type &preprocessed_data; const plonk_polynomial_dfs_table &plonk_columns; - commitment_scheme_type& commitment_scheme; - transcript_type& transcript; + commitment_scheme_type &commitment_scheme; + transcript_type &transcript; std::shared_ptr> basic_domain; - const std::vector>>& lookup_gates; - const std::vector>& lookup_tables; + const std::vector>> &lookup_gates; + const std::vector> &lookup_tables; typename FieldType::value_type theta; std::size_t lookup_chunks; }; @@ -657,27 +661,26 @@ namespace nil { static constexpr std::size_t argument_size = 4; - typedef detail::placeholder_policy policy_type; + typedef placeholder policy_type; public: std::array verify_eval( - const typename placeholder_public_preprocessor::preprocessed_data_type::common_data_type &common_data, - const std::vector &special_selector_values, - const std::vector &special_selector_values_shifted, - const plonk_constraint_system &constraint_system, - // y - const typename FieldType::value_type &challenge, - typename policy_type::evaluation_map &evaluations, - // sorted_batch_values. Pair value/shifted_value - const std::vector> &sorted, - // V_L(y), V_L(omega* Y) - std::vector V_L_values, - // parts values - std::vector parts_values, - // Commitment - const typename CommitmentSchemeTypePermutation::commitment_type &lookup_commitment, - transcript_type &transcript = transcript_type() - ) { + const typename placeholder_public_preprocessor::preprocessed_data_type::common_data_type &common_data, + const std::vector &special_selector_values, + const std::vector &special_selector_values_shifted, + const plonk_constraint_system &constraint_system, + // y + const typename FieldType::value_type &challenge, + typename policy_type::evaluation_map &evaluations, + // sorted_batch_values. Pair value/shifted_value + const std::vector> &sorted, + // V_L(y), V_L(omega* Y) + std::vector V_L_values, + // parts values + std::vector parts_values, + // Commitment + const typename CommitmentSchemeTypePermutation::commitment_type &lookup_commitment, + transcript_type &transcript = transcript_type()) { const std::vector>> &lookup_gates = constraint_system.lookup_gates(); const std::vector> &lookup_tables = constraint_system.lookup_tables(); std::array F; @@ -691,29 +694,34 @@ namespace nil { typename FieldType::value_type one = FieldType::value_type::one(); auto mask_value = (one - (special_selector_values[1] + special_selector_values[2])); - auto shifted_mask_value = (one - (special_selector_values_shifted[0] + special_selector_values_shifted[1])); + auto shifted_mask_value = (one - (special_selector_values_shifted[0] + + special_selector_values_shifted[1])); typename FieldType::value_type theta_acc = FieldType::value_type::one(); std::vector lookup_value; std::vector shifted_lookup_value; - for( std::size_t t_id = 0; t_id < lookup_tables.size(); t_id++){ + for (std::size_t t_id = 0; t_id < lookup_tables.size(); t_id++) { const auto &table = lookup_tables[t_id]; - auto key = std::tuple(table.tag_index, 0, plonk_variable::column_type::selector); - auto shifted_key = std::tuple(table.tag_index, 1, plonk_variable::column_type::selector); + auto key = std::tuple(table.tag_index, 0, + plonk_variable::column_type::selector); + auto shifted_key = std::tuple(table.tag_index, 1, + plonk_variable::column_type::selector); auto selector_value = evaluations[key]; auto shifted_selector_value = evaluations[shifted_key]; - for( std::size_t o_id = 0; o_id < table.lookup_options.size(); o_id++){ + for (std::size_t o_id = 0; o_id < table.lookup_options.size(); o_id++) { typename FieldType::value_type v = selector_value * (t_id + 1); typename FieldType::value_type shifted_v = shifted_selector_value * (t_id + 1); theta_acc = theta; BOOST_ASSERT(table.lookup_options[o_id].size() == table.columns_number); - for( std::size_t i = 0; i < table.lookup_options[o_id].size(); i++){ - auto key1 = std::tuple(table.lookup_options[o_id][i].index, 0, plonk_variable::column_type::constant); - auto shifted_key1 = std::tuple(table.lookup_options[o_id][i].index, 1, plonk_variable::column_type::constant); + for (std::size_t i = 0; i < table.lookup_options[o_id].size(); i++) { + auto key1 = std::tuple(table.lookup_options[o_id][i].index, 0, + plonk_variable::column_type::constant); + auto shifted_key1 = std::tuple(table.lookup_options[o_id][i].index, 1, + plonk_variable::column_type::constant); v += theta_acc * evaluations[key1] * selector_value; - shifted_v += theta_acc * evaluations[shifted_key1]* shifted_selector_value; + shifted_v += theta_acc * evaluations[shifted_key1] * shifted_selector_value; theta_acc *= theta; } v *= mask_value; @@ -725,15 +733,16 @@ namespace nil { // 4. Calculate compressed lookup inputs std::vector lookup_input; - for( std::size_t g_id = 0; g_id < lookup_gates.size(); g_id++ ){ + for (std::size_t g_id = 0; g_id < lookup_gates.size(); g_id++) { const auto &gate = lookup_gates[g_id]; - auto key = std::tuple(gate.tag_index, 0, plonk_variable::column_type::selector); + auto key = std::tuple(gate.tag_index, 0, + plonk_variable::column_type::selector); auto selector_value = evaluations[key]; - for( std::size_t c_id = 0; c_id < gate.constraints.size(); c_id++){ + for (std::size_t c_id = 0; c_id < gate.constraints.size(); c_id++) { const auto &constraint = gate.constraints[c_id]; typename FieldType::value_type l = selector_value * constraint.table_id; theta_acc = theta; - for( std::size_t k = 0; k < constraint.lookup_input.size(); k++ ) { + for (std::size_t k = 0; k < constraint.lookup_input.size(); k++) { l += selector_value * theta_acc * constraint.lookup_input[k].evaluate(evaluations); theta_acc *= theta; } @@ -746,7 +755,7 @@ namespace nil { std::vector lookup_alphas; auto parts = lookup_parts(constraint_system, common_data.max_quotient_chunks); - for(std::size_t i = 0; i < parts.size() - 1; i++){ + for (std::size_t i = 0; i < parts.size() - 1; i++) { lookup_alphas.push_back(transcript.template challenge()); } BOOST_ASSERT(lookup_alphas.size() == parts_values.size()); @@ -757,20 +766,20 @@ namespace nil { std::size_t current_part = 0; std::size_t current_size = 0; typename FieldType::value_type g(1); - for( std::size_t i = 0; i < lookup_input.size(); i++){ - g *= (one+beta)*(gamma + lookup_input[i]); + for (std::size_t i = 0; i < lookup_input.size(); i++) { + g *= (one + beta) * (gamma + lookup_input[i]); current_size++; - if( current_size == parts[current_part] ){ + if (current_size == parts[current_part]) { gs.push_back(g); g = FieldType::value_type::one(); current_size = 0; current_part++; } } - for( std::size_t i = 0; i < lookup_value.size(); i++ ){ - g *= (one+beta) * gamma + lookup_value[i] + beta * shifted_lookup_value[i]; + for (std::size_t i = 0; i < lookup_value.size(); i++) { + g *= (one + beta) * gamma + lookup_value[i] + beta * shifted_lookup_value[i]; current_size++; - if( current_size == parts[current_part] ){ + if (current_size == parts[current_part]) { gs.push_back(g); g = FieldType::value_type::one(); current_size = 0; @@ -782,10 +791,10 @@ namespace nil { typename FieldType::value_type h(1); current_part = 0; current_size = 0; - for( std::size_t i = 0; i < sorted.size(); i++){ - h *= (one+beta) * gamma + sorted[i][0] + beta * sorted[i][1]; + for (std::size_t i = 0; i < sorted.size(); i++) { + h *= (one + beta) * gamma + sorted[i][0] + beta * sorted[i][1]; current_size++; - if( current_size == parts[current_part] ){ + if (current_size == parts[current_part]) { hs.push_back(h); h = FieldType::value_type::one(); current_size = 0; @@ -801,7 +810,7 @@ namespace nil { F[0] = (one - V_L_value) * special_selector_values[0]; F[1] = special_selector_values[1] * (V_L_value * V_L_value - V_L_value); - if(parts.size() == 1){ + if (parts.size() == 1) { g = gs[0]; h = hs[0]; F[2] = (one - (special_selector_values[1] + special_selector_values[2])) * @@ -809,7 +818,7 @@ namespace nil { } else { typename FieldType::value_type current_value; typename FieldType::value_type previous_value = V_L_value; - for( std::size_t i = 0; i < lookup_alphas.size(); i ++ ){ + for (std::size_t i = 0; i < lookup_alphas.size(); i++) { auto g = gs[i]; auto h = hs[i]; current_value = parts_values[i]; @@ -824,9 +833,9 @@ namespace nil { F[2] *= (special_selector_values[1] + special_selector_values[2]) - one; } F[3] = 0; - for( std::size_t i = 1; i < sorted.size(); i++ ){ + for (std::size_t i = 1; i < sorted.size(); i++) { typename FieldType::value_type alpha = transcript.template challenge(); - F[3] += (sorted[i][0] - sorted[i-1][2]) * alpha * special_selector_values[0]; + F[3] += (sorted[i][0] - sorted[i - 1][2]) * alpha * special_selector_values[0]; } return F; } diff --git a/include/nil/crypto3/zk/snark/systems/plonk/placeholder/permutation_argument.hpp b/include/nil/crypto3/zk/snark/systems/plonk/placeholder/permutation_argument.hpp index 110f38d08..9cb2d5e4e 100644 --- a/include/nil/crypto3/zk/snark/systems/plonk/placeholder/permutation_argument.hpp +++ b/include/nil/crypto3/zk/snark/systems/plonk/placeholder/permutation_argument.hpp @@ -41,7 +41,7 @@ #include #include -#include +#include "nil/crypto3/zk/snark/systems/plonk/placeholder.hpp" #include #include diff --git a/include/nil/crypto3/zk/snark/systems/plonk/placeholder/preprocessor.hpp b/include/nil/crypto3/zk/snark/systems/plonk/placeholder/preprocessor.hpp index d2118043e..a2e5656a1 100644 --- a/include/nil/crypto3/zk/snark/systems/plonk/placeholder/preprocessor.hpp +++ b/include/nil/crypto3/zk/snark/systems/plonk/placeholder/preprocessor.hpp @@ -42,7 +42,7 @@ #include #include #include -#include +#include #include #include #include @@ -64,7 +64,7 @@ namespace nil { template class placeholder_public_preprocessor { - typedef detail::placeholder_policy policy_type; + typedef placeholder policy_type; typedef typename plonk_constraint::variable_type variable_type; typedef typename math::polynomial polynomial_type; typedef typename math::polynomial_dfs polynomial_dfs_type; @@ -613,7 +613,7 @@ namespace nil { template class placeholder_private_preprocessor { - using policy_type = detail::placeholder_policy; + using policy_type = placeholder; public: struct preprocessed_data_type { diff --git a/include/nil/crypto3/zk/snark/systems/plonk/placeholder/prover.hpp b/include/nil/crypto3/zk/snark/systems/plonk/placeholder/prover.hpp index 4f07262f2..5ef48b946 100644 --- a/include/nil/crypto3/zk/snark/systems/plonk/placeholder/prover.hpp +++ b/include/nil/crypto3/zk/snark/systems/plonk/placeholder/prover.hpp @@ -39,7 +39,8 @@ #include #include #include -#include + +#include #include #include #include @@ -54,8 +55,8 @@ namespace nil { namespace detail { template static inline std::vector> - split_polynomial(const math::polynomial &f, - std::size_t max_degree) { + split_polynomial(const math::polynomial &f, + std::size_t max_degree) { PROFILE_PLACEHOLDER_SCOPE("split_polynomial_time"); std::vector> f_splitted; @@ -74,7 +75,7 @@ namespace nil { using transcript_hash_type = typename ParamsType::transcript_hash_type; using transcript_type = transcript::fiat_shamir_heuristic_sequential; - using policy_type = detail::placeholder_policy; + using policy_type = placeholder; typedef typename math::polynomial polynomial_type; typedef typename math::polynomial_dfs polynomial_dfs_type; @@ -89,45 +90,41 @@ namespace nil { constexpr static const std::size_t permutation_parts = 3; constexpr static const std::size_t lookup_parts = 6; constexpr static const std::size_t f_parts = 8; - public: + public: static inline placeholder_proof process( - const typename public_preprocessor_type::preprocessed_data_type &preprocessed_public_data, - typename private_preprocessor_type::preprocessed_data_type preprocessed_private_data, - const plonk_table_description &table_description, - const plonk_constraint_system &constraint_system, - commitment_scheme_type commitment_scheme - ) { + const typename public_preprocessor_type::preprocessed_data_type &preprocessed_public_data, + typename private_preprocessor_type::preprocessed_data_type preprocessed_private_data, + const plonk_table_description &table_description, + const plonk_constraint_system &constraint_system, + commitment_scheme_type commitment_scheme) { auto prover = placeholder_prover( - preprocessed_public_data, std::move(preprocessed_private_data), table_description, - constraint_system, commitment_scheme); + preprocessed_public_data, std::move(preprocessed_private_data), table_description, + constraint_system, commitment_scheme); return prover.process(); } placeholder_prover( - const typename public_preprocessor_type::preprocessed_data_type &preprocessed_public_data, - typename private_preprocessor_type::preprocessed_data_type preprocessed_private_data, - const plonk_table_description &table_description, - const plonk_constraint_system &constraint_system, - const commitment_scheme_type &commitment_scheme - ) - : preprocessed_public_data(preprocessed_public_data) - , table_description(table_description) - , constraint_system(constraint_system) - , _polynomial_table(new plonk_polynomial_dfs_table( - std::move(preprocessed_private_data.private_polynomial_table), - preprocessed_public_data.public_polynomial_table)) - - , transcript(std::vector({})) - , _is_lookup_enabled(constraint_system.lookup_gates().size() > 0) - , _commitment_scheme(commitment_scheme) - { + const typename public_preprocessor_type::preprocessed_data_type &preprocessed_public_data, + typename private_preprocessor_type::preprocessed_data_type preprocessed_private_data, + const plonk_table_description &table_description, + const plonk_constraint_system &constraint_system, + const commitment_scheme_type &commitment_scheme) + : preprocessed_public_data(preprocessed_public_data), table_description(table_description), + constraint_system(constraint_system), + _polynomial_table(new plonk_polynomial_dfs_table( + std::move(preprocessed_private_data.private_polynomial_table), + preprocessed_public_data.public_polynomial_table)), + transcript(std::vector({})), + _is_lookup_enabled(constraint_system.lookup_gates().size() > 0), + _commitment_scheme(commitment_scheme) { // Initialize transcript. transcript(preprocessed_public_data.common_data.vk.constraint_system_with_params_hash); transcript(preprocessed_public_data.common_data.vk.fixed_values_commitment); // Setup commitment scheme. LPC adds an additional point here. - _commitment_scheme.setup(transcript, preprocessed_public_data.common_data.commitment_scheme_data); + _commitment_scheme.setup(transcript, + preprocessed_public_data.common_data.commitment_scheme_data); } placeholder_proof process() { @@ -138,19 +135,20 @@ namespace nil { _commitment_scheme.append_to_batch(VARIABLE_VALUES_BATCH, _polynomial_table->public_inputs()); { PROFILE_PLACEHOLDER_SCOPE("variable_values_precommit_time"); - _proof.commitments[VARIABLE_VALUES_BATCH] = _commitment_scheme.commit(VARIABLE_VALUES_BATCH); + _proof.commitments[VARIABLE_VALUES_BATCH] = _commitment_scheme.commit( + VARIABLE_VALUES_BATCH); } transcript(_proof.commitments[VARIABLE_VALUES_BATCH]); // 4. permutation_argument - if( constraint_system.copy_constraints().size() > 0 ){ + if (constraint_system.copy_constraints().size() > 0) { auto permutation_argument = placeholder_permutation_argument::prove_eval( - constraint_system, - preprocessed_public_data, - table_description, - *_polynomial_table, - _commitment_scheme, - transcript); + constraint_system, + preprocessed_public_data, + table_description, + *_polynomial_table, + _commitment_scheme, + transcript); _F_dfs[0] = std::move(permutation_argument.F_dfs[0]); _F_dfs[1] = std::move(permutation_argument.F_dfs[1]); @@ -166,26 +164,24 @@ namespace nil { _F_dfs[6] = std::move(lookup_argument_result.F_dfs[3]); } - if( constraint_system.copy_constraints().size() > 0 || constraint_system.lookup_gates().size() > 0){ + if (constraint_system.copy_constraints().size() > 0 || + constraint_system.lookup_gates().size() > 0) { _proof.commitments[PERMUTATION_BATCH] = _commitment_scheme.commit(PERMUTATION_BATCH); transcript(_proof.commitments[PERMUTATION_BATCH]); } // 6. circuit-satisfability - polynomial_dfs_type mask_polynomial( - 0, preprocessed_public_data.common_data.basic_domain->m, - typename FieldType::value_type(1) - ); + polynomial_dfs_type mask_polynomial(0, preprocessed_public_data.common_data.basic_domain->m, + typename FieldType::value_type(1)); mask_polynomial -= preprocessed_public_data.q_last; mask_polynomial -= preprocessed_public_data.q_blind; _F_dfs[7] = placeholder_gates_argument::prove_eval( - constraint_system, *_polynomial_table, - preprocessed_public_data.common_data.basic_domain, - preprocessed_public_data.common_data.max_gates_degree, - mask_polynomial, - transcript - )[0]; + constraint_system, *_polynomial_table, + preprocessed_public_data.common_data.basic_domain, + preprocessed_public_data.common_data.max_gates_degree, + mask_polynomial, + transcript)[0]; /////TEST #ifdef ZK_PLACEHOLDER_DEBUG_ENABLED @@ -197,7 +193,7 @@ namespace nil { // 7. Aggregate quotient polynomial { std::vector T_splitted_dfs = - quotient_polynomial_split_dfs(); + quotient_polynomial_split_dfs(); _proof.commitments[QUOTIENT_BATCH] = T_commit(T_splitted_dfs); } @@ -220,24 +216,27 @@ namespace nil { std::vector quotient_polynomial_split_dfs() { // TODO: pass max_degree parameter placeholder std::vector T_splitted = detail::split_polynomial( - quotient_polynomial(), table_description.rows_amount - 1 - ); + quotient_polynomial(), table_description.rows_amount - 1); PROFILE_PLACEHOLDER_SCOPE("split_polynomial_dfs_conversion_time"); std::size_t split_polynomial_size = std::max( - (preprocessed_public_data.identity_polynomials.size() + 2) * (preprocessed_public_data.common_data.desc.rows_amount -1 ), - (constraint_system.lookup_poly_degree_bound() + 1) * (preprocessed_public_data.common_data.desc.rows_amount -1 )//, - ); - split_polynomial_size = std::max( - split_polynomial_size, - (preprocessed_public_data.common_data.max_gates_degree + 1) * (preprocessed_public_data.common_data.desc.rows_amount -1) - ); - split_polynomial_size = (split_polynomial_size % preprocessed_public_data.common_data.desc.rows_amount != 0)? - (split_polynomial_size / preprocessed_public_data.common_data.desc.rows_amount + 1): - (split_polynomial_size / preprocessed_public_data.common_data.desc.rows_amount); - - if( preprocessed_public_data.common_data.max_quotient_chunks != 0 && split_polynomial_size > preprocessed_public_data.common_data.max_quotient_chunks){ + (preprocessed_public_data.identity_polynomials.size() + 2) * + (preprocessed_public_data.common_data.desc.rows_amount - 1), + (constraint_system.lookup_poly_degree_bound() + 1) * + (preprocessed_public_data.common_data.desc.rows_amount - 1)); + split_polynomial_size = std::max(split_polynomial_size, + (preprocessed_public_data.common_data.max_gates_degree + 1) * + (preprocessed_public_data.common_data.desc.rows_amount - 1)); + split_polynomial_size = (split_polynomial_size % + preprocessed_public_data.common_data.desc.rows_amount != 0) ? + (split_polynomial_size / + preprocessed_public_data.common_data.desc.rows_amount + 1) : + (split_polynomial_size / + preprocessed_public_data.common_data.desc.rows_amount); + + if (preprocessed_public_data.common_data.max_quotient_chunks != 0 && + split_polynomial_size > preprocessed_public_data.common_data.max_quotient_chunks) { split_polynomial_size = preprocessed_public_data.common_data.max_quotient_chunks; } @@ -249,7 +248,8 @@ namespace nil { // If some columns used in permutation or lookup argument are zero, real quotient polynomial degree // may be less than split_polynomial_size. std::vector T_splitted_dfs(split_polynomial_size, - polynomial_dfs_type(0, _F_dfs[0].size(), FieldType::value_type::zero())); + polynomial_dfs_type(0, _F_dfs[0].size(), + FieldType::value_type::zero())); for (std::size_t k = 0; k < T_splitted.size(); k++) { T_splitted_dfs[k].from_coefficients(T_splitted[k]); @@ -262,11 +262,11 @@ namespace nil { // 7.1. Get $\alpha_0, \dots, \alpha_8 \in \mathbb{F}$ from $hash(\text{transcript})$ std::array alphas = - transcript.template challenges(); + transcript.template challenges(); // 7.2. Compute F_consolidated polynomial_dfs_type F_consolidated_dfs( - 0, _F_dfs[0].size(), FieldType::value_type::zero()); + 0, _F_dfs[0].size(), FieldType::value_type::zero()); for (std::size_t i = 0; i < f_parts; i++) { if (_F_dfs[i].is_zero()) { continue; @@ -277,41 +277,44 @@ namespace nil { polynomial_type F_consolidated_normal(F_consolidated_dfs.coefficients()); polynomial_type T_consolidated = - F_consolidated_normal / preprocessed_public_data.common_data.Z; + F_consolidated_normal / preprocessed_public_data.common_data.Z; return T_consolidated; } typename placeholder_lookup_argument_prover::prover_lookup_result - lookup_argument() { + lookup_argument() { PROFILE_PLACEHOLDER_SCOPE("lookup_argument_time"); typename placeholder_lookup_argument_prover< - FieldType, - commitment_scheme_type, - ParamsType>::prover_lookup_result lookup_argument_result; - - lookup_argument_result.F_dfs[0] = polynomial_dfs_type(0, table_description.rows_amount, FieldType::value_type::zero()); - lookup_argument_result.F_dfs[1] = polynomial_dfs_type(0, table_description.rows_amount, FieldType::value_type::zero()); - lookup_argument_result.F_dfs[2] = polynomial_dfs_type(0, table_description.rows_amount, FieldType::value_type::zero()); - lookup_argument_result.F_dfs[3] = polynomial_dfs_type(0, table_description.rows_amount, FieldType::value_type::zero()); + FieldType, + commitment_scheme_type, + ParamsType>::prover_lookup_result lookup_argument_result; + + lookup_argument_result.F_dfs[0] = polynomial_dfs_type(0, table_description.rows_amount, + FieldType::value_type::zero()); + lookup_argument_result.F_dfs[1] = polynomial_dfs_type(0, table_description.rows_amount, + FieldType::value_type::zero()); + lookup_argument_result.F_dfs[2] = polynomial_dfs_type(0, table_description.rows_amount, + FieldType::value_type::zero()); + lookup_argument_result.F_dfs[3] = polynomial_dfs_type(0, table_description.rows_amount, + FieldType::value_type::zero()); if (_is_lookup_enabled) { placeholder_lookup_argument_prover lookup_argument_prover( - constraint_system, - preprocessed_public_data, - *_polynomial_table, - _commitment_scheme, - transcript - ); -; + constraint_system, + preprocessed_public_data, + *_polynomial_table, + _commitment_scheme, + transcript + );; lookup_argument_result = lookup_argument_prover.prove_eval(); _proof.commitments[LOOKUP_BATCH] = lookup_argument_result.lookup_commitment; } return lookup_argument_result; } - commitment_type T_commit(const std::vector& T_splitted_dfs) { + commitment_type T_commit(const std::vector &T_splitted_dfs) { PROFILE_PLACEHOLDER_SCOPE("T_splitted_precommit_time"); _commitment_scheme.append_to_batch(QUOTIENT_BATCH, T_splitted_dfs); return _commitment_scheme.commit(QUOTIENT_BATCH); @@ -320,20 +323,24 @@ namespace nil { void placeholder_debug_output() { for (std::size_t i = 0; i < f_parts; i++) { for (std::size_t j = 0; j < table_description.rows_amount; j++) { - if (_F_dfs[i].evaluate(preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) != FieldType::value_type::zero()) { - std::cout << "_F_dfs[" << i << "] on row " << j << " = " << _F_dfs[i].evaluate(preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) << std::endl; + if (_F_dfs[i].evaluate( + preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) != + FieldType::value_type::zero()) { + std::cout << "_F_dfs[" << i << "] on row " << j << " = " << _F_dfs[i].evaluate( + preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) + << std::endl; } } } - const auto& gates = constraint_system.gates(); + const auto &gates = constraint_system.gates(); for (std::size_t i = 0; i < gates.size(); i++) { for (std::size_t j = 0; j < gates[i].constraints.size(); j++) { polynomial_dfs_type constraint_result = - gates[i].constraints[j].evaluate( - *_polynomial_table, preprocessed_public_data.common_data.basic_domain) * - _polynomial_table.selector(gates[i].selector_index); + gates[i].constraints[j].evaluate( + *_polynomial_table, preprocessed_public_data.common_data.basic_domain) * + _polynomial_table.selector(gates[i].selector_index); // for (std::size_t k = 0; k < table_description.rows_amount; k++) { if (constraint_result.evaluate( preprocessed_public_data.common_data.basic_domain->get_domain_element(253)) != @@ -355,31 +362,35 @@ namespace nil { for (std::size_t variable_values_index = 0; variable_values_index < witness_columns + public_input_columns; variable_values_index++ - ) { - const std::set& variable_values_rotation = - preprocessed_public_data.common_data.columns_rotations[variable_values_index]; + ) { + const std::set &variable_values_rotation = + preprocessed_public_data.common_data.columns_rotations[variable_values_index]; for (int rotation: variable_values_rotation) { _commitment_scheme.append_eval_point( - VARIABLE_VALUES_BATCH, - variable_values_index, - _proof.eval_proof.challenge * _omega.pow(rotation) + VARIABLE_VALUES_BATCH, + variable_values_index, + _proof.eval_proof.challenge * _omega.pow(rotation) ); } } - if(_is_lookup_enabled || constraint_system.copy_constraints().size() > 0){ + if (_is_lookup_enabled || constraint_system.copy_constraints().size() > 0) { _commitment_scheme.append_eval_point(PERMUTATION_BATCH, _proof.eval_proof.challenge); - _commitment_scheme.append_eval_point(PERMUTATION_BATCH, 0, _proof.eval_proof.challenge * _omega); + _commitment_scheme.append_eval_point(PERMUTATION_BATCH, 0, + _proof.eval_proof.challenge * _omega); } - if(_is_lookup_enabled){ - _commitment_scheme.append_eval_point(PERMUTATION_BATCH, preprocessed_public_data.common_data.permutation_parts , _proof.eval_proof.challenge * _omega); + if (_is_lookup_enabled) { + _commitment_scheme.append_eval_point(PERMUTATION_BATCH, + preprocessed_public_data.common_data.permutation_parts, + _proof.eval_proof.challenge * _omega); _commitment_scheme.append_eval_point(LOOKUP_BATCH, _proof.eval_proof.challenge); _commitment_scheme.append_eval_point(LOOKUP_BATCH, _proof.eval_proof.challenge * _omega); _commitment_scheme.append_eval_point(LOOKUP_BATCH, _proof.eval_proof.challenge * - _omega.pow(preprocessed_public_data.common_data.desc.usable_rows_amount)); + _omega.pow( + preprocessed_public_data.common_data.desc.usable_rows_amount)); } _commitment_scheme.append_eval_point(QUOTIENT_BATCH, _proof.eval_proof.challenge); @@ -388,28 +399,32 @@ namespace nil { // fixed values' rotations (table columns) std::size_t i = 0; std::size_t start_index = preprocessed_public_data.identity_polynomials.size() + - preprocessed_public_data.permutation_polynomials.size() + 2; + preprocessed_public_data.permutation_polynomials.size() + 2; - for( i = 0; i < start_index; i++){ + for (i = 0; i < start_index; i++) { _commitment_scheme.append_eval_point(FIXED_VALUES_BATCH, i, _proof.eval_proof.challenge); } // For special selectors - _commitment_scheme.append_eval_point(FIXED_VALUES_BATCH, start_index - 2, _proof.eval_proof.challenge * _omega); - _commitment_scheme.append_eval_point(FIXED_VALUES_BATCH, start_index - 1, _proof.eval_proof.challenge * _omega); + _commitment_scheme.append_eval_point(FIXED_VALUES_BATCH, start_index - 2, + _proof.eval_proof.challenge * _omega); + _commitment_scheme.append_eval_point(FIXED_VALUES_BATCH, start_index - 1, + _proof.eval_proof.challenge * _omega); for (std::size_t ind = 0; - ind < constant_columns + preprocessed_public_data.public_polynomial_table.selectors().size(); - ind++, i++ - ) { - const std::set& fixed_values_rotation = - preprocessed_public_data.common_data.columns_rotations[witness_columns + public_input_columns + ind]; + ind < + constant_columns + preprocessed_public_data.public_polynomial_table.selectors().size(); + ind++, i++ + ) { + const std::set &fixed_values_rotation = + preprocessed_public_data.common_data.columns_rotations[witness_columns + + public_input_columns + ind]; for (int rotation: fixed_values_rotation) { _commitment_scheme.append_eval_point( - FIXED_VALUES_BATCH, - start_index + ind, - _proof.eval_proof.challenge * _omega.pow(rotation) + FIXED_VALUES_BATCH, + start_index + ind, + _proof.eval_proof.challenge * _omega.pow(rotation) ); } } @@ -417,41 +432,42 @@ namespace nil { std::vector> compute_evaluation_points_public() { std::vector> evaluation_points_public( - preprocessed_public_data.identity_polynomials.size() + - preprocessed_public_data.permutation_polynomials.size(), - _challenge_point); + preprocessed_public_data.identity_polynomials.size() + + preprocessed_public_data.permutation_polynomials.size(), + _challenge_point); const std::size_t witness_columns = table_description.witness_columns; const std::size_t public_input_columns = table_description.public_input_columns; const std::size_t constant_columns = table_description.constant_columns; for (std::size_t k = 0, rotation_index = witness_columns + public_input_columns; - k < constant_columns; k++, rotation_index++) { + k < constant_columns; k++, rotation_index++) { - const std::set& rotations = - preprocessed_public_data.common_data.columns_rotations[rotation_index]; + const std::set &rotations = + preprocessed_public_data.common_data.columns_rotations[rotation_index]; std::vector point; point.reserve(rotations.size()); for (int rotation: rotations) { // TODO: Maybe precompute values of _omega.pow(rotation)??? Rotation can be -1, causing computation // of inverse element multiple times. - point.push_back( _proof.eval_proof.challenge * _omega.pow(rotation)); + point.push_back(_proof.eval_proof.challenge * _omega.pow(rotation)); } evaluation_points_public.push_back(std::move(point)); } - for (std::size_t k = 0, rotation_index = witness_columns + public_input_columns + constant_columns; - k < preprocessed_public_data.public_polynomial_table.selectors().size(); - k++, rotation_index++) { + for (std::size_t k = 0, rotation_index = + witness_columns + public_input_columns + constant_columns; + k < preprocessed_public_data.public_polynomial_table.selectors().size(); + k++, rotation_index++) { - const std::set& rotations = - preprocessed_public_data.common_data.columns_rotations[rotation_index]; + const std::set &rotations = + preprocessed_public_data.common_data.columns_rotations[rotation_index]; std::vector point; point.reserve(rotations.size()); for (int rotation: rotations) { - point.push_back( _proof.eval_proof.challenge * _omega.pow(rotation)); + point.push_back(_proof.eval_proof.challenge * _omega.pow(rotation)); } evaluation_points_public.push_back(std::move(point)); } diff --git a/include/nil/crypto3/zk/snark/systems/plonk/placeholder/verifier.hpp b/include/nil/crypto3/zk/snark/systems/plonk/placeholder/verifier.hpp index 462978425..e04319585 100644 --- a/include/nil/crypto3/zk/snark/systems/plonk/placeholder/verifier.hpp +++ b/include/nil/crypto3/zk/snark/systems/plonk/placeholder/verifier.hpp @@ -32,7 +32,7 @@ #include #include -#include +#include #include #include #include @@ -47,7 +47,7 @@ namespace nil { template class placeholder_verifier { using transcript_hash_type = typename ParamsType::transcript_hash_type; - using policy_type = detail::placeholder_policy; + using policy_type = placeholder; using public_preprocessor_type = placeholder_public_preprocessor; using commitment_scheme_type = typename ParamsType::commitment_scheme_type; @@ -60,13 +60,12 @@ namespace nil { public: static void generate_evaluation_points( - commitment_scheme_type &_commitment_scheme, - const typename public_preprocessor_type::preprocessed_data_type::common_data_type &common_data, - const plonk_constraint_system &constraint_system, - const plonk_table_description &table_description, - typename FieldType::value_type challenge, - bool _is_lookup_enabled - ) { + commitment_scheme_type &_commitment_scheme, + const typename public_preprocessor_type::preprocessed_data_type::common_data_type &common_data, + const plonk_constraint_system &constraint_system, + const plonk_table_description &table_description, + typename FieldType::value_type challenge, + bool _is_lookup_enabled) { PROFILE_PLACEHOLDER_SCOPE("evaluation_points_generated_time"); const std::size_t witness_columns = table_description.witness_columns; @@ -79,30 +78,31 @@ namespace nil { // variable_values' rotations for (std::size_t variable_values_index = 0; variable_values_index < witness_columns + public_input_columns; - variable_values_index++ - ) { - const std::set& variable_values_rotation = - common_data.columns_rotations[variable_values_index]; + variable_values_index++) { + const std::set &variable_values_rotation = + common_data.columns_rotations[variable_values_index]; for (int rotation: variable_values_rotation) { _commitment_scheme.append_eval_point( - VARIABLE_VALUES_BATCH, - variable_values_index, - challenge * _omega.pow(rotation) + VARIABLE_VALUES_BATCH, + variable_values_index, + challenge * _omega.pow(rotation) ); } } - if( _is_lookup_enabled || constraint_system.copy_constraints().size() > 0){ + if (_is_lookup_enabled || constraint_system.copy_constraints().size() > 0) { _commitment_scheme.append_eval_point(PERMUTATION_BATCH, challenge); _commitment_scheme.append_eval_point(PERMUTATION_BATCH, 0, challenge * _omega); } if (_is_lookup_enabled) { - _commitment_scheme.append_eval_point(PERMUTATION_BATCH, common_data.permutation_parts , challenge * _omega); + _commitment_scheme.append_eval_point(PERMUTATION_BATCH, common_data.permutation_parts, + challenge * _omega); _commitment_scheme.append_eval_point(LOOKUP_BATCH, challenge); _commitment_scheme.append_eval_point(LOOKUP_BATCH, challenge * _omega); - _commitment_scheme.append_eval_point(LOOKUP_BATCH, challenge * _omega.pow(common_data.desc.usable_rows_amount)); + _commitment_scheme.append_eval_point(LOOKUP_BATCH, challenge * _omega.pow( + common_data.desc.usable_rows_amount)); } _commitment_scheme.append_eval_point(QUOTIENT_BATCH, challenge); @@ -112,7 +112,7 @@ namespace nil { std::size_t i = 0; std::size_t start_index = common_data.permuted_columns.size() * 2 + 2; - for( i = 0; i < start_index; i++){ + for (i = 0; i < start_index; i++) { _commitment_scheme.append_eval_point(FIXED_VALUES_BATCH, i, challenge); } @@ -120,31 +120,27 @@ namespace nil { _commitment_scheme.append_eval_point(FIXED_VALUES_BATCH, start_index - 2, challenge * _omega); _commitment_scheme.append_eval_point(FIXED_VALUES_BATCH, start_index - 1, challenge * _omega); - for (std::size_t ind = 0; - ind < constant_columns + selector_columns; - ind++, i++ - ) { - const std::set& fixed_values_rotation = - common_data.columns_rotations[witness_columns + public_input_columns + ind]; + for (std::size_t ind = 0; ind < constant_columns + selector_columns; ind++, i++) { + const std::set &fixed_values_rotation = + common_data.columns_rotations[witness_columns + public_input_columns + ind]; for (int rotation: fixed_values_rotation) { _commitment_scheme.append_eval_point( - FIXED_VALUES_BATCH, - start_index + ind, - challenge * _omega.pow(rotation) + FIXED_VALUES_BATCH, + start_index + ind, + challenge * _omega.pow(rotation) ); } } } static inline bool process( - const typename public_preprocessor_type::preprocessed_data_type::common_data_type &common_data, - const placeholder_proof &proof, - const plonk_table_description &table_description, - const plonk_constraint_system &constraint_system, - commitment_scheme_type commitment_scheme, - const std::vector> &public_input - ){ + const typename public_preprocessor_type::preprocessed_data_type::common_data_type &common_data, + const placeholder_proof &proof, + const plonk_table_description &table_description, + const plonk_constraint_system &constraint_system, + commitment_scheme_type commitment_scheme, + const std::vector> &public_input) { // TODO: process rotations for public input. auto omega = common_data.basic_domain->get_domain_element(1); auto challenge = proof.eval_proof.challenge; @@ -152,23 +148,24 @@ namespace nil { numerator /= typename FieldType::value_type(table_description.rows_amount); // If public input sizes are set, all of them should be set. - if(constraint_system.public_input_sizes_num() != 0 && constraint_system.public_input_sizes_num() != table_description.public_input_columns){ + 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 ){ + 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 = public_input[i].size(); if (constraint_system.public_input_sizes_num() != 0) max_size = std::min(max_size, constraint_system.public_input_size(i)); auto omega_pow = FieldType::value_type::one(); - for( std::size_t j = 0; j < max_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; } value *= numerator; - if( value != proof.eval_proof.eval_proof.z.get(VARIABLE_VALUES_BATCH, table_description.witness_columns + i, 0) ) - { + if (value != proof.eval_proof.eval_proof.z.get(VARIABLE_VALUES_BATCH, + table_description.witness_columns + i, 0)) { return false; } } @@ -176,18 +173,18 @@ namespace nil { } static inline bool process( - const typename public_preprocessor_type::preprocessed_data_type::common_data_type &common_data, - const placeholder_proof &proof, - const plonk_table_description &table_description, - const plonk_constraint_system &constraint_system, - commitment_scheme_type commitment_scheme - ) { + const typename public_preprocessor_type::preprocessed_data_type::common_data_type &common_data, + const placeholder_proof &proof, + const plonk_table_description &table_description, + const plonk_constraint_system &constraint_system, + commitment_scheme_type commitment_scheme) { const std::size_t witness_columns = table_description.witness_columns; const std::size_t public_input_columns = table_description.public_input_columns; const std::size_t constant_columns = table_description.constant_columns; const std::size_t selector_columns = table_description.selector_columns; - transcript::fiat_shamir_heuristic_sequential transcript(std::vector({})); + transcript::fiat_shamir_heuristic_sequential transcript( + std::vector({})); transcript(common_data.vk.constraint_system_with_params_hash); transcript(common_data.vk.fixed_values_commitment); @@ -200,13 +197,19 @@ namespace nil { std::vector special_selector_values(3); special_selector_values[0] = common_data.lagrange_0.evaluate(proof.eval_proof.challenge); - special_selector_values[1] = proof.eval_proof.eval_proof.z.get(FIXED_VALUES_BATCH, 2*common_data.permuted_columns.size(), 0); - special_selector_values[2] = proof.eval_proof.eval_proof.z.get(FIXED_VALUES_BATCH, 2*common_data.permuted_columns.size() + 1, 0); + special_selector_values[1] = proof.eval_proof.eval_proof.z.get(FIXED_VALUES_BATCH, 2 * + common_data.permuted_columns.size(), + 0); + special_selector_values[2] = proof.eval_proof.eval_proof.z.get(FIXED_VALUES_BATCH, 2 * + common_data.permuted_columns.size() + + 1, 0); // 4. prepare evaluaitons of the polynomials that are copy-constrained std::array F; - std::size_t permutation_size = (proof.eval_proof.eval_proof.z.get_batch_size(FIXED_VALUES_BATCH) - 2 - constant_columns - selector_columns) / 2; - if( constraint_system.copy_constraints().size() > 0 ){ + std::size_t permutation_size = + (proof.eval_proof.eval_proof.z.get_batch_size(FIXED_VALUES_BATCH) - 2 - + constant_columns - selector_columns) / 2; + if (constraint_system.copy_constraints().size() > 0) { // Permutation polys std::vector permuted_polys_global_indices = common_data.permuted_columns; std::vector f(permutation_size); @@ -215,39 +218,42 @@ namespace nil { for (std::size_t perm_i = 0; perm_i < permutation_size; perm_i++) { S_id.push_back(proof.eval_proof.eval_proof.z.get(FIXED_VALUES_BATCH, perm_i, 0)); - S_sigma.push_back(proof.eval_proof.eval_proof.z.get(FIXED_VALUES_BATCH, permutation_size + perm_i, 0)); + S_sigma.push_back( + proof.eval_proof.eval_proof.z.get(FIXED_VALUES_BATCH, permutation_size + perm_i, + 0)); std::size_t i = permuted_polys_global_indices[perm_i]; std::size_t zero_index = 0; for (int v: common_data.columns_rotations[i]) { - if (v == 0){ + if (v == 0) { break; } zero_index++; } if (i < witness_columns + public_input_columns) { - f[perm_i] = proof.eval_proof.eval_proof.z.get(VARIABLE_VALUES_BATCH,i,zero_index); - } else if (i >= witness_columns + public_input_columns ) { - std::size_t idx = i - witness_columns - public_input_columns + permutation_size*2 + 2; - f[perm_i] = proof.eval_proof.eval_proof.z.get(FIXED_VALUES_BATCH,idx,zero_index); + f[perm_i] = proof.eval_proof.eval_proof.z.get(VARIABLE_VALUES_BATCH, i, zero_index); + } else if (i >= witness_columns + public_input_columns) { + std::size_t idx = + i - witness_columns - public_input_columns + permutation_size * 2 + 2; + f[perm_i] = proof.eval_proof.eval_proof.z.get(FIXED_VALUES_BATCH, idx, zero_index); } } // 5. permutation argument std::vector perm_partitions; - for( std::size_t i = 1; i < common_data.permutation_parts; i++ ){ + for (std::size_t i = 1; i < common_data.permutation_parts; i++) { perm_partitions.push_back(proof.eval_proof.eval_proof.z.get(PERMUTATION_BATCH, i, 0)); } std::array permutation_argument = - placeholder_permutation_argument::verify_eval( - common_data, - S_id, S_sigma, special_selector_values, - proof.eval_proof.challenge, f, - proof.eval_proof.eval_proof.z.get(PERMUTATION_BATCH, 0, 0), - proof.eval_proof.eval_proof.z.get(PERMUTATION_BATCH, 0, 1), - perm_partitions, - transcript - ); + placeholder_permutation_argument::verify_eval( + common_data, + S_id, S_sigma, special_selector_values, + proof.eval_proof.challenge, f, + proof.eval_proof.eval_proof.z.get(PERMUTATION_BATCH, 0, 0), + proof.eval_proof.eval_proof.z.get(PERMUTATION_BATCH, 0, 1), + perm_partitions, + transcript + ); F[0] = permutation_argument[0]; F[1] = permutation_argument[1]; F[2] = permutation_argument[2]; @@ -259,9 +265,9 @@ namespace nil { std::size_t j = 0; for (int rotation: common_data.columns_rotations[i_global_index]) { auto key = std::make_tuple( - i, - rotation, - plonk_variable::column_type::witness); + i, + rotation, + plonk_variable::column_type::witness); columns_at_y[key] = proof.eval_proof.eval_proof.z.get(VARIABLE_VALUES_BATCH, i, j); ++j; } @@ -272,11 +278,10 @@ namespace nil { std::size_t j = 0; for (int rotation: common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple( - i, - rotation, - plonk_variable::column_type::public_input); - columns_at_y[key] = proof.eval_proof.eval_proof.z.get(VARIABLE_VALUES_BATCH, witness_columns + i, j); + auto key = std::make_tuple(i, rotation, + plonk_variable::column_type::public_input); + columns_at_y[key] = proof.eval_proof.eval_proof.z.get(VARIABLE_VALUES_BATCH, + witness_columns + i, j); ++j; } } @@ -286,10 +291,11 @@ namespace nil { std::size_t j = 0; for (int rotation: common_data.columns_rotations[i_global_index]) { auto key = std::make_tuple( - i, - rotation, - plonk_variable::column_type::constant); - columns_at_y[key] = proof.eval_proof.eval_proof.z.get(FIXED_VALUES_BATCH, i + permutation_size*2 + 2, j); + i, + rotation, + plonk_variable::column_type::constant); + columns_at_y[key] = proof.eval_proof.eval_proof.z.get(FIXED_VALUES_BATCH, + i + permutation_size * 2 + 2, j); ++j; } } @@ -299,10 +305,12 @@ namespace nil { std::size_t j = 0; for (int rotation: common_data.columns_rotations[i_global_index]) { auto key = std::make_tuple( - i, - rotation, - plonk_variable::column_type::selector); - columns_at_y[key] = proof.eval_proof.eval_proof.z.get(FIXED_VALUES_BATCH, i + permutation_size*2 + 2 + constant_columns, j); + i, + rotation, + plonk_variable::column_type::selector); + columns_at_y[key] = proof.eval_proof.eval_proof.z.get(FIXED_VALUES_BATCH, + i + permutation_size * 2 + 2 + + constant_columns, j); ++j; } } @@ -312,43 +320,55 @@ namespace nil { std::array lookup_argument; if (is_lookup_enabled) { std::vector special_selector_values_shifted(2); - special_selector_values_shifted[0] = proof.eval_proof.eval_proof.z.get(FIXED_VALUES_BATCH, 2*common_data.permuted_columns.size(), 1); - special_selector_values_shifted[1] = proof.eval_proof.eval_proof.z.get(FIXED_VALUES_BATCH, 2*common_data.permuted_columns.size() + 1, 1); + special_selector_values_shifted[0] = proof.eval_proof.eval_proof.z.get(FIXED_VALUES_BATCH, + 2 * + common_data.permuted_columns.size(), + 1); + special_selector_values_shifted[1] = proof.eval_proof.eval_proof.z.get(FIXED_VALUES_BATCH, + 2 * + common_data.permuted_columns.size() + + 1, 1); std::vector lookup_parts_values; - for( std::size_t i = common_data.permutation_parts + 1; - i < common_data.permutation_parts + common_data.lookup_parts; - i++ - ) lookup_parts_values.push_back(proof.eval_proof.eval_proof.z.get(PERMUTATION_BATCH, i, 0)); + for (std::size_t i = common_data.permutation_parts + 1; + i < common_data.permutation_parts + common_data.lookup_parts; + i++ + ) + lookup_parts_values.push_back( + proof.eval_proof.eval_proof.z.get(PERMUTATION_BATCH, i, 0)); placeholder_lookup_argument_verifier lookup_argument_verifier; lookup_argument = lookup_argument_verifier.verify_eval( - common_data, - special_selector_values, special_selector_values_shifted, - constraint_system, - proof.eval_proof.challenge, columns_at_y, - proof.eval_proof.eval_proof.z.get(LOOKUP_BATCH), - proof.eval_proof.eval_proof.z.get(PERMUTATION_BATCH, common_data.permutation_parts), - lookup_parts_values, - proof.commitments.at(LOOKUP_BATCH), transcript + common_data, + special_selector_values, special_selector_values_shifted, + constraint_system, + proof.eval_proof.challenge, columns_at_y, + proof.eval_proof.eval_proof.z.get(LOOKUP_BATCH), + proof.eval_proof.eval_proof.z.get(PERMUTATION_BATCH, common_data.permutation_parts), + lookup_parts_values, + proof.commitments.at(LOOKUP_BATCH), transcript ); } - if( constraint_system.copy_constraints().size() > 0 || constraint_system.lookup_gates().size() > 0){ + if (constraint_system.copy_constraints().size() > 0 || + constraint_system.lookup_gates().size() > 0) { transcript(proof.commitments.at(PERMUTATION_BATCH)); } // 7. gate argument std::array gate_argument = - placeholder_gates_argument::verify_eval( - constraint_system.gates(), columns_at_y, proof.eval_proof.challenge, - FieldType::value_type::one() - - proof.eval_proof.eval_proof.z.get(FIXED_VALUES_BATCH, common_data.permuted_columns.size() * 2, 0) - - proof.eval_proof.eval_proof.z.get(FIXED_VALUES_BATCH, common_data.permuted_columns.size() * 2 + 1, 0), - transcript - ); + placeholder_gates_argument::verify_eval( + constraint_system.gates(), columns_at_y, proof.eval_proof.challenge, + FieldType::value_type::one() - + proof.eval_proof.eval_proof.z.get(FIXED_VALUES_BATCH, + common_data.permuted_columns.size() * 2, 0) - + proof.eval_proof.eval_proof.z.get(FIXED_VALUES_BATCH, + common_data.permuted_columns.size() * 2 + 1, + 0), + transcript + ); std::array alphas = - transcript.template challenges(); + transcript.template challenges(); // 9. Evaluation proof check transcript(proof.commitments.at(QUOTIENT_BATCH)); @@ -356,18 +376,24 @@ namespace nil { auto challenge = transcript.template challenge(); BOOST_ASSERT(challenge == proof.eval_proof.challenge); - commitment_scheme.set_batch_size(VARIABLE_VALUES_BATCH, proof.eval_proof.eval_proof.z.get_batch_size(VARIABLE_VALUES_BATCH)); - if( is_lookup_enabled || constraint_system.copy_constraints().size()) - commitment_scheme.set_batch_size(PERMUTATION_BATCH, proof.eval_proof.eval_proof.z.get_batch_size(PERMUTATION_BATCH)); - commitment_scheme.set_batch_size(QUOTIENT_BATCH, proof.eval_proof.eval_proof.z.get_batch_size(QUOTIENT_BATCH)); + commitment_scheme.set_batch_size(VARIABLE_VALUES_BATCH, + proof.eval_proof.eval_proof.z.get_batch_size( + VARIABLE_VALUES_BATCH)); + if (is_lookup_enabled || constraint_system.copy_constraints().size()) + commitment_scheme.set_batch_size(PERMUTATION_BATCH, + proof.eval_proof.eval_proof.z.get_batch_size( + PERMUTATION_BATCH)); + commitment_scheme.set_batch_size(QUOTIENT_BATCH, + proof.eval_proof.eval_proof.z.get_batch_size(QUOTIENT_BATCH)); if (is_lookup_enabled) - commitment_scheme.set_batch_size(LOOKUP_BATCH, proof.eval_proof.eval_proof.z.get_batch_size(LOOKUP_BATCH)); + commitment_scheme.set_batch_size(LOOKUP_BATCH, proof.eval_proof.eval_proof.z.get_batch_size( + LOOKUP_BATCH)); generate_evaluation_points(commitment_scheme, common_data, constraint_system, table_description, challenge, is_lookup_enabled); std::map commitments = proof.commitments; commitments[FIXED_VALUES_BATCH] = common_data.commitments.fixed_values; - if (!commitment_scheme.verify_eval( proof.eval_proof.eval_proof, commitments, transcript )) { + if (!commitment_scheme.verify_eval(proof.eval_proof.eval_proof, commitments, transcript)) { return false; } @@ -386,7 +412,7 @@ namespace nil { typename FieldType::value_type T_consolidated = FieldType::value_type::zero(); for (std::size_t i = 0; i < proof.eval_proof.eval_proof.z.get_batch_size(QUOTIENT_BATCH); i++) { T_consolidated += proof.eval_proof.eval_proof.z.get(QUOTIENT_BATCH, i, 0) * - challenge.pow((common_data.desc.rows_amount) * i); + challenge.pow((common_data.desc.rows_amount) * i); } // Z is polynomial -1, 0 ...., 0, 1 diff --git a/include/nil/crypto3/zk/snark/systems/ppzksnark/r1cs_gg_ppzksnark.hpp b/include/nil/crypto3/zk/snark/systems/ppzksnark/r1cs_gg_ppzksnark.hpp index 1f20e1574..7b94c31ac 100644 --- a/include/nil/crypto3/zk/snark/systems/ppzksnark/r1cs_gg_ppzksnark.hpp +++ b/include/nil/crypto3/zk/snark/systems/ppzksnark/r1cs_gg_ppzksnark.hpp @@ -48,32 +48,32 @@ namespace nil { namespace snark { template using is_basic_mode = typename std::bool_constant< - std::is_same, Generator>::value && - std::is_same, Prover>::value && - (std::is_same, - Verifier>::value || - std::is_same, - Verifier>::value - // || std::is_same, - // Verifier>::value - )>; + std::is_same, Generator>::value && + std::is_same, Prover>::value && + (std::is_same, + Verifier>::value || + std::is_same, + Verifier>::value + // || std::is_same, + // Verifier>::value + )>; template using is_aggregate_mode = typename std::bool_constant< - std::is_same, Generator>::value && - std::is_same, Prover>::value && - std::is_same< - r1cs_gg_ppzksnark_verifier_strong_input_consistency, - Verifier>::value>; + std::is_same, Generator>::value && + std::is_same, Prover>::value && + std::is_same< + r1cs_gg_ppzksnark_verifier_strong_input_consistency, + Verifier>::value>; template using is_encrypted_input_mode = typename std::bool_constant< - std::is_same, - Generator>::value && - std::is_same, Prover>::value && - std::is_same< - r1cs_gg_ppzksnark_verifier_strong_input_consistency, - Verifier>::value>; + std::is_same, + Generator>::value && + std::is_same, Prover>::value && + std::is_same< + r1cs_gg_ppzksnark_verifier_strong_input_consistency, + Verifier>::value>; /*! * @brief ppzkSNARK for R1CS with a security proof in the generic group (GG) model @@ -104,15 +104,15 @@ namespace nil { * */ template, - typename Prover = r1cs_gg_ppzksnark_prover, - typename Verifier = r1cs_gg_ppzksnark_verifier_strong_input_consistency, - proving_mode Mode = proving_mode::basic, typename = void> + typename Prover = r1cs_gg_ppzksnark_prover, + typename Verifier = r1cs_gg_ppzksnark_verifier_strong_input_consistency, + proving_mode Mode = proving_mode::basic, typename = void> class r1cs_gg_ppzksnark; template class r1cs_gg_ppzksnark< - CurveType, Generator, Prover, Verifier, proving_mode::basic, - typename std::enable_if::value>::type> { + CurveType, Generator, Prover, Verifier, proving_mode::basic, + typename std::enable_if::value>::type> { typedef detail::r1cs_gg_ppzksnark_basic_policy policy_type; public: @@ -149,8 +149,8 @@ namespace nil { template class r1cs_gg_ppzksnark< - CurveType, Generator, Prover, Verifier, proving_mode::aggregate, - typename std::enable_if::value>::type> { + CurveType, Generator, Prover, Verifier, proving_mode::aggregate, + typename std::enable_if::value>::type> { typedef detail::r1cs_gg_ppzksnark_basic_policy policy_type; typedef detail::r1cs_gg_ppzksnark_basic_policy basic_policy_type; @@ -175,16 +175,16 @@ namespace nil { // Generate key pair template, - typename GeneratorType = boost::random::mt19937> + typename CurveType::scalar_field_type::integral_type>, + typename GeneratorType = boost::random::mt19937> static inline keypair_type generate(const constraint_system_type &constraint_system) { return Generator::template process(constraint_system); } // Generate SRS pair template, - typename GeneratorType = boost::random::mt19937> + typename CurveType::scalar_field_type::integral_type>, + typename GeneratorType = boost::random::mt19937> static inline srs_pair_type generate(std::size_t num_proofs) { return Generator::template process(num_proofs); } @@ -221,9 +221,9 @@ namespace nil { // aggregate verify template, - typename GeneratorType = boost::random::mt19937, typename Hash = hashes::sha2<256>, - typename InputPrimaryInputRange, typename InputIterator> + typename CurveType::scalar_field_type::integral_type>, + typename GeneratorType = boost::random::mt19937, typename Hash = hashes::sha2<256>, + typename InputPrimaryInputRange, typename InputIterator> static inline bool verify(const verification_srs_type &ip_verifier_srs, const verification_key_type &pvk, const InputPrimaryInputRange &public_inputs, @@ -231,18 +231,18 @@ namespace nil { InputIterator transcript_include_first, InputIterator transcript_include_last) { return Verifier::template process( - ip_verifier_srs, pvk, public_inputs, proof, transcript_include_first, - transcript_include_last); + ip_verifier_srs, pvk, public_inputs, proof, transcript_include_first, + transcript_include_last); } }; template class r1cs_gg_ppzksnark::value>::type> { + typename std::enable_if::value>::type> { typedef detail::r1cs_gg_ppzksnark_basic_policy - policy_type; + policy_type; typedef detail::r1cs_gg_ppzksnark_basic_policy basic_policy_type; typedef typename basic_policy_type::proof_type basic_proof_type; @@ -258,12 +258,12 @@ namespace nil { // Generate key pair template, - typename GeneratorType = boost::random::mt19937> + typename DistributionType = boost::random::uniform_int_distribution< + typename CurveType::scalar_field_type::integral_type>, + typename GeneratorType = boost::random::mt19937> static inline KeyPairType generate(const constraint_system_type &constraint_system) { return Generator::template process( - constraint_system); + constraint_system); } // Proving diff --git a/include/nil/crypto3/zk/snark/verification_key.hpp b/include/nil/crypto3/zk/snark/verification_key.hpp index e67a33799..438d2f4e7 100644 --- a/include/nil/crypto3/zk/snark/verification_key.hpp +++ b/include/nil/crypto3/zk/snark/verification_key.hpp @@ -31,9 +31,9 @@ namespace nil { namespace crypto3 { namespace zk { namespace snark { - template + template struct verification_key { - typedef ZkScheme scheme_type; + typedef ProofSystemType scheme_type; }; } // namespace snark } // namespace zk diff --git a/test/systems/plonk/placeholder/circuits.hpp b/test/systems/plonk/placeholder/circuits.hpp index fa6471299..d178cc51b 100644 --- a/test/systems/plonk/placeholder/circuits.hpp +++ b/test/systems/plonk/placeholder/circuits.hpp @@ -29,8 +29,6 @@ #ifndef CRYPTO3_ZK_TEST_PLONK_CIRCUITS_HPP #define CRYPTO3_ZK_TEST_PLONK_CIRCUITS_HPP -#define _RND_ algebra::random_element(); - #include #include @@ -49,6 +47,7 @@ #include #include #include +#include namespace nil { namespace crypto3 { @@ -56,7 +55,7 @@ namespace nil { namespace snark { template class circuit_description { - typedef zk::snark::detail::placeholder_policy policy_type; + typedef placeholder policy_type; constexpr static const std::size_t witness_columns = ParamsType::witness_columns; constexpr static const std::size_t public_columns = ParamsType::public_input_columns; @@ -73,7 +72,7 @@ namespace nil { std::vector> lookup_tables; std::vector public_input_sizes; - circuit_description() : table_rows(0){ + circuit_description() : table_rows(0) { } }; @@ -97,10 +96,9 @@ namespace nil { template circuit_description, rows_amount_1> circuit_test_1( - typename nil::crypto3::random::algebraic_engine alg_rnd = nil::crypto3::random::algebraic_engine(), - boost::random::mt11213b rnd = boost::random::mt11213b() - ) { - using assignment_type = typename FieldType::value_type; + typename nil::crypto3::random::algebraic_engine alg_rnd = nil::crypto3::random::algebraic_engine(), + boost::random::mt11213b rnd = boost::random::mt11213b()) { + using assignment_type = typename FieldType::value_type; constexpr static const std::size_t usable_rows = rows_amount_1; @@ -140,9 +138,9 @@ namespace nil { q_mul[i] = FieldType::value_type::zero(); plonk_variable x(1, i, false, - plonk_variable::column_type::witness); + plonk_variable::column_type::witness); plonk_variable y(2, i - 1, false, - plonk_variable::column_type::witness); + plonk_variable::column_type::witness); //test_circuit.copy_constraints.push_back(plonk_copy_constraint(x, y)); } @@ -156,9 +154,9 @@ namespace nil { q_mul[i] = one; plonk_variable x(1, i, false, - plonk_variable::column_type::witness); + plonk_variable::column_type::witness); plonk_variable y(0, 0, false, - plonk_variable::column_type::public_input); + plonk_variable::column_type::public_input); test_circuit.copy_constraints.push_back(plonk_copy_constraint(x, y)); } @@ -178,22 +176,26 @@ namespace nil { public_input_assignment[i] = table[witness_columns + i]; } test_circuit.table = plonk_assignment_table( - plonk_private_assignment_table(private_assignment), - plonk_public_assignment_table( - public_input_assignment, constant_assignment, selectors_assignment)); + plonk_private_assignment_table(private_assignment), + plonk_public_assignment_table( + public_input_assignment, constant_assignment, selectors_assignment)); - test_circuit.table_rows = zk_padding>(test_circuit.table, alg_rnd); + test_circuit.table_rows = zk_padding>(test_circuit.table, + alg_rnd); - plonk_variable w0(0, 0, true, plonk_variable::column_type::witness); - plonk_variable w1(1, 0, true, plonk_variable::column_type::witness); - plonk_variable w2(2, 0, true, plonk_variable::column_type::witness); + plonk_variable w0(0, 0, true, + plonk_variable::column_type::witness); + plonk_variable w1(1, 0, true, + plonk_variable::column_type::witness); + plonk_variable w2(2, 0, true, + plonk_variable::column_type::witness); plonk_constraint add_constraint; add_constraint += w0; add_constraint += w1; add_constraint -= w2; - std::vector> add_gate_costraints {add_constraint}; + std::vector> add_gate_costraints{add_constraint}; plonk_gate> add_gate(0, add_gate_costraints); test_circuit.gates.push_back(add_gate); @@ -203,7 +205,7 @@ namespace nil { mul_constraint += w0_term * w1_term; mul_constraint -= w2; - std::vector> mul_gate_costraints {mul_constraint}; + std::vector> mul_gate_costraints{mul_constraint}; plonk_gate> mul_gate(1, mul_gate_costraints); test_circuit.gates.push_back(mul_gate); @@ -230,11 +232,9 @@ namespace nil { template circuit_description, usable_rows_t> - circuit_test_t( - typename FieldType::value_type pi0 = 0, - typename nil::crypto3::random::algebraic_engine alg_rnd = nil::crypto3::random::algebraic_engine(), - boost::random::mt11213b rnd = boost::random::mt11213b() - ) { + circuit_test_t(typename FieldType::value_type pi0 = 0, + typename nil::crypto3::random::algebraic_engine alg_rnd = nil::crypto3::random::algebraic_engine(), + boost::random::mt11213b rnd = boost::random::mt11213b()) { using assignment_type = typename FieldType::value_type; constexpr static const std::size_t usable_rows = usable_rows_t; @@ -277,9 +277,9 @@ namespace nil { q_mul[i] = FieldType::value_type::zero(); plonk_variable x(1, i, false, - plonk_variable::column_type::witness); + plonk_variable::column_type::witness); plonk_variable y(2, i - 1, false, - plonk_variable::column_type::witness); + plonk_variable::column_type::witness); test_circuit.copy_constraints.push_back(plonk_copy_constraint(x, y)); } @@ -293,9 +293,9 @@ namespace nil { q_mul[i] = one; plonk_variable x(1, i, false, - plonk_variable::column_type::witness); + plonk_variable::column_type::witness); plonk_variable y(0, 0, false, - plonk_variable::column_type::public_input); + plonk_variable::column_type::public_input); test_circuit.copy_constraints.push_back(plonk_copy_constraint(x, y)); } table[3][1] = FieldType::value_type::zero(); @@ -317,26 +317,27 @@ namespace nil { public_input_assignment[i] = table[witness_columns + i]; } test_circuit.table = plonk_assignment_table( - plonk_private_assignment_table(private_assignment), - plonk_public_assignment_table( - public_input_assignment, constant_assignment, selectors_assignment)); - test_circuit.table_rows = zk_padding>(test_circuit.table, alg_rnd); + plonk_private_assignment_table(private_assignment), + plonk_public_assignment_table( + public_input_assignment, constant_assignment, selectors_assignment)); + test_circuit.table_rows = zk_padding>(test_circuit.table, + alg_rnd); plonk_variable w0(0, 0, true, - plonk_variable::column_type::witness); + plonk_variable::column_type::witness); plonk_variable w1(1, 0, true, - plonk_variable::column_type::witness); + plonk_variable::column_type::witness); plonk_variable w2(2, 0, true, - plonk_variable::column_type::witness); + plonk_variable::column_type::witness); plonk_variable w0_prev(0, -1, true, - plonk_variable::column_type::witness); + plonk_variable::column_type::witness); plonk_constraint add_constraint; add_constraint += w0; add_constraint += w1; add_constraint -= w2; - std::vector> add_gate_costraints {add_constraint}; + std::vector> add_gate_costraints{add_constraint}; plonk_gate> add_gate(0, add_gate_costraints); test_circuit.gates.push_back(add_gate); @@ -347,7 +348,7 @@ namespace nil { mul_constraint -= w2; mul_constraint += w0_prev; - std::vector> mul_gate_costraints {mul_constraint}; + std::vector> mul_gate_costraints{mul_constraint}; plonk_gate> mul_gate(1, mul_gate_costraints); //test_circuit.gates.push_back(mul_gate); @@ -363,8 +364,8 @@ namespace nil { template circuit_description, usable_rows_3> circuit_test_3( - typename nil::crypto3::random::algebraic_engine alg_rnd = nil::crypto3::random::algebraic_engine(), - boost::random::mt11213b rnd = boost::random::mt11213b() + typename nil::crypto3::random::algebraic_engine alg_rnd = nil::crypto3::random::algebraic_engine(), + boost::random::mt11213b rnd = boost::random::mt11213b() ) { using assignment_type = typename FieldType::value_type; @@ -390,9 +391,9 @@ namespace nil { table[1] = {0, 0, 0, 0}; table[2] = {0, 0, 0, 0}; - table[3] = {0, 1, 0, 1}; //Lookup values - table[4] = {0, 0, 1, 0}; //Lookup values - table[5] = {0, 1, 0, 0}; //Lookup values + table[3] = {0, 1, 0, 1}; //Lookup values + table[4] = {0, 0, 1, 0}; //Lookup values + table[5] = {0, 1, 0, 0}; //Lookup values std::vector> private_assignment(witness_columns); for (std::size_t i = 0; i < witness_columns; i++) { @@ -415,18 +416,24 @@ namespace nil { constant_assignment[i] = table[witness_columns + i]; } test_circuit.table = plonk_assignment_table( - plonk_private_assignment_table(private_assignment), - plonk_public_assignment_table( - public_input_assignment, constant_assignment, selectors_assignment)); + plonk_private_assignment_table(private_assignment), + plonk_public_assignment_table( + public_input_assignment, constant_assignment, selectors_assignment)); test_circuit.table_rows = zk_padding(test_circuit.table, alg_rnd); - plonk_variable w0(0, 0, true, plonk_variable::column_type::witness); - plonk_variable w1(1, 0, true, plonk_variable::column_type::witness); - plonk_variable w2(2, 0, true, plonk_variable::column_type::witness); + plonk_variable w0(0, 0, true, + plonk_variable::column_type::witness); + plonk_variable w1(1, 0, true, + plonk_variable::column_type::witness); + plonk_variable w2(2, 0, true, + plonk_variable::column_type::witness); - plonk_variable c0(0, 0, true, plonk_variable::column_type::constant); - plonk_variable c1(1, 0, true, plonk_variable::column_type::constant); - plonk_variable c2(2, 0, true, plonk_variable::column_type::constant); + plonk_variable c0(0, 0, true, + plonk_variable::column_type::constant); + plonk_variable c1(1, 0, true, + plonk_variable::column_type::constant); + plonk_variable c2(2, 0, true, + plonk_variable::column_type::constant); plonk_lookup_constraint lookup_constraint; @@ -461,7 +468,7 @@ namespace nil { // | 1 | 1 | b7 | b8 | b7*b8 | 1 | 1 | 1 | 1 | // ---------------------------------------------------------------------------------- - constexpr static const std::size_t witness_columns_4= 3; + constexpr static const std::size_t witness_columns_4 = 3; constexpr static const std::size_t public_columns_4 = 0; constexpr static const std::size_t constant_columns_4 = 3; constexpr static const std::size_t selector_columns_4 = 3; @@ -470,7 +477,7 @@ namespace nil { circuit_description, 5> circuit_test_4( typename nil::crypto3::random::algebraic_engine alg_rnd = nil::crypto3::random::algebraic_engine(), boost::random::mt11213b rnd = boost::random::mt11213b() - ) { + ) { using assignment_type = typename FieldType::value_type; constexpr static const std::size_t rows_log = 3; @@ -495,7 +502,8 @@ namespace nil { // lookup inputs table[0] = {rnd() % 2, rnd() % 2, rnd(), rnd() % 2, rnd() % 2, 0, 0, 0}; table[1] = {rnd() % 2, rnd() % 2, rnd(), rnd() % 2, rnd() % 2, 0, 0, 0};; - table[2] = {table[0][0] * table[1][0], table[0][1] * table[1][1], table[0][2] * table[1][2], table[0][3] * table[1][3], table[0][4] * table[1][4], 0, 0, 0}; + table[2] = {table[0][0] * table[1][0], table[0][1] * table[1][1], table[0][2] * table[1][2], + table[0][3] * table[1][3], table[0][4] * table[1][4], 0, 0, 0}; //lookup values @@ -521,18 +529,24 @@ namespace nil { constant_assignment[i] = table[witness_columns + i]; } test_circuit.table = plonk_assignment_table( - plonk_private_assignment_table(private_assignment), - plonk_public_assignment_table( - public_input_assignment, constant_assignment, selectors_assignment)); + plonk_private_assignment_table(private_assignment), + plonk_public_assignment_table( + public_input_assignment, constant_assignment, selectors_assignment)); - plonk_variable w0(0, 0, true, plonk_variable::column_type::witness); - plonk_variable w1(1, 0, true, plonk_variable::column_type::witness); - plonk_variable w2(2, 0, true, plonk_variable::column_type::witness); + plonk_variable w0(0, 0, true, + plonk_variable::column_type::witness); + plonk_variable w1(1, 0, true, + plonk_variable::column_type::witness); + plonk_variable w2(2, 0, true, + plonk_variable::column_type::witness); - plonk_variable c0(0, 0, true, plonk_variable::column_type::constant); - plonk_variable c1(1, 0, true, plonk_variable::column_type::constant); - plonk_variable c2(2, 0, true, plonk_variable::column_type::constant); + plonk_variable c0(0, 0, true, + plonk_variable::column_type::constant); + plonk_variable c1(1, 0, true, + plonk_variable::column_type::constant); + plonk_variable c2(2, 0, true, + plonk_variable::column_type::constant); plonk_constraint mul_constraint; typename plonk_constraint::term_type w0_term(w0); @@ -540,7 +554,7 @@ namespace nil { mul_constraint += w0_term * w1_term; mul_constraint -= w2; - std::vector> mul_gate_costraints {mul_constraint}; + std::vector> mul_gate_costraints{mul_constraint}; plonk_gate> mul_gate(1, mul_gate_costraints); test_circuit.gates.push_back(mul_gate); @@ -581,8 +595,8 @@ namespace nil { template circuit_description, usable_rows_5> circuit_test_5( - typename nil::crypto3::random::algebraic_engine alg_rnd = nil::crypto3::random::algebraic_engine(), - boost::random::mt11213b rnd = boost::random::mt11213b() + typename nil::crypto3::random::algebraic_engine alg_rnd = nil::crypto3::random::algebraic_engine(), + boost::random::mt11213b rnd = boost::random::mt11213b() ) { using assignment_type = typename FieldType::value_type; @@ -600,33 +614,41 @@ namespace nil { test_circuit.public_input_sizes = {witness_columns}; std::vector> private_assignment(witness_columns); - std::vector> public_input_assignment(public_input_columns); + std::vector> public_input_assignment( + public_input_columns); std::vector> constant_assignment(constant_columns); std::vector> selectors_assignment(selector_columns); public_input_assignment[0].resize(test_circuit.usable_rows); selectors_assignment[0].resize(test_circuit.usable_rows); - for(std::size_t i = 0; i < witness_columns; i++) { + for (std::size_t i = 0; i < witness_columns; i++) { private_assignment[i].resize(test_circuit.usable_rows); - private_assignment[i][0] = private_assignment[i][2] = public_input_assignment[0][i] = typename FieldType::value_type(rnd() % witness_columns); + private_assignment[i][0] = private_assignment[i][2] = public_input_assignment[0][i] = typename FieldType::value_type( + rnd() % witness_columns); private_assignment[i][1] = 1; - plonk_variable pi(0, i, false, plonk_variable::column_type::public_input); - plonk_variable wi(i, 0, false, plonk_variable::column_type::witness); + plonk_variable pi(0, i, false, + plonk_variable::column_type::public_input); + plonk_variable wi(i, 0, false, + plonk_variable::column_type::witness); test_circuit.copy_constraints.push_back(plonk_copy_constraint({pi, wi})); } selectors_assignment[0][1] = 1; test_circuit.table = plonk_assignment_table( - plonk_private_assignment_table(private_assignment), - plonk_public_assignment_table( - public_input_assignment, constant_assignment, selectors_assignment)); - test_circuit.table_rows = zk_padding>(test_circuit.table, alg_rnd); + plonk_private_assignment_table(private_assignment), + plonk_public_assignment_table( + public_input_assignment, constant_assignment, selectors_assignment)); + test_circuit.table_rows = zk_padding>(test_circuit.table, + alg_rnd); std::vector> mul_gate_constraints; - for( std::size_t i = 0; i < witness_columns; i++){ - plonk_variable w0(i, -1, true, plonk_variable::column_type::witness); - plonk_variable w1(i, 0, true, plonk_variable::column_type::witness); - plonk_variable w2(i, 1, true, plonk_variable::column_type::witness); + for (std::size_t i = 0; i < witness_columns; i++) { + plonk_variable w0(i, -1, true, + plonk_variable::column_type::witness); + plonk_variable w1(i, 0, true, + plonk_variable::column_type::witness); + plonk_variable w2(i, 1, true, + plonk_variable::column_type::witness); mul_gate_constraints.push_back(w0 - w1 * w2); } @@ -656,7 +678,7 @@ namespace nil { template circuit_description, usable_rows> circuit_test_fib( - typename nil::crypto3::random::algebraic_engine alg_rnd = nil::crypto3::random::algebraic_engine() + typename nil::crypto3::random::algebraic_engine alg_rnd = nil::crypto3::random::algebraic_engine() ) { using assignment_type = typename FieldType::value_type; @@ -701,9 +723,9 @@ namespace nil { // test_circuit.copy_constraints.push_back(plonk_copy_constraint(x1, p1)); for (std::size_t i = 2; i < test_circuit.usable_rows - 1; i++) { - table[0][i] = table[0][i-2] + table[0][i-1]; + table[0][i] = table[0][i - 2] + table[0][i - 1]; table[1][i] = zero; - table[2][i-1] = one; + table[2][i - 1] = one; } std::vector> private_assignment(witness_columns); @@ -720,14 +742,18 @@ namespace nil { test_circuit.table = plonk_assignment_table( - plonk_private_assignment_table(private_assignment), - plonk_public_assignment_table( - public_input_assignment, constant_assignment, selectors_assignment)); - test_circuit.table_rows = zk_padding>(test_circuit.table, alg_rnd); - - plonk_variable w0(0, -1, true, plonk_variable::column_type::witness); - plonk_variable w1(0, 0, true, plonk_variable::column_type::witness); - plonk_variable w2(0, 1, true, plonk_variable::column_type::witness); + plonk_private_assignment_table(private_assignment), + plonk_public_assignment_table( + public_input_assignment, constant_assignment, selectors_assignment)); + test_circuit.table_rows = zk_padding>(test_circuit.table, + alg_rnd); + + plonk_variable w0(0, -1, true, + plonk_variable::column_type::witness); + plonk_variable w1(0, 0, true, + plonk_variable::column_type::witness); + plonk_variable w2(0, 1, true, + plonk_variable::column_type::witness); typename plonk_constraint::term_type w0_term(w0); typename plonk_constraint::term_type w1_term(w1); @@ -738,7 +764,7 @@ namespace nil { fib_constraint += w1_term; fib_constraint -= w2_term; - std::vector> fib_costraints {fib_constraint}; + std::vector> fib_costraints{fib_constraint}; plonk_gate> fib_gate(0, fib_costraints); test_circuit.gates.push_back(fib_gate); @@ -764,7 +790,7 @@ namespace nil { // | 1 | 1 | r6 | r5+r6 | 1 | 6 | 11 | 12 | // --------------------------------------------------------------------------------- - constexpr static const std::size_t witness_columns_6= 2; + constexpr static const std::size_t witness_columns_6 = 2; constexpr static const std::size_t public_columns_6 = 0; constexpr static const std::size_t constant_columns_6 = 3; constexpr static const std::size_t selector_columns_6 = 3; @@ -774,7 +800,7 @@ namespace nil { circuit_description, usable_rows_6> circuit_test_6( typename nil::crypto3::random::algebraic_engine alg_rnd = nil::crypto3::random::algebraic_engine(), boost::random::mt11213b rnd = boost::random::mt11213b() - ) { + ) { using assignment_type = typename FieldType::value_type; constexpr static const std::size_t witness_columns = witness_columns_6; @@ -794,8 +820,10 @@ namespace nil { } // lookup inputs - table[0] = {rnd() % 5 + 2, rnd() % 5 + 2, rnd() % 5 + 2, rnd() % 5 + 2, rnd() % 5 + 2, rnd() % 5 + 2}; - table[1] = {7, table[0][0] + table[0][1], table[0][1] + table[0][2], table[0][2] + table[0][3], table[0][3] + table[0][4], table[0][4] + table[0][5]}; + table[0] = {rnd() % 5 + 2, rnd() % 5 + 2, rnd() % 5 + 2, rnd() % 5 + 2, rnd() % 5 + 2, + rnd() % 5 + 2}; + table[1] = {7, table[0][0] + table[0][1], table[0][1] + table[0][2], table[0][2] + table[0][3], + table[0][3] + table[0][4], table[0][4] + table[0][5]}; // selectors @@ -805,8 +833,8 @@ namespace nil { table[4] = {0, 1, 1, 1, 1, 1}; // For the second lookup gate // Lookup values - table[5] = {0, 2, 3, 4, 5, 6}; // L1 - table[6] = {0, 7, 8, 9, 10, 11}; // L2 + table[5] = {0, 2, 3, 4, 5, 6}; // L1 + table[6] = {0, 7, 8, 9, 10, 11}; // L2 table[7] = {0, 12, 12, 12, 12, 12}; // L3 std::vector> private_assignment(witness_columns); @@ -826,18 +854,24 @@ namespace nil { constant_assignment[i] = table[witness_columns + selector_columns + i]; } test_circuit.table = plonk_assignment_table( - plonk_private_assignment_table(private_assignment), - plonk_public_assignment_table( - public_input_assignment, constant_assignment, selectors_assignment)); + plonk_private_assignment_table(private_assignment), + plonk_public_assignment_table( + public_input_assignment, constant_assignment, selectors_assignment)); test_circuit.table_rows = zk_padding(test_circuit.table, alg_rnd); - plonk_variable w0( 0, 0, true, plonk_variable::column_type::witness); - plonk_variable w0_1(0,-1, true, plonk_variable::column_type::witness); - plonk_variable w1( 1, 0, true, plonk_variable::column_type::witness); + plonk_variable w0(0, 0, true, + plonk_variable::column_type::witness); + plonk_variable w0_1(0, -1, true, + plonk_variable::column_type::witness); + plonk_variable w1(1, 0, true, + plonk_variable::column_type::witness); - plonk_variable c0(0, 0, true, plonk_variable::column_type::constant); - plonk_variable c1(1, 0, true, plonk_variable::column_type::constant); - plonk_variable c2(2, 0, true, plonk_variable::column_type::constant); + plonk_variable c0(0, 0, true, + plonk_variable::column_type::constant); + plonk_variable c1(1, 0, true, + plonk_variable::column_type::constant); + plonk_variable c2(2, 0, true, + plonk_variable::column_type::constant); plonk_lookup_constraint lookup_constraint1; lookup_constraint1.lookup_input.push_back(w0); @@ -847,7 +881,8 @@ namespace nil { lookup_constraint2.lookup_input.push_back(w1); lookup_constraint2.table_id = 2; - std::vector> lookup_constraints = {lookup_constraint1, lookup_constraint2}; + std::vector> lookup_constraints = {lookup_constraint1, + lookup_constraint2}; plonk_lookup_gate> lookup_gate(1, lookup_constraints); test_circuit.lookup_gates.push_back(lookup_gate); @@ -860,7 +895,8 @@ namespace nil { lookup_constraint3.table_id = 2; std::vector> lookup_constraints2 = {lookup_constraint3}; - plonk_lookup_gate> lookup_gate2(2, lookup_constraints2); + plonk_lookup_gate> lookup_gate2(2, + lookup_constraints2); test_circuit.lookup_gates.push_back(lookup_gate2); // Add constructor for lookup table @@ -905,7 +941,7 @@ namespace nil { // | 1 | 0 | 0 | 1 | 5 | 2^w1 | 0 | 1 | 4 | 16 | 7 | 128| 16 |1024|16384 // | 1 | 0 | 0 | 1 | 6 | 2^w1 | 0 | 1 | 5 | 32 | 7 | 128| 32 |2048|16384 // --------------------------------------------------------------------------------- - constexpr static const std::size_t witness_columns_7= 2; + constexpr static const std::size_t witness_columns_7 = 2; constexpr static const std::size_t public_columns_7 = 0; constexpr static const std::size_t constant_columns_7 = 7; constexpr static const std::size_t selector_columns_7 = 6; @@ -915,7 +951,7 @@ namespace nil { circuit_description, usable_rows_7> circuit_test_7( typename nil::crypto3::random::algebraic_engine alg_rnd = nil::crypto3::random::algebraic_engine(), boost::random::mt11213b rnd = boost::random::mt11213b() - ) { + ) { using assignment_type = typename FieldType::value_type; constexpr static const std::size_t witness_columns = witness_columns_7; @@ -938,36 +974,41 @@ namespace nil { auto r = rnd() % 7; table[0] = std::vector(16); std::size_t j = 0; - for( std::size_t i = 0; i < 7; i++){ - if( j == r ) j++; + for (std::size_t i = 0; i < 7; i++) { + if (j == r) j++; table[0][i] = j; table[1][i] = (typename FieldType::value_type(2)).pow(j); j++; } - for( std::size_t i = 7; i < 14; i++){ - table[0][i]=table[0][i-7]; - table[1][i]=table[1][i-7]; + for (std::size_t i = 7; i < 14; i++) { + table[0][i] = table[0][i - 7]; + table[1][i] = table[1][i - 7]; } // selectors // Reserved zero row for unselected lookup input rows std::vector> selectors_assignment(selector_columns); - selectors_assignment[0] = {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 }; // Selector for single gate - selectors_assignment[1] = {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 }; // Selector lookup gate with multiple rotations - selectors_assignment[2] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; // Selector for gate w1 = 2^w0 - selectors_assignment[3] = {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; // Selector for gate w1_{-1} * w1 \in Table 3 - selectors_assignment[4] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1 }; // Selector for lookup tables 2, 3 - selectors_assignment[5] = {0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 }; // Selector for lookup table with 7 columns + selectors_assignment[0] = {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1}; // Selector for single gate + selectors_assignment[1] = {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, + 0}; // Selector lookup gate with multiple rotations + selectors_assignment[2] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; // Selector for gate w1 = 2^w0 + selectors_assignment[3] = {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1}; // Selector for gate w1_{-1} * w1 \in Table 3 + selectors_assignment[4] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, + 1}; // Selector for lookup tables 2, 3 + selectors_assignment[5] = {0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, + 0}; // Selector for lookup table with 7 columns // Lookup values std::vector> constant_assignment(constant_columns); - constant_assignment[0] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5 }; // Lookup tables - constant_assignment[1] = {0, 2, 2, 1, 1, 1, 1, 1, 1, 2, 4, 8, 16, 32 }; // Lookup tables - constant_assignment[2] = {0, 3, 3, 3, 2, 2, 2, 2, 6, 7, 7, 7, 7, 7 }; // Lookup tables - constant_assignment[3] = {0, 4, 4, 4, 4, 3, 3, 3, 64, 128, 128, 128, 128, 128 }; // Lookup tables - constant_assignment[4] = {0, 5, 5, 5, 5, 5, 4, 4, 1, 2, 4, 8, 16, 32 }; // Lookup tables - constant_assignment[5] = {0, 6, 6, 6, 6, 6, 6, 5, 64, 128, 256, 512, 1024, 2048 }; // Lookup tables - constant_assignment[6] = {0, 7, 7, 7, 7, 7, 7, 7,4096,8192,16384,16384,16384,16384 }; // Lookup tables + constant_assignment[0] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5}; // Lookup tables + constant_assignment[1] = {0, 2, 2, 1, 1, 1, 1, 1, 1, 2, 4, 8, 16, 32}; // Lookup tables + constant_assignment[2] = {0, 3, 3, 3, 2, 2, 2, 2, 6, 7, 7, 7, 7, 7}; // Lookup tables + constant_assignment[3] = {0, 4, 4, 4, 4, 3, 3, 3, 64, 128, 128, 128, 128, 128}; // Lookup tables + constant_assignment[4] = {0, 5, 5, 5, 5, 5, 4, 4, 1, 2, 4, 8, 16, 32}; // Lookup tables + constant_assignment[5] = {0, 6, 6, 6, 6, 6, 6, 5, 64, 128, 256, 512, 1024, 2048}; // Lookup tables + constant_assignment[6] = {0, 7, 7, 7, 7, 7, 7, 7, 4096, 8192, 16384, 16384, 16384, + 16384}; // Lookup tables std::vector> private_assignment(witness_columns); for (std::size_t i = 0; i < witness_columns; i++) { @@ -977,37 +1018,52 @@ namespace nil { std::vector> public_input_assignment(public_columns); test_circuit.table = plonk_assignment_table( - plonk_private_assignment_table(private_assignment), - plonk_public_assignment_table( - public_input_assignment, constant_assignment, selectors_assignment)); + plonk_private_assignment_table(private_assignment), + plonk_public_assignment_table( + public_input_assignment, constant_assignment, selectors_assignment)); test_circuit.table_rows = zk_padding(test_circuit.table, alg_rnd); - plonk_variable w0( 0, 0, true, plonk_variable::column_type::witness); - plonk_variable w0__7(0,-7, true, plonk_variable::column_type::witness); + plonk_variable w0(0, 0, true, + plonk_variable::column_type::witness); + plonk_variable w0__7(0, -7, true, + plonk_variable::column_type::witness); plonk_constraint add_constraint; add_constraint += w0; add_constraint -= w0__7; - std::vector> add_gate_costraints {add_constraint}; + std::vector> add_gate_costraints{add_constraint}; plonk_gate> add_gate(0, add_gate_costraints); test_circuit.gates.push_back(add_gate); - plonk_variable w0__3( 0,-3, true, plonk_variable::column_type::witness); - plonk_variable w0__2( 0,-2, true, plonk_variable::column_type::witness); - plonk_variable w0__1( 0,-1, true, plonk_variable::column_type::witness); - plonk_variable w0_1 ( 0, 1, true, plonk_variable::column_type::witness); - plonk_variable w0_2 ( 0, 2, true, plonk_variable::column_type::witness); - plonk_variable w0_3 ( 0, 3, true, plonk_variable::column_type::witness); - - - plonk_variable c0 ( 0, 0, true, plonk_variable::column_type::constant); - plonk_variable c1 ( 1, 0, true, plonk_variable::column_type::constant); - plonk_variable c2 ( 2, 0, true, plonk_variable::column_type::constant); - plonk_variable c3 ( 3, 0, true, plonk_variable::column_type::constant); - plonk_variable c4 ( 4, 0, true, plonk_variable::column_type::constant); - plonk_variable c5 ( 5, 0, true, plonk_variable::column_type::constant); - plonk_variable c6 ( 6, 0, true, plonk_variable::column_type::constant); + plonk_variable w0__3(0, -3, true, + plonk_variable::column_type::witness); + plonk_variable w0__2(0, -2, true, + plonk_variable::column_type::witness); + plonk_variable w0__1(0, -1, true, + plonk_variable::column_type::witness); + plonk_variable w0_1(0, 1, true, + plonk_variable::column_type::witness); + plonk_variable w0_2(0, 2, true, + plonk_variable::column_type::witness); + plonk_variable w0_3(0, 3, true, + plonk_variable::column_type::witness); + + + plonk_variable c0(0, 0, true, + plonk_variable::column_type::constant); + plonk_variable c1(1, 0, true, + plonk_variable::column_type::constant); + plonk_variable c2(2, 0, true, + plonk_variable::column_type::constant); + plonk_variable c3(3, 0, true, + plonk_variable::column_type::constant); + plonk_variable c4(4, 0, true, + plonk_variable::column_type::constant); + plonk_variable c5(5, 0, true, + plonk_variable::column_type::constant); + plonk_variable c6(6, 0, true, + plonk_variable::column_type::constant); plonk_lookup_constraint lookup_constraint1; lookup_constraint1.lookup_input = {w0__3, w0__2, w0__1, w0, w0_1, w0_2, w0_3}; @@ -1017,24 +1073,28 @@ namespace nil { plonk_lookup_gate> lookup_gate(1, lookup_constraints); test_circuit.lookup_gates.push_back(lookup_gate); - plonk_variable w1( 1, 0, true, plonk_variable::column_type::witness); + plonk_variable w1(1, 0, true, + plonk_variable::column_type::witness); plonk_lookup_constraint lookup_constraint2; lookup_constraint2.lookup_input = {w0, w1}; lookup_constraint2.table_id = 2; std::vector> lookup_constraints2 = {lookup_constraint2}; - plonk_lookup_gate> lookup_gate2(2, lookup_constraints2); + plonk_lookup_gate> lookup_gate2(2, + lookup_constraints2); test_circuit.lookup_gates.push_back(lookup_gate2); - plonk_variable w1__1( 1, -1, true, plonk_variable::column_type::witness); + plonk_variable w1__1(1, -1, true, + plonk_variable::column_type::witness); plonk_lookup_constraint lookup_constraint3; typename plonk_constraint::term_type w1__1_term(w1__1); typename plonk_constraint::term_type w1_term(w1); - lookup_constraint3.lookup_input = {w1__1_term* w1_term}; + lookup_constraint3.lookup_input = {w1__1_term * w1_term}; lookup_constraint3.table_id = 3; std::vector> lookup_constraints3 = {lookup_constraint3}; - plonk_lookup_gate> lookup_gate3(3, lookup_constraints3); + plonk_lookup_gate> lookup_gate3(3, + lookup_constraints3); test_circuit.lookup_gates.push_back(lookup_gate3); plonk_lookup_table lookup_table(7, 5); // 5 -- selector_id, 7 -- number of columns; diff --git a/test/systems/plonk/placeholder/performance.cpp b/test/systems/plonk/placeholder/performance.cpp index 006e2d0e6..1a3d66bdd 100644 --- a/test/systems/plonk/placeholder/performance.cpp +++ b/test/systems/plonk/placeholder/performance.cpp @@ -24,6 +24,7 @@ //---------------------------------------------------------------------------// #include "nil/crypto3/zk/snark/arithmetization/plonk/table_description.hpp" + #define BOOST_TEST_MODULE placeholder_performance_test // NOTE: Most of the following code is taken from main.cpp of transpiler. @@ -62,7 +63,7 @@ #include #include #include -#include +#include "nil/crypto3/zk/snark/systems/plonk/placeholder.hpp" #include #include #include @@ -83,6 +84,8 @@ #include #include +#include + #include "circuits.hpp" using namespace nil::crypto3; @@ -90,7 +93,7 @@ using namespace nil::crypto3::zk; using namespace nil::crypto3::zk::snark; class placeholder_performance_test_base { - public: +public: static std::vector generate_random_step_list(const std::size_t r, const int max_step) { using dist_type = std::uniform_int_distribution; static std::random_device random_engine; @@ -133,13 +136,13 @@ class placeholder_performance_test : public placeholder_performance_test_base { static constexpr std::size_t TotalColumns = WitnessColumns + PublicInputColumns + ConstantColumns + SelectorColumns; using lpc_params_type = commitments::list_polynomial_commitment_params< - hash_type, hash_type, m>; + hash_type, hash_type, m>; using lpc_type = commitments::list_polynomial_commitment; using lpc_scheme_type = typename commitments::lpc_commitment_scheme; using circuit_params_type = placeholder_circuit_params; using lpc_placeholder_params_type = zk::snark::placeholder_params; - using policy_type = zk::snark::detail::placeholder_policy; + using policy_type = placeholder; using constraint_system_type = zk::snark::plonk_constraint_system; using table_description_type = zk::snark::plonk_table_description; @@ -156,9 +159,9 @@ class placeholder_performance_test : public placeholder_performance_test_base { using transcript_type = typename transcript::fiat_shamir_heuristic_sequential; void run_placeholder_perf_test(std::string test_name, std::string circuit_file_path, - std::string assignment_table_file_path) { + std::string assignment_table_file_path) { - std::cout << std::endl << "Running '" << test_name << "' performance test" << std::endl; + std::cout << std::endl << "Running '" << test_name << "' performance test" << std::endl; load_circuit(circuit_file_path); load_assignment_table(assignment_table_file_path); @@ -169,11 +172,11 @@ class placeholder_performance_test : public placeholder_performance_test_base { std::size_t table_rows_log = std::ceil(std::log2(table_description.rows_amount)); typename lpc_type::fri_type::params_type fri_params( - 1, table_rows_log, lambda, 7, true + 1, table_rows_log, lambda, 7, true ); std::size_t permutation_size = table_description.witness_columns + - table_description.public_input_columns + table_description.constant_columns; + table_description.public_input_columns + table_description.constant_columns; std::cout << "table_rows_log = " << table_rows_log << std::endl; @@ -183,20 +186,20 @@ class placeholder_performance_test : public placeholder_performance_test_base { auto lpc_preprocessed_public_data = placeholder_public_preprocessor::process( constraint_system, assignments.public_table(), table_description, lpc_scheme, permutation_size - ); + ); std::cout << "max_gates_degree = " << lpc_preprocessed_public_data.common_data.max_gates_degree << std::endl; auto lpc_preprocessed_private_data = placeholder_private_preprocessor::process( constraint_system, assignments.private_table(), table_description - ); + ); auto lpc_proof = placeholder_prover::process( - lpc_preprocessed_public_data, lpc_preprocessed_private_data, table_description, - constraint_system, lpc_scheme + lpc_preprocessed_public_data, lpc_preprocessed_private_data, table_description, + constraint_system, lpc_scheme ); bool verifier_res = placeholder_verifier::process( - lpc_preprocessed_public_data, lpc_proof, table_description, constraint_system, lpc_scheme + lpc_preprocessed_public_data.common_data, lpc_proof, table_description, constraint_system, lpc_scheme ); BOOST_CHECK(verifier_res); @@ -247,7 +250,7 @@ class placeholder_performance_test : public placeholder_performance_test_base { auto status = marshalled_data.read(read_iter, v.size()); BOOST_ASSERT(status == nil::marshalling::status_type::success); std::tie(table_description, assignments) = - marshalling::types::make_assignment_table(marshalled_data); + marshalling::types::make_assignment_table(marshalled_data); } void load_circuit(std::string circuit_file_path) { @@ -264,21 +267,21 @@ class placeholder_performance_test : public placeholder_performance_test_base { auto status = marshalled_data.read(read_iter, v.size()); BOOST_ASSERT(status == nil::marshalling::status_type::success); constraint_system = marshalling::types::make_plonk_constraint_system< - Endianness, constraint_system_type>(marshalled_data); + Endianness, constraint_system_type>(marshalled_data); } void compute_columns_rotations() { using variable_type = typename zk::snark::plonk_variable; - for (const auto& gate: constraint_system.gates()) { - for (const auto& constraint: gate.constraints) { + for (const auto &gate: constraint_system.gates()) { + for (const auto &constraint: gate.constraints) { math::expression_for_each_variable_visitor visitor( - [this](const variable_type& var) { - if (var.relative) { - std::size_t column_index = this->table_description.global_index(var); - this->columns_rotations[column_index].insert(var.rotation); - } - }); + [this](const variable_type &var) { + if (var.relative) { + std::size_t column_index = this->table_description.global_index(var); + this->columns_rotations[column_index].insert(var.rotation); + } + }); visitor.visit(constraint); } } @@ -303,119 +306,116 @@ class placeholder_performance_test : public placeholder_performance_test_base { constraint_system_type constraint_system; table_description_type table_description = table_description_type( - WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns); + WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns); assignment_table_type assignments = assignment_table_type( - WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns); + WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns); columns_rotations_type columns_rotations = columns_rotations_type(TotalColumns); }; BOOST_AUTO_TEST_SUITE(placeholder_transpiler_suite, *boost::unit_test::disabled()) -BOOST_FIXTURE_TEST_CASE(placeholder_merkle_tree_poseidon_test, placeholder_performance_test<2>) { + BOOST_FIXTURE_TEST_CASE(placeholder_merkle_tree_poseidon_test, placeholder_performance_test<2>) { - run_placeholder_perf_test( - "Merkle tree poseidon performance test", - "../libs/zk/test/systems/plonk/placeholder/data/merkle_tree_poseidon/merkle_tree_posseidon_circuit.crct", - "../libs/zk/test/systems/plonk/placeholder/data/merkle_tree_poseidon/merkle_tree_posseidon_assignment.tbl" - ); -} + run_placeholder_perf_test( + "Merkle tree poseidon performance test", + "../libs/zk/test/systems/plonk/placeholder/data/merkle_tree_poseidon/merkle_tree_posseidon_circuit.crct", + "../libs/zk/test/systems/plonk/placeholder/data/merkle_tree_poseidon/merkle_tree_posseidon_assignment.tbl" + ); + } -BOOST_FIXTURE_TEST_CASE(placeholder_many_hashes_test, placeholder_performance_test<2>) { - run_placeholder_perf_test( - "Many hashes performance test", - "../libs/zk/test/systems/plonk/placeholder/data/many_hashes/many_hashes_circuit.crct", - "../libs/zk/test/systems/plonk/placeholder/data/many_hashes/many_hashes_assignment.tbl" - ); -} + BOOST_FIXTURE_TEST_CASE(placeholder_many_hashes_test, placeholder_performance_test<2>) { + run_placeholder_perf_test( + "Many hashes performance test", + "../libs/zk/test/systems/plonk/placeholder/data/many_hashes/many_hashes_circuit.crct", + "../libs/zk/test/systems/plonk/placeholder/data/many_hashes/many_hashes_assignment.tbl" + ); + } BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE(placeholder_prover_test_suite) // using curve_type = algebra::curves::bls12<381>; -using curve_type = algebra::curves::pallas; -using field_type = typename curve_type::base_field_type; + using curve_type = algebra::curves::pallas; + using field_type = typename curve_type::base_field_type; -struct placeholder_fibonacci_params { - using merkle_hash_type = hashes::keccak_1600<512>; - using transcript_hash_type = hashes::sha2<256>; + struct placeholder_fibonacci_params { + using merkle_hash_type = hashes::keccak_1600<512>; + using transcript_hash_type = hashes::sha2<256>; - constexpr static const std::size_t witness_columns = 1; - constexpr static const std::size_t public_input_columns = 1; - constexpr static const std::size_t constant_columns = 0; - constexpr static const std::size_t selector_columns = 1; + constexpr static const std::size_t witness_columns = 1; + constexpr static const std::size_t public_input_columns = 1; + constexpr static const std::size_t constant_columns = 0; + constexpr static const std::size_t selector_columns = 1; - constexpr static const std::size_t lambda = 1; - constexpr static const std::size_t m = 2; -}; + constexpr static const std::size_t lambda = 1; + constexpr static const std::size_t m = 2; + }; -using circuit_fib_params = placeholder_circuit_params; + using circuit_fib_params = placeholder_circuit_params; -constexpr static const std::size_t table_columns = - placeholder_fibonacci_params::witness_columns + placeholder_fibonacci_params::public_input_columns; + constexpr static const std::size_t table_columns = + placeholder_fibonacci_params::witness_columns + placeholder_fibonacci_params::public_input_columns; -using transcript_type = typename transcript::fiat_shamir_heuristic_sequential; + using transcript_type = typename transcript::fiat_shamir_heuristic_sequential; -using lpc_params_type = commitments::list_polynomial_commitment_params< - typename placeholder_fibonacci_params::merkle_hash_type, - typename placeholder_fibonacci_params::transcript_hash_type, - placeholder_fibonacci_params::m ->; + using lpc_params_type = commitments::list_polynomial_commitment_params< + typename placeholder_fibonacci_params::merkle_hash_type, + typename placeholder_fibonacci_params::transcript_hash_type, + placeholder_fibonacci_params::m + >; -using lpc_type = commitments::list_polynomial_commitment; -using lpc_scheme_type = typename commitments::lpc_commitment_scheme; -using lpc_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params; -using policy_type = zk::snark::detail::placeholder_policy; + using lpc_type = commitments::list_polynomial_commitment; + using lpc_scheme_type = typename commitments::lpc_commitment_scheme; + using lpc_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params; + using policy_type = placeholder; -const std::size_t rows_amount = 987; + const std::size_t rows_amount = 987; -BOOST_FIXTURE_TEST_CASE(placeholder_large_fibonacci_test, placeholder_performance_test_base) { - auto circuit = circuit_test_fib(); + BOOST_FIXTURE_TEST_CASE(placeholder_large_fibonacci_test, placeholder_performance_test_base) { + auto circuit = circuit_test_fib(); - plonk_table_description desc( - placeholder_fibonacci_params::witness_columns, - placeholder_fibonacci_params::public_input_columns, - placeholder_fibonacci_params::constant_columns, - placeholder_fibonacci_params::selector_columns - ); + plonk_table_description desc( + placeholder_fibonacci_params::witness_columns, + placeholder_fibonacci_params::public_input_columns, + placeholder_fibonacci_params::constant_columns, + placeholder_fibonacci_params::selector_columns + ); - desc.rows_amount = circuit.table_rows; - desc.usable_rows_amount = circuit.usable_rows; - std::size_t table_rows_log = std::log2(desc.rows_amount); + desc.rows_amount = circuit.table_rows; + desc.usable_rows_amount = circuit.usable_rows; + std::size_t table_rows_log = std::log2(desc.rows_amount); - typename lpc_type::fri_type::params_type fri_params( - 1, table_rows_log, placeholder_fibonacci_params::lambda, 7, true - ); + typename lpc_type::fri_type::params_type fri_params( + 1, table_rows_log, placeholder_fibonacci_params::lambda, 7, true + ); - typename policy_type::constraint_system_type constraint_system( - circuit.gates, circuit.copy_constraints, circuit.lookup_gates); - typename policy_type::variable_assignment_type assignments = circuit.table; + typename policy_type::constraint_system_type constraint_system( + circuit.gates, circuit.copy_constraints, circuit.lookup_gates); + typename policy_type::variable_assignment_type assignments = circuit.table; - std::vector columns_with_copy_constraints = {0, 1}; + std::vector columns_with_copy_constraints = {0, 1}; - lpc_scheme_type lpc_scheme(fri_params); + lpc_scheme_type lpc_scheme(fri_params); - typename placeholder_public_preprocessor::preprocessed_data_type - lpc_preprocessed_public_data = placeholder_public_preprocessor::process( - constraint_system, assignments.public_table(), desc, lpc_scheme, columns_with_copy_constraints.size() + typename placeholder_public_preprocessor::preprocessed_data_type + lpc_preprocessed_public_data = placeholder_public_preprocessor::process( + constraint_system, assignments.public_table(), desc, lpc_scheme, columns_with_copy_constraints.size() ); - typename placeholder_private_preprocessor::preprocessed_data_type - lpc_preprocessed_private_data = placeholder_private_preprocessor::process( - constraint_system, assignments.private_table(), desc - ); + typename placeholder_private_preprocessor::preprocessed_data_type + lpc_preprocessed_private_data = placeholder_private_preprocessor::process( + constraint_system, assignments.private_table(), desc); - auto lpc_proof = placeholder_prover::process( - lpc_preprocessed_public_data, lpc_preprocessed_private_data, desc, - constraint_system, lpc_scheme - ); - - bool verifier_res = placeholder_verifier::process( - lpc_preprocessed_public_data, lpc_proof, desc, constraint_system, lpc_scheme - ); - BOOST_CHECK(verifier_res); - std::cout << "==========================================================="<::process( + lpc_preprocessed_public_data, lpc_preprocessed_private_data, desc, + constraint_system, lpc_scheme); + + bool verifier_res = placeholder_verifier::process( + lpc_preprocessed_public_data.common_data, lpc_proof, desc, constraint_system, lpc_scheme); + BOOST_CHECK(verifier_res); + std::cout << "===========================================================" << std::endl; + } BOOST_AUTO_TEST_SUITE_END() diff --git a/test/systems/plonk/placeholder/placeholder.cpp b/test/systems/plonk/placeholder/placeholder.cpp index ae6b648de..609e1855d 100644 --- a/test/systems/plonk/placeholder/placeholder.cpp +++ b/test/systems/plonk/placeholder/placeholder.cpp @@ -80,7 +80,7 @@ #include #include -#include +#include "nil/crypto3/zk/snark/systems/plonk/placeholder.hpp" #include #include #include @@ -97,7 +97,8 @@ using namespace nil::crypto3::zk; using namespace nil::crypto3::zk::snark; template -class dummy_commitment_scheme_type : public nil::crypto3::zk::commitments::polys_evaluator { +class dummy_commitment_scheme_type + : public nil::crypto3::zk::commitments::polys_evaluator { private: public: using params_type = CommitmentSchemeParamsType; @@ -111,30 +112,30 @@ class dummy_commitment_scheme_type : public nil::crypto3::zk::commitments::polys return params_type(); } - struct proof_type{ + struct proof_type { nil::crypto3::zk::commitments::eval_storage z; }; - preprocessed_data_type preprocess(transcript_type &preprocessed_transript) const{ + preprocessed_data_type preprocess(transcript_type &preprocessed_transript) const { return true; } - void setup(transcript_type &preprocessed_transript, preprocessed_data_type prep = true){ + void setup(transcript_type &preprocessed_transript, preprocessed_data_type prep = true) { } - void mark_batch_as_fixed(std::size_t batch_id){ + void mark_batch_as_fixed(std::size_t batch_id) { } proof_type proof_eval( - transcript_type &transcript - ){ + transcript_type &transcript + ) { this->eval_polys(); return proof_type({this->_z}); } commitment_type commit( - std::size_t index - ){ + std::size_t index + ) { this->state_commited(index); std::vector arr = {std::uint8_t(index)}; @@ -142,9 +143,9 @@ class dummy_commitment_scheme_type : public nil::crypto3::zk::commitments::polys } bool verify_eval( - const proof_type &proof, - const std::map &commitments, - transcript_type &transcript + const proof_type &proof, + const std::map &commitments, + transcript_type &transcript ) const { return true; } @@ -175,7 +176,7 @@ inline std::vector generate_random_step_list(const std::size_t r, c template typename kzg_type::params_type create_kzg_params(std::size_t degree_log) { // TODO: what cases t != d? - typename kzg_type::field_type::value_type alpha (7); + typename kzg_type::field_type::value_type alpha(7); std::size_t d = 1 << degree_log; typename kzg_type::params_type params(d, d, alpha); @@ -185,7 +186,7 @@ typename kzg_type::params_type create_kzg_params(std::size_t degree_log) { template typename kzg_type::params_type create_kzg_v2_params(std::size_t degree_log) { // TODO: what cases t != d? - typename kzg_type::field_type::value_type alpha (7); + typename kzg_type::field_type::value_type alpha(7); std::size_t d = 1 << degree_log; typename kzg_type::params_type params(d, 1, alpha); @@ -242,8 +243,8 @@ struct test_initializer { }; template + std::size_t WitnessColumns, std::size_t PublicInputColumns, std::size_t ConstantColumns, std::size_t SelectorColumns, + std::size_t usable_rows_amount, bool UseGrinding = false, std::size_t max_quotient_poly_chunks = 0> struct placeholder_test_fixture : public test_initializer { using field_type = FieldType; @@ -263,25 +264,22 @@ struct placeholder_test_fixture : public test_initializer { using transcript_type = typename transcript::fiat_shamir_heuristic_sequential; using lpc_params_type = commitments::list_polynomial_commitment_params< - merkle_hash_type, - transcript_hash_type, - placeholder_test_params::m + merkle_hash_type, + transcript_hash_type, + placeholder_test_params::m >; using lpc_type = commitments::list_polynomial_commitment; using lpc_scheme_type = typename commitments::lpc_commitment_scheme; using lpc_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params; - using policy_type = zk::snark::detail::placeholder_policy; + using policy_type = placeholder; using circuit_type = circuit_description, usable_rows_amount>; - placeholder_test_fixture(const circuit_type& circuit_in, std::size_t usable_rows, std::size_t table_rows) - : circuit(circuit_in) - , desc(WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns) - , constraint_system(circuit.gates, circuit.copy_constraints, circuit.lookup_gates, circuit.lookup_tables) - , assignments(circuit.table) - , table_rows_log(std::log2(table_rows)) - , fri_params(1,table_rows_log, placeholder_test_params::lambda, 4) - { + placeholder_test_fixture(const circuit_type &circuit_in, std::size_t usable_rows, std::size_t table_rows) + : circuit(circuit_in), desc(WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns), + constraint_system(circuit.gates, circuit.copy_constraints, circuit.lookup_gates, circuit.lookup_tables), + assignments(circuit.table), table_rows_log(std::log2(table_rows)), + fri_params(1, table_rows_log, placeholder_test_params::lambda, 4) { desc.rows_amount = table_rows; desc.usable_rows_amount = usable_rows; } @@ -291,22 +289,19 @@ struct placeholder_test_fixture : public test_initializer { lpc_scheme_type lpc_scheme(fri_params); typename placeholder_public_preprocessor::preprocessed_data_type - lpc_preprocessed_public_data = placeholder_public_preprocessor::process( - constraint_system, assignments.move_public_table(), desc, lpc_scheme, max_quotient_poly_chunks - ); + lpc_preprocessed_public_data = placeholder_public_preprocessor::process( + constraint_system, assignments.move_public_table(), desc, lpc_scheme, max_quotient_poly_chunks); typename placeholder_private_preprocessor::preprocessed_data_type - lpc_preprocessed_private_data = placeholder_private_preprocessor::process( - constraint_system, assignments.move_private_table(), desc - ); + lpc_preprocessed_private_data = placeholder_private_preprocessor::process( + constraint_system, assignments.move_private_table(), desc); auto lpc_proof = placeholder_prover::process( - lpc_preprocessed_public_data, std::move(lpc_preprocessed_private_data), desc, constraint_system, lpc_scheme - ); + lpc_preprocessed_public_data, std::move(lpc_preprocessed_private_data), desc, constraint_system, + lpc_scheme); bool verifier_res = placeholder_verifier::process( - lpc_preprocessed_public_data.common_data, lpc_proof, desc, constraint_system, lpc_scheme - ); + lpc_preprocessed_public_data.common_data, lpc_proof, desc, constraint_system, lpc_scheme); test_initializer::teardown(); return verifier_res; } @@ -320,7 +315,6 @@ struct placeholder_test_fixture : public test_initializer { }; - BOOST_AUTO_TEST_SUITE(placeholder_circuit2) using curve_type = algebra::curves::bls12<381>; using field_type = typename curve_type::scalar_field_type; @@ -344,12 +338,12 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit2) using commitment_scheme_params_type = nil::crypto3::zk::commitments::commitment_scheme_params_type>; using commitment_scheme_dummy_type = dummy_commitment_scheme_type; using placeholder_params_type = nil::crypto3::zk::snark::placeholder_params; - using policy_type = zk::snark::detail::placeholder_policy; + using policy_type = placeholder; using lpc_params_type = commitments::list_polynomial_commitment_params< - typename placeholder_test_params::merkle_hash_type, - typename placeholder_test_params::transcript_hash_type, - placeholder_test_params::m + typename placeholder_test_params::merkle_hash_type, + typename placeholder_test_params::transcript_hash_type, + placeholder_test_params::m >; using lpc_type = commitments::list_polynomial_commitment; @@ -360,430 +354,445 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit2) using kzg_scheme_type = typename commitments::kzg_commitment_scheme; using kzg_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params; -BOOST_FIXTURE_TEST_CASE(prover_test, test_initializer){ - typename field_type::value_type pi0 = test_global_alg_rnd_engine(); - auto circuit = circuit_test_t(pi0, test_global_alg_rnd_engine); - - plonk_table_description desc( - placeholder_test_params::witness_columns, - placeholder_test_params::public_input_columns, - placeholder_test_params::constant_columns, - placeholder_test_params::selector_columns - ); - desc.rows_amount = circuit.table_rows; - desc.usable_rows_amount = circuit.usable_rows; - std::size_t table_rows_log = std::log2(desc.rows_amount); - - typename policy_type::constraint_system_type constraint_system( - circuit.gates, circuit.copy_constraints, - circuit.lookup_gates, circuit.lookup_tables, - circuit.public_input_sizes - ); - typename policy_type::variable_assignment_type assignments = circuit.table; - - bool verifier_res; - - // Dummy commitment scheme - commitment_scheme_dummy_type commitment_scheme; - - typename placeholder_public_preprocessor::preprocessed_data_type - preprocessed_public_data = placeholder_public_preprocessor::process( - constraint_system, assignments.public_table(), desc, commitment_scheme - ); + BOOST_FIXTURE_TEST_CASE(prover_test, test_initializer) { + typename field_type::value_type pi0 = test_global_alg_rnd_engine(); + auto circuit = circuit_test_t(pi0, test_global_alg_rnd_engine); - typename placeholder_private_preprocessor::preprocessed_data_type - preprocessed_private_data = placeholder_private_preprocessor::process( - constraint_system, assignments.private_table(), desc + plonk_table_description desc( + placeholder_test_params::witness_columns, + placeholder_test_params::public_input_columns, + placeholder_test_params::constant_columns, + placeholder_test_params::selector_columns ); + desc.rows_amount = circuit.table_rows; + desc.usable_rows_amount = circuit.usable_rows; + std::size_t table_rows_log = std::log2(desc.rows_amount); - auto proof = placeholder_prover::process( - preprocessed_public_data, std::move(preprocessed_private_data), desc, constraint_system, commitment_scheme - ); - - verifier_res = placeholder_verifier::process( - preprocessed_public_data.common_data, proof, desc, constraint_system, commitment_scheme - ); - BOOST_CHECK(verifier_res); - - // Public inputs checks - // Completely correct public input - typename placeholder_params_type::public_input_type public_input(1); - public_input[0] = {pi0, 0, 1}; - verifier_res = placeholder_verifier::process( - preprocessed_public_data.common_data, proof, desc, constraint_system, commitment_scheme, public_input - ); - BOOST_CHECK(verifier_res); - - // Completely correct zeroes after it are not important - public_input[0] = {pi0, 0, 1, 0}; - verifier_res = placeholder_verifier::process( - preprocessed_public_data.common_data, proof, desc, constraint_system, commitment_scheme, public_input - ); - BOOST_CHECK(verifier_res); - - // Incorrect public input - public_input[0] = {pi0, 1}; - verifier_res = placeholder_verifier::process( - preprocessed_public_data.common_data, proof, desc, constraint_system, commitment_scheme, public_input - ); - BOOST_CHECK(!verifier_res); - - // LPC commitment scheme - typename lpc_type::fri_type::params_type fri_params( - 1, table_rows_log, placeholder_test_params::lambda, 4 - ); - lpc_scheme_type lpc_scheme(fri_params); - transcript_type lpc_transcript; - - // Normally we would use "assignments.move_public_table()" here, but assignments are reused in this test. - typename placeholder_public_preprocessor::preprocessed_data_type - lpc_preprocessed_public_data = placeholder_public_preprocessor::process( - constraint_system, assignments.public_table(), desc, lpc_scheme + typename policy_type::constraint_system_type constraint_system( + circuit.gates, circuit.copy_constraints, + circuit.lookup_gates, circuit.lookup_tables, + circuit.public_input_sizes ); + typename policy_type::variable_assignment_type assignments = circuit.table; - // Normally we would use "assignments.move_private_table()" here, but assignments are reused in this test. - typename placeholder_private_preprocessor::preprocessed_data_type - lpc_preprocessed_private_data = placeholder_private_preprocessor::process( - constraint_system, assignments.private_table(), desc + bool verifier_res; + + // Dummy commitment scheme + commitment_scheme_dummy_type commitment_scheme; + + typename placeholder_public_preprocessor::preprocessed_data_type + preprocessed_public_data = placeholder_public_preprocessor::process( + constraint_system, assignments.public_table(), desc, commitment_scheme ); - auto lpc_proof = placeholder_prover::process( - lpc_preprocessed_public_data, std::move(lpc_preprocessed_private_data), desc, constraint_system, lpc_scheme - ); + typename placeholder_private_preprocessor::preprocessed_data_type + preprocessed_private_data = placeholder_private_preprocessor::process( + constraint_system, assignments.private_table(), desc); - verifier_res = placeholder_verifier::process( - lpc_preprocessed_public_data.common_data, lpc_proof, desc, constraint_system, lpc_scheme - ); - BOOST_CHECK(verifier_res); + auto proof = placeholder_prover::process( + preprocessed_public_data, std::move(preprocessed_private_data), desc, constraint_system, + commitment_scheme); - // KZG commitment scheme - auto kzg_params = create_kzg_params(table_rows_log); - kzg_scheme_type kzg_scheme(kzg_params); + verifier_res = placeholder_verifier::process( + preprocessed_public_data.common_data, proof, desc, constraint_system, commitment_scheme); + BOOST_CHECK(verifier_res); - typename placeholder_public_preprocessor::preprocessed_data_type - kzg_preprocessed_public_data = placeholder_public_preprocessor::process( - constraint_system, assignments.public_table(), desc, kzg_scheme + // Public inputs checks + // Completely correct public input + typename placeholder_params_type::public_input_type public_input(1); + public_input[0] = {pi0, 0, 1}; + verifier_res = placeholder_verifier::process( + preprocessed_public_data.common_data, proof, desc, constraint_system, commitment_scheme, public_input ); + BOOST_CHECK(verifier_res); - typename placeholder_private_preprocessor::preprocessed_data_type - kzg_preprocessed_private_data = placeholder_private_preprocessor::process( - constraint_system, assignments.private_table(), desc + // Completely correct zeroes after it are not important + public_input[0] = {pi0, 0, 1, 0}; + verifier_res = placeholder_verifier::process( + preprocessed_public_data.common_data, proof, desc, constraint_system, commitment_scheme, public_input ); + BOOST_CHECK(verifier_res); - auto kzg_proof = placeholder_prover::process( - kzg_preprocessed_public_data, std::move(kzg_preprocessed_private_data), desc, constraint_system, kzg_scheme - ); + // Incorrect public input + public_input[0] = {pi0, 1}; + verifier_res = placeholder_verifier::process( + preprocessed_public_data.common_data, proof, desc, constraint_system, commitment_scheme, public_input + ); + BOOST_CHECK(!verifier_res); - verifier_res = placeholder_verifier::process( - kzg_preprocessed_public_data.common_data, kzg_proof, desc, constraint_system, kzg_scheme - ); - BOOST_CHECK(verifier_res); -} + // LPC commitment scheme + typename lpc_type::fri_type::params_type fri_params( + 1, table_rows_log, placeholder_test_params::lambda, 4 + ); + lpc_scheme_type lpc_scheme(fri_params); + transcript_type lpc_transcript; -BOOST_AUTO_TEST_CASE(permutation_polynomials_test) { - typename field_type::value_type pi0 = test_global_alg_rnd_engine(); - auto circuit = circuit_test_t(pi0, test_global_alg_rnd_engine); - - plonk_table_description desc( - placeholder_test_params::witness_columns, - placeholder_test_params::public_input_columns, - placeholder_test_params::constant_columns, - placeholder_test_params::selector_columns - ); - desc.rows_amount = circuit.table_rows; - desc.usable_rows_amount = circuit.usable_rows; - std::size_t table_rows_log = std::log2(desc.rows_amount); - - typename policy_type::constraint_system_type constraint_system(circuit.gates, circuit.copy_constraints, - circuit.lookup_gates); - typename policy_type::variable_assignment_type assignments = circuit.table; - - typename lpc_type::fri_type::params_type fri_params(1,table_rows_log, placeholder_test_params::lambda, 4); - lpc_scheme_type lpc_scheme(fri_params); - transcript_type lpc_transcript; - - typename placeholder_public_preprocessor::preprocessed_data_type - lpc_preprocessed_public_data = placeholder_public_preprocessor::process( - constraint_system, assignments.move_public_table(), desc, lpc_scheme + // Normally we would use "assignments.move_public_table()" here, but assignments are reused in this test. + typename placeholder_public_preprocessor::preprocessed_data_type + lpc_preprocessed_public_data = placeholder_public_preprocessor::process( + constraint_system, assignments.public_table(), desc, lpc_scheme ); - typename placeholder_private_preprocessor::preprocessed_data_type - lpc_preprocessed_private_data = placeholder_private_preprocessor::process( - constraint_system, assignments.move_private_table(), desc + // Normally we would use "assignments.move_private_table()" here, but assignments are reused in this test. + typename placeholder_private_preprocessor::preprocessed_data_type + lpc_preprocessed_private_data = placeholder_private_preprocessor::process( + constraint_system, assignments.private_table(), desc ); - auto polynomial_table = - plonk_polynomial_dfs_table( - lpc_preprocessed_private_data.private_polynomial_table, lpc_preprocessed_public_data.public_polynomial_table); + auto lpc_proof = placeholder_prover::process( + lpc_preprocessed_public_data, std::move(lpc_preprocessed_private_data), desc, constraint_system, + lpc_scheme + ); - std::shared_ptr> domain = lpc_preprocessed_public_data.common_data.basic_domain; - typename field_type::value_type id_res = field_type::value_type::one(); - typename field_type::value_type sigma_res = field_type::value_type::one(); - for (std::size_t i = 0; i < desc.rows_amount; i++) { - for (auto &identity_polynomial : lpc_preprocessed_public_data.identity_polynomials) { - id_res = id_res * identity_polynomial.evaluate(domain->get_domain_element(i)); - } + verifier_res = placeholder_verifier::process( + lpc_preprocessed_public_data.common_data, lpc_proof, desc, constraint_system, lpc_scheme + ); + BOOST_CHECK(verifier_res); - for (auto &permutation_polynomial : lpc_preprocessed_public_data.permutation_polynomials) { - sigma_res = sigma_res * permutation_polynomial.evaluate(domain->get_domain_element(i)); - } + // KZG commitment scheme + auto kzg_params = create_kzg_params(table_rows_log); + kzg_scheme_type kzg_scheme(kzg_params); + + typename placeholder_public_preprocessor::preprocessed_data_type + kzg_preprocessed_public_data = placeholder_public_preprocessor::process( + constraint_system, assignments.public_table(), desc, kzg_scheme + ); + + typename placeholder_private_preprocessor::preprocessed_data_type + kzg_preprocessed_private_data = placeholder_private_preprocessor::process( + constraint_system, assignments.private_table(), desc + ); + + auto kzg_proof = placeholder_prover::process( + kzg_preprocessed_public_data, std::move(kzg_preprocessed_private_data), desc, constraint_system, + kzg_scheme + ); + + verifier_res = placeholder_verifier::process( + kzg_preprocessed_public_data.common_data, kzg_proof, desc, constraint_system, kzg_scheme + ); + BOOST_CHECK(verifier_res); } - BOOST_CHECK_MESSAGE(id_res == sigma_res, "Simple check"); - typename field_type::value_type beta = algebra::random_element(); - typename field_type::value_type gamma = algebra::random_element(); + BOOST_AUTO_TEST_CASE(permutation_polynomials_test) { + typename field_type::value_type pi0 = test_global_alg_rnd_engine(); + auto circuit = circuit_test_t(pi0, test_global_alg_rnd_engine); + + plonk_table_description desc( + placeholder_test_params::witness_columns, + placeholder_test_params::public_input_columns, + placeholder_test_params::constant_columns, + placeholder_test_params::selector_columns + ); + desc.rows_amount = circuit.table_rows; + desc.usable_rows_amount = circuit.usable_rows; + std::size_t table_rows_log = std::log2(desc.rows_amount); + + typename policy_type::constraint_system_type constraint_system(circuit.gates, circuit.copy_constraints, + circuit.lookup_gates); + typename policy_type::variable_assignment_type assignments = circuit.table; + + typename lpc_type::fri_type::params_type fri_params(1, table_rows_log, placeholder_test_params::lambda, 4); + lpc_scheme_type lpc_scheme(fri_params); + transcript_type lpc_transcript; + + typename placeholder_public_preprocessor::preprocessed_data_type + lpc_preprocessed_public_data = placeholder_public_preprocessor::process( + constraint_system, assignments.move_public_table(), desc, lpc_scheme + ); + + typename placeholder_private_preprocessor::preprocessed_data_type + lpc_preprocessed_private_data = placeholder_private_preprocessor::process( + constraint_system, assignments.move_private_table(), desc + ); - id_res = field_type::value_type::one(); - sigma_res = field_type::value_type::one(); - const auto &permuted_columns = lpc_preprocessed_public_data.common_data.permuted_columns; + auto polynomial_table = + plonk_polynomial_dfs_table( + lpc_preprocessed_private_data.private_polynomial_table, + lpc_preprocessed_public_data.public_polynomial_table); + + std::shared_ptr> domain = lpc_preprocessed_public_data.common_data.basic_domain; + typename field_type::value_type id_res = field_type::value_type::one(); + typename field_type::value_type sigma_res = field_type::value_type::one(); + for (std::size_t i = 0; i < desc.rows_amount; i++) { + for (auto &identity_polynomial: lpc_preprocessed_public_data.identity_polynomials) { + id_res = id_res * identity_polynomial.evaluate(domain->get_domain_element(i)); + } - for (std::size_t i = 0; i < desc.rows_amount; i++) { - for (std::size_t j = 0; j < lpc_preprocessed_public_data.identity_polynomials.size(); j++) { - id_res = id_res * - (polynomial_table[permuted_columns[j]].evaluate(domain->get_domain_element(i)) + - beta * lpc_preprocessed_public_data.identity_polynomials[j].evaluate(domain->get_domain_element(i)) + - gamma); + for (auto &permutation_polynomial: lpc_preprocessed_public_data.permutation_polynomials) { + sigma_res = sigma_res * permutation_polynomial.evaluate(domain->get_domain_element(i)); + } } + BOOST_CHECK_MESSAGE(id_res == sigma_res, "Simple check"); + + typename field_type::value_type beta = algebra::random_element(); + typename field_type::value_type gamma = algebra::random_element(); + + id_res = field_type::value_type::one(); + sigma_res = field_type::value_type::one(); + const auto &permuted_columns = lpc_preprocessed_public_data.common_data.permuted_columns; + + for (std::size_t i = 0; i < desc.rows_amount; i++) { + for (std::size_t j = 0; j < lpc_preprocessed_public_data.identity_polynomials.size(); j++) { + id_res = id_res * + (polynomial_table[permuted_columns[j]].evaluate(domain->get_domain_element(i)) + + beta * + lpc_preprocessed_public_data.identity_polynomials[j].evaluate(domain->get_domain_element(i)) + + gamma); + } - for (std::size_t j = 0; j < lpc_preprocessed_public_data.permutation_polynomials.size(); j++) { - sigma_res = - sigma_res * - (polynomial_table[permuted_columns[j]].evaluate(domain->get_domain_element(i)) + - beta * lpc_preprocessed_public_data.permutation_polynomials[j].evaluate(domain->get_domain_element(i)) + - gamma); + for (std::size_t j = 0; j < lpc_preprocessed_public_data.permutation_polynomials.size(); j++) { + sigma_res = + sigma_res * + (polynomial_table[permuted_columns[j]].evaluate(domain->get_domain_element(i)) + + beta * lpc_preprocessed_public_data.permutation_polynomials[j].evaluate( + domain->get_domain_element(i)) + + gamma); + } } + BOOST_CHECK_MESSAGE(id_res == sigma_res, "Complex check"); } - BOOST_CHECK_MESSAGE(id_res == sigma_res, "Complex check"); -} -BOOST_AUTO_TEST_CASE(placeholder_split_polynomial_test) { - math::polynomial f = {1, 3, 4, 1, 5, 6, 7, 2, 8, 7, 5, 6, 1, 2, 1, 1}; - std::size_t expected_size = 4; - std::size_t max_degree = 3; + BOOST_AUTO_TEST_CASE(placeholder_split_polynomial_test) { + math::polynomial f = {1, 3, 4, 1, 5, 6, 7, 2, 8, 7, 5, 6, 1, 2, 1, 1}; + std::size_t expected_size = 4; + std::size_t max_degree = 3; - std::vector> f_splitted = - zk::snark::detail::split_polynomial(f, max_degree); + std::vector> f_splitted = + zk::snark::detail::split_polynomial(f, max_degree); - BOOST_CHECK(f_splitted.size() == expected_size); + BOOST_CHECK(f_splitted.size() == expected_size); - typename field_type::value_type y = algebra::random_element(); + typename field_type::value_type y = algebra::random_element(); - typename field_type::value_type f_at_y = f.evaluate(y); - typename field_type::value_type f_splitted_at_y = field_type::value_type::zero(); - for (std::size_t i = 0; i < f_splitted.size(); i++) { - f_splitted_at_y = f_splitted_at_y + f_splitted[i].evaluate(y) * y.pow((max_degree + 1) * i); - } + typename field_type::value_type f_at_y = f.evaluate(y); + typename field_type::value_type f_splitted_at_y = field_type::value_type::zero(); + for (std::size_t i = 0; i < f_splitted.size(); i++) { + f_splitted_at_y = f_splitted_at_y + f_splitted[i].evaluate(y) * y.pow((max_degree + 1) * i); + } - BOOST_CHECK(f_at_y == f_splitted_at_y); -} + BOOST_CHECK(f_at_y == f_splitted_at_y); + } -BOOST_AUTO_TEST_CASE(permutation_argument_test) { - auto circuit = circuit_test_t(); + BOOST_AUTO_TEST_CASE(permutation_argument_test) { + auto circuit = circuit_test_t(); - plonk_table_description desc( - placeholder_test_params::witness_columns, - placeholder_test_params::public_input_columns, - placeholder_test_params::constant_columns, - placeholder_test_params::selector_columns - ); - desc.rows_amount = circuit.table_rows; - desc.usable_rows_amount = circuit.usable_rows; - std::size_t table_rows_log = std::log2(desc.rows_amount); + plonk_table_description desc( + placeholder_test_params::witness_columns, + placeholder_test_params::public_input_columns, + placeholder_test_params::constant_columns, + placeholder_test_params::selector_columns + ); + desc.rows_amount = circuit.table_rows; + desc.usable_rows_amount = circuit.usable_rows; + std::size_t table_rows_log = std::log2(desc.rows_amount); - const std::size_t argument_size = 3; + const std::size_t argument_size = 3; - typename lpc_type::fri_type::params_type fri_params(1, table_rows_log, placeholder_test_params::lambda, 4); - lpc_scheme_type lpc_scheme(fri_params); + typename lpc_type::fri_type::params_type fri_params(1, table_rows_log, placeholder_test_params::lambda, 4); + lpc_scheme_type lpc_scheme(fri_params); - typename policy_type::constraint_system_type constraint_system(circuit.gates, circuit.copy_constraints, - circuit.lookup_gates); - typename policy_type::variable_assignment_type assignments = circuit.table; + typename policy_type::constraint_system_type constraint_system(circuit.gates, circuit.copy_constraints, + circuit.lookup_gates); + typename policy_type::variable_assignment_type assignments = circuit.table; - transcript_type transcript; + transcript_type transcript; - typename placeholder_public_preprocessor::preprocessed_data_type - preprocessed_public_data = placeholder_public_preprocessor::process( - constraint_system, assignments.move_public_table(), desc, lpc_scheme + typename placeholder_public_preprocessor::preprocessed_data_type + preprocessed_public_data = placeholder_public_preprocessor::process( + constraint_system, assignments.move_public_table(), desc, lpc_scheme ); - typename placeholder_private_preprocessor::preprocessed_data_type - preprocessed_private_data = placeholder_private_preprocessor::process( - constraint_system, assignments.move_private_table(), desc + typename placeholder_private_preprocessor::preprocessed_data_type + preprocessed_private_data = placeholder_private_preprocessor::process( + constraint_system, assignments.move_private_table(), desc ); - auto polynomial_table = - plonk_polynomial_dfs_table( - preprocessed_private_data.private_polynomial_table, preprocessed_public_data.public_polynomial_table); - - std::vector init_blob {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - transcript::fiat_shamir_heuristic_sequential prover_transcript(init_blob); - transcript::fiat_shamir_heuristic_sequential verifier_transcript(init_blob); - - typename placeholder_permutation_argument::prover_result_type prover_res = - placeholder_permutation_argument::prove_eval( - constraint_system, preprocessed_public_data, desc, polynomial_table, lpc_scheme, prover_transcript); - - // Challenge phase - const auto &permuted_columns = preprocessed_public_data.common_data.permuted_columns; - - typename field_type::value_type y = algebra::random_element(); - std::vector f_at_y(permuted_columns.size()); - std::vector S_id(permuted_columns.size()); - std::vector S_sigma(permuted_columns.size()); - for (std::size_t i = 0; i < permuted_columns.size(); i++) { - f_at_y[i] = polynomial_table[permuted_columns[i]].evaluate(y); - S_id[i] = preprocessed_public_data.identity_polynomials[i].evaluate(y); - S_sigma[i] = preprocessed_public_data.permutation_polynomials[i].evaluate(y); - } - - auto omega = preprocessed_public_data.common_data.basic_domain->get_domain_element(1); + auto polynomial_table = + plonk_polynomial_dfs_table( + preprocessed_private_data.private_polynomial_table, + preprocessed_public_data.public_polynomial_table); + + std::vector init_blob{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + transcript::fiat_shamir_heuristic_sequential prover_transcript( + init_blob); + transcript::fiat_shamir_heuristic_sequential verifier_transcript( + init_blob); + + typename placeholder_permutation_argument::prover_result_type prover_res = + placeholder_permutation_argument::prove_eval( + constraint_system, preprocessed_public_data, desc, polynomial_table, lpc_scheme, + prover_transcript); + + // Challenge phase + const auto &permuted_columns = preprocessed_public_data.common_data.permuted_columns; + + typename field_type::value_type y = algebra::random_element(); + std::vector f_at_y(permuted_columns.size()); + std::vector S_id(permuted_columns.size()); + std::vector S_sigma(permuted_columns.size()); + for (std::size_t i = 0; i < permuted_columns.size(); i++) { + f_at_y[i] = polynomial_table[permuted_columns[i]].evaluate(y); + S_id[i] = preprocessed_public_data.identity_polynomials[i].evaluate(y); + S_sigma[i] = preprocessed_public_data.permutation_polynomials[i].evaluate(y); + } - typename field_type::value_type v_p_at_y = prover_res.permutation_polynomial_dfs.evaluate(y); - typename field_type::value_type v_p_at_y_shifted = prover_res.permutation_polynomial_dfs.evaluate(omega * y); + auto omega = preprocessed_public_data.common_data.basic_domain->get_domain_element(1); - std::vector special_selector_values(3); - special_selector_values[0] = preprocessed_public_data.common_data.lagrange_0.evaluate(y); - special_selector_values[1] = preprocessed_public_data.q_last.evaluate(y); - special_selector_values[2] = preprocessed_public_data.q_blind.evaluate(y); + typename field_type::value_type v_p_at_y = prover_res.permutation_polynomial_dfs.evaluate(y); + typename field_type::value_type v_p_at_y_shifted = prover_res.permutation_polynomial_dfs.evaluate(omega * y); + std::vector special_selector_values(3); + special_selector_values[0] = preprocessed_public_data.common_data.lagrange_0.evaluate(y); + special_selector_values[1] = preprocessed_public_data.q_last.evaluate(y); + special_selector_values[2] = preprocessed_public_data.q_blind.evaluate(y); - auto permutation_commitment = lpc_scheme.commit(PERMUTATION_BATCH); - std::array verifier_res = - placeholder_permutation_argument::verify_eval( - preprocessed_public_data.common_data, - S_id, S_sigma, - special_selector_values, - y, f_at_y, v_p_at_y, v_p_at_y_shifted, {}, verifier_transcript - ); - typename field_type::value_type verifier_next_challenge = verifier_transcript.template challenge(); - typename field_type::value_type prover_next_challenge = prover_transcript.template challenge(); - BOOST_CHECK(verifier_next_challenge == prover_next_challenge); + auto permutation_commitment = lpc_scheme.commit(PERMUTATION_BATCH); + std::array verifier_res = + placeholder_permutation_argument::verify_eval( + preprocessed_public_data.common_data, + S_id, S_sigma, + special_selector_values, + y, f_at_y, v_p_at_y, v_p_at_y_shifted, {}, verifier_transcript + ); - for (std::size_t i = 0; i < argument_size; i++) { - BOOST_CHECK(prover_res.F_dfs[i].evaluate(y) == verifier_res[i]); - for (std::size_t j = 0; j < desc.rows_amount; j++) { - BOOST_CHECK( - prover_res.F_dfs[i].evaluate(preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) == field_type::value_type::zero() - ); + typename field_type::value_type verifier_next_challenge = verifier_transcript.template challenge(); + typename field_type::value_type prover_next_challenge = prover_transcript.template challenge(); + BOOST_CHECK(verifier_next_challenge == prover_next_challenge); + + for (std::size_t i = 0; i < argument_size; i++) { + BOOST_CHECK(prover_res.F_dfs[i].evaluate(y) == verifier_res[i]); + for (std::size_t j = 0; j < desc.rows_amount; j++) { + BOOST_CHECK( + prover_res.F_dfs[i].evaluate( + preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) == + field_type::value_type::zero() + ); + } } } -} -BOOST_AUTO_TEST_CASE(placeholder_gate_argument_test) { - auto circuit = circuit_test_t(); + BOOST_AUTO_TEST_CASE(placeholder_gate_argument_test) { + auto circuit = circuit_test_t(); - plonk_table_description desc( - placeholder_test_params::witness_columns, - placeholder_test_params::public_input_columns, - placeholder_test_params::constant_columns, - placeholder_test_params::selector_columns - ); + plonk_table_description desc( + placeholder_test_params::witness_columns, + placeholder_test_params::public_input_columns, + placeholder_test_params::constant_columns, + placeholder_test_params::selector_columns + ); - desc.rows_amount = circuit.table_rows; - desc.usable_rows_amount = circuit.usable_rows; - std::size_t table_rows_log = std::log2(desc.rows_amount); + desc.rows_amount = circuit.table_rows; + desc.usable_rows_amount = circuit.usable_rows; + std::size_t table_rows_log = std::log2(desc.rows_amount); - typename policy_type::constraint_system_type constraint_system( - circuit.gates, circuit.copy_constraints, circuit.lookup_gates); - typename policy_type::variable_assignment_type assignments = circuit.table; + typename policy_type::constraint_system_type constraint_system( + circuit.gates, circuit.copy_constraints, circuit.lookup_gates); + typename policy_type::variable_assignment_type assignments = circuit.table; - typename lpc_type::fri_type::params_type fri_params(1, table_rows_log, placeholder_test_params::lambda, 4); - lpc_scheme_type lpc_scheme(fri_params); + typename lpc_type::fri_type::params_type fri_params(1, table_rows_log, placeholder_test_params::lambda, 4); + lpc_scheme_type lpc_scheme(fri_params); - std::vector init_blob {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - transcript_type transcript(init_blob); + std::vector init_blob{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + transcript_type transcript(init_blob); - typename placeholder_public_preprocessor::preprocessed_data_type - preprocessed_public_data = placeholder_public_preprocessor::process( - constraint_system, assignments.public_table(), desc, lpc_scheme + typename placeholder_public_preprocessor::preprocessed_data_type + preprocessed_public_data = placeholder_public_preprocessor::process( + constraint_system, assignments.public_table(), desc, lpc_scheme ); - typename placeholder_private_preprocessor::preprocessed_data_type - preprocessed_private_data = placeholder_private_preprocessor::process( - constraint_system, assignments.private_table(), desc + typename placeholder_private_preprocessor::preprocessed_data_type + preprocessed_private_data = placeholder_private_preprocessor::process( + constraint_system, assignments.private_table(), desc ); - auto polynomial_table = - plonk_polynomial_dfs_table( - preprocessed_private_data.private_polynomial_table, preprocessed_public_data.public_polynomial_table); + auto polynomial_table = + plonk_polynomial_dfs_table( + preprocessed_private_data.private_polynomial_table, + preprocessed_public_data.public_polynomial_table); - transcript::fiat_shamir_heuristic_sequential prover_transcript = transcript; - transcript::fiat_shamir_heuristic_sequential verifier_transcript = transcript; + transcript::fiat_shamir_heuristic_sequential prover_transcript = transcript; + transcript::fiat_shamir_heuristic_sequential verifier_transcript = transcript; - math::polynomial_dfs mask_polynomial( - 0, preprocessed_public_data.common_data.basic_domain->m, - typename field_type::value_type(1) - ); - mask_polynomial -= preprocessed_public_data.q_last; - mask_polynomial -= preprocessed_public_data.q_blind; + math::polynomial_dfs mask_polynomial( + 0, preprocessed_public_data.common_data.basic_domain->m, + typename field_type::value_type(1) + ); + mask_polynomial -= preprocessed_public_data.q_last; + mask_polynomial -= preprocessed_public_data.q_blind; - std::array, 1> prover_res = - placeholder_gates_argument::prove_eval( - constraint_system, polynomial_table, preprocessed_public_data.common_data.basic_domain, - preprocessed_public_data.common_data.max_gates_degree, mask_polynomial, prover_transcript); + std::array, 1> prover_res = + placeholder_gates_argument::prove_eval( + constraint_system, polynomial_table, preprocessed_public_data.common_data.basic_domain, + preprocessed_public_data.common_data.max_gates_degree, mask_polynomial, prover_transcript); - // Challenge phase - typename field_type::value_type y = algebra::random_element(); - typename field_type::value_type omega = preprocessed_public_data.common_data.basic_domain->get_domain_element(1); + // Challenge phase + typename field_type::value_type y = algebra::random_element(); + typename field_type::value_type omega = preprocessed_public_data.common_data.basic_domain->get_domain_element( + 1); - typename policy_type::evaluation_map columns_at_y; - for (std::size_t i = 0; i < placeholder_test_params::witness_columns; i++) { + typename policy_type::evaluation_map columns_at_y; + for (std::size_t i = 0; i < placeholder_test_params::witness_columns; i++) { - std::size_t i_global_index = i; + std::size_t i_global_index = i; - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::witness); - columns_at_y[key] = polynomial_table.witness(i).evaluate(y * omega.pow(rotation)); + for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + auto key = std::make_tuple(i, rotation, + plonk_variable::column_type::witness); + columns_at_y[key] = polynomial_table.witness(i).evaluate(y * omega.pow(rotation)); + } } - } - for (std::size_t i = 0; i < 0 + placeholder_test_params::public_input_columns; i++) { + for (std::size_t i = 0; i < 0 + placeholder_test_params::public_input_columns; i++) { - std::size_t i_global_index = placeholder_test_params::witness_columns + i; + std::size_t i_global_index = placeholder_test_params::witness_columns + i; - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::public_input); + auto key = std::make_tuple(i, rotation, + plonk_variable::column_type::public_input); - columns_at_y[key] = polynomial_table.public_input(i).evaluate(y * omega.pow(rotation)); + columns_at_y[key] = polynomial_table.public_input(i).evaluate(y * omega.pow(rotation)); + } } - } - for (std::size_t i = 0; i < 0 + placeholder_test_params::constant_columns; i++) { + for (std::size_t i = 0; i < 0 + placeholder_test_params::constant_columns; i++) { - std::size_t i_global_index = - placeholder_test_params::witness_columns + placeholder_test_params::public_input_columns + i; + std::size_t i_global_index = + placeholder_test_params::witness_columns + placeholder_test_params::public_input_columns + i; - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::constant); + for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + auto key = std::make_tuple(i, rotation, + plonk_variable::column_type::constant); - columns_at_y[key] = polynomial_table.constant(i).evaluate(y * omega.pow(rotation)); + columns_at_y[key] = polynomial_table.constant(i).evaluate(y * omega.pow(rotation)); + } } - } - for (std::size_t i = 0; i < placeholder_test_params::selector_columns; i++) { + for (std::size_t i = 0; i < placeholder_test_params::selector_columns; i++) { - std::size_t i_global_index = placeholder_test_params::witness_columns + - placeholder_test_params::constant_columns + - placeholder_test_params::public_input_columns + i; + std::size_t i_global_index = placeholder_test_params::witness_columns + + placeholder_test_params::constant_columns + + placeholder_test_params::public_input_columns + i; - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::selector); + for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + auto key = std::make_tuple(i, rotation, + plonk_variable::column_type::selector); - columns_at_y[key] = polynomial_table.selector(i).evaluate(y * omega.pow(rotation)); + columns_at_y[key] = polynomial_table.selector(i).evaluate(y * omega.pow(rotation)); + } } - } - auto mask_value = field_type::value_type::one() - preprocessed_public_data.q_last.evaluate(y) - - preprocessed_public_data.q_blind.evaluate(y); - std::array verifier_res = - placeholder_gates_argument::verify_eval( - constraint_system.gates(), columns_at_y, y, mask_value, verifier_transcript); + auto mask_value = field_type::value_type::one() - preprocessed_public_data.q_last.evaluate(y) - + preprocessed_public_data.q_blind.evaluate(y); + std::array verifier_res = + placeholder_gates_argument::verify_eval( + constraint_system.gates(), columns_at_y, y, mask_value, verifier_transcript); - typename field_type::value_type verifier_next_challenge = verifier_transcript.template challenge(); - typename field_type::value_type prover_next_challenge = prover_transcript.template challenge(); - BOOST_CHECK(verifier_next_challenge == prover_next_challenge); + typename field_type::value_type verifier_next_challenge = verifier_transcript.template challenge(); + typename field_type::value_type prover_next_challenge = prover_transcript.template challenge(); + BOOST_CHECK(verifier_next_challenge == prover_next_challenge); - BOOST_CHECK(prover_res[0].evaluate(y) == verifier_res[0]); -} + BOOST_CHECK(prover_res[0].evaluate(y) == verifier_res[0]); + } BOOST_AUTO_TEST_SUITE_END() @@ -811,152 +820,163 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit3_lookup_test) using circuit_params = placeholder_circuit_params; using transcript_type = typename transcript::fiat_shamir_heuristic_sequential; using lpc_params_type = commitments::list_polynomial_commitment_params< - typename placeholder_test_params::merkle_hash_type, - typename placeholder_test_params::transcript_hash_type, - placeholder_test_params::m + typename placeholder_test_params::merkle_hash_type, + typename placeholder_test_params::transcript_hash_type, + placeholder_test_params::m >; using lpc_type = commitments::list_polynomial_commitment; using lpc_scheme_type = typename commitments::lpc_commitment_scheme; using lpc_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params; - using policy_type = zk::snark::detail::placeholder_policy; - -BOOST_AUTO_TEST_CASE(lookup_test) { - auto circuit = circuit_test_3(); - constexpr std::size_t argument_size = 4; - - plonk_table_description desc( - placeholder_test_params::witness_columns, - placeholder_test_params::public_input_columns, - placeholder_test_params::constant_columns, - placeholder_test_params::selector_columns - ); - - desc.rows_amount = table_rows; - desc.usable_rows_amount = usable_rows; - - typename policy_type::constraint_system_type constraint_system( - circuit.gates, - circuit.copy_constraints, - circuit.lookup_gates, - circuit.lookup_tables - ); - typename policy_type::variable_assignment_type assignments = circuit.table; - - typename lpc_type::fri_type::params_type fri_params(1, table_rows_log, placeholder_test_params::lambda, 4, true); - lpc_scheme_type lpc_scheme(fri_params); - - typename placeholder_public_preprocessor::preprocessed_data_type - preprocessed_public_data = placeholder_public_preprocessor::process( - constraint_system, assignments.public_table(), desc, lpc_scheme ); - - typename placeholder_private_preprocessor::preprocessed_data_type - preprocessed_private_data = placeholder_private_preprocessor::process( - constraint_system, assignments.private_table(), desc); - - auto polynomial_table = - plonk_polynomial_dfs_table( - preprocessed_private_data.private_polynomial_table, preprocessed_public_data.public_polynomial_table - ); - - std::vector init_blob {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - transcript_type prover_transcript(init_blob); - transcript_type verifier_transcript(init_blob); - - placeholder_lookup_argument_prover lookup_prover( - constraint_system, preprocessed_public_data, polynomial_table, lpc_scheme, prover_transcript); - auto prover_res = lookup_prover.prove_eval(); - auto omega = preprocessed_public_data.common_data.basic_domain->get_domain_element(1); - - // Challenge phase - typename field_type::value_type y = algebra::random_element(); - typename policy_type::evaluation_map columns_at_y; - for (std::size_t i = 0; i < placeholder_test_params::witness_columns; i++) { - - std::size_t i_global_index = i; - - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::witness); - columns_at_y[key] = polynomial_table.witness(i).evaluate(y * omega.pow(rotation)); - } - } + using policy_type = placeholder; - for (std::size_t i = 0; i < 0 + placeholder_test_params::constant_columns; i++) { + BOOST_AUTO_TEST_CASE(lookup_test) { + auto circuit = circuit_test_3(); + constexpr std::size_t argument_size = 4; - std::size_t i_global_index = placeholder_test_params::witness_columns + - placeholder_test_params::public_input_columns + i; + plonk_table_description desc( + placeholder_test_params::witness_columns, + placeholder_test_params::public_input_columns, + placeholder_test_params::constant_columns, + placeholder_test_params::selector_columns + ); - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::constant); + desc.rows_amount = table_rows; + desc.usable_rows_amount = usable_rows; + + typename policy_type::constraint_system_type constraint_system( + circuit.gates, + circuit.copy_constraints, + circuit.lookup_gates, + circuit.lookup_tables + ); + typename policy_type::variable_assignment_type assignments = circuit.table; - columns_at_y[key] = polynomial_table.constant(i).evaluate(y * omega.pow(rotation)); + typename lpc_type::fri_type::params_type fri_params(1, table_rows_log, placeholder_test_params::lambda, 4, + true); + lpc_scheme_type lpc_scheme(fri_params); + + typename placeholder_public_preprocessor::preprocessed_data_type + preprocessed_public_data = placeholder_public_preprocessor::process( + constraint_system, assignments.public_table(), desc, lpc_scheme); + + typename placeholder_private_preprocessor::preprocessed_data_type + preprocessed_private_data = placeholder_private_preprocessor::process( + constraint_system, assignments.private_table(), desc); + + auto polynomial_table = + plonk_polynomial_dfs_table( + preprocessed_private_data.private_polynomial_table, + preprocessed_public_data.public_polynomial_table + ); + + std::vector init_blob{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + transcript_type prover_transcript(init_blob); + transcript_type verifier_transcript(init_blob); + + placeholder_lookup_argument_prover lookup_prover( + constraint_system, preprocessed_public_data, polynomial_table, lpc_scheme, prover_transcript); + auto prover_res = lookup_prover.prove_eval(); + auto omega = preprocessed_public_data.common_data.basic_domain->get_domain_element(1); + + // Challenge phase + typename field_type::value_type y = algebra::random_element(); + typename policy_type::evaluation_map columns_at_y; + for (std::size_t i = 0; i < placeholder_test_params::witness_columns; i++) { + + std::size_t i_global_index = i; + + for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + auto key = std::make_tuple(i, rotation, + plonk_variable::column_type::witness); + columns_at_y[key] = polynomial_table.witness(i).evaluate(y * omega.pow(rotation)); + } } - } - for (std::size_t i = 0; i < placeholder_test_params::selector_columns; i++) { + for (std::size_t i = 0; i < 0 + placeholder_test_params::constant_columns; i++) { - std::size_t i_global_index = placeholder_test_params::witness_columns + - placeholder_test_params::constant_columns + - placeholder_test_params::public_input_columns + i; + std::size_t i_global_index = placeholder_test_params::witness_columns + + placeholder_test_params::public_input_columns + i; - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::selector); + for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + auto key = std::make_tuple(i, rotation, + plonk_variable::column_type::constant); - columns_at_y[key] = polynomial_table.selector(i).evaluate(y * omega.pow(rotation)); + columns_at_y[key] = polynomial_table.constant(i).evaluate(y * omega.pow(rotation)); + } } - } - lpc_scheme.append_eval_point(LOOKUP_BATCH, y); - lpc_scheme.append_eval_point(LOOKUP_BATCH, y * omega); - lpc_scheme.append_eval_point(LOOKUP_BATCH, y * omega.pow(usable_rows)); - - lpc_scheme.commit(PERMUTATION_BATCH); - lpc_scheme.append_eval_point(PERMUTATION_BATCH, y); - lpc_scheme.append_eval_point(PERMUTATION_BATCH, preprocessed_public_data.common_data.permutation_parts, y * omega); - - transcript_type transcript; - lpc_scheme.setup(transcript, preprocessed_public_data.common_data.commitment_scheme_data); - auto lpc_proof = lpc_scheme.proof_eval(transcript); - // Prepare sorted and V_L values - std::vector special_selector_values(3); - special_selector_values[0] = preprocessed_public_data.common_data.lagrange_0.evaluate(y); - special_selector_values[1] = preprocessed_public_data.q_last.evaluate(y); - special_selector_values[2] = preprocessed_public_data.q_blind.evaluate(y); - - std::vector special_selector_values_shifted(2); - special_selector_values_shifted[0] = preprocessed_public_data.q_last.evaluate(y * omega); - special_selector_values_shifted[1] = preprocessed_public_data.q_blind.evaluate(y * omega); - - placeholder_lookup_argument_verifier lookup_verifier; - std::array verifier_res = lookup_verifier.verify_eval( - preprocessed_public_data.common_data, - special_selector_values, special_selector_values_shifted, - constraint_system, - y, columns_at_y, lpc_proof.z.get(LOOKUP_BATCH), - lpc_proof.z.get(PERMUTATION_BATCH, preprocessed_public_data.common_data.permutation_parts), - {}, - prover_res.lookup_commitment, - verifier_transcript - ); - - typename field_type::value_type verifier_next_challenge = verifier_transcript.template challenge(); - typename field_type::value_type prover_next_challenge = prover_transcript.template challenge(); - BOOST_CHECK(verifier_next_challenge == prover_next_challenge); - - for (int i = 0; i < argument_size; i++) { - BOOST_CHECK(prover_res.F_dfs[i].evaluate(y) == verifier_res[i]); - if( prover_res.F_dfs[i].evaluate(y) != verifier_res[i] ){ - std::cout << prover_res.F_dfs[i].evaluate(y) << "!=" << verifier_res[i] << std::endl; + for (std::size_t i = 0; i < placeholder_test_params::selector_columns; i++) { + + std::size_t i_global_index = placeholder_test_params::witness_columns + + placeholder_test_params::constant_columns + + placeholder_test_params::public_input_columns + i; + + for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + auto key = std::make_tuple(i, rotation, + plonk_variable::column_type::selector); + + columns_at_y[key] = polynomial_table.selector(i).evaluate(y * omega.pow(rotation)); + } } - for (std::size_t j = 0; j < desc.rows_amount; j++) { - if(prover_res.F_dfs[i].evaluate(preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) != field_type::value_type::zero()){ - std::cout << "!["<< i << "][" << j << "]" << std::endl; + lpc_scheme.append_eval_point(LOOKUP_BATCH, y); + lpc_scheme.append_eval_point(LOOKUP_BATCH, y * omega); + lpc_scheme.append_eval_point(LOOKUP_BATCH, y * omega.pow(usable_rows)); + + lpc_scheme.commit(PERMUTATION_BATCH); + lpc_scheme.append_eval_point(PERMUTATION_BATCH, y); + lpc_scheme.append_eval_point(PERMUTATION_BATCH, preprocessed_public_data.common_data.permutation_parts, + y * omega); + + transcript_type transcript; + lpc_scheme.setup(transcript, preprocessed_public_data.common_data.commitment_scheme_data); + auto lpc_proof = lpc_scheme.proof_eval(transcript); + // Prepare sorted and V_L values + std::vector special_selector_values(3); + special_selector_values[0] = preprocessed_public_data.common_data.lagrange_0.evaluate(y); + special_selector_values[1] = preprocessed_public_data.q_last.evaluate(y); + special_selector_values[2] = preprocessed_public_data.q_blind.evaluate(y); + + std::vector special_selector_values_shifted(2); + special_selector_values_shifted[0] = preprocessed_public_data.q_last.evaluate(y * omega); + special_selector_values_shifted[1] = preprocessed_public_data.q_blind.evaluate(y * omega); + + placeholder_lookup_argument_verifier lookup_verifier; + std::array verifier_res = lookup_verifier.verify_eval( + preprocessed_public_data.common_data, + special_selector_values, special_selector_values_shifted, + constraint_system, + y, columns_at_y, lpc_proof.z.get(LOOKUP_BATCH), + lpc_proof.z.get(PERMUTATION_BATCH, preprocessed_public_data.common_data.permutation_parts), + {}, + prover_res.lookup_commitment, + verifier_transcript + ); + + typename field_type::value_type verifier_next_challenge = verifier_transcript.template challenge(); + typename field_type::value_type prover_next_challenge = prover_transcript.template challenge(); + BOOST_CHECK(verifier_next_challenge == prover_next_challenge); + + for (int i = 0; i < argument_size; i++) { + BOOST_CHECK(prover_res.F_dfs[i].evaluate(y) == verifier_res[i]); + if (prover_res.F_dfs[i].evaluate(y) != verifier_res[i]) { + std::cout << prover_res.F_dfs[i].evaluate(y) << "!=" << verifier_res[i] << std::endl; + } + for (std::size_t j = 0; j < desc.rows_amount; j++) { + if (prover_res.F_dfs[i].evaluate( + preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) != + field_type::value_type::zero()) { + std::cout << "![" << i << "][" << j << "]" << std::endl; + + } + BOOST_CHECK(prover_res.F_dfs[i].evaluate( + preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) == + field_type::value_type::zero()); } - BOOST_CHECK(prover_res.F_dfs[i].evaluate(preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) == field_type::value_type::zero()); } } -} + BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE(placeholder_circuit4_lookup_test) @@ -983,292 +1003,313 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit4_lookup_test) using circuit_params = placeholder_circuit_params; using transcript_type = typename transcript::fiat_shamir_heuristic_sequential; using lpc_params_type = commitments::list_polynomial_commitment_params< - typename placeholder_test_params::merkle_hash_type, - typename placeholder_test_params::transcript_hash_type, - placeholder_test_params::m + typename placeholder_test_params::merkle_hash_type, + typename placeholder_test_params::transcript_hash_type, + placeholder_test_params::m >; using lpc_type = commitments::list_polynomial_commitment; using lpc_scheme_type = typename commitments::lpc_commitment_scheme; using lpc_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params; - using policy_type = zk::snark::detail::placeholder_policy; - -BOOST_AUTO_TEST_CASE(lookup_test) { - auto circuit = circuit_test_4(test_global_alg_rnd_engine); - constexpr std::size_t argument_size = 4; - - plonk_table_description desc( - placeholder_test_params::witness_columns, - placeholder_test_params::public_input_columns, - placeholder_test_params::constant_columns, - placeholder_test_params::selector_columns - ); - - desc.rows_amount = table_rows; - desc.usable_rows_amount = usable_rows; - - typename policy_type::constraint_system_type constraint_system( - circuit.gates, - circuit.copy_constraints, - circuit.lookup_gates, - circuit.lookup_tables - ); - typename policy_type::variable_assignment_type assignments = circuit.table; - - typename lpc_type::fri_type::params_type fri_params(1, table_rows_log, placeholder_test_params::lambda, 4); - lpc_scheme_type lpc_scheme(fri_params); - - transcript_type transcript; - - typename placeholder_public_preprocessor::preprocessed_data_type - preprocessed_public_data = placeholder_public_preprocessor::process( - constraint_system, assignments.public_table(), desc, lpc_scheme); - - typename placeholder_private_preprocessor::preprocessed_data_type - preprocessed_private_data = placeholder_private_preprocessor::process( - constraint_system, assignments.private_table(), desc); - lpc_scheme.setup(transcript, preprocessed_public_data.common_data.commitment_scheme_data); - - auto polynomial_table = - plonk_polynomial_dfs_table( - preprocessed_private_data.private_polynomial_table, preprocessed_public_data.public_polynomial_table); - - std::vector init_blob {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - transcript_type prover_transcript(init_blob); - transcript_type verifier_transcript(init_blob); - - placeholder_lookup_argument_prover prover( - constraint_system, preprocessed_public_data, polynomial_table, lpc_scheme, prover_transcript); - auto prover_res = prover.prove_eval(); - - // Challenge phase - auto omega = preprocessed_public_data.common_data.basic_domain->get_domain_element(1); - typename field_type::value_type y = algebra::random_element(); - typename policy_type::evaluation_map columns_at_y; - for (std::size_t i = 0; i < placeholder_test_params::witness_columns; i++) { - - std::size_t i_global_index = i; - - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::witness); - columns_at_y[key] = polynomial_table.witness(i).evaluate(y * omega.pow(rotation)); + using policy_type = placeholder; + + BOOST_AUTO_TEST_CASE(lookup_test) { + auto circuit = circuit_test_4(test_global_alg_rnd_engine); + constexpr std::size_t argument_size = 4; + + plonk_table_description desc( + placeholder_test_params::witness_columns, + placeholder_test_params::public_input_columns, + placeholder_test_params::constant_columns, + placeholder_test_params::selector_columns + ); + + desc.rows_amount = table_rows; + desc.usable_rows_amount = usable_rows; + + typename policy_type::constraint_system_type constraint_system( + circuit.gates, + circuit.copy_constraints, + circuit.lookup_gates, + circuit.lookup_tables + ); + typename policy_type::variable_assignment_type assignments = circuit.table; + + typename lpc_type::fri_type::params_type fri_params(1, table_rows_log, placeholder_test_params::lambda, 4); + lpc_scheme_type lpc_scheme(fri_params); + + transcript_type transcript; + + typename placeholder_public_preprocessor::preprocessed_data_type + preprocessed_public_data = placeholder_public_preprocessor::process( + constraint_system, assignments.public_table(), desc, lpc_scheme); + + typename placeholder_private_preprocessor::preprocessed_data_type + preprocessed_private_data = placeholder_private_preprocessor::process( + constraint_system, assignments.private_table(), desc); + lpc_scheme.setup(transcript, preprocessed_public_data.common_data.commitment_scheme_data); + + auto polynomial_table = + plonk_polynomial_dfs_table( + preprocessed_private_data.private_polynomial_table, + preprocessed_public_data.public_polynomial_table); + + std::vector init_blob{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + transcript_type prover_transcript(init_blob); + transcript_type verifier_transcript(init_blob); + + placeholder_lookup_argument_prover prover( + constraint_system, preprocessed_public_data, polynomial_table, lpc_scheme, prover_transcript); + auto prover_res = prover.prove_eval(); + + // Challenge phase + auto omega = preprocessed_public_data.common_data.basic_domain->get_domain_element(1); + typename field_type::value_type y = algebra::random_element(); + typename policy_type::evaluation_map columns_at_y; + for (std::size_t i = 0; i < placeholder_test_params::witness_columns; i++) { + + std::size_t i_global_index = i; + + for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + auto key = std::make_tuple(i, rotation, + plonk_variable::column_type::witness); + columns_at_y[key] = polynomial_table.witness(i).evaluate(y * omega.pow(rotation)); + } } - } - for (std::size_t i = 0; i < 0 + placeholder_test_params::constant_columns; i++) { + for (std::size_t i = 0; i < 0 + placeholder_test_params::constant_columns; i++) { - std::size_t i_global_index = placeholder_test_params::witness_columns + - placeholder_test_params::public_input_columns + i; + std::size_t i_global_index = placeholder_test_params::witness_columns + + placeholder_test_params::public_input_columns + i; - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::constant); + for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + auto key = std::make_tuple(i, rotation, + plonk_variable::column_type::constant); - columns_at_y[key] = polynomial_table.constant(i).evaluate(y * omega.pow(rotation)); + columns_at_y[key] = polynomial_table.constant(i).evaluate(y * omega.pow(rotation)); + } } - } - for (std::size_t i = 0; i < placeholder_test_params::selector_columns; i++) { + for (std::size_t i = 0; i < placeholder_test_params::selector_columns; i++) { - std::size_t i_global_index = placeholder_test_params::witness_columns + - placeholder_test_params::constant_columns + - placeholder_test_params::public_input_columns + i; + std::size_t i_global_index = placeholder_test_params::witness_columns + + placeholder_test_params::constant_columns + + placeholder_test_params::public_input_columns + i; - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::selector); + for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + auto key = std::make_tuple(i, rotation, + plonk_variable::column_type::selector); - columns_at_y[key] = polynomial_table.selector(i).evaluate(y * omega.pow(rotation)); + columns_at_y[key] = polynomial_table.selector(i).evaluate(y * omega.pow(rotation)); + } } - } - lpc_scheme.append_eval_point(LOOKUP_BATCH, y); - lpc_scheme.append_eval_point(LOOKUP_BATCH, y * omega); - lpc_scheme.append_eval_point(LOOKUP_BATCH, y * omega.pow(usable_rows)); - - lpc_scheme.commit(PERMUTATION_BATCH); - lpc_scheme.append_eval_point(PERMUTATION_BATCH, y); - lpc_scheme.append_eval_point(PERMUTATION_BATCH, y * omega); - - auto lpc_proof = lpc_scheme.proof_eval(transcript); - // Prepare sorted, V_L and V_S values. - - auto special_selectors = (field_type::value_type::one() - (preprocessed_public_data.q_last.evaluate(y) + - preprocessed_public_data.q_blind.evaluate(y))); - auto half = prover_res.F_dfs[2].evaluate(y) * special_selectors.inversed(); - - std::vector special_selector_values(3); - special_selector_values[0] = preprocessed_public_data.common_data.lagrange_0.evaluate(y); - special_selector_values[1] = preprocessed_public_data.q_last.evaluate(y); - special_selector_values[2] = preprocessed_public_data.q_blind.evaluate(y); - - std::vector special_selector_values_shifted(2); - special_selector_values_shifted[0] = preprocessed_public_data.q_last.evaluate(y * omega); - special_selector_values_shifted[1] = preprocessed_public_data.q_blind.evaluate(y * omega); - - placeholder_lookup_argument_verifier verifier; - std::array verifier_res = verifier.verify_eval( - preprocessed_public_data.common_data, - special_selector_values, special_selector_values_shifted, - constraint_system, - y, columns_at_y, lpc_proof.z.get(LOOKUP_BATCH), - lpc_proof.z.get(PERMUTATION_BATCH, 0), - {}, - prover_res.lookup_commitment, - verifier_transcript - ); - - typename field_type::value_type verifier_next_challenge = verifier_transcript.template challenge(); - typename field_type::value_type prover_next_challenge = prover_transcript.template challenge(); - BOOST_CHECK(verifier_next_challenge == prover_next_challenge); - - for (std::size_t i = 0; i < argument_size; i++) { - BOOST_CHECK(prover_res.F_dfs[i].evaluate(y) == verifier_res[i]); - for (std::size_t j = 0; j < desc.rows_amount; j++) { - if (prover_res.F_dfs[i].evaluate(preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) != - field_type::value_type::zero()){ - std::cout << "!["<< i << "][" << j << "]" << std::endl; + lpc_scheme.append_eval_point(LOOKUP_BATCH, y); + lpc_scheme.append_eval_point(LOOKUP_BATCH, y * omega); + lpc_scheme.append_eval_point(LOOKUP_BATCH, y * omega.pow(usable_rows)); + + lpc_scheme.commit(PERMUTATION_BATCH); + lpc_scheme.append_eval_point(PERMUTATION_BATCH, y); + lpc_scheme.append_eval_point(PERMUTATION_BATCH, y * omega); + + auto lpc_proof = lpc_scheme.proof_eval(transcript); + // Prepare sorted, V_L and V_S values. + + auto special_selectors = (field_type::value_type::one() - (preprocessed_public_data.q_last.evaluate(y) + + preprocessed_public_data.q_blind.evaluate(y))); + auto half = prover_res.F_dfs[2].evaluate(y) * special_selectors.inversed(); + + std::vector special_selector_values(3); + special_selector_values[0] = preprocessed_public_data.common_data.lagrange_0.evaluate(y); + special_selector_values[1] = preprocessed_public_data.q_last.evaluate(y); + special_selector_values[2] = preprocessed_public_data.q_blind.evaluate(y); + + std::vector special_selector_values_shifted(2); + special_selector_values_shifted[0] = preprocessed_public_data.q_last.evaluate(y * omega); + special_selector_values_shifted[1] = preprocessed_public_data.q_blind.evaluate(y * omega); + + placeholder_lookup_argument_verifier verifier; + std::array verifier_res = verifier.verify_eval( + preprocessed_public_data.common_data, + special_selector_values, special_selector_values_shifted, + constraint_system, + y, columns_at_y, lpc_proof.z.get(LOOKUP_BATCH), + lpc_proof.z.get(PERMUTATION_BATCH, 0), + {}, + prover_res.lookup_commitment, + verifier_transcript + ); + + typename field_type::value_type verifier_next_challenge = verifier_transcript.template challenge(); + typename field_type::value_type prover_next_challenge = prover_transcript.template challenge(); + BOOST_CHECK(verifier_next_challenge == prover_next_challenge); + + for (std::size_t i = 0; i < argument_size; i++) { + BOOST_CHECK(prover_res.F_dfs[i].evaluate(y) == verifier_res[i]); + for (std::size_t j = 0; j < desc.rows_amount; j++) { + if (prover_res.F_dfs[i].evaluate( + preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) != + field_type::value_type::zero()) { + std::cout << "![" << i << "][" << j << "]" << std::endl; + } + BOOST_CHECK( + prover_res.F_dfs[i].evaluate( + preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) == + field_type::value_type::zero()); } - BOOST_CHECK( - prover_res.F_dfs[i].evaluate(preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) == - field_type::value_type::zero()); } } -} + BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE(placeholder_circuit1) -using curve_type = algebra::curves::pallas; -using field_type = typename curve_type::base_field_type; -using poseidon_type = hashes::poseidon>; + using curve_type = algebra::curves::pallas; + using field_type = typename curve_type::base_field_type; + using poseidon_type = hashes::poseidon>; -using TestFixtures = boost::mpl::list< - placeholder_test_fixture, - placeholder_test_fixture, hashes::keccak_1600<512>, witness_columns_1, public_columns_1, constant_columns_1, selector_columns_1, rows_amount_1> + using TestFixtures = boost::mpl::list< + placeholder_test_fixture, + placeholder_test_fixture, hashes::keccak_1600<512>, witness_columns_1, public_columns_1, constant_columns_1, selector_columns_1, rows_amount_1> >; -BOOST_AUTO_TEST_CASE_TEMPLATE(prover_test, F, TestFixtures) { - auto circuit = circuit_test_1(test_global_alg_rnd_engine); - F fixture(circuit, circuit.usable_rows, circuit.table_rows); - BOOST_CHECK(fixture.run_test()); -} + + BOOST_AUTO_TEST_CASE_TEMPLATE(prover_test, F, TestFixtures) { + auto circuit = circuit_test_1(test_global_alg_rnd_engine); + F fixture(circuit, circuit.usable_rows, circuit.table_rows); + BOOST_CHECK(fixture.run_test()); + } + BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE(placeholder_circuit1_goldilocks) -using field_type = typename algebra::fields::goldilocks64; -using poseidon_type = hashes::poseidon>; + using field_type = typename algebra::fields::goldilocks64; + using poseidon_type = hashes::poseidon>; -using TestFixtures = boost::mpl::list< - placeholder_test_fixture, hashes::keccak_1600<512>, witness_columns_1, public_columns_1, constant_columns_1, selector_columns_1, rows_amount_1> + using TestFixtures = boost::mpl::list< + placeholder_test_fixture, hashes::keccak_1600<512>, witness_columns_1, public_columns_1, constant_columns_1, selector_columns_1, rows_amount_1> >; -BOOST_AUTO_TEST_CASE_TEMPLATE(prover_test, F, TestFixtures) { - auto circuit = circuit_test_1(test_global_alg_rnd_engine); - F fixture(circuit, circuit.usable_rows, circuit.table_rows); - BOOST_CHECK(fixture.run_test()); -} + + BOOST_AUTO_TEST_CASE_TEMPLATE(prover_test, F, TestFixtures) { + auto circuit = circuit_test_1(test_global_alg_rnd_engine); + F fixture(circuit, circuit.usable_rows, circuit.table_rows); + BOOST_CHECK(fixture.run_test()); + } + BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE(placeholder_circuit3) -using curve_type = algebra::curves::pallas; -using field_type = typename curve_type::base_field_type; -using poseidon_type = hashes::poseidon>; -const size_t usable_rows_3 = 4; + using curve_type = algebra::curves::pallas; + using field_type = typename curve_type::base_field_type; + using poseidon_type = hashes::poseidon>; + const size_t usable_rows_3 = 4; -using TestFixtures = boost::mpl::list< - placeholder_test_fixture, - placeholder_test_fixture, hashes::keccak_1600<512>, witness_columns_3, public_columns_3, constant_columns_3, selector_columns_3, usable_rows_3> + using TestFixtures = boost::mpl::list< + placeholder_test_fixture, + placeholder_test_fixture, hashes::keccak_1600<512>, witness_columns_3, public_columns_3, constant_columns_3, selector_columns_3, usable_rows_3> >; -BOOST_AUTO_TEST_CASE_TEMPLATE(prover_test, F, TestFixtures) { - auto circuit = circuit_test_3(test_global_alg_rnd_engine); - F fixture(circuit, usable_rows_3, 1 << 3); - BOOST_CHECK(fixture.run_test()); -} + + BOOST_AUTO_TEST_CASE_TEMPLATE(prover_test, F, TestFixtures) { + auto circuit = circuit_test_3(test_global_alg_rnd_engine); + F fixture(circuit, usable_rows_3, 1 << 3); + BOOST_CHECK(fixture.run_test()); + } + BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE(placeholder_circuit4) -using curve_type = algebra::curves::pallas; -using field_type = typename curve_type::base_field_type; -using poseidon_type = hashes::poseidon>; -const size_t usable_rows_4 = 5; + using curve_type = algebra::curves::pallas; + using field_type = typename curve_type::base_field_type; + using poseidon_type = hashes::poseidon>; + const size_t usable_rows_4 = 5; -using TestFixtures = boost::mpl::list< - placeholder_test_fixture, - placeholder_test_fixture, hashes::keccak_1600<512>, witness_columns_4, public_columns_4, constant_columns_4, selector_columns_4, usable_rows_4> + using TestFixtures = boost::mpl::list< + placeholder_test_fixture, + placeholder_test_fixture, hashes::keccak_1600<512>, witness_columns_4, public_columns_4, constant_columns_4, selector_columns_4, usable_rows_4> >; -BOOST_AUTO_TEST_CASE_TEMPLATE(prover_test, F, TestFixtures) { - auto circuit = circuit_test_4(test_global_alg_rnd_engine); - F fixture(circuit, usable_rows_4, 1 << 3); - BOOST_CHECK(fixture.run_test()); -} + + BOOST_AUTO_TEST_CASE_TEMPLATE(prover_test, F, TestFixtures) { + auto circuit = circuit_test_4(test_global_alg_rnd_engine); + F fixture(circuit, usable_rows_4, 1 << 3); + BOOST_CHECK(fixture.run_test()); + } + BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE(placeholder_circuit5) -using curve_type = algebra::curves::pallas; -using field_type = typename curve_type::base_field_type; -using poseidon_type = hashes::poseidon>; - -using TestFixtures = boost::mpl::list< - placeholder_test_fixture, - placeholder_test_fixture, hashes::keccak_1600<512>, witness_columns_5, public_columns_5, constant_columns_5, selector_columns_5, usable_rows_5, false, 10> ->; -BOOST_AUTO_TEST_CASE_TEMPLATE(prover_test, F, TestFixtures) { - auto circuit = circuit_test_5(test_global_alg_rnd_engine); - F fixture(circuit, circuit.usable_rows, circuit.table_rows); - BOOST_CHECK(fixture.run_test()); -} + using curve_type = algebra::curves::pallas; + using field_type = typename curve_type::base_field_type; + using poseidon_type = hashes::poseidon>; + + using TestFixtures = boost::mpl::list< + placeholder_test_fixture, + placeholder_test_fixture, hashes::keccak_1600<512>, witness_columns_5, public_columns_5, constant_columns_5, selector_columns_5, usable_rows_5, false, 10> + >; + + BOOST_AUTO_TEST_CASE_TEMPLATE(prover_test, F, TestFixtures) { + auto circuit = circuit_test_5(test_global_alg_rnd_engine); + F fixture(circuit, circuit.usable_rows, circuit.table_rows); + BOOST_CHECK(fixture.run_test()); + } + BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE(placeholder_circuit6) -using curve_type = algebra::curves::pallas; -using field_type = typename curve_type::base_field_type; -using poseidon_type = hashes::poseidon>; + using curve_type = algebra::curves::pallas; + using field_type = typename curve_type::base_field_type; + using poseidon_type = hashes::poseidon>; -using TestFixtures = boost::mpl::list< - placeholder_test_fixture, - placeholder_test_fixture, hashes::keccak_1600<512>, witness_columns_6, public_columns_6, constant_columns_6, selector_columns_6, usable_rows_6, true> + using TestFixtures = boost::mpl::list< + placeholder_test_fixture, + placeholder_test_fixture, hashes::keccak_1600<512>, witness_columns_6, public_columns_6, constant_columns_6, selector_columns_6, usable_rows_6, true> >; -BOOST_AUTO_TEST_CASE_TEMPLATE(prover_test, F, TestFixtures) { - auto circuit = circuit_test_6(test_global_alg_rnd_engine); - F fixture(circuit, circuit.usable_rows, circuit.table_rows); - BOOST_CHECK(fixture.run_test()); -} + + BOOST_AUTO_TEST_CASE_TEMPLATE(prover_test, F, TestFixtures) { + auto circuit = circuit_test_6(test_global_alg_rnd_engine); + F fixture(circuit, circuit.usable_rows, circuit.table_rows); + BOOST_CHECK(fixture.run_test()); + } + BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE(placeholder_circuit7) -using curve_type = algebra::curves::pallas; -using field_type = typename curve_type::base_field_type; -using poseidon_type = hashes::poseidon>; + using curve_type = algebra::curves::pallas; + using field_type = typename curve_type::base_field_type; + using poseidon_type = hashes::poseidon>; -using TestFixtures = boost::mpl::list< - placeholder_test_fixture, - placeholder_test_fixture, hashes::keccak_1600<512>, witness_columns_7, public_columns_7, constant_columns_7, selector_columns_7, usable_rows_7, true, 8>, - placeholder_test_fixture, hashes::keccak_1600<512>, witness_columns_7, public_columns_7, constant_columns_7, selector_columns_7, usable_rows_7, true, 10>, - placeholder_test_fixture, hashes::keccak_1600<512>, witness_columns_7, public_columns_7, constant_columns_7, selector_columns_7, usable_rows_7, true, 30>, - placeholder_test_fixture, hashes::keccak_1600<512>, witness_columns_7, public_columns_7, constant_columns_7, selector_columns_7, usable_rows_7, true, 50> + using TestFixtures = boost::mpl::list< + placeholder_test_fixture, + placeholder_test_fixture, hashes::keccak_1600<512>, witness_columns_7, public_columns_7, constant_columns_7, selector_columns_7, usable_rows_7, true, 8>, + placeholder_test_fixture, hashes::keccak_1600<512>, witness_columns_7, public_columns_7, constant_columns_7, selector_columns_7, usable_rows_7, true, 10>, + placeholder_test_fixture, hashes::keccak_1600<512>, witness_columns_7, public_columns_7, constant_columns_7, selector_columns_7, usable_rows_7, true, 30>, + placeholder_test_fixture, hashes::keccak_1600<512>, witness_columns_7, public_columns_7, constant_columns_7, selector_columns_7, usable_rows_7, true, 50> >; -BOOST_AUTO_TEST_CASE_TEMPLATE(prover_test, F, TestFixtures) { - auto circuit = circuit_test_7(test_global_alg_rnd_engine); - F fixture(circuit, circuit.usable_rows, circuit.table_rows); - BOOST_CHECK(fixture.run_test()); -} + + BOOST_AUTO_TEST_CASE_TEMPLATE(prover_test, F, TestFixtures) { + auto circuit = circuit_test_7(test_global_alg_rnd_engine); + F fixture(circuit, circuit.usable_rows, circuit.table_rows); + BOOST_CHECK(fixture.run_test()); + } + BOOST_AUTO_TEST_SUITE_END() template< - typename curve_type, - typename merkle_hash_type, - typename transcript_hash_type, - std::size_t WitnessColumns, - std::size_t PublicInputColumns, - std::size_t ConstantColumns, - std::size_t SelectorColumns, - std::size_t usable_rows_amount, - std::size_t permutation, - bool UseGrinding = false> + typename curve_type, + typename merkle_hash_type, + typename transcript_hash_type, + std::size_t WitnessColumns, + std::size_t PublicInputColumns, + std::size_t ConstantColumns, + std::size_t SelectorColumns, + std::size_t usable_rows_amount, + std::size_t permutation, + bool UseGrinding = false> struct placeholder_kzg_test_fixture : public test_initializer { using field_type = typename curve_type::scalar_field_type; @@ -1289,16 +1330,15 @@ struct placeholder_kzg_test_fixture : public test_initializer { using kzg_scheme_type = typename commitments::kzg_commitment_scheme; using kzg_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params; - using policy_type = zk::snark::detail::placeholder_policy; + using policy_type = placeholder; using circuit_type = - circuit_description, - usable_rows_amount>; + circuit_description, + usable_rows_amount>; placeholder_kzg_test_fixture() - : desc(WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns) - { + : desc(WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns) { } bool run_test() { @@ -1309,7 +1349,8 @@ struct placeholder_kzg_test_fixture : public test_initializer { desc.usable_rows_amount = circuit.usable_rows; std::size_t table_rows_log = std::log2(circuit.table_rows); - typename policy_type::constraint_system_type constraint_system(circuit.gates, circuit.copy_constraints, circuit.lookup_gates); + typename policy_type::constraint_system_type constraint_system(circuit.gates, circuit.copy_constraints, + circuit.lookup_gates); typename policy_type::variable_assignment_type assignments = circuit.table; bool verifier_res; @@ -1319,22 +1360,23 @@ struct placeholder_kzg_test_fixture : public test_initializer { kzg_scheme_type kzg_scheme(kzg_params); typename placeholder_public_preprocessor::preprocessed_data_type - kzg_preprocessed_public_data = - placeholder_public_preprocessor::process( - constraint_system, assignments.public_table(), desc, kzg_scheme - ); + kzg_preprocessed_public_data = + placeholder_public_preprocessor::process( + constraint_system, assignments.public_table(), desc, kzg_scheme + ); typename placeholder_private_preprocessor::preprocessed_data_type - kzg_preprocessed_private_data = placeholder_private_preprocessor::process( + kzg_preprocessed_private_data = placeholder_private_preprocessor::process( constraint_system, assignments.private_table(), desc - ); + ); auto kzg_proof = placeholder_prover::process( - kzg_preprocessed_public_data, std::move(kzg_preprocessed_private_data), desc, constraint_system, kzg_scheme + kzg_preprocessed_public_data, std::move(kzg_preprocessed_private_data), desc, constraint_system, + kzg_scheme ); verifier_res = placeholder_verifier::process( - kzg_preprocessed_public_data.common_data, kzg_proof, desc, constraint_system, kzg_scheme + kzg_preprocessed_public_data.common_data, kzg_proof, desc, constraint_system, kzg_scheme ); test_initializer::teardown(); return verifier_res; @@ -1347,80 +1389,80 @@ struct placeholder_kzg_test_fixture : public test_initializer { BOOST_AUTO_TEST_SUITE(placeholder_circuit2_kzg) using TestFixtures = boost::mpl::list< - placeholder_kzg_test_fixture< - algebra::curves::bls12<381>, - hashes::keccak_1600<256>, - hashes::keccak_1600<256>, - witness_columns_t, - public_columns_t, - constant_columns_t, - selector_columns_t, - usable_rows_t, - true> - /* - , placeholder_kzg_test_fixture< - algebra::curves::alt_bn128_254, - hashes::keccak_1600<256>, - hashes::keccak_1600<256>, - witness_columns_t, - public_columns_t, - constant_columns_t, - selector_columns_t, - usable_rows_t, - 4, true>*/ - , placeholder_kzg_test_fixture< - algebra::curves::mnt4_298, - hashes::keccak_1600<256>, - hashes::keccak_1600<256>, - witness_columns_t, - public_columns_t, - constant_columns_t, - selector_columns_t, - usable_rows_t, - true> - , placeholder_kzg_test_fixture< - algebra::curves::mnt6_298, - hashes::keccak_1600<256>, - hashes::keccak_1600<256>, - witness_columns_t, - public_columns_t, - constant_columns_t, - selector_columns_t, - usable_rows_t, - true> - /*, -- Not yet implemented - placeholder_kzg_test_fixture< - algebra::curves::mnt6_298, - hashes::poseidon>, - hashes::poseidon>, - witness_columns_t, - public_columns_t, - constant_columns_t, - selector_columns_t, - usable_rows_t, - 4, - true> - */ + placeholder_kzg_test_fixture< + algebra::curves::bls12<381>, + hashes::keccak_1600<256>, + hashes::keccak_1600<256>, + witness_columns_t, + public_columns_t, + constant_columns_t, + selector_columns_t, + usable_rows_t, + true> + /* + , placeholder_kzg_test_fixture< + algebra::curves::alt_bn128_254, + hashes::keccak_1600<256>, + hashes::keccak_1600<256>, + witness_columns_t, + public_columns_t, + constant_columns_t, + selector_columns_t, + usable_rows_t, + 4, true>*/ + , placeholder_kzg_test_fixture< + algebra::curves::mnt4_298, + hashes::keccak_1600<256>, + hashes::keccak_1600<256>, + witness_columns_t, + public_columns_t, + constant_columns_t, + selector_columns_t, + usable_rows_t, + true>, placeholder_kzg_test_fixture< + algebra::curves::mnt6_298, + hashes::keccak_1600<256>, + hashes::keccak_1600<256>, + witness_columns_t, + public_columns_t, + constant_columns_t, + selector_columns_t, + usable_rows_t, + true> + /*, -- Not yet implemented + placeholder_kzg_test_fixture< + algebra::curves::mnt6_298, + hashes::poseidon>, + hashes::poseidon>, + witness_columns_t, + public_columns_t, + constant_columns_t, + selector_columns_t, + usable_rows_t, + 4, + true> + */ >; -BOOST_AUTO_TEST_CASE_TEMPLATE(prover_test, F, TestFixtures) { - F fixture; - BOOST_CHECK(fixture.run_test()); -} + BOOST_AUTO_TEST_CASE_TEMPLATE(prover_test, F, TestFixtures) { + F fixture; + BOOST_CHECK(fixture.run_test()); + } + BOOST_AUTO_TEST_SUITE_END() template< - typename curve_type, - typename merkle_hash_type, - typename transcript_hash_type, - std::size_t WitnessColumns, - std::size_t PublicInputColumns, - std::size_t ConstantColumns, - std::size_t SelectorColumns, - std::size_t usable_rows_amount, - std::size_t permutation, - bool UseGrinding = false> + typename curve_type, + typename merkle_hash_type, + typename transcript_hash_type, + std::size_t WitnessColumns, + std::size_t PublicInputColumns, + std::size_t ConstantColumns, + std::size_t SelectorColumns, + std::size_t usable_rows_amount, + std::size_t permutation, + bool UseGrinding = false> struct placeholder_kzg_test_fixture_v2 : public test_initializer { using field_type = typename curve_type::scalar_field_type; @@ -1441,16 +1483,15 @@ struct placeholder_kzg_test_fixture_v2 : public test_initializer { using kzg_scheme_type = typename commitments::kzg_commitment_scheme_v2; using kzg_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params; - using policy_type = zk::snark::detail::placeholder_policy; + using policy_type = placeholder; using circuit_type = - circuit_description, - usable_rows_amount>; + circuit_description, + usable_rows_amount>; placeholder_kzg_test_fixture_v2() - : desc(WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns) - { + : desc(WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns) { } bool run_test() { @@ -1461,7 +1502,8 @@ struct placeholder_kzg_test_fixture_v2 : public test_initializer { desc.usable_rows_amount = circuit.usable_rows; std::size_t table_rows_log = std::log2(circuit.table_rows); - typename policy_type::constraint_system_type constraint_system(circuit.gates, circuit.copy_constraints, circuit.lookup_gates); + typename policy_type::constraint_system_type constraint_system(circuit.gates, circuit.copy_constraints, + circuit.lookup_gates); typename policy_type::variable_assignment_type assignments = circuit.table; bool verifier_res; @@ -1471,23 +1513,24 @@ struct placeholder_kzg_test_fixture_v2 : public test_initializer { kzg_scheme_type kzg_scheme(kzg_params); typename placeholder_public_preprocessor::preprocessed_data_type - kzg_preprocessed_public_data = - placeholder_public_preprocessor::process( - constraint_system, assignments.public_table(), desc, kzg_scheme - ); + kzg_preprocessed_public_data = + placeholder_public_preprocessor::process( + constraint_system, assignments.public_table(), desc, kzg_scheme + ); typename placeholder_private_preprocessor::preprocessed_data_type - kzg_preprocessed_private_data = placeholder_private_preprocessor::process( - constraint_system, assignments.private_table(), desc - ); + kzg_preprocessed_private_data = placeholder_private_preprocessor::process( + constraint_system, assignments.private_table(), desc + ); auto kzg_proof = placeholder_prover::process( - kzg_preprocessed_public_data, std::move(kzg_preprocessed_private_data), desc, constraint_system, kzg_scheme - ); + kzg_preprocessed_public_data, std::move(kzg_preprocessed_private_data), desc, constraint_system, + kzg_scheme + ); verifier_res = placeholder_verifier::process( kzg_preprocessed_public_data.common_data, kzg_proof, desc, constraint_system, kzg_scheme - ); + ); test_initializer::teardown(); return verifier_res; } @@ -1499,16 +1542,16 @@ struct placeholder_kzg_test_fixture_v2 : public test_initializer { BOOST_AUTO_TEST_SUITE(placeholder_circuit2_kzg_v2) using TestFixtures = boost::mpl::list< - placeholder_kzg_test_fixture_v2< - algebra::curves::bls12_381, - hashes::keccak_1600<256>, - hashes::keccak_1600<256>, - witness_columns_t, - public_columns_t, - constant_columns_t, - selector_columns_t, - usable_rows_t, - true> + placeholder_kzg_test_fixture_v2< + algebra::curves::bls12_381, + hashes::keccak_1600<256>, + hashes::keccak_1600<256>, + witness_columns_t, + public_columns_t, + constant_columns_t, + selector_columns_t, + usable_rows_t, + true> /* , placeholder_kzg_test_fixture< algebra::curves::alt_bn128_254, hashes::keccak_1600<256>, @@ -1519,45 +1562,44 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit2_kzg_v2) selector_columns_t, usable_rows_t, 4, true>*/ - , placeholder_kzg_test_fixture< - algebra::curves::mnt4_298, - hashes::keccak_1600<256>, - hashes::keccak_1600<256>, - witness_columns_t, - public_columns_t, - constant_columns_t, - selector_columns_t, - usable_rows_t, - true> - , placeholder_kzg_test_fixture_v2< - algebra::curves::mnt6_298, - hashes::keccak_1600<256>, - hashes::keccak_1600<256>, - witness_columns_t, - public_columns_t, - constant_columns_t, - selector_columns_t, - usable_rows_t, - true> - /*, -- Not yet implemented - placeholder_kzg_test_fixture< - algebra::curves::mnt6_298, - hashes::poseidon>, - hashes::poseidon>, - witness_columns_t, - public_columns_t, - constant_columns_t, - selector_columns_t, - usable_rows_t, - 4, - true> - */ + , placeholder_kzg_test_fixture< + algebra::curves::mnt4_298, + hashes::keccak_1600<256>, + hashes::keccak_1600<256>, + witness_columns_t, + public_columns_t, + constant_columns_t, + selector_columns_t, + usable_rows_t, + true>, placeholder_kzg_test_fixture_v2< + algebra::curves::mnt6_298, + hashes::keccak_1600<256>, + hashes::keccak_1600<256>, + witness_columns_t, + public_columns_t, + constant_columns_t, + selector_columns_t, + usable_rows_t, + true> + /*, -- Not yet implemented + placeholder_kzg_test_fixture< + algebra::curves::mnt6_298, + hashes::poseidon>, + hashes::poseidon>, + witness_columns_t, + public_columns_t, + constant_columns_t, + selector_columns_t, + usable_rows_t, + 4, + true> + */ >; -BOOST_AUTO_TEST_CASE_TEMPLATE(prover_test, F, TestFixtures) { - F fixture; - BOOST_CHECK(fixture.run_test()); -} + BOOST_AUTO_TEST_CASE_TEMPLATE(prover_test, F, TestFixtures) { + F fixture; + BOOST_CHECK(fixture.run_test()); + } BOOST_AUTO_TEST_SUITE_END()