-
Notifications
You must be signed in to change notification settings - Fork 1
/
probabilities.py
368 lines (283 loc) · 10.9 KB
/
probabilities.py
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# -*- coding: utf-8 -*-
"""
Estimating relevant probabilities on the sphere and for popcount.
To run doctests, run: ``PYTHONPATH=`pwd` sage -t probabilities.py``
"""
from mpmath import mp
from collections import namedtuple
from functools import partial
from memoize import memoize
Probabilities = namedtuple(
"Probabilities", ("d", "n", "k", "gr", "ngr", "pf", "ngr_pf", "gr_pf", "rho", "eta", "beta", "prec")
)
def C(d, theta, integrate=False, prec=None):
"""
The probability that some v from the sphere has angle at most θ with some fixed u.
:param d: We consider spheres of dimension `d-1`
:param theta: angle in radians
:param: compute via explicit integration
:param: precision to use
EXAMPLE::
sage: C(80, pi/3)
mpf('1.0042233739846629e-6')
"""
prec = prec if prec else mp.prec
with mp.workprec(prec):
theta = mp.mpf(theta)
d = mp.mpf(d)
if integrate:
r = (
1
/ mp.sqrt(mp.pi)
* mp.gamma(d / 2)
/ mp.gamma((d - 1) / 2)
* mp.quad(lambda x: mp.sin(x) ** (d - 2), (0, theta), error=True)[0]
)
else:
r = mp.betainc((d - 1) / 2, 1 / 2.0, x2=mp.sin(theta) ** 2, regularized=True) / 2
return r
def A(d, theta, prec=53):
"""
The density of the event that some v from the sphere has angle θ with some fixed u.
:param d: We consider spheres of dimension `d-1`
:param theta: angle in radians
:param: compute via explicit integration
:param: precision to use
EXAMPLES::
sage: A(80, pi/3)
mpf('4.7395659506025816e-5')
sage: A(80, pi/3) * 2*pi/100000
mpf('2.9779571143234787e-9')
sage: C(80, pi/3+pi/100000) - C(80, pi/3-pi/100000)
mpf('2.9779580567976835e-9')
"""
prec = prec if prec else mp.prec
with mp.workprec(prec):
theta = mp.mpf(theta)
d = mp.mpf(d)
r = 1 / mp.sqrt(mp.pi) * mp.gamma(d / 2) / mp.gamma((d - 1) / 2) * mp.sin(theta) ** (d - 2)
return r
@memoize
def log2_sphere(d):
# NOTE: hardcoding 53 here
with mp.workprec(53):
return (d / 2 * mp.log(mp.pi, 2) + 1) / mp.gamma(d / 2)
@memoize
def sphere(d):
# NOTE: hardcoding 53 here
with mp.workprec(53):
return 2 ** (d / 2 * mp.log(mp.pi, 2) + 1) / mp.gamma(d / 2)
@memoize
def W(d, alpha, beta, theta, integrate=True, prec=None):
assert alpha <= mp.pi / 2
assert beta <= mp.pi / 2
assert 0 >= (mp.cos(beta) - mp.cos(alpha) * mp.cos(theta)) * (mp.cos(beta) * mp.cos(theta) - mp.cos(alpha))
if theta >= alpha + beta:
return mp.mpf(0.0)
prec = prec if prec else mp.prec
with mp.workprec(prec):
alpha = mp.mpf(alpha)
beta = mp.mpf(beta)
theta = mp.mpf(theta)
d = mp.mpf(d)
if integrate:
c = mp.atan(mp.cos(alpha) / (mp.cos(beta) * mp.sin(theta)) - 1 / mp.tan(theta))
def f_alpha(x):
return mp.sin(x) ** (d - 2) * mp.betainc(
(d - 2) / 2,
1 / 2.0,
x2=mp.sin(mp.re(mp.acos(mp.tan(theta - c) / mp.tan(x)))) ** 2,
regularized=True,
)
def f_beta(x):
return mp.sin(x) ** (d - 2) * mp.betainc(
(d - 2) / 2, 1 / 2.0, x2=mp.sin(mp.re(mp.acos(mp.tan(c) / mp.tan(x)))) ** 2, regularized=True
)
S_alpha = mp.quad(f_alpha, (theta - c, alpha), error=True)[0] / 2
S_beta = mp.quad(f_beta, (c, beta), error=True)[0] / 2
return (S_alpha + S_beta) * sphere(d - 1) / sphere(d)
else:
# Wedge volume formula from Lemma 2.2 of [BDGL16] Anja Becker, Léo Ducas, Nicolas Gama,
# Thijs Laarhoven. "New directions in nearest neighbor searching with applications to
# lattice sieving." SODA 2016. https://eprint.iacr.org/2015/1128
# g_sq = (mp.cos(alpha)**2 + mp.cos(beta)**2 -
# 2*mp.cos(alpha)*mp.cos(beta)*mp.cos(theta))/mp.sin(theta)**2
# log2_A = mp.log(g_sq, 2) - 2*mp.log(1-g_sq, 2)
# r = (d-4) * mp.log(mp.sqrt(1-g_sq), 2) + log2_A - 2*mp.log(d-4, 2) + log2_sphere(d-2) - log2_sphere(d)
# return 2**r
raise NotImplementedError("Results don't match.")
@memoize
def binomial(n, i):
# NOTE: hardcoding 53 here
with mp.workprec(53):
return mp.binomial(n, i)
@memoize
def P(n, k, theta, prec=None):
"""
Probability that two vectors with angle θ pass a popcount filter
:param n: number of popcount vectors
:param k: number of popcount tests required to pass
:param theta: angle in radians
"""
prec = prec if prec else mp.prec
with mp.workprec(prec):
theta = mp.mpf(theta)
# binomial cdf for 0 <= successes <= k
# r = 0
# for i in range(k):
# r += binomial(n, i) * (theta/mp.pi)**i * (1-theta/mp.pi)**(n-i)
# r = mp.betainc(n-k, k+1, x2=1-(theta/mp.pi), regularized=True)
# NOTE: This routine uses obscene precision
def _betainc(a, b, x2):
return (
x2 ** a
* mp.hyp2f1(
a, 1 - b, a + 1, x2, maxprec=2 ** mp.ceil(2 * mp.log(n, 2)), maxterms=2 ** mp.ceil(mp.log(n, 2))
)
/ a
/ mp.beta(a, b)
)
r = _betainc(n - k, k + 1, x2=1 - (theta / mp.pi))
return r
def pf(d, n, k, beta=None, lb=None, ub=None, beta_and=False, prec=None):
"""
Let `Pr[P_{k,n}]` be the probability that a popcount filter passes. We assume the probability
is over the vectors `u,v`. Let `¬G` be the event that two random vectors are not Gauss reduced.
We start with Pr[P_{k,n}]::
sage: pf(80, 128, 40)
mpf('0.00031063713572376122')
sage: pf(80, 128, 128)
mpf('1.0000000000000002')
Pr[P_{k,n} ∧ ¬G]::
sage: pf(80, 128, 40, ub=mp.pi/3)
mpf('3.3598092589552732e-7')
Pr[¬G]::
sage: pf(80, 128, 128, ub=mp.pi/3)
mpf('1.0042233739846644e-6')
sage: ngr_pf(80, 128, 128)
mpf('1.0042233739846644e-6')
sage: ngr(80)
mpf('1.0042233739846629e-6')
Pr[Pr_{k,n} ∧ G]::
sage: pf(80, 128, 40, lb=mp.pi/3)
mpf('0.00031030115479786595')
Pr[G]::
sage: pf(80, 128, 128, lb=mp.pi/3)
mpf('0.99999899577662632')
sage: gr_pf(80, 128, 128)
mpf('0.99999899577662632')
sage: gr(80)
mpf('0.99999899577662599')
Pr[P_{k,n} | C(w,β)]::
sage: pf(80, 128, 40, beta=mp.pi/3)
mpf('0.019786655048072234')
Pr[P_{k,n} ∧ ¬G | C(w,β)]::
sage: pf(80, 128, 40, beta=mp.pi/3, ub=mp.pi/3)
mpf('0.00077177364924089652')
Pr[¬G | C(w,β)]::
sage: pf(80, 128, 128, beta=mp.pi/3, ub=mp.pi/3)
mpf('0.0021964683579090904')
sage: ngr_pf(80, 128, 128, beta=mp.pi/3)
mpf('0.0021964683579090904')
sage: ngr(80, beta=mp.pi/3)
mpf('0.0021964683579090904')
Pr[Pr_{k,n} ∧ G | C(w,β)]::
sage: pf(80, 128, 40, beta=mp.pi/3, lb=mp.pi/3)
mpf('0.019014953591444488')
sage: gr_pf(80, 128, 40, beta=mp.pi/3)
mpf('0.019014953591444488')
Pr[G | C(w,β)]::
sage: pf(80, 128, 128, beta=mp.pi/3, lb=mp.pi/3)
mpf('0.99780353164285229')
sage: gr_pf(80, 128, 128, beta=mp.pi/3)
mpf('0.99780353164285229')
sage: gr(80, beta=mp.pi/3)
mpf('0.9978035316420909')
:param d: We consider the sphere `S^{d-1}`
:param n: Number of popcount vectors
:param k: popcount threshold
:param beta: If not ``None`` vectors are considered in a bucket around some `w` with angle β.
:param lb: lower bound of integration (see above)
:param ub: upper bound of integration (see above)
:param beta_and: return Pr[P_{k,n} ∧ C(w,β)] instead of Pr[P_{k,n} | C(w,β)]
:param prec: compute with this precision
"""
prec = prec if prec else mp.prec
with mp.workprec(prec):
if lb is None:
lb = 0
if ub is None:
ub = mp.pi
if beta is None:
return mp.quad(lambda x: P(n, k, x) * A(d, x), (lb, ub), error=True)[0]
else:
num = mp.quad(lambda x: P(n, k, x) * W(d, beta, beta, x) * A(d, x), (lb, min(ub, 2 * beta)), error=True)[0]
if not beta_and:
den = mp.quad(lambda x: W(d, beta, beta, x) * A(d, x), (0, 2 * beta), error=True)[0]
else:
den = 1
return num / den
ngr_pf = partial(pf, lb=0, ub=mp.pi / 3)
gr_pf = partial(pf, lb=mp.pi / 3)
def ngr(d, beta=None, prec=None):
"""
Probability that two random vectors (in a cap parameterised by β) are not Gauss reduced.
:param d: We consider the sphere `S^{d-1}`
:param beta: If not ``None`` vectors are considered in a bucket around some `w` with angle β.
:param prec: compute with this precision
"""
prec = prec if prec else mp.prec
with mp.workprec(prec):
if beta is None:
return C(d, mp.pi / 3)
elif beta < mp.pi / 6:
return mp.mpf(1.0)
else:
# Pr[¬G ∧ E]
num = mp.quad(lambda x: W(d, beta, beta, x) * A(d, x), (0, mp.pi / 3), error=True)[0]
# Pr[E]
den = mp.quad(lambda x: W(d, beta, beta, x) * A(d, x), (0, 2 * beta), error=True)[0]
# Pr[¬G | E] = Pr[¬G ∧ E]/Pr[E]
return num / den
def gr(d, beta=None, prec=None):
"""
Probability that two random vectors (in a cap parameterised by β) are Gauss reduced.
:param d: We consider the sphere `S^{d-1}`
:param beta: If not ``None`` vectors are considered in a bucket around some `w` with angle β.
:param prec: compute with this precision
"""
prec = prec if prec else mp.prec
with mp.workprec(prec):
return 1 - ngr(d, beta, prec)
def probabilities(d, n, k, beta=None, prec=None):
"""
Useful probabilities.
:param d: We consider the sphere `S^{d-1}`
:param n: Number of popcount vectors
:param k: popcount threshold
:param beta: If not ``None`` vectors are considered in a bucket around some `w` with angle β.
:param prec: compute with this precision
"""
prec = prec if prec else mp.prec
with mp.workprec(prec):
pf_ = pf(d, n, k, beta=beta, prec=prec)
ngr_ = ngr(d, beta=beta, prec=prec)
ngr_pf_ = ngr_pf(d, n, k, beta=beta, prec=prec)
gr_pf_ = gr_pf(d, n, k, beta=beta, prec=prec)
rho = 1 - ngr_pf_ / pf_
eta = 1 - ngr_pf_ / ngr_
probs = Probabilities(
d=d,
n=n,
k=k,
ngr=ngr_,
gr=1 - ngr_,
pf=pf_,
gr_pf=gr_pf_,
ngr_pf=ngr_pf_,
rho=rho,
eta=eta,
beta=beta,
prec=prec,
)
return probs