/src/gmp-6.2.1/mpz/invert.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /* mpz_invert (inv, x, n).  Find multiplicative inverse of X in Z(N).  | 
2  |  |    If X has an inverse, return non-zero and store inverse in INVERSE,  | 
3  |  |    otherwise, return 0 and put garbage in INVERSE.  | 
4  |  |  | 
5  |  | Copyright 1996-2001, 2005, 2012, 2014 Free Software Foundation, Inc.  | 
6  |  |  | 
7  |  | This file is part of the GNU MP Library.  | 
8  |  |  | 
9  |  | The GNU MP Library is free software; you can redistribute it and/or modify  | 
10  |  | it under the terms of either:  | 
11  |  |  | 
12  |  |   * the GNU Lesser General Public License as published by the Free  | 
13  |  |     Software Foundation; either version 3 of the License, or (at your  | 
14  |  |     option) any later version.  | 
15  |  |  | 
16  |  | or  | 
17  |  |  | 
18  |  |   * the GNU General Public License as published by the Free Software  | 
19  |  |     Foundation; either version 2 of the License, or (at your option) any  | 
20  |  |     later version.  | 
21  |  |  | 
22  |  | or both in parallel, as here.  | 
23  |  |  | 
24  |  | The GNU MP Library is distributed in the hope that it will be useful, but  | 
25  |  | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY  | 
26  |  | or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License  | 
27  |  | for more details.  | 
28  |  |  | 
29  |  | You should have received copies of the GNU General Public License and the  | 
30  |  | GNU Lesser General Public License along with the GNU MP Library.  If not,  | 
31  |  | see https://www.gnu.org/licenses/.  */  | 
32  |  |  | 
33  |  | #include "gmp-impl.h"  | 
34  |  |  | 
35  |  | int  | 
36  |  | mpz_invert (mpz_ptr inverse, mpz_srcptr x, mpz_srcptr n)  | 
37  | 1.43k  | { | 
38  | 1.43k  |   mpz_t gcd, tmp;  | 
39  | 1.43k  |   mp_size_t xsize, nsize, size;  | 
40  | 1.43k  |   TMP_DECL;  | 
41  |  |  | 
42  | 1.43k  |   xsize = ABSIZ (x);  | 
43  | 1.43k  |   nsize = ABSIZ (n);  | 
44  |  |  | 
45  | 1.43k  |   size = MAX (xsize, nsize) + 1;  | 
46  | 1.43k  |   TMP_MARK;  | 
47  |  |  | 
48  | 1.43k  |   MPZ_TMP_INIT (gcd, size);  | 
49  | 1.43k  |   MPZ_TMP_INIT (tmp, size);  | 
50  | 1.43k  |   mpz_gcdext (gcd, tmp, (mpz_ptr) 0, x, n);  | 
51  |  |  | 
52  |  |   /* If no inverse existed, return with an indication of that.  */  | 
53  | 1.43k  |   if (!MPZ_EQUAL_1_P (gcd))  | 
54  | 775  |     { | 
55  | 775  |       TMP_FREE;  | 
56  | 775  |       return 0;  | 
57  | 775  |     }  | 
58  |  |  | 
59  |  |   /* Make sure we return a positive inverse.  */  | 
60  | 662  |   if (SIZ (tmp) < 0)  | 
61  | 324  |     { | 
62  | 324  |       if (SIZ (n) < 0)  | 
63  | 0  |   mpz_sub (inverse, tmp, n);  | 
64  | 324  |       else  | 
65  | 324  |   mpz_add (inverse, tmp, n);  | 
66  | 324  |     }  | 
67  | 338  |   else  | 
68  | 338  |     mpz_set (inverse, tmp);  | 
69  |  |  | 
70  | 662  |   TMP_FREE;  | 
71  | 662  |   return 1;  | 
72  | 1.43k  | }  |