forked from AvivAbramovich/HElib_MatrixOperations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.cpp
More file actions
578 lines (500 loc) · 23.5 KB
/
Copy pathGrid.cpp
File metadata and controls
578 lines (500 loc) · 23.5 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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
// Created by Aviv Abramovich on 4/08/15.
// Copyright (c) 2015 Aviv Abramovich. All rights reserved.
#include "Grid.h"
bool isPowerOf2(unsigned int num){
if( num == 1)
return true;
return num%2 ? false : isPowerOf2(num/2);
}
EncryptedMatrixGrid StrassenMultiplication(const EncryptedMatrixGrid& grid1, const EncryptedMatrixGrid& grid2)
/*Strassen algorithm for matrices multiplication. Work ONLY on square grids at size 2^n (power on 2). Complixety is O(n^2.8074) instead O(n^3)
Strassen algorithm:
grid1 is [a11, a12 ; a21, a22], grid2 is [b11, b12 ; b21, b22], result is C: [C11 , C12 ; C21, C22]
G1 = (a11 + a22)*(b11+b22)
G2 = (a21 + a22)*b11
G3 = a11*(b12 - b22)
G4 = a22*(b21 - b11)
G5 = (a11 + a12)*b22
G6 = (a21 - a11)*(b11 + b12)
G7 = (a12 - a22)*(b21 + b22)
C11 = G1 + G4 - G5 + G7
C12 = G3 + G5
C21 = G2 + G4
C22 = G1 - G2 +G3 + G6
*/
{
unsigned int len = grid1.size();
unsigned int quartLen = len/4;
EncryptedMatrixGrid a11 = grid1.getSubGrid(0,0, quartLen, quartLen), a12 = grid1.getSubGrid(quartLen,0, quartLen, quartLen),
a21 = grid1.getSubGrid(0,quartLen, quartLen, quartLen), a22 = grid1.getSubGrid(quartLen,quartLen, quartLen, quartLen),
b11 = grid2.getSubGrid(0,0, quartLen, quartLen), b12 = grid2.getSubGrid(quartLen,0, quartLen, quartLen),
b21 = grid2.getSubGrid(0,quartLen, quartLen, quartLen), b22 = grid2.getSubGrid(quartLen,quartLen, quartLen, quartLen);
EncryptedMatrixGrid G1 = (a11 + a22)*(b11+b22);
EncryptedMatrixGrid G2 = (a21 + a22)*b11;
EncryptedMatrixGrid G3 = a11*(b12 - b22);
EncryptedMatrixGrid G4 = a22*(b21 - b11);
EncryptedMatrixGrid G5 = (a11 + a12)*b22;
EncryptedMatrixGrid G6 = (a21 - a11)*(b11 + b12);
EncryptedMatrixGrid G7 = (a12 - a22)*(b21 + b22);
EncryptedMatrixGrid C11 = G1+G4-G5+G7;
EncryptedMatrixGrid C12 = G3+G5;
EncryptedMatrixGrid C21 = G2+G4;
EncryptedMatrixGrid C22 = G1-G2+G3+G6;
C11.concat(C12); C21.concat(C22);
C11.push(C21);
return C11;
}
/* ------------------------------- PTMatrixGrid --------------------------------------*/
vector<vector<PTMatrix> > init(const PTMatrix& matrix, const MatSize blockSize){
unsigned int numRows = 0, numCols = 0;
MatSize origSize = matrix.getMatrixSize();
numRows = origSize.rows/blockSize.rows; numCols = origSize.columns/blockSize.columns;
if(origSize.rows % blockSize.rows != 0){
cout << "The number of rows in the wanted size (" << blockSize.rows <<") is not divide the matrix size (" << origSize.rows <<")"<<endl;
numRows++; //for the "שארית"
}
if(origSize.columns % blockSize.columns != 0){
cout << "The number of columns in the wanted size (" << blockSize.columns <<") is not divide the matrix size (" << origSize.columns <<")"<<endl;
numCols++; //for the "שארית"
}
vector<vector<PTMatrix> > grid = vector<vector<PTMatrix> >(numRows);
for(unsigned int i=0; i< numRows; i++)
for(unsigned int j=0; j< numCols; j++)
grid[i].push_back(matrix.getSubMatrix(i*blockSize.rows, j*blockSize.columns, blockSize));
return grid;
}
PTMatrixGrid::PTMatrixGrid(const PTMatrix& matrix, const MatSize blockSize): grid(init(matrix, blockSize)) {}
PTMatrixGrid::PTMatrixGrid(const vector<vector<PTMatrix> >& matrixGrid) : grid(matrixGrid) {}
PTMatrixGrid::PTMatrixGrid(ifstream& file)
/*read a matrix from a file
the file format should be
num_of_rows num_of_columns block_num_rows block_num_columns
the matrix
for example:
3 4 3 2
1 2 3 4
5 6 7 8
9 8 7 6
is a 3 by 4 matrix, and the grid is the 2 3 by 2 matrices:
1 2
5 6
9 8
and
3 4
7 8
7 6
*/
{
if(file.is_open()){
string line;
int rows, cols, block_rows, block_cols;
getline(file, line);
rows = stoi(line.substr(0, line.find(' ')));
line = line.substr(line.find(' ')+1);
cols = stoi(line.substr(0, line.find(' ')));
line = line.substr(line.find(' ')+1);
block_rows = stoi(line.substr(0, line.find(' ')));
line = line.substr(line.find(' ')+1);
block_cols = stoi(line.substr(0, line.find(' ')));
MatSize block_size(block_rows, block_cols);
vector<vector<long> > bigMatrix(rows, vector<long>(cols));
for(int i=0; i < rows; i++){
getline(file, line);
for(int j=0; j < cols; j++){
bigMatrix[i][j] = stoi(line.substr(0, line.find(' ')));
line = line.substr(line.find(' ')+1);
}
}
PTMatrix mat(bigMatrix, false); //conert to PTMatrix, when the matrix transformed to diagonal order
grid = init(mat, block_size);
}
else
cout << "Unable to open file";
}
EncryptedMatrixGrid PTMatrixGrid::encrypt(const EncryptedArray& ea, const FHEPubKey& publicKey) const{
vector<vector<EncryptedMatrix> > mat(grid.size());
for(unsigned int i=0; i < grid.size(); i++)
for(unsigned int j=0; j< grid[0].size(); j++)
mat[i].push_back(grid[i][j].encrypt(ea, publicKey));
return EncryptedMatrixGrid(mat);
}
EncryptedMatrixGrid PTMatrixGrid::encrypt(const FHEPubKey& publicKey) const{
EncryptedArray ea(publicKey.getContext());
return encrypt(ea, publicKey);
}
PTMatrix PTMatrixGrid::reunion() const {
vector<vector<long> >mat(getRows());
unsigned int rowsInBlock = grid[0][0].getRows();
for(unsigned int i=0, sz1 = grid.size(); i< sz1; i++)
for(unsigned int j=0, sz2 = grid[i].size(); j< sz2; j++)
for(unsigned int k=0, sz3 = grid[i][j].getRows(); k< sz3; k++)
for(unsigned int l=0, sz4 = grid[i][j].getColumns(); l < sz4; l++)
mat[i*rowsInBlock+k].push_back((*this)[i][j](k,l));
return PTMatrix(mat, false);
}
bool PTMatrixGrid::save(ofstream& file) const {
PTMatrixGrid copy = (*this);
PTMatrix mat = copy.reunion();
if (file.is_open())
{
MatSize matSZ = getMatrixSize(), gridSZ = getGridSize();
unsigned int rows = matSZ.rows, cols = matSZ.columns, block_rows = gridSZ.rows, block_cols = gridSZ.columns;
file << rows << " " << cols << " " << block_rows << " " << block_cols << "\n";
for(unsigned int i=0; i < rows; i++){
for(unsigned int j=0; j < cols; j++)
file << mat(i,j) << " ";
file << "\n";
}
file.close();
}
else{
cout << "Unable to open file";
return false;
}
return true;
}
//in getRows and getColumns, multiply the number of blocks in rows/columns with the size of the first block,
//except the last block, that calculated seperatly because it may be smaller than the other
unsigned int PTMatrixGrid::getRows() const{ return grid[0][0].getRows()*(grid.size()-1)+grid[grid.size()-1][0].getRows(); }
unsigned int PTMatrixGrid::getColumns() const{ return grid[0][0].getColumns()*(grid[0].size()-1)+grid[0][grid.size()-1].getColumns(); }
const vector<PTMatrix>& PTMatrixGrid::operator[](unsigned int i) const { return grid[i]; }
vector<PTMatrix>& PTMatrixGrid::operator[](unsigned int i) { return grid[i]; }
MatSize PTMatrixGrid::getGridSize() const { return MatSize(grid.size(), grid[0].size()); }
MatSize PTMatrixGrid::getMatrixSize() const { return MatSize(getRows(), getColumns()); }
unsigned int PTMatrixGrid::size() const { return grid.size(); }
bool PTMatrixGrid::concat(const PTMatrixGrid& other){
if(grid.size() != other.grid.size() || grid[0][0].getMatrixSize() != other[0][0].getMatrixSize())
return false;
for(unsigned int i=0; i < grid.size(); i++)
grid[i].insert(grid[i].end(), other[i].begin(), other[i].end());
return true;
}
bool PTMatrixGrid::push(const PTMatrixGrid& other){
if(grid[0].size() != other[0].size() || grid[0][0].getMatrixSize() != other[0][0].getMatrixSize())
return false;
for(unsigned int i=0; i < other.grid.size(); i++)
grid.push_back(other[i]);
return true;
}
//operators
//Grids multiplication
PTMatrixGrid PTMatrixGrid::operator*(const PTMatrixGrid& other) const {
if(getGridSize().canMultiply(other.getGridSize())) //check sizes
throw MatricesSizesNotMatch(getGridSize(), other.getGridSize());
vector<vector<PTMatrix> > ret(size());
for(unsigned int i=0, sz1= size(); i < sz1; i++)
for(unsigned int j=0, sz2= other.size(); j < sz2; j++){
PTMatrix temp = grid[i][0]*other[0][j];
for(unsigned int k=1, sz3 = grid[i].size(); k < sz3; k++)
temp += grid[i][k]*other[k][j];
ret[i].push_back(temp);
}
return PTMatrixGrid(ret);
}
PTMatrixGrid PTMatrixGrid::operator*=(const PTMatrixGrid& other) { return (*this) = (*this)*other;}
//Grids Addition
PTMatrixGrid PTMatrixGrid::operator+(const PTMatrixGrid& other) const {
if(getGridSize() != other.getGridSize()) //check sizes
throw MatricesSizesNotMatch(getGridSize(), other.getGridSize());
PTMatrixGrid ret = *this;
for(unsigned int i=0, sz1 = grid.size(); i < sz1; i++)
for(unsigned int j=0, sz2 = grid[i].size(); j < sz2; j++)
ret[i][j] += other[i][j];
return ret;
}
PTMatrixGrid PTMatrixGrid::operator+=(const PTMatrixGrid& other) { return (*this) = (*this)+other;}
//Grids Substraction
PTMatrixGrid PTMatrixGrid::operator-(const PTMatrixGrid& other) const {
if(getGridSize() != other.getGridSize()) //check sizes
throw MatricesSizesNotMatch(getGridSize(), other.getGridSize());
PTMatrixGrid ret = *this;
for(unsigned int i=0, sz1 = grid.size(); i < sz1; i++)
for(unsigned int j=0, sz2 = grid[i].size(); j < sz2; j++)
ret[i][j] -= other[i][j];
return ret;
}
PTMatrixGrid PTMatrixGrid::operator-=(const PTMatrixGrid& other) { return (*this) = (*this)-other;}
//trnapose
PTMatrixGrid PTMatrixGrid::transpose() const{
vector<vector<PTMatrix>> transposedGrid(this->grid[0].size());
for(int i = 0; i < this->grid[0].size(); i++)
for(int j = 0; j < this->grid.size(); j++)
transposedGrid[i][j] = this->grid[j][i];
return PTMatrixGrid(transposedGrid);
}
PTMatrixGrid PTMatrixGrid::operator>(const PTMatrixGrid& other) const {
if(getGridSize() != other.getGridSize()) //check sizes
throw MatricesSizesNotMatch(getGridSize(), other.getGridSize());
vector<vector<PTMatrix> > ret(size());
for(unsigned int i=0, sz1 = size(); i < sz1; i++)
for(unsigned int j=0, sz2 = grid[0].size(); j < sz2; j++)
ret[i].push_back(grid[i][j] > other[i][j]);
return PTMatrixGrid(ret);
}
PTMatrixGrid PTMatrixGrid::operator<(const PTMatrixGrid& other) const {
if(getGridSize() != other.getGridSize()) //check sizes
throw MatricesSizesNotMatch(getGridSize(), other.getGridSize());
vector<vector<PTMatrix> > ret(size());
for(unsigned int i=0, sz1 = size(); i < sz1; i++)
for(unsigned int j=0, sz2 = grid[0].size(); j < sz2; j++)
ret[i].push_back(grid[i][j] < other[i][j]);
return PTMatrixGrid(ret);
}
PTMatrixGrid PTMatrixGrid::operator>=(const PTMatrixGrid& other) const {
if(getGridSize() != other.getGridSize()) //check sizes
throw MatricesSizesNotMatch(getGridSize(), other.getGridSize());
vector<vector<PTMatrix> > ret(size());
for(unsigned int i=0, sz1 = size(); i < sz1; i++)
for(unsigned int j=0, sz2 = grid[0].size(); j < sz2; j++)
ret[i].push_back(grid[i][j] >= other[i][j]);
return PTMatrixGrid(ret);
}
PTMatrixGrid PTMatrixGrid::operator<=(const PTMatrixGrid& other) const {
if(getGridSize() != other.getGridSize()) //check sizes
throw MatricesSizesNotMatch(getGridSize(), other.getGridSize());
vector<vector<PTMatrix> > ret(size());
for(unsigned int i=0, sz1 = size(); i < sz1; i++)
for(unsigned int j=0, sz2 = grid[0].size(); j < sz2; j++)
ret[i].push_back(grid[i][j] <= other[i][j]);
return PTMatrixGrid(ret);
}
//Grids comparison
bool PTMatrixGrid::operator==(const PTMatrixGrid& other) const{
if(getGridSize() != other.getGridSize())
return false;
for(unsigned int i=0, sz1 = size(); i < sz1; i++)
for(unsigned int j=0, sz2 = grid[i].size(); j < sz2; j++){
if(grid[i][j] != other[i][j])
return false;
}
return true;
}
bool PTMatrixGrid::operator!=(const PTMatrixGrid& other) const { return !((*this)==other); }
/* ------------------------------- PTVectorGrid --------------------------------------*/
PTVectorGrid::PTVectorGrid(const vector<long>& vec, unsigned int size){
grid = vector<vector<long> >(vec.size()/size, vector<long>(size));
for(unsigned int i=0; i < grid.size(); i++)
for(unsigned int j=0; j< grid[i].size(); j++)
grid[i][j] = vec[i*size+j];
if(vec.size() % size !=0){
cout << "The wnated size of each sub-vector (" << size << ") is not divide the vector length (" << vec.size() << ")" << endl;
//adding the ״שארית״
unsigned int totalSize = size*grid.size();
vector<long> sheerit;
for(unsigned int i=totalSize; i< vec.size(); i++)
sheerit.push_back(vec[i]);
grid.push_back(sheerit);
}
}
PTVectorGrid::PTVectorGrid(const vector<vector<long> >& gridVec): grid(gridVec) {}
EncryptedVectorGrid PTVectorGrid::encrypt(const EncryptedArray& ea, const FHEPubKey& publicKey) const{
vector<Ctxt> enc;
unsigned int nslots = ea.size();
for(unsigned int i=0; i< grid.size(); i++){
vector<long> temp = grid[i];
Ctxt encTemp(publicKey);
if(temp.size() < nslots)
temp.resize(nslots, 0);
ea.encrypt(encTemp, publicKey, temp);
enc.push_back(encTemp);
}
return EncryptedVectorGrid(enc);
}
EncryptedVectorGrid PTVectorGrid::encrypt(const FHEPubKey& publicKey) const{
EncryptedArray ea(publicKey.getContext());
return encrypt(ea, publicKey);
}
vector<long> PTVectorGrid::reunion() const {
vector<long> result;
for(unsigned int i=0; i< grid.size(); i++)
for(unsigned int j=0; j < grid[i].size(); j++)
result.push_back(grid[i][j]);
return result;
}
vector<long> PTVectorGrid::operator[](unsigned int i) const { return grid[i]; }
MatSize PTVectorGrid::getVectorSize() const {
unsigned int len = 0, sz = grid.size();
for(unsigned int i=0; i < sz; i++)
len += grid[i].size();
return MatSize(len, 1);
}
/* ------------------------------- EncryptedMatrixGrid -------------------------------*/
EncryptedMatrixGrid::EncryptedMatrixGrid(const vector<vector<EncryptedMatrix> >& matrix): grid(matrix), StrassenEnabled(false), StrassenLimit(0) {}
PTMatrixGrid EncryptedMatrixGrid::decrypt(const EncryptedArray& ea, const FHESecKey& secretKey) const{
vector<vector<PTMatrix> > result(grid.size());
for(unsigned int i=0; i< grid.size(); i++)
for(unsigned int j=0; j < grid[i].size(); j++)
result[i].push_back(grid[i][j].decrypt(ea, secretKey));
return PTMatrixGrid(result);
}
PTMatrixGrid EncryptedMatrixGrid::decrypt(const FHESecKey& secretKey) const{
EncryptedArray ea(secretKey.getContext());
return decrypt(ea, secretKey);
}
EncryptedVectorGrid EncryptedMatrixGrid::operator*(const EncryptedVectorGrid& vec) const{
if(!getGridSize().canMultiply(vec.size())){
cout << "ERROR! Sizes not acceptable! return the Encrypted vector grid" << endl;
return vec;
}
vector<Ctxt> result;
if(grid[0].size() != vec.size()){
cout << "ERROR! matrix and vector sizes not acceptable!" << endl;
return EncryptedVectorGrid(result); //return an empty vector
}
for(unsigned int i=0; i< grid.size(); i++){
Ctxt temp(vec[0].getPubKey());
for(unsigned int j=0; j< grid[i].size(); j++)
temp += grid[i][j]*vec[j];
result.push_back(temp);
}
return EncryptedVectorGrid(result);
}
EncryptedMatrixGrid EncryptedMatrixGrid::operator*(const EncryptedMatrixGrid& other) const{
//Check if sizes are ok
if(!getGridSize().canMultiply(other.getGridSize()) || !grid[0][0].getMatrixSize().canMultiply(other[0][0].getMatrixSize())){
cout << "Grids sizes not compatible. return the first grid" << endl;
return *this;
}
//Check if can use Strassen algorithm
if(StrassenEnabled && grid.size() == grid[0].size() && other.size() == other[0].size() && isPowerOf2(grid.size()) && grid.size() > StrassenLimit)
return StrassenMultiplication(*this, other);
vector<vector<EncryptedMatrix> > result(grid.size());
for(unsigned int i=0; i< size(); i++)
for(unsigned int j=0; j < other[0].size(); j++){
EncryptedMatrix temp = grid[i][0]*other[0][j];
for(unsigned int k=1; k < grid[i].size(); k++){
temp += grid[i][k]*other[k][j];
}
result[i].push_back(temp);
}
return EncryptedMatrixGrid(result);
}
EncryptedMatrixGrid EncryptedMatrixGrid::operator*=(const EncryptedMatrixGrid& other){ return ((*this) = (*this)*other); }
EncryptedMatrixGrid EncryptedMatrixGrid::operator+(const EncryptedMatrixGrid& other) const{
if(getGridSize()!=other.getGridSize() || grid[0][0].getMatrixSize()!=other[0][0].getMatrixSize()){
cout << "Grids sizes not compatible. return the first grid" << endl;
return *this;
}
vector<vector<EncryptedMatrix> > result(grid.size());
for(unsigned int i=0; i< size(); i++)
for(unsigned int j=0; j < other[0].size(); j++)
result[i].push_back(grid[i][j]+other[i][j]);
return EncryptedMatrixGrid(result);
}
EncryptedMatrixGrid EncryptedMatrixGrid::operator+=(const EncryptedMatrixGrid& other){ return ((*this) = (*this)+other); }
EncryptedMatrixGrid EncryptedMatrixGrid::operator-(const EncryptedMatrixGrid& other) const{
if(getGridSize()!=other.getGridSize() || grid[0][0].getMatrixSize()!=other[0][0].getMatrixSize()){
cout << "Grids sizes not compatible. return the first grid" << endl;
return *this;
}
vector<vector<EncryptedMatrix> > result(grid.size());
for(unsigned int i=0; i< size(); i++)
for(unsigned int j=0; j < other[0].size(); j++)
result[i].push_back(grid[i][j]-other[i][j]);
return EncryptedMatrixGrid(result);
}
EncryptedMatrixGrid EncryptedMatrixGrid::operator-=(const EncryptedMatrixGrid& other){ return ((*this) = (*this)-other); }
EncryptedMatrixGrid EncryptedMatrixGrid::transpose() const{
vector<vector<EncryptedMatrix>> transposedGrid(this->grid[0].size());
for(int i = 0; i < this->grid[0].size(); i++)
for(int j = 0; j < this->grid.size(); j++)
transposedGrid[i][j] = this->grid[j][i];
return EncryptedMatrixGrid(transposedGrid);
}
EncryptedMatrixGrid EncryptedMatrixGrid::operator>(const EncryptedMatrixGrid& other) const {
if(getGridSize() != other.getGridSize())
throw MatricesSizesNotMatch(getGridSize(), other.getGridSize());
vector<vector<EncryptedMatrix> > ret(size());
for(unsigned int i=0, sz1 = size(); i < sz1; i++)
for(unsigned int j=0, sz2 = grid[i].size(); j < sz2; j++)
ret[i].push_back(grid[i][j] > other[i][j]);
return EncryptedMatrixGrid(ret);
}
EncryptedMatrixGrid EncryptedMatrixGrid::operator<(const EncryptedMatrixGrid& other) const {
if(getGridSize() != other.getGridSize())
throw MatricesSizesNotMatch(getGridSize(), other.getGridSize());
vector<vector<EncryptedMatrix> > ret(size());
for(unsigned int i=0, sz1 = size(); i < sz1; i++)
for(unsigned int j=0, sz2 = grid[i].size(); j < sz2; j++)
ret[i].push_back(grid[i][j] < other[i][j]);
return EncryptedMatrixGrid(ret);
}
EncryptedMatrixGrid EncryptedMatrixGrid::operator>=(const EncryptedMatrixGrid& other) const {
if(getGridSize() != other.getGridSize())
throw MatricesSizesNotMatch(getGridSize(), other.getGridSize());
vector<vector<EncryptedMatrix> > ret(size());
for(unsigned int i=0, sz1 = size(); i < sz1; i++)
for(unsigned int j=0, sz2 = grid[i].size(); j < sz2; j++)
ret[i].push_back(grid[i][j] >= other[i][j]);
return EncryptedMatrixGrid(ret);
}
EncryptedMatrixGrid EncryptedMatrixGrid::operator<=(const EncryptedMatrixGrid& other) const {
if(getGridSize() != other.getGridSize())
throw MatricesSizesNotMatch(getGridSize(), other.getGridSize());
vector<vector<EncryptedMatrix> > ret(size());
for(unsigned int i=0, sz1 = size(); i < sz1; i++)
for(unsigned int j=0, sz2 = grid[i].size(); j < sz2; j++)
ret[i].push_back(grid[i][j] <= other[i][j]);
return EncryptedMatrixGrid(ret);
}
bool EncryptedMatrixGrid::operator==(const EncryptedMatrixGrid& other) const {
if(getGridSize() != other.getGridSize() || grid[0][0].getMatrixSize() != other[0][0].getMatrixSize())
return false;
MatSize sz = getGridSize();
for(unsigned int i=0; i < sz.rows; i++)
for(unsigned int j=0; j < sz.columns; j++)
if(grid[i][j] != other[i][j])
return false;
return true;
}
bool EncryptedMatrixGrid::operator!=(const EncryptedMatrixGrid& other) const { return !(*this == other); }
MatSize EncryptedMatrixGrid::getGridSize() const { return MatSize(grid.size(), grid[0].size()); }
vector<EncryptedMatrix> EncryptedMatrixGrid::operator[](unsigned int i) const { return grid[i]; }
EncryptedMatrixGrid EncryptedMatrixGrid::getSubGrid(unsigned int firstRow, unsigned int firstColumn, unsigned int numRows, unsigned int numColumns) const{
unsigned int len = numRows > grid.size() - firstRow ? grid.size() - firstRow : numRows;
vector<vector<EncryptedMatrix> > res(len);
for(unsigned int i=0; i < numRows && i+firstRow < grid.size(); i++)
for(unsigned int j=0 ; j < numColumns && j+firstColumn < grid[i].size(); j++)
res[i].push_back(grid[i+firstRow][j+firstColumn]);
return EncryptedMatrixGrid(res);
}
bool EncryptedMatrixGrid::concat(const EncryptedMatrixGrid& other){
if(grid.size() != other.size() || grid[0][0].getMatrixSize() != other[0][0].getMatrixSize())
return false;
for(unsigned int i=0; i < grid.size(); i++)
grid[i].insert(grid[i].end(), other[i].begin(), other[i].end());
return true;
}
bool EncryptedMatrixGrid::push(const EncryptedMatrixGrid& other){
if(grid[0].size() != other[0].size() || grid[0][0].getMatrixSize() != other[0][0].getMatrixSize())
return false;
for(unsigned int i=0; i < other.size(); i++)
grid.push_back(other[i]);
return true;
}
unsigned int EncryptedMatrixGrid::size() const { return grid.size(); }
//enable/disable Strassen
void EncryptedMatrixGrid::enableStrassenMultiplication(unsigned int limit){
StrassenEnabled = true;
StrassenLimit = limit;
}
void EncryptedMatrixGrid::disableStrassenMultiplication(){ StrassenEnabled = false; }
bool EncryptedMatrixGrid::isStrassenEnabled() const { return StrassenEnabled; }
unsigned int EncryptedMatrixGrid::getStrassenLimit() const { return StrassenLimit; }
/* ------------------------------- EncryptedVectorGrid -------------------------------*/
EncryptedVectorGrid::EncryptedVectorGrid(vector<Ctxt>& vec): grid(vec) {}
PTVectorGrid EncryptedVectorGrid::decrypt(const EncryptedArray& ea, const FHESecKey& secretKey) const{
vector<vector<long> > vec;
for(unsigned int i=0; i< grid.size(); i++){
vector<long> temp;
ea.decrypt(grid[i], secretKey, temp);
vec.push_back(temp);
}
return PTVectorGrid(vec);
}
PTVectorGrid EncryptedVectorGrid::decrypt(const FHESecKey& secretKey) const{
EncryptedArray ea(secretKey.getContext());
return decrypt(ea, secretKey);
}
unsigned int EncryptedVectorGrid::size() const{ return grid.size(); }
Ctxt EncryptedVectorGrid::operator[](unsigned int i) const { return grid[i]; }