Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
libff::bn128_G2 Class Reference

#include <bn128_g2.hpp>

Collaboration diagram for libff::bn128_G2:

Public Types

typedef bn128_Fq base_field
 
typedef bn128_Fr scalar_field
 

Public Member Functions

void fill_coord (bn::Fp2 coord[3]) const
 
 bn128_G2 ()
 
 bn128_G2 (bn::Fp2 coord[3])
 
void print () const
 
void print_coordinates () const
 
void to_affine_coordinates ()
 
void to_special ()
 
bool is_special () const
 
bool is_zero () const
 
bool operator== (const bn128_G2 &other) const
 
bool operator!= (const bn128_G2 &other) const
 
bn128_G2 operator+ (const bn128_G2 &other) const
 
bn128_G2 operator- () const
 
bn128_G2 operator- (const bn128_G2 &other) const
 
bn128_G2 add (const bn128_G2 &other) const
 
bn128_G2 mixed_add (const bn128_G2 &other) const
 
bn128_G2 dbl () const
 
bool is_well_formed () const
 

Static Public Member Functions

static bn128_G2 zero ()
 
static bn128_G2 one ()
 
static bn128_G2 random_element ()
 
static size_t size_in_bits ()
 
static bigint< base_field::num_limbsbase_field_char ()
 
static bigint< scalar_field::num_limbsorder ()
 
static void batch_to_special_all_non_zeros (std::vector< bn128_G2 > &vec)
 

Public Attributes

bn::Fp2 X
 
bn::Fp2 Y
 
bn::Fp2 Z
 

Static Public Attributes

static std::vector< size_t > wnaf_window_table
 
static std::vector< size_t > fixed_base_exp_window_table
 
static bn128_G2 G2_zero = {}
 
static bn128_G2 G2_one = {}
 
static bool initialized = false
 

Friends

std::ostream & operator<< (std::ostream &out, const bn128_G2 &g)
 
std::istream & operator>> (std::istream &in, bn128_G2 &g)
 

Detailed Description

Definition at line 24 of file bn128_g2.hpp.

Member Typedef Documentation

◆ base_field

Definition at line 38 of file bn128_g2.hpp.

◆ scalar_field

Constructor & Destructor Documentation

◆ bn128_G2() [1/2]

libff::bn128_G2::bn128_G2 ( )

Definition at line 74 of file bn128_g2.cpp.

75{
77 {
78 this->X = G2_zero.X;
79 this->Y = G2_zero.Y;
80 this->Z = G2_zero.Z;
81 }
82}
static bn128_G2 G2_zero
Definition bn128_g2.hpp:34
static bool initialized
Definition bn128_g2.hpp:36
Definition lib.h:43

◆ bn128_G2() [2/2]

libff::bn128_G2::bn128_G2 ( bn::Fp2 coord[3])
inline

Definition at line 45 of file bn128_g2.hpp.

45: X(coord[0]), Y(coord[1]), Z(coord[2]) {};

Member Function Documentation

◆ add()

bn128_G2 libff::bn128_G2::add ( const bn128_G2 & other) const

Definition at line 224 of file bn128_g2.cpp.

225{
226#ifdef PROFILE_OP_COUNTS
227 this->add_cnt++;
228#endif
229
230 bn::Fp2 this_coord[3], other_coord[3], result_coord[3];
231 this->fill_coord(this_coord);
232 other.fill_coord(other_coord);
233 bn::ecop::ECAdd(result_coord, this_coord, other_coord);
234
235 bn128_G2 result(result_coord);
236 return result;
237}
void fill_coord(bn::Fp2 coord[3]) const
Definition bn128_g2.hpp:42
void ECAdd(FF *out, const FF *a, const FF *b)
Definition bn.h:2526
Definition bn.h:348
Here is the call graph for this function:
Here is the caller graph for this function:

◆ base_field_char()

static bigint< base_field::num_limbs > libff::bn128_G2::base_field_char ( )
inlinestatic

