From 68a33bc7b264157bb480460f023941120d00834b Mon Sep 17 00:00:00 2001 From: Martun Karapetyan Date: Tue, 1 Aug 2023 01:11:28 +0400 Subject: [PATCH] Few changes related to changes in zk. (#41) --- .../math/types/flat_expression.hpp | 12 ++--- .../crypto3/marshalling/math/types/term.hpp | 14 ++--- test/plonk_constraint_system.cpp | 51 ++----------------- test/plonk_gates.cpp | 8 +-- 4 files changed, 20 insertions(+), 65 deletions(-) diff --git a/include/nil/crypto3/marshalling/math/types/flat_expression.hpp b/include/nil/crypto3/marshalling/math/types/flat_expression.hpp index 888768faeb..c5a05befbf 100644 --- a/include/nil/crypto3/marshalling/math/types/flat_expression.hpp +++ b/include/nil/crypto3/marshalling/math/types/flat_expression.hpp @@ -118,7 +118,7 @@ namespace nil { class expression_flattener : public boost::static_visitor { public: expression_flattener(const math::expression& expr) { - boost::apply_visitor(*this, expr.expr); + boost::apply_visitor(*this, expr.get_expr()); } const flat_expression& get_result() const { @@ -134,8 +134,8 @@ namespace nil { void operator()( const math::pow_operation& pow) { - boost::apply_visitor(*this, pow.expr.expr); - result.pow_operations.push_back({pow.power, result.root_type, result.root_index}); + boost::apply_visitor(*this, pow.get_expr().get_expr()); + result.pow_operations.push_back({pow.get_power(), result.root_type, result.root_index}); result.root_type = flat_node_type::POWER; result.root_index = result.pow_operations.size() - 1; @@ -144,13 +144,13 @@ namespace nil { void operator()( const math::binary_arithmetic_operation& op) { flat_binary_arithmetic_operation flat_op; - flat_op.op = op.op; + flat_op.op = op.get_op(); - boost::apply_visitor(*this, op.expr_left.expr); + boost::apply_visitor(*this, op.get_expr_left().get_expr()); flat_op.left_type = result.root_type; flat_op.left_index = result.root_index; - boost::apply_visitor(*this, op.expr_right.expr); + boost::apply_visitor(*this, op.get_expr_right().get_expr()); flat_op.right_type = result.root_type; flat_op.right_index = result.root_index; diff --git a/include/nil/crypto3/marshalling/math/types/term.hpp b/include/nil/crypto3/marshalling/math/types/term.hpp index 1de7534b96..b7ada588ff 100644 --- a/include/nil/crypto3/marshalling/math/types/term.hpp +++ b/include/nil/crypto3/marshalling/math/types/term.hpp @@ -84,12 +84,12 @@ namespace nil { nil::marshalling::option::sequence_size_field_prefix>; variable_vector_marshalling_type filled_vars; - for (const auto &var : t.vars) { + for (const auto &var : t.get_vars()) { filled_vars.value().push_back( fill_variable(var)); } - return result_type(std::make_tuple(field_element_marhsalling_type(t.coeff), filled_vars)); + return result_type(std::make_tuple(field_element_marhsalling_type(t.get_coeff()), filled_vars)); } template @@ -99,14 +99,14 @@ namespace nil { NonLinearTerm>::type make_term(const typename term, NonLinearTerm>::type &filled_term) { - NonLinearTerm t; - t.coeff = std::get<0>(filled_term.value()).value(); - t.vars.reserve(std::get<1>(filled_term.value()).value().size()); + std::vector vars; + auto coeff = std::get<0>(filled_term.value()).value(); + vars.reserve(std::get<1>(filled_term.value()).value().size()); for (auto i = 0; i < std::get<1>(filled_term.value()).value().size(); i++) { - t.vars.emplace_back(make_variable( + vars.emplace_back(make_variable( std::get<1>(filled_term.value()).value().at(i))); } - return t; + return NonLinearTerm(vars, coeff); } } // namespace types } // namespace marshalling diff --git a/test/plonk_constraint_system.cpp b/test/plonk_constraint_system.cpp index d6e4b13bcc..89ab01509c 100644 --- a/test/plonk_constraint_system.cpp +++ b/test/plonk_constraint_system.cpp @@ -33,51 +33,6 @@ using namespace nil::crypto3; using namespace nil::crypto3::marshalling; -template -bool are_variables_equal(const nil::crypto3::zk::snark::plonk_variable &lhs, - const nil::crypto3::zk::snark::plonk_variable &rhs) { - if (lhs.index != rhs.index) - return false; - if (lhs.relative != rhs.relative) - return false; - if (lhs.rotation != rhs.rotation) - return false; - if (lhs.type != rhs.type) - return false; - return true; -} - -template -bool are_terms_equal( - const nil::crypto3::math::term> &lhs, - const nil::crypto3::math::term> &rhs) { - if (lhs.coeff != rhs.coeff) { - return false; - } - if (lhs.vars.size() != rhs.vars.size()) { - return false; - } - for (auto i = 0; i < lhs.vars.size(); i++) { - if (!are_variables_equal(lhs.vars[i], rhs.vars[i])) { - return false; - } - } - return true; -} - -template -bool are_expressions_equal( - const nil::crypto3::math::expression> &lhs, - const nil::crypto3::math::expression> &rhs) { - if (lhs.terms.size() != rhs.terms.size()) - return false; - for (auto i = 0; i < lhs.terms.size(); i++) { - if (!are_terms_equal(lhs.terms[i], rhs.terms[i])) - return false; - } - return true; -} - template bool are_plonk_gates_equal(const nil::crypto3::zk::snark::plonk_gate>> &lhs, const nil::crypto3::zk::snark::plonk_gate>> &rhs) { @@ -86,7 +41,7 @@ bool are_plonk_gates_equal(const nil::crypto3::zk::snark::plonk_gate(s1.copy_constraints()[i]), std::get<0>(s2.copy_constraints()[i]))) return false; - if(!are_variables_equal(std::get<1>(s1.copy_constraints()[i]), std::get<1>(s2.copy_constraints()[i]))) return false; + if(std::get<0>(s1.copy_constraints()[i]) != std::get<0>(s2.copy_constraints()[i])) return false; + if(std::get<1>(s1.copy_constraints()[i]) != std::get<1>(s2.copy_constraints()[i])) return false; } // TODO check lookup gates are equal diff --git a/test/plonk_gates.cpp b/test/plonk_gates.cpp index 82b878a44f..21c051800e 100644 --- a/test/plonk_gates.cpp +++ b/test/plonk_gates.cpp @@ -144,13 +144,13 @@ PlonkVariable generate_random_plonk_variable() { template nil::crypto3::math::term generate_random_plonk_term(std::size_t vars_n) { - nil::crypto3::math::term result; + result; nil::crypto3::random::algebraic_random_device d; - result.coeff = d(); + std::vector vars; for (auto i = 0; i < vars_n; i++) { - result.vars.emplace_back(generate_random_plonk_variable()); + vars.emplace_back(generate_random_plonk_variable()); } - return result; + return nil::crypto3::math::term(vars, d()); } template