-
Notifications
You must be signed in to change notification settings - Fork 709
/
rns.h
403 lines (293 loc) · 12.2 KB
/
rns.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
// 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();
}
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;
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:
BaseConverter(const BaseConverter ©) = delete;
BaseConverter(BaseConverter &&source) = delete;
BaseConverter &operator=(const BaseConverter &assign) = delete;
BaseConverter &operator=(BaseConverter &&assign) = delete;
void initialize();
MemoryPoolHandle pool_;
RNSBase ibase_;
RNSBase obase_;
Pointer<Pointer<std::uint64_t>> base_change_matrix_;
};
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;
/**
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;
/**
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);
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_;
// Base converter: q --> B_sk
Pointer<BaseConverter> base_q_to_Bsk_conv_;
// Base converter: q --> {m_tilde}
Pointer<BaseConverter> base_q_to_m_tilde_conv_;
// Base converter: B --> q
Pointer<BaseConverter> base_B_to_q_conv_;
// Base converter: B --> {m_sk}
Pointer<BaseConverter> base_B_to_m_sk_conv_;
// Base converter: q --> {t, gamma}
Pointer<BaseConverter> base_q_to_t_gamma_conv_;
// Base converter: q --> t
Pointer<BaseConverter> base_q_to_t_conv_;
// prod(q)^(-1) mod Bsk
Pointer<MultiplyUIntModOperand> inv_prod_q_mod_Bsk_;
// prod(q)^(-1) mod m_tilde
MultiplyUIntModOperand neg_inv_prod_q_mod_m_tilde_;
// prod(B)^(-1) mod m_sk
MultiplyUIntModOperand inv_prod_B_mod_m_sk_;
// gamma^(-1) mod t
MultiplyUIntModOperand inv_gamma_mod_t_;
// prod(B) mod q
Pointer<std::uint64_t> prod_B_mod_q_;
// m_tilde^(-1) mod Bsk
Pointer<MultiplyUIntModOperand> inv_m_tilde_mod_Bsk_;
// prod(q) mod Bsk
Pointer<std::uint64_t> prod_q_mod_Bsk_;
// -prod(q)^(-1) mod {t, gamma}
Pointer<MultiplyUIntModOperand> neg_inv_q_mod_t_gamma_;
// prod({t, gamma}) mod q
Pointer<MultiplyUIntModOperand> prod_t_gamma_mod_q_;
// q[last]^(-1) mod q[i] for i = 0..last-1
Pointer<MultiplyUIntModOperand> inv_q_last_mod_q_;
// NTTTables for Bsk
Pointer<NTTTables> base_Bsk_ntt_tables_;
Modulus m_tilde_;
Modulus m_sk_;
Modulus t_;
Modulus gamma_;
std::uint64_t inv_q_last_mod_t_ = 1;
std::uint64_t q_last_mod_t_ = 1;
};
} // namespace util
} // namespace seal