fp-test.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 Fp 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/fp.h"
  23. #include "epid/member/tiny/math/mathtypes.h"
  24. }
  25. namespace {
  26. ////////////////////////////////////////////////////////////////////////
  27. // FpInField
  28. TEST(TinyFpTest, FpInFieldPasses) {
  29. FpElem zero = {0};
  30. FpElem p_minus_one = {{0xD10B500C, 0xF62D536C, 0x1299921A, 0x0CDC65FB,
  31. 0xEE71A49E, 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  32. EXPECT_TRUE(FpInField(&zero));
  33. EXPECT_TRUE(FpInField(&p_minus_one));
  34. }
  35. TEST(TinyFpTest, FpInFieldFails) {
  36. FpElem p = {{0xD10B500D, 0xF62D536C, 0x1299921A, 0x0CDC65FB, 0xEE71A49E,
  37. 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  38. EXPECT_FALSE(FpInField(&p));
  39. }
  40. ////////////////////////////////////////////////////////////////////////
  41. // FpAdd
  42. TEST(TinyFpTest, FpAddWorks) {
  43. FpElem 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. FpAdd(&result, &left, &right);
  48. EXPECT_EQ(expected, result);
  49. FpElem p_minus_one = {0xD10B500C, 0xF62D536C, 0x1299921A, 0x0CDC65FB,
  50. 0xEE71A49E, 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF};
  51. right = {2, 0, 0, 0, 0, 0, 0, 0};
  52. expected = {1, 0, 0, 0, 0, 0, 0, 0};
  53. FpAdd(&result, &p_minus_one, &right);
  54. EXPECT_EQ(expected, result);
  55. }
  56. ////////////////////////////////////////////////////////////////////////
  57. // FpMul
  58. TEST(TinyFpTest, FpMultWorks) {
  59. FpElem left = {0x22cfd6a2, 0x23e82f1e, 0xd50e1450, 0xe853e88c,
  60. 0xafa65357, 0x4780716c, 0xffd94b0f, 0x5e643124};
  61. FpElem right = {0x848cdb73, 0x6399829e, 0xcaa20cc0, 0x1b02bff6,
  62. 0x2b477bd2, 0xf9d48534, 0xff7929a0, 0xd4745161};
  63. FpElem expected = {0x3f172ebf, 0xf2219fce, 0x73591802, 0x7a7dbc7f,
  64. 0xf82ed0df, 0xb8c0c56d, 0x3395ff68, 0x83d64983};
  65. FpElem result = {0};
  66. FpMul(&result, &left, &right);
  67. EXPECT_EQ(expected, result);
  68. }
  69. ////////////////////////////////////////////////////////////////////////
  70. // FpSub
  71. TEST(TinyFpTest, FpSubWorks) {
  72. FpElem result = {0}, expected = {0}, left = {0}, right = {0};
  73. left.limbs.word[4] = 2;
  74. right.limbs.word[4] = 1;
  75. expected.limbs.word[4] = 1;
  76. FpSub(&result, &left, &right);
  77. EXPECT_EQ(expected, result);
  78. left = {1, 0, 0, 0, 0, 0, 0, 0};
  79. right = {2, 0, 0, 0, 0, 0, 0, 0};
  80. FpElem p_minus_one = {0xD10B500C, 0xF62D536C, 0x1299921A, 0x0CDC65FB,
  81. 0xEE71A49E, 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF};
  82. FpSub(&result, &left, &right);
  83. EXPECT_EQ(p_minus_one, result);
  84. }
  85. ////////////////////////////////////////////////////////////////////////
  86. // FpExp
  87. TEST(TinyFpTest, FpExpWorks) {
  88. FpElem result = {0}, expected = {0}, in = {0};
  89. VeryLargeInt exp = {0xD10B500C, 0xF62D536C, 0x1299921A, 0x0CDC65FB,
  90. 0xEE71A49E, 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF};
  91. in.limbs.word[0] = 1;
  92. expected.limbs.word[0] = 1;
  93. FpExp(&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. FpExp(&result, &in, &exp);
  99. EXPECT_EQ(expected, result);
  100. }
  101. ////////////////////////////////////////////////////////////////////////
  102. // FpNeg
  103. TEST(TinyFpTest, FpNegWorks) {
  104. FpElem const pairing_p = {{0xD10B500D, 0xF62D536C, 0x1299921A, 0x0CDC65FB,
  105. 0xEE71A49E, 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  106. FpElem neg_value = {0};
  107. FpElem one = {{1, 0, 0, 0, 0, 0, 0, 0}};
  108. FpElem minus_one = pairing_p;
  109. --minus_one.limbs.word[0];
  110. FpNeg(&neg_value, &one);
  111. EXPECT_EQ(minus_one, neg_value);
  112. FpElem value = {{0xD10B500C, 0xF62D536C, 0x1299921A, 0x0CDC65FB, 0xEE71A49E,
  113. 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  114. FpNeg(&neg_value, &value);
  115. FpNeg(&neg_value, &neg_value);
  116. EXPECT_EQ(value, neg_value);
  117. }
  118. ////////////////////////////////////////////////////////////////////////
  119. // FpEq
  120. TEST(TinyFpTest, FpEqPasses) {
  121. FpElem a = {{0xD10B500C, 0xF62D536C, 0x1299921A, 0x0CDC65FB, 0xEE71A49E,
  122. 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  123. FpElem c = {{0xD10B500C, 0xF62D536C, 0x1299921A, 0x0CDC65FB, 0xEE71A49E,
  124. 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  125. EXPECT_TRUE(FpEq(&a, &c));
  126. }
  127. TEST(TinyFpTest, FpEqFails) {
  128. FpElem a = {{0xD10B500C, 0xF62D536C, 0x1299921A, 0x0CDC65FB, 0xEE71A49E,
  129. 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  130. FpElem b = {{0, 0, 0, 0, 0, 0, 1, 0}};
  131. EXPECT_FALSE(FpEq(&a, &b));
  132. }
  133. ////////////////////////////////////////////////////////////////////////
  134. // FpInv
  135. TEST(TinyFpTest, FpInvWorks) {
  136. FpElem a = {{0x22cfd6a2, 0x23e82f1e, 0xd50e1450, 0xe853e88c, 0xafa65357,
  137. 0x4780716c, 0xffd94b0f, 0x5e643124}};
  138. FpElem expected = {{0xd97deaf7, 0xa6011d83, 0x3c381713, 0x92472e34,
  139. 0x997861e8, 0xc1dfdc87, 0x157eb11b, 0xc9cd1238}};
  140. FpElem result;
  141. FpInv(&result, &a);
  142. EXPECT_EQ(result, expected);
  143. }
  144. ////////////////////////////////////////////////////////////////////////
  145. // FpRand
  146. TEST(TinyFpTest, FpRandConsumes384BitsOfEntropy) {
  147. OneTimePad otp(64);
  148. FpElem actual = {0};
  149. EXPECT_TRUE(FpRand(&actual, OneTimePad::Generate, &otp));
  150. EXPECT_EQ(384u, otp.BitsConsumed());
  151. }
  152. TEST(TinyFpTest, FpRandWorks) {
  153. OneTimePad otp({// slen bits
  154. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  155. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  156. // p + 1
  157. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xF0, 0xCD, 0x46, 0xE5,
  158. 0xF2, 0x5E, 0xEE, 0x71, 0xA4, 0x9E, 0x0C, 0xDC, 0x65, 0xFB,
  159. 0x12, 0x99, 0x92, 0x1A, 0xF6, 0x2D, 0x53, 0x6C, 0xD1, 0x0B,
  160. 0x50, 0x0E});
  161. FpElem expected = {{1, 0, 0, 0, 0, 0, 0, 0}};
  162. FpElem actual = {0};
  163. EXPECT_TRUE(FpRand(&actual, OneTimePad::Generate, &otp));
  164. EXPECT_EQ(expected, actual);
  165. }
  166. ////////////////////////////////////////////////////////////////////////
  167. // FpRandNonzero
  168. TEST(TinyFpTest, FpRandNonzeroConsumes384BitsOfEntropy) {
  169. OneTimePad otp(64);
  170. FpElem actual = {0};
  171. EXPECT_TRUE(FpRandNonzero(&actual, OneTimePad::Generate, &otp));
  172. EXPECT_EQ(384u, otp.BitsConsumed());
  173. }
  174. TEST(TinyFpTest, FpRandNonzeroWorks) {
  175. OneTimePad otp({// slen bits
  176. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  177. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  178. // p - 1
  179. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xF0, 0xCD, 0x46, 0xE5,
  180. 0xF2, 0x5E, 0xEE, 0x71, 0xA4, 0x9E, 0x0C, 0xDC, 0x65, 0xFB,
  181. 0x12, 0x99, 0x92, 0x1A, 0xF6, 0x2D, 0x53, 0x6C, 0xD1, 0x0B,
  182. 0x50, 0x0C});
  183. FpElem expected = {{1, 0, 0, 0, 0, 0, 0, 0}};
  184. FpElem actual = {0};
  185. EXPECT_TRUE(FpRandNonzero(&actual, OneTimePad::Generate, &otp));
  186. EXPECT_EQ(expected, actual);
  187. }
  188. ////////////////////////////////////////////////////////////////////////
  189. // FpClear
  190. TEST(TinyFpTest, FpClearWorks) {
  191. FpElem zero = {0};
  192. FpElem a = {{0xD10B500C, 0xF62D536C, 0x1299921A, 0x0CDC65FB, 0xEE71A49E,
  193. 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  194. FpClear(&a);
  195. EXPECT_EQ(zero, a);
  196. }
  197. ////////////////////////////////////////////////////////////////////////
  198. // FpSet
  199. TEST(TinyFpTest, FpSetWorks) {
  200. uint32_t small = 0xffffffff;
  201. FpElem expected = {{small, 0, 0, 0, 0, 0, 0, 0}};
  202. FpElem result = {{0xD10B500C, 0xF62D536C, 0x1299921A, 0x0CDC65FB, 0xEE71A49E,
  203. 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  204. FpSet(&result, small);
  205. EXPECT_EQ(expected, result);
  206. }
  207. ////////////////////////////////////////////////////////////////////////
  208. // FpFromHash
  209. TEST(TinyFpTest, FpFromHashWorks) {
  210. FpElem p_mod_p;
  211. FpElem zero = {0};
  212. uint8_t p_str[32] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xF0, 0xCD,
  213. 0x46, 0xE5, 0xF2, 0x5E, 0xEE, 0x71, 0xA4, 0x9E,
  214. 0x0C, 0xDC, 0x65, 0xFB, 0x12, 0x99, 0x92, 0x1A,
  215. 0xF6, 0x2D, 0x53, 0x6C, 0xD1, 0x0B, 0x50, 0x0D};
  216. FpFromHash(&p_mod_p, p_str, sizeof(p_str));
  217. EXPECT_EQ(zero, p_mod_p);
  218. FpElem one = {{1, 0, 0, 0, 0, 0, 0, 0}};
  219. uint8_t one_str[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  220. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
  221. FpElem one_mod_p;
  222. FpFromHash(&one_mod_p, one_str, sizeof(one_str));
  223. EXPECT_EQ(one, one_mod_p);
  224. }
  225. } // namespace