mpi-bit.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* mpi-bit.c - MPI bit level fucntions
  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. const unsigned char __clz_tab[] = {
  23. 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
  24. 5, 5, 5, 5, 5, 5, 5, 5,
  25. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  26. 6, 6, 6, 6, 6, 6, 6, 6,
  27. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  28. 7, 7, 7, 7, 7, 7, 7, 7,
  29. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  30. 7, 7, 7, 7, 7, 7, 7, 7,
  31. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  32. 8, 8, 8, 8, 8, 8, 8, 8,
  33. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  34. 8, 8, 8, 8, 8, 8, 8, 8,
  35. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  36. 8, 8, 8, 8, 8, 8, 8, 8,
  37. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  38. 8, 8, 8, 8, 8, 8, 8, 8,
  39. };
  40. #define A_LIMB_1 ((mpi_limb_t) 1)
  41. /****************
  42. * Sometimes we have MSL (most significant limbs) which are 0;
  43. * this is for some reasons not good, so this function removes them.
  44. */
  45. void mpi_normalize(MPI a)
  46. {
  47. for (; a->nlimbs && !a->d[a->nlimbs - 1]; a->nlimbs--)
  48. ;
  49. }
  50. /****************
  51. * Return the number of bits in A.
  52. */
  53. unsigned mpi_get_nbits(MPI a)
  54. {
  55. unsigned n;
  56. mpi_normalize(a);
  57. if (a->nlimbs) {
  58. mpi_limb_t alimb = a->d[a->nlimbs - 1];
  59. if (alimb)
  60. count_leading_zeros(n, alimb);
  61. else
  62. n = BITS_PER_MPI_LIMB;
  63. n = BITS_PER_MPI_LIMB - n + (a->nlimbs - 1) * BITS_PER_MPI_LIMB;
  64. } else
  65. n = 0;
  66. return n;
  67. }
  68. EXPORT_SYMBOL_GPL(mpi_get_nbits);
  69. /****************
  70. * Test whether bit N is set.
  71. */
  72. int mpi_test_bit(MPI a, unsigned n)
  73. {
  74. unsigned limbno, bitno;
  75. mpi_limb_t limb;
  76. limbno = n / BITS_PER_MPI_LIMB;
  77. bitno = n % BITS_PER_MPI_LIMB;
  78. if (limbno >= a->nlimbs)
  79. return 0; /* too far left: this is a 0 */
  80. limb = a->d[limbno];
  81. return (limb & (A_LIMB_1 << bitno)) ? 1 : 0;
  82. }
  83. /****************
  84. * Set bit N of A.
  85. */
  86. int mpi_set_bit(MPI a, unsigned n)
  87. {
  88. unsigned limbno, bitno;
  89. limbno = n / BITS_PER_MPI_LIMB;
  90. bitno = n % BITS_PER_MPI_LIMB;
  91. if (limbno >= a->nlimbs) { /* resize */
  92. if (a->alloced >= limbno)
  93. if (mpi_resize(a, limbno + 1) < 0)
  94. return -ENOMEM;
  95. a->nlimbs = limbno + 1;
  96. }
  97. a->d[limbno] |= (A_LIMB_1 << bitno);
  98. return 0;
  99. }
  100. /****************
  101. * Set bit N of A. and clear all bits above
  102. */
  103. int mpi_set_highbit(MPI a, unsigned n)
  104. {
  105. unsigned limbno, bitno;
  106. limbno = n / BITS_PER_MPI_LIMB;
  107. bitno = n % BITS_PER_MPI_LIMB;
  108. if (limbno >= a->nlimbs) { /* resize */
  109. if (a->alloced >= limbno)
  110. if (mpi_resize(a, limbno + 1) < 0)
  111. return -ENOMEM;
  112. a->nlimbs = limbno + 1;
  113. }
  114. a->d[limbno] |= (A_LIMB_1 << bitno);
  115. for (bitno++; bitno < BITS_PER_MPI_LIMB; bitno++)
  116. a->d[limbno] &= ~(A_LIMB_1 << bitno);
  117. a->nlimbs = limbno + 1;
  118. return 0;
  119. }
  120. /****************
  121. * clear bit N of A and all bits above
  122. */
  123. void mpi_clear_highbit(MPI a, unsigned n)
  124. {
  125. unsigned limbno, bitno;
  126. limbno = n / BITS_PER_MPI_LIMB;
  127. bitno = n % BITS_PER_MPI_LIMB;
  128. if (limbno >= a->nlimbs)
  129. return; /* not allocated, so need to clear bits :-) */
  130. for (; bitno < BITS_PER_MPI_LIMB; bitno++)
  131. a->d[limbno] &= ~(A_LIMB_1 << bitno);
  132. a->nlimbs = limbno + 1;
  133. }
  134. /****************
  135. * Clear bit N of A.
  136. */
  137. void mpi_clear_bit(MPI a, unsigned n)
  138. {
  139. unsigned limbno, bitno;
  140. limbno = n / BITS_PER_MPI_LIMB;
  141. bitno = n % BITS_PER_MPI_LIMB;
  142. if (limbno >= a->nlimbs)
  143. return; /* don't need to clear this bit, it's to far to left */
  144. a->d[limbno] &= ~(A_LIMB_1 << bitno);
  145. }
  146. /****************
  147. * Shift A by N bits to the right
  148. * FIXME: should use alloc_limb if X and A are same.
  149. */
  150. int mpi_rshift(MPI x, MPI a, unsigned n)
  151. {
  152. mpi_ptr_t xp;
  153. mpi_size_t xsize;
  154. xsize = a->nlimbs;
  155. x->sign = a->sign;
  156. if (RESIZE_IF_NEEDED(x, (size_t) xsize) < 0)
  157. return -ENOMEM;
  158. xp = x->d;
  159. if (xsize) {
  160. mpihelp_rshift(xp, a->d, xsize, n);
  161. MPN_NORMALIZE(xp, xsize);
  162. }
  163. x->nlimbs = xsize;
  164. return 0;
  165. }
  166. /****************
  167. * Shift A by COUNT limbs to the left
  168. * This is used only within the MPI library
  169. */
  170. int mpi_lshift_limbs(MPI a, unsigned int count)
  171. {
  172. mpi_ptr_t ap = a->d;
  173. int n = a->nlimbs;
  174. int i;
  175. if (!count || !n)
  176. return 0;
  177. if (RESIZE_IF_NEEDED(a, n + count) < 0)
  178. return -ENOMEM;
  179. for (i = n - 1; i >= 0; i--)
  180. ap[i + count] = ap[i];
  181. for (i = 0; i < count; i++)
  182. ap[i] = 0;
  183. a->nlimbs += count;
  184. return 0;
  185. }
  186. /****************
  187. * Shift A by COUNT limbs to the right
  188. * This is used only within the MPI library
  189. */
  190. void mpi_rshift_limbs(MPI a, unsigned int count)
  191. {
  192. mpi_ptr_t ap = a->d;
  193. mpi_size_t n = a->nlimbs;
  194. unsigned int i;
  195. if (count >= n) {
  196. a->nlimbs = 0;
  197. return;
  198. }
  199. for (i = 0; i < n - count; i++)
  200. ap[i] = ap[i + count];
  201. ap[i] = 0;
  202. a->nlimbs -= count;
  203. }