-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAPI.h
365 lines (326 loc) · 12.5 KB
/
API.h
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
#ifndef _HE_API
#define _HE_API
#include <gmpxx.h>
/**
* /!\ WARNING /!\
*
* Constant parameters need to be defined in a params.h file
*
* PLAINTEXT_MODULUS (unsigned long)
* PLAINTEXT_MODULUS_MPZ (string)
* SECURITY_LEVEL (unsigned long)
* MULTIPLICATIVE_DEPTH (unsigned long)
*
* API for fixed-point arithmetic is conditionally included (see below).
*
*/
namespace HE {
extern mpz_class plaintext_modulus_mpz;
extern unsigned long plaintext_modulus;
extern unsigned long security_level;
extern unsigned long multiplicative_depth;
extern bool supports_bit_encryption;
extern bool supports_unsigned_encryption;
extern bool supports_mpz_encryption;
extern bool supports_polynomial_encryption;
extern bool supports_vector_encryption;
size_t number_of_polynomial_slots(void* pk);
size_t number_of_vector_slots(void* pk);
/**
* Init function (uses values in params.h)
*
* @param parameters reference to the pointer to the parameters
*
* @return error code
*/
int init(void** parameters);
/**
* Key Generation
*
* @param parameters pointer to the parameters
* @param sk reference to the pointer to the secret key
* @param pk reference to the pointer to the public key
* @param evk reference to the pointer to the evaluation key
*
* @return error code
*/
int keygen(void* parameters, void** sk, void** pk, void** evk);
/**
* Encrypt in integer (usigned or mpz_class) using the public key
*
* @param pk pointer to the public key
* @param ciphertext reference to the pointer to the ciphertext
* @param message integer message (unsigned long or mpz_class)
* @param level (optional) level to encrypt
*
* @return error code
*/
int encryptInteger(void* pk, void** ciphertext, unsigned long message,
unsigned long level = 1);
int encryptInteger(void* pk, void** ciphertext, mpz_class const& message,
unsigned long level = 1);
/**
* Encrypt in integer (usigned or mpz_class) using the secret key
*
* @param sk pointer to the secret key
* @param pk pointer to the public key
* @param ciphertext reference to the pointer to the ciphertext
* @param message integer message (unsigned long or mpz_class)
* @param level (optional) level to encrypt
*
* @return error code
*/
int encryptIntegerWithSK(void* sk, void* pk, void** ciphertext,
unsigned long message, unsigned long level = 1);
int encryptIntegerWithSK(void* sk, void* pk, void** ciphertext,
mpz_class const& message, unsigned long level = 1);
/**
* Encrypt a polynomial (of usigned or mpz_class) using the public key
*
* @param pk pointer to the public key
* @param ciphertext reference to the pointer to the ciphertext
* @param message_p pointer to the polynomial coefficients (of type unsigned
*long or mpz_class)
* @param size number of coefficients in the polynomial
* @param level (optional) level to encrypt
*
* @return error code
*/
int encryptPolynomial(void* pk, void** ciphertext, unsigned long* message_p,
size_t size, unsigned long level = 1);
int encryptPolynomial(void* pk, void** ciphertext, mpz_class* message_p,
size_t size, unsigned long level = 1);
/**
* Encrypt a polynomial (of usigned or mpz_class) using the secret key
*
* @param sk pointer to the secret key
* @param pk pointer to the public key
* @param ciphertext reference to the pointer to the ciphertext
* @param message_p pointer to the polynomial coefficients (of type unsigned
*long or mpz_class)
* @param size number of coefficients in the polynomial
* @param level (optional) level to encrypt
*
* @return error code
*/
int encryptPolynomialWithSK(void* sk, void* pk, void** ciphertext,
unsigned long* message_p, size_t size,
unsigned long level = 1);
int encryptPolynomialWithSK(void* sk, void* pk, void** ciphertext,
mpz_class* message_p, size_t size,
unsigned long level = 1);
/**
* Encrypt a vector (of usigned or mpz_class) using the public key [BATCHING]
*
* @param pk pointer to the public key
* @param ciphertext reference to the pointer to the ciphertext
* @param message_v pointer to the vector coefficients (of type unsigned long
*or mpz_class)
* @param size number of coefficients
* @param level (optional) level to encrypt
*
* @return error code
*/
int encryptVector(void* pk, void** ciphertext, unsigned long* message_v,
size_t size, unsigned long level = 1);
int encryptVector(void* pk, void** ciphertext, mpz_class* message_v,
size_t size, unsigned long level = 1);
/**
* Encrypt a vector (of usigned or mpz_class) using the secret key [BATCHING]
*
* @param sk pointer to the secret key
* @param pk pointer to the public key
* @param ciphertext reference to the pointer to the ciphertext
* @param message_v pointer to the vector coefficients (of type unsigned long
*or mpz_class)
* @param size number of coefficients
* @param level (optional) level to encrypt
*
* @return error code
*/
int encryptVectorWithSK(void* sk, void* pk, void** ciphertext,
unsigned long* message_v, size_t size,
unsigned long level = 1);
int encryptVectorWithSK(void* sk, void* pk, void** ciphertext,
mpz_class* message_v, size_t size,
unsigned long level = 1);
/**
* Decrypt a ciphertext to an integer (unsigned long of mpz_class)
*
* @param sk pointer to the secret key
* @param pk pointer to the public key
* @param ciphertext pointer to the ciphertext
* @param message reference to the message
* @param level (optional) level to decrypt
*
* @return error code
*/
int decryptInteger(void* sk, void* pk, void* ciphertext,
unsigned long* message, unsigned long level = 1);
int decryptInteger(void* sk, void* pk, void* ciphertext, mpz_class* message,
unsigned long level = 1);
/**
* Decrypt a ciphertext to a polynomial (of unsigned long of mpz_class)
*
* @param sk pointer to the secret key
* @param pk pointer to the public key
* @param ciphertext pointer to the ciphertext
* @param message_p reference to the pointer to the polynomial coefficients
* @param size number of coefficients in the polynomial
* @param level (optional) level to decrypt
*
* @return error code
*/
int decryptPolynomial(void* sk, void* pk, void* ciphertext,
unsigned long** message_p, size_t size,
unsigned long level = 1);
int decryptPolynomial(void* sk, void* pk, void* ciphertext,
mpz_class** message_p, size_t size,
unsigned long level = 1);
/**
* Decrypt a ciphertext to a vector (of unsigned long of mpz_class) [BATCHING]
*
* @param sk pointer to the secret key
* @param pk pointer to the public key
* @param ciphertext pointer to the ciphertext
* @param message_p reference to the pointer to the vector
* @param size number of coefficients
* @param level (optional) level to decrypt
*
* @return error code
*/
int decryptVector(void* sk, void* pk, void* ciphertext,
unsigned long** message_v, size_t size,
unsigned long level = 1);
int decryptVector(void* sk, void* pk, void* ciphertext,
mpz_class** message_v, size_t size,
unsigned long level = 1);
/**
* Homomorphic addition of two ciphertexts
*
* @param pk pointer to the public key
* @param evk pointer to the evaluation key
* @param output reference to the pointer to the output ciphertext
* @param input1 pointer to the input1 ciphertext
* @param input2 pointer to the input2 ciphertext
* @param level1 (optional) level of the first input
* @param level2 (optional) level of the second input
*
* @return error code
*/
int add(void* pk, void* evk, void** output, void* input1, void* input2,
unsigned long level1 = 1, unsigned long level2 = 1);
/**
* Homomorphic multiplication of two ciphertexts
*
* @param pk pointer to the public key
* @param evk pointer to the evaluation key
* @param output reference to the pointer to the output ciphertext
* @param input1 pointer to the input1 ciphertext
* @param input2 pointer to the input2 ciphertext
* @param level1 (optional) level of the first input
* @param level2 (optional) level of the second input
*
* @return error code
*/
int mul(void* pk, void* evk, void** output, void* input1, void* input2,
unsigned long level1 = 1, unsigned long level2 = 1);
/**
* Homomorphic multiplication by a constant of a ciphertext
*
* @param pk pointer to the public key
* @param evk pointer to the evaluation key
* @param output reference to the pointer to the output ciphertext
* @param input pointer to the input ciphertext
* @param constant constant (unsigned long or mpz_class)
* @param level (optional) level of the input
*
* @return error code
*/
int mulByConstant(void* pk, void* evk, void** output, void* input,
unsigned long constant, unsigned long level = 1);
int mulByConstant(void* pk, void* evk, void** output, void* input,
mpz_class const& constant, unsigned long level = 1);
/**
* Deallocate the memory occupied by a ciphertext
*
* @param pk pointer to the public key
* @param ciphertext pointer to the ciphertext to be deleted
*
* @return error code
*/
int freeup_ciphertext(void* pk, void* ciphertext);
/**
* Deallocate the memory occupied by parameters generated during HE::init() and HE::keygen()
*
* @param parameters pointer to the parameters
* @param sk pointer to the secret key
* @param pk pointer to the public key
* @param evk pointer to the evaluation key
*
* @return error code
*/
int freeup_keys(void* parameters, void* sk, void* pk, void* evk);
/**
* Serialize functions (back and forth)
*
* @param file name
* @param element to serialize or reference to element to deserialize
*
* @return error code
*/
int serialize_parameters (const char* filename, void* parameters);
int serialize_sk (const char* filename, void* sk);
int serialize_evk (const char* filename, void* evk);
int serialize_pk (const char* filename, void* pk);
int serialize_ciphertext (const char* filename, void* ciphertext);
int deserialize_parameters(const char* filename, void** parameters);
int deserialize_sk (const char* filename, void** sk);
int deserialize_evk (const char* filename, void** evk);
int deserialize_pk (const char* filename, void** pk);
int deserialize_ciphertext(const char* filename, void** ciphertext);
#ifdef FXPT
extern bool supports_fixedpt_encryption;
/**
* Encrypt a fixed-point number (double) using the public key
*
* @param pk pointer to the public key
* @param ciphertext reference to the pointer to the ciphertext
* @param message fixed-point number message (double)
* @param level (optional) level to encrypt
*
* @return error code
*/
int encryptFixedpt(void* pk, void** ciphertext, double message,
unsigned long level = 1);
/**
* Encrypt a fixed-point number (double) using the secret key
*
* @param sk pointer to the secret key
* @param pk pointer to the public key
* @param ciphertext reference to the pointer to the ciphertext
* @param message fixed-point number message (double)
* @param level (optional) level to encrypt
*
* @return error code
*/
int encryptFixedptWithSK(void* sk, void* pk, void** ciphertext,
double message, unsigned long level = 1);
/**
* Decrypt a ciphertext to a fixed-point number (double)
*
* @param sk pointer to the secret key
* @param pk pointer to the public key
* @param ciphertext pointer to the ciphertext
* @param message reference to the fixed-point number message
* @param level (optional) level to decrypt
*
* @return error code
*/
int decryptFixedpt(void* sk, void* pk, void* ciphertext,
double* message, unsigned long level = 1);
int mulByConstant(void* pk, void* evk, void** output, void* input,
double constant, unsigned long level = 1);
#endif //FXPT
}
#endif