mpi-bit.c 4.6 KB

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