Skip to content

Commit 6db1a87

Browse files
committed
remove comment and add inverse_mod
1 parent f793241 commit 6db1a87

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/flint/test/test_all.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -3986,7 +3986,6 @@ def test_fq_default_poly():
39863986
# pow_mod
39873987
assert f.pow_mod(2, g) == (f*f) % g
39883988
assert raises(lambda: f.pow_mod(2, "AAA"), TypeError)
3989-
39903989
assert raises(lambda: f.complex_roots(), DomainError)
39913990

39923991
# compose errors
@@ -3995,6 +3994,16 @@ def test_fq_default_poly():
39953994
assert raises(lambda: f.compose_mod(g, "A"), TypeError)
39963995
assert raises(lambda: f.compose_mod(g, R_test.zero()), ZeroDivisionError)
39973996

3997+
# inverse_mod
3998+
f = R_test.random_element()
3999+
while True:
4000+
h = R_test.random_element()
4001+
if f.gcd(h).is_one():
4002+
break
4003+
g = f.inverse_mod(h)
4004+
assert f.mul_mod(g, h).is_one()
4005+
assert raises(lambda: f.inverse_mod(2*f), ValueError)
4006+
39984007
# series
39994008
f_non_square = R_test([nqr, 1, 1, 1])
40004009
f_zero = R_test([0, 1, 1, 1])

src/flint/types/fq_default_poly.pyx

+17-3
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,6 @@ cdef class fq_default_poly_ctx:
169169
``True``, ensures the output is monic. If ``irreducible`` is
170170
``True``, ensures that the output is irreducible.
171171
172-
TODO: there is currently no fq_defualt_poly method for testing that
173-
a polynomial is irreducible?!
174-
175172
>>> R = fq_default_poly_ctx(163, 3)
176173
>>> f = R.random_element()
177174
>>> f.degree() <= 3
@@ -1316,6 +1313,23 @@ cdef class fq_default_poly(flint_poly):
13161313

13171314
return (G, S, T)
13181315

1316+
def inverse_mod(self, other):
1317+
"""
1318+
Returns the inverse of ``self`` modulo ``other``
1319+
1320+
>>> R = fq_default_poly_ctx(163)
1321+
>>> f = R([123, 129, 63, 14, 51, 76, 133])
1322+
>>> h = R([139, 9, 35, 154, 87, 120, 24])
1323+
>>> g = f.inverse_mod(h)
1324+
>>> g
1325+
41*x^5 + 121*x^4 + 47*x^3 + 41*x^2 + 6*x + 5
1326+
>>> assert f.mul_mod(g, h).is_one()
1327+
"""
1328+
G, S, _ = self.xgcd(other)
1329+
if not G.is_one():
1330+
raise ValueError(f"polynomial has no inverse modulo {other = }")
1331+
return S
1332+
13191333
# ====================================
13201334
# Derivative
13211335
# ====================================

0 commit comments

Comments
 (0)