forked from bristolcrypto/SPDZ-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Random_Coins.h
102 lines (75 loc) · 2.74 KB
/
Random_Coins.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// (C) 2017 University of Bristol. See License.txt
#ifndef _Random_Coins
#define _Random_Coins
/* Randomness used to encrypt */
#include "FHE/FHE_Params.h"
#include "FHE/Rq_Element.h"
#include "FHE/AddableVector.h"
class FHE_PK;
class Int_Random_Coins : public AddableMatrix<bigint>
{
const FHE_Params* params;
public:
Int_Random_Coins(const FHE_Params& params) : params(¶ms)
{ resize(3, params.phi_m()); }
Int_Random_Coins(const FHE_PK& pk);
void sample(PRNG& G)
{
(*this)[0].from(HalfGenerator(G));
for (int i = 1; i < 3; i++)
(*this)[i].from(GaussianGenerator(params->get_DG(), G));
}
};
class Random_Coins
{
Rq_Element uu,vv,ww;
const FHE_Params *params;
public:
const FHE_Params& get_params() const { return *params; }
Random_Coins(const FHE_Params& p)
: uu(p.FFTD(),evaluation,evaluation),
vv(p.FFTD(),evaluation,evaluation),
ww(p.FFTD(),polynomial,polynomial)
{ params=&p; }
Random_Coins(const FHE_PK& pk);
~Random_Coins() { ; }
// Rely on default copy assignment/constructor
const Rq_Element& u() const { return uu; }
const Rq_Element& v() const { return vv; }
const Rq_Element& w() const { return ww; }
void assign(const Rq_Element& u,const Rq_Element& v,const Rq_Element& w)
{ uu=u; vv=v; ww=w; }
template <class T>
void assign(const vector<T>& u,const vector<T>& v,const vector<T>& w)
{ uu.from_vec(u); vv.from_vec(v); ww.from_vec(w); }
void assign(const Int_Random_Coins& rc)
{ uu.from_vec(rc[0]); vv.from_vec(rc[1]); ww.from_vec(rc[2]); }
/* Generate a standard distribution */
void generate(PRNG& G)
{ uu.from(HalfGenerator(G));
vv.from(GaussianGenerator(params->get_DG(), G));
ww.from(GaussianGenerator(params->get_DG(), G));
}
// Generate all from Uniform in range (-B,...B)
void generateUniform(PRNG& G,const bigint& B1,const bigint& B2,const bigint& B3)
{
if (B1 == 0)
uu.assign_zero();
else
uu.from(UniformGenerator(G,numBits(B1)));
vv.from(UniformGenerator(G,numBits(B2)));
ww.from(UniformGenerator(G,numBits(B3)));
}
// ans,x and y must have same params otherwise error
friend void add(Random_Coins& ans,
const Random_Coins& x,const Random_Coins& y);
// Don't bother outputing params, assumes these are implicitly known
friend ostream& operator<<(ostream& s,const Random_Coins& rc)
{ s << rc.uu << " " << rc.vv << " " << rc.ww;
return s;
}
void pack(octetStream& o) const { uu.pack(o); vv.pack(o); ww.pack(o); }
size_t report_size(ReportType type)
{ return uu.report_size(type) + vv.report_size(type) + ww.report_size(type); }
};
#endif