Skip to content
This repository has been archived by the owner on Feb 17, 2025. It is now read-only.

Commit

Permalink
Few changes related to changes in zk. (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
martun authored Jul 31, 2023
1 parent 763c20f commit 68a33bc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 65 deletions.
12 changes: 6 additions & 6 deletions include/nil/crypto3/marshalling/math/types/flat_expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ namespace nil {
class expression_flattener : public boost::static_visitor<void> {
public:
expression_flattener(const math::expression<VariableType>& expr) {
boost::apply_visitor(*this, expr.expr);
boost::apply_visitor(*this, expr.get_expr());
}

const flat_expression<VariableType>& get_result() const {
Expand All @@ -134,8 +134,8 @@ namespace nil {

void operator()(
const math::pow_operation<VariableType>& 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;
Expand All @@ -144,13 +144,13 @@ namespace nil {
void operator()(
const math::binary_arithmetic_operation<VariableType>& 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;

Expand Down
14 changes: 7 additions & 7 deletions include/nil/crypto3/marshalling/math/types/term.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ namespace nil {
nil::marshalling::option::sequence_size_field_prefix<size_t_marshalling_type>>;

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<typename NonLinearTerm::variable_type, Endianness>(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<typename NonLinearTerm, typename Endianness>
Expand All @@ -99,14 +99,14 @@ namespace nil {
NonLinearTerm>::type
make_term(const typename term<nil::marshalling::field_type<Endianness>,
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<typename NonLinearTerm::variable_type> 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<typename NonLinearTerm::variable_type, Endianness>(
vars.emplace_back(make_variable<typename NonLinearTerm::variable_type, Endianness>(
std::get<1>(filled_term.value()).value().at(i)));
}
return t;
return NonLinearTerm(vars, coeff);
}
} // namespace types
} // namespace marshalling
Expand Down
51 changes: 3 additions & 48 deletions test/plonk_constraint_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,51 +33,6 @@
using namespace nil::crypto3;
using namespace nil::crypto3::marshalling;

template<typename Field>
bool are_variables_equal(const nil::crypto3::zk::snark::plonk_variable<typename Field::value_type> &lhs,
const nil::crypto3::zk::snark::plonk_variable<typename Field::value_type> &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<typename Field>
bool are_terms_equal(
const nil::crypto3::math::term<nil::crypto3::zk::snark::plonk_variable<typename Field::value_type>> &lhs,
const nil::crypto3::math::term<nil::crypto3::zk::snark::plonk_variable<typename Field::value_type>> &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<typename Field>
bool are_expressions_equal(
const nil::crypto3::math::expression<nil::crypto3::zk::snark::plonk_variable<typename Field::value_type>> &lhs,
const nil::crypto3::math::expression<nil::crypto3::zk::snark::plonk_variable<typename Field::value_type>> &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<typename Field>
bool are_plonk_gates_equal(const nil::crypto3::zk::snark::plonk_gate<Field, nil::crypto3::zk::snark::plonk_constraint<Field, nil::crypto3::zk::snark::plonk_variable<typename Field::value_type>>> &lhs,
const nil::crypto3::zk::snark::plonk_gate<Field, nil::crypto3::zk::snark::plonk_constraint<Field, nil::crypto3::zk::snark::plonk_variable<typename Field::value_type>>> &rhs) {
Expand All @@ -86,7 +41,7 @@ bool are_plonk_gates_equal(const nil::crypto3::zk::snark::plonk_gate<Field, nil:
if (lhs.constraints.size() != rhs.constraints.size())
return false;
for (auto i = 0; i < lhs.constraints.size(); i++) {
if (!are_expressions_equal(lhs.constraints[i], rhs.constraints[i]))
if (lhs.constraints[i] != rhs.constraints[i])
return false;
}
return true;
Expand All @@ -101,8 +56,8 @@ bool are_constraint_systems_equal(const ConstraintSystem &s1, const ConstraintSy

if(s1.copy_constraints().size() != s2.copy_constraints().size()) return false;
for(size_t i = 0; i < s1.copy_constraints().size(); i++){
if(!are_variables_equal(std::get<0>(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
Expand Down
8 changes: 4 additions & 4 deletions test/plonk_gates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ PlonkVariable generate_random_plonk_variable() {

template<typename PlonkVariable>
nil::crypto3::math::term<PlonkVariable> generate_random_plonk_term(std::size_t vars_n) {
nil::crypto3::math::term<PlonkVariable> result;
result;
nil::crypto3::random::algebraic_random_device<typename PlonkVariable::field_type> d;
result.coeff = d();
std::vector<PlonkVariable> vars;
for (auto i = 0; i < vars_n; i++) {
result.vars.emplace_back(generate_random_plonk_variable<PlonkVariable>());
vars.emplace_back(generate_random_plonk_variable<PlonkVariable>());
}
return result;
return nil::crypto3::math::term<PlonkVariable>(vars, d());
}

template<typename PlonkVariable>
Expand Down

0 comments on commit 68a33bc

Please sign in to comment.