From f5b888601ce76d3183997c13366a15c4cb9f98a1 Mon Sep 17 00:00:00 2001 From: Gareth Ma Date: Mon, 31 Jul 2023 01:12:56 +0300 Subject: [PATCH 1/5] Speed up `coded_bkw` computation Avoids symbolic computation at all cost for faster computation, especially since on line 62 there's a `sum` which computes the sum of (I believe) hundreds of expressions. Benchmarks on my laptop: Kyber512: 277.6s -> 12.25s Kyber1024: Long time -> 17.40s TFHE630: 539.4s -> 20.15s Look at that, 20x speed up! --- estimator/lwe_bkw.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/estimator/lwe_bkw.py b/estimator/lwe_bkw.py index 3ee4c7e..f40e601 100644 --- a/estimator/lwe_bkw.py +++ b/estimator/lwe_bkw.py @@ -2,7 +2,18 @@ """ 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.rings.all import QQ +from sage.calculus.var import var +from sage.functions.all import log +from sage.functions.error import erf +from sage.functions.other import floor, ceil +from sage.misc.cachefunc import cached_function +from sage.misc.functional import sqrt +from sage.numerical.optimize import find_root +from sage.rings.infinity import infinity as oo +from sage.rings.integer_ring import ZZ +from sage.rings.real_mpfr import RR + from .lwe_parameters import LWEParameters from .util import local_minimum from .cost import Cost @@ -44,10 +55,13 @@ 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) @@ -55,7 +69,7 @@ def ntest(n, ell, t1, t2, b, q): 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) From fe478add5298116a083e5680ccbb4cd61f4625b6 Mon Sep 17 00:00:00 2001 From: Gareth Ma Date: Tue, 22 Aug 2023 20:20:29 +0300 Subject: [PATCH 2/5] Apply review suggestions --- estimator/lwe_bkw.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/estimator/lwe_bkw.py b/estimator/lwe_bkw.py index f40e601..d74f116 100644 --- a/estimator/lwe_bkw.py +++ b/estimator/lwe_bkw.py @@ -3,8 +3,6 @@ See :ref:`Coded-BKW for LWE` for what is available. """ from sage.rings.all import QQ -from sage.calculus.var import var -from sage.functions.all import log from sage.functions.error import erf from sage.functions.other import floor, ceil from sage.misc.cachefunc import cached_function From 9ec2fd0c5a5b5243bb7023634ca6077234589072 Mon Sep 17 00:00:00 2001 From: Gareth Ma Date: Tue, 22 Aug 2023 20:25:05 +0300 Subject: [PATCH 3/5] Add log to import list --- estimator/lwe_bkw.py | 1 + 1 file changed, 1 insertion(+) diff --git a/estimator/lwe_bkw.py b/estimator/lwe_bkw.py index d74f116..caf2ba7 100644 --- a/estimator/lwe_bkw.py +++ b/estimator/lwe_bkw.py @@ -3,6 +3,7 @@ See :ref:`Coded-BKW for LWE` for what is available. """ from sage.rings.all import QQ +from sage.functions.all import log from sage.functions.error import erf from sage.functions.other import floor, ceil from sage.misc.cachefunc import cached_function From 1c9f2cd3658a2dae7ffa15c7188076f2db8b63c2 Mon Sep 17 00:00:00 2001 From: Gareth Ma Date: Wed, 23 Aug 2023 20:43:08 +0300 Subject: [PATCH 4/5] Reverted imports (issue #76) --- estimator/lwe_bkw.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/estimator/lwe_bkw.py b/estimator/lwe_bkw.py index caf2ba7..7bece07 100644 --- a/estimator/lwe_bkw.py +++ b/estimator/lwe_bkw.py @@ -2,16 +2,7 @@ """ See :ref:`Coded-BKW for LWE` for what is available. """ -from sage.rings.all import QQ -from sage.functions.all import log -from sage.functions.error import erf -from sage.functions.other import floor, ceil -from sage.misc.cachefunc import cached_function -from sage.misc.functional import sqrt -from sage.numerical.optimize import find_root -from sage.rings.infinity import infinity as oo -from sage.rings.integer_ring import ZZ -from sage.rings.real_mpfr import RR +from sage.all import ZZ, ceil, log, floor, sqrt, var, find_root, erf, oo, cached_function from .lwe_parameters import LWEParameters from .util import local_minimum From 41462b8c2e2031069455b4d765d9ab8c3706027f Mon Sep 17 00:00:00 2001 From: Gareth Ma Date: Wed, 23 Aug 2023 20:46:47 +0300 Subject: [PATCH 5/5] Apply review suggestions --- estimator/lwe_bkw.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/estimator/lwe_bkw.py b/estimator/lwe_bkw.py index 7bece07..c277b45 100644 --- a/estimator/lwe_bkw.py +++ b/estimator/lwe_bkw.py @@ -2,7 +2,7 @@ """ 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