Definition at line 74 of file bn128_g2.hpp.

74{ return base_field::field_char(); }
static bigint< n > field_char()
Definition fp.hpp:116
Here is the call graph for this function:

◆ batch_to_special_all_non_zeros()

void libff::bn128_G2::batch_to_special_all_non_zeros ( std::vector< bn128_G2 > & vec)
static

Definition at line 489 of file bn128_g2.cpp.

490{
491 std::vector<bn::Fp2> Z_vec;
492 Z_vec.reserve(vec.size());
493
494 for (auto &el: vec)
495 {
496 Z_vec.emplace_back(el.Z);
497 }
499
500 const bn::Fp2 one = 1;
501
502 for (size_t i = 0; i < vec.size(); ++i)
503 {
504 bn::Fp2 Z2, Z3;
505 bn::Fp2::square(Z2, Z_vec[i]);
506 bn::Fp2::mul(Z3, Z2, Z_vec[i]);
507
508 bn::Fp2::mul(vec[i].X, vec[i].X, Z2);
509 bn::Fp2::mul(vec[i].Y, vec[i].Y, Z3);
510 vec[i].Z = one;
511 }
512}
static bn128_G2 one()
Definition bn128_g2.cpp:376
void bn_batch_invert(std::vector< FieldT > &vec)
static void(* square)(Fp2T &z, const Fp2T &x)
Definition bn.h:373
static void(* mul)(Fp2T &z, const Fp2T &x, const Fp2T &y)
Definition bn.h:372
Here is the call graph for this function:

◆ dbl()

bn128_G2 libff::bn128_G2::dbl ( ) const

Definition at line 326 of file bn128_g2.cpp.

327{
328#ifdef PROFILE_OP_COUNTS
329 this->dbl_cnt++;
330#endif
331
332 bn::Fp2 this_coord[3], result_coord[3];
333 this->fill_coord(this_coord);
334 bn::ecop::ECDouble(result_coord, this_coord);
335
336 bn128_G2 result(result_coord);
337 return result;
338}
void ECDouble(FF *out, const FF *in)
Definition bn.h:2497
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fill_coord()

void libff::bn128_G2::fill_coord ( bn::Fp2 coord[3]) const
inline

Definition at line 42 of file bn128_g2.hpp.

42{ coord[0] = this->X; coord[1] = this->Y; coord[2] = this->Z; };
Here is the caller graph for this function:

◆ is_special()

bool libff::bn128_G2::is_special ( ) const

Definition at line 136 of file bn128_g2.cpp.

137{
138 return (this->is_zero() || this->Z == 1);
139}
bool is_zero() const
Definition bn128_g2.cpp:141
Here is the call graph for this function:

◆ is_well_formed()

bool libff::bn128_G2::is_well_formed ( ) const

Definition at line 340 of file bn128_g2.cpp.

341{
342 if (this->is_zero())
343 {
344 return true;
345 }
346 else
347 {
348 /*
349 y^2 = x^3 + b
350
351 We are using Jacobian coordinates, so equation we need to check is actually
352
353 (y/z^3)^2 = (x/z^2)^3 + b
354 y^2 / z^6 = x^3 / z^6 + b
355 y^2 = x^3 + b z^6
356 */
357 bn::Fp2 X2, Y2, Z2;
358 bn::Fp2::square(X2, this->X);
359 bn::Fp2::square(Y2, this->Y);
360 bn::Fp2::square(Z2, this->Z);
361
362 bn::Fp2 X3, Z3, Z6;
363 bn::Fp2::mul(X3, X2, this->X);
364 bn::Fp2::mul(Z3, Z2, this->Z);
365 bn::Fp2::square(Z6, Z3);
366
367 return (Y2 == X3 + bn128_twist_coeff_b * Z6);
368 }
369}
bn::Fp2 bn128_twist_coeff_b
Here is the call graph for this function:

