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

Fix a bug in factorization function causing infinite loops in rare cases #450

Merged

Conversation

pivis
Copy link
Contributor

@pivis pivis commented Dec 12, 2022

There is a small bug in factorization function when it gets to factors above 10M and starts calling pollard_rho. If it can't find a factor using f(x)=x^2+c (mod n) with c==1 it is supposed to update c to some other random value. But initialization accidentally got in the loop body and would always reset c back to 1.

Since pollard_rho is only called to get factors larger than 10M at the point, and the fact that c=1 usually works it is quite rare for this bug to surface, but in the rare case when c=1 doesn't work it causes the infinite loop. Example would be galois.GF(2400610585866217) which requires factorization of 2400610585866217 - 1 = 2^3 * 3 * 100025441077759 = 2^3 * 3 * 10000537 * 10002007.

@codecov
Copy link

codecov bot commented Dec 12, 2022

Codecov Report

Base: 96.38% // Head: 96.38% // No change to project coverage 👍

Coverage data is based on head (64f3cf2) compared to base (648aa06).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #450   +/-   ##
=======================================
  Coverage   96.38%   96.38%           
=======================================
  Files          42       42           
  Lines        5533     5533           
=======================================
  Hits         5333     5333           
  Misses        200      200           
Impacted Files Coverage Δ
src/galois/_prime.py 93.53% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@mhostetter
Copy link
Owner

Thanks for looking into this. That's a bit of an embarrassing bug, but you're absolutely right!

I'm inclined to merge this and immediately push a bug fix release 0.3.1. Then I will respond to your comments/suggestions about improving prime factoring generally #187 (comment).

Thanks again for the PR!

@mhostetter
Copy link
Owner

Performance improvements:

In [1]: import galois

# Before PR
In [2]: %time galois.GF(2400610585866217)
# Never returns...

# After PR
In [2]: %time galois.GF(2400610585866217)
Wall time: 96 ms
Out[2]: <class 'galois.GF(2400610585866217)'>

@mhostetter mhostetter changed the base branch from master to release/0.3.x December 12, 2022 12:44
@mhostetter mhostetter merged commit deb4c1e into mhostetter:release/0.3.x Dec 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants