bn_mp_invmod_slow.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include <tommath.h>
  2. #ifdef BN_MP_INVMOD_SLOW_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. /* hac 14.61, pp608 */
  18. int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c)
  19. {
  20. mp_int x, y, u, v, A, B, C, D;
  21. int res;
  22. /* b cannot be negative */
  23. if (b->sign == MP_NEG || mp_iszero(b) == 1) {
  24. return MP_VAL;
  25. }
  26. /* init temps */
  27. if ((res = mp_init_multi(&x, &y, &u, &v,
  28. &A, &B, &C, &D, NULL)) != MP_OKAY) {
  29. return res;
  30. }
  31. /* x = a, y = b */
  32. if ((res = mp_mod(a, b, &x)) != MP_OKAY) {
  33. goto LBL_ERR;
  34. }
  35. if ((res = mp_copy (b, &y)) != MP_OKAY) {
  36. goto LBL_ERR;
  37. }
  38. /* 2. [modified] if x,y are both even then return an error! */
  39. if (mp_iseven (&x) == 1 && mp_iseven (&y) == 1) {
  40. res = MP_VAL;
  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 (&A, 1);
  51. mp_set (&D, 1);
  52. top:
  53. /* 4. while u is even do */
  54. while (mp_iseven (&u) == 1) {
  55. /* 4.1 u = u/2 */
  56. if ((res = mp_div_2 (&u, &u)) != MP_OKAY) {
  57. goto LBL_ERR;
  58. }
  59. /* 4.2 if A or B is odd then */
  60. if (mp_isodd (&A) == 1 || mp_isodd (&B) == 1) {
  61. /* A = (A+y)/2, B = (B-x)/2 */
  62. if ((res = mp_add (&A, &y, &A)) != MP_OKAY) {
  63. goto LBL_ERR;
  64. }
  65. if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) {
  66. goto LBL_ERR;
  67. }
  68. }
  69. /* A = A/2, B = B/2 */
  70. if ((res = mp_div_2 (&A, &A)) != MP_OKAY) {
  71. goto LBL_ERR;
  72. }
  73. if ((res = mp_div_2 (&B, &B)) != MP_OKAY) {
  74. goto LBL_ERR;
  75. }
  76. }
  77. /* 5. while v is even do */
  78. while (mp_iseven (&v) == 1) {
  79. /* 5.1 v = v/2 */
  80. if ((res = mp_div_2 (&v, &v)) != MP_OKAY) {
  81. goto LBL_ERR;
  82. }
  83. /* 5.2 if C or D is odd then */
  84. if (mp_isodd (&C) == 1 || mp_isodd (&D) == 1) {
  85. /* C = (C+y)/2, D = (D-x)/2 */
  86. if ((res = mp_add (&C, &y, &C)) != MP_OKAY) {
  87. goto LBL_ERR;
  88. }
  89. if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) {
  90. goto LBL_ERR;
  91. }
  92. }
  93. /* C = C/2, D = D/2 */
  94. if ((res = mp_div_2 (&C, &C)) != MP_OKAY) {
  95. goto LBL_ERR;
  96. }
  97. if ((res = mp_div_2 (&D, &D)) != MP_OKAY) {
  98. goto LBL_ERR;
  99. }
  100. }
  101. /* 6. if u >= v then */
  102. if (mp_cmp (&u, &v) != MP_LT) {
  103. /* u = u - v, A = A - C, B = B - D */
  104. if ((res = mp_sub (&u, &v, &u)) != MP_OKAY) {
  105. goto LBL_ERR;
  106. }
  107. if ((res = mp_sub (&A, &C, &A)) != MP_OKAY) {
  108. goto LBL_ERR;
  109. }
  110. if ((res = mp_sub (&B, &D, &B)) != MP_OKAY) {
  111. goto LBL_ERR;
  112. }
  113. } else {
  114. /* v - v - u, C = C - A, D = D - B */
  115. if ((res = mp_sub (&v, &u, &v)) != MP_OKAY) {
  116. goto LBL_ERR;
  117. }
  118. if ((res = mp_sub (&C, &A, &C)) != MP_OKAY) {
  119. goto LBL_ERR;
  120. }
  121. if ((res = mp_sub (&D, &B, &D)) != MP_OKAY) {
  122. goto LBL_ERR;
  123. }
  124. }
  125. /* if not zero goto step 4 */
  126. if (mp_iszero (&u) == 0)
  127. goto top;
  128. /* now a = C, b = D, gcd == g*v */
  129. /* if v != 1 then there is no inverse */
  130. if (mp_cmp_d (&v, 1) != MP_EQ) {
  131. res = MP_VAL;
  132. goto LBL_ERR;
  133. }
  134. /* if its too low */
  135. while (mp_cmp_d(&C, 0) == MP_LT) {
  136. if ((res = mp_add(&C, b, &C)) != MP_OKAY) {
  137. goto LBL_ERR;
  138. }
  139. }
  140. /* too big */
  141. while (mp_cmp_mag(&C, b) != MP_LT) {
  142. if ((res = mp_sub(&C, b, &C)) != MP_OKAY) {
  143. goto LBL_ERR;
  144. }
  145. }
  146. /* C is now the inverse */
  147. mp_exch (&C, c);
  148. res = MP_OKAY;
  149. LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &A, &B, &C, &D, NULL);
  150. return res;
  151. }
  152. #endif
  153. /* $Source: /cvs/libtom/libtommath/bn_mp_invmod_slow.c,v $ */
  154. /* $Revision: 1.3 $ */
  155. /* $Date: 2006/03/31 14:18:44 $ */