bn_mp_invmod.c 650 B

1234567891011121314151617181920212223
  1. #include "tommath_private.h"
  2. #ifdef BN_MP_INVMOD_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis */
  4. /* SPDX-License-Identifier: Unlicense */
  5. /* hac 14.61, pp608 */
  6. mp_err mp_invmod(const mp_int *a, const mp_int *b, mp_int *c)
  7. {
  8. /* b cannot be negative and has to be >1 */
  9. if ((b->sign == MP_NEG) || (mp_cmp_d(b, 1uL) != MP_GT)) {
  10. return MP_VAL;
  11. }
  12. /* if the modulus is odd we can use a faster routine instead */
  13. if (MP_HAS(S_MP_INVMOD_FAST) && MP_IS_ODD(b)) {
  14. return s_mp_invmod_fast(a, b, c);
  15. }
  16. return MP_HAS(S_MP_INVMOD_SLOW)
  17. ? s_mp_invmod_slow(a, b, c)
  18. : MP_VAL;
  19. }
  20. #endif