Skip to content

Commit

Permalink
Final fixes, now everything builds.
Browse files Browse the repository at this point in the history
  • Loading branch information
martun committed Oct 6, 2023
1 parent 1c83a2c commit d02e4ac
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 69 deletions.
123 changes: 65 additions & 58 deletions include/nil/actor/zk/commitments/detail/polynomial/basic_fri.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,22 @@
#define ACTOR_ZK_COMMITMENTS_BASIC_FRI_HPP

#include <nil/crypto3/marshalling/algebra/types/field_element.hpp>
#include <nil/crypto3/container/merkle/proof.hpp>

#include <nil/actor/math/domains/evaluation_domain.hpp>
#include <nil/actor/math/algorithms/make_evaluation_domain.hpp>
#include <nil/actor/math/polynomial/polynomial.hpp>
#include <nil/actor/math/polynomial/polynomial_dfs.hpp>
#include <nil/actor/math/polynomial/lagrange_interpolation.hpp>
#include <nil/actor/math/domains/evaluation_domain.hpp>
#include <nil/actor/math/algorithms/make_evaluation_domain.hpp>

#include <nil/actor/container/merkle/tree.hpp>
#include <nil/crypto3/container/merkle/proof.hpp>

#include <nil/actor/zk/transcript/fiat_shamir.hpp>

#include <nil/actor/zk/commitments/type_traits.hpp>
#include <nil/actor/zk/commitments/detail/polynomial/fold_polynomial.hpp>
#include <nil/actor/zk/commitments/detail/polynomial/proof_of_work.hpp>


