bn_mp_prime_next_prime.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include <tommath.h>
  2. #ifdef BN_MP_PRIME_NEXT_PRIME_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. /* finds the next prime after the number "a" using "t" trials
  18. * of Miller-Rabin.
  19. *
  20. * bbs_style = 1 means the prime must be congruent to 3 mod 4
  21. */
  22. int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
  23. {
  24. int err, res, x, y;
  25. mp_digit res_tab[PRIME_SIZE], step, kstep;
  26. mp_int b;
  27. /* ensure t is valid */
  28. if (t <= 0 || t > PRIME_SIZE) {
  29. return MP_VAL;
  30. }
  31. /* force positive */
  32. a->sign = MP_ZPOS;
  33. /* simple algo if a is less than the largest prime in the table */
  34. if (mp_cmp_d(a, ltm_prime_tab[PRIME_SIZE-1]) == MP_LT) {
  35. /* find which prime it is bigger than */
  36. for (x = PRIME_SIZE - 2; x >= 0; x--) {
  37. if (mp_cmp_d(a, ltm_prime_tab[x]) != MP_LT) {
  38. if (bbs_style == 1) {
  39. /* ok we found a prime smaller or
  40. * equal [so the next is larger]
  41. *
  42. * however, the prime must be
  43. * congruent to 3 mod 4
  44. */
  45. if ((ltm_prime_tab[x + 1] & 3) != 3) {
  46. /* scan upwards for a prime congruent to 3 mod 4 */
  47. for (y = x + 1; y < PRIME_SIZE; y++) {
  48. if ((ltm_prime_tab[y] & 3) == 3) {
  49. mp_set(a, ltm_prime_tab[y]);
  50. return MP_OKAY;
  51. }
  52. }
  53. }
  54. } else {
  55. mp_set(a, ltm_prime_tab[x + 1]);
  56. return MP_OKAY;
  57. }
  58. }
  59. }
  60. /* at this point a maybe 1 */
  61. if (mp_cmp_d(a, 1) == MP_EQ) {
  62. mp_set(a, 2);
  63. return MP_OKAY;
  64. }
  65. /* fall through to the sieve */
  66. }
  67. /* generate a prime congruent to 3 mod 4 or 1/3 mod 4? */
  68. if (bbs_style == 1) {
  69. kstep = 4;
  70. } else {
  71. kstep = 2;
  72. }
  73. /* at this point we will use a combination of a sieve and Miller-Rabin */
  74. if (bbs_style == 1) {
  75. /* if a mod 4 != 3 subtract the correct value to make it so */
  76. if ((a->dp[0] & 3) != 3) {
  77. if ((err = mp_sub_d(a, (a->dp[0] & 3) + 1, a)) != MP_OKAY) { return err; };
  78. }
  79. } else {
  80. if (mp_iseven(a) == 1) {
  81. /* force odd */
  82. if ((err = mp_sub_d(a, 1, a)) != MP_OKAY) {
  83. return err;
  84. }
  85. }
  86. }
  87. /* generate the restable */
  88. for (x = 1; x < PRIME_SIZE; x++) {
  89. if ((err = mp_mod_d(a, ltm_prime_tab[x], res_tab + x)) != MP_OKAY) {
  90. return err;
  91. }
  92. }
  93. /* init temp used for Miller-Rabin Testing */
  94. if ((err = mp_init(&b)) != MP_OKAY) {
  95. return err;
  96. }
  97. for (;;) {
  98. /* skip to the next non-trivially divisible candidate */
  99. step = 0;
  100. do {
  101. /* y == 1 if any residue was zero [e.g. cannot be prime] */
  102. y = 0;
  103. /* increase step to next candidate */
  104. step += kstep;
  105. /* compute the new residue without using division */
  106. for (x = 1; x < PRIME_SIZE; x++) {
  107. /* add the step to each residue */
  108. res_tab[x] += kstep;
  109. /* subtract the modulus [instead of using division] */
  110. if (res_tab[x] >= ltm_prime_tab[x]) {
  111. res_tab[x] -= ltm_prime_tab[x];
  112. }
  113. /* set flag if zero */
  114. if (res_tab[x] == 0) {
  115. y = 1;
  116. }
  117. }
  118. } while (y == 1 && step < ((((mp_digit)1)<<DIGIT_BIT) - kstep));
  119. /* add the step */
  120. if ((err = mp_add_d(a, step, a)) != MP_OKAY) {
  121. goto LBL_ERR;
  122. }
  123. /* if didn't pass sieve and step == MAX then skip test */
  124. if (y == 1 && step >= ((((mp_digit)1)<<DIGIT_BIT) - kstep)) {
  125. continue;
  126. }
  127. /* is this prime? */
  128. for (x = 0; x < t; x++) {
  129. mp_set(&b, ltm_prime_tab[x]);
  130. if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) {
  131. goto LBL_ERR;
  132. }
  133. if (res == MP_NO) {
  134. break;
  135. }
  136. }
  137. if (res == MP_YES) {
  138. break;
  139. }
  140. }
  141. err = MP_OKAY;
  142. LBL_ERR:
  143. mp_clear(&b);
  144. return err;
  145. }
  146. #endif
  147. /* $Source: /cvs/libtom/libtommath/bn_mp_prime_next_prime.c,v $ */
  148. /* $Revision: 1.3 $ */
  149. /* $Date: 2006/03/31 14:18:44 $ */