forked from lducas/SchnorrGate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fac.sage
83 lines (64 loc) · 1.44 KB
/
fac.sage
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from fpylll import IntegerMatrix, SVP
import sys
def svp(B):
A = IntegerMatrix.from_matrix(B)
return SVP.shortest_vector(A)
def first_primes(n):
p = 1
P = []
while len(P) < n:
p = next_prime(p)
P += [p]
return P
def is_smooth(x, P):
y = x
for p in P:
while p.divides(y):
y /= p
return abs(y) == 1
# Test if a factoring relation was indeed found.
def test_Schnorr(N, n, prec=1000):
P = first_primes(n)
f = list(range(1, n+1))
shuffle(f)
# Scale up and round
def sr(x):
return round(x * 2^prec)
diag = [sr(N*f[i]) for i in range(n)] + [sr(N*ln(N))]
B = diagonal_matrix(diag, sparse=False)
for i in range(n):
B[i, n] = sr(N*ln(P[i]))
b = svp(B)
e = [b[i] / sr(N*f[i]) for i in range(n)]
u = 1
v = 1
for i in range(n):
assert e[i] in ZZ
if e[i] > 0:
u *= P[i]^e[i]
if e[i] < 0:
v *= P[i]^(-e[i])
return is_smooth(u - v*N, P)
try:
bits = int(sys.argv[1])
except:
bits = 400
try:
n = int(sys.argv[2])
except:
n = 47
try:
trials = int(sys.argv[3])
except:
trials = 100
print("Testing Schnorr's relation finding algorithm with n=%d on RSA-moduli of %d bits, %d trials"%(n, bits, trials))
successes = 0
for i in range(trials):
p = random_prime(2^(bits/2), false, 2^(bits/2-1))
q = random_prime(2^(bits/2), false, 2^(bits/2-1))
N = p*q
success = test_Schnorr(N, n)
successes += success
print(success, end="\t")
sys.stdout.flush()
print("\n%d Factoring Relation found out of %d trials"%(successes, trials))