bn_s_mp_invmod_fast.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "tommath_private.h"
  2. #ifdef BN_S_MP_INVMOD_FAST_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis */
  4. /* SPDX-License-Identifier: Unlicense */
  5. /* computes the modular inverse via binary extended euclidean algorithm,
  6. * that is c = 1/a mod b
  7. *
  8. * Based on slow invmod except this is optimized for the case where b is
  9. * odd as per HAC Note 14.64 on pp. 610
  10. */
  11. mp_err s_mp_invmod_fast(const mp_int *a, const mp_int *b, mp_int *c)
  12. {
  13. mp_int x, y, u, v, B, D;
  14. mp_sign neg;
  15. mp_err err;
  16. /* 2. [modified] b must be odd */
  17. if (MP_IS_EVEN(b)) {
  18. return MP_VAL;
  19. }
  20. /* init all our temps */
  21. if ((err = mp_init_multi(&x, &y, &u, &v, &B, &D, NULL)) != MP_OKAY) {
  22. return err;
  23. }
  24. /* x == modulus, y == value to invert */
  25. if ((err = mp_copy(b, &x)) != MP_OKAY) goto LBL_ERR;
  26. /* we need y = |a| */
  27. if ((err = mp_mod(a, b, &y)) != MP_OKAY) goto LBL_ERR;
  28. /* if one of x,y is zero return an error! */
  29. if (MP_IS_ZERO(&x) || MP_IS_ZERO(&y)) {
  30. err = MP_VAL;
  31. goto LBL_ERR;
  32. }
  33. /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
  34. if ((err = mp_copy(&x, &u)) != MP_OKAY) goto LBL_ERR;
  35. if ((err = mp_copy(&y, &v)) != MP_OKAY) goto LBL_ERR;
  36. mp_set(&D, 1uL);
  37. top:
  38. /* 4. while u is even do */
  39. while (MP_IS_EVEN(&u)) {
  40. /* 4.1 u = u/2 */
  41. if ((err = mp_div_2(&u, &u)) != MP_OKAY) goto LBL_ERR;
  42. /* 4.2 if B is odd then */
  43. if (MP_IS_ODD(&B)) {
  44. if ((err = mp_sub(&B, &x, &B)) != MP_OKAY) goto LBL_ERR;
  45. }
  46. /* B = B/2 */
  47. if ((err = mp_div_2(&B, &B)) != MP_OKAY) goto LBL_ERR;
  48. }
  49. /* 5. while v is even do */
  50. while (MP_IS_EVEN(&v)) {
  51. /* 5.1 v = v/2 */
  52. if ((err = mp_div_2(&v, &v)) != MP_OKAY) goto LBL_ERR;
  53. /* 5.2 if D is odd then */
  54. if (MP_IS_ODD(&D)) {
  55. /* D = (D-x)/2 */
  56. if ((err = mp_sub(&D, &x, &D)) != MP_OKAY) goto LBL_ERR;
  57. }
  58. /* D = D/2 */
  59. if ((err = mp_div_2(&D, &D)) != MP_OKAY) goto LBL_ERR;
  60. }
  61. /* 6. if u >= v then */
  62. if (mp_cmp(&u, &v) != MP_LT) {
  63. /* u = u - v, B = B - D */
  64. if ((err = mp_sub(&u, &v, &u)) != MP_OKAY) goto LBL_ERR;
  65. if ((err = mp_sub(&B, &D, &B)) != MP_OKAY) goto LBL_ERR;
  66. } else {
  67. /* v - v - u, D = D - B */
  68. if ((err = mp_sub(&v, &u, &v)) != MP_OKAY) goto LBL_ERR;
  69. if ((err = mp_sub(&D, &B, &D)) != MP_OKAY) goto LBL_ERR;
  70. }
  71. /* if not zero goto step 4 */
  72. if (!MP_IS_ZERO(&u)) {
  73. goto top;
  74. }
  75. /* now a = C, b = D, gcd == g*v */
  76. /* if v != 1 then there is no inverse */
  77. if (mp_cmp_d(&v, 1uL) != MP_EQ) {
  78. err = MP_VAL;
  79. goto LBL_ERR;
  80. }
  81. /* b is now the inverse */
  82. neg = a->sign;
  83. while (D.sign == MP_NEG) {
  84. if ((err = mp_add(&D, b, &D)) != MP_OKAY) goto LBL_ERR;
  85. }
  86. /* too big */
  87. while (mp_cmp_mag(&D, b) != MP_LT) {
  88. if ((err = mp_sub(&D, b, &D)) != MP_OKAY) goto LBL_ERR;
  89. }
  90. mp_exch(&D, c);
  91. c->sign = neg;
  92. err = MP_OKAY;
  93. LBL_ERR:
  94. mp_clear_multi(&x, &y, &u, &v, &B, &D, NULL);
  95. return err;
  96. }
  97. #endif