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