Skip to content

Commit

Permalink
Correct modular powers of GF(2) polynomials
Browse files Browse the repository at this point in the history
The NTL PowerMod API requires that the base argument
has smaller degree than the modulus.

Also enable the dedicated API for x^e mod f.
  • Loading branch information
remyoudompheng committed Apr 7, 2023
1 parent 27b077e commit 141fe7a
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/sage/libs/ntl/ntl_GF2X_linkage.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,23 @@ cdef inline int celement_pow(GF2X_c* res, GF2X_c* x, long e, GF2X_c *modulus, lo
x^9 + x^8 + x^7 + x^5 + x^3
sage: pow(f, 2, h)
x^9 + x^8 + x^7 + x^5 + x^3
sage: pow(x, 1000, h)
x^8 + x^7 + x^4
Check that deg x >= deg modulus works (:issue:`35324`)::
sage: pow(x+1, 2, x^2+x+1)
x
sage: pow(x^2+1, 2, x^2+x+1)
x + 1
"""
cdef GF2XModulus_c mod
cdef GF2X_c xmod

if modulus == NULL:
if GF2X_IsX(x[0]):
GF2X_LeftShift(res[0], x[0], e - 1)
GF2X_LeftShift(res[0], x[0], e - 1)
else:
do_sig = GF2X_deg(x[0]) > 1e5
if do_sig:
Expand All @@ -353,14 +364,17 @@ cdef inline int celement_pow(GF2X_c* res, GF2X_c* x, long e, GF2X_c *modulus, lo
if do_sig:
sig_off()
else:
GF2X_rem(xmod, x[0], modulus[0])
GF2XModulus_build(mod, modulus[0])

do_sig = GF2X_deg(x[0]) > 1e5
if do_sig:
sig_on()
GF2X_PowerMod_long_pre(res[0], x[0], e, mod)
if do_sig:
sig_off()
if GF2X_IsX(xmod):
GF2X_PowerXMod_long_pre(res[0], e, mod)
else:
do_sig = GF2X_deg(x[0]) > 1e5
if do_sig:
sig_on()
GF2X_PowerMod_long_pre(res[0], xmod, e, mod)
if do_sig:
sig_off()


cdef inline int celement_gcd(GF2X_c* res, GF2X_c* a, GF2X_c *b, long parent) except -2:
Expand Down

0 comments on commit 141fe7a

Please sign in to comment.