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