Repository navigation
Expand file tree
/
Copy pathSampling.cc
More file actions
169 lines (147 loc) · 4.27 KB
/
Copy pathSampling.cc
File metadata and controls
169 lines (147 loc) · 4.27 KB
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
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <complex.h>
#include <time.h>
#include <NTL/ZZ.h>
#include <NTL/ZZX.h>
#include <NTL/mat_ZZ.h>
#include <gmp.h>
#include "Sampling.h"
#include "params.h"
using namespace std;
using namespace NTL;
//==============================================================================
// Takes in input a random value and samples from distribution D_{\sigma_2}^+,
// Samples an element in Z+ with probability proportionnal to 2^{-x^2}
//==============================================================================
unsigned int Sample0(unsigned long alea)
{
if((alea&1UL)==0UL)
{
return 0;
}
unsigned int i;
unsigned int k = 1;
unsigned long mask=0;
unsigned long aux;
for(i=1; i<1000;)
{
aux = (alea&mask);
alea = (alea>>k);
if(aux)
{
return Sample0(alea);
}
else
{
if((alea&1UL)==0UL)
{
return i;
}
}
i++;
k += 2;
mask = (mask<<2)|6UL;
}
cout << "ERROR" << endl;
return 999999;
}
//==============================================================================
// Samples from distribution D_{k\sigma_2}^+, ie
// Samples an element in Z+ with probability proportionnal to 2^{-(x/k)^2}
//==============================================================================
unsigned int Sample1(const unsigned int k)
{
unsigned int x, y, z;
unsigned long alea = rand();
x = Sample0(alea);
y = rand()%k;
z = k*x + y;
RR_t w = y*( (z<<1) - y );
RR_t borne = LDRMX / exp( w*log_2/(k*k) );
alea = rand();
if(alea>borne)
{
return Sample1(k);
}
else
{
return z;
}
cout << "ERROR" << endl;
return 999999;
}
//==============================================================================
// Samples from distribution D_{k\sigma_2}, ie
// Samples an element in Z with probability proportionnal to 2^{-(x/k)^2}
//==============================================================================
signed int Sample2(const unsigned int k)
{
signed int signe;
signed int x;
unsigned long alea = rand();
while(1)
{
x = Sample1(k);
if( (x!=0) || ((alea&1)==1) )
{
alea >>= 1;
signe = 1 - 2*(alea&1);
x *= signe;
return x;
}
alea >>= 1;
}
}
//==============================================================================
// Samples from distribution D_{sigma}, ie
// Samples an element in Z with probability proportionnal to e^{-x^2/2*(sigma^2)}
//==============================================================================
signed int Sample3(const RR_t sigma128)
{
signed int x;
double alea, borne;
const RR_t sigma = sigma128;
const unsigned long k = ( (unsigned long) ceil( (RR_t) sigma/sigma_1 ) );
while(1)
{
x = Sample2(k);
alea = ((RR_t)rand()) / LDRMX;
borne = exp( -x*x*( 1/(2*sigma*sigma) - 1/(2*k*k*sigma_1*sigma_1) ) );
assert(borne<=1);
if(alea<borne)
{
return x;
}
}
}
//==============================================================================
// Samples from distribution D_{c,sigma}, ie
// Samples an element in Z with probability proportionnal to e^{-(c-x)^2/2*(sigma^2)}
//==============================================================================
signed int Sample4(RR_t c, RR_t sigma)
{
// cout << "Sampling D_{c,sigma} for c = " << c << ", and sigma = " << sigma << endl;
RR_t alea, borne;
signed int x;
unsigned int coin;
const signed int intc = ( (signed int) floor(c) );
const RR_t fracc = c-intc;
coin = rand();
const RR_t denom = 1/(2*sigma*sigma);
while(1)
{
x = Sample3(sigma);
x += (coin&1);
if(abs(x)>8){cout << x << endl;}
coin >>= 1;
borne = exp(-(x-fracc)*(x-fracc)*denom)/ ( exp(-x*x*denom) + exp(-(x-1)*(x-1)*denom) );
assert(borne<1);
alea = ( (RR_t)rand() ) / LDRMX;
if(alea<borne)
{
return (x+intc);
}
}
}