From f610031f9e0411395c062ba387c295f6e563b960 Mon Sep 17 00:00:00 2001 From: Aditya Kumar Mishra <154746713+adityakrmishra@users.noreply.github.com> Date: Wed, 29 Jan 2025 19:47:41 +0530 Subject: [PATCH 1/2] =?UTF-8?q?Added=20classical=20implementation=20of=20S?= =?UTF-8?q?hor=E2=80=99s=20Algorithm?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Implemented a classical simulation of Shor’s Algorithm in `algorithms/cryptography/shor_algorithm.py` - The algorithm factors a composite number N without using quantum computing libraries - Uses modular exponentiation and order-finding to determine factors - Includes an example usage for testing --- algorithms/cryptography/shor_algorithm.py | 56 +++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 algorithms/cryptography/shor_algorithm.py diff --git a/algorithms/cryptography/shor_algorithm.py b/algorithms/cryptography/shor_algorithm.py new file mode 100644 index 000000000000..b5ac08f59549 --- /dev/null +++ b/algorithms/cryptography/shor_algorithm.py @@ -0,0 +1,56 @@ +import math +import random + +def gcd(a, b): + """Computes the greatest common divisor using Euclidean algorithm.""" + while b: + a, b = b, a % b + return a + +def modular_exponentiation(base, exp, mod): + """Computes (base^exp) % mod using fast modular exponentiation.""" + result = 1 + while exp > 0: + if exp % 2 == 1: + result = (result * base) % mod + base = (base * base) % mod + exp //= 2 + return result + +def find_order(a, N): + """Finds the smallest r such that a^r ≡ 1 (mod N)""" + r = 1 + while modular_exponentiation(a, r, N) != 1: + r += 1 + if r > N: # Prevent infinite loops + return None + return r + +def shor_algorithm(N): + """Simulates Shor’s Algorithm classically to factorize N.""" + if N % 2 == 0: + return 2, N // 2 # Trivial case if N is even + + while True: + a = random.randint(2, N - 1) + factor = gcd(a, N) + if factor > 1: + return factor, N // factor # Lucky case: a and N are not coprime + + r = find_order(a, N) + if r is None or r % 2 == 1: + continue # Retry if order is not even + + factor1 = gcd(modular_exponentiation(a, r // 2, N) - 1, N) + factor2 = gcd(modular_exponentiation(a, r // 2, N) + 1, N) + + if 1 < factor1 < N: + return factor1, N // factor1 + if 1 < factor2 < N: + return factor2, N // factor2 + +# Example usage +if __name__ == "__main__": + N = 15 # You can test with 21, 35, 55, etc. + factors = shor_algorithm(N) + print(f"Factors of {N}: {factors}") From b0833eb96b223522993eafe98b6b0ec8f40b84c9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 29 Jan 2025 14:19:42 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- algorithms/cryptography/shor_algorithm.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/algorithms/cryptography/shor_algorithm.py b/algorithms/cryptography/shor_algorithm.py index b5ac08f59549..1cdefaa3c0fb 100644 --- a/algorithms/cryptography/shor_algorithm.py +++ b/algorithms/cryptography/shor_algorithm.py @@ -1,12 +1,14 @@ import math import random + def gcd(a, b): """Computes the greatest common divisor using Euclidean algorithm.""" while b: a, b = b, a % b return a + def modular_exponentiation(base, exp, mod): """Computes (base^exp) % mod using fast modular exponentiation.""" result = 1 @@ -17,6 +19,7 @@ def modular_exponentiation(base, exp, mod): exp //= 2 return result + def find_order(a, N): """Finds the smallest r such that a^r ≡ 1 (mod N)""" r = 1 @@ -26,6 +29,7 @@ def find_order(a, N): return None return r + def shor_algorithm(N): """Simulates Shor’s Algorithm classically to factorize N.""" if N % 2 == 0: @@ -49,6 +53,7 @@ def shor_algorithm(N): if 1 < factor2 < N: return factor2, N // factor2 + # Example usage if __name__ == "__main__": N = 15 # You can test with 21, 35, 55, etc.