◆ is_zero()

bool libff::bn128_G2::is_zero ( ) const

Definition at line 141 of file bn128_g2.cpp.

142{
143 return Z.isZero();
144}
bool isZero() const
Definition bn.h:364
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mixed_add()

bn128_G2 libff::bn128_G2::mixed_add ( const bn128_G2 & other) const

Definition at line 239 of file bn128_g2.cpp.

240{
241 if (this->is_zero())
242 {
243 return other;
244 }
245
246 if (other.is_zero())
247 {
248 return *this;
249 }
250
251 // no need to handle points of order 2,4
252 // (they cannot exist in a prime-order subgroup)
253
254#ifdef DEBUG
255 assert(other.is_special());
256#endif
257
258 // check for doubling case
259
260 // using Jacobian coordinates so:
261 // (X1:Y1:Z1) = (X2:Y2:Z2)
262 // iff
263 // X1/Z1^2 == X2/Z2^2 and Y1/Z1^3 == Y2/Z2^3
264 // iff
265 // X1 * Z2^2 == X2 * Z1^2 and Y1 * Z2^3 == Y2 * Z1^3
266
267 // we know that Z2 = 1
268
269 bn::Fp2 Z1Z1;
270 bn::Fp2::square(Z1Z1, this->Z);
271 const bn::Fp2 &U1 = this->X;
272 bn::Fp2 U2;
273 bn::Fp2::mul(U2, other.X, Z1Z1);
274 bn::Fp2 Z1_cubed;
275 bn::Fp2::mul(Z1_cubed, this->Z, Z1Z1);
276
277 const bn::Fp2 &S1 = this->Y;
278 bn::Fp2 S2;
279 bn::Fp2::mul(S2, other.Y, Z1_cubed); // S2 = Y2*Z1*Z1Z1
280
281 if (U1 == U2 && S1 == S2)
282 {
283 // dbl case; nothing of above can be reused
284 return this->dbl();
285 }
286
287#ifdef PROFILE_OP_COUNTS
288 this->add_cnt++;
289#endif
290
291 bn128_G2 result;
292 bn::Fp2 H, HH, I, J, r, V, tmp;
293 // H = U2-X1
294 bn::Fp2::sub(H, U2, this->X);
295 // HH = H^2
296 bn::Fp2::square(HH, H);
297 // I = 4*HH
298 bn::Fp2::add(tmp, HH, HH);
299 bn::Fp2::add(I, tmp, tmp);
300 // J = H*I
301 bn::Fp2::mul(J, H, I);
302 // r = 2*(S2-Y1)
303 bn::Fp2::sub(tmp, S2, this->Y);
304 bn::Fp2::add(r, tmp, tmp);
305 // V = X1*I
306 bn::Fp2::mul(V, this->X, I);
307 // X3 = r^2-J-2*V
308 bn::Fp2::square(result.X, r);
309 bn::Fp2::sub(result.X, result.X, J);
310 bn::Fp2::sub(result.X, result.X, V);
311 bn::Fp2::sub(result.X, result.X, V);
312 // Y3 = r*(V-X3)-2*Y1*J
313 bn::Fp2::sub(tmp, V, result.X);
314 bn::Fp2::mul(result.Y, r, tmp);
315 bn::Fp2::mul(tmp, this->Y, J);
316 bn::Fp2::sub(result.Y, result.Y, tmp);
317 bn::Fp2::sub(result.Y, result.Y, tmp);
318 // Z3 = (Z1+H)^2-Z1Z1-HH
319 bn::Fp2::add(tmp, this->Z, H);
320 bn::Fp2::square(result.Z, tmp);
321 bn::Fp2::sub(result.Z, result.Z, Z1Z1);
322 bn::Fp2::sub(result.Z, result.Z, HH);
323 return result;
324}
const mie::Vuint & r
Definition bn.cpp:28
bn128_G2 dbl() const
Definition bn128_g2.cpp:326
Definition test_zm.cpp:19
static void(* sub)(Fp2T &z, const Fp2T &x, const Fp2T &y)
Definition bn.h:370
static void(* add)(Fp2T &z, const Fp2T &x, const Fp2T &y)
Definition bn.h:368
Here is the call graph for this function:

