modular arithmetic course - challenge 6 - résidus quadratique

This commit is contained in:
boyreau 2026-11-14 14:46:20 +01:00
commit 9c960bf2f1
2 changed files with 18 additions and 0 deletions

View file

@ -11,6 +11,7 @@ from intro import (
from modular_arithmetic import ( from modular_arithmetic import (
gcd, gcd,
extended_gcd, extended_gcd,
quadratic_residue,
) )
from public_key_cryptography import ( from public_key_cryptography import (
modular_exponentiation, modular_exponentiation,
@ -22,6 +23,7 @@ from public_key_cryptography import (
prime_factorization, prime_factorization,
reverse_single_prime_RSA_cipher, reverse_single_prime_RSA_cipher,
) )
import functools
if __name__ == "__main__": if __name__ == "__main__":
# fmt: off # fmt: off
@ -58,4 +60,5 @@ if __name__ == "__main__":
print(gcd(66528, 52920)) print(gcd(66528, 52920))
print(extended_gcd(26513, 32321)) print(extended_gcd(26513, 32321))
print(f"11 % 6 = {11 % 6}; 8146798528947 % 17 = {8146798528947 % 17}") print(f"11 % 6 = {11 % 6}; 8146798528947 % 17 = {8146798528947 % 17}")
print(min([(quadratic_residue(29, x) or 30) for x in [14, 6, 11]])) # Bancal mais on est mod29 donc 30 est forcément pas le min.
# fmt: on # fmt: on

View file

@ -26,3 +26,18 @@ def extended_gcd(a: int, b: int) -> (int, int, int):
a = b a = b
b = r b = r
return a, u[-2], v[-2] return a, u[-2], v[-2]
# Challenge 4 - quadratic residues
def quadratic_residue(N: int, x: int):
"""Find the quadratic residue of x mod N.
Args:
N: a prime integer
x: the integer for which we look for a quadratic residue.
Returns:
Either none or the square root of x mod N.
"""
for i in range(N):
if pow(i, 2, N) == x:
return i
return None