blueprint/include/nil/crypto3/zk/components/algebra/curves/scalar_mul.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 G1 components.
26 //
27 // The components verify curve arithmetic in G1 = E(F) where E/F: y^2 = x^3 + A * X + B
28 // is an elliptic curve over F in short Weierstrass form.
29 //---------------------------------------------------------------------------//
30 
31 #ifndef CRYPTO3_ZK_BLUEPRINT_WEIERSTRASS_G1_COMPONENT_HPP
32 #define CRYPTO3_ZK_BLUEPRINT_WEIERSTRASS_G1_COMPONENT_HPP
33 
35 
37 
38 namespace nil {
39  namespace crypto3 {
40  namespace zk {
41  namespace components {
42 
43  template<typename CurveType>
44  class element_g1;
45 
46  template<typename CurveType>
47  class element_g1_add;
48 
49  template<typename CurveType>
50  class element_g1_doubled;
51 
55  template<typename CurveType>
56  class scalar_mul : public component<typename CurveType::scalar_field_type> {
57  typedef typename CurveType::scalar_field_type FieldType;
58 
59  public:
60  std::vector<element_g1<CurveType>> computed_results;
61  std::vector<element_g1<CurveType>> chosen_results;
62  std::vector<element_g1_add<CurveType>> adders;
63  std::vector<element_g1_doubled<CurveType>> doublers;
64 
67  std::vector<element_g1<CurveType>> points;
68  std::vector<element_g1<CurveType>> points_and_powers;
70 
71  const std::size_t elt_size;
72  const std::size_t num_points;
73  const std::size_t scalar_size;
74 
78  const std::size_t elt_size,
81  component<FieldType>(bp),
83  num_points(points.size()), scalar_size(scalars.size()) {
84 
85  assert(num_points >= 1);
86  assert(num_points * elt_size == scalar_size);
87 
88  for (std::size_t i = 0; i < num_points; ++i) {
89  points_and_powers.emplace_back(points[i]);
90  for (std::size_t j = 0; j < elt_size - 1; ++j) {
93  bp, points_and_powers[i * elt_size + j], points_and_powers[i * elt_size + j + 1]));
94  }
95  }
96 
97  chosen_results.emplace_back(base);
98  for (std::size_t i = 0; i < scalar_size; ++i) {
100  if (i < scalar_size - 1) {
101  chosen_results.emplace_back(element_g1<CurveType>(bp));
102  } else {
103  chosen_results.emplace_back(result);
104  }
105 
106  adders.emplace_back(element_g1_add<CurveType>(
108  }
109  }
110 
112  const std::size_t num_constraints_before = this->bp.num_constraints();
113 
114  for (std::size_t i = 0; i < scalar_size - num_points; ++i) {
115  doublers[i].generate_r1cs_constraints();
116  }
117 
118  for (std::size_t i = 0; i < scalar_size; ++i) {
119  adders[i].generate_r1cs_constraints();
120 
121  /*
122  chosen_results[i+1].X = scalars[i] * computed_results[i].X + (1-scalars[i]) *
123  chosen_results[i].X chosen_results[i+1].X - chosen_results[i].X = scalars[i] *
124  (computed_results[i].X - chosen_results[i].X)
125  */
126  this->bp.add_r1cs_constraint(
128  computed_results[i].X - chosen_results[i].X,
129  chosen_results[i + 1].X - chosen_results[i].X));
130  this->bp.add_r1cs_constraint(
132  computed_results[i].Y - chosen_results[i].Y,
133  chosen_results[i + 1].Y - chosen_results[i].Y));
134  }
135 
136  const std::size_t num_constraints_after = this->bp.num_constraints();
137  assert(num_constraints_after - num_constraints_before ==
138  4 * (scalar_size - num_points) + (4 + 2) * scalar_size);
139  }
140 
142  for (std::size_t i = 0; i < scalar_size - num_points; ++i) {
143  doublers[i].generate_r1cs_witness();
144  }
145 
146  for (std::size_t i = 0; i < scalar_size; ++i) {
147  adders[i].generate_r1cs_witness();
148  this->bp.lc_val(chosen_results[i + 1].X) =
149  (this->bp.val(scalars[i]) == typename CurveType::scalar_field_type::value_type::zero() ?
150  this->bp.lc_val(chosen_results[i].X) :
151  this->bp.lc_val(computed_results[i].X));
152  this->bp.lc_val(chosen_results[i + 1].Y) =
153  (this->bp.val(scalars[i]) == typename CurveType::scalar_field_type::value_type::zero() ?
154  this->bp.lc_val(chosen_results[i].Y) :
155  this->bp.lc_val(computed_results[i].Y));
156  }
157  }
158  };
159  } // namespace components
160  } // namespace zk
161  } // namespace crypto3
162 } // namespace nil
163 
164 #endif // CRYPTO3_ZK_BLUEPRINT_WEIERSTRASS_G1_COMPONENT_HPP
Definition: blueprint_variable.hpp:57
Definition: blueprint.hpp:46
Definition: component.hpp:37
blueprint< CurveType::scalar_field_type > & bp
Definition: component.hpp:39
Definition: blueprint/include/nil/crypto3/zk/components/algebra/curves/edwards/element_g1.hpp:193
Definition: blueprint/include/nil/crypto3/zk/components/algebra/curves/weierstrass/element_g1.hpp:210
Definition: blueprint/include/nil/crypto3/zk/components/algebra/curves/edwards/element_g1.hpp:51
Definition: blueprint/include/nil/crypto3/zk/components/algebra/curves/scalar_mul.hpp:56
const std::size_t elt_size
Definition: blueprint/include/nil/crypto3/zk/components/algebra/curves/scalar_mul.hpp:71
std::vector< element_g1< CurveType > > computed_results
Definition: blueprint/include/nil/crypto3/zk/components/algebra/curves/scalar_mul.hpp:60
element_g1< CurveType > base
Definition: blueprint/include/nil/crypto3/zk/components/algebra/curves/scalar_mul.hpp:65
const std::size_t num_points
Definition: blueprint/include/nil/crypto3/zk/components/algebra/curves/scalar_mul.hpp:72
std::vector< element_g1< CurveType > > chosen_results
Definition: blueprint/include/nil/crypto3/zk/components/algebra/curves/scalar_mul.hpp:61
std::vector< element_g1< CurveType > > points_and_powers
Definition: blueprint/include/nil/crypto3/zk/components/algebra/curves/scalar_mul.hpp:68
void generate_r1cs_constraints()
Definition: blueprint/include/nil/crypto3/zk/components/algebra/curves/scalar_mul.hpp:111
scalar_mul(blueprint< FieldType > &bp, const element_g1< CurveType > &base, const blueprint_variable_vector< FieldType > &scalars, const std::size_t elt_size, const std::vector< element_g1< CurveType >> &points, const element_g1< CurveType > &result)
Definition: blueprint/include/nil/crypto3/zk/components/algebra/curves/scalar_mul.hpp:75
element_g1< CurveType > result
Definition: blueprint/include/nil/crypto3/zk/components/algebra/curves/scalar_mul.hpp:69
blueprint_variable_vector< FieldType > scalars
Definition: blueprint/include/nil/crypto3/zk/components/algebra/curves/scalar_mul.hpp:66
void generate_r1cs_witness()
Definition: blueprint/include/nil/crypto3/zk/components/algebra/curves/scalar_mul.hpp:141
std::vector< element_g1< CurveType > > points
Definition: blueprint/include/nil/crypto3/zk/components/algebra/curves/scalar_mul.hpp:67
std::vector< element_g1_add< CurveType > > adders
Definition: blueprint/include/nil/crypto3/zk/components/algebra/curves/scalar_mul.hpp:62
std::vector< element_g1_doubled< CurveType > > doublers
Definition: blueprint/include/nil/crypto3/zk/components/algebra/curves/scalar_mul.hpp:63
const std::size_t scalar_size
Definition: blueprint/include/nil/crypto3/zk/components/algebra/curves/scalar_mul.hpp:73
vector(T, U...) -> vector< std::enable_if_t<(std::is_same_v< T, U > &&...), T >, 1+sizeof...(U)>
deduction guide for uniform initialization
Definition: pair.hpp:31