forked from data61/MP-SPDZ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiscreteGauss.h
86 lines (67 loc) · 2.09 KB
/
DiscreteGauss.h
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
#ifndef _DiscreteGauss
#define _DiscreteGauss
/* Class to sample from a Discrete Gauss distribution of
standard deviation R
*/
#include <FHE/Generator.h>
#include "Tools/random.h"
#include <vector>
#include <math.h>
class DiscreteGauss
{
/* This is the bound we use on for the NewHope approximation
* to a discrete Gaussian with sigma=sqrt(B/2)
*/
int NewHopeB;
public:
void set(double R);
void pack(octetStream& o) const { o.serialize(NewHopeB); }
void unpack(octetStream& o) { o.unserialize(NewHopeB); }
DiscreteGauss(double R) { set(R); }
// Rely on default copy constructor/assignment
int sample(PRNG& G, int stretch = 1) const;
double get_R() const { return sqrt(0.5 * NewHopeB); }
int get_NewHopeB() const { return NewHopeB; }
bool operator!=(const DiscreteGauss& other) const;
};
template<class T>
class RandomGenerator : public Generator<T>
{
protected:
mutable PRNG G;
public:
RandomGenerator(PRNG& G) { this->G.SetSeed(G); }
};
template<class T>
class UniformGenerator : public RandomGenerator<T>
{
int n_bits;
bool positive;
public:
UniformGenerator(PRNG& G, int n_bits, bool positive = true) :
RandomGenerator<T>(G), n_bits(n_bits), positive(positive) {}
Generator<T>* clone() const { return new UniformGenerator<T>(*this); }
void get(T& x) const { this->G.get(x, n_bits, positive); }
};
template<class T = bigint>
class GaussianGenerator : public RandomGenerator<T>
{
DiscreteGauss DG;
int stretch;
public:
GaussianGenerator(const DiscreteGauss& DG, PRNG& G, int stretch = 1) :
RandomGenerator<T>(G), DG(DG), stretch(stretch) {}
Generator<T>* clone() const { return new GaussianGenerator<T>(*this); }
void get(T& x) const { x = DG.sample(this->G, stretch); }
};
int sample_half(PRNG& G);
template<class T>
class HalfGenerator : public RandomGenerator<T>
{
public:
HalfGenerator(PRNG& G) :
RandomGenerator<T>(G) {}
Generator<T>* clone() const { return new HalfGenerator<T>(*this); }
void get(T& x) const { x = sample_half(this->G); }
};
#endif