Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
alt_bn128_g2.cpp
Go to the documentation of this file.
1
9
10namespace libff {
11
12#ifdef PROFILE_OP_COUNTS
13long long alt_bn128_G2::add_cnt = 0;
14long long alt_bn128_G2::dbl_cnt = 0;
15#endif
16
17std::vector<size_t> alt_bn128_G2::wnaf_window_table;
19alt_bn128_G2 alt_bn128_G2::G2_zero = {};
20alt_bn128_G2 alt_bn128_G2::G2_one = {};
21bool alt_bn128_G2::initialized = false;
22
24{
25 if (initialized)
26 {
27 this->X = G2_zero.X;
28 this->Y = G2_zero.Y;
29 this->Z = G2_zero.Z;
30 }
31}
32
37
39{
40 if (this->is_zero())
41 {
42 printf("O\n");
43 }
44 else
45 {
46 alt_bn128_G2 copy(*this);
47 copy.to_affine_coordinates();
48 gmp_printf("(%Nd*z + %Nd , %Nd*z + %Nd)\n",
49 copy.X.c1.as_bigint().data, alt_bn128_Fq::num_limbs,
50 copy.X.c0.as_bigint().data, alt_bn128_Fq::num_limbs,
51 copy.Y.c1.as_bigint().data, alt_bn128_Fq::num_limbs,
52 copy.Y.c0.as_bigint().data, alt_bn128_Fq::num_limbs);
53 }
54}
55
57{
58 if (this->is_zero())
59 {
60 printf("O\n");
61 }
62 else
63 {
64 gmp_printf("(%Nd*z + %Nd : %Nd*z + %Nd : %Nd*z + %Nd)\n",
65 this->X.c1.as_bigint().data, alt_bn128_Fq::num_limbs,
66 this->X.c0.as_bigint().data, alt_bn128_Fq::num_limbs,
67 this->Y.c1.as_bigint().data, alt_bn128_Fq::num_limbs,
68 this->Y.c0.as_bigint().data, alt_bn128_Fq::num_limbs,
69 this->Z.c1.as_bigint().data, alt_bn128_Fq::num_limbs,
70 this->Z.c0.as_bigint().data, alt_bn128_Fq::num_limbs);
71 }
72}
73
75{
76 if (this->is_zero())
77 {
78 this->X = alt_bn128_Fq2::zero();
79 this->Y = alt_bn128_Fq2::one();
80 this->Z = alt_bn128_Fq2::zero();
81 }
82 else
83 {
84 alt_bn128_Fq2 Z_inv = Z.inverse();
85 alt_bn128_Fq2 Z2_inv = Z_inv.squared();
86 alt_bn128_Fq2 Z3_inv = Z2_inv * Z_inv;
87 this->X = this->X * Z2_inv;
88 this->Y = this->Y * Z3_inv;
89 this->Z = alt_bn128_Fq2::one();
90 }
91}
92
97
99{
100 return (this->is_zero() || this->Z == alt_bn128_Fq2::one());
101}
102
104{
105 return (this->Z.is_zero());
106}
107
109{
110 if (this->is_zero())
111 {
112 return other.is_zero();
113 }
114
115 if (other.is_zero())
116 {
117 return false;
118 }
119
120 /* now neither is O */
121
122 // using Jacobian coordinates so:
123 // (X1:Y1:Z1) = (X2:Y2:Z2)
124 // iff
125 // X1/Z1^2 == X2/Z2^2 and Y1/Z1^3 == Y2/Z2^3
126 // iff
127 // X1 * Z2^2 == X2 * Z1^2 and Y1 * Z2^3 == Y2 * Z1^3
128
129 alt_bn128_Fq2 Z1_squared = (this->Z).squared();
130 alt_bn128_Fq2 Z2_squared = (other.Z).squared();
131
132 if ((this->X * Z2_squared) != (other.X * Z1_squared))
133 {
134 return false;
135 }
136
137 alt_bn128_Fq2 Z1_cubed = (this->Z) * Z1_squared;
138 alt_bn128_Fq2 Z2_cubed = (other.Z) * Z2_squared;
139
140 if ((this->Y * Z2_cubed) != (other.Y * Z1_cubed))
141 {
142 return false;
143 }
144
145 return true;
146}
147
149{
150 return !(operator==(other));
151}
152
154{
155 // handle special cases having to do with O
156 if (this->is_zero())
157 {
158 return other;
159 }
160
161 if (other.is_zero())
162 {
163 return *this;
164 }
165
166 // no need to handle points of order 2,4
167 // (they cannot exist in a prime-order subgroup)
168
169 // check for doubling case
170
171 // using Jacobian coordinates so:
172 // (X1:Y1:Z1) = (X2:Y2:Z2)
173 // iff
174 // X1/Z1^2 == X2/Z2^2 and Y1/Z1^3 == Y2/Z2^3
175 // iff
176 // X1 * Z2^2 == X2 * Z1^2 and Y1 * Z2^3 == Y2 * Z1^3
177
178 alt_bn128_Fq2 Z1Z1 = (this->Z).squared();
179 alt_bn128_Fq2 Z2Z2 = (other.Z).squared();
180
181 alt_bn128_Fq2 U1 = this->X * Z2Z2;
182 alt_bn128_Fq2 U2 = other.X * Z1Z1;
183
184 alt_bn128_Fq2 Z1_cubed = (this->Z) * Z1Z1;
185 alt_bn128_Fq2 Z2_cubed = (other.Z) * Z2Z2;
186
187 alt_bn128_Fq2 S1 = (this->Y) * Z2_cubed; // S1 = Y1 * Z2 * Z2Z2
188 alt_bn128_Fq2 S2 = (other.Y) * Z1_cubed; // S2 = Y2 * Z1 * Z1Z1
189
190 if (U1 == U2 && S1 == S2)
191 {
192 // dbl case; nothing of above can be reused
193 return this->dbl();
194 }
195
196 // rest of add case
197 alt_bn128_Fq2 H = U2 - U1; // H = U2-U1
198 alt_bn128_Fq2 S2_minus_S1 = S2-S1;
199 alt_bn128_Fq2 I = (H+H).squared(); // I = (2 * H)^2
200 alt_bn128_Fq2 J = H * I; // J = H * I
201 alt_bn128_Fq2 r = S2_minus_S1 + S2_minus_S1; // r = 2 * (S2-S1)
202 alt_bn128_Fq2 V = U1 * I; // V = U1 * I
203 alt_bn128_Fq2 X3 = r.squared() - J - (V+V); // X3 = r^2 - J - 2 * V
204 alt_bn128_Fq2 S1_J = S1 * J;
205 alt_bn128_Fq2 Y3 = r * (V-X3) - (S1_J+S1_J); // Y3 = r * (V-X3)-2 S1 J
206 alt_bn128_Fq2 Z3 = ((this->Z+other.Z).squared()-Z1Z1-Z2Z2) * H; // Z3 = ((Z1+Z2)^2-Z1Z1-Z2Z2) * H
207
208 return alt_bn128_G2(X3, Y3, Z3);
209}
210
212{
213 return alt_bn128_G2(this->X, -(this->Y), this->Z);
214}
215
216
218{
219 return (*this) + (-other);
220}
221
223{
224 // handle special cases having to do with O
225 if (this->is_zero())
226 {
227 return other;
228 }
229
230 if (other.is_zero())
231 {
232 return *this;
233 }
234
235 // no need to handle points of order 2,4
236 // (they cannot exist in a prime-order subgroup)
237
238 // handle double case
239 if (this->operator==(other))
240 {
241 return this->dbl();
242 }
243
244#ifdef PROFILE_OP_COUNTS
245 this->add_cnt++;
246#endif
247 // NOTE: does not handle O and pts of order 2,4
248 // http://www.hyperelliptic.org/EFD/g1p/auto-shortw-projective.html#addition-add-1998-cmo-2
249
250 alt_bn128_Fq2 Z1Z1 = (this->Z).squared(); // Z1Z1 = Z1^2
251 alt_bn128_Fq2 Z2Z2 = (other.Z).squared(); // Z2Z2 = Z2^2
252 alt_bn128_Fq2 U1 = (this->X) * Z2Z2; // U1 = X1 * Z2Z2
253 alt_bn128_Fq2 U2 = (other.X) * Z1Z1; // U2 = X2 * Z1Z1
254 alt_bn128_Fq2 S1 = (this->Y) * (other.Z) * Z2Z2; // S1 = Y1 * Z2 * Z2Z2
255 alt_bn128_Fq2 S2 = (other.Y) * (this->Z) * Z1Z1; // S2 = Y2 * Z1 * Z1Z1
256 alt_bn128_Fq2 H = U2 - U1; // H = U2-U1
257 alt_bn128_Fq2 S2_minus_S1 = S2-S1;
258 alt_bn128_Fq2 I = (H+H).squared(); // I = (2 * H)^2
259 alt_bn128_Fq2 J = H * I; // J = H * I
260 alt_bn128_Fq2 r = S2_minus_S1 + S2_minus_S1; // r = 2 * (S2-S1)
261 alt_bn128_Fq2 V = U1 * I; // V = U1 * I
262 alt_bn128_Fq2 X3 = r.squared() - J - (V+V); // X3 = r^2 - J - 2 * V
263 alt_bn128_Fq2 S1_J = S1 * J;
264 alt_bn128_Fq2 Y3 = r * (V-X3) - (S1_J+S1_J); // Y3 = r * (V-X3)-2 S1 J
265 alt_bn128_Fq2 Z3 = ((this->Z+other.Z).squared()-Z1Z1-Z2Z2) * H; // Z3 = ((Z1+Z2)^2-Z1Z1-Z2Z2) * H
266
267 return alt_bn128_G2(X3, Y3, Z3);
268}
269
271{
272#ifdef DEBUG
273 assert(other.is_special());
274#endif
275
276 // handle special cases having to do with O
277 if (this->is_zero())
278 {
279 return other;
280 }
281
282 if (other.is_zero())
283 {
284 return *this;
285 }
286
287 // no need to handle points of order 2,4
288 // (they cannot exist in a prime-order subgroup)
289
290 // check for doubling case
291
292 // using Jacobian coordinates so:
293 // (X1:Y1:Z1) = (X2:Y2:Z2)
294 // iff
295 // X1/Z1^2 == X2/Z2^2 and Y1/Z1^3 == Y2/Z2^3
296 // iff
297 // X1 * Z2^2 == X2 * Z1^2 and Y1 * Z2^3 == Y2 * Z1^3
298
299 // we know that Z2 = 1
300
301 const alt_bn128_Fq2 Z1Z1 = (this->Z).squared();
302
303 const alt_bn128_Fq2 &U1 = this->X;
304 const alt_bn128_Fq2 U2 = other.X * Z1Z1;
305
306 const alt_bn128_Fq2 Z1_cubed = (this->Z) * Z1Z1;
307
308 const alt_bn128_Fq2 &S1 = (this->Y); // S1 = Y1 * Z2 * Z2Z2
309 const alt_bn128_Fq2 S2 = (other.Y) * Z1_cubed; // S2 = Y2 * Z1 * Z1Z1
310
311 if (U1 == U2 && S1 == S2)
312 {
313 // dbl case; nothing of above can be reused
314 return this->dbl();
315 }
316
317#ifdef PROFILE_OP_COUNTS
318 this->add_cnt++;
319#endif
320
321 // NOTE: does not handle O and pts of order 2,4
322 // http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-madd-2007-bl
323 alt_bn128_Fq2 H = U2-(this->X); // H = U2-X1
324 alt_bn128_Fq2 HH = H.squared() ; // HH = H&2
325 alt_bn128_Fq2 I = HH+HH; // I = 4*HH
326 I = I + I;
327 alt_bn128_Fq2 J = H*I; // J = H*I
328 alt_bn128_Fq2 r = S2-(this->Y); // r = 2*(S2-Y1)
329 r = r + r;
330 alt_bn128_Fq2 V = (this->X) * I ; // V = X1*I
331 alt_bn128_Fq2 X3 = r.squared()-J-V-V; // X3 = r^2-J-2*V
332 alt_bn128_Fq2 Y3 = (this->Y)*J; // Y3 = r*(V-X3)-2*Y1*J
333 Y3 = r*(V-X3) - Y3 - Y3;
334 alt_bn128_Fq2 Z3 = ((this->Z)+H).squared() - Z1Z1 - HH; // Z3 = (Z1+H)^2-Z1Z1-HH
335
336 return alt_bn128_G2(X3, Y3, Z3);
337}
338
340{
341#ifdef PROFILE_OP_COUNTS
342 this->dbl_cnt++;
343#endif
344 // handle point at infinity
345 if (this->is_zero())
346 {
347 return (*this);
348 }
349
350 // NOTE: does not handle O and pts of order 2,4
351 // http://www.hyperelliptic.org/EFD/g1p/auto-shortw-projective.html#doubling-dbl-2007-bl
352
353 alt_bn128_Fq2 A = (this->X).squared(); // A = X1^2
354 alt_bn128_Fq2 B = (this->Y).squared(); // B = Y1^2
355 alt_bn128_Fq2 C = B.squared(); // C = B^2
356 alt_bn128_Fq2 D = (this->X + B).squared() - A - C;
357 D = D+D; // D = 2 * ((X1 + B)^2 - A - C)
358 alt_bn128_Fq2 E = A + A + A; // E = 3 * A
359 alt_bn128_Fq2 F = E.squared(); // F = E^2
360 alt_bn128_Fq2 X3 = F - (D+D); // X3 = F - 2 D
361 alt_bn128_Fq2 eightC = C+C;
362 eightC = eightC + eightC;
363 eightC = eightC + eightC;
364 alt_bn128_Fq2 Y3 = E * (D - X3) - eightC; // Y3 = E * (D - X3) - 8 * C
365 alt_bn128_Fq2 Y1Z1 = (this->Y)*(this->Z);
366 alt_bn128_Fq2 Z3 = Y1Z1 + Y1Z1; // Z3 = 2 * Y1 * Z1
367
368 return alt_bn128_G2(X3, Y3, Z3);
369}
370
372{
373 return alt_bn128_G2(alt_bn128_twist_mul_by_q_X * (this->X).Frobenius_map(1),
374 alt_bn128_twist_mul_by_q_Y * (this->Y).Frobenius_map(1),
375 (this->Z).Frobenius_map(1));
376}
377
379{
380 if (this->is_zero())
381 {
382 return true;
383 }
384 else
385 {
386 /*
387 y^2 = x^3 + b
388
389 We are using Jacobian coordinates, so equation we need to check is actually
390
391 (y/z^3)^2 = (x/z^2)^3 + b
392 y^2 / z^6 = x^3 / z^6 + b
393 y^2 = x^3 + b z^6
394 */
395 alt_bn128_Fq2 X2 = this->X.squared();
396 alt_bn128_Fq2 Y2 = this->Y.squared();
397 alt_bn128_Fq2 Z2 = this->Z.squared();
398
399 alt_bn128_Fq2 X3 = this->X * X2;
400 alt_bn128_Fq2 Z3 = this->Z * Z2;
401 alt_bn128_Fq2 Z6 = Z3.squared();
402
403 return (Y2 == X3 + alt_bn128_twist_coeff_b * Z6);
404 }
405}
406
411
413{
414 return G2_one;
415}
416
421
422std::ostream& operator<<(std::ostream &out, const alt_bn128_G2 &g)
423{
424 alt_bn128_G2 copy(g);
425 copy.to_affine_coordinates();
426 out << (copy.is_zero() ? 1 : 0) << OUTPUT_SEPARATOR;
427#ifdef NO_PT_COMPRESSION
428 out << copy.X << OUTPUT_SEPARATOR << copy.Y;
429#else
430 /* storing LSB of Y */
431 out << copy.X << OUTPUT_SEPARATOR << (copy.Y.c0.as_bigint().data[0] & 1);
432#endif
433
434 return out;
435}
436
437std::istream& operator>>(std::istream &in, alt_bn128_G2 &g)
438{
439 char is_zero;
440 alt_bn128_Fq2 tX, tY;
441
442#ifdef NO_PT_COMPRESSION
443 in >> is_zero >> tX >> tY;
444 is_zero -= '0';
445#else
446 in.read((char*)&is_zero, 1); // this reads is_zero;
447 is_zero -= '0';
449
450 unsigned char Y_lsb;
451 in >> tX;
453 in.read((char*)&Y_lsb, 1);
454 Y_lsb -= '0';
455
456 // y = +/- sqrt(x^3 + b)
457 if (!is_zero)
458 {
459 alt_bn128_Fq2 tX2 = tX.squared();
460 alt_bn128_Fq2 tY2 = tX2 * tX + alt_bn128_twist_coeff_b;
461 tY = tY2.sqrt();
462
463 if ((tY.c0.as_bigint().data[0] & 1) != Y_lsb)
464 {
465 tY = -tY;
466 }
467 }
468#endif
469 // using projective coordinates
470 if (!is_zero)
471 {
472 g.X = tX;
473 g.Y = tY;
474 g.Z = alt_bn128_Fq2::one();
475 }
476 else
477 {
478 g = alt_bn128_G2::zero();
479 }
480
481 return in;
482}
483
484void alt_bn128_G2::batch_to_special_all_non_zeros(std::vector<alt_bn128_G2> &vec)
485{
486 std::vector<alt_bn128_Fq2> Z_vec;
487 Z_vec.reserve(vec.size());
488
489 for (auto &el: vec)
490 {
491 Z_vec.emplace_back(el.Z);
492 }
494
496
497 for (size_t i = 0; i < vec.size(); ++i)
498 {
499 alt_bn128_Fq2 Z2 = Z_vec[i].squared();
500 alt_bn128_Fq2 Z3 = Z_vec[i] * Z2;
501
502 vec[i].X = vec[i].X * Z2;
503 vec[i].Y = vec[i].Y * Z3;
504 vec[i].Z = one;
505 }
506}
507
508} // libff
const mie::Vuint & r
Definition bn.cpp:28
bool is_zero() const
Definition fp2.hpp:60
Fp2_model sqrt() const
Fp2_model inverse() const
Fp2_model squared() const
bigint< n > as_bigint() const
static Fp_model< n, modulus > random_element()
bool operator!=(const alt_bn128_G2 &other) const
static alt_bn128_G2 zero()
static std::vector< size_t > wnaf_window_table
static alt_bn128_Fq2 mul_by_b(const alt_bn128_Fq2 &elt)
alt_bn128_G2 mixed_add(const alt_bn128_G2 &other) const
static alt_bn128_G2 random_element()
alt_bn128_G2 add(const alt_bn128_G2 &other) const
alt_bn128_G2 operator+(const alt_bn128_G2 &other) const
bool operator==(const alt_bn128_G2 &other) const
alt_bn128_G2 operator-() const
static alt_bn128_G2 one()
static alt_bn128_G2 G2_one
static alt_bn128_G2 G2_zero
static std::vector< size_t > fixed_base_exp_window_table
bool is_well_formed() const
static bool initialized
void print_coordinates() const
static void batch_to_special_all_non_zeros(std::vector< alt_bn128_G2 > &vec)
bool is_special() const
alt_bn128_G2 dbl() const
alt_bn128_G2 mul_by_q() const
#define D(var, file, col, who, lev,...)
Definition debug.h:44
#define OUTPUT_SEPARATOR
std::istream & operator>>(std::istream &in, alt_bn128_G1 &g)
void consume_OUTPUT_SEPARATOR(std::istream &in)
Fp2_model< alt_bn128_q_limbs, alt_bn128_modulus_q > alt_bn128_Fq2
alt_bn128_Fq alt_bn128_twist_mul_by_b_c0
alt_bn128_Fq2 alt_bn128_twist_mul_by_q_Y
std::ostream & operator<<(std::ostream &out, const alt_bn128_G1 &g)
alt_bn128_Fq2 alt_bn128_twist_coeff_b
alt_bn128_Fq alt_bn128_twist_mul_by_b_c1
void batch_invert(std::vector< FieldT > &vec)
alt_bn128_Fq2 alt_bn128_twist_mul_by_q_X
Definition test_zm.cpp:19
Definition lib.h:43
#define A