fp.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 //
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_FIELDS_ELEMENT_FP_HPP
27 #define CRYPTO3_ALGEBRA_FIELDS_ELEMENT_FP_HPP
28 
31 
32 #include <nil/crypto3/multiprecision/ressol.hpp>
33 #include <nil/crypto3/multiprecision/inverse.hpp>
34 #include <nil/crypto3/multiprecision/number.hpp>
35 #include <nil/crypto3/multiprecision/cpp_int.hpp>
36 #include <nil/crypto3/multiprecision/modular/modular_adaptor.hpp>
37 
38 #include <boost/type_traits/is_integral.hpp>
39 
40 #include <type_traits>
41 
42 namespace nil {
43  namespace crypto3 {
44  namespace algebra {
45  namespace fields {
46  namespace detail {
47  template<typename FieldParams>
48  class element_fp {
49  typedef FieldParams policy_type;
50 
51  public:
52  typedef typename policy_type::field_type field_type;
53 
54  typedef typename policy_type::modular_type modular_type;
55  typedef typename policy_type::integral_type integral_type;
56  typedef typename policy_type::modular_backend modular_backend;
57  typedef typename policy_type::modular_params_type modular_params_type;
58 
59  constexpr static const modular_params_type modulus_params = policy_type::modulus_params;
60  constexpr static const integral_type modulus = policy_type::modulus;
61 
64 
65  constexpr element_fp() : data(data_type(0, modulus_params)) {};
66 
67  constexpr element_fp(const data_type &data) : data(data) {};
68 
69  template<typename Number,
70  typename std::enable_if<(multiprecision::is_number<Number>::value &&
71  !multiprecision::is_modular_number<Number>::value) ||
72  std::is_integral<Number>::value,
73  bool>::type = true>
74  constexpr element_fp(const Number &data) : data(data, modulus_params) {};
75 
76  constexpr element_fp(const element_fp &B) {
77  data = B.data;
78  };
79 
80  constexpr inline static element_fp zero() {
81  return element_fp(0);
82  }
83 
84  constexpr inline static element_fp one() {
85  return element_fp(1);
86  }
87 
88  constexpr bool is_zero() const {
89  return data == data_type(0, modulus_params);
90  }
91 
92  constexpr bool is_one() const {
93  return data == data_type(1, modulus_params);
94  }
95 
96  constexpr bool operator==(const element_fp &B) const {
97  return data == B.data;
98  }
99 
100  constexpr bool operator!=(const element_fp &B) const {
101  return data != B.data;
102  }
103 
104  constexpr element_fp &operator=(const element_fp &B) {
105  data = B.data;
106 
107  return *this;
108  }
109 
110  constexpr element_fp operator+(const element_fp &B) const {
111  return element_fp(data + B.data);
112  }
113 
114  constexpr element_fp operator-(const element_fp &B) const {
115  return element_fp(data - B.data);
116  }
117 
118  constexpr element_fp &operator-=(const element_fp &B) {
119  data -= B.data;
120 
121  return *this;
122  }
123 
124  constexpr element_fp &operator+=(const element_fp &B) {
125  data += B.data;
126 
127  return *this;
128  }
129 
130  constexpr element_fp &operator*=(const element_fp &B) {
131  data *= B.data;
132 
133  return *this;
134  }
135 
136  constexpr element_fp &operator/=(const element_fp &B) {
137  data *= B.inversed().data;
138 
139  return *this;
140  }
141 
142  constexpr element_fp operator-() const {
143  return element_fp(-data);
144  }
145 
146  constexpr element_fp operator*(const element_fp &B) const {
147  return element_fp(data * B.data);
148  }
149 
150  constexpr element_fp operator/(const element_fp &B) const {
151  // return element_fp(data / B.data);
152  return element_fp(data * B.inversed().data);
153  }
154 
155  constexpr bool operator<(const element_fp &B) const {
156  return data < B.data;
157  }
158 
159  constexpr bool operator>(const element_fp &B) const {
160  return data > B.data;
161  }
162 
163  constexpr bool operator<=(const element_fp &B) const {
164  return data <= B.data;
165  }
166 
167  constexpr bool operator>=(const element_fp &B) const {
168  return data >= B.data;
169  }
170 
171  constexpr element_fp &operator++() {
173  return *this;
174  }
175 
176  constexpr element_fp operator++(int) {
177  element_fp temp(*this);
178  ++*this;
179  return temp;
180  }
181 
182  constexpr element_fp &operator--() {
184  return *this;
185  }
186 
187  constexpr element_fp operator--(int) {
188  element_fp temp(*this);
189  --*this;
190  return temp;
191  }
192 
193  constexpr element_fp doubled() const {
194  return element_fp(data + data);
195  }
196 
197  // TODO: maybe incorrect result here
198  constexpr element_fp sqrt() const {
199  return element_fp(ressol(data));
200  }
201 
202  constexpr element_fp inversed() const {
203  return element_fp(inverse_mod(data));
204  }
205 
206  // TODO: complete method
207  constexpr element_fp _2z_add_3x() {
208  }
209 
210  constexpr element_fp squared() const {
211  return element_fp(data * data); // maybe can be done more effective
212  }
213 
214  // TODO: maybe error here
215  constexpr bool is_square() const {
216  return (this->sqrt() != -1); // maybe can be done more effective
217  }
218 
219  template<typename PowerType,
220  typename = typename std::enable_if<boost::is_integral<PowerType>::value>::type>
221  constexpr element_fp pow(const PowerType pwr) const {
222  return element_fp(multiprecision::powm(data, multiprecision::uint128_t(pwr)));
223  }
224 
225  template<typename Backend, multiprecision::expression_template_option ExpressionTemplates>
226  constexpr element_fp
227  pow(const multiprecision::number<Backend, ExpressionTemplates> &pwr) const {
228  return element_fp(multiprecision::powm(data, pwr));
229  }
230  };
231 
232  template<typename FieldParams>
234 
235  template<typename FieldParams>
237 
238  } // namespace detail
239  } // namespace fields
240  } // namespace algebra
241  } // namespace crypto3
242 } // namespace nil
243 
244 #endif // CRYPTO3_ALGEBRA_FIELDS_ELEMENT_FP_HPP
constexpr bool operator>(const element_fp &B) const
Definition: fp.hpp:159
policy_type::modular_backend modular_backend
Definition: fp.hpp:56
constexpr element_fp operator--(int)
Definition: fp.hpp:187
constexpr bool is_zero() const
Definition: fp.hpp:88
constexpr element_fp(const data_type &data)
Definition: fp.hpp:67
constexpr element_fp operator/(const element_fp &B) const
Definition: fp.hpp:150
constexpr bool operator==(const element_fp &B) const
Definition: fp.hpp:96
constexpr bool operator<(const element_fp &B) const
Definition: fp.hpp:155
constexpr element_fp()
Definition: fp.hpp:65
constexpr element_fp(const element_fp &B)
Definition: fp.hpp:76
constexpr element_fp & operator+=(const element_fp &B)
Definition: fp.hpp:124
constexpr static const modular_params_type modulus_params
Definition: fp.hpp:59
constexpr element_fp & operator--()
Definition: fp.hpp:182
constexpr element_fp & operator=(const element_fp &B)
Definition: fp.hpp:104
constexpr bool is_square() const
Definition: fp.hpp:215
constexpr bool operator!=(const element_fp &B) const
Definition: fp.hpp:100
constexpr static const integral_type modulus
Definition: fp.hpp:60
constexpr element_fp _2z_add_3x()
Definition: fp.hpp:207
constexpr bool operator<=(const element_fp &B) const
Definition: fp.hpp:163
constexpr element_fp operator-() const
Definition: fp.hpp:142
policy_type::modular_type modular_type
Definition: fp.hpp:54
constexpr element_fp pow(const PowerType pwr) const
Definition: fp.hpp:221
policy_type::field_type field_type
Definition: fp.hpp:52
constexpr static element_fp zero()
Definition: fp.hpp:80
constexpr element_fp inversed() const
Definition: fp.hpp:202
constexpr element_fp doubled() const
Definition: fp.hpp:193
constexpr element_fp(const Number &data)
Definition: fp.hpp:74
modular_type data_type
Definition: fp.hpp:62
constexpr element_fp operator++(int)
Definition: fp.hpp:176
constexpr bool operator>=(const element_fp &B) const
Definition: fp.hpp:167
constexpr element_fp squared() const
Definition: fp.hpp:210
constexpr element_fp & operator++()
Definition: fp.hpp:171
constexpr element_fp operator*(const element_fp &B) const
Definition: fp.hpp:146
constexpr element_fp pow(const multiprecision::number< Backend, ExpressionTemplates > &pwr) const
Definition: fp.hpp:227
policy_type::integral_type integral_type
Definition: fp.hpp:55
constexpr bool is_one() const
Definition: fp.hpp:92
constexpr element_fp operator+(const element_fp &B) const
Definition: fp.hpp:110
constexpr element_fp sqrt() const
Definition: fp.hpp:198
constexpr element_fp operator-(const element_fp &B) const
Definition: fp.hpp:114
constexpr element_fp & operator*=(const element_fp &B)
Definition: fp.hpp:130
constexpr element_fp & operator-=(const element_fp &B)
Definition: fp.hpp:118
policy_type::modular_params_type modular_params_type
Definition: fp.hpp:57
constexpr static element_fp one()
Definition: fp.hpp:84
constexpr element_fp & operator/=(const element_fp &B)
Definition: fp.hpp:136
Definition: pair.hpp:31