-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattack.py
60 lines (53 loc) · 1.9 KB
/
attack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from fpylll import *
from ast import literal_eval
n = 21
def egcd(a, b):
# Source : https://stackoverflow.com/questions/4798654/modular-multiplicative-inverse-function-in-python
if a == 0:
return (b, 0, 1)
else:
g, y, x = egcd(b % a, a)
return (g, x - (b // a) * y, y)
def modinv(a, m):
# Source : https://stackoverflow.com/questions/4798654/modular-multiplicative-inverse-function-in-python
g, x, y = egcd(a, m)
if g != 1:
raise Exception('modular inverse does not exist')
else:
return x % m
def gen_matrix(a:int, modulus:int, size:int) -> list:
"""
Application directe de ce qui est écrit dans le cours.
"""
matrix = [[0]*size for _ in range(size)]
for i in range(size):
matrix[0][i] = pow(a, i, modulus)
for i,j in zip(range(1,size), range(1,size)):
if i==j:
matrix[i][j] = modulus
return matrix
def main(a:int, p:int, Ys:list):
I = modinv(2**8, p)
y = [ (el*I) % p for el in Ys]
L = IntegerMatrix.from_matrix(gen_matrix(a, p, n))
reduced = LLL.reduction(L)
Xi_I = CVP.closest_vector(reduced, y, method="fast")
Xi = [xi_i*2**8 % p for xi_i in Xi_I]
inv_a = modinv(a, p)
probable_seed = Xi[0] * inv_a % p
probable_ys = [probable_seed * pow(a,i,p) % p % 2**8 for i in range(1,len(Ys)+1)]
if probable_ys == Ys:
print("Seed recovery complete")
print("Seed : " + str(probable_seed))
else:
print("Not enough information to recover the seed.")
if __name__ == "__main__":
a = int(input("a : "))
p = int(input("p : "))
Ys = literal_eval(input("Ys : "))
main(a, p, Ys)
# a : 150879928588932929063915567870431524202
# p : 170141183460469231731687303715884105727
# Ys : [244, 73, 77, 149, 250, 28, 117, 8, 9, 178, 104, 0, 17, 136, 153, 54, 219, 253, 72, 219, 153]
# Seed recovery complete
# Seed : 46006708213540093617449443081394574359