-
Notifications
You must be signed in to change notification settings - Fork 23
/
libpir.hpp
229 lines (188 loc) · 7.3 KB
/
libpir.hpp
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
#ifndef DEF_LIBPIR
#define DEF_LIBPIR
#include <pir/shared_queue.hpp>
#include <pir/PIRParameters.hpp>
#include <pir/queryGen/PIRQueryGenerator_internal.hpp>
#include <pir/replyExtraction/PIRReplyExtraction_internal.hpp>
#include <pir/replyGenerator/PIRReplyGeneratorNFL_internal.hpp>
#include <crypto/NFLLWE.hpp>
#include <crypto/HomomorphicCryptoFactory_internal.hpp>
#include <crypto/HomomorphicCrypto.hpp>
#include "pir/dbhandlers/DBGenerator.hpp"
#include "pir/dbhandlers/DBDirectoryProcessor.hpp"
#include "pir/dbhandlers/DBVectorProcessor.hpp"
#include <stdint.h>
/**
* Class storing the database (or a chunk of it) after pre-processing
* Type returned by the funcion importData below
**/
class imported_database : public imported_database_t
{
public:
~imported_database();
};
/**
* HomomorphicCryptoFactory is used to create a generic cryptographic object (Ring-LWE, Paillier,
* mockup-cryptography, etc.). This API only exposes the Ring-LWE cryptosystem, but we still
* use the generic factory to get an instance of this cryptosystem in order to avoid code duplication.
**/
class HomomorphicCryptoFactory : public HomomorphicCryptoFactory_internal
{
public:
/** prints a list of the available crypto parameters
* (CryptoSystem:SecurityMax:PolyDegree:ModulusBitsize)
**/
static void printAllCryptoParams();
/** takes one of the parameters given by the previous
* method as a string and returns an instance of a cryptosystem for the given parameters
**/
static HomomorphicCrypto* getCryptoMethod(std::string cryptoParams);
};
/**
* PIRQueryGenerator is Client side, it initiates the PIR protocol by generating a query
* corresponding to the chosen element
**/
class PIRQueryGenerator : public PIRQueryGenerator_internal
{
public:
/**
* Class constructor
* Params:
* - PIRParameters& pirParameters_ : PIRParameters structure defining the cryptographic
* parameters, aggregation, recursion, and the shape of the database, see the main()
* function of apps/simplepir/simplePIR.cpp for detailed usage examples
* - NFLLWE& cryptoMethod_ : shared_pointer of the cryptographic instance
**/
PIRQueryGenerator(PIRParameters& pirParameters, HomomorphicCrypto& cryptoMethod_);
/**
* Generates asynchronously queries (a set of encryptions of 0 or 1).
* Params:
* - uint64_t _chosenElement : index of the element to be retrieved (indexes start at 0)
* Can be called on a separate thread.
**/
void generateQuery(uint64_t _chosenElement );
/**
* Function to get a pointer to a char* query element.
* Returns false when the queue is over (true otherwise) and waits when it's empty
* Can be called on a separate thread.
**/
bool popQuery(char** query);
/**
* Get the size in bytes of a query element
**/
uint64_t getQueryElementBytesize();
private:
int nbQueries;
};
/**
* PIRReplyGenerator is Server side
* It handles the request generated by the client and generates the reply
**/
class PIRReplyGenerator : public PIRReplyGeneratorNFL_internal
{
public:
/**
* Constructor of the class.
* Params :
* - vector <std::string>& database : reference of vector of the database elements
* - PIRParameters& param : reference to a PIRParameters structure (see the PIRQueryGenerator
* constructor help for more details).
* - DBHandler* db : abstract type for a database handler, at the moment it can be a DBGenerator
* that generates a fake database or a DBDirectoryProcessor that works on real files. See the
* main() function of apps/simplepir/simplePIR.cpp for examples.
**/
PIRReplyGenerator(PIRParameters& param, HomomorphicCrypto& cryptoMethod_, DBHandler *db);
/**
* Feeds the Server with the queries, this includes a pre-computation phase that speeds up
* reply generation (computation of newton coefficients, see the associated paper)
* Can be called on a separate thread but all the query elements must be pushed before
* starting the reply generation.
**/
void pushQuery(char* rawQuery);
/**
* Imports the database into a usable form. If the database is too large to fit in RAM
* (there is an expansion factor of 3 due to pre-processing) it can be cut by reading
* only for each element a chunk of size bytes_per_db_element starting at a given offset
* Can be called on a separate thread but the database must be imported before starting
* the reply generation.
**/
imported_database* importData(uint64_t offset, uint64_t bytes_per_db_element);
/**
* Prepares reply and start absoptions, returns number of chunks.
* Precondition: database is precomputed
*
* The PIRParameters given to the constructor are used and therefore the reply will
* be potentially generated with recursion and aggregation
* Can be called on a separate thread but the database must be imported and the query pushed
* before starting the reply generation.
**/
void generateReply(const imported_database* database);
/**
* Frees the queries allocated
**/
void freeQueries();
/**
* Gets a pointer to a char* reply element.
* Returns false when the queue is over (true otherwise) and waits when it's empty
* Can be called on a separate thread
**/
bool popReply(char** reply);
/**
* Getter for nbRepliesGenerated, the amount or reply elements generated
**/
uint64_t getnbRepliesGenerated();
/**
* Gets the size in bytes of a reply element
**/
uint64_t getReplyElementBytesize();
private:
uint64_t nbRepliesToHandle, nbRepliesGenerated, currentReply;
};
/**
* PIRReplyExtraction is Client side, it extracts the chosen element from the reply of the Server
**/
class PIRReplyExtraction : public PIRReplyExtraction_internal
{
public :
/**
* Class constructor
* Params:
* - PIRParameters& pirParameters : reference to a PIRParameters structure (see the
* PIRQueryGenerator constructor help for more details).
* - NFLLWE& cryptoMethod_ : shared_pointer of the cryptographic instance.
**/
PIRReplyExtraction(PIRParameters& pirParameters, HomomorphicCrypto& cryptoMethod);
/**
* Can be called on a separate thread
* Feed the client with the reply, may wait if the internal queue is full until
* enough replies are decrypted. If such thing happens extractReply should be called
* on a different thread
**/
void pushEncryptedReply(char* rawBytes);
/**
* Can be called on a separate thread
* extract/decrypt the result from the pushed replies, the plaintext results have to be popped.
* May block if the internal queue of plaintext results is full. If such a thing happens
* a thread that calls regularly popPlaintextResult must be used.
**/
void extractReply(uint64_t maxFileBytesize);
/**
* Can be called on a separate thread
* Function to get a pointer to a char* result element
* Returns false when the queue is over (true otherwise) and waits when its empty
**/
bool popPlaintextResult(char** result);
/**
* Get plaintext reply size
**/
uint64_t getPlaintextReplyBytesize();
/**
* Getter for nbPlaintextReplies, the number of plaintext replies that will be generated
* in the extraction processs
**/
uint64_t getnbPlaintextReplies(uint64_t maxFileBytesize);
private:
shared_queue<char*> clearChunks;
uint64_t nbPlaintextReplies;
};
#endif