algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian/element_g1.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2020-2021 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 2020-2021 Nikita Kaskov <nbering@nil.foundation>
4 // Copyright (c) 2020-2021 Ilias Khairullin <ilias@nil.foundation>
5 //
6 // MIT License
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 //---------------------------------------------------------------------------//
26 
27 #ifndef CRYPTO3_ALGEBRA_CURVES_SHORT_WEIERSTRASS_G1_ELEMENT_JACOBIAN_HPP
28 #define CRYPTO3_ALGEBRA_CURVES_SHORT_WEIERSTRASS_G1_ELEMENT_JACOBIAN_HPP
29 
32 
38 
39 namespace nil {
40  namespace crypto3 {
41  namespace algebra {
42  namespace curves {
43  namespace detail {
49  template<typename CurveParams, typename Form, typename Coordinates>
50  struct curve_element;
51 
57  template<typename CurveParams>
58  struct curve_element<CurveParams, forms::short_weierstrass, coordinates::jacobian> {
59 
60  using params_type = CurveParams;
61  using field_type = typename params_type::field_type;
62 
63  private:
64  using field_value_type = typename field_type::value_type;
65 
69 
70  public:
72  using coordinates = coordinates::jacobian;
73 
74  using group_type = typename params_type::template group_type<coordinates>;
75 
76  field_value_type X;
77  field_value_type Y;
78  field_value_type Z;
79 
80  /************************* Constructors and zero/one ***********************************/
81 
86  constexpr curve_element() :
87  curve_element(params_type::zero_fill[0],
88  params_type::zero_fill[1],
89  field_value_type::zero()) {};
90 
95  constexpr curve_element(field_value_type X, field_value_type Y, field_value_type Z) {
96  this->X = X;
97  this->Y = Y;
98  this->Z = Z;
99  };
100 
104  constexpr static curve_element zero() {
105  return curve_element();
106  }
107 
111  constexpr static curve_element one() {
112  return curve_element(params_type::one_fill[0], params_type::one_fill[1],
113  field_value_type::one());
114  }
115 
116  /************************* Comparison operations ***********************************/
117 
118  constexpr bool operator==(const curve_element &other) const {
119  if (this->is_zero()) {
120  return other.is_zero();
121  }
122 
123  if (other.is_zero()) {
124  return false;
125  }
126 
127  /* now neither is O */
128 
129  // using Jacobian coordinates so:
130  // (X1:Y1:Z1) = (X2:Y2:Z2)
131  // iff
132  // X1/Z1^2 == X2/Z2^2 and Y1/Z1^3 == Y2/Z2^3
133  // iff
134  // X1 * Z2^2 == X2 * Z1^2 and Y1 * Z2^3 == Y2 * Z1^3
135 
136  field_value_type Z1_squared = (this->Z).squared();
137  field_value_type Z2_squared = (other.Z).squared();
138 
139  if ((this->X * Z2_squared) != (other.X * Z1_squared)) {
140  return false;
141  }
142 
143  field_value_type Z1_cubed = (this->Z) * Z1_squared;
144  field_value_type Z2_cubed = (other.Z) * Z2_squared;
145 
146  if ((this->Y * Z2_cubed) != (other.Y * Z1_cubed)) {
147  return false;
148  }
149 
150  return true;
151  }
152 
153  constexpr bool operator!=(const curve_element &other) const {
154  return !(operator==(other));
155  }
160  constexpr bool is_zero() const {
161  return (this->Z.is_zero());
162  }
163 
168  constexpr bool is_well_formed() const {
169  if (this->is_zero()) {
170  return true;
171  } else {
172  /*
173  y^2 = x^3 + a x + b
174 
175  We are using Jacobian coordinates, so equation we need to check is actually
176 
177  (y/z^3)^2 = (x/z^2)^3 + a(x/z^2) + b
178  y^2 / z^6 = x^3 / z^6 + a*x/z^2 + b
179  y^2 = x^3 + a*x*z^4 + b*z^6
180  */
181  field_value_type X2 = this->X.squared();
182  field_value_type Y2 = this->Y.squared();
183  field_value_type Z2 = this->Z.squared();
184 
185  field_value_type X3 = this->X * X2;
186  field_value_type Z3 = this->Z * Z2;
187  field_value_type Z4 = Z2.squared();
188  field_value_type Z6 = Z3.squared();
189 
190  return (Y2 == X3 + params_type::a * this->X * Z4 + params_type::b * Z6);
191  }
192  }
193 
194  /************************* Reducing operations ***********************************/
195 
202  to_affine() const {
203 
205 
206  if (is_zero()) {
207  return result_type::zero();
208  }
209 
210  return result_type(X / Z.squared(), Y / (Z * Z.squared())); // x=X/Z^2, y=Y/Z^3
211  }
212 
219  to_projective() const {
220 
221  using result_type =
223 
224  if (is_zero()) {
225  return result_type::zero();
226  }
227 
228  return result_type(X / Z, Y / Z.squared(),
229  Z); // X = X/Z, Y = Y/Z^2, Z = Z
230  }
231 
232  /************************* Arithmetic operations ***********************************/
233 
234  constexpr curve_element operator=(const curve_element &other) {
235  // handle special cases having to do with O
236  this->X = other.X;
237  this->Y = other.Y;
238  this->Z = other.Z;
239 
240  return *this;
241  }
242 
243  constexpr curve_element operator+(const curve_element &other) const {
244  // handle special cases having to do with O
245  if (this->is_zero()) {
246  return other;
247  }
248 
249  if (other.is_zero()) {
250  return (*this);
251  }
252 
253  if (*this == other) {
254  return this->doubled();
255  }
256 
257  return common_addition_processor::process(*this, other);
258  }
259 
260  constexpr curve_element operator-() const {
261  return curve_element(this->X, -(this->Y), this->Z);
262  }
263 
264  constexpr curve_element operator-(const curve_element &other) const {
265  return (*this) + (-other);
266  }
267 
272  constexpr curve_element doubled() const {
273  return common_doubling_processor::process(*this);
274  }
275 
281  constexpr curve_element mixed_add(const curve_element &other) const {
282 
283  // handle special cases having to do with O
284  if (this->is_zero()) {
285  return other;
286  }
287 
288  if (other.is_zero()) {
289  return *this;
290  }
291 
292  return mixed_addition_processor::process(*this, other);
293  }
294  };
295 
296  } // namespace detail
297  } // namespace curves
298  } // namespace algebra
299  } // namespace crypto3
300 } // namespace nil
301 #endif // CRYPTO3_ALGEBRA_CURVES_SHORT_WEIERSTRASS_G1_ELEMENT_JACOBIAN_HPP
constexpr bool operator==(const matrix< T, N, M > &a, const matrix< T, N, M > &b)
checks equality of two matrices
Definition: matrix/operators.hpp:48
bool is_zero(const Range &a)
Definition: basic_operations.hpp:43
Definition: pair.hpp:31
constexpr bool is_well_formed() const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian/element_g1.hpp:168
coordinates::jacobian coordinates
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian/element_g1.hpp:72
typename params_type::field_type field_type
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian/element_g1.hpp:61
constexpr curve_element operator-(const curve_element &other) const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian/element_g1.hpp:264
constexpr curve_element doubled() const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian/element_g1.hpp:272
constexpr curve_element()
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian/element_g1.hpp:86
constexpr curve_element operator+(const curve_element &other) const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian/element_g1.hpp:243
field_value_type Y
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian/element_g1.hpp:77
constexpr curve_element(field_value_type X, field_value_type Y, field_value_type Z)
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian/element_g1.hpp:95
constexpr curve_element operator-() const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian/element_g1.hpp:260
constexpr curve_element operator=(const curve_element &other)
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian/element_g1.hpp:234
field_value_type Z
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian/element_g1.hpp:78
constexpr curve_element mixed_add(const curve_element &other) const
“Mixed addition” refers to the case Z2 known to be 1.
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian/element_g1.hpp:281
typename params_type::template group_type< coordinates > group_type
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian/element_g1.hpp:74
constexpr static curve_element one()
Get the generator of group G1.
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian/element_g1.hpp:111
CurveParams params_type
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian/element_g1.hpp:60
constexpr bool operator==(const curve_element &other) const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian/element_g1.hpp:118
constexpr bool operator!=(const curve_element &other) const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian/element_g1.hpp:153
field_value_type X
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian/element_g1.hpp:76
constexpr curve_element< params_type, form, typename curves::coordinates::projective > to_projective() const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian/element_g1.hpp:219
constexpr bool is_zero() const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian/element_g1.hpp:160
constexpr curve_element< params_type, form, typename curves::coordinates::affine > to_affine() const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian/element_g1.hpp:202
constexpr static curve_element zero()
Get the point at infinity.
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian/element_g1.hpp:104
A struct representing a group G1 of elliptic curve.
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/edwards/inverted/element_g1.hpp:50
A struct representing element addition from the group G1 of short Weierstrass curve for jacobian_ coo...
Definition: short_weierstrass/jacobian/add_2007_bl.hpp:41
A struct representing element doubling from the group G1 of short Weierstrass curve for jacobian_ coo...
Definition: short_weierstrass/jacobian/dbl_2007_bl.hpp:40
A struct representing element addition from the group G1 of short Weierstrass curve for jacobian coor...
Definition: short_weierstrass/jacobian/madd_2007_bl.hpp:41
Definition: forms.hpp:34