2018-12-14 17:11:03 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
import math
|
2018-12-20 14:52:00 +01:00
|
|
|
import sys
|
|
|
|
import random
|
2018-12-14 17:11:03 +01:00
|
|
|
|
|
|
|
c = int("2A4C9AA52257B56837369D5DD7019451C0EC04427EB95EB741D0273D55", 16)
|
|
|
|
n = int("0D8A7A45D9BE42BB3F03F710CF105628E8080F6105224612481908DC721", 16)
|
2018-12-16 18:10:42 +01:00
|
|
|
t = int("1398ED7F59A62962D5A47DD0D32B71156DD6AF6B46BEA949976331B8E1", 16)
|
|
|
|
|
2018-12-14 17:11:03 +01:00
|
|
|
def linear_diophantine_equation(a, b):
|
|
|
|
if b > a:
|
|
|
|
return linear_diophantine_equation(b, a)
|
|
|
|
|
|
|
|
if b == 0:
|
|
|
|
return a, 1, 0
|
|
|
|
|
|
|
|
d, x, y = linear_diophantine_equation(b, a % b)
|
|
|
|
return (d, y, x - (a // b) * y)
|
|
|
|
|
|
|
|
def multiplicative_inverse(a, b):
|
|
|
|
d, x, y = linear_diophantine_equation(a, b)
|
|
|
|
if y < 0:
|
|
|
|
y = (b if b > a else a) + y
|
|
|
|
return y
|
|
|
|
|
|
|
|
def gcd(a, b):
|
|
|
|
d, x, y = linear_diophantine_equation(a, b)
|
|
|
|
return d
|
|
|
|
|
2018-12-16 18:10:42 +01:00
|
|
|
def test_solution(m):
|
|
|
|
return (m**2) % n == c
|
|
|
|
|
|
|
|
def is_square_num(n):
|
|
|
|
if n <= 0:
|
|
|
|
return False
|
|
|
|
return math.sqrt(n)**2 == n
|
|
|
|
|
|
|
|
def is_int(n):
|
|
|
|
return int(n) == n
|
|
|
|
|
2018-12-20 14:52:00 +01:00
|
|
|
def is_nth_root_num(a, b):
|
|
|
|
c = a**(1/float(b))
|
|
|
|
return abs(math.pow(c, b) - a) < 0.000001
|
2018-12-16 18:10:42 +01:00
|
|
|
|
2018-12-20 14:52:00 +01:00
|
|
|
def log(a, b):
|
|
|
|
return math.log(a) / math.log(b)
|
2018-12-16 18:10:42 +01:00
|
|
|
|
2018-12-20 14:52:00 +01:00
|
|
|
def ascii(n):
|
|
|
|
h = hex(n)[2:]
|
|
|
|
return ''.join([chr(int(h[i:i+2], 16)) for i in range(0, len(h), 2)])
|
2018-12-16 18:10:42 +01:00
|
|
|
|
2018-12-14 17:11:03 +01:00
|
|
|
# n > t > c
|
2018-12-20 14:52:00 +01:00
|
|
|
# gcd(m, n) = gcd(m², n) = gcd(c, n) = gcd(t, n) = gcd(t, c) = gcd(t*c, n) = 1
|
|
|
|
# m² ≡ c (mod n)
|