-
Notifications
You must be signed in to change notification settings - Fork 1
/
hastad_broadcast.py
36 lines (32 loc) · 1006 Bytes
/
hastad_broadcast.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
import operator
from gmpy2 import iroot
from functools import reduce
from Crypto.Util.number import inverse
def crt(remainders, modules):
"""
Solving Chinese Remainder Theorem
@modules and @remainders are lists.
"""
x = 0
N = reduce(operator.mul, modules)
for i, module in enumerate(modules):
if module == 1:
continue
Ni = N // module
b = inverse(Ni, module)
x += remainders[i] * Ni * b
return x % N
def hastad_broadcast(cipher, module, exponent):
"""
Chinese Remainder
If the same message, encrypt by same exponent but different module
hastad broadcast attack may solved it.
usage : hastad_broadcast([C1,C2,C3],[N1,N2,N3])
return : C = M^e mod (N1*N2*N3)
if M^e < N1*N2*N3 : solved.
"""
assert len(cipher) == len(module), "Amount of (cipher, modulo) pair unmatch."
C = crt(cipher,module)
m, root = iroot(C, exponent)
if root == True :
return int(m)