1 #include "tommath_private.h"
2 #ifdef BN_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 * SPDX-License-Identifier: Unlicense
13 */
14
15 /* hac 14.61, pp608 */
mp_invmod(const mp_int * a,const mp_int * b,mp_int * c)16 int mp_invmod(const mp_int *a, const mp_int *b, mp_int *c)
17 {
18 /* b cannot be negative and has to be >1 */
19 if ((b->sign == MP_NEG) || (mp_cmp_d(b, 1uL) != MP_GT)) {
20 return MP_VAL;
21 }
22
23 #ifdef BN_FAST_MP_INVMOD_C
24 /* if the modulus is odd we can use a faster routine instead */
25 if ((mp_isodd(b) == MP_YES)) {
26 return fast_mp_invmod(a, b, c);
27 }
28 #endif
29
30 #ifdef BN_MP_INVMOD_SLOW_C
31 return mp_invmod_slow(a, b, c);
32 #else
33 return MP_VAL;
34 #endif
35 }
36 #endif
37
38 /* ref: $Format:%D$ */
39 /* git commit: $Format:%H$ */
40 /* commit time: $Format:%ai$ */
41