bn_mp_exptmod_fast.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #include <tommath.h>
  2. #ifdef BN_MP_EXPTMOD_FAST_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. /* computes Y == G**X mod P, HAC pp.616, Algorithm 14.85
  18. *
  19. * Uses a left-to-right k-ary sliding window to compute the modular exponentiation.
  20. * The value of k changes based on the size of the exponent.
  21. *
  22. * Uses Montgomery or Diminished Radix reduction [whichever appropriate]
  23. */
  24. #ifdef MP_LOW_MEM
  25. #define TAB_SIZE 32
  26. #else
  27. #define TAB_SIZE 256
  28. #endif
  29. int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
  30. {
  31. mp_int M[TAB_SIZE], res;
  32. mp_digit buf, mp;
  33. int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
  34. /* use a pointer to the reduction algorithm. This allows us to use
  35. * one of many reduction algorithms without modding the guts of
  36. * the code with if statements everywhere.
  37. */
  38. int (*redux)(mp_int*,mp_int*,mp_digit);
  39. /* find window size */
  40. x = mp_count_bits (X);
  41. if (x <= 7) {
  42. winsize = 2;
  43. } else if (x <= 36) {
  44. winsize = 3;
  45. } else if (x <= 140) {
  46. winsize = 4;
  47. } else if (x <= 450) {
  48. winsize = 5;
  49. } else if (x <= 1303) {
  50. winsize = 6;
  51. } else if (x <= 3529) {
  52. winsize = 7;
  53. } else {
  54. winsize = 8;
  55. }
  56. #ifdef MP_LOW_MEM
  57. if (winsize > 5) {
  58. winsize = 5;
  59. }
  60. #endif
  61. /* init M array */
  62. /* init first cell */
  63. if ((err = mp_init_size(&M[1], P->alloc)) != MP_OKAY) {
  64. return err;
  65. }
  66. /* now init the second half of the array */
  67. for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
  68. if ((err = mp_init_size(&M[x], P->alloc)) != MP_OKAY) {
  69. for (y = 1<<(winsize-1); y < x; y++) {
  70. mp_clear (&M[y]);
  71. }
  72. mp_clear(&M[1]);
  73. return err;
  74. }
  75. }
  76. /* determine and setup reduction code */
  77. if (redmode == 0) {
  78. #ifdef BN_MP_MONTGOMERY_SETUP_C
  79. /* now setup montgomery */
  80. if ((err = mp_montgomery_setup (P, &mp)) != MP_OKAY) {
  81. goto LBL_M;
  82. }
  83. #else
  84. err = MP_VAL;
  85. goto LBL_M;
  86. #endif
  87. /* automatically pick the comba one if available (saves quite a few calls/ifs) */
  88. #ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
  89. if (((P->used * 2 + 1) < MP_WARRAY) &&
  90. P->used < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
  91. redux = fast_mp_montgomery_reduce;
  92. } else
  93. #endif
  94. {
  95. #ifdef BN_MP_MONTGOMERY_REDUCE_C
  96. /* use slower baseline Montgomery method */
  97. redux = mp_montgomery_reduce;
  98. #else
  99. err = MP_VAL;
  100. goto LBL_M;
  101. #endif
  102. }
  103. } else if (redmode == 1) {
  104. #if defined(BN_MP_DR_SETUP_C) && defined(BN_MP_DR_REDUCE_C)
  105. /* setup DR reduction for moduli of the form B**k - b */
  106. mp_dr_setup(P, &mp);
  107. redux = mp_dr_reduce;
  108. #else
  109. err = MP_VAL;
  110. goto LBL_M;
  111. #endif
  112. } else {
  113. #if defined(BN_MP_REDUCE_2K_SETUP_C) && defined(BN_MP_REDUCE_2K_C)
  114. /* setup DR reduction for moduli of the form 2**k - b */
  115. if ((err = mp_reduce_2k_setup(P, &mp)) != MP_OKAY) {
  116. goto LBL_M;
  117. }
  118. redux = mp_reduce_2k;
  119. #else
  120. err = MP_VAL;
  121. goto LBL_M;
  122. #endif
  123. }
  124. /* setup result */
  125. if ((err = mp_init_size (&res, P->alloc)) != MP_OKAY) {
  126. goto LBL_M;
  127. }
  128. /* create M table
  129. *
  130. *
  131. * The first half of the table is not computed though accept for M[0] and M[1]
  132. */
  133. if (redmode == 0) {
  134. #ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
  135. /* now we need R mod m */
  136. if ((err = mp_montgomery_calc_normalization (&res, P)) != MP_OKAY) {
  137. goto LBL_RES;
  138. }
  139. #else
  140. err = MP_VAL;
  141. goto LBL_RES;
  142. #endif
  143. /* now set M[1] to G * R mod m */
  144. if ((err = mp_mulmod (G, &res, P, &M[1])) != MP_OKAY) {
  145. goto LBL_RES;
  146. }
  147. } else {
  148. mp_set(&res, 1);
  149. if ((err = mp_mod(G, P, &M[1])) != MP_OKAY) {
  150. goto LBL_RES;
  151. }
  152. }
  153. /* compute the value at M[1<<(winsize-1)] by squaring M[1] (winsize-1) times */
  154. if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) {
  155. goto LBL_RES;
  156. }
  157. for (x = 0; x < (winsize - 1); x++) {
  158. if ((err = mp_sqr (&M[1 << (winsize - 1)], &M[1 << (winsize - 1)])) != MP_OKAY) {
  159. goto LBL_RES;
  160. }
  161. if ((err = redux (&M[1 << (winsize - 1)], P, mp)) != MP_OKAY) {
  162. goto LBL_RES;
  163. }
  164. }
  165. /* create upper table */
  166. for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) {
  167. if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) {
  168. goto LBL_RES;
  169. }
  170. if ((err = redux (&M[x], P, mp)) != MP_OKAY) {
  171. goto LBL_RES;
  172. }
  173. }
  174. /* set initial mode and bit cnt */
  175. mode = 0;
  176. bitcnt = 1;
  177. buf = 0;
  178. digidx = X->used - 1;
  179. bitcpy = 0;
  180. bitbuf = 0;
  181. for (;;) {
  182. /* grab next digit as required */
  183. if (--bitcnt == 0) {
  184. /* if digidx == -1 we are out of digits so break */
  185. if (digidx == -1) {
  186. break;
  187. }
  188. /* read next digit and reset bitcnt */
  189. buf = X->dp[digidx--];
  190. bitcnt = (int)DIGIT_BIT;
  191. }
  192. /* grab the next msb from the exponent */
  193. y = (mp_digit)(buf >> (DIGIT_BIT - 1)) & 1;
  194. buf <<= (mp_digit)1;
  195. /* if the bit is zero and mode == 0 then we ignore it
  196. * These represent the leading zero bits before the first 1 bit
  197. * in the exponent. Technically this opt is not required but it
  198. * does lower the # of trivial squaring/reductions used
  199. */
  200. if (mode == 0 && y == 0) {
  201. continue;
  202. }
  203. /* if the bit is zero and mode == 1 then we square */
  204. if (mode == 1 && y == 0) {
  205. if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
  206. goto LBL_RES;
  207. }
  208. if ((err = redux (&res, P, mp)) != MP_OKAY) {
  209. goto LBL_RES;
  210. }
  211. continue;
  212. }
  213. /* else we add it to the window */
  214. bitbuf |= (y << (winsize - ++bitcpy));
  215. mode = 2;
  216. if (bitcpy == winsize) {
  217. /* ok window is filled so square as required and multiply */
  218. /* square first */
  219. for (x = 0; x < winsize; x++) {
  220. if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
  221. goto LBL_RES;
  222. }
  223. if ((err = redux (&res, P, mp)) != MP_OKAY) {
  224. goto LBL_RES;
  225. }
  226. }
  227. /* then multiply */
  228. if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) {
  229. goto LBL_RES;
  230. }
  231. if ((err = redux (&res, P, mp)) != MP_OKAY) {
  232. goto LBL_RES;
  233. }
  234. /* empty window and reset */
  235. bitcpy = 0;
  236. bitbuf = 0;
  237. mode = 1;
  238. }
  239. }
  240. /* if bits remain then square/multiply */
  241. if (mode == 2 && bitcpy > 0) {
  242. /* square then multiply if the bit is set */
  243. for (x = 0; x < bitcpy; x++) {
  244. if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
  245. goto LBL_RES;
  246. }
  247. if ((err = redux (&res, P, mp)) != MP_OKAY) {
  248. goto LBL_RES;
  249. }
  250. /* get next bit of the window */
  251. bitbuf <<= 1;
  252. if ((bitbuf & (1 << winsize)) != 0) {
  253. /* then multiply */
  254. if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) {
  255. goto LBL_RES;
  256. }
  257. if ((err = redux (&res, P, mp)) != MP_OKAY) {
  258. goto LBL_RES;
  259. }
  260. }
  261. }
  262. }
  263. if (redmode == 0) {
  264. /* fixup result if Montgomery reduction is used
  265. * recall that any value in a Montgomery system is
  266. * actually multiplied by R mod n. So we have
  267. * to reduce one more time to cancel out the factor
  268. * of R.
  269. */
  270. if ((err = redux(&res, P, mp)) != MP_OKAY) {
  271. goto LBL_RES;
  272. }
  273. }
  274. /* swap res with Y */
  275. mp_exch (&res, Y);
  276. err = MP_OKAY;
  277. LBL_RES:mp_clear (&res);
  278. LBL_M:
  279. mp_clear(&M[1]);
  280. for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
  281. mp_clear (&M[x]);
  282. }
  283. return err;
  284. }
  285. #endif
  286. /* $Source: /cvs/libtom/libtommath/bn_mp_exptmod_fast.c,v $ */
  287. /* $Revision: 1.3 $ */
  288. /* $Date: 2006/03/31 14:18:44 $ */