Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jvdsn
GitHub Repository: jvdsn/crypto-attacks
Path: blob/master/shared/small_roots/herrmann_may.py
2589 views
1
import logging
2
3
from sage.all import ZZ
4
5
from shared import small_roots
6
7
8
def modular_bivariate(f, e, m, t, X, Y, roots_method="groebner"):
9
"""
10
Computes small modular roots of a bivariate polynomial.
11
More information: Herrmann M., May A., "Maximizing Small Root Bounds by Linearization and Applications to Small Secret Exponent RSA"
12
:param f: the polynomial
13
:param e: the modulus
14
:param m: the amount of normal shifts to use
15
:param t: the amount of additional shifts to use
16
:param X: an approximate bound on the x roots
17
:param Y: an approximate bound on the y roots
18
:param roots_method: the method to use to find roots (default: "groebner")
19
:return: a generator generating small roots (tuples of x and y roots) of the polynomial
20
"""
21
f = f.change_ring(ZZ)
22
23
pr = ZZ["x", "y", "u"]
24
x, y, u = pr.gens()
25
qr = pr.quotient(1 + x * y - u)
26
U = X * Y
27
28
logging.debug("Generating shifts...")
29
30
shifts = []
31
for k in range(m + 1):
32
for i in range(m - k + 1):
33
g = x ** i * f ** k * e ** (m - k)
34
g = qr(g).lift()
35
shifts.append(g)
36
37
for j in range(1, t + 1):
38
for k in range(m // t * j, m + 1):
39
h = y ** j * f ** k * e ** (m - k)
40
h = qr(h).lift()
41
shifts.append(h)
42
43
L, monomials = small_roots.create_lattice(pr, shifts, [X, Y, U])
44
L = small_roots.reduce_lattice(L)
45
46
pr = f.parent()
47
x, y = pr.gens()
48
49
polynomials = small_roots.reconstruct_polynomials(L, f, None, monomials, [X, Y, U], preprocess_polynomial=lambda p: p(x, y, 1 + x * y))
50
for roots in small_roots.find_roots(pr, polynomials, method=roots_method):
51
yield roots[x], roots[y]
52
53