modular arithmetic course - challenge 8 - tonelli-shanks (au secours les courbes eliptiques me font peur)
This commit is contained in:
parent
e79ee10479
commit
df5c4b4dcb
2 changed files with 22 additions and 19 deletions
|
|
@ -50,7 +50,26 @@ def _is_quadratic_residue(x: int, N: int) -> bool:
|
|||
return False
|
||||
|
||||
|
||||
def _tonelli_shanks(x, p):
|
||||
def legendre_symbol(N: int, candidates: [int]):
|
||||
"""Find the square root of x mod N using Legendre's symbol.
|
||||
Args:
|
||||
N: a prime integer.
|
||||
x: a list of integer that may be quadratic residues.
|
||||
Returns:
|
||||
The square root of the first element in x which is a quadratic residue.
|
||||
"""
|
||||
for x in candidates:
|
||||
if _is_quadratic_residue(x, N):
|
||||
# TODO find the square root of x mod N
|
||||
try:
|
||||
return tonelli_shanks(x, N)
|
||||
except:
|
||||
return None
|
||||
return None
|
||||
|
||||
|
||||
# Challenge 8 - tonelli-shanks
|
||||
def tonelli_shanks(x, p):
|
||||
q = p - 1
|
||||
k = 0
|
||||
while q % 2 == 0:
|
||||
|
|
@ -77,21 +96,3 @@ def _tonelli_shanks(x, p):
|
|||
c = pow(b, 2, p)
|
||||
t = t * pow(b, 2) % p
|
||||
r = r * b % p
|
||||
|
||||
|
||||
def legendre_symbol(N: int, candidates: [int]):
|
||||
"""Find the square root of x mod N using Legendre's symbol.
|
||||
Args:
|
||||
N: a prime integer.
|
||||
x: a list of integer that may be quadratic residues.
|
||||
Returns:
|
||||
The square root of the first element in x which is a quadratic residue.
|
||||
"""
|
||||
for x in candidates:
|
||||
if _is_quadratic_residue(x, N):
|
||||
# TODO find the square root of x mod N
|
||||
try:
|
||||
return _tonelli_shanks(x, N)
|
||||
except:
|
||||
return None
|
||||
return None
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue