Skip to content

Commit

Permalink
perf(prob): improve performance of hybrid bdd
Browse files Browse the repository at this point in the history
  • Loading branch information
bencrts committed Dec 18, 2024
1 parent 374f073 commit 76efab2
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions estimator/prob.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,23 @@ def mitm_babai_probability(r, stddev, fast=False):
:param fast: toggle for setting p = 1 (faster, but underestimates security)
:return: probability for the mitm process
"""
import numpy as np
from scipy.special import erf as erf_s

if fast:
# overestimate the probability -> underestimate security
return 1

# Note: `r` contains *square norms*, so convert to non-square norms.
# Follow the proof of Lemma 4.2 [WAHC:SonChe19]_, because that one uses standard deviation.
xs = [sqrt(.5 * ri) / stddev for ri in r]
p = prod(RR(erf(x) - (1 - exp(-x**2)) / (x * sqrt(pi))) for x in xs)
assert 0.0 <= p <= 1.0

# Take constant sqrt(pi) out of the array so we only compute once
sqrt_pi = np.sqrt(np.pi)
# Use numpy for fast computation of sqrt, erf, exp
xs = np.sqrt(0.5 * np.array(r)) / stddev
erf_xs = erf_s(xs)
p_vec = erf_xs - (1 - np.exp(-xs**2)) / (xs * sqrt_pi)
p = RR(np.prod(p_vec))

return p


Expand Down

0 comments on commit 76efab2

Please sign in to comment.