finitefield-test.cc 11 KB

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