bn_mp_prime_is_prime.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #include "tommath_private.h"
  2. #ifdef BN_MP_PRIME_IS_PRIME_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis */
  4. /* SPDX-License-Identifier: Unlicense */
  5. /* portable integer log of two with small footprint */
  6. static unsigned int s_floor_ilog2(int value)
  7. {
  8. unsigned int r = 0;
  9. while ((value >>= 1) != 0) {
  10. r++;
  11. }
  12. return r;
  13. }
  14. mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result)
  15. {
  16. mp_int b;
  17. int ix, p_max = 0, size_a, len;
  18. mp_bool res;
  19. mp_err err;
  20. unsigned int fips_rand, mask;
  21. /* default to no */
  22. *result = MP_NO;
  23. /* Some shortcuts */
  24. /* N > 3 */
  25. if (a->used == 1) {
  26. if ((a->dp[0] == 0u) || (a->dp[0] == 1u)) {
  27. *result = MP_NO;
  28. return MP_OKAY;
  29. }
  30. if (a->dp[0] == 2u) {
  31. *result = MP_YES;
  32. return MP_OKAY;
  33. }
  34. }
  35. /* N must be odd */
  36. if (MP_IS_EVEN(a)) {
  37. return MP_OKAY;
  38. }
  39. /* N is not a perfect square: floor(sqrt(N))^2 != N */
  40. if ((err = mp_is_square(a, &res)) != MP_OKAY) {
  41. return err;
  42. }
  43. if (res != MP_NO) {
  44. return MP_OKAY;
  45. }
  46. /* is the input equal to one of the primes in the table? */
  47. for (ix = 0; ix < PRIVATE_MP_PRIME_TAB_SIZE; ix++) {
  48. if (mp_cmp_d(a, s_mp_prime_tab[ix]) == MP_EQ) {
  49. *result = MP_YES;
  50. return MP_OKAY;
  51. }
  52. }
  53. #ifdef MP_8BIT
  54. /* The search in the loop above was exhaustive in this case */
  55. if ((a->used == 1) && (PRIVATE_MP_PRIME_TAB_SIZE >= 31)) {
  56. return MP_OKAY;
  57. }
  58. #endif
  59. /* first perform trial division */
  60. if ((err = s_mp_prime_is_divisible(a, &res)) != MP_OKAY) {
  61. return err;
  62. }
  63. /* return if it was trivially divisible */
  64. if (res == MP_YES) {
  65. return MP_OKAY;
  66. }
  67. /*
  68. Run the Miller-Rabin test with base 2 for the BPSW test.
  69. */
  70. if ((err = mp_init_set(&b, 2uL)) != MP_OKAY) {
  71. return err;
  72. }
  73. if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) {
  74. goto LBL_B;
  75. }
  76. if (res == MP_NO) {
  77. goto LBL_B;
  78. }
  79. /*
  80. Rumours have it that Mathematica does a second M-R test with base 3.
  81. Other rumours have it that their strong L-S test is slightly different.
  82. It does not hurt, though, beside a bit of extra runtime.
  83. */
  84. b.dp[0]++;
  85. if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) {
  86. goto LBL_B;
  87. }
  88. if (res == MP_NO) {
  89. goto LBL_B;
  90. }
  91. /*
  92. * Both, the Frobenius-Underwood test and the the Lucas-Selfridge test are quite
  93. * slow so if speed is an issue, define LTM_USE_ONLY_MR to use M-R tests with
  94. * bases 2, 3 and t random bases.
  95. */
  96. #ifndef LTM_USE_ONLY_MR
  97. if (t >= 0) {
  98. /*
  99. * Use a Frobenius-Underwood test instead of the Lucas-Selfridge test for
  100. * MP_8BIT (It is unknown if the Lucas-Selfridge test works with 16-bit
  101. * integers but the necesssary analysis is on the todo-list).
  102. */
  103. #if defined (MP_8BIT) || defined (LTM_USE_FROBENIUS_TEST)
  104. err = mp_prime_frobenius_underwood(a, &res);
  105. if ((err != MP_OKAY) && (err != MP_ITER)) {
  106. goto LBL_B;
  107. }
  108. if (res == MP_NO) {
  109. goto LBL_B;
  110. }
  111. #else
  112. if ((err = mp_prime_strong_lucas_selfridge(a, &res)) != MP_OKAY) {
  113. goto LBL_B;
  114. }
  115. if (res == MP_NO) {
  116. goto LBL_B;
  117. }
  118. #endif
  119. }
  120. #endif
  121. /* run at least one Miller-Rabin test with a random base */
  122. if (t == 0) {
  123. t = 1;
  124. }
  125. /*
  126. Only recommended if the input range is known to be < 3317044064679887385961981
  127. It uses the bases necessary for a deterministic M-R test if the input is
  128. smaller than 3317044064679887385961981
  129. The caller has to check the size.
  130. TODO: can be made a bit finer grained but comparing is not free.
  131. */
  132. if (t < 0) {
  133. /*
  134. Sorenson, Jonathan; Webster, Jonathan (2015).
  135. "Strong Pseudoprimes to Twelve Prime Bases".
  136. */
  137. /* 0x437ae92817f9fc85b7e5 = 318665857834031151167461 */
  138. if ((err = mp_read_radix(&b, "437ae92817f9fc85b7e5", 16)) != MP_OKAY) {
  139. goto LBL_B;
  140. }
  141. if (mp_cmp(a, &b) == MP_LT) {
  142. p_max = 12;
  143. } else {
  144. /* 0x2be6951adc5b22410a5fd = 3317044064679887385961981 */
  145. if ((err = mp_read_radix(&b, "2be6951adc5b22410a5fd", 16)) != MP_OKAY) {
  146. goto LBL_B;
  147. }
  148. if (mp_cmp(a, &b) == MP_LT) {
  149. p_max = 13;
  150. } else {
  151. err = MP_VAL;
  152. goto LBL_B;
  153. }
  154. }
  155. /* we did bases 2 and 3 already, skip them */
  156. for (ix = 2; ix < p_max; ix++) {
  157. mp_set(&b, s_mp_prime_tab[ix]);
  158. if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) {
  159. goto LBL_B;
  160. }
  161. if (res == MP_NO) {
  162. goto LBL_B;
  163. }
  164. }
  165. }
  166. /*
  167. Do "t" M-R tests with random bases between 3 and "a".
  168. See Fips 186.4 p. 126ff
  169. */
  170. else if (t > 0) {
  171. /*
  172. * The mp_digit's have a defined bit-size but the size of the
  173. * array a.dp is a simple 'int' and this library can not assume full
  174. * compliance to the current C-standard (ISO/IEC 9899:2011) because
  175. * it gets used for small embeded processors, too. Some of those MCUs
  176. * have compilers that one cannot call standard compliant by any means.
  177. * Hence the ugly type-fiddling in the following code.
  178. */
  179. size_a = mp_count_bits(a);
  180. mask = (1u << s_floor_ilog2(size_a)) - 1u;
  181. /*
  182. Assuming the General Rieman hypothesis (never thought to write that in a
  183. comment) the upper bound can be lowered to 2*(log a)^2.
  184. E. Bach, "Explicit bounds for primality testing and related problems,"
  185. Math. Comp. 55 (1990), 355-380.
  186. size_a = (size_a/10) * 7;
  187. len = 2 * (size_a * size_a);
  188. E.g.: a number of size 2^2048 would be reduced to the upper limit
  189. floor(2048/10)*7 = 1428
  190. 2 * 1428^2 = 4078368
  191. (would have been ~4030331.9962 with floats and natural log instead)
  192. That number is smaller than 2^28, the default bit-size of mp_digit.
  193. */
  194. /*
  195. How many tests, you might ask? Dana Jacobsen of Math::Prime::Util fame
  196. does exactly 1. In words: one. Look at the end of _GMP_is_prime() in
  197. Math-Prime-Util-GMP-0.50/primality.c if you do not believe it.
  198. The function mp_rand() goes to some length to use a cryptographically
  199. good PRNG. That also means that the chance to always get the same base
  200. in the loop is non-zero, although very low.
  201. If the BPSW test and/or the addtional Frobenious test have been
  202. performed instead of just the Miller-Rabin test with the bases 2 and 3,
  203. a single extra test should suffice, so such a very unlikely event
  204. will not do much harm.
  205. To preemptivly answer the dangling question: no, a witness does not
  206. need to be prime.
  207. */
  208. for (ix = 0; ix < t; ix++) {
  209. /* mp_rand() guarantees the first digit to be non-zero */
  210. if ((err = mp_rand(&b, 1)) != MP_OKAY) {
  211. goto LBL_B;
  212. }
  213. /*
  214. * Reduce digit before casting because mp_digit might be bigger than
  215. * an unsigned int and "mask" on the other side is most probably not.
  216. */
  217. fips_rand = (unsigned int)(b.dp[0] & (mp_digit) mask);
  218. #ifdef MP_8BIT
  219. /*
  220. * One 8-bit digit is too small, so concatenate two if the size of
  221. * unsigned int allows for it.
  222. */
  223. if ((MP_SIZEOF_BITS(unsigned int)/2) >= MP_SIZEOF_BITS(mp_digit)) {
  224. if ((err = mp_rand(&b, 1)) != MP_OKAY) {
  225. goto LBL_B;
  226. }
  227. fips_rand <<= MP_SIZEOF_BITS(mp_digit);
  228. fips_rand |= (unsigned int) b.dp[0];
  229. fips_rand &= mask;
  230. }
  231. #endif
  232. if (fips_rand > (unsigned int)(INT_MAX - MP_DIGIT_BIT)) {
  233. len = INT_MAX / MP_DIGIT_BIT;
  234. } else {
  235. len = (((int)fips_rand + MP_DIGIT_BIT) / MP_DIGIT_BIT);
  236. }
  237. /* Unlikely. */
  238. if (len < 0) {
  239. ix--;
  240. continue;
  241. }
  242. /*
  243. * As mentioned above, one 8-bit digit is too small and
  244. * although it can only happen in the unlikely case that
  245. * an "unsigned int" is smaller than 16 bit a simple test
  246. * is cheap and the correction even cheaper.
  247. */
  248. #ifdef MP_8BIT
  249. /* All "a" < 2^8 have been caught before */
  250. if (len == 1) {
  251. len++;
  252. }
  253. #endif
  254. if ((err = mp_rand(&b, len)) != MP_OKAY) {
  255. goto LBL_B;
  256. }
  257. /*
  258. * That number might got too big and the witness has to be
  259. * smaller than "a"
  260. */
  261. len = mp_count_bits(&b);
  262. if (len >= size_a) {
  263. len = (len - size_a) + 1;
  264. if ((err = mp_div_2d(&b, len, &b, NULL)) != MP_OKAY) {
  265. goto LBL_B;
  266. }
  267. }
  268. /* Although the chance for b <= 3 is miniscule, try again. */
  269. if (mp_cmp_d(&b, 3uL) != MP_GT) {
  270. ix--;
  271. continue;
  272. }
  273. if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) {
  274. goto LBL_B;
  275. }
  276. if (res == MP_NO) {
  277. goto LBL_B;
  278. }
  279. }
  280. }
  281. /* passed the test */
  282. *result = MP_YES;
  283. LBL_B:
  284. mp_clear(&b);
  285. return err;
  286. }
  287. #endif