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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
| from subprocess import check_output, DEVNULL, CalledProcessError import itertools import IPython import time
def to_fplll_format(M): m, n = M.dimensions() ret = "" s = "[" for i in range(m): s += "[" for j in range(n): s += str(M[i, j]) if j < n - 1: s += " " s += "]" ret += s + "\n" s = "" ret += "]" return ret
def from_fplll_format(s): rows = [] for line in s.splitlines(): line = line.lstrip("[").rstrip("\n").rstrip("]") if len(line) == 0: break
row = [int(x) for x in line.split(" ") if len(x) > 0 and x != "]"] rows += [row] m = len(rows) n = len(rows[0]) for row in rows: assert len(row) == n
L = Matrix(ZZ, m, n) for i in range(m): for j in range(n): L[i, j] = rows[i][j] return L
def sort_by_norm(M): M2 = [(sum([y ^ 2 for y in x]), x) for x in M] M2.sort() return matrix([y for x, y in M2])
def LLL(M): return M.LLL()
def flatter(M): s = to_fplll_format(M) try: ret = check_output(["flatter"], input=s.encode(), stderr=DEVNULL) return from_fplll_format(ret.decode()) except CalledProcessError: print("Flatter error, matrix written to /tmp/flatter_error") with open("/tmp/flatter_error", "w") as f: f.write(s) return M.change_ring(ZZ)
def subset_sum(): print("=" * 40) print("Subset sum") print("=" * 40) n = 128 B = vector([ZZ(randrange(0, 1 << 2048)) for _ in range(n)]) s = random_vector(Zmod(2), n).change_ring(ZZ) C = B * s density = n / log(max(B), 2) print("density =", density.n())
M = Matrix(ZZ, n + 1, n + 1) for i in range(n): M[i, i] = 2 M[i, n] = B[i] M[n, i] = -1 M[n, n] = -C M[:, n] *= M.det()
print("flatter") print(IPython.get_ipython().run_line_magic("time", "flatter(M)[0]")) print("LLL") print(IPython.get_ipython().run_line_magic("time", "M.LLL()[0]")) print(vector([2 * x - 1 for x in s]))
def small_roots(f, bounds, m=1, d=None, reduction=LLL): if not d: d = f.degree()
R = f.base_ring() N = R.cardinality()
f /= f.coefficients().pop(0) f = f.change_ring(ZZ)
G = Sequence([], f.parent()) for i in range(m + 1): base = N ^ (m - i) * f ^ i for shifts in itertools.product(range(d), repeat=f.nvariables()): g = base * prod(map(power, f.variables(), shifts)) G.append(g)
B, monomials = G.coefficients_monomials() monomials = vector(monomials)
factors = [monomial(*bounds) for monomial in monomials] for i, factor in enumerate(factors): B.rescale_col(i, factor)
B = reduction(B.dense_matrix())
B = B.change_ring(QQ) for i, factor in enumerate(factors): B.rescale_col(i, 1 / factor)
H = Sequence([], f.parent().change_ring(QQ)) for h in filter(None, B * monomials): H.append(h) I = H.ideal() if I.dimension() == -1: H.pop() elif I.dimension() == 0: roots = [] for root in I.variety(ring=QQ, algorithm="msolve", proof=False): root = tuple(R(root[var]) for var in f.variables()) roots.append(root) return roots
return []
def univariate(): print("=" * 40) print("Univariate Coppersmith") print("=" * 40) p = random_prime(2 ^ 1024, proof=False) q = random_prime(2 ^ 1024, proof=False) N = p * q bounds = (floor(N ^ 0.315),) roots = tuple(randrange(bound) for bound in bounds) R = Integers(N) P = PolynomialRing(R, 1, "x") x = P.gen() monomials = [x, x ^ 2, x ^ 3] f = sum(randrange(N) * monomial for monomial in monomials) f -= f(*roots) print("flatter") print( IPython.get_ipython().run_line_magic( "time", "small_roots(f, bounds, m=12, reduction=flatter)" ) ) print("LLL") print(IPython.get_ipython().run_line_magic("time", "small_roots(f, bounds, m=12)"))
def bivariate(): def flatter_echelon(M): st = time.time() M = M.echelon_form(algorithm="pari", include_zero_rows=False) print("Echelon form done", time.time() - st) return flatter(M)
def flatter_echelon_sort_by_norm(M): st = time.time() M = M.echelon_form(algorithm="pari", include_zero_rows=False) M = sort_by_norm(M) print("Echelon form + sort done", time.time() - st) return flatter(M)
print("=" * 40) print("Bivariate Coppersmith") print("=" * 40) p = random_prime(2 ^ 1024, proof=False) q = random_prime(2 ^ 1024, proof=False) N = p * q bounds = (floor(N ^ 0.12), floor(N ^ 0.12)) roots = tuple(randrange(bound) for bound in bounds) R = Integers(N) P = PolynomialRing(R, "x, y") x, y = P.gens() monomials = [x, y, x * y, x ^ 2, y ^ 2] f = sum(randrange(N) * monomial for monomial in monomials) f -= f(*roots) print("flatter") print( IPython.get_ipython().run_line_magic( "time", "small_roots(f, bounds, m=4, d=3, reduction=flatter)" ) ) print("flatter_echelon") print( IPython.get_ipython().run_line_magic( "time", "small_roots(f, bounds, m=4, d=3, reduction=flatter_echelon)" ) ) print("flatter_echelon_sort_by_norm") print( IPython.get_ipython().run_line_magic( "time", "small_roots(f, bounds, m=4, d=3, reduction=flatter_echelon_sort_by_norm)", ) ) print("LLL") print( IPython.get_ipython().run_line_magic("time", "small_roots(f, bounds, m=4, d=3)") )
if __name__ == "__main__": subset_sum() univariate() bivariate() ''' ======================================== Subset sum ======================================== density = 0.0625000360302351 flatter CPU times: user 13.8 ms, sys: 114 µs, total: 13.9 ms Wall time: 9.1 s (1, -1, 1, -1, -1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, 1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, 0) LLL CPU times: user 13.5 s, sys: 9.3 ms, total: 13.5 s Wall time: 13.5 s (-1, 1, -1, 1, 1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, 1, 1, -1, 1, -1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, 1, -1, -1, -1, 0) (-1, 1, -1, 1, 1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, 1, 1, -1, 1, -1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, 1, -1, -1, -1) ======================================== Univariate Coppersmith ======================================== flatter CPU times: user 577 ms, sys: 10.3 ms, total: 587 ms Wall time: 3.02 s [(52898868887146752972212802565290540830347003906693038177010989263599521640793523062140183403701304030725704274812929807988885129712177601140616553514257943382667480845507998269206545006670478278,)] LLL CPU times: user 21.6 s, sys: 0 ns, total: 21.6 s Wall time: 22 s [(52898868887146752972212802565290540830347003906693038177010989263599521640793523062140183403701304030725704274812929807988885129712177601140616553514257943382667480845507998269206545006670478278,)] ======================================== Bivariate Coppersmith ======================================== flatter Flatter error, matrix written to /tmp/flatter_error CPU times: user 52.5 ms, sys: 0 ns, total: 52.5 ms Wall time: 795 ms [(0, 0)] flatter_echelon Echelon form done 0.05237317085266113 CPU times: user 158 ms, sys: 0 ns, total: 158 ms Wall time: 20.4 s [(20370105518095839079344261190460931964311641178643539749741763646736449383, 54661297645350919577042112920463577161355888674536247152335056763963988510)] flatter_echelon_sort_by_norm Echelon form + sort done 0.06062889099121094 CPU times: user 160 ms, sys: 9.74 ms, total: 169 ms Wall time: 6.31 s [(20370105518095839079344261190460931964311641178643539749741763646736449383, 54661297645350919577042112920463577161355888674536247152335056763963988510)] LLL CPU times: user 1.14 s, sys: 7 µs, total: 1.14 s Wall time: 1.25 s [(20370105518095839079344261190460931964311641178643539749741763646736449383, 54661297645350919577042112920463577161355888674536247152335056763963988510)]
'''
|