@@ -156,10 +156,9 @@ alt_bn128_G1 alt_bn128_G1::operator+(const alt_bn128_G1 &other) const
156
156
// no need to handle points of order 2,4
157
157
// (they cannot exist in a prime-order subgroup)
158
158
159
- // check for doubling case
160
-
161
- // using Jacobian coordinates so:
162
- // (X1:Y1:Z1) = (X2:Y2:Z2)
159
+ // using Jacobian coordinates according to
160
+ // https://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-add-2007-bl
161
+ // Note: (X1:Y1:Z1) = (X2:Y2:Z2)
163
162
// iff
164
163
// X1/Z1^2 == X2/Z2^2 and Y1/Z1^3 == Y2/Z2^3
165
164
// iff
@@ -177,12 +176,17 @@ alt_bn128_G1 alt_bn128_G1::operator+(const alt_bn128_G1 &other) const
177
176
alt_bn128_Fq S1 = (this ->Y ) * Z2_cubed; // S1 = Y1 * Z2 * Z2Z2
178
177
alt_bn128_Fq S2 = (other.Y ) * Z1_cubed; // S2 = Y2 * Z1 * Z1Z1
179
178
179
+ // check for doubling case
180
180
if (U1 == U2 && S1 == S2)
181
181
{
182
182
// dbl case; nothing of above can be reused
183
183
return this ->dbl ();
184
184
}
185
185
186
+ #ifdef PROFILE_OP_COUNTS
187
+ this ->add_cnt ++;
188
+ #endif
189
+
186
190
// rest of add case
187
191
alt_bn128_Fq H = U2 - U1; // H = U2-U1
188
192
alt_bn128_Fq S2_minus_S1 = S2-S1;
@@ -211,50 +215,7 @@ alt_bn128_G1 alt_bn128_G1::operator-(const alt_bn128_G1 &other) const
211
215
212
216
alt_bn128_G1 alt_bn128_G1::add (const alt_bn128_G1 &other) const
213
217
{
214
- // handle special cases having to do with O
215
- if (this ->is_zero ())
216
- {
217
- return other;
218
- }
219
-
220
- if (other.is_zero ())
221
- {
222
- return *this ;
223
- }
224
-
225
- // no need to handle points of order 2,4
226
- // (they cannot exist in a prime-order subgroup)
227
-
228
- // handle double case
229
- if (this ->operator ==(other))
230
- {
231
- return this ->dbl ();
232
- }
233
-
234
- #ifdef PROFILE_OP_COUNTS
235
- this ->add_cnt ++;
236
- #endif
237
- // NOTE: does not handle O and pts of order 2,4
238
- // http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-add-2007-bl
239
-
240
- alt_bn128_Fq Z1Z1 = (this ->Z ).squared (); // Z1Z1 = Z1^2
241
- alt_bn128_Fq Z2Z2 = (other.Z ).squared (); // Z2Z2 = Z2^2
242
- alt_bn128_Fq U1 = (this ->X ) * Z2Z2; // U1 = X1 * Z2Z2
243
- alt_bn128_Fq U2 = (other.X ) * Z1Z1; // U2 = X2 * Z1Z1
244
- alt_bn128_Fq S1 = (this ->Y ) * (other.Z ) * Z2Z2; // S1 = Y1 * Z2 * Z2Z2
245
- alt_bn128_Fq S2 = (other.Y ) * (this ->Z ) * Z1Z1; // S2 = Y2 * Z1 * Z1Z1
246
- alt_bn128_Fq H = U2 - U1; // H = U2-U1
247
- alt_bn128_Fq S2_minus_S1 = S2-S1;
248
- alt_bn128_Fq I = (H+H).squared (); // I = (2 * H)^2
249
- alt_bn128_Fq J = H * I; // J = H * I
250
- alt_bn128_Fq r = S2_minus_S1 + S2_minus_S1; // r = 2 * (S2-S1)
251
- alt_bn128_Fq V = U1 * I; // V = U1 * I
252
- alt_bn128_Fq X3 = r.squared () - J - (V+V); // X3 = r^2 - J - 2 * V
253
- alt_bn128_Fq S1_J = S1 * J;
254
- alt_bn128_Fq Y3 = r * (V-X3) - (S1_J+S1_J); // Y3 = r * (V-X3)-2 S1 J
255
- alt_bn128_Fq Z3 = ((this ->Z +other.Z ).squared ()-Z1Z1-Z2Z2) * H; // Z3 = ((Z1+Z2)^2-Z1Z1-Z2Z2) * H
256
-
257
- return alt_bn128_G1 (X3, Y3, Z3);
218
+ return (*this ) + other;
258
219
}
259
220
260
221
alt_bn128_G1 alt_bn128_G1::mixed_add (const alt_bn128_G1 &other) const
@@ -277,15 +238,13 @@ alt_bn128_G1 alt_bn128_G1::mixed_add(const alt_bn128_G1 &other) const
277
238
// no need to handle points of order 2,4
278
239
// (they cannot exist in a prime-order subgroup)
279
240
280
- // check for doubling case
281
-
282
- // using Jacobian coordinates so:
283
- // (X1:Y1:Z1) = (X2:Y2:Z2)
241
+ // using Jacobian coordinates according to
242
+ // http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-madd-2007-bl
243
+ // Note: (X1:Y1:Z1) = (X2:Y2:Z2)
284
244
// iff
285
245
// X1/Z1^2 == X2/Z2^2 and Y1/Z1^3 == Y2/Z2^3
286
246
// iff
287
247
// X1 * Z2^2 == X2 * Z1^2 and Y1 * Z2^3 == Y2 * Z1^3
288
-
289
248
// we know that Z2 = 1
290
249
291
250
const alt_bn128_Fq Z1Z1 = (this ->Z ).squared ();
@@ -298,6 +257,7 @@ alt_bn128_G1 alt_bn128_G1::mixed_add(const alt_bn128_G1 &other) const
298
257
const alt_bn128_Fq &S1 = (this ->Y ); // S1 = Y1 * Z2 * Z2Z2
299
258
const alt_bn128_Fq S2 = (other.Y ) * Z1_cubed; // S2 = Y2 * Z1 * Z1Z1
300
259
260
+ // check for doubling case
301
261
if (U1 == U2 && S1 == S2)
302
262
{
303
263
// dbl case; nothing of above can be reused
@@ -307,11 +267,9 @@ alt_bn128_G1 alt_bn128_G1::mixed_add(const alt_bn128_G1 &other) const
307
267
#ifdef PROFILE_OP_COUNTS
308
268
this ->add_cnt ++;
309
269
#endif
310
-
311
- // NOTE: does not handle O and pts of order 2,4
312
- // http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-madd-2007-bl
270
+
313
271
alt_bn128_Fq H = U2-(this ->X ); // H = U2-X1
314
- alt_bn128_Fq HH = H.squared () ; // HH = H& 2
272
+ alt_bn128_Fq HH = H.squared () ; // HH = H^ 2
315
273
alt_bn128_Fq I = HH+HH; // I = 4*HH
316
274
I = I + I;
317
275
alt_bn128_Fq J = H*I; // J = H*I
@@ -340,9 +298,9 @@ alt_bn128_G1 alt_bn128_G1::dbl() const
340
298
// no need to handle points of order 2,4
341
299
// (they cannot exist in a prime-order subgroup)
342
300
343
- // NOTE: does not handle O and pts of order 2,4
344
- // http ://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l
345
-
301
+ // using Jacobian coordinates according to
302
+ // https ://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l
303
+
346
304
alt_bn128_Fq A = (this ->X ).squared (); // A = X1^2
347
305
alt_bn128_Fq B = (this ->Y ).squared (); // B = Y1^2
348
306
alt_bn128_Fq C = B.squared (); // C = B^2
0 commit comments