algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/extended_with_a_minus_1/element_g1.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2021 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 2021 Ilias Khairullin <ilias@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_ALGEBRA_CURVES_TWISTED_EDWARDS_G1_ELEMENT_EXTENDED_WITH_A_MINUS_1_HPP
27 #define CRYPTO3_ALGEBRA_CURVES_TWISTED_EDWARDS_G1_ELEMENT_EXTENDED_WITH_A_MINUS_1_HPP
28 
31 
37 
38 namespace nil {
39  namespace crypto3 {
40  namespace algebra {
41  namespace curves {
42  namespace detail {
48  template<typename CurveParams, typename Form, typename Coordinates>
49  struct curve_element;
50 
56  template<typename CurveParams>
57  struct curve_element<CurveParams, forms::twisted_edwards, coordinates::extended_with_a_minus_1> {
58 
59  using params_type = CurveParams;
60  using field_type = typename params_type::field_type;
61 
62  private:
63  using field_value_type = typename field_type::value_type;
64 
71 
72  public:
74  using coordinates = coordinates::extended_with_a_minus_1;
75 
76  using group_type = typename params_type::template group_type<coordinates>;
77 
78  field_value_type X;
79  field_value_type Y;
80  field_value_type T;
81  field_value_type Z;
82 
83  /************************* Constructors and zero/one ***********************************/
84 
89  constexpr curve_element() :
90  curve_element(params_type::zero_fill[0],
91  params_type::zero_fill[1],
92  field_value_type::zero(),
93  field_value_type::one()) {};
94 
99  constexpr curve_element(field_value_type X,
100  field_value_type Y,
101  field_value_type T,
102  field_value_type Z) {
103  this->X = X;
104  this->Y = Y;
105  this->T = T;
106  this->Z = Z;
107  };
108 
112  constexpr static curve_element zero() {
113  return curve_element();
114  }
115 
119  constexpr static curve_element one() {
120  return curve_element(params_type::one_fill[0],
121  params_type::one_fill[1],
122  params_type::one_fill[0] * params_type::one_fill[1],
123  field_value_type::one());
124  }
125 
126  /************************* Comparison operations ***********************************/
127 
128  constexpr bool operator==(const curve_element &other) const {
129  if (this->is_zero()) {
130  return other.is_zero();
131  }
132 
133  if (other.is_zero()) {
134  return false;
135  }
136 
137  /* now neither is O */
138 
139  // X1/Z1 = X2/Z2 <=> X1*Z2 = X2*Z1
140  if ((this->X * other.Z) != (other.X * this->Z)) {
141  return false;
142  }
143 
144  // Y1/Z1 = Y2/Z2 <=> Y1*Z2 = Y2*Z1
145  if ((this->Y * other.Z) != (other.Y * this->Z)) {
146  return false;
147  }
148 
149  // T1/Z1 = T2/Z2 <=> T1*Z2 = T2*Z1
150  if ((this->T * other.Z) != (other.T * this->Z)) {
151  return false;
152  }
153 
154  return true;
155  }
156 
157  constexpr bool operator!=(const curve_element &other) const {
158  return !(operator==(other));
159  }
164  constexpr bool is_zero() const {
165  return (this->X.is_zero() && this->T.is_zero() && this->Z.is_zero());
166  }
167 
172  constexpr bool is_well_formed() const {
173  assert(false && "Not implemented yet.");
174  return true;
175  }
176 
177  /************************* Reducing operations ***********************************/
178 
185 
187 
188  if (is_zero()) {
189  return result_type::zero();
190  }
191 
192  // assert((X/Z)*(Y/Z) == (T/Z));
193  return result_type(X / Z, Y / Z); // x=X/Z, y=Y/Z
194  }
195 
196  /************************* Arithmetic operations ***********************************/
197 
198  constexpr curve_element operator=(const curve_element &other) {
199  // handle special cases having to do with O
200  this->X = other.X;
201  this->Y = other.Y;
202  this->T = other.T;
203  this->Z = other.Z;
204 
205  return *this;
206  }
207 
208  constexpr curve_element operator+(const curve_element &other) const {
209  // handle special cases having to do with O
210  if (this->is_zero()) {
211  return other;
212  }
213 
214  if (other.is_zero()) {
215  return (*this);
216  }
217 
218  if (*this == other) {
219  return this->doubled();
220  }
221 
222  return common_addition_processor::process(*this, other);
223  }
224 
225  constexpr curve_element operator-() const {
226  return curve_element(-X, Y, -T, Z);
227  }
228 
229  constexpr curve_element operator-(const curve_element &other) const {
230  return (*this) + (-other);
231  }
232 
237  constexpr curve_element doubled() const {
238  return common_doubling_processor::process(*this);
239  }
240 
246  curve_element mixed_add(const curve_element &other) const {
247 
248  // handle special cases having to do with O
249  if (this->is_zero()) {
250  return other;
251  }
252 
253  if (other.is_zero()) {
254  return *this;
255  }
256 
257  return mixed_addition_processor::process(*this, other);
258  }
259  };
260 
261  } // namespace detail
262  } // namespace curves
263  } // namespace algebra
264  } // namespace crypto3
265 } // namespace nil
266 #endif // CRYPTO3_ALGEBRA_CURVES_TWISTED_EDWARDS_G1_ELEMENT_EXTENDED_WITH_A_MINUS_1_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 curve_element operator+(const curve_element &other) const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/extended_with_a_minus_1/element_g1.hpp:208
constexpr bool is_zero() const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/extended_with_a_minus_1/element_g1.hpp:164
typename params_type::field_type field_type
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/extended_with_a_minus_1/element_g1.hpp:60
typename params_type::template group_type< coordinates > group_type
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/extended_with_a_minus_1/element_g1.hpp:76
constexpr curve_element(field_value_type X, field_value_type Y, field_value_type T, field_value_type Z)
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/extended_with_a_minus_1/element_g1.hpp:99
field_value_type T
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/extended_with_a_minus_1/element_g1.hpp:80
field_value_type Y
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/extended_with_a_minus_1/element_g1.hpp:79
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/twisted_edwards/extended_with_a_minus_1/element_g1.hpp:246
constexpr bool is_well_formed() const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/extended_with_a_minus_1/element_g1.hpp:172
constexpr bool operator!=(const curve_element &other) const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/extended_with_a_minus_1/element_g1.hpp:157
coordinates::extended_with_a_minus_1 coordinates
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/extended_with_a_minus_1/element_g1.hpp:74
field_value_type Z
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/extended_with_a_minus_1/element_g1.hpp:81
CurveParams params_type
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/extended_with_a_minus_1/element_g1.hpp:59
constexpr curve_element< params_type, form, curves::coordinates::affine > to_affine() const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/extended_with_a_minus_1/element_g1.hpp:184
constexpr curve_element()
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/extended_with_a_minus_1/element_g1.hpp:89
constexpr curve_element operator-() const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/extended_with_a_minus_1/element_g1.hpp:225
constexpr static curve_element zero()
Get the point at infinity.
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/extended_with_a_minus_1/element_g1.hpp:112
constexpr static curve_element one()
Get the generator of group G1.
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/extended_with_a_minus_1/element_g1.hpp:119
constexpr curve_element operator=(const curve_element &other)
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/extended_with_a_minus_1/element_g1.hpp:198
constexpr bool operator==(const curve_element &other) const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/extended_with_a_minus_1/element_g1.hpp:128
constexpr curve_element doubled() const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/extended_with_a_minus_1/element_g1.hpp:237
field_value_type X
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/extended_with_a_minus_1/element_g1.hpp:78
constexpr curve_element operator-(const curve_element &other) const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/extended_with_a_minus_1/element_g1.hpp:229
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 twisted Edwards curve for extended coordi...
Definition: add_2008_hwcd_3.hpp:39
A struct representing element addition from the group G1 of twisted Edwards curve for extended coordi...
Definition: madd_2008_hwcd_2.hpp:40
A struct representing element doubling from the group G1 of twisted Edwards curve for extended coordi...
Definition: dbl_2008_hwcd.hpp:41
Definition: forms.hpp:34