vli-test.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*############################################################################
  2. # Copyright 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. /// Unit tests of large integer implementation.
  17. /*! \file */
  18. #include <gtest/gtest.h>
  19. #include <limits.h> // for CHAR_BIT
  20. #include <cstring>
  21. #include <random>
  22. #include "epid/member/tiny/math/unittests/cmp-testhelper.h"
  23. #include "epid/member/tiny/math/unittests/onetimepad.h"
  24. extern "C" {
  25. #include "epid/member/tiny/math/mathtypes.h"
  26. #include "epid/member/tiny/math/vli.h"
  27. }
  28. namespace {
  29. ////////////////////////////////////////////////////////////////////////
  30. // VliAdd
  31. TEST(TinyVliTest, VliAddWorks) {
  32. VeryLargeInt result = {0};
  33. VeryLargeInt expected = {0};
  34. VeryLargeInt left = {0};
  35. VeryLargeInt right = {0};
  36. left.word[0] = 1;
  37. right.word[0] = 2;
  38. expected.word[0] = 3;
  39. VliAdd(&result, &left, &right);
  40. EXPECT_EQ(expected, result);
  41. }
  42. TEST(TinyVliTest, VliAddCalculatesCarry) {
  43. VeryLargeInt left = {{0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
  44. 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff}};
  45. VeryLargeInt right = {{0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1}};
  46. VeryLargeInt expected = {{0x0, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1}};
  47. uint32_t expected_carry = 0x1;
  48. uint32_t carry = 0;
  49. VeryLargeInt result = {0};
  50. carry = VliAdd(&result, &left, &right);
  51. EXPECT_EQ(expected, result);
  52. EXPECT_EQ(expected_carry, carry);
  53. }
  54. ////////////////////////////////////////////////////////////////////////
  55. // VliMul
  56. TEST(TinyVliTest, VliMultWorks) {
  57. VeryLargeIntProduct result = {0};
  58. VeryLargeIntProduct expected = {0};
  59. VeryLargeInt left = {0}, right = {0};
  60. left.word[0] = 2;
  61. right.word[0] = 2;
  62. expected.word[0] = 4;
  63. VliMul(&result, &left, &right);
  64. EXPECT_EQ(expected, result);
  65. }
  66. TEST(TinyVliTest, VliMultWorksWithOverflow) {
  67. VeryLargeIntProduct result = {0};
  68. VeryLargeIntProduct expected = {
  69. {0xfffffffe, 0xfffffffd, 0xfffffffd, 0xfffffffd, 0xfffffffd, 0xfffffffd,
  70. 0xfffffffd, 0xfffffffd, 0x1, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2}};
  71. VeryLargeInt left = {{0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
  72. 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff}};
  73. VeryLargeInt right = {{0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2}};
  74. VliMul(&result, &left, &right);
  75. EXPECT_EQ(expected, result);
  76. }
  77. ////////////////////////////////////////////////////////////////////////
  78. // VliRShift
  79. TEST(TinyVliTest, VliRShiftWorks) {
  80. VeryLargeInt result = {0}, expected = {0};
  81. VeryLargeInt in = {0};
  82. uint32_t shift = 1;
  83. in.word[0] = 4;
  84. expected.word[0] = 2;
  85. VliRShift(&result, &in, shift);
  86. EXPECT_EQ(expected, result);
  87. }
  88. TEST(TinyVliTest, VliRShiftWorksWithOverlap) {
  89. VeryLargeInt result = {0}, expected = {0};
  90. VeryLargeInt in = {0};
  91. uint32_t shift = 4;
  92. in.word[0] = 0x00000008;
  93. in.word[1] = 0xffffffff;
  94. expected.word[0] = 0xf0000000;
  95. expected.word[1] = 0x0fffffff;
  96. VliRShift(&result, &in, shift);
  97. EXPECT_EQ(expected, result);
  98. }
  99. ////////////////////////////////////////////////////////////////////////
  100. // VliSub
  101. TEST(TinyVliTest, VliSubWorks) {
  102. VeryLargeInt result = {0}, expected = {0};
  103. VeryLargeInt left = {0}, right = {0};
  104. uint32_t borrow = 0;
  105. uint32_t expected_borrow = 0;
  106. left.word[0] = 4;
  107. right.word[0] = 2;
  108. expected.word[0] = 2;
  109. borrow = VliSub(&result, &left, &right);
  110. EXPECT_EQ(expected, result);
  111. EXPECT_EQ(expected_borrow, borrow);
  112. }
  113. TEST(TinyVliTest, VliSubWorksWithBorrow) {
  114. VeryLargeInt result = {0};
  115. VeryLargeInt expected = {{0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
  116. 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff}};
  117. VeryLargeInt left = {0}, right = {0};
  118. uint32_t borrow = 0;
  119. uint32_t expected_borrow = 1;
  120. left.word[0] = 2;
  121. right.word[0] = 3;
  122. borrow = VliSub(&result, &left, &right);
  123. EXPECT_EQ(expected, result);
  124. EXPECT_EQ(expected_borrow, borrow);
  125. }
  126. ////////////////////////////////////////////////////////////////////////
  127. // VliSet
  128. TEST(TinyVliTest, VliSetWorks) {
  129. VeryLargeInt result = {0};
  130. VeryLargeInt in = {{0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
  131. 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff}};
  132. VliSet(&result, &in);
  133. EXPECT_EQ(in, result);
  134. }
  135. ////////////////////////////////////////////////////////////////////////
  136. // VliClear
  137. TEST(TinyVliTest, VliClearWorks) {
  138. VeryLargeInt expected = {0};
  139. VeryLargeInt in_out = {{0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
  140. 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff}};
  141. VliClear(&in_out);
  142. EXPECT_EQ(expected, in_out);
  143. }
  144. ////////////////////////////////////////////////////////////////////////
  145. // VliIsZero
  146. TEST(TinyVliTest, VliIsZeroAcceptsZero) {
  147. int is_zero = 0;
  148. VeryLargeInt in_zero = {0};
  149. is_zero = VliIsZero(&in_zero);
  150. EXPECT_TRUE(is_zero);
  151. }
  152. TEST(TinyVliTest, VliIsZeroRejectsNonZero) {
  153. int is_zero = 0;
  154. VeryLargeInt in_nonzero = {{0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
  155. 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff}};
  156. is_zero = VliIsZero(&in_nonzero);
  157. EXPECT_FALSE(is_zero);
  158. }
  159. ////////////////////////////////////////////////////////////////////////
  160. // VliCondSet
  161. TEST(TinyVliTest, VliCondSetWorksForTrue) {
  162. VeryLargeInt result = {0};
  163. VeryLargeInt true_val = {{0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
  164. 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff}};
  165. VeryLargeInt false_val = {{0xaaaaaaaa, 0xaaaaaaaa, 0xaaaaaaaa, 0xaaaaaaaa,
  166. 0xaaaaaaaa, 0xaaaaaaaa, 0xaaaaaaaa, 0xaaaaaaaa}};
  167. VliCondSet(&result, &true_val, &false_val, 1);
  168. EXPECT_EQ(true_val, result);
  169. }
  170. TEST(TinyVliTest, VliCondSetWorksForFalse) {
  171. VeryLargeInt result = {0};
  172. VeryLargeInt true_val = {{0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
  173. 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff}};
  174. VeryLargeInt false_val = {{0xaaaaaaaa, 0xaaaaaaaa, 0xaaaaaaaa, 0xaaaaaaaa,
  175. 0xaaaaaaaa, 0xaaaaaaaa, 0xaaaaaaaa, 0xaaaaaaaa}};
  176. VliCondSet(&result, &true_val, &false_val, 0);
  177. EXPECT_EQ(false_val, result);
  178. }
  179. ////////////////////////////////////////////////////////////////////////
  180. // VliTestBit
  181. TEST(TinyVliTest, VliTestBitWorks) {
  182. VeryLargeInt in = {0};
  183. uint32_t bit_set = 0;
  184. in.word[0] = 4;
  185. bit_set = VliTestBit(&in, 1);
  186. EXPECT_EQ((uint32_t)0, bit_set);
  187. bit_set = VliTestBit(&in, 2);
  188. EXPECT_EQ((uint32_t)1, bit_set);
  189. }
  190. ////////////////////////////////////////////////////////////////////////
  191. // VliRand
  192. TEST(TinyVliTest, VliRandWorks) {
  193. OneTimePad my_prng;
  194. VeryLargeInt expected_rand_val1 = {{1, 0, 0, 0, 0, 0, 0, 0}};
  195. my_prng.InitUint8({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  196. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1});
  197. VeryLargeInt rand_val1 = {0};
  198. EXPECT_TRUE(VliRand(&rand_val1, &OneTimePad::Generate, &my_prng));
  199. EXPECT_EQ(expected_rand_val1, rand_val1);
  200. EXPECT_EQ(256u, my_prng.BitsConsumed());
  201. VeryLargeInt expected_rand_val2 = {{0x1c6f5a0f, 0xeaa878b3, 0xc71dab6b,
  202. 0x1a101ad6, 0x1fe6394f, 0x1bec36ab,
  203. 0x07a3e97f, 0x36507914}};
  204. VeryLargeInt rand_val2 = {0};
  205. my_prng.InitUint32({0x14795036, 0x7fe9a307, 0xab36ec1b, 0x4f39e61f,
  206. 0xd61a101a, 0x6bab1dc7, 0xb378a8ea, 0x0f5a6f1c});
  207. EXPECT_TRUE(VliRand(&rand_val2, &OneTimePad::Generate, &my_prng));
  208. EXPECT_EQ(expected_rand_val2, rand_val2);
  209. EXPECT_EQ(256u, my_prng.BitsConsumed());
  210. }
  211. ////////////////////////////////////////////////////////////////////////
  212. // VliCmp
  213. TEST(TinyVliTest, VliCmpWorksForLessThan) {
  214. VeryLargeInt in_val1 = {0};
  215. VeryLargeInt in_val2 = {0};
  216. int res = 0;
  217. in_val1.word[0] = 1;
  218. in_val2.word[0] = 2;
  219. res = VliCmp(&in_val1, &in_val2);
  220. EXPECT_EQ(-1, res);
  221. }
  222. TEST(TinyVliTest, VliCmpWorksForEqual) {
  223. VeryLargeInt in_val1 = {0};
  224. VeryLargeInt in_val2 = {0};
  225. int res = 0;
  226. in_val1.word[0] = 2;
  227. in_val2.word[0] = 2;
  228. res = VliCmp(&in_val1, &in_val2);
  229. EXPECT_EQ(0, res);
  230. }
  231. TEST(TinyVliTest, VliCmpWorksGreaterThan) {
  232. VeryLargeInt in_val1 = {0};
  233. VeryLargeInt in_val2 = {0};
  234. int res = 0;
  235. in_val1.word[0] = 1;
  236. in_val2.word[0] = 2;
  237. res = VliCmp(&in_val2, &in_val1);
  238. EXPECT_EQ(1, res);
  239. }
  240. ////////////////////////////////////////////////////////////////////////
  241. // VliModAdd
  242. TEST(TinyVliTest, VliModAddWorks) {
  243. VeryLargeInt result = {0};
  244. VeryLargeInt left = {0};
  245. VeryLargeInt right = {0};
  246. VeryLargeInt expected = {0};
  247. VeryLargeInt mod = {0xAED33013, 0xD3292DDB, 0x12980A82, 0x0CDC65FB,
  248. 0xEE71A49F, 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF};
  249. left.word[0] = 0x9;
  250. right.word[0] = 0x8;
  251. expected.word[0] = 0x11;
  252. VliModAdd(&result, &left, &right, &mod);
  253. EXPECT_EQ(expected, result);
  254. }
  255. ////////////////////////////////////////////////////////////////////////
  256. // VliModSub
  257. TEST(TinyVliTest, VliModSubWorks) {
  258. VeryLargeInt result = {0};
  259. VeryLargeInt left = {0};
  260. VeryLargeInt right = {0};
  261. VeryLargeInt expected = {0};
  262. VeryLargeInt mod = {0xAED33013, 0xD3292DDB, 0x12980A82, 0x0CDC65FB,
  263. 0xEE71A49F, 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF};
  264. left.word[0] = 0x18;
  265. right.word[0] = 0x12;
  266. expected.word[0] = 0x6;
  267. VliModSub(&result, &left, &right, &mod);
  268. EXPECT_EQ(expected, result);
  269. }
  270. ////////////////////////////////////////////////////////////////////////
  271. // VliModMul
  272. TEST(TinyVliTest, VliModMultWorks) {
  273. VeryLargeInt result = {0};
  274. VeryLargeInt left = {0};
  275. VeryLargeInt right = {0};
  276. VeryLargeInt expected = {0};
  277. VeryLargeInt mod = {0xAED33013, 0xD3292DDB, 0x12980A82, 0x0CDC65FB,
  278. 0xEE71A49F, 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF};
  279. left.word[0] = 0x10;
  280. right.word[0] = 0x2;
  281. expected.word[0] = 0x20;
  282. VliModMul(&result, &left, &right, &mod);
  283. EXPECT_EQ(expected, result);
  284. }
  285. ////////////////////////////////////////////////////////////////////////
  286. // VliModExp
  287. TEST(TinyVliTest, VliModExpWorks) {
  288. VeryLargeInt result = {0};
  289. VeryLargeInt base = {0};
  290. VeryLargeInt exp = {0};
  291. VeryLargeInt mod = {0xAED33013, 0xD3292DDB, 0x12980A82, 0x0CDC65FB,
  292. 0xEE71A49F, 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF};
  293. VeryLargeInt expected = {0};
  294. base.word[0] = 0x4;
  295. exp.word[0] = 0x2;
  296. expected.word[0] = 0x10;
  297. VliModExp(&result, &base, &exp, &mod);
  298. EXPECT_EQ(expected, result);
  299. }
  300. ////////////////////////////////////////////////////////////////////////
  301. // VliModInv
  302. TEST(TinyVliTest, VliModInvWorks) {
  303. VeryLargeInt a = {0x76abb18a, 0x92c0f7b9, 0x2c1a37e0, 0x7fdf6ca1,
  304. 0xe3401760, 0x66eb7d52, 0x918d50a7, 0x12a65bd6};
  305. VeryLargeInt q = {0xAED33013, 0xD3292DDB, 0x12980A82, 0x0CDC65FB,
  306. 0xEE71A49F, 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF};
  307. VeryLargeInt expected = {0x5a686df6, 0x56b6ab63, 0xdf907c6f, 0x44ad8d51,
  308. 0xa5513462, 0xc597ef78, 0x93711b39, 0x15171a1e};
  309. VeryLargeInt result;
  310. VliModInv(&result, &a, &q);
  311. EXPECT_EQ(result, expected);
  312. }
  313. ////////////////////////////////////////////////////////////////////////
  314. // VliModSquare
  315. TEST(TinyVliTest, VliModSquareWorks) {
  316. VeryLargeInt result = {0};
  317. VeryLargeInt input = {0};
  318. VeryLargeInt mod = {0xAED33013, 0xD3292DDB, 0x12980A82, 0x0CDC65FB,
  319. 0xEE71A49F, 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF};
  320. VeryLargeInt expected = {0};
  321. input.word[0] = 0x4;
  322. expected.word[0] = 0x10;
  323. VliModSquare(&result, &input, &mod);
  324. EXPECT_EQ(expected, result);
  325. }
  326. ////////////////////////////////////////////////////////////////////////
  327. // VliModBarrett
  328. TEST(TinyVliTest, VliModBarrettWorks) {
  329. VeryLargeInt result = {0};
  330. VeryLargeIntProduct product = {0xAED33013, 0xD3292DDB, 0x12980A82, 0x0CDC65FB,
  331. 0xEE71A49F, 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF,
  332. 0x0, 0x0, 0x0, 0x0,
  333. 0x0, 0x0, 0x0, 0x0};
  334. VeryLargeInt mod = {0xAED33013, 0xD3292DDB, 0x12980A82, 0x0CDC65FB,
  335. 0xEE71A49F, 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF};
  336. VeryLargeInt expected = {0};
  337. product.word[0] += 0xF;
  338. expected.word[0] = 0xF;
  339. VliModBarrett(&result, &product, &mod);
  340. EXPECT_EQ(expected, result);
  341. }
  342. } // namespace