-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathStatistics.h
210 lines (168 loc) · 5.35 KB
/
Statistics.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
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
#ifndef _STATISTICS_H_
#define _STATISTICS_H_
#include "FHEContext.h"
#include <vector>
#include "FHE-SI.h"
#include "Ciphertext.h"
#include "Matrix.h"
#include "Matrix.cpp"
class Statistics {
public:
Statistics(const FHEcontext &context) : context(context), secretKey(context),
publicKey(secretKey), keySwitch(secretKey),
data(Ciphertext(publicKey)) {
int k = context.Generator();
int nSlots = context.GetPlaintextSpace().GetUsableSlots();
while (nSlots > 1) {
autoKeySwitch.push_back(KeySwitchSI(secretKey, k));
nSlots >>= 1;
k *= k;
k %= context.zMstar.M();
}
}
void AddData(const Matrix<Plaintext> &blocks, const vector<Plaintext> &blockSizes) {
for (unsigned i = 0; i < blocks.NumRows(); i++) {
vector<Ciphertext> encExample(blocks[i].size(), Ciphertext(publicKey));
for (unsigned j = 0; j < blocks[i].size(); j++) {
publicKey.Encrypt(encExample[j], blocks[i][j]);
}
Ciphertext encN(publicKey);
publicKey.Encrypt(encN, blockSizes[i]);
data.AddRow(encExample);
nElems.push_back(encN);
}
}
void Clear() {
data.Clear();
nElems.clear();
}
void ComputeNthMoment(vector<Ciphertext> &moment, Ciphertext &denom, unsigned n) {
if (n < 1 || n > 2) { // Not supported currently
return;
}
moment.resize(data.NumCols(), Ciphertext(publicKey));
denom = nElems[0];
for (unsigned j = 0; j < data.NumCols(); j++) {
moment[j] = data(0, j);
if (n == 2) {
moment[j] *= moment[j];
}
for (unsigned i = 1; i < data.NumRows(); i++) {
if (j == 0) {
denom += nElems[i];
}
Ciphertext tmp = data(i, j);
if (n == 2) {
tmp *= tmp;
}
moment[j] += tmp;
}
if (n == 2) {
keySwitch.ApplyKeySwitch(moment[j]);
}
SumBatchedData(moment[j]);
}
Ciphertext noise(publicKey);
for (unsigned i = 0; i < moment.size(); i++) {
GenerateNoise(noise);
moment[i] += noise;
}
}
void ComputeCovariance(Matrix<Ciphertext> &cov, vector<Ciphertext> &mu, Ciphertext &n, Ciphertext &n2) {
ComputeNthMoment(mu, n, 1);
Ciphertext dummy(publicKey);
Matrix<Ciphertext> muMat(dummy);
muMat.AddRow(mu);
muMat.Transpose();
muMat.MultByTranspose();
// Symmetric matrix so just process half the entries
for (unsigned i = 0; i < muMat.NumRows(); i++) {
for (unsigned j = i; j < muMat.NumCols(); j++) {
keySwitch.ApplyKeySwitch(muMat(i,j));
muMat(i,j) *= -1;
}
}
cov = data;
cov.Transpose();
cov.MultByTranspose();
Ciphertext noise(publicKey);
// Again, symmetric matrix so we just process half the entries
for (unsigned i = 0; i < cov.NumRows(); i++) {
for (unsigned j = i; j < cov.NumCols(); j++) {
keySwitch.ApplyKeySwitch(cov(i,j));
SumBatchedData(cov(i,j));
cov(i,j) *= n;
keySwitch.ApplyKeySwitch(cov(i,j));
cov(i,j) += muMat(i,j);
GenerateNoise(noise);
cov(i,j) += noise;
cov(j,i) = cov(i,j);
}
}
n2 = n;
n2 *= n2;
keySwitch.ApplyKeySwitch(n2);
}
// For debugging purposes
FHESISecKey &GetSecretKey() { return secretKey; }
FHESIPubKey &GetPublicKey() { return publicKey; }
private:
const FHEcontext &context;
FHESISecKey secretKey;
FHESIPubKey publicKey;
KeySwitchSI keySwitch;
vector<KeySwitchSI> autoKeySwitch;
Matrix<Ciphertext> data;
vector<Ciphertext> nElems;
void SumBatchedData(Ciphertext &batchedData) const {
int k = context.Generator();
for (unsigned i = 0; i < autoKeySwitch.size(); i++) {
Ciphertext tmp = batchedData;
tmp >>= k;
autoKeySwitch[i].ApplyKeySwitch(tmp);
batchedData += tmp;
k *= k;
k %= context.zMstar.M();
}
}
void GenerateNoise(Ciphertext &noiseCtxt) const {
vector<ZZ_pX> randVec(context.GetPlaintextSpace().GetTotalSlots());
randVec[0] = to_ZZ_pX(0);
for (unsigned i = 1; i < randVec.size(); i++) {
randVec[i] = to_ZZ_pX(random_ZZ_p());
}
Plaintext ptxt(context);
ptxt.EmbedInSlots(randVec, false);
publicKey.Encrypt(noiseCtxt, ptxt);
}
};
void ComputeNthMomentPT(vector<ZZ> &moment, unsigned n, const Matrix<ZZ> &data) {
moment.resize(data.NumCols());
for (unsigned j = 0; j < data.NumCols(); j++) {
moment[j] = 0;
ZZ val;
for (unsigned i = 0; i < data.NumRows(); i++) {
power(val, data(i,j), n);
moment[j] += val;
}
}
}
void ComputeMomentsPT(vector<ZZ> &sum, vector<ZZ> &sqSum, const Matrix<ZZ> &data) {
ComputeNthMomentPT(sum, 1, data);
ComputeNthMomentPT(sqSum, 2, data);
}
void ComputeCovariancePT(Matrix<ZZ> &cov, const Matrix<ZZ> &data) {
cov = data;
cov.Transpose();
cov.MultByTranspose();
ZZ n = to_ZZ(data.NumRows());
cov *= n;
vector<ZZ> muVec;
ComputeNthMomentPT(muVec, 1, data);
Matrix<ZZ> mu;
mu.AddRow(muVec);
mu.Transpose();
mu.MultByTranspose();
cov -= mu;
}
#endif