Path: blob/master/shared/small_roots/blomer_may.py
2589 views
import logging12from sage.all import ZZ34from shared import small_roots567def modular_trivariate(f, N, m, t, X, Y, Z, roots_method="groebner"):8"""9Computes small modular roots of a trivariate polynomial.10More information: Blomer J., May A., "New Partial Key Exposure Attacks on RSA" (Section 4)11:param f: the polynomial12:param N: the modulus13:param m: the parameter m14:param t: the parameter t15:param X: an approximate bound on the x roots16:param Y: an approximate bound on the y roots17:param Z: an approximate bound on the z roots18:param roots_method: the method to use to find roots (default: "groebner")19:return: a generator generating small roots (tuples of x, y, and z roots) of the polynomial20"""21f = f.change_ring(ZZ)22pr = f.parent()23x, y, z = pr.gens()2425logging.debug("Generating shifts...")2627shifts = []28for i in range(m + 1):29for j in range(i + 1):30for k in range(j + 1):31g = x ** (j - k) * z ** k * N ** i * f ** (m - i)32shifts.append(g)3334for k in range(1, t + 1):35h = x ** j * y ** k * N ** i * f ** (m - i)36shifts.append(h)3738L, monomials = small_roots.create_lattice(pr, shifts, [X, Y, Z])39L = small_roots.reduce_lattice(L)40polynomials = small_roots.reconstruct_polynomials(L, f, N ** m, monomials, [X, Y, Z])41for roots in small_roots.find_roots(pr, polynomials, method=roots_method):42yield roots[x], roots[y], roots[z]434445def modular_bivariate(f, eM, m, t, Y, Z, roots_method="groebner"):46"""47Computes small modular roots of a bivariate polynomial.48More information: Blomer J., May A., "New Partial Key Exposure Attacks on RSA" (Section 6)49:param f: the polynomial50:param eM: the modulus51:param m: the parameter m52:param t: the parameter t53:param Y: an approximate bound on the y roots54:param Z: an approximate bound on the z roots55:param roots_method: the method to use to find roots (default: "groebner")56:return: a generator generating small roots (tuples of y and z roots) of the polynomial57"""58f = f.change_ring(ZZ)59pr = f.parent()60y, z = pr.gens()6162logging.debug("Generating shifts...")6364shifts = []65for i in range(m + 1):66for j in range(i + 1):67g = y ** j * eM ** i * f ** (m - i)68shifts.append(g)6970for j in range(1, t + 1):71h = z ** j * eM ** i * f ** (m - i)72shifts.append(h)7374L, monomials = small_roots.create_lattice(pr, shifts, [Y, Z])75L = small_roots.reduce_lattice(L)76polynomials = small_roots.reconstruct_polynomials(L, f, eM ** m, monomials, [Y, Z])77for roots in small_roots.find_roots(pr, polynomials, method=roots_method):78yield roots[y], roots[z]798081