forked from thinklancer/code_cryptography
/
hw6.py
57 lines (48 loc) · 1.91 KB
/
hw6.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Homework 6
import gmpy2
from gmpy2 import mpz
def calcA(a,n):
x=a**2-n
if gmpy2.is_square(x):
x = gmpy2.isqrt(x)
p = a+x
q = a-x
if p*q == n:
if not gmpy2.is_prime(q):
print "not prime q" # simple check
return q
return 0
def qs1():
n = mpz('179769313486231590772930519078902473361797697894230657273430081157732675805505620686985379449212982959585501387537164015710139858647833778606925583497541085196591615128057575940752635007475935288710823649949940771895617054361149474865046711015101563940680527540071584560878577663743040086340742855278549092581')
a=gmpy2.isqrt(n)+1
print calcA(a,n)
def qs2():
n = mpz('648455842808071669662824265346772278726343720706976263060439070378797308618081116462714015276061417569195587321840254520655424906719892428844841839353281972988531310511738648965962582821502504990264452100885281673303711142296421027840289307657458645233683357077834689715838646088239640236866252211790085787877')
low = gmpy2.isqrt(n)+1
for i in range(2**20):
a = low+i
q = calcA(a,n)
if q != 0:
print i
return q
return 0
def qs3():
n = mpz('720062263747350425279564435525583738338084451473999841826653057981916355690188337790423408664187663938485175264994017897083524079135686877441155132015188279331812309091996246361896836573643119174094961348524639707885238799396839230364676670221627018353299443241192173812729276147530748597302192751375739387929')
a = gmpy2.isqrt(4*6*n)+1 # 2A
x = a**2-6*n*4
if gmpy2.is_square(x):
x = gmpy2.isqrt(x)
p = (a-x)/6
q = (a+x)/4
if p*q == n:
if not gmpy2.is_prime(q):
print "not prime q" # simple check
if p>q:
return q
else:
return p
return 0
def qs4():
return 0
if __name__=="__main__":
print qs3()