mpi-div.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. memset(marker, 0, sizeof(marker));
  131. /* Ensure space is enough for quotient and remainder.
  132. * We need space for an extra limb in the remainder, because it's
  133. * up-shifted (normalized) below. */
  134. rsize = nsize + 1;
  135. if (mpi_resize(rem, rsize) < 0)
  136. goto nomem;
  137. qsize = rsize - dsize; /* qsize cannot be bigger than this. */
  138. if (qsize <= 0) {
  139. if (num != rem) {
  140. rem->nlimbs = num->nlimbs;
  141. rem->sign = num->sign;
  142. MPN_COPY(rem->d, num->d, nsize);
  143. }
  144. if (quot) {
  145. /* This needs to follow the assignment to rem, in case the
  146. * numerator and quotient are the same. */
  147. quot->nlimbs = 0;
  148. quot->sign = 0;
  149. }
  150. return 0;
  151. }
  152. if (quot)
  153. if (mpi_resize(quot, qsize) < 0)
  154. goto nomem;
  155. /* Read pointers here, when reallocation is finished. */
  156. np = num->d;
  157. dp = den->d;
  158. rp = rem->d;
  159. /* Optimize division by a single-limb divisor. */
  160. if (dsize == 1) {
  161. mpi_limb_t rlimb;
  162. if (quot) {
  163. qp = quot->d;
  164. rlimb = mpihelp_divmod_1(qp, np, nsize, dp[0]);
  165. qsize -= qp[qsize - 1] == 0;
  166. quot->nlimbs = qsize;
  167. quot->sign = sign_quotient;
  168. } else
  169. rlimb = mpihelp_mod_1(np, nsize, dp[0]);
  170. rp[0] = rlimb;
  171. rsize = rlimb != 0 ? 1 : 0;
  172. rem->nlimbs = rsize;
  173. rem->sign = sign_remainder;
  174. return 0;
  175. }
  176. if (quot) {
  177. qp = quot->d;
  178. /* Make sure QP and NP point to different objects. Otherwise the
  179. * numerator would be gradually overwritten by the quotient limbs. */
  180. if (qp == np) { /* Copy NP object to temporary space. */
  181. np = marker[markidx++] = mpi_alloc_limb_space(nsize);
  182. MPN_COPY(np, qp, nsize);
  183. }
  184. } else /* Put quotient at top of remainder. */
  185. qp = rp + dsize;
  186. count_leading_zeros(normalization_steps, dp[dsize - 1]);
  187. /* Normalize the denominator, i.e. make its most significant bit set by
  188. * shifting it NORMALIZATION_STEPS bits to the left. Also shift the
  189. * numerator the same number of steps (to keep the quotient the same!).
  190. */
  191. if (normalization_steps) {
  192. mpi_ptr_t tp;
  193. mpi_limb_t nlimb;
  194. /* Shift up the denominator setting the most significant bit of
  195. * the most significant word. Use temporary storage not to clobber
  196. * the original contents of the denominator. */
  197. tp = marker[markidx++] = mpi_alloc_limb_space(dsize);
  198. if (!tp)
  199. goto nomem;
  200. mpihelp_lshift(tp, dp, dsize, normalization_steps);
  201. dp = tp;
  202. /* Shift up the numerator, possibly introducing a new most
  203. * significant word. Move the shifted numerator in the remainder
  204. * meanwhile. */
  205. nlimb = mpihelp_lshift(rp, np, nsize, normalization_steps);
  206. if (nlimb) {
  207. rp[nsize] = nlimb;
  208. rsize = nsize + 1;
  209. } else
  210. rsize = nsize;
  211. } else {
  212. /* The denominator is already normalized, as required. Copy it to
  213. * temporary space if it overlaps with the quotient or remainder. */
  214. if (dp == rp || (quot && (dp == qp))) {
  215. mpi_ptr_t tp;
  216. tp = marker[markidx++] = mpi_alloc_limb_space(dsize);
  217. if (!tp)
  218. goto nomem;
  219. MPN_COPY(tp, dp, dsize);
  220. dp = tp;
  221. }
  222. /* Move the numerator to the remainder. */
  223. if (rp != np)
  224. MPN_COPY(rp, np, nsize);
  225. rsize = nsize;
  226. }
  227. q_limb = mpihelp_divrem(qp, 0, rp, rsize, dp, dsize);
  228. if (quot) {
  229. qsize = rsize - dsize;
  230. if (q_limb) {
  231. qp[qsize] = q_limb;
  232. qsize += 1;
  233. }
  234. quot->nlimbs = qsize;
  235. quot->sign = sign_quotient;
  236. }
  237. rsize = dsize;
  238. MPN_NORMALIZE(rp, rsize);
  239. if (normalization_steps && rsize) {
  240. mpihelp_rshift(rp, rp, rsize, normalization_steps);
  241. rsize -= rp[rsize - 1] == 0 ? 1 : 0;
  242. }
  243. rem->nlimbs = rsize;
  244. rem->sign = sign_remainder;
  245. rc = 0;
  246. nomem:
  247. while (markidx)
  248. mpi_free_limb_space(marker[--markidx]);
  249. return rc;
  250. }
  251. int mpi_tdiv_q_2exp(MPI w, MPI u, unsigned count)
  252. {
  253. mpi_size_t usize, wsize;
  254. mpi_size_t limb_cnt;
  255. usize = u->nlimbs;
  256. limb_cnt = count / BITS_PER_MPI_LIMB;
  257. wsize = usize - limb_cnt;
  258. if (limb_cnt >= usize)
  259. w->nlimbs = 0;
  260. else {
  261. mpi_ptr_t wp;
  262. mpi_ptr_t up;
  263. if (RESIZE_IF_NEEDED(w, wsize) < 0)
  264. return -ENOMEM;
  265. wp = w->d;
  266. up = u->d;
  267. count %= BITS_PER_MPI_LIMB;
  268. if (count) {
  269. mpihelp_rshift(wp, up + limb_cnt, wsize, count);
  270. wsize -= !wp[wsize - 1];
  271. } else {
  272. MPN_COPY_INCR(wp, up + limb_cnt, wsize);
  273. }
  274. w->nlimbs = wsize;
  275. }
  276. return 0;
  277. }
  278. /****************
  279. * Check whether dividend is divisible by divisor
  280. * (note: divisor must fit into a limb)
  281. */
  282. int mpi_divisible_ui(MPI dividend, ulong divisor)
  283. {
  284. return !mpihelp_mod_1(dividend->d, dividend->nlimbs, divisor);
  285. }