This repository was archived by the owner on Aug 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathutilities.cpp
More file actions
235 lines (195 loc) · 9.75 KB
/
Copy pathutilities.cpp
File metadata and controls
235 lines (195 loc) · 9.75 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
#include "CppUnitTest.h"
#include "seal/bigpoly.h"
#include "seal/utilities.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace seal::util;
using namespace seal;
using namespace std;
namespace SEALTest
{
TEST_CLASS(Utilities)
{
public:
TEST_METHOD(BigPolyInftyNorm)
{
BigPoly poly("1x^10 + 2x^9 + 5x^8 + Ax^7 + Bx^6 + 4x^5 + 1x^2 + 1");
BigUInt inftynorm = poly_infty_norm(poly);
Assert::IsTrue(inftynorm == BigUInt("B"));
poly = BigPoly("AAx^10 + ABx^9 + CAx^8 + CFx^7 + FEx^6 + F7x^5 + 1x^2 + 2");
inftynorm = poly_infty_norm(poly);
Assert::IsTrue(inftynorm == BigUInt("FE"));
poly = BigPoly("Ax^10 + ABx^9 + ABCx^8 + ABCDx^7 + ABCDEx^6 + ABCDEFx^5 + 1x^2 + 2");
inftynorm = poly_infty_norm(poly);
Assert::IsTrue(inftynorm == BigUInt("ABCDEF"));
poly = BigPoly("1x^5 + 2x^4 + 3x^3 + 4x^2 + 5x^1 + 6");
inftynorm = poly_infty_norm(poly);
Assert::IsTrue(inftynorm == BigUInt("6"));
}
TEST_METHOD(BigPolyInftyNormCoeffMod)
{
BigPoly poly("1x^10 + 2x^9 + 5x^8 + Ax^7 + Bx^6 + 4x^5 + 1x^2 + 1");
BigUInt modulus(poly.coeff_bit_count());
modulus = 5;
BigUInt inftynorm = poly_infty_norm_coeffmod(poly, modulus);
Assert::IsTrue(inftynorm == BigUInt("2"));
poly = BigPoly("AAx^10 + ABx^9 + CAx^8 + CFx^7 + FEx^6 + F7x^5 + 1x^2 + 2");
modulus = "10";
inftynorm = poly_infty_norm_coeffmod(poly, modulus);
Assert::IsTrue(inftynorm == BigUInt("7"));
poly = BigPoly("Ax^10 + ABx^9 + ABCx^8 + ABCDx^7 + ABCDEx^6 + ABCDEFx^5 + 1x^2 + 2");
modulus = "4";
inftynorm = poly_infty_norm_coeffmod(poly, modulus);
Assert::IsTrue(inftynorm == BigUInt("2"));
poly = BigPoly("1x^5 + 2x^4 + 3x^3 + 4x^2 + 5x^1 + 6");
modulus = "4";
inftynorm = poly_infty_norm_coeffmod(poly, modulus);
Assert::IsTrue(inftynorm == BigUInt("2"));
}
TEST_METHOD(ExponentiateBigUIntMod)
{
BigUInt uint("ABABABAB");
BigUInt modulus("CAACAACAA");
BigUInt exponent("5");
BigUInt result = exponentiate_uint_mod(uint, exponent, modulus);
Assert::IsTrue(result.to_dec_string() == "33773505765");
uint = "1";
exponent = "F00F00F00F00F00";
result = exponentiate_uint_mod(uint, exponent, modulus);
Assert::IsTrue(result.to_dec_string() == "1");
modulus = "AAAAAAAAAAAAAAAAAAAAA";
uint = "F00F00F00F00F00";
exponent = "1";
result = exponentiate_uint_mod(uint, exponent, modulus);
Assert::IsTrue(result.to_string() == "F00F00F00F00F00");
uint = "F00F00F00F00F00";
exponent = "0";
result = exponentiate_uint_mod(uint, exponent, modulus);
Assert::IsTrue(result.to_string() == "1");
}
TEST_METHOD(ExponentiatePolyPolyModCoeffMod)
{
BigPoly poly("1x^2 + 2x^1 + 3");
BigUInt modulus("5");
BigUInt exponent("1");
BigPoly polymod("1x^3 + 3x^1 + 1");
BigPoly result = exponentiate_poly_polymod_coeffmod(poly, exponent, polymod, modulus);
Assert::IsTrue(result.to_string() == "1x^2 + 2x^1 + 3");
exponent = "2";
result = exponentiate_poly_polymod_coeffmod(poly, exponent, polymod, modulus);
Assert::IsTrue(result.to_string() == "2x^2 + 4x^1");
poly = "1";
exponent = "2";
result = exponentiate_poly_polymod_coeffmod(poly, exponent, polymod, modulus);
Assert::IsTrue(result.to_string() == "1");
exponent = "F00F000F00";
result = exponentiate_poly_polymod_coeffmod(poly, exponent, polymod, modulus);
Assert::IsTrue(result.to_string() == "1");
}
TEST_METHOD(BigPolyEvalPoly)
{
BigPoly poly_to_eval("0");
BigPoly poly("0");
BigPoly result = poly_eval_poly(poly_to_eval, poly);
Assert::IsTrue(result.to_string() == "0");
poly_to_eval = "1";
poly = "0";
result = poly_eval_poly(poly_to_eval, poly);
Assert::IsTrue(result.to_string() == "1");
poly_to_eval = "12345ABCDE";
poly = "0";
result = poly_eval_poly(poly_to_eval, poly);
Assert::IsTrue(result.to_string() == "12345ABCDE");
poly_to_eval = "12345ABCDE";
poly = "1";
result = poly_eval_poly(poly_to_eval, poly);
Assert::IsTrue(result.to_string() == "12345ABCDE");
poly_to_eval = "0";
poly = "1";
result = poly_eval_poly(poly_to_eval, poly);
Assert::IsTrue(result.to_string() == "0");
poly_to_eval = "1x^1 + 2";
poly = "1";
result = poly_eval_poly(poly_to_eval, poly);
Assert::IsTrue(result.to_string() == "3");
poly_to_eval = "1x^1 + FFFFFFF";
poly = "2x^1 + 1";
result = poly_eval_poly(poly_to_eval, poly);
Assert::IsTrue(result.to_string() == "2x^1 + 10000000");
poly_to_eval = "1x^1 + 1";
poly = "1x^100 + 2x^90 + 3x^80 + 4x^70 + 5x^60 + 6x^50 + 7x^40 + 8x^30 + 9x^20 + Ax^10 + B";
result = poly_eval_poly(poly_to_eval, poly);
Assert::IsTrue(result.to_string() == "1x^100 + 2x^90 + 3x^80 + 4x^70 + 5x^60 + 6x^50 + 7x^40 + 8x^30 + 9x^20 + Ax^10 + C");
poly_to_eval = "1x^2 + 1";
poly = "1x^10 + 2x^9 + 3x^8 + 4x^7 + 5x^6 + 6x^5 + 7x^4 + 8x^3 + 9x^2 + Ax^1 + B";
result = poly_eval_poly(poly_to_eval, poly);
Assert::IsTrue(result.to_string() == "1x^20 + 4x^19 + Ax^18 + 14x^17 + 23x^16 + 38x^15 + 54x^14 + 78x^13 + A5x^12 + DCx^11 + 11Ex^10 + 154x^9 + 17Dx^8 + 198x^7 + 1A4x^6 + 1A0x^5 + 18Bx^4 + 164x^3 + 12Ax^2 + DCx^1 + 7A");
poly_to_eval = "1x^3 + 1x^2 + 1";
poly = "1x^2 + 1x^1 + 1";
result = poly_eval_poly(poly_to_eval, poly);
Assert::IsTrue(result.to_string() == "1x^6 + 3x^5 + 7x^4 + 9x^3 + 9x^2 + 5x^1 + 3");
poly_to_eval = "1x^100 + 2x^95 + 3x^90 + Ax^75 + Bx^70 + Cx^65 + Dx^60 + Fx^30 + Ex^20 + Dx^10 + 1x^9 + 2x^8 + 3x^7 + 4x^6 + 5x^5 + 1x^2 + 1";
poly = "1";
result = poly_eval_poly(poly_to_eval, poly);
Assert::IsTrue(result.to_string() == "6F");
poly_to_eval = "1x^100 + 2x^95 + 3x^90 + Ax^75 + Bx^70 + Cx^65 + Dx^60 + Fx^30 + Ex^20 + Dx^10 + 1x^9 + 2x^8 + 3x^7 + 4x^6 + 5x^5 + 1x^2 + 1";
poly = "3";
result = poly_eval_poly(poly_to_eval, poly);
Assert::IsTrue(result.to_string() == "5B05B5BB47C5083385621FA57ACC77AAFD787C71");
}
TEST_METHOD(BigPolyEvalPolyPolyModCoeffMod)
{
BigUInt modulus("5");
BigPoly polymod("1x^3 + 3x^1 + 1");
BigPoly poly_to_eval("0");
BigPoly poly("0");
BigPoly result = poly_eval_poly_polymod_coeffmod(poly_to_eval, poly, polymod, modulus);
Assert::IsTrue(result.to_string() == "0");
poly_to_eval = "1";
poly = "0";
result = poly_eval_poly_polymod_coeffmod(poly_to_eval, poly, polymod, modulus);
Assert::IsTrue(result.to_string() == "1");
poly_to_eval = "4";
poly = "0";
result = poly_eval_poly_polymod_coeffmod(poly_to_eval, poly, polymod, modulus);
Assert::IsTrue(result.to_string() == "4");
poly_to_eval = "4";
poly = "1";
result = poly_eval_poly_polymod_coeffmod(poly_to_eval, poly, polymod, modulus);
Assert::IsTrue(result.to_string() == "4");
poly_to_eval = "0";
poly = "1";
result = poly_eval_poly_polymod_coeffmod(poly_to_eval, poly, polymod, modulus);
Assert::IsTrue(result.to_string() == "0");
poly_to_eval = "1x^1 + 2";
poly = "1";
result = poly_eval_poly_polymod_coeffmod(poly_to_eval, poly, polymod, modulus);
Assert::IsTrue(result.to_string() == "3");
poly_to_eval = "1x^1 + 4";
poly = "2x^1 + 1";
result = poly_eval_poly_polymod_coeffmod(poly_to_eval, poly, polymod, modulus);
Assert::IsTrue(result.to_string() == "2x^1");
poly_to_eval = "1x^1 + 1";
poly = "1x^2 + 2x^1 + 3";
result = poly_eval_poly_polymod_coeffmod(poly_to_eval, poly, polymod, modulus);
Assert::IsTrue(result.to_string() == "1x^2 + 2x^1 + 4");
poly_to_eval = "1x^2 + 1";
poly = "1x^2 + 2x^1 + 3";
result = poly_eval_poly_polymod_coeffmod(poly_to_eval, poly, polymod, modulus);
Assert::IsTrue(result.to_string() == "2x^2 + 4x^1 + 1");
}
TEST_METHOD(BigPolyEvalUIntMod)
{
BigUInt modulus("5");
BigPoly poly("1x^2 + 1x^1 + 1");
BigUInt value("0");
BigUInt result = poly_eval_uint_mod(poly, value, modulus);
Assert::IsTrue(result.to_string() == "1");
value = "1";
result = poly_eval_uint_mod(poly, value, modulus);
Assert::IsTrue(result.to_string() == "3");
value = "4";
result = poly_eval_uint_mod(poly, value, modulus);
Assert::IsTrue(result.to_string() == "1");
}
};
}