@@ -75,9 +75,9 @@ bn128_G1::bn128_G1()
75
75
{
76
76
if (bn128_G1::initialized)
77
77
{
78
- this ->coord [ 0 ] = G1_zero.coord [ 0 ] ;
79
- this ->coord [ 1 ] = G1_zero.coord [ 1 ] ;
80
- this ->coord [ 2 ] = G1_zero.coord [ 2 ] ;
78
+ this ->X = G1_zero.X ;
79
+ this ->Y = G1_zero.Y ;
80
+ this ->Z = G1_zero.Z ;
81
81
}
82
82
}
83
83
@@ -91,7 +91,7 @@ void bn128_G1::print() const
91
91
{
92
92
bn128_G1 copy (*this );
93
93
copy.to_affine_coordinates ();
94
- std::cout << " (" << copy.coord [ 0 ] .toString (10 ) << " : " << copy.coord [ 1 ] .toString (10 ) << " : " << copy.coord [ 2 ] .toString (10 ) << " )\n " ;
94
+ std::cout << " (" << copy.X .toString (10 ) << " : " << copy.Y .toString (10 ) << " : " << copy.Z .toString (10 ) << " )\n " ;
95
95
}
96
96
}
97
97
@@ -103,28 +103,28 @@ void bn128_G1::print_coordinates() const
103
103
}
104
104
else
105
105
{
106
- std::cout << " (" << coord[ 0 ] .toString (10 ) << " : " << coord[ 1 ] .toString (10 ) << " : " << coord[ 2 ] .toString (10 ) << " )\n " ;
106
+ std::cout << " (" << X .toString (10 ) << " : " << Y .toString (10 ) << " : " << Z .toString (10 ) << " )\n " ;
107
107
}
108
108
}
109
109
110
110
void bn128_G1::to_affine_coordinates ()
111
111
{
112
112
if (this ->is_zero ())
113
113
{
114
- coord[ 0 ] = 0 ;
115
- coord[ 1 ] = 1 ;
116
- coord[ 2 ] = 0 ;
114
+ X = 0 ;
115
+ Y = 1 ;
116
+ Z = 0 ;
117
117
}
118
118
else
119
119
{
120
120
bn::Fp r;
121
- r = coord[ 2 ] ;
121
+ r = Z ;
122
122
r.inverse ();
123
- bn::Fp::square (coord[ 2 ] , r);
124
- coord[ 0 ] *= coord[ 2 ] ;
125
- r *= coord[ 2 ] ;
126
- coord[ 1 ] *= r;
127
- coord[ 2 ] = 1 ;
123
+ bn::Fp::square (Z , r);
124
+ X *= Z ;
125
+ r *= Z ;
126
+ Y *= r;
127
+ Z = 1 ;
128
128
}
129
129
}
130
130
@@ -135,12 +135,12 @@ void bn128_G1::to_special()
135
135
136
136
bool bn128_G1::is_special () const
137
137
{
138
- return (this ->is_zero () || this ->coord [ 2 ] == 1 );
138
+ return (this ->is_zero () || this ->Z == 1 );
139
139
}
140
140
141
141
bool bn128_G1::is_zero () const
142
142
{
143
- return coord[ 2 ] .isZero ();
143
+ return Z .isZero ();
144
144
}
145
145
146
146
bool bn128_G1::operator ==(const bn128_G1 &other) const
@@ -158,21 +158,21 @@ bool bn128_G1::operator==(const bn128_G1 &other) const
158
158
/* now neither is O */
159
159
160
160
bn::Fp Z1sq, Z2sq, lhs, rhs;
161
- bn::Fp::square (Z1sq, this ->coord [ 2 ] );
162
- bn::Fp::square (Z2sq, other.coord [ 2 ] );
163
- bn::Fp::mul (lhs, Z2sq, this ->coord [ 0 ] );
164
- bn::Fp::mul (rhs, Z1sq, other.coord [ 0 ] );
161
+ bn::Fp::square (Z1sq, this ->Z );
162
+ bn::Fp::square (Z2sq, other.Z );
163
+ bn::Fp::mul (lhs, Z2sq, this ->X );
164
+ bn::Fp::mul (rhs, Z1sq, other.X );
165
165
166
166
if (lhs != rhs)
167
167
{
168
168
return false ;
169
169
}
170
170
171
171
bn::Fp Z1cubed, Z2cubed;
172
- bn::Fp::mul (Z1cubed, Z1sq, this ->coord [ 2 ] );
173
- bn::Fp::mul (Z2cubed, Z2sq, other.coord [ 2 ] );
174
- bn::Fp::mul (lhs, Z2cubed, this ->coord [ 1 ] );
175
- bn::Fp::mul (rhs, Z1cubed, other.coord [ 1 ] );
172
+ bn::Fp::mul (Z1cubed, Z1sq, this ->Z );
173
+ bn::Fp::mul (Z2cubed, Z2sq, other.Z );
174
+ bn::Fp::mul (lhs, Z2cubed, this ->Y );
175
+ bn::Fp::mul (rhs, Z1cubed, other.Y );
176
176
177
177
return (lhs == rhs);
178
178
}
@@ -212,7 +212,7 @@ bn128_G1 bn128_G1::operator+(const bn128_G1 &other) const
212
212
bn128_G1 bn128_G1::operator -() const
213
213
{
214
214
bn128_G1 result (*this );
215
- bn::Fp::neg (result.coord [ 1 ] , result.coord [ 1 ] );
215
+ bn::Fp::neg (result.Y , result.Y );
216
216
return result;
217
217
}
218
218
@@ -227,8 +227,12 @@ bn128_G1 bn128_G1::add(const bn128_G1 &other) const
227
227
this ->add_cnt ++;
228
228
#endif
229
229
230
- bn128_G1 result;
231
- bn::ecop::ECAdd (result.coord , this ->coord , other.coord );
230
+ bn::Fp 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_G1 result (result_coord);
232
236
return result;
233
237
}
234
238
@@ -263,16 +267,16 @@ bn128_G1 bn128_G1::mixed_add(const bn128_G1 &other) const
263
267
// we know that Z2 = 1
264
268
265
269
bn::Fp Z1Z1;
266
- bn::Fp::square (Z1Z1, this ->coord [ 2 ] );
267
- const bn::Fp &U1 = this ->coord [ 0 ] ;
270
+ bn::Fp::square (Z1Z1, this ->Z );
271
+ const bn::Fp &U1 = this ->X ;
268
272
bn::Fp U2;
269
- bn::Fp::mul (U2, other.coord [ 0 ] , Z1Z1);
273
+ bn::Fp::mul (U2, other.X , Z1Z1);
270
274
bn::Fp Z1_cubed;
271
- bn::Fp::mul (Z1_cubed, this ->coord [ 2 ] , Z1Z1);
275
+ bn::Fp::mul (Z1_cubed, this ->Z , Z1Z1);
272
276
273
- const bn::Fp &S1 = this ->coord [ 1 ] ;
277
+ const bn::Fp &S1 = this ->Y ;
274
278
bn::Fp S2;
275
- bn::Fp::mul (S2, other.coord [ 1 ] , Z1_cubed); // S2 = Y2*Z1*Z1Z1
279
+ bn::Fp::mul (S2, other.Y , Z1_cubed); // S2 = Y2*Z1*Z1Z1
276
280
277
281
if (U1 == U2 && S1 == S2)
278
282
{
@@ -287,7 +291,7 @@ bn128_G1 bn128_G1::mixed_add(const bn128_G1 &other) const
287
291
bn128_G1 result;
288
292
bn::Fp H, HH, I, J, r, V, tmp;
289
293
// H = U2-X1
290
- bn::Fp::sub (H, U2, this ->coord [ 0 ] );
294
+ bn::Fp::sub (H, U2, this ->X );
291
295
// HH = H^2
292
296
bn::Fp::square (HH, H);
293
297
// I = 4*HH
@@ -296,26 +300,26 @@ bn128_G1 bn128_G1::mixed_add(const bn128_G1 &other) const
296
300
// J = H*I
297
301
bn::Fp::mul (J, H, I);
298
302
// r = 2*(S2-Y1)
299
- bn::Fp::sub (tmp, S2, this ->coord [ 1 ] );
303
+ bn::Fp::sub (tmp, S2, this ->Y );
300
304
bn::Fp::add (r, tmp, tmp);
301
305
// V = X1*I
302
- bn::Fp::mul (V, this ->coord [ 0 ] , I);
306
+ bn::Fp::mul (V, this ->X , I);
303
307
// X3 = r^2-J-2*V
304
- bn::Fp::square (result.coord [ 0 ] , r);
305
- bn::Fp::sub (result.coord [ 0 ] , result.coord [ 0 ] , J);
306
- bn::Fp::sub (result.coord [ 0 ] , result.coord [ 0 ] , V);
307
- bn::Fp::sub (result.coord [ 0 ] , result.coord [ 0 ] , V);
308
+ bn::Fp::square (result.X , r);
309
+ bn::Fp::sub (result.X , result.X , J);
310
+ bn::Fp::sub (result.X , result.X , V);
311
+ bn::Fp::sub (result.X , result.X , V);
308
312
// Y3 = r*(V-X3)-2*Y1*J
309
- bn::Fp::sub (tmp, V, result.coord [ 0 ] );
310
- bn::Fp::mul (result.coord [ 1 ] , r, tmp);
311
- bn::Fp::mul (tmp, this ->coord [ 1 ] , J);
312
- bn::Fp::sub (result.coord [ 1 ] , result.coord [ 1 ] , tmp);
313
- bn::Fp::sub (result.coord [ 1 ] , result.coord [ 1 ] , tmp);
313
+ bn::Fp::sub (tmp, V, result.X );
314
+ bn::Fp::mul (result.Y , r, tmp);
315
+ bn::Fp::mul (tmp, this ->Y , J);
316
+ bn::Fp::sub (result.Y , result.Y , tmp);
317
+ bn::Fp::sub (result.Y , result.Y , tmp);
314
318
// Z3 = (Z1+H)^2-Z1Z1-HH
315
- bn::Fp::add (tmp, this ->coord [ 2 ] , H);
316
- bn::Fp::square (result.coord [ 2 ] , tmp);
317
- bn::Fp::sub (result.coord [ 2 ] , result.coord [ 2 ] , Z1Z1);
318
- bn::Fp::sub (result.coord [ 2 ] , result.coord [ 2 ] , HH);
319
+ bn::Fp::add (tmp, this ->Z , H);
320
+ bn::Fp::square (result.Z , tmp);
321
+ bn::Fp::sub (result.Z , result.Z , Z1Z1);
322
+ bn::Fp::sub (result.Z , result.Z , HH);
319
323
return result;
320
324
}
321
325
@@ -325,8 +329,11 @@ bn128_G1 bn128_G1::dbl() const
325
329
this ->dbl_cnt ++;
326
330
#endif
327
331
328
- bn128_G1 result;
329
- bn::ecop::ECDouble (result.coord , this ->coord );
332
+ bn::Fp this_coord[3 ], result_coord[3 ];
333
+ this ->fill_coord (this_coord);
334
+ bn::ecop::ECDouble (result_coord, this_coord);
335
+
336
+ bn128_G1 result (result_coord);
330
337
return result;
331
338
}
332
339
@@ -355,20 +362,20 @@ std::ostream& operator<<(std::ostream &out, const bn128_G1 &g)
355
362
#ifdef NO_PT_COMPRESSION
356
363
/* no point compression case */
357
364
#ifndef BINARY_OUTPUT
358
- out << gcopy.coord [ 0 ] << OUTPUT_SEPARATOR << gcopy.coord [ 1 ] ;
365
+ out << gcopy.X << OUTPUT_SEPARATOR << gcopy.Y ;
359
366
#else
360
- out.write ((char *) &gcopy.coord [ 0 ] , sizeof (gcopy.coord [ 0 ] ));
361
- out.write ((char *) &gcopy.coord [ 1 ] , sizeof (gcopy.coord [ 1 ] ));
367
+ out.write ((char *) &gcopy.X , sizeof (gcopy.X ));
368
+ out.write ((char *) &gcopy.Y , sizeof (gcopy.Y ));
362
369
#endif
363
370
364
371
#else
365
372
/* point compression case */
366
373
#ifndef BINARY_OUTPUT
367
- out << gcopy.coord [ 0 ] ;
374
+ out << gcopy.X ;
368
375
#else
369
- out.write ((char *) &gcopy.coord [ 0 ] , sizeof (gcopy.coord [ 0 ] ));
376
+ out.write ((char *) &gcopy.X , sizeof (gcopy.X ));
370
377
#endif
371
- out << OUTPUT_SEPARATOR << (((unsigned char *)&gcopy.coord [ 1 ] )[0 ] & 1 ? ' 1' : ' 0' );
378
+ out << OUTPUT_SEPARATOR << (((unsigned char *)&gcopy.Y )[0 ] & 1 ? ' 1' : ' 0' );
372
379
#endif
373
380
374
381
return out;
@@ -392,13 +399,13 @@ bool bn128_G1::is_well_formed() const
392
399
y^2 = x^3 + b z^6
393
400
*/
394
401
bn::Fp X2, Y2, Z2;
395
- bn::Fp::square (X2, this ->coord [ 0 ] );
396
- bn::Fp::square (Y2, this ->coord [ 1 ] );
397
- bn::Fp::square (Z2, this ->coord [ 2 ] );
402
+ bn::Fp::square (X2, this ->X );
403
+ bn::Fp::square (Y2, this ->Y );
404
+ bn::Fp::square (Z2, this ->Z );
398
405
399
406
bn::Fp X3, Z3, Z6;
400
- bn::Fp::mul (X3, X2, this ->coord [ 0 ] );
401
- bn::Fp::mul (Z3, Z2, this ->coord [ 2 ] );
407
+ bn::Fp::mul (X3, X2, this ->X );
408
+ bn::Fp::mul (Z3, Z2, this ->Z );
402
409
bn::Fp::square (Z6, Z3);
403
410
404
411
return (Y2 == X3 + bn128_coeff_b * Z6);
@@ -415,12 +422,12 @@ std::istream& operator>>(std::istream &in, bn128_G1 &g)
415
422
#ifdef NO_PT_COMPRESSION
416
423
/* no point compression case */
417
424
#ifndef BINARY_OUTPUT
418
- in >> g.coord [ 0 ] ;
425
+ in >> g.X ;
419
426
consume_OUTPUT_SEPARATOR (in);
420
- in >> g.coord [ 1 ] ;
427
+ in >> g.Y ;
421
428
#else
422
- in.read ((char *) &g.coord [ 0 ] , sizeof (g.coord [ 0 ] ));
423
- in.read ((char *) &g.coord [ 1 ] , sizeof (g.coord [ 1 ] ));
429
+ in.read ((char *) &g.X , sizeof (g.X ));
430
+ in.read ((char *) &g.Y , sizeof (g.Y ));
424
431
#endif
425
432
426
433
#else
@@ -439,24 +446,24 @@ std::istream& operator>>(std::istream &in, bn128_G1 &g)
439
446
// y = +/- sqrt(x^3 + b)
440
447
if (!is_zero)
441
448
{
442
- g.coord [ 0 ] = tX;
449
+ g.X = tX;
443
450
bn::Fp tX2, tY2;
444
451
bn::Fp::square (tX2, tX);
445
452
bn::Fp::mul (tY2, tX2, tX);
446
453
bn::Fp::add (tY2, tY2, bn128_coeff_b);
447
454
448
- g.coord [ 1 ] = bn128_G1::sqrt (tY2);
449
- if ((((unsigned char *)&g.coord [ 1 ] )[0 ] & 1 ) != Y_lsb)
455
+ g.Y = bn128_G1::sqrt (tY2);
456
+ if ((((unsigned char *)&g.Y )[0 ] & 1 ) != Y_lsb)
450
457
{
451
- bn::Fp::neg (g.coord [ 1 ] , g.coord [ 1 ] );
458
+ bn::Fp::neg (g.Y , g.Y );
452
459
}
453
460
}
454
461
#endif
455
462
456
463
/* finalize */
457
464
if (!is_zero)
458
465
{
459
- g.coord [ 2 ] = bn::Fp (1 );
466
+ g.Z = bn::Fp (1 );
460
467
}
461
468
else
462
469
{
@@ -502,7 +509,7 @@ void bn128_G1::batch_to_special_all_non_zeros(std::vector<bn128_G1> &vec)
502
509
503
510
for (auto &el: vec)
504
511
{
505
- Z_vec.emplace_back (el.coord [ 2 ] );
512
+ Z_vec.emplace_back (el.Z );
506
513
}
507
514
bn_batch_invert<bn::Fp>(Z_vec);
508
515
@@ -514,9 +521,9 @@ void bn128_G1::batch_to_special_all_non_zeros(std::vector<bn128_G1> &vec)
514
521
bn::Fp::square (Z2, Z_vec[i]);
515
522
bn::Fp::mul (Z3, Z2, Z_vec[i]);
516
523
517
- bn::Fp::mul (vec[i].coord [ 0 ] , vec[i].coord [ 0 ] , Z2);
518
- bn::Fp::mul (vec[i].coord [ 1 ] , vec[i].coord [ 1 ] , Z3);
519
- vec[i].coord [ 2 ] = one;
524
+ bn::Fp::mul (vec[i].X , vec[i].X , Z2);
525
+ bn::Fp::mul (vec[i].Y , vec[i].Y , Z3);
526
+ vec[i].Z = one;
520
527
}
521
528
}
522
529
0 commit comments