-
Notifications
You must be signed in to change notification settings - Fork 0
/
encryptor.h
457 lines (387 loc) · 19.3 KB
/
encryptor.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
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
#include "ciphertext.h"
#include "context.h"
#include "encryptionparams.h"
#include "memorymanager.h"
#include "plaintext.cuh"
#include "publickey.h"
#include "secretkey.h"
#include "serializable.h"
#include "util/defines.h"
#include "util/ntt.h"
//#include "util/randomgenerator.cuh"
//#include "util/ntt_60bit.cuh"
#include <vector>
namespace sigma
{
namespace util {
class RandomGenerator;
}
/**
Encrypts Plaintext objects into Ciphertext objects. Constructing an Encryptor
requires a SIGMAContext with valid encryption parameters, the public key and/or
the secret key. If an Encrytor is given a secret key, it supports symmetric-key
encryption. If an Encryptor is given a public key, it supports asymmetric-key
encryption.
@par Overloads
For the encrypt function we provide two overloads concerning the memory pool
used in allocations needed during the operation. In one overload the global
memory pool is used for this purpose, and in another overload the user can
supply a MemoryPoolHandle to to be used instead. This is to allow one single
Encryptor to be used concurrently by several threads without running into thread
contention in allocations taking place during operations. For example, one can
share one single Encryptor across any number of threads, but in each thread
call the encrypt function by giving it a thread-local MemoryPoolHandle to use.
It is important for a developer to understand how this works to avoid unnecessary
performance bottlenecks.
@par NTT form
When using the BFV/BGV scheme (scheme_type::bfv/bgv), all plaintext and ciphertexts should
remain by default in the usual coefficient representation, i.e. not in NTT form.
When using the CKKS scheme (scheme_type::ckks), all plaintexts and ciphertexts
should remain by default in NTT form. We call these scheme-specific NTT states
the "default NTT form". Decryption requires the input ciphertexts to be in
the default NTT form, and will throw an exception if this is not the case.
*/
class Encryptor
{
public:
/**
Creates an Encryptor instance initialized with the specified SIGMAContext
and public key.
@param[in] context The SIGMAContext
@param[in] public_key The public key
@throws std::invalid_argument if the encryption parameters are not valid
@throws std::invalid_argument if public_key is not valid
*/
Encryptor(const SIGMAContext &context, const PublicKey &public_key);
/**
Creates an Encryptor instance initialized with the specified SIGMAContext
and secret key.
@param[in] context The SIGMAContext
@param[in] secret_key The secret key
@throws std::invalid_argument if the encryption parameters are not valid
@throws std::invalid_argument if secret_key is not valid
*/
Encryptor(const SIGMAContext &context, const SecretKey &secret_key);
/**
Creates an Encryptor instance initialized with the specified SIGMAContext,
secret key, and public key.
@param[in] context The SIGMAContext
@param[in] public_key The public key
@param[in] secret_key The secret key
@throws std::invalid_argument if the encryption parameters are not valid
@throws std::invalid_argument if public_key or secret_key is not valid
*/
Encryptor(const SIGMAContext &context, const PublicKey &public_key, const SecretKey &secret_key);
~Encryptor();
/**
Give a new instance of public key.
@param[in] public_key The public key
@throws std::invalid_argument if public_key is not valid
*/
inline void set_public_key(const PublicKey &public_key)
{
if (!is_valid_for(public_key, context_))
{
throw std::invalid_argument("public key is not valid for encryption parameters");
}
public_key_ = public_key;
}
/**
Give a new instance of secret key.
@param[in] secret_key The secret key
@throws std::invalid_argument if secret_key is not valid
*/
inline void set_secret_key(const SecretKey &secret_key)
{
if (!is_valid_for(secret_key, context_))
{
throw std::invalid_argument("secret key is not valid for encryption parameters");
}
secret_key_ = secret_key;
}
/**
Encrypts a plaintext with the public key and stores the result in
destination.
The encryption parameters for the resulting ciphertext correspond to:
1) in BFV/BGV, the highest (data) level in the modulus switching chain,
2) in CKKS, the encryption parameters of the plaintext.
Dynamic memory allocations in the process are allocated from the memory
pool pointed to by the given MemoryPoolHandle.
@param[in] plain The plaintext to encrypt
@param[out] destination The ciphertext to overwrite with the encrypted
plaintext
@param[in] pool The MemoryPoolHandle pointing to a valid memory pool
@throws std::logic_error if a public key is not set
@throws std::invalid_argument if plain is not valid for the encryption
parameters
@throws std::invalid_argument if plain is not in default NTT form
@throws std::invalid_argument if pool is uninitialized
*/
inline void encrypt(
const Plaintext &plain, Ciphertext &destination, MemoryPoolHandle pool = MemoryManager::GetPool()) const
{
encrypt_internal(plain, true, false, destination, pool);
}
/**
Encrypts a plaintext with the public key and returns the ciphertext as
a serializable object.
The encryption parameters for the resulting ciphertext correspond to:
1) in BFV/BGV, the highest (data) level in the modulus switching chain,
2) in CKKS, the encryption parameters of the plaintext.
Dynamic memory allocations in the process are allocated from the memory
pool pointed to by the given MemoryPoolHandle.
@param[in] plain The plaintext to encrypt
@param[in] pool The MemoryPoolHandle pointing to a valid memory pool
@throws std::logic_error if a public key is not set
@throws std::invalid_argument if plain is not valid for the encryption
parameters
@throws std::invalid_argument if plain is not in default NTT form
@throws std::invalid_argument if pool is uninitialized
*/
SIGMA_NODISCARD inline Serializable<Ciphertext> encrypt(
const Plaintext &plain, MemoryPoolHandle pool = MemoryManager::GetPool()) const
{
Ciphertext destination;
encrypt_internal(plain, true, true, destination, pool);
return destination;
}
/**
Encrypts a zero plaintext with the public key and stores the result in
destination.
The encryption parameters for the resulting ciphertext correspond to the
highest (data) level in the modulus switching chain. Dynamic memory
allocations in the process are allocated from the memory pool pointed to
by the given MemoryPoolHandle.
@param[out] destination The ciphertext to overwrite with the encrypted
plaintext
@param[in] pool The MemoryPoolHandle pointing to a valid memory pool
@throws std::logic_error if a public key is not set
@throws std::invalid_argument if pool is uninitialized
*/
inline void encrypt_zero(Ciphertext &destination, MemoryPoolHandle pool = MemoryManager::GetPool()) const
{
encrypt_zero(context_.first_parms_id(), destination, pool);
}
/**
Encrypts a zero plaintext with the public key and returns the ciphertext
as a serializable object.
The encryption parameters for the resulting ciphertext correspond to the
given parms_id. Dynamic memory allocations in the process are allocated
from the memory pool pointed to by the given MemoryPoolHandle.
@param[in] parms_id The parms_id for the resulting ciphertext
@param[in] pool The MemoryPoolHandle pointing to a valid memory pool
@throws std::logic_error if a public key is not set
@throws std::invalid_argument if parms_id is not valid for the encryption
parameters
@throws std::invalid_argument if pool is uninitialized
*/
SIGMA_NODISCARD inline Serializable<Ciphertext> encrypt_zero(
parms_id_type parms_id, MemoryPoolHandle pool = MemoryManager::GetPool()) const
{
Ciphertext destination;
encrypt_zero_internal(parms_id, true, true, destination, pool);
return destination;
}
/**
Encrypts a zero plaintext with the public key and stores the result in
destination.
The encryption parameters for the resulting ciphertext correspond to the
given parms_id. Dynamic memory allocations in the process are allocated
from the memory pool pointed to by the given MemoryPoolHandle.
@param[in] parms_id The parms_id for the resulting ciphertext
@param[out] destination The ciphertext to overwrite with the encrypted
plaintext
@param[in] pool The MemoryPoolHandle pointing to a valid memory pool
@throws std::logic_error if a public key is not set
@throws std::invalid_argument if parms_id is not valid for the encryption
parameters
@throws std::invalid_argument if pool is uninitialized
*/
inline void encrypt_zero(
parms_id_type parms_id, Ciphertext &destination, MemoryPoolHandle pool = MemoryManager::GetPool()) const
{
encrypt_zero_internal(parms_id, true, false, destination, pool);
}
/**
Encrypts a zero plaintext with the public key and returns the ciphertext
as a serializable object.
The encryption parameters for the resulting ciphertext correspond to the
highest (data) level in the modulus switching chain. Dynamic memory
allocations in the process are allocated from the memory pool pointed to
by the given MemoryPoolHandle.
@param[in] pool The MemoryPoolHandle pointing to a valid memory pool
@throws std::logic_error if a public key is not set
@throws std::invalid_argument if pool is uninitialized
*/
SIGMA_NODISCARD inline Serializable<Ciphertext> encrypt_zero(
MemoryPoolHandle pool = MemoryManager::GetPool()) const
{
return encrypt_zero(context_.first_parms_id(), pool);
}
/**
Encrypts a plaintext with the secret key and stores the result in
destination.
The encryption parameters for the resulting ciphertext correspond to:
1) in BFV/BGV, the highest (data) level in the modulus switching chain,
2) in CKKS, the encryption parameters of the plaintext.
Dynamic memory allocations in the process are allocated from the memory
pool pointed to by the given MemoryPoolHandle.
@param[in] plain The plaintext to encrypt
@param[out] destination The ciphertext to overwrite with the encrypted
plaintext
@param[in] pool The MemoryPoolHandle pointing to a valid memory pool
@throws std::logic_error if a secret key is not set
@throws std::invalid_argument if plain is not valid for the encryption
parameters
@throws std::invalid_argument if plain is not in default NTT form
@throws std::invalid_argument if pool is uninitialized
*/
inline void encrypt_symmetric(
const Plaintext &plain, Ciphertext &destination, MemoryPoolHandle pool = MemoryManager::GetPool()) const
{
encrypt_internal(plain, false, false, destination, pool);
}
inline void encrypt_symmetric_ckks(
const Plaintext &plain, Ciphertext &destination, const Ciphertext &c1)
{
encrypt_symmetric_ckks_internal(plain, destination, c1);
}
inline void sample_symmetric_ckks_c1(Ciphertext &destination) const {
sample_symmetric_ckks_c1_internal(destination);
}
/**
Encrypts a plaintext with the secret key and returns the ciphertext as
a serializable object.
Half of the ciphertext data is pseudo-randomly generated from a seed to
reduce the object size. The resulting serializable object cannot be used
directly and is meant to be serialized for the size reduction to have an
impact.
The encryption parameters for the resulting ciphertext correspond to:
1) in BFV/BGV, the highest (data) level in the modulus switching chain,
2) in CKKS, the encryption parameters of the plaintext.
Dynamic memory allocations in the process are allocated from the memory
pool pointed to by the given MemoryPoolHandle.
@param[in] plain The plaintext to encrypt
@param[in] pool The MemoryPoolHandle pointing to a valid memory pool
@throws std::logic_error if a secret key is not set
@throws std::invalid_argument if plain is not valid for the encryption
parameters
@throws std::invalid_argument if plain is not in default NTT form
@throws std::invalid_argument if pool is uninitialized
*/
SIGMA_NODISCARD inline Serializable<Ciphertext> encrypt_symmetric(
const Plaintext &plain, MemoryPoolHandle pool = MemoryManager::GetPool()) const
{
Ciphertext destination;
encrypt_internal(plain, false, true, destination, pool);
return destination;
}
/**
Encrypts a zero plaintext with the secret key and stores the result in
destination.
The encryption parameters for the resulting ciphertext correspond to the
given parms_id. Dynamic memory allocations in the process are allocated
from the memory pool pointed to by the given MemoryPoolHandle.
@param[in] parms_id The parms_id for the resulting ciphertext
@param[out] destination The ciphertext to overwrite with the encrypted
plaintext
@param[in] pool The MemoryPoolHandle pointing to a valid memory pool
@throws std::logic_error if a secret key is not set
@throws std::invalid_argument if parms_id is not valid for the encryption
parameters
@throws std::invalid_argument if pool is uninitialized
*/
inline void encrypt_zero_symmetric(
parms_id_type parms_id, Ciphertext &destination, MemoryPoolHandle pool = MemoryManager::GetPool()) const
{
encrypt_zero_internal(parms_id, false, false, destination, pool);
}
/**
Encrypts a zero plaintext with the secret key and returns the ciphertext
as a serializable object.
Half of the ciphertext data is pseudo-randomly generated from a seed to
reduce the object size. The resulting serializable object cannot be used
directly and is meant to be serialized for the size reduction to have an
impact.
The encryption parameters for the resulting ciphertext correspond to the
given parms_id. Dynamic memory allocations in the process are allocated
from the memory pool pointed to by the given MemoryPoolHandle.
@param[in] parms_id The parms_id for the resulting ciphertext
@param[in] pool The MemoryPoolHandle pointing to a valid memory pool
@throws std::logic_error if a secret key is not set
@throws std::invalid_argument if parms_id is not valid for the encryption
parameters
@throws std::invalid_argument if pool is uninitialized
*/
SIGMA_NODISCARD inline Serializable<Ciphertext> encrypt_zero_symmetric(
parms_id_type parms_id, MemoryPoolHandle pool = MemoryManager::GetPool()) const
{
Ciphertext destination;
encrypt_zero_internal(parms_id, false, true, destination, pool);
return destination;
}
/**
Encrypts a zero plaintext with the secret key and stores the result in
destination.
The encryption parameters for the resulting ciphertext correspond to the
highest (data) level in the modulus switching chain. Dynamic memory
allocations in the process are allocated from the memory pool pointed to
by the given MemoryPoolHandle.
@param[out] destination The ciphertext to overwrite with the encrypted
plaintext
@param[in] pool The MemoryPoolHandle pointing to a valid memory pool
@throws std::logic_error if a secret key is not set
@throws std::invalid_argument if pool is uninitialized
*/
inline void encrypt_zero_symmetric(
Ciphertext &destination, MemoryPoolHandle pool = MemoryManager::GetPool()) const
{
encrypt_zero_symmetric(context_.first_parms_id(), destination, pool);
}
/**
Encrypts a zero plaintext with the secret key and returns the ciphertext
as a serializable object.
Half of the ciphertext data is pseudo-randomly generated from a seed to
reduce the object size. The resulting serializable object cannot be used
directly and is meant to be serialized for the size reduction to have an
impact.
The encryption parameters for the resulting ciphertext correspond to the
highest (data) level in the modulus switching chain. Dynamic memory
allocations in the process are allocated from the memory pool pointed to
by the given MemoryPoolHandle.
@param[in] pool The MemoryPoolHandle pointing to a valid memory pool
@throws std::logic_error if a secret key is not set
@throws std::invalid_argument if pool is uninitialized
*/
SIGMA_NODISCARD inline Serializable<Ciphertext> encrypt_zero_symmetric(
MemoryPoolHandle pool = MemoryManager::GetPool()) const
{
return encrypt_zero_symmetric(context_.first_parms_id(), pool);
}
/**
Enables access to private members of sigma::Encryptor for SIGMA_C.
*/
struct EncryptorPrivateHelper;
private:
Encryptor(const Encryptor ©) = delete;
Encryptor(Encryptor &&source) = delete;
Encryptor &operator=(const Encryptor &assign) = delete;
Encryptor &operator=(Encryptor &&assign) = delete;
void encrypt_zero_internal(
parms_id_type parms_id, bool is_asymmetric, bool save_seed, Ciphertext &destination,
MemoryPoolHandle pool = MemoryManager::GetPool()) const;
void encrypt_internal(
const Plaintext &plain, bool is_asymmetric, bool save_seed, Ciphertext &destination,
MemoryPoolHandle pool = MemoryManager::GetPool()) const;
void sample_symmetric_ckks_c1_internal(Ciphertext &destination) const;
void encrypt_symmetric_ckks_internal(const Plaintext &plain, Ciphertext &destination, const Ciphertext &c1);
SIGMAContext context_;
PublicKey public_key_;
SecretKey secret_key_;
util::RandomGenerator *random_generator_ = nullptr;
// util::DeviceArray<uint64_t> temp_noise_;
};
} // namespace sigma