pairing-test.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 Pairing unit tests.
  19. */
  20. #include <cstring>
  21. #include "gtest/gtest.h"
  22. #include "epid/common-testhelper/errors-testhelper.h"
  23. #include "epid/common-testhelper/epid_params-testhelper.h"
  24. #include "epid/common-testhelper/finite_field_wrapper-testhelper.h"
  25. #include "epid/common-testhelper/ffelement_wrapper-testhelper.h"
  26. #include "epid/common-testhelper/ecgroup_wrapper-testhelper.h"
  27. #include "epid/common-testhelper/ecpoint_wrapper-testhelper.h"
  28. extern "C" {
  29. #include "epid/common/math/pairing.h"
  30. #include "epid/common/math/src/ecgroup-internal.h"
  31. #include "epid/common/math/src/finitefield-internal.h"
  32. #include "epid/common/math/src/pairing-internal.h"
  33. }
  34. /// compares Fq12ElemStr values
  35. bool operator==(GtElemStr const& lhs, GtElemStr const& rhs) {
  36. return 0 == std::memcmp(&lhs, &rhs, sizeof(lhs));
  37. }
  38. namespace {
  39. class PairingTest : public Epid20Params, public ::testing::Test {
  40. public:
  41. static const BigNumStr t_str;
  42. virtual void SetUp() { params = new Epid20Params(); }
  43. virtual void TearDown() { delete params; }
  44. Epid20Params* params;
  45. };
  46. const BigNumStr PairingTest::t_str = {
  47. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49. 0x00, 0x00, 0x68, 0x82, 0xF5, 0xC0, 0x30, 0xB0, 0xA8, 0x01};
  50. ///////////////////////////////////////////////////////////////////////
  51. // NewPairingState / DeletePairingState
  52. // test that delete works in a "normal" valid case.
  53. TEST_F(PairingTest, DeleteWorksGivenNewlyCreatedPairingState) {
  54. PairingState* ps = nullptr;
  55. EpidStatus sts = kEpidNoErr;
  56. EXPECT_EQ(kEpidNoErr,
  57. NewPairingState(this->params->G1, this->params->G2,
  58. this->params->GT, &this->t_str, true, &ps));
  59. EXPECT_EQ(kEpidNoErr, sts);
  60. EXPECT_NO_THROW(DeletePairingState(&ps));
  61. }
  62. // test that delete works if there is nothing to do
  63. TEST_F(PairingTest, DeleteWorksGivenNullPointer) {
  64. EXPECT_NO_THROW(DeletePairingState(nullptr));
  65. PairingState* ps = nullptr;
  66. EXPECT_NO_THROW(DeletePairingState(&ps));
  67. }
  68. // test that new succeeds with valid parameters
  69. TEST_F(PairingTest, NewSucceedsGivenValidParameters) {
  70. PairingState* ps = nullptr;
  71. EXPECT_EQ(kEpidNoErr,
  72. NewPairingState(this->params->G1, this->params->G2,
  73. this->params->GT, &this->t_str, true, &ps));
  74. DeletePairingState(&ps);
  75. }
  76. // test that new fails if any options are NULL
  77. TEST_F(PairingTest, NewFailsGivenNullParameters) {
  78. PairingState* ps = nullptr;
  79. EXPECT_EQ(kEpidBadArgErr,
  80. NewPairingState(nullptr, this->params->G2, this->params->GT,
  81. &this->t_str, true, &ps));
  82. DeletePairingState(&ps);
  83. EXPECT_EQ(kEpidBadArgErr,
  84. NewPairingState(this->params->G1, nullptr, this->params->GT,
  85. &this->t_str, true, &ps));
  86. DeletePairingState(&ps);
  87. EXPECT_EQ(kEpidBadArgErr, NewPairingState(this->params->G1, this->params->G2,
  88. nullptr, &this->t_str, true, &ps));
  89. DeletePairingState(&ps);
  90. EXPECT_EQ(kEpidBadArgErr,
  91. NewPairingState(this->params->G1, this->params->G2,
  92. this->params->GT, nullptr, true, &ps));
  93. DeletePairingState(&ps);
  94. EXPECT_EQ(kEpidBadArgErr,
  95. NewPairingState(this->params->G1, this->params->G2,
  96. this->params->GT, &this->t_str, true, nullptr));
  97. }
  98. // test that new checks that G1 is valid
  99. TEST_F(PairingTest, NewFailsGivenInvalidG1) {
  100. PairingState* ps = nullptr;
  101. EcGroup ga;
  102. ga.ipp_ec = nullptr;
  103. ga.scratch_buffer = nullptr;
  104. EXPECT_EQ(kEpidBadArgErr,
  105. NewPairingState(&ga, this->params->G2, this->params->GT,
  106. &this->t_str, true, &ps));
  107. DeletePairingState(&ps);
  108. }
  109. // test that new checks that G2 is valid
  110. TEST_F(PairingTest, NewFailsGivenInvalidG2) {
  111. PairingState* ps = nullptr;
  112. EcGroup gb;
  113. gb.ipp_ec = nullptr;
  114. gb.scratch_buffer = nullptr;
  115. EXPECT_EQ(kEpidBadArgErr,
  116. NewPairingState(this->params->G1, &gb, this->params->GT,
  117. &this->t_str, true, &ps));
  118. DeletePairingState(&ps);
  119. }
  120. // test that new checks that GT is valid
  121. TEST_F(PairingTest, NewFailsGivenInvalidGT) {
  122. PairingState* ps = nullptr;
  123. FiniteField ff;
  124. ff.ipp_ff = nullptr;
  125. EXPECT_EQ(kEpidBadArgErr, NewPairingState(this->params->G1, this->params->G2,
  126. &ff, &this->t_str, true, &ps));
  127. DeletePairingState(&ps);
  128. }
  129. ///////////////////////////////////////////////////////////////////////
  130. // Pairing
  131. TEST_F(PairingTest, PairingWorksFromG1AndG2ToGt) {
  132. const bool neg = true;
  133. GtElemStr r_expected_str = {
  134. 0xba, 0x10, 0x1f, 0xf6, 0x46, 0x8b, 0xe9, 0x32, 0x4f, 0xc0, 0xa5, 0x01,
  135. 0xad, 0x5e, 0xe2, 0x31, 0x16, 0x29, 0x96, 0xed, 0xa7, 0xde, 0x4c, 0xe1,
  136. 0xd2, 0x8d, 0x33, 0xca, 0x50, 0xab, 0x7b, 0xc6, 0x15, 0xeb, 0x79, 0xf4,
  137. 0xeb, 0xde, 0x30, 0xb6, 0xc4, 0x07, 0x7c, 0x42, 0xcb, 0x04, 0x54, 0xf2,
  138. 0x1f, 0x4d, 0x1f, 0xc0, 0xdf, 0xa2, 0x2b, 0x9e, 0x34, 0xc4, 0x4c, 0x84,
  139. 0x14, 0xd3, 0x62, 0x07, 0xf1, 0x8b, 0x84, 0xd1, 0x46, 0x57, 0xb6, 0xe7,
  140. 0x80, 0xe1, 0x46, 0x49, 0x1c, 0x0d, 0xef, 0x81, 0x31, 0xb0, 0xbe, 0x8c,
  141. 0xb9, 0x08, 0xd0, 0xd3, 0xc4, 0x56, 0xca, 0xad, 0xf9, 0x1d, 0x75, 0x19,
  142. 0x3f, 0xee, 0x7c, 0x43, 0xc1, 0xfa, 0x4e, 0x50, 0xb7, 0x19, 0x01, 0x00,
  143. 0x6f, 0xd5, 0x16, 0xb6, 0xf4, 0x85, 0xe0, 0xeb, 0x2e, 0x5f, 0x0a, 0x7e,
  144. 0xf8, 0xac, 0xbc, 0x05, 0xec, 0x73, 0xb5, 0x57, 0xe3, 0xb3, 0x18, 0x29,
  145. 0xbb, 0xef, 0x86, 0x50, 0x87, 0xcf, 0x70, 0xba, 0x13, 0x8b, 0xb1, 0xb6,
  146. 0x2d, 0x6f, 0x65, 0x3d, 0xa1, 0x0b, 0xe3, 0x92, 0xc5, 0x72, 0x86, 0x6a,
  147. 0xb3, 0xeb, 0xe0, 0xe5, 0xda, 0x0e, 0x57, 0x87, 0xd5, 0xa9, 0x61, 0xa5,
  148. 0x1e, 0xcb, 0x04, 0x86, 0xcd, 0xc3, 0x18, 0x2a, 0x36, 0xa0, 0x81, 0x73,
  149. 0xe7, 0x13, 0x87, 0x80, 0x8d, 0x1a, 0xfe, 0x6e, 0x4b, 0xa3, 0x13, 0x03,
  150. 0x66, 0x9e, 0x80, 0x4d, 0x8a, 0xaa, 0x00, 0x95, 0x72, 0xce, 0xbb, 0x51,
  151. 0xe8, 0x01, 0x09, 0x41, 0xd3, 0x63, 0x28, 0x05, 0xa4, 0xbe, 0xd6, 0x41,
  152. 0xa6, 0x2f, 0x5f, 0xbf, 0x0b, 0x13, 0xb4, 0x54, 0x5b, 0x50, 0x65, 0xdc,
  153. 0x6f, 0x29, 0xd6, 0xda, 0xbf, 0xc2, 0x06, 0xea, 0x3b, 0xb2, 0xf1, 0xd4,
  154. 0x26, 0x5c, 0x92, 0x6b, 0x95, 0x6d, 0x88, 0xab, 0x8f, 0xc6, 0x9d, 0x31,
  155. 0xe4, 0x9b, 0x71, 0x49, 0xe0, 0xce, 0x97, 0x8f, 0xc9, 0x9f, 0xbc, 0xa8,
  156. 0x4a, 0xc6, 0xaa, 0x4a, 0xc8, 0x0d, 0x2a, 0x60, 0x1a, 0x43, 0x40, 0x03,
  157. 0xb3, 0x53, 0x30, 0x98, 0x1f, 0x3f, 0xdf, 0x5c, 0x0f, 0xf0, 0x84, 0x8e,
  158. 0x5a, 0x5d, 0x41, 0xd2, 0x47, 0x78, 0x6d, 0x9f, 0x89, 0xce, 0xf5, 0x8e,
  159. 0xb6, 0x54, 0xa2, 0x26, 0xe5, 0x40, 0x39, 0x5c, 0x59, 0x08, 0xb3, 0xda,
  160. 0xf5, 0xf8, 0xa0, 0x18, 0x33, 0x57, 0xd1, 0x72, 0xbb, 0xba, 0x6c, 0xed,
  161. 0xe8, 0xa0, 0x5e, 0xc8, 0x81, 0xc5, 0xac, 0x15, 0x1b, 0xd0, 0xe6, 0xc8,
  162. 0x92, 0xf9, 0x43, 0x03, 0x5a, 0x00, 0x42, 0xe3, 0x49, 0xa5, 0xf7, 0x19,
  163. 0x78, 0x8a, 0x39, 0x89, 0x32, 0xae, 0xbf, 0x4d, 0x4b, 0xb3, 0x33, 0x76,
  164. 0x16, 0xfd, 0x0b, 0xfe, 0x42, 0x1e, 0x17, 0x37, 0x2a, 0x04, 0xea, 0x26,
  165. 0xba, 0x6e, 0x2c, 0x36, 0xaf, 0x35, 0x1b, 0x75, 0x6d, 0x17, 0xdc, 0x8e,
  166. };
  167. G1ElemStr ga_elem_str = {
  168. 0xd7, 0xe2, 0xf9, 0x37, 0x21, 0x0f, 0x09, 0x97, 0x0f, 0xca, 0xa6,
  169. 0x03, 0x7d, 0x91, 0xc3, 0x75, 0x8a, 0xc9, 0x44, 0x11, 0xfc, 0xaa,
  170. 0x55, 0x67, 0xba, 0xce, 0xaf, 0x8d, 0xf6, 0x7c, 0x84, 0x83, 0x04,
  171. 0xb7, 0xa6, 0xff, 0x9f, 0x0d, 0x26, 0x73, 0xaf, 0x6c, 0xd0, 0x0a,
  172. 0xf6, 0x13, 0xc9, 0x44, 0x3f, 0xf0, 0x82, 0x58, 0x48, 0x59, 0x03,
  173. 0x3f, 0x88, 0xe2, 0x46, 0xd6, 0x0f, 0x93, 0x42, 0x4b,
  174. };
  175. G2ElemStr gb_elem_str = {
  176. 0x3f, 0x4c, 0xb5, 0x2d, 0xbc, 0x72, 0xb0, 0x9c, 0x6f, 0xb2, 0xb5, 0xc1,
  177. 0xdc, 0xfb, 0xda, 0x35, 0x91, 0xa6, 0x8d, 0x51, 0x37, 0x70, 0xe2, 0x17,
  178. 0xad, 0x53, 0x23, 0xdc, 0xa3, 0xc3, 0xfd, 0x4c, 0x90, 0xfa, 0x4f, 0xa2,
  179. 0xcb, 0x35, 0xf3, 0x50, 0x5e, 0x8e, 0xf4, 0xce, 0x7f, 0xb0, 0x8a, 0x69,
  180. 0x49, 0xdf, 0xf5, 0x4f, 0xb0, 0xc1, 0xd7, 0xf9, 0xb8, 0xfb, 0x89, 0xd1,
  181. 0xb6, 0xf8, 0x74, 0x04, 0xef, 0xc6, 0x60, 0x05, 0x62, 0xf3, 0x17, 0x5a,
  182. 0x80, 0xf4, 0x4b, 0x97, 0x08, 0x3e, 0x43, 0xa1, 0x44, 0x4c, 0x54, 0x86,
  183. 0x16, 0x20, 0xb9, 0xcc, 0xfb, 0xbd, 0x00, 0x5f, 0xc8, 0x01, 0xfb, 0x5b,
  184. 0xc1, 0x6e, 0x2b, 0x46, 0xe2, 0x04, 0x70, 0xeb, 0xa2, 0xaa, 0x86, 0x5a,
  185. 0x35, 0x14, 0x0e, 0xc9, 0xdf, 0xba, 0x9b, 0x6f, 0x3a, 0xca, 0x94, 0x9c,
  186. 0x44, 0x89, 0x94, 0xa3, 0xeb, 0x61, 0x8b, 0x01,
  187. };
  188. GtElemStr r_str = {0};
  189. FfElementObj r(&this->params->GT);
  190. EcPointObj ga_elem(&this->params->G1, ga_elem_str);
  191. EcPointObj gb_elem(&this->params->G2, gb_elem_str);
  192. PairingState* ps = nullptr;
  193. THROW_ON_EPIDERR(NewPairingState(this->params->G1, this->params->G2,
  194. this->params->GT, &this->t_str, neg, &ps));
  195. EXPECT_EQ(kEpidNoErr, Pairing(ps, r, ga_elem, gb_elem));
  196. DeletePairingState(&ps);
  197. THROW_ON_EPIDERR(WriteFfElement(this->params->GT, r, &r_str, sizeof(r_str)));
  198. EXPECT_EQ(r_expected_str, r_str);
  199. }
  200. } // namespace