-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathAsciiString_impl.h
More file actions
330 lines (298 loc) · 11.3 KB
/
Copy pathAsciiString_impl.h
File metadata and controls
330 lines (298 loc) · 11.3 KB
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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <fmt/format.h>
#include <stdexcept>
#include "./AsciiString.h" // @manual
#include "./Util.h" // @manual
#pragma once
namespace fbpcf::edit_distance {
template <int maxWidth, bool isSecret, int schedulerId, bool usingBatch>
AsciiString<maxWidth, isSecret, schedulerId, usingBatch>::AsciiString(
const StringType& data) {
static_assert(!isSecret, "Private value must not be created public input");
if constexpr (usingBatch) {
batchSize_ = data.size();
knownSize_.reserve(batchSize_);
for (size_t i = 0; i < batchSize_; i++) {
if (data[i].size() > maxWidth) {
throw std::runtime_error(fmt::format(
"Input value is too large. Maximum string width is %d, was given %d",
maxWidth,
data[i].size()));
}
knownSize_.push_back(data[i].size());
}
for (size_t i = 0; i < maxWidth; i++) {
std::vector<char> chars(batchSize_);
for (size_t j = 0; j < batchSize_; j++) {
chars[j] = i < data[j].size() ? data[j][i] : 0;
}
data_[i] = frontend::Int<true, 8, false, schedulerId, usingBatch>(chars);
}
} else {
if (data.size() > maxWidth) {
throw std::runtime_error(fmt::format(
"Input value is too large. Maximum string width is %d, was given %d",
maxWidth,
data.size()));
}
knownSize_ = data.size();
for (size_t i = 0; i < data.size(); i++) {
data_[i] =
frontend::Int<true, 8, false, schedulerId, usingBatch>(data[i]);
}
for (size_t i = data.size(); i < maxWidth; i++) {
data_[i] = frontend::Int<true, 8, false, schedulerId, usingBatch>(0);
}
}
}
template <int maxWidth, bool isSecret, int schedulerId, bool usingBatch>
AsciiString<maxWidth, isSecret, schedulerId, usingBatch>::AsciiString(
const StringType& data,
int partyId) {
static_assert(isSecret, "Public value must not be created secret input");
if constexpr (usingBatch) {
batchSize_ = data.size();
for (size_t j = 0; j < batchSize_; j++) {
if (data[j].size() > maxWidth) {
throw std::runtime_error(fmt::format(
"Input value is too large. Maximum string width is %d, was given %d",
maxWidth,
data[j].size()));
}
}
for (size_t i = 0; i < maxWidth; i++) {
std::vector<char> chars(batchSize_);
for (size_t j = 0; j < batchSize_; j++) {
chars[j] = i < data[j].size() ? data[j][i] : 0;
}
data_[i] =
frontend::Int<true, 8, true, schedulerId, usingBatch>(chars, partyId);
}
} else {
if (data.size() > maxWidth) {
throw std::runtime_error(fmt::format(
"Input value is too large. Maximum string width is %d, was given %d",
maxWidth,
data.size()));
}
for (size_t i = 0; i < data.size(); i++) {
data_[i] = frontend::Int<true, 8, true, schedulerId, usingBatch>(
data[i], partyId);
}
for (size_t i = data.size(); i < maxWidth; i++) {
data_[i] =
frontend::Int<true, 8, true, schedulerId, usingBatch>(0, partyId);
}
}
}
template <int maxWidth, bool isSecret, int schedulerId, bool usingBatch>
AsciiString<maxWidth, isSecret, schedulerId, usingBatch>::AsciiString(
ExtractedAsciiString&& extractedAsciiString) {
static_assert(isSecret, "Can only recover shared secrets");
for (size_t i = 0; i < maxWidth; i++) {
data_[i] = frontend::Int<true, 8, isSecret, schedulerId, usingBatch>(
std::move(extractedAsciiString[i]));
}
}
template <int maxWidth, bool isSecret, int schedulerId, bool usingBatch>
AsciiString<maxWidth, false, schedulerId, usingBatch>
AsciiString<maxWidth, isSecret, schedulerId, usingBatch>::openToParty(
int partyId) const {
static_assert(isSecret, "No need to open a public value");
AsciiString<maxWidth, false, schedulerId, usingBatch> rst;
for (size_t i = 0; i < maxWidth; i++) {
rst.data_[i] = data_.at(i).openToParty(partyId);
}
if constexpr (usingBatch) {
std::array<std::vector<char>, maxWidth> values;
for (int i = 0; i < maxWidth; i++) {
values[i] = convertLongsToChar(rst.data_[i].getValue());
}
size_t batchSize = values[0].size();
SizeType sizes(batchSize);
for (int wordIndex = 0; wordIndex < batchSize; wordIndex++) {
int letterIndex = 0;
while (letterIndex < maxWidth && values[letterIndex][wordIndex] != 0) {
letterIndex++;
}
sizes[wordIndex] = letterIndex;
}
rst.knownSize_ = sizes;
} else {
int letterIndex = 0;
while (letterIndex < maxWidth && rst.data_[letterIndex].getValue() != 0) {
letterIndex++;
}
rst.knownSize_ = letterIndex;
}
return rst;
}
template <int maxWidth, bool isSecret, int schedulerId, bool usingBatch>
typename AsciiString<maxWidth, isSecret, schedulerId, usingBatch>::StringType
AsciiString<maxWidth, isSecret, schedulerId, usingBatch>::getValue() const {
static_assert(!isSecret, "Can't get value on secret wires");
if constexpr (usingBatch) {
std::vector<char> chars = convertLongsToChar(data_[0].getValue());
size_t batchSize = chars.size();
std::vector<std::string> rst(batchSize);
for (size_t i = 0; i < batchSize; i++) {
rst[i].reserve(maxWidth);
if (chars[i] != 0) {
rst[i].push_back(chars[i]);
}
}
for (size_t j = 1; j < maxWidth; j++) {
chars = convertLongsToChar(data_[j].getValue());
for (size_t i = 0; i < batchSize; i++) {
if (chars[i] != 0) {
rst[i].push_back(chars[i]);
}
}
}
return rst;
} else {
std::string rst = "";
for (size_t i = 0; i < maxWidth; i++) {
char c = data_[i].getValue();
if (c != 0) {
rst.push_back(c);
}
}
return rst;
}
}
template <int maxWidth, bool isSecret, int schedulerId, bool usingBatch>
typename AsciiString<maxWidth, true, schedulerId, usingBatch>::
ExtractedAsciiString
AsciiString<maxWidth, isSecret, schedulerId, usingBatch>::
extractAsciiStringShare() const {
static_assert(isSecret, "No need to extract a public value.");
ExtractedAsciiString rst;
for (size_t i = 0; i < maxWidth; i++) {
rst[i] = data_.at(i).extractIntShare();
}
return rst;
}
template <int maxWidth, bool isSecret, int schedulerId, bool usingBatch>
template <int sizeWidth>
frontend::Int<false, sizeWidth, true, schedulerId, usingBatch>
AsciiString<maxWidth, isSecret, schedulerId, usingBatch>::privateSize() const {
static_assert(isSecret, "Only private values have an unknown size");
static_assert(
maxWidth >> (sizeWidth - 1) == 0,
"Private int width must be large enough for string width");
static_assert(maxWidth > 0, "String can not be always empty");
frontend::Int<false, sizeWidth, true, schedulerId, usingBatch> rst;
if constexpr (usingBatch) {
frontend::Int<false, sizeWidth, false, schedulerId, usingBatch> zeroIndex =
createPublicBatchConstant<
frontend::Int<false, sizeWidth, false, schedulerId, usingBatch>,
uint64_t>(0, batchSize_);
frontend::Int<false, sizeWidth, false, schedulerId, usingBatch> one =
createPublicBatchConstant<
frontend::Int<false, sizeWidth, false, schedulerId, usingBatch>,
uint64_t>(1, batchSize_);
frontend::Int<true, 8, false, schedulerId, usingBatch> zeroChar =
createPublicBatchConstant<
frontend::Int<true, 8, false, schedulerId, usingBatch>,
int64_t>(0, batchSize_);
rst = one.mux(data_[0] == zeroChar, zeroIndex);
for (size_t i = 1; i < maxWidth; i++) {
rst = (rst + one).mux(data_[i] == zeroChar, rst);
}
} else {
frontend::Int<false, sizeWidth, false, schedulerId, usingBatch> zeroIndex(
0U);
frontend::Int<false, sizeWidth, false, schedulerId, usingBatch> one(1U);
frontend::Int<true, 8, false, schedulerId, usingBatch> zeroChar(0);
rst = one.mux(data_[0] == zeroChar, zeroIndex);
for (size_t i = 1; i < maxWidth; i++) {
rst = (rst + one).mux(data_[i] == zeroChar, rst);
}
}
return rst;
}
template <int maxWidth, bool isSecret, int schedulerId, bool usingBatch>
AsciiString<maxWidth, isSecret, schedulerId, usingBatch>
AsciiString<maxWidth, isSecret, schedulerId, usingBatch>::toUpperCase() const {
if constexpr (usingBatch) {
AsciiString<maxWidth, isSecret, schedulerId, usingBatch> rst;
std::vector<int64_t> v1(batchSize_, 'a');
std::vector<int64_t> v2(batchSize_, 'z');
std::vector<int64_t> v3(batchSize_, 0);
std::vector<int64_t> v4(batchSize_, 'A' - 'a');
frontend::Int<true, 8, false, schedulerId, usingBatch> a(v1);
frontend::Int<true, 8, false, schedulerId, usingBatch> z(v2);
frontend::Int<true, 8, false, schedulerId, usingBatch> zero(v3);
frontend::Int<true, 8, false, schedulerId, usingBatch> upper(v4);
for (int i = 0; i < maxWidth; i++) {
auto isLower = data_[i] >= a & data_[i] <= z;
rst.data_[i] = data_[i] + zero.mux(isLower, upper);
}
return rst;
} else {
AsciiString<maxWidth, isSecret, schedulerId, usingBatch> rst;
frontend::Int<true, 8, false, schedulerId, usingBatch> a('a');
frontend::Int<true, 8, false, schedulerId, usingBatch> z('z');
frontend::Int<true, 8, false, schedulerId, usingBatch> zero(0);
frontend::Int<true, 8, false, schedulerId, usingBatch> upper('A' - 'a');
for (size_t i = 0; i < maxWidth; i++) {
auto isLower = data_[i] >= a & data_[i] <= z;
rst.data_[i] = data_[i] + zero.mux(isLower, upper);
}
return rst;
}
}
template <int maxWidth, bool isSecret, int schedulerId, bool usingBatch>
AsciiString<maxWidth, isSecret, schedulerId, usingBatch>
AsciiString<maxWidth, isSecret, schedulerId, usingBatch>::toLowerCase() const {
throw std::runtime_error("Unimplemented");
}
template <int maxWidth, bool isSecret, int schedulerId, bool usingBatch>
template <bool isSecretOther, int otherMaxWidth>
AsciiString<
maxWidth + otherMaxWidth,
isSecret || isSecretOther,
schedulerId,
usingBatch>
AsciiString<maxWidth, isSecret, schedulerId, usingBatch>::concat(
const AsciiString<otherMaxWidth, isSecretOther, schedulerId, usingBatch>&
other) const {}
template <int maxWidth, bool isSecret, int schedulerId, bool usingBatch>
template <bool isSecretChoice, bool isSecretOther>
AsciiString<
maxWidth,
isSecret || isSecretChoice || isSecretOther,
schedulerId,
usingBatch>
AsciiString<maxWidth, isSecret, schedulerId, usingBatch>::mux(
const frontend::Bit<isSecretChoice, schedulerId, usingBatch>& choice,
const AsciiString<maxWidth, isSecretOther, schedulerId, usingBatch>& other)
const {
AsciiString<
maxWidth,
isSecret || isSecretChoice || isSecretOther,
schedulerId,
usingBatch>
rst;
for (size_t i = 0; i < maxWidth; i++) {
rst.data_[i] = data_[i].mux(choice, other.data_[i]);
}
return rst;
}
template <int maxWidth, bool isSecret, int schedulerId, bool usingBatch>
std::vector<char>
AsciiString<maxWidth, isSecret, schedulerId, usingBatch>::convertLongsToChar(
std::vector<int64_t> values) const {
std::vector<char> rst(values.size());
for (size_t i = 0; i < values.size(); i++) {
rst[i] = values[i];
}
return rst;
}
} // namespace fbpcf::edit_distance