◆ one()

bn128_G2 libff::bn128_G2::one ( )
static

Definition at line 376 of file bn128_g2.cpp.

377{
378 return G2_one;
379}
static bn128_G2 G2_one
Definition bn128_g2.hpp:35
Here is the caller graph for this function:

◆ operator!=()

bool libff::bn128_G2::operator!= ( const bn128_G2 & other) const

Definition at line 180 of file bn128_g2.cpp.

181{
182 return !(operator==(other));
183}
bool operator==(const bn128_G2 &other) const
Definition bn128_g2.cpp:146
Here is the call graph for this function:

◆ operator+()

bn128_G2 libff::bn128_G2::operator+ ( const bn128_G2 & other) const

Definition at line 185 of file bn128_g2.cpp.

186{
187 // handle special cases having to do with O
188 if (this->is_zero())
189 {
190 return other;
191 }
192
193 if (other.is_zero())
194 {
195 return *this;
196 }
197
198 // no need to handle points of order 2,4
199 // (they cannot exist in a prime-order subgroup)
200
201 // handle double case, and then all the rest
202 if (this->operator==(other))
203 {
204 return this->dbl();
205 }
206 else
207 {
208 return this->add(other);
209 }
210}
bn128_G2 add(const bn128_G2 &other) const
Definition bn128_g2.cpp:224
Here is the call graph for this function:

◆ operator-() [1/2]

bn128_G2 libff::bn128_G2::operator- ( ) const

Definition at line 212 of file bn128_g2.cpp.

213{
214 bn128_G2 result(*this);
215 bn::Fp2::neg(result.Y, result.Y);
216 return result;
217}
static void neg(Fp2T &z, const Fp2T &x)
Definition bn.h:400
Here is the call graph for this function:

◆ operator-() [2/2]

bn128_G2 libff::bn128_G2::operator- ( const bn128_G2 & other) const

Definition at line 219 of file bn128_g2.cpp.

220{
221 return (*this) + (-other);
222}

◆ operator==()

bool libff::bn128_G2::operator== ( const bn128_G2 & other) const

Definition at line 146 of file bn128_g2.cpp.

147{
148 if (this->is_zero())
149 {
150 return other.is_zero();
151 }
152
153 if (other.is_zero())
154 {
155 return false;
156 }
157
158 /* now neither is O */
159
160 bn::Fp2 Z1sq, Z2sq, lhs, rhs;
161 bn::Fp2::square(Z1sq, this->Z);
162 bn::Fp2::square(Z2sq, other.Z);
163 bn::Fp2::mul(lhs, Z2sq, this->X);
164 bn::Fp2::mul(rhs, Z1sq, other.X);
165
166 if (lhs != rhs)
167 {
168 return false;
169 }
170
171 bn::Fp2 Z1cubed, Z2cubed;
172 bn::Fp2::mul(Z1cubed, Z1sq, this->Z);
173 bn::Fp2::mul(Z2cubed, Z2sq, other.Z);
174 bn::Fp2::mul(lhs, Z2cubed, this->Y);
175 bn::Fp2::mul(rhs, Z1cubed, other.Y);
176
177 return (lhs == rhs);
178}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ order()

static bigint< scalar_field::num_limbs > libff::bn128_G2::order ( )
inlinestatic

Definition at line 75 of file bn128_g2.hpp.

75{ return scalar_field::field_char(); }
Here is the call graph for this function:

◆ print()

void libff::bn128_G2::print ( ) const

Definition at line 84 of file bn128_g2.cpp.

