Skip to content

Feat/shor classical implementation #12799

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

khushipy
Copy link

@khushipy khushipy commented Jun 18, 2025

This PR adds a classical implementation of Shor’s Algorithm to the cryptography module. The algorithm factors a given composite number N by:

  • Checking for trivial factors using GCD
  • Finding the order of a random integer a modulo N
  • Using the order to derive factors of N
    Changes:
    Added shor_algorithm.py in cryptography directory
    Implemented modular exponentiation and order-finding methods
    Added tests in cryptography
    Issue Reference: Request to Implement Shor Algorithm #12318

@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Jun 18, 2025
@algorithms-keeper algorithms-keeper bot added the require descriptive names This PR needs descriptive function and/or variable names label Jun 20, 2025
Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

from typing import Any


def is_prime(n: int) -> bool:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: n

return all(n % i != 0 for i in range(3, r + 1, 2))


def modexp(a: int, b: int, m: int) -> int:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: a

Please provide descriptive name for the parameter: b

Please provide descriptive name for the parameter: m

return result


def shor_classical(n: int, max_attempts: int = 10) -> str | tuple[int, int]:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: n

@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Jun 20, 2025
Copy link

@dcq01 dcq01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! I'm a grad student working on a research project about using large language models to automate code review. Based on your commit c568a09 and the changes in cryptography/shor_algorithm.py, my tool generated this comment:

  1. In the shor_classical function, ensure that n is greater than 2 before the random.randrange(2, n - 1) call, or handle the case where n is 2 or less appropriately.
  2. In the modexp function, add a check to ensure that m is greater than zero before proceeding with the calculations.
  3. In the is_prime function, consider raising an exception or returning a specific error message for negative numbers or zero.
  4. In the shor_classical function, check if factor1 and factor2 are valid factors (greater than 1 and less than n) to ensure meaningful results.
  5. Consider raising exceptions instead of returning a string message for failure cases in the shor_classical function. This would allow calling code to handle errors more flexibly and facilitate logging or other error-handling strategies in the future.
  6. Ensure that the environment where this code will run supports the syntax str | tuple[int, int]. If not, it may lead to a type error.
  7. Ensure that the codebase is consistent with type hinting styles. If the rest of the code uses Union, it might be better to stick with that for consistency.
  8. Consider adding logging statements to capture the input values, the results of primality checks, and the factors found (if any) in the shor_classical function.
  9. The variable names factor1 and factor2 could be more descriptive. Consider renaming them to first_factor and second_factor to clarify their purpose in the context of the function.
  10. The variable g is used to store the greatest common divisor, but the name g does not convey this meaning clearly. A more descriptive name like gcd_value or greatest_common_divisor would enhance readability.
  11. The variable r is used as a counter in the while loop, but it is not immediately clear what it represents. A name like order or exponent_order could provide better context.
  12. The variable x is used to store the result of the modular exponentiation, but its purpose is not clear from the name alone. Consider renaming it to mod_exp_result or exponentiation_result for clarity.
  13. Update the docstring of shor_classical to replace N with n in the examples for consistency.
  14. Ensure that any other documentation or comments referring to N are also updated to n to avoid confusion.

As part of my research, I'm trying to understand how useful these comments are in real-world development. If you have a moment, I'd be super grateful if you could quickly reply to these two yes/no questions:

  1. Does this comment provide suggestions from a dimension you hadn’t considered?
    1. Do you find this comment helpful?

Thanks a lot for your time and feedback! And sorry again if this message is a bother.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting reviews This PR is ready to be reviewed require descriptive names This PR needs descriptive function and/or variable names tests are failing Do not merge until tests pass
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants