Debian Codegraph @

src/crypto/crypto_ops_builder/crypto-ops-old.c

raw symbols SCIP
1 // Copyright (c) 2014-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30
31
32 #include <stdint.h> //standard
33 #include <assert.h> //standard
34
35 //#include "warnings.h" //where is this??
36 #include "crypto-ops.h" //fine
37
38 //DISABLE_VS_WARNINGS(4146 4244)
39
40 /* Predeclarations */
41
42 static void fe_mul(fe, const fe, const fe);
43 static void fe_sq(fe, const fe);
44 static void fe_tobytes(unsigned char *, const fe);
45 static void ge_madd(ge_p1p1 *, const ge_p3 *, const ge_precomp *);
46 static void ge_msub(ge_p1p1 *, const ge_p3 *, const ge_precomp *);
47 static void ge_p2_0(ge_p2 *);
48 static void ge_p3_dbl(ge_p1p1 *, const ge_p3 *);
49 static void ge_sub(ge_p1p1 *, const ge_p3 *, const ge_cached *);
50 static void fe_divpowm1(fe, const fe, const fe);
51
52 /* Common functions */
53
54 //x
55 //u
56 static uint64_t load_3(const unsigned char *in) {
57 uint64_t result;
58 result = (uint64_t) in[0];
59 result |= ((uint64_t) in[1]) << 8;
60 result |= ((uint64_t) in[2]) << 16;
61 return result;
62 }
63
64 //x
65 //u
66 static uint64_t load_4(const unsigned char *in)
67 {
68 uint64_t result;
69 result = (uint64_t) in[0];
70 result |= ((uint64_t) in[1]) << 8;
71 result |= ((uint64_t) in[2]) << 16;
72 result |= ((uint64_t) in[3]) << 24;
73 return result;
74 }
75
76 /* From fe_0.c */
77
78 /*
79 h = 0
80 */
81
82 //x
83 static void fe_0(fe h) {
84 h[0] = 0;
85 h[1] = 0;
86 h[2] = 0;
87 h[3] = 0;
88 h[4] = 0;
89 h[5] = 0;
90 h[6] = 0;
91 h[7] = 0;
92 h[8] = 0;
93 h[9] = 0;
94 }
95
96 /* From fe_1.c */
97
98 /*
99 h = 1
100 */
101
102 //x
103 static void fe_1(fe h) {
104 h[0] = 1;
105 h[1] = 0;
106 h[2] = 0;
107 h[3] = 0;
108 h[4] = 0;
109 h[5] = 0;
110 h[6] = 0;
111 h[7] = 0;
112 h[8] = 0;
113 h[9] = 0;
114 }
115
116 /* From fe_add.c */
117
118 /*
119 h = f + g
120 Can overlap h with f or g.
121
122 Preconditions:
123 |f| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc.
124 |g| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc.
125
126 Postconditions:
127 |h| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc.
128 */
129
130 static void fe_add(fe h, const fe f, const fe g) {
131 int32_t f0 = f[0];
132 int32_t f1 = f[1];
133 int32_t f2 = f[2];
134 int32_t f3 = f[3];
135 int32_t f4 = f[4];
136 int32_t f5 = f[5];
137 int32_t f6 = f[6];
138 int32_t f7 = f[7];
139 int32_t f8 = f[8];
140 int32_t f9 = f[9];
141 int32_t g0 = g[0];
142 int32_t g1 = g[1];
143 int32_t g2 = g[2];
144 int32_t g3 = g[3];
145 int32_t g4 = g[4];
146 int32_t g5 = g[5];
147 int32_t g6 = g[6];
148 int32_t g7 = g[7];
149 int32_t g8 = g[8];
150 int32_t g9 = g[9];
151 int32_t h0 = f0 + g0;
152 int32_t h1 = f1 + g1;
153 int32_t h2 = f2 + g2;
154 int32_t h3 = f3 + g3;
155 int32_t h4 = f4 + g4;
156 int32_t h5 = f5 + g5;
157 int32_t h6 = f6 + g6;
158 int32_t h7 = f7 + g7;
159 int32_t h8 = f8 + g8;
160 int32_t h9 = f9 + g9;
161 h[0] = h0;
162 h[1] = h1;
163 h[2] = h2;
164 h[3] = h3;
165 h[4] = h4;
166 h[5] = h5;
167 h[6] = h6;
168 h[7] = h7;
169 h[8] = h8;
170 h[9] = h9;
171 }
172
173 /* From fe_cmov.c */
174
175 /*
176 Replace (f,g) with (g,g) if b == 1;
177 replace (f,g) with (f,g) if b == 0.
178
179 Preconditions: b in {0,1}.
180 */
181
182 //x
183 static void fe_cmov(fe f, const fe g, unsigned int b) {
184 int32_t f0 = f[0];
185 int32_t f1 = f[1];
186 int32_t f2 = f[2];
187 int32_t f3 = f[3];
188 int32_t f4 = f[4];
189 int32_t f5 = f[5];
190 int32_t f6 = f[6];
191 int32_t f7 = f[7];
192 int32_t f8 = f[8];
193 int32_t f9 = f[9];
194 int32_t g0 = g[0];
195 int32_t g1 = g[1];
196 int32_t g2 = g[2];
197 int32_t g3 = g[3];
198 int32_t g4 = g[4];
199 int32_t g5 = g[5];
200 int32_t g6 = g[6];
201 int32_t g7 = g[7];
202 int32_t g8 = g[8];
203 int32_t g9 = g[9];
204 int32_t x0 = f0 ^ g0;
205 int32_t x1 = f1 ^ g1;
206 int32_t x2 = f2 ^ g2;
207 int32_t x3 = f3 ^ g3;
208 int32_t x4 = f4 ^ g4;
209 int32_t x5 = f5 ^ g5;
210 int32_t x6 = f6 ^ g6;
211 int32_t x7 = f7 ^ g7;
212 int32_t x8 = f8 ^ g8;
213 int32_t x9 = f9 ^ g9;
214 assert((((b - 1) & ~b) | ((b - 2) & ~(b - 1))) == (unsigned int) -1);
215 b = -b;
216 x0 &= b;
217 x1 &= b;
218 x2 &= b;
219 x3 &= b;
220 x4 &= b;
221 x5 &= b;
222 x6 &= b;
223 x7 &= b;
224 x8 &= b;
225 x9 &= b;
226 f[0] = f0 ^ x0;
227 f[1] = f1 ^ x1;
228 f[2] = f2 ^ x2;
229 f[3] = f3 ^ x3;
230 f[4] = f4 ^ x4;
231 f[5] = f5 ^ x5;
232 f[6] = f6 ^ x6;
233 f[7] = f7 ^ x7;
234 f[8] = f8 ^ x8;
235 f[9] = f9 ^ x9;
236 }
237
238 /* From fe_copy.c */
239
240 /*
241 h = f
242 */
243
244 //x
245 static void fe_copy(fe h, const fe f) {
246 int32_t f0 = f[0];
247 int32_t f1 = f[1];
248 int32_t f2 = f[2];
249 int32_t f3 = f[3];
250 int32_t f4 = f[4];
251 int32_t f5 = f[5];
252 int32_t f6 = f[6];
253 int32_t f7 = f[7];
254 int32_t f8 = f[8];
255 int32_t f9 = f[9];
256 h[0] = f0;
257 h[1] = f1;
258 h[2] = f2;
259 h[3] = f3;
260 h[4] = f4;
261 h[5] = f5;
262 h[6] = f6;
263 h[7] = f7;
264 h[8] = f8;
265 h[9] = f9;
266 }
267
268 /* From fe_invert.c */
269
270 //x using fermat
271 static void fe_invert(fe out, const fe z) {
272 fe t0;
273 fe t1;
274 fe t2;
275 fe t3;
276 int i;
277
278 fe_sq(t0, z);
279 fe_sq(t1, t0);
280 fe_sq(t1, t1);
281 fe_mul(t1, z, t1);
282 fe_mul(t0, t0, t1);
283 fe_sq(t2, t0);
284 fe_mul(t1, t1, t2);
285 fe_sq(t2, t1);
286 for (i = 0; i < 4; ++i) {
287 fe_sq(t2, t2);
288 }
289 fe_mul(t1, t2, t1);
290 fe_sq(t2, t1);
291 for (i = 0; i < 9; ++i) {
292 fe_sq(t2, t2);
293 }
294 fe_mul(t2, t2, t1);
295 fe_sq(t3, t2);
296 for (i = 0; i < 19; ++i) {
297 fe_sq(t3, t3);
298 }
299 fe_mul(t2, t3, t2);
300 fe_sq(t2, t2);
301 for (i = 0; i < 9; ++i) {
302 fe_sq(t2, t2);
303 }
304 fe_mul(t1, t2, t1);
305 fe_sq(t2, t1);
306 for (i = 0; i < 49; ++i) {
307 fe_sq(t2, t2);
308 }
309 fe_mul(t2, t2, t1);
310 fe_sq(t3, t2);
311 for (i = 0; i < 99; ++i) {
312 fe_sq(t3, t3);
313 }
314 fe_mul(t2, t3, t2);
315 fe_sq(t2, t2);
316 for (i = 0; i < 49; ++i) {
317 fe_sq(t2, t2);
318 }
319 fe_mul(t1, t2, t1);
320 fe_sq(t1, t1);
321 for (i = 0; i < 4; ++i) {
322 fe_sq(t1, t1);
323 }
324 fe_mul(out, t1, t0);
325
326 return;
327 }
328
329 /* From fe_isnegative.c */
330
331 /*
332 return 1 if f is in {1,3,5,...,q-2}
333 return 0 if f is in {0,2,4,...,q-1}
334
335 Preconditions:
336 |f| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc.
337 */
338
339 //x
340 static int fe_isnegative(const fe f) {
341 unsigned char s[32];
342 fe_tobytes(s, f);
343 return s[0] & 1;
344 }
345
346 /* From fe_isnonzero.c, modified */
347
348 //x
349 static int fe_isnonzero(const fe f) {
350 unsigned char s[32];
351 fe_tobytes(s, f);
352 return (((int) (s[0] | s[1] | s[2] | s[3] | s[4] | s[5] | s[6] | s[7] | s[8] |
353 s[9] | s[10] | s[11] | s[12] | s[13] | s[14] | s[15] | s[16] | s[17] |
354 s[18] | s[19] | s[20] | s[21] | s[22] | s[23] | s[24] | s[25] | s[26] |
355 s[27] | s[28] | s[29] | s[30] | s[31]) - 1) >> 8) + 1;
356 }
357
358 /* From fe_mul.c */
359
360 /*
361 h = f * g
362 Can overlap h with f or g.
363
364 Preconditions:
365 |f| bounded by 1.65*2^26,1.65*2^25,1.65*2^26,1.65*2^25,etc.
366 |g| bounded by 1.65*2^26,1.65*2^25,1.65*2^26,1.65*2^25,etc.
367
368 Postconditions:
369 |h| bounded by 1.01*2^25,1.01*2^24,1.01*2^25,1.01*2^24,etc.
370 */
371
372 /*
373 Notes on implementation strategy:
374
375 Using schoolbook multiplication.
376 Karatsuba would save a little in some cost models.
377
378 Most multiplications by 2 and 19 are 32-bit precomputations;
379 cheaper than 64-bit postcomputations.
380
381 There is one remaining multiplication by 19 in the carry chain;
382 one *19 precomputation can be merged into this,
383 but the resulting data flow is considerably less clean.
384
385 There are 12 carries below.
386 10 of them are 2-way parallelizable and vectorizable.
387 Can get away with 11 carries, but then data flow is much deeper.
388
389 With tighter constraints on inputs can squeeze carries into int32.
390 */
391
392 static void fe_mul(fe h, const fe f, const fe g) {
393 int32_t f0 = f[0];
394 int32_t f1 = f[1];
395 int32_t f2 = f[2];
396 int32_t f3 = f[3];
397 int32_t f4 = f[4];
398 int32_t f5 = f[5];
399 int32_t f6 = f[6];
400 int32_t f7 = f[7];
401 int32_t f8 = f[8];
402 int32_t f9 = f[9];
403 int32_t g0 = g[0];
404 int32_t g1 = g[1];
405 int32_t g2 = g[2];
406 int32_t g3 = g[3];
407 int32_t g4 = g[4];
408 int32_t g5 = g[5];
409 int32_t g6 = g[6];
410 int32_t g7 = g[7];
411 int32_t g8 = g[8];
412 int32_t g9 = g[9];
413 int32_t g1_19 = 19 * g1; /* 1.959375*2^29 */
414 int32_t g2_19 = 19 * g2; /* 1.959375*2^30; still ok */
415 int32_t g3_19 = 19 * g3;
416 int32_t g4_19 = 19 * g4;
417 int32_t g5_19 = 19 * g5;
418 int32_t g6_19 = 19 * g6;
419 int32_t g7_19 = 19 * g7;
420 int32_t g8_19 = 19 * g8;
421 int32_t g9_19 = 19 * g9;
422 int32_t f1_2 = 2 * f1;
423 int32_t f3_2 = 2 * f3;
424 int32_t f5_2 = 2 * f5;
425 int32_t f7_2 = 2 * f7;
426 int32_t f9_2 = 2 * f9;
427 int64_t f0g0 = f0 * (int64_t) g0;
428 int64_t f0g1 = f0 * (int64_t) g1;
429 int64_t f0g2 = f0 * (int64_t) g2;
430 int64_t f0g3 = f0 * (int64_t) g3;
431 int64_t f0g4 = f0 * (int64_t) g4;
432 int64_t f0g5 = f0 * (int64_t) g5;
433 int64_t f0g6 = f0 * (int64_t) g6;
434 int64_t f0g7 = f0 * (int64_t) g7;
435 int64_t f0g8 = f0 * (int64_t) g8;
436 int64_t f0g9 = f0 * (int64_t) g9;
437 int64_t f1g0 = f1 * (int64_t) g0;
438 int64_t f1g1_2 = f1_2 * (int64_t) g1;
439 int64_t f1g2 = f1 * (int64_t) g2;
440 int64_t f1g3_2 = f1_2 * (int64_t) g3;
441 int64_t f1g4 = f1 * (int64_t) g4;
442 int64_t f1g5_2 = f1_2 * (int64_t) g5;
443 int64_t f1g6 = f1 * (int64_t) g6;
444 int64_t f1g7_2 = f1_2 * (int64_t) g7;
445 int64_t f1g8 = f1 * (int64_t) g8;
446 int64_t f1g9_38 = f1_2 * (int64_t) g9_19;
447 int64_t f2g0 = f2 * (int64_t) g0;
448 int64_t f2g1 = f2 * (int64_t) g1;
449 int64_t f2g2 = f2 * (int64_t) g2;
450 int64_t f2g3 = f2 * (int64_t) g3;
451 int64_t f2g4 = f2 * (int64_t) g4;
452 int64_t f2g5 = f2 * (int64_t) g5;
453 int64_t f2g6 = f2 * (int64_t) g6;
454 int64_t f2g7 = f2 * (int64_t) g7;
455 int64_t f2g8_19 = f2 * (int64_t) g8_19;
456 int64_t f2g9_19 = f2 * (int64_t) g9_19;
457 int64_t f3g0 = f3 * (int64_t) g0;
458 int64_t f3g1_2 = f3_2 * (int64_t) g1;
459 int64_t f3g2 = f3 * (int64_t) g2;
460 int64_t f3g3_2 = f3_2 * (int64_t) g3;
461 int64_t f3g4 = f3 * (int64_t) g4;
462 int64_t f3g5_2 = f3_2 * (int64_t) g5;
463 int64_t f3g6 = f3 * (int64_t) g6;
464 int64_t f3g7_38 = f3_2 * (int64_t) g7_19;
465 int64_t f3g8_19 = f3 * (int64_t) g8_19;
466 int64_t f3g9_38 = f3_2 * (int64_t) g9_19;
467 int64_t f4g0 = f4 * (int64_t) g0;
468 int64_t f4g1 = f4 * (int64_t) g1;
469 int64_t f4g2 = f4 * (int64_t) g2;
470 int64_t f4g3 = f4 * (int64_t) g3;
471 int64_t f4g4 = f4 * (int64_t) g4;
472 int64_t f4g5 = f4 * (int64_t) g5;
473 int64_t f4g6_19 = f4 * (int64_t) g6_19;
474 int64_t f4g7_19 = f4 * (int64_t) g7_19;
475 int64_t f4g8_19 = f4 * (int64_t) g8_19;
476 int64_t f4g9_19 = f4 * (int64_t) g9_19;
477 int64_t f5g0 = f5 * (int64_t) g0;
478 int64_t f5g1_2 = f5_2 * (int64_t) g1;
479 int64_t f5g2 = f5 * (int64_t) g2;
480 int64_t f5g3_2 = f5_2 * (int64_t) g3;
481 int64_t f5g4 = f5 * (int64_t) g4;
482 int64_t f5g5_38 = f5_2 * (int64_t) g5_19;
483 int64_t f5g6_19 = f5 * (int64_t) g6_19;
484 int64_t f5g7_38 = f5_2 * (int64_t) g7_19;
485 int64_t f5g8_19 = f5 * (int64_t) g8_19;
486 int64_t f5g9_38 = f5_2 * (int64_t) g9_19;
487 int64_t f6g0 = f6 * (int64_t) g0;
488 int64_t f6g1 = f6 * (int64_t) g1;
489 int64_t f6g2 = f6 * (int64_t) g2;
490 int64_t f6g3 = f6 * (int64_t) g3;
491 int64_t f6g4_19 = f6 * (int64_t) g4_19;
492 int64_t f6g5_19 = f6 * (int64_t) g5_19;
493 int64_t f6g6_19 = f6 * (int64_t) g6_19;
494 int64_t f6g7_19 = f6 * (int64_t) g7_19;
495 int64_t f6g8_19 = f6 * (int64_t) g8_19;
496 int64_t f6g9_19 = f6 * (int64_t) g9_19;
497 int64_t f7g0 = f7 * (int64_t) g0;
498 int64_t f7g1_2 = f7_2 * (int64_t) g1;
499 int64_t f7g2 = f7 * (int64_t) g2;
500 int64_t f7g3_38 = f7_2 * (int64_t) g3_19;
501 int64_t f7g4_19 = f7 * (int64_t) g4_19;
502 int64_t f7g5_38 = f7_2 * (int64_t) g5_19;
503 int64_t f7g6_19 = f7 * (int64_t) g6_19;
504 int64_t f7g7_38 = f7_2 * (int64_t) g7_19;
505 int64_t f7g8_19 = f7 * (int64_t) g8_19;
506 int64_t f7g9_38 = f7_2 * (int64_t) g9_19;
507 int64_t f8g0 = f8 * (int64_t) g0;
508 int64_t f8g1 = f8 * (int64_t) g1;
509 int64_t f8g2_19 = f8 * (int64_t) g2_19;
510 int64_t f8g3_19 = f8 * (int64_t) g3_19;
511 int64_t f8g4_19 = f8 * (int64_t) g4_19;
512 int64_t f8g5_19 = f8 * (int64_t) g5_19;
513 int64_t f8g6_19 = f8 * (int64_t) g6_19;
514 int64_t f8g7_19 = f8 * (int64_t) g7_19;
515 int64_t f8g8_19 = f8 * (int64_t) g8_19;
516 int64_t f8g9_19 = f8 * (int64_t) g9_19;
517 int64_t f9g0 = f9 * (int64_t) g0;
518 int64_t f9g1_38 = f9_2 * (int64_t) g1_19;
519 int64_t f9g2_19 = f9 * (int64_t) g2_19;
520 int64_t f9g3_38 = f9_2 * (int64_t) g3_19;
521 int64_t f9g4_19 = f9 * (int64_t) g4_19;
522 int64_t f9g5_38 = f9_2 * (int64_t) g5_19;
523 int64_t f9g6_19 = f9 * (int64_t) g6_19;
524 int64_t f9g7_38 = f9_2 * (int64_t) g7_19;
525 int64_t f9g8_19 = f9 * (int64_t) g8_19;
526 int64_t f9g9_38 = f9_2 * (int64_t) g9_19;
527 int64_t h0 = f0g0+f1g9_38+f2g8_19+f3g7_38+f4g6_19+f5g5_38+f6g4_19+f7g3_38+f8g2_19+f9g1_38;
528 int64_t h1 = f0g1+f1g0 +f2g9_19+f3g8_19+f4g7_19+f5g6_19+f6g5_19+f7g4_19+f8g3_19+f9g2_19;
529 int64_t h2 = f0g2+f1g1_2 +f2g0 +f3g9_38+f4g8_19+f5g7_38+f6g6_19+f7g5_38+f8g4_19+f9g3_38;
530 int64_t h3 = f0g3+f1g2 +f2g1 +f3g0 +f4g9_19+f5g8_19+f6g7_19+f7g6_19+f8g5_19+f9g4_19;
531 int64_t h4 = f0g4+f1g3_2 +f2g2 +f3g1_2 +f4g0 +f5g9_38+f6g8_19+f7g7_38+f8g6_19+f9g5_38;
532 int64_t h5 = f0g5+f1g4 +f2g3 +f3g2 +f4g1 +f5g0 +f6g9_19+f7g8_19+f8g7_19+f9g6_19;
533 int64_t h6 = f0g6+f1g5_2 +f2g4 +f3g3_2 +f4g2 +f5g1_2 +f6g0 +f7g9_38+f8g8_19+f9g7_38;
534 int64_t h7 = f0g7+f1g6 +f2g5 +f3g4 +f4g3 +f5g2 +f6g1 +f7g0 +f8g9_19+f9g8_19;
535 int64_t h8 = f0g8+f1g7_2 +f2g6 +f3g5_2 +f4g4 +f5g3_2 +f6g2 +f7g1_2 +f8g0 +f9g9_38;
536 int64_t h9 = f0g9+f1g8 +f2g7 +f3g6 +f4g5 +f5g4 +f6g3 +f7g2 +f8g1 +f9g0 ;
537 int64_t carry0;
538 int64_t carry1;
539 int64_t carry2;
540 int64_t carry3;
541 int64_t carry4;
542 int64_t carry5;
543 int64_t carry6;
544 int64_t carry7;
545 int64_t carry8;
546 int64_t carry9;
547
548 /*
549 |h0| <= (1.65*1.65*2^52*(1+19+19+19+19)+1.65*1.65*2^50*(38+38+38+38+38))
550 i.e. |h0| <= 1.4*2^60; narrower ranges for h2, h4, h6, h8
551 |h1| <= (1.65*1.65*2^51*(1+1+19+19+19+19+19+19+19+19))
552 i.e. |h1| <= 1.7*2^59; narrower ranges for h3, h5, h7, h9
553 */
554
555 carry0 = (h0 + (int64_t) (1<<25)) >> 26; h1 += carry0; h0 -= carry0 << 26;
556 carry4 = (h4 + (int64_t) (1<<25)) >> 26; h5 += carry4; h4 -= carry4 << 26;
557 /* |h0| <= 2^25 */
558 /* |h4| <= 2^25 */
559 /* |h1| <= 1.71*2^59 */
560 /* |h5| <= 1.71*2^59 */
561
562 carry1 = (h1 + (int64_t) (1<<24)) >> 25; h2 += carry1; h1 -= carry1 << 25;
563 carry5 = (h5 + (int64_t) (1<<24)) >> 25; h6 += carry5; h5 -= carry5 << 25;
564 /* |h1| <= 2^24; from now on fits into int32 */
565 /* |h5| <= 2^24; from now on fits into int32 */
566 /* |h2| <= 1.41*2^60 */
567 /* |h6| <= 1.41*2^60 */
568
569 carry2 = (h2 + (int64_t) (1<<25)) >> 26; h3 += carry2; h2 -= carry2 << 26;
570 carry6 = (h6 + (int64_t) (1<<25)) >> 26; h7 += carry6; h6 -= carry6 << 26;
571 /* |h2| <= 2^25; from now on fits into int32 unchanged */
572 /* |h6| <= 2^25; from now on fits into int32 unchanged */
573 /* |h3| <= 1.71*2^59 */
574 /* |h7| <= 1.71*2^59 */
575
576 carry3 = (h3 + (int64_t) (1<<24)) >> 25; h4 += carry3; h3 -= carry3 << 25;
577 carry7 = (h7 + (int64_t) (1<<24)) >> 25; h8 += carry7; h7 -= carry7 << 25;
578 /* |h3| <= 2^24; from now on fits into int32 unchanged */
579 /* |h7| <= 2^24; from now on fits into int32 unchanged */
580 /* |h4| <= 1.72*2^34 */
581 /* |h8| <= 1.41*2^60 */
582
583 carry4 = (h4 + (int64_t) (1<<25)) >> 26; h5 += carry4; h4 -= carry4 << 26;
584 carry8 = (h8 + (int64_t) (1<<25)) >> 26; h9 += carry8; h8 -= carry8 << 26;
585 /* |h4| <= 2^25; from now on fits into int32 unchanged */
586 /* |h8| <= 2^25; from now on fits into int32 unchanged */
587 /* |h5| <= 1.01*2^24 */
588 /* |h9| <= 1.71*2^59 */
589
590 carry9 = (h9 + (int64_t) (1<<24)) >> 25; h0 += carry9 * 19; h9 -= carry9 << 25;
591 /* |h9| <= 2^24; from now on fits into int32 unchanged */
592 /* |h0| <= 1.1*2^39 */
593
594 carry0 = (h0 + (int64_t) (1<<25)) >> 26; h1 += carry0; h0 -= carry0 << 26;
595 /* |h0| <= 2^25; from now on fits into int32 unchanged */
596 /* |h1| <= 1.01*2^24 */
597
598 h[0] = h0;
599 h[1] = h1;
600 h[2] = h2;
601 h[3] = h3;
602 h[4] = h4;
603 h[5] = h5;
604 h[6] = h6;
605 h[7] = h7;
606 h[8] = h8;
607 h[9] = h9;
608 }
609
610 /* From fe_neg.c */
611
612 /*
613 h = -f
614
615 Preconditions:
616 |f| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc.
617
618 Postconditions:
619 |h| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc.
620 */
621
622 //x
623 static void fe_neg(fe h, const fe f) {
624 int32_t f0 = f[0];
625 int32_t f1 = f[1];
626 int32_t f2 = f[2];
627 int32_t f3 = f[3];
628 int32_t f4 = f[4];
629 int32_t f5 = f[5];
630 int32_t f6 = f[6];
631 int32_t f7 = f[7];
632 int32_t f8 = f[8];
633 int32_t f9 = f[9];
634 int32_t h0 = -f0;
635 int32_t h1 = -f1;
636 int32_t h2 = -f2;
637 int32_t h3 = -f3;
638 int32_t h4 = -f4;
639 int32_t h5 = -f5;
640 int32_t h6 = -f6;
641 int32_t h7 = -f7;
642 int32_t h8 = -f8;
643 int32_t h9 = -f9;
644 h[0] = h0;
645 h[1] = h1;
646 h[2] = h2;
647 h[3] = h3;
648 h[4] = h4;
649 h[5] = h5;
650 h[6] = h6;
651 h[7] = h7;
652 h[8] = h8;
653 h[9] = h9;
654 }
655
656 /* From fe_sq.c */
657
658 /*
659 h = f * f
660 Can overlap h with f.
661
662 Preconditions:
663 |f| bounded by 1.65*2^26,1.65*2^25,1.65*2^26,1.65*2^25,etc.
664
665 Postconditions:
666 |h| bounded by 1.01*2^25,1.01*2^24,1.01*2^25,1.01*2^24,etc.
667 */
668
669 /*
670 See fe_mul.c for discussion of implementation strategy.
671 */
672
673 static void fe_sq(fe h, const fe f) {
674 int32_t f0 = f[0];
675 int32_t f1 = f[1];
676 int32_t f2 = f[2];
677 int32_t f3 = f[3];
678 int32_t f4 = f[4];
679 int32_t f5 = f[5];
680 int32_t f6 = f[6];
681 int32_t f7 = f[7];
682 int32_t f8 = f[8];
683 int32_t f9 = f[9];
684 int32_t f0_2 = 2 * f0;
685 int32_t f1_2 = 2 * f1;
686 int32_t f2_2 = 2 * f2;
687 int32_t f3_2 = 2 * f3;
688 int32_t f4_2 = 2 * f4;
689 int32_t f5_2 = 2 * f5;
690 int32_t f6_2 = 2 * f6;
691 int32_t f7_2 = 2 * f7;
692 int32_t f5_38 = 38 * f5; /* 1.959375*2^30 */
693 int32_t f6_19 = 19 * f6; /* 1.959375*2^30 */
694 int32_t f7_38 = 38 * f7; /* 1.959375*2^30 */
695 int32_t f8_19 = 19 * f8; /* 1.959375*2^30 */
696 int32_t f9_38 = 38 * f9; /* 1.959375*2^30 */
697 int64_t f0f0 = f0 * (int64_t) f0;
698 int64_t f0f1_2 = f0_2 * (int64_t) f1;
699 int64_t f0f2_2 = f0_2 * (int64_t) f2;
700 int64_t f0f3_2 = f0_2 * (int64_t) f3;
701 int64_t f0f4_2 = f0_2 * (int64_t) f4;
702 int64_t f0f5_2 = f0_2 * (int64_t) f5;
703 int64_t f0f6_2 = f0_2 * (int64_t) f6;
704 int64_t f0f7_2 = f0_2 * (int64_t) f7;
705 int64_t f0f8_2 = f0_2 * (int64_t) f8;
706 int64_t f0f9_2 = f0_2 * (int64_t) f9;
707 int64_t f1f1_2 = f1_2 * (int64_t) f1;
708 int64_t f1f2_2 = f1_2 * (int64_t) f2;
709 int64_t f1f3_4 = f1_2 * (int64_t) f3_2;
710 int64_t f1f4_2 = f1_2 * (int64_t) f4;
711 int64_t f1f5_4 = f1_2 * (int64_t) f5_2;
712 int64_t f1f6_2 = f1_2 * (int64_t) f6;
713 int64_t f1f7_4 = f1_2 * (int64_t) f7_2;
714 int64_t f1f8_2 = f1_2 * (int64_t) f8;
715 int64_t f1f9_76 = f1_2 * (int64_t) f9_38;
716 int64_t f2f2 = f2 * (int64_t) f2;
717 int64_t f2f3_2 = f2_2 * (int64_t) f3;
718 int64_t f2f4_2 = f2_2 * (int64_t) f4;
719 int64_t f2f5_2 = f2_2 * (int64_t) f5;
720 int64_t f2f6_2 = f2_2 * (int64_t) f6;
721 int64_t f2f7_2 = f2_2 * (int64_t) f7;
722 int64_t f2f8_38 = f2_2 * (int64_t) f8_19;
723 int64_t f2f9_38 = f2 * (int64_t) f9_38;
724 int64_t f3f3_2 = f3_2 * (int64_t) f3;
725 int64_t f3f4_2 = f3_2 * (int64_t) f4;
726 int64_t f3f5_4 = f3_2 * (int64_t) f5_2;
727 int64_t f3f6_2 = f3_2 * (int64_t) f6;
728 int64_t f3f7_76 = f3_2 * (int64_t) f7_38;
729 int64_t f3f8_38 = f3_2 * (int64_t) f8_19;
730 int64_t f3f9_76 = f3_2 * (int64_t) f9_38;
731 int64_t f4f4 = f4 * (int64_t) f4;
732 int64_t f4f5_2 = f4_2 * (int64_t) f5;
733 int64_t f4f6_38 = f4_2 * (int64_t) f6_19;
734 int64_t f4f7_38 = f4 * (int64_t) f7_38;
735 int64_t f4f8_38 = f4_2 * (int64_t) f8_19;
736 int64_t f4f9_38 = f4 * (int64_t) f9_38;
737 int64_t f5f5_38 = f5 * (int64_t) f5_38;
738 int64_t f5f6_38 = f5_2 * (int64_t) f6_19;
739 int64_t f5f7_76 = f5_2 * (int64_t) f7_38;
740 int64_t f5f8_38 = f5_2 * (int64_t) f8_19;
741 int64_t f5f9_76 = f5_2 * (int64_t) f9_38;
742 int64_t f6f6_19 = f6 * (int64_t) f6_19;
743 int64_t f6f7_38 = f6 * (int64_t) f7_38;
744 int64_t f6f8_38 = f6_2 * (int64_t) f8_19;
745 int64_t f6f9_38 = f6 * (int64_t) f9_38;
746 int64_t f7f7_38 = f7 * (int64_t) f7_38;
747 int64_t f7f8_38 = f7_2 * (int64_t) f8_19;
748 int64_t f7f9_76 = f7_2 * (int64_t) f9_38;
749 int64_t f8f8_19 = f8 * (int64_t) f8_19;
750 int64_t f8f9_38 = f8 * (int64_t) f9_38;
751 int64_t f9f9_38 = f9 * (int64_t) f9_38;
752 int64_t h0 = f0f0 +f1f9_76+f2f8_38+f3f7_76+f4f6_38+f5f5_38;
753 int64_t h1 = f0f1_2+f2f9_38+f3f8_38+f4f7_38+f5f6_38;
754 int64_t h2 = f0f2_2+f1f1_2 +f3f9_76+f4f8_38+f5f7_76+f6f6_19;
755 int64_t h3 = f0f3_2+f1f2_2 +f4f9_38+f5f8_38+f6f7_38;
756 int64_t h4 = f0f4_2+f1f3_4 +f2f2 +f5f9_76+f6f8_38+f7f7_38;
757 int64_t h5 = f0f5_2+f1f4_2 +f2f3_2 +f6f9_38+f7f8_38;
758 int64_t h6 = f0f6_2+f1f5_4 +f2f4_2 +f3f3_2 +f7f9_76+f8f8_19;
759 int64_t h7 = f0f7_2+f1f6_2 +f2f5_2 +f3f4_2 +f8f9_38;
760 int64_t h8 = f0f8_2+f1f7_4 +f2f6_2 +f3f5_4 +f4f4 +f9f9_38;
761 int64_t h9 = f0f9_2+f1f8_2 +f2f7_2 +f3f6_2 +f4f5_2;
762 int64_t carry0;
763 int64_t carry1;
764 int64_t carry2;
765 int64_t carry3;
766 int64_t carry4;
767 int64_t carry5;
768 int64_t carry6;
769 int64_t carry7;
770 int64_t carry8;
771 int64_t carry9;
772
773 carry0 = (h0 + (int64_t) (1<<25)) >> 26; h1 += carry0; h0 -= carry0 << 26;
774 carry4 = (h4 + (int64_t) (1<<25)) >> 26; h5 += carry4; h4 -= carry4 << 26;
775
776 carry1 = (h1 + (int64_t) (1<<24)) >> 25; h2 += carry1; h1 -= carry1 << 25;
777 carry5 = (h5 + (int64_t) (1<<24)) >> 25; h6 += carry5; h5 -= carry5 << 25;
778
779 carry2 = (h2 + (int64_t) (1<<25)) >> 26; h3 += carry2; h2 -= carry2 << 26;
780 carry6 = (h6 + (int64_t) (1<<25)) >> 26; h7 += carry6; h6 -= carry6 << 26;
781
782 carry3 = (h3 + (int64_t) (1<<24)) >> 25; h4 += carry3; h3 -= carry3 << 25;
783 carry7 = (h7 + (int64_t) (1<<24)) >> 25; h8 += carry7; h7 -= carry7 << 25;
784
785 carry4 = (h4 + (int64_t) (1<<25)) >> 26; h5 += carry4; h4 -= carry4 << 26;
786 carry8 = (h8 + (int64_t) (1<<25)) >> 26; h9 += carry8; h8 -= carry8 << 26;
787
788 carry9 = (h9 + (int64_t) (1<<24)) >> 25; h0 += carry9 * 19; h9 -= carry9 << 25;
789
790 carry0 = (h0 + (int64_t) (1<<25)) >> 26; h1 += carry0; h0 -= carry0 << 26;
791
792 h[0] = h0;
793 h[1] = h1;
794 h[2] = h2;
795 h[3] = h3;
796 h[4] = h4;
797 h[5] = h5;
798 h[6] = h6;
799 h[7] = h7;
800 h[8] = h8;
801 h[9] = h9;
802 }
803
804 /* From fe_sq2.c */
805
806 /*
807 h = 2 * f * f
808 Can overlap h with f.
809
810 Preconditions:
811 |f| bounded by 1.65*2^26,1.65*2^25,1.65*2^26,1.65*2^25,etc.
812
813 Postconditions:
814 |h| bounded by 1.01*2^25,1.01*2^24,1.01*2^25,1.01*2^24,etc.
815 */
816
817 /*
818 See fe_mul.c for discussion of implementation strategy.
819 */
820
821 static void fe_sq2(fe h, const fe f) {
822 int32_t f0 = f[0];
823 int32_t f1 = f[1];
824 int32_t f2 = f[2];
825 int32_t f3 = f[3];
826 int32_t f4 = f[4];
827 int32_t f5 = f[5];
828 int32_t f6 = f[6];
829 int32_t f7 = f[7];
830 int32_t f8 = f[8];
831 int32_t f9 = f[9];
832 int32_t f0_2 = 2 * f0;
833 int32_t f1_2 = 2 * f1;
834 int32_t f2_2 = 2 * f2;
835 int32_t f3_2 = 2 * f3;
836 int32_t f4_2 = 2 * f4;
837 int32_t f5_2 = 2 * f5;
838 int32_t f6_2 = 2 * f6;
839 int32_t f7_2 = 2 * f7;
840 int32_t f5_38 = 38 * f5; /* 1.959375*2^30 */
841 int32_t f6_19 = 19 * f6; /* 1.959375*2^30 */
842 int32_t f7_38 = 38 * f7; /* 1.959375*2^30 */
843 int32_t f8_19 = 19 * f8; /* 1.959375*2^30 */
844 int32_t f9_38 = 38 * f9; /* 1.959375*2^30 */
845 int64_t f0f0 = f0 * (int64_t) f0;
846 int64_t f0f1_2 = f0_2 * (int64_t) f1;
847 int64_t f0f2_2 = f0_2 * (int64_t) f2;
848 int64_t f0f3_2 = f0_2 * (int64_t) f3;
849 int64_t f0f4_2 = f0_2 * (int64_t) f4;
850 int64_t f0f5_2 = f0_2 * (int64_t) f5;
851 int64_t f0f6_2 = f0_2 * (int64_t) f6;
852 int64_t f0f7_2 = f0_2 * (int64_t) f7;
853 int64_t f0f8_2 = f0_2 * (int64_t) f8;
854 int64_t f0f9_2 = f0_2 * (int64_t) f9;
855 int64_t f1f1_2 = f1_2 * (int64_t) f1;
856 int64_t f1f2_2 = f1_2 * (int64_t) f2;
857 int64_t f1f3_4 = f1_2 * (int64_t) f3_2;
858 int64_t f1f4_2 = f1_2 * (int64_t) f4;
859 int64_t f1f5_4 = f1_2 * (int64_t) f5_2;
860 int64_t f1f6_2 = f1_2 * (int64_t) f6;
861 int64_t f1f7_4 = f1_2 * (int64_t) f7_2;
862 int64_t f1f8_2 = f1_2 * (int64_t) f8;
863 int64_t f1f9_76 = f1_2 * (int64_t) f9_38;
864 int64_t f2f2 = f2 * (int64_t) f2;
865 int64_t f2f3_2 = f2_2 * (int64_t) f3;
866 int64_t f2f4_2 = f2_2 * (int64_t) f4;
867 int64_t f2f5_2 = f2_2 * (int64_t) f5;
868 int64_t f2f6_2 = f2_2 * (int64_t) f6;
869 int64_t f2f7_2 = f2_2 * (int64_t) f7;
870 int64_t f2f8_38 = f2_2 * (int64_t) f8_19;
871 int64_t f2f9_38 = f2 * (int64_t) f9_38;
872 int64_t f3f3_2 = f3_2 * (int64_t) f3;
873 int64_t f3f4_2 = f3_2 * (int64_t) f4;
874 int64_t f3f5_4 = f3_2 * (int64_t) f5_2;
875 int64_t f3f6_2 = f3_2 * (int64_t) f6;
876 int64_t f3f7_76 = f3_2 * (int64_t) f7_38;
877 int64_t f3f8_38 = f3_2 * (int64_t) f8_19;
878 int64_t f3f9_76 = f3_2 * (int64_t) f9_38;
879 int64_t f4f4 = f4 * (int64_t) f4;
880 int64_t f4f5_2 = f4_2 * (int64_t) f5;
881 int64_t f4f6_38 = f4_2 * (int64_t) f6_19;
882 int64_t f4f7_38 = f4 * (int64_t) f7_38;
883 int64_t f4f8_38 = f4_2 * (int64_t) f8_19;
884 int64_t f4f9_38 = f4 * (int64_t) f9_38;
885 int64_t f5f5_38 = f5 * (int64_t) f5_38;
886 int64_t f5f6_38 = f5_2 * (int64_t) f6_19;
887 int64_t f5f7_76 = f5_2 * (int64_t) f7_38;
888 int64_t f5f8_38 = f5_2 * (int64_t) f8_19;
889 int64_t f5f9_76 = f5_2 * (int64_t) f9_38;
890 int64_t f6f6_19 = f6 * (int64_t) f6_19;
891 int64_t f6f7_38 = f6 * (int64_t) f7_38;
892 int64_t f6f8_38 = f6_2 * (int64_t) f8_19;
893 int64_t f6f9_38 = f6 * (int64_t) f9_38;
894 int64_t f7f7_38 = f7 * (int64_t) f7_38;
895 int64_t f7f8_38 = f7_2 * (int64_t) f8_19;
896 int64_t f7f9_76 = f7_2 * (int64_t) f9_38;
897 int64_t f8f8_19 = f8 * (int64_t) f8_19;
898 int64_t f8f9_38 = f8 * (int64_t) f9_38;
899 int64_t f9f9_38 = f9 * (int64_t) f9_38;
900 int64_t h0 = f0f0 +f1f9_76+f2f8_38+f3f7_76+f4f6_38+f5f5_38;
901 int64_t h1 = f0f1_2+f2f9_38+f3f8_38+f4f7_38+f5f6_38;
902 int64_t h2 = f0f2_2+f1f1_2 +f3f9_76+f4f8_38+f5f7_76+f6f6_19;
903 int64_t h3 = f0f3_2+f1f2_2 +f4f9_38+f5f8_38+f6f7_38;
904 int64_t h4 = f0f4_2+f1f3_4 +f2f2 +f5f9_76+f6f8_38+f7f7_38;
905 int64_t h5 = f0f5_2+f1f4_2 +f2f3_2 +f6f9_38+f7f8_38;
906 int64_t h6 = f0f6_2+f1f5_4 +f2f4_2 +f3f3_2 +f7f9_76+f8f8_19;
907 int64_t h7 = f0f7_2+f1f6_2 +f2f5_2 +f3f4_2 +f8f9_38;
908 int64_t h8 = f0f8_2+f1f7_4 +f2f6_2 +f3f5_4 +f4f4 +f9f9_38;
909 int64_t h9 = f0f9_2+f1f8_2 +f2f7_2 +f3f6_2 +f4f5_2;
910 int64_t carry0;
911 int64_t carry1;
912 int64_t carry2;
913 int64_t carry3;
914 int64_t carry4;
915 int64_t carry5;
916 int64_t carry6;
917 int64_t carry7;
918 int64_t carry8;
919 int64_t carry9;
920
921 h0 += h0;
922 h1 += h1;
923 h2 += h2;
924 h3 += h3;
925 h4 += h4;
926 h5 += h5;
927 h6 += h6;
928 h7 += h7;
929 h8 += h8;
930 h9 += h9;
931
932 carry0 = (h0 + (int64_t) (1<<25)) >> 26; h1 += carry0; h0 -= carry0 << 26;
933 carry4 = (h4 + (int64_t) (1<<25)) >> 26; h5 += carry4; h4 -= carry4 << 26;
934
935 carry1 = (h1 + (int64_t) (1<<24)) >> 25; h2 += carry1; h1 -= carry1 << 25;
936 carry5 = (h5 + (int64_t) (1<<24)) >> 25; h6 += carry5; h5 -= carry5 << 25;
937
938 carry2 = (h2 + (int64_t) (1<<25)) >> 26; h3 += carry2; h2 -= carry2 << 26;
939 carry6 = (h6 + (int64_t) (1<<25)) >> 26; h7 += carry6; h6 -= carry6 << 26;
940
941 carry3 = (h3 + (int64_t) (1<<24)) >> 25; h4 += carry3; h3 -= carry3 << 25;
942 carry7 = (h7 + (int64_t) (1<<24)) >> 25; h8 += carry7; h7 -= carry7 << 25;
943
944 carry4 = (h4 + (int64_t) (1<<25)) >> 26; h5 += carry4; h4 -= carry4 << 26;
945 carry8 = (h8 + (int64_t) (1<<25)) >> 26; h9 += carry8; h8 -= carry8 << 26;
946
947 carry9 = (h9 + (int64_t) (1<<24)) >> 25; h0 += carry9 * 19; h9 -= carry9 << 25;
948
949 carry0 = (h0 + (int64_t) (1<<25)) >> 26; h1 += carry0; h0 -= carry0 << 26;
950
951 h[0] = h0;
952 h[1] = h1;
953 h[2] = h2;
954 h[3] = h3;
955 h[4] = h4;
956 h[5] = h5;
957 h[6] = h6;
958 h[7] = h7;
959 h[8] = h8;
960 h[9] = h9;
961 }
962
963 /* From fe_sub.c */
964
965 /*
966 h = f - g
967 Can overlap h with f or g.
968
969 Preconditions:
970 |f| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc.
971 |g| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc.
972
973 Postconditions:
974 |h| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc.
975 */
976
977 //x
978 static void fe_sub(fe h, const fe f, const fe g) {
979 int32_t f0 = f[0];
980 int32_t f1 = f[1];
981 int32_t f2 = f[2];
982 int32_t f3 = f[3];
983 int32_t f4 = f[4];
984 int32_t f5 = f[5];
985 int32_t f6 = f[6];
986 int32_t f7 = f[7];
987 int32_t f8 = f[8];
988 int32_t f9 = f[9];
989 int32_t g0 = g[0];
990 int32_t g1 = g[1];
991 int32_t g2 = g[2];
992 int32_t g3 = g[3];
993 int32_t g4 = g[4];
994 int32_t g5 = g[5];
995 int32_t g6 = g[6];
996 int32_t g7 = g[7];
997 int32_t g8 = g[8];
998 int32_t g9 = g[9];
999 int32_t h0 = f0 - g0;
1000 int32_t h1 = f1 - g1;
1001 int32_t h2 = f2 - g2;
1002 int32_t h3 = f3 - g3;
1003 int32_t h4 = f4 - g4;
1004 int32_t h5 = f5 - g5;
1005 int32_t h6 = f6 - g6;
1006 int32_t h7 = f7 - g7;
1007 int32_t h8 = f8 - g8;
1008 int32_t h9 = f9 - g9;
1009 h[0] = h0;
1010 h[1] = h1;
1011 h[2] = h2;
1012 h[3] = h3;
1013 h[4] = h4;
1014 h[5] = h5;
1015 h[6] = h6;
1016 h[7] = h7;
1017 h[8] = h8;
1018 h[9] = h9;
1019 }
1020
1021 /* From fe_tobytes.c */
1022
1023 /*
1024 Preconditions:
1025 |h| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc.
1026
1027 Write p=2^255-19; q=floor(h/p).
1028 Basic claim: q = floor(2^(-255)(h + 19 2^(-25)h9 + 2^(-1))).
1029
1030 Proof:
1031 Have |h|<=p so |q|<=1 so |19^2 2^(-255) q|<1/4.
1032 Also have |h-2^230 h9|<2^231 so |19 2^(-255)(h-2^230 h9)|<1/4.
1033
1034 Write y=2^(-1)-19^2 2^(-255)q-19 2^(-255)(h-2^230 h9).
1035 Then 0<y<1.
1036
1037 Write r=h-pq.
1038 Have 0<=r<=p-1=2^255-20.
1039 Thus 0<=r+19(2^-255)r<r+19(2^-255)2^255<=2^255-1.
1040
1041 Write x=r+19(2^-255)r+y.
1042 Then 0<x<2^255 so floor(2^(-255)x) = 0 so floor(q+2^(-255)x) = q.
1043
1044 Have q+2^(-255)x = 2^(-255)(h + 19 2^(-25) h9 + 2^(-1))
1045 so floor(2^(-255)(h + 19 2^(-25) h9 + 2^(-1))) = q.
1046 */
1047
1048 static void fe_tobytes(unsigned char *s, const fe h) {
1049 int32_t h0 = h[0];
1050 int32_t h1 = h[1];
1051 int32_t h2 = h[2];
1052 int32_t h3 = h[3];
1053 int32_t h4 = h[4];
1054 int32_t h5 = h[5];
1055 int32_t h6 = h[6];
1056 int32_t h7 = h[7];
1057 int32_t h8 = h[8];
1058 int32_t h9 = h[9];
1059 int32_t q;
1060 int32_t carry0;
1061 int32_t carry1;
1062 int32_t carry2;
1063 int32_t carry3;
1064 int32_t carry4;
1065 int32_t carry5;
1066 int32_t carry6;
1067 int32_t carry7;
1068 int32_t carry8;
1069 int32_t carry9;
1070
1071 q = (19 * h9 + (((int32_t) 1) << 24)) >> 25;
1072 q = (h0 + q) >> 26;
1073 q = (h1 + q) >> 25;
1074 q = (h2 + q) >> 26;
1075 q = (h3 + q) >> 25;
1076 q = (h4 + q) >> 26;
1077 q = (h5 + q) >> 25;
1078 q = (h6 + q) >> 26;
1079 q = (h7 + q) >> 25;
1080 q = (h8 + q) >> 26;
1081 q = (h9 + q) >> 25;
1082
1083 /* Goal: Output h-(2^255-19)q, which is between 0 and 2^255-20. */
1084 h0 += 19 * q;
1085 /* Goal: Output h-2^255 q, which is between 0 and 2^255-20. */
1086
1087 carry0 = h0 >> 26; h1 += carry0; h0 -= carry0 << 26;
1088 carry1 = h1 >> 25; h2 += carry1; h1 -= carry1 << 25;
1089 carry2 = h2 >> 26; h3 += carry2; h2 -= carry2 << 26;
1090 carry3 = h3 >> 25; h4 += carry3; h3 -= carry3 << 25;
1091 carry4 = h4 >> 26; h5 += carry4; h4 -= carry4 << 26;
1092 carry5 = h5 >> 25; h6 += carry5; h5 -= carry5 << 25;
1093 carry6 = h6 >> 26; h7 += carry6; h6 -= carry6 << 26;
1094 carry7 = h7 >> 25; h8 += carry7; h7 -= carry7 << 25;
1095 carry8 = h8 >> 26; h9 += carry8; h8 -= carry8 << 26;
1096 carry9 = h9 >> 25; h9 -= carry9 << 25;
1097 /* h10 = carry9 */
1098
1099 /*
1100 Goal: Output h0+...+2^255 h10-2^255 q, which is between 0 and 2^255-20.
1101 Have h0+...+2^230 h9 between 0 and 2^255-1;
1102 evidently 2^255 h10-2^255 q = 0.
1103 Goal: Output h0+...+2^230 h9.
1104 */
1105
1106 s[0] = h0 >> 0;
1107 s[1] = h0 >> 8;
1108 s[2] = h0 >> 16;
1109 s[3] = (h0 >> 24) | (h1 << 2);
1110 s[4] = h1 >> 6;
1111 s[5] = h1 >> 14;
1112 s[6] = (h1 >> 22) | (h2 << 3);
1113 s[7] = h2 >> 5;
1114 s[8] = h2 >> 13;
1115 s[9] = (h2 >> 21) | (h3 << 5);
1116 s[10] = h3 >> 3;
1117 s[11] = h3 >> 11;
1118 s[12] = (h3 >> 19) | (h4 << 6);
1119 s[13] = h4 >> 2;
1120 s[14] = h4 >> 10;
1121 s[15] = h4 >> 18;
1122 s[16] = h5 >> 0;
1123 s[17] = h5 >> 8;
1124 s[18] = h5 >> 16;
1125 s[19] = (h5 >> 24) | (h6 << 1);
1126 s[20] = h6 >> 7;
1127 s[21] = h6 >> 15;
1128 s[22] = (h6 >> 23) | (h7 << 3);
1129 s[23] = h7 >> 5;
1130 s[24] = h7 >> 13;
1131 s[25] = (h7 >> 21) | (h8 << 4);
1132 s[26] = h8 >> 4;
1133 s[27] = h8 >> 12;
1134 s[28] = (h8 >> 20) | (h9 << 6);
1135 s[29] = h9 >> 2;
1136 s[30] = h9 >> 10;
1137 s[31] = h9 >> 18;
1138 }
1139
1140 /* From ge_add.c */
1141
1142 //x
1143 void ge_add(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q) {
1144 fe t0;
1145 fe_add(r->X, p->Y, p->X);
1146 fe_sub(r->Y, p->Y, p->X);
1147 fe_mul(r->Z, r->X, q->YplusX);
1148 fe_mul(r->Y, r->Y, q->YminusX);
1149 fe_mul(r->T, q->T2d, p->T);
1150 fe_mul(r->X, p->Z, q->Z);
1151 fe_add(t0, r->X, r->X);
1152 fe_sub(r->X, r->Z, r->Y);
1153 fe_add(r->Y, r->Z, r->Y);
1154 fe_add(r->Z, t0, r->T);
1155 fe_sub(r->T, t0, r->T);
1156 }
1157
1158 /* From ge_double_scalarmult.c, modified */
1159
1160 static void slide(signed char *r, const unsigned char *a) {
1161 int i;
1162 int b;
1163 int k;
1164
1165 //this loop just makes a binary string out of a
1166 //e.g. a = {3, 0, 0, ...} then r = 110000....
1167 for (i = 0; i < 256; ++i) {
1168 r[i] = 1 & (a[i >> 3] >> (i & 7));
1169 }
1170
1171 for (i = 0; i < 256; ++i) {
1172 if (r[i]) {
1173 for (b = 1; b <= 6 && i + b < 256; ++b) {
1174 if (r[i + b]) {
1175 if (r[i] + (r[i + b] << b) <= 15) {
1176 r[i] += r[i + b] << b; r[i + b] = 0;
1177 } else if (r[i] - (r[i + b] << b) >= -15) {
1178 r[i] -= r[i + b] << b;
1179 for (k = i + b; k < 256; ++k) {
1180 if (!r[k]) {
1181 r[k] = 1;
1182 break;
1183 }
1184 r[k] = 0;
1185 }
1186 } else
1187 break;
1188 }
1189 }
1190 }
1191 }
1192 }
1193
1194 //double scalar mult precomp
1195 void ge_dsm_precomp(ge_dsmp r, const ge_p3 *s) {
1196 ge_p1p1 t;
1197 ge_p3 s2, u;
1198 ge_p3_to_cached(&r[0], s);
1199 ge_p3_dbl(&t, s); ge_p1p1_to_p3(&s2, &t);
1200 ge_add(&t, &s2, &r[0]); ge_p1p1_to_p3(&u, &t); ge_p3_to_cached(&r[1], &u);
1201 ge_add(&t, &s2, &r[1]); ge_p1p1_to_p3(&u, &t); ge_p3_to_cached(&r[2], &u);
1202 ge_add(&t, &s2, &r[2]); ge_p1p1_to_p3(&u, &t); ge_p3_to_cached(&r[3], &u);
1203 ge_add(&t, &s2, &r[3]); ge_p1p1_to_p3(&u, &t); ge_p3_to_cached(&r[4], &u);
1204 ge_add(&t, &s2, &r[4]); ge_p1p1_to_p3(&u, &t); ge_p3_to_cached(&r[5], &u);
1205 ge_add(&t, &s2, &r[5]); ge_p1p1_to_p3(&u, &t); ge_p3_to_cached(&r[6], &u);
1206 ge_add(&t, &s2, &r[6]); ge_p1p1_to_p3(&u, &t); ge_p3_to_cached(&r[7], &u);
1207 }
1208
1209 /*
1210 r = a * A + b * B
1211 where a = a[0]+256*a[1]+...+256^31 a[31].
1212 and b = b[0]+256*b[1]+...+256^31 b[31].
1213 B is the Ed25519 base point (x,4/5) with x positive.
1214 */
1215
1216 //this is a really simple alg. actually
1217 void ge_double_scalarmult_base_vartime(ge_p2 *r, const unsigned char *a, const ge_p3 *A, const unsigned char *b) {
1218 signed char aslide[256];
1219 signed char bslide[256];
1220 ge_dsmp Ai; /* A, 3A, 5A, 7A, 9A, 11A, 13A, 15A */
1221 ge_p1p1 t;
1222 ge_p3 u;
1223 int i;
1224
1225 slide(aslide, a);
1226 slide(bslide, b);
1227 ge_dsm_precomp(Ai, A);/* A, 3A, 5A, 7A, 9A, 11A, 13A, 15A */
1228
1229 ge_p2_0(r);
1230
1231 for (i = 255; i >= 0; --i) {
1232 //so this gets us to the highest i such that both have a nonzero
1233 if (aslide[i] || bslide[i]) break;
1234 }
1235
1236 for (; i >= 0; --i) {
1237 ge_p2_dbl(&t, r);//simple double and add
1238
1239 if (aslide[i] > 0) {
1240 ge_p1p1_to_p3(&u, &t);
1241 ge_add(&t, &u, &Ai[aslide[i]/2]);
1242 } else if (aslide[i] < 0) {
1243 ge_p1p1_to_p3(&u, &t);
1244 ge_sub(&t, &u, &Ai[(-aslide[i])/2]);
1245 }
1246
1247 if (bslide[i] > 0) {
1248 ge_p1p1_to_p3(&u, &t);
1249 ge_madd(&t, &u, &ge_Bi[bslide[i]/2]);
1250 } else if (bslide[i] < 0) {
1251 ge_p1p1_to_p3(&u, &t);
1252 ge_msub(&t, &u, &ge_Bi[(-bslide[i])/2]);
1253 }
1254
1255 ge_p1p1_to_p2(r, &t);
1256 }
1257 }
1258
1259 /* From ge_frombytes.c, modified */
1260 //this is like xrecover ..
1261 //x
1262 int ge_frombytes_vartime(ge_p3 *h, const unsigned char *s) {
1263 fe u;
1264 fe v;
1265 fe vxx;
1266 fe check;
1267
1268 /* From fe_frombytes.c */
1269
1270 int64_t h0 = load_4(s);
1271 int64_t h1 = load_3(s + 4) << 6;
1272 int64_t h2 = load_3(s + 7) << 5;
1273 int64_t h3 = load_3(s + 10) << 3;
1274 int64_t h4 = load_3(s + 13) << 2;
1275 int64_t h5 = load_4(s + 16);
1276 int64_t h6 = load_3(s + 20) << 7;
1277 int64_t h7 = load_3(s + 23) << 5;
1278 int64_t h8 = load_3(s + 26) << 4;
1279 int64_t h9 = (load_3(s + 29) & 8388607) << 2;
1280 int64_t carry0;
1281 int64_t carry1;
1282 int64_t carry2;
1283 int64_t carry3;
1284 int64_t carry4;
1285 int64_t carry5;
1286 int64_t carry6;
1287 int64_t carry7;
1288 int64_t carry8;
1289 int64_t carry9;
1290
1291 /* Validate the number to be canonical */
1292 if (h9 == 33554428 && h8 == 268435440 && h7 == 536870880 && h6 == 2147483520 &&
1293 h5 == 4294967295 && h4 == 67108860 && h3 == 134217720 && h2 == 536870880 &&
1294 h1 == 1073741760 && h0 >= 4294967277) {
1295 return -1;
1296 }
1297
1298 carry9 = (h9 + (int64_t) (1<<24)) >> 25; h0 += carry9 * 19; h9 -= carry9 << 25;
1299 carry1 = (h1 + (int64_t) (1<<24)) >> 25; h2 += carry1; h1 -= carry1 << 25;
1300 carry3 = (h3 + (int64_t) (1<<24)) >> 25; h4 += carry3; h3 -= carry3 << 25;
1301 carry5 = (h5 + (int64_t) (1<<24)) >> 25; h6 += carry5; h5 -= carry5 << 25;
1302 carry7 = (h7 + (int64_t) (1<<24)) >> 25; h8 += carry7; h7 -= carry7 << 25;
1303
1304 carry0 = (h0 + (int64_t) (1<<25)) >> 26; h1 += carry0; h0 -= carry0 << 26;
1305 carry2 = (h2 + (int64_t) (1<<25)) >> 26; h3 += carry2; h2 -= carry2 << 26;
1306 carry4 = (h4 + (int64_t) (1<<25)) >> 26; h5 += carry4; h4 -= carry4 << 26;
1307 carry6 = (h6 + (int64_t) (1<<25)) >> 26; h7 += carry6; h6 -= carry6 << 26;
1308 carry8 = (h8 + (int64_t) (1<<25)) >> 26; h9 += carry8; h8 -= carry8 << 26;
1309
1310 h->Y[0] = h0;
1311 h->Y[1] = h1;
1312 h->Y[2] = h2;
1313 h->Y[3] = h3;
1314 h->Y[4] = h4;
1315 h->Y[5] = h5;
1316 h->Y[6] = h6;
1317 h->Y[7] = h7;
1318 h->Y[8] = h8;
1319 h->Y[9] = h9;
1320
1321 /* End fe_frombytes.c */
1322
1323 fe_1(h->Z);
1324 fe_sq(u, h->Y);
1325 fe_mul(v, u, fe_d);
1326 fe_sub(u, u, h->Z); /* u = y^2-1 */
1327 fe_add(v, v, h->Z); /* v = dy^2+1 */
1328
1329 fe_divpowm1(h->X, u, v); /* x = uv^3(uv^7)^((q-5)/8) */
1330
1331 fe_sq(vxx, h->X);
1332 fe_mul(vxx, vxx, v);
1333 fe_sub(check, vxx, u); /* vx^2-u */
1334 if (fe_isnonzero(check)) {
1335 fe_add(check, vxx, u); /* vx^2+u */
1336 if (fe_isnonzero(check)) {
1337 return -1;
1338 }
1339 fe_mul(h->X, h->X, fe_sqrtm1); //this is mapping X to X * sqrt(-1) c.f. 3.1 in hisil, dong, etc.
1340 }
1341
1342 if (fe_isnegative(h->X) != (s[31] >> 7)) {
1343 /* If x = 0, the sign must be positive */
1344 if (!fe_isnonzero(h->X)) {
1345 return -1;
1346 }
1347 fe_neg(h->X, h->X);
1348 }
1349
1350 fe_mul(h->T, h->X, h->Y);
1351 return 0;
1352 }
1353
1354 /* From ge_madd.c */
1355
1356 /*
1357 r = p + q
1358 */
1359
1360 //x
1361 static void ge_madd(ge_p1p1 *r, const ge_p3 *p, const ge_precomp *q) {
1362 fe t0;
1363 fe_add(r->X, p->Y, p->X);
1364 fe_sub(r->Y, p->Y, p->X);
1365 fe_mul(r->Z, r->X, q->yplusx);
1366 fe_mul(r->Y, r->Y, q->yminusx);
1367 fe_mul(r->T, q->xy2d, p->T);
1368 fe_add(t0, p->Z, p->Z);
1369 fe_sub(r->X, r->Z, r->Y);
1370 fe_add(r->Y, r->Z, r->Y);
1371 fe_add(r->Z, t0, r->T);
1372 fe_sub(r->T, t0, r->T);
1373 }
1374
1375 /* From ge_msub.c */
1376
1377 /*
1378 r = p - q
1379 */
1380
1381 //x
1382 static void ge_msub(ge_p1p1 *r, const ge_p3 *p, const ge_precomp *q) {
1383 fe t0;
1384 fe_add(r->X, p->Y, p->X);
1385 fe_sub(r->Y, p->Y, p->X);
1386 fe_mul(r->Z, r->X, q->yminusx);
1387 fe_mul(r->Y, r->Y, q->yplusx);
1388 fe_mul(r->T, q->xy2d, p->T);
1389 fe_add(t0, p->Z, p->Z);
1390 fe_sub(r->X, r->Z, r->Y);
1391 fe_add(r->Y, r->Z, r->Y);
1392 fe_sub(r->Z, t0, r->T);
1393 fe_add(r->T, t0, r->T);
1394 }
1395
1396 /* From ge_p1p1_to_p2.c */
1397
1398 /*
1399 r = p
1400 */
1401
1402 //x
1403 void ge_p1p1_to_p2(ge_p2 *r, const ge_p1p1 *p) {
1404 fe_mul(r->X, p->X, p->T);
1405 fe_mul(r->Y, p->Y, p->Z);
1406 fe_mul(r->Z, p->Z, p->T);
1407 }
1408
1409 /* From ge_p1p1_to_p3.c */
1410
1411 /*
1412 r = p
1413 */
1414
1415 //x
1416 void ge_p1p1_to_p3(ge_p3 *r, const ge_p1p1 *p) {
1417 fe_mul(r->X, p->X, p->T);
1418 fe_mul(r->Y, p->Y, p->Z);
1419 fe_mul(r->Z, p->Z, p->T);
1420 fe_mul(r->T, p->X, p->Y);
1421 }
1422
1423 /* From ge_p2_0.c */
1424
1425 //x
1426 static void ge_p2_0(ge_p2 *h) {
1427 fe_0(h->X);
1428 fe_1(h->Y);
1429 fe_1(h->Z);
1430 }
1431
1432 /* From ge_p2_dbl.c */
1433
1434 /*
1435 r = 2 * p
1436 */
1437
1438 //x
1439 void ge_p2_dbl(ge_p1p1 *r, const ge_p2 *p) {
1440 fe t0;
1441 fe_sq(r->X, p->X);
1442 fe_sq(r->Z, p->Y);
1443 fe_sq2(r->T, p->Z);
1444 fe_add(r->Y, p->X, p->Y);
1445 fe_sq(t0, r->Y);
1446 fe_add(r->Y, r->Z, r->X);
1447 fe_sub(r->Z, r->Z, r->X);
1448 fe_sub(r->X, t0, r->Y);
1449 fe_sub(r->T, r->T, r->Z);
1450 }
1451
1452 /* From ge_p3_0.c */
1453
1454 //x
1455 static void ge_p3_0(ge_p3 *h) {
1456 fe_0(h->X);
1457 fe_1(h->Y);
1458 fe_1(h->Z);
1459 fe_0(h->T);
1460 }
1461
1462 /* From ge_p3_dbl.c */
1463
1464 /*
1465 r = 2 * p
1466 */
1467
1468 //x
1469 static void ge_p3_dbl(ge_p1p1 *r, const ge_p3 *p) {
1470 ge_p2 q;
1471 ge_p3_to_p2(&q, p);
1472 ge_p2_dbl(r, &q);
1473 }
1474
1475 /* From ge_p3_to_cached.c */
1476
1477 /*
1478 r = p
1479 */
1480
1481 //x
1482 void ge_p3_to_cached(ge_cached *r, const ge_p3 *p) {
1483 fe_add(r->YplusX, p->Y, p->X);
1484 fe_sub(r->YminusX, p->Y, p->X);
1485 fe_copy(r->Z, p->Z);
1486 fe_mul(r->T2d, p->T, fe_d2);
1487 }
1488
1489 /* From ge_p3_to_p2.c */
1490
1491 /*
1492 r = p
1493 */
1494
1495 //x
1496 void ge_p3_to_p2(ge_p2 *r, const ge_p3 *p) {
1497 fe_copy(r->X, p->X);
1498 fe_copy(r->Y, p->Y);
1499 fe_copy(r->Z, p->Z);
1500 }
1501
1502 /* From ge_p3_tobytes.c */
1503
1504 //x
1505 void ge_p3_tobytes(unsigned char *s, const ge_p3 *h) {
1506 fe recip;
1507 fe x;
1508 fe y;
1509
1510 fe_invert(recip, h->Z);
1511 fe_mul(x, h->X, recip);
1512 fe_mul(y, h->Y, recip);
1513 fe_tobytes(s, y);
1514 s[31] ^= fe_isnegative(x) << 7;
1515 }
1516
1517 /* From ge_precomp_0.c */
1518
1519 //x
1520 static void ge_precomp_0(ge_precomp *h) {
1521 fe_1(h->yplusx);
1522 fe_1(h->yminusx);
1523 fe_0(h->xy2d);
1524 }
1525
1526 /* From ge_scalarmult_base.c */
1527
1528 //x
1529 static unsigned char equal(signed char b, signed char c) {
1530 unsigned char ub = b;
1531 unsigned char uc = c;
1532 unsigned char x = ub ^ uc; /* 0: yes; 1..255: no */
1533 uint32_t y = x; /* 0: yes; 1..255: no */
1534 y -= 1; /* 4294967295: yes; 0..254: no */
1535 y >>= 31; /* 1: yes; 0: no */
1536 return y;
1537 }
1538
1539 static unsigned char negative(signed char b) {
1540 unsigned long long x = b; /* 18446744073709551361..18446744073709551615: yes; 0..255: no */
1541 x >>= 63; /* 1: yes; 0: no */
1542 return x;
1543 }
1544
1545 static void ge_precomp_cmov(ge_precomp *t, const ge_precomp *u, unsigned char b) {
1546 fe_cmov(t->yplusx, u->yplusx, b);
1547 fe_cmov(t->yminusx, u->yminusx, b);
1548 fe_cmov(t->xy2d, u->xy2d, b);
1549 }
1550
1551 static void select(ge_precomp *t, int pos, signed char b) {
1552 ge_precomp minust;
1553 unsigned char bnegative = negative(b);
1554 unsigned char babs = b - (((-bnegative) & b) << 1);
1555
1556 ge_precomp_0(t);
1557 ge_precomp_cmov(t, &ge_base[pos][0], equal(babs, 1));
1558 ge_precomp_cmov(t, &ge_base[pos][1], equal(babs, 2));
1559 ge_precomp_cmov(t, &ge_base[pos][2], equal(babs, 3));
1560 ge_precomp_cmov(t, &ge_base[pos][3], equal(babs, 4));
1561 ge_precomp_cmov(t, &ge_base[pos][4], equal(babs, 5));
1562 ge_precomp_cmov(t, &ge_base[pos][5], equal(babs, 6));
1563 ge_precomp_cmov(t, &ge_base[pos][6], equal(babs, 7));
1564 ge_precomp_cmov(t, &ge_base[pos][7], equal(babs, 8));
1565 fe_copy(minust.yplusx, t->yminusx);
1566 fe_copy(minust.yminusx, t->yplusx);
1567 fe_neg(minust.xy2d, t->xy2d);
1568 ge_precomp_cmov(t, &minust, bnegative);
1569 }
1570
1571 /*
1572 h = a * B
1573 where a = a[0]+256*a[1]+...+256^31 a[31]
1574 B is the Ed25519 base point (x,4/5) with x positive.
1575
1576 Preconditions:
1577 a[31] <= 127
1578 */
1579
1580 void ge_scalarmult_base(ge_p3 *h, const unsigned char *a) {
1581 signed char e[64];
1582 signed char carry;
1583 ge_p1p1 r;
1584 ge_p2 s;
1585 ge_precomp t;
1586 int i;
1587
1588 for (i = 0; i < 32; ++i) {
1589 e[2 * i + 0] = (a[i] >> 0) & 15;
1590 e[2 * i + 1] = (a[i] >> 4) & 15;
1591 }
1592 /* each e[i] is between 0 and 15 */
1593 /* e[63] is between 0 and 7 */
1594
1595 carry = 0;
1596 for (i = 0; i < 63; ++i) {
1597 e[i] += carry;
1598 carry = e[i] + 8;
1599 carry >>= 4;
1600 e[i] -= carry << 4;
1601 }
1602 e[63] += carry;
1603 /* each e[i] is between -8 and 8 */
1604
1605 ge_p3_0(h);
1606 //note, these are indexing from 1, 3, 5, etc.
1607 for (i = 1; i < 64; i += 2) {
1608 select(&t, i / 2, e[i]); //here's where you grab multiples of the basepoint ..
1609 //so must be doing e P = (e[0] (2^4)^0 P + e[1] (2^4)^1 P + e[2] (2^4)^2 P + ...)
1610 //except, probably writing in big-endian to make more sense..
1611 //and the e[i] P are going to be pre-computed...
1612 //so have to wonder if by precomputing more, if can save time??
1613 ge_madd(&r, h, &t); ge_p1p1_to_p3(h, &r);
1614 }
1615
1616 ge_p3_dbl(&r, h); ge_p1p1_to_p2(&s, &r);
1617 ge_p2_dbl(&r, &s); ge_p1p1_to_p2(&s, &r);
1618 ge_p2_dbl(&r, &s); ge_p1p1_to_p2(&s, &r);
1619 ge_p2_dbl(&r, &s); ge_p1p1_to_p3(h, &r);
1620
1621 //note, these are indexing from 0, 2, 4, etc.
1622 for (i = 0; i < 64; i += 2) {
1623 select(&t, i / 2, e[i]);
1624 ge_madd(&r, h, &t); ge_p1p1_to_p3(h, &r);
1625 }
1626 }
1627
1628 /* From ge_sub.c */
1629
1630 /*
1631 r = p - q
1632 */
1633
1634 //x
1635 static void ge_sub(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q) {
1636 fe t0;
1637 fe_add(r->X, p->Y, p->X);
1638 fe_sub(r->Y, p->Y, p->X);
1639 fe_mul(r->Z, r->X, q->YminusX);
1640 fe_mul(r->Y, r->Y, q->YplusX);
1641 fe_mul(r->T, q->T2d, p->T);
1642 fe_mul(r->X, p->Z, q->Z);
1643 fe_add(t0, r->X, r->X);
1644 fe_sub(r->X, r->Z, r->Y);
1645 fe_add(r->Y, r->Z, r->Y);
1646 fe_sub(r->Z, t0, r->T);
1647 fe_add(r->T, t0, r->T);
1648 }
1649
1650 /* From ge_tobytes.c */
1651
1652 //x
1653 void ge_tobytes(unsigned char *s, const ge_p2 *h) {
1654 fe recip;
1655 fe x;
1656 fe y;
1657
1658 fe_invert(recip, h->Z);
1659 fe_mul(x, h->X, recip);
1660 fe_mul(y, h->Y, recip);
1661 fe_tobytes(s, y);
1662 s[31] ^= fe_isnegative(x) << 7;
1663 }
1664
1665 /* From sc_reduce.c */
1666
1667 /*
1668 Input:
1669 s[0]+256*s[1]+...+256^63*s[63] = s
1670
1671 Output:
1672 s[0]+256*s[1]+...+256^31*s[31] = s mod l
1673 where l = 2^252 + 27742317777372353535851937790883648493.
1674 Overwrites s in place.
1675 */
1676
1677 //x
1678 void sc_reduce(unsigned char *s) {
1679 //the point is to load in 21 bits at a time..
1680 //Mininero should give you the ints 2097151, the shifts below, and either load 3 or 4
1681 int64_t s0 = 2097151 & load_3(s); //2097151 is 21 1's in binary, so there are possible 5 bits left over..
1682 int64_t s1 = 2097151 & (load_4(s + 2) >> 5); //now you start at 2^16, so you have to move right 5 bits to 2^21.., the 2 comes from the fact that you loaded 3 last time..
1683 int64_t s2 = 2097151 & (load_3(s + 5) >> 2); //in last one you had bits 22 ->42, you are starting at (2^8)^5 = 2^40, so need to move right 2 bits...
1684 int64_t s3 = 2097151 & (load_4(s + 7) >> 7); //etc...
1685 int64_t s4 = 2097151 & (load_4(s + 10) >> 4); //ahh, these are "int 64;s"
1686 int64_t s5 = 2097151 & (load_3(s + 13) >> 1); //not uint 64s
1687 int64_t s6 = 2097151 & (load_4(s + 15) >> 6);
1688 int64_t s7 = 2097151 & (load_3(s + 18) >> 3);
1689 int64_t s8 = 2097151 & load_3(s + 21);
1690 int64_t s9 = 2097151 & (load_4(s + 23) >> 5);
1691 int64_t s10 = 2097151 & (load_3(s + 26) >> 2);
1692 int64_t s11 = 2097151 & (load_4(s + 28) >> 7);
1693 int64_t s12 = 2097151 & (load_4(s + 31) >> 4);
1694 int64_t s13 = 2097151 & (load_3(s + 34) >> 1);
1695 int64_t s14 = 2097151 & (load_4(s + 36) >> 6);
1696 int64_t s15 = 2097151 & (load_3(s + 39) >> 3);
1697 int64_t s16 = 2097151 & load_3(s + 42);
1698 int64_t s17 = 2097151 & (load_4(s + 44) >> 5);
1699 int64_t s18 = 2097151 & (load_3(s + 47) >> 2);
1700 int64_t s19 = 2097151 & (load_4(s + 49) >> 7);
1701 int64_t s20 = 2097151 & (load_4(s + 52) >> 4);
1702 int64_t s21 = 2097151 & (load_3(s + 55) >> 1);
1703 int64_t s22 = 2097151 & (load_4(s + 57) >> 6);
1704 int64_t s23 = (load_4(s + 60) >> 3); //note no &2097151 here ..
1705 //so rather than doing 24 = 512
1706 //we've done 23 = 504
1707 //so curve size rather than
1708 //size of s
1709 //this should be handled
1710 //at the end with those
1711 //two extra reductions
1712 int64_t carry0;
1713 int64_t carry1;
1714 int64_t carry2;
1715 int64_t carry3;
1716 int64_t carry4;
1717 int64_t carry5;
1718 int64_t carry6;
1719 int64_t carry7;
1720 int64_t carry8;
1721 int64_t carry9;
1722 int64_t carry10;
1723 int64_t carry11;
1724 int64_t carry12;
1725 int64_t carry13;
1726 int64_t carry14;
1727 int64_t carry15;
1728 int64_t carry16;
1729 //17 carries because 11, 12, ... 16 = #digits in l
1730
1731 //reduce first 105 bits.. to go from 504 to 399
1732 //Note that 683901 * 2^(5*21) - 136657 * 2^(4 * 21) + ... - 66643 = 27742317777372353535851937790883648493, which is c in the curve order..
1733 s11 += s23 * 666643; // =1430509 - 2 ^ 21 //note there is a 12 step difference, 12^21 = 252
1734 //so you subtract q 12^21 and then add c at each step..
1735 //just have to be careful of the carries ..
1736 //If you are using unsigned int's, you had better carry at each step
1737 //so the first step is to factorize 252 and whatever radix
1738 //must be in those factors..
1739 s12 += s23 * 470296; // = 1626855 - 2^21 - 1
1740 s13 += s23 * 654183; // = 1442968 - 2 ^ 21 - 1?
1741 s14 -= s23 * 997805; //note the negative ones are same as result from toDigits
1742 s15 += s23 * 136657; // 1960495 - 2 ** 21
1743 s16 -= s23 * 683901; //note negative ones are same
1744
1745 s10 += s22 * 666643; //22 - 10 = 12, 12 * 21 = 252
1746 s11 += s22 * 470296;
1747 s12 += s22 * 654183;
1748 s13 -= s22 * 997805;
1749 s14 += s22 * 136657;
1750 s15 -= s22 * 683901;
1751
1752 s9 += s21 * 666643; //21 - 9 = 12
1753 s10 += s21 * 470296;
1754 s11 += s21 * 654183;
1755 s12 -= s21 * 997805;
1756 s13 += s21 * 136657;
1757 s14 -= s21 * 683901;
1758
1759 s8 += s20 * 666643;
1760 s9 += s20 * 470296;
1761 s10 += s20 * 654183;
1762 s11 -= s20 * 997805;
1763 s12 += s20 * 136657;
1764 s13 -= s20 * 683901;
1765
1766 s7 += s19 * 666643;
1767 s8 += s19 * 470296;
1768 s9 += s19 * 654183;
1769 s10 -= s19 * 997805;
1770 s11 += s19 * 136657;
1771 s12 -= s19 * 683901;
1772
1773 s6 += s18 * 666643;
1774 s7 += s18 * 470296;
1775 s8 += s18 * 654183;
1776 s9 -= s18 * 997805;
1777 s10 += s18 * 136657;
1778 s11 -= s18 * 683901;
1779 //so we repeat this until we get down to s11
1780 //note that 11 * 21 = 231
1781
1782 //1st carry...
1783 //note span = 6 here
1784 carry6 = (s6 + (1<<20)) >> 21; s7 += carry6; s6 -= carry6 << 21;//take parts that are more than 21 bits in new s6 and add to s7..
1785 carry8 = (s8 + (1<<20)) >> 21; s9 += carry8; s8 -= carry8 << 21;
1786 carry10 = (s10 + (1<<20)) >> 21; s11 += carry10; s10 -= carry10 << 21;
1787 carry12 = (s12 + (1<<20)) >> 21; s13 += carry12; s12 -= carry12 << 21;
1788 carry14 = (s14 + (1<<20)) >> 21; s15 += carry14; s14 -= carry14 << 21;
1789 carry16 = (s16 + (1<<20)) >> 21; s17 += carry16; s16 -= carry16 << 21;
1790
1791 //span = 5 here
1792 carry7 = (s7 + (1<<20)) >> 21; s8 += carry7; s7 -= carry7 << 21;
1793 carry9 = (s9 + (1<<20)) >> 21; s10 += carry9; s9 -= carry9 << 21;
1794 carry11 = (s11 + (1<<20)) >> 21; s12 += carry11; s11 -= carry11 << 21;
1795 carry13 = (s13 + (1<<20)) >> 21; s14 += carry13; s13 -= carry13 << 21;
1796 carry15 = (s15 + (1<<20)) >> 21; s16 += carry15; s15 -= carry15 << 21;
1797
1798 //reduce 105 bits to go from 399 to 294
1799 s5 += s17 * 666643; //17 - 5 = 12 = 252/21
1800 s6 += s17 * 470296;
1801 s7 += s17 * 654183;
1802 s8 -= s17 * 997805;
1803 s9 += s17 * 136657;
1804 s10 -= s17 * 683901;
1805
1806 s4 += s16 * 666643;
1807 s5 += s16 * 470296;
1808 s6 += s16 * 654183;
1809 s7 -= s16 * 997805;
1810 s8 += s16 * 136657;
1811 s9 -= s16 * 683901;
1812
1813 s3 += s15 * 666643;
1814 s4 += s15 * 470296;
1815 s5 += s15 * 654183;
1816 s6 -= s15 * 997805;
1817 s7 += s15 * 136657;
1818 s8 -= s15 * 683901;
1819
1820 s2 += s14 * 666643;
1821 s3 += s14 * 470296;
1822 s4 += s14 * 654183;
1823 s5 -= s14 * 997805;
1824 s6 += s14 * 136657;
1825 s7 -= s14 * 683901;
1826
1827 s1 += s13 * 666643;
1828 s2 += s13 * 470296;
1829 s3 += s13 * 654183;
1830 s4 -= s13 * 997805;
1831 s5 += s13 * 136657;
1832 s6 -= s13 * 683901;
1833
1834 s0 += s12 * 666643; //12 - 0 = 12..
1835 s1 += s12 * 470296;
1836 s2 += s12 * 654183;
1837 s3 -= s12 * 997805;
1838 s4 += s12 * 136657;
1839 s5 -= s12 * 683901;
1840 s12 = 0;
1841
1842 //2nd carry
1843 carry0 = (s0 + (1<<20)) >> 21; s1 += carry0; s0 -= carry0 << 21;
1844 carry2 = (s2 + (1<<20)) >> 21; s3 += carry2; s2 -= carry2 << 21;
1845 carry4 = (s4 + (1<<20)) >> 21; s5 += carry4; s4 -= carry4 << 21;
1846 carry6 = (s6 + (1<<20)) >> 21; s7 += carry6; s6 -= carry6 << 21;
1847 carry8 = (s8 + (1<<20)) >> 21; s9 += carry8; s8 -= carry8 << 21;
1848 carry10 = (s10 + (1<<20)) >> 21; s11 += carry10; s10 -= carry10 << 21;
1849
1850 carry1 = (s1 + (1<<20)) >> 21; s2 += carry1; s1 -= carry1 << 21;
1851 carry3 = (s3 + (1<<20)) >> 21; s4 += carry3; s3 -= carry3 << 21;
1852 carry5 = (s5 + (1<<20)) >> 21; s6 += carry5; s5 -= carry5 << 21;
1853 carry7 = (s7 + (1<<20)) >> 21; s8 += carry7; s7 -= carry7 << 21;
1854 carry9 = (s9 + (1<<20)) >> 21; s10 += carry9; s9 -= carry9 << 21;
1855 carry11 = (s11 + (1<<20)) >> 21; s12 += carry11; s11 -= carry11 << 21;
1856
1857 //reduce another 21 bits... go from 294 to 273
1858 s0 += s12 * 666643;
1859 s1 += s12 * 470296;
1860 s2 += s12 * 654183;
1861 s3 -= s12 * 997805;
1862 s4 += s12 * 136657;
1863 s5 -= s12 * 683901;
1864 s12 = 0;
1865
1866 //3rd carry
1867 carry0 = s0 >> 21; s1 += carry0; s0 -= carry0 << 21;
1868 carry1 = s1 >> 21; s2 += carry1; s1 -= carry1 << 21;
1869 carry2 = s2 >> 21; s3 += carry2; s2 -= carry2 << 21;
1870 carry3 = s3 >> 21; s4 += carry3; s3 -= carry3 << 21;
1871 carry4 = s4 >> 21; s5 += carry4; s4 -= carry4 << 21;
1872 carry5 = s5 >> 21; s6 += carry5; s5 -= carry5 << 21;
1873 carry6 = s6 >> 21; s7 += carry6; s6 -= carry6 << 21;
1874 carry7 = s7 >> 21; s8 += carry7; s7 -= carry7 << 21;
1875 carry8 = s8 >> 21; s9 += carry8; s8 -= carry8 << 21;
1876 carry9 = s9 >> 21; s10 += carry9; s9 -= carry9 << 21;
1877 carry10 = s10 >> 21; s11 += carry10; s10 -= carry10 << 21;
1878 carry11 = s11 >> 21; s12 += carry11; s11 -= carry11 << 21;
1879
1880 //reduce last 21 bits to go from 273 to get to 2^252..
1881 s0 += s12 * 666643;
1882 s1 += s12 * 470296;
1883 s2 += s12 * 654183;
1884 s3 -= s12 * 997805;
1885 s4 += s12 * 136657;
1886 s5 -= s12 * 683901;
1887
1888 //carry
1889 carry0 = s0 >> 21; s1 += carry0; s0 -= carry0 << 21;
1890 carry1 = s1 >> 21; s2 += carry1; s1 -= carry1 << 21;
1891 carry2 = s2 >> 21; s3 += carry2; s2 -= carry2 << 21;
1892 carry3 = s3 >> 21; s4 += carry3; s3 -= carry3 << 21;
1893 carry4 = s4 >> 21; s5 += carry4; s4 -= carry4 << 21;
1894 carry5 = s5 >> 21; s6 += carry5; s5 -= carry5 << 21;
1895 carry6 = s6 >> 21; s7 += carry6; s6 -= carry6 << 21;
1896 carry7 = s7 >> 21; s8 += carry7; s7 -= carry7 << 21;
1897 carry8 = s8 >> 21; s9 += carry8; s8 -= carry8 << 21;
1898 carry9 = s9 >> 21; s10 += carry9; s9 -= carry9 << 21;
1899 carry10 = s10 >> 21; s11 += carry10; s10 -= carry10 << 21;
1900
1901 //go from 2^[256/12]'s to 2^[256/32]'s
1902 s[0] = s0 >> 0;
1903 s[1] = s0 >> 8;
1904 s[2] = (s0 >> 16) | (s1 << 5); // | since there is no overlap
1905 s[3] = s1 >> 3;
1906 s[4] = s1 >> 11;
1907 s[5] = (s1 >> 19) | (s2 << 2);
1908 s[6] = s2 >> 6;
1909 s[7] = (s2 >> 14) | (s3 << 7);
1910 s[8] = s3 >> 1;
1911 s[9] = s3 >> 9;
1912 s[10] = (s3 >> 17) | (s4 << 4);
1913 s[11] = s4 >> 4;
1914 s[12] = s4 >> 12;
1915 s[13] = (s4 >> 20) | (s5 << 1);
1916 s[14] = s5 >> 7;
1917 s[15] = (s5 >> 15) | (s6 << 6);
1918 s[16] = s6 >> 2;
1919 s[17] = s6 >> 10;
1920 s[18] = (s6 >> 18) | (s7 << 3);
1921 s[19] = s7 >> 5;
1922 s[20] = s7 >> 13;
1923 s[21] = s8 >> 0;
1924 s[22] = s8 >> 8;
1925 s[23] = (s8 >> 16) | (s9 << 5);
1926 s[24] = s9 >> 3;
1927 s[25] = s9 >> 11;
1928 s[26] = (s9 >> 19) | (s10 << 2);
1929 s[27] = s10 >> 6;
1930 s[28] = (s10 >> 14) | (s11 << 7);
1931 s[29] = s11 >> 1;
1932 s[30] = s11 >> 9;
1933 s[31] = s11 >> 17;
1934 }
1935
1936 /* New code */
1937 /* (u / v)^(m + 1) */
1938 static void fe_divpowm1(fe r, const fe u, const fe v) {
1939 fe v3, uv7, t0, t1, t2;
1940 int i;
1941
1942 fe_sq(v3, v);
1943 fe_mul(v3, v3, v); /* v3 = v^3 */
1944 fe_sq(uv7, v3);
1945 fe_mul(uv7, uv7, v);
1946 fe_mul(uv7, uv7, u); /* uv7 = uv^7 */
1947
1948 /*fe_pow22523(uv7, uv7);*/
1949
1950 /* From fe_pow22523.c */
1951
1952 fe_sq(t0, uv7);
1953 fe_sq(t1, t0);
1954 fe_sq(t1, t1);
1955 fe_mul(t1, uv7, t1);
1956 fe_mul(t0, t0, t1);
1957 fe_sq(t0, t0);
1958 fe_mul(t0, t1, t0);
1959 fe_sq(t1, t0);
1960 for (i = 0; i < 4; ++i) {
1961 fe_sq(t1, t1);
1962 }
1963 fe_mul(t0, t1, t0);
1964 fe_sq(t1, t0);
1965 for (i = 0; i < 9; ++i) {
1966 fe_sq(t1, t1);
1967 }
1968 fe_mul(t1, t1, t0);
1969 fe_sq(t2, t1);
1970 for (i = 0; i < 19; ++i) {
1971 fe_sq(t2, t2);
1972 }
1973 fe_mul(t1, t2, t1);
1974 for (i = 0; i < 10; ++i) {
1975 fe_sq(t1, t1);
1976 }
1977 fe_mul(t0, t1, t0);
1978 fe_sq(t1, t0);
1979 for (i = 0; i < 49; ++i) {
1980 fe_sq(t1, t1);
1981 }
1982 fe_mul(t1, t1, t0);
1983 fe_sq(t2, t1);
1984 for (i = 0; i < 99; ++i) {
1985 fe_sq(t2, t2);
1986 }
1987 fe_mul(t1, t2, t1);
1988 for (i = 0; i < 50; ++i) {
1989 fe_sq(t1, t1);
1990 }
1991 fe_mul(t0, t1, t0);
1992 fe_sq(t0, t0);
1993 fe_sq(t0, t0);
1994 fe_mul(t0, t0, uv7);
1995
1996 /* End fe_pow22523.c */
1997 /* t0 = (uv^7)^((q-5)/8) */
1998 fe_mul(t0, t0, v3);
1999 fe_mul(r, t0, u); /* u^(m+1)v^(-(m+1)) */
2000 }
2001
2002 static void ge_cached_0(ge_cached *r) {
2003 fe_1(r->YplusX);
2004 fe_1(r->YminusX);
2005 fe_1(r->Z);
2006 fe_0(r->T2d);
2007 }
2008
2009 static void ge_cached_cmov(ge_cached *t, const ge_cached *u, unsigned char b) {
2010 fe_cmov(t->YplusX, u->YplusX, b);
2011 fe_cmov(t->YminusX, u->YminusX, b);
2012 fe_cmov(t->Z, u->Z, b);
2013 fe_cmov(t->T2d, u->T2d, b);
2014 }
2015
2016 /* Assumes that a[31] <= 127 */
2017 void ge_scalarmult(ge_p2 *r, const unsigned char *a, const ge_p3 *A) {
2018 signed char e[64];
2019 int carry, carry2, i;
2020 ge_cached Ai[8]; /* 1 * A, 2 * A, ..., 8 * A */
2021 ge_p1p1 t;
2022 ge_p3 u;
2023
2024 carry = 0; /* 0..1 */
2025 for (i = 0; i < 31; i++) {
2026 carry += a[i]; /* 0..256 */
2027 carry2 = (carry + 8) >> 4; /* 0..16 */
2028 e[2 * i] = carry - (carry2 << 4); /* -8..7 */
2029 carry = (carry2 + 8) >> 4; /* 0..1 */
2030 e[2 * i + 1] = carry2 - (carry << 4); /* -8..7 */
2031 }
2032 carry += a[31]; /* 0..128 */
2033 carry2 = (carry + 8) >> 4; /* 0..8 */
2034 e[62] = carry - (carry2 << 4); /* -8..7 */
2035 e[63] = carry2; /* 0..8 */
2036
2037 ge_p3_to_cached(&Ai[0], A);
2038 for (i = 0; i < 7; i++) {
2039 ge_add(&t, A, &Ai[i]);
2040 ge_p1p1_to_p3(&u, &t);
2041 ge_p3_to_cached(&Ai[i + 1], &u);
2042 }
2043
2044 ge_p2_0(r);
2045 for (i = 63; i >= 0; i--) {
2046 signed char b = e[i];
2047 unsigned char bnegative = negative(b);
2048 unsigned char babs = b - (((-bnegative) & b) << 1);
2049 ge_cached cur, minuscur;
2050 ge_p2_dbl(&t, r);
2051 ge_p1p1_to_p2(r, &t);
2052 ge_p2_dbl(&t, r);
2053 ge_p1p1_to_p2(r, &t);
2054 ge_p2_dbl(&t, r);
2055 ge_p1p1_to_p2(r, &t);
2056 ge_p2_dbl(&t, r);
2057 ge_p1p1_to_p3(&u, &t);
2058 ge_cached_0(&cur);
2059 ge_cached_cmov(&cur, &Ai[0], equal(babs, 1));
2060 ge_cached_cmov(&cur, &Ai[1], equal(babs, 2));
2061 ge_cached_cmov(&cur, &Ai[2], equal(babs, 3));
2062 ge_cached_cmov(&cur, &Ai[3], equal(babs, 4));
2063 ge_cached_cmov(&cur, &Ai[4], equal(babs, 5));
2064 ge_cached_cmov(&cur, &Ai[5], equal(babs, 6));
2065 ge_cached_cmov(&cur, &Ai[6], equal(babs, 7));
2066 ge_cached_cmov(&cur, &Ai[7], equal(babs, 8));
2067 fe_copy(minuscur.YplusX, cur.YminusX);
2068 fe_copy(minuscur.YminusX, cur.YplusX);
2069 fe_copy(minuscur.Z, cur.Z);
2070 fe_neg(minuscur.T2d, cur.T2d);
2071 ge_cached_cmov(&cur, &minuscur, bnegative);
2072 ge_add(&t, &u, &cur);
2073 ge_p1p1_to_p2(r, &t);
2074 }
2075 }
2076
2077 //assume Bi has precomputed multiples
2078 void ge_double_scalarmult_precomp_vartime(ge_p2 *r, const unsigned char *a, const ge_p3 *A, const unsigned char *b, const ge_dsmp Bi) {
2079 signed char aslide[256];
2080 signed char bslide[256];
2081 ge_dsmp Ai; /* A, 3A, 5A, 7A, 9A, 11A, 13A, 15A */ //precomps
2082 ge_p1p1 t;
2083 ge_p3 u;
2084 int i;
2085
2086 slide(aslide, a);
2087 slide(bslide, b);
2088 ge_dsm_precomp(Ai, A);
2089
2090 ge_p2_0(r);
2091
2092 for (i = 255; i >= 0; --i) {
2093 if (aslide[i] || bslide[i]) break;
2094 }
2095
2096 for (; i >= 0; --i) {
2097 ge_p2_dbl(&t, r);
2098
2099 if (aslide[i] > 0) {
2100 ge_p1p1_to_p3(&u, &t);
2101 ge_add(&t, &u, &Ai[aslide[i]/2]);
2102 } else if (aslide[i] < 0) {
2103 ge_p1p1_to_p3(&u, &t);
2104 ge_sub(&t, &u, &Ai[(-aslide[i])/2]);
2105 }
2106
2107 if (bslide[i] > 0) {
2108 ge_p1p1_to_p3(&u, &t);
2109 ge_add(&t, &u, &Bi[bslide[i]/2]);
2110 } else if (bslide[i] < 0) {
2111 ge_p1p1_to_p3(&u, &t);
2112 ge_sub(&t, &u, &Bi[(-bslide[i])/2]);
2113 }
2114
2115 ge_p1p1_to_p2(r, &t);
2116 }
2117 }
2118
2119 //x
2120 void ge_mul8(ge_p1p1 *r, const ge_p2 *t) {
2121 ge_p2 u;
2122 ge_p2_dbl(r, t);
2123 ge_p1p1_to_p2(&u, r);
2124 ge_p2_dbl(r, &u);
2125 ge_p1p1_to_p2(&u, r);
2126 ge_p2_dbl(r, &u);
2127 }
2128
2129 //x
2130 void ge_fromfe_frombytes_vartime(ge_p2 *r, const unsigned char *s) {
2131 fe u, v, w, x, y, z;
2132 unsigned char sign;
2133
2134 /* From fe_frombytes.c */
2135
2136 int64_t h0 = load_4(s);
2137 int64_t h1 = load_3(s + 4) << 6;
2138 int64_t h2 = load_3(s + 7) << 5;
2139 int64_t h3 = load_3(s + 10) << 3;
2140 int64_t h4 = load_3(s + 13) << 2;
2141 int64_t h5 = load_4(s + 16);
2142 int64_t h6 = load_3(s + 20) << 7;
2143 int64_t h7 = load_3(s + 23) << 5;
2144 int64_t h8 = load_3(s + 26) << 4;
2145 int64_t h9 = load_3(s + 29) << 2;
2146 int64_t carry0;
2147 int64_t carry1;
2148 int64_t carry2;
2149 int64_t carry3;
2150 int64_t carry4;
2151 int64_t carry5;
2152 int64_t carry6;
2153 int64_t carry7;
2154 int64_t carry8;
2155 int64_t carry9;
2156
2157 carry9 = (h9 + (int64_t) (1<<24)) >> 25; h0 += carry9 * 19; h9 -= carry9 << 25;
2158 carry1 = (h1 + (int64_t) (1<<24)) >> 25; h2 += carry1; h1 -= carry1 << 25;
2159 carry3 = (h3 + (int64_t) (1<<24)) >> 25; h4 += carry3; h3 -= carry3 << 25;
2160 carry5 = (h5 + (int64_t) (1<<24)) >> 25; h6 += carry5; h5 -= carry5 << 25;
2161 carry7 = (h7 + (int64_t) (1<<24)) >> 25; h8 += carry7; h7 -= carry7 << 25;
2162
2163 carry0 = (h0 + (int64_t) (1<<25)) >> 26; h1 += carry0; h0 -= carry0 << 26;
2164 carry2 = (h2 + (int64_t) (1<<25)) >> 26; h3 += carry2; h2 -= carry2 << 26;
2165 carry4 = (h4 + (int64_t) (1<<25)) >> 26; h5 += carry4; h4 -= carry4 << 26;
2166 carry6 = (h6 + (int64_t) (1<<25)) >> 26; h7 += carry6; h6 -= carry6 << 26;
2167 carry8 = (h8 + (int64_t) (1<<25)) >> 26; h9 += carry8; h8 -= carry8 << 26;
2168
2169 u[0] = h0;
2170 u[1] = h1;
2171 u[2] = h2;
2172 u[3] = h3;
2173 u[4] = h4;
2174 u[5] = h5;
2175 u[6] = h6;
2176 u[7] = h7;
2177 u[8] = h8;
2178 u[9] = h9;
2179
2180 /* End fe_frombytes.c */
2181
2182 fe_sq2(v, u); /* 2 * u^2 */
2183 fe_1(w);
2184 fe_add(w, v, w); /* w = 2 * u^2 + 1 */
2185 fe_sq(x, w); /* w^2 */
2186 fe_mul(y, fe_ma2, v); /* -2 * A^2 * u^2 */
2187 fe_add(x, x, y); /* x = w^2 - 2 * A^2 * u^2 */
2188 fe_divpowm1(r->X, w, x); /* (w / x)^(m + 1) */
2189 fe_sq(y, r->X);
2190 fe_mul(x, y, x);
2191 fe_sub(y, w, x);
2192 fe_copy(z, fe_ma);
2193 if (fe_isnonzero(y)) {
2194 fe_add(y, w, x);
2195 if (fe_isnonzero(y)) {
2196 goto negative;
2197 } else {
2198 fe_mul(r->X, r->X, fe_fffb1);
2199 }
2200 } else {
2201 fe_mul(r->X, r->X, fe_fffb2);
2202 }
2203 fe_mul(r->X, r->X, u); /* u * sqrt(2 * A * (A + 2) * w / x) */
2204 fe_mul(z, z, v); /* -2 * A * u^2 */
2205 sign = 0;
2206 goto setsign;
2207 negative:
2208 fe_mul(x, x, fe_sqrtm1);
2209 fe_sub(y, w, x);
2210 if (fe_isnonzero(y)) {
2211 assert((fe_add(y, w, x), !fe_isnonzero(y)));
2212 fe_mul(r->X, r->X, fe_fffb3);
2213 } else {
2214 fe_mul(r->X, r->X, fe_fffb4);
2215 }
2216 /* r->X = sqrt(A * (A + 2) * w / x) */
2217 /* z = -A */
2218 sign = 1;
2219 setsign:
2220 if (fe_isnegative(r->X) != sign) {
2221 assert(fe_isnonzero(r->X));
2222 fe_neg(r->X, r->X);
2223 }
2224 fe_add(r->Z, z, w);
2225 fe_sub(r->Y, z, w);
2226 fe_mul(r->X, r->X, r->Z);
2227 #if !defined(NDEBUG)
2228 {
2229 fe check_x, check_y, check_iz, check_v;
2230 fe_invert(check_iz, r->Z);
2231 fe_mul(check_x, r->X, check_iz);
2232 fe_mul(check_y, r->Y, check_iz);
2233 fe_sq(check_x, check_x);
2234 fe_sq(check_y, check_y);
2235 fe_mul(check_v, check_x, check_y);
2236 fe_mul(check_v, fe_d, check_v);
2237 fe_add(check_v, check_v, check_x);
2238 fe_sub(check_v, check_v, check_y);
2239 fe_1(check_x);
2240 fe_add(check_v, check_v, check_x);
2241 assert(!fe_isnonzero(check_v));
2242 }
2243 #endif
2244 }
2245
2246 //x
2247 void sc_0(unsigned char *s) {
2248 int i;
2249 for (i = 0; i < 32; i++) {
2250 s[i] = 0;
2251 }
2252 }
2253
2254 //x
2255 void sc_reduce32(unsigned char *s) {
2256 int64_t s0 = 2097151 & load_3(s);
2257 int64_t s1 = 2097151 & (load_4(s + 2) >> 5);
2258 int64_t s2 = 2097151 & (load_3(s + 5) >> 2);
2259 int64_t s3 = 2097151 & (load_4(s + 7) >> 7);
2260 int64_t s4 = 2097151 & (load_4(s + 10) >> 4);
2261 int64_t s5 = 2097151 & (load_3(s + 13) >> 1);
2262 int64_t s6 = 2097151 & (load_4(s + 15) >> 6);
2263 int64_t s7 = 2097151 & (load_3(s + 18) >> 3);
2264 int64_t s8 = 2097151 & load_3(s + 21);
2265 int64_t s9 = 2097151 & (load_4(s + 23) >> 5);
2266 int64_t s10 = 2097151 & (load_3(s + 26) >> 2);
2267 int64_t s11 = (load_4(s + 28) >> 7);
2268 int64_t s12 = 0;
2269 int64_t carry0;
2270 int64_t carry1;
2271 int64_t carry2;
2272 int64_t carry3;
2273 int64_t carry4;
2274 int64_t carry5;
2275 int64_t carry6;
2276 int64_t carry7;
2277 int64_t carry8;
2278 int64_t carry9;
2279 int64_t carry10;
2280 int64_t carry11;
2281
2282 carry0 = (s0 + (1<<20)) >> 21; s1 += carry0; s0 -= carry0 << 21;
2283 carry2 = (s2 + (1<<20)) >> 21; s3 += carry2; s2 -= carry2 << 21;
2284 carry4 = (s4 + (1<<20)) >> 21; s5 += carry4; s4 -= carry4 << 21;
2285 carry6 = (s6 + (1<<20)) >> 21; s7 += carry6; s6 -= carry6 << 21;
2286 carry8 = (s8 + (1<<20)) >> 21; s9 += carry8; s8 -= carry8 << 21;
2287 carry10 = (s10 + (1<<20)) >> 21; s11 += carry10; s10 -= carry10 << 21;
2288
2289 carry1 = (s1 + (1<<20)) >> 21; s2 += carry1; s1 -= carry1 << 21;
2290 carry3 = (s3 + (1<<20)) >> 21; s4 += carry3; s3 -= carry3 << 21;
2291 carry5 = (s5 + (1<<20)) >> 21; s6 += carry5; s5 -= carry5 << 21;
2292 carry7 = (s7 + (1<<20)) >> 21; s8 += carry7; s7 -= carry7 << 21;
2293 carry9 = (s9 + (1<<20)) >> 21; s10 += carry9; s9 -= carry9 << 21;
2294 carry11 = (s11 + (1<<20)) >> 21; s12 += carry11; s11 -= carry11 << 21;
2295
2296 s0 += s12 * 666643;
2297 s1 += s12 * 470296;
2298 s2 += s12 * 654183;
2299 s3 -= s12 * 997805;
2300 s4 += s12 * 136657;
2301 s5 -= s12 * 683901;
2302 s12 = 0;
2303
2304 carry0 = s0 >> 21; s1 += carry0; s0 -= carry0 << 21;
2305 carry1 = s1 >> 21; s2 += carry1; s1 -= carry1 << 21;
2306 carry2 = s2 >> 21; s3 += carry2; s2 -= carry2 << 21;
2307 carry3 = s3 >> 21; s4 += carry3; s3 -= carry3 << 21;
2308 carry4 = s4 >> 21; s5 += carry4; s4 -= carry4 << 21;
2309 carry5 = s5 >> 21; s6 += carry5; s5 -= carry5 << 21;
2310 carry6 = s6 >> 21; s7 += carry6; s6 -= carry6 << 21;
2311 carry7 = s7 >> 21; s8 += carry7; s7 -= carry7 << 21;
2312 carry8 = s8 >> 21; s9 += carry8; s8 -= carry8 << 21;
2313 carry9 = s9 >> 21; s10 += carry9; s9 -= carry9 << 21;
2314 carry10 = s10 >> 21; s11 += carry10; s10 -= carry10 << 21;
2315 carry11 = s11 >> 21; s12 += carry11; s11 -= carry11 << 21;
2316
2317 s0 += s12 * 666643;
2318 s1 += s12 * 470296;
2319 s2 += s12 * 654183;
2320 s3 -= s12 * 997805;
2321 s4 += s12 * 136657;
2322 s5 -= s12 * 683901;
2323
2324 carry0 = s0 >> 21; s1 += carry0; s0 -= carry0 << 21;
2325 carry1 = s1 >> 21; s2 += carry1; s1 -= carry1 << 21;
2326 carry2 = s2 >> 21; s3 += carry2; s2 -= carry2 << 21;
2327 carry3 = s3 >> 21; s4 += carry3; s3 -= carry3 << 21;
2328 carry4 = s4 >> 21; s5 += carry4; s4 -= carry4 << 21;
2329 carry5 = s5 >> 21; s6 += carry5; s5 -= carry5 << 21;
2330 carry6 = s6 >> 21; s7 += carry6; s6 -= carry6 << 21;
2331 carry7 = s7 >> 21; s8 += carry7; s7 -= carry7 << 21;
2332 carry8 = s8 >> 21; s9 += carry8; s8 -= carry8 << 21;
2333 carry9 = s9 >> 21; s10 += carry9; s9 -= carry9 << 21;
2334 carry10 = s10 >> 21; s11 += carry10; s10 -= carry10 << 21;
2335
2336 s[0] = s0 >> 0;
2337 s[1] = s0 >> 8;
2338 s[2] = (s0 >> 16) | (s1 << 5);
2339 s[3] = s1 >> 3;
2340 s[4] = s1 >> 11;
2341 s[5] = (s1 >> 19) | (s2 << 2);
2342 s[6] = s2 >> 6;
2343 s[7] = (s2 >> 14) | (s3 << 7);
2344 s[8] = s3 >> 1;
2345 s[9] = s3 >> 9;
2346 s[10] = (s3 >> 17) | (s4 << 4);
2347 s[11] = s4 >> 4;
2348 s[12] = s4 >> 12;
2349 s[13] = (s4 >> 20) | (s5 << 1);
2350 s[14] = s5 >> 7;
2351 s[15] = (s5 >> 15) | (s6 << 6);
2352 s[16] = s6 >> 2;
2353 s[17] = s6 >> 10;
2354 s[18] = (s6 >> 18) | (s7 << 3);
2355 s[19] = s7 >> 5;
2356 s[20] = s7 >> 13;
2357 s[21] = s8 >> 0;
2358 s[22] = s8 >> 8;
2359 s[23] = (s8 >> 16) | (s9 << 5);
2360 s[24] = s9 >> 3;
2361 s[25] = s9 >> 11;
2362 s[26] = (s9 >> 19) | (s10 << 2);
2363 s[27] = s10 >> 6;
2364 s[28] = (s10 >> 14) | (s11 << 7);
2365 s[29] = s11 >> 1;
2366 s[30] = s11 >> 9;
2367 s[31] = s11 >> 17;
2368 }
2369
2370 //x
2371 void sc_add(unsigned char *s, const unsigned char *a, const unsigned char *b) {
2372 int64_t a0 = 2097151 & load_3(a);
2373 int64_t a1 = 2097151 & (load_4(a + 2) >> 5);
2374 int64_t a2 = 2097151 & (load_3(a + 5) >> 2);
2375 int64_t a3 = 2097151 & (load_4(a + 7) >> 7);
2376 int64_t a4 = 2097151 & (load_4(a + 10) >> 4);
2377 int64_t a5 = 2097151 & (load_3(a + 13) >> 1);
2378 int64_t a6 = 2097151 & (load_4(a + 15) >> 6);
2379 int64_t a7 = 2097151 & (load_3(a + 18) >> 3);
2380 int64_t a8 = 2097151 & load_3(a + 21);
2381 int64_t a9 = 2097151 & (load_4(a + 23) >> 5);
2382 int64_t a10 = 2097151 & (load_3(a + 26) >> 2);
2383 int64_t a11 = (load_4(a + 28) >> 7);
2384 int64_t b0 = 2097151 & load_3(b);
2385 int64_t b1 = 2097151 & (load_4(b + 2) >> 5);
2386 int64_t b2 = 2097151 & (load_3(b + 5) >> 2);
2387 int64_t b3 = 2097151 & (load_4(b + 7) >> 7);
2388 int64_t b4 = 2097151 & (load_4(b + 10) >> 4);
2389 int64_t b5 = 2097151 & (load_3(b + 13) >> 1);
2390 int64_t b6 = 2097151 & (load_4(b + 15) >> 6);
2391 int64_t b7 = 2097151 & (load_3(b + 18) >> 3);
2392 int64_t b8 = 2097151 & load_3(b + 21);
2393 int64_t b9 = 2097151 & (load_4(b + 23) >> 5);
2394 int64_t b10 = 2097151 & (load_3(b + 26) >> 2);
2395 int64_t b11 = (load_4(b + 28) >> 7);
2396 int64_t s0 = a0 + b0;
2397 int64_t s1 = a1 + b1;
2398 int64_t s2 = a2 + b2;
2399 int64_t s3 = a3 + b3;
2400 int64_t s4 = a4 + b4;
2401 int64_t s5 = a5 + b5;
2402 int64_t s6 = a6 + b6;
2403 int64_t s7 = a7 + b7;
2404 int64_t s8 = a8 + b8;
2405 int64_t s9 = a9 + b9;
2406 int64_t s10 = a10 + b10;
2407 int64_t s11 = a11 + b11;
2408 int64_t s12 = 0;
2409 int64_t carry0;
2410 int64_t carry1;
2411 int64_t carry2;
2412 int64_t carry3;
2413 int64_t carry4;
2414 int64_t carry5;
2415 int64_t carry6;
2416 int64_t carry7;
2417 int64_t carry8;
2418 int64_t carry9;
2419 int64_t carry10;
2420 int64_t carry11;
2421
2422 carry0 = (s0 + (1<<20)) >> 21; s1 += carry0; s0 -= carry0 << 21;
2423 carry2 = (s2 + (1<<20)) >> 21; s3 += carry2; s2 -= carry2 << 21;
2424 carry4 = (s4 + (1<<20)) >> 21; s5 += carry4; s4 -= carry4 << 21;
2425 carry6 = (s6 + (1<<20)) >> 21; s7 += carry6; s6 -= carry6 << 21;
2426 carry8 = (s8 + (1<<20)) >> 21; s9 += carry8; s8 -= carry8 << 21;
2427 carry10 = (s10 + (1<<20)) >> 21; s11 += carry10; s10 -= carry10 << 21;
2428
2429 carry1 = (s1 + (1<<20)) >> 21; s2 += carry1; s1 -= carry1 << 21;
2430 carry3 = (s3 + (1<<20)) >> 21; s4 += carry3; s3 -= carry3 << 21;
2431 carry5 = (s5 + (1<<20)) >> 21; s6 += carry5; s5 -= carry5 << 21;
2432 carry7 = (s7 + (1<<20)) >> 21; s8 += carry7; s7 -= carry7 << 21;
2433 carry9 = (s9 + (1<<20)) >> 21; s10 += carry9; s9 -= carry9 << 21;
2434 carry11 = (s11 + (1<<20)) >> 21; s12 += carry11; s11 -= carry11 << 21;
2435
2436 s0 += s12 * 666643;
2437 s1 += s12 * 470296;
2438 s2 += s12 * 654183;
2439 s3 -= s12 * 997805;
2440 s4 += s12 * 136657;
2441 s5 -= s12 * 683901;
2442 s12 = 0;
2443
2444 carry0 = s0 >> 21; s1 += carry0; s0 -= carry0 << 21;
2445 carry1 = s1 >> 21; s2 += carry1; s1 -= carry1 << 21;
2446 carry2 = s2 >> 21; s3 += carry2; s2 -= carry2 << 21;
2447 carry3 = s3 >> 21; s4 += carry3; s3 -= carry3 << 21;
2448 carry4 = s4 >> 21; s5 += carry4; s4 -= carry4 << 21;
2449 carry5 = s5 >> 21; s6 += carry5; s5 -= carry5 << 21;
2450 carry6 = s6 >> 21; s7 += carry6; s6 -= carry6 << 21;
2451 carry7 = s7 >> 21; s8 += carry7; s7 -= carry7 << 21;
2452 carry8 = s8 >> 21; s9 += carry8; s8 -= carry8 << 21;
2453 carry9 = s9 >> 21; s10 += carry9; s9 -= carry9 << 21;
2454 carry10 = s10 >> 21; s11 += carry10; s10 -= carry10 << 21;
2455 carry11 = s11 >> 21; s12 += carry11; s11 -= carry11 << 21;
2456
2457 s0 += s12 * 666643;
2458 s1 += s12 * 470296;
2459 s2 += s12 * 654183;
2460 s3 -= s12 * 997805;
2461 s4 += s12 * 136657;
2462 s5 -= s12 * 683901;
2463
2464 carry0 = s0 >> 21; s1 += carry0; s0 -= carry0 << 21;
2465 carry1 = s1 >> 21; s2 += carry1; s1 -= carry1 << 21;
2466 carry2 = s2 >> 21; s3 += carry2; s2 -= carry2 << 21;
2467 carry3 = s3 >> 21; s4 += carry3; s3 -= carry3 << 21;
2468 carry4 = s4 >> 21; s5 += carry4; s4 -= carry4 << 21;
2469 carry5 = s5 >> 21; s6 += carry5; s5 -= carry5 << 21;
2470 carry6 = s6 >> 21; s7 += carry6; s6 -= carry6 << 21;
2471 carry7 = s7 >> 21; s8 += carry7; s7 -= carry7 << 21;
2472 carry8 = s8 >> 21; s9 += carry8; s8 -= carry8 << 21;
2473 carry9 = s9 >> 21; s10 += carry9; s9 -= carry9 << 21;
2474 carry10 = s10 >> 21; s11 += carry10; s10 -= carry10 << 21;
2475
2476 s[0] = s0 >> 0;
2477 s[1] = s0 >> 8;
2478 s[2] = (s0 >> 16) | (s1 << 5);
2479 s[3] = s1 >> 3;
2480 s[4] = s1 >> 11;
2481 s[5] = (s1 >> 19) | (s2 << 2);
2482 s[6] = s2 >> 6;
2483 s[7] = (s2 >> 14) | (s3 << 7);
2484 s[8] = s3 >> 1;
2485 s[9] = s3 >> 9;
2486 s[10] = (s3 >> 17) | (s4 << 4);
2487 s[11] = s4 >> 4;
2488 s[12] = s4 >> 12;
2489 s[13] = (s4 >> 20) | (s5 << 1);
2490 s[14] = s5 >> 7;
2491 s[15] = (s5 >> 15) | (s6 << 6);
2492 s[16] = s6 >> 2;
2493 s[17] = s6 >> 10;
2494 s[18] = (s6 >> 18) | (s7 << 3);
2495 s[19] = s7 >> 5;
2496 s[20] = s7 >> 13;
2497 s[21] = s8 >> 0;
2498 s[22] = s8 >> 8;
2499 s[23] = (s8 >> 16) | (s9 << 5);
2500 s[24] = s9 >> 3;
2501 s[25] = s9 >> 11;
2502 s[26] = (s9 >> 19) | (s10 << 2);
2503 s[27] = s10 >> 6;
2504 s[28] = (s10 >> 14) | (s11 << 7);
2505 s[29] = s11 >> 1;
2506 s[30] = s11 >> 9;
2507 s[31] = s11 >> 17;
2508 }
2509
2510 //x
2511 void sc_sub(unsigned char *s, const unsigned char *a, const unsigned char *b) {
2512 int64_t a0 = 2097151 & load_3(a);
2513 int64_t a1 = 2097151 & (load_4(a + 2) >> 5);
2514 int64_t a2 = 2097151 & (load_3(a + 5) >> 2);
2515 int64_t a3 = 2097151 & (load_4(a + 7) >> 7);
2516 int64_t a4 = 2097151 & (load_4(a + 10) >> 4);
2517 int64_t a5 = 2097151 & (load_3(a + 13) >> 1);
2518 int64_t a6 = 2097151 & (load_4(a + 15) >> 6);
2519 int64_t a7 = 2097151 & (load_3(a + 18) >> 3);
2520 int64_t a8 = 2097151 & load_3(a + 21);
2521 int64_t a9 = 2097151 & (load_4(a + 23) >> 5);
2522 int64_t a10 = 2097151 & (load_3(a + 26) >> 2);
2523 int64_t a11 = (load_4(a + 28) >> 7);
2524 int64_t b0 = 2097151 & load_3(b);
2525 int64_t b1 = 2097151 & (load_4(b + 2) >> 5);
2526 int64_t b2 = 2097151 & (load_3(b + 5) >> 2);
2527 int64_t b3 = 2097151 & (load_4(b + 7) >> 7);
2528 int64_t b4 = 2097151 & (load_4(b + 10) >> 4);
2529 int64_t b5 = 2097151 & (load_3(b + 13) >> 1);
2530 int64_t b6 = 2097151 & (load_4(b + 15) >> 6);
2531 int64_t b7 = 2097151 & (load_3(b + 18) >> 3);
2532 int64_t b8 = 2097151 & load_3(b + 21);
2533 int64_t b9 = 2097151 & (load_4(b + 23) >> 5);
2534 int64_t b10 = 2097151 & (load_3(b + 26) >> 2);
2535 int64_t b11 = (load_4(b + 28) >> 7);
2536 int64_t s0 = a0 - b0;
2537 int64_t s1 = a1 - b1;
2538 int64_t s2 = a2 - b2;
2539 int64_t s3 = a3 - b3;
2540 int64_t s4 = a4 - b4;
2541 int64_t s5 = a5 - b5;
2542 int64_t s6 = a6 - b6;
2543 int64_t s7 = a7 - b7;
2544 int64_t s8 = a8 - b8;
2545 int64_t s9 = a9 - b9;
2546 int64_t s10 = a10 - b10;
2547 int64_t s11 = a11 - b11;
2548 int64_t s12 = 0;
2549 int64_t carry0;
2550 int64_t carry1;
2551 int64_t carry2;
2552 int64_t carry3;
2553 int64_t carry4;
2554 int64_t carry5;
2555 int64_t carry6;
2556 int64_t carry7;
2557 int64_t carry8;
2558 int64_t carry9;
2559 int64_t carry10;
2560 int64_t carry11;
2561
2562 carry0 = (s0 + (1<<20)) >> 21; s1 += carry0; s0 -= carry0 << 21;
2563 carry2 = (s2 + (1<<20)) >> 21; s3 += carry2; s2 -= carry2 << 21;
2564 carry4 = (s4 + (1<<20)) >> 21; s5 += carry4; s4 -= carry4 << 21;
2565 carry6 = (s6 + (1<<20)) >> 21; s7 += carry6; s6 -= carry6 << 21;
2566 carry8 = (s8 + (1<<20)) >> 21; s9 += carry8; s8 -= carry8 << 21;
2567 carry10 = (s10 + (1<<20)) >> 21; s11 += carry10; s10 -= carry10 << 21;
2568
2569 carry1 = (s1 + (1<<20)) >> 21; s2 += carry1; s1 -= carry1 << 21;
2570 carry3 = (s3 + (1<<20)) >> 21; s4 += carry3; s3 -= carry3 << 21;
2571 carry5 = (s5 + (1<<20)) >> 21; s6 += carry5; s5 -= carry5 << 21;
2572 carry7 = (s7 + (1<<20)) >> 21; s8 += carry7; s7 -= carry7 << 21;
2573 carry9 = (s9 + (1<<20)) >> 21; s10 += carry9; s9 -= carry9 << 21;
2574 carry11 = (s11 + (1<<20)) >> 21; s12 += carry11; s11 -= carry11 << 21;
2575
2576 s0 += s12 * 666643;
2577 s1 += s12 * 470296;
2578 s2 += s12 * 654183;
2579 s3 -= s12 * 997805;
2580 s4 += s12 * 136657;
2581 s5 -= s12 * 683901;
2582 s12 = 0;
2583
2584 carry0 = s0 >> 21; s1 += carry0; s0 -= carry0 << 21;
2585 carry1 = s1 >> 21; s2 += carry1; s1 -= carry1 << 21;
2586 carry2 = s2 >> 21; s3 += carry2; s2 -= carry2 << 21;
2587 carry3 = s3 >> 21; s4 += carry3; s3 -= carry3 << 21;
2588 carry4 = s4 >> 21; s5 += carry4; s4 -= carry4 << 21;
2589 carry5 = s5 >> 21; s6 += carry5; s5 -= carry5 << 21;
2590 carry6 = s6 >> 21; s7 += carry6; s6 -= carry6 << 21;
2591 carry7 = s7 >> 21; s8 += carry7; s7 -= carry7 << 21;
2592 carry8 = s8 >> 21; s9 += carry8; s8 -= carry8 << 21;
2593 carry9 = s9 >> 21; s10 += carry9; s9 -= carry9 << 21;
2594 carry10 = s10 >> 21; s11 += carry10; s10 -= carry10 << 21;
2595 carry11 = s11 >> 21; s12 += carry11; s11 -= carry11 << 21;
2596
2597 s0 += s12 * 666643;
2598 s1 += s12 * 470296;
2599 s2 += s12 * 654183;
2600 s3 -= s12 * 997805;
2601 s4 += s12 * 136657;
2602 s5 -= s12 * 683901;
2603
2604 carry0 = s0 >> 21; s1 += carry0; s0 -= carry0 << 21;
2605 carry1 = s1 >> 21; s2 += carry1; s1 -= carry1 << 21;
2606 carry2 = s2 >> 21; s3 += carry2; s2 -= carry2 << 21;
2607 carry3 = s3 >> 21; s4 += carry3; s3 -= carry3 << 21;
2608 carry4 = s4 >> 21; s5 += carry4; s4 -= carry4 << 21;
2609 carry5 = s5 >> 21; s6 += carry5; s5 -= carry5 << 21;
2610 carry6 = s6 >> 21; s7 += carry6; s6 -= carry6 << 21;
2611 carry7 = s7 >> 21; s8 += carry7; s7 -= carry7 << 21;
2612 carry8 = s8 >> 21; s9 += carry8; s8 -= carry8 << 21;
2613 carry9 = s9 >> 21; s10 += carry9; s9 -= carry9 << 21;
2614 carry10 = s10 >> 21; s11 += carry10; s10 -= carry10 << 21;
2615
2616 s[0] = s0 >> 0;
2617 s[1] = s0 >> 8;
2618 s[2] = (s0 >> 16) | (s1 << 5);
2619 s[3] = s1 >> 3;
2620 s[4] = s1 >> 11;
2621 s[5] = (s1 >> 19) | (s2 << 2);
2622 s[6] = s2 >> 6;
2623 s[7] = (s2 >> 14) | (s3 << 7);
2624 s[8] = s3 >> 1;
2625 s[9] = s3 >> 9;
2626 s[10] = (s3 >> 17) | (s4 << 4);
2627 s[11] = s4 >> 4;
2628 s[12] = s4 >> 12;
2629 s[13] = (s4 >> 20) | (s5 << 1);
2630 s[14] = s5 >> 7;
2631 s[15] = (s5 >> 15) | (s6 << 6);
2632 s[16] = s6 >> 2;
2633 s[17] = s6 >> 10;
2634 s[18] = (s6 >> 18) | (s7 << 3);
2635 s[19] = s7 >> 5;
2636 s[20] = s7 >> 13;
2637 s[21] = s8 >> 0;
2638 s[22] = s8 >> 8;
2639 s[23] = (s8 >> 16) | (s9 << 5);
2640 s[24] = s9 >> 3;
2641 s[25] = s9 >> 11;
2642 s[26] = (s9 >> 19) | (s10 << 2);
2643 s[27] = s10 >> 6;
2644 s[28] = (s10 >> 14) | (s11 << 7);
2645 s[29] = s11 >> 1;
2646 s[30] = s11 >> 9;
2647 s[31] = s11 >> 17;
2648 }
2649
2650 /*
2651 Input:
2652 a[0]+256*a[1]+...+256^31*a[31] = a
2653 b[0]+256*b[1]+...+256^31*b[31] = b
2654 c[0]+256*c[1]+...+256^31*c[31] = c
2655
2656 Output:
2657 s[0]+256*s[1]+...+256^31*s[31] = (c-ab) mod l
2658 where l = 2^252 + 27742317777372353535851937790883648493.
2659 */
2660
2661 //x
2662 void sc_mulsub(unsigned char *s, const unsigned char *a, const unsigned char *b, const unsigned char *c) {
2663 int64_t a0 = 2097151 & load_3(a);
2664 int64_t a1 = 2097151 & (load_4(a + 2) >> 5);
2665 int64_t a2 = 2097151 & (load_3(a + 5) >> 2);
2666 int64_t a3 = 2097151 & (load_4(a + 7) >> 7);
2667 int64_t a4 = 2097151 & (load_4(a + 10) >> 4);
2668 int64_t a5 = 2097151 & (load_3(a + 13) >> 1);
2669 int64_t a6 = 2097151 & (load_4(a + 15) >> 6);
2670 int64_t a7 = 2097151 & (load_3(a + 18) >> 3);
2671 int64_t a8 = 2097151 & load_3(a + 21);
2672 int64_t a9 = 2097151 & (load_4(a + 23) >> 5);
2673 int64_t a10 = 2097151 & (load_3(a + 26) >> 2);
2674 int64_t a11 = (load_4(a + 28) >> 7);
2675 int64_t b0 = 2097151 & load_3(b);
2676 int64_t b1 = 2097151 & (load_4(b + 2) >> 5);
2677 int64_t b2 = 2097151 & (load_3(b + 5) >> 2);
2678 int64_t b3 = 2097151 & (load_4(b + 7) >> 7);
2679 int64_t b4 = 2097151 & (load_4(b + 10) >> 4);
2680 int64_t b5 = 2097151 & (load_3(b + 13) >> 1);
2681 int64_t b6 = 2097151 & (load_4(b + 15) >> 6);
2682 int64_t b7 = 2097151 & (load_3(b + 18) >> 3);
2683 int64_t b8 = 2097151 & load_3(b + 21);
2684 int64_t b9 = 2097151 & (load_4(b + 23) >> 5);
2685 int64_t b10 = 2097151 & (load_3(b + 26) >> 2);
2686 int64_t b11 = (load_4(b + 28) >> 7);
2687 int64_t c0 = 2097151 & load_3(c);
2688 int64_t c1 = 2097151 & (load_4(c + 2) >> 5);
2689 int64_t c2 = 2097151 & (load_3(c + 5) >> 2);
2690 int64_t c3 = 2097151 & (load_4(c + 7) >> 7);
2691 int64_t c4 = 2097151 & (load_4(c + 10) >> 4);
2692 int64_t c5 = 2097151 & (load_3(c + 13) >> 1);
2693 int64_t c6 = 2097151 & (load_4(c + 15) >> 6);
2694 int64_t c7 = 2097151 & (load_3(c + 18) >> 3);
2695 int64_t c8 = 2097151 & load_3(c + 21);
2696 int64_t c9 = 2097151 & (load_4(c + 23) >> 5);
2697 int64_t c10 = 2097151 & (load_3(c + 26) >> 2);
2698 int64_t c11 = (load_4(c + 28) >> 7);
2699 int64_t s0;
2700 int64_t s1;
2701 int64_t s2;
2702 int64_t s3;
2703 int64_t s4;
2704 int64_t s5;
2705 int64_t s6;
2706 int64_t s7;
2707 int64_t s8;
2708 int64_t s9;
2709 int64_t s10;
2710 int64_t s11;
2711 int64_t s12;
2712 int64_t s13;
2713 int64_t s14;
2714 int64_t s15;
2715 int64_t s16;
2716 int64_t s17;
2717 int64_t s18;
2718 int64_t s19;
2719 int64_t s20;
2720 int64_t s21;
2721 int64_t s22;
2722 int64_t s23;
2723 int64_t carry0;
2724 int64_t carry1;
2725 int64_t carry2;
2726 int64_t carry3;
2727 int64_t carry4;
2728 int64_t carry5;
2729 int64_t carry6;
2730 int64_t carry7;
2731 int64_t carry8;
2732 int64_t carry9;
2733 int64_t carry10;
2734 int64_t carry11;
2735 int64_t carry12;
2736 int64_t carry13;
2737 int64_t carry14;
2738 int64_t carry15;
2739 int64_t carry16;
2740 int64_t carry17;
2741 int64_t carry18;
2742 int64_t carry19;
2743 int64_t carry20;
2744 int64_t carry21;
2745 int64_t carry22;
2746
2747 s0 = c0 - a0*b0;
2748 s1 = c1 - (a0*b1 + a1*b0);
2749 s2 = c2 - (a0*b2 + a1*b1 + a2*b0);
2750 s3 = c3 - (a0*b3 + a1*b2 + a2*b1 + a3*b0);
2751 s4 = c4 - (a0*b4 + a1*b3 + a2*b2 + a3*b1 + a4*b0);
2752 s5 = c5 - (a0*b5 + a1*b4 + a2*b3 + a3*b2 + a4*b1 + a5*b0);
2753 s6 = c6 - (a0*b6 + a1*b5 + a2*b4 + a3*b3 + a4*b2 + a5*b1 + a6*b0);
2754 s7 = c7 - (a0*b7 + a1*b6 + a2*b5 + a3*b4 + a4*b3 + a5*b2 + a6*b1 + a7*b0);
2755 s8 = c8 - (a0*b8 + a1*b7 + a2*b6 + a3*b5 + a4*b4 + a5*b3 + a6*b2 + a7*b1 + a8*b0);
2756 s9 = c9 - (a0*b9 + a1*b8 + a2*b7 + a3*b6 + a4*b5 + a5*b4 + a6*b3 + a7*b2 + a8*b1 + a9*b0);
2757 s10 = c10 - (a0*b10 + a1*b9 + a2*b8 + a3*b7 + a4*b6 + a5*b5 + a6*b4 + a7*b3 + a8*b2 + a9*b1 + a10*b0);
2758 s11 = c11 - (a0*b11 + a1*b10 + a2*b9 + a3*b8 + a4*b7 + a5*b6 + a6*b5 + a7*b4 + a8*b3 + a9*b2 + a10*b1 + a11*b0);
2759 s12 = -(a1*b11 + a2*b10 + a3*b9 + a4*b8 + a5*b7 + a6*b6 + a7*b5 + a8*b4 + a9*b3 + a10*b2 + a11*b1);
2760 s13 = -(a2*b11 + a3*b10 + a4*b9 + a5*b8 + a6*b7 + a7*b6 + a8*b5 + a9*b4 + a10*b3 + a11*b2);
2761 s14 = -(a3*b11 + a4*b10 + a5*b9 + a6*b8 + a7*b7 + a8*b6 + a9*b5 + a10*b4 + a11*b3);
2762 s15 = -(a4*b11 + a5*b10 + a6*b9 + a7*b8 + a8*b7 + a9*b6 + a10*b5 + a11*b4);
2763 s16 = -(a5*b11 + a6*b10 + a7*b9 + a8*b8 + a9*b7 + a10*b6 + a11*b5);
2764 s17 = -(a6*b11 + a7*b10 + a8*b9 + a9*b8 + a10*b7 + a11*b6);
2765 s18 = -(a7*b11 + a8*b10 + a9*b9 + a10*b8 + a11*b7);
2766 s19 = -(a8*b11 + a9*b10 + a10*b9 + a11*b8);
2767 s20 = -(a9*b11 + a10*b10 + a11*b9);
2768 s21 = -(a10*b11 + a11*b10);
2769 s22 = -a11*b11;
2770 s23 = 0;
2771
2772 carry0 = (s0 + (1<<20)) >> 21; s1 += carry0; s0 -= carry0 << 21;
2773 carry2 = (s2 + (1<<20)) >> 21; s3 += carry2; s2 -= carry2 << 21;
2774 carry4 = (s4 + (1<<20)) >> 21; s5 += carry4; s4 -= carry4 << 21;
2775 carry6 = (s6 + (1<<20)) >> 21; s7 += carry6; s6 -= carry6 << 21;
2776 carry8 = (s8 + (1<<20)) >> 21; s9 += carry8; s8 -= carry8 << 21;
2777 carry10 = (s10 + (1<<20)) >> 21; s11 += carry10; s10 -= carry10 << 21;
2778 carry12 = (s12 + (1<<20)) >> 21; s13 += carry12; s12 -= carry12 << 21;
2779 carry14 = (s14 + (1<<20)) >> 21; s15 += carry14; s14 -= carry14 << 21;
2780 carry16 = (s16 + (1<<20)) >> 21; s17 += carry16; s16 -= carry16 << 21;
2781 carry18 = (s18 + (1<<20)) >> 21; s19 += carry18; s18 -= carry18 << 21;
2782 carry20 = (s20 + (1<<20)) >> 21; s21 += carry20; s20 -= carry20 << 21;
2783 carry22 = (s22 + (1<<20)) >> 21; s23 += carry22; s22 -= carry22 << 21;
2784
2785 carry1 = (s1 + (1<<20)) >> 21; s2 += carry1; s1 -= carry1 << 21;
2786 carry3 = (s3 + (1<<20)) >> 21; s4 += carry3; s3 -= carry3 << 21;
2787 carry5 = (s5 + (1<<20)) >> 21; s6 += carry5; s5 -= carry5 << 21;
2788 carry7 = (s7 + (1<<20)) >> 21; s8 += carry7; s7 -= carry7 << 21;
2789 carry9 = (s9 + (1<<20)) >> 21; s10 += carry9; s9 -= carry9 << 21;
2790 carry11 = (s11 + (1<<20)) >> 21; s12 += carry11; s11 -= carry11 << 21;
2791 carry13 = (s13 + (1<<20)) >> 21; s14 += carry13; s13 -= carry13 << 21;
2792 carry15 = (s15 + (1<<20)) >> 21; s16 += carry15; s15 -= carry15 << 21;
2793 carry17 = (s17 + (1<<20)) >> 21; s18 += carry17; s17 -= carry17 << 21;
2794 carry19 = (s19 + (1<<20)) >> 21; s20 += carry19; s19 -= carry19 << 21;
2795 carry21 = (s21 + (1<<20)) >> 21; s22 += carry21; s21 -= carry21 << 21;
2796
2797 s11 += s23 * 666643;
2798 s12 += s23 * 470296;
2799 s13 += s23 * 654183;
2800 s14 -= s23 * 997805;
2801 s15 += s23 * 136657;
2802 s16 -= s23 * 683901;
2803
2804 s10 += s22 * 666643;
2805 s11 += s22 * 470296;
2806 s12 += s22 * 654183;
2807 s13 -= s22 * 997805;
2808 s14 += s22 * 136657;
2809 s15 -= s22 * 683901;
2810
2811 s9 += s21 * 666643;
2812 s10 += s21 * 470296;
2813 s11 += s21 * 654183;
2814 s12 -= s21 * 997805;
2815 s13 += s21 * 136657;
2816 s14 -= s21 * 683901;
2817
2818 s8 += s20 * 666643;
2819 s9 += s20 * 470296;
2820 s10 += s20 * 654183;
2821 s11 -= s20 * 997805;
2822 s12 += s20 * 136657;
2823 s13 -= s20 * 683901;
2824
2825 s7 += s19 * 666643;
2826 s8 += s19 * 470296;
2827 s9 += s19 * 654183;
2828 s10 -= s19 * 997805;
2829 s11 += s19 * 136657;
2830 s12 -= s19 * 683901;
2831
2832 s6 += s18 * 666643;
2833 s7 += s18 * 470296;
2834 s8 += s18 * 654183;
2835 s9 -= s18 * 997805;
2836 s10 += s18 * 136657;
2837 s11 -= s18 * 683901;
2838
2839 carry6 = (s6 + (1<<20)) >> 21; s7 += carry6; s6 -= carry6 << 21;
2840 carry8 = (s8 + (1<<20)) >> 21; s9 += carry8; s8 -= carry8 << 21;
2841 carry10 = (s10 + (1<<20)) >> 21; s11 += carry10; s10 -= carry10 << 21;
2842 carry12 = (s12 + (1<<20)) >> 21; s13 += carry12; s12 -= carry12 << 21;
2843 carry14 = (s14 + (1<<20)) >> 21; s15 += carry14; s14 -= carry14 << 21;
2844 carry16 = (s16 + (1<<20)) >> 21; s17 += carry16; s16 -= carry16 << 21;
2845
2846 carry7 = (s7 + (1<<20)) >> 21; s8 += carry7; s7 -= carry7 << 21;
2847 carry9 = (s9 + (1<<20)) >> 21; s10 += carry9; s9 -= carry9 << 21;
2848 carry11 = (s11 + (1<<20)) >> 21; s12 += carry11; s11 -= carry11 << 21;
2849 carry13 = (s13 + (1<<20)) >> 21; s14 += carry13; s13 -= carry13 << 21;
2850 carry15 = (s15 + (1<<20)) >> 21; s16 += carry15; s15 -= carry15 << 21;
2851
2852 s5 += s17 * 666643;
2853 s6 += s17 * 470296;
2854 s7 += s17 * 654183;
2855 s8 -= s17 * 997805;
2856 s9 += s17 * 136657;
2857 s10 -= s17 * 683901;
2858
2859 s4 += s16 * 666643;
2860 s5 += s16 * 470296;
2861 s6 += s16 * 654183;
2862 s7 -= s16 * 997805;
2863 s8 += s16 * 136657;
2864 s9 -= s16 * 683901;
2865
2866 s3 += s15 * 666643;
2867 s4 += s15 * 470296;
2868 s5 += s15 * 654183;
2869 s6 -= s15 * 997805;
2870 s7 += s15 * 136657;
2871 s8 -= s15 * 683901;
2872
2873 s2 += s14 * 666643;
2874 s3 += s14 * 470296;
2875 s4 += s14 * 654183;
2876 s5 -= s14 * 997805;
2877 s6 += s14 * 136657;
2878 s7 -= s14 * 683901;
2879
2880 s1 += s13 * 666643;
2881 s2 += s13 * 470296;
2882 s3 += s13 * 654183;
2883 s4 -= s13 * 997805;
2884 s5 += s13 * 136657;
2885 s6 -= s13 * 683901;
2886
2887 s0 += s12 * 666643;
2888 s1 += s12 * 470296;
2889 s2 += s12 * 654183;
2890 s3 -= s12 * 997805;
2891 s4 += s12 * 136657;
2892 s5 -= s12 * 683901;
2893 s12 = 0;
2894
2895 carry0 = (s0 + (1<<20)) >> 21; s1 += carry0; s0 -= carry0 << 21;
2896 carry2 = (s2 + (1<<20)) >> 21; s3 += carry2; s2 -= carry2 << 21;
2897 carry4 = (s4 + (1<<20)) >> 21; s5 += carry4; s4 -= carry4 << 21;
2898 carry6 = (s6 + (1<<20)) >> 21; s7 += carry6; s6 -= carry6 << 21;
2899 carry8 = (s8 + (1<<20)) >> 21; s9 += carry8; s8 -= carry8 << 21;
2900 carry10 = (s10 + (1<<20)) >> 21; s11 += carry10; s10 -= carry10 << 21;
2901
2902 carry1 = (s1 + (1<<20)) >> 21; s2 += carry1; s1 -= carry1 << 21;
2903 carry3 = (s3 + (1<<20)) >> 21; s4 += carry3; s3 -= carry3 << 21;
2904 carry5 = (s5 + (1<<20)) >> 21; s6 += carry5; s5 -= carry5 << 21;
2905 carry7 = (s7 + (1<<20)) >> 21; s8 += carry7; s7 -= carry7 << 21;
2906 carry9 = (s9 + (1<<20)) >> 21; s10 += carry9; s9 -= carry9 << 21;
2907 carry11 = (s11 + (1<<20)) >> 21; s12 += carry11; s11 -= carry11 << 21;
2908
2909 s0 += s12 * 666643;
2910 s1 += s12 * 470296;
2911 s2 += s12 * 654183;
2912 s3 -= s12 * 997805;
2913 s4 += s12 * 136657;
2914 s5 -= s12 * 683901;
2915 s12 = 0;
2916
2917 carry0 = s0 >> 21; s1 += carry0; s0 -= carry0 << 21;
2918 carry1 = s1 >> 21; s2 += carry1; s1 -= carry1 << 21;
2919 carry2 = s2 >> 21; s3 += carry2; s2 -= carry2 << 21;
2920 carry3 = s3 >> 21; s4 += carry3; s3 -= carry3 << 21;
2921 carry4 = s4 >> 21; s5 += carry4; s4 -= carry4 << 21;
2922 carry5 = s5 >> 21; s6 += carry5; s5 -= carry5 << 21;
2923 carry6 = s6 >> 21; s7 += carry6; s6 -= carry6 << 21;
2924 carry7 = s7 >> 21; s8 += carry7; s7 -= carry7 << 21;
2925 carry8 = s8 >> 21; s9 += carry8; s8 -= carry8 << 21;
2926 carry9 = s9 >> 21; s10 += carry9; s9 -= carry9 << 21;
2927 carry10 = s10 >> 21; s11 += carry10; s10 -= carry10 << 21;
2928 carry11 = s11 >> 21; s12 += carry11; s11 -= carry11 << 21;
2929
2930 s0 += s12 * 666643;
2931 s1 += s12 * 470296;
2932 s2 += s12 * 654183;
2933 s3 -= s12 * 997805;
2934 s4 += s12 * 136657;
2935 s5 -= s12 * 683901;
2936
2937 carry0 = s0 >> 21; s1 += carry0; s0 -= carry0 << 21;
2938 carry1 = s1 >> 21; s2 += carry1; s1 -= carry1 << 21;
2939 carry2 = s2 >> 21; s3 += carry2; s2 -= carry2 << 21;
2940 carry3 = s3 >> 21; s4 += carry3; s3 -= carry3 << 21;
2941 carry4 = s4 >> 21; s5 += carry4; s4 -= carry4 << 21;
2942 carry5 = s5 >> 21; s6 += carry5; s5 -= carry5 << 21;
2943 carry6 = s6 >> 21; s7 += carry6; s6 -= carry6 << 21;
2944 carry7 = s7 >> 21; s8 += carry7; s7 -= carry7 << 21;
2945 carry8 = s8 >> 21; s9 += carry8; s8 -= carry8 << 21;
2946 carry9 = s9 >> 21; s10 += carry9; s9 -= carry9 << 21;
2947 carry10 = s10 >> 21; s11 += carry10; s10 -= carry10 << 21;
2948
2949 s[0] = s0 >> 0;
2950 s[1] = s0 >> 8;
2951 s[2] = (s0 >> 16) | (s1 << 5);
2952 s[3] = s1 >> 3;
2953 s[4] = s1 >> 11;
2954 s[5] = (s1 >> 19) | (s2 << 2);
2955 s[6] = s2 >> 6;
2956 s[7] = (s2 >> 14) | (s3 << 7);
2957 s[8] = s3 >> 1;
2958 s[9] = s3 >> 9;
2959 s[10] = (s3 >> 17) | (s4 << 4);
2960 s[11] = s4 >> 4;
2961 s[12] = s4 >> 12;
2962 s[13] = (s4 >> 20) | (s5 << 1);
2963 s[14] = s5 >> 7;
2964 s[15] = (s5 >> 15) | (s6 << 6);
2965 s[16] = s6 >> 2;
2966 s[17] = s6 >> 10;
2967 s[18] = (s6 >> 18) | (s7 << 3);
2968 s[19] = s7 >> 5;
2969 s[20] = s7 >> 13;
2970 s[21] = s8 >> 0;
2971 s[22] = s8 >> 8;
2972 s[23] = (s8 >> 16) | (s9 << 5);
2973 s[24] = s9 >> 3;
2974 s[25] = s9 >> 11;
2975 s[26] = (s9 >> 19) | (s10 << 2);
2976 s[27] = s10 >> 6;
2977 s[28] = (s10 >> 14) | (s11 << 7);
2978 s[29] = s11 >> 1;
2979 s[30] = s11 >> 9;
2980 s[31] = s11 >> 17;
2981 }
2982
2983 /* Assumes that a != INT64_MIN */
2984 //x c.f. lt in 32 bit ref
2985 static int64_t signum(int64_t a) {
2986 return (a >> 63) - ((-a) >> 63);
2987 }
2988
2989 //x
2990 int sc_check(const unsigned char *s) {
2991 int64_t s0 = load_4(s);
2992 int64_t s1 = load_4(s + 4);
2993 int64_t s2 = load_4(s + 8);
2994 int64_t s3 = load_4(s + 12);
2995 int64_t s4 = load_4(s + 16);
2996 int64_t s5 = load_4(s + 20);
2997 int64_t s6 = load_4(s + 24);
2998 int64_t s7 = load_4(s + 28);
2999 return (signum(1559614444 - s0) + (signum(1477600026 - s1) << 1) + (signum(2734136534 - s2) << 2) + (signum(350157278 - s3) << 3) + (signum(-s4) << 4) + (signum(-s5) << 5) + (signum(-s6) << 6) + (signum(268435456 - s7) << 7)) >> 8;
3000 }
3001
3002 //x
3003 int sc_isnonzero(const unsigned char *s) {
3004 return (((int) (s[0] | s[1] | s[2] | s[3] | s[4] | s[5] | s[6] | s[7] | s[8] |
3005 s[9] | s[10] | s[11] | s[12] | s[13] | s[14] | s[15] | s[16] | s[17] |
3006 s[18] | s[19] | s[20] | s[21] | s[22] | s[23] | s[24] | s[25] | s[26] |
3007 s[27] | s[28] | s[29] | s[30] | s[31]) - 1) >> 8) + 1;
3008 }