-
Notifications
You must be signed in to change notification settings - Fork 713
/
rlwe.cpp
408 lines (358 loc) · 18 KB
/
rlwe.cpp
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#include "seal/ciphertext.h"
#include "seal/randomgen.h"
#include "seal/randomtostd.h"
#include "seal/util/clipnormal.h"
#include "seal/util/common.h"
#include "seal/util/globals.h"
#include "seal/util/ntt.h"
#include "seal/util/polyarithsmallmod.h"
#include "seal/util/polycore.h"
#include "seal/util/rlwe.h"
using namespace std;
namespace seal
{
namespace util
{
void sample_poly_ternary(
shared_ptr<UniformRandomGenerator> prng, const EncryptionParameters &parms, uint64_t *destination)
{
auto coeff_modulus = parms.coeff_modulus();
size_t coeff_modulus_size = coeff_modulus.size();
size_t coeff_count = parms.poly_modulus_degree();
RandomToStandardAdapter engine(prng);
uniform_int_distribution<uint64_t> dist(0, 2);
SEAL_ITERATE(iter(destination), coeff_count, [&](auto &I) {
uint64_t rand = dist(engine);
uint64_t flag = static_cast<uint64_t>(-static_cast<int64_t>(rand == 0));
SEAL_ITERATE(
iter(StrideIter<uint64_t *>(&I, coeff_count), coeff_modulus), coeff_modulus_size,
[&](auto J) { *get<0>(J) = rand + (flag & get<1>(J).value()) - 1; });
});
}
void sample_poly_normal(
shared_ptr<UniformRandomGenerator> prng, const EncryptionParameters &parms, uint64_t *destination)
{
auto coeff_modulus = parms.coeff_modulus();
size_t coeff_modulus_size = coeff_modulus.size();
size_t coeff_count = parms.poly_modulus_degree();
if (are_close(global_variables::noise_max_deviation, 0.0))
{
set_zero_poly(coeff_count, coeff_modulus_size, destination);
return;
}
RandomToStandardAdapter engine(prng);
ClippedNormalDistribution dist(
0, global_variables::noise_standard_deviation, global_variables::noise_max_deviation);
SEAL_ITERATE(iter(destination), coeff_count, [&](auto &I) {
int64_t noise = static_cast<int64_t>(dist(engine));
uint64_t flag = static_cast<uint64_t>(-static_cast<int64_t>(noise < 0));
SEAL_ITERATE(
iter(StrideIter<uint64_t *>(&I, coeff_count), coeff_modulus), coeff_modulus_size,
[&](auto J) { *get<0>(J) = static_cast<uint64_t>(noise) + (flag & get<1>(J).value()); });
});
}
void sample_poly_cbd(
shared_ptr<UniformRandomGenerator> prng, const EncryptionParameters &parms, uint64_t *destination)
{
auto coeff_modulus = parms.coeff_modulus();
size_t coeff_modulus_size = coeff_modulus.size();
size_t coeff_count = parms.poly_modulus_degree();
if (are_close(global_variables::noise_max_deviation, 0.0))
{
set_zero_poly(coeff_count, coeff_modulus_size, destination);
return;
}
if (!are_close(global_variables::noise_standard_deviation, 3.2))
{
throw logic_error("centered binomial distribution only supports standard deviation 3.2; use rounded "
"Gaussian instead");
}
auto cbd = [&]() {
unsigned char x[6];
prng->generate(6, reinterpret_cast<seal_byte *>(x));
x[2] &= 0x1F;
x[5] &= 0x1F;
return hamming_weight(x[0]) + hamming_weight(x[1]) + hamming_weight(x[2]) - hamming_weight(x[3]) -
hamming_weight(x[4]) - hamming_weight(x[5]);
};
SEAL_ITERATE(iter(destination), coeff_count, [&](auto &I) {
int32_t noise = cbd();
uint64_t flag = static_cast<uint64_t>(-static_cast<int64_t>(noise < 0));
SEAL_ITERATE(
iter(StrideIter<uint64_t *>(&I, coeff_count), coeff_modulus), coeff_modulus_size,
[&](auto J) { *get<0>(J) = static_cast<uint64_t>(noise) + (flag & get<1>(J).value()); });
});
}
void sample_poly_uniform(
shared_ptr<UniformRandomGenerator> prng, const EncryptionParameters &parms, uint64_t *destination)
{
// Extract encryption parameters
auto coeff_modulus = parms.coeff_modulus();
size_t coeff_modulus_size = coeff_modulus.size();
size_t coeff_count = parms.poly_modulus_degree();
size_t dest_byte_count = mul_safe(coeff_modulus_size, coeff_count, sizeof(uint64_t));
constexpr uint64_t max_random = static_cast<uint64_t>(0xFFFFFFFFFFFFFFFFULL);
// Fill the destination buffer with fresh randomness
prng->generate(dest_byte_count, reinterpret_cast<seal_byte *>(destination));
for (size_t j = 0; j < coeff_modulus_size; j++)
{
auto &modulus = coeff_modulus[j];
uint64_t max_multiple = max_random - barrett_reduce_64(max_random, modulus) - 1;
transform(destination, destination + coeff_count, destination, [&](uint64_t rand) {
// This ensures uniform distribution
while (rand >= max_multiple)
{
prng->generate(sizeof(uint64_t), reinterpret_cast<seal_byte *>(&rand));
}
return barrett_reduce_64(rand, modulus);
});
destination += coeff_count;
}
}
void sample_poly_uniform_seal_3_4(
shared_ptr<UniformRandomGenerator> prng, const EncryptionParameters &parms, uint64_t *destination)
{
// Extract encryption parameters
auto coeff_modulus = parms.coeff_modulus();
size_t coeff_modulus_size = coeff_modulus.size();
size_t coeff_count = parms.poly_modulus_degree();
RandomToStandardAdapter engine(prng);
constexpr uint64_t max_random = static_cast<uint64_t>(0x7FFFFFFFFFFFFFFFULL);
for (size_t j = 0; j < coeff_modulus_size; j++)
{
auto &modulus = coeff_modulus[j];
uint64_t max_multiple = max_random - barrett_reduce_64(max_random, modulus) - 1;
for (size_t i = 0; i < coeff_count; i++)
{
// This ensures uniform distribution
uint64_t rand;
do
{
rand = (static_cast<uint64_t>(engine()) << 31) | (static_cast<uint64_t>(engine()) >> 1);
} while (rand >= max_multiple);
destination[i + j * coeff_count] = barrett_reduce_64(rand, modulus);
}
}
}
void sample_poly_uniform_seal_3_5(
shared_ptr<UniformRandomGenerator> prng, const EncryptionParameters &parms, uint64_t *destination)
{
// Extract encryption parameters
auto coeff_modulus = parms.coeff_modulus();
size_t coeff_modulus_size = coeff_modulus.size();
size_t coeff_count = parms.poly_modulus_degree();
RandomToStandardAdapter engine(prng);
constexpr uint64_t max_random = static_cast<uint64_t>(0xFFFFFFFFFFFFFFFFULL);
for (size_t j = 0; j < coeff_modulus_size; j++)
{
auto &modulus = coeff_modulus[j];
uint64_t max_multiple = max_random - barrett_reduce_64(max_random, modulus) - 1;
for (size_t i = 0; i < coeff_count; i++)
{
// This ensures uniform distribution
uint64_t rand;
do
{
rand = (static_cast<uint64_t>(engine()) << 32) | static_cast<uint64_t>(engine());
} while (rand >= max_multiple);
destination[i + j * coeff_count] = barrett_reduce_64(rand, modulus);
}
}
}
void encrypt_zero_asymmetric(
const PublicKey &public_key, const SEALContext &context, parms_id_type parms_id, bool is_ntt_form,
Ciphertext &destination)
{
#ifdef SEAL_DEBUG
if (!is_valid_for(public_key, context))
{
throw invalid_argument("public key is not valid for the encryption parameters");
}
#endif
// We use a fresh memory pool with `clear_on_destruction' enabled
MemoryPoolHandle pool = MemoryManager::GetPool(mm_prof_opt::mm_force_new, true);
auto &context_data = *context.get_context_data(parms_id);
auto &parms = context_data.parms();
auto &coeff_modulus = parms.coeff_modulus();
auto &plain_modulus = parms.plain_modulus();
size_t coeff_modulus_size = coeff_modulus.size();
size_t coeff_count = parms.poly_modulus_degree();
auto ntt_tables = context_data.small_ntt_tables();
size_t encrypted_size = public_key.data().size();
scheme_type type = parms.scheme();
// Make destination have right size and parms_id
// Ciphertext (c_0,c_1, ...)
destination.resize(context, parms_id, encrypted_size);
destination.is_ntt_form() = is_ntt_form;
destination.scale() = 1.0;
destination.correction_factor() = 1;
// c[j] = public_key[j] * u + e[j] in BFV/CKKS = public_key[j] * u + p * e[j] in BGV
// where e[j] <-- chi, u <-- R_3
// Create a PRNG; u and the noise/error share the same PRNG
auto prng = parms.random_generator()->create();
// Generate u <-- R_3
auto u(allocate_poly(coeff_count, coeff_modulus_size, pool));
sample_poly_ternary(prng, parms, u.get());
// c[j] = u * public_key[j]
for (size_t i = 0; i < coeff_modulus_size; i++)
{
ntt_negacyclic_harvey(u.get() + i * coeff_count, ntt_tables[i]);
for (size_t j = 0; j < encrypted_size; j++)
{
dyadic_product_coeffmod(
u.get() + i * coeff_count, public_key.data().data(j) + i * coeff_count, coeff_count,
coeff_modulus[i], destination.data(j) + i * coeff_count);
// Addition with e_0, e_1 is in non-NTT form
if (!is_ntt_form)
{
inverse_ntt_negacyclic_harvey(destination.data(j) + i * coeff_count, ntt_tables[i]);
}
}
}
// Generate e_j <-- chi
// c[j] = public_key[j] * u + e[j] in BFV/CKKS, = public_key[j] * u + p * e[j] in BGV,
for (size_t j = 0; j < encrypted_size; j++)
{
SEAL_NOISE_SAMPLER(prng, parms, u.get());
RNSIter gaussian_iter(u.get(), coeff_count);
// In BGV, p * e is used
if (type == scheme_type::bgv)
{
if (is_ntt_form)
{
ntt_negacyclic_harvey_lazy(gaussian_iter, coeff_modulus_size, ntt_tables);
}
multiply_poly_scalar_coeffmod(
gaussian_iter, coeff_modulus_size, plain_modulus.value(), coeff_modulus, gaussian_iter);
}
else
{
if (is_ntt_form)
{
ntt_negacyclic_harvey(gaussian_iter, coeff_modulus_size, ntt_tables);
}
}
RNSIter dst_iter(destination.data(j), coeff_count);
add_poly_coeffmod(gaussian_iter, dst_iter, coeff_modulus_size, coeff_modulus, dst_iter);
}
}
void encrypt_zero_symmetric(
const SecretKey &secret_key, const SEALContext &context, parms_id_type parms_id, bool is_ntt_form,
bool save_seed, Ciphertext &destination)
{
#ifdef SEAL_DEBUG
if (!is_valid_for(secret_key, context))
{
throw invalid_argument("secret key is not valid for the encryption parameters");
}
#endif
// We use a fresh memory pool with `clear_on_destruction' enabled.
MemoryPoolHandle pool = MemoryManager::GetPool(mm_prof_opt::mm_force_new, true);
auto &context_data = *context.get_context_data(parms_id);
auto &parms = context_data.parms();
auto &coeff_modulus = parms.coeff_modulus();
auto &plain_modulus = parms.plain_modulus();
size_t coeff_modulus_size = coeff_modulus.size();
size_t coeff_count = parms.poly_modulus_degree();
auto ntt_tables = context_data.small_ntt_tables();
size_t encrypted_size = 2;
scheme_type type = parms.scheme();
// If a polynomial is too small to store UniformRandomGeneratorInfo,
// it is best to just disable save_seed. Note that the size needed is
// the size of UniformRandomGeneratorInfo plus one (uint64_t) because
// of an indicator word that indicates a seeded ciphertext.
size_t poly_uint64_count = mul_safe(coeff_count, coeff_modulus_size);
size_t prng_info_byte_count =
static_cast<size_t>(UniformRandomGeneratorInfo::SaveSize(compr_mode_type::none));
size_t prng_info_uint64_count =
divide_round_up(prng_info_byte_count, static_cast<size_t>(bytes_per_uint64));
if (save_seed && poly_uint64_count < prng_info_uint64_count + 1)
{
save_seed = false;
}
destination.resize(context, parms_id, encrypted_size);
destination.is_ntt_form() = is_ntt_form;
destination.scale() = 1.0;
destination.correction_factor() = 1;
// Create an instance of a random number generator. We use this for sampling
// a seed for a second PRNG used for sampling u (the seed can be public
// information. This PRNG is also used for sampling the noise/error below.
auto bootstrap_prng = parms.random_generator()->create();
// Sample a public seed for generating uniform randomness
prng_seed_type public_prng_seed;
bootstrap_prng->generate(prng_seed_byte_count, reinterpret_cast<seal_byte *>(public_prng_seed.data()));
// Set up a new default PRNG for expanding u from the seed sampled above
auto ciphertext_prng = UniformRandomGeneratorFactory::DefaultFactory()->create(public_prng_seed);
// Generate ciphertext: (c[0], c[1]) = ([-(as+ e)]_q, a) in BFV/CKKS
// Generate ciphertext: (c[0], c[1]) = ([-(as+pe)]_q, a) in BGV
uint64_t *c0 = destination.data();
uint64_t *c1 = destination.data(1);
// Sample a uniformly at random
if (is_ntt_form || !save_seed)
{
// Sample the NTT form directly
sample_poly_uniform(ciphertext_prng, parms, c1);
}
else if (save_seed)
{
// Sample non-NTT form and store the seed
sample_poly_uniform(ciphertext_prng, parms, c1);
for (size_t i = 0; i < coeff_modulus_size; i++)
{
// Transform the c1 into NTT representation
ntt_negacyclic_harvey(c1 + i * coeff_count, ntt_tables[i]);
}
}
// Sample e <-- chi
auto noise(allocate_poly(coeff_count, coeff_modulus_size, pool));
SEAL_NOISE_SAMPLER(bootstrap_prng, parms, noise.get());
// Calculate -(as+ e) (mod q) and store in c[0] in BFV/CKKS
// Calculate -(as+pe) (mod q) and store in c[0] in BGV
for (size_t i = 0; i < coeff_modulus_size; i++)
{
dyadic_product_coeffmod(
secret_key.data().data() + i * coeff_count, c1 + i * coeff_count, coeff_count, coeff_modulus[i],
c0 + i * coeff_count);
if (is_ntt_form)
{
// Transform the noise e into NTT representation
ntt_negacyclic_harvey(noise.get() + i * coeff_count, ntt_tables[i]);
}
else
{
inverse_ntt_negacyclic_harvey(c0 + i * coeff_count, ntt_tables[i]);
}
if (type == scheme_type::bgv)
{
// noise = pe instead of e in BGV
multiply_poly_scalar_coeffmod(
noise.get() + i * coeff_count, coeff_count, plain_modulus.value(), coeff_modulus[i],
noise.get() + i * coeff_count);
}
// c0 = as + noise
add_poly_coeffmod(
noise.get() + i * coeff_count, c0 + i * coeff_count, coeff_count, coeff_modulus[i],
c0 + i * coeff_count);
// (as + noise, a) -> (-(as + noise), a),
negate_poly_coeffmod(c0 + i * coeff_count, coeff_count, coeff_modulus[i], c0 + i * coeff_count);
}
if (!is_ntt_form && !save_seed)
{
for (size_t i = 0; i < coeff_modulus_size; i++)
{
// Transform the c1 into non-NTT representation
inverse_ntt_negacyclic_harvey(c1 + i * coeff_count, ntt_tables[i]);
}
}
if (save_seed)
{
UniformRandomGeneratorInfo prng_info = ciphertext_prng->info();
// Write prng_info to destination.data(1) after an indicator word
c1[0] = static_cast<uint64_t>(0xFFFFFFFFFFFFFFFFULL);
prng_info.save(reinterpret_cast<seal_byte *>(c1 + 1), prng_info_byte_count, compr_mode_type::none);
}
}
} // namespace util
} // namespace seal