PageRenderTime 54ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/src/gmp/mpz/powm.c

https://gitlab.com/evilbinary/ecl
C | 461 lines | 349 code | 40 blank | 72 comment | 80 complexity | 607e5ea62e561c28202d148eac7a9e85 MD5 | raw file
Possible License(s): JSON, GPL-2.0, LGPL-2.1, LGPL-2.0
  1. /* mpz_powm(res,base,exp,mod) -- Set RES to (base**exp) mod MOD.
  2. Copyright 1991, 1993, 1994, 1996, 1997, 2000, 2001, 2002, 2005 Free Software
  3. Foundation, Inc. Contributed by Paul Zimmermann.
  4. This file is part of the GNU MP Library.
  5. The GNU MP Library is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 2.1 of the License, or (at your
  8. option) any later version.
  9. The GNU MP Library is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  12. License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with the GNU MP Library; see the file COPYING.LIB. If not, write to
  15. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "gmp.h"
  18. #include "gmp-impl.h"
  19. #include "longlong.h"
  20. #ifdef BERKELEY_MP
  21. #include "mp.h"
  22. #endif
  23. /* Set cp[] <- tp[]/R^n mod mp[]. Clobber tp[].
  24. mp[] is n limbs; tp[] is 2n limbs. */
  25. #if ! WANT_REDC_GLOBAL
  26. static
  27. #endif
  28. void
  29. redc (mp_ptr cp, mp_srcptr mp, mp_size_t n, mp_limb_t Nprim, mp_ptr tp)
  30. {
  31. mp_limb_t cy;
  32. mp_limb_t q;
  33. mp_size_t j;
  34. ASSERT_MPN (tp, 2*n);
  35. for (j = 0; j < n; j++)
  36. {
  37. q = (tp[0] * Nprim) & GMP_NUMB_MASK;
  38. tp[0] = mpn_addmul_1 (tp, mp, n, q);
  39. tp++;
  40. }
  41. cy = mpn_add_n (cp, tp, tp - n, n);
  42. if (cy != 0)
  43. mpn_sub_n (cp, cp, mp, n);
  44. }
  45. /* Compute t = a mod m, a is defined by (ap,an), m is defined by (mp,mn), and
  46. t is defined by (tp,mn). */
  47. static void
  48. reduce (mp_ptr tp, mp_srcptr ap, mp_size_t an, mp_srcptr mp, mp_size_t mn)
  49. {
  50. mp_ptr qp;
  51. TMP_DECL;
  52. TMP_MARK;
  53. qp = TMP_ALLOC_LIMBS (an - mn + 1);
  54. mpn_tdiv_qr (qp, tp, 0L, ap, an, mp, mn);
  55. TMP_FREE;
  56. }
  57. #if REDUCE_EXPONENT
  58. /* Return the group order of the ring mod m. */
  59. static mp_limb_t
  60. phi (mp_limb_t t)
  61. {
  62. mp_limb_t d, m, go;
  63. go = 1;
  64. if (t % 2 == 0)
  65. {
  66. t = t / 2;
  67. while (t % 2 == 0)
  68. {
  69. go *= 2;
  70. t = t / 2;
  71. }
  72. }
  73. for (d = 3;; d += 2)
  74. {
  75. m = d - 1;
  76. for (;;)
  77. {
  78. unsigned long int q = t / d;
  79. if (q < d)
  80. {
  81. if (t <= 1)
  82. return go;
  83. if (t == d)
  84. return go * m;
  85. return go * (t - 1);
  86. }
  87. if (t != q * d)
  88. break;
  89. go *= m;
  90. m = d;
  91. t = q;
  92. }
  93. }
  94. }
  95. #endif
  96. /* average number of calls to redc for an exponent of n bits
  97. with the sliding window algorithm of base 2^k: the optimal is
  98. obtained for the value of k which minimizes 2^(k-1)+n/(k+1):
  99. n\k 4 5 6 7 8
  100. 128 156* 159 171 200 261
  101. 256 309 307* 316 343 403
  102. 512 617 607* 610 632 688
  103. 1024 1231 1204 1195* 1207 1256
  104. 2048 2461 2399 2366 2360* 2396
  105. 4096 4918 4787 4707 4665* 4670
  106. */
  107. /* Use REDC instead of usual reduction for sizes < POWM_THRESHOLD. In REDC
  108. each modular multiplication costs about 2*n^2 limbs operations, whereas
  109. using usual reduction it costs 3*K(n), where K(n) is the cost of a
  110. multiplication using Karatsuba, and a division is assumed to cost 2*K(n),
  111. for example using Burnikel-Ziegler's algorithm. This gives a theoretical
  112. threshold of a*SQR_KARATSUBA_THRESHOLD, with a=(3/2)^(1/(2-ln(3)/ln(2))) ~
  113. 2.66. */
  114. /* For now, also disable REDC when MOD is even, as the inverse can't handle
  115. that. At some point, we might want to make the code faster for that case,
  116. perhaps using CRR. */
  117. #ifndef POWM_THRESHOLD
  118. #define POWM_THRESHOLD ((8 * SQR_KARATSUBA_THRESHOLD) / 3)
  119. #endif
  120. #define HANDLE_NEGATIVE_EXPONENT 1
  121. #undef REDUCE_EXPONENT
  122. void
  123. #ifndef BERKELEY_MP
  124. mpz_powm (mpz_ptr r, mpz_srcptr b, mpz_srcptr e, mpz_srcptr m)
  125. #else /* BERKELEY_MP */
  126. pow (mpz_srcptr b, mpz_srcptr e, mpz_srcptr m, mpz_ptr r)
  127. #endif /* BERKELEY_MP */
  128. {
  129. mp_ptr xp, tp, qp, gp, this_gp;
  130. mp_srcptr bp, ep, mp;
  131. mp_size_t bn, es, en, mn, xn;
  132. mp_limb_t invm, c;
  133. unsigned long int enb;
  134. mp_size_t i, K, j, l, k;
  135. int m_zero_cnt, e_zero_cnt;
  136. int sh;
  137. int use_redc;
  138. #if HANDLE_NEGATIVE_EXPONENT
  139. mpz_t new_b;
  140. #endif
  141. #if REDUCE_EXPONENT
  142. mpz_t new_e;
  143. #endif
  144. TMP_DECL;
  145. mp = PTR(m);
  146. mn = ABSIZ (m);
  147. if (mn == 0)
  148. DIVIDE_BY_ZERO;
  149. TMP_MARK;
  150. es = SIZ (e);
  151. if (es <= 0)
  152. {
  153. if (es == 0)
  154. {
  155. /* Exponent is zero, result is 1 mod m, i.e., 1 or 0 depending on if
  156. m equals 1. */
  157. SIZ(r) = (mn == 1 && mp[0] == 1) ? 0 : 1;
  158. PTR(r)[0] = 1;
  159. TMP_FREE; /* we haven't really allocated anything here */
  160. return;
  161. }
  162. #if HANDLE_NEGATIVE_EXPONENT
  163. MPZ_TMP_INIT (new_b, mn + 1);
  164. if (! mpz_invert (new_b, b, m))
  165. DIVIDE_BY_ZERO;
  166. b = new_b;
  167. es = -es;
  168. #else
  169. DIVIDE_BY_ZERO;
  170. #endif
  171. }
  172. en = es;
  173. #if REDUCE_EXPONENT
  174. /* Reduce exponent by dividing it by phi(m) when m small. */
  175. if (mn == 1 && mp[0] < 0x7fffffffL && en * GMP_NUMB_BITS > 150)
  176. {
  177. MPZ_TMP_INIT (new_e, 2);
  178. mpz_mod_ui (new_e, e, phi (mp[0]));
  179. e = new_e;
  180. }
  181. #endif
  182. use_redc = mn < POWM_THRESHOLD && mp[0] % 2 != 0;
  183. if (use_redc)
  184. {
  185. /* invm = -1/m mod 2^BITS_PER_MP_LIMB, must have m odd */
  186. modlimb_invert (invm, mp[0]);
  187. invm = -invm;
  188. }
  189. else
  190. {
  191. /* Normalize m (i.e. make its most significant bit set) as required by
  192. division functions below. */
  193. count_leading_zeros (m_zero_cnt, mp[mn - 1]);
  194. m_zero_cnt -= GMP_NAIL_BITS;
  195. if (m_zero_cnt != 0)
  196. {
  197. mp_ptr new_mp;
  198. new_mp = TMP_ALLOC_LIMBS (mn);
  199. mpn_lshift (new_mp, mp, mn, m_zero_cnt);
  200. mp = new_mp;
  201. }
  202. }
  203. /* Determine optimal value of k, the number of exponent bits we look at
  204. at a time. */
  205. count_leading_zeros (e_zero_cnt, PTR(e)[en - 1]);
  206. e_zero_cnt -= GMP_NAIL_BITS;
  207. enb = en * GMP_NUMB_BITS - e_zero_cnt; /* number of bits of exponent */
  208. k = 1;
  209. K = 2;
  210. while (2 * enb > K * (2 + k * (3 + k)))
  211. {
  212. k++;
  213. K *= 2;
  214. if (k == 10) /* cap allocation */
  215. break;
  216. }
  217. tp = TMP_ALLOC_LIMBS (2 * mn);
  218. qp = TMP_ALLOC_LIMBS (mn + 1);
  219. gp = __GMP_ALLOCATE_FUNC_LIMBS (K / 2 * mn);
  220. /* Compute x*R^n where R=2^BITS_PER_MP_LIMB. */
  221. bn = ABSIZ (b);
  222. bp = PTR(b);
  223. /* Handle |b| >= m by computing b mod m. FIXME: It is not strictly necessary
  224. for speed or correctness to do this when b and m have the same number of
  225. limbs, perhaps remove mpn_cmp call. */
  226. if (bn > mn || (bn == mn && mpn_cmp (bp, mp, mn) >= 0))
  227. {
  228. /* Reduce possibly huge base while moving it to gp[0]. Use a function
  229. call to reduce, since we don't want the quotient allocation to
  230. live until function return. */
  231. if (use_redc)
  232. {
  233. reduce (tp + mn, bp, bn, mp, mn); /* b mod m */
  234. MPN_ZERO (tp, mn);
  235. mpn_tdiv_qr (qp, gp, 0L, tp, 2 * mn, mp, mn); /* unnormnalized! */
  236. }
  237. else
  238. {
  239. reduce (gp, bp, bn, mp, mn);
  240. }
  241. }
  242. else
  243. {
  244. /* |b| < m. We pad out operands to become mn limbs, which simplifies
  245. the rest of the function, but slows things down when the |b| << m. */
  246. if (use_redc)
  247. {
  248. MPN_ZERO (tp, mn);
  249. MPN_COPY (tp + mn, bp, bn);
  250. MPN_ZERO (tp + mn + bn, mn - bn);
  251. mpn_tdiv_qr (qp, gp, 0L, tp, 2 * mn, mp, mn);
  252. }
  253. else
  254. {
  255. MPN_COPY (gp, bp, bn);
  256. MPN_ZERO (gp + bn, mn - bn);
  257. }
  258. }
  259. /* Compute xx^i for odd g < 2^i. */
  260. xp = TMP_ALLOC_LIMBS (mn);
  261. mpn_sqr_n (tp, gp, mn);
  262. if (use_redc)
  263. redc (xp, mp, mn, invm, tp); /* xx = x^2*R^n */
  264. else
  265. mpn_tdiv_qr (qp, xp, 0L, tp, 2 * mn, mp, mn);
  266. this_gp = gp;
  267. for (i = 1; i < K / 2; i++)
  268. {
  269. mpn_mul_n (tp, this_gp, xp, mn);
  270. this_gp += mn;
  271. if (use_redc)
  272. redc (this_gp, mp, mn, invm, tp); /* g[i] = x^(2i+1)*R^n */
  273. else
  274. mpn_tdiv_qr (qp, this_gp, 0L, tp, 2 * mn, mp, mn);
  275. }
  276. /* Start the real stuff. */
  277. ep = PTR (e);
  278. i = en - 1; /* current index */
  279. c = ep[i]; /* current limb */
  280. sh = GMP_NUMB_BITS - e_zero_cnt; /* significant bits in ep[i] */
  281. sh -= k; /* index of lower bit of ep[i] to take into account */
  282. if (sh < 0)
  283. { /* k-sh extra bits are needed */
  284. if (i > 0)
  285. {
  286. i--;
  287. c <<= (-sh);
  288. sh += GMP_NUMB_BITS;
  289. c |= ep[i] >> sh;
  290. }
  291. }
  292. else
  293. c >>= sh;
  294. for (j = 0; c % 2 == 0; j++)
  295. c >>= 1;
  296. MPN_COPY (xp, gp + mn * (c >> 1), mn);
  297. while (--j >= 0)
  298. {
  299. mpn_sqr_n (tp, xp, mn);
  300. if (use_redc)
  301. redc (xp, mp, mn, invm, tp);
  302. else
  303. mpn_tdiv_qr (qp, xp, 0L, tp, 2 * mn, mp, mn);
  304. }
  305. while (i > 0 || sh > 0)
  306. {
  307. c = ep[i];
  308. l = k; /* number of bits treated */
  309. sh -= l;
  310. if (sh < 0)
  311. {
  312. if (i > 0)
  313. {
  314. i--;
  315. c <<= (-sh);
  316. sh += GMP_NUMB_BITS;
  317. c |= ep[i] >> sh;
  318. }
  319. else
  320. {
  321. l += sh; /* last chunk of bits from e; l < k */
  322. }
  323. }
  324. else
  325. c >>= sh;
  326. c &= ((mp_limb_t) 1 << l) - 1;
  327. /* This while loop implements the sliding window improvement--loop while
  328. the most significant bit of c is zero, squaring xx as we go. */
  329. while ((c >> (l - 1)) == 0 && (i > 0 || sh > 0))
  330. {
  331. mpn_sqr_n (tp, xp, mn);
  332. if (use_redc)
  333. redc (xp, mp, mn, invm, tp);
  334. else
  335. mpn_tdiv_qr (qp, xp, 0L, tp, 2 * mn, mp, mn);
  336. if (sh != 0)
  337. {
  338. sh--;
  339. c = (c << 1) + ((ep[i] >> sh) & 1);
  340. }
  341. else
  342. {
  343. i--;
  344. sh = GMP_NUMB_BITS - 1;
  345. c = (c << 1) + (ep[i] >> sh);
  346. }
  347. }
  348. /* Replace xx by xx^(2^l)*x^c. */
  349. if (c != 0)
  350. {
  351. for (j = 0; c % 2 == 0; j++)
  352. c >>= 1;
  353. /* c0 = c * 2^j, i.e. xx^(2^l)*x^c = (A^(2^(l - j))*c)^(2^j) */
  354. l -= j;
  355. while (--l >= 0)
  356. {
  357. mpn_sqr_n (tp, xp, mn);
  358. if (use_redc)
  359. redc (xp, mp, mn, invm, tp);
  360. else
  361. mpn_tdiv_qr (qp, xp, 0L, tp, 2 * mn, mp, mn);
  362. }
  363. mpn_mul_n (tp, xp, gp + mn * (c >> 1), mn);
  364. if (use_redc)
  365. redc (xp, mp, mn, invm, tp);
  366. else
  367. mpn_tdiv_qr (qp, xp, 0L, tp, 2 * mn, mp, mn);
  368. }
  369. else
  370. j = l; /* case c=0 */
  371. while (--j >= 0)
  372. {
  373. mpn_sqr_n (tp, xp, mn);
  374. if (use_redc)
  375. redc (xp, mp, mn, invm, tp);
  376. else
  377. mpn_tdiv_qr (qp, xp, 0L, tp, 2 * mn, mp, mn);
  378. }
  379. }
  380. if (use_redc)
  381. {
  382. /* Convert back xx to xx/R^n. */
  383. MPN_COPY (tp, xp, mn);
  384. MPN_ZERO (tp + mn, mn);
  385. redc (xp, mp, mn, invm, tp);
  386. if (mpn_cmp (xp, mp, mn) >= 0)
  387. mpn_sub_n (xp, xp, mp, mn);
  388. }
  389. else
  390. {
  391. if (m_zero_cnt != 0)
  392. {
  393. mp_limb_t cy;
  394. cy = mpn_lshift (tp, xp, mn, m_zero_cnt);
  395. tp[mn] = cy;
  396. mpn_tdiv_qr (qp, xp, 0L, tp, mn + (cy != 0), mp, mn);
  397. mpn_rshift (xp, xp, mn, m_zero_cnt);
  398. }
  399. }
  400. xn = mn;
  401. MPN_NORMALIZE (xp, xn);
  402. if ((ep[0] & 1) && SIZ(b) < 0 && xn != 0)
  403. {
  404. mp = PTR(m); /* want original, unnormalized m */
  405. mpn_sub (xp, mp, mn, xp, xn);
  406. xn = mn;
  407. MPN_NORMALIZE (xp, xn);
  408. }
  409. MPZ_REALLOC (r, xn);
  410. SIZ (r) = xn;
  411. MPN_COPY (PTR(r), xp, xn);
  412. __GMP_FREE_FUNC_LIMBS (gp, K / 2 * mn);
  413. TMP_FREE;
  414. }