1
0
This commit is contained in:
Dmitry Sorokin
2017-01-22 21:25:24 +03:00
committed by ThomasV
parent f70408cef5
commit 5be78950ca
64 changed files with 1232 additions and 657 deletions

View File

@@ -1,3 +1,9 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import six
# from http://eli.thegreenplace.net/2009/03/07/computing-modular-square-roots-in-python/
def modular_sqrt(a, p):
@@ -26,7 +32,7 @@ def modular_sqrt(a, p):
elif p == 2:
return p
elif p % 4 == 3:
return pow(a, (p + 1) / 4, p)
return pow(a, (p + 1) // 4, p)
# Partition p-1 to s * 2^e for an odd s (i.e.
# reduce all the powers of 2 from p-1)
@@ -34,7 +40,7 @@ def modular_sqrt(a, p):
s = p - 1
e = 0
while s % 2 == 0:
s /= 2
s //= 2
e += 1
# Find some 'n' with a legendre symbol n|p = -1.
@@ -59,7 +65,7 @@ def modular_sqrt(a, p):
# both a and b
# r is the exponent - decreases with each update
#
x = pow(a, (s + 1) / 2, p)
x = pow(a, (s + 1) // 2, p)
b = pow(a, s, p)
g = pow(n, s, p)
r = e
@@ -67,7 +73,7 @@ def modular_sqrt(a, p):
while True:
t = b
m = 0
for m in xrange(r):
for m in range(r):
if t == 1:
break
t = pow(t, 2, p)
@@ -90,5 +96,5 @@ def legendre_symbol(a, p):
Returns 1 if a has a square root modulo
p, -1 otherwise.
"""
ls = pow(a, (p - 1) / 2, p)
ls = pow(a, (p - 1) // 2, p)
return -1 if ls == p - 1 else ls