algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian_with_a4_minus_3/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_WITH_A4_MINUS_3_HPP
28 #define CRYPTO3_ALGEBRA_CURVES_SHORT_WEIERSTRASS_G1_ELEMENT_JACOBIAN_WITH_A4_MINUS_3_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_with_a4_minus_3> {
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_with_a4_minus_3;
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 - 3x + 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 - 3(x/z^2) + b
178  y^2 / z^6 = x^3 / z^6 - 3x/z^2 + b
179  y^2 = x^3 - 3x 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  field_value_type XXX = this->X.doubled() + this->X;
190 
191  return (Y2 == X3 - XXX* Z4 + params_type::b * Z6);
192  }
193  }
194 
195  /************************* Reducing operations ***********************************/
196 
203  to_affine() const {
204 
206 
207  if (is_zero()) {
208  return result_type::zero();
209  }
210 
211  return result_type(X / Z.squared(), Y / (Z * Z.squared())); // x=X/Z^2, y=Y/Z^3
212  }
213 
220  to_projective() const {
221 
222  using result_type =
224 
225  if (is_zero()) {
226  return result_type::zero();
227  }
228 
229  return result_type(X / Z, Y / Z.squared(),
230  Z); // X = X/Z, Y = Y/Z^2, Z = Z
231  }
232 
233  /************************* Arithmetic operations ***********************************/
234 
235  constexpr curve_element operator=(const curve_element &other) {
236  // handle special cases having to do with O
237  this->X = other.X;
238  this->Y = other.Y;
239  this->Z = other.Z;
240 
241  return *this;
242  }
243 
244  constexpr curve_element operator+(const curve_element &other) const {
245  // handle special cases having to do with O
246  if (this->is_zero()) {
247  return other;
248  }
249 
250  if (other.is_zero()) {
251  return (*this);
252  }
253 
254  if (*this == other) {
255  return this->doubled();
256  }
257 
258  return common_addition_processor::process(*this, other);
259  }
260 
261  constexpr curve_element operator-() const {
262  return curve_element(this->X, -(this->Y), this->Z);
263  }
264 
265  constexpr curve_element operator-(const curve_element &other) const {
266  return (*this) + (-other);
267  }
268 
273  constexpr curve_element doubled() const {
274  return common_doubling_processor::process(*this);
275  }
276 
282  constexpr curve_element mixed_add(const curve_element &other) const {
283 
284  // handle special cases having to do with O
285  if (this->is_zero()) {
286  return other;
287  }
288 
289  if (other.is_zero()) {
290  return *this;
291  }
292 
293  return mixed_addition_processor::process(*this, other);
294  }
295  };
296 
297  } // namespace detail
298  } // namespace curves
299  } // namespace algebra
300  } // namespace crypto3
301 } // namespace nil
302 #endif // CRYPTO3_ALGEBRA_CURVES_SHORT_WEIERSTRASS_G1_ELEMENT_JACOBIAN_WITH_A4_MINUS_3_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
field_value_type Z
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian_with_a4_minus_3/element_g1.hpp:78
field_value_type Y
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian_with_a4_minus_3/element_g1.hpp:77
constexpr curve_element operator=(const curve_element &other)
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian_with_a4_minus_3/element_g1.hpp:235
coordinates::jacobian_with_a4_minus_3 coordinates
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian_with_a4_minus_3/element_g1.hpp:72
constexpr curve_element doubled() const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian_with_a4_minus_3/element_g1.hpp:273
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_with_a4_minus_3/element_g1.hpp:203
typename params_type::field_type field_type
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian_with_a4_minus_3/element_g1.hpp:61
typename params_type::template group_type< coordinates > group_type
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian_with_a4_minus_3/element_g1.hpp:74
constexpr curve_element operator+(const curve_element &other) const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian_with_a4_minus_3/element_g1.hpp:244
constexpr curve_element()
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian_with_a4_minus_3/element_g1.hpp:86
constexpr bool operator!=(const curve_element &other) const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian_with_a4_minus_3/element_g1.hpp:153
constexpr bool operator==(const curve_element &other) const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian_with_a4_minus_3/element_g1.hpp:118
constexpr curve_element operator-(const curve_element &other) const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian_with_a4_minus_3/element_g1.hpp:265
constexpr curve_element operator-() const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian_with_a4_minus_3/element_g1.hpp:261
constexpr static curve_element one()
Get the generator of group G1.
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian_with_a4_minus_3/element_g1.hpp:111
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_with_a4_minus_3/element_g1.hpp:282
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_with_a4_minus_3/element_g1.hpp:95
CurveParams params_type
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian_with_a4_minus_3/element_g1.hpp:60
constexpr bool is_zero() const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian_with_a4_minus_3/element_g1.hpp:160
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_with_a4_minus_3/element_g1.hpp:220
constexpr bool is_well_formed() const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian_with_a4_minus_3/element_g1.hpp:168
field_value_type X
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian_with_a4_minus_3/element_g1.hpp:76
constexpr static curve_element zero()
Get the point at infinity.
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/short_weierstrass/jacobian_with_a4_minus_3/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_with...
Definition: short_weierstrass/jacobian_with_a4_minus_3/add_2007_bl.hpp:41
A struct representing element doubling from the group G1 of short Weierstrass curve for jacobian_with...
Definition: short_weierstrass/jacobian_with_a4_minus_3/dbl_2007_bl.hpp:40
A struct representing element addition from the group G1 of short Weierstrass curve for jacobian_with...
Definition: short_weierstrass/jacobian_with_a4_minus_3/madd_2007_bl.hpp:41
Definition: forms.hpp:34