bn_mp_prime_miller_rabin.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include <tommath.h>
  2. #ifdef BN_MP_PRIME_MILLER_RABIN_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. /* Miller-Rabin test of "a" to the base of "b" as described in
  18. * HAC pp. 139 Algorithm 4.24
  19. *
  20. * Sets result to 0 if definitely composite or 1 if probably prime.
  21. * Randomly the chance of error is no more than 1/4 and often
  22. * very much lower.
  23. */
  24. int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
  25. {
  26. mp_int n1, y, r;
  27. int s, j, err;
  28. /* default */
  29. *result = MP_NO;
  30. /* ensure b > 1 */
  31. if (mp_cmp_d(b, 1) != MP_GT) {
  32. return MP_VAL;
  33. }
  34. /* get n1 = a - 1 */
  35. if ((err = mp_init_copy (&n1, a)) != MP_OKAY) {
  36. return err;
  37. }
  38. if ((err = mp_sub_d (&n1, 1, &n1)) != MP_OKAY) {
  39. goto LBL_N1;
  40. }
  41. /* set 2**s * r = n1 */
  42. if ((err = mp_init_copy (&r, &n1)) != MP_OKAY) {
  43. goto LBL_N1;
  44. }
  45. /* count the number of least significant bits
  46. * which are zero
  47. */
  48. s = mp_cnt_lsb(&r);
  49. /* now divide n - 1 by 2**s */
  50. if ((err = mp_div_2d (&r, s, &r, NULL)) != MP_OKAY) {
  51. goto LBL_R;
  52. }
  53. /* compute y = b**r mod a */
  54. if ((err = mp_init (&y)) != MP_OKAY) {
  55. goto LBL_R;
  56. }
  57. if ((err = mp_exptmod (b, &r, a, &y)) != MP_OKAY) {
  58. goto LBL_Y;
  59. }
  60. /* if y != 1 and y != n1 do */
  61. if (mp_cmp_d (&y, 1) != MP_EQ && mp_cmp (&y, &n1) != MP_EQ) {
  62. j = 1;
  63. /* while j <= s-1 and y != n1 */
  64. while ((j <= (s - 1)) && mp_cmp (&y, &n1) != MP_EQ) {
  65. if ((err = mp_sqrmod (&y, a, &y)) != MP_OKAY) {
  66. goto LBL_Y;
  67. }
  68. /* if y == 1 then composite */
  69. if (mp_cmp_d (&y, 1) == MP_EQ) {
  70. goto LBL_Y;
  71. }
  72. ++j;
  73. }
  74. /* if y != n1 then composite */
  75. if (mp_cmp (&y, &n1) != MP_EQ) {
  76. goto LBL_Y;
  77. }
  78. }
  79. /* probably prime now */
  80. *result = MP_YES;
  81. LBL_Y:mp_clear (&y);
  82. LBL_R:mp_clear (&r);
  83. LBL_N1:mp_clear (&n1);
  84. return err;
  85. }
  86. #endif
  87. /* $Source: /cvs/libtom/libtommath/bn_mp_prime_miller_rabin.c,v $ */
  88. /* $Revision: 1.3 $ */
  89. /* $Date: 2006/03/31 14:18:44 $ */