1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
Copyright (C) 2009 William Hart
This file is part of FLINT.
FLINT is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (LGPL) as published
by the Free Software Foundation; either version 3 of the License, or
(at your option) any later version. See <https://www.gnu.org/licenses/>.
*/
#include "gmpcompat.h"
#include "ulong_extras.h"
#include "fmpz.h"
static ulong
z_gcdinv(ulong * inv, slong a, ulong b)
{
ulong g, ua = FLINT_ABS(a);
if (ua >= b)
ua %= b;
g = n_gcdinv(inv, ua, b);
if (a < WORD(0))
*inv = n_submod(UWORD(0), *inv, b);
return g;
}
int
fmpz_invmod(fmpz_t f, const fmpz_t g, const fmpz_t h)
{
fmpz c1 = *g;
fmpz c2 = *h;
int val;
if (fmpz_is_zero(h))
{
flint_throw(FLINT_DIVZERO, "Exception (fmpz_invmod). Division by zero.\n");
}
if (!COEFF_IS_MPZ(c1)) /* g is small */
{
if (!COEFF_IS_MPZ(c2)) /* h is also small */
{
ulong inv, gcd;
if (c2 < WORD(0))
c2 = -c2;
if (c2 == WORD(1))
{
fmpz_zero(f);
return 1; /* special case not handled by n_invmod */
}
gcd = z_gcdinv(&inv, c1, c2);
return (gcd == UWORD(1) ? fmpz_set_si(f, inv), 1 : 0);
}
else /* h is large and g is small */
{
__mpz_struct temp; /* put g into a temporary mpz_t */
mpz_ptr mf;
if (c1 < WORD(0))
{
c1 = -c1;
temp._mp_d = (ulong *) & c1;
temp._mp_size = -1;
}
else if (c1 == WORD(0))
temp._mp_size = 0;
else
{
temp._mp_d = (ulong *) & c1;
temp._mp_size = 1;
}
mf = _fmpz_promote(f);
val = mpz_invert(mf, &temp, COEFF_TO_PTR(c2));
_fmpz_demote_val(f); /* inverse mod h may result in small value */
return val;
}
}
else /* g is large */
{
if (!COEFF_IS_MPZ(c2)) /* h is small */
{
ulong gcd, inv, r;
if (c2 < WORD(0))
c2 = -c2;
if (c2 == WORD(1))
{
fmpz_zero(f);
return 1; /* special case not handled by z_gcd_invert */
}
/* reduce g mod h first */
r = flint_mpz_fdiv_ui(COEFF_TO_PTR(c1), c2);
gcd = z_gcdinv(&inv, r, c2);
return (gcd == UWORD(1) ? fmpz_set_si(f, inv), 1 : 0);
}
else /* both are large */
{
mpz_ptr mf = _fmpz_promote(f);
val = mpz_invert(mf, COEFF_TO_PTR(c1), COEFF_TO_PTR(c2));
_fmpz_demote_val(f); /* reduction mod h may result in small value */
return val;
}
}
}