mpi-bit.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 <linux/export.h>
  21. #include "mpi-internal.h"
  22. #include "longlong.h"
  23. #define A_LIMB_1 ((mpi_limb_t) 1)
  24. /****************
  25. * Sometimes we have MSL (most significant limbs) which are 0;
  26. * this is for some reasons not good, so this function removes them.
  27. */
  28. void mpi_normalize(MPI a)
  29. {
  30. for (; a->nlimbs && !a->d[a->nlimbs - 1]; a->nlimbs--)
  31. ;
  32. }
  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. int mpi_set_bit(MPI a, unsigned int n)
  71. {
  72. unsigned int i, limbno, bitno;
  73. int err;
  74. limbno = n / BITS_PER_MPI_LIMB;
  75. bitno = n % BITS_PER_MPI_LIMB;
  76. if (limbno >= a->nlimbs) {
  77. for (i = a->nlimbs; i < a->alloced; i++)
  78. a->d[i] = 0;
  79. err = mpi_resize(a, limbno+1);
  80. if (err)
  81. return err;
  82. a->nlimbs = limbno+1;
  83. }
  84. a->d[limbno] |= (A_LIMB_1<<bitno);
  85. return 0;
  86. }
  87. EXPORT_SYMBOL_GPL(mpi_set_bit);
  88. /*
  89. * Shift A by N bits to the right.
  90. */
  91. int mpi_rshift(MPI x, MPI a, unsigned int n)
  92. {
  93. mpi_size_t xsize;
  94. unsigned int i;
  95. unsigned int nlimbs = (n/BITS_PER_MPI_LIMB);
  96. unsigned int nbits = (n%BITS_PER_MPI_LIMB);
  97. int err;
  98. if (x == a) {
  99. /* In-place operation. */
  100. if (nlimbs >= x->nlimbs) {
  101. x->nlimbs = 0;
  102. return 0;
  103. }
  104. if (nlimbs) {
  105. for (i = 0; i < x->nlimbs - nlimbs; i++)
  106. x->d[i] = x->d[i+nlimbs];
  107. x->d[i] = 0;
  108. x->nlimbs -= nlimbs;
  109. }
  110. if (x->nlimbs && nbits)
  111. mpihelp_rshift(x->d, x->d, x->nlimbs, nbits);
  112. } else if (nlimbs) {
  113. /* Copy and shift by more or equal bits than in a limb. */
  114. xsize = a->nlimbs;
  115. x->sign = a->sign;
  116. err = RESIZE_IF_NEEDED(x, xsize);
  117. if (err)
  118. return err;
  119. x->nlimbs = xsize;
  120. for (i = 0; i < a->nlimbs; i++)
  121. x->d[i] = a->d[i];
  122. x->nlimbs = i;
  123. if (nlimbs >= x->nlimbs) {
  124. x->nlimbs = 0;
  125. return 0;
  126. }
  127. for (i = 0; i < x->nlimbs - nlimbs; i++)
  128. x->d[i] = x->d[i+nlimbs];
  129. x->d[i] = 0;
  130. x->nlimbs -= nlimbs;
  131. if (x->nlimbs && nbits)
  132. mpihelp_rshift(x->d, x->d, x->nlimbs, nbits);
  133. } else {
  134. /* Copy and shift by less than bits in a limb. */
  135. xsize = a->nlimbs;
  136. x->sign = a->sign;
  137. err = RESIZE_IF_NEEDED(x, xsize);
  138. if (err)
  139. return err;
  140. x->nlimbs = xsize;
  141. if (xsize) {
  142. if (nbits)
  143. mpihelp_rshift(x->d, a->d, x->nlimbs, nbits);
  144. else {
  145. /* The rshift helper function is not specified for
  146. * NBITS==0, thus we do a plain copy here.
  147. */
  148. for (i = 0; i < x->nlimbs; i++)
  149. x->d[i] = a->d[i];
  150. }
  151. }
  152. }
  153. MPN_NORMALIZE(x->d, x->nlimbs);
  154. return 0;
  155. }
  156. EXPORT_SYMBOL_GPL(mpi_rshift);