-
Notifications
You must be signed in to change notification settings - Fork 420
/
Copy pathNumbertheo.py
177 lines (149 loc) · 4.3 KB
/
Numbertheo.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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python
# encoding: utf-8
"""
chapter_2.py
Created by Daniel O'Donovan on 2008-10-09.
Copyright (c) 2008 University of Cambridge. All rights reserved.
"""
import math
import gmpy
# Test case:
# the non-trivial factors of 27595893247589237508237458728307 are
# 5253107790730936 and 5253250903452230
def int2bin(n, count=24):
"""returns the binary of integer n, using count number of digits"""
return "".join([str((n >> y) & 1) for y in range(count - 1, -1, -1)])
def alg_231(n, improved=True):
""" Page 197: Trial Division """
# [1]
t = 0
k = 2
dk = 2
sqrt_n = math.sqrt(n)
count = 0
while True:
if count > sqrt_n:
break
if int(n) == 1:
break # [2]
if improved:
dk = int(gmpy.next_prime(dk))
# [3]
if improved:
q = n / dk
r = n % dk
else:
q = n / k
r = n % k
if r != 0:
if improved:
if q > dk:
k += 1
else:
t += 1
pt = n
else:
if q > k: # [4]
k += 1
else:
t += 1
pt = n
else:
t += 1
if improved:
pt = dk
else:
pt = k
n = q
break
count += 1
# print( q, r )
print(n, pt)
def FermatFactAlg(n):
""" Fermat's factoring algorithm 2.5.3 p 196 """
n = gmpy.mpz(n)
if n % 2 == 0:
print("%d is even" % n)
# gmpy.sqrt is largest truncated sqrt
# k = int( math.sqrt(n) ) + 1
k = gmpy.sqrt(n) + 1
y = k ** 2 - n
d = 1
while True:
# floor_sqrt_y = float( int( math.sqrt(y) ) )
floor_sqrt_y = gmpy.sqrt(y)
if ((floor_sqrt_y ** 2) ** 2) == (y ** 2):
break
else:
y = y + 2 * k + d
d += 2
# print floor_sqrt_y, gmpy.qdiv( n, 2.0)
# print ((floor_sqrt_y ** 2) ** 2), (y ** 2)
if floor_sqrt_y > gmpy.qdiv(n, 2.0):
print("No factor found ")
return
x = gmpy.sqrt(n + y)
y = gmpy.sqrt(y)
print(" the non-trivial factors of %d are" % n)
print(" %d and %d" % (x - y, x + y))
def PollardsRho(N):
# Pollard's Rho method for factoring
from exercises import Alg_improved_Euclid as gcd
from random import randint
N = gmpy.mpz(N)
""" generator function """
def f(x, N):
return (((x ** 2) + 1) % N)
t = 75 * gmpy.sqrt(N)
sqrt_t = gmpy.sqrt(t)
factor_found = False
while not factor_found:
xim1 = randint(1, gmpy.sqrt(N) / 10) # seed
yim1 = f(f(xim1, N), N)
i = 0
while i < sqrt_t:
xi = f(xim1, N)
yi = f(f(yim1, N), N)
# yi = f( yim1, N )
# xi = f( f( xim1, N ), N )
# print( 'gcd( xi - yi ) = gcd( %d )' % abs(xi - yi) )
d = gcd(abs(xi - yi), N)
if d != 1:
print('Non trivial factor found: ', d)
factor_found = True
break
if xi == yi % N:
print('Running with new seed')
break
xim1 = xi
yim1 = yi
i += 1
def shanks(y, a, n):
""" Shanks' baby-step giant-step for finding discrete logarithms
of form : x = log_a ( y mod n )
"""
s = gmpy.sqrt(n)
S = {} # calculate the baby steps
T = {} # calculate the giant steps
for i in range(s):
S['%s' % gmpy.mpz((y * (a ** i)) % n)] = int(i)
T['%s' % gmpy.mpz((a ** ((i + 1) * s)) % n)] = int(i)
# mathching and computing
for key in S.keys():
if key in T:
r = S[key]
st = (T[key] + 1) * s
break
x = st - r
print 'So log_%d %d\t(mod %d) =\t%d ' % (a, y, n, x)
print 'or equiv. %d^%d\t(mod %d) =\t%d ' % (a, x, n, y)
return x
if __name__ == '__main__':
# FermatFactAlg( 278153 )
# FermatFactAlg( 340282366920938463463374607431768211457L )
# FermatFactAlg( 3248523672894567297 )
# PollardsRho( 1387 )
# PollardsRho( 278153 )
# PollardsRho( 3248523672894567297)
# x = shanks( 67, 59, 113 )
x = shanks(6, 2, 19)