-
Notifications
You must be signed in to change notification settings - Fork 709
/
uintarithsmallmod.h
455 lines (417 loc) · 15.9 KB
/
uintarithsmallmod.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
#include "seal/modulus.h"
#include "seal/util/defines.h"
#include "seal/util/numth.h"
#include "seal/util/pointer.h"
#include "seal/util/uintarith.h"
#include <cstdint>
#include <type_traits>
namespace seal
{
namespace util
{
/**
Returns (operand++) mod modulus.
Correctness: operand must be at most (2 * modulus -2) for correctness.
*/
SEAL_NODISCARD inline std::uint64_t increment_uint_mod(std::uint64_t operand, const Modulus &modulus)
{
#ifdef SEAL_DEBUG
if (modulus.is_zero())
{
throw std::invalid_argument("modulus");
}
if (operand > (modulus.value() - 1) << 1)
{
throw std::out_of_range("operand");
}
#endif
operand++;
return operand - (modulus.value() &
static_cast<std::uint64_t>(-static_cast<std::int64_t>(operand >= modulus.value())));
}
/**
Returns (operand--) mod modulus.
@param[in] operand Must be at most (modulus - 1).
*/
SEAL_NODISCARD inline std::uint64_t decrement_uint_mod(std::uint64_t operand, const Modulus &modulus)
{
#ifdef SEAL_DEBUG
if (modulus.is_zero())
{
throw std::invalid_argument("modulus");
}
if (operand >= modulus.value())
{
throw std::out_of_range("operand");
}
#endif
std::int64_t carry = static_cast<std::int64_t>(operand == 0);
return operand - 1 + (modulus.value() & static_cast<std::uint64_t>(-carry));
}
/**
Returns (-operand) mod modulus.
Correctness: operand must be at most modulus for correctness.
*/
SEAL_NODISCARD inline std::uint64_t negate_uint_mod(std::uint64_t operand, const Modulus &modulus)
{
#ifdef SEAL_DEBUG
if (modulus.is_zero())
{
throw std::invalid_argument("modulus");
}
if (operand >= modulus.value())
{
throw std::out_of_range("operand");
}
#endif
std::int64_t non_zero = static_cast<std::int64_t>(operand != 0);
return (modulus.value() - operand) & static_cast<std::uint64_t>(-non_zero);
}
/**
Returns (operand * inv(2)) mod modulus.
Correctness: operand must be even and at most (2 * modulus - 2) or odd and at most (modulus - 2).
@param[in] operand Should be at most (modulus - 1).
*/
SEAL_NODISCARD inline std::uint64_t div2_uint_mod(std::uint64_t operand, const Modulus &modulus)
{
#ifdef SEAL_DEBUG
if (modulus.is_zero())
{
throw std::invalid_argument("modulus");
}
if (operand >= modulus.value())
{
throw std::out_of_range("operand");
}
#endif
if (operand & 1)
{
unsigned long long temp;
unsigned char carry = add_uint64(operand, modulus.value(), 0, &temp);
operand = temp >> 1;
if (carry)
{
return operand | (std::uint64_t(1) << (bits_per_uint64 - 1));
}
return operand;
}
return operand >> 1;
}
/**
Returns (operand1 + operand2) mod modulus.
Correctness: (operand1 + operand2) must be at most (2 * modulus - 1).
*/
SEAL_NODISCARD inline std::uint64_t add_uint_mod(
std::uint64_t operand1, std::uint64_t operand2, const Modulus &modulus)
{
#ifdef SEAL_DEBUG
if (modulus.is_zero())
{
throw std::invalid_argument("modulus");
}
if (operand1 + operand2 >= modulus.value() << 1)
{
throw std::out_of_range("operands");
}
#endif
// Sum of operands modulo Modulus can never wrap around 2^64
operand1 += operand2;
return SEAL_COND_SELECT(operand1 >= modulus.value(), operand1 - modulus.value(), operand1);
}
/**
Returns (operand1 - operand2) mod modulus.
Correctness: (operand1 - operand2) must be at most (modulus - 1) and at least (-modulus).
@param[in] operand1 Should be at most (modulus - 1).
@param[in] operand2 Should be at most (modulus - 1).
*/
SEAL_NODISCARD inline std::uint64_t sub_uint_mod(
std::uint64_t operand1, std::uint64_t operand2, const Modulus &modulus)
{
#ifdef SEAL_DEBUG
if (modulus.is_zero())
{
throw std::invalid_argument("modulus");
}
if (operand1 >= modulus.value())
{
throw std::out_of_range("operand1");
}
if (operand2 >= modulus.value())
{
throw std::out_of_range("operand2");
}
#endif
unsigned long long temp;
std::int64_t borrow = static_cast<std::int64_t>(SEAL_SUB_BORROW_UINT64(operand1, operand2, 0, &temp));
return static_cast<std::uint64_t>(temp) + (modulus.value() & static_cast<std::uint64_t>(-borrow));
}
/**
Returns input mod modulus. This is not standard Barrett reduction.
Correctness: modulus must be at most 63-bit.
@param[in] input Should be at most 128-bit.
*/
template <typename T, typename = std::enable_if_t<is_uint64_v<T>>>
SEAL_NODISCARD inline std::uint64_t barrett_reduce_128(const T *input, const Modulus &modulus)
{
#ifdef SEAL_DEBUG
if (!input)
{
throw std::invalid_argument("input");
}
if (modulus.is_zero())
{
throw std::invalid_argument("modulus");
}
#endif
// Reduces input using base 2^64 Barrett reduction
// input allocation size must be 128 bits
unsigned long long tmp1, tmp2[2], tmp3, carry;
const std::uint64_t *const_ratio = modulus.const_ratio().data();
// Multiply input and const_ratio
// Round 1
multiply_uint64_hw64(input[0], const_ratio[0], &carry);
multiply_uint64(input[0], const_ratio[1], tmp2);
tmp3 = tmp2[1] + add_uint64(tmp2[0], carry, &tmp1);
// Round 2
multiply_uint64(input[1], const_ratio[0], tmp2);
carry = tmp2[1] + add_uint64(tmp1, tmp2[0], &tmp1);
// This is all we care about
tmp1 = input[1] * const_ratio[1] + tmp3 + carry;
// Barrett subtraction
tmp3 = input[0] - tmp1 * modulus.value();
// One more subtraction is enough
return SEAL_COND_SELECT(tmp3 >= modulus.value(), tmp3 - modulus.value(), tmp3);
}
/**
Returns input mod modulus. This is not standard Barrett reduction.
Correctness: modulus must be at most 63-bit.
*/
template <typename T, typename = std::enable_if_t<is_uint64_v<T>>>
SEAL_NODISCARD inline std::uint64_t barrett_reduce_64(T input, const Modulus &modulus)
{
#ifdef SEAL_DEBUG
if (modulus.is_zero())
{
throw std::invalid_argument("modulus");
}
#endif
// Reduces input using base 2^64 Barrett reduction
// floor(2^64 / mod) == floor( floor(2^128 / mod) )
unsigned long long tmp[2];
const std::uint64_t *const_ratio = modulus.const_ratio().data();
multiply_uint64_hw64(input, const_ratio[1], tmp + 1);
// Barrett subtraction
tmp[0] = input - tmp[1] * modulus.value();
// One more subtraction is enough
return SEAL_COND_SELECT(tmp[0] >= modulus.value(), tmp[0] - modulus.value(), tmp[0]);
}
/**
Returns (operand1 * operand2) mod modulus.
Correctness: Follows the condition of barrett_reduce_128.
*/
SEAL_NODISCARD inline std::uint64_t multiply_uint_mod(
std::uint64_t operand1, std::uint64_t operand2, const Modulus &modulus)
{
#ifdef SEAL_DEBUG
if (modulus.is_zero())
{
throw std::invalid_argument("modulus");
}
#endif
unsigned long long z[2];
multiply_uint64(operand1, operand2, z);
return barrett_reduce_128(z, modulus);
}
/**
This struct contains a operand and a precomputed quotient: (operand << 64) / modulus, for a specific modulus.
When passed to multiply_uint_mod, a faster variant of Barrett reduction will be performed.
Operand must be less than modulus.
*/
struct MultiplyUIntModOperand
{
std::uint64_t operand;
std::uint64_t quotient;
void set_quotient(const Modulus &modulus)
{
#ifdef SEAL_DEBUG
if (operand >= modulus.value())
{
throw std::invalid_argument("input must be less than modulus");
}
#endif
std::uint64_t wide_quotient[2]{ 0, 0 };
std::uint64_t wide_coeff[2]{ 0, operand };
divide_uint128_inplace(wide_coeff, modulus.value(), wide_quotient);
quotient = wide_quotient[0];
}
void set(std::uint64_t new_operand, const Modulus &modulus)
{
#ifdef SEAL_DEBUG
if (new_operand >= modulus.value())
{
throw std::invalid_argument("input must be less than modulus");
}
#endif
operand = new_operand;
set_quotient(modulus);
}
};
/**
Returns x * y mod modulus.
This is a highly-optimized variant of Barrett reduction.
Correctness: modulus should be at most 63-bit, and y must be less than modulus.
*/
SEAL_NODISCARD inline std::uint64_t multiply_uint_mod(
std::uint64_t x, MultiplyUIntModOperand y, const Modulus &modulus)
{
#ifdef SEAL_DEBUG
if (y.operand >= modulus.value())
{
throw std::invalid_argument("operand y must be less than modulus");
}
#endif
unsigned long long tmp1, tmp2;
const std::uint64_t p = modulus.value();
multiply_uint64_hw64(x, y.quotient, &tmp1);
tmp2 = y.operand * x - tmp1 * p;
return SEAL_COND_SELECT(tmp2 >= p, tmp2 - p, tmp2);
}
/**
Returns x * y mod modulus or x * y mod modulus + modulus.
This is a highly-optimized variant of Barrett reduction and reduce to [0, 2 * modulus - 1].
Correctness: modulus should be at most 63-bit, and y must be less than modulus.
*/
SEAL_NODISCARD inline std::uint64_t multiply_uint_mod_lazy(
std::uint64_t x, MultiplyUIntModOperand y, const Modulus &modulus)
{
#ifdef SEAL_DEBUG
if (y.operand >= modulus.value())
{
throw std::invalid_argument("operand y must be less than modulus");
}
#endif
unsigned long long tmp1;
const std::uint64_t p = modulus.value();
multiply_uint64_hw64(x, y.quotient, &tmp1);
return y.operand * x - tmp1 * p;
}
/**
Returns value[0] = value mod modulus.
Correctness: Follows the condition of barrett_reduce_128.
*/
inline void modulo_uint_inplace(std::uint64_t *value, std::size_t value_uint64_count, const Modulus &modulus)
{
#ifdef SEAL_DEBUG
if (!value)
{
throw std::invalid_argument("value");
}
if (!value_uint64_count)
{
throw std::invalid_argument("value_uint64_count");
}
#endif
if (value_uint64_count == 1)
{
if (*value < modulus.value())
{
return;
}
else
{
*value = barrett_reduce_64(*value, modulus);
}
}
// Starting from the top, reduce always 128-bit blocks
for (std::size_t i = value_uint64_count - 1; i--;)
{
value[i] = barrett_reduce_128(value + i, modulus);
value[i + 1] = 0;
}
}
/**
Returns value mod modulus.
Correctness: Follows the condition of barrett_reduce_128.
*/
SEAL_NODISCARD inline std::uint64_t modulo_uint(
const std::uint64_t *value, std::size_t value_uint64_count, const Modulus &modulus)
{
#ifdef SEAL_DEBUG
if (!value && value_uint64_count)
{
throw std::invalid_argument("value");
}
if (!value_uint64_count)
{
throw std::invalid_argument("value_uint64_count");
}
#endif
if (value_uint64_count == 1)
{
// If value < modulus no operation is needed
if (*value < modulus.value())
return *value;
else
return barrett_reduce_64(*value, modulus);
}
// Temporary space for 128-bit reductions
uint64_t temp[2]{ 0, value[value_uint64_count - 1] };
for (size_t k = value_uint64_count - 1; k--;)
{
temp[0] = value[k];
temp[1] = barrett_reduce_128(temp, modulus);
}
// Save the result modulo i-th prime
return temp[1];
}
/**
Returns (operand1 * operand2) + operand3 mod modulus.
Correctness: Follows the condition of barrett_reduce_128.
*/
inline std::uint64_t multiply_add_uint_mod(
std::uint64_t operand1, std::uint64_t operand2, std::uint64_t operand3, const Modulus &modulus)
{
// Lazy reduction
unsigned long long temp[2];
multiply_uint64(operand1, operand2, temp);
temp[1] += add_uint64(temp[0], operand3, temp);
return barrett_reduce_128(temp, modulus);
}
/**
Returns (operand1 * operand2) + operand3 mod modulus.
Correctness: Follows the condition of multiply_uint_mod.
*/
inline std::uint64_t multiply_add_uint_mod(
std::uint64_t operand1, MultiplyUIntModOperand operand2, std::uint64_t operand3, const Modulus &modulus)
{
return add_uint_mod(
multiply_uint_mod(operand1, operand2, modulus), barrett_reduce_64(operand3, modulus), modulus);
}
inline bool try_invert_uint_mod(std::uint64_t operand, const Modulus &modulus, std::uint64_t &result)
{
return try_invert_uint_mod(operand, modulus.value(), result);
}
/**
Returns operand^exponent mod modulus.
Correctness: Follows the condition of barrett_reduce_128.
*/
SEAL_NODISCARD std::uint64_t exponentiate_uint_mod(
std::uint64_t operand, std::uint64_t exponent, const Modulus &modulus);
/**
Computes numerator = numerator mod modulus, quotient = numerator / modulus.
Correctness: Follows the condition of barrett_reduce_128.
*/
void divide_uint_mod_inplace(
std::uint64_t *numerator, const Modulus &modulus, std::size_t uint64_count, std::uint64_t *quotient,
MemoryPool &pool);
/**
Computes <operand1, operand2> mod modulus.
Correctness: Follows the condition of barrett_reduce_128.
*/
SEAL_NODISCARD std::uint64_t dot_product_mod(
const std::uint64_t *operand1, const std::uint64_t *operand2, std::size_t count, const Modulus &modulus);
} // namespace util
} // namespace seal