mpi-div.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /* mpi-div.c - MPI functions
  2. * Copyright (C) 1994, 1996 Free Software Foundation, Inc.
  3. * Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
  4. *
  5. * This file is part of GnuPG.
  6. *
  7. * GnuPG is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * GnuPG is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  20. *
  21. * Note: This code is heavily based on the GNU MP Library.
  22. * Actually it's the same code with only minor changes in the
  23. * way the data is stored; this is to support the abstraction
  24. * of an optional secure memory allocation which may be used
  25. * to avoid revealing of sensitive data due to paging etc.
  26. * The GNU MP Library itself is published under the LGPL;
  27. * however I decided to publish this code under the plain GPL.
  28. */
  29. #include <linux/string.h>
  30. #include "mpi-internal.h"
  31. #include "longlong.h"
  32. int mpi_fdiv_r(MPI rem, MPI dividend, MPI divisor)
  33. {
  34. int rc = -ENOMEM;
  35. int divisor_sign = divisor->sign;
  36. MPI temp_divisor = NULL;
  37. /* We need the original value of the divisor after the remainder has been
  38. * preliminary calculated. We have to copy it to temporary space if it's
  39. * the same variable as REM. */
  40. if (rem == divisor) {
  41. if (mpi_copy(&temp_divisor, divisor) < 0)
  42. goto nomem;
  43. divisor = temp_divisor;
  44. }
  45. if (mpi_tdiv_qr(NULL, rem, dividend, divisor) < 0)
  46. goto nomem;
  47. if (((divisor_sign ? 1 : 0) ^ (dividend->sign ? 1 : 0)) && rem->nlimbs)
  48. if (mpi_add(rem, rem, divisor) < 0)
  49. goto nomem;
  50. rc = 0;
  51. nomem:
  52. if (temp_divisor)
  53. mpi_free(temp_divisor);
  54. return rc;
  55. }
  56. /****************
  57. * Division rounding the quotient towards -infinity.
  58. * The remainder gets the same sign as the denominator.
  59. * rem is optional
  60. */
  61. ulong mpi_fdiv_r_ui(MPI rem, MPI dividend, ulong divisor)
  62. {
  63. mpi_limb_t rlimb;
  64. rlimb = mpihelp_mod_1(dividend->d, dividend->nlimbs, divisor);
  65. if (rlimb && dividend->sign)
  66. rlimb = divisor - rlimb;
  67. if (rem) {
  68. rem->d[0] = rlimb;
  69. rem->nlimbs = rlimb ? 1 : 0;
  70. }
  71. return rlimb;
  72. }
  73. int mpi_fdiv_q(MPI quot, MPI dividend, MPI divisor)
  74. {
  75. MPI tmp = mpi_alloc(mpi_get_nlimbs(quot));
  76. if (!tmp)
  77. return -ENOMEM;
  78. mpi_fdiv_qr(quot, tmp, dividend, divisor);
  79. mpi_free(tmp);
  80. return 0;
  81. }
  82. int mpi_fdiv_qr(MPI quot, MPI rem, MPI dividend, MPI divisor)
  83. {
  84. int divisor_sign = divisor->sign;
  85. MPI temp_divisor = NULL;
  86. if (quot == divisor || rem == divisor) {
  87. if (mpi_copy(&temp_divisor, divisor) < 0)
  88. return -ENOMEM;
  89. divisor = temp_divisor;
  90. }
  91. if (mpi_tdiv_qr(quot, rem, dividend, divisor) < 0)
  92. goto nomem;
  93. if ((divisor_sign ^ dividend->sign) && rem->nlimbs) {
  94. if (mpi_sub_ui(quot, quot, 1) < 0)
  95. goto nomem;
  96. if (mpi_add(rem, rem, divisor) < 0)
  97. goto nomem;
  98. }
  99. if (temp_divisor)
  100. mpi_free(temp_divisor);
  101. return 0;
  102. nomem:
  103. mpi_free(temp_divisor);
  104. return -ENOMEM;
  105. }
  106. /* If den == quot, den needs temporary storage.
  107. * If den == rem, den needs temporary storage.
  108. * If num == quot, num needs temporary storage.
  109. * If den has temporary storage, it can be normalized while being copied,
  110. * i.e no extra storage should be allocated.
  111. */
  112. int mpi_tdiv_r(MPI rem, MPI num, MPI den)
  113. {
  114. return mpi_tdiv_qr(NULL, rem, num, den);
  115. }
  116. int mpi_tdiv_qr(MPI quot, MPI rem, MPI num, MPI den)
  117. {
  118. int rc = -ENOMEM;
  119. mpi_ptr_t np, dp;
  120. mpi_ptr_t qp, rp;
  121. mpi_size_t nsize = num->nlimbs;
  122. mpi_size_t dsize = den->nlimbs;
  123. mpi_size_t qsize, rsize;
  124. mpi_size_t sign_remainder = num->sign;
  125. mpi_size_t sign_quotient = num->sign ^ den->sign;
  126. unsigned normalization_steps;
  127. mpi_limb_t q_limb;
  128. mpi_ptr_t marker[5];
  129. int markidx = 0;
  130. if (!dsize)
  131. return -EINVAL;
  132. memset(marker, 0, sizeof(marker));
  133. /* Ensure space is enough for quotient and remainder.
  134. * We need space for an extra limb in the remainder, because it's
  135. * up-shifted (normalized) below. */
  136. rsize = nsize + 1;
  137. if (mpi_resize(rem, rsize) < 0)
  138. goto nomem;
  139. qsize = rsize - dsize; /* qsize cannot be bigger than this. */
  140. if (qsize <= 0) {
  141. if (num != rem) {
  142. rem->nlimbs = num->nlimbs;
  143. rem->sign = num->sign;
  144. MPN_COPY(rem->d, num->d, nsize);
  145. }
  146. if (quot) {
  147. /* This needs to follow the assignment to rem, in case the
  148. * numerator and quotient are the same. */
  149. quot->nlimbs = 0;
  150. quot->sign = 0;
  151. }
  152. return 0;
  153. }
  154. if (quot)
  155. if (mpi_resize(quot, qsize) < 0)
  156. goto nomem;
  157. /* Read pointers here, when reallocation is finished. */
  158. np = num->d;
  159. dp = den->d;
  160. rp = rem->d;
  161. /* Optimize division by a single-limb divisor. */
  162. if (dsize == 1) {
  163. mpi_limb_t rlimb;
  164. if (quot) {
  165. qp = quot->d;
  166. rlimb = mpihelp_divmod_1(qp, np, nsize, dp[0]);
  167. qsize -= qp[qsize - 1] == 0;
  168. quot->nlimbs = qsize;
  169. quot->sign = sign_quotient;
  170. } else
  171. rlimb = mpihelp_mod_1(np, nsize, dp[0]);
  172. rp[0] = rlimb;
  173. rsize = rlimb != 0 ? 1 : 0;
  174. rem->nlimbs = rsize;
  175. rem->sign = sign_remainder;
  176. return 0;
  177. }
  178. if (quot) {
  179. qp = quot->d;
  180. /* Make sure QP and NP point to different objects. Otherwise the
  181. * numerator would be gradually overwritten by the quotient limbs. */
  182. if (qp == np) { /* Copy NP object to temporary space. */
  183. np = marker[markidx++] = mpi_alloc_limb_space(nsize);
  184. if (!np)
  185. goto nomem;
  186. MPN_COPY(np, qp, nsize);
  187. }
  188. } else /* Put quotient at top of remainder. */
  189. qp = rp + dsize;
  190. count_leading_zeros(normalization_steps, dp[dsize - 1]);
  191. /* Normalize the denominator, i.e. make its most significant bit set by
  192. * shifting it NORMALIZATION_STEPS bits to the left. Also shift the
  193. * numerator the same number of steps (to keep the quotient the same!).
  194. */
  195. if (normalization_steps) {
  196. mpi_ptr_t tp;
  197. mpi_limb_t nlimb;
  198. /* Shift up the denominator setting the most significant bit of
  199. * the most significant word. Use temporary storage not to clobber
  200. * the original contents of the denominator. */
  201. tp = marker[markidx++] = mpi_alloc_limb_space(dsize);
  202. if (!tp)
  203. goto nomem;
  204. mpihelp_lshift(tp, dp, dsize, normalization_steps);
  205. dp = tp;
  206. /* Shift up the numerator, possibly introducing a new most
  207. * significant word. Move the shifted numerator in the remainder
  208. * meanwhile. */
  209. nlimb = mpihelp_lshift(rp, np, nsize, normalization_steps);
  210. if (nlimb) {
  211. rp[nsize] = nlimb;
  212. rsize = nsize + 1;
  213. } else
  214. rsize = nsize;
  215. } else {
  216. /* The denominator is already normalized, as required. Copy it to
  217. * temporary space if it overlaps with the quotient or remainder. */
  218. if (dp == rp || (quot && (dp == qp))) {
  219. mpi_ptr_t tp;
  220. tp = marker[markidx++] = mpi_alloc_limb_space(dsize);
  221. if (!tp)
  222. goto nomem;
  223. MPN_COPY(tp, dp, dsize);
  224. dp = tp;
  225. }
  226. /* Move the numerator to the remainder. */
  227. if (rp != np)
  228. MPN_COPY(rp, np, nsize);
  229. rsize = nsize;
  230. }
  231. q_limb = mpihelp_divrem(qp, 0, rp, rsize, dp, dsize);
  232. if (quot) {
  233. qsize = rsize - dsize;
  234. if (q_limb) {
  235. qp[qsize] = q_limb;
  236. qsize += 1;
  237. }
  238. quot->nlimbs = qsize;
  239. quot->sign = sign_quotient;
  240. }
  241. rsize = dsize;
  242. MPN_NORMALIZE(rp, rsize);
  243. if (normalization_steps && rsize) {
  244. mpihelp_rshift(rp, rp, rsize, normalization_steps);
  245. rsize -= rp[rsize - 1] == 0 ? 1 : 0;
  246. }
  247. rem->nlimbs = rsize;
  248. rem->sign = sign_remainder;
  249. rc = 0;
  250. nomem:
  251. while (markidx)
  252. mpi_free_limb_space(marker[--markidx]);
  253. return rc;
  254. }
  255. int mpi_tdiv_q_2exp(MPI w, MPI u, unsigned count)
  256. {
  257. mpi_size_t usize, wsize;
  258. mpi_size_t limb_cnt;
  259. usize = u->nlimbs;
  260. limb_cnt = count / BITS_PER_MPI_LIMB;
  261. wsize = usize - limb_cnt;
  262. if (limb_cnt >= usize)
  263. w->nlimbs = 0;
  264. else {
  265. mpi_ptr_t wp;
  266. mpi_ptr_t up;
  267. if (RESIZE_IF_NEEDED(w, wsize) < 0)
  268. return -ENOMEM;
  269. wp = w->d;
  270. up = u->d;
  271. count %= BITS_PER_MPI_LIMB;
  272. if (count) {
  273. mpihelp_rshift(wp, up + limb_cnt, wsize, count);
  274. wsize -= !wp[wsize - 1];
  275. } else {
  276. MPN_COPY_INCR(wp, up + limb_cnt, wsize);
  277. }
  278. w->nlimbs = wsize;
  279. }
  280. return 0;
  281. }
  282. /****************
  283. * Check whether dividend is divisible by divisor
  284. * (note: divisor must fit into a limb)
  285. */
  286. int mpi_divisible_ui(MPI dividend, ulong divisor)
  287. {
  288. return !mpihelp_mod_1(dividend->d, dividend->nlimbs, divisor);
  289. }