-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern_matching.cpp
More file actions
432 lines (380 loc) · 12 KB
/
Copy pathpattern_matching.cpp
File metadata and controls
432 lines (380 loc) · 12 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/* Copyright (C) 2019 IBM Corp.
* This program is Licensed under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. See accompanying LICENSE file.
*/
#include <iostream>
#include <time.h>
#include <random>
#include <helib/helib.h>
#include <helib/debugging.h>
using namespace std;
using namespace NTL;
using namespace helib;
double intlog(unsigned int base, unsigned int input)
{
return floor(log2(input)/log2(base));
}
void convertToBaset(vector<long>& decomp, unsigned int input, unsigned int base, int nslots)
{
decomp.clear();
decomp.resize(nslots,0);
int power = static_cast<int>(intlog(base, input)) + 1;
if (power > nslots)
{
cout << "Input character is too big to be converted" << endl;
exit(1);
}
unsigned int rest = input;
unsigned int coeff;
int i = 0;
while(i < power)
{
coeff = rest % base;
decomp[i] = static_cast<long>(coeff);
rest = (rest - coeff) / base;
i++;
}
}
void rotate_and_add(helib::Ctxt& x, int nb_slots, long pat_len, const helib::EncryptedArray& ea)
{
if (nb_slots == 0)
{
std::vector<long> ptxt1(nb_slots,1);
ea.encrypt(x,x.getPubKey(),ptxt1);
}
helib::Ctxt y(x.getPubKey());
std::vector<long> ptxt0(nb_slots,0);
ea.encrypt(y,x.getPubKey(),ptxt0);
int rot_x = 1;
//rot_y = -2^K, where K is floor(log_2(pat_len))
int rot_y = pat_len;
helib::Ctxt temp(x.getPubKey());
//std::vector<long> decryptedx(nb_slots);
//std::vector<long> decryptedy(nb_slots);
int tmp_len = pat_len;
while (tmp_len > 1)
{
if (tmp_len % 2 == 0)
{
temp = x;
ea.rotate(temp, -rot_x);
x += temp;
rot_x *=2;
tmp_len = tmp_len/2;
}
else //if (nb_slots % 2 == 1)
{
rot_y -= rot_x;
temp = x;
ea.rotate(temp, -rot_y);
y += temp;
temp = x;
ea.rotate(temp, -rot_x);
x += temp;
rot_x *= 2;
tmp_len = (tmp_len - 1)/2;
}
}
x +=y;
}
void prob_eq_circuit(Ctxt& ctxt_res, Ctxt& ctxtx, helib::Ctxt& ctxty, int nslots, long pat_len, int p, int ord_p, bool wildcard, SecKey& secret_key, const helib::EncryptedArray& ea)
{
FHE_NTIMER_START(EqualityCircuit);
//first compute the difference slotwise
ctxt_res = ctxtx;
ctxt_res -= ctxty;
//multiply by a random polynomial
Ptxt<BGV> poly_r(ea.getContext());
poly_r.random();
ctxt_res.multByConstant(poly_r);
// additional multiplication when using wildcards
if(wildcard)
ctxt_res.multiplyBy(ctxty);
vector<ZZX> decrypted(nslots);
ea.decrypt(ctxt_res, secret_key, decrypted);
//rotate and add
rotate_and_add(ctxt_res, nslots, pat_len, ea);
mapTo01(ea, ctxt_res);
std::vector<long> ptxtones(nslots,1);
NTL::ZZX poly_ones;
ea.encode(poly_ones,ptxtones);
ctxt_res.negate();
ctxt_res.addConstant(poly_ones,1);
FHE_NTIMER_STOP(EqualityCircuit);
}
// main call examples
// ./pattern_matching p m q pattern_len experiment_runs wildcard_bool
// ./pattern_matching 7 21177 290 5 100 0
// ./pattern_matching 7 21177 320 7 100 1
// ./pattern_matching 17 18913 330 3 90 0
// ./pattern_matching 17 18913 360 3 90 1
int main(int argc, char *argv[]) {
// initialize the random generator
random_device rd;
mt19937 eng(rd());
uniform_int_distribution<unsigned int> distr_u;
uniform_int_distribution<int> distr_i;
// Wildcard. If true, then the wildcard character is encoded by 0.
bool wildcard;
if (atoi(argv[6]) == 0)
{
wildcard = false;
}
else if (atoi(argv[6]) == 1)
{
wildcard = true;
}
else
{
cout << "Wildcard parameter should be either 0 or 1" << endl;
return 1;
}
// Plaintext prime modulus
unsigned int p = atol(argv[1]);
// Cyclotomic polynomial - defines phi(m)
unsigned long m = atol(argv[2]);
// Hensel lifting (default = 1)
unsigned long r = 1;
// Number of ciphertext prime bits in the modulus chain = depth of the computation
unsigned long nb_primes = atol(argv[3]);
// Number of columns of Key-Switching matix (default = 2 or 3)
unsigned long c = 3;
std::cout << "Initialising context object..." << std::endl;
// Intialise context
Context context(m, p, r);
context.scale = 6;
// Modify the context, adding primes to the modulus chain
cout << "Building modulus chain..." << endl;
buildModChain(context, nb_primes, c);
// Print the context
context.zMStar.printout();
cout << endl;
//determine the order of p in (Z/mZ)*
long ord_p = context.zMStar.getOrdP();
// Print the security level
cout << "Q size: " << context.logOfProduct(context.ctxtPrimes)/log(2.0) << endl;
cout << "Q*P size: " << context.logOfProduct(context.fullPrimes())/log(2.0) << endl;
cout << "Security: " << context.securityLevel() << endl;
// Secret key management
cout << "Creating secret key..." << endl;
// Create a secret key associated with the context
SecKey secret_key(context);
// Generate the secret key
secret_key.GenSecKey();
cout << "Generating key-switching matrices..." << endl;
// Compute key-switching matrices that we need
add1DMatrices(secret_key);
addFrbMatrices(secret_key);
// Public key management
// Set the secret key (upcast: SecKey is a subclass of PubKey)
const PubKey& public_key = secret_key;
// Get the EncryptedArray of the context
const EncryptedArray& ea = *(context.ea);
// Get the number of slot (phi(m))
long nslots = ea.size();
cout << "Number of slots: " << nslots << endl;
cout << "Extension degree of a slot: " << ord_p << endl;
//pattern length
long pat_len = atol(argv[4]);
//pattern copies in one ciphertext
long pat_copies = nslots/pat_len;
//timers
setTimersOn();
//repeat experiments several times
int runs = atoi(argv[5]);
long min_capacity = 1000;
long capacity;
for (int run = 0; run < runs; run++)
{
vector<ZZX> expected_result(nslots);
vector<ZZX> decrypted(nslots);
// Create the plaintext polynomials for the text and for the pattern
vector<ZZX> pol_txt(nslots);
vector<ZZX> pol_pat(nslots);
// Generates copies of a random pattern of length len with characters in the set {0,..., 2^32-1} and encode each character into F_(p^{ord_p})
unsigned int input_pat_coef;
ZZX pol_pat_slot;
for (int i = 0; i < pat_len; i++)
{
if (wildcard && (i > 0))
{
// generate a wildcard character with probability 0.33
input_pat_coef = distr_u(eng) * static_cast<unsigned int>(distr_i(eng) % 3);
}
else
{
input_pat_coef = distr_u(eng);
}
vector<long> decomp_char;
convertToBaset(decomp_char, input_pat_coef, p, ord_p);
for (int j = 0; j < ord_p; j++)
{
SetCoeff(pol_pat_slot, j, decomp_char[j]);
}
for (int j = 0; j < pat_copies; j++)
{
pol_pat[i + j * pat_len] = pol_pat_slot;
}
}
/*
cout << "Input pattern: " << endl;
for (int i =0; i< nslots; i++)
{
printZZX(cout, pol_pat[i], ord_p);
cout << "Is zero?" << IsZero(pol_pat[i]);
cout << endl;
}
*/
// text generation
// indicates whether a snippet of the text will be a copy of the pattern
int is_equal;
ZZX pol_txt_slot;
unsigned int input_txt_coef;
int iChar = 0;
while (iChar < nslots)
{
//randomly decide whether the text substring is equal to the pattern
if (iChar <= nslots - pat_len)
{
is_equal = distr_i(eng) % 20;
is_equal = (is_equal > 0) ? 0 : 1;
}
else
{
is_equal = 0;
}
expected_result[iChar] = ZZX(INIT_MONO, 0, is_equal);
if (is_equal)
{
for (int i = 0; i < pat_len; i++)
{
if (IsZero(pol_pat[i]) && wildcard)
{
input_txt_coef = distr_u(eng)|1u;
vector<long> decomp_char;
convertToBaset(decomp_char, input_txt_coef,p, ord_p);
for (int j = 0; j < ord_p; j++)
{
SetCoeff(pol_txt_slot, j, decomp_char[j]);
}
pol_txt[iChar + i] = pol_txt_slot;
}
else
pol_txt[iChar + i] = pol_pat[i];
}
iChar += pat_len;
}
else
{
if(wildcard)
{
input_txt_coef = distr_u(eng)|1u;
}
else
{
input_txt_coef = distr_u(eng);
}
vector<long> decomp_char;
convertToBaset(decomp_char, input_txt_coef,p, ord_p);
for (int i = 0; i < ord_p; i++)
{
SetCoeff(pol_txt_slot, i, decomp_char[i]);
}
if (pol_txt_slot != pol_pat[0])
{
pol_txt[iChar] = pol_txt_slot;
iChar++;
}
}
}
/*
cout << "Input: " << endl;
for (int i =0; i< nslots; i++)
{
printZZX(cout, pol_pat[i], ord_p);
cout << '\t';
printZZX(cout, pol_txt[i], ord_p);
cout << endl;
}
cout << "Expected results:" << endl;
for(int i=0; i < expected_result.size(); i++)
{
printZZX(cout, expected_result[i], ord_p);
}
cout << endl;
*/
Ctxt ctxt_pat(public_key);
Ctxt ctxt_txt(public_key);
ea.encrypt(ctxt_pat, public_key, pol_pat);
ea.encrypt(ctxt_txt, public_key, pol_txt);
//results
Ctxt ctxt_res(ZeroCtxtLike, ctxt_txt);
//compute the equality
printf("Run %d started\n", run);
FHE_NTIMER_START(PatternMatching);
for (int iRot = 0; iRot < pat_len; iRot++)
{
//printf("Rotation %d\n", iRot);
Ctxt ctxt_tmp(public_key);
Ctxt ctxt_pat_rot(ctxt_pat);
if (iRot > 0)
{
ea.rotate(ctxt_pat_rot, iRot);
}
prob_eq_circuit(ctxt_tmp, ctxt_txt, ctxt_pat_rot, nslots, pat_len, p, ord_p, wildcard, secret_key, ea);
// select correct slots and zeroized the rest
vector<long> sel_vec(nslots,0);
//index of the first slot containing the equality function result
int iSlot = iRot;
while(iSlot < nslots)
{
if (iSlot <= nslots - pat_len)
{
sel_vec[iSlot] = 1;
iSlot += pat_len;
}
else
{
sel_vec[iSlot] = 0;
iSlot++;
}
}
//cout << "Selector slots: " << helib::vecToStr(sel_vec) << endl;
ZZX sel_poly;
ea.encode(sel_poly, sel_vec);
ctxt_tmp.multByConstant(sel_poly);
ctxt_res += ctxt_tmp;
}
printNamedTimer(cout, "EqualityCircuit");
FHE_NTIMER_STOP(PatternMatching);
printNamedTimer(cout, "PatternMatching");
// remove the line below if it gives bizarre results
ctxt_res.cleanUp();
capacity = ctxt_res.bitCapacity();
cout << "Final capacity: " << capacity << endl;
if (capacity < min_capacity)
min_capacity = capacity;
cout << "Min. capacity: " << min_capacity << endl;
cout << "Final size: " << ctxt_res.logOfPrimeSet()/log(2.0) << endl;
ea.decrypt(ctxt_res, secret_key, decrypted);
for(int i = 0; i < nslots; i++)
{
if (decrypted[i] != expected_result[i])
{
printf("Slot %d: ", i);
printZZX(cout, decrypted[i], ord_p);
cout << endl;
cout << "Failure" << endl;
return 1;
}
}
}
return 0;
}