From df5c4b4dcb7b56139c8c54b2d782c2f283f5627d Mon Sep 17 00:00:00 2001 From: boyreau Date: Sat, 14 Nov 2026 23:34:50 +0100 Subject: [PATCH] modular arithmetic course - challenge 8 - tonelli-shanks (au secours les courbes eliptiques me font peur) --- challenge.py | 2 ++ modular_arithmetic.py | 39 ++++++++++++++++++++------------------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/challenge.py b/challenge.py index ddbe0f8..cef7812 100644 --- a/challenge.py +++ b/challenge.py @@ -13,6 +13,7 @@ from modular_arithmetic import ( extended_gcd, quadratic_residue, legendre_symbol, + tonelli_shanks, ) from public_key_cryptography import ( modular_exponentiation, @@ -79,4 +80,5 @@ if __name__ == "__main__": ], ) ) # Bancal mais on est mod beaucoup donc -1 est forcément pas le max. + print(tonelli_shanks(8479994658316772151941616510097127087554541274812435112009425778595495359700244470400642403747058566807127814165396640215844192327900454116257979487432016769329970767046735091249898678088061634796559556704959846424131820416048436501387617211770124292793308079214153179977624440438616958575058361193975686620046439877308339989295604537867493683872778843921771307305602776398786978353866231661453376056771972069776398999013769588936194859344941268223184197231368887060609212875507518936172060702209557124430477137421847130682601666968691651447236917018634902407704797328509461854842432015009878011354022108661461024768, 30531851861994333252675935111487950694414332763909083514133769861350960895076504687261369815735742549428789138300843082086550059082835141454526618160634109969195486322015775943030060449557090064811940139431735209185996454739163555910726493597222646855506445602953689527405362207926990442391705014604777038685880527537489845359101552442292804398472642356609304810680731556542002301547846635101455995732584071355903010856718680732337369128498655255277003643669031694516851390505923416710601212618443109844041514942401969629158975457079026906304328749039997262960301209158175920051890620947063936347307238412281568760161)) # fmt: on diff --git a/modular_arithmetic.py b/modular_arithmetic.py index f75fcbd..419d39f 100644 --- a/modular_arithmetic.py +++ b/modular_arithmetic.py @@ -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