bn_mp_div.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #include <tommath.h>
  2. #ifdef BN_MP_DIV_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. #ifdef BN_MP_DIV_SMALL
  18. /* slower bit-bang division... also smaller */
  19. int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d)
  20. {
  21. mp_int ta, tb, tq, q;
  22. int res, n, n2;
  23. /* is divisor zero ? */
  24. if (mp_iszero (b) == 1) {
  25. return MP_VAL;
  26. }
  27. /* if a < b then q=0, r = a */
  28. if (mp_cmp_mag (a, b) == MP_LT) {
  29. if (d != NULL) {
  30. res = mp_copy (a, d);
  31. } else {
  32. res = MP_OKAY;
  33. }
  34. if (c != NULL) {
  35. mp_zero (c);
  36. }
  37. return res;
  38. }
  39. /* init our temps */
  40. if ((res = mp_init_multi(&ta, &tb, &tq, &q, NULL) != MP_OKAY)) {
  41. return res;
  42. }
  43. mp_set(&tq, 1);
  44. n = mp_count_bits(a) - mp_count_bits(b);
  45. if (((res = mp_abs(a, &ta)) != MP_OKAY) ||
  46. ((res = mp_abs(b, &tb)) != MP_OKAY) ||
  47. ((res = mp_mul_2d(&tb, n, &tb)) != MP_OKAY) ||
  48. ((res = mp_mul_2d(&tq, n, &tq)) != MP_OKAY)) {
  49. goto LBL_ERR;
  50. }
  51. while (n-- >= 0) {
  52. if (mp_cmp(&tb, &ta) != MP_GT) {
  53. if (((res = mp_sub(&ta, &tb, &ta)) != MP_OKAY) ||
  54. ((res = mp_add(&q, &tq, &q)) != MP_OKAY)) {
  55. goto LBL_ERR;
  56. }
  57. }
  58. if (((res = mp_div_2d(&tb, 1, &tb, NULL)) != MP_OKAY) ||
  59. ((res = mp_div_2d(&tq, 1, &tq, NULL)) != MP_OKAY)) {
  60. goto LBL_ERR;
  61. }
  62. }
  63. /* now q == quotient and ta == remainder */
  64. n = a->sign;
  65. n2 = (a->sign == b->sign ? MP_ZPOS : MP_NEG);
  66. if (c != NULL) {
  67. mp_exch(c, &q);
  68. c->sign = (mp_iszero(c) == MP_YES) ? MP_ZPOS : n2;
  69. }
  70. if (d != NULL) {
  71. mp_exch(d, &ta);
  72. d->sign = (mp_iszero(d) == MP_YES) ? MP_ZPOS : n;
  73. }
  74. LBL_ERR:
  75. mp_clear_multi(&ta, &tb, &tq, &q, NULL);
  76. return res;
  77. }
  78. #else
  79. /* integer signed division.
  80. * c*b + d == a [e.g. a/b, c=quotient, d=remainder]
  81. * HAC pp.598 Algorithm 14.20
  82. *
  83. * Note that the description in HAC is horribly
  84. * incomplete. For example, it doesn't consider
  85. * the case where digits are removed from 'x' in
  86. * the inner loop. It also doesn't consider the
  87. * case that y has fewer than three digits, etc..
  88. *
  89. * The overall algorithm is as described as
  90. * 14.20 from HAC but fixed to treat these cases.
  91. */
  92. int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
  93. {
  94. mp_int q, x, y, t1, t2;
  95. int res, n, t, i, norm, neg;
  96. /* is divisor zero ? */
  97. if (mp_iszero (b) == 1) {
  98. return MP_VAL;
  99. }
  100. /* if a < b then q=0, r = a */
  101. if (mp_cmp_mag (a, b) == MP_LT) {
  102. if (d != NULL) {
  103. res = mp_copy (a, d);
  104. } else {
  105. res = MP_OKAY;
  106. }
  107. if (c != NULL) {
  108. mp_zero (c);
  109. }
  110. return res;
  111. }
  112. if ((res = mp_init_size (&q, a->used + 2)) != MP_OKAY) {
  113. return res;
  114. }
  115. q.used = a->used + 2;
  116. if ((res = mp_init (&t1)) != MP_OKAY) {
  117. goto LBL_Q;
  118. }
  119. if ((res = mp_init (&t2)) != MP_OKAY) {
  120. goto LBL_T1;
  121. }
  122. if ((res = mp_init_copy (&x, a)) != MP_OKAY) {
  123. goto LBL_T2;
  124. }
  125. if ((res = mp_init_copy (&y, b)) != MP_OKAY) {
  126. goto LBL_X;
  127. }
  128. /* fix the sign */
  129. neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
  130. x.sign = y.sign = MP_ZPOS;
  131. /* normalize both x and y, ensure that y >= b/2, [b == 2**DIGIT_BIT] */
  132. norm = mp_count_bits(&y) % DIGIT_BIT;
  133. if (norm < (int)(DIGIT_BIT-1)) {
  134. norm = (DIGIT_BIT-1) - norm;
  135. if ((res = mp_mul_2d (&x, norm, &x)) != MP_OKAY) {
  136. goto LBL_Y;
  137. }
  138. if ((res = mp_mul_2d (&y, norm, &y)) != MP_OKAY) {
  139. goto LBL_Y;
  140. }
  141. } else {
  142. norm = 0;
  143. }
  144. /* note hac does 0 based, so if used==5 then its 0,1,2,3,4, e.g. use 4 */
  145. n = x.used - 1;
  146. t = y.used - 1;
  147. /* while (x >= y*b**n-t) do { q[n-t] += 1; x -= y*b**{n-t} } */
  148. if ((res = mp_lshd (&y, n - t)) != MP_OKAY) { /* y = y*b**{n-t} */
  149. goto LBL_Y;
  150. }
  151. while (mp_cmp (&x, &y) != MP_LT) {
  152. ++(q.dp[n - t]);
  153. if ((res = mp_sub (&x, &y, &x)) != MP_OKAY) {
  154. goto LBL_Y;
  155. }
  156. }
  157. /* reset y by shifting it back down */
  158. mp_rshd (&y, n - t);
  159. /* step 3. for i from n down to (t + 1) */
  160. for (i = n; i >= (t + 1); i--) {
  161. if (i > x.used) {
  162. continue;
  163. }
  164. /* step 3.1 if xi == yt then set q{i-t-1} to b-1,
  165. * otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */
  166. if (x.dp[i] == y.dp[t]) {
  167. q.dp[i - t - 1] = ((((mp_digit)1) << DIGIT_BIT) - 1);
  168. } else {
  169. mp_word tmp;
  170. tmp = ((mp_word) x.dp[i]) << ((mp_word) DIGIT_BIT);
  171. tmp |= ((mp_word) x.dp[i - 1]);
  172. tmp /= ((mp_word) y.dp[t]);
  173. if (tmp > (mp_word) MP_MASK)
  174. tmp = MP_MASK;
  175. q.dp[i - t - 1] = (mp_digit) (tmp & (mp_word) (MP_MASK));
  176. }
  177. /* while (q{i-t-1} * (yt * b + y{t-1})) >
  178. xi * b**2 + xi-1 * b + xi-2
  179. do q{i-t-1} -= 1;
  180. */
  181. q.dp[i - t - 1] = (q.dp[i - t - 1] + 1) & MP_MASK;
  182. do {
  183. q.dp[i - t - 1] = (q.dp[i - t - 1] - 1) & MP_MASK;
  184. /* find left hand */
  185. mp_zero (&t1);
  186. t1.dp[0] = (t - 1 < 0) ? 0 : y.dp[t - 1];
  187. t1.dp[1] = y.dp[t];
  188. t1.used = 2;
  189. if ((res = mp_mul_d (&t1, q.dp[i - t - 1], &t1)) != MP_OKAY) {
  190. goto LBL_Y;
  191. }
  192. /* find right hand */
  193. t2.dp[0] = (i - 2 < 0) ? 0 : x.dp[i - 2];
  194. t2.dp[1] = (i - 1 < 0) ? 0 : x.dp[i - 1];
  195. t2.dp[2] = x.dp[i];
  196. t2.used = 3;
  197. } while (mp_cmp_mag(&t1, &t2) == MP_GT);
  198. /* step 3.3 x = x - q{i-t-1} * y * b**{i-t-1} */
  199. if ((res = mp_mul_d (&y, q.dp[i - t - 1], &t1)) != MP_OKAY) {
  200. goto LBL_Y;
  201. }
  202. if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) {
  203. goto LBL_Y;
  204. }
  205. if ((res = mp_sub (&x, &t1, &x)) != MP_OKAY) {
  206. goto LBL_Y;
  207. }
  208. /* if x < 0 then { x = x + y*b**{i-t-1}; q{i-t-1} -= 1; } */
  209. if (x.sign == MP_NEG) {
  210. if ((res = mp_copy (&y, &t1)) != MP_OKAY) {
  211. goto LBL_Y;
  212. }
  213. if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) {
  214. goto LBL_Y;
  215. }
  216. if ((res = mp_add (&x, &t1, &x)) != MP_OKAY) {
  217. goto LBL_Y;
  218. }
  219. q.dp[i - t - 1] = (q.dp[i - t - 1] - 1UL) & MP_MASK;
  220. }
  221. }
  222. /* now q is the quotient and x is the remainder
  223. * [which we have to normalize]
  224. */
  225. /* get sign before writing to c */
  226. x.sign = x.used == 0 ? MP_ZPOS : a->sign;
  227. if (c != NULL) {
  228. mp_clamp (&q);
  229. mp_exch (&q, c);
  230. c->sign = neg;
  231. }
  232. if (d != NULL) {
  233. if ((res = mp_div_2d (&x, norm, &x, NULL)) != MP_OKAY) {
  234. goto LBL_Y;
  235. }
  236. mp_exch (&x, d);
  237. }
  238. res = MP_OKAY;
  239. LBL_Y:mp_clear (&y);
  240. LBL_X:mp_clear (&x);
  241. LBL_T2:mp_clear (&t2);
  242. LBL_T1:mp_clear (&t1);
  243. LBL_Q:mp_clear (&q);
  244. return res;
  245. }
  246. #endif
  247. #endif
  248. /* $Source: /cvs/libtom/libtommath/bn_mp_div.c,v $ */
  249. /* $Revision: 1.3 $ */
  250. /* $Date: 2006/03/31 14:18:44 $ */