-
Notifications
You must be signed in to change notification settings - Fork 709
/
kswitchkeys.cpp
289 lines (245 loc) · 7.13 KB
/
kswitchkeys.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
// SEALNet
#include "seal/c/kswitchkeys.h"
#include "seal/c/utilities.h"
// SEAL
#include "seal/kswitchkeys.h"
using namespace std;
using namespace seal;
using namespace seal::c;
namespace
{
HRESULT GetKeyFromVector(const vector<PublicKey> &key, uint64_t *count, void **key_list)
{
*count = key.size();
if (nullptr == key_list)
{
// We only wanted the count
return S_OK;
}
auto pkeys = reinterpret_cast<PublicKey **>(key_list);
for (size_t i = 0; i < key.size(); i++)
{
pkeys[i] = new PublicKey(key[i]);
}
return S_OK;
}
} // namespace
// Enables access to private members of seal::PublicKey.
using ph = struct PublicKey::PublicKeyPrivateHelper
{
inline static PublicKey Create(MemoryPoolHandle pool)
{
return PublicKey(pool);
}
};
SEAL_C_FUNC KSwitchKeys_Create1(void **kswitch_keys)
{
IfNullRet(kswitch_keys, E_POINTER);
KSwitchKeys *keys = new KSwitchKeys();
*kswitch_keys = keys;
return S_OK;
}
SEAL_C_FUNC KSwitchKeys_Create2(void *copy, void **kswitch_keys)
{
KSwitchKeys *copyptr = FromVoid<KSwitchKeys>(copy);
IfNullRet(copyptr, E_POINTER);
IfNullRet(kswitch_keys, E_POINTER);
KSwitchKeys *keys = new KSwitchKeys(*copyptr);
*kswitch_keys = keys;
return S_OK;
}
SEAL_C_FUNC KSwitchKeys_Destroy(void *thisptr)
{
KSwitchKeys *keys = FromVoid<KSwitchKeys>(thisptr);
IfNullRet(keys, E_POINTER);
delete keys;
return S_OK;
}
SEAL_C_FUNC KSwitchKeys_Set(void *thisptr, void *assign)
{
KSwitchKeys *keys = FromVoid<KSwitchKeys>(thisptr);
IfNullRet(keys, E_POINTER);
KSwitchKeys *assignptr = FromVoid<KSwitchKeys>(assign);
IfNullRet(assignptr, E_POINTER);
*keys = *assignptr;
return S_OK;
}
SEAL_C_FUNC KSwitchKeys_Size(void *thisptr, uint64_t *size)
{
KSwitchKeys *keys = FromVoid<KSwitchKeys>(thisptr);
IfNullRet(keys, E_POINTER);
IfNullRet(size, E_POINTER);
*size = keys->size();
return S_OK;
}
SEAL_C_FUNC KSwitchKeys_RawSize(void *thisptr, uint64_t *size)
{
KSwitchKeys *keys = FromVoid<KSwitchKeys>(thisptr);
IfNullRet(keys, E_POINTER);
IfNullRet(size, E_POINTER);
*size = keys->data().size();
return S_OK;
}
SEAL_C_FUNC KSwitchKeys_GetKeyList(void *thisptr, uint64_t index, uint64_t *count, void **key_list)
{
KSwitchKeys *keys = FromVoid<KSwitchKeys>(thisptr);
IfNullRet(keys, E_POINTER);
IfNullRet(count, E_POINTER);
auto key = keys->data()[index];
return GetKeyFromVector(key, count, key_list);
}
SEAL_C_FUNC KSwitchKeys_ClearDataAndReserve(void *thisptr, uint64_t size)
{
KSwitchKeys *keys = FromVoid<KSwitchKeys>(thisptr);
IfNullRet(keys, E_POINTER);
keys->data().clear();
keys->data().reserve(size);
return S_OK;
}
SEAL_C_FUNC KSwitchKeys_AddKeyList(void *thisptr, uint64_t count, void **key_list)
{
KSwitchKeys *keys = FromVoid<KSwitchKeys>(thisptr);
IfNullRet(keys, E_POINTER);
IfNullRet(key_list, E_POINTER);
PublicKey **key = reinterpret_cast<PublicKey **>(key_list);
// Don't resize, only reserve
keys->data().emplace_back();
keys->data().back().reserve(count);
for (uint64_t i = 0; i < count; i++)
{
PublicKey *pkey = key[i];
PublicKey new_pkey(ph::Create(keys->pool()));
new_pkey = *pkey;
keys->data().back().emplace_back(move(new_pkey));
}
return S_OK;
}
SEAL_C_FUNC KSwitchKeys_GetParmsId(void *thisptr, uint64_t *parms_id)
{
KSwitchKeys *keys = FromVoid<KSwitchKeys>(thisptr);
IfNullRet(keys, E_POINTER);
IfNullRet(parms_id, E_POINTER);
for (size_t i = 0; i < keys->parms_id().size(); i++)
{
parms_id[i] = keys->parms_id()[i];
}
return S_OK;
}
SEAL_C_FUNC KSwitchKeys_SetParmsId(void *thisptr, uint64_t *parms_id)
{
KSwitchKeys *keys = FromVoid<KSwitchKeys>(thisptr);
IfNullRet(keys, E_POINTER);
IfNullRet(parms_id, E_POINTER);
CopyParmsId(parms_id, keys->parms_id());
return S_OK;
}
SEAL_C_FUNC KSwitchKeys_Pool(void *thisptr, void **pool)
{
KSwitchKeys *keys = FromVoid<KSwitchKeys>(thisptr);
IfNullRet(keys, E_POINTER);
IfNullRet(pool, E_POINTER);
MemoryPoolHandle *handleptr = new MemoryPoolHandle(keys->pool());
*pool = handleptr;
return S_OK;
}
SEAL_C_FUNC KSwitchKeys_SaveSize(void *thisptr, uint8_t compr_mode, int64_t *result)
{
KSwitchKeys *keys = FromVoid<KSwitchKeys>(thisptr);
IfNullRet(keys, E_POINTER);
IfNullRet(result, E_POINTER);
try
{
*result = static_cast<int64_t>(keys->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 KSwitchKeys_Save(void *thisptr, uint8_t *outptr, uint64_t size, uint8_t compr_mode, int64_t *out_bytes)
{
KSwitchKeys *keys = FromVoid<KSwitchKeys>(thisptr);
IfNullRet(keys, E_POINTER);
IfNullRet(outptr, E_POINTER);
IfNullRet(out_bytes, E_POINTER);
try
{
*out_bytes = util::safe_cast<int64_t>(keys->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 KSwitchKeys_UnsafeLoad(void *thisptr, void *context, uint8_t *inptr, uint64_t size, int64_t *in_bytes)
{
const SEALContext *ctx = FromVoid<SEALContext>(context);
IfNullRet(ctx, E_POINTER);
KSwitchKeys *keys = FromVoid<KSwitchKeys>(thisptr);
IfNullRet(keys, E_POINTER);
IfNullRet(inptr, E_POINTER);
IfNullRet(in_bytes, E_POINTER);
try
{
*in_bytes = util::safe_cast<int64_t>(
keys->unsafe_load(*ctx, 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 KSwitchKeys_Load(void *thisptr, void *context, uint8_t *inptr, uint64_t size, int64_t *in_bytes)
{
const SEALContext *ctx = FromVoid<SEALContext>(context);
IfNullRet(ctx, E_POINTER);
KSwitchKeys *keys = FromVoid<KSwitchKeys>(thisptr);
IfNullRet(keys, E_POINTER);
IfNullRet(inptr, E_POINTER);
IfNullRet(in_bytes, E_POINTER);
try
{
*in_bytes = util::safe_cast<int64_t>(
keys->load(*ctx, 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;
}
}