Documente Academic
Documente Profesional
Documente Cultură
def D(n,E):
p=int(mpqs(n))
q=int(n/p)
phi=((p-1)*(q-1))
D=ext(E,phi)
return D
def phi(n,E):
p=int(mpqs(n))
q=int(n/p)
phi=((p-1)*(q-1))
return phi
def decrypt(n,EM,E):
p=int(mpqs(n))
q=int(n/p)
phi=((p-1)*(q-1))
#I'm so dumb
D=( (ext(E,phi) ) % (phi) )
return pow(EM,D,n)
#Test set
#M should be 302824
#n=40806607
#E=677
#EM=4096672
n=int(raw_input("n:"))
E=int(raw_input("E:"))
EM=int(raw_input("EM:"))
#print(D(n,E))
#print(phi(n,E))
#Dv=D(n,E)
#phii=phi(n,E)
#print ((Dv*E)%phii) #should always give 1
#print(mpqs(n,verbose=True))
#cd C:\Python27
#pypy D.py
----------------------------------------------------------------------------------------------------------------------------
n=2
while legendre(n, p) != p-1:
n += 1
w = pow(a, q, p)
x = pow(a, (q+1) >> 1, p)
y = pow(n, q, p)
r=e
while True:
if w == 1:
return x
v=w
k=0
while v != 1 and k+1 < r:
v = (v*v)%p
k += 1
if k == 0:
return x
#integer sqrt of n
def isqrt(n):
c = n*4/3
d = c.bit_length()
a = d>>1
if d&1:
x = 1 << a
y = (x + (n >> a)) >> 1
else:
x = (3 << a) >> 2
y = (x + (c >> a)) >> 1
if x != y:
x=y
y = (x + n/x) >> 1
while y < x:
x=y
y = (x + n/x) >> 1
return x
return False
# use the same bit reversal process to calculate the sth Lucas number
# keep track of q = Q**n as we go
U=0
V=2
q=1
# mod_inv(2, n)
inv_2 = (n+1) >> 1
while t:
if t&1:
# U, V of n+1
U, V = ((U + V) * inv_2)%n, ((D*U + V) * inv_2)%n
q = (q * Q)%n
t -= 1
else:
# U, V of n*2
U, V = (U * V)%n, (V * V - 2 * q)%n
q = (q * q)%n
t >>= 1
# primality check
# if n is prime, n divides the n+1st Lucas number, given the assumptions
return U == 0
max_int = 2147483647
for p in small_primes:
if n%p == 0:
return False
a=5
s=2
# if n is a perfect square, this will never terminate
# yikes
while legendre(a, n) != n-1:
s = -s
a = s-a
return is_lucas_prp(n, a)
----------------------------------------------------------------------------------------------------------------------------
root_n = isqrt(n)
root_2n = isqrt(n+n)
prime = []
mod_root = []
log_p = []
num_prime = 0
p=2
while p < bound or num_prime < 3:
if leg == 1:
prime += [p]
mod_root += [int(mod_sqrt(n, p))]
log_p += [log(p, 10)]
num_prime += 1
elif leg == 0:
if verbose:
print 'cancer found factors:'
print p, 'x', n/p
return p
p = next_prime(p)
# size of sieve
x_max = len(prime)*60
if verbose:
print 'smoothness bound:', bound
print 'sieve size:', x_max
print 'log threshold:', thresh
print 'skipping primes less than:', min_prime
smooth = []
used_prime = set()
partial = {}
num_smooth = 0
num_used_prime = 0
num_partial = 0
num_poly = 0
root_A = isqrt(root_2n / x_max)
if verbose:
print 'sieving for smooths...'
while True:
# find an integer value A
while True:
root_A = next_prime(root_A)
leg = legendre(n, root_A)
if leg == 1:
break
elif leg == 0:
if verbose:
print '"luck" :) found factors:'
print root_A, 'x', n/root_A
return root_A
A = root_A * root_A
b = mod_sqrt(n, root_A)
B = (b + (n - b*b) * mod_inv(b + b, root_A))%A
C = (B*B - n) / A
num_poly += 1
sums = [0.0]*(2*x_max)
i=0
for p in prime:
if p < min_prime:
i += 1
continue
logp = log_p[i]
inv_A = mod_inv(A, p)
# modular root of the quadratic
a = int(((mod_root[i] - B) * inv_A)%p)
b = int(((p - mod_root[i] - B) * inv_A)%p)
k=0
while k < x_max:
if k+a < x_max:
sums[k+a] += logp
if k+b < x_max:
sums[k+b] += logp
if k:
sums[k-a+x_max] += logp
sums[k-b+x_max] += logp
k += p
i += 1
if sieve_val < 0:
vec = set([-1])
sieve_val = -sieve_val
for p in prime:
while sieve_val%p == 0:
if p in vec:
#avoid taking the sqrt of a gigantic number at the end
sqr += [p]
vec ^= set([p])
sieve_val = int(sieve_val / p)
if sieve_val == 1:
# smooth
smooth += [(vec, (sqr, (A*x+B), root_A))]
used_prime |= vec
elif sieve_val in partial:
# combine two partials to make a (xor) smooth
pair_vec, pair_vals = partial[sieve_val]
sqr += list(vec & pair_vec) + [sieve_val]
vec ^= pair_vec
smooth += [(vec, (sqr + pair_vals[0], (A*x+B)*pair_vals[1], root_A*pair_vals[2]))]
used_prime |= vec
num_partial += 1
else:
# save partial for later pairing
partial[sieve_val] = (vec, (sqr, A*x+B, root_A))
i += 1
num_smooth = len(smooth)
num_used_prime = len(used_prime)
if verbose:
print 100 * num_smooth / num_prime, 'percent complete\r',
used_prime_list = sorted(list(used_prime))
# set up bit fields for gaussian elimination, to get to row echelon form
masks = []
mask = 1
bit_fields = [0]*num_used_prime
for vec, vals in smooth:
masks += [mask]
i=0
for p in used_prime_list:
if p in vec: bit_fields[i] |= mask
i += 1
mask <<= 1
if verbose:
print 'factors found:'
print factor, 'x', n/factor
print 'time elapsed: %f seconds'%(clock()-time1)
return factor
if __name__ == "__main__":
parser =ArgumentParser(description='Uses a MPQS to factor a composite number')
parser.add_argument('composite', metavar='number_to_factor', type=long,
help='the composite number to factor')
parser.add_argument('--verbose', dest='verbose', action='store_true',
help="enable verbose output")
args = parser.parse_args()
if args.verbose:
mpqs(args.composite, args.verbose)
else:
time1 = clock()
print mpqs(args.composite)
print 'time elapsed: %f seconds'%(clock()-time1)