/src/mozilla-central/security/nss/lib/freebl/mpi/mpi.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * mpi.c |
3 | | * |
4 | | * Arbitrary precision integer arithmetic library |
5 | | * |
6 | | * This Source Code Form is subject to the terms of the Mozilla Public |
7 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
8 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
9 | | |
10 | | #include "mpi-priv.h" |
11 | | #if defined(OSF1) |
12 | | #include <c_asm.h> |
13 | | #endif |
14 | | |
15 | | #if defined(__arm__) && \ |
16 | | ((defined(__thumb__) && !defined(__thumb2__)) || defined(__ARM_ARCH_3__)) |
17 | | /* 16-bit thumb or ARM v3 doesn't work inlined assember version */ |
18 | | #undef MP_ASSEMBLY_MULTIPLY |
19 | | #undef MP_ASSEMBLY_SQUARE |
20 | | #endif |
21 | | |
22 | | #if MP_LOGTAB |
23 | | /* |
24 | | A table of the logs of 2 for various bases (the 0 and 1 entries of |
25 | | this table are meaningless and should not be referenced). |
26 | | |
27 | | This table is used to compute output lengths for the mp_toradix() |
28 | | function. Since a number n in radix r takes up about log_r(n) |
29 | | digits, we estimate the output size by taking the least integer |
30 | | greater than log_r(n), where: |
31 | | |
32 | | log_r(n) = log_2(n) * log_r(2) |
33 | | |
34 | | This table, therefore, is a table of log_r(2) for 2 <= r <= 36, |
35 | | which are the output bases supported. |
36 | | */ |
37 | | #include "logtab.h" |
38 | | #endif |
39 | | |
40 | | #ifdef CT_VERIF |
41 | | #include <valgrind/memcheck.h> |
42 | | #endif |
43 | | |
44 | | /* {{{ Constant strings */ |
45 | | |
46 | | /* Constant strings returned by mp_strerror() */ |
47 | | static const char *mp_err_string[] = { |
48 | | "unknown result code", /* say what? */ |
49 | | "boolean true", /* MP_OKAY, MP_YES */ |
50 | | "boolean false", /* MP_NO */ |
51 | | "out of memory", /* MP_MEM */ |
52 | | "argument out of range", /* MP_RANGE */ |
53 | | "invalid input parameter", /* MP_BADARG */ |
54 | | "result is undefined" /* MP_UNDEF */ |
55 | | }; |
56 | | |
57 | | /* Value to digit maps for radix conversion */ |
58 | | |
59 | | /* s_dmap_1 - standard digits and letters */ |
60 | | static const char *s_dmap_1 = |
61 | | "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"; |
62 | | |
63 | | /* }}} */ |
64 | | |
65 | | /* {{{ Default precision manipulation */ |
66 | | |
67 | | /* Default precision for newly created mp_int's */ |
68 | | static mp_size s_mp_defprec = MP_DEFPREC; |
69 | | |
70 | | mp_size |
71 | | mp_get_prec(void) |
72 | 0 | { |
73 | 0 | return s_mp_defprec; |
74 | 0 |
|
75 | 0 | } /* end mp_get_prec() */ |
76 | | |
77 | | void |
78 | | mp_set_prec(mp_size prec) |
79 | 0 | { |
80 | 0 | if (prec == 0) |
81 | 0 | s_mp_defprec = MP_DEFPREC; |
82 | 0 | else |
83 | 0 | s_mp_defprec = prec; |
84 | 0 |
|
85 | 0 | } /* end mp_set_prec() */ |
86 | | |
87 | | /* }}} */ |
88 | | |
89 | | #ifdef CT_VERIF |
90 | | void |
91 | | mp_taint(mp_int *mp) |
92 | | { |
93 | | size_t i; |
94 | | for (i = 0; i < mp->used; ++i) { |
95 | | VALGRIND_MAKE_MEM_UNDEFINED(&(mp->dp[i]), sizeof(mp_digit)); |
96 | | } |
97 | | } |
98 | | |
99 | | void |
100 | | mp_untaint(mp_int *mp) |
101 | | { |
102 | | size_t i; |
103 | | for (i = 0; i < mp->used; ++i) { |
104 | | VALGRIND_MAKE_MEM_DEFINED(&(mp->dp[i]), sizeof(mp_digit)); |
105 | | } |
106 | | } |
107 | | #endif |
108 | | |
109 | | /*------------------------------------------------------------------------*/ |
110 | | /* {{{ mp_init(mp) */ |
111 | | |
112 | | /* |
113 | | mp_init(mp) |
114 | | |
115 | | Initialize a new zero-valued mp_int. Returns MP_OKAY if successful, |
116 | | MP_MEM if memory could not be allocated for the structure. |
117 | | */ |
118 | | |
119 | | mp_err |
120 | | mp_init(mp_int *mp) |
121 | 0 | { |
122 | 0 | return mp_init_size(mp, s_mp_defprec); |
123 | 0 |
|
124 | 0 | } /* end mp_init() */ |
125 | | |
126 | | /* }}} */ |
127 | | |
128 | | /* {{{ mp_init_size(mp, prec) */ |
129 | | |
130 | | /* |
131 | | mp_init_size(mp, prec) |
132 | | |
133 | | Initialize a new zero-valued mp_int with at least the given |
134 | | precision; returns MP_OKAY if successful, or MP_MEM if memory could |
135 | | not be allocated for the structure. |
136 | | */ |
137 | | |
138 | | mp_err |
139 | | mp_init_size(mp_int *mp, mp_size prec) |
140 | 0 | { |
141 | 0 | ARGCHK(mp != NULL && prec > 0, MP_BADARG); |
142 | 0 |
|
143 | 0 | prec = MP_ROUNDUP(prec, s_mp_defprec); |
144 | 0 | if ((DIGITS(mp) = s_mp_alloc(prec, sizeof(mp_digit))) == NULL) |
145 | 0 | return MP_MEM; |
146 | 0 | |
147 | 0 | SIGN(mp) = ZPOS; |
148 | 0 | USED(mp) = 1; |
149 | 0 | ALLOC(mp) = prec; |
150 | 0 |
|
151 | 0 | return MP_OKAY; |
152 | 0 |
|
153 | 0 | } /* end mp_init_size() */ |
154 | | |
155 | | /* }}} */ |
156 | | |
157 | | /* {{{ mp_init_copy(mp, from) */ |
158 | | |
159 | | /* |
160 | | mp_init_copy(mp, from) |
161 | | |
162 | | Initialize mp as an exact copy of from. Returns MP_OKAY if |
163 | | successful, MP_MEM if memory could not be allocated for the new |
164 | | structure. |
165 | | */ |
166 | | |
167 | | mp_err |
168 | | mp_init_copy(mp_int *mp, const mp_int *from) |
169 | 0 | { |
170 | 0 | ARGCHK(mp != NULL && from != NULL, MP_BADARG); |
171 | 0 |
|
172 | 0 | if (mp == from) |
173 | 0 | return MP_OKAY; |
174 | 0 | |
175 | 0 | if ((DIGITS(mp) = s_mp_alloc(ALLOC(from), sizeof(mp_digit))) == NULL) |
176 | 0 | return MP_MEM; |
177 | 0 | |
178 | 0 | s_mp_copy(DIGITS(from), DIGITS(mp), USED(from)); |
179 | 0 | USED(mp) = USED(from); |
180 | 0 | ALLOC(mp) = ALLOC(from); |
181 | 0 | SIGN(mp) = SIGN(from); |
182 | 0 |
|
183 | 0 | return MP_OKAY; |
184 | 0 |
|
185 | 0 | } /* end mp_init_copy() */ |
186 | | |
187 | | /* }}} */ |
188 | | |
189 | | /* {{{ mp_copy(from, to) */ |
190 | | |
191 | | /* |
192 | | mp_copy(from, to) |
193 | | |
194 | | Copies the mp_int 'from' to the mp_int 'to'. It is presumed that |
195 | | 'to' has already been initialized (if not, use mp_init_copy() |
196 | | instead). If 'from' and 'to' are identical, nothing happens. |
197 | | */ |
198 | | |
199 | | mp_err |
200 | | mp_copy(const mp_int *from, mp_int *to) |
201 | 0 | { |
202 | 0 | ARGCHK(from != NULL && to != NULL, MP_BADARG); |
203 | 0 |
|
204 | 0 | if (from == to) |
205 | 0 | return MP_OKAY; |
206 | 0 | |
207 | 0 | { /* copy */ |
208 | 0 | mp_digit *tmp; |
209 | 0 |
|
210 | 0 | /* |
211 | 0 | If the allocated buffer in 'to' already has enough space to hold |
212 | 0 | all the used digits of 'from', we'll re-use it to avoid hitting |
213 | 0 | the memory allocater more than necessary; otherwise, we'd have |
214 | 0 | to grow anyway, so we just allocate a hunk and make the copy as |
215 | 0 | usual |
216 | 0 | */ |
217 | 0 | if (ALLOC(to) >= USED(from)) { |
218 | 0 | s_mp_setz(DIGITS(to) + USED(from), ALLOC(to) - USED(from)); |
219 | 0 | s_mp_copy(DIGITS(from), DIGITS(to), USED(from)); |
220 | 0 |
|
221 | 0 | } else { |
222 | 0 | if ((tmp = s_mp_alloc(ALLOC(from), sizeof(mp_digit))) == NULL) |
223 | 0 | return MP_MEM; |
224 | 0 | |
225 | 0 | s_mp_copy(DIGITS(from), tmp, USED(from)); |
226 | 0 |
|
227 | 0 | if (DIGITS(to) != NULL) { |
228 | 0 | s_mp_setz(DIGITS(to), ALLOC(to)); |
229 | 0 | s_mp_free(DIGITS(to)); |
230 | 0 | } |
231 | 0 |
|
232 | 0 | DIGITS(to) = tmp; |
233 | 0 | ALLOC(to) = ALLOC(from); |
234 | 0 | } |
235 | 0 |
|
236 | 0 | /* Copy the precision and sign from the original */ |
237 | 0 | USED(to) = USED(from); |
238 | 0 | SIGN(to) = SIGN(from); |
239 | 0 | } /* end copy */ |
240 | 0 |
|
241 | 0 | return MP_OKAY; |
242 | 0 |
|
243 | 0 | } /* end mp_copy() */ |
244 | | |
245 | | /* }}} */ |
246 | | |
247 | | /* {{{ mp_exch(mp1, mp2) */ |
248 | | |
249 | | /* |
250 | | mp_exch(mp1, mp2) |
251 | | |
252 | | Exchange mp1 and mp2 without allocating any intermediate memory |
253 | | (well, unless you count the stack space needed for this call and the |
254 | | locals it creates...). This cannot fail. |
255 | | */ |
256 | | |
257 | | void |
258 | | mp_exch(mp_int *mp1, mp_int *mp2) |
259 | 0 | { |
260 | | #if MP_ARGCHK == 2 |
261 | | assert(mp1 != NULL && mp2 != NULL); |
262 | | #else |
263 | 0 | if (mp1 == NULL || mp2 == NULL) |
264 | 0 | return; |
265 | 0 | #endif |
266 | 0 | |
267 | 0 | s_mp_exch(mp1, mp2); |
268 | 0 |
|
269 | 0 | } /* end mp_exch() */ |
270 | | |
271 | | /* }}} */ |
272 | | |
273 | | /* {{{ mp_clear(mp) */ |
274 | | |
275 | | /* |
276 | | mp_clear(mp) |
277 | | |
278 | | Release the storage used by an mp_int, and void its fields so that |
279 | | if someone calls mp_clear() again for the same int later, we won't |
280 | | get tollchocked. |
281 | | */ |
282 | | |
283 | | void |
284 | | mp_clear(mp_int *mp) |
285 | 0 | { |
286 | 0 | if (mp == NULL) |
287 | 0 | return; |
288 | 0 | |
289 | 0 | if (DIGITS(mp) != NULL) { |
290 | 0 | s_mp_setz(DIGITS(mp), ALLOC(mp)); |
291 | 0 | s_mp_free(DIGITS(mp)); |
292 | 0 | DIGITS(mp) = NULL; |
293 | 0 | } |
294 | 0 |
|
295 | 0 | USED(mp) = 0; |
296 | 0 | ALLOC(mp) = 0; |
297 | 0 |
|
298 | 0 | } /* end mp_clear() */ |
299 | | |
300 | | /* }}} */ |
301 | | |
302 | | /* {{{ mp_zero(mp) */ |
303 | | |
304 | | /* |
305 | | mp_zero(mp) |
306 | | |
307 | | Set mp to zero. Does not change the allocated size of the structure, |
308 | | and therefore cannot fail (except on a bad argument, which we ignore) |
309 | | */ |
310 | | void |
311 | | mp_zero(mp_int *mp) |
312 | 0 | { |
313 | 0 | if (mp == NULL) |
314 | 0 | return; |
315 | 0 | |
316 | 0 | s_mp_setz(DIGITS(mp), ALLOC(mp)); |
317 | 0 | USED(mp) = 1; |
318 | 0 | SIGN(mp) = ZPOS; |
319 | 0 |
|
320 | 0 | } /* end mp_zero() */ |
321 | | |
322 | | /* }}} */ |
323 | | |
324 | | /* {{{ mp_set(mp, d) */ |
325 | | |
326 | | void |
327 | | mp_set(mp_int *mp, mp_digit d) |
328 | 0 | { |
329 | 0 | if (mp == NULL) |
330 | 0 | return; |
331 | 0 | |
332 | 0 | mp_zero(mp); |
333 | 0 | DIGIT(mp, 0) = d; |
334 | 0 |
|
335 | 0 | } /* end mp_set() */ |
336 | | |
337 | | /* }}} */ |
338 | | |
339 | | /* {{{ mp_set_int(mp, z) */ |
340 | | |
341 | | mp_err |
342 | | mp_set_int(mp_int *mp, long z) |
343 | 0 | { |
344 | 0 | int ix; |
345 | 0 | unsigned long v = labs(z); |
346 | 0 | mp_err res; |
347 | 0 |
|
348 | 0 | ARGCHK(mp != NULL, MP_BADARG); |
349 | 0 |
|
350 | 0 | mp_zero(mp); |
351 | 0 | if (z == 0) |
352 | 0 | return MP_OKAY; /* shortcut for zero */ |
353 | 0 | |
354 | 0 | if (sizeof v <= sizeof(mp_digit)) { |
355 | 0 | DIGIT(mp, 0) = v; |
356 | 0 | } else { |
357 | 0 | for (ix = sizeof(long) - 1; ix >= 0; ix--) { |
358 | 0 | if ((res = s_mp_mul_d(mp, (UCHAR_MAX + 1))) != MP_OKAY) |
359 | 0 | return res; |
360 | 0 | |
361 | 0 | res = s_mp_add_d(mp, (mp_digit)((v >> (ix * CHAR_BIT)) & UCHAR_MAX)); |
362 | 0 | if (res != MP_OKAY) |
363 | 0 | return res; |
364 | 0 | } |
365 | 0 | } |
366 | 0 | if (z < 0) |
367 | 0 | SIGN(mp) = NEG; |
368 | 0 |
|
369 | 0 | return MP_OKAY; |
370 | 0 |
|
371 | 0 | } /* end mp_set_int() */ |
372 | | |
373 | | /* }}} */ |
374 | | |
375 | | /* {{{ mp_set_ulong(mp, z) */ |
376 | | |
377 | | mp_err |
378 | | mp_set_ulong(mp_int *mp, unsigned long z) |
379 | 0 | { |
380 | 0 | int ix; |
381 | 0 | mp_err res; |
382 | 0 |
|
383 | 0 | ARGCHK(mp != NULL, MP_BADARG); |
384 | 0 |
|
385 | 0 | mp_zero(mp); |
386 | 0 | if (z == 0) |
387 | 0 | return MP_OKAY; /* shortcut for zero */ |
388 | 0 | |
389 | 0 | if (sizeof z <= sizeof(mp_digit)) { |
390 | 0 | DIGIT(mp, 0) = z; |
391 | 0 | } else { |
392 | 0 | for (ix = sizeof(long) - 1; ix >= 0; ix--) { |
393 | 0 | if ((res = s_mp_mul_d(mp, (UCHAR_MAX + 1))) != MP_OKAY) |
394 | 0 | return res; |
395 | 0 | |
396 | 0 | res = s_mp_add_d(mp, (mp_digit)((z >> (ix * CHAR_BIT)) & UCHAR_MAX)); |
397 | 0 | if (res != MP_OKAY) |
398 | 0 | return res; |
399 | 0 | } |
400 | 0 | } |
401 | 0 | return MP_OKAY; |
402 | 0 | } /* end mp_set_ulong() */ |
403 | | |
404 | | /* }}} */ |
405 | | |
406 | | /*------------------------------------------------------------------------*/ |
407 | | /* {{{ Digit arithmetic */ |
408 | | |
409 | | /* {{{ mp_add_d(a, d, b) */ |
410 | | |
411 | | /* |
412 | | mp_add_d(a, d, b) |
413 | | |
414 | | Compute the sum b = a + d, for a single digit d. Respects the sign of |
415 | | its primary addend (single digits are unsigned anyway). |
416 | | */ |
417 | | |
418 | | mp_err |
419 | | mp_add_d(const mp_int *a, mp_digit d, mp_int *b) |
420 | 0 | { |
421 | 0 | mp_int tmp; |
422 | 0 | mp_err res; |
423 | 0 |
|
424 | 0 | ARGCHK(a != NULL && b != NULL, MP_BADARG); |
425 | 0 |
|
426 | 0 | if ((res = mp_init_copy(&tmp, a)) != MP_OKAY) |
427 | 0 | return res; |
428 | 0 | |
429 | 0 | if (SIGN(&tmp) == ZPOS) { |
430 | 0 | if ((res = s_mp_add_d(&tmp, d)) != MP_OKAY) |
431 | 0 | goto CLEANUP; |
432 | 0 | } else if (s_mp_cmp_d(&tmp, d) >= 0) { |
433 | 0 | if ((res = s_mp_sub_d(&tmp, d)) != MP_OKAY) |
434 | 0 | goto CLEANUP; |
435 | 0 | } else { |
436 | 0 | mp_neg(&tmp, &tmp); |
437 | 0 |
|
438 | 0 | DIGIT(&tmp, 0) = d - DIGIT(&tmp, 0); |
439 | 0 | } |
440 | 0 |
|
441 | 0 | if (s_mp_cmp_d(&tmp, 0) == 0) |
442 | 0 | SIGN(&tmp) = ZPOS; |
443 | 0 |
|
444 | 0 | s_mp_exch(&tmp, b); |
445 | 0 |
|
446 | 0 | CLEANUP: |
447 | 0 | mp_clear(&tmp); |
448 | 0 | return res; |
449 | 0 |
|
450 | 0 | } /* end mp_add_d() */ |
451 | | |
452 | | /* }}} */ |
453 | | |
454 | | /* {{{ mp_sub_d(a, d, b) */ |
455 | | |
456 | | /* |
457 | | mp_sub_d(a, d, b) |
458 | | |
459 | | Compute the difference b = a - d, for a single digit d. Respects the |
460 | | sign of its subtrahend (single digits are unsigned anyway). |
461 | | */ |
462 | | |
463 | | mp_err |
464 | | mp_sub_d(const mp_int *a, mp_digit d, mp_int *b) |
465 | 0 | { |
466 | 0 | mp_int tmp; |
467 | 0 | mp_err res; |
468 | 0 |
|
469 | 0 | ARGCHK(a != NULL && b != NULL, MP_BADARG); |
470 | 0 |
|
471 | 0 | if ((res = mp_init_copy(&tmp, a)) != MP_OKAY) |
472 | 0 | return res; |
473 | 0 | |
474 | 0 | if (SIGN(&tmp) == NEG) { |
475 | 0 | if ((res = s_mp_add_d(&tmp, d)) != MP_OKAY) |
476 | 0 | goto CLEANUP; |
477 | 0 | } else if (s_mp_cmp_d(&tmp, d) >= 0) { |
478 | 0 | if ((res = s_mp_sub_d(&tmp, d)) != MP_OKAY) |
479 | 0 | goto CLEANUP; |
480 | 0 | } else { |
481 | 0 | mp_neg(&tmp, &tmp); |
482 | 0 |
|
483 | 0 | DIGIT(&tmp, 0) = d - DIGIT(&tmp, 0); |
484 | 0 | SIGN(&tmp) = NEG; |
485 | 0 | } |
486 | 0 |
|
487 | 0 | if (s_mp_cmp_d(&tmp, 0) == 0) |
488 | 0 | SIGN(&tmp) = ZPOS; |
489 | 0 |
|
490 | 0 | s_mp_exch(&tmp, b); |
491 | 0 |
|
492 | 0 | CLEANUP: |
493 | 0 | mp_clear(&tmp); |
494 | 0 | return res; |
495 | 0 |
|
496 | 0 | } /* end mp_sub_d() */ |
497 | | |
498 | | /* }}} */ |
499 | | |
500 | | /* {{{ mp_mul_d(a, d, b) */ |
501 | | |
502 | | /* |
503 | | mp_mul_d(a, d, b) |
504 | | |
505 | | Compute the product b = a * d, for a single digit d. Respects the sign |
506 | | of its multiplicand (single digits are unsigned anyway) |
507 | | */ |
508 | | |
509 | | mp_err |
510 | | mp_mul_d(const mp_int *a, mp_digit d, mp_int *b) |
511 | 0 | { |
512 | 0 | mp_err res; |
513 | 0 |
|
514 | 0 | ARGCHK(a != NULL && b != NULL, MP_BADARG); |
515 | 0 |
|
516 | 0 | if (d == 0) { |
517 | 0 | mp_zero(b); |
518 | 0 | return MP_OKAY; |
519 | 0 | } |
520 | 0 |
|
521 | 0 | if ((res = mp_copy(a, b)) != MP_OKAY) |
522 | 0 | return res; |
523 | 0 | |
524 | 0 | res = s_mp_mul_d(b, d); |
525 | 0 |
|
526 | 0 | return res; |
527 | 0 |
|
528 | 0 | } /* end mp_mul_d() */ |
529 | | |
530 | | /* }}} */ |
531 | | |
532 | | /* {{{ mp_mul_2(a, c) */ |
533 | | |
534 | | mp_err |
535 | | mp_mul_2(const mp_int *a, mp_int *c) |
536 | 0 | { |
537 | 0 | mp_err res; |
538 | 0 |
|
539 | 0 | ARGCHK(a != NULL && c != NULL, MP_BADARG); |
540 | 0 |
|
541 | 0 | if ((res = mp_copy(a, c)) != MP_OKAY) |
542 | 0 | return res; |
543 | 0 | |
544 | 0 | return s_mp_mul_2(c); |
545 | 0 |
|
546 | 0 | } /* end mp_mul_2() */ |
547 | | |
548 | | /* }}} */ |
549 | | |
550 | | /* {{{ mp_div_d(a, d, q, r) */ |
551 | | |
552 | | /* |
553 | | mp_div_d(a, d, q, r) |
554 | | |
555 | | Compute the quotient q = a / d and remainder r = a mod d, for a |
556 | | single digit d. Respects the sign of its divisor (single digits are |
557 | | unsigned anyway). |
558 | | */ |
559 | | |
560 | | mp_err |
561 | | mp_div_d(const mp_int *a, mp_digit d, mp_int *q, mp_digit *r) |
562 | 0 | { |
563 | 0 | mp_err res; |
564 | 0 | mp_int qp; |
565 | 0 | mp_digit rem = 0; |
566 | 0 | int pow; |
567 | 0 |
|
568 | 0 | ARGCHK(a != NULL, MP_BADARG); |
569 | 0 |
|
570 | 0 | if (d == 0) |
571 | 0 | return MP_RANGE; |
572 | 0 | |
573 | 0 | /* Shortcut for powers of two ... */ |
574 | 0 | if ((pow = s_mp_ispow2d(d)) >= 0) { |
575 | 0 | mp_digit mask; |
576 | 0 |
|
577 | 0 | mask = ((mp_digit)1 << pow) - 1; |
578 | 0 | rem = DIGIT(a, 0) & mask; |
579 | 0 |
|
580 | 0 | if (q) { |
581 | 0 | if ((res = mp_copy(a, q)) != MP_OKAY) { |
582 | 0 | return res; |
583 | 0 | } |
584 | 0 | s_mp_div_2d(q, pow); |
585 | 0 | } |
586 | 0 |
|
587 | 0 | if (r) |
588 | 0 | *r = rem; |
589 | 0 |
|
590 | 0 | return MP_OKAY; |
591 | 0 | } |
592 | 0 | |
593 | 0 | if ((res = mp_init_copy(&qp, a)) != MP_OKAY) |
594 | 0 | return res; |
595 | 0 | |
596 | 0 | res = s_mp_div_d(&qp, d, &rem); |
597 | 0 |
|
598 | 0 | if (s_mp_cmp_d(&qp, 0) == 0) |
599 | 0 | SIGN(q) = ZPOS; |
600 | 0 |
|
601 | 0 | if (r) { |
602 | 0 | *r = rem; |
603 | 0 | } |
604 | 0 |
|
605 | 0 | if (q) |
606 | 0 | s_mp_exch(&qp, q); |
607 | 0 |
|
608 | 0 | mp_clear(&qp); |
609 | 0 | return res; |
610 | 0 |
|
611 | 0 | } /* end mp_div_d() */ |
612 | | |
613 | | /* }}} */ |
614 | | |
615 | | /* {{{ mp_div_2(a, c) */ |
616 | | |
617 | | /* |
618 | | mp_div_2(a, c) |
619 | | |
620 | | Compute c = a / 2, disregarding the remainder. |
621 | | */ |
622 | | |
623 | | mp_err |
624 | | mp_div_2(const mp_int *a, mp_int *c) |
625 | 0 | { |
626 | 0 | mp_err res; |
627 | 0 |
|
628 | 0 | ARGCHK(a != NULL && c != NULL, MP_BADARG); |
629 | 0 |
|
630 | 0 | if ((res = mp_copy(a, c)) != MP_OKAY) |
631 | 0 | return res; |
632 | 0 | |
633 | 0 | s_mp_div_2(c); |
634 | 0 |
|
635 | 0 | return MP_OKAY; |
636 | 0 |
|
637 | 0 | } /* end mp_div_2() */ |
638 | | |
639 | | /* }}} */ |
640 | | |
641 | | /* {{{ mp_expt_d(a, d, b) */ |
642 | | |
643 | | mp_err |
644 | | mp_expt_d(const mp_int *a, mp_digit d, mp_int *c) |
645 | 0 | { |
646 | 0 | mp_int s, x; |
647 | 0 | mp_err res; |
648 | 0 |
|
649 | 0 | ARGCHK(a != NULL && c != NULL, MP_BADARG); |
650 | 0 |
|
651 | 0 | if ((res = mp_init(&s)) != MP_OKAY) |
652 | 0 | return res; |
653 | 0 | if ((res = mp_init_copy(&x, a)) != MP_OKAY) |
654 | 0 | goto X; |
655 | 0 | |
656 | 0 | DIGIT(&s, 0) = 1; |
657 | 0 |
|
658 | 0 | while (d != 0) { |
659 | 0 | if (d & 1) { |
660 | 0 | if ((res = s_mp_mul(&s, &x)) != MP_OKAY) |
661 | 0 | goto CLEANUP; |
662 | 0 | } |
663 | 0 | |
664 | 0 | d /= 2; |
665 | 0 |
|
666 | 0 | if ((res = s_mp_sqr(&x)) != MP_OKAY) |
667 | 0 | goto CLEANUP; |
668 | 0 | } |
669 | 0 |
|
670 | 0 | s_mp_exch(&s, c); |
671 | 0 |
|
672 | 0 | CLEANUP: |
673 | 0 | mp_clear(&x); |
674 | 0 | X: |
675 | 0 | mp_clear(&s); |
676 | 0 |
|
677 | 0 | return res; |
678 | 0 |
|
679 | 0 | } /* end mp_expt_d() */ |
680 | | |
681 | | /* }}} */ |
682 | | |
683 | | /* }}} */ |
684 | | |
685 | | /*------------------------------------------------------------------------*/ |
686 | | /* {{{ Full arithmetic */ |
687 | | |
688 | | /* {{{ mp_abs(a, b) */ |
689 | | |
690 | | /* |
691 | | mp_abs(a, b) |
692 | | |
693 | | Compute b = |a|. 'a' and 'b' may be identical. |
694 | | */ |
695 | | |
696 | | mp_err |
697 | | mp_abs(const mp_int *a, mp_int *b) |
698 | 0 | { |
699 | 0 | mp_err res; |
700 | 0 |
|
701 | 0 | ARGCHK(a != NULL && b != NULL, MP_BADARG); |
702 | 0 |
|
703 | 0 | if ((res = mp_copy(a, b)) != MP_OKAY) |
704 | 0 | return res; |
705 | 0 | |
706 | 0 | SIGN(b) = ZPOS; |
707 | 0 |
|
708 | 0 | return MP_OKAY; |
709 | 0 |
|
710 | 0 | } /* end mp_abs() */ |
711 | | |
712 | | /* }}} */ |
713 | | |
714 | | /* {{{ mp_neg(a, b) */ |
715 | | |
716 | | /* |
717 | | mp_neg(a, b) |
718 | | |
719 | | Compute b = -a. 'a' and 'b' may be identical. |
720 | | */ |
721 | | |
722 | | mp_err |
723 | | mp_neg(const mp_int *a, mp_int *b) |
724 | 0 | { |
725 | 0 | mp_err res; |
726 | 0 |
|
727 | 0 | ARGCHK(a != NULL && b != NULL, MP_BADARG); |
728 | 0 |
|
729 | 0 | if ((res = mp_copy(a, b)) != MP_OKAY) |
730 | 0 | return res; |
731 | 0 | |
732 | 0 | if (s_mp_cmp_d(b, 0) == MP_EQ) |
733 | 0 | SIGN(b) = ZPOS; |
734 | 0 | else |
735 | 0 | SIGN(b) = (SIGN(b) == NEG) ? ZPOS : NEG; |
736 | 0 |
|
737 | 0 | return MP_OKAY; |
738 | 0 |
|
739 | 0 | } /* end mp_neg() */ |
740 | | |
741 | | /* }}} */ |
742 | | |
743 | | /* {{{ mp_add(a, b, c) */ |
744 | | |
745 | | /* |
746 | | mp_add(a, b, c) |
747 | | |
748 | | Compute c = a + b. All parameters may be identical. |
749 | | */ |
750 | | |
751 | | mp_err |
752 | | mp_add(const mp_int *a, const mp_int *b, mp_int *c) |
753 | 0 | { |
754 | 0 | mp_err res; |
755 | 0 |
|
756 | 0 | ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG); |
757 | 0 |
|
758 | 0 | if (SIGN(a) == SIGN(b)) { /* same sign: add values, keep sign */ |
759 | 0 | MP_CHECKOK(s_mp_add_3arg(a, b, c)); |
760 | 0 | } else if (s_mp_cmp(a, b) >= 0) { /* different sign: |a| >= |b| */ |
761 | 0 | MP_CHECKOK(s_mp_sub_3arg(a, b, c)); |
762 | 0 | } else { /* different sign: |a| < |b| */ |
763 | 0 | MP_CHECKOK(s_mp_sub_3arg(b, a, c)); |
764 | 0 | } |
765 | 0 |
|
766 | 0 | if (s_mp_cmp_d(c, 0) == MP_EQ) |
767 | 0 | SIGN(c) = ZPOS; |
768 | 0 |
|
769 | 0 | CLEANUP: |
770 | 0 | return res; |
771 | 0 |
|
772 | 0 | } /* end mp_add() */ |
773 | | |
774 | | /* }}} */ |
775 | | |
776 | | /* {{{ mp_sub(a, b, c) */ |
777 | | |
778 | | /* |
779 | | mp_sub(a, b, c) |
780 | | |
781 | | Compute c = a - b. All parameters may be identical. |
782 | | */ |
783 | | |
784 | | mp_err |
785 | | mp_sub(const mp_int *a, const mp_int *b, mp_int *c) |
786 | 0 | { |
787 | 0 | mp_err res; |
788 | 0 | int magDiff; |
789 | 0 |
|
790 | 0 | ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG); |
791 | 0 |
|
792 | 0 | if (a == b) { |
793 | 0 | mp_zero(c); |
794 | 0 | return MP_OKAY; |
795 | 0 | } |
796 | 0 |
|
797 | 0 | if (MP_SIGN(a) != MP_SIGN(b)) { |
798 | 0 | MP_CHECKOK(s_mp_add_3arg(a, b, c)); |
799 | 0 | } else if (!(magDiff = s_mp_cmp(a, b))) { |
800 | 0 | mp_zero(c); |
801 | 0 | res = MP_OKAY; |
802 | 0 | } else if (magDiff > 0) { |
803 | 0 | MP_CHECKOK(s_mp_sub_3arg(a, b, c)); |
804 | 0 | } else { |
805 | 0 | MP_CHECKOK(s_mp_sub_3arg(b, a, c)); |
806 | 0 | MP_SIGN(c) = !MP_SIGN(a); |
807 | 0 | } |
808 | 0 |
|
809 | 0 | if (s_mp_cmp_d(c, 0) == MP_EQ) |
810 | 0 | MP_SIGN(c) = MP_ZPOS; |
811 | 0 |
|
812 | 0 | CLEANUP: |
813 | 0 | return res; |
814 | 0 |
|
815 | 0 | } /* end mp_sub() */ |
816 | | |
817 | | /* }}} */ |
818 | | |
819 | | /* {{{ mp_mul(a, b, c) */ |
820 | | |
821 | | /* |
822 | | mp_mul(a, b, c) |
823 | | |
824 | | Compute c = a * b. All parameters may be identical. |
825 | | */ |
826 | | mp_err |
827 | | mp_mul(const mp_int *a, const mp_int *b, mp_int *c) |
828 | 0 | { |
829 | 0 | mp_digit *pb; |
830 | 0 | mp_int tmp; |
831 | 0 | mp_err res; |
832 | 0 | mp_size ib; |
833 | 0 | mp_size useda, usedb; |
834 | 0 |
|
835 | 0 | ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG); |
836 | 0 |
|
837 | 0 | if (a == c) { |
838 | 0 | if ((res = mp_init_copy(&tmp, a)) != MP_OKAY) |
839 | 0 | return res; |
840 | 0 | if (a == b) |
841 | 0 | b = &tmp; |
842 | 0 | a = &tmp; |
843 | 0 | } else if (b == c) { |
844 | 0 | if ((res = mp_init_copy(&tmp, b)) != MP_OKAY) |
845 | 0 | return res; |
846 | 0 | b = &tmp; |
847 | 0 | } else { |
848 | 0 | MP_DIGITS(&tmp) = 0; |
849 | 0 | } |
850 | 0 |
|
851 | 0 | if (MP_USED(a) < MP_USED(b)) { |
852 | 0 | const mp_int *xch = b; /* switch a and b, to do fewer outer loops */ |
853 | 0 | b = a; |
854 | 0 | a = xch; |
855 | 0 | } |
856 | 0 |
|
857 | 0 | MP_USED(c) = 1; |
858 | 0 | MP_DIGIT(c, 0) = 0; |
859 | 0 | if ((res = s_mp_pad(c, USED(a) + USED(b))) != MP_OKAY) |
860 | 0 | goto CLEANUP; |
861 | 0 | |
862 | | #ifdef NSS_USE_COMBA |
863 | | if ((MP_USED(a) == MP_USED(b)) && IS_POWER_OF_2(MP_USED(b))) { |
864 | | if (MP_USED(a) == 4) { |
865 | | s_mp_mul_comba_4(a, b, c); |
866 | | goto CLEANUP; |
867 | | } |
868 | | if (MP_USED(a) == 8) { |
869 | | s_mp_mul_comba_8(a, b, c); |
870 | | goto CLEANUP; |
871 | | } |
872 | | if (MP_USED(a) == 16) { |
873 | | s_mp_mul_comba_16(a, b, c); |
874 | | goto CLEANUP; |
875 | | } |
876 | | if (MP_USED(a) == 32) { |
877 | | s_mp_mul_comba_32(a, b, c); |
878 | | goto CLEANUP; |
879 | | } |
880 | | } |
881 | | #endif |
882 | | |
883 | 0 | pb = MP_DIGITS(b); |
884 | 0 | s_mpv_mul_d(MP_DIGITS(a), MP_USED(a), *pb++, MP_DIGITS(c)); |
885 | 0 |
|
886 | 0 | /* Outer loop: Digits of b */ |
887 | 0 | useda = MP_USED(a); |
888 | 0 | usedb = MP_USED(b); |
889 | 0 | for (ib = 1; ib < usedb; ib++) { |
890 | 0 | mp_digit b_i = *pb++; |
891 | 0 |
|
892 | 0 | /* Inner product: Digits of a */ |
893 | 0 | if (b_i) |
894 | 0 | s_mpv_mul_d_add(MP_DIGITS(a), useda, b_i, MP_DIGITS(c) + ib); |
895 | 0 | else |
896 | 0 | MP_DIGIT(c, ib + useda) = b_i; |
897 | 0 | } |
898 | 0 |
|
899 | 0 | s_mp_clamp(c); |
900 | 0 |
|
901 | 0 | if (SIGN(a) == SIGN(b) || s_mp_cmp_d(c, 0) == MP_EQ) |
902 | 0 | SIGN(c) = ZPOS; |
903 | 0 | else |
904 | 0 | SIGN(c) = NEG; |
905 | 0 |
|
906 | 0 | CLEANUP: |
907 | 0 | mp_clear(&tmp); |
908 | 0 | return res; |
909 | 0 | } /* end mp_mul() */ |
910 | | |
911 | | /* }}} */ |
912 | | |
913 | | /* {{{ mp_sqr(a, sqr) */ |
914 | | |
915 | | #if MP_SQUARE |
916 | | /* |
917 | | Computes the square of a. This can be done more |
918 | | efficiently than a general multiplication, because many of the |
919 | | computation steps are redundant when squaring. The inner product |
920 | | step is a bit more complicated, but we save a fair number of |
921 | | iterations of the multiplication loop. |
922 | | */ |
923 | | |
924 | | /* sqr = a^2; Caller provides both a and tmp; */ |
925 | | mp_err |
926 | | mp_sqr(const mp_int *a, mp_int *sqr) |
927 | 0 | { |
928 | 0 | mp_digit *pa; |
929 | 0 | mp_digit d; |
930 | 0 | mp_err res; |
931 | 0 | mp_size ix; |
932 | 0 | mp_int tmp; |
933 | 0 | int count; |
934 | 0 |
|
935 | 0 | ARGCHK(a != NULL && sqr != NULL, MP_BADARG); |
936 | 0 |
|
937 | 0 | if (a == sqr) { |
938 | 0 | if ((res = mp_init_copy(&tmp, a)) != MP_OKAY) |
939 | 0 | return res; |
940 | 0 | a = &tmp; |
941 | 0 | } else { |
942 | 0 | DIGITS(&tmp) = 0; |
943 | 0 | res = MP_OKAY; |
944 | 0 | } |
945 | 0 |
|
946 | 0 | ix = 2 * MP_USED(a); |
947 | 0 | if (ix > MP_ALLOC(sqr)) { |
948 | 0 | MP_USED(sqr) = 1; |
949 | 0 | MP_CHECKOK(s_mp_grow(sqr, ix)); |
950 | 0 | } |
951 | 0 | MP_USED(sqr) = ix; |
952 | 0 | MP_DIGIT(sqr, 0) = 0; |
953 | 0 |
|
954 | | #ifdef NSS_USE_COMBA |
955 | | if (IS_POWER_OF_2(MP_USED(a))) { |
956 | | if (MP_USED(a) == 4) { |
957 | | s_mp_sqr_comba_4(a, sqr); |
958 | | goto CLEANUP; |
959 | | } |
960 | | if (MP_USED(a) == 8) { |
961 | | s_mp_sqr_comba_8(a, sqr); |
962 | | goto CLEANUP; |
963 | | } |
964 | | if (MP_USED(a) == 16) { |
965 | | s_mp_sqr_comba_16(a, sqr); |
966 | | goto CLEANUP; |
967 | | } |
968 | | if (MP_USED(a) == 32) { |
969 | | s_mp_sqr_comba_32(a, sqr); |
970 | | goto CLEANUP; |
971 | | } |
972 | | } |
973 | | #endif |
974 | |
|
975 | 0 | pa = MP_DIGITS(a); |
976 | 0 | count = MP_USED(a) - 1; |
977 | 0 | if (count > 0) { |
978 | 0 | d = *pa++; |
979 | 0 | s_mpv_mul_d(pa, count, d, MP_DIGITS(sqr) + 1); |
980 | 0 | for (ix = 3; --count > 0; ix += 2) { |
981 | 0 | d = *pa++; |
982 | 0 | s_mpv_mul_d_add(pa, count, d, MP_DIGITS(sqr) + ix); |
983 | 0 | } /* for(ix ...) */ |
984 | 0 | MP_DIGIT(sqr, MP_USED(sqr) - 1) = 0; /* above loop stopped short of this. */ |
985 | 0 |
|
986 | 0 | /* now sqr *= 2 */ |
987 | 0 | s_mp_mul_2(sqr); |
988 | 0 | } else { |
989 | 0 | MP_DIGIT(sqr, 1) = 0; |
990 | 0 | } |
991 | 0 |
|
992 | 0 | /* now add the squares of the digits of a to sqr. */ |
993 | 0 | s_mpv_sqr_add_prop(MP_DIGITS(a), MP_USED(a), MP_DIGITS(sqr)); |
994 | 0 |
|
995 | 0 | SIGN(sqr) = ZPOS; |
996 | 0 | s_mp_clamp(sqr); |
997 | 0 |
|
998 | 0 | CLEANUP: |
999 | 0 | mp_clear(&tmp); |
1000 | 0 | return res; |
1001 | 0 |
|
1002 | 0 | } /* end mp_sqr() */ |
1003 | | #endif |
1004 | | |
1005 | | /* }}} */ |
1006 | | |
1007 | | /* {{{ mp_div(a, b, q, r) */ |
1008 | | |
1009 | | /* |
1010 | | mp_div(a, b, q, r) |
1011 | | |
1012 | | Compute q = a / b and r = a mod b. Input parameters may be re-used |
1013 | | as output parameters. If q or r is NULL, that portion of the |
1014 | | computation will be discarded (although it will still be computed) |
1015 | | */ |
1016 | | mp_err |
1017 | | mp_div(const mp_int *a, const mp_int *b, mp_int *q, mp_int *r) |
1018 | 0 | { |
1019 | 0 | mp_err res; |
1020 | 0 | mp_int *pQ, *pR; |
1021 | 0 | mp_int qtmp, rtmp, btmp; |
1022 | 0 | int cmp; |
1023 | 0 | mp_sign signA; |
1024 | 0 | mp_sign signB; |
1025 | 0 |
|
1026 | 0 | ARGCHK(a != NULL && b != NULL, MP_BADARG); |
1027 | 0 |
|
1028 | 0 | signA = MP_SIGN(a); |
1029 | 0 | signB = MP_SIGN(b); |
1030 | 0 |
|
1031 | 0 | if (mp_cmp_z(b) == MP_EQ) |
1032 | 0 | return MP_RANGE; |
1033 | 0 | |
1034 | 0 | DIGITS(&qtmp) = 0; |
1035 | 0 | DIGITS(&rtmp) = 0; |
1036 | 0 | DIGITS(&btmp) = 0; |
1037 | 0 |
|
1038 | 0 | /* Set up some temporaries... */ |
1039 | 0 | if (!r || r == a || r == b) { |
1040 | 0 | MP_CHECKOK(mp_init_copy(&rtmp, a)); |
1041 | 0 | pR = &rtmp; |
1042 | 0 | } else { |
1043 | 0 | MP_CHECKOK(mp_copy(a, r)); |
1044 | 0 | pR = r; |
1045 | 0 | } |
1046 | 0 |
|
1047 | 0 | if (!q || q == a || q == b) { |
1048 | 0 | MP_CHECKOK(mp_init_size(&qtmp, MP_USED(a))); |
1049 | 0 | pQ = &qtmp; |
1050 | 0 | } else { |
1051 | 0 | MP_CHECKOK(s_mp_pad(q, MP_USED(a))); |
1052 | 0 | pQ = q; |
1053 | 0 | mp_zero(pQ); |
1054 | 0 | } |
1055 | 0 |
|
1056 | 0 | /* |
1057 | 0 | If |a| <= |b|, we can compute the solution without division; |
1058 | 0 | otherwise, we actually do the work required. |
1059 | 0 | */ |
1060 | 0 | if ((cmp = s_mp_cmp(a, b)) <= 0) { |
1061 | 0 | if (cmp) { |
1062 | 0 | /* r was set to a above. */ |
1063 | 0 | mp_zero(pQ); |
1064 | 0 | } else { |
1065 | 0 | mp_set(pQ, 1); |
1066 | 0 | mp_zero(pR); |
1067 | 0 | } |
1068 | 0 | } else { |
1069 | 0 | MP_CHECKOK(mp_init_copy(&btmp, b)); |
1070 | 0 | MP_CHECKOK(s_mp_div(pR, &btmp, pQ)); |
1071 | 0 | } |
1072 | 0 |
|
1073 | 0 | /* Compute the signs for the output */ |
1074 | 0 | MP_SIGN(pR) = signA; /* Sr = Sa */ |
1075 | 0 | /* Sq = ZPOS if Sa == Sb */ /* Sq = NEG if Sa != Sb */ |
1076 | 0 | MP_SIGN(pQ) = (signA == signB) ? ZPOS : NEG; |
1077 | 0 |
|
1078 | 0 | if (s_mp_cmp_d(pQ, 0) == MP_EQ) |
1079 | 0 | SIGN(pQ) = ZPOS; |
1080 | 0 | if (s_mp_cmp_d(pR, 0) == MP_EQ) |
1081 | 0 | SIGN(pR) = ZPOS; |
1082 | 0 |
|
1083 | 0 | /* Copy output, if it is needed */ |
1084 | 0 | if (q && q != pQ) |
1085 | 0 | s_mp_exch(pQ, q); |
1086 | 0 |
|
1087 | 0 | if (r && r != pR) |
1088 | 0 | s_mp_exch(pR, r); |
1089 | 0 |
|
1090 | 0 | CLEANUP: |
1091 | 0 | mp_clear(&btmp); |
1092 | 0 | mp_clear(&rtmp); |
1093 | 0 | mp_clear(&qtmp); |
1094 | 0 |
|
1095 | 0 | return res; |
1096 | 0 |
|
1097 | 0 | } /* end mp_div() */ |
1098 | | |
1099 | | /* }}} */ |
1100 | | |
1101 | | /* {{{ mp_div_2d(a, d, q, r) */ |
1102 | | |
1103 | | mp_err |
1104 | | mp_div_2d(const mp_int *a, mp_digit d, mp_int *q, mp_int *r) |
1105 | 0 | { |
1106 | 0 | mp_err res; |
1107 | 0 |
|
1108 | 0 | ARGCHK(a != NULL, MP_BADARG); |
1109 | 0 |
|
1110 | 0 | if (q) { |
1111 | 0 | if ((res = mp_copy(a, q)) != MP_OKAY) |
1112 | 0 | return res; |
1113 | 0 | } |
1114 | 0 | if (r) { |
1115 | 0 | if ((res = mp_copy(a, r)) != MP_OKAY) |
1116 | 0 | return res; |
1117 | 0 | } |
1118 | 0 | if (q) { |
1119 | 0 | s_mp_div_2d(q, d); |
1120 | 0 | } |
1121 | 0 | if (r) { |
1122 | 0 | s_mp_mod_2d(r, d); |
1123 | 0 | } |
1124 | 0 |
|
1125 | 0 | return MP_OKAY; |
1126 | 0 |
|
1127 | 0 | } /* end mp_div_2d() */ |
1128 | | |
1129 | | /* }}} */ |
1130 | | |
1131 | | /* {{{ mp_expt(a, b, c) */ |
1132 | | |
1133 | | /* |
1134 | | mp_expt(a, b, c) |
1135 | | |
1136 | | Compute c = a ** b, that is, raise a to the b power. Uses a |
1137 | | standard iterative square-and-multiply technique. |
1138 | | */ |
1139 | | |
1140 | | mp_err |
1141 | | mp_expt(mp_int *a, mp_int *b, mp_int *c) |
1142 | 0 | { |
1143 | 0 | mp_int s, x; |
1144 | 0 | mp_err res; |
1145 | 0 | mp_digit d; |
1146 | 0 | unsigned int dig, bit; |
1147 | 0 |
|
1148 | 0 | ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG); |
1149 | 0 |
|
1150 | 0 | if (mp_cmp_z(b) < 0) |
1151 | 0 | return MP_RANGE; |
1152 | 0 | |
1153 | 0 | if ((res = mp_init(&s)) != MP_OKAY) |
1154 | 0 | return res; |
1155 | 0 | |
1156 | 0 | mp_set(&s, 1); |
1157 | 0 |
|
1158 | 0 | if ((res = mp_init_copy(&x, a)) != MP_OKAY) |
1159 | 0 | goto X; |
1160 | 0 | |
1161 | 0 | /* Loop over low-order digits in ascending order */ |
1162 | 0 | for (dig = 0; dig < (USED(b) - 1); dig++) { |
1163 | 0 | d = DIGIT(b, dig); |
1164 | 0 |
|
1165 | 0 | /* Loop over bits of each non-maximal digit */ |
1166 | 0 | for (bit = 0; bit < DIGIT_BIT; bit++) { |
1167 | 0 | if (d & 1) { |
1168 | 0 | if ((res = s_mp_mul(&s, &x)) != MP_OKAY) |
1169 | 0 | goto CLEANUP; |
1170 | 0 | } |
1171 | 0 | |
1172 | 0 | d >>= 1; |
1173 | 0 |
|
1174 | 0 | if ((res = s_mp_sqr(&x)) != MP_OKAY) |
1175 | 0 | goto CLEANUP; |
1176 | 0 | } |
1177 | 0 | } |
1178 | 0 |
|
1179 | 0 | /* Consider now the last digit... */ |
1180 | 0 | d = DIGIT(b, dig); |
1181 | 0 |
|
1182 | 0 | while (d) { |
1183 | 0 | if (d & 1) { |
1184 | 0 | if ((res = s_mp_mul(&s, &x)) != MP_OKAY) |
1185 | 0 | goto CLEANUP; |
1186 | 0 | } |
1187 | 0 | |
1188 | 0 | d >>= 1; |
1189 | 0 |
|
1190 | 0 | if ((res = s_mp_sqr(&x)) != MP_OKAY) |
1191 | 0 | goto CLEANUP; |
1192 | 0 | } |
1193 | 0 |
|
1194 | 0 | if (mp_iseven(b)) |
1195 | 0 | SIGN(&s) = SIGN(a); |
1196 | 0 |
|
1197 | 0 | res = mp_copy(&s, c); |
1198 | 0 |
|
1199 | 0 | CLEANUP: |
1200 | 0 | mp_clear(&x); |
1201 | 0 | X: |
1202 | 0 | mp_clear(&s); |
1203 | 0 |
|
1204 | 0 | return res; |
1205 | 0 |
|
1206 | 0 | } /* end mp_expt() */ |
1207 | | |
1208 | | /* }}} */ |
1209 | | |
1210 | | /* {{{ mp_2expt(a, k) */ |
1211 | | |
1212 | | /* Compute a = 2^k */ |
1213 | | |
1214 | | mp_err |
1215 | | mp_2expt(mp_int *a, mp_digit k) |
1216 | 0 | { |
1217 | 0 | ARGCHK(a != NULL, MP_BADARG); |
1218 | 0 |
|
1219 | 0 | return s_mp_2expt(a, k); |
1220 | 0 |
|
1221 | 0 | } /* end mp_2expt() */ |
1222 | | |
1223 | | /* }}} */ |
1224 | | |
1225 | | /* {{{ mp_mod(a, m, c) */ |
1226 | | |
1227 | | /* |
1228 | | mp_mod(a, m, c) |
1229 | | |
1230 | | Compute c = a (mod m). Result will always be 0 <= c < m. |
1231 | | */ |
1232 | | |
1233 | | mp_err |
1234 | | mp_mod(const mp_int *a, const mp_int *m, mp_int *c) |
1235 | 0 | { |
1236 | 0 | mp_err res; |
1237 | 0 | int mag; |
1238 | 0 |
|
1239 | 0 | ARGCHK(a != NULL && m != NULL && c != NULL, MP_BADARG); |
1240 | 0 |
|
1241 | 0 | if (SIGN(m) == NEG) |
1242 | 0 | return MP_RANGE; |
1243 | 0 | |
1244 | 0 | /* |
1245 | 0 | If |a| > m, we need to divide to get the remainder and take the |
1246 | 0 | absolute value. |
1247 | 0 | |
1248 | 0 | If |a| < m, we don't need to do any division, just copy and adjust |
1249 | 0 | the sign (if a is negative). |
1250 | 0 | |
1251 | 0 | If |a| == m, we can simply set the result to zero. |
1252 | 0 | |
1253 | 0 | This order is intended to minimize the average path length of the |
1254 | 0 | comparison chain on common workloads -- the most frequent cases are |
1255 | 0 | that |a| != m, so we do those first. |
1256 | 0 | */ |
1257 | 0 | if ((mag = s_mp_cmp(a, m)) > 0) { |
1258 | 0 | if ((res = mp_div(a, m, NULL, c)) != MP_OKAY) |
1259 | 0 | return res; |
1260 | 0 | |
1261 | 0 | if (SIGN(c) == NEG) { |
1262 | 0 | if ((res = mp_add(c, m, c)) != MP_OKAY) |
1263 | 0 | return res; |
1264 | 0 | } |
1265 | 0 | |
1266 | 0 | } else if (mag < 0) { |
1267 | 0 | if ((res = mp_copy(a, c)) != MP_OKAY) |
1268 | 0 | return res; |
1269 | 0 | |
1270 | 0 | if (mp_cmp_z(a) < 0) { |
1271 | 0 | if ((res = mp_add(c, m, c)) != MP_OKAY) |
1272 | 0 | return res; |
1273 | 0 | } |
1274 | 0 | |
1275 | 0 | } else { |
1276 | 0 | mp_zero(c); |
1277 | 0 | } |
1278 | 0 |
|
1279 | 0 | return MP_OKAY; |
1280 | 0 |
|
1281 | 0 | } /* end mp_mod() */ |
1282 | | |
1283 | | /* }}} */ |
1284 | | |
1285 | | /* {{{ mp_mod_d(a, d, c) */ |
1286 | | |
1287 | | /* |
1288 | | mp_mod_d(a, d, c) |
1289 | | |
1290 | | Compute c = a (mod d). Result will always be 0 <= c < d |
1291 | | */ |
1292 | | mp_err |
1293 | | mp_mod_d(const mp_int *a, mp_digit d, mp_digit *c) |
1294 | 0 | { |
1295 | 0 | mp_err res; |
1296 | 0 | mp_digit rem; |
1297 | 0 |
|
1298 | 0 | ARGCHK(a != NULL && c != NULL, MP_BADARG); |
1299 | 0 |
|
1300 | 0 | if (s_mp_cmp_d(a, d) > 0) { |
1301 | 0 | if ((res = mp_div_d(a, d, NULL, &rem)) != MP_OKAY) |
1302 | 0 | return res; |
1303 | 0 | |
1304 | 0 | } else { |
1305 | 0 | if (SIGN(a) == NEG) |
1306 | 0 | rem = d - DIGIT(a, 0); |
1307 | 0 | else |
1308 | 0 | rem = DIGIT(a, 0); |
1309 | 0 | } |
1310 | 0 |
|
1311 | 0 | if (c) |
1312 | 0 | *c = rem; |
1313 | 0 |
|
1314 | 0 | return MP_OKAY; |
1315 | 0 |
|
1316 | 0 | } /* end mp_mod_d() */ |
1317 | | |
1318 | | /* }}} */ |
1319 | | |
1320 | | /* }}} */ |
1321 | | |
1322 | | /*------------------------------------------------------------------------*/ |
1323 | | /* {{{ Modular arithmetic */ |
1324 | | |
1325 | | #if MP_MODARITH |
1326 | | /* {{{ mp_addmod(a, b, m, c) */ |
1327 | | |
1328 | | /* |
1329 | | mp_addmod(a, b, m, c) |
1330 | | |
1331 | | Compute c = (a + b) mod m |
1332 | | */ |
1333 | | |
1334 | | mp_err |
1335 | | mp_addmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c) |
1336 | 0 | { |
1337 | 0 | mp_err res; |
1338 | 0 |
|
1339 | 0 | ARGCHK(a != NULL && b != NULL && m != NULL && c != NULL, MP_BADARG); |
1340 | 0 |
|
1341 | 0 | if ((res = mp_add(a, b, c)) != MP_OKAY) |
1342 | 0 | return res; |
1343 | 0 | if ((res = mp_mod(c, m, c)) != MP_OKAY) |
1344 | 0 | return res; |
1345 | 0 | |
1346 | 0 | return MP_OKAY; |
1347 | 0 | } |
1348 | | |
1349 | | /* }}} */ |
1350 | | |
1351 | | /* {{{ mp_submod(a, b, m, c) */ |
1352 | | |
1353 | | /* |
1354 | | mp_submod(a, b, m, c) |
1355 | | |
1356 | | Compute c = (a - b) mod m |
1357 | | */ |
1358 | | |
1359 | | mp_err |
1360 | | mp_submod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c) |
1361 | 0 | { |
1362 | 0 | mp_err res; |
1363 | 0 |
|
1364 | 0 | ARGCHK(a != NULL && b != NULL && m != NULL && c != NULL, MP_BADARG); |
1365 | 0 |
|
1366 | 0 | if ((res = mp_sub(a, b, c)) != MP_OKAY) |
1367 | 0 | return res; |
1368 | 0 | if ((res = mp_mod(c, m, c)) != MP_OKAY) |
1369 | 0 | return res; |
1370 | 0 | |
1371 | 0 | return MP_OKAY; |
1372 | 0 | } |
1373 | | |
1374 | | /* }}} */ |
1375 | | |
1376 | | /* {{{ mp_mulmod(a, b, m, c) */ |
1377 | | |
1378 | | /* |
1379 | | mp_mulmod(a, b, m, c) |
1380 | | |
1381 | | Compute c = (a * b) mod m |
1382 | | */ |
1383 | | |
1384 | | mp_err |
1385 | | mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c) |
1386 | 0 | { |
1387 | 0 | mp_err res; |
1388 | 0 |
|
1389 | 0 | ARGCHK(a != NULL && b != NULL && m != NULL && c != NULL, MP_BADARG); |
1390 | 0 |
|
1391 | 0 | if ((res = mp_mul(a, b, c)) != MP_OKAY) |
1392 | 0 | return res; |
1393 | 0 | if ((res = mp_mod(c, m, c)) != MP_OKAY) |
1394 | 0 | return res; |
1395 | 0 | |
1396 | 0 | return MP_OKAY; |
1397 | 0 | } |
1398 | | |
1399 | | /* }}} */ |
1400 | | |
1401 | | /* {{{ mp_sqrmod(a, m, c) */ |
1402 | | |
1403 | | #if MP_SQUARE |
1404 | | mp_err |
1405 | | mp_sqrmod(const mp_int *a, const mp_int *m, mp_int *c) |
1406 | 0 | { |
1407 | 0 | mp_err res; |
1408 | 0 |
|
1409 | 0 | ARGCHK(a != NULL && m != NULL && c != NULL, MP_BADARG); |
1410 | 0 |
|
1411 | 0 | if ((res = mp_sqr(a, c)) != MP_OKAY) |
1412 | 0 | return res; |
1413 | 0 | if ((res = mp_mod(c, m, c)) != MP_OKAY) |
1414 | 0 | return res; |
1415 | 0 | |
1416 | 0 | return MP_OKAY; |
1417 | 0 |
|
1418 | 0 | } /* end mp_sqrmod() */ |
1419 | | #endif |
1420 | | |
1421 | | /* }}} */ |
1422 | | |
1423 | | /* {{{ s_mp_exptmod(a, b, m, c) */ |
1424 | | |
1425 | | /* |
1426 | | s_mp_exptmod(a, b, m, c) |
1427 | | |
1428 | | Compute c = (a ** b) mod m. Uses a standard square-and-multiply |
1429 | | method with modular reductions at each step. (This is basically the |
1430 | | same code as mp_expt(), except for the addition of the reductions) |
1431 | | |
1432 | | The modular reductions are done using Barrett's algorithm (see |
1433 | | s_mp_reduce() below for details) |
1434 | | */ |
1435 | | |
1436 | | mp_err |
1437 | | s_mp_exptmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c) |
1438 | 0 | { |
1439 | 0 | mp_int s, x, mu; |
1440 | 0 | mp_err res; |
1441 | 0 | mp_digit d; |
1442 | 0 | unsigned int dig, bit; |
1443 | 0 |
|
1444 | 0 | ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG); |
1445 | 0 |
|
1446 | 0 | if (mp_cmp_z(b) < 0 || mp_cmp_z(m) <= 0) |
1447 | 0 | return MP_RANGE; |
1448 | 0 | |
1449 | 0 | if ((res = mp_init(&s)) != MP_OKAY) |
1450 | 0 | return res; |
1451 | 0 | if ((res = mp_init_copy(&x, a)) != MP_OKAY || |
1452 | 0 | (res = mp_mod(&x, m, &x)) != MP_OKAY) |
1453 | 0 | goto X; |
1454 | 0 | if ((res = mp_init(&mu)) != MP_OKAY) |
1455 | 0 | goto MU; |
1456 | 0 | |
1457 | 0 | mp_set(&s, 1); |
1458 | 0 |
|
1459 | 0 | /* mu = b^2k / m */ |
1460 | 0 | if ((res = s_mp_add_d(&mu, 1)) != MP_OKAY) |
1461 | 0 | goto CLEANUP; |
1462 | 0 | if ((res = s_mp_lshd(&mu, 2 * USED(m))) != MP_OKAY) |
1463 | 0 | goto CLEANUP; |
1464 | 0 | if ((res = mp_div(&mu, m, &mu, NULL)) != MP_OKAY) |
1465 | 0 | goto CLEANUP; |
1466 | 0 | |
1467 | 0 | /* Loop over digits of b in ascending order, except highest order */ |
1468 | 0 | for (dig = 0; dig < (USED(b) - 1); dig++) { |
1469 | 0 | d = DIGIT(b, dig); |
1470 | 0 |
|
1471 | 0 | /* Loop over the bits of the lower-order digits */ |
1472 | 0 | for (bit = 0; bit < DIGIT_BIT; bit++) { |
1473 | 0 | if (d & 1) { |
1474 | 0 | if ((res = s_mp_mul(&s, &x)) != MP_OKAY) |
1475 | 0 | goto CLEANUP; |
1476 | 0 | if ((res = s_mp_reduce(&s, m, &mu)) != MP_OKAY) |
1477 | 0 | goto CLEANUP; |
1478 | 0 | } |
1479 | 0 | |
1480 | 0 | d >>= 1; |
1481 | 0 |
|
1482 | 0 | if ((res = s_mp_sqr(&x)) != MP_OKAY) |
1483 | 0 | goto CLEANUP; |
1484 | 0 | if ((res = s_mp_reduce(&x, m, &mu)) != MP_OKAY) |
1485 | 0 | goto CLEANUP; |
1486 | 0 | } |
1487 | 0 | } |
1488 | 0 |
|
1489 | 0 | /* Now do the last digit... */ |
1490 | 0 | d = DIGIT(b, dig); |
1491 | 0 |
|
1492 | 0 | while (d) { |
1493 | 0 | if (d & 1) { |
1494 | 0 | if ((res = s_mp_mul(&s, &x)) != MP_OKAY) |
1495 | 0 | goto CLEANUP; |
1496 | 0 | if ((res = s_mp_reduce(&s, m, &mu)) != MP_OKAY) |
1497 | 0 | goto CLEANUP; |
1498 | 0 | } |
1499 | 0 | |
1500 | 0 | d >>= 1; |
1501 | 0 |
|
1502 | 0 | if ((res = s_mp_sqr(&x)) != MP_OKAY) |
1503 | 0 | goto CLEANUP; |
1504 | 0 | if ((res = s_mp_reduce(&x, m, &mu)) != MP_OKAY) |
1505 | 0 | goto CLEANUP; |
1506 | 0 | } |
1507 | 0 |
|
1508 | 0 | s_mp_exch(&s, c); |
1509 | 0 |
|
1510 | 0 | CLEANUP: |
1511 | 0 | mp_clear(&mu); |
1512 | 0 | MU: |
1513 | 0 | mp_clear(&x); |
1514 | 0 | X: |
1515 | 0 | mp_clear(&s); |
1516 | 0 |
|
1517 | 0 | return res; |
1518 | 0 |
|
1519 | 0 | } /* end s_mp_exptmod() */ |
1520 | | |
1521 | | /* }}} */ |
1522 | | |
1523 | | /* {{{ mp_exptmod_d(a, d, m, c) */ |
1524 | | |
1525 | | mp_err |
1526 | | mp_exptmod_d(const mp_int *a, mp_digit d, const mp_int *m, mp_int *c) |
1527 | 0 | { |
1528 | 0 | mp_int s, x; |
1529 | 0 | mp_err res; |
1530 | 0 |
|
1531 | 0 | ARGCHK(a != NULL && c != NULL, MP_BADARG); |
1532 | 0 |
|
1533 | 0 | if ((res = mp_init(&s)) != MP_OKAY) |
1534 | 0 | return res; |
1535 | 0 | if ((res = mp_init_copy(&x, a)) != MP_OKAY) |
1536 | 0 | goto X; |
1537 | 0 | |
1538 | 0 | mp_set(&s, 1); |
1539 | 0 |
|
1540 | 0 | while (d != 0) { |
1541 | 0 | if (d & 1) { |
1542 | 0 | if ((res = s_mp_mul(&s, &x)) != MP_OKAY || |
1543 | 0 | (res = mp_mod(&s, m, &s)) != MP_OKAY) |
1544 | 0 | goto CLEANUP; |
1545 | 0 | } |
1546 | 0 | |
1547 | 0 | d /= 2; |
1548 | 0 |
|
1549 | 0 | if ((res = s_mp_sqr(&x)) != MP_OKAY || |
1550 | 0 | (res = mp_mod(&x, m, &x)) != MP_OKAY) |
1551 | 0 | goto CLEANUP; |
1552 | 0 | } |
1553 | 0 |
|
1554 | 0 | s_mp_exch(&s, c); |
1555 | 0 |
|
1556 | 0 | CLEANUP: |
1557 | 0 | mp_clear(&x); |
1558 | 0 | X: |
1559 | 0 | mp_clear(&s); |
1560 | 0 |
|
1561 | 0 | return res; |
1562 | 0 |
|
1563 | 0 | } /* end mp_exptmod_d() */ |
1564 | | |
1565 | | /* }}} */ |
1566 | | #endif /* if MP_MODARITH */ |
1567 | | |
1568 | | /* }}} */ |
1569 | | |
1570 | | /*------------------------------------------------------------------------*/ |
1571 | | /* {{{ Comparison functions */ |
1572 | | |
1573 | | /* {{{ mp_cmp_z(a) */ |
1574 | | |
1575 | | /* |
1576 | | mp_cmp_z(a) |
1577 | | |
1578 | | Compare a <=> 0. Returns <0 if a<0, 0 if a=0, >0 if a>0. |
1579 | | */ |
1580 | | |
1581 | | int |
1582 | | mp_cmp_z(const mp_int *a) |
1583 | 0 | { |
1584 | 0 | if (SIGN(a) == NEG) |
1585 | 0 | return MP_LT; |
1586 | 0 | else if (USED(a) == 1 && DIGIT(a, 0) == 0) |
1587 | 0 | return MP_EQ; |
1588 | 0 | else |
1589 | 0 | return MP_GT; |
1590 | 0 |
|
1591 | 0 | } /* end mp_cmp_z() */ |
1592 | | |
1593 | | /* }}} */ |
1594 | | |
1595 | | /* {{{ mp_cmp_d(a, d) */ |
1596 | | |
1597 | | /* |
1598 | | mp_cmp_d(a, d) |
1599 | | |
1600 | | Compare a <=> d. Returns <0 if a<d, 0 if a=d, >0 if a>d |
1601 | | */ |
1602 | | |
1603 | | int |
1604 | | mp_cmp_d(const mp_int *a, mp_digit d) |
1605 | 0 | { |
1606 | 0 | ARGCHK(a != NULL, MP_EQ); |
1607 | 0 |
|
1608 | 0 | if (SIGN(a) == NEG) |
1609 | 0 | return MP_LT; |
1610 | 0 | |
1611 | 0 | return s_mp_cmp_d(a, d); |
1612 | 0 |
|
1613 | 0 | } /* end mp_cmp_d() */ |
1614 | | |
1615 | | /* }}} */ |
1616 | | |
1617 | | /* {{{ mp_cmp(a, b) */ |
1618 | | |
1619 | | int |
1620 | | mp_cmp(const mp_int *a, const mp_int *b) |
1621 | 0 | { |
1622 | 0 | ARGCHK(a != NULL && b != NULL, MP_EQ); |
1623 | 0 |
|
1624 | 0 | if (SIGN(a) == SIGN(b)) { |
1625 | 0 | int mag; |
1626 | 0 |
|
1627 | 0 | if ((mag = s_mp_cmp(a, b)) == MP_EQ) |
1628 | 0 | return MP_EQ; |
1629 | 0 | |
1630 | 0 | if (SIGN(a) == ZPOS) |
1631 | 0 | return mag; |
1632 | 0 | else |
1633 | 0 | return -mag; |
1634 | 0 | |
1635 | 0 | } else if (SIGN(a) == ZPOS) { |
1636 | 0 | return MP_GT; |
1637 | 0 | } else { |
1638 | 0 | return MP_LT; |
1639 | 0 | } |
1640 | 0 |
|
1641 | 0 | } /* end mp_cmp() */ |
1642 | | |
1643 | | /* }}} */ |
1644 | | |
1645 | | /* {{{ mp_cmp_mag(a, b) */ |
1646 | | |
1647 | | /* |
1648 | | mp_cmp_mag(a, b) |
1649 | | |
1650 | | Compares |a| <=> |b|, and returns an appropriate comparison result |
1651 | | */ |
1652 | | |
1653 | | int |
1654 | | mp_cmp_mag(const mp_int *a, const mp_int *b) |
1655 | 0 | { |
1656 | 0 | ARGCHK(a != NULL && b != NULL, MP_EQ); |
1657 | 0 |
|
1658 | 0 | return s_mp_cmp(a, b); |
1659 | 0 |
|
1660 | 0 | } /* end mp_cmp_mag() */ |
1661 | | |
1662 | | /* }}} */ |
1663 | | |
1664 | | /* {{{ mp_isodd(a) */ |
1665 | | |
1666 | | /* |
1667 | | mp_isodd(a) |
1668 | | |
1669 | | Returns a true (non-zero) value if a is odd, false (zero) otherwise. |
1670 | | */ |
1671 | | int |
1672 | | mp_isodd(const mp_int *a) |
1673 | 0 | { |
1674 | 0 | ARGCHK(a != NULL, 0); |
1675 | 0 |
|
1676 | 0 | return (int)(DIGIT(a, 0) & 1); |
1677 | 0 |
|
1678 | 0 | } /* end mp_isodd() */ |
1679 | | |
1680 | | /* }}} */ |
1681 | | |
1682 | | /* {{{ mp_iseven(a) */ |
1683 | | |
1684 | | int |
1685 | | mp_iseven(const mp_int *a) |
1686 | 0 | { |
1687 | 0 | return !mp_isodd(a); |
1688 | 0 |
|
1689 | 0 | } /* end mp_iseven() */ |
1690 | | |
1691 | | /* }}} */ |
1692 | | |
1693 | | /* }}} */ |
1694 | | |
1695 | | /*------------------------------------------------------------------------*/ |
1696 | | /* {{{ Number theoretic functions */ |
1697 | | |
1698 | | /* {{{ mp_gcd(a, b, c) */ |
1699 | | |
1700 | | /* |
1701 | | Like the old mp_gcd() function, except computes the GCD using the |
1702 | | binary algorithm due to Josef Stein in 1961 (via Knuth). |
1703 | | */ |
1704 | | mp_err |
1705 | | mp_gcd(mp_int *a, mp_int *b, mp_int *c) |
1706 | 0 | { |
1707 | 0 | mp_err res; |
1708 | 0 | mp_int u, v, t; |
1709 | 0 | mp_size k = 0; |
1710 | 0 |
|
1711 | 0 | ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG); |
1712 | 0 |
|
1713 | 0 | if (mp_cmp_z(a) == MP_EQ && mp_cmp_z(b) == MP_EQ) |
1714 | 0 | return MP_RANGE; |
1715 | 0 | if (mp_cmp_z(a) == MP_EQ) { |
1716 | 0 | return mp_copy(b, c); |
1717 | 0 | } else if (mp_cmp_z(b) == MP_EQ) { |
1718 | 0 | return mp_copy(a, c); |
1719 | 0 | } |
1720 | 0 | |
1721 | 0 | if ((res = mp_init(&t)) != MP_OKAY) |
1722 | 0 | return res; |
1723 | 0 | if ((res = mp_init_copy(&u, a)) != MP_OKAY) |
1724 | 0 | goto U; |
1725 | 0 | if ((res = mp_init_copy(&v, b)) != MP_OKAY) |
1726 | 0 | goto V; |
1727 | 0 | |
1728 | 0 | SIGN(&u) = ZPOS; |
1729 | 0 | SIGN(&v) = ZPOS; |
1730 | 0 |
|
1731 | 0 | /* Divide out common factors of 2 until at least 1 of a, b is even */ |
1732 | 0 | while (mp_iseven(&u) && mp_iseven(&v)) { |
1733 | 0 | s_mp_div_2(&u); |
1734 | 0 | s_mp_div_2(&v); |
1735 | 0 | ++k; |
1736 | 0 | } |
1737 | 0 |
|
1738 | 0 | /* Initialize t */ |
1739 | 0 | if (mp_isodd(&u)) { |
1740 | 0 | if ((res = mp_copy(&v, &t)) != MP_OKAY) |
1741 | 0 | goto CLEANUP; |
1742 | 0 | |
1743 | 0 | /* t = -v */ |
1744 | 0 | if (SIGN(&v) == ZPOS) |
1745 | 0 | SIGN(&t) = NEG; |
1746 | 0 | else |
1747 | 0 | SIGN(&t) = ZPOS; |
1748 | 0 |
|
1749 | 0 | } else { |
1750 | 0 | if ((res = mp_copy(&u, &t)) != MP_OKAY) |
1751 | 0 | goto CLEANUP; |
1752 | 0 | } |
1753 | 0 | |
1754 | 0 | for (;;) { |
1755 | 0 | while (mp_iseven(&t)) { |
1756 | 0 | s_mp_div_2(&t); |
1757 | 0 | } |
1758 | 0 |
|
1759 | 0 | if (mp_cmp_z(&t) == MP_GT) { |
1760 | 0 | if ((res = mp_copy(&t, &u)) != MP_OKAY) |
1761 | 0 | goto CLEANUP; |
1762 | 0 | |
1763 | 0 | } else { |
1764 | 0 | if ((res = mp_copy(&t, &v)) != MP_OKAY) |
1765 | 0 | goto CLEANUP; |
1766 | 0 | |
1767 | 0 | /* v = -t */ |
1768 | 0 | if (SIGN(&t) == ZPOS) |
1769 | 0 | SIGN(&v) = NEG; |
1770 | 0 | else |
1771 | 0 | SIGN(&v) = ZPOS; |
1772 | 0 | } |
1773 | 0 |
|
1774 | 0 | if ((res = mp_sub(&u, &v, &t)) != MP_OKAY) |
1775 | 0 | goto CLEANUP; |
1776 | 0 | |
1777 | 0 | if (s_mp_cmp_d(&t, 0) == MP_EQ) |
1778 | 0 | break; |
1779 | 0 | } |
1780 | 0 |
|
1781 | 0 | s_mp_2expt(&v, k); /* v = 2^k */ |
1782 | 0 | res = mp_mul(&u, &v, c); /* c = u * v */ |
1783 | 0 |
|
1784 | 0 | CLEANUP: |
1785 | 0 | mp_clear(&v); |
1786 | 0 | V: |
1787 | 0 | mp_clear(&u); |
1788 | 0 | U: |
1789 | 0 | mp_clear(&t); |
1790 | 0 |
|
1791 | 0 | return res; |
1792 | 0 |
|
1793 | 0 | } /* end mp_gcd() */ |
1794 | | |
1795 | | /* }}} */ |
1796 | | |
1797 | | /* {{{ mp_lcm(a, b, c) */ |
1798 | | |
1799 | | /* We compute the least common multiple using the rule: |
1800 | | |
1801 | | ab = [a, b](a, b) |
1802 | | |
1803 | | ... by computing the product, and dividing out the gcd. |
1804 | | */ |
1805 | | |
1806 | | mp_err |
1807 | | mp_lcm(mp_int *a, mp_int *b, mp_int *c) |
1808 | 0 | { |
1809 | 0 | mp_int gcd, prod; |
1810 | 0 | mp_err res; |
1811 | 0 |
|
1812 | 0 | ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG); |
1813 | 0 |
|
1814 | 0 | /* Set up temporaries */ |
1815 | 0 | if ((res = mp_init(&gcd)) != MP_OKAY) |
1816 | 0 | return res; |
1817 | 0 | if ((res = mp_init(&prod)) != MP_OKAY) |
1818 | 0 | goto GCD; |
1819 | 0 | |
1820 | 0 | if ((res = mp_mul(a, b, &prod)) != MP_OKAY) |
1821 | 0 | goto CLEANUP; |
1822 | 0 | if ((res = mp_gcd(a, b, &gcd)) != MP_OKAY) |
1823 | 0 | goto CLEANUP; |
1824 | 0 | |
1825 | 0 | res = mp_div(&prod, &gcd, c, NULL); |
1826 | 0 |
|
1827 | 0 | CLEANUP: |
1828 | 0 | mp_clear(&prod); |
1829 | 0 | GCD: |
1830 | 0 | mp_clear(&gcd); |
1831 | 0 |
|
1832 | 0 | return res; |
1833 | 0 |
|
1834 | 0 | } /* end mp_lcm() */ |
1835 | | |
1836 | | /* }}} */ |
1837 | | |
1838 | | /* {{{ mp_xgcd(a, b, g, x, y) */ |
1839 | | |
1840 | | /* |
1841 | | mp_xgcd(a, b, g, x, y) |
1842 | | |
1843 | | Compute g = (a, b) and values x and y satisfying Bezout's identity |
1844 | | (that is, ax + by = g). This uses the binary extended GCD algorithm |
1845 | | based on the Stein algorithm used for mp_gcd() |
1846 | | See algorithm 14.61 in Handbook of Applied Cryptogrpahy. |
1847 | | */ |
1848 | | |
1849 | | mp_err |
1850 | | mp_xgcd(const mp_int *a, const mp_int *b, mp_int *g, mp_int *x, mp_int *y) |
1851 | 0 | { |
1852 | 0 | mp_int gx, xc, yc, u, v, A, B, C, D; |
1853 | 0 | mp_int *clean[9]; |
1854 | 0 | mp_err res; |
1855 | 0 | int last = -1; |
1856 | 0 |
|
1857 | 0 | if (mp_cmp_z(b) == 0) |
1858 | 0 | return MP_RANGE; |
1859 | 0 | |
1860 | 0 | /* Initialize all these variables we need */ |
1861 | 0 | MP_CHECKOK(mp_init(&u)); |
1862 | 0 | clean[++last] = &u; |
1863 | 0 | MP_CHECKOK(mp_init(&v)); |
1864 | 0 | clean[++last] = &v; |
1865 | 0 | MP_CHECKOK(mp_init(&gx)); |
1866 | 0 | clean[++last] = &gx; |
1867 | 0 | MP_CHECKOK(mp_init(&A)); |
1868 | 0 | clean[++last] = &A; |
1869 | 0 | MP_CHECKOK(mp_init(&B)); |
1870 | 0 | clean[++last] = &B; |
1871 | 0 | MP_CHECKOK(mp_init(&C)); |
1872 | 0 | clean[++last] = &C; |
1873 | 0 | MP_CHECKOK(mp_init(&D)); |
1874 | 0 | clean[++last] = &D; |
1875 | 0 | MP_CHECKOK(mp_init_copy(&xc, a)); |
1876 | 0 | clean[++last] = &xc; |
1877 | 0 | mp_abs(&xc, &xc); |
1878 | 0 | MP_CHECKOK(mp_init_copy(&yc, b)); |
1879 | 0 | clean[++last] = &yc; |
1880 | 0 | mp_abs(&yc, &yc); |
1881 | 0 |
|
1882 | 0 | mp_set(&gx, 1); |
1883 | 0 |
|
1884 | 0 | /* Divide by two until at least one of them is odd */ |
1885 | 0 | while (mp_iseven(&xc) && mp_iseven(&yc)) { |
1886 | 0 | mp_size nx = mp_trailing_zeros(&xc); |
1887 | 0 | mp_size ny = mp_trailing_zeros(&yc); |
1888 | 0 | mp_size n = MP_MIN(nx, ny); |
1889 | 0 | s_mp_div_2d(&xc, n); |
1890 | 0 | s_mp_div_2d(&yc, n); |
1891 | 0 | MP_CHECKOK(s_mp_mul_2d(&gx, n)); |
1892 | 0 | } |
1893 | 0 |
|
1894 | 0 | MP_CHECKOK(mp_copy(&xc, &u)); |
1895 | 0 | MP_CHECKOK(mp_copy(&yc, &v)); |
1896 | 0 | mp_set(&A, 1); |
1897 | 0 | mp_set(&D, 1); |
1898 | 0 |
|
1899 | 0 | /* Loop through binary GCD algorithm */ |
1900 | 0 | do { |
1901 | 0 | while (mp_iseven(&u)) { |
1902 | 0 | s_mp_div_2(&u); |
1903 | 0 |
|
1904 | 0 | if (mp_iseven(&A) && mp_iseven(&B)) { |
1905 | 0 | s_mp_div_2(&A); |
1906 | 0 | s_mp_div_2(&B); |
1907 | 0 | } else { |
1908 | 0 | MP_CHECKOK(mp_add(&A, &yc, &A)); |
1909 | 0 | s_mp_div_2(&A); |
1910 | 0 | MP_CHECKOK(mp_sub(&B, &xc, &B)); |
1911 | 0 | s_mp_div_2(&B); |
1912 | 0 | } |
1913 | 0 | } |
1914 | 0 |
|
1915 | 0 | while (mp_iseven(&v)) { |
1916 | 0 | s_mp_div_2(&v); |
1917 | 0 |
|
1918 | 0 | if (mp_iseven(&C) && mp_iseven(&D)) { |
1919 | 0 | s_mp_div_2(&C); |
1920 | 0 | s_mp_div_2(&D); |
1921 | 0 | } else { |
1922 | 0 | MP_CHECKOK(mp_add(&C, &yc, &C)); |
1923 | 0 | s_mp_div_2(&C); |
1924 | 0 | MP_CHECKOK(mp_sub(&D, &xc, &D)); |
1925 | 0 | s_mp_div_2(&D); |
1926 | 0 | } |
1927 | 0 | } |
1928 | 0 |
|
1929 | 0 | if (mp_cmp(&u, &v) >= 0) { |
1930 | 0 | MP_CHECKOK(mp_sub(&u, &v, &u)); |
1931 | 0 | MP_CHECKOK(mp_sub(&A, &C, &A)); |
1932 | 0 | MP_CHECKOK(mp_sub(&B, &D, &B)); |
1933 | 0 | } else { |
1934 | 0 | MP_CHECKOK(mp_sub(&v, &u, &v)); |
1935 | 0 | MP_CHECKOK(mp_sub(&C, &A, &C)); |
1936 | 0 | MP_CHECKOK(mp_sub(&D, &B, &D)); |
1937 | 0 | } |
1938 | 0 | } while (mp_cmp_z(&u) != 0); |
1939 | 0 |
|
1940 | 0 | /* copy results to output */ |
1941 | 0 | if (x) |
1942 | 0 | MP_CHECKOK(mp_copy(&C, x)); |
1943 | 0 |
|
1944 | 0 | if (y) |
1945 | 0 | MP_CHECKOK(mp_copy(&D, y)); |
1946 | 0 |
|
1947 | 0 | if (g) |
1948 | 0 | MP_CHECKOK(mp_mul(&gx, &v, g)); |
1949 | 0 |
|
1950 | 0 | CLEANUP: |
1951 | 0 | while (last >= 0) |
1952 | 0 | mp_clear(clean[last--]); |
1953 | 0 |
|
1954 | 0 | return res; |
1955 | 0 |
|
1956 | 0 | } /* end mp_xgcd() */ |
1957 | | |
1958 | | /* }}} */ |
1959 | | |
1960 | | mp_size |
1961 | | mp_trailing_zeros(const mp_int *mp) |
1962 | 0 | { |
1963 | 0 | mp_digit d; |
1964 | 0 | mp_size n = 0; |
1965 | 0 | unsigned int ix; |
1966 | 0 |
|
1967 | 0 | if (!mp || !MP_DIGITS(mp) || !mp_cmp_z(mp)) |
1968 | 0 | return n; |
1969 | 0 | |
1970 | 0 | for (ix = 0; !(d = MP_DIGIT(mp, ix)) && (ix < MP_USED(mp)); ++ix) |
1971 | 0 | n += MP_DIGIT_BIT; |
1972 | 0 | if (!d) |
1973 | 0 | return 0; /* shouldn't happen, but ... */ |
1974 | 0 | #if !defined(MP_USE_UINT_DIGIT) |
1975 | 0 | if (!(d & 0xffffffffU)) { |
1976 | 0 | d >>= 32; |
1977 | 0 | n += 32; |
1978 | 0 | } |
1979 | 0 | #endif |
1980 | 0 | if (!(d & 0xffffU)) { |
1981 | 0 | d >>= 16; |
1982 | 0 | n += 16; |
1983 | 0 | } |
1984 | 0 | if (!(d & 0xffU)) { |
1985 | 0 | d >>= 8; |
1986 | 0 | n += 8; |
1987 | 0 | } |
1988 | 0 | if (!(d & 0xfU)) { |
1989 | 0 | d >>= 4; |
1990 | 0 | n += 4; |
1991 | 0 | } |
1992 | 0 | if (!(d & 0x3U)) { |
1993 | 0 | d >>= 2; |
1994 | 0 | n += 2; |
1995 | 0 | } |
1996 | 0 | if (!(d & 0x1U)) { |
1997 | 0 | d >>= 1; |
1998 | 0 | n += 1; |
1999 | 0 | } |
2000 | | #if MP_ARGCHK == 2 |
2001 | | assert(0 != (d & 1)); |
2002 | | #endif |
2003 | | return n; |
2004 | 0 | } |
2005 | | |
2006 | | /* Given a and prime p, computes c and k such that a*c == 2**k (mod p). |
2007 | | ** Returns k (positive) or error (negative). |
2008 | | ** This technique from the paper "Fast Modular Reciprocals" (unpublished) |
2009 | | ** by Richard Schroeppel (a.k.a. Captain Nemo). |
2010 | | */ |
2011 | | mp_err |
2012 | | s_mp_almost_inverse(const mp_int *a, const mp_int *p, mp_int *c) |
2013 | 0 | { |
2014 | 0 | mp_err res; |
2015 | 0 | mp_err k = 0; |
2016 | 0 | mp_int d, f, g; |
2017 | 0 |
|
2018 | 0 | ARGCHK(a && p && c, MP_BADARG); |
2019 | 0 |
|
2020 | 0 | MP_DIGITS(&d) = 0; |
2021 | 0 | MP_DIGITS(&f) = 0; |
2022 | 0 | MP_DIGITS(&g) = 0; |
2023 | 0 | MP_CHECKOK(mp_init(&d)); |
2024 | 0 | MP_CHECKOK(mp_init_copy(&f, a)); /* f = a */ |
2025 | 0 | MP_CHECKOK(mp_init_copy(&g, p)); /* g = p */ |
2026 | 0 |
|
2027 | 0 | mp_set(c, 1); |
2028 | 0 | mp_zero(&d); |
2029 | 0 |
|
2030 | 0 | if (mp_cmp_z(&f) == 0) { |
2031 | 0 | res = MP_UNDEF; |
2032 | 0 | } else |
2033 | 0 | for (;;) { |
2034 | 0 | int diff_sign; |
2035 | 0 | while (mp_iseven(&f)) { |
2036 | 0 | mp_size n = mp_trailing_zeros(&f); |
2037 | 0 | if (!n) { |
2038 | 0 | res = MP_UNDEF; |
2039 | 0 | goto CLEANUP; |
2040 | 0 | } |
2041 | 0 | s_mp_div_2d(&f, n); |
2042 | 0 | MP_CHECKOK(s_mp_mul_2d(&d, n)); |
2043 | 0 | k += n; |
2044 | 0 | } |
2045 | 0 | if (mp_cmp_d(&f, 1) == MP_EQ) { /* f == 1 */ |
2046 | 0 | res = k; |
2047 | 0 | break; |
2048 | 0 | } |
2049 | 0 | diff_sign = mp_cmp(&f, &g); |
2050 | 0 | if (diff_sign < 0) { /* f < g */ |
2051 | 0 | s_mp_exch(&f, &g); |
2052 | 0 | s_mp_exch(c, &d); |
2053 | 0 | } else if (diff_sign == 0) { /* f == g */ |
2054 | 0 | res = MP_UNDEF; /* a and p are not relatively prime */ |
2055 | 0 | break; |
2056 | 0 | } |
2057 | 0 | if ((MP_DIGIT(&f, 0) % 4) == (MP_DIGIT(&g, 0) % 4)) { |
2058 | 0 | MP_CHECKOK(mp_sub(&f, &g, &f)); /* f = f - g */ |
2059 | 0 | MP_CHECKOK(mp_sub(c, &d, c)); /* c = c - d */ |
2060 | 0 | } else { |
2061 | 0 | MP_CHECKOK(mp_add(&f, &g, &f)); /* f = f + g */ |
2062 | 0 | MP_CHECKOK(mp_add(c, &d, c)); /* c = c + d */ |
2063 | 0 | } |
2064 | 0 | } |
2065 | 0 | if (res >= 0) { |
2066 | 0 | while (MP_SIGN(c) != MP_ZPOS) { |
2067 | 0 | MP_CHECKOK(mp_add(c, p, c)); |
2068 | 0 | } |
2069 | 0 | res = k; |
2070 | 0 | } |
2071 | 0 |
|
2072 | 0 | CLEANUP: |
2073 | 0 | mp_clear(&d); |
2074 | 0 | mp_clear(&f); |
2075 | 0 | mp_clear(&g); |
2076 | 0 | return res; |
2077 | 0 | } |
2078 | | |
2079 | | /* Compute T = (P ** -1) mod MP_RADIX. Also works for 16-bit mp_digits. |
2080 | | ** This technique from the paper "Fast Modular Reciprocals" (unpublished) |
2081 | | ** by Richard Schroeppel (a.k.a. Captain Nemo). |
2082 | | */ |
2083 | | mp_digit |
2084 | | s_mp_invmod_radix(mp_digit P) |
2085 | 0 | { |
2086 | 0 | mp_digit T = P; |
2087 | 0 | T *= 2 - (P * T); |
2088 | 0 | T *= 2 - (P * T); |
2089 | 0 | T *= 2 - (P * T); |
2090 | 0 | T *= 2 - (P * T); |
2091 | 0 | #if !defined(MP_USE_UINT_DIGIT) |
2092 | 0 | T *= 2 - (P * T); |
2093 | 0 | T *= 2 - (P * T); |
2094 | 0 | #endif |
2095 | 0 | return T; |
2096 | 0 | } |
2097 | | |
2098 | | /* Given c, k, and prime p, where a*c == 2**k (mod p), |
2099 | | ** Compute x = (a ** -1) mod p. This is similar to Montgomery reduction. |
2100 | | ** This technique from the paper "Fast Modular Reciprocals" (unpublished) |
2101 | | ** by Richard Schroeppel (a.k.a. Captain Nemo). |
2102 | | */ |
2103 | | mp_err |
2104 | | s_mp_fixup_reciprocal(const mp_int *c, const mp_int *p, int k, mp_int *x) |
2105 | 0 | { |
2106 | 0 | int k_orig = k; |
2107 | 0 | mp_digit r; |
2108 | 0 | mp_size ix; |
2109 | 0 | mp_err res; |
2110 | 0 |
|
2111 | 0 | if (mp_cmp_z(c) < 0) { /* c < 0 */ |
2112 | 0 | MP_CHECKOK(mp_add(c, p, x)); /* x = c + p */ |
2113 | 0 | } else { |
2114 | 0 | MP_CHECKOK(mp_copy(c, x)); /* x = c */ |
2115 | 0 | } |
2116 | 0 |
|
2117 | 0 | /* make sure x is large enough */ |
2118 | 0 | ix = MP_HOWMANY(k, MP_DIGIT_BIT) + MP_USED(p) + 1; |
2119 | 0 | ix = MP_MAX(ix, MP_USED(x)); |
2120 | 0 | MP_CHECKOK(s_mp_pad(x, ix)); |
2121 | 0 |
|
2122 | 0 | r = 0 - s_mp_invmod_radix(MP_DIGIT(p, 0)); |
2123 | 0 |
|
2124 | 0 | for (ix = 0; k > 0; ix++) { |
2125 | 0 | int j = MP_MIN(k, MP_DIGIT_BIT); |
2126 | 0 | mp_digit v = r * MP_DIGIT(x, ix); |
2127 | 0 | if (j < MP_DIGIT_BIT) { |
2128 | 0 | v &= ((mp_digit)1 << j) - 1; /* v = v mod (2 ** j) */ |
2129 | 0 | } |
2130 | 0 | s_mp_mul_d_add_offset(p, v, x, ix); /* x += p * v * (RADIX ** ix) */ |
2131 | 0 | k -= j; |
2132 | 0 | } |
2133 | 0 | s_mp_clamp(x); |
2134 | 0 | s_mp_div_2d(x, k_orig); |
2135 | 0 | res = MP_OKAY; |
2136 | 0 |
|
2137 | 0 | CLEANUP: |
2138 | 0 | return res; |
2139 | 0 | } |
2140 | | |
2141 | | /* compute mod inverse using Schroeppel's method, only if m is odd */ |
2142 | | mp_err |
2143 | | s_mp_invmod_odd_m(const mp_int *a, const mp_int *m, mp_int *c) |
2144 | 0 | { |
2145 | 0 | int k; |
2146 | 0 | mp_err res; |
2147 | 0 | mp_int x; |
2148 | 0 |
|
2149 | 0 | ARGCHK(a && m && c, MP_BADARG); |
2150 | 0 |
|
2151 | 0 | if (mp_cmp_z(a) == 0 || mp_cmp_z(m) == 0) |
2152 | 0 | return MP_RANGE; |
2153 | 0 | if (mp_iseven(m)) |
2154 | 0 | return MP_UNDEF; |
2155 | 0 | |
2156 | 0 | MP_DIGITS(&x) = 0; |
2157 | 0 |
|
2158 | 0 | if (a == c) { |
2159 | 0 | if ((res = mp_init_copy(&x, a)) != MP_OKAY) |
2160 | 0 | return res; |
2161 | 0 | if (a == m) |
2162 | 0 | m = &x; |
2163 | 0 | a = &x; |
2164 | 0 | } else if (m == c) { |
2165 | 0 | if ((res = mp_init_copy(&x, m)) != MP_OKAY) |
2166 | 0 | return res; |
2167 | 0 | m = &x; |
2168 | 0 | } else { |
2169 | 0 | MP_DIGITS(&x) = 0; |
2170 | 0 | } |
2171 | 0 |
|
2172 | 0 | MP_CHECKOK(s_mp_almost_inverse(a, m, c)); |
2173 | 0 | k = res; |
2174 | 0 | MP_CHECKOK(s_mp_fixup_reciprocal(c, m, k, c)); |
2175 | 0 | CLEANUP: |
2176 | 0 | mp_clear(&x); |
2177 | 0 | return res; |
2178 | 0 | } |
2179 | | |
2180 | | /* Known good algorithm for computing modular inverse. But slow. */ |
2181 | | mp_err |
2182 | | mp_invmod_xgcd(const mp_int *a, const mp_int *m, mp_int *c) |
2183 | 0 | { |
2184 | 0 | mp_int g, x; |
2185 | 0 | mp_err res; |
2186 | 0 |
|
2187 | 0 | ARGCHK(a && m && c, MP_BADARG); |
2188 | 0 |
|
2189 | 0 | if (mp_cmp_z(a) == 0 || mp_cmp_z(m) == 0) |
2190 | 0 | return MP_RANGE; |
2191 | 0 | |
2192 | 0 | MP_DIGITS(&g) = 0; |
2193 | 0 | MP_DIGITS(&x) = 0; |
2194 | 0 | MP_CHECKOK(mp_init(&x)); |
2195 | 0 | MP_CHECKOK(mp_init(&g)); |
2196 | 0 |
|
2197 | 0 | MP_CHECKOK(mp_xgcd(a, m, &g, &x, NULL)); |
2198 | 0 |
|
2199 | 0 | if (mp_cmp_d(&g, 1) != MP_EQ) { |
2200 | 0 | res = MP_UNDEF; |
2201 | 0 | goto CLEANUP; |
2202 | 0 | } |
2203 | 0 |
|
2204 | 0 | res = mp_mod(&x, m, c); |
2205 | 0 | SIGN(c) = SIGN(a); |
2206 | 0 |
|
2207 | 0 | CLEANUP: |
2208 | 0 | mp_clear(&x); |
2209 | 0 | mp_clear(&g); |
2210 | 0 |
|
2211 | 0 | return res; |
2212 | 0 | } |
2213 | | |
2214 | | /* modular inverse where modulus is 2**k. */ |
2215 | | /* c = a**-1 mod 2**k */ |
2216 | | mp_err |
2217 | | s_mp_invmod_2d(const mp_int *a, mp_size k, mp_int *c) |
2218 | 0 | { |
2219 | 0 | mp_err res; |
2220 | 0 | mp_size ix = k + 4; |
2221 | 0 | mp_int t0, t1, val, tmp, two2k; |
2222 | 0 |
|
2223 | 0 | static const mp_digit d2 = 2; |
2224 | 0 | static const mp_int two = { MP_ZPOS, 1, 1, (mp_digit *)&d2 }; |
2225 | 0 |
|
2226 | 0 | if (mp_iseven(a)) |
2227 | 0 | return MP_UNDEF; |
2228 | 0 | if (k <= MP_DIGIT_BIT) { |
2229 | 0 | mp_digit i = s_mp_invmod_radix(MP_DIGIT(a, 0)); |
2230 | 0 | if (k < MP_DIGIT_BIT) |
2231 | 0 | i &= ((mp_digit)1 << k) - (mp_digit)1; |
2232 | 0 | mp_set(c, i); |
2233 | 0 | return MP_OKAY; |
2234 | 0 | } |
2235 | 0 | MP_DIGITS(&t0) = 0; |
2236 | 0 | MP_DIGITS(&t1) = 0; |
2237 | 0 | MP_DIGITS(&val) = 0; |
2238 | 0 | MP_DIGITS(&tmp) = 0; |
2239 | 0 | MP_DIGITS(&two2k) = 0; |
2240 | 0 | MP_CHECKOK(mp_init_copy(&val, a)); |
2241 | 0 | s_mp_mod_2d(&val, k); |
2242 | 0 | MP_CHECKOK(mp_init_copy(&t0, &val)); |
2243 | 0 | MP_CHECKOK(mp_init_copy(&t1, &t0)); |
2244 | 0 | MP_CHECKOK(mp_init(&tmp)); |
2245 | 0 | MP_CHECKOK(mp_init(&two2k)); |
2246 | 0 | MP_CHECKOK(s_mp_2expt(&two2k, k)); |
2247 | 0 | do { |
2248 | 0 | MP_CHECKOK(mp_mul(&val, &t1, &tmp)); |
2249 | 0 | MP_CHECKOK(mp_sub(&two, &tmp, &tmp)); |
2250 | 0 | MP_CHECKOK(mp_mul(&t1, &tmp, &t1)); |
2251 | 0 | s_mp_mod_2d(&t1, k); |
2252 | 0 | while (MP_SIGN(&t1) != MP_ZPOS) { |
2253 | 0 | MP_CHECKOK(mp_add(&t1, &two2k, &t1)); |
2254 | 0 | } |
2255 | 0 | if (mp_cmp(&t1, &t0) == MP_EQ) |
2256 | 0 | break; |
2257 | 0 | MP_CHECKOK(mp_copy(&t1, &t0)); |
2258 | 0 | } while (--ix > 0); |
2259 | 0 | if (!ix) { |
2260 | 0 | res = MP_UNDEF; |
2261 | 0 | } else { |
2262 | 0 | mp_exch(c, &t1); |
2263 | 0 | } |
2264 | 0 |
|
2265 | 0 | CLEANUP: |
2266 | 0 | mp_clear(&t0); |
2267 | 0 | mp_clear(&t1); |
2268 | 0 | mp_clear(&val); |
2269 | 0 | mp_clear(&tmp); |
2270 | 0 | mp_clear(&two2k); |
2271 | 0 | return res; |
2272 | 0 | } |
2273 | | |
2274 | | mp_err |
2275 | | s_mp_invmod_even_m(const mp_int *a, const mp_int *m, mp_int *c) |
2276 | 0 | { |
2277 | 0 | mp_err res; |
2278 | 0 | mp_size k; |
2279 | 0 | mp_int oddFactor, evenFactor; /* factors of the modulus */ |
2280 | 0 | mp_int oddPart, evenPart; /* parts to combine via CRT. */ |
2281 | 0 | mp_int C2, tmp1, tmp2; |
2282 | 0 |
|
2283 | 0 | /*static const mp_digit d1 = 1; */ |
2284 | 0 | /*static const mp_int one = { MP_ZPOS, 1, 1, (mp_digit *)&d1 }; */ |
2285 | 0 |
|
2286 | 0 | if ((res = s_mp_ispow2(m)) >= 0) { |
2287 | 0 | k = res; |
2288 | 0 | return s_mp_invmod_2d(a, k, c); |
2289 | 0 | } |
2290 | 0 | MP_DIGITS(&oddFactor) = 0; |
2291 | 0 | MP_DIGITS(&evenFactor) = 0; |
2292 | 0 | MP_DIGITS(&oddPart) = 0; |
2293 | 0 | MP_DIGITS(&evenPart) = 0; |
2294 | 0 | MP_DIGITS(&C2) = 0; |
2295 | 0 | MP_DIGITS(&tmp1) = 0; |
2296 | 0 | MP_DIGITS(&tmp2) = 0; |
2297 | 0 |
|
2298 | 0 | MP_CHECKOK(mp_init_copy(&oddFactor, m)); /* oddFactor = m */ |
2299 | 0 | MP_CHECKOK(mp_init(&evenFactor)); |
2300 | 0 | MP_CHECKOK(mp_init(&oddPart)); |
2301 | 0 | MP_CHECKOK(mp_init(&evenPart)); |
2302 | 0 | MP_CHECKOK(mp_init(&C2)); |
2303 | 0 | MP_CHECKOK(mp_init(&tmp1)); |
2304 | 0 | MP_CHECKOK(mp_init(&tmp2)); |
2305 | 0 |
|
2306 | 0 | k = mp_trailing_zeros(m); |
2307 | 0 | s_mp_div_2d(&oddFactor, k); |
2308 | 0 | MP_CHECKOK(s_mp_2expt(&evenFactor, k)); |
2309 | 0 |
|
2310 | 0 | /* compute a**-1 mod oddFactor. */ |
2311 | 0 | MP_CHECKOK(s_mp_invmod_odd_m(a, &oddFactor, &oddPart)); |
2312 | 0 | /* compute a**-1 mod evenFactor, where evenFactor == 2**k. */ |
2313 | 0 | MP_CHECKOK(s_mp_invmod_2d(a, k, &evenPart)); |
2314 | 0 |
|
2315 | 0 | /* Use Chinese Remainer theorem to compute a**-1 mod m. */ |
2316 | 0 | /* let m1 = oddFactor, v1 = oddPart, |
2317 | 0 | * let m2 = evenFactor, v2 = evenPart. |
2318 | 0 | */ |
2319 | 0 |
|
2320 | 0 | /* Compute C2 = m1**-1 mod m2. */ |
2321 | 0 | MP_CHECKOK(s_mp_invmod_2d(&oddFactor, k, &C2)); |
2322 | 0 |
|
2323 | 0 | /* compute u = (v2 - v1)*C2 mod m2 */ |
2324 | 0 | MP_CHECKOK(mp_sub(&evenPart, &oddPart, &tmp1)); |
2325 | 0 | MP_CHECKOK(mp_mul(&tmp1, &C2, &tmp2)); |
2326 | 0 | s_mp_mod_2d(&tmp2, k); |
2327 | 0 | while (MP_SIGN(&tmp2) != MP_ZPOS) { |
2328 | 0 | MP_CHECKOK(mp_add(&tmp2, &evenFactor, &tmp2)); |
2329 | 0 | } |
2330 | 0 |
|
2331 | 0 | /* compute answer = v1 + u*m1 */ |
2332 | 0 | MP_CHECKOK(mp_mul(&tmp2, &oddFactor, c)); |
2333 | 0 | MP_CHECKOK(mp_add(&oddPart, c, c)); |
2334 | 0 | /* not sure this is necessary, but it's low cost if not. */ |
2335 | 0 | MP_CHECKOK(mp_mod(c, m, c)); |
2336 | 0 |
|
2337 | 0 | CLEANUP: |
2338 | 0 | mp_clear(&oddFactor); |
2339 | 0 | mp_clear(&evenFactor); |
2340 | 0 | mp_clear(&oddPart); |
2341 | 0 | mp_clear(&evenPart); |
2342 | 0 | mp_clear(&C2); |
2343 | 0 | mp_clear(&tmp1); |
2344 | 0 | mp_clear(&tmp2); |
2345 | 0 | return res; |
2346 | 0 | } |
2347 | | |
2348 | | /* {{{ mp_invmod(a, m, c) */ |
2349 | | |
2350 | | /* |
2351 | | mp_invmod(a, m, c) |
2352 | | |
2353 | | Compute c = a^-1 (mod m), if there is an inverse for a (mod m). |
2354 | | This is equivalent to the question of whether (a, m) = 1. If not, |
2355 | | MP_UNDEF is returned, and there is no inverse. |
2356 | | */ |
2357 | | |
2358 | | mp_err |
2359 | | mp_invmod(const mp_int *a, const mp_int *m, mp_int *c) |
2360 | 0 | { |
2361 | 0 |
|
2362 | 0 | ARGCHK(a && m && c, MP_BADARG); |
2363 | 0 |
|
2364 | 0 | if (mp_cmp_z(a) == 0 || mp_cmp_z(m) == 0) |
2365 | 0 | return MP_RANGE; |
2366 | 0 | |
2367 | 0 | if (mp_isodd(m)) { |
2368 | 0 | return s_mp_invmod_odd_m(a, m, c); |
2369 | 0 | } |
2370 | 0 | if (mp_iseven(a)) |
2371 | 0 | return MP_UNDEF; /* not invertable */ |
2372 | 0 | |
2373 | 0 | return s_mp_invmod_even_m(a, m, c); |
2374 | 0 |
|
2375 | 0 | } /* end mp_invmod() */ |
2376 | | |
2377 | | /* }}} */ |
2378 | | |
2379 | | /* }}} */ |
2380 | | |
2381 | | /*------------------------------------------------------------------------*/ |
2382 | | /* {{{ mp_print(mp, ofp) */ |
2383 | | |
2384 | | #if MP_IOFUNC |
2385 | | /* |
2386 | | mp_print(mp, ofp) |
2387 | | |
2388 | | Print a textual representation of the given mp_int on the output |
2389 | | stream 'ofp'. Output is generated using the internal radix. |
2390 | | */ |
2391 | | |
2392 | | void |
2393 | | mp_print(mp_int *mp, FILE *ofp) |
2394 | | { |
2395 | | int ix; |
2396 | | |
2397 | | if (mp == NULL || ofp == NULL) |
2398 | | return; |
2399 | | |
2400 | | fputc((SIGN(mp) == NEG) ? '-' : '+', ofp); |
2401 | | |
2402 | | for (ix = USED(mp) - 1; ix >= 0; ix--) { |
2403 | | fprintf(ofp, DIGIT_FMT, DIGIT(mp, ix)); |
2404 | | } |
2405 | | |
2406 | | } /* end mp_print() */ |
2407 | | |
2408 | | #endif /* if MP_IOFUNC */ |
2409 | | |
2410 | | /* }}} */ |
2411 | | |
2412 | | /*------------------------------------------------------------------------*/ |
2413 | | /* {{{ More I/O Functions */ |
2414 | | |
2415 | | /* {{{ mp_read_raw(mp, str, len) */ |
2416 | | |
2417 | | /* |
2418 | | mp_read_raw(mp, str, len) |
2419 | | |
2420 | | Read in a raw value (base 256) into the given mp_int |
2421 | | */ |
2422 | | |
2423 | | mp_err |
2424 | | mp_read_raw(mp_int *mp, char *str, int len) |
2425 | 0 | { |
2426 | 0 | int ix; |
2427 | 0 | mp_err res; |
2428 | 0 | unsigned char *ustr = (unsigned char *)str; |
2429 | 0 |
|
2430 | 0 | ARGCHK(mp != NULL && str != NULL && len > 0, MP_BADARG); |
2431 | 0 |
|
2432 | 0 | mp_zero(mp); |
2433 | 0 |
|
2434 | 0 | /* Get sign from first byte */ |
2435 | 0 | if (ustr[0]) |
2436 | 0 | SIGN(mp) = NEG; |
2437 | 0 | else |
2438 | 0 | SIGN(mp) = ZPOS; |
2439 | 0 |
|
2440 | 0 | /* Read the rest of the digits */ |
2441 | 0 | for (ix = 1; ix < len; ix++) { |
2442 | 0 | if ((res = mp_mul_d(mp, 256, mp)) != MP_OKAY) |
2443 | 0 | return res; |
2444 | 0 | if ((res = mp_add_d(mp, ustr[ix], mp)) != MP_OKAY) |
2445 | 0 | return res; |
2446 | 0 | } |
2447 | 0 |
|
2448 | 0 | return MP_OKAY; |
2449 | 0 |
|
2450 | 0 | } /* end mp_read_raw() */ |
2451 | | |
2452 | | /* }}} */ |
2453 | | |
2454 | | /* {{{ mp_raw_size(mp) */ |
2455 | | |
2456 | | int |
2457 | | mp_raw_size(mp_int *mp) |
2458 | 0 | { |
2459 | 0 | ARGCHK(mp != NULL, 0); |
2460 | 0 |
|
2461 | 0 | return (USED(mp) * sizeof(mp_digit)) + 1; |
2462 | 0 |
|
2463 | 0 | } /* end mp_raw_size() */ |
2464 | | |
2465 | | /* }}} */ |
2466 | | |
2467 | | /* {{{ mp_toraw(mp, str) */ |
2468 | | |
2469 | | mp_err |
2470 | | mp_toraw(mp_int *mp, char *str) |
2471 | 0 | { |
2472 | 0 | int ix, jx, pos = 1; |
2473 | 0 |
|
2474 | 0 | ARGCHK(mp != NULL && str != NULL, MP_BADARG); |
2475 | 0 |
|
2476 | 0 | str[0] = (char)SIGN(mp); |
2477 | 0 |
|
2478 | 0 | /* Iterate over each digit... */ |
2479 | 0 | for (ix = USED(mp) - 1; ix >= 0; ix--) { |
2480 | 0 | mp_digit d = DIGIT(mp, ix); |
2481 | 0 |
|
2482 | 0 | /* Unpack digit bytes, high order first */ |
2483 | 0 | for (jx = sizeof(mp_digit) - 1; jx >= 0; jx--) { |
2484 | 0 | str[pos++] = (char)(d >> (jx * CHAR_BIT)); |
2485 | 0 | } |
2486 | 0 | } |
2487 | 0 |
|
2488 | 0 | return MP_OKAY; |
2489 | 0 |
|
2490 | 0 | } /* end mp_toraw() */ |
2491 | | |
2492 | | /* }}} */ |
2493 | | |
2494 | | /* {{{ mp_read_radix(mp, str, radix) */ |
2495 | | |
2496 | | /* |
2497 | | mp_read_radix(mp, str, radix) |
2498 | | |
2499 | | Read an integer from the given string, and set mp to the resulting |
2500 | | value. The input is presumed to be in base 10. Leading non-digit |
2501 | | characters are ignored, and the function reads until a non-digit |
2502 | | character or the end of the string. |
2503 | | */ |
2504 | | |
2505 | | mp_err |
2506 | | mp_read_radix(mp_int *mp, const char *str, int radix) |
2507 | 0 | { |
2508 | 0 | int ix = 0, val = 0; |
2509 | 0 | mp_err res; |
2510 | 0 | mp_sign sig = ZPOS; |
2511 | 0 |
|
2512 | 0 | ARGCHK(mp != NULL && str != NULL && radix >= 2 && radix <= MAX_RADIX, |
2513 | 0 | MP_BADARG); |
2514 | 0 |
|
2515 | 0 | mp_zero(mp); |
2516 | 0 |
|
2517 | 0 | /* Skip leading non-digit characters until a digit or '-' or '+' */ |
2518 | 0 | while (str[ix] && |
2519 | 0 | (s_mp_tovalue(str[ix], radix) < 0) && |
2520 | 0 | str[ix] != '-' && |
2521 | 0 | str[ix] != '+') { |
2522 | 0 | ++ix; |
2523 | 0 | } |
2524 | 0 |
|
2525 | 0 | if (str[ix] == '-') { |
2526 | 0 | sig = NEG; |
2527 | 0 | ++ix; |
2528 | 0 | } else if (str[ix] == '+') { |
2529 | 0 | sig = ZPOS; /* this is the default anyway... */ |
2530 | 0 | ++ix; |
2531 | 0 | } |
2532 | 0 |
|
2533 | 0 | while ((val = s_mp_tovalue(str[ix], radix)) >= 0) { |
2534 | 0 | if ((res = s_mp_mul_d(mp, radix)) != MP_OKAY) |
2535 | 0 | return res; |
2536 | 0 | if ((res = s_mp_add_d(mp, val)) != MP_OKAY) |
2537 | 0 | return res; |
2538 | 0 | ++ix; |
2539 | 0 | } |
2540 | 0 |
|
2541 | 0 | if (s_mp_cmp_d(mp, 0) == MP_EQ) |
2542 | 0 | SIGN(mp) = ZPOS; |
2543 | 0 | else |
2544 | 0 | SIGN(mp) = sig; |
2545 | 0 |
|
2546 | 0 | return MP_OKAY; |
2547 | 0 |
|
2548 | 0 | } /* end mp_read_radix() */ |
2549 | | |
2550 | | mp_err |
2551 | | mp_read_variable_radix(mp_int *a, const char *str, int default_radix) |
2552 | 0 | { |
2553 | 0 | int radix = default_radix; |
2554 | 0 | int cx; |
2555 | 0 | mp_sign sig = ZPOS; |
2556 | 0 | mp_err res; |
2557 | 0 |
|
2558 | 0 | /* Skip leading non-digit characters until a digit or '-' or '+' */ |
2559 | 0 | while ((cx = *str) != 0 && |
2560 | 0 | (s_mp_tovalue(cx, radix) < 0) && |
2561 | 0 | cx != '-' && |
2562 | 0 | cx != '+') { |
2563 | 0 | ++str; |
2564 | 0 | } |
2565 | 0 |
|
2566 | 0 | if (cx == '-') { |
2567 | 0 | sig = NEG; |
2568 | 0 | ++str; |
2569 | 0 | } else if (cx == '+') { |
2570 | 0 | sig = ZPOS; /* this is the default anyway... */ |
2571 | 0 | ++str; |
2572 | 0 | } |
2573 | 0 |
|
2574 | 0 | if (str[0] == '0') { |
2575 | 0 | if ((str[1] | 0x20) == 'x') { |
2576 | 0 | radix = 16; |
2577 | 0 | str += 2; |
2578 | 0 | } else { |
2579 | 0 | radix = 8; |
2580 | 0 | str++; |
2581 | 0 | } |
2582 | 0 | } |
2583 | 0 | res = mp_read_radix(a, str, radix); |
2584 | 0 | if (res == MP_OKAY) { |
2585 | 0 | MP_SIGN(a) = (s_mp_cmp_d(a, 0) == MP_EQ) ? ZPOS : sig; |
2586 | 0 | } |
2587 | 0 | return res; |
2588 | 0 | } |
2589 | | |
2590 | | /* }}} */ |
2591 | | |
2592 | | /* {{{ mp_radix_size(mp, radix) */ |
2593 | | |
2594 | | int |
2595 | | mp_radix_size(mp_int *mp, int radix) |
2596 | 0 | { |
2597 | 0 | int bits; |
2598 | 0 |
|
2599 | 0 | if (!mp || radix < 2 || radix > MAX_RADIX) |
2600 | 0 | return 0; |
2601 | 0 | |
2602 | 0 | bits = USED(mp) * DIGIT_BIT - 1; |
2603 | 0 |
|
2604 | 0 | return s_mp_outlen(bits, radix); |
2605 | 0 |
|
2606 | 0 | } /* end mp_radix_size() */ |
2607 | | |
2608 | | /* }}} */ |
2609 | | |
2610 | | /* {{{ mp_toradix(mp, str, radix) */ |
2611 | | |
2612 | | mp_err |
2613 | | mp_toradix(mp_int *mp, char *str, int radix) |
2614 | 0 | { |
2615 | 0 | int ix, pos = 0; |
2616 | 0 |
|
2617 | 0 | ARGCHK(mp != NULL && str != NULL, MP_BADARG); |
2618 | 0 | ARGCHK(radix > 1 && radix <= MAX_RADIX, MP_RANGE); |
2619 | 0 |
|
2620 | 0 | if (mp_cmp_z(mp) == MP_EQ) { |
2621 | 0 | str[0] = '0'; |
2622 | 0 | str[1] = '\0'; |
2623 | 0 | } else { |
2624 | 0 | mp_err res; |
2625 | 0 | mp_int tmp; |
2626 | 0 | mp_sign sgn; |
2627 | 0 | mp_digit rem, rdx = (mp_digit)radix; |
2628 | 0 | char ch; |
2629 | 0 |
|
2630 | 0 | if ((res = mp_init_copy(&tmp, mp)) != MP_OKAY) |
2631 | 0 | return res; |
2632 | 0 | |
2633 | 0 | /* Save sign for later, and take absolute value */ |
2634 | 0 | sgn = SIGN(&tmp); |
2635 | 0 | SIGN(&tmp) = ZPOS; |
2636 | 0 |
|
2637 | 0 | /* Generate output digits in reverse order */ |
2638 | 0 | while (mp_cmp_z(&tmp) != 0) { |
2639 | 0 | if ((res = mp_div_d(&tmp, rdx, &tmp, &rem)) != MP_OKAY) { |
2640 | 0 | mp_clear(&tmp); |
2641 | 0 | return res; |
2642 | 0 | } |
2643 | 0 | |
2644 | 0 | /* Generate digits, use capital letters */ |
2645 | 0 | ch = s_mp_todigit(rem, radix, 0); |
2646 | 0 |
|
2647 | 0 | str[pos++] = ch; |
2648 | 0 | } |
2649 | 0 |
|
2650 | 0 | /* Add - sign if original value was negative */ |
2651 | 0 | if (sgn == NEG) |
2652 | 0 | str[pos++] = '-'; |
2653 | 0 |
|
2654 | 0 | /* Add trailing NUL to end the string */ |
2655 | 0 | str[pos--] = '\0'; |
2656 | 0 |
|
2657 | 0 | /* Reverse the digits and sign indicator */ |
2658 | 0 | ix = 0; |
2659 | 0 | while (ix < pos) { |
2660 | 0 | char tmpc = str[ix]; |
2661 | 0 |
|
2662 | 0 | str[ix] = str[pos]; |
2663 | 0 | str[pos] = tmpc; |
2664 | 0 | ++ix; |
2665 | 0 | --pos; |
2666 | 0 | } |
2667 | 0 |
|
2668 | 0 | mp_clear(&tmp); |
2669 | 0 | } |
2670 | 0 |
|
2671 | 0 | return MP_OKAY; |
2672 | 0 |
|
2673 | 0 | } /* end mp_toradix() */ |
2674 | | |
2675 | | /* }}} */ |
2676 | | |
2677 | | /* {{{ mp_tovalue(ch, r) */ |
2678 | | |
2679 | | int |
2680 | | mp_tovalue(char ch, int r) |
2681 | 0 | { |
2682 | 0 | return s_mp_tovalue(ch, r); |
2683 | 0 |
|
2684 | 0 | } /* end mp_tovalue() */ |
2685 | | |
2686 | | /* }}} */ |
2687 | | |
2688 | | /* }}} */ |
2689 | | |
2690 | | /* {{{ mp_strerror(ec) */ |
2691 | | |
2692 | | /* |
2693 | | mp_strerror(ec) |
2694 | | |
2695 | | Return a string describing the meaning of error code 'ec'. The |
2696 | | string returned is allocated in static memory, so the caller should |
2697 | | not attempt to modify or free the memory associated with this |
2698 | | string. |
2699 | | */ |
2700 | | const char * |
2701 | | mp_strerror(mp_err ec) |
2702 | 0 | { |
2703 | 0 | int aec = (ec < 0) ? -ec : ec; |
2704 | 0 |
|
2705 | 0 | /* Code values are negative, so the senses of these comparisons |
2706 | 0 | are accurate */ |
2707 | 0 | if (ec < MP_LAST_CODE || ec > MP_OKAY) { |
2708 | 0 | return mp_err_string[0]; /* unknown error code */ |
2709 | 0 | } else { |
2710 | 0 | return mp_err_string[aec + 1]; |
2711 | 0 | } |
2712 | 0 |
|
2713 | 0 | } /* end mp_strerror() */ |
2714 | | |
2715 | | /* }}} */ |
2716 | | |
2717 | | /*========================================================================*/ |
2718 | | /*------------------------------------------------------------------------*/ |
2719 | | /* Static function definitions (internal use only) */ |
2720 | | |
2721 | | /* {{{ Memory management */ |
2722 | | |
2723 | | /* {{{ s_mp_grow(mp, min) */ |
2724 | | |
2725 | | /* Make sure there are at least 'min' digits allocated to mp */ |
2726 | | mp_err |
2727 | | s_mp_grow(mp_int *mp, mp_size min) |
2728 | 0 | { |
2729 | 0 | if (min > ALLOC(mp)) { |
2730 | 0 | mp_digit *tmp; |
2731 | 0 |
|
2732 | 0 | /* Set min to next nearest default precision block size */ |
2733 | 0 | min = MP_ROUNDUP(min, s_mp_defprec); |
2734 | 0 |
|
2735 | 0 | if ((tmp = s_mp_alloc(min, sizeof(mp_digit))) == NULL) |
2736 | 0 | return MP_MEM; |
2737 | 0 | |
2738 | 0 | s_mp_copy(DIGITS(mp), tmp, USED(mp)); |
2739 | 0 |
|
2740 | 0 | s_mp_setz(DIGITS(mp), ALLOC(mp)); |
2741 | 0 | s_mp_free(DIGITS(mp)); |
2742 | 0 | DIGITS(mp) = tmp; |
2743 | 0 | ALLOC(mp) = min; |
2744 | 0 | } |
2745 | 0 |
|
2746 | 0 | return MP_OKAY; |
2747 | 0 |
|
2748 | 0 | } /* end s_mp_grow() */ |
2749 | | |
2750 | | /* }}} */ |
2751 | | |
2752 | | /* {{{ s_mp_pad(mp, min) */ |
2753 | | |
2754 | | /* Make sure the used size of mp is at least 'min', growing if needed */ |
2755 | | mp_err |
2756 | | s_mp_pad(mp_int *mp, mp_size min) |
2757 | 0 | { |
2758 | 0 | if (min > USED(mp)) { |
2759 | 0 | mp_err res; |
2760 | 0 |
|
2761 | 0 | /* Make sure there is room to increase precision */ |
2762 | 0 | if (min > ALLOC(mp)) { |
2763 | 0 | if ((res = s_mp_grow(mp, min)) != MP_OKAY) |
2764 | 0 | return res; |
2765 | 0 | } else { |
2766 | 0 | s_mp_setz(DIGITS(mp) + USED(mp), min - USED(mp)); |
2767 | 0 | } |
2768 | 0 |
|
2769 | 0 | /* Increase precision; should already be 0-filled */ |
2770 | 0 | USED(mp) = min; |
2771 | 0 | } |
2772 | 0 |
|
2773 | 0 | return MP_OKAY; |
2774 | 0 |
|
2775 | 0 | } /* end s_mp_pad() */ |
2776 | | |
2777 | | /* }}} */ |
2778 | | |
2779 | | /* {{{ s_mp_setz(dp, count) */ |
2780 | | |
2781 | | /* Set 'count' digits pointed to by dp to be zeroes */ |
2782 | | void |
2783 | | s_mp_setz(mp_digit *dp, mp_size count) |
2784 | 0 | { |
2785 | 0 | memset(dp, 0, count * sizeof(mp_digit)); |
2786 | 0 | } /* end s_mp_setz() */ |
2787 | | |
2788 | | /* }}} */ |
2789 | | |
2790 | | /* {{{ s_mp_copy(sp, dp, count) */ |
2791 | | |
2792 | | /* Copy 'count' digits from sp to dp */ |
2793 | | void |
2794 | | s_mp_copy(const mp_digit *sp, mp_digit *dp, mp_size count) |
2795 | 0 | { |
2796 | 0 | memcpy(dp, sp, count * sizeof(mp_digit)); |
2797 | 0 | } /* end s_mp_copy() */ |
2798 | | |
2799 | | /* }}} */ |
2800 | | |
2801 | | /* {{{ s_mp_alloc(nb, ni) */ |
2802 | | |
2803 | | /* Allocate ni records of nb bytes each, and return a pointer to that */ |
2804 | | void * |
2805 | | s_mp_alloc(size_t nb, size_t ni) |
2806 | 0 | { |
2807 | 0 | return calloc(nb, ni); |
2808 | 0 |
|
2809 | 0 | } /* end s_mp_alloc() */ |
2810 | | |
2811 | | /* }}} */ |
2812 | | |
2813 | | /* {{{ s_mp_free(ptr) */ |
2814 | | |
2815 | | /* Free the memory pointed to by ptr */ |
2816 | | void |
2817 | | s_mp_free(void *ptr) |
2818 | 0 | { |
2819 | 0 | if (ptr) { |
2820 | 0 | free(ptr); |
2821 | 0 | } |
2822 | 0 | } /* end s_mp_free() */ |
2823 | | |
2824 | | /* }}} */ |
2825 | | |
2826 | | /* {{{ s_mp_clamp(mp) */ |
2827 | | |
2828 | | /* Remove leading zeroes from the given value */ |
2829 | | void |
2830 | | s_mp_clamp(mp_int *mp) |
2831 | 0 | { |
2832 | 0 | mp_size used = MP_USED(mp); |
2833 | 0 | while (used > 1 && DIGIT(mp, used - 1) == 0) |
2834 | 0 | --used; |
2835 | 0 | MP_USED(mp) = used; |
2836 | 0 | } /* end s_mp_clamp() */ |
2837 | | |
2838 | | /* }}} */ |
2839 | | |
2840 | | /* {{{ s_mp_exch(a, b) */ |
2841 | | |
2842 | | /* Exchange the data for a and b; (b, a) = (a, b) */ |
2843 | | void |
2844 | | s_mp_exch(mp_int *a, mp_int *b) |
2845 | 0 | { |
2846 | 0 | mp_int tmp; |
2847 | 0 | if (!a || !b) { |
2848 | 0 | return; |
2849 | 0 | } |
2850 | 0 | |
2851 | 0 | tmp = *a; |
2852 | 0 | *a = *b; |
2853 | 0 | *b = tmp; |
2854 | 0 |
|
2855 | 0 | } /* end s_mp_exch() */ |
2856 | | |
2857 | | /* }}} */ |
2858 | | |
2859 | | /* }}} */ |
2860 | | |
2861 | | /* {{{ Arithmetic helpers */ |
2862 | | |
2863 | | /* {{{ s_mp_lshd(mp, p) */ |
2864 | | |
2865 | | /* |
2866 | | Shift mp leftward by p digits, growing if needed, and zero-filling |
2867 | | the in-shifted digits at the right end. This is a convenient |
2868 | | alternative to multiplication by powers of the radix |
2869 | | */ |
2870 | | |
2871 | | mp_err |
2872 | | s_mp_lshd(mp_int *mp, mp_size p) |
2873 | 0 | { |
2874 | 0 | mp_err res; |
2875 | 0 | unsigned int ix; |
2876 | 0 |
|
2877 | 0 | if (p == 0) |
2878 | 0 | return MP_OKAY; |
2879 | 0 | |
2880 | 0 | if (MP_USED(mp) == 1 && MP_DIGIT(mp, 0) == 0) |
2881 | 0 | return MP_OKAY; |
2882 | 0 | |
2883 | 0 | if ((res = s_mp_pad(mp, USED(mp) + p)) != MP_OKAY) |
2884 | 0 | return res; |
2885 | 0 | |
2886 | 0 | /* Shift all the significant figures over as needed */ |
2887 | 0 | for (ix = USED(mp) - p; ix-- > 0;) { |
2888 | 0 | DIGIT(mp, ix + p) = DIGIT(mp, ix); |
2889 | 0 | } |
2890 | 0 |
|
2891 | 0 | /* Fill the bottom digits with zeroes */ |
2892 | 0 | for (ix = 0; (mp_size)ix < p; ix++) |
2893 | 0 | DIGIT(mp, ix) = 0; |
2894 | 0 |
|
2895 | 0 | return MP_OKAY; |
2896 | 0 |
|
2897 | 0 | } /* end s_mp_lshd() */ |
2898 | | |
2899 | | /* }}} */ |
2900 | | |
2901 | | /* {{{ s_mp_mul_2d(mp, d) */ |
2902 | | |
2903 | | /* |
2904 | | Multiply the integer by 2^d, where d is a number of bits. This |
2905 | | amounts to a bitwise shift of the value. |
2906 | | */ |
2907 | | mp_err |
2908 | | s_mp_mul_2d(mp_int *mp, mp_digit d) |
2909 | 0 | { |
2910 | 0 | mp_err res; |
2911 | 0 | mp_digit dshift, bshift; |
2912 | 0 | mp_digit mask; |
2913 | 0 |
|
2914 | 0 | ARGCHK(mp != NULL, MP_BADARG); |
2915 | 0 |
|
2916 | 0 | dshift = d / MP_DIGIT_BIT; |
2917 | 0 | bshift = d % MP_DIGIT_BIT; |
2918 | 0 | /* bits to be shifted out of the top word */ |
2919 | 0 | if (bshift) { |
2920 | 0 | mask = (mp_digit)~0 << (MP_DIGIT_BIT - bshift); |
2921 | 0 | mask &= MP_DIGIT(mp, MP_USED(mp) - 1); |
2922 | 0 | } else { |
2923 | 0 | mask = 0; |
2924 | 0 | } |
2925 | 0 |
|
2926 | 0 | if (MP_OKAY != (res = s_mp_pad(mp, MP_USED(mp) + dshift + (mask != 0)))) |
2927 | 0 | return res; |
2928 | 0 | |
2929 | 0 | if (dshift && MP_OKAY != (res = s_mp_lshd(mp, dshift))) |
2930 | 0 | return res; |
2931 | 0 | |
2932 | 0 | if (bshift) { |
2933 | 0 | mp_digit *pa = MP_DIGITS(mp); |
2934 | 0 | mp_digit *alim = pa + MP_USED(mp); |
2935 | 0 | mp_digit prev = 0; |
2936 | 0 |
|
2937 | 0 | for (pa += dshift; pa < alim;) { |
2938 | 0 | mp_digit x = *pa; |
2939 | 0 | *pa++ = (x << bshift) | prev; |
2940 | 0 | prev = x >> (DIGIT_BIT - bshift); |
2941 | 0 | } |
2942 | 0 | } |
2943 | 0 |
|
2944 | 0 | s_mp_clamp(mp); |
2945 | 0 | return MP_OKAY; |
2946 | 0 | } /* end s_mp_mul_2d() */ |
2947 | | |
2948 | | /* {{{ s_mp_rshd(mp, p) */ |
2949 | | |
2950 | | /* |
2951 | | Shift mp rightward by p digits. Maintains the invariant that |
2952 | | digits above the precision are all zero. Digits shifted off the |
2953 | | end are lost. Cannot fail. |
2954 | | */ |
2955 | | |
2956 | | void |
2957 | | s_mp_rshd(mp_int *mp, mp_size p) |
2958 | 0 | { |
2959 | 0 | mp_size ix; |
2960 | 0 | mp_digit *src, *dst; |
2961 | 0 |
|
2962 | 0 | if (p == 0) |
2963 | 0 | return; |
2964 | 0 | |
2965 | 0 | /* Shortcut when all digits are to be shifted off */ |
2966 | 0 | if (p >= USED(mp)) { |
2967 | 0 | s_mp_setz(DIGITS(mp), ALLOC(mp)); |
2968 | 0 | USED(mp) = 1; |
2969 | 0 | SIGN(mp) = ZPOS; |
2970 | 0 | return; |
2971 | 0 | } |
2972 | 0 |
|
2973 | 0 | /* Shift all the significant figures over as needed */ |
2974 | 0 | dst = MP_DIGITS(mp); |
2975 | 0 | src = dst + p; |
2976 | 0 | for (ix = USED(mp) - p; ix > 0; ix--) |
2977 | 0 | *dst++ = *src++; |
2978 | 0 |
|
2979 | 0 | MP_USED(mp) -= p; |
2980 | 0 | /* Fill the top digits with zeroes */ |
2981 | 0 | while (p-- > 0) |
2982 | 0 | *dst++ = 0; |
2983 | 0 |
|
2984 | 0 | } /* end s_mp_rshd() */ |
2985 | | |
2986 | | /* }}} */ |
2987 | | |
2988 | | /* {{{ s_mp_div_2(mp) */ |
2989 | | |
2990 | | /* Divide by two -- take advantage of radix properties to do it fast */ |
2991 | | void |
2992 | | s_mp_div_2(mp_int *mp) |
2993 | 0 | { |
2994 | 0 | s_mp_div_2d(mp, 1); |
2995 | 0 |
|
2996 | 0 | } /* end s_mp_div_2() */ |
2997 | | |
2998 | | /* }}} */ |
2999 | | |
3000 | | /* {{{ s_mp_mul_2(mp) */ |
3001 | | |
3002 | | mp_err |
3003 | | s_mp_mul_2(mp_int *mp) |
3004 | 0 | { |
3005 | 0 | mp_digit *pd; |
3006 | 0 | unsigned int ix, used; |
3007 | 0 | mp_digit kin = 0; |
3008 | 0 |
|
3009 | 0 | /* Shift digits leftward by 1 bit */ |
3010 | 0 | used = MP_USED(mp); |
3011 | 0 | pd = MP_DIGITS(mp); |
3012 | 0 | for (ix = 0; ix < used; ix++) { |
3013 | 0 | mp_digit d = *pd; |
3014 | 0 | *pd++ = (d << 1) | kin; |
3015 | 0 | kin = (d >> (DIGIT_BIT - 1)); |
3016 | 0 | } |
3017 | 0 |
|
3018 | 0 | /* Deal with rollover from last digit */ |
3019 | 0 | if (kin) { |
3020 | 0 | if (ix >= ALLOC(mp)) { |
3021 | 0 | mp_err res; |
3022 | 0 | if ((res = s_mp_grow(mp, ALLOC(mp) + 1)) != MP_OKAY) |
3023 | 0 | return res; |
3024 | 0 | } |
3025 | 0 | |
3026 | 0 | DIGIT(mp, ix) = kin; |
3027 | 0 | USED(mp) += 1; |
3028 | 0 | } |
3029 | 0 |
|
3030 | 0 | return MP_OKAY; |
3031 | 0 |
|
3032 | 0 | } /* end s_mp_mul_2() */ |
3033 | | |
3034 | | /* }}} */ |
3035 | | |
3036 | | /* {{{ s_mp_mod_2d(mp, d) */ |
3037 | | |
3038 | | /* |
3039 | | Remainder the integer by 2^d, where d is a number of bits. This |
3040 | | amounts to a bitwise AND of the value, and does not require the full |
3041 | | division code |
3042 | | */ |
3043 | | void |
3044 | | s_mp_mod_2d(mp_int *mp, mp_digit d) |
3045 | 0 | { |
3046 | 0 | mp_size ndig = (d / DIGIT_BIT), nbit = (d % DIGIT_BIT); |
3047 | 0 | mp_size ix; |
3048 | 0 | mp_digit dmask; |
3049 | 0 |
|
3050 | 0 | if (ndig >= USED(mp)) |
3051 | 0 | return; |
3052 | 0 | |
3053 | 0 | /* Flush all the bits above 2^d in its digit */ |
3054 | 0 | dmask = ((mp_digit)1 << nbit) - 1; |
3055 | 0 | DIGIT(mp, ndig) &= dmask; |
3056 | 0 |
|
3057 | 0 | /* Flush all digits above the one with 2^d in it */ |
3058 | 0 | for (ix = ndig + 1; ix < USED(mp); ix++) |
3059 | 0 | DIGIT(mp, ix) = 0; |
3060 | 0 |
|
3061 | 0 | s_mp_clamp(mp); |
3062 | 0 |
|
3063 | 0 | } /* end s_mp_mod_2d() */ |
3064 | | |
3065 | | /* }}} */ |
3066 | | |
3067 | | /* {{{ s_mp_div_2d(mp, d) */ |
3068 | | |
3069 | | /* |
3070 | | Divide the integer by 2^d, where d is a number of bits. This |
3071 | | amounts to a bitwise shift of the value, and does not require the |
3072 | | full division code (used in Barrett reduction, see below) |
3073 | | */ |
3074 | | void |
3075 | | s_mp_div_2d(mp_int *mp, mp_digit d) |
3076 | 0 | { |
3077 | 0 | int ix; |
3078 | 0 | mp_digit save, next, mask; |
3079 | 0 |
|
3080 | 0 | s_mp_rshd(mp, d / DIGIT_BIT); |
3081 | 0 | d %= DIGIT_BIT; |
3082 | 0 | if (d) { |
3083 | 0 | mask = ((mp_digit)1 << d) - 1; |
3084 | 0 | save = 0; |
3085 | 0 | for (ix = USED(mp) - 1; ix >= 0; ix--) { |
3086 | 0 | next = DIGIT(mp, ix) & mask; |
3087 | 0 | DIGIT(mp, ix) = (DIGIT(mp, ix) >> d) | (save << (DIGIT_BIT - d)); |
3088 | 0 | save = next; |
3089 | 0 | } |
3090 | 0 | } |
3091 | 0 | s_mp_clamp(mp); |
3092 | 0 |
|
3093 | 0 | } /* end s_mp_div_2d() */ |
3094 | | |
3095 | | /* }}} */ |
3096 | | |
3097 | | /* {{{ s_mp_norm(a, b, *d) */ |
3098 | | |
3099 | | /* |
3100 | | s_mp_norm(a, b, *d) |
3101 | | |
3102 | | Normalize a and b for division, where b is the divisor. In order |
3103 | | that we might make good guesses for quotient digits, we want the |
3104 | | leading digit of b to be at least half the radix, which we |
3105 | | accomplish by multiplying a and b by a power of 2. The exponent |
3106 | | (shift count) is placed in *pd, so that the remainder can be shifted |
3107 | | back at the end of the division process. |
3108 | | */ |
3109 | | |
3110 | | mp_err |
3111 | | s_mp_norm(mp_int *a, mp_int *b, mp_digit *pd) |
3112 | 0 | { |
3113 | 0 | mp_digit d; |
3114 | 0 | mp_digit mask; |
3115 | 0 | mp_digit b_msd; |
3116 | 0 | mp_err res = MP_OKAY; |
3117 | 0 |
|
3118 | 0 | d = 0; |
3119 | 0 | mask = DIGIT_MAX & ~(DIGIT_MAX >> 1); /* mask is msb of digit */ |
3120 | 0 | b_msd = DIGIT(b, USED(b) - 1); |
3121 | 0 | while (!(b_msd & mask)) { |
3122 | 0 | b_msd <<= 1; |
3123 | 0 | ++d; |
3124 | 0 | } |
3125 | 0 |
|
3126 | 0 | if (d) { |
3127 | 0 | MP_CHECKOK(s_mp_mul_2d(a, d)); |
3128 | 0 | MP_CHECKOK(s_mp_mul_2d(b, d)); |
3129 | 0 | } |
3130 | 0 |
|
3131 | 0 | *pd = d; |
3132 | 0 | CLEANUP: |
3133 | 0 | return res; |
3134 | 0 |
|
3135 | 0 | } /* end s_mp_norm() */ |
3136 | | |
3137 | | /* }}} */ |
3138 | | |
3139 | | /* }}} */ |
3140 | | |
3141 | | /* {{{ Primitive digit arithmetic */ |
3142 | | |
3143 | | /* {{{ s_mp_add_d(mp, d) */ |
3144 | | |
3145 | | /* Add d to |mp| in place */ |
3146 | | mp_err s_mp_add_d(mp_int *mp, mp_digit d) /* unsigned digit addition */ |
3147 | 0 | { |
3148 | | #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD) |
3149 | | mp_word w, k = 0; |
3150 | | mp_size ix = 1; |
3151 | | |
3152 | | w = (mp_word)DIGIT(mp, 0) + d; |
3153 | | DIGIT(mp, 0) = ACCUM(w); |
3154 | | k = CARRYOUT(w); |
3155 | | |
3156 | | while (ix < USED(mp) && k) { |
3157 | | w = (mp_word)DIGIT(mp, ix) + k; |
3158 | | DIGIT(mp, ix) = ACCUM(w); |
3159 | | k = CARRYOUT(w); |
3160 | | ++ix; |
3161 | | } |
3162 | | |
3163 | | if (k != 0) { |
3164 | | mp_err res; |
3165 | | |
3166 | | if ((res = s_mp_pad(mp, USED(mp) + 1)) != MP_OKAY) |
3167 | | return res; |
3168 | | |
3169 | | DIGIT(mp, ix) = (mp_digit)k; |
3170 | | } |
3171 | | |
3172 | | return MP_OKAY; |
3173 | | #else |
3174 | 0 | mp_digit *pmp = MP_DIGITS(mp); |
3175 | 0 | mp_digit sum, mp_i, carry = 0; |
3176 | 0 | mp_err res = MP_OKAY; |
3177 | 0 | int used = (int)MP_USED(mp); |
3178 | 0 |
|
3179 | 0 | mp_i = *pmp; |
3180 | 0 | *pmp++ = sum = d + mp_i; |
3181 | 0 | carry = (sum < d); |
3182 | 0 | while (carry && --used > 0) { |
3183 | 0 | mp_i = *pmp; |
3184 | 0 | *pmp++ = sum = carry + mp_i; |
3185 | 0 | carry = !sum; |
3186 | 0 | } |
3187 | 0 | if (carry && !used) { |
3188 | 0 | /* mp is growing */ |
3189 | 0 | used = MP_USED(mp); |
3190 | 0 | MP_CHECKOK(s_mp_pad(mp, used + 1)); |
3191 | 0 | MP_DIGIT(mp, used) = carry; |
3192 | 0 | } |
3193 | 0 | CLEANUP: |
3194 | 0 | return res; |
3195 | 0 | #endif |
3196 | 0 | } /* end s_mp_add_d() */ |
3197 | | |
3198 | | /* }}} */ |
3199 | | |
3200 | | /* {{{ s_mp_sub_d(mp, d) */ |
3201 | | |
3202 | | /* Subtract d from |mp| in place, assumes |mp| > d */ |
3203 | | mp_err s_mp_sub_d(mp_int *mp, mp_digit d) /* unsigned digit subtract */ |
3204 | 0 | { |
3205 | | #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD) |
3206 | | mp_word w, b = 0; |
3207 | | mp_size ix = 1; |
3208 | | |
3209 | | /* Compute initial subtraction */ |
3210 | | w = (RADIX + (mp_word)DIGIT(mp, 0)) - d; |
3211 | | b = CARRYOUT(w) ? 0 : 1; |
3212 | | DIGIT(mp, 0) = ACCUM(w); |
3213 | | |
3214 | | /* Propagate borrows leftward */ |
3215 | | while (b && ix < USED(mp)) { |
3216 | | w = (RADIX + (mp_word)DIGIT(mp, ix)) - b; |
3217 | | b = CARRYOUT(w) ? 0 : 1; |
3218 | | DIGIT(mp, ix) = ACCUM(w); |
3219 | | ++ix; |
3220 | | } |
3221 | | |
3222 | | /* Remove leading zeroes */ |
3223 | | s_mp_clamp(mp); |
3224 | | |
3225 | | /* If we have a borrow out, it's a violation of the input invariant */ |
3226 | | if (b) |
3227 | | return MP_RANGE; |
3228 | | else |
3229 | | return MP_OKAY; |
3230 | | #else |
3231 | 0 | mp_digit *pmp = MP_DIGITS(mp); |
3232 | 0 | mp_digit mp_i, diff, borrow; |
3233 | 0 | mp_size used = MP_USED(mp); |
3234 | 0 |
|
3235 | 0 | mp_i = *pmp; |
3236 | 0 | *pmp++ = diff = mp_i - d; |
3237 | 0 | borrow = (diff > mp_i); |
3238 | 0 | while (borrow && --used) { |
3239 | 0 | mp_i = *pmp; |
3240 | 0 | *pmp++ = diff = mp_i - borrow; |
3241 | 0 | borrow = (diff > mp_i); |
3242 | 0 | } |
3243 | 0 | s_mp_clamp(mp); |
3244 | 0 | return (borrow && !used) ? MP_RANGE : MP_OKAY; |
3245 | 0 | #endif |
3246 | 0 | } /* end s_mp_sub_d() */ |
3247 | | |
3248 | | /* }}} */ |
3249 | | |
3250 | | /* {{{ s_mp_mul_d(a, d) */ |
3251 | | |
3252 | | /* Compute a = a * d, single digit multiplication */ |
3253 | | mp_err |
3254 | | s_mp_mul_d(mp_int *a, mp_digit d) |
3255 | 0 | { |
3256 | 0 | mp_err res; |
3257 | 0 | mp_size used; |
3258 | 0 | int pow; |
3259 | 0 |
|
3260 | 0 | if (!d) { |
3261 | 0 | mp_zero(a); |
3262 | 0 | return MP_OKAY; |
3263 | 0 | } |
3264 | 0 | if (d == 1) |
3265 | 0 | return MP_OKAY; |
3266 | 0 | if (0 <= (pow = s_mp_ispow2d(d))) { |
3267 | 0 | return s_mp_mul_2d(a, (mp_digit)pow); |
3268 | 0 | } |
3269 | 0 | |
3270 | 0 | used = MP_USED(a); |
3271 | 0 | MP_CHECKOK(s_mp_pad(a, used + 1)); |
3272 | 0 |
|
3273 | 0 | s_mpv_mul_d(MP_DIGITS(a), used, d, MP_DIGITS(a)); |
3274 | 0 |
|
3275 | 0 | s_mp_clamp(a); |
3276 | 0 |
|
3277 | 0 | CLEANUP: |
3278 | 0 | return res; |
3279 | 0 |
|
3280 | 0 | } /* end s_mp_mul_d() */ |
3281 | | |
3282 | | /* }}} */ |
3283 | | |
3284 | | /* {{{ s_mp_div_d(mp, d, r) */ |
3285 | | |
3286 | | /* |
3287 | | s_mp_div_d(mp, d, r) |
3288 | | |
3289 | | Compute the quotient mp = mp / d and remainder r = mp mod d, for a |
3290 | | single digit d. If r is null, the remainder will be discarded. |
3291 | | */ |
3292 | | |
3293 | | mp_err |
3294 | | s_mp_div_d(mp_int *mp, mp_digit d, mp_digit *r) |
3295 | 0 | { |
3296 | | #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_DIV_WORD) |
3297 | | mp_word w = 0, q; |
3298 | | #else |
3299 | | mp_digit w = 0, q; |
3300 | 0 | #endif |
3301 | 0 | int ix; |
3302 | 0 | mp_err res; |
3303 | 0 | mp_int quot; |
3304 | 0 | mp_int rem; |
3305 | 0 |
|
3306 | 0 | if (d == 0) |
3307 | 0 | return MP_RANGE; |
3308 | 0 | if (d == 1) { |
3309 | 0 | if (r) |
3310 | 0 | *r = 0; |
3311 | 0 | return MP_OKAY; |
3312 | 0 | } |
3313 | 0 | /* could check for power of 2 here, but mp_div_d does that. */ |
3314 | 0 | if (MP_USED(mp) == 1) { |
3315 | 0 | mp_digit n = MP_DIGIT(mp, 0); |
3316 | 0 | mp_digit remdig; |
3317 | 0 |
|
3318 | 0 | q = n / d; |
3319 | 0 | remdig = n % d; |
3320 | 0 | MP_DIGIT(mp, 0) = q; |
3321 | 0 | if (r) { |
3322 | 0 | *r = remdig; |
3323 | 0 | } |
3324 | 0 | return MP_OKAY; |
3325 | 0 | } |
3326 | 0 |
|
3327 | 0 | MP_DIGITS(&rem) = 0; |
3328 | 0 | MP_DIGITS(") = 0; |
3329 | 0 | /* Make room for the quotient */ |
3330 | 0 | MP_CHECKOK(mp_init_size(", USED(mp))); |
3331 | 0 |
|
3332 | | #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_DIV_WORD) |
3333 | | for (ix = USED(mp) - 1; ix >= 0; ix--) { |
3334 | | w = (w << DIGIT_BIT) | DIGIT(mp, ix); |
3335 | | |
3336 | | if (w >= d) { |
3337 | | q = w / d; |
3338 | | w = w % d; |
3339 | | } else { |
3340 | | q = 0; |
3341 | | } |
3342 | | |
3343 | | s_mp_lshd(", 1); |
3344 | | DIGIT(", 0) = (mp_digit)q; |
3345 | | } |
3346 | | #else |
3347 | 0 | { |
3348 | 0 | mp_digit p; |
3349 | 0 | #if !defined(MP_ASSEMBLY_DIV_2DX1D) |
3350 | 0 | mp_digit norm; |
3351 | 0 | #endif |
3352 | 0 |
|
3353 | 0 | MP_CHECKOK(mp_init_copy(&rem, mp)); |
3354 | 0 |
|
3355 | 0 | #if !defined(MP_ASSEMBLY_DIV_2DX1D) |
3356 | 0 | MP_DIGIT(", 0) = d; |
3357 | 0 | MP_CHECKOK(s_mp_norm(&rem, ", &norm)); |
3358 | 0 | if (norm) |
3359 | 0 | d <<= norm; |
3360 | 0 | MP_DIGIT(", 0) = 0; |
3361 | 0 | #endif |
3362 | 0 |
|
3363 | 0 | p = 0; |
3364 | 0 | for (ix = USED(&rem) - 1; ix >= 0; ix--) { |
3365 | 0 | w = DIGIT(&rem, ix); |
3366 | 0 |
|
3367 | 0 | if (p) { |
3368 | 0 | MP_CHECKOK(s_mpv_div_2dx1d(p, w, d, &q, &w)); |
3369 | 0 | } else if (w >= d) { |
3370 | 0 | q = w / d; |
3371 | 0 | w = w % d; |
3372 | 0 | } else { |
3373 | 0 | q = 0; |
3374 | 0 | } |
3375 | 0 |
|
3376 | 0 | MP_CHECKOK(s_mp_lshd(", 1)); |
3377 | 0 | DIGIT(", 0) = q; |
3378 | 0 | p = w; |
3379 | 0 | } |
3380 | 0 | #if !defined(MP_ASSEMBLY_DIV_2DX1D) |
3381 | 0 | if (norm) |
3382 | 0 | w >>= norm; |
3383 | 0 | #endif |
3384 | 0 | } |
3385 | 0 | #endif |
3386 | 0 |
|
3387 | 0 | /* Deliver the remainder, if desired */ |
3388 | 0 | if (r) { |
3389 | 0 | *r = (mp_digit)w; |
3390 | 0 | } |
3391 | 0 |
|
3392 | 0 | s_mp_clamp("); |
3393 | 0 | mp_exch(", mp); |
3394 | 0 | CLEANUP: |
3395 | 0 | mp_clear("); |
3396 | 0 | mp_clear(&rem); |
3397 | 0 |
|
3398 | 0 | return res; |
3399 | 0 | } /* end s_mp_div_d() */ |
3400 | | |
3401 | | /* }}} */ |
3402 | | |
3403 | | /* }}} */ |
3404 | | |
3405 | | /* {{{ Primitive full arithmetic */ |
3406 | | |
3407 | | /* {{{ s_mp_add(a, b) */ |
3408 | | |
3409 | | /* Compute a = |a| + |b| */ |
3410 | | mp_err s_mp_add(mp_int *a, const mp_int *b) /* magnitude addition */ |
3411 | 0 | { |
3412 | | #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD) |
3413 | | mp_word w = 0; |
3414 | | #else |
3415 | | mp_digit d, sum, carry = 0; |
3416 | 0 | #endif |
3417 | 0 | mp_digit *pa, *pb; |
3418 | 0 | mp_size ix; |
3419 | 0 | mp_size used; |
3420 | 0 | mp_err res; |
3421 | 0 |
|
3422 | 0 | /* Make sure a has enough precision for the output value */ |
3423 | 0 | if ((USED(b) > USED(a)) && (res = s_mp_pad(a, USED(b))) != MP_OKAY) |
3424 | 0 | return res; |
3425 | 0 | |
3426 | 0 | /* |
3427 | 0 | Add up all digits up to the precision of b. If b had initially |
3428 | 0 | the same precision as a, or greater, we took care of it by the |
3429 | 0 | padding step above, so there is no problem. If b had initially |
3430 | 0 | less precision, we'll have to make sure the carry out is duly |
3431 | 0 | propagated upward among the higher-order digits of the sum. |
3432 | 0 | */ |
3433 | 0 | pa = MP_DIGITS(a); |
3434 | 0 | pb = MP_DIGITS(b); |
3435 | 0 | used = MP_USED(b); |
3436 | 0 | for (ix = 0; ix < used; ix++) { |
3437 | | #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD) |
3438 | | w = w + *pa + *pb++; |
3439 | | *pa++ = ACCUM(w); |
3440 | | w = CARRYOUT(w); |
3441 | | #else |
3442 | | d = *pa; |
3443 | 0 | sum = d + *pb++; |
3444 | 0 | d = (sum < d); /* detect overflow */ |
3445 | 0 | *pa++ = sum += carry; |
3446 | 0 | carry = d + (sum < carry); /* detect overflow */ |
3447 | 0 | #endif |
3448 | 0 | } |
3449 | 0 |
|
3450 | 0 | /* If we run out of 'b' digits before we're actually done, make |
3451 | 0 | sure the carries get propagated upward... |
3452 | 0 | */ |
3453 | 0 | used = MP_USED(a); |
3454 | | #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD) |
3455 | | while (w && ix < used) { |
3456 | | w = w + *pa; |
3457 | | *pa++ = ACCUM(w); |
3458 | | w = CARRYOUT(w); |
3459 | | ++ix; |
3460 | | } |
3461 | | #else |
3462 | 0 | while (carry && ix < used) { |
3463 | 0 | sum = carry + *pa; |
3464 | 0 | *pa++ = sum; |
3465 | 0 | carry = !sum; |
3466 | 0 | ++ix; |
3467 | 0 | } |
3468 | 0 | #endif |
3469 | 0 |
|
3470 | 0 | /* If there's an overall carry out, increase precision and include |
3471 | 0 | it. We could have done this initially, but why touch the memory |
3472 | 0 | allocator unless we're sure we have to? |
3473 | 0 | */ |
3474 | | #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD) |
3475 | | if (w) { |
3476 | | if ((res = s_mp_pad(a, used + 1)) != MP_OKAY) |
3477 | | return res; |
3478 | | |
3479 | | DIGIT(a, ix) = (mp_digit)w; |
3480 | | } |
3481 | | #else |
3482 | 0 | if (carry) { |
3483 | 0 | if ((res = s_mp_pad(a, used + 1)) != MP_OKAY) |
3484 | 0 | return res; |
3485 | 0 | |
3486 | 0 | DIGIT(a, used) = carry; |
3487 | 0 | } |
3488 | 0 | #endif |
3489 | 0 |
|
3490 | 0 | return MP_OKAY; |
3491 | 0 | } /* end s_mp_add() */ |
3492 | | |
3493 | | /* }}} */ |
3494 | | |
3495 | | /* Compute c = |a| + |b| */ /* magnitude addition */ |
3496 | | mp_err |
3497 | | s_mp_add_3arg(const mp_int *a, const mp_int *b, mp_int *c) |
3498 | 0 | { |
3499 | 0 | mp_digit *pa, *pb, *pc; |
3500 | | #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD) |
3501 | | mp_word w = 0; |
3502 | | #else |
3503 | | mp_digit sum, carry = 0, d; |
3504 | 0 | #endif |
3505 | 0 | mp_size ix; |
3506 | 0 | mp_size used; |
3507 | 0 | mp_err res; |
3508 | 0 |
|
3509 | 0 | MP_SIGN(c) = MP_SIGN(a); |
3510 | 0 | if (MP_USED(a) < MP_USED(b)) { |
3511 | 0 | const mp_int *xch = a; |
3512 | 0 | a = b; |
3513 | 0 | b = xch; |
3514 | 0 | } |
3515 | 0 |
|
3516 | 0 | /* Make sure a has enough precision for the output value */ |
3517 | 0 | if (MP_OKAY != (res = s_mp_pad(c, MP_USED(a)))) |
3518 | 0 | return res; |
3519 | 0 | |
3520 | 0 | /* |
3521 | 0 | Add up all digits up to the precision of b. If b had initially |
3522 | 0 | the same precision as a, or greater, we took care of it by the |
3523 | 0 | exchange step above, so there is no problem. If b had initially |
3524 | 0 | less precision, we'll have to make sure the carry out is duly |
3525 | 0 | propagated upward among the higher-order digits of the sum. |
3526 | 0 | */ |
3527 | 0 | pa = MP_DIGITS(a); |
3528 | 0 | pb = MP_DIGITS(b); |
3529 | 0 | pc = MP_DIGITS(c); |
3530 | 0 | used = MP_USED(b); |
3531 | 0 | for (ix = 0; ix < used; ix++) { |
3532 | | #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD) |
3533 | | w = w + *pa++ + *pb++; |
3534 | | *pc++ = ACCUM(w); |
3535 | | w = CARRYOUT(w); |
3536 | | #else |
3537 | | d = *pa++; |
3538 | 0 | sum = d + *pb++; |
3539 | 0 | d = (sum < d); /* detect overflow */ |
3540 | 0 | *pc++ = sum += carry; |
3541 | 0 | carry = d + (sum < carry); /* detect overflow */ |
3542 | 0 | #endif |
3543 | 0 | } |
3544 | 0 |
|
3545 | 0 | /* If we run out of 'b' digits before we're actually done, make |
3546 | 0 | sure the carries get propagated upward... |
3547 | 0 | */ |
3548 | 0 | for (used = MP_USED(a); ix < used; ++ix) { |
3549 | | #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD) |
3550 | | w = w + *pa++; |
3551 | | *pc++ = ACCUM(w); |
3552 | | w = CARRYOUT(w); |
3553 | | #else |
3554 | | *pc++ = sum = carry + *pa++; |
3555 | 0 | carry = (sum < carry); |
3556 | 0 | #endif |
3557 | 0 | } |
3558 | 0 |
|
3559 | 0 | /* If there's an overall carry out, increase precision and include |
3560 | 0 | it. We could have done this initially, but why touch the memory |
3561 | 0 | allocator unless we're sure we have to? |
3562 | 0 | */ |
3563 | | #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD) |
3564 | | if (w) { |
3565 | | if ((res = s_mp_pad(c, used + 1)) != MP_OKAY) |
3566 | | return res; |
3567 | | |
3568 | | DIGIT(c, used) = (mp_digit)w; |
3569 | | ++used; |
3570 | | } |
3571 | | #else |
3572 | 0 | if (carry) { |
3573 | 0 | if ((res = s_mp_pad(c, used + 1)) != MP_OKAY) |
3574 | 0 | return res; |
3575 | 0 | |
3576 | 0 | DIGIT(c, used) = carry; |
3577 | 0 | ++used; |
3578 | 0 | } |
3579 | 0 | #endif |
3580 | 0 | MP_USED(c) = used; |
3581 | 0 | return MP_OKAY; |
3582 | 0 | } |
3583 | | /* {{{ s_mp_add_offset(a, b, offset) */ |
3584 | | |
3585 | | /* Compute a = |a| + ( |b| * (RADIX ** offset) ) */ |
3586 | | mp_err |
3587 | | s_mp_add_offset(mp_int *a, mp_int *b, mp_size offset) |
3588 | 0 | { |
3589 | | #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD) |
3590 | | mp_word w, k = 0; |
3591 | | #else |
3592 | | mp_digit d, sum, carry = 0; |
3593 | 0 | #endif |
3594 | 0 | mp_size ib; |
3595 | 0 | mp_size ia; |
3596 | 0 | mp_size lim; |
3597 | 0 | mp_err res; |
3598 | 0 |
|
3599 | 0 | /* Make sure a has enough precision for the output value */ |
3600 | 0 | lim = MP_USED(b) + offset; |
3601 | 0 | if ((lim > USED(a)) && (res = s_mp_pad(a, lim)) != MP_OKAY) |
3602 | 0 | return res; |
3603 | 0 | |
3604 | 0 | /* |
3605 | 0 | Add up all digits up to the precision of b. If b had initially |
3606 | 0 | the same precision as a, or greater, we took care of it by the |
3607 | 0 | padding step above, so there is no problem. If b had initially |
3608 | 0 | less precision, we'll have to make sure the carry out is duly |
3609 | 0 | propagated upward among the higher-order digits of the sum. |
3610 | 0 | */ |
3611 | 0 | lim = USED(b); |
3612 | 0 | for (ib = 0, ia = offset; ib < lim; ib++, ia++) { |
3613 | | #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD) |
3614 | | w = (mp_word)DIGIT(a, ia) + DIGIT(b, ib) + k; |
3615 | | DIGIT(a, ia) = ACCUM(w); |
3616 | | k = CARRYOUT(w); |
3617 | | #else |
3618 | 0 | d = MP_DIGIT(a, ia); |
3619 | 0 | sum = d + MP_DIGIT(b, ib); |
3620 | 0 | d = (sum < d); |
3621 | 0 | MP_DIGIT(a, ia) = sum += carry; |
3622 | 0 | carry = d + (sum < carry); |
3623 | 0 | #endif |
3624 | 0 | } |
3625 | 0 |
|
3626 | 0 | /* If we run out of 'b' digits before we're actually done, make |
3627 | 0 | sure the carries get propagated upward... |
3628 | 0 | */ |
3629 | | #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD) |
3630 | | for (lim = MP_USED(a); k && (ia < lim); ++ia) { |
3631 | | w = (mp_word)DIGIT(a, ia) + k; |
3632 | | DIGIT(a, ia) = ACCUM(w); |
3633 | | k = CARRYOUT(w); |
3634 | | } |
3635 | | #else |
3636 | 0 | for (lim = MP_USED(a); carry && (ia < lim); ++ia) { |
3637 | 0 | d = MP_DIGIT(a, ia); |
3638 | 0 | MP_DIGIT(a, ia) = sum = d + carry; |
3639 | 0 | carry = (sum < d); |
3640 | 0 | } |
3641 | 0 | #endif |
3642 | 0 |
|
3643 | 0 | /* If there's an overall carry out, increase precision and include |
3644 | 0 | it. We could have done this initially, but why touch the memory |
3645 | 0 | allocator unless we're sure we have to? |
3646 | 0 | */ |
3647 | | #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD) |
3648 | | if (k) { |
3649 | | if ((res = s_mp_pad(a, USED(a) + 1)) != MP_OKAY) |
3650 | | return res; |
3651 | | |
3652 | | DIGIT(a, ia) = (mp_digit)k; |
3653 | | } |
3654 | | #else |
3655 | 0 | if (carry) { |
3656 | 0 | if ((res = s_mp_pad(a, lim + 1)) != MP_OKAY) |
3657 | 0 | return res; |
3658 | 0 | |
3659 | 0 | DIGIT(a, lim) = carry; |
3660 | 0 | } |
3661 | 0 | #endif |
3662 | 0 | s_mp_clamp(a); |
3663 | 0 |
|
3664 | 0 | return MP_OKAY; |
3665 | 0 |
|
3666 | 0 | } /* end s_mp_add_offset() */ |
3667 | | |
3668 | | /* }}} */ |
3669 | | |
3670 | | /* {{{ s_mp_sub(a, b) */ |
3671 | | |
3672 | | /* Compute a = |a| - |b|, assumes |a| >= |b| */ |
3673 | | mp_err s_mp_sub(mp_int *a, const mp_int *b) /* magnitude subtract */ |
3674 | 0 | { |
3675 | 0 | mp_digit *pa, *pb, *limit; |
3676 | | #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD) |
3677 | | mp_sword w = 0; |
3678 | | #else |
3679 | | mp_digit d, diff, borrow = 0; |
3680 | 0 | #endif |
3681 | 0 |
|
3682 | 0 | /* |
3683 | 0 | Subtract and propagate borrow. Up to the precision of b, this |
3684 | 0 | accounts for the digits of b; after that, we just make sure the |
3685 | 0 | carries get to the right place. This saves having to pad b out to |
3686 | 0 | the precision of a just to make the loops work right... |
3687 | 0 | */ |
3688 | 0 | pa = MP_DIGITS(a); |
3689 | 0 | pb = MP_DIGITS(b); |
3690 | 0 | limit = pb + MP_USED(b); |
3691 | 0 | while (pb < limit) { |
3692 | | #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD) |
3693 | | w = w + *pa - *pb++; |
3694 | | *pa++ = ACCUM(w); |
3695 | | w >>= MP_DIGIT_BIT; |
3696 | | #else |
3697 | | d = *pa; |
3698 | 0 | diff = d - *pb++; |
3699 | 0 | d = (diff > d); /* detect borrow */ |
3700 | 0 | if (borrow && --diff == MP_DIGIT_MAX) |
3701 | 0 | ++d; |
3702 | 0 | *pa++ = diff; |
3703 | 0 | borrow = d; |
3704 | 0 | #endif |
3705 | 0 | } |
3706 | 0 | limit = MP_DIGITS(a) + MP_USED(a); |
3707 | | #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD) |
3708 | | while (w && pa < limit) { |
3709 | | w = w + *pa; |
3710 | | *pa++ = ACCUM(w); |
3711 | | w >>= MP_DIGIT_BIT; |
3712 | | } |
3713 | | #else |
3714 | 0 | while (borrow && pa < limit) { |
3715 | 0 | d = *pa; |
3716 | 0 | *pa++ = diff = d - borrow; |
3717 | 0 | borrow = (diff > d); |
3718 | 0 | } |
3719 | 0 | #endif |
3720 | 0 |
|
3721 | 0 | /* Clobber any leading zeroes we created */ |
3722 | 0 | s_mp_clamp(a); |
3723 | 0 |
|
3724 | 0 | /* |
3725 | 0 | If there was a borrow out, then |b| > |a| in violation |
3726 | 0 | of our input invariant. We've already done the work, |
3727 | 0 | but we'll at least complain about it... |
3728 | 0 | */ |
3729 | | #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD) |
3730 | | return w ? MP_RANGE : MP_OKAY; |
3731 | | #else |
3732 | 0 | return borrow ? MP_RANGE : MP_OKAY; |
3733 | 0 | #endif |
3734 | 0 | } /* end s_mp_sub() */ |
3735 | | |
3736 | | /* }}} */ |
3737 | | |
3738 | | /* Compute c = |a| - |b|, assumes |a| >= |b| */ /* magnitude subtract */ |
3739 | | mp_err |
3740 | | s_mp_sub_3arg(const mp_int *a, const mp_int *b, mp_int *c) |
3741 | 0 | { |
3742 | 0 | mp_digit *pa, *pb, *pc; |
3743 | | #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD) |
3744 | | mp_sword w = 0; |
3745 | | #else |
3746 | | mp_digit d, diff, borrow = 0; |
3747 | 0 | #endif |
3748 | 0 | int ix, limit; |
3749 | 0 | mp_err res; |
3750 | 0 |
|
3751 | 0 | MP_SIGN(c) = MP_SIGN(a); |
3752 | 0 |
|
3753 | 0 | /* Make sure a has enough precision for the output value */ |
3754 | 0 | if (MP_OKAY != (res = s_mp_pad(c, MP_USED(a)))) |
3755 | 0 | return res; |
3756 | 0 | |
3757 | 0 | /* |
3758 | 0 | Subtract and propagate borrow. Up to the precision of b, this |
3759 | 0 | accounts for the digits of b; after that, we just make sure the |
3760 | 0 | carries get to the right place. This saves having to pad b out to |
3761 | 0 | the precision of a just to make the loops work right... |
3762 | 0 | */ |
3763 | 0 | pa = MP_DIGITS(a); |
3764 | 0 | pb = MP_DIGITS(b); |
3765 | 0 | pc = MP_DIGITS(c); |
3766 | 0 | limit = MP_USED(b); |
3767 | 0 | for (ix = 0; ix < limit; ++ix) { |
3768 | | #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD) |
3769 | | w = w + *pa++ - *pb++; |
3770 | | *pc++ = ACCUM(w); |
3771 | | w >>= MP_DIGIT_BIT; |
3772 | | #else |
3773 | | d = *pa++; |
3774 | 0 | diff = d - *pb++; |
3775 | 0 | d = (diff > d); |
3776 | 0 | if (borrow && --diff == MP_DIGIT_MAX) |
3777 | 0 | ++d; |
3778 | 0 | *pc++ = diff; |
3779 | 0 | borrow = d; |
3780 | 0 | #endif |
3781 | 0 | } |
3782 | 0 | for (limit = MP_USED(a); ix < limit; ++ix) { |
3783 | | #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD) |
3784 | | w = w + *pa++; |
3785 | | *pc++ = ACCUM(w); |
3786 | | w >>= MP_DIGIT_BIT; |
3787 | | #else |
3788 | | d = *pa++; |
3789 | 0 | *pc++ = diff = d - borrow; |
3790 | 0 | borrow = (diff > d); |
3791 | 0 | #endif |
3792 | 0 | } |
3793 | 0 |
|
3794 | 0 | /* Clobber any leading zeroes we created */ |
3795 | 0 | MP_USED(c) = ix; |
3796 | 0 | s_mp_clamp(c); |
3797 | 0 |
|
3798 | 0 | /* |
3799 | 0 | If there was a borrow out, then |b| > |a| in violation |
3800 | 0 | of our input invariant. We've already done the work, |
3801 | 0 | but we'll at least complain about it... |
3802 | 0 | */ |
3803 | | #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD) |
3804 | | return w ? MP_RANGE : MP_OKAY; |
3805 | | #else |
3806 | 0 | return borrow ? MP_RANGE : MP_OKAY; |
3807 | 0 | #endif |
3808 | 0 | } |
3809 | | /* {{{ s_mp_mul(a, b) */ |
3810 | | |
3811 | | /* Compute a = |a| * |b| */ |
3812 | | mp_err |
3813 | | s_mp_mul(mp_int *a, const mp_int *b) |
3814 | 0 | { |
3815 | 0 | return mp_mul(a, b, a); |
3816 | 0 | } /* end s_mp_mul() */ |
3817 | | |
3818 | | /* }}} */ |
3819 | | |
3820 | | #if defined(MP_USE_UINT_DIGIT) && defined(MP_USE_LONG_LONG_MULTIPLY) |
3821 | | /* This trick works on Sparc V8 CPUs with the Workshop compilers. */ |
3822 | | #define MP_MUL_DxD(a, b, Phi, Plo) \ |
3823 | | { \ |
3824 | | unsigned long long product = (unsigned long long)a * b; \ |
3825 | | Plo = (mp_digit)product; \ |
3826 | | Phi = (mp_digit)(product >> MP_DIGIT_BIT); \ |
3827 | | } |
3828 | | #elif defined(OSF1) |
3829 | | #define MP_MUL_DxD(a, b, Phi, Plo) \ |
3830 | | { \ |
3831 | | Plo = asm("mulq %a0, %a1, %v0", a, b); \ |
3832 | | Phi = asm("umulh %a0, %a1, %v0", a, b); \ |
3833 | | } |
3834 | | #else |
3835 | | #define MP_MUL_DxD(a, b, Phi, Plo) \ |
3836 | 0 | { \ |
3837 | 0 | mp_digit a0b1, a1b0; \ |
3838 | 0 | Plo = (a & MP_HALF_DIGIT_MAX) * (b & MP_HALF_DIGIT_MAX); \ |
3839 | 0 | Phi = (a >> MP_HALF_DIGIT_BIT) * (b >> MP_HALF_DIGIT_BIT); \ |
3840 | 0 | a0b1 = (a & MP_HALF_DIGIT_MAX) * (b >> MP_HALF_DIGIT_BIT); \ |
3841 | 0 | a1b0 = (a >> MP_HALF_DIGIT_BIT) * (b & MP_HALF_DIGIT_MAX); \ |
3842 | 0 | a1b0 += a0b1; \ |
3843 | 0 | Phi += a1b0 >> MP_HALF_DIGIT_BIT; \ |
3844 | 0 | if (a1b0 < a0b1) \ |
3845 | 0 | Phi += MP_HALF_RADIX; \ |
3846 | 0 | a1b0 <<= MP_HALF_DIGIT_BIT; \ |
3847 | 0 | Plo += a1b0; \ |
3848 | 0 | if (Plo < a1b0) \ |
3849 | 0 | ++Phi; \ |
3850 | 0 | } |
3851 | | #endif |
3852 | | |
3853 | | #if !defined(MP_ASSEMBLY_MULTIPLY) |
3854 | | /* c = a * b */ |
3855 | | void |
3856 | | s_mpv_mul_d(const mp_digit *a, mp_size a_len, mp_digit b, mp_digit *c) |
3857 | 0 | { |
3858 | | #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_MUL_WORD) |
3859 | | mp_digit d = 0; |
3860 | | |
3861 | | /* Inner product: Digits of a */ |
3862 | | while (a_len--) { |
3863 | | mp_word w = ((mp_word)b * *a++) + d; |
3864 | | *c++ = ACCUM(w); |
3865 | | d = CARRYOUT(w); |
3866 | | } |
3867 | | *c = d; |
3868 | | #else |
3869 | | mp_digit carry = 0; |
3870 | 0 | while (a_len--) { |
3871 | 0 | mp_digit a_i = *a++; |
3872 | 0 | mp_digit a0b0, a1b1; |
3873 | 0 |
|
3874 | 0 | MP_MUL_DxD(a_i, b, a1b1, a0b0); |
3875 | 0 |
|
3876 | 0 | a0b0 += carry; |
3877 | 0 | if (a0b0 < carry) |
3878 | 0 | ++a1b1; |
3879 | 0 | *c++ = a0b0; |
3880 | 0 | carry = a1b1; |
3881 | 0 | } |
3882 | 0 | *c = carry; |
3883 | 0 | #endif |
3884 | 0 | } |
3885 | | |
3886 | | /* c += a * b */ |
3887 | | void |
3888 | | s_mpv_mul_d_add(const mp_digit *a, mp_size a_len, mp_digit b, |
3889 | | mp_digit *c) |
3890 | 0 | { |
3891 | | #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_MUL_WORD) |
3892 | | mp_digit d = 0; |
3893 | | |
3894 | | /* Inner product: Digits of a */ |
3895 | | while (a_len--) { |
3896 | | mp_word w = ((mp_word)b * *a++) + *c + d; |
3897 | | *c++ = ACCUM(w); |
3898 | | d = CARRYOUT(w); |
3899 | | } |
3900 | | *c = d; |
3901 | | #else |
3902 | | mp_digit carry = 0; |
3903 | 0 | while (a_len--) { |
3904 | 0 | mp_digit a_i = *a++; |
3905 | 0 | mp_digit a0b0, a1b1; |
3906 | 0 |
|
3907 | 0 | MP_MUL_DxD(a_i, b, a1b1, a0b0); |
3908 | 0 |
|
3909 | 0 | a0b0 += carry; |
3910 | 0 | if (a0b0 < carry) |
3911 | 0 | ++a1b1; |
3912 | 0 | a0b0 += a_i = *c; |
3913 | 0 | if (a0b0 < a_i) |
3914 | 0 | ++a1b1; |
3915 | 0 | *c++ = a0b0; |
3916 | 0 | carry = a1b1; |
3917 | 0 | } |
3918 | 0 | *c = carry; |
3919 | 0 | #endif |
3920 | 0 | } |
3921 | | |
3922 | | /* Presently, this is only used by the Montgomery arithmetic code. */ |
3923 | | /* c += a * b */ |
3924 | | void |
3925 | | s_mpv_mul_d_add_prop(const mp_digit *a, mp_size a_len, mp_digit b, mp_digit *c) |
3926 | 0 | { |
3927 | | #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_MUL_WORD) |
3928 | | mp_digit d = 0; |
3929 | | |
3930 | | /* Inner product: Digits of a */ |
3931 | | while (a_len--) { |
3932 | | mp_word w = ((mp_word)b * *a++) + *c + d; |
3933 | | *c++ = ACCUM(w); |
3934 | | d = CARRYOUT(w); |
3935 | | } |
3936 | | |
3937 | | while (d) { |
3938 | | mp_word w = (mp_word)*c + d; |
3939 | | *c++ = ACCUM(w); |
3940 | | d = CARRYOUT(w); |
3941 | | } |
3942 | | #else |
3943 | | mp_digit carry = 0; |
3944 | 0 | while (a_len--) { |
3945 | 0 | mp_digit a_i = *a++; |
3946 | 0 | mp_digit a0b0, a1b1; |
3947 | 0 |
|
3948 | 0 | MP_MUL_DxD(a_i, b, a1b1, a0b0); |
3949 | 0 |
|
3950 | 0 | a0b0 += carry; |
3951 | 0 | if (a0b0 < carry) |
3952 | 0 | ++a1b1; |
3953 | 0 |
|
3954 | 0 | a0b0 += a_i = *c; |
3955 | 0 | if (a0b0 < a_i) |
3956 | 0 | ++a1b1; |
3957 | 0 |
|
3958 | 0 | *c++ = a0b0; |
3959 | 0 | carry = a1b1; |
3960 | 0 | } |
3961 | 0 | while (carry) { |
3962 | 0 | mp_digit c_i = *c; |
3963 | 0 | carry += c_i; |
3964 | 0 | *c++ = carry; |
3965 | 0 | carry = carry < c_i; |
3966 | 0 | } |
3967 | 0 | #endif |
3968 | 0 | } |
3969 | | #endif |
3970 | | |
3971 | | #if defined(MP_USE_UINT_DIGIT) && defined(MP_USE_LONG_LONG_MULTIPLY) |
3972 | | /* This trick works on Sparc V8 CPUs with the Workshop compilers. */ |
3973 | | #define MP_SQR_D(a, Phi, Plo) \ |
3974 | | { \ |
3975 | | unsigned long long square = (unsigned long long)a * a; \ |
3976 | | Plo = (mp_digit)square; \ |
3977 | | Phi = (mp_digit)(square >> MP_DIGIT_BIT); \ |
3978 | | } |
3979 | | #elif defined(OSF1) |
3980 | | #define MP_SQR_D(a, Phi, Plo) \ |
3981 | | { \ |
3982 | | Plo = asm("mulq %a0, %a0, %v0", a); \ |
3983 | | Phi = asm("umulh %a0, %a0, %v0", a); \ |
3984 | | } |
3985 | | #else |
3986 | | #define MP_SQR_D(a, Phi, Plo) \ |
3987 | 0 | { \ |
3988 | 0 | mp_digit Pmid; \ |
3989 | 0 | Plo = (a & MP_HALF_DIGIT_MAX) * (a & MP_HALF_DIGIT_MAX); \ |
3990 | 0 | Phi = (a >> MP_HALF_DIGIT_BIT) * (a >> MP_HALF_DIGIT_BIT); \ |
3991 | 0 | Pmid = (a & MP_HALF_DIGIT_MAX) * (a >> MP_HALF_DIGIT_BIT); \ |
3992 | 0 | Phi += Pmid >> (MP_HALF_DIGIT_BIT - 1); \ |
3993 | 0 | Pmid <<= (MP_HALF_DIGIT_BIT + 1); \ |
3994 | 0 | Plo += Pmid; \ |
3995 | 0 | if (Plo < Pmid) \ |
3996 | 0 | ++Phi; \ |
3997 | 0 | } |
3998 | | #endif |
3999 | | |
4000 | | #if !defined(MP_ASSEMBLY_SQUARE) |
4001 | | /* Add the squares of the digits of a to the digits of b. */ |
4002 | | void |
4003 | | s_mpv_sqr_add_prop(const mp_digit *pa, mp_size a_len, mp_digit *ps) |
4004 | 0 | { |
4005 | | #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_MUL_WORD) |
4006 | | mp_word w; |
4007 | | mp_digit d; |
4008 | | mp_size ix; |
4009 | | |
4010 | | w = 0; |
4011 | | #define ADD_SQUARE(n) \ |
4012 | | d = pa[n]; \ |
4013 | | w += (d * (mp_word)d) + ps[2 * n]; \ |
4014 | | ps[2 * n] = ACCUM(w); \ |
4015 | | w = (w >> DIGIT_BIT) + ps[2 * n + 1]; \ |
4016 | | ps[2 * n + 1] = ACCUM(w); \ |
4017 | | w = (w >> DIGIT_BIT) |
4018 | | |
4019 | | for (ix = a_len; ix >= 4; ix -= 4) { |
4020 | | ADD_SQUARE(0); |
4021 | | ADD_SQUARE(1); |
4022 | | ADD_SQUARE(2); |
4023 | | ADD_SQUARE(3); |
4024 | | pa += 4; |
4025 | | ps += 8; |
4026 | | } |
4027 | | if (ix) { |
4028 | | ps += 2 * ix; |
4029 | | pa += ix; |
4030 | | switch (ix) { |
4031 | | case 3: |
4032 | | ADD_SQUARE(-3); /* FALLTHRU */ |
4033 | | case 2: |
4034 | | ADD_SQUARE(-2); /* FALLTHRU */ |
4035 | | case 1: |
4036 | | ADD_SQUARE(-1); /* FALLTHRU */ |
4037 | | case 0: |
4038 | | break; |
4039 | | } |
4040 | | } |
4041 | | while (w) { |
4042 | | w += *ps; |
4043 | | *ps++ = ACCUM(w); |
4044 | | w = (w >> DIGIT_BIT); |
4045 | | } |
4046 | | #else |
4047 | | mp_digit carry = 0; |
4048 | 0 | while (a_len--) { |
4049 | 0 | mp_digit a_i = *pa++; |
4050 | 0 | mp_digit a0a0, a1a1; |
4051 | 0 |
|
4052 | 0 | MP_SQR_D(a_i, a1a1, a0a0); |
4053 | 0 |
|
4054 | 0 | /* here a1a1 and a0a0 constitute a_i ** 2 */ |
4055 | 0 | a0a0 += carry; |
4056 | 0 | if (a0a0 < carry) |
4057 | 0 | ++a1a1; |
4058 | 0 |
|
4059 | 0 | /* now add to ps */ |
4060 | 0 | a0a0 += a_i = *ps; |
4061 | 0 | if (a0a0 < a_i) |
4062 | 0 | ++a1a1; |
4063 | 0 | *ps++ = a0a0; |
4064 | 0 | a1a1 += a_i = *ps; |
4065 | 0 | carry = (a1a1 < a_i); |
4066 | 0 | *ps++ = a1a1; |
4067 | 0 | } |
4068 | 0 | while (carry) { |
4069 | 0 | mp_digit s_i = *ps; |
4070 | 0 | carry += s_i; |
4071 | 0 | *ps++ = carry; |
4072 | 0 | carry = carry < s_i; |
4073 | 0 | } |
4074 | 0 | #endif |
4075 | 0 | } |
4076 | | #endif |
4077 | | |
4078 | | #if !defined(MP_ASSEMBLY_DIV_2DX1D) |
4079 | | /* |
4080 | | ** Divide 64-bit (Nhi,Nlo) by 32-bit divisor, which must be normalized |
4081 | | ** so its high bit is 1. This code is from NSPR. |
4082 | | */ |
4083 | | mp_err |
4084 | | s_mpv_div_2dx1d(mp_digit Nhi, mp_digit Nlo, mp_digit divisor, |
4085 | | mp_digit *qp, mp_digit *rp) |
4086 | 0 | { |
4087 | 0 | mp_digit d1, d0, q1, q0; |
4088 | 0 | mp_digit r1, r0, m; |
4089 | 0 |
|
4090 | 0 | d1 = divisor >> MP_HALF_DIGIT_BIT; |
4091 | 0 | d0 = divisor & MP_HALF_DIGIT_MAX; |
4092 | 0 | r1 = Nhi % d1; |
4093 | 0 | q1 = Nhi / d1; |
4094 | 0 | m = q1 * d0; |
4095 | 0 | r1 = (r1 << MP_HALF_DIGIT_BIT) | (Nlo >> MP_HALF_DIGIT_BIT); |
4096 | 0 | if (r1 < m) { |
4097 | 0 | q1--, r1 += divisor; |
4098 | 0 | if (r1 >= divisor && r1 < m) { |
4099 | 0 | q1--, r1 += divisor; |
4100 | 0 | } |
4101 | 0 | } |
4102 | 0 | r1 -= m; |
4103 | 0 | r0 = r1 % d1; |
4104 | 0 | q0 = r1 / d1; |
4105 | 0 | m = q0 * d0; |
4106 | 0 | r0 = (r0 << MP_HALF_DIGIT_BIT) | (Nlo & MP_HALF_DIGIT_MAX); |
4107 | 0 | if (r0 < m) { |
4108 | 0 | q0--, r0 += divisor; |
4109 | 0 | if (r0 >= divisor && r0 < m) { |
4110 | 0 | q0--, r0 += divisor; |
4111 | 0 | } |
4112 | 0 | } |
4113 | 0 | if (qp) |
4114 | 0 | *qp = (q1 << MP_HALF_DIGIT_BIT) | q0; |
4115 | 0 | if (rp) |
4116 | 0 | *rp = r0 - m; |
4117 | 0 | return MP_OKAY; |
4118 | 0 | } |
4119 | | #endif |
4120 | | |
4121 | | #if MP_SQUARE |
4122 | | /* {{{ s_mp_sqr(a) */ |
4123 | | |
4124 | | mp_err |
4125 | | s_mp_sqr(mp_int *a) |
4126 | 0 | { |
4127 | 0 | mp_err res; |
4128 | 0 | mp_int tmp; |
4129 | 0 |
|
4130 | 0 | if ((res = mp_init_size(&tmp, 2 * USED(a))) != MP_OKAY) |
4131 | 0 | return res; |
4132 | 0 | res = mp_sqr(a, &tmp); |
4133 | 0 | if (res == MP_OKAY) { |
4134 | 0 | s_mp_exch(&tmp, a); |
4135 | 0 | } |
4136 | 0 | mp_clear(&tmp); |
4137 | 0 | return res; |
4138 | 0 | } |
4139 | | |
4140 | | /* }}} */ |
4141 | | #endif |
4142 | | |
4143 | | /* {{{ s_mp_div(a, b) */ |
4144 | | |
4145 | | /* |
4146 | | s_mp_div(a, b) |
4147 | | |
4148 | | Compute a = a / b and b = a mod b. Assumes b > a. |
4149 | | */ |
4150 | | |
4151 | | mp_err s_mp_div(mp_int *rem, /* i: dividend, o: remainder */ |
4152 | | mp_int *div, /* i: divisor */ |
4153 | | mp_int *quot) /* i: 0; o: quotient */ |
4154 | 0 | { |
4155 | 0 | mp_int part, t; |
4156 | 0 | mp_digit q_msd; |
4157 | 0 | mp_err res; |
4158 | 0 | mp_digit d; |
4159 | 0 | mp_digit div_msd; |
4160 | 0 | int ix; |
4161 | 0 |
|
4162 | 0 | if (mp_cmp_z(div) == 0) |
4163 | 0 | return MP_RANGE; |
4164 | 0 | |
4165 | 0 | DIGITS(&t) = 0; |
4166 | 0 | /* Shortcut if divisor is power of two */ |
4167 | 0 | if ((ix = s_mp_ispow2(div)) >= 0) { |
4168 | 0 | MP_CHECKOK(mp_copy(rem, quot)); |
4169 | 0 | s_mp_div_2d(quot, (mp_digit)ix); |
4170 | 0 | s_mp_mod_2d(rem, (mp_digit)ix); |
4171 | 0 |
|
4172 | 0 | return MP_OKAY; |
4173 | 0 | } |
4174 | 0 | |
4175 | 0 | MP_SIGN(rem) = ZPOS; |
4176 | 0 | MP_SIGN(div) = ZPOS; |
4177 | 0 | MP_SIGN(&part) = ZPOS; |
4178 | 0 |
|
4179 | 0 | /* A working temporary for division */ |
4180 | 0 | MP_CHECKOK(mp_init_size(&t, MP_ALLOC(rem))); |
4181 | 0 |
|
4182 | 0 | /* Normalize to optimize guessing */ |
4183 | 0 | MP_CHECKOK(s_mp_norm(rem, div, &d)); |
4184 | 0 |
|
4185 | 0 | /* Perform the division itself...woo! */ |
4186 | 0 | MP_USED(quot) = MP_ALLOC(quot); |
4187 | 0 |
|
4188 | 0 | /* Find a partial substring of rem which is at least div */ |
4189 | 0 | /* If we didn't find one, we're finished dividing */ |
4190 | 0 | while (MP_USED(rem) > MP_USED(div) || s_mp_cmp(rem, div) >= 0) { |
4191 | 0 | int i; |
4192 | 0 | int unusedRem; |
4193 | 0 | int partExtended = 0; /* set to true if we need to extend part */ |
4194 | 0 |
|
4195 | 0 | unusedRem = MP_USED(rem) - MP_USED(div); |
4196 | 0 | MP_DIGITS(&part) = MP_DIGITS(rem) + unusedRem; |
4197 | 0 | MP_ALLOC(&part) = MP_ALLOC(rem) - unusedRem; |
4198 | 0 | MP_USED(&part) = MP_USED(div); |
4199 | 0 |
|
4200 | 0 | /* We have now truncated the part of the remainder to the same length as |
4201 | 0 | * the divisor. If part is smaller than div, extend part by one digit. */ |
4202 | 0 | if (s_mp_cmp(&part, div) < 0) { |
4203 | 0 | --unusedRem; |
4204 | | #if MP_ARGCHK == 2 |
4205 | | assert(unusedRem >= 0); |
4206 | | #endif |
4207 | 0 | --MP_DIGITS(&part); |
4208 | 0 | ++MP_USED(&part); |
4209 | 0 | ++MP_ALLOC(&part); |
4210 | 0 | partExtended = 1; |
4211 | 0 | } |
4212 | 0 |
|
4213 | 0 | /* Compute a guess for the next quotient digit */ |
4214 | 0 | q_msd = MP_DIGIT(&part, MP_USED(&part) - 1); |
4215 | 0 | div_msd = MP_DIGIT(div, MP_USED(div) - 1); |
4216 | 0 | if (!partExtended) { |
4217 | 0 | /* In this case, q_msd /= div_msd is always 1. First, since div_msd is |
4218 | 0 | * normalized to have the high bit set, 2*div_msd > MP_DIGIT_MAX. Since |
4219 | 0 | * we didn't extend part, q_msd >= div_msd. Therefore we know that |
4220 | 0 | * div_msd <= q_msd <= MP_DIGIT_MAX < 2*div_msd. Dividing by div_msd we |
4221 | 0 | * get 1 <= q_msd/div_msd < 2. So q_msd /= div_msd must be 1. */ |
4222 | 0 | q_msd = 1; |
4223 | 0 | } else { |
4224 | 0 | if (q_msd == div_msd) { |
4225 | 0 | q_msd = MP_DIGIT_MAX; |
4226 | 0 | } else { |
4227 | 0 | mp_digit r; |
4228 | 0 | MP_CHECKOK(s_mpv_div_2dx1d(q_msd, MP_DIGIT(&part, MP_USED(&part) - 2), |
4229 | 0 | div_msd, &q_msd, &r)); |
4230 | 0 | } |
4231 | 0 | } |
4232 | | #if MP_ARGCHK == 2 |
4233 | | assert(q_msd > 0); /* This case should never occur any more. */ |
4234 | | #endif |
4235 | 0 | if (q_msd <= 0) |
4236 | 0 | break; |
4237 | 0 | |
4238 | 0 | /* See what that multiplies out to */ |
4239 | 0 | mp_copy(div, &t); |
4240 | 0 | MP_CHECKOK(s_mp_mul_d(&t, q_msd)); |
4241 | 0 |
|
4242 | 0 | /* |
4243 | 0 | If it's too big, back it off. We should not have to do this |
4244 | 0 | more than once, or, in rare cases, twice. Knuth describes a |
4245 | 0 | method by which this could be reduced to a maximum of once, but |
4246 | 0 | I didn't implement that here. |
4247 | 0 | When using s_mpv_div_2dx1d, we may have to do this 3 times. |
4248 | 0 | */ |
4249 | 0 | for (i = 4; s_mp_cmp(&t, &part) > 0 && i > 0; --i) { |
4250 | 0 | --q_msd; |
4251 | 0 | MP_CHECKOK(s_mp_sub(&t, div)); /* t -= div */ |
4252 | 0 | } |
4253 | 0 | if (i < 0) { |
4254 | 0 | res = MP_RANGE; |
4255 | 0 | goto CLEANUP; |
4256 | 0 | } |
4257 | 0 |
|
4258 | 0 | /* At this point, q_msd should be the right next digit */ |
4259 | 0 | MP_CHECKOK(s_mp_sub(&part, &t)); /* part -= t */ |
4260 | 0 | s_mp_clamp(rem); |
4261 | 0 |
|
4262 | 0 | /* |
4263 | 0 | Include the digit in the quotient. We allocated enough memory |
4264 | 0 | for any quotient we could ever possibly get, so we should not |
4265 | 0 | have to check for failures here |
4266 | 0 | */ |
4267 | 0 | MP_DIGIT(quot, unusedRem) = q_msd; |
4268 | 0 | } |
4269 | 0 |
|
4270 | 0 | /* Denormalize remainder */ |
4271 | 0 | if (d) { |
4272 | 0 | s_mp_div_2d(rem, d); |
4273 | 0 | } |
4274 | 0 |
|
4275 | 0 | s_mp_clamp(quot); |
4276 | 0 |
|
4277 | 0 | CLEANUP: |
4278 | 0 | mp_clear(&t); |
4279 | 0 |
|
4280 | 0 | return res; |
4281 | 0 |
|
4282 | 0 | } /* end s_mp_div() */ |
4283 | | |
4284 | | /* }}} */ |
4285 | | |
4286 | | /* {{{ s_mp_2expt(a, k) */ |
4287 | | |
4288 | | mp_err |
4289 | | s_mp_2expt(mp_int *a, mp_digit k) |
4290 | 0 | { |
4291 | 0 | mp_err res; |
4292 | 0 | mp_size dig, bit; |
4293 | 0 |
|
4294 | 0 | dig = k / DIGIT_BIT; |
4295 | 0 | bit = k % DIGIT_BIT; |
4296 | 0 |
|
4297 | 0 | mp_zero(a); |
4298 | 0 | if ((res = s_mp_pad(a, dig + 1)) != MP_OKAY) |
4299 | 0 | return res; |
4300 | 0 | |
4301 | 0 | DIGIT(a, dig) |= ((mp_digit)1 << bit); |
4302 | 0 |
|
4303 | 0 | return MP_OKAY; |
4304 | 0 |
|
4305 | 0 | } /* end s_mp_2expt() */ |
4306 | | |
4307 | | /* }}} */ |
4308 | | |
4309 | | /* {{{ s_mp_reduce(x, m, mu) */ |
4310 | | |
4311 | | /* |
4312 | | Compute Barrett reduction, x (mod m), given a precomputed value for |
4313 | | mu = b^2k / m, where b = RADIX and k = #digits(m). This should be |
4314 | | faster than straight division, when many reductions by the same |
4315 | | value of m are required (such as in modular exponentiation). This |
4316 | | can nearly halve the time required to do modular exponentiation, |
4317 | | as compared to using the full integer divide to reduce. |
4318 | | |
4319 | | This algorithm was derived from the _Handbook of Applied |
4320 | | Cryptography_ by Menezes, Oorschot and VanStone, Ch. 14, |
4321 | | pp. 603-604. |
4322 | | */ |
4323 | | |
4324 | | mp_err |
4325 | | s_mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu) |
4326 | 0 | { |
4327 | 0 | mp_int q; |
4328 | 0 | mp_err res; |
4329 | 0 |
|
4330 | 0 | if ((res = mp_init_copy(&q, x)) != MP_OKAY) |
4331 | 0 | return res; |
4332 | 0 | |
4333 | 0 | s_mp_rshd(&q, USED(m) - 1); /* q1 = x / b^(k-1) */ |
4334 | 0 | s_mp_mul(&q, mu); /* q2 = q1 * mu */ |
4335 | 0 | s_mp_rshd(&q, USED(m) + 1); /* q3 = q2 / b^(k+1) */ |
4336 | 0 |
|
4337 | 0 | /* x = x mod b^(k+1), quick (no division) */ |
4338 | 0 | s_mp_mod_2d(x, DIGIT_BIT * (USED(m) + 1)); |
4339 | 0 |
|
4340 | 0 | /* q = q * m mod b^(k+1), quick (no division) */ |
4341 | 0 | s_mp_mul(&q, m); |
4342 | 0 | s_mp_mod_2d(&q, DIGIT_BIT * (USED(m) + 1)); |
4343 | 0 |
|
4344 | 0 | /* x = x - q */ |
4345 | 0 | if ((res = mp_sub(x, &q, x)) != MP_OKAY) |
4346 | 0 | goto CLEANUP; |
4347 | 0 | |
4348 | 0 | /* If x < 0, add b^(k+1) to it */ |
4349 | 0 | if (mp_cmp_z(x) < 0) { |
4350 | 0 | mp_set(&q, 1); |
4351 | 0 | if ((res = s_mp_lshd(&q, USED(m) + 1)) != MP_OKAY) |
4352 | 0 | goto CLEANUP; |
4353 | 0 | if ((res = mp_add(x, &q, x)) != MP_OKAY) |
4354 | 0 | goto CLEANUP; |
4355 | 0 | } |
4356 | 0 | |
4357 | 0 | /* Back off if it's too big */ |
4358 | 0 | while (mp_cmp(x, m) >= 0) { |
4359 | 0 | if ((res = s_mp_sub(x, m)) != MP_OKAY) |
4360 | 0 | break; |
4361 | 0 | } |
4362 | 0 |
|
4363 | 0 | CLEANUP: |
4364 | 0 | mp_clear(&q); |
4365 | 0 |
|
4366 | 0 | return res; |
4367 | 0 |
|
4368 | 0 | } /* end s_mp_reduce() */ |
4369 | | |
4370 | | /* }}} */ |
4371 | | |
4372 | | /* }}} */ |
4373 | | |
4374 | | /* {{{ Primitive comparisons */ |
4375 | | |
4376 | | /* {{{ s_mp_cmp(a, b) */ |
4377 | | |
4378 | | /* Compare |a| <=> |b|, return 0 if equal, <0 if a<b, >0 if a>b */ |
4379 | | int |
4380 | | s_mp_cmp(const mp_int *a, const mp_int *b) |
4381 | 0 | { |
4382 | 0 | mp_size used_a = MP_USED(a); |
4383 | 0 | { |
4384 | 0 | mp_size used_b = MP_USED(b); |
4385 | 0 |
|
4386 | 0 | if (used_a > used_b) |
4387 | 0 | goto IS_GT; |
4388 | 0 | if (used_a < used_b) |
4389 | 0 | goto IS_LT; |
4390 | 0 | } |
4391 | 0 | { |
4392 | 0 | mp_digit *pa, *pb; |
4393 | 0 | mp_digit da = 0, db = 0; |
4394 | 0 |
|
4395 | 0 | #define CMP_AB(n) \ |
4396 | 0 | if ((da = pa[n]) != (db = pb[n])) \ |
4397 | 0 | goto done |
4398 | 0 |
|
4399 | 0 | pa = MP_DIGITS(a) + used_a; |
4400 | 0 | pb = MP_DIGITS(b) + used_a; |
4401 | 0 | while (used_a >= 4) { |
4402 | 0 | pa -= 4; |
4403 | 0 | pb -= 4; |
4404 | 0 | used_a -= 4; |
4405 | 0 | CMP_AB(3); |
4406 | 0 | CMP_AB(2); |
4407 | 0 | CMP_AB(1); |
4408 | 0 | CMP_AB(0); |
4409 | 0 | } |
4410 | 0 | while (used_a-- > 0 && ((da = *--pa) == (db = *--pb))) |
4411 | 0 | /* do nothing */; |
4412 | 0 | done: |
4413 | 0 | if (da > db) |
4414 | 0 | goto IS_GT; |
4415 | 0 | if (da < db) |
4416 | 0 | goto IS_LT; |
4417 | 0 | } |
4418 | 0 | return MP_EQ; |
4419 | 0 | IS_LT: |
4420 | 0 | return MP_LT; |
4421 | 0 | IS_GT: |
4422 | 0 | return MP_GT; |
4423 | 0 | } /* end s_mp_cmp() */ |
4424 | | |
4425 | | /* }}} */ |
4426 | | |
4427 | | /* {{{ s_mp_cmp_d(a, d) */ |
4428 | | |
4429 | | /* Compare |a| <=> d, return 0 if equal, <0 if a<d, >0 if a>d */ |
4430 | | int |
4431 | | s_mp_cmp_d(const mp_int *a, mp_digit d) |
4432 | 0 | { |
4433 | 0 | if (USED(a) > 1) |
4434 | 0 | return MP_GT; |
4435 | 0 | |
4436 | 0 | if (DIGIT(a, 0) < d) |
4437 | 0 | return MP_LT; |
4438 | 0 | else if (DIGIT(a, 0) > d) |
4439 | 0 | return MP_GT; |
4440 | 0 | else |
4441 | 0 | return MP_EQ; |
4442 | 0 |
|
4443 | 0 | } /* end s_mp_cmp_d() */ |
4444 | | |
4445 | | /* }}} */ |
4446 | | |
4447 | | /* {{{ s_mp_ispow2(v) */ |
4448 | | |
4449 | | /* |
4450 | | Returns -1 if the value is not a power of two; otherwise, it returns |
4451 | | k such that v = 2^k, i.e. lg(v). |
4452 | | */ |
4453 | | int |
4454 | | s_mp_ispow2(const mp_int *v) |
4455 | 0 | { |
4456 | 0 | mp_digit d; |
4457 | 0 | int extra = 0, ix; |
4458 | 0 |
|
4459 | 0 | ix = MP_USED(v) - 1; |
4460 | 0 | d = MP_DIGIT(v, ix); /* most significant digit of v */ |
4461 | 0 |
|
4462 | 0 | extra = s_mp_ispow2d(d); |
4463 | 0 | if (extra < 0 || ix == 0) |
4464 | 0 | return extra; |
4465 | 0 | |
4466 | 0 | while (--ix >= 0) { |
4467 | 0 | if (DIGIT(v, ix) != 0) |
4468 | 0 | return -1; /* not a power of two */ |
4469 | 0 | extra += MP_DIGIT_BIT; |
4470 | 0 | } |
4471 | 0 |
|
4472 | 0 | return extra; |
4473 | 0 |
|
4474 | 0 | } /* end s_mp_ispow2() */ |
4475 | | |
4476 | | /* }}} */ |
4477 | | |
4478 | | /* {{{ s_mp_ispow2d(d) */ |
4479 | | |
4480 | | int |
4481 | | s_mp_ispow2d(mp_digit d) |
4482 | 0 | { |
4483 | 0 | if ((d != 0) && ((d & (d - 1)) == 0)) { /* d is a power of 2 */ |
4484 | 0 | int pow = 0; |
4485 | | #if defined(MP_USE_UINT_DIGIT) |
4486 | | if (d & 0xffff0000U) |
4487 | | pow += 16; |
4488 | | if (d & 0xff00ff00U) |
4489 | | pow += 8; |
4490 | | if (d & 0xf0f0f0f0U) |
4491 | | pow += 4; |
4492 | | if (d & 0xccccccccU) |
4493 | | pow += 2; |
4494 | | if (d & 0xaaaaaaaaU) |
4495 | | pow += 1; |
4496 | | #elif defined(MP_USE_LONG_LONG_DIGIT) |
4497 | | if (d & 0xffffffff00000000ULL) |
4498 | | pow += 32; |
4499 | | if (d & 0xffff0000ffff0000ULL) |
4500 | | pow += 16; |
4501 | | if (d & 0xff00ff00ff00ff00ULL) |
4502 | | pow += 8; |
4503 | | if (d & 0xf0f0f0f0f0f0f0f0ULL) |
4504 | | pow += 4; |
4505 | | if (d & 0xccccccccccccccccULL) |
4506 | | pow += 2; |
4507 | | if (d & 0xaaaaaaaaaaaaaaaaULL) |
4508 | | pow += 1; |
4509 | | #elif defined(MP_USE_LONG_DIGIT) |
4510 | 0 | if (d & 0xffffffff00000000UL) |
4511 | 0 | pow += 32; |
4512 | 0 | if (d & 0xffff0000ffff0000UL) |
4513 | 0 | pow += 16; |
4514 | 0 | if (d & 0xff00ff00ff00ff00UL) |
4515 | 0 | pow += 8; |
4516 | 0 | if (d & 0xf0f0f0f0f0f0f0f0UL) |
4517 | 0 | pow += 4; |
4518 | 0 | if (d & 0xccccccccccccccccUL) |
4519 | 0 | pow += 2; |
4520 | 0 | if (d & 0xaaaaaaaaaaaaaaaaUL) |
4521 | 0 | pow += 1; |
4522 | | #else |
4523 | | #error "unknown type for mp_digit" |
4524 | | #endif |
4525 | | return pow; |
4526 | 0 | } |
4527 | 0 | return -1; |
4528 | 0 |
|
4529 | 0 | } /* end s_mp_ispow2d() */ |
4530 | | |
4531 | | /* }}} */ |
4532 | | |
4533 | | /* }}} */ |
4534 | | |
4535 | | /* {{{ Primitive I/O helpers */ |
4536 | | |
4537 | | /* {{{ s_mp_tovalue(ch, r) */ |
4538 | | |
4539 | | /* |
4540 | | Convert the given character to its digit value, in the given radix. |
4541 | | If the given character is not understood in the given radix, -1 is |
4542 | | returned. Otherwise the digit's numeric value is returned. |
4543 | | |
4544 | | The results will be odd if you use a radix < 2 or > 62, you are |
4545 | | expected to know what you're up to. |
4546 | | */ |
4547 | | int |
4548 | | s_mp_tovalue(char ch, int r) |
4549 | 0 | { |
4550 | 0 | int val, xch; |
4551 | 0 |
|
4552 | 0 | if (r > 36) |
4553 | 0 | xch = ch; |
4554 | 0 | else |
4555 | 0 | xch = toupper(ch); |
4556 | 0 |
|
4557 | 0 | if (isdigit(xch)) |
4558 | 0 | val = xch - '0'; |
4559 | 0 | else if (isupper(xch)) |
4560 | 0 | val = xch - 'A' + 10; |
4561 | 0 | else if (islower(xch)) |
4562 | 0 | val = xch - 'a' + 36; |
4563 | 0 | else if (xch == '+') |
4564 | 0 | val = 62; |
4565 | 0 | else if (xch == '/') |
4566 | 0 | val = 63; |
4567 | 0 | else |
4568 | 0 | return -1; |
4569 | 0 | |
4570 | 0 | if (val < 0 || val >= r) |
4571 | 0 | return -1; |
4572 | 0 | |
4573 | 0 | return val; |
4574 | 0 |
|
4575 | 0 | } /* end s_mp_tovalue() */ |
4576 | | |
4577 | | /* }}} */ |
4578 | | |
4579 | | /* {{{ s_mp_todigit(val, r, low) */ |
4580 | | |
4581 | | /* |
4582 | | Convert val to a radix-r digit, if possible. If val is out of range |
4583 | | for r, returns zero. Otherwise, returns an ASCII character denoting |
4584 | | the value in the given radix. |
4585 | | |
4586 | | The results may be odd if you use a radix < 2 or > 64, you are |
4587 | | expected to know what you're doing. |
4588 | | */ |
4589 | | |
4590 | | char |
4591 | | s_mp_todigit(mp_digit val, int r, int low) |
4592 | 0 | { |
4593 | 0 | char ch; |
4594 | 0 |
|
4595 | 0 | if (val >= r) |
4596 | 0 | return 0; |
4597 | 0 | |
4598 | 0 | ch = s_dmap_1[val]; |
4599 | 0 |
|
4600 | 0 | if (r <= 36 && low) |
4601 | 0 | ch = tolower(ch); |
4602 | 0 |
|
4603 | 0 | return ch; |
4604 | 0 |
|
4605 | 0 | } /* end s_mp_todigit() */ |
4606 | | |
4607 | | /* }}} */ |
4608 | | |
4609 | | /* {{{ s_mp_outlen(bits, radix) */ |
4610 | | |
4611 | | /* |
4612 | | Return an estimate for how long a string is needed to hold a radix |
4613 | | r representation of a number with 'bits' significant bits, plus an |
4614 | | extra for a zero terminator (assuming C style strings here) |
4615 | | */ |
4616 | | int |
4617 | | s_mp_outlen(int bits, int r) |
4618 | 0 | { |
4619 | 0 | return (int)((double)bits * LOG_V_2(r) + 1.5) + 1; |
4620 | 0 |
|
4621 | 0 | } /* end s_mp_outlen() */ |
4622 | | |
4623 | | /* }}} */ |
4624 | | |
4625 | | /* }}} */ |
4626 | | |
4627 | | /* {{{ mp_read_unsigned_octets(mp, str, len) */ |
4628 | | /* mp_read_unsigned_octets(mp, str, len) |
4629 | | Read in a raw value (base 256) into the given mp_int |
4630 | | No sign bit, number is positive. Leading zeros ignored. |
4631 | | */ |
4632 | | |
4633 | | mp_err |
4634 | | mp_read_unsigned_octets(mp_int *mp, const unsigned char *str, mp_size len) |
4635 | 0 | { |
4636 | 0 | int count; |
4637 | 0 | mp_err res; |
4638 | 0 | mp_digit d; |
4639 | 0 |
|
4640 | 0 | ARGCHK(mp != NULL && str != NULL && len > 0, MP_BADARG); |
4641 | 0 |
|
4642 | 0 | mp_zero(mp); |
4643 | 0 |
|
4644 | 0 | count = len % sizeof(mp_digit); |
4645 | 0 | if (count) { |
4646 | 0 | for (d = 0; count-- > 0; --len) { |
4647 | 0 | d = (d << 8) | *str++; |
4648 | 0 | } |
4649 | 0 | MP_DIGIT(mp, 0) = d; |
4650 | 0 | } |
4651 | 0 |
|
4652 | 0 | /* Read the rest of the digits */ |
4653 | 0 | for (; len > 0; len -= sizeof(mp_digit)) { |
4654 | 0 | for (d = 0, count = sizeof(mp_digit); count > 0; --count) { |
4655 | 0 | d = (d << 8) | *str++; |
4656 | 0 | } |
4657 | 0 | if (MP_EQ == mp_cmp_z(mp)) { |
4658 | 0 | if (!d) |
4659 | 0 | continue; |
4660 | 0 | } else { |
4661 | 0 | if ((res = s_mp_lshd(mp, 1)) != MP_OKAY) |
4662 | 0 | return res; |
4663 | 0 | } |
4664 | 0 | MP_DIGIT(mp, 0) = d; |
4665 | 0 | } |
4666 | 0 | return MP_OKAY; |
4667 | 0 | } /* end mp_read_unsigned_octets() */ |
4668 | | /* }}} */ |
4669 | | |
4670 | | /* {{{ mp_unsigned_octet_size(mp) */ |
4671 | | unsigned int |
4672 | | mp_unsigned_octet_size(const mp_int *mp) |
4673 | 0 | { |
4674 | 0 | unsigned int bytes; |
4675 | 0 | int ix; |
4676 | 0 | mp_digit d = 0; |
4677 | 0 |
|
4678 | 0 | ARGCHK(mp != NULL, MP_BADARG); |
4679 | 0 | ARGCHK(MP_ZPOS == SIGN(mp), MP_BADARG); |
4680 | 0 |
|
4681 | 0 | bytes = (USED(mp) * sizeof(mp_digit)); |
4682 | 0 |
|
4683 | 0 | /* subtract leading zeros. */ |
4684 | 0 | /* Iterate over each digit... */ |
4685 | 0 | for (ix = USED(mp) - 1; ix >= 0; ix--) { |
4686 | 0 | d = DIGIT(mp, ix); |
4687 | 0 | if (d) |
4688 | 0 | break; |
4689 | 0 | bytes -= sizeof(d); |
4690 | 0 | } |
4691 | 0 | if (!bytes) |
4692 | 0 | return 1; |
4693 | 0 | |
4694 | 0 | /* Have MSD, check digit bytes, high order first */ |
4695 | 0 | for (ix = sizeof(mp_digit) - 1; ix >= 0; ix--) { |
4696 | 0 | unsigned char x = (unsigned char)(d >> (ix * CHAR_BIT)); |
4697 | 0 | if (x) |
4698 | 0 | break; |
4699 | 0 | --bytes; |
4700 | 0 | } |
4701 | 0 | return bytes; |
4702 | 0 | } /* end mp_unsigned_octet_size() */ |
4703 | | /* }}} */ |
4704 | | |
4705 | | /* {{{ mp_to_unsigned_octets(mp, str) */ |
4706 | | /* output a buffer of big endian octets no longer than specified. */ |
4707 | | mp_err |
4708 | | mp_to_unsigned_octets(const mp_int *mp, unsigned char *str, mp_size maxlen) |
4709 | 0 | { |
4710 | 0 | int ix, pos = 0; |
4711 | 0 | unsigned int bytes; |
4712 | 0 |
|
4713 | 0 | ARGCHK(mp != NULL && str != NULL && !SIGN(mp), MP_BADARG); |
4714 | 0 |
|
4715 | 0 | bytes = mp_unsigned_octet_size(mp); |
4716 | 0 | ARGCHK(bytes <= maxlen, MP_BADARG); |
4717 | 0 |
|
4718 | 0 | /* Iterate over each digit... */ |
4719 | 0 | for (ix = USED(mp) - 1; ix >= 0; ix--) { |
4720 | 0 | mp_digit d = DIGIT(mp, ix); |
4721 | 0 | int jx; |
4722 | 0 |
|
4723 | 0 | /* Unpack digit bytes, high order first */ |
4724 | 0 | for (jx = sizeof(mp_digit) - 1; jx >= 0; jx--) { |
4725 | 0 | unsigned char x = (unsigned char)(d >> (jx * CHAR_BIT)); |
4726 | 0 | if (!pos && !x) /* suppress leading zeros */ |
4727 | 0 | continue; |
4728 | 0 | str[pos++] = x; |
4729 | 0 | } |
4730 | 0 | } |
4731 | 0 | if (!pos) |
4732 | 0 | str[pos++] = 0; |
4733 | 0 | return pos; |
4734 | 0 | } /* end mp_to_unsigned_octets() */ |
4735 | | /* }}} */ |
4736 | | |
4737 | | /* {{{ mp_to_signed_octets(mp, str) */ |
4738 | | /* output a buffer of big endian octets no longer than specified. */ |
4739 | | mp_err |
4740 | | mp_to_signed_octets(const mp_int *mp, unsigned char *str, mp_size maxlen) |
4741 | 0 | { |
4742 | 0 | int ix, pos = 0; |
4743 | 0 | unsigned int bytes; |
4744 | 0 |
|
4745 | 0 | ARGCHK(mp != NULL && str != NULL && !SIGN(mp), MP_BADARG); |
4746 | 0 |
|
4747 | 0 | bytes = mp_unsigned_octet_size(mp); |
4748 | 0 | ARGCHK(bytes <= maxlen, MP_BADARG); |
4749 | 0 |
|
4750 | 0 | /* Iterate over each digit... */ |
4751 | 0 | for (ix = USED(mp) - 1; ix >= 0; ix--) { |
4752 | 0 | mp_digit d = DIGIT(mp, ix); |
4753 | 0 | int jx; |
4754 | 0 |
|
4755 | 0 | /* Unpack digit bytes, high order first */ |
4756 | 0 | for (jx = sizeof(mp_digit) - 1; jx >= 0; jx--) { |
4757 | 0 | unsigned char x = (unsigned char)(d >> (jx * CHAR_BIT)); |
4758 | 0 | if (!pos) { |
4759 | 0 | if (!x) /* suppress leading zeros */ |
4760 | 0 | continue; |
4761 | 0 | if (x & 0x80) { /* add one leading zero to make output positive. */ |
4762 | 0 | ARGCHK(bytes + 1 <= maxlen, MP_BADARG); |
4763 | 0 | if (bytes + 1 > maxlen) |
4764 | 0 | return MP_BADARG; |
4765 | 0 | str[pos++] = 0; |
4766 | 0 | } |
4767 | 0 | } |
4768 | 0 | str[pos++] = x; |
4769 | 0 | } |
4770 | 0 | } |
4771 | 0 | if (!pos) |
4772 | 0 | str[pos++] = 0; |
4773 | 0 | return pos; |
4774 | 0 | } /* end mp_to_signed_octets() */ |
4775 | | /* }}} */ |
4776 | | |
4777 | | /* {{{ mp_to_fixlen_octets(mp, str) */ |
4778 | | /* output a buffer of big endian octets exactly as long as requested. */ |
4779 | | mp_err |
4780 | | mp_to_fixlen_octets(const mp_int *mp, unsigned char *str, mp_size length) |
4781 | 0 | { |
4782 | 0 | int ix, pos = 0; |
4783 | 0 | unsigned int bytes; |
4784 | 0 |
|
4785 | 0 | ARGCHK(mp != NULL && str != NULL && !SIGN(mp), MP_BADARG); |
4786 | 0 |
|
4787 | 0 | bytes = mp_unsigned_octet_size(mp); |
4788 | 0 | ARGCHK(bytes <= length, MP_BADARG); |
4789 | 0 |
|
4790 | 0 | /* place any needed leading zeros */ |
4791 | 0 | for (; length > bytes; --length) { |
4792 | 0 | *str++ = 0; |
4793 | 0 | } |
4794 | 0 |
|
4795 | 0 | /* Iterate over each digit... */ |
4796 | 0 | for (ix = USED(mp) - 1; ix >= 0; ix--) { |
4797 | 0 | mp_digit d = DIGIT(mp, ix); |
4798 | 0 | int jx; |
4799 | 0 |
|
4800 | 0 | /* Unpack digit bytes, high order first */ |
4801 | 0 | for (jx = sizeof(mp_digit) - 1; jx >= 0; jx--) { |
4802 | 0 | unsigned char x = (unsigned char)(d >> (jx * CHAR_BIT)); |
4803 | 0 | if (!pos && !x) /* suppress leading zeros */ |
4804 | 0 | continue; |
4805 | 0 | str[pos++] = x; |
4806 | 0 | } |
4807 | 0 | } |
4808 | 0 | if (!pos) |
4809 | 0 | str[pos++] = 0; |
4810 | 0 | return MP_OKAY; |
4811 | 0 | } /* end mp_to_fixlen_octets() */ |
4812 | | /* }}} */ |
4813 | | |
4814 | | /*------------------------------------------------------------------------*/ |
4815 | | /* HERE THERE BE DRAGONS */ |