forked from AvivAbramovich/HElib_MatrixOperations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.h
More file actions
166 lines (128 loc) · 7.1 KB
/
Copy pathGrid.h
File metadata and controls
166 lines (128 loc) · 7.1 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
// Created by Aviv Abramovich on 4/08/15.
// Copyright (c) 2015 Aviv Abramovich. All rights reserved.
#include "Matrices.h"
//This class purpose is to take a single matrix and break it down to grid of smaller matrices
class PTMatrixGrid;
class PTVectorGrid;
class EncryptedMatrixGrid;
class EncryptedVectorGrid;
//This class represents a plain text matrix that divided to smaller plain text matrices (PTMatrix)
class PTMatrixGrid{
friend class PTMatrix;
private:
vector<vector<PTMatrix> > grid;
public:
//C'tors
PTMatrixGrid(const PTMatrix& matrix, const MatSize blockSize); //each PTMatrix save the MatrixRepresentation of the given PTMatrix
PTMatrixGrid(const vector<vector<PTMatrix> >& matrixGrid);
PTMatrixGrid(ifstream& file); //read from a file
//Encryption
EncryptedMatrixGrid encrypt(const EncryptedArray& ea, const FHEPubKey& publicKey) const;
EncryptedMatrixGrid encrypt(const FHEPubKey& publicKey) const;
//PTMatrixGrid& resize(MatSize origSize, MatSize blockSize); //good after decryption when the blocks get in size of nslots. NOTE: change all the matrices representation into RowsOrder
//Re-unite the grid to a one "big" matrix
PTMatrix reunion() const;
bool save(ofstream& file) const;
unsigned int getRows() const; //returns number of rows in the big matrix
unsigned int getColumns() const; //returns number of columns in the big matrix
const vector<PTMatrix>& operator[](unsigned int i) const; //returns the i-th vector of the grid. Not really useful, uses for inner operations
vector<PTMatrix>& operator[](unsigned int i);
unsigned int size() const; //grid.size()
MatSize getGridSize() const; //returns the size of the grid as a Matsize object
MatSize getMatrixSize() const; //returns the total size of the matrix represented by that grid
//operators
//NOTE: same as I wrote in PTMatrix, the idea of Homomorphic Encryption is to do these operations on the encrypted data. These operations provided to test the correctness of the encrypted data operations and statistics of how slower them compared to the regular operations
//Grids multiplication
PTMatrixGrid operator*(const PTMatrixGrid& other) const;
PTMatrixGrid operator*=(const PTMatrixGrid& other);
//Grids Addition
PTMatrixGrid operator+(const PTMatrixGrid& other) const;
PTMatrixGrid operator+=(const PTMatrixGrid& other);
//Grids Substraction
PTMatrixGrid operator-(const PTMatrixGrid& other) const;
PTMatrixGrid operator-=(const PTMatrixGrid& other);
//transpse
PTMatrixGrid transpose() const;
PTMatrixGrid operator>(const PTMatrixGrid& other) const;
PTMatrixGrid operator<(const PTMatrixGrid& other) const;
PTMatrixGrid operator>=(const PTMatrixGrid& other) const;
PTMatrixGrid operator<=(const PTMatrixGrid& other) const;
//Grids comparison
bool operator==(const PTMatrixGrid& other) const;
bool operator!=(const PTMatrixGrid& other) const;
bool concat(const PTMatrixGrid& other); //concat 2 matrices and returns true if concat is possible (and done). Uses for the Starssen's algorithm
bool push(const PTMatrixGrid& other); //return true if pushing is possible (and done)
};
//This class represents a plain text vector divided to 1D grid
class PTVectorGrid{
private:
vector<vector<long> > grid;
public:
//C'tors
PTVectorGrid(const vector<long>& vec, unsigned int size);
PTVectorGrid(const vector<vector<long> >& gridVec);
//Encryption
EncryptedVectorGrid encrypt(const EncryptedArray& ea, const FHEPubKey& publicKey) const;
EncryptedVectorGrid encrypt(const FHEPubKey& publicKey) const;
vector<long> reunion() const; //reunite to 1 "long" vector
vector<long> operator[](unsigned int i) const;
MatSize getVectorSize() const;
};
//This class represents an encrypted matrices grid. Useful to handle really big matrices, that HElib won't let you encrypt, and the operation may take very long time and be very noisy
class EncryptedMatrixGrid{
friend class EncryptedMatrix;
private:
vector<vector<EncryptedMatrix> > grid;
bool StrassenEnabled; //Is Strassen's matrices multiplication algorithm enabled bu the user for this grid
unsigned int StrassenLimit; //What is the size that below that stop using Strassen's algorithm and using regular multiplication
public:
EncryptedMatrixGrid(const vector<vector<EncryptedMatrix> >& matrix); //c'tor
//Decryption
PTMatrixGrid decrypt(const EncryptedArray& ea, const FHESecKey& secretKey) const;
PTMatrixGrid decrypt(const FHESecKey& secretKey) const;
//Operators:
//Matrix by vector (bith as grids) multiplication
EncryptedVectorGrid operator*(const EncryptedVectorGrid& vec) const;
//Grids multiplication
EncryptedMatrixGrid operator*(const EncryptedMatrixGrid& other) const;
EncryptedMatrixGrid operator*=(const EncryptedMatrixGrid& other);
//Grids Addition
EncryptedMatrixGrid operator+(const EncryptedMatrixGrid& other) const;
EncryptedMatrixGrid operator+=(const EncryptedMatrixGrid& other);
//Grids Substraction
EncryptedMatrixGrid operator-(const EncryptedMatrixGrid& other) const;
EncryptedMatrixGrid operator-=(const EncryptedMatrixGrid& other);
EncryptedMatrixGrid operator>(const EncryptedMatrixGrid& other) const;
EncryptedMatrixGrid operator<(const EncryptedMatrixGrid& other) const;
EncryptedMatrixGrid operator>=(const EncryptedMatrixGrid& other) const;
EncryptedMatrixGrid operator<=(const EncryptedMatrixGrid& other) const;
//transpose
EncryptedMatrixGrid transpose() const;
//Grids comparison. Again, it based on Ctxt:operator==
bool operator==(const EncryptedMatrixGrid& other) const;
bool operator!=(const EncryptedMatrixGrid& other) const;
//get a sub grid. usefully for strassen algorithm
EncryptedMatrixGrid getSubGrid(unsigned int firstRow, unsigned int firstColumn, unsigned int numRows, unsigned int numColumns) const;
//enable/disable Strassen
void enableStrassenMultiplication(unsigned int limit);
void disableStrassenMultiplication();
bool isStrassenEnabled() const;
unsigned int getStrassenLimit() const;
bool concat(const EncryptedMatrixGrid& other); //return true if concating is possible (and done)
bool push(const EncryptedMatrixGrid& other); //return true if pushing is possible (and done)
MatSize getGridSize() const;
unsigned int size() const; //same as grid[0].size();
vector<EncryptedMatrix> operator[](unsigned int i) const;
};
//This class represents a divided encrypted vector. Not so useful by itself, but can be helpful to multiply a big matrix by vector, by dividing them to grids
class EncryptedVectorGrid{
private:
vector<Ctxt> grid;
public:
EncryptedVectorGrid(vector<Ctxt>& vec); //C'tor
//Decryption
PTVectorGrid decrypt(const EncryptedArray& ea, const FHESecKey& secretKey) const;
PTVectorGrid decrypt(const FHESecKey& secretKey) const;
Ctxt operator[](unsigned int i) const;
unsigned int size() const;
};