Skip to content

Commit

Permalink
remove comment and add inverse_mod
Browse files Browse the repository at this point in the history
  • Loading branch information
GiacomoPope committed Aug 15, 2024
1 parent f793241 commit 6db1a87
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/flint/test/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -3986,7 +3986,6 @@ def test_fq_default_poly():
# pow_mod
assert f.pow_mod(2, g) == (f*f) % g
assert raises(lambda: f.pow_mod(2, "AAA"), TypeError)

assert raises(lambda: f.complex_roots(), DomainError)

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

# inverse_mod
f = R_test.random_element()
while True:
h = R_test.random_element()
if f.gcd(h).is_one():
break
g = f.inverse_mod(h)
assert f.mul_mod(g, h).is_one()
assert raises(lambda: f.inverse_mod(2*f), ValueError)

# series
f_non_square = R_test([nqr, 1, 1, 1])
f_zero = R_test([0, 1, 1, 1])
Expand Down
20 changes: 17 additions & 3 deletions src/flint/types/fq_default_poly.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,6 @@ cdef class fq_default_poly_ctx:
``True``, ensures the output is monic. If ``irreducible`` is
``True``, ensures that the output is irreducible.
TODO: there is currently no fq_defualt_poly method for testing that
a polynomial is irreducible?!
>>> R = fq_default_poly_ctx(163, 3)
>>> f = R.random_element()
>>> f.degree() <= 3
Expand Down Expand Up @@ -1316,6 +1313,23 @@ cdef class fq_default_poly(flint_poly):

return (G, S, T)

def inverse_mod(self, other):
"""
Returns the inverse of ``self`` modulo ``other``
>>> R = fq_default_poly_ctx(163)
>>> f = R([123, 129, 63, 14, 51, 76, 133])
>>> h = R([139, 9, 35, 154, 87, 120, 24])
>>> g = f.inverse_mod(h)
>>> g
41*x^5 + 121*x^4 + 47*x^3 + 41*x^2 + 6*x + 5
>>> assert f.mul_mod(g, h).is_one()
"""
G, S, _ = self.xgcd(other)
if not G.is_one():
raise ValueError(f"polynomial has no inverse modulo {other = }")
return S

# ====================================
# Derivative
# ====================================
Expand Down

0 comments on commit 6db1a87

Please sign in to comment.