blueprint/include/nil/crypto3/zk/components/inner_product.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_INNER_PRODUCT_COMPONENT_HPP
27 #define CRYPTO3_ZK_BLUEPRINT_INNER_PRODUCT_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  template<typename FieldType>
51  class inner_product : public component<FieldType> {
52  private:
53  /* S_i = \sum_{k=0}^{i+1} A[i] * B[i] */
55 
56  public:
60 
65  component<FieldType>(bp),
66  A(A), B(B), result(result) {
67  assert(A.size() >= 1);
68  assert(A.size() == B.size());
69 
70  S.allocate(bp, A.size() - 1);
71  }
72 
74  /*
75  S_i = \sum_{k=0}^{i+1} A[i] * B[i]
76  S[0] = A[0] * B[0]
77  S[i+1] - S[i] = A[i] * B[i]
78  */
79  for (std::size_t i = 0; i < A.size(); ++i) {
80  this->bp.add_r1cs_constraint(snark::r1cs_constraint<FieldType>(
81  A[i], B[i],
82  (i == A.size() - 1 ? result : S[i]) +
83  (i == 0 ? 0 * blueprint_variable<FieldType>(0) : -S[i - 1])));
84  }
85  }
86 
88  typename FieldType::value_type total = FieldType::value_type::zero();
89  for (std::size_t i = 0; i < A.size(); ++i) {
90  A[i].evaluate(this->bp);
91  B[i].evaluate(this->bp);
92 
93  total += this->bp.lc_val(A[i]) * this->bp.lc_val(B[i]);
94  this->bp.val(i == A.size() - 1 ? result : S[i]) = total;
95  }
96  }
97  };
98 
99  } // namespace components
100  } // namespace zk
101  } // namespace crypto3
102 } // namespace nil
103 #endif // CRYPTO3_ZK_BLUEPRINT_INNER_PRODUCT_COMPONENT_HPP
Definition: blueprint_linear_combination.hpp:115
void evaluate(blueprint< field_type > &bp) const
Definition: blueprint_linear_combination.hpp:155
Definition: blueprint_variable.hpp:57
void allocate(blueprint< field_type > &bp, const std::size_t n)
Definition: blueprint_variable.hpp:91
Definition: blueprint_variable.hpp:46
Definition: blueprint.hpp:46
Definition: component.hpp:37
blueprint< FieldType > & bp
Definition: component.hpp:39
Definition: blueprint/include/nil/crypto3/zk/components/inner_product.hpp:51
const blueprint_variable< FieldType > result
Definition: blueprint/include/nil/crypto3/zk/components/inner_product.hpp:59
void generate_r1cs_witness()
Definition: blueprint/include/nil/crypto3/zk/components/inner_product.hpp:87
const blueprint_linear_combination_vector< FieldType > B
Definition: blueprint/include/nil/crypto3/zk/components/inner_product.hpp:58
inner_product(blueprint< FieldType > &bp, const blueprint_linear_combination_vector< FieldType > &A, const blueprint_linear_combination_vector< FieldType > &B, const blueprint_variable< FieldType > &result)
Definition: blueprint/include/nil/crypto3/zk/components/inner_product.hpp:61
const blueprint_linear_combination_vector< FieldType > A
Definition: blueprint/include/nil/crypto3/zk/components/inner_product.hpp:57
void generate_r1cs_constraints()
Definition: blueprint/include/nil/crypto3/zk/components/inner_product.hpp:73
Definition: pair.hpp:31