85{
86 if (this->is_zero())
87 {
88 printf("O\n");
89 }
90 else
91 {
92 bn128_G2 copy(*this);
93 copy.to_affine_coordinates();
94 std::cout << "(" << copy.X.toString(10) << " : " << copy.Y.toString(10) << " : " << copy.Z.toString(10) << ")\n";
95 }
96}
LOGGING_API void printf(Category category, const char *format,...)
Definition Logging.cpp:30
void copy(const path &from, const path &to)
Here is the call graph for this function:

◆ print_coordinates()

void libff::bn128_G2::print_coordinates ( ) const

Definition at line 98 of file bn128_g2.cpp.

99{
100 if (this->is_zero())
101 {
102 printf("O\n");
103 }
104 else
105 {
106 std::cout << "(" << X.toString(10) << " : " << Y.toString(10) << " : " << Z.toString(10) << ")\n";
107 }
108}
std::string toString(int base=10) const
Definition bn.h:521
Here is the call graph for this function:

◆ random_element()

bn128_G2 libff::bn128_G2::random_element ( )
static

Definition at line 381 of file bn128_g2.cpp.

382{
383 return bn128_Fr::random_element().as_bigint() * G2_one;
384}
static Fp_model< n, modulus > random_element()
Here is the call graph for this function:

◆ size_in_bits()

static size_t libff::bn128_G2::size_in_bits ( )
inlinestatic

Definition at line 73 of file bn128_g2.hpp.

73{ return 2*base_field::size_in_bits() + 1; }
static size_t size_in_bits()
Definition fp.hpp:114
Here is the call graph for this function:

◆ to_affine_coordinates()

void libff::bn128_G2::to_affine_coordinates ( )

Definition at line 110 of file bn128_g2.cpp.

111{
112 if (this->is_zero())
113 {
114 X = 0;
115 Y = 1;
116 Z = 0;
117 }
118 else
119 {
120 bn::Fp2 r;
121 r = Z;
122 r.inverse();
124 X *= Z;
125 r *= Z;
126 Y *= r;
127 Z = 1;
128 }
129}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ to_special()

void libff::bn128_G2::to_special ( )

Definition at line 131 of file bn128_g2.cpp.

132{
133 this->to_affine_coordinates();
134}
void to_affine_coordinates()
Definition bn128_g2.cpp:110
Here is the call graph for this function:

◆ zero()

bn128_G2 libff::bn128_G2::zero ( )
static

Definition at line 371 of file bn128_g2.cpp.

372{
373 return G2_zero;
374}

Friends And Related Symbol Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream & out,
const bn128_G2 & g )
friend

Definition at line 386 of file bn128_g2.cpp.

387{
388 bn128_G2 gcopy(g);
389 gcopy.to_affine_coordinates();
390
391 out << (gcopy.is_zero() ? '1' : '0') << OUTPUT_SEPARATOR;
392
393#ifdef NO_PT_COMPRESSION
394 /* no point compression case */
395#ifndef BINARY_OUTPUT
396 out << gcopy.X.a_ << OUTPUT_SEPARATOR << gcopy.X.b_ << OUTPUT_SEPARATOR;
397 out << gcopy.Y.a_ << OUTPUT_SEPARATOR << gcopy.Y.b_;
398#else
399 out.write((char*) &gcopy.X.a_, sizeof(gcopy.X.a_));
400 out.write((char*) &gcopy.X.b_, sizeof(gcopy.X.b_));
401 out.write((char*) &gcopy.Y.a_, sizeof(gcopy.Y.a_));
402 out.write((char*) &gcopy.Y.b_, sizeof(gcopy.Y.b_));
403#endif
404
405#else
406 /* point compression case */
407#ifndef BINARY_OUTPUT
408 out << gcopy.X.a_ << OUTPUT_SEPARATOR << gcopy.X.b_;
409#else
410 out.write((char*) &gcopy.X.a_, sizeof(gcopy.X.a_));
411 out.write((char*) &gcopy.X.b_, sizeof(gcopy.X.b_));
412#endif
413 out << OUTPUT_SEPARATOR << (((unsigned char*)&gcopy.Y.a_)[0] & 1 ? '1' : '0');
414#endif
415
416 return out;
417}
#define OUTPUT_SEPARATOR

