uscs.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2021 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 2020-2021 Nikita Kaskov <nbering@nil.foundation>
4 //
5 // MIT License
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in all
15 // copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 // SOFTWARE.
24 //---------------------------------------------------------------------------//
25 // @file Declaration of interfaces for:
26 // - a USCS constraint,
27 // - a USCS variable assignment, and
28 // - a USCS constraint system.
29 //
30 // Above, USCS stands for "Unitary-Square Constraint System".
31 //---------------------------------------------------------------------------//
32 
33 #ifndef CRYPTO3_ZK_USCS_HPP
34 #define CRYPTO3_ZK_USCS_HPP
35 
36 #include <cstdlib>
37 #include <vector>
38 
40 
41 namespace nil {
42  namespace crypto3 {
43  namespace zk {
44  namespace snark {
45 
46  /************************* USCS constraint ***********************************/
47 
57  template<typename FieldType>
59 
60  /************************* USCS variable assignment **************************/
61 
66  template<typename FieldType>
67  using uscs_primary_input = std::vector<typename FieldType::value_type>;
68 
69  template<typename FieldType>
70  using uscs_auxiliary_input = std::vector<typename FieldType::value_type>;
71 
72  template<typename FieldType>
73  using uscs_variable_assignment = std::vector<typename FieldType::value_type>;
74 
75  /************************* USCS constraint system ****************************/
76 
89  template<typename FieldType>
91 
92  std::size_t primary_input_size;
93  std::size_t auxiliary_input_size;
94 
95  std::vector<uscs_constraint<FieldType>> constraints;
96 
98 
99  std::size_t num_inputs() const {
100  return primary_input_size;
101  }
102 
103  std::size_t num_variables() const {
105  }
106 
107  std::size_t num_constraints() const {
108  return constraints.size();
109  }
110 
111  bool is_valid() const {
112  if (this->num_inputs() > this->num_variables())
113  return false;
114 
115  for (std::size_t c = 0; c < constraints.size(); ++c) {
116  if (!valid_vector(constraints[c], this->num_variables())) {
117  return false;
118  }
119  }
120 
121  return true;
122  }
123 
124  bool is_satisfied(const uscs_primary_input<FieldType> &primary_input,
125  const uscs_auxiliary_input<FieldType> &auxiliary_input) const {
126  assert(primary_input.size() == num_inputs());
127  assert(primary_input.size() + auxiliary_input.size() == num_variables());
128 
129  uscs_variable_assignment<FieldType> full_variable_assignment = primary_input;
130  full_variable_assignment.insert(
131  full_variable_assignment.end(), auxiliary_input.begin(), auxiliary_input.end());
132 
133  for (std::size_t c = 0; c < constraints.size(); ++c) {
134  typename FieldType::value_type res = constraints[c].evaluate(full_variable_assignment);
135  if (!(res.squared() == FieldType::value_type::one())) {
136  return false;
137  }
138  }
139 
140  return true;
141  }
142 
143  void add_constraint(const uscs_constraint<FieldType> &constraint) {
144  constraints.emplace_back(constraint);
145  }
146 
148  return (this->constraints == other.constraints &&
149  this->primary_input_size == other.primary_input_size &&
150  this->auxiliary_input_size == other.auxiliary_input_size);
151  }
152  };
153  } // namespace snark
154  } // namespace zk
155  } // namespace crypto3
156 } // namespace nil
157 
158 #endif // CRYPTO3_USCS_HPP
std::vector< typename FieldType::value_type > uscs_auxiliary_input
Definition: uscs.hpp:70
std::vector< typename FieldType::value_type > uscs_variable_assignment
Definition: uscs.hpp:73
std::vector< typename FieldType::value_type > uscs_primary_input
Definition: uscs.hpp:67
Definition: pair.hpp:31
void add_constraint(const uscs_constraint< FieldType > &constraint)
Definition: uscs.hpp:143
bool is_satisfied(const uscs_primary_input< FieldType > &primary_input, const uscs_auxiliary_input< FieldType > &auxiliary_input) const
Definition: uscs.hpp:124
bool operator==(const uscs_constraint_system< FieldType > &other) const
Definition: uscs.hpp:147
std::size_t auxiliary_input_size
Definition: uscs.hpp:93
std::size_t num_constraints() const
Definition: uscs.hpp:107
std::size_t num_variables() const
Definition: uscs.hpp:103
std::size_t num_inputs() const
Definition: uscs.hpp:99
bool is_valid() const
Definition: uscs.hpp:111
std::vector< uscs_constraint< FieldType > > constraints
Definition: uscs.hpp:95
std::size_t primary_input_size
Definition: uscs.hpp:92