-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_bin.cpp
274 lines (245 loc) · 10.2 KB
/
model_bin.cpp
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
#include "seal/seal.h"
#include <iostream>
#include "helper.h"
#include <functional>
#include <cmath>
#include "SEAL/native/examples/examples.h"
#include <sys/wait.h>
#include <unistd.h>
using namespace std;
using namespace seal;
int main(int argc, char *argv[])
{
string phenotype = argv[1];
EncryptionParameters parms(scheme_type::bfv);
size_t poly_modulus_degree = 4096;
parms.set_poly_modulus_degree(poly_modulus_degree);
parms.set_coeff_modulus(CoeffModulus::Create(poly_modulus_degree, {35, 30, 35}));
parms.set_plain_modulus(PlainModulus::Batching(poly_modulus_degree, 25));
SEALContext context(parms);
print_parameters(context);
auto &context_data = *context.key_context_data();
uint64_t plain_modulus = context_data.parms().plain_modulus().value();
KeyGenerator keygen(context);
SecretKey secret_key = keygen.secret_key();
PublicKey public_key;
keygen.create_public_key(public_key);
RelinKeys relin_keys;
keygen.create_relin_keys(relin_keys);
Encryptor encryptor_sec(context, secret_key);
Encryptor encryptor_pub(context, public_key);
Evaluator evaluator(context);
Decryptor decryptor(context, secret_key);
BatchEncoder batch_encoder(context);
int slot_count = batch_encoder.slot_count();
string geno = "data/testset.tsv";
string coef = "weights/weight" + phenotype + ".csv";
int num_samples;
auto start_enc_geno = std::chrono::high_resolution_clock::now();
string filename = "savefiles/genotype_ctxt" + phenotype + ".dat";
cout << "\n-----------ENCRYPTING GENOTYPE (Client) -----------\n"
<< endl;
cout << "\nReading in the genotype:" << endl;
vector<vector<int>> genotype = TSVtoMatrix(geno);
vector<vector<int>> geno_T = transpose_matrix(genotype);
cout << "\nEncoding genotype:" << endl;
num_samples = geno_T.size();
int num_full_ctxt = 20390 / poly_modulus_degree;
vector<vector<Plaintext>> g(num_samples, vector<Plaintext>(num_full_ctxt + 1));
for (int sample = 0; sample < num_samples; sample++)
{
// encode zero vector to plaintext: this is because we don't know how to make zero plain matrix yet :(
// -----For genotype, insert in order
vector<int64_t> vec1(slot_count);
for (int j = 0; j < num_full_ctxt + 1; j++) {
batch_encoder.encode(vec1, g[sample][j]);
for (int i = 0; i < g[sample][0].coeff_count(); i++)
{
g[sample][j].data()[i] = 0;
}
}
// for Plaintext, plain.data()[index] means the "index"th coefficient of plaintext
// pad zero for all coefficients
for (int j = 0; j < num_full_ctxt; j++) {
for (int i = 0; i < g[sample][0].coeff_count(); i++)
{
g[sample][j].data()[i] = geno_T[sample][i + j * g[sample][0].coeff_count()];
}
}
int remainder = geno_T[sample].size() - num_full_ctxt * g[sample][0].coeff_count();
for (int i = 0; i < remainder; i++)
{
g[sample][num_full_ctxt].data()[i] = geno_T[sample][i + num_full_ctxt * g[sample][0].coeff_count()];
}
}
cout << "\nEncrypting snp vectors..." << endl;
vector<vector<Ciphertext>> gcipher(num_samples, vector<Ciphertext>(num_full_ctxt + 1));
for (int sample = 0; sample < num_samples; sample++)
{
for (int j = 0; j < num_full_ctxt + 1; j++) {
encryptor_sec.encrypt_symmetric(g[sample][j], gcipher[sample][j]);
}
}
// SAVING ENCRYPTED GENOTYPE
cout << "Enc encryption" << endl;
cout << "save ctxt to " << filename << endl;
SaveGenotypeEnc(gcipher, filename);
auto end_enc_geno = std::chrono::high_resolution_clock::now();
/*
-----For weight, insert backwards after the first element
If element is negative, insert positive element (-element) as coefficient
If element is positive, insert plain_modulus - element as coefficient
*/
int scaling = pow(2, 15);
string modelname = "savefiles/model_ctxt" + phenotype + ".dat";
auto start_enc_model = std::chrono::high_resolution_clock::now();
cout << "\n-----------ENCRYPTING MODEL (Modeler) -----------\n"
<< endl;
cout << "\nReading in the weights input:" << endl;
vector<double> input_weights = CSVtoVector(coef);
vector<int64_t> weights = ScaleVector(input_weights, scaling);
cout << "\nEncoding weights..." << endl;
vector<int64_t> vec4(slot_count);
vector<Plaintext> w(num_full_ctxt + 1);
for (int j = 0; j < w.size(); j++) {
batch_encoder.encode(vec4, w[j]);
}
// padding zero for the coefficients initially
for (int i = 0; i < w[0].coeff_count(); i++)
{
for (int j = 0; j < w.size(); j++) {
w[j].data()[i] = 0;
}
}
int num_coeff = w[0].coeff_count();
for (int k = 0; k < num_full_ctxt; k++) {
for (int j = 0; j < w[k].coeff_count(); j++)
{
if (j == 0)
{
if (weights[j + k * num_coeff] < 0)
{
w[k].data()[j] = plain_modulus - weights[j + k * num_coeff];
}
else
{
w[k].data()[j] = weights[j + k * num_coeff];
}
}
else
{
if (weights[j + k * num_coeff] > 0)
{
w[k].data()[poly_modulus_degree - j] = plain_modulus - weights[j + k * num_coeff];
}
else
{
w[k].data()[poly_modulus_degree - j] = -weights[j + k * num_coeff];
}
}
}
}
int remainder = weights.size() - num_full_ctxt * num_coeff;
for (int j = 0; j < remainder; j++)
{
if (j == 0)
{
if (weights[j + num_full_ctxt * num_coeff] < 0)
{
w[num_full_ctxt].data()[j] = plain_modulus - weights[j + num_full_ctxt * num_coeff];
}
else
{
w[num_full_ctxt].data()[j] = weights[j + num_full_ctxt * num_coeff];
}
}
else
{
if (weights[j + num_full_ctxt * num_coeff] > 0)
{
w[num_full_ctxt].data()[poly_modulus_degree - j] = plain_modulus - weights[j + num_full_ctxt * num_coeff];
}
else
{
w[num_full_ctxt].data()[poly_modulus_degree - j] = -weights[j + num_full_ctxt * num_coeff];
}
}
}
vector<Ciphertext> cipherweight(num_full_ctxt + 1);
cout << "\nEncrypting model..." << endl;
for (int j = 0; j < num_full_ctxt+1; j++) {
encryptor_pub.encrypt(w[j], cipherweight[j]);
}
cout << "Encryption done" << endl;
/// SAVING ENCRYPTED MODEL
cout << "save ctxt into " << modelname << endl;
SaveVec(cipherweight, modelname);
auto end_enc_model = std::chrono::high_resolution_clock::now();
auto start_eval = std::chrono::high_resolution_clock::now();
cout << "\n-----------EVALUATION (Evaluator) -----------\n"
<< endl;
//// LOADING ENCRYPTED GENOTYPE AND MODEL
cout << "load ctxts from " << filename << endl;
vector<vector<Ciphertext>> genocipher = LoadGenotype(filename, context, num_samples, num_full_ctxt + 1);
cout << "load ctxts from " << modelname << endl;
vector<Ciphertext> weightcipher = LoadVec(modelname, context, num_full_ctxt + 1);
vector<Ciphertext> predictions;
for (int sample = 0; sample < num_samples; sample++)
{
vector<Ciphertext> wg(num_full_ctxt + 1);
for (int j = 0; j < num_full_ctxt + 1; j++) {
evaluator.multiply(weightcipher[j], genocipher[sample][j], wg[j]);
}
for (int j = 1; j < num_full_ctxt + 1; j++) {
evaluator.add_inplace(wg[0], wg[j]);
}
predictions.push_back(wg[0]);
}
//// SAVING RESULTS
string resultsname = "savefiles/cipherresults" + phenotype + ".dat";
cout << "save results into " << resultsname << endl;
SaveVec(predictions, resultsname);
auto end_eval = std::chrono::high_resolution_clock::now();
auto start_dec = std::chrono::high_resolution_clock::now();
cout << "\n-----------DECRYPTION (Client) -----------\n"
<< endl;
cout << "load ctxts from " << resultsname << endl;
vector<Ciphertext> resultciphers = LoadVec(resultsname, context, num_samples);
vector<Plaintext> pt_result;
for (int sample = 0; sample < num_samples; sample++)
{
Plaintext result;
decryptor.decrypt(resultciphers[sample], result);
pt_result.push_back(result);
}
auto end_dec = std::chrono::high_resolution_clock::now();
cout << "\n-----------Saving RESULTS-----------\n"
<< endl;
vector<double> final_result;
for (int sample = 0; sample < num_samples; sample++)
{
double final;
if (pt_result[sample].data()[0] > plain_modulus / 2)
{
final = -(double)(plain_modulus - pt_result[sample].data()[0]) / (double)scaling;
}
else
{
final = (double)pt_result[sample].data()[0] / (double)scaling;
}
final_result.push_back(final);
}
ofstream ofs;
SaveResult(final_result, "output/out_model" + phenotype + ".csv");
auto duration_enc_geno = std::chrono::duration_cast<std::chrono::milliseconds>(end_enc_geno - start_enc_geno);
auto duration_enc_model = std::chrono::duration_cast<std::chrono::milliseconds>(end_enc_model - start_enc_model);
auto duration_eval = std::chrono::duration_cast<std::chrono::milliseconds>(end_eval - start_eval);
auto duration_dec = std::chrono::duration_cast<std::chrono::milliseconds>(end_dec - start_dec);
auto total_time = duration_enc_geno.count() + duration_enc_model.count() + duration_eval.count() + duration_dec.count();
cout << "enc_geno time (ms) : " << duration_enc_geno.count() << endl;
cout << "enc_model time (ms) : " << duration_enc_model.count() << endl;
cout << "eval time (ms) : " << duration_eval.count() << endl;
cout << "dec time (ms) : " << duration_dec.count() << endl;
cout << endl;
cout << "TOTAL time (ms) : " << total_time << endl;
}