◆ operator>>

std::istream & operator>> ( std::istream & in,
bn128_G2 & g )
friend

Definition at line 419 of file bn128_g2.cpp.

420{
421 char is_zero;
422 in.read((char*)&is_zero, 1); // this reads is_zero;
423 is_zero -= '0';
425
426#ifdef NO_PT_COMPRESSION
427 /* no point compression case */
428#ifndef BINARY_OUTPUT
429 in >> g.X.a_;
431 in >> g.X.b_;
433 in >> g.Y.a_;
435 in >> g.Y.b_;
436#else
437 in.read((char*) &g.X.a_, sizeof(g.X.a_));
438 in.read((char*) &g.X.b_, sizeof(g.X.b_));
439 in.read((char*) &g.Y.a_, sizeof(g.Y.a_));
440 in.read((char*) &g.Y.b_, sizeof(g.Y.b_));
441#endif
442
443#else
444 /* point compression case */
445 bn::Fp2 tX;
446#ifndef BINARY_OUTPUT
447 in >> tX.a_;
449 in >> tX.b_;
450#else
451 in.read((char*)&tX.a_, sizeof(tX.a_));
452 in.read((char*)&tX.b_, sizeof(tX.b_));
453#endif
455 unsigned char Y_lsb;
456 in.read((char*)&Y_lsb, 1);
457 Y_lsb -= '0';
458
459 // y = +/- sqrt(x^3 + b)
460 if (!is_zero)
461 {
462 g.X = tX;
463 bn::Fp2 tX2, tY2;
464 bn::Fp2::square(tX2, tX);
465 bn::Fp2::mul(tY2, tX2, tX);
467
468 g.Y = bn128_G2::sqrt(tY2);
469 if ((((unsigned char*)&g.Y.a_)[0] & 1) != Y_lsb)
470 {
471 bn::Fp2::neg(g.Y, g.Y);
472 }
473 }
474#endif
475
476 /* finalize */
477 if (!is_zero)
478 {
479 g.Z = bn::Fp2(bn::Fp(1), bn::Fp(0));
480 }
481 else
482 {
483 g = bn128_G2::zero();
484 }
485
486 return in;
487}
static bn128_G2 zero()
Definition bn128_g2.cpp:371
Definition zm2.h:18
Fp2T< Fp > Fp2
Definition bn.h:2954
void consume_OUTPUT_SEPARATOR(std::istream &in)
Fp a_
Definition bn.h:350
Fp b_
Definition bn.h:350

Member Data Documentation

◆ fixed_base_exp_window_table

std::vector< size_t > libff::bn128_G2::fixed_base_exp_window_table
static

Definition at line 33 of file bn128_g2.hpp.

◆ G2_one

bn128_G2 libff::bn128_G2::G2_one = {}
static

Definition at line 35 of file bn128_g2.hpp.

◆ G2_zero

bn128_G2 libff::bn128_G2::G2_zero = {}
static

Definition at line 34 of file bn128_g2.hpp.

◆ initialized

bool libff::bn128_G2::initialized = false
static

Definition at line 36 of file bn128_g2.hpp.

◆ wnaf_window_table

std::vector< size_t > libff::bn128_G2::wnaf_window_table
static

Definition at line 32 of file bn128_g2.hpp.

◆ X

bn::Fp2 libff::bn128_G2::X

Definition at line 41 of file bn128_g2.hpp.

◆ Y

bn::Fp2 libff::bn128_G2::Y

Definition at line 41 of file bn128_g2.hpp.

◆ Z

bn::Fp2 libff::bn128_G2::Z

Definition at line 41 of file bn128_g2.hpp.


The documentation for this class was generated from the following files: