mpi-bit.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* mpi-bit.c - MPI bit level functions
  2. * Copyright (C) 1998, 1999 Free Software Foundation, Inc.
  3. *
  4. * This file is part of GnuPG.
  5. *
  6. * GnuPG is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GnuPG is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  19. */
  20. #include "mpi-internal.h"
  21. #include "longlong.h"
  22. #define A_LIMB_1 ((mpi_limb_t) 1)
  23. /****************
  24. * Sometimes we have MSL (most significant limbs) which are 0;
  25. * this is for some reasons not good, so this function removes them.
  26. */
  27. void mpi_normalize(MPI a)
  28. {
  29. for (; a->nlimbs && !a->d[a->nlimbs - 1]; a->nlimbs--)
  30. ;
  31. }
  32. EXPORT_SYMBOL_GPL(mpi_normalize);
  33. /****************
  34. * Return the number of bits in A.
  35. */
  36. unsigned mpi_get_nbits(MPI a)
  37. {
  38. unsigned n;
  39. mpi_normalize(a);
  40. if (a->nlimbs) {
  41. mpi_limb_t alimb = a->d[a->nlimbs - 1];
  42. if (alimb)
  43. n = count_leading_zeros(alimb);
  44. else
  45. n = BITS_PER_MPI_LIMB;
  46. n = BITS_PER_MPI_LIMB - n + (a->nlimbs - 1) * BITS_PER_MPI_LIMB;
  47. } else
  48. n = 0;
  49. return n;
  50. }
  51. EXPORT_SYMBOL_GPL(mpi_get_nbits);
  52. /****************
  53. * Test whether bit N is set.
  54. */
  55. int mpi_test_bit(MPI a, unsigned int n)
  56. {
  57. unsigned int limbno, bitno;
  58. mpi_limb_t limb;
  59. limbno = n / BITS_PER_MPI_LIMB;
  60. bitno = n % BITS_PER_MPI_LIMB;
  61. if (limbno >= a->nlimbs)
  62. return 0; /* too far left: this is a 0 */
  63. limb = a->d[limbno];
  64. return (limb & (A_LIMB_1 << bitno)) ? 1 : 0;
  65. }
  66. EXPORT_SYMBOL_GPL(mpi_test_bit);
  67. /****************
  68. * Set bit N of A.
  69. */
  70. void mpi_set_bit(MPI a, unsigned int n)
  71. {
  72. unsigned int i, limbno, bitno;
  73. limbno = n / BITS_PER_MPI_LIMB;
  74. bitno = n % BITS_PER_MPI_LIMB;
  75. if (limbno >= a->nlimbs) {
  76. for (i = a->nlimbs; i < a->alloced; i++)
  77. a->d[i] = 0;
  78. mpi_resize(a, limbno+1);
  79. a->nlimbs = limbno+1;
  80. }
  81. a->d[limbno] |= (A_LIMB_1<<bitno);
  82. }
  83. /****************
  84. * Set bit N of A. and clear all bits above
  85. */
  86. void mpi_set_highbit(MPI a, unsigned int n)
  87. {
  88. unsigned int i, limbno, bitno;
  89. limbno = n / BITS_PER_MPI_LIMB;
  90. bitno = n % BITS_PER_MPI_LIMB;
  91. if (limbno >= a->nlimbs) {
  92. for (i = a->nlimbs; i < a->alloced; i++)
  93. a->d[i] = 0;
  94. mpi_resize(a, limbno+1);
  95. a->nlimbs = limbno+1;
  96. }
  97. a->d[limbno] |= (A_LIMB_1<<bitno);
  98. for (bitno++; bitno < BITS_PER_MPI_LIMB; bitno++)
  99. a->d[limbno] &= ~(A_LIMB_1 << bitno);
  100. a->nlimbs = limbno+1;
  101. }
  102. EXPORT_SYMBOL_GPL(mpi_set_highbit);
  103. /****************
  104. * clear bit N of A and all bits above
  105. */
  106. void mpi_clear_highbit(MPI a, unsigned int n)
  107. {
  108. unsigned int limbno, bitno;
  109. limbno = n / BITS_PER_MPI_LIMB;
  110. bitno = n % BITS_PER_MPI_LIMB;
  111. if (limbno >= a->nlimbs)
  112. return; /* not allocated, therefore no need to clear bits :-) */
  113. for ( ; bitno < BITS_PER_MPI_LIMB; bitno++)
  114. a->d[limbno] &= ~(A_LIMB_1 << bitno);
  115. a->nlimbs = limbno+1;
  116. }
  117. /****************
  118. * Clear bit N of A.
  119. */
  120. void mpi_clear_bit(MPI a, unsigned int n)
  121. {
  122. unsigned int limbno, bitno;
  123. limbno = n / BITS_PER_MPI_LIMB;
  124. bitno = n % BITS_PER_MPI_LIMB;
  125. if (limbno >= a->nlimbs)
  126. return; /* Don't need to clear this bit, it's far too left. */
  127. a->d[limbno] &= ~(A_LIMB_1 << bitno);
  128. }
  129. EXPORT_SYMBOL_GPL(mpi_clear_bit);
  130. /****************
  131. * Shift A by COUNT limbs to the right
  132. * This is used only within the MPI library
  133. */
  134. void mpi_rshift_limbs(MPI a, unsigned int count)
  135. {
  136. mpi_ptr_t ap = a->d;
  137. mpi_size_t n = a->nlimbs;
  138. unsigned int i;
  139. if (count >= n) {
  140. a->nlimbs = 0;
  141. return;
  142. }
  143. for (i = 0; i < n - count; i++)
  144. ap[i] = ap[i+count];
  145. ap[i] = 0;
  146. a->nlimbs -= count;
  147. }
  148. /*
  149. * Shift A by N bits to the right.
  150. */
  151. void mpi_rshift(MPI x, MPI a, unsigned int n)
  152. {
  153. mpi_size_t xsize;
  154. unsigned int i;
  155. unsigned int nlimbs = (n/BITS_PER_MPI_LIMB);
  156. unsigned int nbits = (n%BITS_PER_MPI_LIMB);
  157. if (x == a) {
  158. /* In-place operation. */
  159. if (nlimbs >= x->nlimbs) {
  160. x->nlimbs = 0;
  161. return;
  162. }
  163. if (nlimbs) {
  164. for (i = 0; i < x->nlimbs - nlimbs; i++)
  165. x->d[i] = x->d[i+nlimbs];
  166. x->d[i] = 0;
  167. x->nlimbs -= nlimbs;
  168. }
  169. if (x->nlimbs && nbits)
  170. mpihelp_rshift(x->d, x->d, x->nlimbs, nbits);
  171. } else if (nlimbs) {
  172. /* Copy and shift by more or equal bits than in a limb. */
  173. xsize = a->nlimbs;
  174. x->sign = a->sign;
  175. RESIZE_IF_NEEDED(x, xsize);
  176. x->nlimbs = xsize;
  177. for (i = 0; i < a->nlimbs; i++)
  178. x->d[i] = a->d[i];
  179. x->nlimbs = i;
  180. if (nlimbs >= x->nlimbs) {
  181. x->nlimbs = 0;
  182. return;
  183. }
  184. if (nlimbs) {
  185. for (i = 0; i < x->nlimbs - nlimbs; i++)
  186. x->d[i] = x->d[i+nlimbs];
  187. x->d[i] = 0;
  188. x->nlimbs -= nlimbs;
  189. }
  190. if (x->nlimbs && nbits)
  191. mpihelp_rshift(x->d, x->d, x->nlimbs, nbits);
  192. } else {
  193. /* Copy and shift by less than bits in a limb. */
  194. xsize = a->nlimbs;
  195. x->sign = a->sign;
  196. RESIZE_IF_NEEDED(x, xsize);
  197. x->nlimbs = xsize;
  198. if (xsize) {
  199. if (nbits)
  200. mpihelp_rshift(x->d, a->d, x->nlimbs, nbits);
  201. else {
  202. /* The rshift helper function is not specified for
  203. * NBITS==0, thus we do a plain copy here.
  204. */
  205. for (i = 0; i < x->nlimbs; i++)
  206. x->d[i] = a->d[i];
  207. }
  208. }
  209. }
  210. MPN_NORMALIZE(x->d, x->nlimbs);
  211. }
  212. EXPORT_SYMBOL_GPL(mpi_rshift);
  213. /****************
  214. * Shift A by COUNT limbs to the left
  215. * This is used only within the MPI library
  216. */
  217. void mpi_lshift_limbs(MPI a, unsigned int count)
  218. {
  219. mpi_ptr_t ap;
  220. int n = a->nlimbs;
  221. int i;
  222. if (!count || !n)
  223. return;
  224. RESIZE_IF_NEEDED(a, n+count);
  225. ap = a->d;
  226. for (i = n-1; i >= 0; i--)
  227. ap[i+count] = ap[i];
  228. for (i = 0; i < count; i++)
  229. ap[i] = 0;
  230. a->nlimbs += count;
  231. }
  232. /*
  233. * Shift A by N bits to the left.
  234. */
  235. void mpi_lshift(MPI x, MPI a, unsigned int n)
  236. {
  237. unsigned int nlimbs = (n/BITS_PER_MPI_LIMB);
  238. unsigned int nbits = (n%BITS_PER_MPI_LIMB);
  239. if (x == a && !n)
  240. return; /* In-place shift with an amount of zero. */
  241. if (x != a) {
  242. /* Copy A to X. */
  243. unsigned int alimbs = a->nlimbs;
  244. int asign = a->sign;
  245. mpi_ptr_t xp, ap;
  246. RESIZE_IF_NEEDED(x, alimbs+nlimbs+1);
  247. xp = x->d;
  248. ap = a->d;
  249. MPN_COPY(xp, ap, alimbs);
  250. x->nlimbs = alimbs;
  251. x->flags = a->flags;
  252. x->sign = asign;
  253. }
  254. if (nlimbs && !nbits) {
  255. /* Shift a full number of limbs. */
  256. mpi_lshift_limbs(x, nlimbs);
  257. } else if (n) {
  258. /* We use a very dump approach: Shift left by the number of
  259. * limbs plus one and than fix it up by an rshift.
  260. */
  261. mpi_lshift_limbs(x, nlimbs+1);
  262. mpi_rshift(x, x, BITS_PER_MPI_LIMB - nbits);
  263. }
  264. MPN_NORMALIZE(x->d, x->nlimbs);
  265. }