-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpermute.cpp
More file actions
140 lines (106 loc) · 2.78 KB
/
Copy pathpermute.cpp
File metadata and controls
140 lines (106 loc) · 2.78 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
// SPDX-License-Identifier: Apache-2.0
// Copyright Contributors to the OpenQMC Project.
#include <oqmc/permute.h>
#include <oqmc/reverse.h>
#include <gtest/gtest.h>
#include <array>
#include <cstdint>
#include <vector>
namespace
{
constexpr std::array<std::uint32_t, 20> primes{
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
};
// clang-format off
constexpr std::array<std::uint32_t, 5> values{
0b01010101010101010011001100110011,
0b11111111000000001111000011110000,
0b11111111111111110000000011111111,
0b11111111111111111111111111111111,
0b00000000000000000000000000000000,
};
// clang-format on
TEST(PermuteTest, LeftNestedHashing)
{
constexpr std::uint32_t mask = 0b00000000000000001111111111111111;
constexpr std::uint32_t flip = 0b00000000000000010000000000000000;
for(const auto value : values)
{
for(const auto prime : primes)
{
const auto flipped = value ^ flip;
const auto v1 = oqmc::laineKarrasPermutation(value, prime);
const auto v2 = oqmc::laineKarrasPermutation(flipped, prime);
const auto v1First = v1 & mask;
const auto v2First = v2 & mask;
EXPECT_EQ(v1First, v2First);
const auto v1Second = v1 & ~mask;
const auto v2Second = v2 & ~mask;
EXPECT_NE(v1Second, v2Second);
}
}
}
TEST(PermuteTest, Reverse)
{
// clang-format off
constexpr std::array<std::uint32_t, 3> values{
0b01010101010101010011001100110011,
0b11111111000000001111000011110000,
0b11111111111111110000000011111111,
};
// clang-format on
for(const auto value : values)
{
for(const auto prime : primes)
{
const auto reversed = oqmc::reverseBits32(value);
EXPECT_NE(value, reversed);
const auto v1 = oqmc::laineKarrasPermutation(value, prime);
const auto v2 = oqmc::laineKarrasPermutation(reversed, prime);
EXPECT_NE(v1, v2);
const auto shuffled = oqmc::reverseAndShuffle(value, prime);
EXPECT_EQ(shuffled, v2);
}
}
}
TEST(PermuteTest, FullPermutation)
{
constexpr auto size = 1 << 4;
constexpr auto mask = size - 1;
std::array<bool, size> values;
for(const auto prime : primes)
{
values.fill(false);
for(int i = 0; i < size; ++i)
{
const auto shuffled = oqmc::reverseAndShuffle(i, prime);
const auto permuted = oqmc::reverseBits32(shuffled);
ASSERT_EQ(permuted, oqmc::shuffle(i, prime));
const auto index = permuted & mask;
auto& value = values[index];
ASSERT_FALSE(value);
value = true;
}
for(const auto value : values)
{
EXPECT_TRUE(value);
}
}
}
TEST(PermuteTest, ChangeSeed)
{
for(const auto value : values)
{
std::vector<std::uint32_t> results;
for(const auto prime : primes)
{
const auto key = oqmc::reverseAndShuffle(value, prime);
for(const auto result : results)
{
EXPECT_NE(key, result);
}
results.push_back(key);
}
}
}
} // namespace