Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jvdsn
GitHub Repository: jvdsn/crypto-attacks
Path: blob/master/attacks/factorization/twin_primes.py
2589 views
1
from math import isqrt
2
3
4
def factorize(N):
5
"""
6
Recovers the prime factors from a modulus if the factors are twin primes.
7
:param N: the modulus
8
:return: a tuple containing the prime factors, or None if there is no factorization
9
"""
10
p = isqrt(N + 1) - 1
11
q = isqrt(N + 1) + 1
12
return p, q if p * q == N else None
13
14