disjunction.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 
26 #ifndef CRYPTO3_ZK_BLUEPRINT_DISJUNCTION_COMPONENT_HPP
27 #define CRYPTO3_ZK_BLUEPRINT_DISJUNCTION_COMPONENT_HPP
28 
29 #include <cassert>
30 #include <memory>
31 
33 
34 #include <nil/crypto3/multiprecision/number.hpp>
36 
37 namespace nil {
38  namespace crypto3 {
39  namespace zk {
40  namespace components {
41 
42  /*
43  the components below are Fp specific:
44  I * X = R
45  (1-R) * X = 0
46 
47  if X = 0 then R = 0
48  if X != 0 then R = 1 and I = X^{-1}
49  */
50 
51  template<typename FieldType>
52  class disjunction : public component<FieldType> {
53  private:
55 
56  public:
59 
63  component<FieldType>(bp),
65  assert(inputs.size() >= 1);
66  inv.allocate(bp);
67  }
68 
70  /* inv * sum = output */
72  a1.add_term(inv);
73  for (std::size_t i = 0; i < inputs.size(); ++i) {
74  b1.add_term(inputs[i]);
75  }
76  c1.add_term(output);
77 
78  this->bp.add_r1cs_constraint(snark::r1cs_constraint<FieldType>(a1, b1, c1));
79 
80  /* (1-output) * sum = 0 */
83  a2.add_term(output, -1);
84  for (std::size_t i = 0; i < inputs.size(); ++i) {
85  b2.add_term(inputs[i]);
86  }
88 
89  this->bp.add_r1cs_constraint(snark::r1cs_constraint<FieldType>(a2, b2, c2));
90  }
91 
93  typename FieldType::value_type sum = FieldType::value_type::zero();
94 
95  for (std::size_t i = 0; i < inputs.size(); ++i) {
96  sum += this->bp.val(inputs[i]);
97  }
98 
99  if (sum.is_zero()) {
100  this->bp.val(inv) = FieldType::value_type::zero();
101  this->bp.val(output) = FieldType::value_type::zero();
102  } else {
103  this->bp.val(inv) = sum.inversed();
104  this->bp.val(output) = FieldType::value_type::one();
105  }
106  }
107  };
108 
109  } // namespace components
110  } // namespace zk
111  } // namespace crypto3
112 } // namespace nil
113 #endif // CRYPTO3_ZK_BLUEPRINT_DISJUNCTION_COMPONENT_HPP
Definition: blueprint_linear_combination.hpp:47
Definition: blueprint_variable.hpp:57
Definition: blueprint_variable.hpp:46
void allocate(blueprint< FieldType > &bp)
Definition: blueprint_variable.hpp:51
Definition: blueprint.hpp:46
Definition: component.hpp:37
blueprint< FieldType > & bp
Definition: component.hpp:39
Definition: disjunction.hpp:52
const blueprint_variable< FieldType > output
Definition: disjunction.hpp:58
const blueprint_variable_vector< FieldType > inputs
Definition: disjunction.hpp:57
disjunction(blueprint< FieldType > &bp, const blueprint_variable_vector< FieldType > &inputs, const blueprint_variable< FieldType > &output)
Definition: disjunction.hpp:60
void generate_r1cs_witness()
Definition: disjunction.hpp:92
void generate_r1cs_constraints()
Definition: disjunction.hpp:69
constexpr T sum(const vector< T, N > &v)
computes the sum of elements
Definition: algebra/include/nil/crypto3/algebra/vector/math.hpp:124
Definition: pair.hpp:31
void add_term(const variable< FieldType > &var)
Definition: variable.hpp:277