Skip to content

Commit

Permalink
powmod to pow_mod
Browse files Browse the repository at this point in the history
  • Loading branch information
GiacomoPope committed Aug 6, 2024
1 parent 9589530 commit ae31437
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
12 changes: 6 additions & 6 deletions src/flint/test/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -2095,17 +2095,17 @@ def test_fmpz_mod_poly():
assert f*f == f**2
assert f*f == f**fmpz(2)

# powmod
# pow_mod
# assert ui and fmpz exp agree for polynomials and generators
R_gen = R_test.gen()
assert pow(f, 2**60, g) == pow(pow(f, 2**30, g), 2**30, g)
assert pow(R_gen, 2**60, g) == pow(pow(R_gen, 2**30, g), 2**30, g)

# Check other typechecks for powmod
# Check other typechecks for pow_mod
assert raises(lambda: pow(f, -2, g), ValueError)
assert raises(lambda: pow(f, 1, "A"), TypeError)
assert raises(lambda: pow(f, "A", g), TypeError)
assert raises(lambda: f.powmod(2**32, g, mod_rev_inv="A"), TypeError)
assert raises(lambda: f.pow_mod(2**32, g, mod_rev_inv="A"), TypeError)

# Shifts
assert raises(lambda: R_test([1,2,3]).left_shift(-1), ValueError)
Expand Down Expand Up @@ -2162,9 +2162,9 @@ def test_fmpz_mod_poly():
assert raises(lambda: f.mulmod(f, "AAA"), TypeError)
assert raises(lambda: f.mulmod("AAA", g), TypeError)

# powmod
assert f.powmod(2, g) == (f*f) % g
assert raises(lambda: f.powmod(2, "AAA"), TypeError)
# pow_mod
assert f.pow_mod(2, g) == (f*f) % g
assert raises(lambda: f.pow_mod(2, "AAA"), TypeError)

# divmod
S, T = f.divmod(g)
Expand Down
12 changes: 6 additions & 6 deletions src/flint/types/fmpz_mod_poly.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ cdef class fmpz_mod_poly(flint_poly):

def __pow__(self, e, mod=None):
if mod is not None:
return self.powmod(e, mod)
return self.pow_mod(e, mod)

cdef fmpz_mod_poly res
if e < 0:
Expand Down Expand Up @@ -1137,13 +1137,13 @@ cdef class fmpz_mod_poly(flint_poly):
)
return res

def powmod(self, e, modulus, mod_rev_inv=None):
def pow_mod(self, e, modulus, mod_rev_inv=None):
"""
Returns ``self`` raised to the power ``e`` modulo ``modulus``:
:math:`f^e \mod g`/
``mod_rev_inv`` is the inverse of the reverse of the modulus,
precomputing it and passing it to ``powmod()`` can optimise
precomputing it and passing it to ``pow_mod()`` can optimise
powering of polynomials with large exponents.
>>> R = fmpz_mod_poly_ctx(163)
Expand All @@ -1152,12 +1152,12 @@ cdef class fmpz_mod_poly(flint_poly):
>>> g = 43*x**6 + 91*x**5 + 77*x**4 + 113*x**3 + 71*x**2 + 132*x + 60
>>> mod = x**4 + 93*x**3 + 78*x**2 + 72*x + 149
>>>
>>> f.powmod(123, mod)
>>> f.pow_mod(123, mod)
3*x^3 + 25*x^2 + 115*x + 161
>>> f.powmod(2**64, mod)
>>> f.pow_mod(2**64, mod)
52*x^3 + 96*x^2 + 136*x + 9
>>> mod_rev_inv = mod.reverse().inverse_series_trunc(4)
>>> f.powmod(2**64, mod, mod_rev_inv)
>>> f.pow_mod(2**64, mod, mod_rev_inv)
52*x^3 + 96*x^2 + 136*x + 9
"""
cdef fmpz_mod_poly res
Expand Down
12 changes: 6 additions & 6 deletions src/flint/types/nmod_poly.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -474,34 +474,34 @@ cdef class nmod_poly(flint_poly):
def __pow__(nmod_poly self, exp, mod=None):
cdef nmod_poly res
if mod is not None:
return self.powmod(exp, mod)
return self.pow_mod(exp, mod)
if exp < 0:
raise ValueError("negative exponent")
res = nmod_poly.__new__(nmod_poly)
nmod_poly_init_preinv(res.val, (<nmod_poly>self).val.mod.n, (<nmod_poly>self).val.mod.ninv)
nmod_poly_pow(res.val, self.val, <ulong>exp)
return res

def powmod(self, e, modulus, mod_rev_inv=None):
def pow_mod(self, e, modulus, mod_rev_inv=None):
"""
Returns ``self`` raised to the power ``e`` modulo ``modulus``:
:math:`f^e \mod g`/
``mod_rev_inv`` is the inverse of the reverse of the modulus,
precomputing it and passing it to ``powmod()`` can optimise
precomputing it and passing it to ``pow_mod()`` can optimise
powering of polynomials with large exponents.
>>> x = nmod_poly([0,1], 163)
>>> f = 30*x**6 + 104*x**5 + 76*x**4 + 33*x**3 + 70*x**2 + 44*x + 65
>>> g = 43*x**6 + 91*x**5 + 77*x**4 + 113*x**3 + 71*x**2 + 132*x + 60
>>> mod = x**4 + 93*x**3 + 78*x**2 + 72*x + 149
>>>
>>> f.powmod(123, mod)
>>> f.pow_mod(123, mod)
3*x^3 + 25*x^2 + 115*x + 161
>>> f.powmod(2**64, mod)
>>> f.pow_mod(2**64, mod)
52*x^3 + 96*x^2 + 136*x + 9
>>> mod_rev_inv = mod.reverse().inverse_series_trunc(4)
>>> f.powmod(2**64, mod, mod_rev_inv)
>>> f.pow_mod(2**64, mod, mod_rev_inv)
52*x^3 + 96*x^2 + 136*x + 9
"""
cdef nmod_poly res
Expand Down

0 comments on commit ae31437

Please sign in to comment.