1 /*############################################################################
2 # Copyright 2016-2017 Intel Corporation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 ############################################################################*/
16
17 /*!
18 * \file
19 * \brief BigNum unit tests.
20 */
21
22 #include "epid/common-testhelper/epid_gtest-testhelper.h"
23 #include "gtest/gtest.h"
24
25 #include "epid/common-testhelper/bignum_wrapper-testhelper.h"
26 #include "epid/common-testhelper/errors-testhelper.h"
27
28 extern "C" {
29 #include "epid/common/math/bignum.h"
30 }
31
32 namespace {
33
34 // Use Test Fixture for SetUp and TearDown
35 class BigNumTest : public ::testing::Test {
36 public:
37 static const BigNumStr str_0;
38 static const BigNumStr str_1;
39 static const BigNumStr str_2;
40 static const BigNumStr str_big;
41 static const BigNumStr str_2big;
42 static const BigNumStr str_large_m1;
43 static const BigNumStr str_large;
44 static const BigNumStr str_large_p1;
45 static const BigNumStr str_32byte_high_bit_set;
46 static const BigNumStr str_32byte_high;
47 static const std::vector<unsigned char> vec_33byte_low;
SetUp()48 virtual void SetUp() {}
49
TearDown()50 virtual void TearDown() {}
51
52 ::testing::AssertionResult CompareBigNumStr(const BigNumStr* expected,
53 const BigNumStr* actual);
54
55 ::testing::AssertionResult CompareBigNum(const BigNum* expected,
56 const BigNum* actual);
57 };
58
59 const BigNumStr BigNumTest::str_0{
60 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
63
64 const BigNumStr BigNumTest::str_1{
65 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
66 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
68 const BigNumStr BigNumTest::str_2{
69 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
70 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02};
72 const BigNumStr BigNumTest::str_big{
73 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
75 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
76 const BigNumStr BigNumTest::str_2big{
77 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
78 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
79 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
80 const BigNumStr BigNumTest::str_large_m1{
81 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xF0, 0xCD, 0x46, 0xE5, 0xF2,
82 0x5E, 0xEE, 0x71, 0xA4, 0x9E, 0x0C, 0xDC, 0x65, 0xFB, 0x12, 0x99,
83 0x92, 0x1A, 0xF6, 0x2D, 0x53, 0x6C, 0xD1, 0x0B, 0x50, 0x0C};
84
85 /// Intel(R) EPID 2.0 parameter p
86 const BigNumStr BigNumTest::str_large{
87 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xF0, 0xCD, 0x46, 0xE5, 0xF2,
88 0x5E, 0xEE, 0x71, 0xA4, 0x9E, 0x0C, 0xDC, 0x65, 0xFB, 0x12, 0x99,
89 0x92, 0x1A, 0xF6, 0x2D, 0x53, 0x6C, 0xD1, 0x0B, 0x50, 0x0D};
90 const BigNumStr BigNumTest::str_large_p1{
91 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xF0, 0xCD, 0x46, 0xE5, 0xF2,
92 0x5E, 0xEE, 0x71, 0xA4, 0x9E, 0x0C, 0xDC, 0x65, 0xFB, 0x12, 0x99,
93 0x92, 0x1A, 0xF6, 0x2D, 0x53, 0x6C, 0xD1, 0x0B, 0x50, 0x0E};
94 const BigNumStr BigNumTest::str_32byte_high{
95 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
96 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
97 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
98 const BigNumStr BigNumTest::str_32byte_high_bit_set{
99 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
100 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
101 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
102 const std::vector<unsigned char> BigNumTest::vec_33byte_low{
103 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
104 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
105 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
106
CompareBigNumStr(const BigNumStr * expected,const BigNumStr * actual)107 ::testing::AssertionResult BigNumTest::CompareBigNumStr(
108 const BigNumStr* expected, const BigNumStr* actual) {
109 int size = sizeof(BigNumStr);
110 unsigned char* expected_str = (unsigned char*)expected;
111 unsigned char* actual_str = (unsigned char*)actual;
112 for (int i = 0; i < size; ++i) {
113 if (expected_str[i] != actual_str[i]) {
114 return ::testing::AssertionFailure()
115 << "Mismatch at " << i << " : Expected " << std::hex
116 << expected_str[i] << " Found " << std::hex << actual_str[i];
117 }
118 }
119
120 return ::testing::AssertionSuccess();
121 }
122
CompareBigNum(const BigNum * expected_bn,const BigNum * actual_bn)123 ::testing::AssertionResult BigNumTest::CompareBigNum(const BigNum* expected_bn,
124 const BigNum* actual_bn) {
125 size_t size = 0;
126 std::vector<unsigned char> expected_str;
127 std::vector<unsigned char> actual_str;
128 // Use an extra huge size so we have plenty of room to check
129 // overflow tests. This assumes no tests try to create a number
130 // bigger than 64 bytes.
131 size = sizeof(BigNumStr) * 2;
132 expected_str.resize(size, 0);
133 actual_str.resize(size, 0);
134
135 THROW_ON_EPIDERR(WriteBigNum(expected_bn, size, &expected_str[0]));
136 THROW_ON_EPIDERR(WriteBigNum(actual_bn, size, &actual_str[0]));
137
138 for (size_t i = 0; i < size; ++i) {
139 if (expected_str[i] != actual_str[i]) {
140 return ::testing::AssertionFailure() << "Numbers do not match";
141 }
142 }
143
144 return ::testing::AssertionSuccess();
145 }
146
147 ///////////////////////////////////////////////////////////////////////
148 // Create / Destroy
149
TEST_F(BigNumTest,NewCanCreate256BitBigNum)150 TEST_F(BigNumTest, NewCanCreate256BitBigNum) {
151 BigNum* bn = nullptr;
152 EXPECT_EQ(kEpidNoErr, NewBigNum(32, &bn));
153 DeleteBigNum(&bn);
154 }
155
TEST_F(BigNumTest,NewFailsGivenNullPointer)156 TEST_F(BigNumTest, NewFailsGivenNullPointer) {
157 EXPECT_EQ(kEpidBadArgErr, NewBigNum(sizeof(BigNumStr), NULL));
158 }
159
TEST_F(BigNumTest,NewFailsGivenSizeZero)160 TEST_F(BigNumTest, NewFailsGivenSizeZero) {
161 BigNum* bn = nullptr;
162 EXPECT_EQ(kEpidBadArgErr, NewBigNum(0, &bn));
163 DeleteBigNum(&bn);
164 }
165
TEST_F(BigNumTest,DeleteBigNumNullsPointer)166 TEST_F(BigNumTest, DeleteBigNumNullsPointer) {
167 BigNum* bn = nullptr;
168 THROW_ON_EPIDERR(NewBigNum(sizeof(BigNumStr), &bn));
169 DeleteBigNum(&bn);
170 EXPECT_EQ(nullptr, bn);
171 }
172
TEST_F(BigNumTest,DeleteWorksGivenNullPointer)173 TEST_F(BigNumTest, DeleteWorksGivenNullPointer) {
174 BigNum* bn = nullptr;
175 DeleteBigNum(nullptr);
176 EXPECT_EQ(nullptr, bn);
177 }
178
179 ///////////////////////////////////////////////////////////////////////
180 // Serialization
181
TEST_F(BigNumTest,ReadFailsGivenNullPtr)182 TEST_F(BigNumTest, ReadFailsGivenNullPtr) {
183 BigNum* bn = nullptr;
184 THROW_ON_EPIDERR(NewBigNum(sizeof(BigNumStr), &bn));
185 EXPECT_EQ(kEpidBadArgErr, ReadBigNum(NULL, sizeof(BigNumStr), bn));
186 EXPECT_EQ(kEpidBadArgErr,
187 ReadBigNum(&this->str_large, sizeof(BigNumStr), NULL));
188 DeleteBigNum(&bn);
189 }
190
TEST_F(BigNumTest,ReadFailsGivenInvalidBufferSize)191 TEST_F(BigNumTest, ReadFailsGivenInvalidBufferSize) {
192 BigNumObj bn(32);
193 EXPECT_EQ(kEpidBadArgErr, ReadBigNum(&this->str_0, 0, bn));
194 EXPECT_EQ(kEpidBadArgErr,
195 ReadBigNum(&this->str_0, std::numeric_limits<size_t>::max(), bn));
196 #if (SIZE_MAX >= 0x100000001) // When size_t value allowed to be 0x100000001
197 EXPECT_EQ(kEpidBadArgErr, ReadBigNum(&this->str_0, 0x100000001, bn));
198 #endif
199 }
200
TEST_F(BigNumTest,ReadFailsGivenTooBigBuffer)201 TEST_F(BigNumTest, ReadFailsGivenTooBigBuffer) {
202 BigNum* bn = nullptr;
203 THROW_ON_EPIDERR(NewBigNum(sizeof(BigNumStr), &bn));
204 EXPECT_NE(kEpidNoErr, ReadBigNum(&this->vec_33byte_low[0],
205 this->vec_33byte_low.size(), bn));
206 DeleteBigNum(&bn);
207 }
208
TEST_F(BigNumTest,WriteFailsGivenNullPtr)209 TEST_F(BigNumTest, WriteFailsGivenNullPtr) {
210 BigNum* bn = nullptr;
211 BigNumStr str = {0};
212 THROW_ON_EPIDERR(NewBigNum(sizeof(BigNumStr), &bn));
213 EXPECT_EQ(kEpidBadArgErr, WriteBigNum(NULL, sizeof(str), &str));
214 EXPECT_EQ(kEpidBadArgErr, WriteBigNum(bn, 0, NULL));
215 DeleteBigNum(&bn);
216 }
217
TEST_F(BigNumTest,WriteFailsGivenTooSmallBuffer)218 TEST_F(BigNumTest, WriteFailsGivenTooSmallBuffer) {
219 BigNumStr str;
220 BigNumObj bn(this->vec_33byte_low);
221 EXPECT_NE(kEpidNoErr, WriteBigNum(bn, sizeof(str), &str));
222 }
223
TEST_F(BigNumTest,ReadCanDeSerializeBigNumStrZero)224 TEST_F(BigNumTest, ReadCanDeSerializeBigNumStrZero) {
225 BigNumObj bn_ref;
226 BigNumObj bn;
227 EXPECT_EQ(kEpidNoErr, ReadBigNum(&this->str_0, sizeof(this->str_0), bn));
228 // No way to check this yet
229 }
230
TEST_F(BigNumTest,ReadCanDeSerializeBigNum)231 TEST_F(BigNumTest, ReadCanDeSerializeBigNum) {
232 BigNumObj bn;
233 EXPECT_EQ(kEpidNoErr,
234 ReadBigNum(&this->str_large, sizeof(this->str_large), bn));
235 // No way to check this yet
236 }
237
TEST_F(BigNumTest,WriteCanSerializeBigNumZero)238 TEST_F(BigNumTest, WriteCanSerializeBigNumZero) {
239 BigNumObj bn; // defaults to 0
240 BigNumStr str;
241 EXPECT_EQ(kEpidNoErr, WriteBigNum(bn, sizeof(str), &str));
242 EXPECT_TRUE(CompareBigNumStr(&str, &this->str_0));
243 }
244
TEST_F(BigNumTest,DeSerializeFollowedBySerializeHasSameValue)245 TEST_F(BigNumTest, DeSerializeFollowedBySerializeHasSameValue) {
246 BigNumStr str;
247 BigNumObj bn;
248 EXPECT_EQ(kEpidNoErr,
249 ReadBigNum(&this->str_large, sizeof(this->str_large), bn));
250 EXPECT_EQ(kEpidNoErr, WriteBigNum(bn, sizeof(str), &str));
251 EXPECT_TRUE(CompareBigNumStr(&this->str_large, &str));
252 }
253
254 ///////////////////////////////////////////////////////////////////////
255 // Addition
256
TEST_F(BigNumTest,AddBadArgumentsFail)257 TEST_F(BigNumTest, AddBadArgumentsFail) {
258 BigNumObj bn;
259 EXPECT_NE(kEpidNoErr, BigNumAdd(nullptr, nullptr, nullptr));
260 EXPECT_NE(kEpidNoErr, BigNumAdd(bn, nullptr, nullptr));
261 EXPECT_NE(kEpidNoErr, BigNumAdd(nullptr, bn, nullptr));
262 EXPECT_NE(kEpidNoErr, BigNumAdd(nullptr, nullptr, bn));
263 EXPECT_NE(kEpidNoErr, BigNumAdd(bn, bn, nullptr));
264 EXPECT_NE(kEpidNoErr, BigNumAdd(nullptr, bn, bn));
265 EXPECT_NE(kEpidNoErr, BigNumAdd(bn, nullptr, bn));
266 }
267
TEST_F(BigNumTest,AddZeroIsIdentity)268 TEST_F(BigNumTest, AddZeroIsIdentity) {
269 BigNumObj bn;
270 BigNumObj bn_0(this->str_0);
271 BigNumObj bn_large(this->str_large);
272 EXPECT_EQ(kEpidNoErr, BigNumAdd(bn_large, bn_0, bn));
273 EXPECT_TRUE(CompareBigNum(bn, bn_large));
274 }
275
TEST_F(BigNumTest,AddOneIncrements)276 TEST_F(BigNumTest, AddOneIncrements) {
277 BigNumObj bn;
278 BigNumObj bn_1(this->str_1);
279 BigNumObj bn_large(this->str_large);
280 BigNumObj bn_large_p1(this->str_large_p1);
281 EXPECT_EQ(kEpidNoErr, BigNumAdd(bn_large, bn_1, bn));
282 EXPECT_TRUE(CompareBigNum(bn, bn_large_p1));
283 }
284
TEST_F(BigNumTest,AddOneTo32ByteInTo32BytesFails)285 TEST_F(BigNumTest, AddOneTo32ByteInTo32BytesFails) {
286 BigNumObj bn(32);
287 BigNumObj bn_1(this->str_1);
288 BigNumObj bn_32high(this->str_32byte_high);
289 EXPECT_NE(kEpidNoErr, BigNumAdd(bn_32high, bn_1, bn));
290 }
291
TEST_F(BigNumTest,AddOneTo32ByteInTo33BytesIncrements)292 TEST_F(BigNumTest, AddOneTo32ByteInTo33BytesIncrements) {
293 BigNumObj bn(33);
294 BigNumObj bn_1(this->str_1);
295 BigNumObj bn_32high(this->str_32byte_high);
296 BigNumObj bn_33low(this->vec_33byte_low);
297 EXPECT_EQ(kEpidNoErr, BigNumAdd(bn_32high, bn_1, bn));
298 EXPECT_TRUE(CompareBigNum(bn, bn_33low));
299 }
300
301 ///////////////////////////////////////////////////////////////////////
302 // Subtraction
303
TEST_F(BigNumTest,SubBadArgumentsFail)304 TEST_F(BigNumTest, SubBadArgumentsFail) {
305 BigNumObj bn;
306 EXPECT_NE(kEpidNoErr, BigNumSub(nullptr, nullptr, nullptr));
307 EXPECT_NE(kEpidNoErr, BigNumSub(bn, nullptr, nullptr));
308 EXPECT_NE(kEpidNoErr, BigNumSub(nullptr, bn, nullptr));
309 EXPECT_NE(kEpidNoErr, BigNumSub(nullptr, nullptr, bn));
310 EXPECT_NE(kEpidNoErr, BigNumSub(bn, bn, nullptr));
311 EXPECT_NE(kEpidNoErr, BigNumSub(nullptr, bn, bn));
312 EXPECT_NE(kEpidNoErr, BigNumSub(bn, nullptr, bn));
313 }
314
TEST_F(BigNumTest,SubOneFromZeroFails)315 TEST_F(BigNumTest, SubOneFromZeroFails) {
316 BigNumObj bn;
317 BigNumObj bn_0(this->str_0);
318 BigNumObj bn_1(this->str_1);
319 EXPECT_EQ(kEpidUnderflowErr, BigNumSub(bn_0, bn_1, bn));
320 }
321
TEST_F(BigNumTest,SubZeroIsIdentity)322 TEST_F(BigNumTest, SubZeroIsIdentity) {
323 BigNumObj bn;
324 BigNumObj bn_0(this->str_0);
325 BigNumObj bn_large(this->str_large);
326 EXPECT_EQ(kEpidNoErr, BigNumSub(bn_large, bn_0, bn));
327 EXPECT_TRUE(CompareBigNum(bn, bn_large));
328 }
329
TEST_F(BigNumTest,SubOneDecrements)330 TEST_F(BigNumTest, SubOneDecrements) {
331 BigNumObj bn;
332 BigNumObj bn_1(this->str_1);
333 BigNumObj bn_large(this->str_large);
334 BigNumObj bn_large_m1(this->str_large_m1);
335 EXPECT_EQ(kEpidNoErr, BigNumSub(bn_large, bn_1, bn));
336 EXPECT_TRUE(CompareBigNum(bn, bn_large_m1));
337 }
338
339 ///////////////////////////////////////////////////////////////////////
340 // Multiplication
341
TEST_F(BigNumTest,MulBadArgumentsFail)342 TEST_F(BigNumTest, MulBadArgumentsFail) {
343 BigNumObj bn;
344 EXPECT_NE(kEpidNoErr, BigNumMul(nullptr, nullptr, nullptr));
345 EXPECT_NE(kEpidNoErr, BigNumMul(bn, nullptr, nullptr));
346 EXPECT_NE(kEpidNoErr, BigNumMul(nullptr, bn, nullptr));
347 EXPECT_NE(kEpidNoErr, BigNumMul(nullptr, nullptr, bn));
348 EXPECT_NE(kEpidNoErr, BigNumMul(bn, bn, nullptr));
349 EXPECT_NE(kEpidNoErr, BigNumMul(nullptr, bn, bn));
350 EXPECT_NE(kEpidNoErr, BigNumMul(bn, nullptr, bn));
351 }
352
TEST_F(BigNumTest,MulOneIsIdentity)353 TEST_F(BigNumTest, MulOneIsIdentity) {
354 BigNumObj bn;
355 BigNumObj bn_1(this->str_1);
356 BigNumObj bn_large(this->str_large);
357 EXPECT_EQ(kEpidNoErr, BigNumMul(bn_large, bn_1, bn));
358 EXPECT_TRUE(CompareBigNum(bn, bn_large));
359 }
360
TEST_F(BigNumTest,MulTwoIsDouble)361 TEST_F(BigNumTest, MulTwoIsDouble) {
362 BigNumObj bn;
363 BigNumObj bn_2(this->str_2);
364 BigNumObj bn_big(this->str_big);
365 BigNumObj bn_2big(this->str_2big);
366 EXPECT_EQ(kEpidNoErr, BigNumMul(bn_big, bn_2, bn));
367 EXPECT_TRUE(CompareBigNum(bn, bn_2big));
368 }
369
TEST_F(BigNumTest,MulZeroIsZero)370 TEST_F(BigNumTest, MulZeroIsZero) {
371 BigNumObj bn;
372 BigNumObj bn_0(this->str_0);
373 BigNumObj bn_large(this->str_large);
374 EXPECT_EQ(kEpidNoErr, BigNumMul(bn_large, bn_0, bn));
375 EXPECT_TRUE(CompareBigNum(bn, bn_0));
376 }
377
TEST_F(BigNumTest,MulReportsErrorGivenOverflow)378 TEST_F(BigNumTest, MulReportsErrorGivenOverflow) {
379 BigNumObj bn(32);
380 BigNumObj bn_2(this->str_2);
381 BigNumObj bn_high_bit_set(this->str_32byte_high_bit_set);
382 EXPECT_EQ(kEpidBadArgErr, BigNumMul(bn_high_bit_set, bn_2, bn));
383 }
384
TEST_F(BigNumTest,MulWorksWith264BitValue)385 TEST_F(BigNumTest, MulWorksWith264BitValue) {
386 BigNumObj bn(33);
387 BigNumObj bn_2(this->str_2);
388 BigNumObj bn_high_bit_set(this->str_32byte_high_bit_set);
389 BigNumObj bn_33low(this->vec_33byte_low);
390 EXPECT_EQ(kEpidNoErr, BigNumMul(bn_high_bit_set, bn_2, bn));
391 EXPECT_TRUE(CompareBigNum(bn, bn_33low));
392 }
393
394 ///////////////////////////////////////////////////////////////////////
395 // Division
396
TEST_F(BigNumTest,DivFailsGivenNullPointer)397 TEST_F(BigNumTest, DivFailsGivenNullPointer) {
398 BigNumObj a, b, q, r;
399 EXPECT_EQ(kEpidBadArgErr, BigNumDiv(nullptr, b, q, r));
400 EXPECT_EQ(kEpidBadArgErr, BigNumDiv(a, nullptr, q, r));
401 EXPECT_EQ(kEpidBadArgErr, BigNumDiv(a, b, nullptr, r));
402 EXPECT_EQ(kEpidBadArgErr, BigNumDiv(a, b, q, nullptr));
403 }
404
TEST_F(BigNumTest,DivFailsGivenDivByZero)405 TEST_F(BigNumTest, DivFailsGivenDivByZero) {
406 BigNumObj a;
407 BigNumObj zero(this->str_0);
408 BigNumObj q, r;
409 EXPECT_EQ(kEpidBadArgErr, BigNumDiv(a, zero, q, r));
410 }
411
TEST_F(BigNumTest,DivToOneKeepsOriginal)412 TEST_F(BigNumTest, DivToOneKeepsOriginal) {
413 BigNumObj a(this->str_large);
414 BigNumObj zero(this->str_0);
415 BigNumObj one(this->str_1);
416 BigNumObj q, r;
417 EXPECT_EQ(kEpidNoErr, BigNumDiv(a, one, q, r));
418 EXPECT_TRUE(CompareBigNum(a, q));
419 EXPECT_TRUE(CompareBigNum(zero, r));
420 }
421
TEST_F(BigNumTest,DivToItselfIsIdentity)422 TEST_F(BigNumTest, DivToItselfIsIdentity) {
423 BigNumObj a(this->str_large);
424 BigNumObj zero(this->str_0);
425 BigNumObj one(this->str_1);
426 BigNumObj q, r;
427 EXPECT_EQ(kEpidNoErr, BigNumDiv(a, a, q, r));
428 EXPECT_TRUE(CompareBigNum(one, q));
429 EXPECT_TRUE(CompareBigNum(zero, r));
430 }
431
TEST_F(BigNumTest,DivOneByTwoIsZero)432 TEST_F(BigNumTest, DivOneByTwoIsZero) {
433 BigNumObj zero(this->str_0);
434 BigNumObj one(this->str_1);
435 BigNumObj two(this->str_2);
436 BigNumObj q, r;
437 EXPECT_EQ(kEpidNoErr, BigNumDiv(one, two, q, r));
438 EXPECT_TRUE(CompareBigNum(zero, q));
439 EXPECT_TRUE(CompareBigNum(one, r));
440 }
441
442 ///////////////////////////////////////////////////////////////////////
443 // IsEven
444
TEST_F(BigNumTest,IsEvenFailsGivenNullPointer)445 TEST_F(BigNumTest, IsEvenFailsGivenNullPointer) {
446 BigNumObj zero(this->str_0);
447 bool r;
448 EXPECT_EQ(kEpidBadArgErr, BigNumIsEven(nullptr, &r));
449 EXPECT_EQ(kEpidBadArgErr, BigNumIsEven(zero, nullptr));
450 }
451
TEST_F(BigNumTest,IsEvenPassesEvenNumbers)452 TEST_F(BigNumTest, IsEvenPassesEvenNumbers) {
453 BigNumObj zero(this->str_0);
454 BigNumObj two(this->str_2);
455 BigNumObj big(this->str_big);
456 bool r;
457 EXPECT_EQ(kEpidNoErr, BigNumMul(big, two, big));
458 EXPECT_EQ(kEpidNoErr, BigNumIsEven(zero, &r));
459 EXPECT_EQ(kEpidNoErr, BigNumIsEven(two, &r));
460 EXPECT_EQ(kEpidNoErr, BigNumIsEven(big, &r));
461 }
462
TEST_F(BigNumTest,IsEvenFailsOddNumbers)463 TEST_F(BigNumTest, IsEvenFailsOddNumbers) {
464 BigNumObj zero(this->str_0);
465 BigNumObj one(this->str_1);
466 BigNumObj two(this->str_2);
467 BigNumObj big(this->str_big);
468 bool r;
469 EXPECT_EQ(kEpidNoErr, BigNumMul(big, two, big));
470 EXPECT_EQ(kEpidNoErr, BigNumAdd(big, one, big));
471 EXPECT_EQ(kEpidNoErr, BigNumIsEven(one, &r));
472 EXPECT_EQ(kEpidNoErr, BigNumIsEven(big, &r));
473 }
474
475 ///////////////////////////////////////////////////////////////////////
476 // IsZero
TEST_F(BigNumTest,IsZeroFailsGivenNullPointer)477 TEST_F(BigNumTest, IsZeroFailsGivenNullPointer) {
478 BigNumObj zero(this->str_0);
479 bool r;
480 EXPECT_EQ(kEpidBadArgErr, BigNumIsZero(nullptr, &r));
481 EXPECT_EQ(kEpidBadArgErr, BigNumIsZero(zero, nullptr));
482 }
483
TEST_F(BigNumTest,IsZeroPassesZero)484 TEST_F(BigNumTest, IsZeroPassesZero) {
485 BigNumObj zero(this->str_0);
486 bool r;
487 EXPECT_EQ(kEpidNoErr, BigNumIsZero(zero, &r));
488 }
489
TEST_F(BigNumTest,IsZeroFailsNonZero)490 TEST_F(BigNumTest, IsZeroFailsNonZero) {
491 BigNumObj one(this->str_1);
492 BigNumObj two(this->str_2);
493 BigNumObj big(this->str_big);
494 bool r;
495 EXPECT_EQ(kEpidNoErr, BigNumIsZero(one, &r));
496 EXPECT_EQ(kEpidNoErr, BigNumIsZero(two, &r));
497 EXPECT_EQ(kEpidNoErr, BigNumIsZero(big, &r));
498 }
499
500 ///////////////////////////////////////////////////////////////////////
501 // Pow2N
TEST_F(BigNumTest,Pow2NFailsGivenNullPointer)502 TEST_F(BigNumTest, Pow2NFailsGivenNullPointer) {
503 EXPECT_EQ(kEpidBadArgErr, BigNumPow2N(1, nullptr));
504 }
505
TEST_F(BigNumTest,Pow2NZeroGivesOne)506 TEST_F(BigNumTest, Pow2NZeroGivesOne) {
507 BigNumObj r;
508 BigNumObj one(this->str_1);
509 EXPECT_EQ(kEpidNoErr, BigNumPow2N(0, r));
510 EXPECT_TRUE(CompareBigNum(one, r));
511 }
512
TEST_F(BigNumTest,Pow2NOneGivesTwo)513 TEST_F(BigNumTest, Pow2NOneGivesTwo) {
514 BigNumObj r;
515 BigNumObj two(this->str_2);
516 EXPECT_EQ(kEpidNoErr, BigNumPow2N(1, r));
517 EXPECT_TRUE(CompareBigNum(two, r));
518 }
519
TEST_F(BigNumTest,Pow2NGivesPow2n)520 TEST_F(BigNumTest, Pow2NGivesPow2n) {
521 unsigned int n = 2;
522 BigNumObj r;
523 BigNumObj two(this->str_2);
524 BigNumObj expect;
525 EXPECT_EQ(kEpidNoErr, BigNumMul(two, two, expect));
526 for (n = 2; n < 4; n++) {
527 EXPECT_EQ(kEpidNoErr, BigNumPow2N(n, r));
528 EXPECT_TRUE(CompareBigNum(expect, r));
529 EXPECT_EQ(kEpidNoErr, BigNumMul(expect, two, expect));
530 n++;
531 }
532 }
533
534 } // namespace
535