Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overflow in polynomial pow #487

Closed
Lasagnenator opened this issue May 20, 2023 · 2 comments · Fixed by #488
Closed

Overflow in polynomial pow #487

Lasagnenator opened this issue May 20, 2023 · 2 comments · Fixed by #488
Labels
bug Something isn't working poly Related to polynomials

Comments

@Lasagnenator
Copy link
Contributor

Hi, this is quite a small issue with regards to very large polynomials. Given a specific order and degree, the irreducible polynomial function can cause an overflow error. I'm not sure if this is an issue with other values but it definitely fails with the below code.

Code to reproduce the issue.

import galois
galois.irreducible_poly(2**13, 128, method="random")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.8/dist-packages/galois/_polys/_irreducible.py", line 247, in irreducible_poly
    return next(_random_search(order, degree, "is_irreducible"))
  File "/usr/local/lib/python3.8/dist-packages/galois/_polys/_search.py", line 107, in _random_search
    if getattr(poly, test)():
  File "/usr/local/lib/python3.8/dist-packages/galois/_polys/_irreducible.py", line 112, in is_irreducible
    hi = pow(h0, q ** (ni - n0), f)
  File "/usr/local/lib/python3.8/dist-packages/galois/_polys/_poly.py", line 1442, in __pow__
    q = _dense.pow_jit(self.field)(a, exponent, b)
  File "/usr/local/lib/python3.8/dist-packages/galois/_polys/_dense.py", line 352, in __call__
    b_vec = np.array(b_vec[::-1], dtype=np.uint64)  # Make vector MSWord -> LSWord
OverflowError: Python int too large to convert to C long

I've done my due diligence and tried to find something to help with the issue. It appears that b_vec will have exactly 2**64 as its last value given the above code which causes the overflow. Changing while b > 2**64: to while b >= 2**64: seems to fix the problem. I must admit I'm not that familiar with the specifics of how the pow function implementation works and if this might cause an issue (and if it might cause an issue with something else).

Thanks for the awesome library!

@mhostetter
Copy link
Owner

Thanks for the report! This is a bug.

I believe the fix you identified is the correct one. If you'd like to submit a pull request, that would be great. If not, no worries; I can open one later.

@mhostetter mhostetter added bug Something isn't working poly Related to polynomials labels May 20, 2023
mhostetter added a commit that referenced this issue May 21, 2023
mhostetter added a commit that referenced this issue Oct 1, 2023
@mhostetter
Copy link
Owner

Bug fix released in v0.3.6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working poly Related to polynomials
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants