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