Skip to content

Commit

Permalink
Merge pull request #77 from grhkm21/fix-slow-bkw
Browse files Browse the repository at this point in the history
Speed up `coded_bkw` computation
  • Loading branch information
malb authored Feb 3, 2024
2 parents de615e7 + 41462b8 commit 97c093d
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions estimator/lwe_bkw.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"""
See :ref:`Coded-BKW for LWE` for what is available.
"""
from sage.all import ZZ, ceil, log, floor, sqrt, var, find_root, erf, oo, cached_function
from sage.all import ZZ, ceil, log, floor, sqrt, find_root, erf, oo, cached_function

from .lwe_parameters import LWEParameters
from .util import local_minimum
from .cost import Cost
Expand Down Expand Up @@ -44,18 +45,21 @@ def ntest(n, ell, t1, t2, b, q):
return 0

# solve for ntest by aiming for ntop == 0
ntest = var("ntest")
sigma_set = sqrt(q ** (2 * (1 - ell / ntest)) / 12)
ncod = sum(CodedBKW.N(i, sigma_set, b, q) for i in range(1, t2 + 1))
ntop = n - ncod - ntest - t1 * b
def ntop(ntest):
# Patch so that `find_root` (which uses float) doesn't error
ntest = RR(ntest)
sigma_set = sqrt(q ** (2 * (1 - ell / ntest)) / 12)
ncod = sum(CodedBKW.N(i, sigma_set, b, q) for i in range(1, t2 + 1))
res = n - ncod - ntest - t1 * b
return res

try:
start = max(int(round(find_root(ntop, 2, n - t1 * b + 1, rtol=0.1))) - 1, 2)
except RuntimeError:
start = 2
ntest_min = 1
for ntest in range(start, n - t1 * b + 1):
if abs(ntop(ntest=ntest).n()) >= abs(ntop(ntest=ntest_min).n()):
if abs(ntop(ntest).n()) >= abs(ntop(ntest_min).n()):
break
ntest_min = ntest
return int(ntest_min)
Expand Down

0 comments on commit 97c093d

Please sign in to comment.