fq-test.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 Fq implementation.
  17. /*! \file */
  18. #include <gtest/gtest.h>
  19. #include "epid/member/tiny/math/unittests/cmp-testhelper.h"
  20. #include "epid/member/tiny/math/unittests/onetimepad.h"
  21. extern "C" {
  22. #include "epid/member/tiny/math/fq.h"
  23. #include "epid/member/tiny/math/mathtypes.h"
  24. }
  25. namespace {
  26. ////////////////////////////////////////////////////////////////////////
  27. // FqInField
  28. TEST(TinyFqTest, FqInFieldPasses) {
  29. FqElem zero = {0};
  30. FqElem q_minus_one = {{0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB,
  31. 0xEE71A49F, 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  32. EXPECT_TRUE(FqInField(&zero));
  33. EXPECT_TRUE(FqInField(&q_minus_one));
  34. }
  35. TEST(TinyFqTest, FqInFieldFails) {
  36. FqElem q = {{0xAED33013, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
  37. 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  38. EXPECT_FALSE(FqInField(&q));
  39. }
  40. ////////////////////////////////////////////////////////////////////////
  41. // FqAdd
  42. TEST(TinyFqTest, FqAddWorks) {
  43. FqElem result = {0}, expected = {0}, left = {0}, right = {0};
  44. left.limbs.word[5] = 1;
  45. right.limbs.word[5] = 2;
  46. expected.limbs.word[5] = 3;
  47. FqAdd(&result, &left, &right);
  48. EXPECT_EQ(expected, result);
  49. FqElem q_minus_one = {0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB,
  50. 0xEE71A49F, 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF};
  51. right = {2, 0, 0, 0, 0, 0, 0, 0};
  52. expected = {1, 0, 0, 0, 0, 0, 0, 0};
  53. FqAdd(&result, &q_minus_one, &right);
  54. EXPECT_EQ(expected, result);
  55. }
  56. ////////////////////////////////////////////////////////////////////////
  57. // FqSub
  58. TEST(TinyFqTest, FqSubWorks) {
  59. FqElem result = {0}, expected = {0}, left = {0}, right = {0};
  60. left.limbs.word[4] = 2;
  61. right.limbs.word[4] = 1;
  62. expected.limbs.word[4] = 1;
  63. FqSub(&result, &left, &right);
  64. EXPECT_EQ(expected, result);
  65. left = {1, 0, 0, 0, 0, 0, 0, 0};
  66. right = {2, 0, 0, 0, 0, 0, 0, 0};
  67. FqElem q_minus_one = {0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB,
  68. 0xEE71A49F, 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF};
  69. FqSub(&result, &left, &right);
  70. EXPECT_EQ(q_minus_one, result);
  71. }
  72. ////////////////////////////////////////////////////////////////////////
  73. // FqMul
  74. TEST(TinyFqTest, FqMulWorks) {
  75. FqElem left = {0x22cfd6a2, 0x23e82f1e, 0xd50e1450, 0xe853e88c,
  76. 0xafa65357, 0x4780716c, 0xffd94b0f, 0x5e643124};
  77. FqElem right = {0x848cdb73, 0x6399829e, 0xcaa20cc0, 0x1b02bff6,
  78. 0x2b477bd2, 0xf9d48534, 0xff7929a0, 0xd4745161};
  79. FqElem expected = {0x28f2f1dd, 0x2cb2b611, 0xa24767b3, 0x4e880c0e,
  80. 0xed7f7b9e, 0x6ff4a7f2, 0x25fb15d0, 0x7b8c4fed};
  81. FqElem result = {0};
  82. FqMul(&result, &left, &right);
  83. EXPECT_EQ(expected, result);
  84. }
  85. ////////////////////////////////////////////////////////////////////////
  86. // FqExp
  87. TEST(TinyFqTest, FqExpWorks) {
  88. FqElem result = {0}, expected = {0}, in = {0};
  89. VeryLargeInt exp = {0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB,
  90. 0xEE71A49F, 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF};
  91. in.limbs.word[0] = 1;
  92. expected.limbs.word[0] = 1;
  93. FqExp(&result, &in, &exp);
  94. EXPECT_EQ(expected, result);
  95. exp = {4, 0, 0, 0, 0, 0, 0, 0};
  96. in = {{0x0000007B, 0, 0, 0, 0, 0, 0, 0}};
  97. expected = {{0x0DA48871, 0, 0, 0, 0, 0, 0, 0}};
  98. FqExp(&result, &in, &exp);
  99. EXPECT_EQ(expected, result);
  100. }
  101. ////////////////////////////////////////////////////////////////////////
  102. // FqCp
  103. TEST(TinyFqTest, FqCpWorks) {
  104. FqElem a = {{0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
  105. 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  106. FqElem result = {0};
  107. FqCp(&result, &a);
  108. EXPECT_EQ(a, result);
  109. }
  110. ////////////////////////////////////////////////////////////////////////
  111. // FqIsZero
  112. TEST(TinyFqTest, FqIsZeroPasses) {
  113. FqElem zero = {0};
  114. EXPECT_TRUE(FqIsZero(&zero));
  115. }
  116. TEST(TinyFqTest, FqIsZeroFails) {
  117. FqElem non_zero = {{0, 0, 0, 0, 0, 0, 1, 0}};
  118. EXPECT_FALSE(FqIsZero(&non_zero));
  119. }
  120. ////////////////////////////////////////////////////////////////////////
  121. // FqInv
  122. TEST(TinyFqTest, FqInvWorks) {
  123. FqElem a = {{0x76abb18a, 0x92c0f7b9, 0x2c1a37e0, 0x7fdf6ca1, 0xe3401760,
  124. 0x66eb7d52, 0x918d50a7, 0x12a65bd6}};
  125. FqElem expected = {{0x5a686df6, 0x56b6ab63, 0xdf907c6f, 0x44ad8d51,
  126. 0xa5513462, 0xc597ef78, 0x93711b39, 0x15171a1e}};
  127. FqElem result;
  128. FqInv(&result, &a);
  129. EXPECT_EQ(result, expected);
  130. }
  131. ////////////////////////////////////////////////////////////////////////
  132. // FqNeg
  133. TEST(TinyFqTest, FqNegWorks) {
  134. FqElem const pairing_q = {{0xAED33013, 0xD3292DDB, 0x12980A82, 0x0CDC65FB,
  135. 0xEE71A49F, 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  136. FqElem neg_value = {0};
  137. FqElem one = {{1, 0, 0, 0, 0, 0, 0, 0}};
  138. FqElem minus_one = pairing_q;
  139. --minus_one.limbs.word[0];
  140. FqNeg(&neg_value, &one);
  141. EXPECT_EQ(minus_one, neg_value);
  142. FqElem value = {{0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
  143. 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  144. FqNeg(&neg_value, &value);
  145. FqNeg(&neg_value, &neg_value);
  146. EXPECT_EQ(value, neg_value);
  147. }
  148. ////////////////////////////////////////////////////////////////////////
  149. // FqSquare
  150. TEST(TinyFqTest, FqSquareWorks) {
  151. FqElem const pairing_q = {{0xAED33013, 0xD3292DDB, 0x12980A82, 0x0CDC65FB,
  152. 0xEE71A49F, 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  153. FqElem four = {{4, 0, 0, 0, 0, 0, 0, 0}}, result = {0};
  154. FqElem minus_two = pairing_q;
  155. minus_two.limbs.word[0] -= 2;
  156. FqSquare(&result, &minus_two);
  157. EXPECT_EQ(four, result);
  158. FqElem in = {{0x00003039, 0, 0, 0, 0, 0, 0, 0}};
  159. FqElem expected = {{0x09156CB1, 0, 0, 0, 0, 0, 0, 0}};
  160. FqSquare(&result, &in);
  161. EXPECT_EQ(expected, result);
  162. }
  163. ////////////////////////////////////////////////////////////////////////
  164. // FqClear
  165. TEST(TinyFqTest, FqClearWorks) {
  166. FqElem zero = {0};
  167. FqElem a = {{0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
  168. 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  169. FqClear(&a);
  170. EXPECT_EQ(zero, a);
  171. }
  172. ////////////////////////////////////////////////////////////////////////
  173. // FqSet
  174. TEST(TinyFqTest, FqSetWorks) {
  175. uint32_t small = 0xffffffff;
  176. FqElem expected = {{small, 0, 0, 0, 0, 0, 0, 0}};
  177. FqElem result = {{0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
  178. 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  179. FqSet(&result, small);
  180. EXPECT_EQ(expected, result);
  181. }
  182. ////////////////////////////////////////////////////////////////////////
  183. // FqEq
  184. TEST(TinyFqTest, FqEqPasses) {
  185. FqElem a = {{0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
  186. 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  187. FqElem c = {{0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
  188. 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  189. EXPECT_TRUE(FqEq(&a, &c));
  190. }
  191. TEST(TinyFqTest, FqEqFails) {
  192. FqElem a = {{0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
  193. 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  194. FqElem b = {{0, 0, 0, 0, 0, 0, 1, 0}};
  195. EXPECT_FALSE(FqEq(&a, &b));
  196. }
  197. ////////////////////////////////////////////////////////////////////////
  198. // FqCondSet
  199. TEST(TinyFqTest, FqCondSetWorksForTrue) {
  200. FqElem a = {{0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
  201. 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  202. FqElem b = {{0, 0, 0, 0, 0, 0, 1, 0}};
  203. FqElem result = {0};
  204. FqCondSet(&result, &a, &b, true);
  205. EXPECT_EQ(a, result);
  206. }
  207. TEST(TinyFqTest, FqCondSetWorksForFalse) {
  208. FqElem a = {{0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
  209. 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  210. FqElem b = {{0, 0, 0, 0, 0, 0, 1, 0}};
  211. FqElem result = {0};
  212. FqCondSet(&result, &a, &b, false);
  213. EXPECT_EQ(b, result);
  214. }
  215. ////////////////////////////////////////////////////////////////////////
  216. // FqSqrt
  217. TEST(TinyFqTest, FqSqrtWorks) {
  218. FqElem actual = {0};
  219. FqElem in = {0x02869F4A, 0xDF28D8AC, 0x2B4BD20F, 0xB158EA77,
  220. 0x89D86073, 0x4D6F2CE1, 0x0211F496, 0x9C977A80};
  221. FqElem expected = {0x0E41765B, 0x7E2AEDF6, 0x7C01BC1B, 0xB1C0F3E4,
  222. 0x95CB5637, 0x8B275494, 0x3FBF1556, 0x8DAE1450};
  223. EXPECT_TRUE(FqSqrt(&actual, &in));
  224. EXPECT_EQ(expected, actual);
  225. }
  226. TEST(TinyFqTest, FqSqrtWorksForFour) {
  227. FqElem actual = {0};
  228. FqElem in = {0x00000004, 0x00000000, 0x00000000, 0x00000000,
  229. 0x00000000, 0x00000000, 0x00000000, 0x00000000};
  230. FqElem expected = {0xAED33011, 0xD3292DDB, 0x12980A82, 0x0CDC65FB,
  231. 0xEE71A49F, 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF};
  232. EXPECT_TRUE(FqSqrt(&actual, &in));
  233. EXPECT_EQ(expected, actual);
  234. }
  235. TEST(TinyFqTest, FqSqrtFailsForFive) {
  236. FqElem actual = {0};
  237. FqElem in = {0x00000005, 0x00000000, 0x00000000, 0x00000000,
  238. 0x00000000, 0x00000000, 0x00000000, 0x00000000};
  239. EXPECT_FALSE(FqSqrt(&actual, &in));
  240. }
  241. ////////////////////////////////////////////////////////////////////////
  242. // FqRand
  243. TEST(TinyFqTest, FqRandConsumes384BitsOfEntropy) {
  244. OneTimePad otp(64);
  245. FqElem actual = {0};
  246. EXPECT_TRUE(FqRand(&actual, OneTimePad::Generate, &otp));
  247. EXPECT_EQ(384u, otp.BitsConsumed());
  248. }
  249. TEST(TinyFqTest, FqRandWorks) {
  250. OneTimePad otp({// slen bits
  251. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  252. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  253. // q + 1
  254. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xF0, 0xCD, 0x46, 0xE5,
  255. 0xF2, 0x5E, 0xEE, 0x71, 0xA4, 0x9F, 0x0C, 0xDC, 0x65, 0xFB,
  256. 0x12, 0x98, 0x0A, 0x82, 0xD3, 0x29, 0x2D, 0xDB, 0xAE, 0xD3,
  257. 0x30, 0x14});
  258. FqElem expected = {{1, 0, 0, 0, 0, 0, 0, 0}};
  259. FqElem actual = {0};
  260. EXPECT_TRUE(FqRand(&actual, OneTimePad::Generate, &otp));
  261. EXPECT_EQ(expected, actual);
  262. }
  263. ////////////////////////////////////////////////////////////////////////
  264. // FqFromHash
  265. TEST(TinyFqTest, FqFromHashWorks) {
  266. FqElem q_mod_q;
  267. FqElem zero = {0};
  268. uint8_t q_str[32] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xF0, 0xCD,
  269. 0x46, 0xE5, 0xF2, 0x5E, 0xEE, 0x71, 0xA4, 0x9F,
  270. 0x0C, 0xDC, 0x65, 0xFB, 0x12, 0x98, 0x0A, 0x82,
  271. 0xD3, 0x29, 0x2D, 0xDB, 0xAE, 0xD3, 0x30, 0x13};
  272. FqFromHash(&q_mod_q, q_str, sizeof(q_str));
  273. EXPECT_EQ(zero, q_mod_q);
  274. FqElem one = {{1, 0, 0, 0, 0, 0, 0, 0}};
  275. uint8_t one_str[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  276. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
  277. FqElem one_mod_q;
  278. FqFromHash(&one_mod_q, one_str, sizeof(one_str));
  279. EXPECT_EQ(one, one_mod_q);
  280. }
  281. } // namespace