Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jvdsn
GitHub Repository: jvdsn/crypto-attacks
Path: blob/master/attacks/factorization/gaa.py
2589 views
1
from math import ceil
2
3
from sage.all import ZZ
4
from sage.all import sqrt
5
6
7
def factorize(N, rp, rq):
8
"""
9
Recovers the prime factors from a modulus using the Ghafar-Ariffin-Asbullah attack.
10
More information: Ghafar AHA. et al., "A New LSB Attack on Special-Structured RSA Primes"
11
:param N: the modulus
12
:param rp: the value rp
13
:param rq: the value rq
14
:return: a tuple containing the prime factors
15
"""
16
i = ceil(sqrt(rp * rq))
17
x = ZZ["x"].gen()
18
while True:
19
sigma = (round(int(sqrt(N))) - i) ** 2
20
z = (N - (rp * rq)) % sigma
21
f = x ** 2 - z * x + sigma * rp * rq
22
for x0 in f.roots(multiplicities=False):
23
if x0 % rp == 0:
24
p = int((x0 // rp) + rq)
25
assert N % p == 0
26
return p, N // p
27
if x0 % rq == 0:
28
p = int((x0 // rq) + rp)
29
assert N % p == 0
30
return p, N // p
31
32
i += 1
33
34