algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/inverted/element_g1.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2021 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 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 
26 #ifndef CRYPTO3_ALGEBRA_CURVES_TWISTED_EDWARDS_G1_ELEMENT_INVERTED_HPP
27 #define CRYPTO3_ALGEBRA_CURVES_TWISTED_EDWARDS_G1_ELEMENT_INVERTED_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::inverted> {
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 
68 
69  public:
71  using coordinates = coordinates::inverted;
72 
73  using group_type = typename params_type::template group_type<coordinates>;
74 
75  field_value_type X;
76  field_value_type Y;
77  field_value_type Z;
78 
79  /************************* Constructors and zero/one ***********************************/
80 
85  constexpr curve_element() :
86  curve_element(params_type::zero_fill[1],
87  params_type::zero_fill[0],
88  field_value_type::zero()) {};
89 
94  constexpr curve_element(field_value_type X, field_value_type Y, field_value_type Z) {
95  this->X = X;
96  this->Y = Y;
97  this->Z = Z;
98  };
99 
103  constexpr static curve_element zero() {
104  return curve_element();
105  }
106 
110  constexpr static curve_element one() {
111  return curve_element(params_type::one_fill[0].inversed(),
112  params_type::one_fill[1].inversed(),
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  // X1/Z1 = X2/Z2 <=> X1*Z2 = X2*Z1
130  if ((this->X * other.Z) != (other.X * this->Z)) {
131  return false;
132  }
133 
134  // Y1/Z1 = Y2/Z2 <=> Y1*Z2 = Y2*Z1
135  if ((this->Y * other.Z) != (other.Y * this->Z)) {
136  return false;
137  }
138 
139  return true;
140  }
141 
142  constexpr bool operator!=(const curve_element &other) const {
143  return !(operator==(other));
144  }
149  constexpr bool is_zero() const {
150  return (this->Y.is_zero() && this->Z.is_zero());
151  }
152 
157  constexpr bool is_well_formed() const {
158  assert(false && "Not implemented yet.");
159  return true;
160  }
161 
162  /************************* Reducing operations ***********************************/
163 
170  to_affine() const {
171 
173 
174  if (is_zero()) {
175  return result_type::zero();
176  }
177 
178  return result_type(Z / X, Z / Y); // x=Z/X, y=Z/Y
179  }
180 
181  /************************* Arithmetic operations ***********************************/
182 
183  constexpr curve_element operator=(const curve_element &other) {
184  // handle special cases having to do with O
185  this->X = other.X;
186  this->Y = other.Y;
187  this->Z = other.Z;
188 
189  return *this;
190  }
191 
192  constexpr curve_element operator+(const curve_element &other) const {
193  // handle special cases having to do with O
194  if (this->is_zero()) {
195  return other;
196  }
197 
198  if (other.is_zero()) {
199  return (*this);
200  }
201 
202  if (*this == other) {
203  return this->doubled();
204  }
205 
206  return common_addition_processor::process(*this, other);
207  }
208 
209  constexpr curve_element operator-() const {
210  return curve_element(-(this->X), this->Y, this->Z);
211  }
212 
213  constexpr curve_element operator-(const curve_element &other) const {
214  return (*this) + (-other);
215  }
216 
221  constexpr curve_element doubled() const {
222  return common_doubling_processor::process(*this);
223  }
224 
230  curve_element mixed_add(const curve_element &other) const {
231 
232  // handle special cases having to do with O
233  if (this->is_zero()) {
234  return other;
235  }
236 
237  if (other.is_zero()) {
238  return *this;
239  }
240 
241  return mixed_addition_processor::process(*this, other);
242  }
243  };
244 
245  } // namespace detail
246  } // namespace curves
247  } // namespace algebra
248  } // namespace crypto3
249 } // namespace nil
250 #endif // CRYPTO3_ALGEBRA_CURVES_TWISTED_EDWARDS_G1_ELEMENT_INVERTED_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_zero() const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/inverted/element_g1.hpp:149
constexpr bool operator==(const curve_element &other) const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/inverted/element_g1.hpp:118
constexpr curve_element< params_type, form, typename curves::coordinates::affine > to_affine() const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/inverted/element_g1.hpp:170
field_value_type X
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/inverted/element_g1.hpp:75
field_value_type Z
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/inverted/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/twisted_edwards/inverted/element_g1.hpp:94
constexpr curve_element()
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/inverted/element_g1.hpp:85
constexpr curve_element operator-() const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/inverted/element_g1.hpp:209
constexpr bool is_well_formed() const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/inverted/element_g1.hpp:157
constexpr curve_element doubled() const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/inverted/element_g1.hpp:221
constexpr curve_element operator-(const curve_element &other) const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/inverted/element_g1.hpp:213
CurveParams params_type
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/inverted/element_g1.hpp:59
constexpr static curve_element one()
Get the generator of group G1.
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/inverted/element_g1.hpp:110
coordinates::inverted coordinates
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/inverted/element_g1.hpp:71
typename params_type::template group_type< coordinates > group_type
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/inverted/element_g1.hpp:73
constexpr curve_element operator=(const curve_element &other)
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/inverted/element_g1.hpp:183
constexpr bool operator!=(const curve_element &other) const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/inverted/element_g1.hpp:142
field_value_type Y
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/inverted/element_g1.hpp:76
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/inverted/element_g1.hpp:230
constexpr static curve_element zero()
Get the point at infinity.
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/inverted/element_g1.hpp:103
typename params_type::field_type field_type
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/inverted/element_g1.hpp:60
constexpr curve_element operator+(const curve_element &other) const
Definition: algebra/include/nil/crypto3/algebra/curves/detail/forms/twisted_edwards/inverted/element_g1.hpp:192
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 inversed coordi...
Definition: add_2008_bbjlp.hpp:41
A struct representing element doubling from the group G1 of twisted Edwards curve for inversed coordi...
Definition: dbl_2008_bbjlp.hpp:41
A struct representing element addition from the group G1 of twisted Edwards curve for inversed coordi...
Definition: madd_2008_bbjlp.hpp:41
Definition: forms.hpp:34