bignum-test.cc 18 KB

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