bn_s_mp_exptmod.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #include <tommath.h>
  2. #ifdef BN_S_MP_EXPTMOD_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis
  4. *
  5. * LibTomMath is a library that provides multiple-precision
  6. * integer arithmetic as well as number theoretic functionality.
  7. *
  8. * The library was designed directly after the MPI library by
  9. * Michael Fromberger but has been written from scratch with
  10. * additional optimizations in place.
  11. *
  12. * The library is free for all purposes without any express
  13. * guarantee it works.
  14. *
  15. * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
  16. */
  17. #ifdef MP_LOW_MEM
  18. #define TAB_SIZE 32
  19. #else
  20. #define TAB_SIZE 256
  21. #endif
  22. int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
  23. {
  24. mp_int M[TAB_SIZE], res, mu;
  25. mp_digit buf;
  26. int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
  27. int (*redux)(mp_int*,mp_int*,mp_int*);
  28. /* find window size */
  29. x = mp_count_bits (X);
  30. if (x <= 7) {
  31. winsize = 2;
  32. } else if (x <= 36) {
  33. winsize = 3;
  34. } else if (x <= 140) {
  35. winsize = 4;
  36. } else if (x <= 450) {
  37. winsize = 5;
  38. } else if (x <= 1303) {
  39. winsize = 6;
  40. } else if (x <= 3529) {
  41. winsize = 7;
  42. } else {
  43. winsize = 8;
  44. }
  45. #ifdef MP_LOW_MEM
  46. if (winsize > 5) {
  47. winsize = 5;
  48. }
  49. #endif
  50. /* init M array */
  51. /* init first cell */
  52. if ((err = mp_init(&M[1])) != MP_OKAY) {
  53. return err;
  54. }
  55. /* now init the second half of the array */
  56. for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
  57. if ((err = mp_init(&M[x])) != MP_OKAY) {
  58. for (y = 1<<(winsize-1); y < x; y++) {
  59. mp_clear (&M[y]);
  60. }
  61. mp_clear(&M[1]);
  62. return err;
  63. }
  64. }
  65. /* create mu, used for Barrett reduction */
  66. if ((err = mp_init (&mu)) != MP_OKAY) {
  67. goto LBL_M;
  68. }
  69. if (redmode == 0) {
  70. if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) {
  71. goto LBL_MU;
  72. }
  73. redux = mp_reduce;
  74. } else {
  75. if ((err = mp_reduce_2k_setup_l (P, &mu)) != MP_OKAY) {
  76. goto LBL_MU;
  77. }
  78. redux = mp_reduce_2k_l;
  79. }
  80. /* create M table
  81. *
  82. * The M table contains powers of the base,
  83. * e.g. M[x] = G**x mod P
  84. *
  85. * The first half of the table is not
  86. * computed though accept for M[0] and M[1]
  87. */
  88. if ((err = mp_mod (G, P, &M[1])) != MP_OKAY) {
  89. goto LBL_MU;
  90. }
  91. /* compute the value at M[1<<(winsize-1)] by squaring
  92. * M[1] (winsize-1) times
  93. */
  94. if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) {
  95. goto LBL_MU;
  96. }
  97. for (x = 0; x < (winsize - 1); x++) {
  98. /* square it */
  99. if ((err = mp_sqr (&M[1 << (winsize - 1)],
  100. &M[1 << (winsize - 1)])) != MP_OKAY) {
  101. goto LBL_MU;
  102. }
  103. /* reduce modulo P */
  104. if ((err = redux (&M[1 << (winsize - 1)], P, &mu)) != MP_OKAY) {
  105. goto LBL_MU;
  106. }
  107. }
  108. /* create upper table, that is M[x] = M[x-1] * M[1] (mod P)
  109. * for x = (2**(winsize - 1) + 1) to (2**winsize - 1)
  110. */
  111. for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) {
  112. if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) {
  113. goto LBL_MU;
  114. }
  115. if ((err = redux (&M[x], P, &mu)) != MP_OKAY) {
  116. goto LBL_MU;
  117. }
  118. }
  119. /* setup result */
  120. if ((err = mp_init (&res)) != MP_OKAY) {
  121. goto LBL_MU;
  122. }
  123. mp_set (&res, 1);
  124. /* set initial mode and bit cnt */
  125. mode = 0;
  126. bitcnt = 1;
  127. buf = 0;
  128. digidx = X->used - 1;
  129. bitcpy = 0;
  130. bitbuf = 0;
  131. for (;;) {
  132. /* grab next digit as required */
  133. if (--bitcnt == 0) {
  134. /* if digidx == -1 we are out of digits */
  135. if (digidx == -1) {
  136. break;
  137. }
  138. /* read next digit and reset the bitcnt */
  139. buf = X->dp[digidx--];
  140. bitcnt = (int) DIGIT_BIT;
  141. }
  142. /* grab the next msb from the exponent */
  143. y = (buf >> (mp_digit)(DIGIT_BIT - 1)) & 1;
  144. buf <<= (mp_digit)1;
  145. /* if the bit is zero and mode == 0 then we ignore it
  146. * These represent the leading zero bits before the first 1 bit
  147. * in the exponent. Technically this opt is not required but it
  148. * does lower the # of trivial squaring/reductions used
  149. */
  150. if (mode == 0 && y == 0) {
  151. continue;
  152. }
  153. /* if the bit is zero and mode == 1 then we square */
  154. if (mode == 1 && y == 0) {
  155. if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
  156. goto LBL_RES;
  157. }
  158. if ((err = redux (&res, P, &mu)) != MP_OKAY) {
  159. goto LBL_RES;
  160. }
  161. continue;
  162. }
  163. /* else we add it to the window */
  164. bitbuf |= (y << (winsize - ++bitcpy));
  165. mode = 2;
  166. if (bitcpy == winsize) {
  167. /* ok window is filled so square as required and multiply */
  168. /* square first */
  169. for (x = 0; x < winsize; x++) {
  170. if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
  171. goto LBL_RES;
  172. }
  173. if ((err = redux (&res, P, &mu)) != MP_OKAY) {
  174. goto LBL_RES;
  175. }
  176. }
  177. /* then multiply */
  178. if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) {
  179. goto LBL_RES;
  180. }
  181. if ((err = redux (&res, P, &mu)) != MP_OKAY) {
  182. goto LBL_RES;
  183. }
  184. /* empty window and reset */
  185. bitcpy = 0;
  186. bitbuf = 0;
  187. mode = 1;
  188. }
  189. }
  190. /* if bits remain then square/multiply */
  191. if (mode == 2 && bitcpy > 0) {
  192. /* square then multiply if the bit is set */
  193. for (x = 0; x < bitcpy; x++) {
  194. if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
  195. goto LBL_RES;
  196. }
  197. if ((err = redux (&res, P, &mu)) != MP_OKAY) {
  198. goto LBL_RES;
  199. }
  200. bitbuf <<= 1;
  201. if ((bitbuf & (1 << winsize)) != 0) {
  202. /* then multiply */
  203. if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) {
  204. goto LBL_RES;
  205. }
  206. if ((err = redux (&res, P, &mu)) != MP_OKAY) {
  207. goto LBL_RES;
  208. }
  209. }
  210. }
  211. }
  212. mp_exch (&res, Y);
  213. err = MP_OKAY;
  214. LBL_RES:mp_clear (&res);
  215. LBL_MU:mp_clear (&mu);
  216. LBL_M:
  217. mp_clear(&M[1]);
  218. for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
  219. mp_clear (&M[x]);
  220. }
  221. return err;
  222. }
  223. #endif
  224. /* $Source: /cvs/libtom/libtommath/bn_s_mp_exptmod.c,v $ */
  225. /* $Revision: 1.4 $ */
  226. /* $Date: 2006/03/31 14:18:44 $ */