Compare commits

...

9 Commits

Author SHA1 Message Date
Joel John Kurien
44272e6d0a
Merge 5305f5a3b4 into e3bd7721c8 2024-11-16 12:18:36 +05:30
Joelkurien
5305f5a3b4 Merge branch 'joel_quantum' of https://github.com/joelkurien/Python into joel_quantum 2024-10-29 22:43:17 +11:00
Joelkurien
30d41fbd50 Developed the Logical implementation of Shor Algo #12318 2024-10-29 22:43:16 +11:00
pre-commit-ci[bot]
73ffa2485d [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-29 10:37:03 +00:00
Joelkurien
ebf66fb78c Handled type hinds 2024-10-29 21:36:23 +11:00
Joelkurien
17ec57437e Added referred website and handle type hints 2024-10-29 21:35:43 +11:00
pre-commit-ci[bot]
656c6b512e [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-29 10:33:43 +00:00
joelkurien
a616dc90ab updating DIRECTORY.md 2024-10-29 10:32:19 +00:00
Joelkurien
7655bf3f65 Added Shor Algorithm for RSA n factorization 2024-10-29 21:31:59 +11:00
2 changed files with 67 additions and 0 deletions

View File

@ -1192,6 +1192,7 @@
## Quantum ## Quantum
* [Q Fourier Transform](quantum/q_fourier_transform.py) * [Q Fourier Transform](quantum/q_fourier_transform.py)
* [Shor Algorithm](quantum/shor_algorithm.py)
## Scheduling ## Scheduling
* [First Come First Served](scheduling/first_come_first_served.py) * [First Come First Served](scheduling/first_come_first_served.py)

66
quantum/shor_algorithm.py Normal file
View File

@ -0,0 +1,66 @@
import math
import random
"""
Shor Algorithm is one of the basic quantum computing algorithm
that is used in breaking the RSA cryptography protocol, by finding the
prime numbers that are used to create the public key value, n
In this implementation, I have used a very simple construct without
the use of qiskit or cirq to help understand how Shor algorithm's
idea actually works.
Website referred for shor algorithm:
https://www.geeksforgeeks.org/shors-factorization-algorithm/
"""
class Shor:
def period_find(self, num: int, number: int) -> int:
"""
Find the period of a^x mod N.
>>> shor = Shor()
>>> shor.period_find(2, 15)
4
>>> shor.period_find(3, 7)
6
"""
start: int = 1
while pow(num, start, number) != 1:
start += 1
return start
def shor_algorithm(self, number: int) -> tuple[int, int]:
"""
Run Shor's algorithm to factor a number.
>>> shor = Shor()
>>> random.seed(0)
>>> factors = shor.shor_algorithm(15)
>>> isinstance(factors, tuple) and len(factors) == 2
True
>>> factors
(3, 5)
"""
if number % 2 == 0:
return 2, number // 2
while True:
random.seed(0)
num: int = random.randint(2, number - 1)
gcd_number_num: int = math.gcd(number, num)
if gcd_number_num > 1:
return gcd_number_num, number // gcd_number_num
result: int = self.period_find(num, number)
if not result % 2:
start: int = pow(num, result // 2, number)
if start != number - 1:
p_value: int = math.gcd(start - 1, number)
q_value: int = math.gcd(start + 1, number)
if p_value > 1 and q_value > 1:
return p_value, q_value
shor = Shor()
print(shor.shor_algorithm(15))