Skip to content

Incorrect use of NTL::PowerMod with GF2X argument (GF(2)["x"] polynomials) #35324

@remyoudompheng

Description

@remyoudompheng

Is there an existing issue for this?

  • I have searched the existing issues for a bug report that matches the one I want to file, without success.

Did you read the documentation and troubleshoot guide?

  • I have read the documentation and troubleshoot guide

Environment

- **OS**: Archlinux, Kali Linux (~Debian 12)
- **Sage Version**: 9.8

Steps To Reproduce

Use 3-argument pow with polynomials over GF(2).

sage: R.<x> = PolynomialRing(GF(2))
sage: pow(x+1, 2, x^2+x+1)
x
sage: pow(x^2+1, 2, x^2+x+1)

Expected Behavior

Obtain either an exception for unsupported argument, or the expected result:

sage: pow(x^2+1, 2) % (x^2+x+1)
x + 1
sage: pow(x^2+1, 2, x^2+x+1)
x + 1

Actual Behavior

A C++ abort() crash:

sage-9.8/local/var/lib/sage/venv-python3.11/lib/python3.11/site-packages/cysignals/signals.cpython-311-x86_64-linux-gnu.so(+0x811b)[0x7ff92113011b]
sage-9.8/local/var/lib/sage/venv-python3.11/lib/python3.11/site-packages/cysignals/signals.cpython-311-x86_64-linux-gnu.so(+0x81c8)[0x7ff9211301c8]
sage-9.8/local/var/lib/sage/venv-python3.11/lib/python3.11/site-packages/cysignals/signals.cpython-311-x86_64-linux-gnu.so(+0xa6dd)[0x7ff9211326dd]
/lib/x86_64-linux-gnu/libc.so.6(+0x3bf90)[0x7ff921695f90]
/lib/x86_64-linux-gnu/libc.so.6(+0x8accc)[0x7ff9216e4ccc]
/lib/x86_64-linux-gnu/libc.so.6(gsignal+0x12)[0x7ff921695ef2]
/lib/x86_64-linux-gnu/libc.so.6(abort+0xd3)[0x7ff921680472]
/lib/x86_64-linux-gnu/libntl.so.44(+0x6cb24)[0x7ff91726cb24]
/lib/x86_64-linux-gnu/libntl.so.44(_ZN3NTL8PowerModERNS_4GF2XERKS0_RKNS_2ZZERKNS_11GF2XModulusE+0x1ac)[0x7ff9172a977c]
sage-9.8/src/sage/rings/polynomial/polynomial_gf2x.cpython-311-x86_64-linux-gnu.so(+0x1e71b)[0x7ff8c71ab71b]
sage-9.8/src/sage/rings/polynomial/polynomial_gf2x.cpython-311-x86_64-linux-gnu.so(+0x1f0e9)[0x7ff8c71ac0e9]

Additional Information

It seems related to this snippet:
https://github.com/libntl/ntl/blob/main/src/GF2X1.cpp#L1746

void PowerMod(GF2X& h, const GF2X& g, const ZZ& e, const GF2XModulus& F)
// h = g^e mod f using "sliding window" algorithm
{   
   if (deg(g) >= F.n) LogicError("PowerMod: bad args");

The bug is also triggered by:

sage: R.<x> = GF(2)[]
sage: (x+1).any_root(degree=2)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions