bn_mp_karatsuba_mul.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include <tommath.h>
  2. #ifdef BN_MP_KARATSUBA_MUL_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis
  4. *
  5. * LibTomMath is a library that provides multiple-precision
  6. * integer arithmetic as well as number theoretic functionality.
  7. *
  8. * The library was designed directly after the MPI library by
  9. * Michael Fromberger but has been written from scratch with
  10. * additional optimizations in place.
  11. *
  12. * The library is free for all purposes without any express
  13. * guarantee it works.
  14. *
  15. * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
  16. */
  17. /* c = |a| * |b| using Karatsuba Multiplication using
  18. * three half size multiplications
  19. *
  20. * Let B represent the radix [e.g. 2**DIGIT_BIT] and
  21. * let n represent half of the number of digits in
  22. * the min(a,b)
  23. *
  24. * a = a1 * B**n + a0
  25. * b = b1 * B**n + b0
  26. *
  27. * Then, a * b =>
  28. a1b1 * B**2n + ((a1 + a0)(b1 + b0) - (a0b0 + a1b1)) * B + a0b0
  29. *
  30. * Note that a1b1 and a0b0 are used twice and only need to be
  31. * computed once. So in total three half size (half # of
  32. * digit) multiplications are performed, a0b0, a1b1 and
  33. * (a1+b1)(a0+b0)
  34. *
  35. * Note that a multiplication of half the digits requires
  36. * 1/4th the number of single precision multiplications so in
  37. * total after one call 25% of the single precision multiplications
  38. * are saved. Note also that the call to mp_mul can end up back
  39. * in this function if the a0, a1, b0, or b1 are above the threshold.
  40. * This is known as divide-and-conquer and leads to the famous
  41. * O(N**lg(3)) or O(N**1.584) work which is asymptopically lower than
  42. * the standard O(N**2) that the baseline/comba methods use.
  43. * Generally though the overhead of this method doesn't pay off
  44. * until a certain size (N ~ 80) is reached.
  45. */
  46. int mp_karatsuba_mul (mp_int * a, mp_int * b, mp_int * c)
  47. {
  48. mp_int x0, x1, y0, y1, t1, x0y0, x1y1;
  49. int B, err;
  50. /* default the return code to an error */
  51. err = MP_MEM;
  52. /* min # of digits */
  53. B = MIN (a->used, b->used);
  54. /* now divide in two */
  55. B = B >> 1;
  56. /* init copy all the temps */
  57. if (mp_init_size (&x0, B) != MP_OKAY)
  58. goto ERR;
  59. if (mp_init_size (&x1, a->used - B) != MP_OKAY)
  60. goto X0;
  61. if (mp_init_size (&y0, B) != MP_OKAY)
  62. goto X1;
  63. if (mp_init_size (&y1, b->used - B) != MP_OKAY)
  64. goto Y0;
  65. /* init temps */
  66. if (mp_init_size (&t1, B * 2) != MP_OKAY)
  67. goto Y1;
  68. if (mp_init_size (&x0y0, B * 2) != MP_OKAY)
  69. goto T1;
  70. if (mp_init_size (&x1y1, B * 2) != MP_OKAY)
  71. goto X0Y0;
  72. /* now shift the digits */
  73. x0.used = y0.used = B;
  74. x1.used = a->used - B;
  75. y1.used = b->used - B;
  76. {
  77. register int x;
  78. register mp_digit *tmpa, *tmpb, *tmpx, *tmpy;
  79. /* we copy the digits directly instead of using higher level functions
  80. * since we also need to shift the digits
  81. */
  82. tmpa = a->dp;
  83. tmpb = b->dp;
  84. tmpx = x0.dp;
  85. tmpy = y0.dp;
  86. for (x = 0; x < B; x++) {
  87. *tmpx++ = *tmpa++;
  88. *tmpy++ = *tmpb++;
  89. }
  90. tmpx = x1.dp;
  91. for (x = B; x < a->used; x++) {
  92. *tmpx++ = *tmpa++;
  93. }
  94. tmpy = y1.dp;
  95. for (x = B; x < b->used; x++) {
  96. *tmpy++ = *tmpb++;
  97. }
  98. }
  99. /* only need to clamp the lower words since by definition the
  100. * upper words x1/y1 must have a known number of digits
  101. */
  102. mp_clamp (&x0);
  103. mp_clamp (&y0);
  104. /* now calc the products x0y0 and x1y1 */
  105. /* after this x0 is no longer required, free temp [x0==t2]! */
  106. if (mp_mul (&x0, &y0, &x0y0) != MP_OKAY)
  107. goto X1Y1; /* x0y0 = x0*y0 */
  108. if (mp_mul (&x1, &y1, &x1y1) != MP_OKAY)
  109. goto X1Y1; /* x1y1 = x1*y1 */
  110. /* now calc x1+x0 and y1+y0 */
  111. if (s_mp_add (&x1, &x0, &t1) != MP_OKAY)
  112. goto X1Y1; /* t1 = x1 - x0 */
  113. if (s_mp_add (&y1, &y0, &x0) != MP_OKAY)
  114. goto X1Y1; /* t2 = y1 - y0 */
  115. if (mp_mul (&t1, &x0, &t1) != MP_OKAY)
  116. goto X1Y1; /* t1 = (x1 + x0) * (y1 + y0) */
  117. /* add x0y0 */
  118. if (mp_add (&x0y0, &x1y1, &x0) != MP_OKAY)
  119. goto X1Y1; /* t2 = x0y0 + x1y1 */
  120. if (s_mp_sub (&t1, &x0, &t1) != MP_OKAY)
  121. goto X1Y1; /* t1 = (x1+x0)*(y1+y0) - (x1y1 + x0y0) */
  122. /* shift by B */
  123. if (mp_lshd (&t1, B) != MP_OKAY)
  124. goto X1Y1; /* t1 = (x0y0 + x1y1 - (x1-x0)*(y1-y0))<<B */
  125. if (mp_lshd (&x1y1, B * 2) != MP_OKAY)
  126. goto X1Y1; /* x1y1 = x1y1 << 2*B */
  127. if (mp_add (&x0y0, &t1, &t1) != MP_OKAY)
  128. goto X1Y1; /* t1 = x0y0 + t1 */
  129. if (mp_add (&t1, &x1y1, c) != MP_OKAY)
  130. goto X1Y1; /* t1 = x0y0 + t1 + x1y1 */
  131. /* Algorithm succeeded set the return code to MP_OKAY */
  132. err = MP_OKAY;
  133. X1Y1:mp_clear (&x1y1);
  134. X0Y0:mp_clear (&x0y0);
  135. T1:mp_clear (&t1);
  136. Y1:mp_clear (&y1);
  137. Y0:mp_clear (&y0);
  138. X1:mp_clear (&x1);
  139. X0:mp_clear (&x0);
  140. ERR:
  141. return err;
  142. }
  143. #endif
  144. /* $Source: /cvs/libtom/libtommath/bn_mp_karatsuba_mul.c,v $ */
  145. /* $Revision: 1.5 $ */
  146. /* $Date: 2006/03/31 14:18:44 $ */