From da7bdd2bf98c50d32383afe2f4910b8ccb34a5dd Mon Sep 17 00:00:00 2001 From: akokoshn Date: Thu, 11 Apr 2024 18:19:34 +0300 Subject: [PATCH] Disable default constructor of plonk variable --- .../arithmetization/plonk/copy_constraint.hpp | 68 +++++++------------ .../plonk/table_description.hpp | 4 -- .../snark/arithmetization/plonk/variable.hpp | 11 +-- .../plonk/placeholder/gates_argument.hpp | 4 -- .../plonk/placeholder/lookup_argument.hpp | 4 -- 5 files changed, 32 insertions(+), 59 deletions(-) diff --git a/include/nil/crypto3/zk/snark/arithmetization/plonk/copy_constraint.hpp b/include/nil/crypto3/zk/snark/arithmetization/plonk/copy_constraint.hpp index a7b8220af..1b0d9bdc8 100644 --- a/include/nil/crypto3/zk/snark/arithmetization/plonk/copy_constraint.hpp +++ b/include/nil/crypto3/zk/snark/arithmetization/plonk/copy_constraint.hpp @@ -38,67 +38,49 @@ namespace nil { template struct plonk_copy_constraint { - plonk_copy_constraint() = default; - plonk_copy_constraint(const plonk_copy_constraint &other){ - initialize(other.first, other.second); + plonk_copy_constraint() = delete; + plonk_copy_constraint(const plonk_copy_constraint &other): first(other.first), second(other.second) { + initialize(); } plonk_copy_constraint( const plonk_variable &_first, const plonk_variable &_second - ){ - initialize(_first, _second); + ): first(_first), second(_second) { + initialize(); } + plonk_variable first; plonk_variable second; bool operator==(const plonk_copy_constraint &other){ return ((first == other.first ) && (second == other.second)); } protected: - void initialize( - const plonk_variable &_first, - const plonk_variable &_second - ){ - BOOST_ASSERT(_first.relative == false); - BOOST_ASSERT(_second.relative == false); - if(_first.type == _second.type){ - if(_first.index < _second.index){ - first = plonk_variable(_first); - second = plonk_variable(_second); - } else if (_first.index > _second.index){ - first = plonk_variable(_second); - second = plonk_variable(_first); - } else if (_first.rotation < _second.rotation){ - first = plonk_variable(_first); - second = plonk_variable(_second); - } else if (_first.rotation > _second.rotation){ - first = plonk_variable(_second); - second = plonk_variable(_first); - } else { + void initialize() { + BOOST_ASSERT(first.relative == false); + BOOST_ASSERT(second.relative == false); + if(first.type == second.type){ + if(first.index > second.index || (first.index == second.index && first.rotation > second.rotation)){ + swap(); + } else if (first.index == second.index && first.rotation == second.rotation){ BOOST_ASSERT_MSG(false, "Copy constraint with equal variables"); } return; } - if( _first.type == plonk_variable::column_type::witness){ - first = plonk_variable(_first); - second = plonk_variable(_second); - } else if ( - _first.type == plonk_variable::column_type::public_input && - _second.type != plonk_variable::column_type::witness - ){ - first = plonk_variable(_first); - second = plonk_variable(_second); - } else if( - _first.type == plonk_variable::column_type::constant && - _second.type == plonk_variable::column_type::selector - ){ - first = plonk_variable(_first); - second = plonk_variable(_second); - } else { - first = plonk_variable(_second); - second = plonk_variable(_first); + if(first.type != plonk_variable::column_type::witness && + (first.type != plonk_variable::column_type::public_input || + second.type == plonk_variable::column_type::witness) && + (first.type != plonk_variable::column_type::constant || + second.type != plonk_variable::column_type::selector)){ + swap(); } return; } + + void swap() { + const auto tmp = first; + first = second; + second = tmp; + } }; template diff --git a/include/nil/crypto3/zk/snark/arithmetization/plonk/table_description.hpp b/include/nil/crypto3/zk/snark/arithmetization/plonk/table_description.hpp index ffc1694df..fa4ea3e16 100644 --- a/include/nil/crypto3/zk/snark/arithmetization/plonk/table_description.hpp +++ b/include/nil/crypto3/zk/snark/arithmetization/plonk/table_description.hpp @@ -74,10 +74,6 @@ namespace nil { return witness_columns + public_input_columns + a.index; case plonk_variable::column_type::selector: return witness_columns + public_input_columns + constant_columns + a.index; - case plonk_variable::column_type::uninitialized: - std::cerr << "trying to access uninitialized var!"; - std::abort(); - break; } /* unreachable*/ return std::numeric_limits::max(); diff --git a/include/nil/crypto3/zk/snark/arithmetization/plonk/variable.hpp b/include/nil/crypto3/zk/snark/arithmetization/plonk/variable.hpp index 6ffd7ee61..723f3c3af 100644 --- a/include/nil/crypto3/zk/snark/arithmetization/plonk/variable.hpp +++ b/include/nil/crypto3/zk/snark/arithmetization/plonk/variable.hpp @@ -69,7 +69,7 @@ namespace nil { using assignment_type = AssignmentType; enum column_type : std::uint8_t { - witness, public_input, constant, selector, uninitialized + witness, public_input, constant, selector }; /** @@ -80,7 +80,11 @@ namespace nil { bool relative; column_type type; - constexpr plonk_variable() : index(0), rotation(0), relative(false), type(column_type::uninitialized) {}; + constexpr plonk_variable() = delete; + + constexpr plonk_variable(const plonk_variable &other) : + index(other.index), + rotation(other.rotation), relative(other.relative), type(other.type) {}; constexpr plonk_variable(const std::size_t index, std::int32_t rotation, @@ -172,8 +176,7 @@ namespace nil { {plonk_variable::column_type::witness, "w"}, {plonk_variable::column_type::public_input, "pub"}, {plonk_variable::column_type::constant, "c"}, - {plonk_variable::column_type::selector, "sel"}, - {plonk_variable::column_type::uninitialized,"NaN"} + {plonk_variable::column_type::selector, "sel"} }; os << type_map[var.type] << "_" << var.index; if (!var.relative) { 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 92ce849d5..cb46911dd 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 @@ -107,10 +107,6 @@ namespace nil { case polynomial_dfs_variable_type::column_type::selector: assignment = assignments.selector(var.index); break; - case polynomial_dfs_variable_type::column_type::uninitialized: - std::cerr << "trying to access uninitialized var!"; - std::abort(); - break; } if (var.rotation != 0) { 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 1dafb48c8..dc1acca60 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 @@ -462,10 +462,6 @@ namespace nil { case DfsVariableType::column_type::selector: assignment = assignments.selector(var.index); break; - case DfsVariableType::column_type::uninitialized: - std::cerr << "trying to access uninitialized var!"; - std::abort(); - break; } if (var.rotation != 0) {