forked from google/shell-encryption
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranscription_test.cc
479 lines (409 loc) · 17.2 KB
/
transcription_test.cc
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "transcription.h"
#include <cstdint>
#include <random>
#include <vector>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "absl/numeric/int128.h"
#include "integral_types.h"
#include "status_macros.h"
#include "testing/status_matchers.h"
#include "testing/status_testing.h"
namespace rlwe {
namespace {
using ::rlwe::testing::StatusIs;
using ::testing::HasSubstr;
template <typename Int>
class TranscribeTest : public ::testing::Test {};
const int kLength = 10;
using MyTypes = ::testing::Types<Uint8, Uint16, Uint32, absl::uint128>;
TYPED_TEST_SUITE(TranscribeTest, MyTypes);
// Generate a random integer of a specified number of bits.
template <class TypeParam>
TypeParam generate_random(int number_bits, unsigned int* seed) {
if (number_bits == 0) return 0;
TypeParam random_value = static_cast<TypeParam>(rand_r(seed));
if (number_bits >= 8 * sizeof(TypeParam)) {
return random_value;
} else {
TypeParam mask = (static_cast<TypeParam>(1) << number_bits) - 1;
return random_value & mask;
}
}
// Specialization for uint128.
template <>
absl::uint128 generate_random(int number_bits, unsigned int* seed) {
int number_bits_hi = number_bits - std::min(64, number_bits);
int number_bits_lo = number_bits % 64;
uint64_t hi = generate_random<uint64_t>(number_bits_hi, seed);
uint64_t lo = generate_random<uint64_t>(number_bits_lo, seed);
return absl::MakeUint128(hi, lo);
}
// Verifies that the input_vector is long enough.
TYPED_TEST(TranscribeTest, InputLongEnough) {
using InputInt = TypeParam;
using OutputInt = TypeParam;
const int input_number_of_bits = 8 * sizeof(InputInt);
const int output_number_of_bits = 8 * sizeof(OutputInt);
for (auto input_bit_length : {1, 100, 1000, 10000}) {
for (int i = 1; i < input_number_of_bits; i++) {
int necessary_number_of_chunks = (input_bit_length + i - 1) / i;
std::vector<InputInt> input(necessary_number_of_chunks - 1, 0);
for (int j = 1; j < output_number_of_bits; j++) {
EXPECT_THAT(
(TranscribeBits<InputInt, OutputInt>(input, input_bit_length, i,
j)),
StatusIs(::absl::StatusCode::kInvalidArgument,
HasSubstr(absl::StrCat("The input vector of size ",
(necessary_number_of_chunks - 1),
" is too small to contain ",
input_bit_length, " bits."))));
}
}
}
}
// Verifies that the input and output types are consistent.
TYPED_TEST(TranscribeTest, InconsistentInputType) {
using InputInt = TypeParam;
using OutputInt = TypeParam;
const int input_number_of_bits = 8 * sizeof(InputInt);
const int output_number_of_bits = 8 * sizeof(OutputInt);
// Try to extract too many bits.
const int input_bits_per_int = input_number_of_bits + 1;
for (int len = 1; len <= kLength; len++) {
int input_bit_length = input_bits_per_int * len;
int necessary_number_of_chunks =
(input_bit_length + input_number_of_bits - 1) / input_number_of_bits;
std::vector<InputInt> input(necessary_number_of_chunks, 0);
for (int j = 1; j <= output_number_of_bits; j++) {
EXPECT_THAT(
(TranscribeBits<InputInt, OutputInt>(input, input_bit_length,
input_bits_per_int, j)),
StatusIs(::absl::StatusCode::kInvalidArgument,
HasSubstr(absl::StrCat(
"The input type only contains ", input_number_of_bits,
" bits, hence we cannot extract ", input_bits_per_int,
" bits out of each integer."))));
}
}
}
TYPED_TEST(TranscribeTest, InconsistentOutputType) {
using InputInt = TypeParam;
using OutputInt = TypeParam;
const int input_number_of_bits = 8 * sizeof(InputInt);
const int output_number_of_bits = 8 * sizeof(OutputInt);
// Try to store too many bits.
const int output_bits_per_int = output_number_of_bits + 1;
for (int len = 1; len <= kLength; len++) {
std::vector<InputInt> input(len, 0);
for (int i = 1; i <= input_number_of_bits; i++) {
EXPECT_THAT(
(TranscribeBits<InputInt, OutputInt>(input, i * len, i,
output_bits_per_int)),
StatusIs(::absl::StatusCode::kInvalidArgument,
HasSubstr(absl::StrCat(
"The output type only contains ", output_number_of_bits,
" bits, hence we cannot save ", output_bits_per_int,
" bits in each integer."))));
}
}
}
TYPED_TEST(TranscribeTest, NegativeInputLength) {
using InputInt = TypeParam;
using OutputInt = TypeParam;
const int input_bit_length = -1;
// create a zero string
std::vector<InputInt> bits_i(kLength, 0);
EXPECT_THAT(
(TranscribeBits<InputInt, OutputInt>(bits_i, input_bit_length, 0, 0)),
StatusIs(
::absl::StatusCode::kInvalidArgument,
HasSubstr(absl::StrCat("The input bit length, ", input_bit_length,
", cannot be negative."))));
}
TYPED_TEST(TranscribeTest, NonEmptyInputToEmptyOutput) {
using InputInt = TypeParam;
using OutputInt = TypeParam;
const int input_bit_length = 0;
// Create a zero string
std::vector<InputInt> bits_i(kLength, 0);
EXPECT_THAT(
(TranscribeBits<InputInt, OutputInt>(bits_i, input_bit_length, 1, 1)),
StatusIs(::absl::StatusCode::kInvalidArgument,
HasSubstr("Cannot transcribe an empty output "
"vector with a non-empty input "
"vector.")));
}
// Convert a sequence in chunks of i bits into a sequence in chunks of j
// bits.
TYPED_TEST(TranscribeTest, TranscribeTypeToType) {
using InputInt = TypeParam;
using OutputInt = TypeParam;
const int input_number_of_bits = 8 * sizeof(InputInt);
const int output_number_of_bits = 8 * sizeof(OutputInt);
unsigned int seed = 0;
for (int i = 1; i <= input_number_of_bits; i++) {
for (int j = 1; j <= output_number_of_bits; j++) {
for (int len = 1; len <= kLength; len++) {
// Create a random string of len bytes.
std::vector<InputInt> bits_i(len, 0);
for (InputInt& byte : bits_i) {
byte = generate_random<InputInt>(i, &seed);
}
// Convert to j bits.
ASSERT_OK_AND_ASSIGN(
std::vector<OutputInt> bits_j,
(TranscribeBits<InputInt, OutputInt>(bits_i, len * i, i, j)));
// Ensure that bits_j has the right length.
EXPECT_EQ(bits_j.size(), (len * i + (j - 1)) / j);
// Ensure that the bits came out the same.
for (int bit = 0; bit < i * len; bit++) {
InputInt bit_i = bits_i[bit / i] >> (bit % i);
OutputInt bit_j = bits_j[bit / j] >> (bit % j);
EXPECT_EQ(bit_i & 1, bit_j & 1);
}
// Ensure that all other bits in bits_j are zeros.
for (int byte = 0; byte < bits_j.size(); byte++) {
if (j == output_number_of_bits) continue; // no remaining bits
EXPECT_EQ(bits_j[byte] >> j, 0);
}
}
}
}
}
TYPED_TEST(TranscribeTest, TranscribeTypeToTypeAndBack) {
using InputInt = TypeParam;
using OutputInt = TypeParam;
const int input_number_of_bits = 8 * sizeof(InputInt);
const int output_number_of_bits = 8 * sizeof(OutputInt);
unsigned int seed = 0;
for (int i = 1; i <= input_number_of_bits; i++) {
for (int j = 1; j <= output_number_of_bits; j++) {
for (int len = 1; len <= kLength; len++) {
// Create a random string of len bytes.
std::vector<InputInt> bits_i(len, 0);
for (InputInt& byte : bits_i) {
byte = generate_random<InputInt>(i, &seed);
}
// Convert to j bits.
ASSERT_OK_AND_ASSIGN(
std::vector<OutputInt> bits_j,
(TranscribeBits<InputInt, OutputInt>(bits_i, len * i, i, j)));
// Convert back.
ASSERT_OK_AND_ASSIGN(
std::vector<InputInt> bits_i2,
(TranscribeBits<InputInt, OutputInt>(bits_j, len * i, j, i)));
EXPECT_EQ(bits_i, bits_i2);
}
}
}
}
// Test when the input bit length is not a multiple of the number of bits per
// int.
TYPED_TEST(TranscribeTest, InputBitLengthNotMultipleBitsPerInt) {
using InputInt = TypeParam;
using OutputInt = TypeParam;
const int input_number_of_bits = 8 * sizeof(InputInt);
const int output_number_of_bits = 8 * sizeof(OutputInt);
unsigned int seed = 0;
// Reduce the number of elements to speed up the test.
for (auto i : {2, 7, input_number_of_bits / 2, input_number_of_bits / 2 + 1,
input_number_of_bits}) {
for (int j = 1; j <= output_number_of_bits; j++) {
// Reduce the number of elements to speed up the test.
for (int len = 1; len <= kLength / 4 + 1; len++) {
for (int input_bit_length = len * i + 1;
input_bit_length < (len + 1) * i; input_bit_length++) {
int necessary_number_of_chunks = (input_bit_length + i - 1) / i;
// Create a random string of necessary_number_of_chunks bytes.
std::vector<InputInt> bits_i(necessary_number_of_chunks, 0);
for (InputInt& byte : bits_i) {
byte = generate_random<InputInt>(i, &seed);
}
// Convert to j bits.
ASSERT_OK_AND_ASSIGN(std::vector<OutputInt> bits_j,
(TranscribeBits<InputInt, OutputInt>(
bits_i, input_bit_length, i, j)));
// Ensure that the bits came out the same.
for (int bit = 0; bit < input_bit_length; bit++) {
InputInt bit_i = bits_i[bit / i] >> (bit % i);
OutputInt bit_j = bits_j[bit / j] >> (bit % j);
EXPECT_EQ(bit_i & 1, bit_j & 1);
}
// Ensure that all other bits in bits_j are zeros.
for (int byte = 0; byte < bits_j.size(); byte++) {
if (j == output_number_of_bits) continue; // no remaining bits
EXPECT_EQ(bits_j[byte] >> j, 0);
}
// The last element will only have input_bit_length % j bits sets.
// Check that all the other bits are 0. The test is only meaningful
// when input_bit_length % j != 0.
if (input_bit_length % j != 0) {
EXPECT_EQ(bits_j[bits_j.size() - 1] >> (input_bit_length % j), 0);
}
}
}
}
}
}
TYPED_TEST(TranscribeTest, TranscribeTypeToUint64) {
using InputInt = TypeParam;
using OutputInt = uint64_t;
const int input_number_of_bits = 8 * sizeof(InputInt);
const int output_number_of_bits = 8 * sizeof(OutputInt);
unsigned int seed = 0;
for (int i = 1; i <= input_number_of_bits; i++) {
for (int j = 1; j <= output_number_of_bits; j++) {
for (int len = 1; len <= kLength; len++) {
// Create a random string of len bytes.
std::vector<InputInt> bits_i(len, 0);
for (InputInt& byte : bits_i) {
byte = generate_random<InputInt>(i, &seed);
}
// Convert to j bits.
ASSERT_OK_AND_ASSIGN(
std::vector<OutputInt> bits_j,
(TranscribeBits<InputInt, OutputInt>(bits_i, len * i, i, j)));
// Ensure that bits_j has the right length.
EXPECT_EQ(bits_j.size(), (len * i + (j - 1)) / j);
// Ensure that the bits came out the same.
for (int bit = 0; bit < i * len; bit++) {
Uint8 bit_i = static_cast<Uint8>(bits_i[bit / i] >> (bit % i));
Uint8 bit_j = static_cast<Uint8>(bits_j[bit / j] >> (bit % j));
EXPECT_EQ(bit_i & 0x01, bit_j & 0x01);
}
// Ensure that all other bits in bits_j are zeros.
for (int byte = 0; byte < bits_j.size(); byte++) {
if (j == output_number_of_bits) continue; // no remaining bits
EXPECT_EQ(bits_j[byte] >> j, 0);
}
}
}
}
}
TYPED_TEST(TranscribeTest, TranscribeTypeToUint128) {
using InputInt = TypeParam;
using OutputInt = absl::uint128;
const int input_number_of_bits = 8 * sizeof(InputInt);
const int output_number_of_bits = 8 * sizeof(OutputInt);
unsigned int seed = 0;
for (int i = 1; i <= input_number_of_bits; i++) {
// Reduce the number of elements to speed up the test.
for (auto j : {2, 7, output_number_of_bits / 2,
output_number_of_bits / 2 + 1, output_number_of_bits}) {
for (int len = 1; len <= kLength; len++) {
// Create a random string of len bytes.
std::vector<InputInt> bits_i(len, 0);
for (InputInt& byte : bits_i) {
byte = generate_random<InputInt>(i, &seed);
}
// Convert to j bits.
ASSERT_OK_AND_ASSIGN(
std::vector<OutputInt> bits_j,
(TranscribeBits<InputInt, OutputInt>(bits_i, len * i, i, j)));
// Ensure that bits_j has the right length.
EXPECT_EQ(bits_j.size(), (len * i + (j - 1)) / j);
// Ensure that the bits came out the same.
for (int bit = 0; bit < i * len; bit++) {
Uint8 bit_i = static_cast<Uint8>(bits_i[bit / i] >> (bit % i));
Uint8 bit_j = static_cast<Uint8>(bits_j[bit / j] >> (bit % j));
EXPECT_EQ(bit_i & 0x01, bit_j & 0x01);
}
// Ensure that all other bits in bits_j are zeros.
for (int byte = 0; byte < bits_j.size(); byte++) {
if (j == output_number_of_bits) continue; // no remaining bits
EXPECT_EQ(bits_j[byte] >> j, 0);
}
}
}
}
}
TYPED_TEST(TranscribeTest, TranscribeTypeToUint8) {
using InputInt = TypeParam;
using OutputInt = Uint8;
const int input_number_of_bits = 8 * sizeof(InputInt);
const int output_number_of_bits = 8 * sizeof(OutputInt);
unsigned int seed = 0;
for (int i = 1; i <= input_number_of_bits; i++) {
for (int j = 1; j <= output_number_of_bits; j++) {
for (int len = 1; len <= kLength; len++) {
// Create a random string of len bytes.
std::vector<InputInt> bits_i(len, 0);
for (InputInt& byte : bits_i) {
byte = generate_random<InputInt>(i, &seed);
}
// Convert to j bits.
ASSERT_OK_AND_ASSIGN(
std::vector<OutputInt> bits_j,
(TranscribeBits<InputInt, OutputInt>(bits_i, len * i, i, j)));
// Ensure that bits_j has the right length.
EXPECT_EQ(bits_j.size(), (len * i + (j - 1)) / j);
// Ensure that the bits came out the same.
for (int bit = 0; bit < i * len; bit++) {
Uint8 bit_i = static_cast<Uint8>(bits_i[bit / i] >> (bit % i));
Uint8 bit_j = static_cast<Uint8>(bits_j[bit / j] >> (bit % j));
EXPECT_EQ(bit_i & 0x01, bit_j & 0x01);
}
// Ensure that all other bits in bits_j are zeros.
for (int byte = 0; byte < bits_j.size(); byte++) {
if (j == output_number_of_bits) continue; // no remaining bits
EXPECT_EQ(bits_j[byte] >> j, 0);
}
}
}
}
}
TYPED_TEST(TranscribeTest, InputLengthSmallerThanNumberOfBitsPerInput) {
using InputInt = TypeParam;
using OutputInt = TypeParam;
const int input_number_of_bits = 8 * sizeof(InputInt);
const int output_number_of_bits = 8 * sizeof(OutputInt);
unsigned int seed = 0;
for (int input_bit_length = 1; input_bit_length < input_number_of_bits;
input_bit_length++) {
// Create a random string of 1 byte.
std::vector<InputInt> bits_i(
{generate_random<InputInt>(input_bit_length, &seed)});
for (int j = 1; j <= output_number_of_bits; j++) {
// Convert to j bits.
ASSERT_OK_AND_ASSIGN(
std::vector<OutputInt> bits_j,
(TranscribeBits<InputInt, OutputInt>(bits_i, input_bit_length,
input_number_of_bits, j)));
// Ensure that bits_j has the right length.
EXPECT_EQ(bits_j.size(), (input_bit_length + (j - 1)) / j);
// Ensure that the bits came out the same.
for (int bit = 0; bit < input_bit_length; bit++) {
Uint8 bit_i = static_cast<Uint8>(bits_i[0] >> bit);
Uint8 bit_j = static_cast<Uint8>(bits_j[bit / j] >> (bit % j));
EXPECT_EQ(bit_i & 0x01, bit_j & 0x01);
}
// Ensure that all other bits in bits_j are zeros.
for (int byte = 0; byte < bits_j.size(); byte++) {
if (j == output_number_of_bits) continue; // no remaining bits
EXPECT_EQ(bits_j[byte] >> j, 0);
}
// The last element will only have input_bit_length % j bits sets.
// Check that all the other bits are 0. The test is only meaningful
// when input_bit_length % j != 0.
if (input_bit_length % j != 0) {
EXPECT_EQ(bits_j[bits_j.size() - 1] >> (input_bit_length % j), 0);
}
}
}
} // namespace
} // namespace
} // namespace rlwe