finitefield-test.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 FiniteField unit tests.
  19. */
  20. #include "epid/common-testhelper/epid_gtest-testhelper.h"
  21. #include "gtest/gtest.h"
  22. #include "epid/common-testhelper/errors-testhelper.h"
  23. #include "epid/common-testhelper/ffelement_wrapper-testhelper.h"
  24. #include "epid/common-testhelper/finite_field_wrapper-testhelper.h"
  25. extern "C" {
  26. #include "epid/common/math/finitefield.h"
  27. }
  28. #ifndef COUNT_OF
  29. #define COUNT_OF(a) (sizeof(a) / sizeof((a)[0]))
  30. #endif // COUNT_OF
  31. namespace {
  32. /// Intel(R) EPID 2.0 parameters q, beta, xi and v
  33. BigNumStr q = {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xF0, 0xCD,
  34. 0x46, 0xE5, 0xF2, 0x5E, 0xEE, 0x71, 0xA4, 0x9F,
  35. 0x0C, 0xDC, 0x65, 0xFB, 0x12, 0x98, 0x0A, 0x82,
  36. 0xD3, 0x29, 0x2D, 0xDB, 0xAE, 0xD3, 0x30, 0x13}};
  37. FqElemStr beta = {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xF0, 0xCD,
  38. 0x46, 0xE5, 0xF2, 0x5E, 0xEE, 0x71, 0xA4, 0x9F,
  39. 0x0C, 0xDC, 0x65, 0xFB, 0x12, 0x98, 0x0A, 0x82,
  40. 0xD3, 0x29, 0x2D, 0xDB, 0xAE, 0xD3, 0x30, 0x12}};
  41. Fq2ElemStr xi = {
  42. {{{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  43. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  44. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}}},
  45. {{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}}}}};
  48. Fq6ElemStr v = {
  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. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  53. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  54. 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, 0x00, 0x00,
  57. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
  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, 0x00}}},
  61. {{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  62. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  63. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  64. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  65. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  66. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}}}};
  67. FqElemStr qnr = {{0x08, 0x66, 0xA7, 0x67, 0x36, 0x6E, 0x62, 0x71,
  68. 0xB7, 0xA6, 0x52, 0x94, 0x8F, 0xFB, 0x25, 0x9E,
  69. 0xE6, 0x4F, 0x25, 0xE5, 0x26, 0x9A, 0x2B, 0x6E,
  70. 0x7E, 0xF8, 0xA6, 0x39, 0xAE, 0x46, 0xAA, 0x24}};
  71. const BigNumStr coeffs[3] = {
  72. {{{0x02, 0x16, 0x7A, 0x61, 0x53, 0xDD, 0xF6, 0xE2, 0x89, 0x15, 0xA0,
  73. 0x94, 0xF1, 0xB5, 0xDC, 0x65, 0x21, 0x15, 0x62, 0xE1, 0x7D, 0xC5,
  74. 0x43, 0x89, 0xEE, 0xB4, 0xEF, 0xC8, 0xA0, 0x8E, 0x34, 0x0F}}},
  75. {{{0x04, 0x82, 0x27, 0xE1, 0xEB, 0x98, 0x64, 0xC2, 0x8D, 0x8F, 0xDD,
  76. 0x0E, 0x82, 0x40, 0xAE, 0xD4, 0x31, 0x63, 0xD6, 0x46, 0x32, 0x16,
  77. 0x85, 0x7A, 0xB7, 0x18, 0x68, 0xB8, 0x17, 0x02, 0x81, 0xA6}}},
  78. {{{0x06, 0x20, 0x76, 0xE8, 0x54, 0x54, 0x53, 0xB4, 0xA9, 0xD8, 0x44,
  79. 0x4B, 0xAA, 0xFB, 0x1C, 0xFD, 0xAE, 0x15, 0xCA, 0x29, 0x79, 0xA6,
  80. 0x24, 0xA4, 0x0A, 0xF6, 0x1E, 0xAC, 0xED, 0xFB, 0x10, 0x41}}}};
  81. TEST(FiniteField, DeleteWorksGivenNewlyCreatedFiniteField) {
  82. FiniteField* finitefield = nullptr;
  83. EpidStatus sts = NewFiniteField(&q, &finitefield);
  84. EXPECT_EQ(kEpidNoErr, sts);
  85. EXPECT_NO_THROW(DeleteFiniteField(&finitefield));
  86. }
  87. TEST(FiniteField, DeleteWorksGivenNullPointer) {
  88. EXPECT_NO_THROW(DeleteFiniteField(nullptr));
  89. FiniteField* finitefield = nullptr;
  90. EXPECT_NO_THROW(DeleteFiniteField(&finitefield));
  91. }
  92. TEST(FiniteField, NewFailsGivenNullBigNumStr) {
  93. FiniteField* finitefield = nullptr;
  94. EpidStatus sts = NewFiniteField(nullptr, &finitefield);
  95. EXPECT_EQ(kEpidBadArgErr, sts);
  96. DeleteFiniteField(&finitefield);
  97. }
  98. TEST(FiniteField, NewFailsGivenNullFiniteField) {
  99. EpidStatus sts = NewFiniteField(&q, nullptr);
  100. EXPECT_EQ(kEpidBadArgErr, sts);
  101. }
  102. TEST(FiniteField, NewSucceedsGivenNewlyCreatedBigNumStr) {
  103. FiniteField* finitefield = nullptr;
  104. EpidStatus sts = NewFiniteField(&q, &finitefield);
  105. EXPECT_EQ(kEpidNoErr, sts);
  106. DeleteFiniteField(&finitefield);
  107. }
  108. // the following test reproduces a bug in IPP.
  109. TEST(FiniteField, DISABLED_NewSucceedsGivenAllFFBigNumStr) {
  110. const BigNumStr test_prime = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  111. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  112. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  113. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  114. FiniteField* finitefield = nullptr;
  115. EpidStatus sts = NewFiniteField(&test_prime, &finitefield);
  116. EXPECT_EQ(kEpidNoErr, sts);
  117. DeleteFiniteField(&finitefield);
  118. }
  119. TEST(FiniteField, BinomialExtensionFailsGivenNullPointer) {
  120. FiniteField* binom_ext_finite_field_ptr = nullptr;
  121. FiniteFieldObj ground_field(q);
  122. FfElementObj ground_element(&ground_field, beta);
  123. EXPECT_EQ(kEpidBadArgErr,
  124. NewFiniteFieldViaBinomalExtension(nullptr, ground_element, 2,
  125. &binom_ext_finite_field_ptr));
  126. DeleteFiniteField(&binom_ext_finite_field_ptr);
  127. EXPECT_EQ(kEpidBadArgErr,
  128. NewFiniteFieldViaBinomalExtension(ground_field, nullptr, 2,
  129. &binom_ext_finite_field_ptr));
  130. DeleteFiniteField(&binom_ext_finite_field_ptr);
  131. EXPECT_EQ(kEpidBadArgErr, NewFiniteFieldViaBinomalExtension(
  132. ground_field, ground_element, 2, nullptr));
  133. }
  134. TEST(FiniteField, BinomialExtensionFailsGivenBadDegree) {
  135. FiniteField* binom_ext_finite_field_ptr = nullptr;
  136. FiniteFieldObj ground_field(q);
  137. FfElementObj ground_element(&ground_field, beta);
  138. EXPECT_EQ(kEpidBadArgErr,
  139. NewFiniteFieldViaBinomalExtension(ground_field, ground_element, 1,
  140. &binom_ext_finite_field_ptr));
  141. DeleteFiniteField(&binom_ext_finite_field_ptr);
  142. EXPECT_EQ(kEpidBadArgErr,
  143. NewFiniteFieldViaBinomalExtension(ground_field, ground_element, 0,
  144. &binom_ext_finite_field_ptr));
  145. DeleteFiniteField(&binom_ext_finite_field_ptr);
  146. EXPECT_EQ(kEpidBadArgErr,
  147. NewFiniteFieldViaBinomalExtension(ground_field, ground_element, -1,
  148. &binom_ext_finite_field_ptr));
  149. DeleteFiniteField(&binom_ext_finite_field_ptr);
  150. EXPECT_EQ(kEpidBadArgErr,
  151. NewFiniteFieldViaBinomalExtension(ground_field, ground_element, -99,
  152. &binom_ext_finite_field_ptr));
  153. DeleteFiniteField(&binom_ext_finite_field_ptr);
  154. }
  155. TEST(FiniteField, BinomialExtensionCanBuildEpid2GtField) {
  156. // construct Fq finite field
  157. FiniteFieldObj fq(q);
  158. // construct Fq^2 finite field
  159. FfElementObj neg_beta(&fq);
  160. THROW_ON_EPIDERR(FfNeg(fq, FfElementObj(&fq, beta), neg_beta));
  161. FiniteFieldObj fq2(fq, neg_beta, 2);
  162. // construct Fq^6 finite field
  163. FfElementObj neg_xi(&fq2);
  164. THROW_ON_EPIDERR(FfNeg(fq2, FfElementObj(&fq2, xi), neg_xi));
  165. FiniteFieldObj fq6(fq2, neg_xi, 3);
  166. // construct Fq^12 finite field
  167. FfElementObj neg_v(&fq6);
  168. THROW_ON_EPIDERR(FfNeg(fq6, FfElementObj(&fq6, v), neg_v));
  169. FiniteFieldObj fq12(fq6, neg_v, 2);
  170. FiniteField* binom_ext_fq12_ptr = nullptr;
  171. EXPECT_EQ(kEpidNoErr, NewFiniteFieldViaBinomalExtension(fq6, neg_v, 2,
  172. &binom_ext_fq12_ptr));
  173. DeleteFiniteField(&binom_ext_fq12_ptr);
  174. }
  175. TEST(FiniteField, PolynomialExtensionFailsGivenNullPointer) {
  176. FiniteField* ext_finite_field_ptr = nullptr;
  177. FiniteFieldObj ground_field(q);
  178. EXPECT_EQ(kEpidBadArgErr,
  179. NewFiniteFieldViaPolynomialExtension(
  180. nullptr, coeffs, COUNT_OF(coeffs), &ext_finite_field_ptr));
  181. DeleteFiniteField(&ext_finite_field_ptr);
  182. EXPECT_EQ(kEpidBadArgErr,
  183. NewFiniteFieldViaPolynomialExtension(ground_field, nullptr, 2,
  184. &ext_finite_field_ptr));
  185. DeleteFiniteField(&ext_finite_field_ptr);
  186. EXPECT_EQ(kEpidBadArgErr, NewFiniteFieldViaPolynomialExtension(
  187. ground_field, coeffs, 2, nullptr));
  188. }
  189. TEST(FiniteField, PolynomialExtensionFailsGivenBadDegree) {
  190. FiniteField* ext_finite_field_ptr = nullptr;
  191. FiniteFieldObj ground_field(q);
  192. FfElementObj ground_element(&ground_field, beta);
  193. EXPECT_EQ(kEpidBadArgErr,
  194. NewFiniteFieldViaPolynomialExtension(ground_field, coeffs, 0,
  195. &ext_finite_field_ptr));
  196. DeleteFiniteField(&ext_finite_field_ptr);
  197. EXPECT_EQ(kEpidBadArgErr,
  198. NewFiniteFieldViaPolynomialExtension(ground_field, coeffs, -1,
  199. &ext_finite_field_ptr));
  200. DeleteFiniteField(&ext_finite_field_ptr);
  201. EXPECT_EQ(kEpidBadArgErr,
  202. NewFiniteFieldViaPolynomialExtension(ground_field, coeffs, -99,
  203. &ext_finite_field_ptr));
  204. DeleteFiniteField(&ext_finite_field_ptr);
  205. }
  206. TEST(FiniteField, CanBuildEpid11GtField) {
  207. // construct Fq finite field
  208. FiniteFieldObj fq(q);
  209. // construct Fqd finite field
  210. FiniteFieldObj fqd(fq, coeffs, COUNT_OF(coeffs));
  211. // Fqk ground element is {-qnr, 0, 0}
  212. FfElementObj neg_qnr(&fq);
  213. THROW_ON_EPIDERR(FfNeg(fq, FfElementObj(&fq, qnr), neg_qnr));
  214. Fq3ElemStr ground_element_str = {0};
  215. THROW_ON_EPIDERR(WriteFfElement(fq, neg_qnr, &ground_element_str.a[0],
  216. sizeof(ground_element_str.a[0])));
  217. FfElementObj ground_element(&fqd, ground_element_str);
  218. // construct Fqk finite field
  219. FiniteField* gt_ptr = nullptr;
  220. EXPECT_EQ(kEpidNoErr,
  221. NewFiniteFieldViaBinomalExtension(fqd, ground_element, 2, &gt_ptr));
  222. DeleteFiniteField(&gt_ptr);
  223. }
  224. } // namespace