forked from jvdsn/crypto-attacks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathherrmann_may.py
More file actions
56 lines (45 loc) · 1.81 KB
/
herrmann_may.py
File metadata and controls
56 lines (45 loc) · 1.81 KB
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
import logging
from sage.all import ZZ
from shared import small_roots
def modular_bivariate(f, e, m, t, X, Y, roots_method="groebner"):
"""
Computes small modular roots of a bivariate polynomial.
More information: Herrmann M., May A., "Maximizing Small Root Bounds by Linearization and Applications to Small Secret Exponent RSA"
:param f: the polynomial
:param e: the modulus
:param m: the amount of normal shifts to use
:param t: the amount of additional shifts to use
:param X: an approximate bound on the x roots
:param Y: an approximate bound on the y roots
:param roots_method: the method to use to find roots (default: "groebner")
:return: a generator generating small roots (tuples of x and y roots) of the polynomial
"""
f = f.change_ring(ZZ)
pr = ZZ["u, x, y"]
u, x, y = pr.gens()
qr = pr.quotient(1 + x * y - u)
U = X * Y
logging.debug("Generating shifts...")
shifts = set()
monomials = set()
for k in range(m + 1):
for i in range(m - k + 1):
g = x ** i * f ** k * e ** (m - k)
g = qr(g).lift()
shifts.add(g)
monomials.update(g.monomials())
for j in range(1, t + 1):
for k in range(m // t * j, m + 1):
h = y ** j * f ** k * e ** (m - k)
h = qr(h).lift()
shifts.add(h)
monomials.add(u ** k * y ** j)
L = small_roots.fill_lattice(shifts, monomials, [U, X, Y])
L = small_roots.reduce(L)
polynomials = small_roots.reconstruct_polynomials(L, monomials, [U, X, Y])
pr = f.parent()
x, y = pr.gens()
for i, polynomial in enumerate(polynomials):
polynomials[i] = polynomial(1 + x * y, x, y)
for roots in small_roots.find_roots(f, polynomials, pr, method=roots_method):
yield roots[x], roots[y]