]> Git Repo - secp256k1.git/blame - src/group_impl.h
Fix header guards using reserved identifiers
[secp256k1.git] / src / group_impl.h
CommitLineData
71712b27
GM
1/**********************************************************************
2 * Copyright (c) 2013, 2014 Pieter Wuille *
3 * Distributed under the MIT software license, see the accompanying *
4 * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
5 **********************************************************************/
0a433ea2 6
abe2d3e8
DR
7#ifndef SECP256K1_GROUP_IMPL_H
8#define SECP256K1_GROUP_IMPL_H
7a4b7691 9
11ab5622
PW
10#include "num.h"
11#include "field.h"
12#include "group.h"
607884fc 13
83836a95
AP
14/* These points can be generated in sage as follows:
15 *
16 * 0. Setup a worksheet with the following parameters.
17 * b = 4 # whatever CURVE_B will be set to
18 * F = FiniteField (0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F)
19 * C = EllipticCurve ([F (0), F (b)])
20 *
21 * 1. Determine all the small orders available to you. (If there are
22 * no satisfactory ones, go back and change b.)
23 * print C.order().factor(limit=1000)
24 *
25 * 2. Choose an order as one of the prime factors listed in the above step.
26 * (You can also multiply some to get a composite order, though the
27 * tests will crash trying to invert scalars during signing.) We take a
28 * random point and scale it to drop its order to the desired value.
29 * There is some probability this won't work; just try again.
30 * order = 199
31 * P = C.random_point()
32 * P = (int(P.order()) / int(order)) * P
33 * assert(P.order() == order)
34 *
35 * 3. Print the values. You'll need to use a vim macro or something to
36 * split the hex output into 4-byte chunks.
37 * print "%x %x" % P.xy()
38 */
20b8877b
AP
39#if defined(EXHAUSTIVE_TEST_ORDER)
40# if EXHAUSTIVE_TEST_ORDER == 199
41const secp256k1_ge secp256k1_ge_const_g = SECP256K1_GE_CONST(
42 0xFA7CC9A7, 0x0737F2DB, 0xA749DD39, 0x2B4FB069,
43 0x3B017A7D, 0xA808C2F1, 0xFB12940C, 0x9EA66C18,
44 0x78AC123A, 0x5ED8AEF3, 0x8732BC91, 0x1F3A2868,
45 0x48DF246C, 0x808DAE72, 0xCFE52572, 0x7F0501ED
46);
83836a95
AP
47
48const int CURVE_B = 4;
49# elif EXHAUSTIVE_TEST_ORDER == 13
50const secp256k1_ge secp256k1_ge_const_g = SECP256K1_GE_CONST(
51 0xedc60018, 0xa51a786b, 0x2ea91f4d, 0x4c9416c0,
52 0x9de54c3b, 0xa1316554, 0x6cf4345c, 0x7277ef15,
53 0x54cb1b6b, 0xdc8c1273, 0x087844ea, 0x43f4603e,
54 0x0eaf9a43, 0xf6effe55, 0x939f806d, 0x37adf8ac
55);
56const int CURVE_B = 2;
20b8877b
AP
57# else
58# error No known generator for the specified exhaustive test group order.
59# endif
60#else
6efd6e77
GM
61/** Generator for secp256k1, value 'g' defined in
62 * "Standards for Efficient Cryptography" (SEC2) 2.7.1.
63 */
dd891e0e 64static const secp256k1_ge secp256k1_ge_const_g = SECP256K1_GE_CONST(
443cd4b8
PW
65 0x79BE667EUL, 0xF9DCBBACUL, 0x55A06295UL, 0xCE870B07UL,
66 0x029BFCDBUL, 0x2DCE28D9UL, 0x59F2815BUL, 0x16F81798UL,
67 0x483ADA77UL, 0x26A3C465UL, 0x5DA4FBFCUL, 0x0E1108A8UL,
68 0xFD17B448UL, 0xA6855419UL, 0x9C47D08FUL, 0xFB10D4B8UL
69);
83836a95
AP
70
71const int CURVE_B = 7;
20b8877b 72#endif
4732d260 73
dd891e0e 74static void secp256k1_ge_set_gej_zinv(secp256k1_ge *r, const secp256k1_gej *a, const secp256k1_fe *zi) {
7d893f49 75 secp256k1_fe zi2;
dd891e0e 76 secp256k1_fe zi3;
4f9791ab
PD
77 secp256k1_fe_sqr(&zi2, zi);
78 secp256k1_fe_mul(&zi3, &zi2, zi);
79 secp256k1_fe_mul(&r->x, &a->x, &zi2);
80 secp256k1_fe_mul(&r->y, &a->y, &zi3);
81 r->infinity = a->infinity;
82}
83
dd891e0e 84static void secp256k1_ge_set_xy(secp256k1_ge *r, const secp256k1_fe *x, const secp256k1_fe *y) {
f11ff5be
PW
85 r->infinity = 0;
86 r->x = *x;
87 r->y = *y;
607884fc
PW
88}
89
dd891e0e 90static int secp256k1_ge_is_infinity(const secp256k1_ge *a) {
f11ff5be 91 return a->infinity;
607884fc
PW
92}
93
dd891e0e 94static void secp256k1_ge_neg(secp256k1_ge *r, const secp256k1_ge *a) {
39bd94d8 95 *r = *a;
0295f0a3 96 secp256k1_fe_normalize_weak(&r->y);
39bd94d8
PW
97 secp256k1_fe_negate(&r->y, &r->y, 1);
98}
99
dd891e0e
PW
100static void secp256k1_ge_set_gej(secp256k1_ge *r, secp256k1_gej *a) {
101 secp256k1_fe z2, z3;
da55986f
PW
102 r->infinity = a->infinity;
103 secp256k1_fe_inv(&a->z, &a->z);
f735446c
GM
104 secp256k1_fe_sqr(&z2, &a->z);
105 secp256k1_fe_mul(&z3, &a->z, &z2);
da55986f
PW
106 secp256k1_fe_mul(&a->x, &a->x, &z2);
107 secp256k1_fe_mul(&a->y, &a->y, &z3);
108 secp256k1_fe_set_int(&a->z, 1);
109 r->x = a->x;
110 r->y = a->y;
111}
112
dd891e0e
PW
113static void secp256k1_ge_set_gej_var(secp256k1_ge *r, secp256k1_gej *a) {
114 secp256k1_fe z2, z3;
1136bedb
PW
115 r->infinity = a->infinity;
116 if (a->infinity) {
117 return;
118 }
f11ff5be 119 secp256k1_fe_inv_var(&a->z, &a->z);
f735446c
GM
120 secp256k1_fe_sqr(&z2, &a->z);
121 secp256k1_fe_mul(&z3, &a->z, &z2);
f11ff5be
PW
122 secp256k1_fe_mul(&a->x, &a->x, &z2);
123 secp256k1_fe_mul(&a->y, &a->y, &z3);
124 secp256k1_fe_set_int(&a->z, 1);
f11ff5be
PW
125 r->x = a->x;
126 r->y = a->y;
607884fc
PW
127}
128
541b7839 129static void secp256k1_ge_set_all_gej_var(secp256k1_ge *r, const secp256k1_gej *a, size_t len, const secp256k1_callback *cb) {
dd891e0e
PW
130 secp256k1_fe *az;
131 secp256k1_fe *azi;
f735446c 132 size_t i;
65a14abb 133 size_t count = 0;
dd891e0e 134 az = (secp256k1_fe *)checked_malloc(cb, sizeof(secp256k1_fe) * len);
f735446c 135 for (i = 0; i < len; i++) {
f16be77f
PD
136 if (!a[i].infinity) {
137 az[count++] = a[i].z;
138 }
139 }
140
dd891e0e 141 azi = (secp256k1_fe *)checked_malloc(cb, sizeof(secp256k1_fe) * count);
7d893f49 142 secp256k1_fe_inv_all_var(azi, az, count);
f461b769 143 free(az);
f16be77f
PD
144
145 count = 0;
f735446c 146 for (i = 0; i < len; i++) {
f16be77f
PD
147 r[i].infinity = a[i].infinity;
148 if (!a[i].infinity) {
4f9791ab 149 secp256k1_ge_set_gej_zinv(&r[i], &a[i], &azi[count++]);
f16be77f
PD
150 }
151 }
f461b769 152 free(azi);
f16be77f
PD
153}
154
353c1bf0 155static void secp256k1_ge_set_table_gej_var(secp256k1_ge *r, const secp256k1_gej *a, const secp256k1_fe *zr, size_t len) {
4f9791ab 156 size_t i = len - 1;
dd891e0e 157 secp256k1_fe zi;
4f9791ab 158
912f203f
GM
159 if (len > 0) {
160 /* Compute the inverse of the last z coordinate, and use it to compute the last affine output. */
161 secp256k1_fe_inv(&zi, &a[i].z);
4f9791ab 162 secp256k1_ge_set_gej_zinv(&r[i], &a[i], &zi);
912f203f
GM
163
164 /* Work out way backwards, using the z-ratios to scale the x/y values. */
165 while (i > 0) {
166 secp256k1_fe_mul(&zi, &zi, &zr[i]);
167 i--;
168 secp256k1_ge_set_gej_zinv(&r[i], &a[i], &zi);
169 }
4f9791ab
PD
170 }
171}
172
dd891e0e 173static void secp256k1_ge_globalz_set_table_gej(size_t len, secp256k1_ge *r, secp256k1_fe *globalz, const secp256k1_gej *a, const secp256k1_fe *zr) {
4f9791ab 174 size_t i = len - 1;
dd891e0e 175 secp256k1_fe zs;
4f9791ab 176
912f203f
GM
177 if (len > 0) {
178 /* The z of the final point gives us the "global Z" for the table. */
179 r[i].x = a[i].x;
180 r[i].y = a[i].y;
181 *globalz = a[i].z;
182 r[i].infinity = 0;
183 zs = zr[i];
184
185 /* Work our way backwards, using the z-ratios to scale the x/y values. */
186 while (i > 0) {
187 if (i != len - 1) {
188 secp256k1_fe_mul(&zs, &zs, &zr[i]);
189 }
190 i--;
191 secp256k1_ge_set_gej_zinv(&r[i], &a[i], &zs);
4f9791ab 192 }
4f9791ab
PD
193 }
194}
195
dd891e0e 196static void secp256k1_gej_set_infinity(secp256k1_gej *r) {
f11ff5be 197 r->infinity = 1;
20b8877b
AP
198 secp256k1_fe_clear(&r->x);
199 secp256k1_fe_clear(&r->y);
200 secp256k1_fe_clear(&r->z);
201}
202
dd891e0e 203static void secp256k1_gej_clear(secp256k1_gej *r) {
2f6c8019
GM
204 r->infinity = 0;
205 secp256k1_fe_clear(&r->x);
206 secp256k1_fe_clear(&r->y);
207 secp256k1_fe_clear(&r->z);
208}
209
dd891e0e 210static void secp256k1_ge_clear(secp256k1_ge *r) {
2f6c8019
GM
211 r->infinity = 0;
212 secp256k1_fe_clear(&r->x);
213 secp256k1_fe_clear(&r->y);
214}
215
926836ad 216static int secp256k1_ge_set_xquad(secp256k1_ge *r, const secp256k1_fe *x) {
dd891e0e 217 secp256k1_fe x2, x3, c;
f11ff5be 218 r->x = *x;
f735446c
GM
219 secp256k1_fe_sqr(&x2, x);
220 secp256k1_fe_mul(&x3, x, &x2);
eb0be8ee 221 r->infinity = 0;
83836a95 222 secp256k1_fe_set_int(&c, CURVE_B);
f11ff5be 223 secp256k1_fe_add(&c, &x3);
926836ad 224 return secp256k1_fe_sqrt(&r->y, &c);
64666251
PW
225}
226
227static int secp256k1_ge_set_xo_var(secp256k1_ge *r, const secp256k1_fe *x, int odd) {
926836ad 228 if (!secp256k1_ge_set_xquad(r, x)) {
09ca4f32 229 return 0;
26320197 230 }
39bd94d8 231 secp256k1_fe_normalize_var(&r->y);
26320197 232 if (secp256k1_fe_is_odd(&r->y) != odd) {
f11ff5be 233 secp256k1_fe_negate(&r->y, &r->y, 1);
26320197 234 }
09ca4f32 235 return 1;
64666251 236
910d0de4 237}
607884fc 238
dd891e0e 239static void secp256k1_gej_set_ge(secp256k1_gej *r, const secp256k1_ge *a) {
f11ff5be
PW
240 r->infinity = a->infinity;
241 r->x = a->x;
242 r->y = a->y;
243 secp256k1_fe_set_int(&r->z, 1);
910d0de4 244}
607884fc 245
dd891e0e
PW
246static int secp256k1_gej_eq_x_var(const secp256k1_fe *x, const secp256k1_gej *a) {
247 secp256k1_fe r, r2;
ce7eb6fb 248 VERIFY_CHECK(!a->infinity);
f735446c
GM
249 secp256k1_fe_sqr(&r, &a->z); secp256k1_fe_mul(&r, &r, x);
250 r2 = a->x; secp256k1_fe_normalize_weak(&r2);
d7174edf 251 return secp256k1_fe_equal_var(&r, &r2);
910d0de4 252}
607884fc 253
dd891e0e 254static void secp256k1_gej_neg(secp256k1_gej *r, const secp256k1_gej *a) {
f11ff5be
PW
255 r->infinity = a->infinity;
256 r->x = a->x;
257 r->y = a->y;
258 r->z = a->z;
0295f0a3 259 secp256k1_fe_normalize_weak(&r->y);
f11ff5be 260 secp256k1_fe_negate(&r->y, &r->y, 1);
607884fc
PW
261}
262
dd891e0e 263static int secp256k1_gej_is_infinity(const secp256k1_gej *a) {
f11ff5be 264 return a->infinity;
0a07e62f
PW
265}
266
dd891e0e
PW
267static int secp256k1_gej_is_valid_var(const secp256k1_gej *a) {
268 secp256k1_fe y2, x3, z2, z6;
26320197 269 if (a->infinity) {
eb0be8ee 270 return 0;
26320197 271 }
71712b27
GM
272 /** y^2 = x^3 + 7
273 * (Y/Z^3)^2 = (X/Z^2)^3 + 7
274 * Y^2 / Z^6 = X^3 / Z^6 + 7
275 * Y^2 = X^3 + 7*Z^6
276 */
f735446c
GM
277 secp256k1_fe_sqr(&y2, &a->y);
278 secp256k1_fe_sqr(&x3, &a->x); secp256k1_fe_mul(&x3, &x3, &a->x);
279 secp256k1_fe_sqr(&z2, &a->z);
280 secp256k1_fe_sqr(&z6, &z2); secp256k1_fe_mul(&z6, &z6, &z2);
83836a95 281 secp256k1_fe_mul_int(&z6, CURVE_B);
910d0de4 282 secp256k1_fe_add(&x3, &z6);
d7174edf
PW
283 secp256k1_fe_normalize_weak(&x3);
284 return secp256k1_fe_equal_var(&y2, &x3);
607884fc
PW
285}
286
dd891e0e
PW
287static int secp256k1_ge_is_valid_var(const secp256k1_ge *a) {
288 secp256k1_fe y2, x3, c;
26320197 289 if (a->infinity) {
764332d0 290 return 0;
26320197 291 }
71712b27 292 /* y^2 = x^3 + 7 */
f735446c
GM
293 secp256k1_fe_sqr(&y2, &a->y);
294 secp256k1_fe_sqr(&x3, &a->x); secp256k1_fe_mul(&x3, &x3, &a->x);
83836a95 295 secp256k1_fe_set_int(&c, CURVE_B);
764332d0 296 secp256k1_fe_add(&x3, &c);
d7174edf
PW
297 secp256k1_fe_normalize_weak(&x3);
298 return secp256k1_fe_equal_var(&y2, &x3);
764332d0
PW
299}
300
dd891e0e 301static void secp256k1_gej_double_var(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr) {
8ec49d8a
AP
302 /* Operations: 3 mul, 4 sqr, 0 normalize, 12 mul_int/add/negate.
303 *
304 * Note that there is an implementation described at
305 * https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l
306 * which trades a multiply for a square, but in practice this is actually slower,
307 * mainly because it requires more normalizations.
308 */
dd891e0e 309 secp256k1_fe t1,t2,t3,t4;
3627437d
GM
310 /** For secp256k1, 2Q is infinity if and only if Q is infinity. This is because if 2Q = infinity,
311 * Q must equal -Q, or that Q.y == -(Q.y), or Q.y is 0. For a point on y^2 = x^3 + 7 to have
312 * y=0, x^3 must be -7 mod p. However, -7 has no cube root mod p.
7d893f49 313 *
e72e93ad
AP
314 * Having said this, if this function receives a point on a sextic twist, e.g. by
315 * a fault attack, it is possible for y to be 0. This happens for y^2 = x^3 + 6,
316 * since -6 does have a cube root mod p. For this point, this function will not set
317 * the infinity flag even though the point doubles to infinity, and the result
318 * point will be gibberish (z = 0 but infinity = 0).
3627437d 319 */
f7dc1c65
PW
320 r->infinity = a->infinity;
321 if (r->infinity) {
2b199de8 322 if (rzr != NULL) {
4f9791ab
PD
323 secp256k1_fe_set_int(rzr, 1);
324 }
607884fc
PW
325 return;
326 }
327
2b199de8 328 if (rzr != NULL) {
4f9791ab
PD
329 *rzr = a->y;
330 secp256k1_fe_normalize_weak(rzr);
331 secp256k1_fe_mul_int(rzr, 2);
332 }
333
be82e92f 334 secp256k1_fe_mul(&r->z, &a->z, &a->y);
71712b27 335 secp256k1_fe_mul_int(&r->z, 2); /* Z' = 2*Y*Z (2) */
f11ff5be 336 secp256k1_fe_sqr(&t1, &a->x);
71712b27
GM
337 secp256k1_fe_mul_int(&t1, 3); /* T1 = 3*X^2 (3) */
338 secp256k1_fe_sqr(&t2, &t1); /* T2 = 9*X^4 (1) */
f7dc1c65 339 secp256k1_fe_sqr(&t3, &a->y);
71712b27 340 secp256k1_fe_mul_int(&t3, 2); /* T3 = 2*Y^2 (2) */
910d0de4 341 secp256k1_fe_sqr(&t4, &t3);
71712b27 342 secp256k1_fe_mul_int(&t4, 2); /* T4 = 8*Y^4 (2) */
be82e92f 343 secp256k1_fe_mul(&t3, &t3, &a->x); /* T3 = 2*X*Y^2 (1) */
f11ff5be 344 r->x = t3;
71712b27
GM
345 secp256k1_fe_mul_int(&r->x, 4); /* X' = 8*X*Y^2 (4) */
346 secp256k1_fe_negate(&r->x, &r->x, 4); /* X' = -8*X*Y^2 (5) */
347 secp256k1_fe_add(&r->x, &t2); /* X' = 9*X^4 - 8*X*Y^2 (6) */
348 secp256k1_fe_negate(&t2, &t2, 1); /* T2 = -9*X^4 (2) */
349 secp256k1_fe_mul_int(&t3, 6); /* T3 = 12*X*Y^2 (6) */
350 secp256k1_fe_add(&t3, &t2); /* T3 = 12*X*Y^2 - 9*X^4 (8) */
351 secp256k1_fe_mul(&r->y, &t1, &t3); /* Y' = 36*X^3*Y^2 - 27*X^6 (1) */
352 secp256k1_fe_negate(&t2, &t4, 2); /* T2 = -8*Y^4 (3) */
353 secp256k1_fe_add(&r->y, &t2); /* Y' = 36*X^3*Y^2 - 27*X^6 - 8*Y^4 (4) */
607884fc
PW
354}
355
dd891e0e 356static SECP256K1_INLINE void secp256k1_gej_double_nonzero(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr) {
44015000
AP
357 VERIFY_CHECK(!secp256k1_gej_is_infinity(a));
358 secp256k1_gej_double_var(r, a, rzr);
359}
360
dd891e0e 361static void secp256k1_gej_add_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_gej *b, secp256k1_fe *rzr) {
d61e8995 362 /* Operations: 12 mul, 4 sqr, 2 normalize, 12 mul_int/add/negate */
dd891e0e 363 secp256k1_fe z22, z12, u1, u2, s1, s2, h, i, i2, h2, h3, t;
4f9791ab 364
f11ff5be 365 if (a->infinity) {
4f9791ab 366 VERIFY_CHECK(rzr == NULL);
f11ff5be 367 *r = *b;
607884fc
PW
368 return;
369 }
4f9791ab 370
f11ff5be 371 if (b->infinity) {
2b199de8 372 if (rzr != NULL) {
4f9791ab
PD
373 secp256k1_fe_set_int(rzr, 1);
374 }
f11ff5be 375 *r = *a;
607884fc
PW
376 return;
377 }
4f9791ab 378
eb0be8ee 379 r->infinity = 0;
f735446c
GM
380 secp256k1_fe_sqr(&z22, &b->z);
381 secp256k1_fe_sqr(&z12, &a->z);
382 secp256k1_fe_mul(&u1, &a->x, &z22);
383 secp256k1_fe_mul(&u2, &b->x, &z12);
384 secp256k1_fe_mul(&s1, &a->y, &z22); secp256k1_fe_mul(&s1, &s1, &b->z);
385 secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &a->z);
386 secp256k1_fe_negate(&h, &u1, 1); secp256k1_fe_add(&h, &u2);
387 secp256k1_fe_negate(&i, &s1, 1); secp256k1_fe_add(&i, &s2);
49ee0dbe
PD
388 if (secp256k1_fe_normalizes_to_zero_var(&h)) {
389 if (secp256k1_fe_normalizes_to_zero_var(&i)) {
4f9791ab 390 secp256k1_gej_double_var(r, a, rzr);
607884fc 391 } else {
2b199de8 392 if (rzr != NULL) {
4f9791ab
PD
393 secp256k1_fe_set_int(rzr, 0);
394 }
eb0be8ee 395 r->infinity = 1;
607884fc
PW
396 }
397 return;
398 }
f735446c
GM
399 secp256k1_fe_sqr(&i2, &i);
400 secp256k1_fe_sqr(&h2, &h);
401 secp256k1_fe_mul(&h3, &h, &h2);
4f9791ab 402 secp256k1_fe_mul(&h, &h, &b->z);
2b199de8 403 if (rzr != NULL) {
4f9791ab
PD
404 *rzr = h;
405 }
406 secp256k1_fe_mul(&r->z, &a->z, &h);
f735446c 407 secp256k1_fe_mul(&t, &u1, &h2);
f11ff5be
PW
408 r->x = t; secp256k1_fe_mul_int(&r->x, 2); secp256k1_fe_add(&r->x, &h3); secp256k1_fe_negate(&r->x, &r->x, 3); secp256k1_fe_add(&r->x, &i2);
409 secp256k1_fe_negate(&r->y, &r->x, 5); secp256k1_fe_add(&r->y, &t); secp256k1_fe_mul(&r->y, &r->y, &i);
910d0de4 410 secp256k1_fe_mul(&h3, &h3, &s1); secp256k1_fe_negate(&h3, &h3, 1);
f11ff5be 411 secp256k1_fe_add(&r->y, &h3);
607884fc
PW
412}
413
dd891e0e 414static void secp256k1_gej_add_ge_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, secp256k1_fe *rzr) {
d61e8995 415 /* 8 mul, 3 sqr, 4 normalize, 12 mul_int/add/negate */
dd891e0e 416 secp256k1_fe z12, u1, u2, s1, s2, h, i, i2, h2, h3, t;
f11ff5be 417 if (a->infinity) {
2d5a186c
PD
418 VERIFY_CHECK(rzr == NULL);
419 secp256k1_gej_set_ge(r, b);
607884fc
PW
420 return;
421 }
f11ff5be 422 if (b->infinity) {
2b199de8 423 if (rzr != NULL) {
2d5a186c
PD
424 secp256k1_fe_set_int(rzr, 1);
425 }
f11ff5be 426 *r = *a;
607884fc
PW
427 return;
428 }
eb0be8ee 429 r->infinity = 0;
4f9791ab 430
f735446c
GM
431 secp256k1_fe_sqr(&z12, &a->z);
432 u1 = a->x; secp256k1_fe_normalize_weak(&u1);
433 secp256k1_fe_mul(&u2, &b->x, &z12);
434 s1 = a->y; secp256k1_fe_normalize_weak(&s1);
435 secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &a->z);
436 secp256k1_fe_negate(&h, &u1, 1); secp256k1_fe_add(&h, &u2);
437 secp256k1_fe_negate(&i, &s1, 1); secp256k1_fe_add(&i, &s2);
49ee0dbe
PD
438 if (secp256k1_fe_normalizes_to_zero_var(&h)) {
439 if (secp256k1_fe_normalizes_to_zero_var(&i)) {
2d5a186c 440 secp256k1_gej_double_var(r, a, rzr);
4f9791ab 441 } else {
2b199de8 442 if (rzr != NULL) {
2d5a186c
PD
443 secp256k1_fe_set_int(rzr, 0);
444 }
4f9791ab
PD
445 r->infinity = 1;
446 }
447 return;
448 }
449 secp256k1_fe_sqr(&i2, &i);
450 secp256k1_fe_sqr(&h2, &h);
451 secp256k1_fe_mul(&h3, &h, &h2);
2b199de8 452 if (rzr != NULL) {
2d5a186c
PD
453 *rzr = h;
454 }
455 secp256k1_fe_mul(&r->z, &a->z, &h);
4f9791ab
PD
456 secp256k1_fe_mul(&t, &u1, &h2);
457 r->x = t; secp256k1_fe_mul_int(&r->x, 2); secp256k1_fe_add(&r->x, &h3); secp256k1_fe_negate(&r->x, &r->x, 3); secp256k1_fe_add(&r->x, &i2);
458 secp256k1_fe_negate(&r->y, &r->x, 5); secp256k1_fe_add(&r->y, &t); secp256k1_fe_mul(&r->y, &r->y, &i);
459 secp256k1_fe_mul(&h3, &h3, &s1); secp256k1_fe_negate(&h3, &h3, 1);
460 secp256k1_fe_add(&r->y, &h3);
461}
462
dd891e0e 463static void secp256k1_gej_add_zinv_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, const secp256k1_fe *bzinv) {
4f9791ab 464 /* 9 mul, 3 sqr, 4 normalize, 12 mul_int/add/negate */
dd891e0e 465 secp256k1_fe az, z12, u1, u2, s1, s2, h, i, i2, h2, h3, t;
4f9791ab
PD
466
467 if (b->infinity) {
468 *r = *a;
469 return;
470 }
471 if (a->infinity) {
dd891e0e 472 secp256k1_fe bzinv2, bzinv3;
4f9791ab
PD
473 r->infinity = b->infinity;
474 secp256k1_fe_sqr(&bzinv2, bzinv);
475 secp256k1_fe_mul(&bzinv3, &bzinv2, bzinv);
476 secp256k1_fe_mul(&r->x, &b->x, &bzinv2);
477 secp256k1_fe_mul(&r->y, &b->y, &bzinv3);
478 secp256k1_fe_set_int(&r->z, 1);
479 return;
480 }
481 r->infinity = 0;
482
483 /** We need to calculate (rx,ry,rz) = (ax,ay,az) + (bx,by,1/bzinv). Due to
484 * secp256k1's isomorphism we can multiply the Z coordinates on both sides
485 * by bzinv, and get: (rx,ry,rz*bzinv) = (ax,ay,az*bzinv) + (bx,by,1).
486 * This means that (rx,ry,rz) can be calculated as
487 * (ax,ay,az*bzinv) + (bx,by,1), when not applying the bzinv factor to rz.
488 * The variable az below holds the modified Z coordinate for a, which is used
489 * for the computation of rx and ry, but not for rz.
490 */
491 secp256k1_fe_mul(&az, &a->z, bzinv);
492
493 secp256k1_fe_sqr(&z12, &az);
494 u1 = a->x; secp256k1_fe_normalize_weak(&u1);
495 secp256k1_fe_mul(&u2, &b->x, &z12);
496 s1 = a->y; secp256k1_fe_normalize_weak(&s1);
497 secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &az);
498 secp256k1_fe_negate(&h, &u1, 1); secp256k1_fe_add(&h, &u2);
499 secp256k1_fe_negate(&i, &s1, 1); secp256k1_fe_add(&i, &s2);
500 if (secp256k1_fe_normalizes_to_zero_var(&h)) {
501 if (secp256k1_fe_normalizes_to_zero_var(&i)) {
502 secp256k1_gej_double_var(r, a, NULL);
607884fc 503 } else {
eb0be8ee 504 r->infinity = 1;
607884fc
PW
505 }
506 return;
507 }
f735446c
GM
508 secp256k1_fe_sqr(&i2, &i);
509 secp256k1_fe_sqr(&h2, &h);
510 secp256k1_fe_mul(&h3, &h, &h2);
f11ff5be 511 r->z = a->z; secp256k1_fe_mul(&r->z, &r->z, &h);
f735446c 512 secp256k1_fe_mul(&t, &u1, &h2);
f11ff5be
PW
513 r->x = t; secp256k1_fe_mul_int(&r->x, 2); secp256k1_fe_add(&r->x, &h3); secp256k1_fe_negate(&r->x, &r->x, 3); secp256k1_fe_add(&r->x, &i2);
514 secp256k1_fe_negate(&r->y, &r->x, 5); secp256k1_fe_add(&r->y, &t); secp256k1_fe_mul(&r->y, &r->y, &i);
910d0de4 515 secp256k1_fe_mul(&h3, &h3, &s1); secp256k1_fe_negate(&h3, &h3, 1);
f11ff5be 516 secp256k1_fe_add(&r->y, &h3);
607884fc
PW
517}
518
4f9791ab 519
dd891e0e 520static void secp256k1_gej_add_ge(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b) {
5a43124c 521 /* Operations: 7 mul, 5 sqr, 4 normalize, 21 mul_int/add/negate/cmov */
dd891e0e
PW
522 static const secp256k1_fe fe_1 = SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 1);
523 secp256k1_fe zz, u1, u2, s1, s2, t, tt, m, n, q, rr;
524 secp256k1_fe m_alt, rr_alt;
5de4c5df 525 int infinity, degenerate;
9338dbf7
PW
526 VERIFY_CHECK(!b->infinity);
527 VERIFY_CHECK(a->infinity == 0 || a->infinity == 1);
528
71712b27
GM
529 /** In:
530 * Eric Brier and Marc Joye, Weierstrass Elliptic Curves and Side-Channel Attacks.
531 * In D. Naccache and P. Paillier, Eds., Public Key Cryptography, vol. 2274 of Lecture Notes in Computer Science, pages 335-345. Springer-Verlag, 2002.
532 * we find as solution for a unified addition/doubling formula:
533 * lambda = ((x1 + x2)^2 - x1 * x2 + a) / (y1 + y2), with a = 0 for secp256k1's curve equation.
534 * x3 = lambda^2 - (x1 + x2)
535 * 2*y3 = lambda * (x1 + x2 - 2 * x3) - (y1 + y2).
536 *
537 * Substituting x_i = Xi / Zi^2 and yi = Yi / Zi^3, for i=1,2,3, gives:
538 * U1 = X1*Z2^2, U2 = X2*Z1^2
2a54f9bc 539 * S1 = Y1*Z2^3, S2 = Y2*Z1^3
71712b27
GM
540 * Z = Z1*Z2
541 * T = U1+U2
542 * M = S1+S2
543 * Q = T*M^2
544 * R = T^2-U1*U2
545 * X3 = 4*(R^2-Q)
546 * Y3 = 4*(R*(3*Q-2*R^2)-M^4)
547 * Z3 = 2*M*Z
548 * (Note that the paper uses xi = Xi / Zi and yi = Yi / Zi instead.)
5de4c5df
AP
549 *
550 * This formula has the benefit of being the same for both addition
551 * of distinct points and doubling. However, it breaks down in the
552 * case that either point is infinity, or that y1 = -y2. We handle
553 * these cases in the following ways:
554 *
555 * - If b is infinity we simply bail by means of a VERIFY_CHECK.
556 *
557 * - If a is infinity, we detect this, and at the end of the
558 * computation replace the result (which will be meaningless,
559 * but we compute to be constant-time) with b.x : b.y : 1.
560 *
561 * - If a = -b, we have y1 = -y2, which is a degenerate case.
562 * But here the answer is infinity, so we simply set the
563 * infinity flag of the result, overriding the computed values
564 * without even needing to cmov.
565 *
566 * - If y1 = -y2 but x1 != x2, which does occur thanks to certain
567 * properties of our curve (specifically, 1 has nontrivial cube
568 * roots in our field, and the curve equation has no x coefficient)
569 * then the answer is not infinity but also not given by the above
570 * equation. In this case, we cmov in place an alternate expression
571 * for lambda. Specifically (y1 - y2)/(x1 - x2). Where both these
572 * expressions for lambda are defined, they are equal, and can be
573 * obtained from each other by multiplication by (y1 + y2)/(y1 + y2)
574 * then substitution of x^3 + 7 for y^2 (using the curve equation).
575 * For all pairs of nonzero points (a, b) at least one is defined,
576 * so this covers everything.
71712b27
GM
577 */
578
f735446c
GM
579 secp256k1_fe_sqr(&zz, &a->z); /* z = Z1^2 */
580 u1 = a->x; secp256k1_fe_normalize_weak(&u1); /* u1 = U1 = X1*Z2^2 (1) */
581 secp256k1_fe_mul(&u2, &b->x, &zz); /* u2 = U2 = X2*Z1^2 (1) */
582 s1 = a->y; secp256k1_fe_normalize_weak(&s1); /* s1 = S1 = Y1*Z2^3 (1) */
81e45ff9 583 secp256k1_fe_mul(&s2, &b->y, &zz); /* s2 = Y2*Z1^2 (1) */
f735446c 584 secp256k1_fe_mul(&s2, &s2, &a->z); /* s2 = S2 = Y2*Z1^3 (1) */
f735446c
GM
585 t = u1; secp256k1_fe_add(&t, &u2); /* t = T = U1+U2 (2) */
586 m = s1; secp256k1_fe_add(&m, &s2); /* m = M = S1+S2 (2) */
bcf2fcfd 587 secp256k1_fe_sqr(&rr, &t); /* rr = T^2 (1) */
a5d796e0 588 secp256k1_fe_negate(&m_alt, &u2, 1); /* Malt = -X2*Z1^2 */
7d054cd0
PD
589 secp256k1_fe_mul(&tt, &u1, &m_alt); /* tt = -U1*U2 (2) */
590 secp256k1_fe_add(&rr, &tt); /* rr = R = T^2-U1*U2 (3) */
5de4c5df
AP
591 /** If lambda = R/M = 0/0 we have a problem (except in the "trivial"
592 * case that Z = z1z2 = 0, and this is special-cased later on). */
593 degenerate = secp256k1_fe_normalizes_to_zero(&m) &
594 secp256k1_fe_normalizes_to_zero(&rr);
595 /* This only occurs when y1 == -y2 and x1^3 == x2^3, but x1 != x2.
596 * This means either x1 == beta*x2 or beta*x1 == x2, where beta is
597 * a nontrivial cube root of one. In either case, an alternate
598 * non-indeterminate expression for lambda is (y1 - y2)/(x1 - x2),
599 * so we set R/M equal to this. */
5a43124c
PD
600 rr_alt = s1;
601 secp256k1_fe_mul_int(&rr_alt, 2); /* rr = Y1*Z2^3 - Y2*Z1^3 (2) */
a5d796e0 602 secp256k1_fe_add(&m_alt, &u1); /* Malt = X1*Z2^2 - X2*Z1^2 */
5de4c5df
AP
603
604 secp256k1_fe_cmov(&rr_alt, &rr, !degenerate);
605 secp256k1_fe_cmov(&m_alt, &m, !degenerate);
606 /* Now Ralt / Malt = lambda and is guaranteed not to be 0/0.
607 * From here on out Ralt and Malt represent the numerator
608 * and denominator of lambda; R and M represent the explicit
609 * expressions x1^2 + x2^2 + x1x2 and y1 + y2. */
610 secp256k1_fe_sqr(&n, &m_alt); /* n = Malt^2 (1) */
611 secp256k1_fe_mul(&q, &n, &t); /* q = Q = T*Malt^2 (1) */
612 /* These two lines use the observation that either M == Malt or M == 0,
613 * so M^3 * Malt is either Malt^4 (which is computed by squaring), or
614 * zero (which is "computed" by cmov). So the cost is one squaring
615 * versus two multiplications. */
55e7fc32
PD
616 secp256k1_fe_sqr(&n, &n);
617 secp256k1_fe_cmov(&n, &m, degenerate); /* n = M^3 * Malt (2) */
5de4c5df 618 secp256k1_fe_sqr(&t, &rr_alt); /* t = Ralt^2 (1) */
b28d02a5 619 secp256k1_fe_mul(&r->z, &a->z, &m_alt); /* r->z = Malt*Z (1) */
f735446c 620 infinity = secp256k1_fe_normalizes_to_zero(&r->z) * (1 - a->infinity);
5de4c5df 621 secp256k1_fe_mul_int(&r->z, 2); /* r->z = Z3 = 2*Malt*Z (2) */
71712b27 622 secp256k1_fe_negate(&q, &q, 1); /* q = -Q (2) */
55e7fc32
PD
623 secp256k1_fe_add(&t, &q); /* t = Ralt^2-Q (3) */
624 secp256k1_fe_normalize_weak(&t);
625 r->x = t; /* r->x = Ralt^2-Q (1) */
bcf2fcfd 626 secp256k1_fe_mul_int(&t, 2); /* t = 2*x3 (2) */
55e7fc32 627 secp256k1_fe_add(&t, &q); /* t = 2*x3 - Q: (4) */
5de4c5df 628 secp256k1_fe_mul(&t, &t, &rr_alt); /* t = Ralt*(2*x3 - Q) (1) */
55e7fc32
PD
629 secp256k1_fe_add(&t, &n); /* t = Ralt*(2*x3 - Q) + M^3*Malt (3) */
630 secp256k1_fe_negate(&r->y, &t, 3); /* r->y = Ralt*(Q - 2x3) - M^3*Malt (4) */
0295f0a3 631 secp256k1_fe_normalize_weak(&r->y);
5de4c5df
AP
632 secp256k1_fe_mul_int(&r->x, 4); /* r->x = X3 = 4*(Ralt^2-Q) */
633 secp256k1_fe_mul_int(&r->y, 4); /* r->y = Y3 = 4*Ralt*(Q - 2x3) - 4*M^3*Malt (4) */
9338dbf7 634
a1d5ae15 635 /** In case a->infinity == 1, replace r with (b->x, b->y, 1). */
bb0ea50d
GM
636 secp256k1_fe_cmov(&r->x, &b->x, a->infinity);
637 secp256k1_fe_cmov(&r->y, &b->y, a->infinity);
638 secp256k1_fe_cmov(&r->z, &fe_1, a->infinity);
9338dbf7
PW
639 r->infinity = infinity;
640}
641
dd891e0e 642static void secp256k1_gej_rescale(secp256k1_gej *r, const secp256k1_fe *s) {
d2275795 643 /* Operations: 4 mul, 1 sqr */
dd891e0e 644 secp256k1_fe zz;
d2275795
GM
645 VERIFY_CHECK(!secp256k1_fe_is_zero(s));
646 secp256k1_fe_sqr(&zz, s);
647 secp256k1_fe_mul(&r->x, &r->x, &zz); /* r->x *= s^2 */
648 secp256k1_fe_mul(&r->y, &r->y, &zz);
649 secp256k1_fe_mul(&r->y, &r->y, s); /* r->y *= s^3 */
650 secp256k1_fe_mul(&r->z, &r->z, s); /* r->z *= s */
651}
652
dd891e0e
PW
653static void secp256k1_ge_to_storage(secp256k1_ge_storage *r, const secp256k1_ge *a) {
654 secp256k1_fe x, y;
e68d7208
PW
655 VERIFY_CHECK(!a->infinity);
656 x = a->x;
657 secp256k1_fe_normalize(&x);
658 y = a->y;
659 secp256k1_fe_normalize(&y);
660 secp256k1_fe_to_storage(&r->x, &x);
661 secp256k1_fe_to_storage(&r->y, &y);
662}
663
dd891e0e 664static void secp256k1_ge_from_storage(secp256k1_ge *r, const secp256k1_ge_storage *a) {
e68d7208
PW
665 secp256k1_fe_from_storage(&r->x, &a->x);
666 secp256k1_fe_from_storage(&r->y, &a->y);
667 r->infinity = 0;
668}
669
dd891e0e 670static SECP256K1_INLINE void secp256k1_ge_storage_cmov(secp256k1_ge_storage *r, const secp256k1_ge_storage *a, int flag) {
55422b6a
PW
671 secp256k1_fe_storage_cmov(&r->x, &a->x, flag);
672 secp256k1_fe_storage_cmov(&r->y, &a->y, flag);
673}
674
399c03f2 675#ifdef USE_ENDOMORPHISM
dd891e0e
PW
676static void secp256k1_ge_mul_lambda(secp256k1_ge *r, const secp256k1_ge *a) {
677 static const secp256k1_fe beta = SECP256K1_FE_CONST(
4732d260
PW
678 0x7ae96a2bul, 0x657c0710ul, 0x6e64479eul, 0xac3434e9ul,
679 0x9cf04975ul, 0x12f58995ul, 0xc1396c28ul, 0x719501eeul
680 );
f11ff5be 681 *r = *a;
4732d260 682 secp256k1_fe_mul(&r->x, &r->x, &beta);
607884fc 683}
399c03f2 684#endif
607884fc 685
e6e9805f
PW
686static int secp256k1_gej_has_quad_y_var(const secp256k1_gej *a) {
687 secp256k1_fe yz;
688
689 if (a->infinity) {
690 return 0;
691 }
692
693 /* We rely on the fact that the Jacobi symbol of 1 / a->z^3 is the same as
694 * that of a->z. Thus a->y / a->z^3 is a quadratic residue iff a->y * a->z
695 is */
696 secp256k1_fe_mul(&yz, &a->y, &a->z);
697 return secp256k1_fe_is_quad_var(&yz);
698}
699
abe2d3e8 700#endif /* SECP256K1_GROUP_IMPL_H */
This page took 0.165856 seconds and 4 git commands to generate.