Repository navigation
Expand file tree
/
Copy pathrns.h
More file actions
556 lines (414 loc) · 20.1 KB
/
Copy pathrns.h
File metadata and controls
556 lines (414 loc) · 20.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
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
#include "seal/memorymanager.h"
#include "seal/modulus.h"
#include "seal/util/iterator.h"
#include "seal/util/ntt.h"
#include "seal/util/pointer.h"
#include "seal/util/uintarithsmallmod.h"
#include <cstddef>
#include <cstdint>
#include <functional>
#include <stdexcept>
#include <vector>
namespace seal
{
namespace util
{
class RNSBase
{
public:
RNSBase(const std::vector<Modulus> &rnsbase, MemoryPoolHandle pool);
RNSBase(RNSBase &&source) = default;
RNSBase(const RNSBase ©, MemoryPoolHandle pool);
RNSBase(const RNSBase ©) : RNSBase(copy, copy.pool_)
{}
RNSBase &operator=(const RNSBase &assign) = delete;
SEAL_NODISCARD inline const Modulus &operator[](std::size_t index) const
{
if (index >= size_)
{
throw std::out_of_range("index is out of range");
}
return base_[index];
}
SEAL_NODISCARD inline std::size_t size() const noexcept
{
return size_;
}
SEAL_NODISCARD bool contains(const Modulus &value) const noexcept;
SEAL_NODISCARD bool is_subbase_of(const RNSBase &superbase) const noexcept;
SEAL_NODISCARD inline bool is_superbase_of(const RNSBase &subbase) const noexcept
{
return subbase.is_subbase_of(*this);
}
SEAL_NODISCARD inline bool is_proper_subbase_of(const RNSBase &superbase) const noexcept
{
return (size_ < superbase.size_) && is_subbase_of(superbase);
}
SEAL_NODISCARD inline bool is_proper_superbase_of(const RNSBase &subbase) const noexcept
{
return (size_ > subbase.size_) && !is_subbase_of(subbase);
}
SEAL_NODISCARD RNSBase extend(const Modulus &value) const;
SEAL_NODISCARD RNSBase extend(const RNSBase &other) const;
SEAL_NODISCARD RNSBase drop() const;
SEAL_NODISCARD RNSBase drop(const Modulus &value) const;
void decompose(std::uint64_t *value, MemoryPoolHandle pool) const;
void decompose_array(std::uint64_t *value, std::size_t count, MemoryPoolHandle pool) const;
void compose(std::uint64_t *value, MemoryPoolHandle pool) const;
void compose_array(std::uint64_t *value, std::size_t count, MemoryPoolHandle pool) const;
SEAL_NODISCARD inline const Modulus *base() const noexcept
{
return base_.get();
}
SEAL_NODISCARD inline const std::uint64_t *base_prod() const noexcept
{
return base_prod_.get();
}
SEAL_NODISCARD inline const std::uint64_t *punctured_prod_array() const noexcept
{
return punctured_prod_array_.get();
}
SEAL_NODISCARD inline const MultiplyUIntModOperand *inv_punctured_prod_mod_base_array() const noexcept
{
return inv_punctured_prod_mod_base_array_.get();
}
private:
RNSBase(MemoryPoolHandle pool) : pool_(std::move(pool)), size_(0)
{
if (!pool_)
{
throw std::invalid_argument("pool is uninitialized");
}
}
bool initialize();
MemoryPoolHandle pool_;
std::size_t size_;
Pointer<Modulus> base_;
Pointer<std::uint64_t> base_prod_;
Pointer<std::uint64_t> punctured_prod_array_;
Pointer<MultiplyUIntModOperand> inv_punctured_prod_mod_base_array_;
};
class BaseConverter
{
public:
BaseConverter(const RNSBase &ibase, const RNSBase &obase, MemoryPoolHandle pool)
: pool_(std::move(pool)), ibase_(ibase, pool_), obase_(obase, pool_)
{
if (!pool_)
{
throw std::invalid_argument("pool is uninitialized");
}
initialize(nullptr, nullptr);
}
/**
Fast conversions compute out_j = [sum_i [in_i * a_i * (q/q_i)^(-1)]_{q_i} * (q/q_i) * b_j]_{p_j} for ibase
q_i with product q, obase p_j, a_i = ibase_scale[i], and b_j = obase_scale[j]. exact_convert_array does not
support scaling.
@throws std::invalid_argument on a scale size mismatch or an uninitialized pool
*/
BaseConverter(
const RNSBase &ibase, const RNSBase &obase, const std::vector<std::uint64_t> &ibase_scale,
const std::vector<std::uint64_t> &obase_scale, MemoryPoolHandle pool)
: pool_(std::move(pool)), ibase_(ibase, pool_), obase_(obase, pool_)
{
if (!pool_)
{
throw std::invalid_argument("pool is uninitialized");
}
if (ibase_scale.size() != ibase_.size() || obase_scale.size() != obase_.size())
{
throw std::invalid_argument("scale size mismatch");
}
initialize(ibase_scale.data(), obase_scale.data());
}
SEAL_NODISCARD inline std::size_t ibase_size() const noexcept
{
return ibase_.size();
}
SEAL_NODISCARD inline std::size_t obase_size() const noexcept
{
return obase_.size();
}
SEAL_NODISCARD inline const RNSBase &ibase() const noexcept
{
return ibase_;
}
SEAL_NODISCARD inline const RNSBase &obase() const noexcept
{
return obase_;
}
void fast_convert(ConstCoeffIter in, CoeffIter out, MemoryPoolHandle pool) const;
/**
Input coefficients may be any 64-bit values. in and out may overlap only at equal coefficient indices.
*/
void fast_convert_array(ConstRNSIter in, RNSIter out, MemoryPoolHandle pool) const;
// The exact base convertion function, only supports obase size of 1.
void exact_convert_array(ConstRNSIter in, CoeffIter out, MemoryPoolHandle) const;
private:
friend class RNSTool;
BaseConverter(const BaseConverter ©) = delete;
BaseConverter(BaseConverter &&source) = delete;
BaseConverter &operator=(const BaseConverter &assign) = delete;
BaseConverter &operator=(BaseConverter &&assign) = delete;
void initialize(const std::uint64_t *ibase_scale, const std::uint64_t *obase_scale);
/**
Converts count coefficients, with component strides in_stride and out_stride. Set input_reduced only if all
in_i < q_i; out must then not overlap in. out must not overlap temp (count * ibase_size() values), whose row
i receives [in_i * a_i * (q/q_i)^(-1)]_{q_i} if input_reduced is false.
*/
void fast_convert_block(
const std::uint64_t *in, std::size_t in_stride, std::size_t count, bool input_reduced,
std::uint64_t *out, std::size_t out_stride, std::uint64_t *temp) const;
// Maximum block size, in coefficients
static constexpr std::size_t max_block_size = 512;
MemoryPoolHandle pool_;
RNSBase ibase_;
RNSBase obase_;
// True if created with scaling factors
bool is_scaled_ = false;
// True if all scaled_inv_punctured_prod_ are 1
bool unit_first_step_ = false;
// a_i * (q/q_i)^(-1) mod q_i for input scales a_i
Pointer<MultiplyUIntModOperand> scaled_inv_punctured_prod_;
// [j][i] = b_j * (q/q_i) mod p_j for output scales b_j
Pointer<Pointer<std::uint64_t>> base_change_matrix_;
// Row-major copy of base_change_matrix_, with rows for odd p_j in Montgomery form
Pointer<std::uint64_t> conv_matrix_;
// -p_j^(-1) mod 2^64 for odd p_j, and 0 for even p_j
Pointer<std::uint64_t> obase_neg_inv_mod_2_64_;
// Chunk ends of ibase, with sum(q_i - 1) < 2^64 per chunk. Not a std::vector, which is not standard-layout
// in MSVC Debug builds.
Pointer<std::size_t> ibase_chunk_ends_;
std::size_t ibase_chunk_count_ = 0;
};
class RNSTool
{
public:
/**
@throws std::invalid_argument if poly_modulus_degree is out of range, coeff_modulus is not valid, or pool is
invalid.
@throws std::logic_error if coeff_modulus and extended bases do not support NTT or are not coprime.
*/
RNSTool(
std::size_t poly_modulus_degree, const RNSBase &coeff_modulus, const Modulus &plain_modulus,
MemoryPoolHandle pool);
/**
@param[in] input Must be in RNS form, i.e. coefficient must be less than the associated modulus.
*/
void divide_and_round_q_last_inplace(RNSIter input, MemoryPoolHandle pool) const;
void divide_and_round_q_last_ntt_inplace(
RNSIter input, ConstNTTTablesIter rns_ntt_tables, MemoryPoolHandle pool) const;
/**
Out-of-place versions of the functions above, with the result in q without its last prime. Component i of
destination may coincide with component j <= i of input.
*/
void divide_and_round_q_last(ConstRNSIter input, RNSIter destination, MemoryPoolHandle pool) const;
void divide_and_round_q_last_ntt(
ConstRNSIter input, ConstNTTTablesIter rns_ntt_tables, RNSIter destination,
MemoryPoolHandle pool) const;
/**
Computes (x - delta) / q_last for a BGV ciphertext polynomial x in NTT form, where delta is a multiple of t
congruent to x modulo q_last. Aliasing as for divide_and_round_q_last.
*/
void mod_t_and_divide_q_last_ntt(
ConstRNSIter input, ConstNTTTablesIter rns_ntt_tables, RNSIter destination,
MemoryPoolHandle pool) const;
/**
Shenoy-Kumaresan conversion from Bsk to q
*/
void fastbconv_sk(ConstRNSIter input, RNSIter destination, MemoryPoolHandle pool) const;
/**
Montgomery reduction mod q; changes base from Bsk U {m_tilde} to Bsk
*/
void sm_mrq(ConstRNSIter input, RNSIter destination, MemoryPoolHandle pool) const;
/**
Divide by q and fast floor from q U Bsk to Bsk
*/
void fast_floor(ConstRNSIter input, RNSIter destination, MemoryPoolHandle pool) const;
/**
Fast base conversion from q to Bsk U {m_tilde}
*/
void fastbconv_m_tilde(ConstRNSIter input, RNSIter destination, MemoryPoolHandle pool) const;
/**
fastbconv_m_tilde and sm_mrq (BEHZ steps (1)-(2) in Evaluator::bfv_multiply) for any 64-bit input. With
lazy_output, the output is in [0, 3m) for each prime m in Bsk.
*/
void fastbconv_m_tilde_sm_mrq(
ConstRNSIter input, bool lazy_output, RNSIter destination, MemoryPoolHandle pool) const;
/**
Inverse NTT, multiplication by t, fast_floor, and fastbconv_sk (BEHZ steps (5)-(8) in
Evaluator::bfv_multiply). input_q and input_Bsk, in NTT form in [0, 2m) for each prime m, are overwritten.
With input_montgomery_form, they hold c * 2^(-64) mod m, and the result is for c.
*/
void inverse_ntt_multiply_t_fast_floor_fastbconv_sk(
RNSIter input_q, RNSIter input_Bsk, ConstNTTTablesIter base_q_ntt_tables, bool input_montgomery_form,
RNSIter destination, MemoryPoolHandle pool) const;
/**
Compute round(t/q * |input|_q) mod t exactly
*/
void decrypt_scale_and_round(ConstRNSIter phase, CoeffIter destination, MemoryPoolHandle pool) const;
/**
Remove the last q for bgv ciphertext
*/
void mod_t_and_divide_q_last_ntt_inplace(
RNSIter input, ConstNTTTablesIter rns_ntt_tables, MemoryPoolHandle pool) const;
/**
Compute mod t
*/
void decrypt_modt(RNSIter phase, CoeffIter destination, MemoryPoolHandle pool) const;
SEAL_NODISCARD inline auto inv_q_last_mod_q() const noexcept
{
return inv_q_last_mod_q_.get();
}
SEAL_NODISCARD inline auto base_Bsk_ntt_tables() const noexcept
{
return base_Bsk_ntt_tables_.get();
}
SEAL_NODISCARD inline auto base_q() const noexcept
{
return base_q_.get();
}
SEAL_NODISCARD inline auto base_B() const noexcept
{
return base_B_.get();
}
SEAL_NODISCARD inline auto base_Bsk() const noexcept
{
return base_Bsk_.get();
}
SEAL_NODISCARD inline auto base_Bsk_m_tilde() const noexcept
{
return base_Bsk_m_tilde_.get();
}
SEAL_NODISCARD inline auto base_t_gamma() const noexcept
{
return base_t_gamma_.get();
}
SEAL_NODISCARD inline auto &m_tilde() const noexcept
{
return m_tilde_;
}
SEAL_NODISCARD inline auto &m_sk() const noexcept
{
return m_sk_;
}
SEAL_NODISCARD inline auto &t() const noexcept
{
return t_;
}
SEAL_NODISCARD inline auto &gamma() const noexcept
{
return gamma_;
}
SEAL_NODISCARD inline auto &inv_q_last_mod_t() const noexcept
{
return inv_q_last_mod_t_;
}
SEAL_NODISCARD inline const uint64_t &q_last_mod_t() const noexcept
{
return q_last_mod_t_;
}
private:
RNSTool(const RNSTool ©) = delete;
RNSTool(RNSTool &&source) = delete;
RNSTool &operator=(const RNSTool &assign) = delete;
RNSTool &operator=(RNSTool &&assign) = delete;
/**
Generates the pre-computations for the given parameters.
*/
void initialize(std::size_t poly_modulus_degree, const RNSBase &q, const Modulus &t);
// Precomputation for fastbconv_m_tilde, sm_mrq, and fastbconv_m_tilde_sm_mrq
void initialize_behz_extension();
// Precomputation for fast_floor, fastbconv_sk, and inverse_ntt_multiply_t_fast_floor_fastbconv_sk
void initialize_behz_floor();
// Precomputation for decrypt_scale_and_round and decrypt_modt
void initialize_decryption();
// Precomputation for divide_and_round_q_last, divide_and_round_q_last_ntt, and mod_t_and_divide_q_last_ntt
void initialize_mod_switch();
/**
Final step of fastbconv_sk for count coefficients, from the fully reduced conversion conv_q_m_sk of the B
part to q U {m_sk} and the m_sk part input_m_sk. Rows are conv_stride and destination_stride apart; scratch
holds 2 * count values.
*/
void fastbconv_sk_finish(
const std::uint64_t *conv_q_m_sk, std::size_t conv_stride, const std::uint64_t *input_m_sk,
std::uint64_t *destination, std::size_t destination_stride, std::size_t count,
std::uint64_t *scratch) const;
MemoryPoolHandle pool_;
std::size_t coeff_count_ = 0;
Pointer<RNSBase> base_q_;
Pointer<RNSBase> base_B_;
Pointer<RNSBase> base_Bsk_;
Pointer<RNSBase> base_Bsk_m_tilde_;
Pointer<RNSBase> base_t_gamma_;
// NTTTables for Bsk
Pointer<NTTTables> base_Bsk_ntt_tables_;
Modulus m_tilde_;
Modulus m_sk_;
Modulus t_;
Modulus gamma_;
// Set by initialize_behz_extension
// Base converter: q --> Bsk U {m_tilde}, with input scales m_tilde
Pointer<BaseConverter> base_q_to_Bsk_m_tilde_conv_;
// Base converter: q --> Bsk, with input scales m_tilde and output scales m_tilde^(-1)
Pointer<BaseConverter> base_q_to_Bsk_mont_conv_;
// (prod(q)/q_i) * (-prod(q)^(-1)) mod m_tilde
Pointer<std::uint64_t> m_tilde_row_neg_inv_prod_q_;
// -prod(q)^(-1) mod m_tilde
MultiplyUIntModOperand neg_inv_prod_q_mod_m_tilde_;
// m_tilde^(-1) mod Bsk
Pointer<MultiplyUIntModOperand> inv_m_tilde_mod_Bsk_;
// prod(q) * m_tilde^(-1) mod Bsk
Pointer<MultiplyUIntModOperand> prod_q_inv_m_tilde_mod_Bsk_;
// Set by initialize_behz_floor; the scaled converters and inverse NTT scalars only if t is non-zero
// Base converter: q --> Bsk
Pointer<BaseConverter> base_q_to_Bsk_conv_;
// Base converter: B --> q U {m_sk}
Pointer<BaseConverter> base_B_to_q_m_sk_conv_;
// Base converter: q --> Bsk, with input scales prod(q)/q_i and output scales s_j, which are
// prod(q)^(-1) * (prod(B)/b_j)^(-1) for b_j in B and prod(q)^(-1) for m_sk
Pointer<BaseConverter> base_q_to_Bsk_floor_conv_;
// Base converter: B --> q U {m_sk}, with input scales prod(B)/b_j
Pointer<BaseConverter> base_B_to_q_m_sk_unit_conv_;
// Inverse NTT scalars t * (prod(q)/q_i)^(-1) in q and t * s_j in Bsk, followed by the same times 2^64
Pointer<std::uint64_t> intt_scalar_q_;
Pointer<std::uint64_t> intt_scalar_Bsk_;
// prod(q)^(-1) mod Bsk
Pointer<MultiplyUIntModOperand> inv_prod_q_mod_Bsk_;
// prod(B)^(-1) mod m_sk
MultiplyUIntModOperand inv_prod_B_mod_m_sk_;
// prod(B) mod q
Pointer<MultiplyUIntModOperand> prod_B_mod_q_;
// Set by initialize_decryption if t is non-zero
// Base converter: q --> {t, gamma}, with input scales [t * gamma]_q and output scales -prod(q)^(-1)
Pointer<BaseConverter> base_q_to_t_gamma_scaled_conv_;
// Base converter: q --> t
Pointer<BaseConverter> base_q_to_t_conv_;
// gamma^(-1) mod t
MultiplyUIntModOperand inv_gamma_mod_t_;
// Set by initialize_mod_switch
// q[last]^(-1) mod q[i] for i = 0..last-1
Pointer<MultiplyUIntModOperand> inv_q_last_mod_q_;
std::uint64_t inv_q_last_mod_t_ = 1;
std::uint64_t q_last_mod_t_ = 1;
};
#ifdef SEAL_USE_INTEL_HEXL
// HEXL's dyadic products output standard representatives
constexpr bool behz_tensor_product_montgomery_form = false;
#else
// behz_tensor_product outputs c * 2^(-64) mod p (see RNSTool::inverse_ntt_multiply_t_fast_floor_fastbconv_sk)
constexpr bool behz_tensor_product_montgomery_form = true;
#endif
/**
Computes out[k] = sum_{i + j = k} in1[i] * in2[j] in NTT form for one RNS base of moduli with at most
SEAL_MOD_BIT_COUNT_MAX bits, from inputs in [0, 4 * modulus). out is overwritten and fully reduced, times
2^(-64) if behz_tensor_product_montgomery_form is true. Pass the same in1 and in2 to square.
*/
void behz_tensor_product(
ConstPolyIter in1, std::size_t in1_size, ConstPolyIter in2, std::size_t in2_size, ConstModulusIter base,
std::size_t base_size, std::size_t coeff_count, PolyIter out, MemoryPoolHandle pool);
} // namespace util
} // namespace seal