Debian Codegraph @

src/mont.c

raw symbols SCIP
1 /* ===================================================================
2 *
3 * Copyright (c) 2018, Helder Eijs <[email protected]>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 * ===================================================================
30 */
31
32 #include <assert.h>
33
34 #include "common.h"
35 #include "endianess.h"
36 #include "multiply.h"
37 #include "mont.h"
38
39 #if SYS_BITS == 32
40 #include "multiply_32.c"
41 #else
42 #if SYS_BITS == 64
43 #include "multiply_64.c"
44 #else
45 #error You must define the macro SYS_BITS
46 #endif
47 #endif
48
49 #if defined(USE_SSE2)
50 #if defined(HAVE_INTRIN_H)
51 #include <intrin.h>
52 #elif defined(HAVE_X86INTRIN_H)
53 #include <x86intrin.h>
54 #elif defined(HAVE_EMMINTRIN_H)
55 #include <xmmintrin.h>
56 #include <emmintrin.h>
57 #endif
58 #endif
59
60 #include "modexp_utils.c"
61 #include "bignum.c"
62
63 void mont_printf(const char *prefix, const uint64_t *mont_number, const MontContext *ctx)
64 {
65 uint8_t* number;
66 unsigned i;
67
68 number = (uint8_t*)calloc(1, ctx->modulus_len);
69
70 if (NULL == mont_number || NULL == ctx || NULL == number)
71 return;
72
73 mont_to_bytes(number, ctx->modulus_len, mont_number, ctx);
74 printf("%s", prefix);
75 for (i=0; i<ctx->modulus_len; i++) {
76 printf("%02X", number[i]);
77 }
78 printf("\n");
79 free(number);
80 }
81
82 /**
83 * Compute the inverse modulo 2⁶⁴ of a 64-bit odd integer.
84 *
85 * See https://crypto.stackexchange.com/questions/47493/how-to-determine-the-multiplicative-inverse-modulo-64-or-other-power-of-two
86 */
87 STATIC uint64_t inverse64(uint64_t a)
88 {
89 uint64_t x;
90
91 assert(1 & a);
92 x = ((a << 1 ^ a) & 4) << 1 ^ a;
93 x += x - a*x*x;
94 x += x - a*x*x;
95 x += x - a*x*x;
96 x += x - a*x*x;
97 assert((x*a & 0xFFFFFFFFFFFFFFFFULL) == 1);
98
99 return x;
100 }
101
102 /*
103 * Compute R² mod N, where R is the smallest power of 2⁶⁴ larger than N.
104 *
105 * @param r2_mod_n The location where the result is stored at
106 * @param n The modulus N
107 * @param nw The number of 64-bit words of both r2_mod_n and n
108 */
109 STATIC void rsquare(uint64_t *r2_mod_n, uint64_t *n, size_t nw)
110 {
111 size_t i;
112 size_t R_bits;
113
114 memset(r2_mod_n, 0, sizeof(uint64_t)*nw);
115
116 /**
117 * Start with R2=1, double 2*bitlen(R) times,
118 * and reduce it as soon as it exceeds n
119 */
120 r2_mod_n[0] = 1;
121 R_bits = nw * sizeof(uint64_t) * 8;
122 for (i=0; i<R_bits*2; i++) {
123 unsigned overflow;
124 size_t j;
125
126 /** Double, by shifting left by one bit **/
127 overflow = (unsigned)(r2_mod_n[nw-1] >> 63);
128 for (j=nw-1; j>0; j--) {
129 r2_mod_n[j] = (r2_mod_n[j] << 1) + (r2_mod_n[j-1] >> 63);
130 }
131 /** Fill-in with zeroes **/
132 r2_mod_n[0] <<= 1;
133
134 /** Subtract n if the result exceeds it **/
135 while (overflow || ge(r2_mod_n, n, nw)) {
136 sub(r2_mod_n, r2_mod_n, n, nw);
137 overflow = 0;
138 }
139 }
140 }
141
142 /*
143 * Montgomery modular multiplication, that is a*b*R mod N.
144 *
145 * @param out The location where the result is stored
146 * @param a The first term (already in Montgomery form, a*R mod N)
147 * @param b The second term (already in Montgomery form, b*R mod N)
148 * @param n The modulus (in normal form), such that R>N
149 * @param m0 Least-significant word of the opposite of the inverse of n modulo R, that is, -n[0]⁻¹ mod R
150 * @param t Temporary, internal result; it must have been created with mont_number(&p,SCRATCHPAD_NR,ctx).
151 * @param nw Number of words making up the 3 integers: out, a, and b.
152 * It also defines R as 2^(64*nw).
153 *
154 * Useful read:
155 * https://web.archive.org/web/20190917203334/https://alicebob.cryptoland.net/understanding-the-montgomery-reduction-algorithm/
156 */
157 #if SCRATCHPAD_NR < 7
158 #error Scratchpad is too small
159 #endif
160 STATIC void mont_mult_generic(uint64_t *out, const uint64_t *a, const uint64_t *b, const uint64_t *n, uint64_t m0, uint64_t *tmp, size_t nw)
161 {
162 size_t i;
163 uint64_t *t, *scratchpad, *t2;
164 unsigned cond;
165
166 /*
167 * tmp is an array of SCRATCHPAD*nw words
168 * We carve out 3 values in it:
169 * - 3*nw words, the value a*b + m*n (we only use 2*nw+1 words)
170 * - 3*nw words, temporary area for computing the product
171 * - nw words, the reduced value with a final subtraction by n
172 */
173 t = tmp;
174 scratchpad = tmp + 3*nw;
175 t2 = scratchpad + 3*nw;
176
177 if (a == b) {
178 square(t, scratchpad, a, nw);
179 } else {
180 product(t, scratchpad, a, b, nw);
181 }
182
183 t[2*nw] = 0; /** MSW **/
184
185 /** Clear lower words (two at a time) **/
186 for (i=0; i<(nw ^ (nw & 1)); i+=2) {
187 uint64_t k0, k1, ti1, prod_lo, prod_hi;
188
189 /** Multiplier for n that will make t[i+0] go 0 **/
190 k0 = t[i] * m0;
191
192 /** Simulate Muladd for digit 0 **/
193 DP_MULT(k0, n[0], prod_lo, prod_hi);
194 prod_lo += t[i];
195 prod_hi += prod_lo < t[i];
196
197 /** Expected digit 1 **/
198 ti1 = t[i+1] + n[1]*k0 + prod_hi;
199
200 /** Multiplier for n that will make t[i+1] go 0 **/
201 k1 = ti1 * m0;
202
203 addmul128(&t[i], scratchpad, n, k0, k1, 2*nw+1-i, nw);
204 }
205
206 /** One left for odd number of words **/
207 if (is_odd(nw)) {
208 addmul(&t[nw-1], nw+2, n, nw, t[nw-1]*m0);
209 }
210
211 assert(t[2*nw] <= 1); /** MSW **/
212
213 /** t[0..nw-1] == 0 **/
214
215 /** Divide by R and possibly subtract n **/
216 sub(t2, &t[nw], n, nw);
217 cond = (unsigned)(t[2*nw] | (uint64_t)ge(&t[nw], n, nw));
218 mod_select(out, t2, &t[nw], cond, (unsigned)nw);
219 }
220
221 STATIC void mont_mult_p256(uint64_t *out, const uint64_t *a, const uint64_t *b, const uint64_t *n, uint64_t m0, uint64_t *tmp, size_t nw)
222 {
223 unsigned i;
224 uint64_t *t, *scratchpad, *t2;
225 unsigned cond;
226 #define WORDS_64 4U
227 #define PREDIV_WORDS_64 (2*WORDS_64+1) /** Size of the number to divide by R **/
228 #define WORDS_32 (WORDS_64*2)
229 #define PREDIV_WORDS_32 (2*PREDIV_WORDS_64)
230
231 #if SYS_BITS == 32
232 uint32_t t32[18];
233 #endif
234
235 assert(nw == 4);
236 assert(m0 == 1);
237
238 t = tmp;
239 scratchpad = tmp + 3*nw;
240 t2 = scratchpad + 3*nw;
241
242 if (a == b) {
243 square(t, scratchpad, a, WORDS_64);
244 } else {
245 product(t, scratchpad, a, b, WORDS_64);
246 }
247
248 t[PREDIV_WORDS_64-1] = 0; /** MSW **/
249
250 #if SYS_BITS == 32
251 for (i=0; i<PREDIV_WORDS_64; i++) {
252 t32[2*i] = (uint32_t)t[i];
253 t32[2*i+1] = (uint32_t)(t[i] >> 32);
254 }
255
256 for (i=0; i<WORDS_32; i++) {
257 uint32_t k, carry;
258 uint64_t prod, k2;
259 unsigned j;
260
261 k = t32[i];
262 k2 = ((uint64_t)k<<32) - k;
263
264 /* p[0] = 2³²-1 */
265 prod = k2 + t32[i+0];
266 t32[i+0] = (uint32_t)prod;
267 carry = (uint32_t)(prod >> 32);
268 /* p[1] = 2³²-1 */
269 prod = k2 + t32[i+1] + carry;
270 t32[i+1] = (uint32_t)prod;
271 carry = (uint32_t)(prod >> 32);
272 /* p[2] = 2³²-1 */
273 prod = k2 + t32[i+2] + carry;
274 t32[i+2] = (uint32_t)prod;
275 carry = (uint32_t)(prod >> 32);
276 /* p[3] = 0 */
277 t32[i+3] += carry;
278 carry = t32[i+3] < carry;
279 /* p[4] = 0 */
280 t32[i+4] += carry;
281 carry = t32[i+4] < carry;
282 /* p[5] = 0 */
283 t32[i+5] += carry;
284 carry = t32[i+5] < carry;
285 /* p[6] = 1 */
286 t32[i+6] += carry;
287 carry = t32[i+6] < carry;
288 t32[i+6] += k;
289 carry |= t32[i+6] < k;
290 /* p[7] = 2³²-1 */
291 prod = k2 + t32[i+7] + carry;
292 t32[i+7] = (uint32_t)prod;
293 carry = (uint32_t)(prod >> 32);
294
295 for (j=WORDS_32; carry; j++) {
296 t32[i+j] += carry;
297 carry = t32[i+j] < carry;
298 }
299 }
300
301 for (i=0; i<PREDIV_WORDS_64; i++) {
302 t[i] = ((uint64_t)t32[2*i+1]<<32) + t32[2*i];
303 }
304
305 #elif SYS_BITS == 64
306
307 for (i=0; i<WORDS_64; i++) {
308 unsigned j;
309 uint64_t carry, k;
310 uint64_t prod_lo, prod_hi;
311
312 k = t[i];
313
314 /* n[0] = 2⁶⁴ - 1 */
315 prod_lo = (uint64_t)(0 - k);
316 prod_hi = k - (k!=0);
317 t[i+0] += prod_lo;
318 prod_hi += t[i+0] < prod_lo;
319 carry = prod_hi;
320
321 /* n[1] = 2³² - 1 */
322 DP_MULT(n[1], k, prod_lo, prod_hi);
323 prod_lo += carry;
324 prod_hi += prod_lo < carry;
325 t[i+1] += prod_lo;
326 prod_hi += t[i+1] < prod_lo;
327 carry = prod_hi;
328
329 /* n[2] = 0 */
330 t[i+2] += carry;
331 carry = t[i+2] < carry;
332
333 /* n[3] = 2⁶⁴ - 2³² + 1 */
334 DP_MULT(n[3], k, prod_lo, prod_hi);
335 prod_lo += carry;
336 prod_hi += prod_lo < carry;
337 t[i+3] += prod_lo;
338 prod_hi += t[i+3] < prod_lo;
339 carry = prod_hi;
340
341 for (j=WORDS_64; carry; j++) {
342 t[i+j] += carry;
343 carry = t[i+j] < carry;
344 }
345 }
346 #else
347 #error You must define the SYS_BITS macro
348 #endif
349
350 assert(t[PREDIV_WORDS_64-1] <= 1); /** MSW **/
351
352 /** t[0..nw-1] == 0 **/
353
354 /** Divide by R and possibly subtract n **/
355 sub(t2, &t[nw], n, WORDS_64);
356 cond = (unsigned)(t[PREDIV_WORDS_64-1] | (uint64_t)ge(&t[WORDS_64], n, WORDS_64));
357 mod_select(out, t2, &t[WORDS_64], cond, WORDS_64);
358
359 #undef WORDS_64
360 #undef PREDIV_WORDS_64
361 #undef WORDS_32
362 #undef PREDIV_WORDS_32
363 }
364
365 STATIC void mont_mult_p384(uint64_t *out, const uint64_t *a, const uint64_t *b, const uint64_t *n, uint64_t m0, uint64_t *tmp, size_t nw)
366 {
367 size_t i;
368 uint64_t *t, *scratchpad, *t2;
369 unsigned cond;
370 #define WORDS_64 6U
371 #define PREDIV_WORDS_64 (2*WORDS_64+1) /** Size of the number to divide by R **/
372 #define WORDS_32 (WORDS_64*2)
373 #define PREDIV_WORDS_32 (2*PREDIV_WORDS_64)
374
375 #if SYS_BITS == 32
376 uint32_t t32[PREDIV_WORDS_32];
377 #endif
378
379 assert(nw == WORDS_64);
380 assert(m0 == 0x0000000100000001ULL);
381
382 t = tmp;
383 scratchpad = tmp + 3*nw;
384 t2 = scratchpad + 3*nw;
385
386 if (a == b) {
387 square(t, scratchpad, a, WORDS_64);
388 } else {
389 product(t, scratchpad, a, b, WORDS_64);
390 }
391
392 t[PREDIV_WORDS_64-1] = 0; /** MSW **/
393
394 #if SYS_BITS == 32
395 for (i=0; i<PREDIV_WORDS_64; i++) {
396 t32[2*i] = (uint32_t)t[i];
397 t32[2*i+1] = (uint32_t)(t[i] >> 32);
398 }
399
400 for (i=0; i<WORDS_32; i++) {
401 uint32_t k, carry;
402 uint64_t prod, k2, k3;
403 unsigned j;
404
405 k = t32[i];
406 k2 = ((uint64_t)k<<32) - k;
407 k3 = k2 - k;
408
409 /* n32[0] = 2³² - 1 */
410 prod = k2 + t32[i+0];
411 t32[i+0] = (uint32_t)prod;
412 carry = (uint32_t)(prod >> 32);
413 /* n32[1] = 0 */
414 prod = (uint64_t)t32[i+1] + carry;
415 t32[i+1] = (uint32_t)prod;
416 carry = (uint32_t)(prod >> 32);
417 /* n32[2] = 0 */
418 prod = (uint64_t)t32[i+2] + carry;
419 t32[i+2] = (uint32_t)prod;
420 carry = (uint32_t)(prod >> 32);
421 /* n32[3] = 2³² - 1 */
422 prod = k2 + t32[i+3] + carry;
423 t32[i+3] = (uint32_t)prod;
424 carry = (uint32_t)(prod >> 32);
425 /* n32[4] = 2³² - 2 */
426 prod = k3 + t32[i+4] + carry;
427 t32[i+4] = (uint32_t)prod;
428 carry = (uint32_t)(prod >> 32);
429 /* n32[5] = 2³² - 1 */
430 prod = k2 + t32[i+5] + carry;
431 t32[i+5] = (uint32_t)prod;
432 carry = (uint32_t)(prod >> 32);
433 /* n32[6] = 2³² - 1 */
434 prod = k2 + t32[i+6] + carry;
435 t32[i+6] = (uint32_t)prod;
436 carry = (uint32_t)(prod >> 32);
437 /* n32[7] = 2³² - 1 */
438 prod = k2 + t32[i+7] + carry;
439 t32[i+7] = (uint32_t)prod;
440 carry = (uint32_t)(prod >> 32);
441 /* n32[8] = 2³² - 1 */
442 prod = k2 + t32[i+8] + carry;
443 t32[i+8] = (uint32_t)prod;
444 carry = (uint32_t)(prod >> 32);
445 /* n32[9] = 2³² - 1 */
446 prod = k2 + t32[i+9] + carry;
447 t32[i+9] = (uint32_t)prod;
448 carry = (uint32_t)(prod >> 32);
449 /* n32[10] = 2³² - 1 */
450 prod = k2 + t32[i+10] + carry;
451 t32[i+10] = (uint32_t)prod;
452 carry = (uint32_t)(prod >> 32);
453 /* n32[11] = 2³² - 1 */
454 prod = k2 + t32[i+11] + carry;
455 t32[i+11] = (uint32_t)prod;
456 carry = (uint32_t)(prod >> 32);
457
458 for (j=WORDS_32; carry; j++) {
459 t32[i+j] += carry;
460 carry = t32[i+j] < carry;
461 }
462 }
463
464 for (i=0; i<PREDIV_WORDS_64; i++) {
465 t[i] = ((uint64_t)t32[2*i+1]<<32) + t32[2*i];
466 }
467
468 #elif SYS_BITS == 64
469
470 for (i=0; i<WORDS_64; i++) {
471 unsigned j;
472 uint64_t carry;
473 uint64_t k, k2_lo, k2_hi;
474 uint64_t prod_lo, prod_hi;
475
476 k = t[i] + (t[i] << 32);
477 k2_lo = (uint64_t)(0 - k);
478 k2_hi = k - (k!=0);
479
480 /* n[0] = 2³² - 1 */
481 DP_MULT(n[0], k, prod_lo, prod_hi);
482 t[i+0] += prod_lo;
483 prod_hi += t[i+0] < prod_lo;
484 carry = prod_hi;
485 /* n[1] = 2⁶⁴ - 2³² */
486 DP_MULT(n[1], k, prod_lo, prod_hi);
487 prod_lo += carry;
488 prod_hi += prod_lo < carry;
489 t[i+1] += prod_lo;
490 prod_hi += t[i+1] < prod_lo;
491 carry = prod_hi;
492 /* n[2] = 2⁶⁴ - 2 */
493 DP_MULT(n[2], k, prod_lo, prod_hi);
494 prod_lo += carry;
495 prod_hi += prod_lo < carry;
496 t[i+2] += prod_lo;
497 prod_hi += t[i+2] < prod_lo;
498 carry = prod_hi;
499 /* n[3] = 2⁶⁴ - 1 */
500 prod_lo = k2_lo;
501 prod_hi = k2_hi;
502 prod_lo += carry;
503 prod_hi += prod_lo < carry;
504 t[i+3] += prod_lo;
505 prod_hi += t[i+3] < prod_lo;
506 carry = prod_hi;
507 /* n[4] = 2⁶⁴ - 1 */
508 prod_lo = k2_lo;
509 prod_hi = k2_hi;
510 prod_lo += carry;
511 prod_hi += prod_lo < carry;
512 t[i+4] += prod_lo;
513 prod_hi += t[i+4] < prod_lo;
514 carry = prod_hi;
515 /* n[5] = 2⁶⁴ - 1 */
516 prod_lo = k2_lo;
517 prod_hi = k2_hi;
518 prod_lo += carry;
519 prod_hi += prod_lo < carry;
520 t[i+5] += prod_lo;
521 prod_hi += t[i+5] < prod_lo;
522 carry = prod_hi;
523
524 for (j=WORDS_64; carry; j++) {
525 t[i+j] += carry;
526 carry = t[i+j] < carry;
527 }
528 }
529 #else
530 #error You must define the SYS_BITS macro
531 #endif
532
533 assert(t[PREDIV_WORDS_64-1] <= 1); /** MSW **/
534
535 /** Words t[0..WORDS_64-1] have all been set to zero **/
536
537 /** Divide by R and possibly subtract n **/
538 sub(t2, &t[WORDS_64], n, WORDS_64);
539 cond = (unsigned)(t[PREDIV_WORDS_64-1] | (uint64_t)ge(&t[WORDS_64], n, WORDS_64));
540 mod_select(out, t2, &t[WORDS_64], cond, WORDS_64);
541
542 #undef WORDS_64
543 #undef PREDIV_WORDS_64
544 #undef WORDS_32
545 #undef PREDIV_WORDS_32
546 }
547
548 STATIC void mont_mult_p521(uint64_t *out, const uint64_t *a, const uint64_t *b, const uint64_t *n, uint64_t m0, uint64_t *tmp, size_t nw)
549 {
550 uint64_t *t, *scratchpad, *s, *tmp1, *tmp2;
551
552 assert(nw == 9);
553 assert(m0 == 1);
554
555 /*
556 * A number in the form:
557 * x*2⁵²¹ + y
558 * is congruent modulo 2⁵²¹-1 to:
559 * x + y
560 */
561
562 /*
563 * tmp is an array of SCRATCHPAD*nw words
564 * We carve out 3 values in it:
565 * - 2*nw words, the value a*b
566 * - 3*nw words, temporary area for computing the product
567 * - nw words, the second term of the addition
568 */
569
570 t = tmp;
571 scratchpad = t + 2*nw;
572 s = scratchpad + 3*nw;
573 tmp1 = scratchpad;
574 tmp2 = scratchpad + nw;
575
576 if (a == b) {
577 square(t, scratchpad, a, 9);
578 } else {
579 product(t, scratchpad, a, b, 9);
580 }
581
582 /* t is a 1042-bit number, occupying 17 words (of the total 18); the MSW (t[16]) only has 18 bits */
583 s[0] = (t[8] >> 9) | (t[9] << 55); t[8] &= 0x1FF;
584 s[1] = (t[9] >> 9) | (t[10] << 55);
585 s[2] = (t[10] >> 9) | (t[11] << 55);
586 s[3] = (t[11] >> 9) | (t[12] << 55);
587 s[4] = (t[12] >> 9) | (t[13] << 55);
588 s[5] = (t[13] >> 9) | (t[14] << 55);
589 s[6] = (t[14] >> 9) | (t[15] << 55);
590 s[7] = (t[15] >> 9) | (t[16] << 55);
591 s[8] = t[16] >> 9;
592
593 add_mod(out, t, s, n, tmp1, tmp2, nw);
594 }
595
596 STATIC void mont_mult_ed448(uint64_t *out, const uint64_t *a, const uint64_t *b, const uint64_t *n, uint64_t m0, uint64_t *tmp, size_t nw)
597 {
598 size_t i;
599 uint64_t *t, *scratchpad, *t2;
600 unsigned cond;
601
602 assert(nw == 7);
603 assert(m0 == 1);
604
605 /*
606 * tmp is an array of SCRATCHPAD*nw words
607 * We carve out 3 values in it:
608 * - 3*nw words, the value a*b + m*n (we only use 2*nw+1 words)
609 * - 3*nw words, temporary area for computing the product
610 * - nw words, the reduced value with a final subtraction by n
611 */
612 t = tmp;
613 scratchpad = tmp + 3*nw;
614 t2 = scratchpad + 3*nw;
615
616 if (a == b) {
617 square(t, scratchpad, a, nw);
618 } else {
619 product(t, scratchpad, a, b, nw);
620 }
621
622 t[2*nw] = 0; /** MSW **/
623
624 /** Clear lower words **/
625 for (i=0; i<7; i++) {
626 uint64_t k, k2_lo, k2_hi;
627 uint64_t carry, j;
628 uint64_t prod_lo, prod_hi;
629
630 k = t[i];
631 k2_lo = (uint64_t)(0 - k);
632 k2_hi = k - (k!=0);
633
634 /* n[0] = 2⁶⁴ - 1 */
635 prod_lo = k2_lo;
636 prod_hi = k2_hi;
637 t[i+0] += prod_lo;
638 prod_hi += t[i+0] < prod_lo;
639 carry = prod_hi;
640
641 /* n[1] = 2⁶⁴ - 1 */
642 prod_lo = k2_lo;
643 prod_hi = k2_hi;
644 prod_lo += carry;
645 prod_hi += prod_lo < carry;
646 t[i+1] += prod_lo;
647 prod_hi += t[i+1] < prod_lo;
648 carry = prod_hi;
649
650 /* n[2] = 2⁶⁴ - 1 */
651 prod_lo = k2_lo;
652 prod_hi = k2_hi;
653 prod_lo += carry;
654 prod_hi += prod_lo < carry;
655 t[i+2] += prod_lo;
656 prod_hi += t[i+2] < prod_lo;
657 carry = prod_hi;
658
659 /* n[3] = 2⁶⁴ - 2³² - 1 */
660 DP_MULT(n[3], k, prod_lo, prod_hi);
661 prod_lo += carry;
662 prod_hi += prod_lo < carry;
663 t[i+3] += prod_lo;
664 prod_hi += t[i+3] < prod_lo;
665 carry = prod_hi;
666
667 /* n[4] = 2⁶⁴ - 1 */
668 prod_lo = k2_lo;
669 prod_hi = k2_hi;
670 prod_lo += carry;
671 prod_hi += prod_lo < carry;
672 t[i+4] += prod_lo;
673 prod_hi += t[i+4] < prod_lo;
674 carry = prod_hi;
675
676 /* n[5] = 2⁶⁴ - 1 */
677 prod_lo = k2_lo;
678 prod_hi = k2_hi;
679 prod_lo += carry;
680 prod_hi += prod_lo < carry;
681 t[i+5] += prod_lo;
682 prod_hi += t[i+5] < prod_lo;
683 carry = prod_hi;
684
685 /* n[6] = 2⁶⁴ - 1 */
686 prod_lo = k2_lo;
687 prod_hi = k2_hi;
688 prod_lo += carry;
689 prod_hi += prod_lo < carry;
690 t[i+6] += prod_lo;
691 prod_hi += t[i+6] < prod_lo;
692 carry = prod_hi;
693
694 for (j=7; carry; j++) {
695 t[i+j] += carry;
696 carry = t[i+j] < carry;
697 }
698
699 assert(j <= (15-i));
700 }
701
702 assert(t[2*nw] <= 1); /** MSW **/
703
704 /** t[0..nw-1] == 0 **/
705
706 /** Divide by R and possibly subtract n **/
707 sub(t2, &t[nw], n, nw);
708 cond = (unsigned)(t[2*nw] | (uint64_t)ge(&t[nw], n, nw));
709 mod_select(out, t2, &t[nw], cond, (unsigned)nw);
710 }
711
712
713 /* ---- PUBLIC FUNCTIONS ---- */
714
715 void mont_context_free(MontContext *ctx)
716 {
717 if (NULL == ctx)
718 return;
719 free(ctx->one);
720 free(ctx->r2_mod_n);
721 free(ctx->r_mod_n);
722 free(ctx->modulus);
723 free(ctx->modulus_min_2);
724 free(ctx);
725 }
726
727 /*
728 * Return how many bytes a big endian multi-word number takes in memory.
729 */
730 size_t mont_bytes(const MontContext *ctx)
731 {
732 if (NULL == ctx)
733 return 0;
734 return ctx->bytes;
735 }
736
737 /*
738 * Allocate memory for an array of numbers in Montgomery form
739 * and initialize it to 0.
740 *
741 * @param out The location where the address of the newly allocated
742 * array will be placed in.
743 * The caller is responsible for deallocating the memory
744 * using free().
745 * @param count How many numbers the array contains.
746 * @param ctx The Montgomery context.
747 * @return 0 if successful, the relevant error code otherwise.
748 *
749 */
750 int mont_number(uint64_t **out, unsigned count, const MontContext *ctx)
751 {
752 if (NULL == out || NULL == ctx)
753 return ERR_NULL;
754
755 *out = (uint64_t*)calloc(count * ctx->words, sizeof(uint64_t));
756 if (NULL == *out)
757 return ERR_MEMORY;
758
759 return 0;
760 }
761
762 int mont_random_number(uint64_t **out, unsigned count, uint64_t seed, const MontContext *ctx)
763 {
764 int res;
765 unsigned i;
766 uint64_t *number;
767
768 res = mont_number(out, count, ctx);
769 if (res)
770 return res;
771
772 number = *out;
773 expand_seed(seed, (uint8_t*)number, count * ctx->bytes);
774 for (i=0; i<count; i++, number += ctx->words) {
775 number[ctx->words-1] = 0;
776 }
777 return 0;
778 }
779
780 /*
781 * Transform a big endian-encoded number into Montgomery form, by performing memory allocation.
782 *
783 * @param out The location where the pointer to the newly allocated memory will be put in.
784 * The memory will contain the number encoded in Montgomery form.
785 * The caller is responsible for deallocating the memory.
786 * @param ctx Montgomery context, as created by mont_context_init().
787 * @param number The big endian-encoded number to transform, strictly smaller than the modulus.
788 * @param len The length of the big-endian number in bytes (this may be
789 * smaller than the output of mont_bytes(ctx)).
790 * @return 0 in case of success, the relevant error code otherwise.
791 */
792 int mont_from_bytes(uint64_t **out, const uint8_t *number, size_t len, const MontContext *ctx)
793 {
794 uint64_t *encoded = NULL;
795 uint64_t *tmp1 = NULL;
796 uint64_t *scratchpad = NULL;
797 int res = 0;
798
799 if (NULL == out || NULL == ctx || NULL == number)
800 return ERR_NULL;
801
802 *out = NULL;
803
804 /** Removing leading zeroes but avoid a zero-length string **/
805 if (0 == len)
806 return ERR_NOT_ENOUGH_DATA;
807 while (len>1 && *number==0) {
808 len--;
809 number++;
810 }
811
812 if (ctx->bytes < len)
813 return ERR_VALUE;
814
815 /** The caller will deallocate this memory **/
816 *out = encoded = (uint64_t*)calloc(ctx->words, sizeof(uint64_t));
817 if (NULL == encoded)
818 return ERR_MEMORY;
819
820 /** Input number, loaded in words **/
821 tmp1 = (uint64_t*)calloc(ctx->words, sizeof(uint64_t));
822 if (NULL == tmp1) {
823 res = ERR_MEMORY;
824 goto cleanup;
825 }
826 bytes_to_words(tmp1, ctx->words, number, len);
827
828 /** Make sure number<modulus **/
829 if (ge(tmp1, ctx->modulus, ctx->words)) {
830 res = ERR_VALUE;
831 goto cleanup;
832 }
833
834 /** Scratchpad **/
835 scratchpad = (uint64_t*)calloc(SCRATCHPAD_NR, ctx->words*sizeof(uint64_t));
836 if (NULL == scratchpad) {
837 res = ERR_MEMORY;
838 goto cleanup;
839 }
840
841 if (ctx->modulus_type != ModulusP521)
842 mont_mult_generic(encoded, tmp1, ctx->r2_mod_n, ctx->modulus, ctx->m0, scratchpad, ctx->words);
843 else
844 mont_copy(encoded, tmp1, ctx);
845 res = 0;
846
847 cleanup:
848 free(scratchpad);
849 free(tmp1);
850 if (res != 0) {
851 free(encoded);
852 *out = NULL;
853 }
854 return res;
855 }
856
857 /*
858 * Transform a number from Montgomery representation to big endian-encoding.
859 *
860 * @param number The location where the number will be put in, encoded
861 * in big-endian form and with zero padding on the left.
862 * @param len Space allocate at number, at least ctx->modulus_len bytes.
863 * @param ctx The address of the Montgomery context.
864 * @param mont_number The number in Montgomery form to transform.
865 * @return 0 if successful, the relevant error code otherwise.
866 */
867 int mont_to_bytes(uint8_t *number, size_t len, const uint64_t* mont_number, const MontContext *ctx)
868 {
869 uint64_t *tmp1 = NULL;
870 uint64_t *scratchpad = NULL;
871 int res;
872
873 if (NULL == number || NULL == ctx || NULL == mont_number)
874 return ERR_NULL;
875
876 if (len < ctx->modulus_len)
877 return ERR_NOT_ENOUGH_DATA;
878
879 /** Number in normal form, but still in words **/
880 tmp1 = (uint64_t*)calloc(ctx->words, sizeof(uint64_t));
881 if (NULL == tmp1)
882 return ERR_MEMORY;
883
884 /** Scratchpad **/
885 scratchpad = (uint64_t*)calloc(SCRATCHPAD_NR, ctx->words*sizeof(uint64_t));
886 if (NULL == scratchpad) {
887 free(tmp1);
888 return ERR_MEMORY;
889 }
890
891 if (ctx->modulus_type != ModulusP521)
892 mont_mult_generic(tmp1, mont_number, ctx->one, ctx->modulus, ctx->m0, scratchpad, ctx->words);
893 else
894 mont_copy(tmp1, mont_number, ctx);
895 res = words_to_bytes(number, len, tmp1, ctx->words);
896
897 free(scratchpad);
898 free(tmp1);
899 return res;
900 }
901
902 /*
903 * Add two numbers in Montgomery representation.
904 *
905 * @param out The location where the result will be stored; it must have been created with mont_number(&p,1,ctx).
906 * @param a The first term.
907 * @param b The second term.
908 * @param tmp Temporary, internal result; it must have been created with mont_number(&p,SCRATCHPAD_NR,ctx).
909 * @param ctx The Montgomery context.
910 * @return 0 for success, the relevant error code otherwise.
911 */
912 int mont_add(uint64_t* out, const uint64_t* a, const uint64_t* b, uint64_t *tmp, const MontContext *ctx)
913 {
914 if (NULL == out || NULL == a || NULL == b || NULL == tmp || NULL == ctx)
915 return ERR_NULL;
916 add_mod(out, a, b, ctx->modulus, tmp, tmp + ctx->words, ctx->words);
917 return 0;
918 }
919
920 /*
921 * Multiply two numbers in Montgomery representation.
922 *
923 * @param out The location where the result will be stored at; it must have been created with mont_number(&p,1,ctx)
924 * @param a The first term.
925 * @param b The second term.
926 * @param tmp Temporary, internal result; it must have been created with mont_number(&p,SCRATCHPAD_NR,ctx).
927 * @param ctx The Montgomery context.
928 * @return 0 for success, the relevant error code otherwise.
929 */
930 int mont_mult(uint64_t* out, const uint64_t* a, const uint64_t *b, uint64_t *tmp, const MontContext *ctx)
931 {
932 if (NULL == out || NULL == a || NULL == b || NULL == tmp || NULL == ctx)
933 return ERR_NULL;
934
935 switch (ctx->modulus_type) {
936 case ModulusP256:
937 mont_mult_p256(out, a, b, ctx->modulus, ctx->m0, tmp, ctx->words);
938 break;
939 case ModulusP384:
940 mont_mult_p384(out, a, b, ctx->modulus, ctx->m0, tmp, ctx->words);
941 break;
942 case ModulusP521:
943 mont_mult_p521(out, a, b, ctx->modulus, ctx->m0, tmp, ctx->words);
944 break;
945 case ModulusEd448:
946 mont_mult_ed448(out, a, b, ctx->modulus, ctx->m0, tmp, ctx->words);
947 break;
948 case ModulusGeneric:
949 mont_mult_generic(out, a, b, ctx->modulus, ctx->m0, tmp, ctx->words);
950 break;
951 default:
952 return ERR_MODULUS;
953 }
954
955 return 0;
956 }
957
958 /*
959 * Subtract integer b from a.
960 *
961 * @param out The location where the result is stored at; it must have been created with mont_number(&p,1,ctx).
962 * It can be the same as either a or b.
963 * @param a The number it will be subtracted from.
964 * @param b The number to subtract.
965 * @param tmp Temporary, internal result; it must have been created with mont_number(&p,2,ctx).
966 * @param ctx The Montgomery context.
967 * @return 0 for success, the relevant error code otherwise.
968 */
969 int mont_sub(uint64_t *out, const uint64_t *a, const uint64_t *b, uint64_t *tmp, const MontContext *ctx)
970 {
971 if (NULL == out || NULL == a || NULL == b || NULL == tmp || NULL == ctx)
972 return ERR_NULL;
973
974 return sub_mod(out, a, b, ctx->modulus, tmp, tmp + ctx->words, ctx->words);
975 }
976
977 /*
978 * Compute the modular inverse of an integer in Montgomery form.
979 *
980 * Condition: the modulus defining the Montgomery context MUST BE a non-secret prime number.
981 *
982 * @param out The location where the result will be stored at; it must have
983 * been allocated with mont_number(&p, 1, ctx).
984 * @param a The number to compute the modular inverse of, already in Montgomery form.
985 * @param ctx The Montgomery context.
986 * @return 0 for success, the relevant error code otherwise.
987 */
988 int mont_inv_prime(uint64_t *out, uint64_t *a, const MontContext *ctx)
989 {
990 unsigned idx_word;
991 uint64_t bit;
992 uint64_t *tmp1 = NULL;
993 uint64_t *scratchpad = NULL;
994 uint64_t *exponent = NULL;
995 int res;
996
997 if (NULL == out || NULL == a || NULL == ctx)
998 return ERR_NULL;
999
1000 tmp1 = (uint64_t*)calloc(ctx->words, sizeof(uint64_t));
1001 if (NULL == tmp1)
1002 return ERR_MEMORY;
1003
1004 scratchpad = (uint64_t*)calloc(SCRATCHPAD_NR, ctx->words*sizeof(uint64_t));
1005 if (NULL == scratchpad) {
1006 res = ERR_MEMORY;
1007 goto cleanup;
1008 }
1009
1010 /** Exponent is guaranteed to be >0 **/
1011 exponent = ctx->modulus_min_2;
1012
1013 /* Find most significant bit */
1014 idx_word = ctx->words-1;
1015 for (;;) {
1016 if (exponent[idx_word] != 0)
1017 break;
1018 if (idx_word-- == 0)
1019 break;
1020 }
1021 for (bit = (uint64_t)1U << 63; 0 == (exponent[idx_word] & bit); bit>>=1);
1022
1023 /* Start from 1 (in Montgomery form, which is R mod N) */
1024 memcpy(out, ctx->r_mod_n, ctx->bytes);
1025
1026 /** Left-to-right exponentiation **/
1027 for (;;) {
1028 while (bit > 0) {
1029 mont_mult(tmp1, out, out, scratchpad, ctx);
1030 if (exponent[idx_word] & bit) {
1031 mont_mult(out, tmp1, a, scratchpad, ctx);
1032 } else {
1033 memcpy(out, tmp1, ctx->bytes);
1034 }
1035 bit >>= 1;
1036 }
1037 if (idx_word-- == 0)
1038 break;
1039 bit = (uint64_t)1 << 63;
1040 }
1041 res = 0;
1042
1043 cleanup:
1044 free(tmp1);
1045 free(scratchpad);
1046 return res;
1047 }
1048
1049 /*
1050 * Assign a value to a number in Montgomery form.
1051 *
1052 * @param out The location where the result is stored at; it must have been created with mont_number(&p,1,ctx).
1053 * @param x The value to set.
1054 * @param ctx The Montgomery context.
1055 * @return 0 for success, the relevant error code otherwise.
1056 */
1057 int mont_set(uint64_t *out, uint64_t x, const MontContext *ctx)
1058 {
1059 uint64_t *tmp, *scratchpad;
1060
1061 if (NULL == out || NULL == ctx)
1062 return ERR_NULL;
1063
1064 if (x == 0) {
1065 memset(out, 0, ctx->bytes);
1066 return 0;
1067 }
1068 if (x == 1) {
1069 mont_copy(out, ctx->r_mod_n, ctx);
1070 return 0;
1071 }
1072
1073 tmp = (uint64_t*)calloc(ctx->words, sizeof(uint64_t));
1074 if (NULL == tmp)
1075 return ERR_MEMORY;
1076 tmp[0] = x;
1077
1078 scratchpad = (uint64_t*)calloc(SCRATCHPAD_NR, ctx->words*sizeof(uint64_t));
1079 if (NULL == scratchpad) {
1080 free(tmp);
1081 return ERR_MEMORY;
1082 }
1083
1084 if (ctx->modulus_type != ModulusP521)
1085 mont_mult_generic(out, tmp, ctx->r2_mod_n, ctx->modulus, ctx->m0, scratchpad, ctx->words);
1086 else
1087 mont_copy(out, tmp, ctx);
1088
1089 free(tmp);
1090 free(scratchpad);
1091
1092 return 0;
1093 }
1094
1095 static int cmp_modulus(const uint8_t *mod1, size_t mod1_len, const uint8_t *mod2, size_t mod2_len)
1096 {
1097 size_t diff;
1098
1099 if (mod1_len > mod2_len) {
1100 diff = mod1_len - mod2_len;
1101 if (0 != memcmp(mod1+diff, mod2, mod2_len))
1102 return -1;
1103 if (NULL != memchr_not(mod1, 0, diff))
1104 return -1;
1105 } else {
1106 diff = mod2_len - mod1_len;
1107 if (0 != memcmp(mod2+diff, mod1, mod1_len))
1108 return -1;
1109 if (NULL != memchr_not(mod2, 0, diff))
1110 return -1;
1111 }
1112 return 0;
1113 }
1114
1115 /*
1116 * Create a new context for the Montgomery and the given odd modulus.
1117 *
1118 * @param out The locate where the pointer to the newly allocated data will be stored at.
1119 * The memory will contain the new Montgomery context.
1120 * @param modulus The modulus encoded in big endian form.
1121 * @param mod_len The length of the modulus in bytes.
1122 * @return 0 for success, the appropriate code otherwise.
1123 */
1124 int mont_context_init(MontContext **out, const uint8_t *modulus, size_t mod_len)
1125 {
1126 const uint8_t p256_mod[32] = {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01,
1127 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1128 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
1129 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1130 const uint8_t p384_mod[48] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1131 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1132 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1133 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
1134 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
1135 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF};
1136 const uint8_t p521_mod[66] = {0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1137 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1138 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1139 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1140 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1141 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1142 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1143 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1144 0xFF, 0xFF};
1145 const uint8_t ed448_mod[56] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1146 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1147 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1148 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF,
1149 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1150 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1151 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1152 uint64_t *scratchpad = NULL;
1153 MontContext *ctx;
1154 int res;
1155
1156 if (NULL == out || NULL == modulus)
1157 return ERR_NULL;
1158
1159 /** Consume leading zeros **/
1160 while (mod_len>0 && *modulus==0) {
1161 modulus++;
1162 mod_len--;
1163 }
1164 if (0 == mod_len)
1165 return ERR_MODULUS;
1166
1167 /** Ensure modulus is odd and at least 3, otherwise we can't compute its inverse over B **/
1168 if (is_even(modulus[mod_len-1]))
1169 return ERR_MODULUS;
1170 if (mod_len==1 && modulus[0]==1)
1171 return ERR_MODULUS;
1172
1173 *out = ctx = (MontContext*)calloc(1, sizeof(MontContext));
1174 if (NULL == ctx)
1175 return ERR_MEMORY;
1176
1177 /* Check if the modulus has a special form */
1178 /* For P-521, modulo reduction is very simple so the Montgomery
1179 * representation is not actually used.
1180 */
1181 ctx->modulus_type = ModulusGeneric;
1182 switch (mod_len) {
1183 case sizeof(p256_mod):
1184 if (0 == cmp_modulus(modulus, mod_len, p256_mod, sizeof(p256_mod))) {
1185 ctx->modulus_type = ModulusP256;
1186 }
1187 break;
1188 case sizeof(p384_mod):
1189 if (0 == cmp_modulus(modulus, mod_len, p384_mod, sizeof(p384_mod))) {
1190 ctx->modulus_type = ModulusP384;
1191 }
1192 break;
1193 case sizeof(p521_mod):
1194 if (0 == cmp_modulus(modulus, mod_len, p521_mod, sizeof(p521_mod))) {
1195 ctx->modulus_type = ModulusP521;
1196 }
1197 break;
1198 case sizeof(ed448_mod):
1199 if (0 == cmp_modulus(modulus, mod_len, ed448_mod, sizeof(ed448_mod))) {
1200 ctx->modulus_type = ModulusEd448;
1201 }
1202 break;
1203 }
1204
1205 ctx->words = ((unsigned)mod_len + 7) / 8;
1206 ctx->bytes = (unsigned)(ctx->words * sizeof(uint64_t));
1207 ctx->modulus_len = (unsigned)mod_len;
1208
1209 /** Load modulus N **/
1210 ctx->modulus = (uint64_t*)calloc(ctx->words, sizeof(uint64_t));
1211 if (0 == ctx->modulus) {
1212 res = ERR_MEMORY;
1213 goto cleanup;
1214 }
1215 bytes_to_words(ctx->modulus, ctx->words, modulus, mod_len);
1216
1217 /** Prepare 1 **/
1218 ctx->one = (uint64_t*)calloc(ctx->words, sizeof(uint64_t));
1219 if (NULL == ctx->one) {
1220 res = ERR_MEMORY;
1221 goto cleanup;
1222 }
1223 ctx->one[0] = 1;
1224
1225 /** Pre-compute R² mod N **/
1226 /** Pre-compute -n[0]⁻¹ mod R **/
1227 ctx->r2_mod_n = (uint64_t*)calloc(ctx->words, sizeof(uint64_t));
1228 if (0 == ctx->r2_mod_n) {
1229 res = ERR_MEMORY;
1230 goto cleanup;
1231 }
1232 if (ctx->modulus_type != ModulusP521) {
1233 rsquare(ctx->r2_mod_n, ctx->modulus, ctx->words);
1234 ctx->m0 = inverse64(~ctx->modulus[0]+1);
1235 } else {
1236 memcpy(ctx->r2_mod_n, ctx->one, ctx->words * sizeof(uint64_t));
1237 ctx->m0 = 1U;
1238 }
1239
1240 /** Pre-compute R mod N **/
1241 ctx->r_mod_n = (uint64_t*)calloc(ctx->words, sizeof(uint64_t));
1242 if (NULL == ctx->r_mod_n) {
1243 res = ERR_MEMORY;
1244 goto cleanup;
1245 }
1246 scratchpad = (uint64_t*)calloc(SCRATCHPAD_NR, ctx->words*sizeof(uint64_t));
1247 if (NULL == scratchpad) {
1248 res = ERR_MEMORY;
1249 goto cleanup;
1250 }
1251 if (ctx->modulus_type != ModulusP521)
1252 mont_mult_generic(ctx->r_mod_n, ctx->one, ctx->r2_mod_n, ctx->modulus, ctx->m0, scratchpad, ctx->words);
1253 else
1254 memcpy(ctx->r_mod_n, ctx->one, ctx->words * sizeof(uint64_t));
1255
1256 /** Pre-compute modulus - 2 **/
1257 /** Modulus is guaranteed to be >= 3 **/
1258 ctx->modulus_min_2 = (uint64_t*)calloc(ctx->words, sizeof(uint64_t));
1259 if (NULL == ctx->modulus_min_2) {
1260 res = ERR_MEMORY;
1261 goto cleanup;
1262 }
1263 sub(ctx->modulus_min_2, ctx->modulus, ctx->one, ctx->words);
1264 sub(ctx->modulus_min_2, ctx->modulus_min_2, ctx->one, ctx->words);
1265
1266 res = 0;
1267
1268 cleanup:
1269 free(scratchpad);
1270 if (res != 0) {
1271 mont_context_free(ctx);
1272 }
1273 return res;
1274 }
1275
1276 int mont_is_zero(const uint64_t *a, const MontContext *ctx)
1277 {
1278 unsigned i;
1279 uint64_t sum = 0;
1280
1281 if (NULL == a || NULL == ctx)
1282 return -1;
1283
1284 for (i=0; i<ctx->words; i++) {
1285 sum |= *a++;
1286 }
1287
1288 return (sum == 0);
1289 }
1290
1291 int mont_is_one(const uint64_t *a, const MontContext *ctx)
1292 {
1293 unsigned i;
1294 uint64_t sum = 0;
1295
1296 if (NULL == a || NULL == ctx)
1297 return -1;
1298
1299 for (i=0; i<ctx->words; i++) {
1300 sum |= a[i] ^ ctx->r_mod_n[i];
1301 }
1302
1303 return (sum == 0);
1304 }
1305
1306 int mont_is_equal(const uint64_t *a, const uint64_t *b, const MontContext *ctx)
1307 {
1308 unsigned i;
1309 uint64_t result = 0;
1310
1311 if (NULL == a || NULL == b || NULL == ctx)
1312 return -1;
1313
1314 for (i=0; i<ctx->words; i++) {
1315 result |= *a++ ^ *b++;
1316 }
1317
1318 return (result == 0);
1319 }
1320
1321 int mont_copy(uint64_t *out, const uint64_t *a, const MontContext *ctx)
1322 {
1323 unsigned i;
1324
1325 if (NULL == out || NULL == a || NULL == ctx)
1326 return ERR_NULL;
1327
1328 for (i=0; i<ctx->words; i++) {
1329 *out++ = *a++;
1330 }
1331
1332 return 0;
1333 }