Overview
| Artifact ID: | 9ed262d438c0ff33eb7fba781355f79c842943fd |
|---|---|
| Ticket: | 2814286fffffffffffffffffffffffffffffffff |
| Date: | 2009-06-30 06:17:22 |
| User: | kennykb |
| Artifact Attached: | c9ba1da724d5e2d6fd69bc61d2e7dc5cc3c4d8ba |
| Filename: | bn_mp_bz_div.c |
| Description: | Burnikel-Ziegler division |
Content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648#include <stdio.h> #include <tommath.h> #ifdef BN_MP_BZ_DIV_C /* LibTomMath, multiple-precision integer library -- Tom St Denis * * LibTomMath is a library that provides multiple-precision * integer arithmetic as well as number theoretic functionality. * * The library was designed directly after the MPI library by * Michael Fromberger but has been written from scratch with * additional optimizations in place. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, [email protected], http://math.libtomcrypt.com * * This module: Kevin B. Kenny, [email protected] */ /* #define TRACE_BZ_DIV #define TRACE_BZ_2N_1N #define TRACE_BZ_3N_2N */ #if defined(TRACE_BZ_DIV) || defined(TRACE_BZ_2N_1N) || defined(TRACE_BZ_3N_2N) static void traceprint(const char* func, const char* var, const mp_int* val) { int i, j, did; const char* sep = ""; fprintf(stderr, "%s: %s = ", func, var); if (val->sign == MP_NEG) { fprintf(stderr, "-( "); } did = 0; for (i = val->used * DIGIT_BIT - 1; i >= 0; --i) { if (val->dp[i / DIGIT_BIT] & (1 << (i % DIGIT_BIT))) { did = 1; for (j = i-1; j >= 0; --j) { if ((val->dp[j / DIGIT_BIT] & (1 << (j % DIGIT_BIT))) == 0) { break; } } ++j; if (j == i) { fprintf(stderr, "%s2**%d", sep, i); } else if (j == i + 1) { fprintf(stderr, "%s2**%d + 2**%d", sep, i, j); } else { fprintf(stderr, "%s2**%d - 2**%d", sep, i+1, j); } sep = " + "; i = j; } } if (!did) { fprintf(stderr, "0"); } if (val->sign == MP_NEG) { fprintf(stderr, " )"); } fprintf(stderr, "\n"); fflush(stderr); } #endif /* Static functions defined in this file */ static int mp_bz_div_2n_1n(int n, mp_int* a, mp_int* b, mp_int* c, mp_int* d); static int mp_bz_div_3n_2n(int n, mp_int* a, mp_int* b, mp_int* c, mp_int* d); /* * mp_bz_div -- * * Division in Karatsuba time: a/b == c remainder d, or a == bc + d * * Source: Christoph Burnikel, Joachim Ziegler. "Fast Recursive Division." * Forschungsbericht MPI I 98 1 022, Max-Plank-Institut fuer * Informatik, Saarbruecken, Germany (October 1998) * http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.47.565 */ int mp_bz_div(mp_int* a, mp_int* b, mp_int* c, mp_int* d) { int signum; /* Sign of the quotient and remainder */ int k; /* Number of recursive steps we will need */ int j; /* Number of mp_digits in the smallest step */ int m; /* 2**k * BZ_DIV_CUTOFF */ int n; /* Number of mp_digits in the current step */ int sigma; /* Number of bit shifts needed to normalize * the divisor */ int t; int i; mp_int aa, bb, cc, r, q; mp_digit dig; int res; #ifdef TRACE_BZ_DIV fprintf(stderr, "mp_bz_div: a->used=%d, b->used=%d\n", a->used, b->used); traceprint("mp_bz_div", "a", a); traceprint("mp_bz_div", "b", b); #endif /* Algorithm 3. D_{r/s} */ signum = (a->sign == b->sign) ? MP_ZPOS : MP_NEG; /* Determine smallest width that will accommodate the divisor. */ m = BZ_DIV_CUTOFF; k = 0; while (m <= b->used) { m <<= 1; ++k; } j = (b->used + m - 1) / m; n = j * m; /* * Normalize the divisor in an n-mp_digit integer. Shift the dividend * left by the same amount. */ sigma = MP_DIGIT_BIT * (n - b->used); dig = b->dp[b->used - 1]; while (dig < (1 << (MP_DIGIT_BIT-1))) { ++sigma; dig <<= 1; } /* Allocate memory for shifted operands and for results */ if ((res = mp_init_multi(&aa, &bb, NULL)) != MP_OKAY) { return res; } /* Shift b left to normalize it. Shift a left by the same amount. */ if ((res = mp_mul_2d(a, sigma, &aa)) != MP_OKAY) { goto LBL_BB; } aa.sign = MP_ZPOS; if ((res = mp_mul_2d(b, sigma, &bb)) != MP_OKAY) { goto LBL_BB; } bb.sign = MP_ZPOS; /* * bb is now n mp_digits long, and its most significant bit is 1. * Find out how many n-digit pieces there are in aa such that * the most significant bit of the most significant piece is *not* 1. */ t = (aa.used + n - 1) / n; if ((aa.used % n == 0) && ((aa.dp[aa.used-1] & (1 << (DIGIT_BIT - 1))) != 0)) { ++t; } if (t < 2) { t = 2; } #ifdef TRACE_BZ_DIV fprintf(stderr, "mp_bz_div: t = %d, n = %d sigma = %d\n", t, n, sigma); traceprint("mp_bz_div", "shifted dividend aa", &aa); traceprint("mp_bz_div", "shifted divisor bb", &bb); #endif /* Allocate memory for temps */ if ((res = mp_init_size(&cc, t * n)) != MP_OKAY) { goto LBL_BB; } if ((res = mp_init_size(&r, 2 * n)) != MP_OKAY) { goto LBL_CC; } if ((res = mp_init_size(&q, t * n)) != MP_OKAY) { goto LBL_R; } /* * Begin the division with the most significant 2*n mp_digits of aa, * when a is viewed as being t*n digits long. In other words, * shift A to the right by (t-2)*n mp_digits */ for (i = (t-1) * n, j = 0; i < aa.used; ++i, ++j) { r.dp[j] = aa.dp[i]; } r.used = j; /* Do 'school short division' on 'digits' that are each n mp_digits long. */ cc.used = 0; t-=2; while (t >= 0) { /* Shift the next n mp_digits into r */ if ((res = mp_grow(&r, r.used + n)) != MP_OKAY) { goto LBL_Q; } for (i = 0, j = n; i < r.used; ++i, ++j) { r.dp[j] = r.dp[i]; } r.used = j; for (j = 0, i = t * n; j < n && i < aa.used; ++i, ++j) { r.dp[j] = aa.dp[i]; } while (j < n) { r.dp[j] = 0; } /* Perform one division step */ #ifdef TRACE_BZ_DIV fprintf(stderr, "mp_bz_div: part %d\n", t); traceprint("mp_bz_div", "dividend r", &r); traceprint("mp_bz_div", "divisor bb", &bb); #endif if ((res = mp_bz_div_2n_1n(n, &r, &bb, &q, &r)) != MP_OKAY) { goto LBL_Q; } #ifdef TRACE_BZ_DIV traceprint("mp_bz_div", "quotient q", &q); traceprint("mp_bz_div", "remainder r", &r); #endif /* Accumulate the quotient digits in cc */ for (j = t * n, i = 0; i < q.used; ++i, ++j) { cc.dp[j] = q.dp[i]; } for (; i < n; ++i, ++j) { cc.dp[j] = 0; } if (cc.used == 0) { cc.used = j; } #ifdef TRACE_BZ_DIV traceprint("mp_bz_div", "partial quotient cc", &cc); #endif --t; } /* * The loop above has put the quotient in cc and remainder in r, but the * remainder is shifted left by sigma bits. Shift it back to the right. */ if ((res = mp_div_2d(&r, sigma, &r, NULL)) != MP_OKAY) { goto LBL_Q; } /* Store results for the caller */ mp_clamp(&cc); cc.sign = r.sign = signum; if (c != NULL) { mp_exch(&cc, c); } if (d != NULL) { mp_exch(&r, d); } res = MP_OKAY; LBL_Q: mp_clear(&q); LBL_R: mp_clear(&r); LBL_CC: mp_clear(&cc); LBL_BB: mp_clear(&bb); mp_clear(&aa); return res; } /* * mp_bz_div_2n_1n -- * * Recursive division of a 2n-mp_digit number by an n-mp_digit number. * * This procedure corresponds to Algorithm 1 of the Burnikel-Ziegler paper. */ static int mp_bz_div_2n_1n(int n, /* Length of the numbers */ mp_int* a, /* Dividend */ mp_int* b, /* Divisor */ mp_int* c, /* Quotient */ mp_int* d /* Remainder */ ) { mp_int q1, q2, r; int res; int i, j; int halfn; if ((res = mp_init_multi(&q1, &q2, &r, NULL)) != MP_OKAY) { goto LBL_R; } if (n % 1 || n <= BZ_DIV_CUTOFF) { if ((res = mp_div(a, b, &q1, &r)) != MP_OKAY) { goto LBL_R; } if (c != NULL) { mp_exch(&q1, c); } if (d != NULL) { mp_exch(&r, d); } goto LBL_R; } #ifdef TRACE_BZ_2N_1N fprintf(stderr, "mp_bz_div_2n_1n: n=%d, a->used=%d, b->used=%d\n", n, a->used, b->used); traceprint("mp_bz_div_2n_1n", "a", a); traceprint("mp_bz_div_2n_1n", "b", b); #endif halfn = n / 2; /* Extract the most significant 3/4 of the digits from A. */ if (a->used >= halfn) { if ((res = mp_grow(&r, a->used-halfn)) != MP_OKAY) { goto LBL_R; } } for (i = 0, j = halfn; j < a->used; ++i, ++j) { r.dp[i] = a->dp[j]; } r.used = i; /* Generate the most significant digits of the result in q */ #ifdef TRACE_BZ_2N_1N fprintf(stderr, "mp_bz_div_2n_1n: first division n=%d, r->used=%d, b->used=%d\n", n, r.used, b->used); traceprint("mp_bz_div_2n_1n", "r", &r); traceprint("mp_bz_div_2n_1n", "b", b); #endif if ((res = mp_bz_div_3n_2n(halfn, &r, b, &q1, &r)) != MP_OKAY) { goto LBL_R; } #ifdef TRACE_BZ_2N_1N fprintf(stderr, "mp_bz_div_2n_1n: first division returns n=%d\n", n); traceprint("mp_bz_div_2n_1n", "q1", &q1); traceprint("mp_bz_div_2n_1n", "r", &r); /* Shift the remainder into aa and bring in the rest of the dividend */ fprintf(stderr, "mp_bz_div_2n_1n: n=%d halfn=%d r.alloc=%d need %d\n", n, halfn, r.alloc, r.used + halfn); fflush(stderr); #endif if ((res = mp_grow(&r, r.used + halfn)) != MP_OKAY) { goto LBL_R; } #ifdef TRACE_BZ_2N_1N traceprint("mp_bz_div_2n_1n", "a", a); #endif for (i = r.used-1; i >= 0; --i) { r.dp[i + halfn] = r.dp[i]; } for (i = 0; i < halfn && i < a->used; ++i) { r.dp[i] = a->dp[i]; } while (i < halfn) { r.dp[i++] = 0; } r.used += halfn; /* Divide to get the least significant digits */ #ifdef TRACE_BZ_2N_1N fprintf(stderr, "mp_bz_div_2n_1n: second division n=%d, r.used=%d, b->used=%d\n", n, r.used, b->used); traceprint("mp_bz_div_2n_1n", "r", &r); traceprint("mp_bz_div_2n_1n", "b", b); #endif mp_bz_div_3n_2n(halfn, &r, b, &q2, &r); #ifdef TRACE_BZ_2N_1N traceprint("mp_bz_div_2n_1n", "q2", &q2); traceprint("mp_bz_div_2n_1n", "r", &r); #endif /* Compose the quotient */ #ifdef TRACE_BZ_2N_1N fprintf(stderr, "mp_bz_div_2n_1n: combining quotients q1.used %d q2.used %d halfn %d.\n", q1.used, q2.used, halfn); traceprint("mp_bz_div_2n_1n", "q1", &q1); traceprint("mp_bz_div_2n_1n", "q2", &q2); #endif if ((res = mp_grow(&q2, q1.used+halfn)) != MP_OKAY) { goto LBL_R; } for (i = q2.used; i < halfn; ++i) { q2.dp[i] = 0; } for (j = 0; j < q1.used; ++j) { q2.dp[i++] = q1.dp[j]; } q2.used = i; #ifdef TRACE_BZ_2N_1N_ENTRY_EXIT fprintf(stderr, "mp_bz_div_2n_1n: n=%d, q2.used=%d, r.used=%d\n", n, q2.used, r.used); traceprint("mp_bz_div_2n_1n", "q2", &q2); traceprint("mp_bz_div_2n_1n", "r", &r); #endif if (c != NULL) { mp_exch(c, &q2); } if (d != NULL) { mp_exch(d, &r); } LBL_R: mp_clear_multi(&r, &q2, &q1, NULL); return res; } /* * mp_bz_div_3n_2n -- * * Divide a 3n-digit number by a 2n-digit number yielding an n-digit * quotient and a 2n-digit remainder * * This procedure corresponds to Algorithm 2 of the Burnikel-Ziegler paper. */ static int mp_bz_div_3n_2n(int n, /* Length of the numbers */ mp_int* a, /* Dividend */ mp_int* b, /* Divisor */ mp_int* c, /* Quotient */ mp_int* d /* Remainder */ ) { mp_int aa, bb; mp_int Qhat, Rhat, R1; int i, j; int altb; int res; if ((res = mp_init_multi(&Qhat, &R1, &Rhat, NULL)) != MP_OKAY) { return res; } #ifdef TRACE_BZ_3N_2N fprintf(stderr, "mp_bz_div_3n_2n: n = %d, a->used = %d, b->used = %d\n", n, a->used, b->used); fflush(stderr); traceprint ("mp_bz_div_3n_2n", "a", a); traceprint ("mp_bz_div_3n_2n", "b", b); #endif /* * 1.-2. * Extract the most significant 2n digits of a and the most significant n * digits of b. */ if ((res = mp_init_size(&aa, 2 * n)) != MP_OKAY) { goto LBL_RHAT; } if ((res = mp_init_size(&bb, n)) != MP_OKAY) { goto LBL_AA; } for (i = n; i < a->used; ++i) { aa.dp[i-n] = a->dp[i]; } aa.used = i-n; for (i = n; i < b->used; ++i) { bb.dp[i-n] = b->dp[i]; } bb.used = i-n; #ifdef TRACE_BZ_3N_2N fprintf(stderr, "mp_bz_div_3n_2n: aa.used = %d bb.used = %d\n", aa.used, bb.used); fflush(stderr); traceprint ("mp_bz_div_3n_2n", "aa", &aa); traceprint ("mp_bz_div_3n_2n", "bb", &bb); #endif /* At this point, aa is [A1,A2] and bb is B1. Compare A1 and B1. */ altb = 0; if (aa.used > bb.used + n) { altb = 0; } else if (bb.used + n > aa.used) { altb = 1; } else { for (i = aa.used-1, j = bb.used-1; j >= 0; i--, j--) { if (aa.dp[i] < bb.dp[j]) { altb = 1; break; } else if (bb.dp[j] < aa.dp[i]) { altb = 0; break; } } } #ifdef TRACE_BZ_3N_2N fprintf(stderr, "mp_bz_div_3n_2n: A1<B1? %s\n", (altb ? "yes" : "no")); fflush(stderr); #endif /* * 3. If A1 < B1, compute c = (A1,A2)/B1 with remainder d recursively. * Otherwise, let c = 2**(n*DIGIT_BIT)-1, and d = (A1,A2)-(B1,0)+(0,B1). */ if (altb) { #ifdef TRACE_BZ_3N_2N fprintf(stderr, "mp_bz_div_3n_2n: recursing\n"); fflush(stderr); #endif if ((res = mp_bz_div_2n_1n(n, &aa, &bb, &Qhat, &R1)) != MP_OKAY) { goto LBL_BB; } } else { #ifdef TRACE_BZ_3N_2N fprintf(stderr, "mp_bz_div_3n_2n: big quotient\n"); #endif /* Qhat=(beta**n) - 1 */ if ((res = mp_grow(&Qhat, n)) != MP_OKAY) { goto LBL_BB; } for (i = 0; i < n; ++i) { Qhat.dp[i] = MP_DIGIT_MAX; } Qhat.used = n; #ifdef TRACE_BZ_3N_2N traceprint("mp_bz_div_3n_2n", "Qhat", &Qhat); #endif /* R1 = [A1, A2] + [0, B1] - [B1, 0] */ if ((res = mp_add(&aa, &bb, &R1)) != MP_OKAY) { goto LBL_BB; } if ((res = mp_lshd(&bb, n)) != MP_OKAY) { goto LBL_BB; } if ((res = mp_sub(&R1, &bb, &R1)) != MP_OKAY) { goto LBL_BB; } #ifdef TRACE_BZ_3N_2N traceprint("mp_bz_div_3n_2n", "R1", &R1); #endif } #ifdef TRACE_BZ_3N_2N fprintf(stderr, "mp_bz_div_3n_2n (n = %d): first division returns:\n", n); traceprint("mp_bz_div_3n_2n", "[A1,A2]", &aa); traceprint("mp_bz_div_3n_2n", "[B1]", &bb); traceprint("mp_bz_div_3n_2n", "Qhat", &Qhat); traceprint("mp_bz_div_3n_2n", "R1", &R1); #endif /* Get the less significant digits of b. */ #ifdef TRACE_BZ_3N_2N fprintf(stderr, "bb's alloc is %d, need %d\n", bb.alloc, n); fflush(stderr); #endif for (i = 0; i < n; ++i) { bb.dp[i] = b->dp[i]; } while (i < bb.used) { bb.dp[i++] = 0; } bb.used = n; mp_clamp(&bb); /* 4. D = Qhat * B2 */ if ((res = mp_mul(&Qhat, &bb, &aa)) != MP_OKAY) { goto LBL_BB; } #ifdef TRACE_BZ_3N_2N fprintf(stderr, "mp_bz_div_3n_2n (n = %d): trial multiplication:\n", n); traceprint("mp_bz_div_3n_2n", "multiplicand Qhat", &Qhat); traceprint("mp_bz_div_3n_2n", "multiplier B2", &bb); traceprint("mp_bz_div_3n_2n", "product", &aa); #endif /* * 5. Rhat = R1 * beta**n + A3 - D * (Paper has a typo, it has A4 at step 5) */ #ifdef TRACE_BZ_3N_2N fprintf(stderr, "mp_bz_div_3n_2n: making rhat\n"); traceprint("mp_bz_div_3n_2n", "R1", &R1); traceprint("mp_bz_div_3n_2n", "a", a); traceprint("mp_bz_div_3n_2n", "D", &aa); #endif if ((res = mp_lshd(&R1, n)) != MP_OKAY) { goto LBL_BB; } if ((res = mp_mod_2d(a, MP_DIGIT_BIT * n, &Rhat)) != MP_OKAY) { goto LBL_BB; } if ((res = mp_add(&Rhat, &R1, &Rhat)) != MP_OKAY) { goto LBL_BB; } #ifdef TRACE_BZ_3N_2N traceprint("mp_bz_div_3n_2n", "[R1,A3]", &Rhat); #endif if ((res = mp_sub(&Rhat, &aa, &Rhat)) != MP_OKAY) { goto LBL_BB; } #ifdef TRACE_BZ_3N_2N traceprint("mp_bz_div_3n_2n", "[R1,A3] - D", &Rhat); #endif /* 6. While Rhat < 0, Rhat += B and Qhat -= 1 */ while (Rhat.sign == MP_NEG) { if ((res = mp_add(&Rhat, b, &Rhat)) != MP_OKAY) { goto LBL_BB; } if ((res = mp_sub_d(&Qhat, 1, &Qhat)) != MP_OKAY) { goto LBL_BB; } } if (c != NULL) { mp_exch(c,&Qhat); } if (d != NULL) { mp_exch(d,&Rhat); } LBL_BB: mp_clear(&bb); LBL_AA: mp_clear(&aa); LBL_RHAT: mp_clear_multi(&Rhat, &R1, &Qhat, NULL); return res; } #endif