-
Notifications
You must be signed in to change notification settings - Fork 709
/
modulus.cpp
344 lines (290 loc) · 7.85 KB
/
modulus.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
// STD
#include <algorithm>
#include <iterator>
// SEALNet
#include "seal/c/modulus.h"
#include "seal/c/utilities.h"
// SEAL
#include "seal/modulus.h"
using namespace std;
using namespace seal;
using namespace seal::c;
SEAL_C_FUNC Modulus_Create1(uint64_t value, void **small_modulus)
{
IfNullRet(small_modulus, E_POINTER);
try
{
Modulus *sm = new Modulus(value);
*small_modulus = sm;
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
}
SEAL_C_FUNC Modulus_Create2(void *copy, void **small_modulus)
{
Modulus *copypt = FromVoid<Modulus>(copy);
IfNullRet(copypt, E_POINTER);
Modulus *sm = new Modulus(*copypt);
*small_modulus = sm;
return S_OK;
}
SEAL_C_FUNC Modulus_Destroy(void *thisptr)
{
Modulus *sm = FromVoid<Modulus>(thisptr);
IfNullRet(sm, E_POINTER);
delete sm;
return S_OK;
}
SEAL_C_FUNC Modulus_IsZero(void *thisptr, bool *is_zero)
{
Modulus *sm = FromVoid<Modulus>(thisptr);
IfNullRet(sm, E_POINTER);
IfNullRet(is_zero, E_POINTER);
*is_zero = sm->is_zero();
return S_OK;
}
SEAL_C_FUNC Modulus_IsPrime(void *thisptr, bool *is_prime)
{
Modulus *sm = FromVoid<Modulus>(thisptr);
IfNullRet(sm, E_POINTER);
IfNullRet(is_prime, E_POINTER);
*is_prime = sm->is_prime();
return S_OK;
}
SEAL_C_FUNC Modulus_Value(void *thisptr, uint64_t *value)
{
Modulus *sm = FromVoid<Modulus>(thisptr);
IfNullRet(sm, E_POINTER);
IfNullRet(value, E_POINTER);
*value = sm->value();
return S_OK;
}
SEAL_C_FUNC Modulus_BitCount(void *thisptr, int *bit_count)
{
Modulus *sm = FromVoid<Modulus>(thisptr);
IfNullRet(sm, E_POINTER);
IfNullRet(bit_count, E_POINTER);
*bit_count = sm->bit_count();
return S_OK;
}
SEAL_C_FUNC Modulus_UInt64Count(void *thisptr, uint64_t *uint64_count)
{
Modulus *sm = FromVoid<Modulus>(thisptr);
IfNullRet(sm, E_POINTER);
IfNullRet(uint64_count, E_POINTER);
*uint64_count = sm->uint64_count();
return S_OK;
}
SEAL_C_FUNC Modulus_Set1(void *thisptr, void *assign)
{
Modulus *sm = FromVoid<Modulus>(thisptr);
IfNullRet(sm, E_POINTER);
Modulus *assignpt = FromVoid<Modulus>(assign);
IfNullRet(assignpt, E_POINTER);
*sm = *assignpt;
return S_OK;
}
SEAL_C_FUNC Modulus_Set2(void *thisptr, uint64_t value)
{
Modulus *sm = FromVoid<Modulus>(thisptr);
IfNullRet(sm, E_POINTER);
try
{
*sm = value;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
return S_OK;
}
SEAL_C_FUNC Modulus_ConstRatio(void *thisptr, uint64_t length, uint64_t ratio[])
{
Modulus *sm = FromVoid<Modulus>(thisptr);
IfNullRet(sm, E_POINTER);
if (length != 3)
{
return E_INVALIDARG;
}
auto ratio_array = sm->const_ratio();
copy(ratio_array.begin(), ratio_array.end(), ratio);
return S_OK;
}
SEAL_C_FUNC Modulus_Equals1(void *thisptr, void *other, bool *result)
{
Modulus *sm = FromVoid<Modulus>(thisptr);
IfNullRet(sm, E_POINTER);
Modulus *otherpt = FromVoid<Modulus>(other);
IfNullRet(otherpt, E_POINTER);
IfNullRet(result, E_POINTER);
*result = (*sm == *otherpt);
return S_OK;
}
SEAL_C_FUNC Modulus_Equals2(void *thisptr, uint64_t other, bool *result)
{
Modulus *sm = FromVoid<Modulus>(thisptr);
IfNullRet(sm, E_POINTER);
IfNullRet(result, E_POINTER);
*result = (*sm == other);
return S_OK;
}
SEAL_C_FUNC Modulus_SaveSize(void *thisptr, uint8_t compr_mode, int64_t *result)
{
Modulus *sm = FromVoid<Modulus>(thisptr);
IfNullRet(sm, E_POINTER);
IfNullRet(result, E_POINTER);
try
{
*result = static_cast<int64_t>(sm->save_size(static_cast<compr_mode_type>(compr_mode)));
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC Modulus_Save(void *thisptr, uint8_t *outptr, uint64_t size, uint8_t compr_mode, int64_t *out_bytes)
{
Modulus *sm = FromVoid<Modulus>(thisptr);
IfNullRet(sm, E_POINTER);
IfNullRet(outptr, E_POINTER);
IfNullRet(out_bytes, E_POINTER);
try
{
*out_bytes = util::safe_cast<int64_t>(sm->save(
reinterpret_cast<seal_byte *>(outptr), util::safe_cast<size_t>(size),
static_cast<compr_mode_type>(compr_mode)));
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
catch (const runtime_error &)
{
return COR_E_IO;
}
}
SEAL_C_FUNC Modulus_Load(void *thisptr, uint8_t *inptr, uint64_t size, int64_t *in_bytes)
{
Modulus *sm = FromVoid<Modulus>(thisptr);
IfNullRet(sm, E_POINTER);
IfNullRet(inptr, E_POINTER);
IfNullRet(in_bytes, E_POINTER);
try
{
*in_bytes =
util::safe_cast<int64_t>(sm->load(reinterpret_cast<seal_byte *>(inptr), util::safe_cast<size_t>(size)));
return S_OK;
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
catch (const runtime_error &)
{
return COR_E_IO;
}
}
SEAL_C_FUNC Modulus_Reduce(void *thisptr, uint64_t value, uint64_t *result)
{
Modulus *sm = FromVoid<Modulus>(thisptr);
IfNullRet(sm, E_POINTER);
IfNullRet(result, E_POINTER);
try
{
*result = sm->reduce(value);
return S_OK;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
}
SEAL_C_FUNC CoeffModulus_MaxBitCount(uint64_t poly_modulus_degree, int sec_level, int *bit_count)
{
IfNullRet(bit_count, E_POINTER);
sec_level_type security_level = static_cast<sec_level_type>(sec_level);
*bit_count = CoeffModulus::MaxBitCount(poly_modulus_degree, security_level);
return S_OK;
}
SEAL_C_FUNC CoeffModulus_BFVDefault(uint64_t poly_modulus_degree, int sec_level, uint64_t *length, void **coeffs)
{
IfNullRet(length, E_POINTER);
sec_level_type security_level = static_cast<sec_level_type>(sec_level);
vector<Modulus> result;
try
{
result = CoeffModulus::BFVDefault(poly_modulus_degree, security_level);
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
BuildModulusPointers(result, length, coeffs);
return S_OK;
}
SEAL_C_FUNC CoeffModulus_Create1(uint64_t poly_modulus_degree, uint64_t length, int *bit_sizes, void **coeffs)
{
IfNullRet(bit_sizes, E_POINTER);
IfNullRet(coeffs, E_POINTER);
vector<int> bit_sizes_vec;
copy_n(bit_sizes, length, back_inserter(bit_sizes_vec));
vector<Modulus> result;
try
{
result = CoeffModulus::Create(poly_modulus_degree, bit_sizes_vec);
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
BuildModulusPointers(result, &length, coeffs);
return S_OK;
}
SEAL_C_FUNC CoeffModulus_Create2(
uint64_t poly_modulus_degree, uint64_t length, int *bit_sizes, void *plain_modulus, void **coeffs)
{
IfNullRet(bit_sizes, E_POINTER);
IfNullRet(coeffs, E_POINTER);
vector<int> bit_sizes_vec;
copy_n(bit_sizes, length, back_inserter(bit_sizes_vec));
vector<Modulus> result;
Modulus *modulus = FromVoid<Modulus>(plain_modulus);
IfNullRet(modulus, E_POINTER);
try
{
result = CoeffModulus::Create(poly_modulus_degree, *modulus, bit_sizes_vec);
}
catch (const invalid_argument &)
{
return E_INVALIDARG;
}
catch (const logic_error &)
{
return COR_E_INVALIDOPERATION;
}
BuildModulusPointers(result, &length, coeffs);
return S_OK;
}