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 **********************************************************************/
6
7 #ifndef SECP256K1_GROUP_IMPL_H
8 #define SECP256K1_GROUP_IMPL_H
9
10 #include "num.h"
11 #include "field.h"
12 #include "group.h"
13
14 /* These exhaustive group test orders and generators are chosen such that:
15 * - The field size is equal to that of secp256k1, so field code is the same.
16 * - The curve equation is of the form y^2=x^3+B for some constant B.
17 * - The subgroup has a generator 2*P, where P.x=1.
18 * - The subgroup has size less than 1000 to permit exhaustive testing.
19 * - The subgroup admits an endomorphism of the form lambda*(x,y) == (beta*x,y).
20 *
21 * These parameters are generated using sage/gen_exhaustive_groups.sage.
22 */
23 #if defined(EXHAUSTIVE_TEST_ORDER)
24 # if EXHAUSTIVE_TEST_ORDER == 13
25 static const secp256k1_ge secp256k1_ge_const_g = SECP256K1_GE_CONST(
26 0xc3459c3d, 0x35326167, 0xcd86cce8, 0x07a2417f,
27 0x5b8bd567, 0xde8538ee, 0x0d507b0c, 0xd128f5bb,
28 0x8e467fec, 0xcd30000a, 0x6cc1184e, 0x25d382c2,
29 0xa2f4494e, 0x2fbe9abc, 0x8b64abac, 0xd005fb24
30 );
31 static const secp256k1_fe secp256k1_fe_const_b = SECP256K1_FE_CONST(
32 0x3d3486b2, 0x159a9ca5, 0xc75638be, 0xb23a69bc,
33 0x946a45ab, 0x24801247, 0xb4ed2b8e, 0x26b6a417
34 );
35 # elif EXHAUSTIVE_TEST_ORDER == 199
36 static const secp256k1_ge secp256k1_ge_const_g = SECP256K1_GE_CONST(
37 0x226e653f, 0xc8df7744, 0x9bacbf12, 0x7d1dcbf9,
38 0x87f05b2a, 0xe7edbd28, 0x1f564575, 0xc48dcf18,
39 0xa13872c2, 0xe933bb17, 0x5d9ffd5b, 0xb5b6e10c,
40 0x57fe3c00, 0xbaaaa15a, 0xe003ec3e, 0x9c269bae
41 );
42 static const secp256k1_fe secp256k1_fe_const_b = SECP256K1_FE_CONST(
43 0x2cca28fa, 0xfc614b80, 0x2a3db42b, 0x00ba00b1,
44 0xbea8d943, 0xdace9ab2, 0x9536daea, 0x0074defb
45 );
46 # else
47 # error No known generator for the specified exhaustive test group order.
48 # endif
49 #else
50 /** Generator for secp256k1, value 'g' defined in
51 * "Standards for Efficient Cryptography" (SEC2) 2.7.1.
52 */
53 static const secp256k1_ge secp256k1_ge_const_g = SECP256K1_GE_CONST(
54 0x79BE667EUL, 0xF9DCBBACUL, 0x55A06295UL, 0xCE870B07UL,
55 0x029BFCDBUL, 0x2DCE28D9UL, 0x59F2815BUL, 0x16F81798UL,
56 0x483ADA77UL, 0x26A3C465UL, 0x5DA4FBFCUL, 0x0E1108A8UL,
57 0xFD17B448UL, 0xA6855419UL, 0x9C47D08FUL, 0xFB10D4B8UL
58 );
59
60 static const secp256k1_fe secp256k1_fe_const_b = SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 7);
61 #endif
62
secp256k1_ge_set_gej_zinv(secp256k1_ge * r,const secp256k1_gej * a,const secp256k1_fe * zi)63 static void secp256k1_ge_set_gej_zinv(secp256k1_ge *r, const secp256k1_gej *a, const secp256k1_fe *zi) {
64 secp256k1_fe zi2;
65 secp256k1_fe zi3;
66 secp256k1_fe_sqr(&zi2, zi);
67 secp256k1_fe_mul(&zi3, &zi2, zi);
68 secp256k1_fe_mul(&r->x, &a->x, &zi2);
69 secp256k1_fe_mul(&r->y, &a->y, &zi3);
70 r->infinity = a->infinity;
71 }
72
secp256k1_ge_set_xy(secp256k1_ge * r,const secp256k1_fe * x,const secp256k1_fe * y)73 static void secp256k1_ge_set_xy(secp256k1_ge *r, const secp256k1_fe *x, const secp256k1_fe *y) {
74 r->infinity = 0;
75 r->x = *x;
76 r->y = *y;
77 }
78
secp256k1_ge_is_infinity(const secp256k1_ge * a)79 static int secp256k1_ge_is_infinity(const secp256k1_ge *a) {
80 return a->infinity;
81 }
82
secp256k1_ge_neg(secp256k1_ge * r,const secp256k1_ge * a)83 static void secp256k1_ge_neg(secp256k1_ge *r, const secp256k1_ge *a) {
84 *r = *a;
85 secp256k1_fe_normalize_weak(&r->y);
86 secp256k1_fe_negate(&r->y, &r->y, 1);
87 }
88
secp256k1_ge_set_gej(secp256k1_ge * r,secp256k1_gej * a)89 static void secp256k1_ge_set_gej(secp256k1_ge *r, secp256k1_gej *a) {
90 secp256k1_fe z2, z3;
91 r->infinity = a->infinity;
92 secp256k1_fe_inv(&a->z, &a->z);
93 secp256k1_fe_sqr(&z2, &a->z);
94 secp256k1_fe_mul(&z3, &a->z, &z2);
95 secp256k1_fe_mul(&a->x, &a->x, &z2);
96 secp256k1_fe_mul(&a->y, &a->y, &z3);
97 secp256k1_fe_set_int(&a->z, 1);
98 r->x = a->x;
99 r->y = a->y;
100 }
101
secp256k1_ge_set_gej_var(secp256k1_ge * r,secp256k1_gej * a)102 static void secp256k1_ge_set_gej_var(secp256k1_ge *r, secp256k1_gej *a) {
103 secp256k1_fe z2, z3;
104 r->infinity = a->infinity;
105 if (a->infinity) {
106 return;
107 }
108 secp256k1_fe_inv_var(&a->z, &a->z);
109 secp256k1_fe_sqr(&z2, &a->z);
110 secp256k1_fe_mul(&z3, &a->z, &z2);
111 secp256k1_fe_mul(&a->x, &a->x, &z2);
112 secp256k1_fe_mul(&a->y, &a->y, &z3);
113 secp256k1_fe_set_int(&a->z, 1);
114 r->x = a->x;
115 r->y = a->y;
116 }
117
secp256k1_ge_set_all_gej_var(secp256k1_ge * r,const secp256k1_gej * a,size_t len)118 static void secp256k1_ge_set_all_gej_var(secp256k1_ge *r, const secp256k1_gej *a, size_t len) {
119 secp256k1_fe u;
120 size_t i;
121 size_t last_i = SIZE_MAX;
122
123 for (i = 0; i < len; i++) {
124 if (!a[i].infinity) {
125 /* Use destination's x coordinates as scratch space */
126 if (last_i == SIZE_MAX) {
127 r[i].x = a[i].z;
128 } else {
129 secp256k1_fe_mul(&r[i].x, &r[last_i].x, &a[i].z);
130 }
131 last_i = i;
132 }
133 }
134 if (last_i == SIZE_MAX) {
135 return;
136 }
137 secp256k1_fe_inv_var(&u, &r[last_i].x);
138
139 i = last_i;
140 while (i > 0) {
141 i--;
142 if (!a[i].infinity) {
143 secp256k1_fe_mul(&r[last_i].x, &r[i].x, &u);
144 secp256k1_fe_mul(&u, &u, &a[last_i].z);
145 last_i = i;
146 }
147 }
148 VERIFY_CHECK(!a[last_i].infinity);
149 r[last_i].x = u;
150
151 for (i = 0; i < len; i++) {
152 r[i].infinity = a[i].infinity;
153 if (!a[i].infinity) {
154 secp256k1_ge_set_gej_zinv(&r[i], &a[i], &r[i].x);
155 }
156 }
157 }
158
secp256k1_ge_globalz_set_table_gej(size_t len,secp256k1_ge * r,secp256k1_fe * globalz,const secp256k1_gej * a,const secp256k1_fe * zr)159 static void secp256k1_ge_globalz_set_table_gej(size_t len, secp256k1_ge *r, secp256k1_fe *globalz, const secp256k1_gej *a, const secp256k1_fe *zr) {
160 size_t i = len - 1;
161 secp256k1_fe zs;
162
163 if (len > 0) {
164 /* The z of the final point gives us the "global Z" for the table. */
165 r[i].x = a[i].x;
166 r[i].y = a[i].y;
167 /* Ensure all y values are in weak normal form for fast negation of points */
168 secp256k1_fe_normalize_weak(&r[i].y);
169 *globalz = a[i].z;
170 r[i].infinity = 0;
171 zs = zr[i];
172
173 /* Work our way backwards, using the z-ratios to scale the x/y values. */
174 while (i > 0) {
175 if (i != len - 1) {
176 secp256k1_fe_mul(&zs, &zs, &zr[i]);
177 }
178 i--;
179 secp256k1_ge_set_gej_zinv(&r[i], &a[i], &zs);
180 }
181 }
182 }
183
secp256k1_gej_set_infinity(secp256k1_gej * r)184 static void secp256k1_gej_set_infinity(secp256k1_gej *r) {
185 r->infinity = 1;
186 secp256k1_fe_clear(&r->x);
187 secp256k1_fe_clear(&r->y);
188 secp256k1_fe_clear(&r->z);
189 }
190
secp256k1_ge_set_infinity(secp256k1_ge * r)191 static void secp256k1_ge_set_infinity(secp256k1_ge *r) {
192 r->infinity = 1;
193 secp256k1_fe_clear(&r->x);
194 secp256k1_fe_clear(&r->y);
195 }
196
secp256k1_gej_clear(secp256k1_gej * r)197 static void secp256k1_gej_clear(secp256k1_gej *r) {
198 r->infinity = 0;
199 secp256k1_fe_clear(&r->x);
200 secp256k1_fe_clear(&r->y);
201 secp256k1_fe_clear(&r->z);
202 }
203
secp256k1_ge_clear(secp256k1_ge * r)204 static void secp256k1_ge_clear(secp256k1_ge *r) {
205 r->infinity = 0;
206 secp256k1_fe_clear(&r->x);
207 secp256k1_fe_clear(&r->y);
208 }
209
secp256k1_ge_set_xquad(secp256k1_ge * r,const secp256k1_fe * x)210 static int secp256k1_ge_set_xquad(secp256k1_ge *r, const secp256k1_fe *x) {
211 secp256k1_fe x2, x3;
212 r->x = *x;
213 secp256k1_fe_sqr(&x2, x);
214 secp256k1_fe_mul(&x3, x, &x2);
215 r->infinity = 0;
216 secp256k1_fe_add(&x3, &secp256k1_fe_const_b);
217 return secp256k1_fe_sqrt(&r->y, &x3);
218 }
219
secp256k1_ge_set_xo_var(secp256k1_ge * r,const secp256k1_fe * x,int odd)220 static int secp256k1_ge_set_xo_var(secp256k1_ge *r, const secp256k1_fe *x, int odd) {
221 if (!secp256k1_ge_set_xquad(r, x)) {
222 return 0;
223 }
224 secp256k1_fe_normalize_var(&r->y);
225 if (secp256k1_fe_is_odd(&r->y) != odd) {
226 secp256k1_fe_negate(&r->y, &r->y, 1);
227 }
228 return 1;
229
230 }
231
secp256k1_gej_set_ge(secp256k1_gej * r,const secp256k1_ge * a)232 static void secp256k1_gej_set_ge(secp256k1_gej *r, const secp256k1_ge *a) {
233 r->infinity = a->infinity;
234 r->x = a->x;
235 r->y = a->y;
236 secp256k1_fe_set_int(&r->z, 1);
237 }
238
secp256k1_gej_eq_x_var(const secp256k1_fe * x,const secp256k1_gej * a)239 static int secp256k1_gej_eq_x_var(const secp256k1_fe *x, const secp256k1_gej *a) {
240 secp256k1_fe r, r2;
241 VERIFY_CHECK(!a->infinity);
242 secp256k1_fe_sqr(&r, &a->z); secp256k1_fe_mul(&r, &r, x);
243 r2 = a->x; secp256k1_fe_normalize_weak(&r2);
244 return secp256k1_fe_equal_var(&r, &r2);
245 }
246
secp256k1_gej_neg(secp256k1_gej * r,const secp256k1_gej * a)247 static void secp256k1_gej_neg(secp256k1_gej *r, const secp256k1_gej *a) {
248 r->infinity = a->infinity;
249 r->x = a->x;
250 r->y = a->y;
251 r->z = a->z;
252 secp256k1_fe_normalize_weak(&r->y);
253 secp256k1_fe_negate(&r->y, &r->y, 1);
254 }
255
secp256k1_gej_is_infinity(const secp256k1_gej * a)256 static int secp256k1_gej_is_infinity(const secp256k1_gej *a) {
257 return a->infinity;
258 }
259
secp256k1_ge_is_valid_var(const secp256k1_ge * a)260 static int secp256k1_ge_is_valid_var(const secp256k1_ge *a) {
261 secp256k1_fe y2, x3;
262 if (a->infinity) {
263 return 0;
264 }
265 /* y^2 = x^3 + 7 */
266 secp256k1_fe_sqr(&y2, &a->y);
267 secp256k1_fe_sqr(&x3, &a->x); secp256k1_fe_mul(&x3, &x3, &a->x);
268 secp256k1_fe_add(&x3, &secp256k1_fe_const_b);
269 secp256k1_fe_normalize_weak(&x3);
270 return secp256k1_fe_equal_var(&y2, &x3);
271 }
272
secp256k1_gej_double(secp256k1_gej * r,const secp256k1_gej * a)273 static SECP256K1_INLINE void secp256k1_gej_double(secp256k1_gej *r, const secp256k1_gej *a) {
274 /* Operations: 3 mul, 4 sqr, 0 normalize, 12 mul_int/add/negate.
275 *
276 * Note that there is an implementation described at
277 * https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l
278 * which trades a multiply for a square, but in practice this is actually slower,
279 * mainly because it requires more normalizations.
280 */
281 secp256k1_fe t1,t2,t3,t4;
282
283 r->infinity = a->infinity;
284
285 secp256k1_fe_mul(&r->z, &a->z, &a->y);
286 secp256k1_fe_mul_int(&r->z, 2); /* Z' = 2*Y*Z (2) */
287 secp256k1_fe_sqr(&t1, &a->x);
288 secp256k1_fe_mul_int(&t1, 3); /* T1 = 3*X^2 (3) */
289 secp256k1_fe_sqr(&t2, &t1); /* T2 = 9*X^4 (1) */
290 secp256k1_fe_sqr(&t3, &a->y);
291 secp256k1_fe_mul_int(&t3, 2); /* T3 = 2*Y^2 (2) */
292 secp256k1_fe_sqr(&t4, &t3);
293 secp256k1_fe_mul_int(&t4, 2); /* T4 = 8*Y^4 (2) */
294 secp256k1_fe_mul(&t3, &t3, &a->x); /* T3 = 2*X*Y^2 (1) */
295 r->x = t3;
296 secp256k1_fe_mul_int(&r->x, 4); /* X' = 8*X*Y^2 (4) */
297 secp256k1_fe_negate(&r->x, &r->x, 4); /* X' = -8*X*Y^2 (5) */
298 secp256k1_fe_add(&r->x, &t2); /* X' = 9*X^4 - 8*X*Y^2 (6) */
299 secp256k1_fe_negate(&t2, &t2, 1); /* T2 = -9*X^4 (2) */
300 secp256k1_fe_mul_int(&t3, 6); /* T3 = 12*X*Y^2 (6) */
301 secp256k1_fe_add(&t3, &t2); /* T3 = 12*X*Y^2 - 9*X^4 (8) */
302 secp256k1_fe_mul(&r->y, &t1, &t3); /* Y' = 36*X^3*Y^2 - 27*X^6 (1) */
303 secp256k1_fe_negate(&t2, &t4, 2); /* T2 = -8*Y^4 (3) */
304 secp256k1_fe_add(&r->y, &t2); /* Y' = 36*X^3*Y^2 - 27*X^6 - 8*Y^4 (4) */
305 }
306
secp256k1_gej_double_var(secp256k1_gej * r,const secp256k1_gej * a,secp256k1_fe * rzr)307 static void secp256k1_gej_double_var(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr) {
308 /** For secp256k1, 2Q is infinity if and only if Q is infinity. This is because if 2Q = infinity,
309 * 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
310 * y=0, x^3 must be -7 mod p. However, -7 has no cube root mod p.
311 *
312 * Having said this, if this function receives a point on a sextic twist, e.g. by
313 * a fault attack, it is possible for y to be 0. This happens for y^2 = x^3 + 6,
314 * since -6 does have a cube root mod p. For this point, this function will not set
315 * the infinity flag even though the point doubles to infinity, and the result
316 * point will be gibberish (z = 0 but infinity = 0).
317 */
318 if (a->infinity) {
319 r->infinity = 1;
320 if (rzr != NULL) {
321 secp256k1_fe_set_int(rzr, 1);
322 }
323 return;
324 }
325
326 if (rzr != NULL) {
327 *rzr = a->y;
328 secp256k1_fe_normalize_weak(rzr);
329 secp256k1_fe_mul_int(rzr, 2);
330 }
331
332 secp256k1_gej_double(r, a);
333 }
334
secp256k1_gej_add_var(secp256k1_gej * r,const secp256k1_gej * a,const secp256k1_gej * b,secp256k1_fe * rzr)335 static void secp256k1_gej_add_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_gej *b, secp256k1_fe *rzr) {
336 /* Operations: 12 mul, 4 sqr, 2 normalize, 12 mul_int/add/negate */
337 secp256k1_fe z22, z12, u1, u2, s1, s2, h, i, i2, h2, h3, t;
338
339 if (a->infinity) {
340 VERIFY_CHECK(rzr == NULL);
341 *r = *b;
342 return;
343 }
344
345 if (b->infinity) {
346 if (rzr != NULL) {
347 secp256k1_fe_set_int(rzr, 1);
348 }
349 *r = *a;
350 return;
351 }
352
353 r->infinity = 0;
354 secp256k1_fe_sqr(&z22, &b->z);
355 secp256k1_fe_sqr(&z12, &a->z);
356 secp256k1_fe_mul(&u1, &a->x, &z22);
357 secp256k1_fe_mul(&u2, &b->x, &z12);
358 secp256k1_fe_mul(&s1, &a->y, &z22); secp256k1_fe_mul(&s1, &s1, &b->z);
359 secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &a->z);
360 secp256k1_fe_negate(&h, &u1, 1); secp256k1_fe_add(&h, &u2);
361 secp256k1_fe_negate(&i, &s1, 1); secp256k1_fe_add(&i, &s2);
362 if (secp256k1_fe_normalizes_to_zero_var(&h)) {
363 if (secp256k1_fe_normalizes_to_zero_var(&i)) {
364 secp256k1_gej_double_var(r, a, rzr);
365 } else {
366 if (rzr != NULL) {
367 secp256k1_fe_set_int(rzr, 0);
368 }
369 secp256k1_gej_set_infinity(r);
370 }
371 return;
372 }
373 secp256k1_fe_sqr(&i2, &i);
374 secp256k1_fe_sqr(&h2, &h);
375 secp256k1_fe_mul(&h3, &h, &h2);
376 secp256k1_fe_mul(&h, &h, &b->z);
377 if (rzr != NULL) {
378 *rzr = h;
379 }
380 secp256k1_fe_mul(&r->z, &a->z, &h);
381 secp256k1_fe_mul(&t, &u1, &h2);
382 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);
383 secp256k1_fe_negate(&r->y, &r->x, 5); secp256k1_fe_add(&r->y, &t); secp256k1_fe_mul(&r->y, &r->y, &i);
384 secp256k1_fe_mul(&h3, &h3, &s1); secp256k1_fe_negate(&h3, &h3, 1);
385 secp256k1_fe_add(&r->y, &h3);
386 }
387
secp256k1_gej_add_ge_var(secp256k1_gej * r,const secp256k1_gej * a,const secp256k1_ge * b,secp256k1_fe * rzr)388 static void secp256k1_gej_add_ge_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, secp256k1_fe *rzr) {
389 /* 8 mul, 3 sqr, 4 normalize, 12 mul_int/add/negate */
390 secp256k1_fe z12, u1, u2, s1, s2, h, i, i2, h2, h3, t;
391 if (a->infinity) {
392 VERIFY_CHECK(rzr == NULL);
393 secp256k1_gej_set_ge(r, b);
394 return;
395 }
396 if (b->infinity) {
397 if (rzr != NULL) {
398 secp256k1_fe_set_int(rzr, 1);
399 }
400 *r = *a;
401 return;
402 }
403 r->infinity = 0;
404
405 secp256k1_fe_sqr(&z12, &a->z);
406 u1 = a->x; secp256k1_fe_normalize_weak(&u1);
407 secp256k1_fe_mul(&u2, &b->x, &z12);
408 s1 = a->y; secp256k1_fe_normalize_weak(&s1);
409 secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &a->z);
410 secp256k1_fe_negate(&h, &u1, 1); secp256k1_fe_add(&h, &u2);
411 secp256k1_fe_negate(&i, &s1, 1); secp256k1_fe_add(&i, &s2);
412 if (secp256k1_fe_normalizes_to_zero_var(&h)) {
413 if (secp256k1_fe_normalizes_to_zero_var(&i)) {
414 secp256k1_gej_double_var(r, a, rzr);
415 } else {
416 if (rzr != NULL) {
417 secp256k1_fe_set_int(rzr, 0);
418 }
419 secp256k1_gej_set_infinity(r);
420 }
421 return;
422 }
423 secp256k1_fe_sqr(&i2, &i);
424 secp256k1_fe_sqr(&h2, &h);
425 secp256k1_fe_mul(&h3, &h, &h2);
426 if (rzr != NULL) {
427 *rzr = h;
428 }
429 secp256k1_fe_mul(&r->z, &a->z, &h);
430 secp256k1_fe_mul(&t, &u1, &h2);
431 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);
432 secp256k1_fe_negate(&r->y, &r->x, 5); secp256k1_fe_add(&r->y, &t); secp256k1_fe_mul(&r->y, &r->y, &i);
433 secp256k1_fe_mul(&h3, &h3, &s1); secp256k1_fe_negate(&h3, &h3, 1);
434 secp256k1_fe_add(&r->y, &h3);
435 }
436
secp256k1_gej_add_zinv_var(secp256k1_gej * r,const secp256k1_gej * a,const secp256k1_ge * b,const secp256k1_fe * bzinv)437 static void secp256k1_gej_add_zinv_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, const secp256k1_fe *bzinv) {
438 /* 9 mul, 3 sqr, 4 normalize, 12 mul_int/add/negate */
439 secp256k1_fe az, z12, u1, u2, s1, s2, h, i, i2, h2, h3, t;
440
441 if (b->infinity) {
442 *r = *a;
443 return;
444 }
445 if (a->infinity) {
446 secp256k1_fe bzinv2, bzinv3;
447 r->infinity = b->infinity;
448 secp256k1_fe_sqr(&bzinv2, bzinv);
449 secp256k1_fe_mul(&bzinv3, &bzinv2, bzinv);
450 secp256k1_fe_mul(&r->x, &b->x, &bzinv2);
451 secp256k1_fe_mul(&r->y, &b->y, &bzinv3);
452 secp256k1_fe_set_int(&r->z, 1);
453 return;
454 }
455 r->infinity = 0;
456
457 /** We need to calculate (rx,ry,rz) = (ax,ay,az) + (bx,by,1/bzinv). Due to
458 * secp256k1's isomorphism we can multiply the Z coordinates on both sides
459 * by bzinv, and get: (rx,ry,rz*bzinv) = (ax,ay,az*bzinv) + (bx,by,1).
460 * This means that (rx,ry,rz) can be calculated as
461 * (ax,ay,az*bzinv) + (bx,by,1), when not applying the bzinv factor to rz.
462 * The variable az below holds the modified Z coordinate for a, which is used
463 * for the computation of rx and ry, but not for rz.
464 */
465 secp256k1_fe_mul(&az, &a->z, bzinv);
466
467 secp256k1_fe_sqr(&z12, &az);
468 u1 = a->x; secp256k1_fe_normalize_weak(&u1);
469 secp256k1_fe_mul(&u2, &b->x, &z12);
470 s1 = a->y; secp256k1_fe_normalize_weak(&s1);
471 secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &az);
472 secp256k1_fe_negate(&h, &u1, 1); secp256k1_fe_add(&h, &u2);
473 secp256k1_fe_negate(&i, &s1, 1); secp256k1_fe_add(&i, &s2);
474 if (secp256k1_fe_normalizes_to_zero_var(&h)) {
475 if (secp256k1_fe_normalizes_to_zero_var(&i)) {
476 secp256k1_gej_double_var(r, a, NULL);
477 } else {
478 secp256k1_gej_set_infinity(r);
479 }
480 return;
481 }
482 secp256k1_fe_sqr(&i2, &i);
483 secp256k1_fe_sqr(&h2, &h);
484 secp256k1_fe_mul(&h3, &h, &h2);
485 r->z = a->z; secp256k1_fe_mul(&r->z, &r->z, &h);
486 secp256k1_fe_mul(&t, &u1, &h2);
487 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);
488 secp256k1_fe_negate(&r->y, &r->x, 5); secp256k1_fe_add(&r->y, &t); secp256k1_fe_mul(&r->y, &r->y, &i);
489 secp256k1_fe_mul(&h3, &h3, &s1); secp256k1_fe_negate(&h3, &h3, 1);
490 secp256k1_fe_add(&r->y, &h3);
491 }
492
493
secp256k1_gej_add_ge(secp256k1_gej * r,const secp256k1_gej * a,const secp256k1_ge * b)494 static void secp256k1_gej_add_ge(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b) {
495 /* Operations: 7 mul, 5 sqr, 4 normalize, 21 mul_int/add/negate/cmov */
496 static const secp256k1_fe fe_1 = SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 1);
497 secp256k1_fe zz, u1, u2, s1, s2, t, tt, m, n, q, rr;
498 secp256k1_fe m_alt, rr_alt;
499 int infinity, degenerate;
500 VERIFY_CHECK(!b->infinity);
501 VERIFY_CHECK(a->infinity == 0 || a->infinity == 1);
502
503 /** In:
504 * Eric Brier and Marc Joye, Weierstrass Elliptic Curves and Side-Channel Attacks.
505 * In D. Naccache and P. Paillier, Eds., Public Key Cryptography, vol. 2274 of Lecture Notes in Computer Science, pages 335-345. Springer-Verlag, 2002.
506 * we find as solution for a unified addition/doubling formula:
507 * lambda = ((x1 + x2)^2 - x1 * x2 + a) / (y1 + y2), with a = 0 for secp256k1's curve equation.
508 * x3 = lambda^2 - (x1 + x2)
509 * 2*y3 = lambda * (x1 + x2 - 2 * x3) - (y1 + y2).
510 *
511 * Substituting x_i = Xi / Zi^2 and yi = Yi / Zi^3, for i=1,2,3, gives:
512 * U1 = X1*Z2^2, U2 = X2*Z1^2
513 * S1 = Y1*Z2^3, S2 = Y2*Z1^3
514 * Z = Z1*Z2
515 * T = U1+U2
516 * M = S1+S2
517 * Q = T*M^2
518 * R = T^2-U1*U2
519 * X3 = 4*(R^2-Q)
520 * Y3 = 4*(R*(3*Q-2*R^2)-M^4)
521 * Z3 = 2*M*Z
522 * (Note that the paper uses xi = Xi / Zi and yi = Yi / Zi instead.)
523 *
524 * This formula has the benefit of being the same for both addition
525 * of distinct points and doubling. However, it breaks down in the
526 * case that either point is infinity, or that y1 = -y2. We handle
527 * these cases in the following ways:
528 *
529 * - If b is infinity we simply bail by means of a VERIFY_CHECK.
530 *
531 * - If a is infinity, we detect this, and at the end of the
532 * computation replace the result (which will be meaningless,
533 * but we compute to be constant-time) with b.x : b.y : 1.
534 *
535 * - If a = -b, we have y1 = -y2, which is a degenerate case.
536 * But here the answer is infinity, so we simply set the
537 * infinity flag of the result, overriding the computed values
538 * without even needing to cmov.
539 *
540 * - If y1 = -y2 but x1 != x2, which does occur thanks to certain
541 * properties of our curve (specifically, 1 has nontrivial cube
542 * roots in our field, and the curve equation has no x coefficient)
543 * then the answer is not infinity but also not given by the above
544 * equation. In this case, we cmov in place an alternate expression
545 * for lambda. Specifically (y1 - y2)/(x1 - x2). Where both these
546 * expressions for lambda are defined, they are equal, and can be
547 * obtained from each other by multiplication by (y1 + y2)/(y1 + y2)
548 * then substitution of x^3 + 7 for y^2 (using the curve equation).
549 * For all pairs of nonzero points (a, b) at least one is defined,
550 * so this covers everything.
551 */
552
553 secp256k1_fe_sqr(&zz, &a->z); /* z = Z1^2 */
554 u1 = a->x; secp256k1_fe_normalize_weak(&u1); /* u1 = U1 = X1*Z2^2 (1) */
555 secp256k1_fe_mul(&u2, &b->x, &zz); /* u2 = U2 = X2*Z1^2 (1) */
556 s1 = a->y; secp256k1_fe_normalize_weak(&s1); /* s1 = S1 = Y1*Z2^3 (1) */
557 secp256k1_fe_mul(&s2, &b->y, &zz); /* s2 = Y2*Z1^2 (1) */
558 secp256k1_fe_mul(&s2, &s2, &a->z); /* s2 = S2 = Y2*Z1^3 (1) */
559 t = u1; secp256k1_fe_add(&t, &u2); /* t = T = U1+U2 (2) */
560 m = s1; secp256k1_fe_add(&m, &s2); /* m = M = S1+S2 (2) */
561 secp256k1_fe_sqr(&rr, &t); /* rr = T^2 (1) */
562 secp256k1_fe_negate(&m_alt, &u2, 1); /* Malt = -X2*Z1^2 */
563 secp256k1_fe_mul(&tt, &u1, &m_alt); /* tt = -U1*U2 (2) */
564 secp256k1_fe_add(&rr, &tt); /* rr = R = T^2-U1*U2 (3) */
565 /** If lambda = R/M = 0/0 we have a problem (except in the "trivial"
566 * case that Z = z1z2 = 0, and this is special-cased later on). */
567 degenerate = secp256k1_fe_normalizes_to_zero(&m) &
568 secp256k1_fe_normalizes_to_zero(&rr);
569 /* This only occurs when y1 == -y2 and x1^3 == x2^3, but x1 != x2.
570 * This means either x1 == beta*x2 or beta*x1 == x2, where beta is
571 * a nontrivial cube root of one. In either case, an alternate
572 * non-indeterminate expression for lambda is (y1 - y2)/(x1 - x2),
573 * so we set R/M equal to this. */
574 rr_alt = s1;
575 secp256k1_fe_mul_int(&rr_alt, 2); /* rr = Y1*Z2^3 - Y2*Z1^3 (2) */
576 secp256k1_fe_add(&m_alt, &u1); /* Malt = X1*Z2^2 - X2*Z1^2 */
577
578 secp256k1_fe_cmov(&rr_alt, &rr, !degenerate);
579 secp256k1_fe_cmov(&m_alt, &m, !degenerate);
580 /* Now Ralt / Malt = lambda and is guaranteed not to be 0/0.
581 * From here on out Ralt and Malt represent the numerator
582 * and denominator of lambda; R and M represent the explicit
583 * expressions x1^2 + x2^2 + x1x2 and y1 + y2. */
584 secp256k1_fe_sqr(&n, &m_alt); /* n = Malt^2 (1) */
585 secp256k1_fe_mul(&q, &n, &t); /* q = Q = T*Malt^2 (1) */
586 /* These two lines use the observation that either M == Malt or M == 0,
587 * so M^3 * Malt is either Malt^4 (which is computed by squaring), or
588 * zero (which is "computed" by cmov). So the cost is one squaring
589 * versus two multiplications. */
590 secp256k1_fe_sqr(&n, &n);
591 secp256k1_fe_cmov(&n, &m, degenerate); /* n = M^3 * Malt (2) */
592 secp256k1_fe_sqr(&t, &rr_alt); /* t = Ralt^2 (1) */
593 secp256k1_fe_mul(&r->z, &a->z, &m_alt); /* r->z = Malt*Z (1) */
594 infinity = secp256k1_fe_normalizes_to_zero(&r->z) * (1 - a->infinity);
595 secp256k1_fe_mul_int(&r->z, 2); /* r->z = Z3 = 2*Malt*Z (2) */
596 secp256k1_fe_negate(&q, &q, 1); /* q = -Q (2) */
597 secp256k1_fe_add(&t, &q); /* t = Ralt^2-Q (3) */
598 secp256k1_fe_normalize_weak(&t);
599 r->x = t; /* r->x = Ralt^2-Q (1) */
600 secp256k1_fe_mul_int(&t, 2); /* t = 2*x3 (2) */
601 secp256k1_fe_add(&t, &q); /* t = 2*x3 - Q: (4) */
602 secp256k1_fe_mul(&t, &t, &rr_alt); /* t = Ralt*(2*x3 - Q) (1) */
603 secp256k1_fe_add(&t, &n); /* t = Ralt*(2*x3 - Q) + M^3*Malt (3) */
604 secp256k1_fe_negate(&r->y, &t, 3); /* r->y = Ralt*(Q - 2x3) - M^3*Malt (4) */
605 secp256k1_fe_normalize_weak(&r->y);
606 secp256k1_fe_mul_int(&r->x, 4); /* r->x = X3 = 4*(Ralt^2-Q) */
607 secp256k1_fe_mul_int(&r->y, 4); /* r->y = Y3 = 4*Ralt*(Q - 2x3) - 4*M^3*Malt (4) */
608
609 /** In case a->infinity == 1, replace r with (b->x, b->y, 1). */
610 secp256k1_fe_cmov(&r->x, &b->x, a->infinity);
611 secp256k1_fe_cmov(&r->y, &b->y, a->infinity);
612 secp256k1_fe_cmov(&r->z, &fe_1, a->infinity);
613 r->infinity = infinity;
614 }
615
secp256k1_gej_rescale(secp256k1_gej * r,const secp256k1_fe * s)616 static void secp256k1_gej_rescale(secp256k1_gej *r, const secp256k1_fe *s) {
617 /* Operations: 4 mul, 1 sqr */
618 secp256k1_fe zz;
619 VERIFY_CHECK(!secp256k1_fe_is_zero(s));
620 secp256k1_fe_sqr(&zz, s);
621 secp256k1_fe_mul(&r->x, &r->x, &zz); /* r->x *= s^2 */
622 secp256k1_fe_mul(&r->y, &r->y, &zz);
623 secp256k1_fe_mul(&r->y, &r->y, s); /* r->y *= s^3 */
624 secp256k1_fe_mul(&r->z, &r->z, s); /* r->z *= s */
625 }
626
secp256k1_ge_to_storage(secp256k1_ge_storage * r,const secp256k1_ge * a)627 static void secp256k1_ge_to_storage(secp256k1_ge_storage *r, const secp256k1_ge *a) {
628 secp256k1_fe x, y;
629 VERIFY_CHECK(!a->infinity);
630 x = a->x;
631 secp256k1_fe_normalize(&x);
632 y = a->y;
633 secp256k1_fe_normalize(&y);
634 secp256k1_fe_to_storage(&r->x, &x);
635 secp256k1_fe_to_storage(&r->y, &y);
636 }
637
secp256k1_ge_from_storage(secp256k1_ge * r,const secp256k1_ge_storage * a)638 static void secp256k1_ge_from_storage(secp256k1_ge *r, const secp256k1_ge_storage *a) {
639 secp256k1_fe_from_storage(&r->x, &a->x);
640 secp256k1_fe_from_storage(&r->y, &a->y);
641 r->infinity = 0;
642 }
643
secp256k1_ge_storage_cmov(secp256k1_ge_storage * r,const secp256k1_ge_storage * a,int flag)644 static SECP256K1_INLINE void secp256k1_ge_storage_cmov(secp256k1_ge_storage *r, const secp256k1_ge_storage *a, int flag) {
645 secp256k1_fe_storage_cmov(&r->x, &a->x, flag);
646 secp256k1_fe_storage_cmov(&r->y, &a->y, flag);
647 }
648
secp256k1_ge_mul_lambda(secp256k1_ge * r,const secp256k1_ge * a)649 static void secp256k1_ge_mul_lambda(secp256k1_ge *r, const secp256k1_ge *a) {
650 static const secp256k1_fe beta = SECP256K1_FE_CONST(
651 0x7ae96a2bul, 0x657c0710ul, 0x6e64479eul, 0xac3434e9ul,
652 0x9cf04975ul, 0x12f58995ul, 0xc1396c28ul, 0x719501eeul
653 );
654 *r = *a;
655 secp256k1_fe_mul(&r->x, &r->x, &beta);
656 }
657
secp256k1_gej_has_quad_y_var(const secp256k1_gej * a)658 static int secp256k1_gej_has_quad_y_var(const secp256k1_gej *a) {
659 secp256k1_fe yz;
660
661 if (a->infinity) {
662 return 0;
663 }
664
665 /* We rely on the fact that the Jacobi symbol of 1 / a->z^3 is the same as
666 * that of a->z. Thus a->y / a->z^3 is a quadratic residue iff a->y * a->z
667 is */
668 secp256k1_fe_mul(&yz, &a->y, &a->z);
669 return secp256k1_fe_is_quad_var(&yz);
670 }
671
secp256k1_ge_is_in_correct_subgroup(const secp256k1_ge * ge)672 static int secp256k1_ge_is_in_correct_subgroup(const secp256k1_ge* ge) {
673 #ifdef EXHAUSTIVE_TEST_ORDER
674 secp256k1_gej out;
675 int i;
676
677 /* A very simple EC multiplication ladder that avoids a dependecy on ecmult. */
678 secp256k1_gej_set_infinity(&out);
679 for (i = 0; i < 32; ++i) {
680 secp256k1_gej_double_var(&out, &out, NULL);
681 if ((((uint32_t)EXHAUSTIVE_TEST_ORDER) >> (31 - i)) & 1) {
682 secp256k1_gej_add_ge_var(&out, &out, ge, NULL);
683 }
684 }
685 return secp256k1_gej_is_infinity(&out);
686 #else
687 (void)ge;
688 /* The real secp256k1 group has cofactor 1, so the subgroup is the entire curve. */
689 return 1;
690 #endif
691 }
692
693 #endif /* SECP256K1_GROUP_IMPL_H */
694