bn_s_mp_invmod_slow.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "tommath_private.h"
  2. #ifdef BN_S_MP_INVMOD_SLOW_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis */
  4. /* SPDX-License-Identifier: Unlicense */
  5. /* hac 14.61, pp608 */
  6. mp_err s_mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c)
  7. {
  8. mp_int x, y, u, v, A, B, C, D;
  9. mp_err err;
  10. /* b cannot be negative */
  11. if ((b->sign == MP_NEG) || MP_IS_ZERO(b)) {
  12. return MP_VAL;
  13. }
  14. /* init temps */
  15. if ((err = mp_init_multi(&x, &y, &u, &v,
  16. &A, &B, &C, &D, NULL)) != MP_OKAY) {
  17. return err;
  18. }
  19. /* x = a, y = b */
  20. if ((err = mp_mod(a, b, &x)) != MP_OKAY) goto LBL_ERR;
  21. if ((err = mp_copy(b, &y)) != MP_OKAY) goto LBL_ERR;
  22. /* 2. [modified] if x,y are both even then return an error! */
  23. if (MP_IS_EVEN(&x) && MP_IS_EVEN(&y)) {
  24. err = MP_VAL;
  25. goto LBL_ERR;
  26. }
  27. /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
  28. if ((err = mp_copy(&x, &u)) != MP_OKAY) goto LBL_ERR;
  29. if ((err = mp_copy(&y, &v)) != MP_OKAY) goto LBL_ERR;
  30. mp_set(&A, 1uL);
  31. mp_set(&D, 1uL);
  32. top:
  33. /* 4. while u is even do */
  34. while (MP_IS_EVEN(&u)) {
  35. /* 4.1 u = u/2 */
  36. if ((err = mp_div_2(&u, &u)) != MP_OKAY) goto LBL_ERR;
  37. /* 4.2 if A or B is odd then */
  38. if (MP_IS_ODD(&A) || MP_IS_ODD(&B)) {
  39. /* A = (A+y)/2, B = (B-x)/2 */
  40. if ((err = mp_add(&A, &y, &A)) != MP_OKAY) goto LBL_ERR;
  41. if ((err = mp_sub(&B, &x, &B)) != MP_OKAY) goto LBL_ERR;
  42. }
  43. /* A = A/2, B = B/2 */
  44. if ((err = mp_div_2(&A, &A)) != MP_OKAY) goto LBL_ERR;
  45. if ((err = mp_div_2(&B, &B)) != MP_OKAY) goto LBL_ERR;
  46. }
  47. /* 5. while v is even do */
  48. while (MP_IS_EVEN(&v)) {
  49. /* 5.1 v = v/2 */
  50. if ((err = mp_div_2(&v, &v)) != MP_OKAY) goto LBL_ERR;
  51. /* 5.2 if C or D is odd then */
  52. if (MP_IS_ODD(&C) || MP_IS_ODD(&D)) {
  53. /* C = (C+y)/2, D = (D-x)/2 */
  54. if ((err = mp_add(&C, &y, &C)) != MP_OKAY) goto LBL_ERR;
  55. if ((err = mp_sub(&D, &x, &D)) != MP_OKAY) goto LBL_ERR;
  56. }
  57. /* C = C/2, D = D/2 */
  58. if ((err = mp_div_2(&C, &C)) != MP_OKAY) goto LBL_ERR;
  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, A = A - C, B = B - D */
  64. if ((err = mp_sub(&u, &v, &u)) != MP_OKAY) goto LBL_ERR;
  65. if ((err = mp_sub(&A, &C, &A)) != MP_OKAY) goto LBL_ERR;
  66. if ((err = mp_sub(&B, &D, &B)) != MP_OKAY) goto LBL_ERR;
  67. } else {
  68. /* v - v - u, C = C - A, D = D - B */
  69. if ((err = mp_sub(&v, &u, &v)) != MP_OKAY) goto LBL_ERR;
  70. if ((err = mp_sub(&C, &A, &C)) != MP_OKAY) goto LBL_ERR;
  71. if ((err = mp_sub(&D, &B, &D)) != MP_OKAY) goto LBL_ERR;
  72. }
  73. /* if not zero goto step 4 */
  74. if (!MP_IS_ZERO(&u)) {
  75. goto top;
  76. }
  77. /* now a = C, b = D, gcd == g*v */
  78. /* if v != 1 then there is no inverse */
  79. if (mp_cmp_d(&v, 1uL) != MP_EQ) {
  80. err = MP_VAL;
  81. goto LBL_ERR;
  82. }
  83. /* if its too low */
  84. while (mp_cmp_d(&C, 0uL) == MP_LT) {
  85. if ((err = mp_add(&C, b, &C)) != MP_OKAY) goto LBL_ERR;
  86. }
  87. /* too big */
  88. while (mp_cmp_mag(&C, b) != MP_LT) {
  89. if ((err = mp_sub(&C, b, &C)) != MP_OKAY) goto LBL_ERR;
  90. }
  91. /* C is now the inverse */
  92. mp_exch(&C, c);
  93. err = MP_OKAY;
  94. LBL_ERR:
  95. mp_clear_multi(&x, &y, &u, &v, &A, &B, &C, &D, NULL);
  96. return err;
  97. }
  98. #endif