forked from kimlaine/bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
uintarithsmallmod.h
332 lines (296 loc) · 10.8 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
#include <cstdint>
#include <type_traits>
#include "seal/smallmodulus.h"
#include "seal/util/defines.h"
#include "seal/util/pointer.h"
#include "seal/util/numth.h"
#include "seal/util/uintarith.h"
namespace seal
{
namespace util
{
SEAL_NODISCARD inline std::uint64_t increment_uint_mod(
std::uint64_t operand, const SmallModulus &modulus)
{
#ifdef SEAL_DEBUG
if (modulus.is_zero())
{
throw std::invalid_argument("modulus");
}
if (operand >= modulus.value())
{
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())));
}
SEAL_NODISCARD inline std::uint64_t decrement_uint_mod(
std::uint64_t operand, const SmallModulus &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 = (operand == 0);
return operand - 1 + (modulus.value() &
static_cast<std::uint64_t>(-carry));
}
SEAL_NODISCARD inline std::uint64_t negate_uint_mod(
std::uint64_t operand, const SmallModulus &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 = (operand != 0);
return (modulus.value() - operand)
& static_cast<std::uint64_t>(-non_zero);
}
SEAL_NODISCARD inline std::uint64_t div2_uint_mod(
std::uint64_t operand, const SmallModulus &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;
int64_t 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;
}
SEAL_NODISCARD inline std::uint64_t add_uint_uint_mod(
std::uint64_t operand1, std::uint64_t operand2,
const SmallModulus &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
// Sum of operands modulo SmallModulus can never wrap around 2^64
operand1 += operand2;
return operand1 - (modulus.value() & static_cast<std::uint64_t>(
-static_cast<std::int64_t>(operand1 >= modulus.value())));
}
SEAL_NODISCARD inline std::uint64_t sub_uint_uint_mod(
std::uint64_t operand1, std::uint64_t operand2,
const SmallModulus &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 = SEAL_SUB_BORROW_UINT64(operand1, operand2, 0, &temp);
return static_cast<std::uint64_t>(temp) +
(modulus.value() & static_cast<std::uint64_t>(-borrow));
}
template<typename T, typename = std::enable_if<is_uint64_v<T>>>
SEAL_NODISCARD inline std::uint64_t barrett_reduce_128(
const T *input, const SmallModulus &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, 0, &tmp1);
// Round 2
multiply_uint64(input[1], const_ratio[0], tmp2);
carry = tmp2[1] + add_uint64(tmp1, tmp2[0], 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 static_cast<std::uint64_t>(tmp3) -
(modulus.value() & static_cast<std::uint64_t>(
-static_cast<std::int64_t>(tmp3 >= modulus.value())));
}
template<typename T, typename = std::enable_if<is_uint64_v<T>>>
SEAL_NODISCARD inline std::uint64_t barrett_reduce_63(
T input, const SmallModulus &modulus)
{
#ifdef SEAL_DEBUG
if (modulus.is_zero())
{
throw std::invalid_argument("modulus");
}
if (input >> 63)
{
throw std::invalid_argument("input");
}
#endif
// Reduces input using base 2^64 Barrett reduction
// input must be at most 63 bits
unsigned long long tmp[2];
const std::uint64_t *const_ratio = modulus.const_ratio().data();
multiply_uint64(input, const_ratio[1], tmp);
// Barrett subtraction
tmp[0] = input - tmp[1] * modulus.value();
// One more subtraction is enough
return static_cast<std::uint64_t>(tmp[0]) -
(modulus.value() & static_cast<std::uint64_t>(
-static_cast<std::int64_t>(tmp[0] >= modulus.value())));
}
SEAL_NODISCARD inline std::uint64_t multiply_uint_uint_mod(
std::uint64_t operand1, std::uint64_t operand2,
const SmallModulus &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);
}
inline void modulo_uint_inplace(
std::uint64_t *value, std::size_t value_uint64_count,
const SmallModulus &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)
{
value[0] %= modulus.value();
return;
}
// 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;
}
}
SEAL_NODISCARD inline std::uint64_t modulo_uint(
const std::uint64_t *value, std::size_t value_uint64_count,
const SmallModulus &modulus, MemoryPool &pool)
{
#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
return *value % modulus.value();
}
auto value_copy(allocate_uint(value_uint64_count, pool));
set_uint_uint(value, value_uint64_count, value_copy.get());
// Starting from the top, reduce always 128-bit blocks
for (std::size_t i = value_uint64_count - 1; i--; )
{
value_copy[i] = barrett_reduce_128(value_copy.get() + i, modulus);
}
return value_copy[0];
}
inline bool try_invert_uint_mod(
std::uint64_t operand, const SmallModulus &modulus,
std::uint64_t &result)
{
return try_mod_inverse(operand, modulus.value(), result);
}
bool is_primitive_root(
std::uint64_t root, std::uint64_t degree,
const SmallModulus &prime_modulus);
// Try to find a primitive degree-th root of unity modulo small prime
// modulus, where degree must be a power of two.
bool try_primitive_root(
std::uint64_t degree, const SmallModulus &prime_modulus,
std::uint64_t &destination);
// Try to find the smallest (as integer) primitive degree-th root of
// unity modulo small prime modulus, where degree must be a power of two.
bool try_minimal_primitive_root(
std::uint64_t degree, const SmallModulus &prime_modulus,
std::uint64_t &destination);
SEAL_NODISCARD std::uint64_t exponentiate_uint_mod(
std::uint64_t operand, std::uint64_t exponent,
const SmallModulus &modulus);
void divide_uint_uint_mod_inplace(
std::uint64_t *numerator, const SmallModulus &modulus,
std::size_t uint64_count, std::uint64_t *quotient,
MemoryPool &pool);
SEAL_NODISCARD std::uint64_t galois_elt_from_step(
int steps, std::size_t coeff_count);
}
}