bn_fast_mp_invmod.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include <tommath.h>
  2. #ifdef BN_FAST_MP_INVMOD_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 the modular inverse via binary extended euclidean algorithm,
  18. * that is c = 1/a mod b
  19. *
  20. * Based on slow invmod except this is optimized for the case where b is
  21. * odd as per HAC Note 14.64 on pp. 610
  22. */
  23. int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
  24. {
  25. mp_int x, y, u, v, B, D;
  26. int res, neg;
  27. /* 2. [modified] b must be odd */
  28. if (mp_iseven (b) == 1) {
  29. return MP_VAL;
  30. }
  31. /* init all our temps */
  32. if ((res = mp_init_multi(&x, &y, &u, &v, &B, &D, NULL)) != MP_OKAY) {
  33. return res;
  34. }
  35. /* x == modulus, y == value to invert */
  36. if ((res = mp_copy (b, &x)) != MP_OKAY) {
  37. goto LBL_ERR;
  38. }
  39. /* we need y = |a| */
  40. if ((res = mp_mod (a, b, &y)) != MP_OKAY) {
  41. goto LBL_ERR;
  42. }
  43. /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
  44. if ((res = mp_copy (&x, &u)) != MP_OKAY) {
  45. goto LBL_ERR;
  46. }
  47. if ((res = mp_copy (&y, &v)) != MP_OKAY) {
  48. goto LBL_ERR;
  49. }
  50. mp_set (&D, 1);
  51. top:
  52. /* 4. while u is even do */
  53. while (mp_iseven (&u) == 1) {
  54. /* 4.1 u = u/2 */
  55. if ((res = mp_div_2 (&u, &u)) != MP_OKAY) {
  56. goto LBL_ERR;
  57. }
  58. /* 4.2 if B is odd then */
  59. if (mp_isodd (&B) == 1) {
  60. if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) {
  61. goto LBL_ERR;
  62. }
  63. }
  64. /* B = B/2 */
  65. if ((res = mp_div_2 (&B, &B)) != MP_OKAY) {
  66. goto LBL_ERR;
  67. }
  68. }
  69. /* 5. while v is even do */
  70. while (mp_iseven (&v) == 1) {
  71. /* 5.1 v = v/2 */
  72. if ((res = mp_div_2 (&v, &v)) != MP_OKAY) {
  73. goto LBL_ERR;
  74. }
  75. /* 5.2 if D is odd then */
  76. if (mp_isodd (&D) == 1) {
  77. /* D = (D-x)/2 */
  78. if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) {
  79. goto LBL_ERR;
  80. }
  81. }
  82. /* D = D/2 */
  83. if ((res = mp_div_2 (&D, &D)) != MP_OKAY) {
  84. goto LBL_ERR;
  85. }
  86. }
  87. /* 6. if u >= v then */
  88. if (mp_cmp (&u, &v) != MP_LT) {
  89. /* u = u - v, B = B - D */
  90. if ((res = mp_sub (&u, &v, &u)) != MP_OKAY) {
  91. goto LBL_ERR;
  92. }
  93. if ((res = mp_sub (&B, &D, &B)) != MP_OKAY) {
  94. goto LBL_ERR;
  95. }
  96. } else {
  97. /* v - v - u, D = D - B */
  98. if ((res = mp_sub (&v, &u, &v)) != MP_OKAY) {
  99. goto LBL_ERR;
  100. }
  101. if ((res = mp_sub (&D, &B, &D)) != MP_OKAY) {
  102. goto LBL_ERR;
  103. }
  104. }
  105. /* if not zero goto step 4 */
  106. if (mp_iszero (&u) == 0) {
  107. goto top;
  108. }
  109. /* now a = C, b = D, gcd == g*v */
  110. /* if v != 1 then there is no inverse */
  111. if (mp_cmp_d (&v, 1) != MP_EQ) {
  112. res = MP_VAL;
  113. goto LBL_ERR;
  114. }
  115. /* b is now the inverse */
  116. neg = a->sign;
  117. while (D.sign == MP_NEG) {
  118. if ((res = mp_add (&D, b, &D)) != MP_OKAY) {
  119. goto LBL_ERR;
  120. }
  121. }
  122. mp_exch (&D, c);
  123. c->sign = neg;
  124. res = MP_OKAY;
  125. LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &B, &D, NULL);
  126. return res;
  127. }
  128. #endif
  129. /* $Source: /cvs/libtom/libtommath/bn_fast_mp_invmod.c,v $ */
  130. /* $Revision: 1.3 $ */
  131. /* $Date: 2006/03/31 14:18:44 $ */