Hackvent_2018/Day 14/decode.py
Roman Hergenreder 6df7db7cb7 Whatver
2018-12-20 14:52:00 +01:00

56 lines
1.3 KiB
Python
Executable File

#!/usr/bin/python
import math
import sys
import random
c = int("2A4C9AA52257B56837369D5DD7019451C0EC04427EB95EB741D0273D55", 16)
n = int("0D8A7A45D9BE42BB3F03F710CF105628E8080F6105224612481908DC721", 16)
t = int("1398ED7F59A62962D5A47DD0D32B71156DD6AF6B46BEA949976331B8E1", 16)
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
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
def is_nth_root_num(a, b):
c = a**(1/float(b))
return abs(math.pow(c, b) - a) < 0.000001
def log(a, b):
return math.log(a) / math.log(b)
def ascii(n):
h = hex(n)[2:]
return ''.join([chr(int(h[i:i+2], 16)) for i in range(0, len(h), 2)])
# n > t > c
# gcd(m, n) = gcd(m², n) = gcd(c, n) = gcd(t, n) = gcd(t, c) = gcd(t*c, n) = 1
# m² ≡ c (mod n)