-
Notifications
You must be signed in to change notification settings - Fork 121
/
herrmann_may.py
51 lines (40 loc) · 1.68 KB
/
herrmann_may.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
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["x", "y", "u"]
x, y, u = pr.gens()
qr = pr.quotient(1 + x * y - u)
U = X * Y
logging.debug("Generating shifts...")
shifts = []
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.append(g)
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.append(h)
L, monomials = small_roots.create_lattice(pr, shifts, [X, Y, U])
L = small_roots.reduce_lattice(L)
pr = f.parent()
x, y = pr.gens()
polynomials = small_roots.reconstruct_polynomials(L, f, None, monomials, [X, Y, U], preprocess_polynomial=lambda p: p(x, y, 1 + x * y))
for roots in small_roots.find_roots(pr, polynomials, method=roots_method):
yield roots[x], roots[y]