bignum-test.cc 18 KB

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