-
Notifications
You must be signed in to change notification settings - Fork 709
/
modulus.cpp
209 lines (181 loc) · 7.43 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#include "seal/modulus.h"
#include "seal/util/uintcore.h"
#include <stdexcept>
#include "gtest/gtest.h"
using namespace seal;
using namespace seal::util;
using namespace std;
namespace sealtest
{
TEST(ModulusTest, CreateModulus)
{
Modulus mod;
ASSERT_TRUE(mod.is_zero());
ASSERT_EQ(0ULL, mod.value());
ASSERT_EQ(0, mod.bit_count());
ASSERT_EQ(1ULL, mod.uint64_count());
ASSERT_EQ(0ULL, mod.const_ratio()[0]);
ASSERT_EQ(0ULL, mod.const_ratio()[1]);
ASSERT_EQ(0ULL, mod.const_ratio()[2]);
ASSERT_FALSE(mod.is_prime());
mod = 3;
ASSERT_FALSE(mod.is_zero());
ASSERT_EQ(3ULL, mod.value());
ASSERT_EQ(2, mod.bit_count());
ASSERT_EQ(1ULL, mod.uint64_count());
ASSERT_EQ(6148914691236517205ULL, mod.const_ratio()[0]);
ASSERT_EQ(6148914691236517205ULL, mod.const_ratio()[1]);
ASSERT_EQ(1ULL, mod.const_ratio()[2]);
ASSERT_TRUE(mod.is_prime());
Modulus mod2(2);
Modulus mod3(3);
ASSERT_TRUE(mod != mod2);
ASSERT_TRUE(mod == mod3);
mod = 0;
ASSERT_TRUE(mod.is_zero());
ASSERT_EQ(0ULL, mod.value());
ASSERT_EQ(0, mod.bit_count());
ASSERT_EQ(1ULL, mod.uint64_count());
ASSERT_EQ(0ULL, mod.const_ratio()[0]);
ASSERT_EQ(0ULL, mod.const_ratio()[1]);
ASSERT_EQ(0ULL, mod.const_ratio()[2]);
mod = 0xF00000F00000F;
ASSERT_FALSE(mod.is_zero());
ASSERT_EQ(0xF00000F00000FULL, mod.value());
ASSERT_EQ(52, mod.bit_count());
ASSERT_EQ(1ULL, mod.uint64_count());
ASSERT_EQ(1224979098644774929ULL, mod.const_ratio()[0]);
ASSERT_EQ(4369ULL, mod.const_ratio()[1]);
ASSERT_EQ(281470698520321ULL, mod.const_ratio()[2]);
ASSERT_FALSE(mod.is_prime());
mod = 0xF00000F000079;
ASSERT_FALSE(mod.is_zero());
ASSERT_EQ(0xF00000F000079ULL, mod.value());
ASSERT_EQ(52, mod.bit_count());
ASSERT_EQ(1ULL, mod.uint64_count());
ASSERT_EQ(1224979096621368355ULL, mod.const_ratio()[0]);
ASSERT_EQ(4369ULL, mod.const_ratio()[1]);
ASSERT_EQ(1144844808538997ULL, mod.const_ratio()[2]);
ASSERT_TRUE(mod.is_prime());
}
TEST(ModulusTest, CompareModulus)
{
Modulus sm0;
Modulus sm2(2);
Modulus sm5(5);
ASSERT_FALSE(sm0 < sm0);
ASSERT_TRUE(sm0 == sm0);
ASSERT_TRUE(sm0 <= sm0);
ASSERT_TRUE(sm0 >= sm0);
ASSERT_FALSE(sm0 > sm0);
ASSERT_FALSE(sm5 < sm5);
ASSERT_TRUE(sm5 == sm5);
ASSERT_TRUE(sm5 <= sm5);
ASSERT_TRUE(sm5 >= sm5);
ASSERT_FALSE(sm5 > sm5);
ASSERT_FALSE(sm5 < sm2);
ASSERT_FALSE(sm5 == sm2);
ASSERT_FALSE(sm5 <= sm2);
ASSERT_TRUE(sm5 >= sm2);
ASSERT_TRUE(sm5 > sm2);
ASSERT_TRUE(sm5 < 6);
ASSERT_FALSE(sm5 == 6);
ASSERT_TRUE(sm5 <= 6);
ASSERT_FALSE(sm5 >= 6);
ASSERT_FALSE(sm5 > 6);
}
TEST(ModulusTest, SaveLoadModulus)
{
stringstream stream;
Modulus mod;
mod.save(stream);
Modulus mod2;
mod2.load(stream);
ASSERT_EQ(mod2.value(), mod.value());
ASSERT_EQ(mod2.bit_count(), mod.bit_count());
ASSERT_EQ(mod2.uint64_count(), mod.uint64_count());
ASSERT_EQ(mod2.const_ratio()[0], mod.const_ratio()[0]);
ASSERT_EQ(mod2.const_ratio()[1], mod.const_ratio()[1]);
ASSERT_EQ(mod2.const_ratio()[2], mod.const_ratio()[2]);
ASSERT_EQ(mod2.is_prime(), mod.is_prime());
mod = 3;
mod.save(stream);
mod2.load(stream);
ASSERT_EQ(mod2.value(), mod.value());
ASSERT_EQ(mod2.bit_count(), mod.bit_count());
ASSERT_EQ(mod2.uint64_count(), mod.uint64_count());
ASSERT_EQ(mod2.const_ratio()[0], mod.const_ratio()[0]);
ASSERT_EQ(mod2.const_ratio()[1], mod.const_ratio()[1]);
ASSERT_EQ(mod2.const_ratio()[2], mod.const_ratio()[2]);
ASSERT_EQ(mod2.is_prime(), mod.is_prime());
mod = 0xF00000F00000F;
mod.save(stream);
mod2.load(stream);
ASSERT_EQ(mod2.value(), mod.value());
ASSERT_EQ(mod2.bit_count(), mod.bit_count());
ASSERT_EQ(mod2.uint64_count(), mod.uint64_count());
ASSERT_EQ(mod2.const_ratio()[0], mod.const_ratio()[0]);
ASSERT_EQ(mod2.const_ratio()[1], mod.const_ratio()[1]);
ASSERT_EQ(mod2.const_ratio()[2], mod.const_ratio()[2]);
ASSERT_EQ(mod2.is_prime(), mod.is_prime());
mod = 0xF00000F000079;
mod.save(stream);
mod2.load(stream);
ASSERT_EQ(mod2.value(), mod.value());
ASSERT_EQ(mod2.bit_count(), mod.bit_count());
ASSERT_EQ(mod2.uint64_count(), mod.uint64_count());
ASSERT_EQ(mod2.const_ratio()[0], mod.const_ratio()[0]);
ASSERT_EQ(mod2.const_ratio()[1], mod.const_ratio()[1]);
ASSERT_EQ(mod2.const_ratio()[2], mod.const_ratio()[2]);
ASSERT_EQ(mod2.is_prime(), mod.is_prime());
}
TEST(CoeffModTest, CustomExceptionTest)
{
// Too small poly_modulus_degree
ASSERT_THROW(auto modulus = CoeffModulus::Create(1, { 2 }), invalid_argument);
// Too large poly_modulus_degree
ASSERT_THROW(auto modulus = CoeffModulus::Create(262144, { 30 }), invalid_argument);
// Invalid poly_modulus_degree
ASSERT_THROW(auto modulus = CoeffModulus::Create(1023, { 20 }), invalid_argument);
// Invalid bit-size
ASSERT_THROW(auto modulus = CoeffModulus::Create(2048, { 0 }), invalid_argument);
ASSERT_THROW(auto modulus = CoeffModulus::Create(2048, { -30 }), invalid_argument);
ASSERT_THROW(auto modulus = CoeffModulus::Create(2048, { 30, -30 }), invalid_argument);
// Too small primes requested
ASSERT_THROW(auto modulus = CoeffModulus::Create(2, { 2 }), logic_error);
ASSERT_THROW(auto modulus = CoeffModulus::Create(2, { 3, 3, 3 }), logic_error);
ASSERT_THROW(auto modulus = CoeffModulus::Create(1024, { 8 }), logic_error);
}
TEST(CoeffModTest, CustomTest)
{
auto cm = CoeffModulus::Create(2, {});
ASSERT_EQ(0, cm.size());
cm = CoeffModulus::Create(2, { 3 });
ASSERT_EQ(1, cm.size());
ASSERT_EQ(uint64_t(5), cm[0].value());
cm = CoeffModulus::Create(2, { 3, 4 });
ASSERT_EQ(2, cm.size());
ASSERT_EQ(uint64_t(5), cm[0].value());
ASSERT_EQ(uint64_t(13), cm[1].value());
cm = CoeffModulus::Create(2, { 3, 5, 4, 5 });
ASSERT_EQ(4, cm.size());
ASSERT_EQ(uint64_t(5), cm[0].value());
ASSERT_EQ(uint64_t(17), cm[1].value());
ASSERT_EQ(uint64_t(13), cm[2].value());
ASSERT_EQ(uint64_t(29), cm[3].value());
cm = CoeffModulus::Create(32, { 30, 40, 30, 30, 40 });
ASSERT_EQ(5, cm.size());
ASSERT_EQ(30, get_significant_bit_count(cm[0].value()));
ASSERT_EQ(40, get_significant_bit_count(cm[1].value()));
ASSERT_EQ(30, get_significant_bit_count(cm[2].value()));
ASSERT_EQ(30, get_significant_bit_count(cm[3].value()));
ASSERT_EQ(40, get_significant_bit_count(cm[4].value()));
ASSERT_EQ(1ULL, cm[0].value() % 64);
ASSERT_EQ(1ULL, cm[1].value() % 64);
ASSERT_EQ(1ULL, cm[2].value() % 64);
ASSERT_EQ(1ULL, cm[3].value() % 64);
ASSERT_EQ(1ULL, cm[4].value() % 64);
}
} // namespace sealtest