r1cs.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 //
27 // - a R1CS constraint,
28 // - a R1CS variable assignment, and
29 // - a R1CS constraint system.
30 //
31 // Above, R1CS stands for "Rank-1 Constraint System".
32 //---------------------------------------------------------------------------//
33 
34 #ifndef CRYPTO3_ZK_R1CS_HPP
35 #define CRYPTO3_ZK_R1CS_HPP
36 
37 #include <cstdlib>
38 #include <vector>
39 
41 
42 namespace nil {
43  namespace crypto3 {
44  namespace zk {
45  namespace snark {
46 
47  /************************* R1CS constraint ***********************************/
48 
59  template<typename FieldType>
60  struct r1cs_constraint {
61  typedef FieldType field_type;
62 
64 
69  a(a),
70  b(b), c(c) {
71  }
72 
73  r1cs_constraint(const std::initializer_list<linear_combination<FieldType>> &A,
74  const std::initializer_list<linear_combination<FieldType>> &B,
75  const std::initializer_list<linear_combination<FieldType>> &C) {
76  for (auto lc_A : A) {
77  a.terms.insert(a.terms.end(), lc_A.terms.begin(), lc_A.terms.end());
78  }
79  for (auto lc_B : B) {
80  b.terms.insert(b.terms.end(), lc_B.terms.begin(), lc_B.terms.end());
81  }
82  for (auto lc_C : C) {
83  c.terms.insert(c.terms.end(), lc_C.terms.begin(), lc_C.terms.end());
84  }
85  }
86 
87  bool operator==(const r1cs_constraint<FieldType> &other) const {
88  return (this->a == other.a && this->b == other.b && this->c == other.c);
89  }
90  };
91 
92  /************************* R1CS variable assignment **************************/
93 
99  /* TODO: specify that it does *NOT* include the constant 1 */
100  template<typename FieldType>
101  using r1cs_primary_input = std::vector<typename FieldType::value_type>;
102 
103  template<typename FieldType>
104  using r1cs_auxiliary_input = std::vector<typename FieldType::value_type>;
105 
106  template<typename FieldType>
107  using r1cs_variable_assignment = std::vector<typename FieldType::value_type>;
108 
109  /************************* R1CS constraint system ****************************/
110 
123  template<typename FieldType>
125  typedef FieldType field_type;
126 
127  std::size_t primary_input_size;
128  std::size_t auxiliary_input_size;
129 
130  std::vector<r1cs_constraint<FieldType>> constraints;
131 
133  }
134 
135  std::size_t num_inputs() const {
136  return primary_input_size;
137  }
138 
139  std::size_t num_variables() const {
141  }
142 
143  std::size_t num_constraints() const {
144  return constraints.size();
145  }
146 
147  bool is_valid() const {
148  if (this->num_inputs() > this->num_variables())
149  return false;
150 
151  for (std::size_t c = 0; c < constraints.size(); ++c) {
152  if (!(constraints[c].a.is_valid(this->num_variables()) &&
153  constraints[c].b.is_valid(this->num_variables()) &&
154  constraints[c].c.is_valid(this->num_variables()))) {
155  return false;
156  }
157  }
158 
159  return true;
160  }
161 
162  bool is_satisfied(const r1cs_primary_input<FieldType> &primary_input,
163  const r1cs_auxiliary_input<FieldType> &auxiliary_input) const {
164  assert(primary_input.size() == num_inputs());
165  assert(primary_input.size() + auxiliary_input.size() == num_variables());
166 
167  r1cs_variable_assignment<FieldType> full_variable_assignment = primary_input;
168  full_variable_assignment.insert(
169  full_variable_assignment.end(), auxiliary_input.begin(), auxiliary_input.end());
170 
171  for (std::size_t c = 0; c < constraints.size(); ++c) {
172  const typename FieldType::value_type ares =
173  constraints[c].a.evaluate(full_variable_assignment);
174  const typename FieldType::value_type bres =
175  constraints[c].b.evaluate(full_variable_assignment);
176  const typename FieldType::value_type cres =
177  constraints[c].c.evaluate(full_variable_assignment);
178 
179  if (!(ares * bres == cres)) {
180  return false;
181  }
182  }
183 
184  return true;
185  }
186 
188  constraints.emplace_back(c);
189  }
190 
192  std::vector<bool> touched_by_A(this->num_variables() + 1, false),
193  touched_by_B(this->num_variables() + 1, false);
194 
195  for (std::size_t i = 0; i < this->constraints.size(); ++i) {
196  for (std::size_t j = 0; j < this->constraints[i].a.terms.size(); ++j) {
197  touched_by_A[this->constraints[i].a.terms[j].index] = true;
198  }
199 
200  for (std::size_t j = 0; j < this->constraints[i].b.terms.size(); ++j) {
201  touched_by_B[this->constraints[i].b.terms[j].index] = true;
202  }
203  }
204 
205  std::size_t non_zero_A_count = 0, non_zero_B_count = 0;
206  for (std::size_t i = 0; i < this->num_variables() + 1; ++i) {
207  non_zero_A_count += touched_by_A[i] ? 1 : 0;
208  non_zero_B_count += touched_by_B[i] ? 1 : 0;
209  }
210 
211  if (non_zero_B_count > non_zero_A_count) {
212  for (std::size_t i = 0; i < this->constraints.size(); ++i) {
213  std::swap(this->constraints[i].a, this->constraints[i].b);
214  }
215  }
216  }
217 
219  return (this->constraints == other.constraints &&
220  this->primary_input_size == other.primary_input_size &&
221  this->auxiliary_input_size == other.auxiliary_input_size);
222  }
223  };
224 
225  } // namespace snark
226  } // namespace zk
227  } // namespace crypto3
228 } // namespace nil
229 
230 #endif // CRYPTO3_R1CS_HPP
std::vector< typename FieldType::value_type > r1cs_auxiliary_input
Definition: r1cs.hpp:104
std::vector< typename FieldType::value_type > r1cs_primary_input
Definition: r1cs.hpp:101
std::vector< typename FieldType::value_type > r1cs_variable_assignment
Definition: r1cs.hpp:107
Definition: pair.hpp:31
bool is_satisfied(const r1cs_primary_input< FieldType > &primary_input, const r1cs_auxiliary_input< FieldType > &auxiliary_input) const
Definition: r1cs.hpp:162
std::vector< r1cs_constraint< FieldType > > constraints
Definition: r1cs.hpp:130
r1cs_constraint_system()
Definition: r1cs.hpp:132
void add_constraint(const r1cs_constraint< FieldType > &c)
Definition: r1cs.hpp:187
std::size_t num_constraints() const
Definition: r1cs.hpp:143
FieldType field_type
Definition: r1cs.hpp:125
std::size_t primary_input_size
Definition: r1cs.hpp:127
std::size_t num_inputs() const
Definition: r1cs.hpp:135
std::size_t num_variables() const
Definition: r1cs.hpp:139
bool is_valid() const
Definition: r1cs.hpp:147
void swap_AB_if_beneficial()
Definition: r1cs.hpp:191
bool operator==(const r1cs_constraint_system< FieldType > &other) const
Definition: r1cs.hpp:218
std::size_t auxiliary_input_size
Definition: r1cs.hpp:128
linear_combination< FieldType > c
Definition: r1cs.hpp:63
bool operator==(const r1cs_constraint< FieldType > &other) const
Definition: r1cs.hpp:87
linear_combination< FieldType > a
Definition: r1cs.hpp:63
linear_combination< FieldType > b
Definition: r1cs.hpp:63
r1cs_constraint(const linear_combination< FieldType > &a, const linear_combination< FieldType > &b, const linear_combination< FieldType > &c)
Definition: r1cs.hpp:66
FieldType field_type
Definition: r1cs.hpp:61
r1cs_constraint(const std::initializer_list< linear_combination< FieldType >> &A, const std::initializer_list< linear_combination< FieldType >> &B, const std::initializer_list< linear_combination< FieldType >> &C)
Definition: r1cs.hpp:73
r1cs_constraint()
Definition: r1cs.hpp:65