namespace nil {
namespace actor {
namespace zk {
Expand Down Expand Up @@ -98,6 +99,7 @@ namespace nil {
using precommitment_type = merkle_tree_type;
using commitment_type = typename precommitment_type::value_type;
using transcript_type = transcript::fiat_shamir_heuristic_sequential<TranscriptHashType>;
using polynomial_type = math::polynomial<typename FieldType::value_type>;

struct params_type {
using field_type = FieldType;
Expand All @@ -108,8 +110,7 @@ namespace nil {
using transcript_type = transcript::fiat_shamir_heuristic_sequential<TranscriptHashType>;

bool operator==(const params_type &rhs) const {
return r == rhs.r && max_degree == rhs.max_degree && D == rhs.D &&
step_list == rhs.step_list;
return r == rhs.r && max_degree == rhs.max_degree && D == rhs.D && step_list == rhs.step_list;
}

bool operator!=(const params_type &rhs) const {
Expand Down Expand Up @@ -250,7 +251,9 @@ namespace nil {
typename FRI::field_type,
typename FRI::merkle_tree_hash_type,
typename FRI::transcript_hash_type,
FRI::lambda, FRI::m, FRI::batches_num>,
FRI::lambda, FRI::m,
FRI::use_grinding, typename FRI::grinding_type
>,
FRI>::value,
bool>::type = true>
static future<typename FRI::precommitment_type>
Expand Down Expand Up @@ -325,22 +328,22 @@ namespace nil {
}

template<typename FRI, typename ContainerType,
typename std::enable_if<
std::is_base_of<
commitments::detail::basic_batched_fri<
typename FRI::field_type, typename FRI::merkle_tree_hash_type,
typename FRI::transcript_hash_type, FRI::lambda, FRI::m,
FRI::use_grinding, typename FRI::grinding_type>,
FRI>::value,
bool>::type = true>
static future<typename std::enable_if<
(std::is_same<typename ContainerType::value_type, math::polynomial_dfs<typename FRI::field_type::value_type>>::value),
typename FRI::precommitment_type>::type>
precommit(ContainerType poly,
std::shared_ptr<math::evaluation_domain<typename FRI::field_type>>
D,
const std::size_t fri_step
) {
typename std::enable_if<
std::is_base_of<
commitments::detail::basic_batched_fri<
typename FRI::field_type, typename FRI::merkle_tree_hash_type,
typename FRI::transcript_hash_type, FRI::lambda, FRI::m,
FRI::use_grinding, typename FRI::grinding_type>,
FRI>::value,
bool>::type = true>
static future<typename std::enable_if<
(std::is_same<typename ContainerType::value_type, math::polynomial_dfs<typename FRI::field_type::value_type>>::value),
typename FRI::precommitment_type>::type>
precommit(ContainerType poly,
std::shared_ptr<math::evaluation_domain<typename FRI::field_type>>
D,
const std::size_t fri_step
) {

#ifdef ZK_PLACEHOLDER_PROFILING_ENABLED
auto begin = std::chrono::high_resolution_clock::now();
Expand Down Expand Up @@ -398,23 +401,23 @@ namespace nil {
}

template<typename FRI, typename ContainerType,
typename std::enable_if<
std::is_base_of<
commitments::detail::basic_batched_fri<
typename FRI::field_type, typename FRI::merkle_tree_hash_type,
typename FRI::transcript_hash_type,
FRI::lambda, FRI::m,
FRI::use_grinding, typename FRI::grinding_type>,
FRI>::value,
bool>::type = true>
typename std::enable_if<
std::is_base_of<
commitments::detail::basic_batched_fri<
typename FRI::field_type, typename FRI::merkle_tree_hash_type,
typename FRI::transcript_hash_type,
FRI::lambda, FRI::m,
FRI::use_grinding, typename FRI::grinding_type>,
FRI>::value,
bool>::type = true>
static future<typename std::enable_if<
(std::is_same<typename ContainerType::value_type,
math::polynomial<typename FRI::field_type::value_type>>::value),
typename FRI::precommitment_type>::type>
(std::is_same<typename ContainerType::value_type,
math::polynomial<typename FRI::field_type::value_type>>::value),
typename FRI::precommitment_type>::type>
precommit(const ContainerType &poly,
std::shared_ptr<math::evaluation_domain<typename FRI::field_type>>
D,
const std::size_t fri_step
std::shared_ptr<math::evaluation_domain<typename FRI::field_type>>
D,
const std::size_t fri_step
) {
std::size_t list_size = poly.size();
std::vector<math::polynomial_dfs<typename FRI::field_type::value_type>> poly_dfs(list_size);
Expand Down Expand Up @@ -563,29 +566,32 @@ namespace nil {
BOOST_ASSERT(false);
}
}

return correct_order_idx;
}

//template<typename FRI, typename PolynomialType>

template<typename FRI, typename PolynomialType,
typename std::enable_if<
std::is_base_of<
commitments::detail::basic_batched_fri<
typename FRI::field_type, typename FRI::merkle_tree_hash_type,
typename FRI::transcript_hash_type,
FRI::lambda, FRI::m,
FRI::use_grinding, typename FRI::grinding_type>,
FRI>::value,
bool>::type = true>
std::is_base_of<
commitments::detail::basic_batched_fri<
typename FRI::field_type, typename FRI::merkle_tree_hash_type,
typename FRI::transcript_hash_type,
FRI::lambda, FRI::m,
FRI::use_grinding, typename FRI::grinding_type>,
FRI>::value,
bool>::type = true>
static typename FRI::proof_type proof_eval(
std::array<std::vector<PolynomialType>, FRI::batches_num> &g,
std::map<std::size_t, std::vector<PolynomialType>> &g,
const PolynomialType combined_Q,
const std::array<typename FRI::precommitment_type, FRI::batches_num> &precommitments,
const std::map<std::size_t, typename FRI::precommitment_type> &precommitments,
const typename FRI::precommitment_type &combined_Q_precommitment,
const typename FRI::params_type &fri_params,
typename FRI::transcript_type &transcript
) {
typename FRI::proof_type proof;

BOOST_ASSERT(check_step_list<FRI>(fri_params));
// TODO: add necessary checks
//BOOST_ASSERT(check_initial_precommitment<FRI>(precommitments, fri_params));
Expand All @@ -597,9 +603,9 @@ namespace nil {
for (int i = 0; i < g[k].size(); ++i ){
// If LPC works properly this if is never executed.
if (g[k][i].size() != fri_params.D[0]->size()) {
g[k][i].resize(fri_params.D[0]->size()).get();
}
}
g[k][i].resize(fri_params.D[0]->size()).get();
}
}
}
}

Expand All @@ -613,23 +619,24 @@ namespace nil {
std::vector<PolynomialType> fs;
std::size_t t = 0;

for( std::size_t i = 0; i < fri_params.step_list.size(); i++){
for (std::size_t i = 0; i < fri_params.step_list.size(); i++) {
fs.push_back(f);
fri_trees.push_back(precommitment);
fri_roots.push_back(commit<FRI>(precommitment));
transcript(commit<FRI>(precommitment));
for( std::size_t step_i = 0; step_i < fri_params.step_list[i]; step_i++, t++ ){
for (std::size_t step_i = 0; step_i < fri_params.step_list[i]; step_i++, t++) {
alphas.push_back(transcript.template challenge<typename FRI::field_type>());
// Calculate next f.
if constexpr (std::is_same<math::polynomial_dfs<typename FRI::field_type::value_type>,
PolynomialType>::value) {
f = commitments::detail::fold_polynomial<typename FRI::field_type>(f, alphas[t], fri_params.D[t]);
PolynomialType>::value) {
f = commitments::detail::fold_polynomial<typename FRI::field_type>(f, alphas[t],
fri_params.D[t]);
} else {
f = commitments::detail::fold_polynomial<typename FRI::field_type>(f, alphas[t]);
}
}
if( i!= fri_params.step_list.size() - 1)
precommitment = precommit<FRI>(f, fri_params.D[t], fri_params.step_list[i+1]).get();
if (i != fri_params.step_list.size() - 1)
precommitment = precommit<FRI>(f, fri_params.D[t], fri_params.step_list[i + 1]).get();
}
fs.push_back(f);
math::polynomial<typename FRI::field_type::value_type> final_polynomial;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ namespace nil {
class eval_storage{
private:
std::map<std::size_t, std::vector<std::vector<typename FieldType::value_type>>> z;

public:
using field_type = FieldType;

bool operator==(const eval_storage& other) const{
return this->z == other.z;
}
Expand Down Expand Up @@ -95,4 +98,4 @@ namespace nil {
}
}
}
#endif
#endif
9 changes: 6 additions & 3 deletions include/nil/actor/zk/commitments/polynomial/lpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ namespace nil {
using transcript_hash_type = typename LPCScheme::transcript_hash_type;
using poly_type = PolynomialType;
using lpc = LPCScheme;
using eval_storage_type = typename LPCScheme::eval_storage_type;

private:
std::map<std::size_t, precommitment_type> _trees;
typename fri_type::params_type _fri_params;
Expand All @@ -76,7 +78,7 @@ namespace nil {

commitment_type commit(std::size_t index){
this->state_commited(index);
_trees[index] = nil::actor::zk::algorithms::precommit<fri_type>(this->_polys[index], _fri_params.D[0], _fri_params.step_list.front());
_trees[index] = nil::actor::zk::algorithms::precommit<fri_type>(this->_polys[index], _fri_params.D[0], _fri_params.step_list.front()).get();
return _trees[index].root();
}

Expand Down Expand Up @@ -169,7 +171,7 @@ namespace nil {
combined_Q,
_fri_params.D[0],
_fri_params.step_list.front()
);
).get();

typename fri_type::proof_type fri_proof = nil::actor::zk::algorithms::proof_eval<
fri_type, poly_type
Expand Down Expand Up @@ -347,6 +349,7 @@ namespace nil {
using params_type = typename basic_fri::params_type;
using transcript_type = transcript::fiat_shamir_heuristic_sequential<typename LPCParams::transcript_hash_type>;
using transcript_hash_type = typename LPCParams::transcript_hash_type;
using eval_storage_type = eval_storage<field_type>;

struct proof_type {
bool operator==(const proof_type &rhs) const {
Expand All @@ -359,7 +362,7 @@ namespace nil {

typedef std::vector<std::vector<typename FieldType::value_type>> z_type;

eval_storage<field_type> z;
eval_storage_type z;
typename basic_fri::proof_type fri_proof;
};
};
Expand Down
4 changes: 4 additions & 0 deletions include/nil/actor/zk/math/expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ namespace nil {
class expression {
public:
typedef term<VariableType> term_type;
typedef pow_operation<VariableType> pow_operation_type;
typedef binary_arithmetic_operation<VariableType> binary_arithmetic_operation_type;
typedef VariableType variable_type;
typedef typename VariableType::assignment_type assignment_type;

Expand Down Expand Up @@ -335,6 +337,8 @@ namespace nil {
class binary_arithmetic_operation
{
public:
using ArithmeticOperatorType = ArithmeticOperator;

binary_arithmetic_operation(
const expression<VariableType>& expr_left,
const expression<VariableType>& expr_right,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ namespace nil {
template<typename FieldType, typename ArithmetizationParams>
struct plonk_constraint_system {
typedef std::vector<plonk_gate<FieldType, plonk_constraint<FieldType>>> gates_container_type;
typedef plonk_variable<typename FieldType::value_type> variable_type;
typedef std::vector<plonk_copy_constraint<FieldType>> copy_constraints_container_type;
typedef std::vector<plonk_lookup_gate<FieldType, plonk_lookup_constraint<FieldType>>> lookup_gates_container_type;
typedef plonk_lookup_table<FieldType> lookup_table_type;
typedef std::vector<lookup_table_type> lookup_tables_type;
typedef std::vector<plonk_variable<typename FieldType::value_type>> public_input_gate_type;

protected:
gates_container_type _gates;
Expand Down
Loading

0 comments on commit d02e4ac

Please sign in to comment.