Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
mnt4_g2.cpp
Go to the documentation of this file.
1
15
16namespace libff {
17
18#ifdef PROFILE_OP_COUNTS
19long long mnt4_G2::add_cnt = 0;
20long long mnt4_G2::dbl_cnt = 0;
21#endif
22
23std::vector<size_t> mnt4_G2::wnaf_window_table;
24std::vector<size_t> mnt4_G2::fixed_base_exp_window_table;
28mnt4_G2 mnt4_G2::G2_zero = {};
29mnt4_G2 mnt4_G2::G2_one = {};
31
36
41
43{
45 {
46 this->X = G2_zero.X;
47 this->Y = G2_zero.Y;
48 this->Z = G2_zero.Z;
49 }
50}
51
52void mnt4_G2::print() const
53{
54 if (this->is_zero())
55 {
56 printf("O\n");
57 }
58 else
59 {
60 mnt4_G2 copy(*this);
61 copy.to_affine_coordinates();
62 gmp_printf("(%Nd*z + %Nd , %Nd*z + %Nd)\n",
63 copy.X.c1.as_bigint().data, mnt4_Fq::num_limbs,
64 copy.X.c0.as_bigint().data, mnt4_Fq::num_limbs,
65 copy.Y.c1.as_bigint().data, mnt4_Fq::num_limbs,
66 copy.Y.c0.as_bigint().data, mnt4_Fq::num_limbs);
67 }
68}
69
71{
72 if (this->is_zero())
73 {
74 printf("O\n");
75 }
76 else
77 {
78 gmp_printf("(%Nd*z + %Nd : %Nd*z + %Nd : %Nd*z + %Nd)\n",
79 this->X.c1.as_bigint().data, mnt4_Fq::num_limbs,
80 this->X.c0.as_bigint().data, mnt4_Fq::num_limbs,
81 this->Y.c1.as_bigint().data, mnt4_Fq::num_limbs,
82 this->Y.c0.as_bigint().data, mnt4_Fq::num_limbs,
83 this->Z.c1.as_bigint().data, mnt4_Fq::num_limbs,
84 this->Z.c0.as_bigint().data, mnt4_Fq::num_limbs);
85 }
86}
87
89{
90 if (this->is_zero())
91 {
92 this->X = mnt4_Fq2::zero();
93 this->Y = mnt4_Fq2::one();
94 this->Z = mnt4_Fq2::zero();
95 }
96 else
97 {
98 const mnt4_Fq2 Z_inv = Z.inverse();
99 X = X * Z_inv;
100 Y = Y * Z_inv;
101 Z = mnt4_Fq2::one();
102 }
103}
104
106{
107 this->to_affine_coordinates();
108}
109
111{
112 return (this->is_zero() || this->Z == mnt4_Fq2::one());
113}
114
116{
117 return (this->X.is_zero() && this->Z.is_zero());
118}
119
120bool mnt4_G2::operator==(const mnt4_G2 &other) const
121{
122 if (this->is_zero())
123 {
124 return other.is_zero();
125 }
126
127 if (other.is_zero())
128 {
129 return false;
130 }
131
132 /* now neither is O */
133
134 // X1/Z1 = X2/Z2 <=> X1*Z2 = X2*Z1
135 if ((this->X * other.Z) != (other.X * this->Z))
136 {
137 return false;
138 }
139
140 // Y1/Z1 = Y2/Z2 <=> Y1*Z2 = Y2*Z1
141 if ((this->Y * other.Z) != (other.Y * this->Z))
142 {
143 return false;
144 }
145
146 return true;
147}
148
149bool mnt4_G2::operator!=(const mnt4_G2& other) const
150{
151 return !(operator==(other));
152}
153
155{
156 // handle special cases having to do with O
157 if (this->is_zero())
158 {
159 return other;
160 }
161
162 if (other.is_zero())
163 {
164 return *this;
165 }
166
167 // no need to handle points of order 2,4
168 // (they cannot exist in a prime-order subgroup)
169
170 // handle double case, and then all the rest
171 /*
172 The code below is equivalent to (but faster than) the snippet below:
173
174 if (this->operator==(other))
175 {
176 return this->dbl();
177 }
178 else
179 {
180 return this->add(other);
181 }
182 */
183
184 const mnt4_Fq2 X1Z2 = (this->X) * (other.Z); // X1Z2 = X1*Z2
185 const mnt4_Fq2 X2Z1 = (this->Z) * (other.X); // X2Z1 = X2*Z1
186
187 // (used both in add and double checks)
188
189 const mnt4_Fq2 Y1Z2 = (this->Y) * (other.Z); // Y1Z2 = Y1*Z2
190 const mnt4_Fq2 Y2Z1 = (this->Z) * (other.Y); // Y2Z1 = Y2*Z1
191
192 if (X1Z2 == X2Z1 && Y1Z2 == Y2Z1)
193 {
194 // perform dbl case
195 const mnt4_Fq2 XX = (this->X).squared(); // XX = X1^2
196 const mnt4_Fq2 ZZ = (this->Z).squared(); // ZZ = Z1^2
197 const mnt4_Fq2 w = mnt4_G2::mul_by_a(ZZ) + (XX + XX + XX); // w = a*ZZ + 3*XX
198 const mnt4_Fq2 Y1Z1 = (this->Y) * (this->Z);
199 const mnt4_Fq2 s = Y1Z1 + Y1Z1; // s = 2*Y1*Z1
200 const mnt4_Fq2 ss = s.squared(); // ss = s^2
201 const mnt4_Fq2 sss = s * ss; // sss = s*ss
202 const mnt4_Fq2 R = (this->Y) * s; // R = Y1*s
203 const mnt4_Fq2 RR = R.squared(); // RR = R^2
204 const mnt4_Fq2 B = ((this->X)+R).squared()-XX-RR; // B = (X1+R)^2 - XX - RR
205 const mnt4_Fq2 h = w.squared() - (B+B); // h = w^2 - 2*B
206 const mnt4_Fq2 X3 = h * s; // X3 = h*s
207 const mnt4_Fq2 Y3 = w * (B-h)-(RR+RR); // Y3 = w*(B-h) - 2*RR
208 const mnt4_Fq2 Z3 = sss; // Z3 = sss
209
210 return mnt4_G2(X3, Y3, Z3);
211 }
212
213 // if we have arrived here we are in the add case
214 const mnt4_Fq2 Z1Z2 = (this->Z) * (other.Z); // Z1Z2 = Z1*Z2
215 const mnt4_Fq2 u = Y2Z1 - Y1Z2; // u = Y2*Z1-Y1Z2
216 const mnt4_Fq2 uu = u.squared(); // uu = u^2
217 const mnt4_Fq2 v = X2Z1 - X1Z2; // v = X2*Z1-X1Z2
218 const mnt4_Fq2 vv = v.squared(); // vv = v^2
219 const mnt4_Fq2 vvv = v * vv; // vvv = v*vv
220 const mnt4_Fq2 R = vv * X1Z2; // R = vv*X1Z2
221 const mnt4_Fq2 A = uu * Z1Z2 - (vvv + R + R); // A = uu*Z1Z2 - vvv - 2*R
222 const mnt4_Fq2 X3 = v * A; // X3 = v*A
223 const mnt4_Fq2 Y3 = u * (R-A) - vvv * Y1Z2; // Y3 = u*(R-A) - vvv*Y1Z2
224 const mnt4_Fq2 Z3 = vvv * Z1Z2; // Z3 = vvv*Z1Z2
225
226 return mnt4_G2(X3, Y3, Z3);
227}
228
230{
231 return mnt4_G2(this->X, -(this->Y), this->Z);
232}
233
234
236{
237 return (*this) + (-other);
238}
239
240mnt4_G2 mnt4_G2::add(const mnt4_G2 &other) const
241{
242 // handle special cases having to do with O
243 if (this->is_zero())
244 {
245 return other;
246 }
247
248 if (other.is_zero())
249 {
250 return (*this);
251 }
252
253 // no need to handle points of order 2,4
254 // (they cannot exist in a prime-order subgroup)
255
256 // handle double case
257 if (this->operator==(other))
258 {
259 return this->dbl();
260 }
261
262#ifdef PROFILE_OP_COUNTS
263 this->add_cnt++;
264#endif
265 // NOTE: does not handle O and pts of order 2,4
266 // http://www.hyperelliptic.org/EFD/g1p/auto-shortw-projective.html#addition-add-1998-cmo-2
267
268 const mnt4_Fq2 Y1Z2 = (this->Y) * (other.Z); // Y1Z2 = Y1*Z2
269 const mnt4_Fq2 X1Z2 = (this->X) * (other.Z); // X1Z2 = X1*Z2
270 const mnt4_Fq2 Z1Z2 = (this->Z) * (other.Z); // Z1Z2 = Z1*Z2
271 const mnt4_Fq2 u = (other.Y) * (this->Z) - Y1Z2; // u = Y2*Z1-Y1Z2
272 const mnt4_Fq2 uu = u.squared(); // uu = u^2
273 const mnt4_Fq2 v = (other.X) * (this->Z) - X1Z2; // v = X2*Z1-X1Z2
274 const mnt4_Fq2 vv = v.squared(); // vv = v^2
275 const mnt4_Fq2 vvv = v * vv; // vvv = v*vv
276 const mnt4_Fq2 R = vv * X1Z2; // R = vv*X1Z2
277 const mnt4_Fq2 A = uu * Z1Z2 - (vvv + R + R); // A = uu*Z1Z2 - vvv - 2*R
278 const mnt4_Fq2 X3 = v * A; // X3 = v*A
279 const mnt4_Fq2 Y3 = u * (R-A) - vvv * Y1Z2; // Y3 = u*(R-A) - vvv*Y1Z2
280 const mnt4_Fq2 Z3 = vvv * Z1Z2; // Z3 = vvv*Z1Z2
281
282 return mnt4_G2(X3, Y3, Z3);
283}
284
286{
287#ifdef PROFILE_OP_COUNTS
288 this->add_cnt++;
289#endif
290 // NOTE: does not handle O and pts of order 2,4
291 // http://www.hyperelliptic.org/EFD/g1p/auto-shortw-projective.html#addition-add-1998-cmo-2
292 //assert(other.Z == mnt4_Fq2::one());
293
294 if (this->is_zero())
295 {
296 return other;
297 }
298
299 if (other.is_zero())
300 {
301 return (*this);
302 }
303
304#ifdef DEBUG
305 assert(other.is_special());
306#endif
307
308 const mnt4_Fq2 &X1Z2 = (this->X); // X1Z2 = X1*Z2 (but other is special and not zero)
309 const mnt4_Fq2 X2Z1 = (this->Z) * (other.X); // X2Z1 = X2*Z1
310
311 // (used both in add and double checks)
312
313 const mnt4_Fq2 &Y1Z2 = (this->Y); // Y1Z2 = Y1*Z2 (but other is special and not zero)
314 const mnt4_Fq2 Y2Z1 = (this->Z) * (other.Y); // Y2Z1 = Y2*Z1
315
316 if (X1Z2 == X2Z1 && Y1Z2 == Y2Z1)
317 {
318 return this->dbl();
319 }
320
321 const mnt4_Fq2 u = Y2Z1 - this->Y; // u = Y2*Z1-Y1
322 const mnt4_Fq2 uu = u.squared(); // uu = u2
323 const mnt4_Fq2 v = X2Z1 - this->X; // v = X2*Z1-X1
324 const mnt4_Fq2 vv = v.squared(); // vv = v2
325 const mnt4_Fq2 vvv = v*vv; // vvv = v*vv
326 const mnt4_Fq2 R = vv * this->X; // R = vv*X1
327 const mnt4_Fq2 A = uu * this->Z - vvv - R - R; // A = uu*Z1-vvv-2*R
328 const mnt4_Fq2 X3 = v * A; // X3 = v*A
329 const mnt4_Fq2 Y3 = u*(R-A) - vvv * this->Y; // Y3 = u*(R-A)-vvv*Y1
330 const mnt4_Fq2 Z3 = vvv * this->Z; // Z3 = vvv*Z1
331
332 return mnt4_G2(X3, Y3, Z3);
333}
334
336{
337#ifdef PROFILE_OP_COUNTS
338 this->dbl_cnt++;
339#endif
340 if (this->is_zero())
341 {
342 return (*this);
343 }
344 else
345 {
346 // NOTE: does not handle O and pts of order 2,4
347 // http://www.hyperelliptic.org/EFD/g1p/auto-shortw-projective.html#doubling-dbl-2007-bl
348
349 const mnt4_Fq2 XX = (this->X).squared(); // XX = X1^2
350 const mnt4_Fq2 ZZ = (this->Z).squared(); // ZZ = Z1^2
351 const mnt4_Fq2 w = mnt4_G2::mul_by_a(ZZ) + (XX + XX + XX); // w = a*ZZ + 3*XX
352 const mnt4_Fq2 Y1Z1 = (this->Y) * (this->Z);
353 const mnt4_Fq2 s = Y1Z1 + Y1Z1; // s = 2*Y1*Z1
354 const mnt4_Fq2 ss = s.squared(); // ss = s^2
355 const mnt4_Fq2 sss = s * ss; // sss = s*ss
356 const mnt4_Fq2 R = (this->Y) * s; // R = Y1*s
357 const mnt4_Fq2 RR = R.squared(); // RR = R^2
358 const mnt4_Fq2 B = ((this->X)+R).squared()-XX-RR; // B = (X1+R)^2 - XX - RR
359 const mnt4_Fq2 h = w.squared() - (B+B); // h = w^2-2*B
360 const mnt4_Fq2 X3 = h * s; // X3 = h*s
361 const mnt4_Fq2 Y3 = w * (B-h)-(RR+RR); // Y3 = w*(B-h) - 2*RR
362 const mnt4_Fq2 Z3 = sss; // Z3 = sss
363
364 return mnt4_G2(X3, Y3, Z3);
365 }
366}
367
369{
370 return mnt4_G2(mnt4_twist_mul_by_q_X * (this->X).Frobenius_map(1),
371 mnt4_twist_mul_by_q_Y * (this->Y).Frobenius_map(1),
372 (this->Z).Frobenius_map(1));
373}
374
376{
377 if (this->is_zero())
378 {
379 return true;
380 }
381 else
382 {
383 /*
384 y^2 = x^3 + ax + b
385
386 We are using projective, so equation we need to check is actually
387
388 (y/z)^2 = (x/z)^3 + a (x/z) + b
389 z y^2 = x^3 + a z^2 x + b z^3
390
391 z (y^2 - b z^2) = x ( x^2 + a z^2)
392 */
393 const mnt4_Fq2 X2 = this->X.squared();
394 const mnt4_Fq2 Y2 = this->Y.squared();
395 const mnt4_Fq2 Z2 = this->Z.squared();
396 const mnt4_Fq2 aZ2 = mnt4_twist_coeff_a * Z2;
397
398 return (this->Z * (Y2 - mnt4_twist_coeff_b * Z2) == this->X * (X2 + aZ2));
399 }
400}
401
403{
404 return G2_zero;
405}
406
408{
409 return G2_one;
410}
411
413{
414 return (mnt4_Fr::random_element().as_bigint()) * G2_one;
415}
416
417std::ostream& operator<<(std::ostream &out, const mnt4_G2 &g)
418{
419 mnt4_G2 copy(g);
420 copy.to_affine_coordinates();
421
422 out << (copy.is_zero() ? 1 : 0) << OUTPUT_SEPARATOR;
423#ifdef NO_PT_COMPRESSION
424 out << copy.X << OUTPUT_SEPARATOR << copy.Y;
425#else
426 /* storing LSB of Y */
427 out << copy.X << OUTPUT_SEPARATOR << (copy.Y.c0.as_bigint().data[0] & 1);
428#endif
429
430 return out;
431}
432
433std::istream& operator>>(std::istream &in, mnt4_G2 &g)
434{
435 char is_zero;
436 mnt4_Fq2 tX, tY;
437
438#ifdef NO_PT_COMPRESSION
439 in >> is_zero >> tX >> tY;
440 is_zero -= '0';
441#else
442 in.read((char*)&is_zero, 1); // this reads is_zero;
443 is_zero -= '0';
445
446 unsigned char Y_lsb;
447 in >> tX;
449 in.read((char*)&Y_lsb, 1);
450 Y_lsb -= '0';
451
452 // y = +/- sqrt(x^3 + a*x + b)
453 if (!is_zero)
454 {
455 mnt4_Fq2 tX2 = tX.squared();
456 mnt4_Fq2 tY2 = (tX2 + mnt4_twist_coeff_a ) * tX + mnt4_twist_coeff_b;
457 tY = tY2.sqrt();
458
459 if ((tY.c0.as_bigint().data[0] & 1) != Y_lsb)
460 {
461 tY = -tY;
462 }
463 }
464#endif
465 // using projective coordinates
466 if (!is_zero)
467 {
468 g.X = tX;
469 g.Y = tY;
470 g.Z = mnt4_Fq2::one();
471 }
472 else
473 {
474 g = mnt4_G2::zero();
475 }
476
477 return in;
478}
479
480void mnt4_G2::batch_to_special_all_non_zeros(std::vector<mnt4_G2> &vec)
481{
482 std::vector<mnt4_Fq2> Z_vec;
483 Z_vec.reserve(vec.size());
484
485 for (auto &el: vec)
486 {
487 Z_vec.emplace_back(el.Z);
488 }
490
491 const mnt4_Fq2 one = mnt4_Fq2::one();
492
493 for (size_t i = 0; i < vec.size(); ++i)
494 {
495 vec[i] = mnt4_G2(vec[i].X * Z_vec[i], vec[i].Y * Z_vec[i], one);
496 }
497}
498
499} // libff
static Fp2_model< n, modulus > zero()
Fp2_model sqrt() const
Fp2_model inverse() const
static Fp2_model< n, modulus > one()
Fp2_model squared() const
bigint< n > as_bigint() const
static Fp_model< n, modulus > random_element()
static bool initialized
Definition mnt4_g2.hpp:36
static mnt4_G2 random_element()
Definition mnt4_g2.cpp:412
bool operator!=(const mnt4_G2 &other) const
Definition mnt4_g2.cpp:149
static std::vector< size_t > fixed_base_exp_window_table
Definition mnt4_g2.hpp:33
static std::vector< size_t > wnaf_window_table
Definition mnt4_g2.hpp:32
static mnt4_Fq2 coeff_b
Definition mnt4_g2.hpp:39
mnt4_G2 mixed_add(const mnt4_G2 &other) const
Definition mnt4_g2.cpp:285
static mnt4_G2 G2_zero
Definition mnt4_g2.hpp:34
mnt4_G2 operator-() const
Definition mnt4_g2.cpp:229
void print_coordinates() const
Definition mnt4_g2.cpp:70
static void batch_to_special_all_non_zeros(std::vector< mnt4_G2 > &vec)
Definition mnt4_g2.cpp:480
bool is_well_formed() const
Definition mnt4_g2.cpp:375
static mnt4_G2 zero()
Definition mnt4_g2.cpp:402
void to_special()
Definition mnt4_g2.cpp:105
bool is_special() const
Definition mnt4_g2.cpp:110
mnt4_G2 dbl() const
Definition mnt4_g2.cpp:335
mnt4_G2 operator+(const mnt4_G2 &other) const
Definition mnt4_g2.cpp:154
static mnt4_G2 G2_one
Definition mnt4_g2.hpp:35
static mnt4_Fq2 coeff_a
Definition mnt4_g2.hpp:38
static mnt4_Fq2 mul_by_b(const mnt4_Fq2 &elt)
Definition mnt4_g2.cpp:37
void to_affine_coordinates()
Definition mnt4_g2.cpp:88
void print() const
Definition mnt4_g2.cpp:52
bool operator==(const mnt4_G2 &other) const
Definition mnt4_g2.cpp:120
mnt4_G2 mul_by_q() const
Definition mnt4_g2.cpp:368
mnt4_G2 add(const mnt4_G2 &other) const
Definition mnt4_g2.cpp:240
mnt4_Fq2 Y
Definition mnt4_g2.hpp:45
mnt4_Fq2 X
Definition mnt4_g2.hpp:45
static mnt4_Fq2 mul_by_a(const mnt4_Fq2 &elt)
Definition mnt4_g2.cpp:32
mnt4_Fq2 Z
Definition mnt4_g2.hpp:45
static mnt4_Fq2 twist
Definition mnt4_g2.hpp:37
static mnt4_G2 one()
Definition mnt4_g2.cpp:407
bool is_zero() const
Definition mnt4_g2.cpp:115
#define OUTPUT_SEPARATOR
std::istream & operator>>(std::istream &in, alt_bn128_G1 &g)
void consume_OUTPUT_SEPARATOR(std::istream &in)
mnt4_Fq mnt4_twist_mul_by_b_c1
Definition mnt4_init.cpp:29
std::ostream & operator<<(std::ostream &out, const alt_bn128_G1 &g)
mnt4_Fq mnt4_twist_mul_by_a_c0
Definition mnt4_init.cpp:26
mnt4_Fq2 mnt4_twist_coeff_b
Definition mnt4_init.cpp:25
mnt4_Fq mnt4_twist_mul_by_q_X
Definition mnt4_init.cpp:30
mnt4_Fq mnt4_twist_mul_by_a_c1
Definition mnt4_init.cpp:27
mnt4_Fq2 mnt4_twist_coeff_a
Definition mnt4_init.cpp:24
Fp2_model< mnt4_q_limbs, mnt4_modulus_q > mnt4_Fq2
Definition mnt4_init.hpp:37
mnt4_Fq mnt4_twist_mul_by_b_c0
Definition mnt4_init.cpp:28
mnt4_Fq mnt4_twist_mul_by_q_Y
Definition mnt4_init.cpp:31
void batch_invert(std::vector< FieldT > &vec)
Definition lib.h:43
#define R
#define A
char * s