ecgroup-test.cc 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130
  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 EcGroup unit tests.
  19. */
  20. #include <cstring>
  21. #include <memory>
  22. #include <string>
  23. #include <stdexcept>
  24. #include <vector>
  25. #include "gtest/gtest.h"
  26. extern "C" {
  27. #include "epid/common/math/ecgroup.h"
  28. #include "epid/common/math/finitefield.h"
  29. }
  30. #include "epid/common-testhelper/errors-testhelper.h"
  31. #include "epid/common-testhelper/prng-testhelper.h"
  32. #include "epid/common-testhelper/bignum_wrapper-testhelper.h"
  33. #include "epid/common-testhelper/finite_field_wrapper-testhelper.h"
  34. #include "epid/common-testhelper/ffelement_wrapper-testhelper.h"
  35. #include "epid/common-testhelper/ecgroup_wrapper-testhelper.h"
  36. #include "epid/common-testhelper/ecpoint_wrapper-testhelper.h"
  37. /// compares G1ElemStr values
  38. bool operator==(G1ElemStr const& lhs, G1ElemStr const& rhs) {
  39. return 0 == std::memcmp(&lhs, &rhs, sizeof(lhs));
  40. }
  41. /// compares G2ElemStr values
  42. bool operator==(G2ElemStr const& lhs, G2ElemStr const& rhs) {
  43. return 0 == std::memcmp(&lhs, &rhs, sizeof(lhs));
  44. }
  45. namespace {
  46. class EFq2Params {
  47. public:
  48. FiniteFieldObj fq2;
  49. FfElementObj a;
  50. FfElementObj b;
  51. FfElementObj x;
  52. FfElementObj y;
  53. BigNumObj order;
  54. BigNumObj cofactor;
  55. explicit EFq2Params(FiniteFieldObj* fq) {
  56. // Intel(R) EPID 2.0 parameters for EC(Fq2)
  57. static const FqElemStr param_beta = {
  58. {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xF0, 0xCD, 0x46, 0xE5, 0xF2,
  59. 0x5E, 0xEE, 0x71, 0xA4, 0x9F, 0x0C, 0xDC, 0x65, 0xFB, 0x12, 0x98,
  60. 0x0A, 0x82, 0xD3, 0x29, 0x2D, 0xDB, 0xAE, 0xD3, 0x30, 0x12}}};
  61. static const G2ElemStr param_g2 = {
  62. {{{{0xE2, 0x01, 0x71, 0xC5, 0x4A, 0xA3, 0xDA, 0x05, 0x21, 0x67, 0x04,
  63. 0x13, 0x74, 0x3C, 0xCF, 0x22, 0xD2, 0x5D, 0x52, 0x68, 0x3D, 0x32,
  64. 0x47, 0x0E, 0xF6, 0x02, 0x13, 0x43, 0xBF, 0x28, 0x23, 0x94}}},
  65. {{{0x59, 0x2D, 0x1E, 0xF6, 0x53, 0xA8, 0x5A, 0x80, 0x46, 0xCC, 0xDC,
  66. 0x25, 0x4F, 0xBB, 0x56, 0x56, 0x43, 0x43, 0x3B, 0xF6, 0x28, 0x96,
  67. 0x53, 0xE2, 0x7D, 0xF7, 0xB2, 0x12, 0xBA, 0xA1, 0x89, 0xBE}}}},
  68. {{{{0xAE, 0x60, 0xA4, 0xE7, 0x51, 0xFF, 0xD3, 0x50, 0xC6, 0x21, 0xE7,
  69. 0x03, 0x31, 0x28, 0x26, 0xBD, 0x55, 0xE8, 0xB5, 0x9A, 0x4D, 0x91,
  70. 0x68, 0x38, 0x41, 0x4D, 0xB8, 0x22, 0xDD, 0x23, 0x35, 0xAE}}},
  71. {{{0x1A, 0xB4, 0x42, 0xF9, 0x89, 0xAF, 0xE5, 0xAD, 0xF8, 0x02, 0x74,
  72. 0xF8, 0x76, 0x45, 0xE2, 0x53, 0x2C, 0xDC, 0x61, 0x81, 0x90, 0x93,
  73. 0xD6, 0x13, 0x2C, 0x90, 0xFE, 0x89, 0x51, 0xB9, 0x24, 0x21}}}}};
  74. static const Fq2ElemStr param_xi0xi1 = {
  75. {{{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  76. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  77. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}}},
  78. {{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  79. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  80. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}}}}};
  81. static const FqElemStr param_b = {
  82. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  83. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  84. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03}};
  85. // Setup Fq2 with parameters (q, beta)
  86. // Fq^2 = Fq[u] / (u^2 - beta)
  87. FfElementObj neg_beta(fq);
  88. THROW_ON_EPIDERR(FfNeg(*fq, FfElementObj(fq, param_beta), neg_beta));
  89. fq2 = FiniteFieldObj(*fq, neg_beta, 2);
  90. // set x to (g2.x[0], g2.x[1]) and y to (g2.y[0], g2.y[1])
  91. x = FfElementObj(&fq2, &param_g2.x, sizeof(param_g2.x));
  92. y = FfElementObj(&fq2, &param_g2.y, sizeof(param_g2.y));
  93. // set a to identity, NewFfElement does it by default
  94. a = FfElementObj(&fq2);
  95. // set b to inv(xi)*param_b, where xi is (xi0, xi1) element in Fq2
  96. FfElementObj neg_xi(&fq2);
  97. THROW_ON_EPIDERR(FfInv(fq2, FfElementObj(&fq2, param_xi0xi1), neg_xi));
  98. b = FfElementObj(&fq2);
  99. THROW_ON_EPIDERR(FfMul(fq2, neg_xi.get(), FfElementObj(fq, param_b), b));
  100. // set h = 2q - p, aka cofactor
  101. std::vector<uint8_t> cofactor_str(
  102. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  103. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  104. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  105. 0xff, 0xfc, 0xf0, 0xcd, 0x46, 0xe5, 0xf2, 0x5e, 0xee, 0x71, 0xa4, 0xa0,
  106. 0x0c, 0xdc, 0x65, 0xfb, 0x12, 0x96, 0x82, 0xea, 0xb0, 0x25, 0x08, 0x4a,
  107. 0x8c, 0x9b, 0x10, 0x19});
  108. cofactor = BigNumObj(cofactor_str);
  109. // set n = p * h, AKA order
  110. std::vector<uint8_t> order_str(
  111. {0xff, 0xff, 0xff, 0xff, 0xff, 0xf9, 0xe1, 0x9a, 0x8d, 0xcb, 0xe4, 0xc7,
  112. 0x38, 0xfa, 0x9b, 0x98, 0x4d, 0x1c, 0x12, 0x9f, 0x64, 0x97, 0xe8, 0x54,
  113. 0xa3, 0x0a, 0x81, 0xac, 0x42, 0xf9, 0x39, 0x16, 0xa7, 0x70, 0x21, 0xdc,
  114. 0xfb, 0xb6, 0xe7, 0x7e, 0x1f, 0x5b, 0x55, 0xcc, 0x4e, 0x84, 0xcd, 0x19,
  115. 0x4f, 0x49, 0x20, 0x94, 0xb5, 0xd8, 0x12, 0xa0, 0x2e, 0x7f, 0x40, 0x13,
  116. 0xb2, 0xfa, 0xa1, 0x45});
  117. order = BigNumObj(order_str);
  118. }
  119. virtual ~EFq2Params() {}
  120. private:
  121. // This class is not meant to be copied or assigned
  122. EFq2Params(const EFq2Params&);
  123. EFq2Params& operator=(const EFq2Params&);
  124. };
  125. class EcGroupTest : public ::testing::Test {
  126. public:
  127. static const G1ElemStr g1_str;
  128. static const G2ElemStr g2_str;
  129. static const FqElemStr a1;
  130. static const FqElemStr b1;
  131. static const BigNumStr h1;
  132. static const BigNumStr p;
  133. static const BigNumStr q;
  134. static const G1ElemStr efq_a_str;
  135. static const G1ElemStr efq_b_str;
  136. static const BigNumStr x_str;
  137. static const BigNumStr y_str;
  138. static const G1ElemStr efq_mul_ab_str;
  139. static const G1ElemStr efq_exp_ax_str;
  140. static const G1ElemStr efq_multiexp_abxy_str;
  141. static const G1ElemStr efq_inv_a_str;
  142. static const G1ElemStr efq_identity_str;
  143. static const G1ElemStr efq_r_sha256_str;
  144. static const G1ElemStr efq_r_sha384_str;
  145. static const G1ElemStr efq_r_sha512_str;
  146. static const uint8_t sha_msg[];
  147. static const G2ElemStr efq2_a_str;
  148. static const G2ElemStr efq2_b_str;
  149. static const G2ElemStr efq2_mul_ab_str;
  150. static const G2ElemStr efq2_exp_ax_str;
  151. static const G2ElemStr efq2_multiexp_abxy_str;
  152. static const G2ElemStr efq2_inv_a_str;
  153. static const G2ElemStr efq2_identity_str;
  154. // Epid 1.1 hash of message "aad"
  155. static const Epid11G3ElemStr kAadHash;
  156. // Epid 1.1 hash of message "bsn0"
  157. static const Epid11G3ElemStr kBsn0Hash;
  158. // Epid 1.1 hash of message "test"
  159. static const Epid11G3ElemStr kTestHash;
  160. // Epid 1.1 hash of message "aac"
  161. static const Epid11G3ElemStr kAacHash;
  162. virtual void SetUp() {
  163. Epid11Params epid11_params_str = {
  164. #include "epid/common/1.1/src/epid11params_tate.inc"
  165. };
  166. fq = FiniteFieldObj(q);
  167. fq_a = FfElementObj(&fq, a1);
  168. fq_b = FfElementObj(&fq, b1);
  169. g1_x = FfElementObj(&fq, g1_str.x);
  170. g1_y = FfElementObj(&fq, g1_str.y);
  171. bn_p = BigNumObj(p);
  172. bn_h = BigNumObj(h1);
  173. efq = EcGroupObj(&fq, fq_a, fq_b, g1_x, g1_y, bn_p, bn_h);
  174. efq_a = EcPointObj(&efq, efq_a_str);
  175. efq_b = EcPointObj(&efq, efq_b_str);
  176. efq_r = EcPointObj(&efq);
  177. efq_identity = EcPointObj(&efq, efq_identity_str);
  178. efq2_par.reset(new EFq2Params(&fq));
  179. efq2 = EcGroupObj(&efq2_par->fq2, efq2_par->a, efq2_par->b, efq2_par->x,
  180. efq2_par->y, efq2_par->order, efq2_par->cofactor);
  181. efq2_a = EcPointObj(&efq2, efq2_a_str);
  182. efq2_b = EcPointObj(&efq2, efq2_b_str);
  183. efq2_r = EcPointObj(&efq2);
  184. efq2_identity = EcPointObj(&efq2, efq_identity_str);
  185. epid11_Fq_tick = FiniteFieldObj(epid11_params_str.q_tick);
  186. epid11_a_tick = FfElementObj(&epid11_Fq_tick, epid11_params_str.a_tick);
  187. epid11_b_tick = FfElementObj(&epid11_Fq_tick, epid11_params_str.b_tick);
  188. epid11_g3_x = FfElementObj(&epid11_Fq_tick, epid11_params_str.g3.x);
  189. epid11_g3_y = FfElementObj(&epid11_Fq_tick, epid11_params_str.g3.y);
  190. epid11_p_tick = BigNumObj(epid11_params_str.p_tick);
  191. BigNumStr h_tick_str = {0};
  192. ((OctStr32*)
  193. h_tick_str.data.data)[sizeof(BigNumStr) / sizeof(OctStr32) - 1] =
  194. epid11_params_str.h_tick;
  195. epid11_h_tick = BigNumObj(h_tick_str);
  196. epid11_G3 =
  197. EcGroupObj(&epid11_Fq_tick, epid11_a_tick, epid11_b_tick, epid11_g3_x,
  198. epid11_g3_y, epid11_p_tick, epid11_h_tick);
  199. epid11_G3_r = EcPointObj(&epid11_G3);
  200. }
  201. FiniteFieldObj fq;
  202. FfElementObj fq_a;
  203. FfElementObj fq_b;
  204. FfElementObj g1_x;
  205. FfElementObj g1_y;
  206. BigNumObj bn_p;
  207. BigNumObj bn_h;
  208. EcGroupObj efq;
  209. EcPointObj efq_a;
  210. EcPointObj efq_b;
  211. EcPointObj efq_r;
  212. EcPointObj efq_identity;
  213. std::unique_ptr<EFq2Params> efq2_par;
  214. EcGroupObj efq2;
  215. EcPointObj efq2_a;
  216. EcPointObj efq2_b;
  217. EcPointObj efq2_r;
  218. EcPointObj efq2_identity;
  219. FiniteFieldObj epid11_Fq_tick;
  220. FfElementObj epid11_a_tick;
  221. FfElementObj epid11_b_tick;
  222. FfElementObj epid11_g3_x;
  223. FfElementObj epid11_g3_y;
  224. BigNumObj epid11_p_tick;
  225. BigNumObj epid11_h_tick;
  226. EcGroupObj epid11_G3;
  227. EcPointObj epid11_G3_r;
  228. };
  229. const G1ElemStr EcGroupTest::g1_str = {
  230. {{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  231. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  232. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}}},
  233. {{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  234. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  235. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}}}};
  236. const G2ElemStr EcGroupTest::g2_str = {
  237. {{{{0xE2, 0x01, 0x71, 0xC5, 0x4A, 0xA3, 0xDA, 0x05, 0x21, 0x67, 0x04, 0x13,
  238. 0x74, 0x3C, 0xCF, 0x22, 0xD2, 0x5D, 0x52, 0x68, 0x3D, 0x32, 0x47, 0x0E,
  239. 0xF6, 0x02, 0x13, 0x43, 0xBF, 0x28, 0x23, 0x94}}},
  240. {{{0x59, 0x2D, 0x1E, 0xF6, 0x53, 0xA8, 0x5A, 0x80, 0x46, 0xCC, 0xDC, 0x25,
  241. 0x4F, 0xBB, 0x56, 0x56, 0x43, 0x43, 0x3B, 0xF6, 0x28, 0x96, 0x53, 0xE2,
  242. 0x7D, 0xF7, 0xB2, 0x12, 0xBA, 0xA1, 0x89, 0xBE}}}},
  243. {{{{0xAE, 0x60, 0xA4, 0xE7, 0x51, 0xFF, 0xD3, 0x50, 0xC6, 0x21, 0xE7, 0x03,
  244. 0x31, 0x28, 0x26, 0xBD, 0x55, 0xE8, 0xB5, 0x9A, 0x4D, 0x91, 0x68, 0x38,
  245. 0x41, 0x4D, 0xB8, 0x22, 0xDD, 0x23, 0x35, 0xAE}}},
  246. {{{0x1A, 0xB4, 0x42, 0xF9, 0x89, 0xAF, 0xE5, 0xAD, 0xF8, 0x02, 0x74, 0xF8,
  247. 0x76, 0x45, 0xE2, 0x53, 0x2C, 0xDC, 0x61, 0x81, 0x90, 0x93, 0xD6, 0x13,
  248. 0x2C, 0x90, 0xFE, 0x89, 0x51, 0xB9, 0x24, 0x21}}}}};
  249. const FqElemStr EcGroupTest::a1 = {
  250. {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  251. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  252. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}};
  253. const FqElemStr EcGroupTest::b1 = {
  254. {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  255. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  256. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03}}};
  257. const BigNumStr EcGroupTest::h1 = {
  258. {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  259. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  260. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}}};
  261. const BigNumStr EcGroupTest::p = {
  262. {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xF0, 0xCD, 0x46, 0xE5, 0xF2, 0x5E,
  263. 0xEE, 0x71, 0xA4, 0x9E, 0x0C, 0xDC, 0x65, 0xFB, 0x12, 0x99, 0x92, 0x1A,
  264. 0xF6, 0x2D, 0x53, 0x6C, 0xD1, 0x0B, 0x50, 0x0D}}};
  265. const BigNumStr EcGroupTest::q = {
  266. {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xF0, 0xCD, 0x46, 0xE5, 0xF2, 0x5E,
  267. 0xEE, 0x71, 0xA4, 0x9F, 0x0C, 0xDC, 0x65, 0xFB, 0x12, 0x98, 0x0A, 0x82,
  268. 0xD3, 0x29, 0x2D, 0xDB, 0xAE, 0xD3, 0x30, 0x13}}};
  269. const G1ElemStr EcGroupTest::efq_a_str = {
  270. {{{0x12, 0xA6, 0x5B, 0xD6, 0x91, 0x8D, 0x50, 0xA7, 0x66, 0xEB, 0x7D, 0x52,
  271. 0xE3, 0x40, 0x17, 0x60, 0x7F, 0xDF, 0x6C, 0xA1, 0x2C, 0x1A, 0x37, 0xE0,
  272. 0x92, 0xC0, 0xF7, 0xB9, 0x76, 0xAB, 0xB1, 0x8A}}},
  273. {{{0x78, 0x65, 0x28, 0xCB, 0xAF, 0x07, 0x52, 0x50, 0x55, 0x7A, 0x5F, 0x30,
  274. 0x0A, 0xC0, 0xB4, 0x6B, 0xEA, 0x6F, 0xE2, 0xF6, 0x6D, 0x96, 0xF7, 0xCD,
  275. 0xC8, 0xD3, 0x12, 0x7F, 0x1F, 0x3A, 0x8B, 0x42}}}};
  276. const G1ElemStr EcGroupTest::efq_b_str = {
  277. {{{0xE6, 0x65, 0x23, 0x9B, 0xD4, 0x07, 0x16, 0x83, 0x38, 0x23, 0xB2, 0x67,
  278. 0x57, 0xEB, 0x0F, 0x23, 0x3A, 0xF4, 0x8E, 0xDA, 0x71, 0x5E, 0xD9, 0x98,
  279. 0x63, 0x98, 0x2B, 0xBC, 0x78, 0xD1, 0x94, 0xF2}}},
  280. {{{0x63, 0xB0, 0xAD, 0xB8, 0x2C, 0xE8, 0x14, 0xFD, 0xA2, 0x39, 0x0E, 0x66,
  281. 0xB7, 0xD0, 0x6A, 0xAB, 0xEE, 0xFA, 0x2E, 0x24, 0x9B, 0xB5, 0x14, 0x35,
  282. 0xFE, 0xB6, 0xB0, 0xFF, 0xFD, 0x5F, 0x73, 0x19}}}};
  283. const BigNumStr EcGroupTest::x_str = {
  284. {{0xFF, 0xFB, 0x3E, 0x5D, 0xFF, 0x9A, 0xFF, 0x02, 0x00, 0xFF, 0xFF, 0xFF,
  285. 0xF2, 0xE1, 0x85, 0x81, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x81,
  286. 0xFF, 0xFD, 0xFF, 0xEB, 0xFF, 0x29, 0xA7, 0xFF}}};
  287. const BigNumStr EcGroupTest::y_str = {
  288. {{0x11, 0xFF, 0xFF, 0xFF, 0x4F, 0x59, 0xB1, 0xD3, 0x6B, 0x08, 0xFF, 0xFF,
  289. 0x0B, 0xF3, 0xAF, 0x27, 0xFF, 0xB8, 0xFF, 0xFF, 0x98, 0xFF, 0xEB, 0xFF,
  290. 0xF2, 0x6A, 0xFF, 0xFF, 0xEA, 0x31, 0xFF, 0xFF}}};
  291. const G1ElemStr EcGroupTest::efq_mul_ab_str = {
  292. {{{0x30, 0xF8, 0x33, 0xB7, 0x1C, 0x85, 0x94, 0x6D, 0x6F, 0x3C, 0x97, 0x77,
  293. 0x81, 0xA5, 0xC2, 0x98, 0x93, 0x5C, 0x8C, 0xC1, 0xFF, 0x35, 0x9E, 0x68,
  294. 0xF6, 0x4D, 0x18, 0xDD, 0x65, 0xA9, 0xC0, 0x60}}},
  295. {{{0x89, 0xE5, 0x08, 0x2D, 0xD1, 0xD8, 0xC7, 0xBF, 0xDE, 0x16, 0x24, 0xA7,
  296. 0x2F, 0xF1, 0x48, 0x00, 0x26, 0xAF, 0x89, 0xEA, 0xC9, 0x94, 0x78, 0xFF,
  297. 0x2A, 0xB0, 0x20, 0xED, 0x33, 0x0C, 0x4E, 0x88}}}};
  298. const G1ElemStr EcGroupTest::efq_exp_ax_str = {
  299. {{{0x44, 0x45, 0xFA, 0x16, 0x23, 0x66, 0x26, 0x9D, 0x44, 0xB9, 0x43, 0xAB,
  300. 0x87, 0xE3, 0x56, 0xCA, 0x9C, 0x89, 0x44, 0x8E, 0xE8, 0x19, 0x29, 0x4D,
  301. 0x4D, 0x59, 0x7D, 0xBE, 0x46, 0x3F, 0x55, 0x0D}}},
  302. {{{0x98, 0x09, 0xCF, 0x43, 0x46, 0x75, 0xB8, 0x71, 0xFF, 0x37, 0xBA, 0xA0,
  303. 0x63, 0xE2, 0xAC, 0x09, 0x38, 0x10, 0x70, 0xAC, 0x15, 0x52, 0x28, 0xF4,
  304. 0x77, 0x68, 0x32, 0x7B, 0x6E, 0xFB, 0xC1, 0x43}}}};
  305. const G1ElemStr EcGroupTest::efq_multiexp_abxy_str = {
  306. {{{0x63, 0x4A, 0xD4, 0xC1, 0x6B, 0x90, 0x67, 0xA2, 0x0B, 0xE2, 0xB3, 0xE9,
  307. 0x95, 0x3F, 0x82, 0x7E, 0x21, 0xBF, 0x9F, 0xCD, 0xA0, 0x16, 0x56, 0x6B,
  308. 0x31, 0x66, 0x68, 0xBB, 0x25, 0xF8, 0xBD, 0xF3}}},
  309. {{{0xBD, 0x5F, 0xF8, 0x48, 0xD4, 0xBF, 0x35, 0x2D, 0xDC, 0xD1, 0x78, 0x74,
  310. 0xFF, 0xB1, 0x47, 0xD5, 0x6B, 0x21, 0xE5, 0x15, 0x01, 0xA8, 0xDC, 0x8B,
  311. 0x3C, 0x9D, 0x96, 0xC7, 0xC6, 0xB0, 0x05, 0x20}}}};
  312. const G1ElemStr EcGroupTest::efq_inv_a_str = {
  313. {{{0x12, 0xA6, 0x5B, 0xD6, 0x91, 0x8D, 0x50, 0xA7, 0x66, 0xEB, 0x7D, 0x52,
  314. 0xE3, 0x40, 0x17, 0x60, 0x7F, 0xDF, 0x6C, 0xA1, 0x2C, 0x1A, 0x37, 0xE0,
  315. 0x92, 0xC0, 0xF7, 0xB9, 0x76, 0xAB, 0xB1, 0x8A}}},
  316. {{{0x87, 0x9A, 0xD7, 0x34, 0x50, 0xF5, 0x9E, 0x7C, 0xF1, 0x6B, 0x93, 0x2E,
  317. 0xE3, 0xB0, 0xF0, 0x33, 0x22, 0x6C, 0x83, 0x04, 0xA5, 0x01, 0x12, 0xB5,
  318. 0x0A, 0x56, 0x1B, 0x5C, 0x8F, 0x98, 0xA4, 0xD1}}}};
  319. const G1ElemStr EcGroupTest::efq_identity_str = {
  320. {{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  321. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  322. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}},
  323. {{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  324. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  325. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}}};
  326. const uint8_t EcGroupTest::sha_msg[] = {'a', 'b', 'c'};
  327. const G1ElemStr EcGroupTest::efq_r_sha256_str = {
  328. {{{0x2E, 0xBB, 0x50, 0x4D, 0x88, 0xFF, 0x25, 0x62, 0xF3, 0x71, 0x65, 0x81,
  329. 0xAD, 0xBE, 0x83, 0x6E, 0x54, 0xF5, 0xA6, 0x2A, 0x70, 0xE6, 0x18, 0x6B,
  330. 0xD5, 0x4A, 0x10, 0x3C, 0x80, 0x08, 0x95, 0x3D}}},
  331. {{{0x8A, 0x43, 0xA1, 0x04, 0xB1, 0x3F, 0x3C, 0xB4, 0xBD, 0x67, 0x38, 0xB1,
  332. 0x07, 0xF0, 0x7A, 0x32, 0x7E, 0xCD, 0xF0, 0x2E, 0x62, 0x3E, 0x2C, 0x1F,
  333. 0x48, 0xAA, 0x0D, 0x6C, 0xDC, 0x48, 0xF9, 0xF7}}}};
  334. const G1ElemStr EcGroupTest::efq_r_sha384_str = {
  335. {{{0xE1, 0xC8, 0x28, 0xB1, 0x9A, 0xDF, 0x5D, 0x4B, 0xC4, 0x25, 0x90, 0xFB,
  336. 0x38, 0x20, 0xD4, 0x8B, 0x30, 0x8F, 0x95, 0x76, 0xC3, 0x7F, 0x9D, 0xAD,
  337. 0x94, 0xC4, 0x31, 0x80, 0xD7, 0xDF, 0xD5, 0xFE}}},
  338. {{{0x0E, 0x86, 0x11, 0x90, 0xAF, 0xEF, 0xEB, 0x79, 0x4B, 0x3E, 0x80, 0x92,
  339. 0x94, 0x3B, 0x2F, 0x5E, 0x72, 0x21, 0xEF, 0xF8, 0xBC, 0xE3, 0x48, 0xA9,
  340. 0xD0, 0x31, 0x19, 0xAC, 0xD1, 0xD7, 0x49, 0x87}}}};
  341. const G1ElemStr EcGroupTest::efq_r_sha512_str = {
  342. {{{0x8C, 0x62, 0xA0, 0x2D, 0x55, 0x55, 0x55, 0x86, 0xBC, 0x82, 0xA6, 0xA2,
  343. 0x21, 0x97, 0x9B, 0x9B, 0xB4, 0x03, 0x3D, 0x83, 0xF3, 0xBA, 0xDA, 0x9C,
  344. 0x42, 0xF7, 0xB3, 0x94, 0x99, 0x2A, 0x96, 0xE4}}},
  345. {{{0x4C, 0x0E, 0xA7, 0x62, 0x17, 0xB9, 0xFB, 0xE5, 0x21, 0x7D, 0x54, 0x24,
  346. 0xE0, 0x2B, 0x87, 0xF7, 0x69, 0x54, 0x0C, 0xC6, 0xAD, 0xF2, 0xF2, 0x7B,
  347. 0xE6, 0x91, 0xD8, 0xF3, 0x40, 0x6C, 0x8F, 0x03}}}};
  348. const G2ElemStr EcGroupTest::efq2_a_str = {
  349. {
  350. {0x2F, 0x8C, 0xC7, 0xD7, 0xD4, 0x1E, 0x4A, 0xCB, 0x82, 0x92, 0xC7, 0x9C,
  351. 0x0F, 0xA2, 0xF2, 0x1B, 0xDF, 0xEA, 0x96, 0x64, 0x8B, 0xA2, 0x32, 0x7C,
  352. 0xDF, 0xD8, 0x89, 0x10, 0xFD, 0xBB, 0x38, 0xCD},
  353. {0xB1, 0x23, 0x46, 0x13, 0x4D, 0x9B, 0x8E, 0x8A, 0x95, 0x64, 0xDD, 0x37,
  354. 0x29, 0x44, 0x1F, 0x76, 0xB5, 0x3A, 0x47, 0xD3, 0xE0, 0x18, 0x1E, 0x60,
  355. 0xE9, 0x94, 0x13, 0xA4, 0x47, 0xCD, 0xBE, 0x03},
  356. },
  357. {
  358. {0xD3, 0x67, 0xA5, 0xCC, 0xEF, 0x7B, 0xD1, 0x8D, 0x4A, 0x7F, 0xF1, 0x8F,
  359. 0x66, 0xCB, 0x5E, 0x86, 0xAC, 0xCB, 0x36, 0x5F, 0x29, 0x90, 0x28, 0x55,
  360. 0xF0, 0xDC, 0x6E, 0x8B, 0x87, 0xB5, 0xD8, 0x32},
  361. {0x6C, 0x0A, 0xC5, 0x58, 0xB1, 0x4E, 0xCA, 0x85, 0x44, 0x3E, 0xDE, 0x71,
  362. 0x9B, 0xC7, 0x90, 0x19, 0x06, 0xD2, 0xA0, 0x4E, 0xC7, 0x33, 0xF4, 0x5C,
  363. 0xE8, 0x16, 0xE2, 0x67, 0xDB, 0xBF, 0x64, 0x84},
  364. },
  365. };
  366. const G2ElemStr EcGroupTest::efq2_b_str = {
  367. {
  368. {0x16, 0xF1, 0x61, 0x76, 0x06, 0x3E, 0xE9, 0xC0, 0xB9, 0xB1, 0x3A, 0x75,
  369. 0xFC, 0xDB, 0x90, 0xCD, 0x01, 0xF4, 0x9F, 0xCC, 0xAA, 0x24, 0x69, 0x83,
  370. 0xBE, 0x20, 0x44, 0x87, 0x58, 0x90, 0x0F, 0x4F},
  371. {0xC7, 0x50, 0x37, 0xC1, 0xB9, 0x2D, 0xE1, 0xE3, 0x79, 0x20, 0x7B, 0x62,
  372. 0x90, 0xF8, 0xC7, 0xF0, 0xD7, 0x5A, 0xE7, 0xAD, 0x65, 0xE1, 0xC7, 0x50,
  373. 0x59, 0xA1, 0xFC, 0x49, 0xBC, 0x2A, 0xE5, 0xD7},
  374. },
  375. {
  376. {0x12, 0x73, 0x3B, 0xA4, 0xDD, 0x0F, 0xBB, 0x35, 0x38, 0x4A, 0xE0, 0x3D,
  377. 0x79, 0x63, 0x66, 0x73, 0x9C, 0x07, 0xE1, 0xEC, 0x71, 0x16, 0x50, 0x75,
  378. 0xA1, 0xBA, 0xE5, 0x37, 0x45, 0x1A, 0x0C, 0x59},
  379. {0xC9, 0x49, 0xB9, 0xDB, 0x7E, 0x76, 0xC5, 0xC5, 0x0A, 0x87, 0xB7, 0x56,
  380. 0x88, 0x09, 0x21, 0xC6, 0xF6, 0x6C, 0xCC, 0x5E, 0x80, 0xFD, 0x05, 0xD0,
  381. 0x5F, 0xC6, 0x2E, 0x06, 0xA1, 0xBE, 0x5B, 0xA0},
  382. },
  383. };
  384. const G2ElemStr EcGroupTest::efq2_mul_ab_str = {
  385. {
  386. {0x25, 0xCC, 0x11, 0x80, 0x8F, 0x08, 0x1D, 0x66, 0xF8, 0xDB, 0xBC, 0x98,
  387. 0x26, 0x24, 0x26, 0xCF, 0x04, 0x02, 0xB6, 0x99, 0x1B, 0x52, 0xA8, 0xE3,
  388. 0x4E, 0x9A, 0x85, 0xB0, 0x5C, 0xCE, 0xDD, 0xC5},
  389. {0xFC, 0x3C, 0xC2, 0x2C, 0x4B, 0x63, 0x72, 0x5F, 0xA9, 0xF9, 0x8C, 0x62,
  390. 0xF4, 0xE7, 0x30, 0x71, 0x6F, 0x78, 0xF5, 0xFE, 0xF6, 0xDF, 0xF7, 0xB5,
  391. 0x21, 0x69, 0x7C, 0x50, 0xAC, 0x56, 0xD9, 0xB5},
  392. },
  393. {
  394. {0xA5, 0xD6, 0xAB, 0x2D, 0xED, 0x8E, 0xFE, 0x43, 0xCB, 0xC9, 0xEF, 0x09,
  395. 0xC8, 0x2D, 0xE8, 0xD0, 0x3B, 0xC0, 0x5C, 0x7F, 0xE5, 0x3A, 0x1D, 0x72,
  396. 0xF2, 0xF5, 0x03, 0xBD, 0xE5, 0xEB, 0x08, 0xA0},
  397. {0xE6, 0xF3, 0x59, 0xE4, 0xD2, 0x52, 0xFD, 0x4F, 0xEC, 0xCE, 0x49, 0x9F,
  398. 0x86, 0x50, 0x2D, 0x4A, 0x59, 0x2C, 0xA2, 0x4E, 0xE3, 0xFE, 0xF2, 0xFC,
  399. 0xB9, 0xF4, 0x22, 0x88, 0xBC, 0x79, 0x21, 0xD0},
  400. },
  401. };
  402. const G2ElemStr EcGroupTest::efq2_exp_ax_str = {
  403. {
  404. {0xC0, 0x5A, 0x37, 0xAD, 0x08, 0xAB, 0x22, 0xCF, 0xF7, 0xF9, 0xCC, 0xD4,
  405. 0x5A, 0x47, 0x38, 0x82, 0xE1, 0xC2, 0x06, 0x35, 0x4D, 0x5B, 0x95, 0xA1,
  406. 0xA3, 0xC1, 0x83, 0x6C, 0x0F, 0x31, 0x24, 0xD2},
  407. {0xC7, 0x86, 0xE1, 0x59, 0x63, 0xCE, 0x21, 0x2A, 0x57, 0x77, 0xE5, 0x48,
  408. 0xF7, 0x60, 0x21, 0x00, 0x40, 0x2F, 0x09, 0x18, 0x5C, 0x32, 0x32, 0x75,
  409. 0xD7, 0xB9, 0xE7, 0xB1, 0x95, 0xD5, 0xDF, 0x02},
  410. },
  411. {
  412. {0xE5, 0xDE, 0xC6, 0x3E, 0x05, 0xFC, 0x6F, 0x7A, 0xE3, 0x2D, 0x7D, 0x90,
  413. 0x5F, 0x43, 0xE2, 0xB0, 0x9E, 0xCD, 0xEC, 0x7B, 0x37, 0x4C, 0x0A, 0x3E,
  414. 0x87, 0x4E, 0xE6, 0xDA, 0xD1, 0x90, 0xC0, 0xD1},
  415. {0x70, 0x90, 0x54, 0x7F, 0x78, 0x93, 0xFA, 0xC4, 0xF7, 0x3A, 0x4D, 0xBC,
  416. 0x03, 0x5E, 0x83, 0xDF, 0xEF, 0xF7, 0x52, 0xF9, 0x64, 0x7F, 0x17, 0xC1,
  417. 0x69, 0xD6, 0xD7, 0x96, 0x18, 0x62, 0x46, 0xD1},
  418. },
  419. };
  420. const G2ElemStr EcGroupTest::efq2_multiexp_abxy_str = {
  421. {
  422. {0xE8, 0x6E, 0x02, 0x7A, 0xEC, 0xEA, 0xBA, 0x7E, 0xE5, 0x7C, 0xAD, 0x98,
  423. 0x37, 0x54, 0xB2, 0x15, 0x64, 0x9C, 0x81, 0xFF, 0x69, 0xCC, 0xD6, 0xA6,
  424. 0xAA, 0xA7, 0x10, 0x4F, 0x9B, 0x0C, 0x50, 0x14},
  425. {0x7C, 0xAF, 0xC0, 0x6F, 0xC8, 0x87, 0xFF, 0x4A, 0x6F, 0xB5, 0x9E, 0x63,
  426. 0x74, 0x20, 0xB5, 0xC6, 0x4F, 0x14, 0x0B, 0x6C, 0xBF, 0x00, 0x71, 0xE2,
  427. 0x6D, 0x6C, 0x41, 0x6A, 0x0B, 0xA5, 0x5B, 0xCF},
  428. },
  429. {
  430. {0x16, 0xCC, 0x9B, 0x37, 0xE7, 0xCB, 0x16, 0x5C, 0x39, 0x7C, 0x10, 0x7E,
  431. 0xE0, 0xDD, 0x34, 0x90, 0xBE, 0x56, 0x28, 0x76, 0x27, 0x59, 0xCE, 0xB3,
  432. 0xD7, 0xB4, 0x56, 0xD4, 0x0D, 0xD1, 0xB8, 0xFB},
  433. {0x5E, 0x9E, 0x27, 0x30, 0x60, 0x87, 0x3B, 0xA4, 0x9B, 0x15, 0xEE, 0x86,
  434. 0x15, 0x1D, 0xF4, 0xF3, 0x07, 0x31, 0x46, 0xFD, 0xB7, 0x51, 0xFF, 0xC0,
  435. 0x42, 0x94, 0x38, 0xB7, 0x84, 0x5F, 0x86, 0x3A},
  436. },
  437. };
  438. const G2ElemStr EcGroupTest::efq2_inv_a_str = {
  439. {
  440. {0x2F, 0x8C, 0xC7, 0xD7, 0xD4, 0x1E, 0x4A, 0xCB, 0x82, 0x92, 0xC7, 0x9C,
  441. 0x0F, 0xA2, 0xF2, 0x1B, 0xDF, 0xEA, 0x96, 0x64, 0x8B, 0xA2, 0x32, 0x7C,
  442. 0xDF, 0xD8, 0x89, 0x10, 0xFD, 0xBB, 0x38, 0xCD},
  443. {0xB1, 0x23, 0x46, 0x13, 0x4D, 0x9B, 0x8E, 0x8A, 0x95, 0x64, 0xDD, 0x37,
  444. 0x29, 0x44, 0x1F, 0x76, 0xB5, 0x3A, 0x47, 0xD3, 0xE0, 0x18, 0x1E, 0x60,
  445. 0xE9, 0x94, 0x13, 0xA4, 0x47, 0xCD, 0xBE, 0x03},
  446. },
  447. {
  448. {0x2C, 0x98, 0x5A, 0x33, 0x10, 0x81, 0x1F, 0x3F, 0xFC, 0x66, 0x00, 0xCF,
  449. 0x87, 0xA6, 0x46, 0x18, 0x60, 0x11, 0x2F, 0x9B, 0xE9, 0x07, 0xE2, 0x2C,
  450. 0xE2, 0x4C, 0xBF, 0x50, 0x27, 0x1D, 0x57, 0xE1},
  451. {0x93, 0xF5, 0x3A, 0xA7, 0x4E, 0xAE, 0x26, 0x48, 0x02, 0xA7, 0x13, 0xED,
  452. 0x52, 0xAA, 0x14, 0x86, 0x06, 0x09, 0xC5, 0xAC, 0x4B, 0x64, 0x16, 0x25,
  453. 0xEB, 0x12, 0x4B, 0x73, 0xD3, 0x13, 0xCB, 0x8F},
  454. },
  455. };
  456. const G2ElemStr EcGroupTest::efq2_identity_str = {
  457. {
  458. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  459. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  460. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  461. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  462. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  463. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  464. },
  465. {
  466. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  467. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  468. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  469. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  470. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  471. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  472. },
  473. };
  474. // msg=aad, size=3
  475. // algorithm code path: sqrt result <= modulus/2, high bit is 0
  476. const G1ElemStr EcGroupTest::kAadHash = {
  477. 0xB2, 0x12, 0x39, 0x3A, 0xA0, 0xCF, 0xA0, 0xDE, 0xB8, 0x85, 0xE7,
  478. 0x5B, 0x1C, 0x13, 0x01, 0x0D, 0x0D, 0xA2, 0xBA, 0xC5, 0xB4, 0x3F,
  479. 0x5E, 0xC7, 0x5B, 0x5A, 0xE2, 0x49, 0x1B, 0x3F, 0x65, 0x08, 0xC2,
  480. 0x47, 0x40, 0xF3, 0xC7, 0x08, 0xA2, 0x41, 0x61, 0x99, 0x65, 0x4D,
  481. 0x82, 0x2B, 0x9A, 0x06, 0x2C, 0xDF, 0x07, 0x71, 0xCC, 0xFA, 0x73,
  482. 0x51, 0x45, 0x87, 0x55, 0x07, 0x17, 0xD1, 0x9C, 0x0B};
  483. // msg=bsn0, size=4
  484. // algorithm code path: sqrt result <= modulus/2, high bit is 1
  485. const G1ElemStr EcGroupTest::kBsn0Hash = {
  486. 0x04, 0x0C, 0xB6, 0x57, 0x26, 0xD0, 0xE1, 0x48, 0x23, 0xC2, 0x40,
  487. 0x5A, 0x91, 0x7C, 0xC6, 0x33, 0xFE, 0x0C, 0xC2, 0x2B, 0x52, 0x9D,
  488. 0x6B, 0x87, 0xF9, 0xA7, 0x82, 0xCB, 0x36, 0x90, 0xFB, 0x09, 0x10,
  489. 0xB1, 0x55, 0xAD, 0x98, 0x0D, 0x4F, 0x94, 0xDD, 0xBE, 0x52, 0x21,
  490. 0x87, 0xC6, 0x3E, 0x52, 0x22, 0x83, 0xE3, 0x10, 0x36, 0xEF, 0xF8,
  491. 0x6B, 0x04, 0x4D, 0x9F, 0x14, 0xA8, 0x51, 0xAF, 0xC3};
  492. // msg=test, size=4
  493. // algorithm code path: sqrt result > modulus/2, high bit is 0
  494. const G1ElemStr EcGroupTest::kTestHash = {
  495. 0x82, 0x14, 0xAD, 0xE2, 0x0E, 0xCC, 0x95, 0x27, 0x14, 0xD0, 0x70,
  496. 0xF1, 0x70, 0x17, 0xC2, 0xC2, 0x8C, 0x9F, 0x05, 0x79, 0xCD, 0xC8,
  497. 0x72, 0x55, 0xFE, 0xAB, 0x80, 0x6F, 0x40, 0x5A, 0x6E, 0x64, 0x37,
  498. 0x14, 0x7F, 0x8B, 0xF9, 0xD7, 0xEB, 0xA4, 0x5D, 0x9E, 0x57, 0x85,
  499. 0xFF, 0x0F, 0xE5, 0xC6, 0x73, 0x4F, 0x17, 0x19, 0x96, 0x31, 0x3A,
  500. 0xD1, 0xE1, 0x4E, 0xA8, 0xF9, 0x56, 0xD4, 0xBA, 0x4D};
  501. // msg=aac, size=3
  502. const G1ElemStr EcGroupTest::kAacHash = {
  503. 0xAF, 0x5C, 0xBC, 0xD4, 0x88, 0x18, 0xD0, 0x35, 0xBD, 0xE0, 0x2F,
  504. 0x77, 0x8B, 0x76, 0x52, 0x78, 0x92, 0x66, 0x36, 0x3A, 0x72, 0x15,
  505. 0x20, 0x84, 0xE7, 0x1E, 0xFE, 0x94, 0x77, 0xFD, 0x83, 0x08, 0xEF,
  506. 0x4B, 0x6B, 0xDE, 0x24, 0xD8, 0x42, 0x34, 0x88, 0xB8, 0x87, 0x4A,
  507. 0xA8, 0x5D, 0x5A, 0xC1, 0x82, 0xFF, 0xE5, 0x25, 0xD7, 0x20, 0x2D,
  508. 0x99, 0x49, 0xFE, 0x72, 0x34, 0xAA, 0xC9, 0xD2, 0xAA};
  509. ///////////////////////////////////////////////////////////////////////
  510. // NewEcGroup
  511. TEST_F(EcGroupTest, NewFailsGivenArgumentsMismatch) {
  512. // construct Fq^2 finite field
  513. FqElemStr beta_str = {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xF0, 0xCD, 0x46,
  514. 0xE5, 0xF2, 0x5E, 0xEE, 0x71, 0xA4, 0x9F, 0x0C, 0xDC,
  515. 0x65, 0xFB, 0x12, 0x98, 0x0A, 0x82, 0xD3, 0x29, 0x2D,
  516. 0xDB, 0xAE, 0xD3, 0x30, 0x12}};
  517. FfElementObj neg_beta(&fq);
  518. THROW_ON_EPIDERR(FfNeg(fq, FfElementObj(&fq, beta_str), neg_beta));
  519. FiniteFieldObj fq2(fq, neg_beta, 2);
  520. FfElementObj fq2_a(&fq2);
  521. FfElementObj fq2_b(&fq2);
  522. FfElementObj g2_x(&fq2);
  523. FfElementObj g2_y(&fq2);
  524. EcGroup* g = nullptr;
  525. EXPECT_EQ(kEpidBadArgErr, NewEcGroup(fq2, this->fq_a, this->fq_b, this->g1_x,
  526. this->g1_y, this->bn_p, this->bn_h, &g));
  527. DeleteEcGroup(&g);
  528. EXPECT_EQ(kEpidBadArgErr, NewEcGroup(this->fq, fq2_a, this->fq_b, this->g1_x,
  529. this->g1_y, this->bn_p, this->bn_h, &g));
  530. DeleteEcGroup(&g);
  531. EXPECT_EQ(kEpidBadArgErr, NewEcGroup(this->fq, this->fq_a, fq2_b, this->g1_x,
  532. this->g1_y, this->bn_p, this->bn_h, &g));
  533. DeleteEcGroup(&g);
  534. EXPECT_EQ(kEpidBadArgErr, NewEcGroup(this->fq, this->fq_a, this->fq_b, g2_x,
  535. this->g1_y, this->bn_p, this->bn_h, &g));
  536. DeleteEcGroup(&g);
  537. EXPECT_EQ(kEpidBadArgErr,
  538. NewEcGroup(this->fq, this->fq_a, this->fq_b, this->g1_x, g2_y,
  539. this->bn_p, this->bn_h, &g));
  540. DeleteEcGroup(&g);
  541. }
  542. TEST_F(EcGroupTest, NewFailsGivenNullParameters) {
  543. EcGroup* g;
  544. EpidStatus sts;
  545. sts = NewEcGroup(this->fq, nullptr, this->fq_b, this->g1_x, this->g1_y,
  546. this->bn_p, this->bn_h, &g);
  547. EXPECT_EQ(kEpidBadArgErr, sts);
  548. sts = NewEcGroup(this->fq, this->fq_a, nullptr, this->g1_x, this->g1_y,
  549. this->bn_p, this->bn_h, &g);
  550. EXPECT_EQ(kEpidBadArgErr, sts);
  551. sts = NewEcGroup(this->fq, this->fq_a, this->fq_b, nullptr, this->g1_y,
  552. this->bn_p, this->bn_h, &g);
  553. EXPECT_EQ(kEpidBadArgErr, sts);
  554. sts = NewEcGroup(this->fq, this->fq_a, this->fq_b, this->g1_x, nullptr,
  555. this->bn_p, this->bn_h, &g);
  556. EXPECT_EQ(kEpidBadArgErr, sts);
  557. sts = NewEcGroup(this->fq, this->fq_a, this->fq_b, this->g1_x, this->g1_y,
  558. nullptr, this->bn_h, &g);
  559. EXPECT_EQ(kEpidBadArgErr, sts);
  560. sts = NewEcGroup(this->fq, this->fq_a, this->fq_b, this->g1_x, this->g1_y,
  561. this->bn_p, nullptr, &g);
  562. EXPECT_EQ(kEpidBadArgErr, sts);
  563. sts = NewEcGroup(this->fq, this->fq_a, this->fq_b, this->g1_x, this->g1_y,
  564. this->bn_p, this->bn_h, nullptr);
  565. EXPECT_EQ(kEpidBadArgErr, sts);
  566. }
  567. TEST_F(EcGroupTest, CanCreateEcGroupBasedOnFq) {
  568. EcGroup* g;
  569. EpidStatus sts = NewEcGroup(this->fq, this->fq_a, this->fq_b, this->g1_x,
  570. this->g1_y, this->bn_p, this->bn_h, &g);
  571. EXPECT_EQ(kEpidNoErr, sts);
  572. DeleteEcGroup(&g);
  573. }
  574. TEST_F(EcGroupTest, CanCreateEcGroupBasedOnFq2) {
  575. EcGroup* g;
  576. EXPECT_EQ(kEpidNoErr,
  577. NewEcGroup(efq2_par->fq2, efq2_par->a, efq2_par->b, efq2_par->x,
  578. efq2_par->y, efq2_par->order, efq2_par->cofactor, &g));
  579. DeleteEcGroup(&g);
  580. }
  581. ///////////////////////////////////////////////////////////////////////
  582. // DeleteEcGroup
  583. TEST_F(EcGroupTest, DeleteWorksGivenNewlyCreatedEcGroup) {
  584. EcGroup* g;
  585. THROW_ON_EPIDERR(NewEcGroup(this->fq, this->fq_a, this->fq_b, this->g1_x,
  586. this->g1_y, this->bn_p, this->bn_h, &g));
  587. EXPECT_NO_THROW(DeleteEcGroup(&g));
  588. }
  589. TEST_F(EcGroupTest, DeleteWorksGivenNewlyCreatedEcGroupFq2) {
  590. EcGroup* g;
  591. THROW_ON_EPIDERR(NewEcGroup(efq2_par->fq2, efq2_par->a, efq2_par->b,
  592. efq2_par->x, efq2_par->y, efq2_par->order,
  593. efq2_par->cofactor, &g));
  594. EXPECT_NO_THROW(DeleteEcGroup(&g));
  595. }
  596. TEST_F(EcGroupTest, DeleteNullsPointer) {
  597. EcGroup* g = nullptr;
  598. THROW_ON_EPIDERR(NewEcGroup(this->fq, this->fq_a, this->fq_b, this->g1_x,
  599. this->g1_y, this->bn_p, this->bn_h, &g));
  600. EXPECT_NO_THROW(DeleteEcGroup(&g));
  601. EXPECT_EQ(nullptr, g);
  602. }
  603. TEST_F(EcGroupTest, DeleteWorksGivenNullPointer) {
  604. EXPECT_NO_THROW(DeleteEcGroup(nullptr));
  605. EcGroup* g = nullptr;
  606. EXPECT_NO_THROW(DeleteEcGroup(&g));
  607. }
  608. ///////////////////////////////////////////////////////////////////////
  609. // NewEcPoint
  610. TEST_F(EcGroupTest, NewEcPointSucceedsGivenEcGroupBasedOnFq) {
  611. EcPoint* point = nullptr;
  612. EXPECT_EQ(kEpidNoErr, NewEcPoint(this->efq, &point));
  613. DeleteEcPoint(&point);
  614. }
  615. TEST_F(EcGroupTest, NewEcPointFailsGivenNullPointer) {
  616. EcPoint* point = nullptr;
  617. EXPECT_EQ(kEpidBadArgErr, NewEcPoint(nullptr, &point));
  618. EXPECT_EQ(kEpidBadArgErr, NewEcPoint(this->efq, nullptr));
  619. DeleteEcPoint(&point);
  620. }
  621. TEST_F(EcGroupTest, NewEcPointSucceedsGivenEcGroupBasedOnFq2) {
  622. EcPoint* point = nullptr;
  623. EXPECT_EQ(kEpidNoErr, NewEcPoint(this->efq2, &point));
  624. DeleteEcPoint(&point);
  625. }
  626. TEST_F(EcGroupTest, DefaultEcPointIsIdentity) {
  627. G1ElemStr g1_elem_str = {{{{0}}}, {{{0}}}};
  628. EcPoint* point = nullptr;
  629. EXPECT_EQ(kEpidNoErr, NewEcPoint(this->efq, &point));
  630. EpidStatus sts =
  631. WriteEcPoint(this->efq, point, &g1_elem_str, sizeof(g1_elem_str));
  632. EXPECT_EQ(this->efq_identity_str, g1_elem_str);
  633. DeleteEcPoint(&point);
  634. THROW_ON_EPIDERR(sts);
  635. G2ElemStr g2_elem_str = {{{{0}}}, {{{0}}}};
  636. EXPECT_EQ(kEpidNoErr, NewEcPoint(this->efq2, &point));
  637. sts = WriteEcPoint(this->efq2, point, &g2_elem_str, sizeof(g2_elem_str));
  638. EXPECT_EQ(this->efq2_identity_str, g2_elem_str);
  639. DeleteEcPoint(&point);
  640. THROW_ON_EPIDERR(sts);
  641. }
  642. ///////////////////////////////////////////////////////////////////////
  643. // DeleteEcPoint
  644. TEST_F(EcGroupTest, DeleteEcPointNullsPointer) {
  645. EcPoint* point = nullptr;
  646. THROW_ON_EPIDERR(NewEcPoint(this->efq, &point));
  647. EXPECT_NO_THROW(DeleteEcPoint(&point));
  648. EXPECT_EQ(nullptr, point);
  649. }
  650. TEST_F(EcGroupTest, DeleteEcPointWorksGivenNullPointer) {
  651. EXPECT_NO_THROW(DeleteEcPoint(nullptr));
  652. EcPoint* point = nullptr;
  653. EXPECT_NO_THROW(DeleteEcPoint(&point));
  654. EXPECT_EQ(nullptr, point);
  655. }
  656. ///////////////////////////////////////////////////////////////////////
  657. // ReadEcPoint
  658. TEST_F(EcGroupTest, ReadFailsGivenNullPointer) {
  659. EXPECT_EQ(kEpidBadArgErr, ReadEcPoint(nullptr, &(this->efq_a_str),
  660. sizeof(this->efq_a_str), this->efq_a));
  661. EXPECT_EQ(kEpidBadArgErr, ReadEcPoint(this->efq, nullptr,
  662. sizeof(this->efq_a_str), this->efq_a));
  663. EXPECT_EQ(kEpidBadArgErr, ReadEcPoint(this->efq, &(this->efq_a_str),
  664. sizeof(this->efq_a_str), nullptr));
  665. }
  666. TEST_F(EcGroupTest, ReadFailsGivenInvalidBufferSize) {
  667. EXPECT_EQ(kEpidBadArgErr,
  668. ReadEcPoint(this->efq, &(this->efq_a_str), 0, this->efq_a));
  669. EXPECT_EQ(kEpidBadArgErr,
  670. ReadEcPoint(this->efq, &(this->efq_a_str),
  671. sizeof(this->efq_a_str) - 1, this->efq_a));
  672. EXPECT_EQ(kEpidBadArgErr,
  673. ReadEcPoint(this->efq, &(this->efq_a_str),
  674. std::numeric_limits<size_t>::max(), this->efq_a));
  675. }
  676. TEST_F(EcGroupTest, ReadEcPointReadsG1PointCorrectly) {
  677. G1ElemStr g1_elem_str = {{{{0}}}, {{{0}}}};
  678. EXPECT_EQ(kEpidNoErr, ReadEcPoint(this->efq, &this->efq_a_str,
  679. sizeof(this->efq_a_str), this->efq_a));
  680. THROW_ON_EPIDERR(
  681. WriteEcPoint(this->efq, this->efq_a, &g1_elem_str, sizeof(g1_elem_str)));
  682. EXPECT_EQ(this->efq_a_str, g1_elem_str);
  683. }
  684. TEST_F(EcGroupTest, ReadEcPointReadsG1IdentityPointCorrectly) {
  685. G1ElemStr g1_elem_str = {{{{0}}}, {{{0}}}};
  686. EXPECT_EQ(kEpidNoErr,
  687. ReadEcPoint(this->efq, &this->efq_identity_str,
  688. sizeof(this->efq_identity_str), this->efq_a));
  689. THROW_ON_EPIDERR(
  690. WriteEcPoint(this->efq, this->efq_a, &g1_elem_str, sizeof(g1_elem_str)));
  691. EXPECT_EQ(this->efq_identity_str, g1_elem_str);
  692. }
  693. TEST_F(EcGroupTest, ReadEcPointReadsG2IdentityPointCorrectly) {
  694. G2ElemStr g2_elem_str = {{{{0}}}, {{{0}}}};
  695. EXPECT_EQ(kEpidNoErr,
  696. ReadEcPoint(this->efq2, &this->efq2_identity_str,
  697. sizeof(this->efq2_identity_str), this->efq2_r));
  698. THROW_ON_EPIDERR(WriteEcPoint(this->efq2, this->efq2_r, &g2_elem_str,
  699. sizeof(g2_elem_str)));
  700. EXPECT_EQ(this->efq2_identity_str, g2_elem_str);
  701. }
  702. TEST_F(EcGroupTest, ReadEcPointReadsG2PointCorrectly) {
  703. G2ElemStr g2_elem_str = {{{{0}}}, {{{0}}}};
  704. EXPECT_EQ(kEpidNoErr, ReadEcPoint(this->efq2, &this->efq2_a_str,
  705. sizeof(this->efq2_a_str), this->efq2_r));
  706. THROW_ON_EPIDERR(WriteEcPoint(this->efq2, this->efq2_r, &g2_elem_str,
  707. sizeof(g2_elem_str)));
  708. EXPECT_EQ(this->efq2_a_str, g2_elem_str);
  709. }
  710. TEST_F(EcGroupTest, ReadFailsGivenPointDoesNotBelongToEcGroup) {
  711. G1ElemStr bad_g1_point = this->efq_a_str;
  712. bad_g1_point.x.data.data[31]++; // make point not belong to the group
  713. EXPECT_EQ(kEpidBadArgErr, ReadEcPoint(this->efq, &bad_g1_point,
  714. sizeof(bad_g1_point), this->efq_a));
  715. G2ElemStr bad_g2_point = this->efq2_a_str;
  716. bad_g2_point.x[0].data.data[31]++; // make point not belong to the group
  717. EXPECT_EQ(kEpidBadArgErr, ReadEcPoint(this->efq2, &bad_g2_point,
  718. sizeof(bad_g2_point), this->efq2_a));
  719. }
  720. ///////////////////////////////////////////////////////////////////////
  721. // WriteEcPoint
  722. TEST_F(EcGroupTest, WriteFailsGivenNullPointer) {
  723. G1ElemStr g1_elem_str = {{{{0}}}, {{{0}}}};
  724. EXPECT_EQ(kEpidBadArgErr, WriteEcPoint(nullptr, this->efq_a, &g1_elem_str,
  725. sizeof(g1_elem_str)));
  726. EXPECT_EQ(kEpidBadArgErr, WriteEcPoint(this->efq, nullptr, &g1_elem_str,
  727. sizeof(g1_elem_str)));
  728. EXPECT_EQ(kEpidBadArgErr,
  729. WriteEcPoint(this->efq, this->efq_a, nullptr, sizeof(g1_elem_str)));
  730. }
  731. TEST_F(EcGroupTest, WriteFailsGivenInvalidBufferSize) {
  732. G1ElemStr g1_elem_str = {{{{0}}}, {{{0}}}};
  733. EXPECT_EQ(kEpidBadArgErr,
  734. WriteEcPoint(this->efq, this->efq_a, &g1_elem_str, 0));
  735. EXPECT_EQ(kEpidBadArgErr, WriteEcPoint(this->efq, this->efq_a, &g1_elem_str,
  736. sizeof(g1_elem_str) - 1));
  737. EXPECT_EQ(kEpidBadArgErr, WriteEcPoint(this->efq, this->efq_a, &g1_elem_str,
  738. std::numeric_limits<size_t>::max()));
  739. }
  740. TEST_F(EcGroupTest, WriteEcPointWritesG1PointCorrectly) {
  741. G1ElemStr g1_elem_str = {{{{0}}}, {{{0}}}};
  742. EXPECT_EQ(kEpidNoErr, WriteEcPoint(this->efq, this->efq_a, &g1_elem_str,
  743. sizeof(g1_elem_str)));
  744. EXPECT_EQ(this->efq_a_str, g1_elem_str);
  745. }
  746. TEST_F(EcGroupTest, WriteEcPointWritesG1IdentityPointCorrectly) {
  747. G1ElemStr g1_elem_str = {{{{0}}}, {{{0}}}};
  748. EXPECT_EQ(kEpidNoErr, WriteEcPoint(this->efq, this->efq_identity,
  749. &g1_elem_str, sizeof(g1_elem_str)));
  750. EXPECT_EQ(this->efq_identity_str, g1_elem_str);
  751. }
  752. TEST_F(EcGroupTest, WriteEcPointWritesG2IdentityPointCorrectly) {
  753. G2ElemStr g2_elem_str = {{{{0}}}, {{{0}}}};
  754. EXPECT_EQ(kEpidNoErr, WriteEcPoint(this->efq2, this->efq2_identity,
  755. &g2_elem_str, sizeof(g2_elem_str)));
  756. EXPECT_EQ(this->efq2_identity_str, g2_elem_str);
  757. }
  758. TEST_F(EcGroupTest, WriteEcPointWritesG2PointCorrectly) {
  759. G2ElemStr g2_elem_str = {{{{0}}}, {{{0}}}};
  760. EXPECT_EQ(kEpidNoErr, WriteEcPoint(this->efq2, this->efq2_a, &g2_elem_str,
  761. sizeof(g2_elem_str)));
  762. EXPECT_EQ(this->efq2_a_str, g2_elem_str);
  763. }
  764. ///////////////////////////////////////////////////////////////////////
  765. // EcMul
  766. TEST_F(EcGroupTest, MulFailsGivenArgumentsMismatch) {
  767. EXPECT_EQ(kEpidBadArgErr,
  768. EcMul(this->efq2, this->efq_a, this->efq_b, this->efq_r));
  769. EXPECT_EQ(kEpidBadArgErr,
  770. EcMul(this->efq, this->efq2_a, this->efq_b, this->efq_r));
  771. EXPECT_EQ(kEpidBadArgErr,
  772. EcMul(this->efq, this->efq_a, this->efq2_b, this->efq_r));
  773. EXPECT_EQ(kEpidBadArgErr,
  774. EcMul(this->efq, this->efq_a, this->efq_b, this->efq2_r));
  775. }
  776. TEST_F(EcGroupTest, MulFailsGivenNullPointer) {
  777. EXPECT_EQ(kEpidBadArgErr,
  778. EcMul(nullptr, this->efq_a, this->efq_b, this->efq_r));
  779. EXPECT_EQ(kEpidBadArgErr,
  780. EcMul(this->efq, nullptr, this->efq_b, this->efq_r));
  781. EXPECT_EQ(kEpidBadArgErr,
  782. EcMul(this->efq, this->efq_a, nullptr, this->efq_r));
  783. EXPECT_EQ(kEpidBadArgErr,
  784. EcMul(this->efq, this->efq_a, this->efq_b, nullptr));
  785. }
  786. TEST_F(EcGroupTest, MulSucceedsGivenIdentityElement) {
  787. G1ElemStr efq_r_str;
  788. EXPECT_EQ(kEpidNoErr,
  789. EcMul(this->efq, this->efq_a, this->efq_identity, this->efq_r));
  790. THROW_ON_EPIDERR(
  791. WriteEcPoint(this->efq, efq_r, &efq_r_str, sizeof(efq_r_str)));
  792. EXPECT_EQ(this->efq_a_str, efq_r_str);
  793. EXPECT_EQ(kEpidNoErr,
  794. EcMul(this->efq, this->efq_identity, this->efq_a, this->efq_r));
  795. THROW_ON_EPIDERR(
  796. WriteEcPoint(this->efq, efq_r, &efq_r_str, sizeof(efq_r_str)));
  797. EXPECT_EQ(this->efq_a_str, efq_r_str);
  798. }
  799. TEST_F(EcGroupTest, MulSucceedsGivenTwoElements) {
  800. G1ElemStr efq_r_str;
  801. EXPECT_EQ(kEpidNoErr,
  802. EcMul(this->efq, this->efq_a, this->efq_b, this->efq_r));
  803. THROW_ON_EPIDERR(
  804. WriteEcPoint(this->efq, this->efq_r, &efq_r_str, sizeof(efq_r_str)));
  805. EXPECT_EQ(this->efq_mul_ab_str, efq_r_str);
  806. }
  807. TEST_F(EcGroupTest, MulSucceedsGivenG2IdentityElement) {
  808. G2ElemStr efq2_r_str;
  809. EXPECT_EQ(kEpidNoErr,
  810. EcMul(this->efq2, this->efq2_a, this->efq2_identity, this->efq2_r));
  811. THROW_ON_EPIDERR(
  812. WriteEcPoint(this->efq2, efq2_r, &efq2_r_str, sizeof(efq2_r_str)));
  813. EXPECT_EQ(this->efq2_a_str, efq2_r_str);
  814. EXPECT_EQ(kEpidNoErr,
  815. EcMul(this->efq2, this->efq2_identity, this->efq2_a, this->efq2_r));
  816. THROW_ON_EPIDERR(
  817. WriteEcPoint(this->efq2, efq2_r, &efq2_r_str, sizeof(efq2_r_str)));
  818. EXPECT_EQ(this->efq2_a_str, efq2_r_str);
  819. }
  820. TEST_F(EcGroupTest, MulSucceedsGivenTwoG2Elements) {
  821. G2ElemStr efq2_r_str;
  822. EXPECT_EQ(kEpidNoErr,
  823. EcMul(this->efq2, this->efq2_a, this->efq2_b, this->efq2_r));
  824. THROW_ON_EPIDERR(
  825. WriteEcPoint(this->efq2, this->efq2_r, &efq2_r_str, sizeof(efq2_r_str)));
  826. EXPECT_EQ(this->efq2_mul_ab_str, efq2_r_str);
  827. }
  828. ///////////////////////////////////////////////////////////////////////
  829. // EcExp
  830. TEST_F(EcGroupTest, ExpFailsGivenArgumentsMismatch) {
  831. BigNumStr zero_bn_str = {0};
  832. EXPECT_EQ(kEpidBadArgErr,
  833. EcExp(this->efq2, this->efq_a, &zero_bn_str, this->efq_r));
  834. EXPECT_EQ(kEpidBadArgErr,
  835. EcExp(this->efq, this->efq2_a, &zero_bn_str, this->efq_r));
  836. EXPECT_EQ(kEpidBadArgErr,
  837. EcExp(this->efq, this->efq_a, &zero_bn_str, this->efq2_r));
  838. }
  839. TEST_F(EcGroupTest, ExpFailsGivenNullPointer) {
  840. BigNumStr zero_bn_str = {0};
  841. EXPECT_EQ(kEpidBadArgErr,
  842. EcExp(nullptr, this->efq_a, &zero_bn_str, this->efq_r));
  843. EXPECT_EQ(kEpidBadArgErr,
  844. EcExp(this->efq, nullptr, &zero_bn_str, this->efq_r));
  845. EXPECT_EQ(kEpidBadArgErr,
  846. EcExp(this->efq, this->efq_a, nullptr, this->efq_r));
  847. EXPECT_EQ(kEpidBadArgErr,
  848. EcExp(this->efq, this->efq_a, &zero_bn_str, nullptr));
  849. }
  850. TEST_F(EcGroupTest, ExpSucceedsGivenZeroExponent) {
  851. G1ElemStr efq_r_str;
  852. BigNumStr zero_bn_str = {0};
  853. EXPECT_EQ(kEpidNoErr,
  854. EcExp(this->efq, this->efq_a, &zero_bn_str, this->efq_r));
  855. THROW_ON_EPIDERR(
  856. WriteEcPoint(this->efq, this->efq_r, &efq_r_str, sizeof(efq_r_str)));
  857. EXPECT_EQ(this->efq_identity_str, efq_r_str);
  858. }
  859. TEST_F(EcGroupTest, ExpResultIsCorrect) {
  860. G1ElemStr efq_r_str;
  861. EXPECT_EQ(kEpidNoErr,
  862. EcExp(this->efq, this->efq_a, &this->x_str, this->efq_r));
  863. THROW_ON_EPIDERR(
  864. WriteEcPoint(this->efq, this->efq_r, &efq_r_str, sizeof(efq_r_str)));
  865. EXPECT_EQ(this->efq_exp_ax_str, efq_r_str);
  866. }
  867. TEST_F(EcGroupTest, ExpFailsGivenOutOfRangeExponent) {
  868. // The exponent should be less than elliptic curve group order
  869. EXPECT_EQ(kEpidBadArgErr,
  870. EcExp(this->efq, this->efq_a, &this->p, this->efq_r));
  871. }
  872. TEST_F(EcGroupTest, ExpSucceedsGivenG2ZeroExponent) {
  873. G2ElemStr efq2_r_str;
  874. BigNumStr zero_bn_str = {0};
  875. EXPECT_EQ(kEpidNoErr,
  876. EcExp(this->efq2, this->efq2_a, &zero_bn_str, this->efq2_r));
  877. THROW_ON_EPIDERR(
  878. WriteEcPoint(this->efq2, this->efq2_r, &efq2_r_str, sizeof(efq2_r_str)));
  879. EXPECT_EQ(this->efq2_identity_str, efq2_r_str);
  880. }
  881. TEST_F(EcGroupTest, ExpResultIsCorrectForG2) {
  882. G2ElemStr efq2_r_str;
  883. EXPECT_EQ(kEpidNoErr,
  884. EcExp(this->efq2, this->efq2_a, &this->x_str, this->efq2_r));
  885. THROW_ON_EPIDERR(
  886. WriteEcPoint(this->efq2, this->efq2_r, &efq2_r_str, sizeof(efq2_r_str)));
  887. EXPECT_EQ(this->efq2_exp_ax_str, efq2_r_str);
  888. }
  889. ///////////////////////////////////////////////////////////////////////
  890. // EcSscmExp
  891. TEST_F(EcGroupTest, SscmExpFailsGivenArgumentsMismatch) {
  892. BigNumStr zero_bn_str = {0};
  893. EXPECT_EQ(kEpidBadArgErr,
  894. EcSscmExp(this->efq2, this->efq_a, &zero_bn_str, this->efq_r));
  895. EXPECT_EQ(kEpidBadArgErr,
  896. EcSscmExp(this->efq, this->efq2_a, &zero_bn_str, this->efq_r));
  897. EXPECT_EQ(kEpidBadArgErr,
  898. EcSscmExp(this->efq, this->efq_a, &zero_bn_str, this->efq2_r));
  899. }
  900. TEST_F(EcGroupTest, SscmExpFailsGivenNullPointer) {
  901. BigNumStr zero_bn_str = {0};
  902. EXPECT_EQ(kEpidBadArgErr,
  903. EcSscmExp(nullptr, this->efq_a, &zero_bn_str, this->efq_r));
  904. EXPECT_EQ(kEpidBadArgErr,
  905. EcSscmExp(this->efq, nullptr, &zero_bn_str, this->efq_r));
  906. EXPECT_EQ(kEpidBadArgErr,
  907. EcSscmExp(this->efq, this->efq_a, nullptr, this->efq_r));
  908. EXPECT_EQ(kEpidBadArgErr,
  909. EcSscmExp(this->efq, this->efq_a, &zero_bn_str, nullptr));
  910. }
  911. TEST_F(EcGroupTest, SscmExpSucceedsGivenZeroExponent) {
  912. G1ElemStr efq_r_str;
  913. BigNumStr zero_bn_str = {0};
  914. EXPECT_EQ(kEpidNoErr,
  915. EcSscmExp(this->efq, this->efq_a, &zero_bn_str, this->efq_r));
  916. THROW_ON_EPIDERR(
  917. WriteEcPoint(this->efq, this->efq_r, &efq_r_str, sizeof(efq_r_str)));
  918. EXPECT_EQ(this->efq_identity_str, efq_r_str);
  919. }
  920. TEST_F(EcGroupTest, SscmExpResultIsCorrect) {
  921. G1ElemStr efq_r_str;
  922. EXPECT_EQ(kEpidNoErr,
  923. EcSscmExp(this->efq, this->efq_a, &this->x_str, this->efq_r));
  924. THROW_ON_EPIDERR(
  925. WriteEcPoint(this->efq, this->efq_r, &efq_r_str, sizeof(efq_r_str)));
  926. EXPECT_EQ(this->efq_exp_ax_str, efq_r_str);
  927. }
  928. TEST_F(EcGroupTest, SscmExpFailsGivenOutOfRangeExponent) {
  929. // The exponent should be less than elliptic curve group order
  930. EXPECT_EQ(kEpidBadArgErr,
  931. EcSscmExp(this->efq, this->efq_a, &this->p, this->efq_r));
  932. }
  933. TEST_F(EcGroupTest, SscmExpSucceedsGivenG2ZeroExponent) {
  934. G2ElemStr efq2_r_str;
  935. BigNumStr zero_bn_str = {0};
  936. EXPECT_EQ(kEpidNoErr,
  937. EcSscmExp(this->efq2, this->efq2_a, &zero_bn_str, this->efq2_r));
  938. THROW_ON_EPIDERR(
  939. WriteEcPoint(this->efq2, this->efq2_r, &efq2_r_str, sizeof(efq2_r_str)));
  940. EXPECT_EQ(this->efq2_identity_str, efq2_r_str);
  941. }
  942. TEST_F(EcGroupTest, SscmExpResultIsCorrectForG2) {
  943. G2ElemStr efq2_r_str;
  944. EXPECT_EQ(kEpidNoErr,
  945. EcSscmExp(this->efq2, this->efq2_a, &this->x_str, this->efq2_r));
  946. THROW_ON_EPIDERR(
  947. WriteEcPoint(this->efq2, this->efq2_r, &efq2_r_str, sizeof(efq2_r_str)));
  948. EXPECT_EQ(this->efq2_exp_ax_str, efq2_r_str);
  949. }
  950. ///////////////////////////////////////////////////////////////////////
  951. // EcMultiExp
  952. TEST_F(EcGroupTest, MultiExpFailsGivenArgumentsMismatch) {
  953. EcPoint const* pts_ec1[] = {this->efq_a, this->efq_b};
  954. EcPoint const* pts_ec2[] = {this->efq2_a, this->efq2_b};
  955. EcPoint const* pts_ec1_ec2[] = {this->efq_a, this->efq2_b};
  956. const BigNumStr bnm0 = {{0x11, 0xFF, 0xFF, 0xFF, 0x4F, 0x59, 0xB1, 0xD3, 0x6B,
  957. 0x08, 0xFF, 0xFF, 0x0B, 0xF3, 0xAF, 0x27, 0xFF, 0xB8,
  958. 0xFF, 0xFF, 0x98, 0xFF, 0xEB, 0xFF, 0xF2, 0x6A, 0xFF,
  959. 0xFF, 0xEA, 0x31, 0xFF, 0xFF}};
  960. const BigNumStr bnm1 = {{0xE2, 0xFF, 0x03, 0x1D, 0xFF, 0x19, 0x81, 0xCB, 0xFF,
  961. 0xFF, 0x6B, 0xD5, 0x3E, 0xFF, 0xFF, 0xFF, 0xFF, 0xBD,
  962. 0xFF, 0x5A, 0xFF, 0x5C, 0x7C, 0xFF, 0x84, 0xFF, 0xFF,
  963. 0x8C, 0x03, 0xB2, 0x26, 0xFF}};
  964. BigNumStr const* b[] = {&bnm0, &bnm1};
  965. size_t m = 2;
  966. EXPECT_EQ(kEpidBadArgErr, EcMultiExp(this->efq2, pts_ec1, b, m, this->efq_r));
  967. EXPECT_EQ(kEpidBadArgErr, EcMultiExp(this->efq, pts_ec2, b, m, this->efq_r));
  968. EXPECT_EQ(kEpidBadArgErr, EcMultiExp(this->efq, pts_ec1, b, m, this->efq2_r));
  969. EXPECT_EQ(kEpidBadArgErr,
  970. EcMultiExp(this->efq, pts_ec1_ec2, b, m, this->efq_r));
  971. }
  972. TEST_F(EcGroupTest, MultiExpFailsGivenNullPointer) {
  973. EcPoint const* pts[] = {this->efq_a, this->efq_b};
  974. EcPoint const* pts_withnull[] = {nullptr, this->efq_b};
  975. const BigNumStr bnm0 = {{0x11, 0xFF, 0xFF, 0xFF, 0x4F, 0x59, 0xB1, 0xD3, 0x6B,
  976. 0x08, 0xFF, 0xFF, 0x0B, 0xF3, 0xAF, 0x27, 0xFF, 0xB8,
  977. 0xFF, 0xFF, 0x98, 0xFF, 0xEB, 0xFF, 0xF2, 0x6A, 0xFF,
  978. 0xFF, 0xEA, 0x31, 0xFF, 0xFF}};
  979. const BigNumStr bnm1 = {{0xE2, 0xFF, 0x03, 0x1D, 0xFF, 0x19, 0x81, 0xCB, 0xFF,
  980. 0xFF, 0x6B, 0xD5, 0x3E, 0xFF, 0xFF, 0xFF, 0xFF, 0xBD,
  981. 0xFF, 0x5A, 0xFF, 0x5C, 0x7C, 0xFF, 0x84, 0xFF, 0xFF,
  982. 0x8C, 0x03, 0xB2, 0x26, 0xFF}};
  983. BigNumStr const* b[] = {&bnm0, &bnm1};
  984. BigNumStr const* b_withnull[] = {nullptr, &bnm1};
  985. size_t m = 2;
  986. EXPECT_EQ(kEpidBadArgErr, EcMultiExp(nullptr, pts, b, m, this->efq_r));
  987. EXPECT_EQ(kEpidBadArgErr, EcMultiExp(this->efq, nullptr, b, m, this->efq_r));
  988. EXPECT_EQ(kEpidBadArgErr,
  989. EcMultiExp(this->efq, pts, nullptr, m, this->efq_r));
  990. EXPECT_EQ(kEpidBadArgErr, EcMultiExp(this->efq, pts, b, m, nullptr));
  991. EXPECT_EQ(kEpidBadArgErr,
  992. EcMultiExp(this->efq, pts_withnull, b, m, this->efq_r));
  993. EXPECT_EQ(kEpidBadArgErr,
  994. EcMultiExp(this->efq, pts, b_withnull, m, this->efq_r));
  995. }
  996. TEST_F(EcGroupTest, MultiExpFailsGivenIncorrectMLen) {
  997. EcPoint const* pts[] = {this->efq_a, this->efq_b};
  998. const BigNumStr bnm0 = {{0x11, 0xFF, 0xFF, 0xFF, 0x4F, 0x59, 0xB1, 0xD3, 0x6B,
  999. 0x08, 0xFF, 0xFF, 0x0B, 0xF3, 0xAF, 0x27, 0xFF, 0xB8,
  1000. 0xFF, 0xFF, 0x98, 0xFF, 0xEB, 0xFF, 0xF2, 0x6A, 0xFF,
  1001. 0xFF, 0xEA, 0x31, 0xFF, 0xFF}};
  1002. const BigNumStr bnm1 = {{0xE2, 0xFF, 0x03, 0x1D, 0xFF, 0x19, 0x81, 0xCB, 0xFF,
  1003. 0xFF, 0x6B, 0xD5, 0x3E, 0xFF, 0xFF, 0xFF, 0xFF, 0xBD,
  1004. 0xFF, 0x5A, 0xFF, 0x5C, 0x7C, 0xFF, 0x84, 0xFF, 0xFF,
  1005. 0x8C, 0x03, 0xB2, 0x26, 0xFF}};
  1006. BigNumStr const* b[] = {&bnm0, &bnm1};
  1007. EXPECT_EQ(kEpidBadArgErr, EcMultiExp(this->efq, pts, b, 0, this->efq_r));
  1008. EXPECT_EQ(kEpidBadArgErr,
  1009. EcMultiExp(this->efq, pts, b, std::numeric_limits<size_t>::max(),
  1010. this->efq_r));
  1011. EXPECT_EQ(kEpidBadArgErr,
  1012. EcMultiExp(this->efq, pts, b, (size_t)INT_MAX + 1, this->efq_r));
  1013. }
  1014. TEST_F(EcGroupTest, MultiExpFailsGivenOutOfRangeExponent) {
  1015. EcPoint const* pts[] = {this->efq_a};
  1016. BigNumStr const* b_1[] = {&this->p};
  1017. // The exponent should be less than elliptic curve group order
  1018. EXPECT_EQ(kEpidBadArgErr, EcMultiExp(this->efq, pts, b_1, 1, this->efq_r));
  1019. }
  1020. TEST_F(EcGroupTest, MultiExpFailsGivenOutOfRangeExponents) {
  1021. EcPoint const* pts[] = {this->efq_a, this->efq_b};
  1022. const BigNumStr bnm_1 = {{0x11, 0xFF, 0xFF, 0xFF, 0x4F, 0x59, 0xB1, 0xD3,
  1023. 0x6B, 0x08, 0xFF, 0xFF, 0x0B, 0xF3, 0xAF, 0x27,
  1024. 0xFF, 0xB8, 0xFF, 0xFF, 0x98, 0xFF, 0xEB, 0xFF,
  1025. 0xF2, 0x6A, 0xFF, 0xFF, 0xEA, 0x31, 0xFF, 0xFF}};
  1026. BigNumStr const* b_1[] = {&bnm_1, &this->p};
  1027. BigNumStr const* b_2[] = {&this->p, &bnm_1};
  1028. // The exponent should be less than elliptic curve group order
  1029. EXPECT_EQ(kEpidBadArgErr, EcMultiExp(this->efq, pts, b_1, 2, this->efq_r));
  1030. EXPECT_EQ(kEpidBadArgErr, EcMultiExp(this->efq, pts, b_2, 2, this->efq_r));
  1031. }
  1032. TEST_F(EcGroupTest, MultiExpWorksGivenOneZeroExponent) {
  1033. G1ElemStr efq_r_str;
  1034. BigNumStr zero_bn_str = {0};
  1035. EcPoint const* pts[] = {this->efq_a};
  1036. BigNumStr const* b[] = {&zero_bn_str};
  1037. size_t m = 1;
  1038. EXPECT_EQ(kEpidNoErr, EcMultiExp(this->efq, pts, b, m, this->efq_r));
  1039. THROW_ON_EPIDERR(
  1040. WriteEcPoint(this->efq, this->efq_r, &efq_r_str, sizeof(efq_r_str)));
  1041. EXPECT_EQ(this->efq_identity_str, efq_r_str);
  1042. }
  1043. TEST_F(EcGroupTest, MultiExpWorksGivenTwoZeroExponent) {
  1044. G1ElemStr efq_r_str;
  1045. BigNumStr zero_bn_str = {0};
  1046. EcPoint const* pts[] = {this->efq_a, this->efq_a};
  1047. BigNumStr const* b[] = {&zero_bn_str, &zero_bn_str};
  1048. size_t m = 2;
  1049. EXPECT_EQ(kEpidNoErr, EcMultiExp(this->efq, pts, b, m, this->efq_r));
  1050. THROW_ON_EPIDERR(
  1051. WriteEcPoint(this->efq, this->efq_r, &efq_r_str, sizeof(efq_r_str)));
  1052. EXPECT_EQ(this->efq_identity_str, efq_r_str);
  1053. }
  1054. TEST_F(EcGroupTest, MultiExpWorksGivenSixZeroExponent) {
  1055. G1ElemStr efq_r_str;
  1056. BigNumStr zero_bn_str = {0};
  1057. EcPoint const* pts[] = {this->efq_a, this->efq_a, this->efq_a,
  1058. this->efq_a, this->efq_a, this->efq_a};
  1059. BigNumStr const* b[] = {&zero_bn_str, &zero_bn_str, &zero_bn_str,
  1060. &zero_bn_str, &zero_bn_str, &zero_bn_str};
  1061. size_t m = 6;
  1062. EXPECT_EQ(kEpidNoErr, EcMultiExp(this->efq, pts, b, m, this->efq_r));
  1063. THROW_ON_EPIDERR(
  1064. WriteEcPoint(this->efq, this->efq_r, &efq_r_str, sizeof(efq_r_str)));
  1065. EXPECT_EQ(this->efq_identity_str, efq_r_str);
  1066. }
  1067. TEST_F(EcGroupTest, MultiExpWorksGivenOneG2ZeroExponent) {
  1068. G2ElemStr efq2_r_str;
  1069. BigNumStr zero_bn_str = {0};
  1070. EcPoint const* pts[] = {this->efq2_a};
  1071. BigNumStr const* b[] = {&zero_bn_str};
  1072. size_t m = 1;
  1073. EXPECT_EQ(kEpidNoErr, EcMultiExp(this->efq2, pts, b, m, this->efq2_r));
  1074. THROW_ON_EPIDERR(
  1075. WriteEcPoint(this->efq2, this->efq2_r, &efq2_r_str, sizeof(efq2_r_str)));
  1076. EXPECT_EQ(this->efq2_identity_str, efq2_r_str);
  1077. }
  1078. TEST_F(EcGroupTest, MultiExpWorksGivenTwoG2ZeroExponent) {
  1079. G2ElemStr efq2_r_str;
  1080. BigNumStr zero_bn_str = {0};
  1081. EcPoint const* pts[] = {this->efq2_a, this->efq2_a};
  1082. BigNumStr const* b[] = {&zero_bn_str, &zero_bn_str};
  1083. size_t m = 2;
  1084. EXPECT_EQ(kEpidNoErr, EcMultiExp(this->efq2, pts, b, m, this->efq2_r));
  1085. THROW_ON_EPIDERR(
  1086. WriteEcPoint(this->efq2, this->efq2_r, &efq2_r_str, sizeof(efq2_r_str)));
  1087. EXPECT_EQ(this->efq2_identity_str, efq2_r_str);
  1088. }
  1089. TEST_F(EcGroupTest, MultiExpWorksGivenSixG2ZeroExponent) {
  1090. G2ElemStr efq2_r_str;
  1091. BigNumStr zero_bn_str = {0};
  1092. EcPoint const* pts[] = {this->efq2_a, this->efq2_a, this->efq2_a,
  1093. this->efq2_a, this->efq2_a, this->efq2_a};
  1094. BigNumStr const* b[] = {&zero_bn_str, &zero_bn_str, &zero_bn_str,
  1095. &zero_bn_str, &zero_bn_str, &zero_bn_str};
  1096. size_t m = 6;
  1097. EXPECT_EQ(kEpidNoErr, EcMultiExp(this->efq2, pts, b, m, this->efq2_r));
  1098. THROW_ON_EPIDERR(
  1099. WriteEcPoint(this->efq2, this->efq2_r, &efq2_r_str, sizeof(efq2_r_str)));
  1100. EXPECT_EQ(this->efq2_identity_str, efq2_r_str);
  1101. }
  1102. TEST_F(EcGroupTest, MultiExpWorksGivenOneExponent) {
  1103. G1ElemStr efq_r_str;
  1104. EcPoint const* pts[] = {this->efq_a};
  1105. BigNumStr const* b[] = {&this->x_str};
  1106. size_t m = 1;
  1107. EXPECT_EQ(kEpidNoErr, EcMultiExp(this->efq, pts, b, m, this->efq_r));
  1108. THROW_ON_EPIDERR(
  1109. WriteEcPoint(this->efq, this->efq_r, &efq_r_str, sizeof(efq_r_str)));
  1110. EXPECT_EQ(this->efq_exp_ax_str, efq_r_str);
  1111. }
  1112. TEST_F(EcGroupTest, MultiExpWorksGivenTwoExponents) {
  1113. G1ElemStr efq_r_str;
  1114. EcPoint const* pts[] = {this->efq_a, this->efq_b};
  1115. BigNumStr const* b[] = {&this->x_str, &this->y_str};
  1116. size_t m = 2;
  1117. EXPECT_EQ(kEpidNoErr, EcMultiExp(this->efq, pts, b, m, this->efq_r));
  1118. THROW_ON_EPIDERR(
  1119. WriteEcPoint(this->efq, this->efq_r, &efq_r_str, sizeof(efq_r_str)));
  1120. EXPECT_EQ(this->efq_multiexp_abxy_str, efq_r_str);
  1121. }
  1122. TEST_F(EcGroupTest, MultiExpWorksGivenOneG2Exponent) {
  1123. G2ElemStr efq2_r_str;
  1124. EcPoint const* pts[] = {this->efq2_a};
  1125. BigNumStr const* b[] = {&this->x_str};
  1126. size_t m = 1;
  1127. EXPECT_EQ(kEpidNoErr, EcMultiExp(this->efq2, pts, b, m, this->efq2_r));
  1128. THROW_ON_EPIDERR(
  1129. WriteEcPoint(this->efq2, this->efq2_r, &efq2_r_str, sizeof(efq2_r_str)));
  1130. EXPECT_EQ(this->efq2_exp_ax_str, efq2_r_str);
  1131. }
  1132. TEST_F(EcGroupTest, MultiExpWorksGivenTwoG2Exponents) {
  1133. G2ElemStr efq2_r_str;
  1134. EcPoint const* pts[] = {this->efq2_a, this->efq2_b};
  1135. BigNumStr const* b[] = {&this->x_str, &this->y_str};
  1136. size_t m = 2;
  1137. EXPECT_EQ(kEpidNoErr, EcMultiExp(this->efq2, pts, b, m, this->efq2_r));
  1138. THROW_ON_EPIDERR(
  1139. WriteEcPoint(this->efq2, this->efq2_r, &efq2_r_str, sizeof(efq2_r_str)));
  1140. EXPECT_EQ(this->efq2_multiexp_abxy_str, efq2_r_str);
  1141. }
  1142. ///////////////////////////////////////////////////////////////////////
  1143. // EcMultiExpBn
  1144. TEST_F(EcGroupTest, MultiExpBnFailsGivenArgumentsMismatch) {
  1145. EcPoint const* pts_ec1[] = {this->efq_a, this->efq_b};
  1146. EcPoint const* pts_ec2[] = {this->efq2_a, this->efq2_b};
  1147. EcPoint const* pts_ec1_ec2[] = {this->efq_a, this->efq2_b};
  1148. const BigNumStr bnm0 = {{0x11, 0xFF, 0xFF, 0xFF, 0x4F, 0x59, 0xB1, 0xD3, 0x6B,
  1149. 0x08, 0xFF, 0xFF, 0x0B, 0xF3, 0xAF, 0x27, 0xFF, 0xB8,
  1150. 0xFF, 0xFF, 0x98, 0xFF, 0xEB, 0xFF, 0xF2, 0x6A, 0xFF,
  1151. 0xFF, 0xEA, 0x31, 0xFF, 0xFF}};
  1152. const BigNumStr bnm1 = {{0xE2, 0xFF, 0x03, 0x1D, 0xFF, 0x19, 0x81, 0xCB, 0xFF,
  1153. 0xFF, 0x6B, 0xD5, 0x3E, 0xFF, 0xFF, 0xFF, 0xFF, 0xBD,
  1154. 0xFF, 0x5A, 0xFF, 0x5C, 0x7C, 0xFF, 0x84, 0xFF, 0xFF,
  1155. 0x8C, 0x03, 0xB2, 0x26, 0xFF}};
  1156. BigNumObj bno0(bnm0);
  1157. BigNumObj bno1(bnm1);
  1158. BigNum const* b[] = {bno0, bno1};
  1159. size_t m = 2;
  1160. EXPECT_EQ(kEpidBadArgErr,
  1161. EcMultiExpBn(this->efq2, pts_ec1, b, m, this->efq_r));
  1162. EXPECT_EQ(kEpidBadArgErr,
  1163. EcMultiExpBn(this->efq, pts_ec2, b, m, this->efq_r));
  1164. EXPECT_EQ(kEpidBadArgErr,
  1165. EcMultiExpBn(this->efq, pts_ec1, b, m, this->efq2_r));
  1166. EXPECT_EQ(kEpidBadArgErr,
  1167. EcMultiExpBn(this->efq, pts_ec1_ec2, b, m, this->efq_r));
  1168. }
  1169. TEST_F(EcGroupTest, MultiExpBnFailsGivenNullPointer) {
  1170. EcPoint const* pts[] = {this->efq_a, this->efq_b};
  1171. EcPoint const* pts_withnull[] = {nullptr, this->efq_b};
  1172. const BigNumStr bnm0 = {{0x11, 0xFF, 0xFF, 0xFF, 0x4F, 0x59, 0xB1, 0xD3, 0x6B,
  1173. 0x08, 0xFF, 0xFF, 0x0B, 0xF3, 0xAF, 0x27, 0xFF, 0xB8,
  1174. 0xFF, 0xFF, 0x98, 0xFF, 0xEB, 0xFF, 0xF2, 0x6A, 0xFF,
  1175. 0xFF, 0xEA, 0x31, 0xFF, 0xFF}};
  1176. const BigNumStr bnm1 = {{0xE2, 0xFF, 0x03, 0x1D, 0xFF, 0x19, 0x81, 0xCB, 0xFF,
  1177. 0xFF, 0x6B, 0xD5, 0x3E, 0xFF, 0xFF, 0xFF, 0xFF, 0xBD,
  1178. 0xFF, 0x5A, 0xFF, 0x5C, 0x7C, 0xFF, 0x84, 0xFF, 0xFF,
  1179. 0x8C, 0x03, 0xB2, 0x26, 0xFF}};
  1180. BigNumObj bno0(bnm0);
  1181. BigNumObj bno1(bnm1);
  1182. BigNum const* b[] = {bno0, bno1};
  1183. BigNum const* b_withnull[] = {nullptr, bno1};
  1184. size_t m = 2;
  1185. EXPECT_EQ(kEpidBadArgErr, EcMultiExpBn(nullptr, pts, b, m, this->efq_r));
  1186. EXPECT_EQ(kEpidBadArgErr,
  1187. EcMultiExpBn(this->efq, nullptr, b, m, this->efq_r));
  1188. EXPECT_EQ(kEpidBadArgErr,
  1189. EcMultiExpBn(this->efq, pts, nullptr, m, this->efq_r));
  1190. EXPECT_EQ(kEpidBadArgErr, EcMultiExpBn(this->efq, pts, b, m, nullptr));
  1191. EXPECT_EQ(kEpidBadArgErr,
  1192. EcMultiExpBn(this->efq, pts_withnull, b, m, this->efq_r));
  1193. EXPECT_EQ(kEpidBadArgErr,
  1194. EcMultiExpBn(this->efq, pts, b_withnull, m, this->efq_r));
  1195. }
  1196. TEST_F(EcGroupTest, MultiExpBnFailsGivenIncorrectMLen) {
  1197. EcPoint const* pts[] = {this->efq_a, this->efq_b};
  1198. const BigNumStr bnm0 = {{0x11, 0xFF, 0xFF, 0xFF, 0x4F, 0x59, 0xB1, 0xD3, 0x6B,
  1199. 0x08, 0xFF, 0xFF, 0x0B, 0xF3, 0xAF, 0x27, 0xFF, 0xB8,
  1200. 0xFF, 0xFF, 0x98, 0xFF, 0xEB, 0xFF, 0xF2, 0x6A, 0xFF,
  1201. 0xFF, 0xEA, 0x31, 0xFF, 0xFF}};
  1202. const BigNumStr bnm1 = {{0xE2, 0xFF, 0x03, 0x1D, 0xFF, 0x19, 0x81, 0xCB, 0xFF,
  1203. 0xFF, 0x6B, 0xD5, 0x3E, 0xFF, 0xFF, 0xFF, 0xFF, 0xBD,
  1204. 0xFF, 0x5A, 0xFF, 0x5C, 0x7C, 0xFF, 0x84, 0xFF, 0xFF,
  1205. 0x8C, 0x03, 0xB2, 0x26, 0xFF}};
  1206. BigNumObj bno0(bnm0);
  1207. BigNumObj bno1(bnm1);
  1208. BigNum const* b[] = {bno0, bno1};
  1209. EXPECT_EQ(kEpidBadArgErr, EcMultiExpBn(this->efq, pts, b, 0, this->efq_r));
  1210. EXPECT_EQ(kEpidBadArgErr,
  1211. EcMultiExpBn(this->efq, pts, b, std::numeric_limits<size_t>::max(),
  1212. this->efq_r));
  1213. EXPECT_EQ(kEpidBadArgErr,
  1214. EcMultiExpBn(this->efq, pts, b, (size_t)INT_MAX + 1, this->efq_r));
  1215. }
  1216. TEST_F(EcGroupTest, MultiExpBnFailsGivenOutOfRangeExponent) {
  1217. EcPoint const* pt[] = {this->efq_a};
  1218. BigNumObj bno_p(this->p);
  1219. BigNum const* b[] = {bno_p};
  1220. EcPoint const* pts[] = {this->efq_a, this->efq_b};
  1221. const BigNumStr bnm_1 = {{0x11, 0xFF, 0xFF, 0xFF, 0x4F, 0x59, 0xB1, 0xD3,
  1222. 0x6B, 0x08, 0xFF, 0xFF, 0x0B, 0xF3, 0xAF, 0x27,
  1223. 0xFF, 0xB8, 0xFF, 0xFF, 0x98, 0xFF, 0xEB, 0xFF,
  1224. 0xF2, 0x6A, 0xFF, 0xFF, 0xEA, 0x31, 0xFF, 0xFF}};
  1225. BigNumObj bno_1(bnm_1);
  1226. BigNum const* b_1[] = {bno_1, bno_p};
  1227. BigNum const* b_2[] = {bno_p, bno_1};
  1228. EXPECT_EQ(kEpidBadArgErr, EcMultiExpBn(this->efq, pt, b, 1, this->efq_r));
  1229. EXPECT_EQ(kEpidBadArgErr, EcMultiExpBn(this->efq, pts, b_1, 2, this->efq_r));
  1230. EXPECT_EQ(kEpidBadArgErr, EcMultiExpBn(this->efq, pts, b_2, 2, this->efq_r));
  1231. }
  1232. TEST_F(EcGroupTest, MultiExpBnWorksGivenOneZeroExponent) {
  1233. G1ElemStr efq_r_str;
  1234. BigNumStr zero_bn_str = {0};
  1235. EcPoint const* pts[] = {this->efq_a};
  1236. BigNumObj bno_zero(zero_bn_str);
  1237. BigNum const* b[] = {bno_zero};
  1238. size_t m = 1;
  1239. EXPECT_EQ(kEpidNoErr, EcMultiExpBn(this->efq, pts, b, m, this->efq_r));
  1240. THROW_ON_EPIDERR(
  1241. WriteEcPoint(this->efq, this->efq_r, &efq_r_str, sizeof(efq_r_str)));
  1242. EXPECT_EQ(this->efq_identity_str, efq_r_str);
  1243. }
  1244. TEST_F(EcGroupTest, MultiExpBnWorksGivenTwoZeroExponents) {
  1245. G1ElemStr efq_r_str;
  1246. BigNumStr zero_bn_str = {0};
  1247. EcPoint const* pts[] = {this->efq_a, this->efq_a};
  1248. BigNumObj bno_zero0(zero_bn_str);
  1249. BigNumObj bno_zero1(zero_bn_str);
  1250. BigNum const* b[] = {bno_zero0, bno_zero1};
  1251. size_t m = 2;
  1252. EXPECT_EQ(kEpidNoErr, EcMultiExpBn(this->efq, pts, b, m, this->efq_r));
  1253. THROW_ON_EPIDERR(
  1254. WriteEcPoint(this->efq, this->efq_r, &efq_r_str, sizeof(efq_r_str)));
  1255. EXPECT_EQ(this->efq_identity_str, efq_r_str);
  1256. }
  1257. TEST_F(EcGroupTest, MultiExpBnWorksGivenSixZeroExponents) {
  1258. G1ElemStr efq_r_str;
  1259. BigNumStr zero_bn_str = {0};
  1260. EcPoint const* pts[] = {this->efq_a, this->efq_a, this->efq_a,
  1261. this->efq_a, this->efq_a, this->efq_a};
  1262. BigNumObj bno_zero0(zero_bn_str);
  1263. BigNumObj bno_zero1(zero_bn_str);
  1264. BigNumObj bno_zero2(zero_bn_str);
  1265. BigNumObj bno_zero3(zero_bn_str);
  1266. BigNumObj bno_zero4(zero_bn_str);
  1267. BigNumObj bno_zero5(zero_bn_str);
  1268. BigNum const* b[] = {bno_zero0, bno_zero1, bno_zero2,
  1269. bno_zero3, bno_zero4, bno_zero5};
  1270. size_t m = 6;
  1271. EXPECT_EQ(kEpidNoErr, EcMultiExpBn(this->efq, pts, b, m, this->efq_r));
  1272. THROW_ON_EPIDERR(
  1273. WriteEcPoint(this->efq, this->efq_r, &efq_r_str, sizeof(efq_r_str)));
  1274. EXPECT_EQ(this->efq_identity_str, efq_r_str);
  1275. }
  1276. TEST_F(EcGroupTest, MultiExpBnWorksGivenOneG2ZeroExponent) {
  1277. G2ElemStr efq2_r_str;
  1278. BigNumStr zero_bn_str = {0};
  1279. EcPoint const* pts[] = {this->efq2_a};
  1280. BigNumObj bno_zero(zero_bn_str);
  1281. BigNum const* b[] = {bno_zero};
  1282. size_t m = 1;
  1283. EXPECT_EQ(kEpidNoErr, EcMultiExpBn(this->efq2, pts, b, m, this->efq2_r));
  1284. THROW_ON_EPIDERR(
  1285. WriteEcPoint(this->efq2, this->efq2_r, &efq2_r_str, sizeof(efq2_r_str)));
  1286. EXPECT_EQ(this->efq2_identity_str, efq2_r_str);
  1287. }
  1288. TEST_F(EcGroupTest, MultiExpBnWorksGivenTwoG2ZeroExponents) {
  1289. G2ElemStr efq2_r_str;
  1290. BigNumStr zero_bn_str = {0};
  1291. EcPoint const* pts[] = {this->efq2_a, this->efq2_a};
  1292. BigNumObj bno_zero0(zero_bn_str);
  1293. BigNumObj bno_zero1(zero_bn_str);
  1294. BigNum const* b[] = {bno_zero0, bno_zero1};
  1295. size_t m = 2;
  1296. EXPECT_EQ(kEpidNoErr, EcMultiExpBn(this->efq2, pts, b, m, this->efq2_r));
  1297. THROW_ON_EPIDERR(
  1298. WriteEcPoint(this->efq2, this->efq2_r, &efq2_r_str, sizeof(efq2_r_str)));
  1299. EXPECT_EQ(this->efq2_identity_str, efq2_r_str);
  1300. }
  1301. TEST_F(EcGroupTest, MultiExpBnWorksGivenSixG2ZeroExponents) {
  1302. G2ElemStr efq2_r_str;
  1303. BigNumStr zero_bn_str = {0};
  1304. BigNumObj bno_zero0(zero_bn_str);
  1305. BigNumObj bno_zero1(zero_bn_str);
  1306. BigNumObj bno_zero2(zero_bn_str);
  1307. BigNumObj bno_zero3(zero_bn_str);
  1308. BigNumObj bno_zero4(zero_bn_str);
  1309. BigNumObj bno_zero5(zero_bn_str);
  1310. EcPoint const* pts[] = {this->efq2_a, this->efq2_a, this->efq2_a,
  1311. this->efq2_a, this->efq2_a, this->efq2_a};
  1312. BigNum const* b[] = {bno_zero0, bno_zero1, bno_zero2,
  1313. bno_zero3, bno_zero4, bno_zero5};
  1314. size_t m = 6;
  1315. EXPECT_EQ(kEpidNoErr, EcMultiExpBn(this->efq2, pts, b, m, this->efq2_r));
  1316. THROW_ON_EPIDERR(
  1317. WriteEcPoint(this->efq2, this->efq2_r, &efq2_r_str, sizeof(efq2_r_str)));
  1318. EXPECT_EQ(this->efq2_identity_str, efq2_r_str);
  1319. }
  1320. TEST_F(EcGroupTest, MultiExpBnWorksGivenOneExponent) {
  1321. G1ElemStr efq_r_str;
  1322. EcPoint const* pts[] = {this->efq_a};
  1323. BigNumObj bno_x(this->x_str);
  1324. BigNum const* b[] = {bno_x};
  1325. size_t m = 1;
  1326. EXPECT_EQ(kEpidNoErr, EcMultiExpBn(this->efq, pts, b, m, this->efq_r));
  1327. THROW_ON_EPIDERR(
  1328. WriteEcPoint(this->efq, this->efq_r, &efq_r_str, sizeof(efq_r_str)));
  1329. EXPECT_EQ(this->efq_exp_ax_str, efq_r_str);
  1330. }
  1331. TEST_F(EcGroupTest, MultiExpBnWorksGivenTwoExponents) {
  1332. G1ElemStr efq_r_str;
  1333. EcPoint const* pts[] = {this->efq_a, this->efq_b};
  1334. BigNumObj bno_x(this->x_str);
  1335. BigNumObj bno_y(this->y_str);
  1336. BigNum const* b[] = {bno_x, bno_y};
  1337. size_t m = 2;
  1338. EXPECT_EQ(kEpidNoErr, EcMultiExpBn(this->efq, pts, b, m, this->efq_r));
  1339. THROW_ON_EPIDERR(
  1340. WriteEcPoint(this->efq, this->efq_r, &efq_r_str, sizeof(efq_r_str)));
  1341. EXPECT_EQ(this->efq_multiexp_abxy_str, efq_r_str);
  1342. }
  1343. TEST_F(EcGroupTest, MultiExpBnWorksGivenOneG2Exponent) {
  1344. G2ElemStr efq2_r_str;
  1345. EcPoint const* pts[] = {this->efq2_a};
  1346. BigNumObj bno_x(this->x_str);
  1347. BigNum const* b[] = {bno_x};
  1348. size_t m = 1;
  1349. EXPECT_EQ(kEpidNoErr, EcMultiExpBn(this->efq2, pts, b, m, this->efq2_r));
  1350. THROW_ON_EPIDERR(
  1351. WriteEcPoint(this->efq2, this->efq2_r, &efq2_r_str, sizeof(efq2_r_str)));
  1352. EXPECT_EQ(this->efq2_exp_ax_str, efq2_r_str);
  1353. }
  1354. TEST_F(EcGroupTest, MultiExpBnWorksGivenTwoG2Exponents) {
  1355. G2ElemStr efq2_r_str;
  1356. EcPoint const* pts[] = {this->efq2_a, this->efq2_b};
  1357. BigNumObj bno_x(this->x_str);
  1358. BigNumObj bno_y(this->y_str);
  1359. BigNum const* b[] = {bno_x, bno_y};
  1360. size_t m = 2;
  1361. EXPECT_EQ(kEpidNoErr, EcMultiExpBn(this->efq2, pts, b, m, this->efq2_r));
  1362. THROW_ON_EPIDERR(
  1363. WriteEcPoint(this->efq2, this->efq2_r, &efq2_r_str, sizeof(efq2_r_str)));
  1364. EXPECT_EQ(this->efq2_multiexp_abxy_str, efq2_r_str);
  1365. }
  1366. TEST_F(EcGroupTest, MultiExpBnWorksGivenTwoDifferentSizeG3Exponents) {
  1367. const G1ElemStr g3_b_str = {
  1368. {{{
  1369. 0x09, 0x0d, 0x6f, 0x82, 0x77, 0x88, 0x49, 0x53, 0xba, 0x1e, 0x1b,
  1370. 0x0e, 0x5e, 0xae, 0xc0, 0x27, 0xad, 0xe3, 0xb1, 0x09, 0x4f, 0xcd,
  1371. 0xb6, 0xe6, 0x6f, 0x7f, 0xa3, 0x1a, 0x1e, 0xfb, 0x52, 0x72,
  1372. }}},
  1373. {{{
  1374. 0xfa, 0x85, 0x0f, 0x5c, 0x97, 0x61, 0xbf, 0x46, 0x7e, 0xec, 0xd6,
  1375. 0x64, 0xda, 0xa9, 0x8e, 0xf5, 0xd3, 0xdf, 0xfa, 0x13, 0x5a, 0xb2,
  1376. 0x3e, 0xeb, 0x0a, 0x9d, 0x02, 0xc0, 0x33, 0xec, 0x2a, 0x70,
  1377. }}}};
  1378. const G1ElemStr g3_k_str = {
  1379. {{{
  1380. 0x41, 0xb7, 0xa4, 0xc8, 0x43, 0x3f, 0x0b, 0xc2, 0x80, 0x31, 0xbe,
  1381. 0x75, 0x65, 0xe9, 0xbb, 0x81, 0x73, 0x5b, 0x91, 0x4f, 0x3f, 0xd7,
  1382. 0xbe, 0xb5, 0x19, 0x56, 0x3f, 0x18, 0x95, 0xea, 0xc1, 0xd7,
  1383. }}},
  1384. {{{
  1385. 0xa4, 0x5e, 0xb9, 0x86, 0xfc, 0xe5, 0xc4, 0x0f, 0x54, 0x37, 0xab,
  1386. 0xed, 0x59, 0x20, 0xce, 0x67, 0x68, 0x3c, 0x25, 0x4d, 0xbc, 0x5f,
  1387. 0x6a, 0x4d, 0x5a, 0xa7, 0x93, 0xce, 0x90, 0x2d, 0x3e, 0x5a,
  1388. }}}};
  1389. EcPointObj B(&this->epid11_G3, g3_b_str);
  1390. EcPointObj K(&this->epid11_G3, g3_k_str);
  1391. EcPoint const* pts[] = {B, K};
  1392. const std::vector<uint8_t> bnm_sf_str = {
  1393. 0x00, 0x3c, 0xc1, 0x73, 0x35, 0x3c, 0x99, 0x61, 0xb0, 0x80, 0x9a,
  1394. 0x0e, 0x8d, 0xbf, 0x5d, 0x0b, 0xa9, 0x18, 0x2b, 0x36, 0x3c, 0x06,
  1395. 0xbc, 0x1c, 0xc7, 0x9f, 0x76, 0xba, 0x5a, 0x26, 0xcd, 0x5e, 0x24,
  1396. 0xb9, 0x68, 0xde, 0x47, 0x72, 0xf9, 0xf9, 0x1e, 0xaa, 0x74, 0x17,
  1397. 0x31, 0xe4, 0x66, 0x59, 0x69, 0xe5, 0x9e, 0x27, 0x1d, 0x57, 0xe5,
  1398. 0x39, 0x57, 0xd4, 0xc5, 0x78, 0xf2, 0x77, 0x5c, 0x9f, 0x6c, 0xfe,
  1399. 0x12, 0x00, 0xa8, 0xe0, 0xd3, 0x81, 0x38, 0xaa, 0x5a};
  1400. const BigNumStr bnm_nc_tick_str = {{{
  1401. 0xcd, 0x2e, 0xe8, 0xf4, 0x85, 0x95, 0x04, 0x09, 0xbd, 0xa4, 0xfa, 0x07,
  1402. 0xe3, 0x1c, 0xb9, 0x5a, 0x82, 0x73, 0xa6, 0xea, 0x47, 0x5c, 0x31, 0x74,
  1403. 0x3c, 0x0a, 0xeb, 0x62, 0x94, 0x2f, 0x7b, 0x10,
  1404. }}};
  1405. BigNumObj bno_sf(bnm_sf_str);
  1406. // In order to callculate exp sf data should be devided by group order
  1407. THROW_ON_EPIDERR(BigNumMod(bno_sf, epid11_p_tick, bno_sf));
  1408. BigNumObj bno_nc_tick(bnm_nc_tick_str);
  1409. BigNum const* b[] = {bno_sf, bno_nc_tick};
  1410. EcPointObj R3 = EcPointObj(&this->epid11_G3);
  1411. const std::vector<uint8_t> expected_r_str = {
  1412. // X
  1413. 0x1E, 0xDF, 0x9E, 0xA5, 0xF5, 0xED, 0xB3, 0x3F, 0xCC, 0x83, 0x10, 0x5E,
  1414. 0x3E, 0xB7, 0xE5, 0x06, 0x5F, 0x19, 0xF9, 0xFD, 0xE9, 0x57, 0x0B, 0x31,
  1415. 0xC8, 0xDA, 0x0A, 0x7B, 0xCD, 0xB5, 0xAA, 0x2E,
  1416. // Y
  1417. 0x6A, 0x6B, 0x5A, 0x8D, 0x48, 0x5F, 0x2F, 0x72, 0x77, 0x93, 0xD6, 0xD0,
  1418. 0x49, 0xE1, 0x84, 0x35, 0x98, 0xF1, 0xDE, 0x71, 0xC5, 0xF4, 0x40, 0xFB,
  1419. 0x1C, 0x75, 0x83, 0xD7, 0x4F, 0x58, 0x0A, 0x8D};
  1420. std::vector<uint8_t> g3_r_str;
  1421. g3_r_str.resize(expected_r_str.size(), 0);
  1422. size_t m = 2;
  1423. EXPECT_EQ(kEpidNoErr, EcMultiExpBn(this->epid11_G3, pts, b, m, R3));
  1424. THROW_ON_EPIDERR(
  1425. WriteEcPoint(this->epid11_G3, R3, g3_r_str.data(), g3_r_str.size()));
  1426. EXPECT_EQ(g3_r_str, expected_r_str);
  1427. }
  1428. ///////////////////////////////////////////////////////////////////////
  1429. // EcSscmMultiExp
  1430. TEST_F(EcGroupTest, SscmMultiExpFailsGivenArgumentsMismatch) {
  1431. EcPoint const* pts_ec1[] = {this->efq_a, this->efq_b};
  1432. EcPoint const* pts_ec2[] = {this->efq2_a, this->efq2_b};
  1433. EcPoint const* pts_ec1_ec2[] = {this->efq_a, this->efq2_b};
  1434. const BigNumStr bnm0 = {{0x11, 0xFF, 0xFF, 0xFF, 0x4F, 0x59, 0xB1, 0xD3, 0x6B,
  1435. 0x08, 0xFF, 0xFF, 0x0B, 0xF3, 0xAF, 0x27, 0xFF, 0xB8,
  1436. 0xFF, 0xFF, 0x98, 0xFF, 0xEB, 0xFF, 0xF2, 0x6A, 0xFF,
  1437. 0xFF, 0xEA, 0x31, 0xFF, 0xFF}};
  1438. const BigNumStr bnm1 = {{0xE2, 0xFF, 0x03, 0x1D, 0xFF, 0x19, 0x81, 0xCB, 0xFF,
  1439. 0xFF, 0x6B, 0xD5, 0x3E, 0xFF, 0xFF, 0xFF, 0xFF, 0xBD,
  1440. 0xFF, 0x5A, 0xFF, 0x5C, 0x7C, 0xFF, 0x84, 0xFF, 0xFF,
  1441. 0x8C, 0x03, 0xB2, 0x26, 0xFF}};
  1442. BigNumStr const* b[] = {&bnm0, &bnm1};
  1443. size_t m = 2;
  1444. EXPECT_EQ(kEpidBadArgErr,
  1445. EcSscmMultiExp(this->efq2, pts_ec1, b, m, this->efq_r));
  1446. EXPECT_EQ(kEpidBadArgErr,
  1447. EcSscmMultiExp(this->efq, pts_ec2, b, m, this->efq_r));
  1448. EXPECT_EQ(kEpidBadArgErr,
  1449. EcSscmMultiExp(this->efq, pts_ec1, b, m, this->efq2_r));
  1450. EXPECT_EQ(kEpidBadArgErr,
  1451. EcSscmMultiExp(this->efq, pts_ec1_ec2, b, m, this->efq_r));
  1452. }
  1453. TEST_F(EcGroupTest, SscmMultiExpFailsGivenNullPointer) {
  1454. EcPoint const* pts[] = {this->efq_a, this->efq_b};
  1455. EcPoint const* pts_withnull[] = {nullptr, this->efq_b};
  1456. const BigNumStr bnm0 = {{0x11, 0xFF, 0xFF, 0xFF, 0x4F, 0x59, 0xB1, 0xD3, 0x6B,
  1457. 0x08, 0xFF, 0xFF, 0x0B, 0xF3, 0xAF, 0x27, 0xFF, 0xB8,
  1458. 0xFF, 0xFF, 0x98, 0xFF, 0xEB, 0xFF, 0xF2, 0x6A, 0xFF,
  1459. 0xFF, 0xEA, 0x31, 0xFF, 0xFF}};
  1460. const BigNumStr bnm1 = {{0xE2, 0xFF, 0x03, 0x1D, 0xFF, 0x19, 0x81, 0xCB, 0xFF,
  1461. 0xFF, 0x6B, 0xD5, 0x3E, 0xFF, 0xFF, 0xFF, 0xFF, 0xBD,
  1462. 0xFF, 0x5A, 0xFF, 0x5C, 0x7C, 0xFF, 0x84, 0xFF, 0xFF,
  1463. 0x8C, 0x03, 0xB2, 0x26, 0xFF}};
  1464. BigNumStr const* b[] = {&bnm0, &bnm1};
  1465. BigNumStr const* b_withnull[] = {nullptr, &bnm1};
  1466. size_t m = 2;
  1467. EXPECT_EQ(kEpidBadArgErr, EcSscmMultiExp(nullptr, pts, b, m, this->efq_r));
  1468. EXPECT_EQ(kEpidBadArgErr,
  1469. EcSscmMultiExp(this->efq, nullptr, b, m, this->efq_r));
  1470. EXPECT_EQ(kEpidBadArgErr,
  1471. EcSscmMultiExp(this->efq, pts, nullptr, m, this->efq_r));
  1472. EXPECT_EQ(kEpidBadArgErr, EcSscmMultiExp(this->efq, pts, b, m, nullptr));
  1473. EXPECT_EQ(kEpidBadArgErr,
  1474. EcSscmMultiExp(this->efq, pts_withnull, b, m, this->efq_r));
  1475. EXPECT_EQ(kEpidBadArgErr,
  1476. EcSscmMultiExp(this->efq, pts, b_withnull, m, this->efq_r));
  1477. }
  1478. TEST_F(EcGroupTest, SscmMultiExpFailsGivenIncorrectMLen) {
  1479. EcPoint const* pts[] = {this->efq_a, this->efq_b};
  1480. const BigNumStr bnm0 = {{0x11, 0xFF, 0xFF, 0xFF, 0x4F, 0x59, 0xB1, 0xD3, 0x6B,
  1481. 0x08, 0xFF, 0xFF, 0x0B, 0xF3, 0xAF, 0x27, 0xFF, 0xB8,
  1482. 0xFF, 0xFF, 0x98, 0xFF, 0xEB, 0xFF, 0xF2, 0x6A, 0xFF,
  1483. 0xFF, 0xEA, 0x31, 0xFF, 0xFF}};
  1484. const BigNumStr bnm1 = {{0xE2, 0xFF, 0x03, 0x1D, 0xFF, 0x19, 0x81, 0xCB, 0xFF,
  1485. 0xFF, 0x6B, 0xD5, 0x3E, 0xFF, 0xFF, 0xFF, 0xFF, 0xBD,
  1486. 0xFF, 0x5A, 0xFF, 0x5C, 0x7C, 0xFF, 0x84, 0xFF, 0xFF,
  1487. 0x8C, 0x03, 0xB2, 0x26, 0xFF}};
  1488. BigNumStr const* b[] = {&bnm0, &bnm1};
  1489. EXPECT_EQ(kEpidBadArgErr, EcSscmMultiExp(this->efq, pts, b, 0, this->efq_r));
  1490. EXPECT_EQ(kEpidBadArgErr,
  1491. EcSscmMultiExp(this->efq, pts, b,
  1492. std::numeric_limits<size_t>::max(), this->efq_r));
  1493. EXPECT_EQ(kEpidBadArgErr, EcSscmMultiExp(this->efq, pts, b,
  1494. (size_t)INT_MAX + 1, this->efq_r));
  1495. }
  1496. TEST_F(EcGroupTest, SscmMultiExpFailsGivenOutOfRangeExponent) {
  1497. EcPoint const* pts[] = {this->efq_a};
  1498. BigNumStr const* b_1[] = {&this->p};
  1499. // The exponent should be less than elliptic curve group order
  1500. EXPECT_EQ(kEpidBadArgErr,
  1501. EcSscmMultiExp(this->efq, pts, b_1, 1, this->efq_r));
  1502. }
  1503. TEST_F(EcGroupTest, SscmMultiExpFailsGivenOutOfRangeExponents) {
  1504. EcPoint const* pts[] = {this->efq_a, this->efq_b};
  1505. const BigNumStr bnm_1 = {{0x11, 0xFF, 0xFF, 0xFF, 0x4F, 0x59, 0xB1, 0xD3,
  1506. 0x6B, 0x08, 0xFF, 0xFF, 0x0B, 0xF3, 0xAF, 0x27,
  1507. 0xFF, 0xB8, 0xFF, 0xFF, 0x98, 0xFF, 0xEB, 0xFF,
  1508. 0xF2, 0x6A, 0xFF, 0xFF, 0xEA, 0x31, 0xFF, 0xFF}};
  1509. BigNumStr const* b_1[] = {&bnm_1, &this->p};
  1510. BigNumStr const* b_2[] = {&this->p, &bnm_1};
  1511. // The exponent should be less than elliptic curve group order
  1512. EXPECT_EQ(kEpidBadArgErr,
  1513. EcSscmMultiExp(this->efq, pts, b_1, 2, this->efq_r));
  1514. EXPECT_EQ(kEpidBadArgErr,
  1515. EcSscmMultiExp(this->efq, pts, b_2, 2, this->efq_r));
  1516. }
  1517. TEST_F(EcGroupTest, SscmMultiExpWorksGivenOneZeroExponent) {
  1518. G1ElemStr efq_r_str;
  1519. BigNumStr zero_bn_str = {0};
  1520. EcPoint const* pts[] = {this->efq_a};
  1521. BigNumStr const* b[] = {&zero_bn_str};
  1522. size_t m = 1;
  1523. EXPECT_EQ(kEpidNoErr, EcSscmMultiExp(this->efq, pts, b, m, this->efq_r));
  1524. THROW_ON_EPIDERR(
  1525. WriteEcPoint(this->efq, this->efq_r, &efq_r_str, sizeof(efq_r_str)));
  1526. EXPECT_EQ(this->efq_identity_str, efq_r_str);
  1527. }
  1528. TEST_F(EcGroupTest, SscmMultiExpWorksGivenTwoZeroExponent) {
  1529. G1ElemStr efq_r_str;
  1530. BigNumStr zero_bn_str = {0};
  1531. EcPoint const* pts[] = {this->efq_a, this->efq_a};
  1532. BigNumStr const* b[] = {&zero_bn_str, &zero_bn_str};
  1533. size_t m = 2;
  1534. EXPECT_EQ(kEpidNoErr, EcSscmMultiExp(this->efq, pts, b, m, this->efq_r));
  1535. THROW_ON_EPIDERR(
  1536. WriteEcPoint(this->efq, this->efq_r, &efq_r_str, sizeof(efq_r_str)));
  1537. EXPECT_EQ(this->efq_identity_str, efq_r_str);
  1538. }
  1539. TEST_F(EcGroupTest, SscmMultiExpWorksGivenSixZeroExponent) {
  1540. G1ElemStr efq_r_str;
  1541. BigNumStr zero_bn_str = {0};
  1542. EcPoint const* pts[] = {this->efq_a, this->efq_a, this->efq_a,
  1543. this->efq_a, this->efq_a, this->efq_a};
  1544. BigNumStr const* b[] = {&zero_bn_str, &zero_bn_str, &zero_bn_str,
  1545. &zero_bn_str, &zero_bn_str, &zero_bn_str};
  1546. size_t m = 6;
  1547. EXPECT_EQ(kEpidNoErr, EcSscmMultiExp(this->efq, pts, b, m, this->efq_r));
  1548. THROW_ON_EPIDERR(
  1549. WriteEcPoint(this->efq, this->efq_r, &efq_r_str, sizeof(efq_r_str)));
  1550. EXPECT_EQ(this->efq_identity_str, efq_r_str);
  1551. }
  1552. TEST_F(EcGroupTest, SscmMultiExpWorksGivenOneG2ZeroExponent) {
  1553. G2ElemStr efq2_r_str;
  1554. BigNumStr zero_bn_str = {0};
  1555. EcPoint const* pts[] = {this->efq2_a};
  1556. BigNumStr const* b[] = {&zero_bn_str};
  1557. size_t m = 1;
  1558. EXPECT_EQ(kEpidNoErr, EcSscmMultiExp(this->efq2, pts, b, m, this->efq2_r));
  1559. THROW_ON_EPIDERR(
  1560. WriteEcPoint(this->efq2, this->efq2_r, &efq2_r_str, sizeof(efq2_r_str)));
  1561. EXPECT_EQ(this->efq2_identity_str, efq2_r_str);
  1562. }
  1563. TEST_F(EcGroupTest, SscmMultiExpWorksGivenTwoG2ZeroExponent) {
  1564. G2ElemStr efq2_r_str;
  1565. BigNumStr zero_bn_str = {0};
  1566. EcPoint const* pts[] = {this->efq2_a, this->efq2_a};
  1567. BigNumStr const* b[] = {&zero_bn_str, &zero_bn_str};
  1568. size_t m = 2;
  1569. EXPECT_EQ(kEpidNoErr, EcSscmMultiExp(this->efq2, pts, b, m, this->efq2_r));
  1570. THROW_ON_EPIDERR(
  1571. WriteEcPoint(this->efq2, this->efq2_r, &efq2_r_str, sizeof(efq2_r_str)));
  1572. EXPECT_EQ(this->efq2_identity_str, efq2_r_str);
  1573. }
  1574. TEST_F(EcGroupTest, SscmMultiExpWorksGivenSixG2ZeroExponent) {
  1575. G2ElemStr efq2_r_str;
  1576. BigNumStr zero_bn_str = {0};
  1577. EcPoint const* pts[] = {this->efq2_a, this->efq2_a, this->efq2_a,
  1578. this->efq2_a, this->efq2_a, this->efq2_a};
  1579. BigNumStr const* b[] = {&zero_bn_str, &zero_bn_str, &zero_bn_str,
  1580. &zero_bn_str, &zero_bn_str, &zero_bn_str};
  1581. size_t m = 6;
  1582. EXPECT_EQ(kEpidNoErr, EcSscmMultiExp(this->efq2, pts, b, m, this->efq2_r));
  1583. THROW_ON_EPIDERR(
  1584. WriteEcPoint(this->efq2, this->efq2_r, &efq2_r_str, sizeof(efq2_r_str)));
  1585. EXPECT_EQ(this->efq2_identity_str, efq2_r_str);
  1586. }
  1587. TEST_F(EcGroupTest, SscmMultiExpWorksGivenOneExponent) {
  1588. G1ElemStr efq_r_str;
  1589. EcPoint const* pts[] = {this->efq_a};
  1590. BigNumStr const* b[] = {&this->x_str};
  1591. size_t m = 1;
  1592. EXPECT_EQ(kEpidNoErr, EcSscmMultiExp(this->efq, pts, b, m, this->efq_r));
  1593. THROW_ON_EPIDERR(
  1594. WriteEcPoint(this->efq, this->efq_r, &efq_r_str, sizeof(efq_r_str)));
  1595. EXPECT_EQ(this->efq_exp_ax_str, efq_r_str);
  1596. }
  1597. TEST_F(EcGroupTest, SscmMultiExpWorksGivenTwoExponents) {
  1598. G1ElemStr efq_r_str;
  1599. EcPoint const* pts[] = {this->efq_a, this->efq_b};
  1600. BigNumStr const* b[] = {&this->x_str, &this->y_str};
  1601. size_t m = 2;
  1602. EXPECT_EQ(kEpidNoErr, EcSscmMultiExp(this->efq, pts, b, m, this->efq_r));
  1603. THROW_ON_EPIDERR(
  1604. WriteEcPoint(this->efq, this->efq_r, &efq_r_str, sizeof(efq_r_str)));
  1605. EXPECT_EQ(this->efq_multiexp_abxy_str, efq_r_str);
  1606. }
  1607. TEST_F(EcGroupTest, SscmMultiExpWorksGivenOneG2Exponent) {
  1608. G2ElemStr efq2_r_str;
  1609. EcPoint const* pts[] = {this->efq2_a};
  1610. BigNumStr const* b[] = {&this->x_str};
  1611. size_t m = 1;
  1612. EXPECT_EQ(kEpidNoErr, EcSscmMultiExp(this->efq2, pts, b, m, this->efq2_r));
  1613. THROW_ON_EPIDERR(
  1614. WriteEcPoint(this->efq2, this->efq2_r, &efq2_r_str, sizeof(efq2_r_str)));
  1615. EXPECT_EQ(this->efq2_exp_ax_str, efq2_r_str);
  1616. }
  1617. TEST_F(EcGroupTest, SscmMultiExpWorksGivenTwoG2Exponents) {
  1618. G2ElemStr efq2_r_str;
  1619. EcPoint const* pts[] = {this->efq2_a, this->efq2_b};
  1620. BigNumStr const* b[] = {&this->x_str, &this->y_str};
  1621. size_t m = 2;
  1622. EXPECT_EQ(kEpidNoErr, EcSscmMultiExp(this->efq2, pts, b, m, this->efq2_r));
  1623. THROW_ON_EPIDERR(
  1624. WriteEcPoint(this->efq2, this->efq2_r, &efq2_r_str, sizeof(efq2_r_str)));
  1625. EXPECT_EQ(this->efq2_multiexp_abxy_str, efq2_r_str);
  1626. }
  1627. ///////////////////////////////////////////////////////////////////////
  1628. // EcGetRandom
  1629. TEST_F(EcGroupTest, GetRandomFailsGivenArgumentsMismatch) {
  1630. Prng my_prng;
  1631. EXPECT_EQ(kEpidBadArgErr,
  1632. EcGetRandom(this->efq2, &Prng::Generate, &my_prng, this->efq_r));
  1633. EXPECT_EQ(kEpidBadArgErr,
  1634. EcGetRandom(this->efq, &Prng::Generate, &my_prng, this->efq2_r));
  1635. }
  1636. TEST_F(EcGroupTest, GetRandomFailsGivenNullPointer) {
  1637. Prng my_prng;
  1638. EXPECT_EQ(kEpidBadArgErr,
  1639. EcGetRandom(nullptr, &Prng::Generate, &my_prng, this->efq_r));
  1640. EXPECT_EQ(kEpidBadArgErr,
  1641. EcGetRandom(this->efq, nullptr, &my_prng, this->efq_r));
  1642. EXPECT_EQ(kEpidBadArgErr,
  1643. EcGetRandom(this->efq, &Prng::Generate, &my_prng, nullptr));
  1644. }
  1645. TEST_F(EcGroupTest, GetRandomGeneratesDifferentECPoints) {
  1646. Prng my_prng;
  1647. EcPointObj r1(&this->efq);
  1648. EcPointObj r2(&this->efq);
  1649. bool result;
  1650. // __LINE__ makes sure that r1 and r2 are generated using distinct seeds
  1651. my_prng.set_seed(__LINE__);
  1652. EXPECT_EQ(kEpidNoErr, EcGetRandom(this->efq, &Prng::Generate, &my_prng, r1));
  1653. my_prng.set_seed(__LINE__);
  1654. EXPECT_EQ(kEpidNoErr, EcGetRandom(this->efq, &Prng::Generate, &my_prng, r2));
  1655. THROW_ON_EPIDERR(EcIsEqual(this->efq, r1, r2, &result));
  1656. EXPECT_FALSE(result);
  1657. }
  1658. ///////////////////////////////////////////////////////////////////////
  1659. // EcInGroup
  1660. TEST_F(EcGroupTest, InGroupFailsGivenNullPointer) {
  1661. bool in_group;
  1662. EXPECT_EQ(kEpidBadArgErr, EcInGroup(nullptr, &(this->efq_a_str),
  1663. sizeof(this->efq_a_str), &in_group));
  1664. EXPECT_EQ(kEpidBadArgErr,
  1665. EcInGroup(this->efq, nullptr, sizeof(this->efq_a_str), &in_group));
  1666. EXPECT_EQ(kEpidBadArgErr, EcInGroup(this->efq, &(this->efq_a_str),
  1667. sizeof(this->efq_a_str), nullptr));
  1668. }
  1669. TEST_F(EcGroupTest, InGroupFailsGivenInvalidBufferSize) {
  1670. bool in_group;
  1671. EXPECT_EQ(kEpidBadArgErr,
  1672. EcInGroup(this->efq, &(this->efq_a_str), 0, &in_group));
  1673. EXPECT_EQ(kEpidBadArgErr,
  1674. EcInGroup(this->efq, &(this->efq_a_str),
  1675. std::numeric_limits<size_t>::max(), &in_group));
  1676. #if (SIZE_MAX >= 0x100000001) // When size_t value allowed to be 0x100000001
  1677. EXPECT_EQ(kEpidBadArgErr,
  1678. EcInGroup(this->efq, &(this->efq_a_str), 0x100000001, &in_group));
  1679. #endif
  1680. }
  1681. TEST_F(EcGroupTest, InGroupDetectsElementNotInGroup) {
  1682. // element be not in group if Y coordinate increased by 1
  1683. G1ElemStr p_str = this->efq_a_str;
  1684. p_str.y.data.data[31] -= 1;
  1685. bool in_group;
  1686. EXPECT_EQ(kEpidNoErr, EcInGroup(this->efq, &p_str, sizeof(p_str), &in_group));
  1687. EXPECT_FALSE(in_group);
  1688. G2ElemStr p2_str = this->efq2_a_str;
  1689. p2_str.y[0].data.data[31] -= 1;
  1690. EXPECT_EQ(kEpidNoErr,
  1691. EcInGroup(this->efq2, &p2_str, sizeof(p2_str), &in_group));
  1692. EXPECT_FALSE(in_group);
  1693. }
  1694. TEST_F(EcGroupTest, InGroupDetectsIdentityElementInGroup) {
  1695. bool in_group;
  1696. EXPECT_EQ(kEpidNoErr, EcInGroup(this->efq, &(this->efq_identity_str),
  1697. sizeof(this->efq_identity_str), &in_group));
  1698. EXPECT_TRUE(in_group);
  1699. EXPECT_EQ(kEpidNoErr, EcInGroup(this->efq2, &(this->efq2_identity_str),
  1700. sizeof(this->efq2_identity_str), &in_group));
  1701. EXPECT_TRUE(in_group);
  1702. }
  1703. TEST_F(EcGroupTest, InGroupFailsGivenContextMismatch) {
  1704. bool in_group;
  1705. EXPECT_EQ(kEpidBadArgErr, EcInGroup(this->efq2, &(this->efq_a_str),
  1706. sizeof(this->efq_a_str), &in_group));
  1707. EXPECT_FALSE(in_group);
  1708. EXPECT_EQ(kEpidBadArgErr, EcInGroup(this->efq, &(this->efq2_a_str),
  1709. sizeof(this->efq2_a_str), &in_group));
  1710. EXPECT_FALSE(in_group);
  1711. }
  1712. ///////////////////////////////////////////////////////////////////////
  1713. // EcHash
  1714. TEST_F(EcGroupTest, HashFailsGivenArgumentsMismatch) {
  1715. uint8_t const msg[] = {0};
  1716. EXPECT_EQ(kEpidBadArgErr,
  1717. EcHash(this->efq2, msg, sizeof(msg), kSha256, this->efq_r));
  1718. EXPECT_EQ(kEpidBadArgErr,
  1719. EcHash(this->efq, msg, sizeof(msg), kSha256, this->efq2_r));
  1720. }
  1721. TEST_F(EcGroupTest, HashFailsGivenNullPointer) {
  1722. uint8_t const msg[] = {0};
  1723. EXPECT_EQ(kEpidBadArgErr,
  1724. EcHash(nullptr, msg, sizeof(msg), kSha256, this->efq_r));
  1725. EXPECT_EQ(kEpidBadArgErr,
  1726. EcHash(this->efq, nullptr, sizeof(msg), kSha256, this->efq_r));
  1727. EXPECT_EQ(kEpidBadArgErr,
  1728. EcHash(this->efq, msg, sizeof(msg), kSha256, nullptr));
  1729. }
  1730. TEST_F(EcGroupTest, HashFailsGivenUnsupportedHashAlg) {
  1731. uint8_t const msg[] = {0};
  1732. EXPECT_EQ(kEpidHashAlgorithmNotSupported,
  1733. EcHash(this->efq, msg, sizeof(msg), kSha512_256, this->efq_r));
  1734. EXPECT_EQ(kEpidHashAlgorithmNotSupported,
  1735. EcHash(this->efq, msg, sizeof(msg), kSha3_256, this->efq_r));
  1736. EXPECT_EQ(kEpidHashAlgorithmNotSupported,
  1737. EcHash(this->efq, msg, sizeof(msg), kSha3_384, this->efq_r));
  1738. EXPECT_EQ(kEpidHashAlgorithmNotSupported,
  1739. EcHash(this->efq, msg, sizeof(msg), kSha3_512, this->efq_r));
  1740. }
  1741. TEST_F(EcGroupTest, HashFailsGivenIncorrectMsgLen) {
  1742. uint8_t const msg[] = {0};
  1743. EXPECT_EQ(kEpidBadArgErr,
  1744. EcHash(this->efq, nullptr, 1, kSha256, this->efq_r));
  1745. EXPECT_EQ(kEpidBadArgErr,
  1746. EcHash(this->efq, msg, std::numeric_limits<size_t>::max(), kSha256,
  1747. this->efq_r));
  1748. EXPECT_EQ(kEpidBadArgErr,
  1749. EcHash(this->efq, msg, (size_t)INT_MAX + 1, kSha256, this->efq_r));
  1750. #if (SIZE_MAX >= 0x100000001) // When size_t value allowed to be 0x100000001
  1751. EXPECT_EQ(kEpidBadArgErr,
  1752. EcHash(this->efq, msg, (size_t)0x100000001, kSha256, this->efq_r));
  1753. #endif
  1754. }
  1755. TEST_F(EcGroupTest, HashAcceptsZeroLengthMessage) {
  1756. EXPECT_EQ(kEpidNoErr, EcHash(this->efq, "", 0, kSha256, this->efq_r));
  1757. }
  1758. TEST_F(EcGroupTest, HashWorksGivenSHA256HashAlg) {
  1759. G1ElemStr efq_r_str;
  1760. EXPECT_EQ(kEpidNoErr,
  1761. EcHash(this->efq, sha_msg, sizeof(sha_msg), kSha256, this->efq_r));
  1762. THROW_ON_EPIDERR(
  1763. WriteEcPoint(this->efq, this->efq_r, &efq_r_str, sizeof(efq_r_str)));
  1764. EXPECT_EQ(this->efq_r_sha256_str, efq_r_str);
  1765. }
  1766. TEST_F(EcGroupTest, HashWorksGivenSHA384HashAlg) {
  1767. G1ElemStr efq_r_str;
  1768. EXPECT_EQ(kEpidNoErr,
  1769. EcHash(this->efq, sha_msg, sizeof(sha_msg), kSha384, this->efq_r));
  1770. THROW_ON_EPIDERR(
  1771. WriteEcPoint(this->efq, this->efq_r, &efq_r_str, sizeof(efq_r_str)));
  1772. EXPECT_EQ(this->efq_r_sha384_str, efq_r_str);
  1773. }
  1774. TEST_F(EcGroupTest, HashWorksGivenSHA512HashAlg) {
  1775. G1ElemStr efq_r_str;
  1776. EXPECT_EQ(kEpidNoErr,
  1777. EcHash(this->efq, sha_msg, sizeof(sha_msg), kSha512, this->efq_r));
  1778. THROW_ON_EPIDERR(
  1779. WriteEcPoint(this->efq, this->efq_r, &efq_r_str, sizeof(efq_r_str)));
  1780. EXPECT_EQ(this->efq_r_sha512_str, efq_r_str);
  1781. }
  1782. ///////////////////////////////////////////////////////////////////////
  1783. // 1.1 EcHash
  1784. TEST_F(EcGroupTest, Epid11HashFailsGivenMismatchedArguments) {
  1785. uint8_t const msg[] = {0};
  1786. EXPECT_EQ(kEpidBadArgErr,
  1787. Epid11EcHash(this->efq2, msg, sizeof(msg), this->efq_r));
  1788. EXPECT_EQ(kEpidBadArgErr,
  1789. Epid11EcHash(this->efq, msg, sizeof(msg), this->efq2_r));
  1790. }
  1791. TEST_F(EcGroupTest, Epid11HashFailsGivenNullPointer) {
  1792. uint8_t const msg[] = {0};
  1793. EXPECT_EQ(kEpidBadArgErr,
  1794. Epid11EcHash(nullptr, msg, sizeof(msg), this->epid11_G3_r));
  1795. EXPECT_EQ(kEpidBadArgErr, Epid11EcHash(this->epid11_G3, nullptr, sizeof(msg),
  1796. this->epid11_G3_r));
  1797. EXPECT_EQ(kEpidBadArgErr,
  1798. Epid11EcHash(this->epid11_G3, msg, sizeof(msg), nullptr));
  1799. }
  1800. TEST_F(EcGroupTest, Epid11HashFailsGivenInvalidMsgLen) {
  1801. uint8_t const msg[] = {0};
  1802. EXPECT_EQ(kEpidBadArgErr,
  1803. Epid11EcHash(this->epid11_G3, nullptr, 1, this->epid11_G3_r));
  1804. EXPECT_EQ(kEpidBadArgErr, Epid11EcHash(this->epid11_G3, msg,
  1805. std::numeric_limits<size_t>::max(),
  1806. this->epid11_G3_r));
  1807. EXPECT_EQ(kEpidBadArgErr,
  1808. Epid11EcHash(this->epid11_G3, msg, (size_t)INT_MAX + 1,
  1809. this->epid11_G3_r));
  1810. #if (SIZE_MAX >= 0x100000001) // When size_t value allowed to be 0x100000001
  1811. EXPECT_EQ(kEpidBadArgErr,
  1812. Epid11EcHash(this->epid11_G3, msg, (size_t)0x100000001,
  1813. this->epid11_G3_r));
  1814. #endif
  1815. }
  1816. TEST_F(EcGroupTest, Epid11HashAcceptsZeroLengthMessage) {
  1817. EXPECT_EQ(kEpidNoErr,
  1818. Epid11EcHash(this->epid11_G3, "", 0, this->epid11_G3_r));
  1819. }
  1820. TEST_F(EcGroupTest, Epid11HashWorksGivenValidParameters) {
  1821. Epid11G3ElemStr r_str;
  1822. uint8_t const msg0[] = {'a', 'a', 'd'};
  1823. EXPECT_EQ(kEpidNoErr, Epid11EcHash(this->epid11_G3, msg0, sizeof(msg0),
  1824. this->epid11_G3_r));
  1825. THROW_ON_EPIDERR(
  1826. WriteEcPoint(this->epid11_G3, this->epid11_G3_r, &r_str, sizeof(r_str)));
  1827. EXPECT_EQ(this->kAadHash, r_str);
  1828. uint8_t const msg1[] = {'b', 's', 'n', '0'};
  1829. EXPECT_EQ(kEpidNoErr, Epid11EcHash(this->epid11_G3, msg1, sizeof(msg1),
  1830. this->epid11_G3_r));
  1831. THROW_ON_EPIDERR(
  1832. WriteEcPoint(this->epid11_G3, this->epid11_G3_r, &r_str, sizeof(r_str)));
  1833. EXPECT_EQ(this->kBsn0Hash, r_str);
  1834. uint8_t const msg2[] = {'t', 'e', 's', 't'};
  1835. EXPECT_EQ(kEpidNoErr, Epid11EcHash(this->epid11_G3, msg2, sizeof(msg2),
  1836. this->epid11_G3_r));
  1837. THROW_ON_EPIDERR(
  1838. WriteEcPoint(this->epid11_G3, this->epid11_G3_r, &r_str, sizeof(r_str)));
  1839. EXPECT_EQ(this->kTestHash, r_str);
  1840. uint8_t const msg3[] = {'a', 'a', 'c'};
  1841. EXPECT_EQ(kEpidNoErr, Epid11EcHash(this->epid11_G3, msg3, sizeof(msg3),
  1842. this->epid11_G3_r));
  1843. THROW_ON_EPIDERR(
  1844. WriteEcPoint(this->epid11_G3, this->epid11_G3_r, &r_str, sizeof(r_str)));
  1845. EXPECT_EQ(this->kAacHash, r_str);
  1846. }
  1847. ///////////////////////////////////////////////////////////////////////
  1848. // EcMakePoint
  1849. TEST_F(EcGroupTest, MakePointFailsGivenArgumentsMismatch) {
  1850. FfElementObj fq2_a(&this->efq2_par->fq2);
  1851. EXPECT_EQ(kEpidBadArgErr, EcMakePoint(this->efq2, this->fq_a, this->efq_r));
  1852. EXPECT_EQ(kEpidBadArgErr, EcMakePoint(this->efq, fq2_a, this->efq_r));
  1853. EXPECT_EQ(kEpidBadArgErr, EcMakePoint(this->efq2, this->fq_a, this->efq2_r));
  1854. }
  1855. TEST_F(EcGroupTest, MakePointFailsGivenNullPointer) {
  1856. EXPECT_EQ(kEpidBadArgErr, EcMakePoint(nullptr, this->fq_a, this->efq_r));
  1857. EXPECT_EQ(kEpidBadArgErr, EcMakePoint(this->efq, nullptr, this->efq_r));
  1858. EXPECT_EQ(kEpidBadArgErr, EcMakePoint(this->efq, this->fq_a, nullptr));
  1859. }
  1860. TEST_F(EcGroupTest, MakePointSucceedsGivenElement) {
  1861. Prng my_prng;
  1862. G1ElemStr efq_r_str;
  1863. // a pre-computed point in eqf
  1864. G1ElemStr efq_ref_str = {
  1865. {{0X1C, 0X53, 0X40, 0X69, 0X8B, 0X77, 0X75, 0XAA, 0X2B, 0X7D, 0X91, 0XD6,
  1866. 0X29, 0X49, 0X05, 0X7F, 0XF6, 0X4C, 0X63, 0X90, 0X58, 0X22, 0X06, 0XF5,
  1867. 0X1F, 0X3B, 0X9F, 0XA2, 0X04, 0X39, 0XA9, 0X67}},
  1868. {{0X3B, 0X65, 0X58, 0XAC, 0X97, 0X46, 0X47, 0XC9, 0X84, 0X57, 0X3F, 0XFA,
  1869. 0X4F, 0XB0, 0X64, 0X8D, 0X48, 0XC8, 0X14, 0XEB, 0XF1, 0X94, 0X87, 0XDC,
  1870. 0XB3, 0X73, 0X90, 0X1D, 0X75, 0XAD, 0XD5, 0X56}}};
  1871. // create a point with x == ref.x
  1872. FfElementObj elem(&this->fq, efq_ref_str.x);
  1873. EXPECT_EQ(kEpidNoErr, EcMakePoint(this->efq, elem, this->efq_r));
  1874. // check that the point matches ref
  1875. THROW_ON_EPIDERR(
  1876. WriteEcPoint(this->efq, this->efq_r, &efq_r_str, sizeof(efq_r_str)));
  1877. EXPECT_EQ(efq_ref_str, efq_r_str);
  1878. }
  1879. TEST_F(EcGroupTest, MakePointFailsGivenZeroElement) {
  1880. EXPECT_EQ(kEpidBadArgErr,
  1881. EcMakePoint(this->efq, FfElementObj(&this->fq), this->efq_r));
  1882. // EcMakePoint is only defined for G1
  1883. EXPECT_EQ(kEpidBadArgErr,
  1884. EcMakePoint(this->efq2, FfElementObj(&this->efq2_par->fq2),
  1885. this->efq2_r));
  1886. }
  1887. ///////////////////////////////////////////////////////////////////////
  1888. // EcInverse
  1889. TEST_F(EcGroupTest, InverseFailsGivenArgumentsMismatch) {
  1890. EXPECT_EQ(kEpidBadArgErr, EcInverse(this->efq2, this->efq_a, this->efq_r));
  1891. EXPECT_EQ(kEpidBadArgErr, EcInverse(this->efq, this->efq2_a, this->efq_r));
  1892. EXPECT_EQ(kEpidBadArgErr, EcInverse(this->efq, this->efq_a, this->efq2_r));
  1893. }
  1894. TEST_F(EcGroupTest, InverseFailsGivenNullPointer) {
  1895. EXPECT_EQ(kEpidBadArgErr, EcInverse(nullptr, this->efq_a, this->efq_r));
  1896. EXPECT_EQ(kEpidBadArgErr, EcInverse(this->efq, nullptr, this->efq_r));
  1897. EXPECT_EQ(kEpidBadArgErr, EcInverse(this->efq, this->efq_a, nullptr));
  1898. }
  1899. TEST_F(EcGroupTest, InverseSucceedsGivenIdentity) {
  1900. G1ElemStr efq_r_str;
  1901. EXPECT_EQ(kEpidNoErr, EcInverse(this->efq, this->efq_identity, this->efq_r));
  1902. THROW_ON_EPIDERR(
  1903. WriteEcPoint(this->efq, this->efq_r, &efq_r_str, sizeof(efq_r_str)));
  1904. EXPECT_EQ(this->efq_identity_str, efq_r_str);
  1905. G2ElemStr efq2_r_str;
  1906. EXPECT_EQ(kEpidNoErr,
  1907. EcInverse(this->efq2, this->efq2_identity, this->efq2_r));
  1908. THROW_ON_EPIDERR(
  1909. WriteEcPoint(this->efq2, this->efq2_r, &efq2_r_str, sizeof(efq2_r_str)));
  1910. EXPECT_EQ(this->efq2_identity_str, efq2_r_str);
  1911. }
  1912. TEST_F(EcGroupTest, InverseSucceedsGivenElement) {
  1913. G1ElemStr efq_r_str;
  1914. EXPECT_EQ(kEpidNoErr, EcInverse(this->efq, this->efq_a, this->efq_r));
  1915. THROW_ON_EPIDERR(
  1916. WriteEcPoint(this->efq, this->efq_r, &efq_r_str, sizeof(efq_r_str)));
  1917. EXPECT_EQ(this->efq_inv_a_str, efq_r_str);
  1918. G2ElemStr efq2_r_str;
  1919. EXPECT_EQ(kEpidNoErr, EcInverse(this->efq2, this->efq2_a, this->efq2_r));
  1920. THROW_ON_EPIDERR(
  1921. WriteEcPoint(this->efq2, this->efq2_r, &efq2_r_str, sizeof(efq2_r_str)));
  1922. EXPECT_EQ(this->efq2_inv_a_str, efq2_r_str);
  1923. }
  1924. ///////////////////////////////////////////////////////////////////////
  1925. // EcIsEqual
  1926. TEST_F(EcGroupTest, IsEqualFailsGivenArgumentsMismatch) {
  1927. bool result;
  1928. EXPECT_EQ(kEpidBadArgErr,
  1929. EcIsEqual(this->efq2, this->efq_a, this->efq_a, &result));
  1930. EXPECT_EQ(kEpidBadArgErr,
  1931. EcIsEqual(this->efq, this->efq2_a, this->efq_a, &result));
  1932. EXPECT_EQ(kEpidBadArgErr,
  1933. EcIsEqual(this->efq, this->efq_a, this->efq2_a, &result));
  1934. }
  1935. TEST_F(EcGroupTest, IsEqualFailsGivenNullPointer) {
  1936. bool result;
  1937. EXPECT_EQ(kEpidBadArgErr,
  1938. EcIsEqual(nullptr, this->efq_a, this->efq_a, &result));
  1939. EXPECT_EQ(kEpidBadArgErr,
  1940. EcIsEqual(this->efq, nullptr, this->efq_a, &result));
  1941. EXPECT_EQ(kEpidBadArgErr,
  1942. EcIsEqual(this->efq, this->efq_a, nullptr, &result));
  1943. EXPECT_EQ(kEpidBadArgErr,
  1944. EcIsEqual(this->efq, this->efq_a, this->efq_a, nullptr));
  1945. }
  1946. TEST_F(EcGroupTest, IsEqualCanCompareElementWithItself) {
  1947. bool result;
  1948. ASSERT_EQ(kEpidNoErr,
  1949. EcIsEqual(this->efq, this->efq_a, this->efq_a, &result));
  1950. EXPECT_TRUE(result);
  1951. ASSERT_EQ(kEpidNoErr,
  1952. EcIsEqual(this->efq2, this->efq2_a, this->efq2_a, &result));
  1953. EXPECT_TRUE(result);
  1954. }
  1955. TEST_F(EcGroupTest, DifferentEFqElementsAreNotEqual) {
  1956. bool result;
  1957. ASSERT_EQ(kEpidNoErr,
  1958. EcIsEqual(this->efq, this->efq_a, this->efq_b, &result));
  1959. EXPECT_FALSE(result);
  1960. }
  1961. TEST_F(EcGroupTest, SameEFqElementsAreEqual) {
  1962. THROW_ON_EPIDERR(ReadEcPoint(this->efq, &(this->efq_a_str),
  1963. sizeof(this->efq_a_str), this->efq_b));
  1964. bool result;
  1965. ASSERT_EQ(kEpidNoErr,
  1966. EcIsEqual(this->efq, this->efq_a, this->efq_b, &result));
  1967. EXPECT_TRUE(result);
  1968. }
  1969. TEST_F(EcGroupTest, IsEqualCanCompareIdentityEFqElements) {
  1970. THROW_ON_EPIDERR(ReadEcPoint(this->efq, &(this->efq_identity_str),
  1971. sizeof(this->efq_identity_str), this->efq_b));
  1972. bool result;
  1973. ASSERT_EQ(kEpidNoErr,
  1974. EcIsEqual(this->efq, this->efq_identity, this->efq_b, &result));
  1975. EXPECT_TRUE(result);
  1976. }
  1977. TEST_F(EcGroupTest, DifferentEFq2ElementsAreNotEqual) {
  1978. bool result;
  1979. ASSERT_EQ(kEpidNoErr,
  1980. EcIsEqual(this->efq2, this->efq2_a, this->efq2_b, &result));
  1981. EXPECT_FALSE(result);
  1982. }
  1983. TEST_F(EcGroupTest, SameEFq2ElementsAreEqual) {
  1984. THROW_ON_EPIDERR(ReadEcPoint(this->efq2, &(this->efq2_a_str),
  1985. sizeof(this->efq2_a_str), this->efq2_b));
  1986. bool result;
  1987. ASSERT_EQ(kEpidNoErr,
  1988. EcIsEqual(this->efq2, this->efq2_a, this->efq2_b, &result));
  1989. EXPECT_TRUE(result);
  1990. }
  1991. TEST_F(EcGroupTest, IsEqualCanCompareIdentityEFq2Elements) {
  1992. THROW_ON_EPIDERR(ReadEcPoint(this->efq2, &(this->efq2_identity_str),
  1993. sizeof(this->efq2_identity_str), this->efq2_b));
  1994. bool result;
  1995. ASSERT_EQ(kEpidNoErr,
  1996. EcIsEqual(this->efq2, this->efq2_identity, this->efq2_b, &result));
  1997. EXPECT_TRUE(result);
  1998. }
  1999. ///////////////////////////////////////////////////////////////////////
  2000. // EcIsIdentity
  2001. TEST_F(EcGroupTest, IsIdentityFailsGivenArgumentsMismatch) {
  2002. bool result;
  2003. EXPECT_EQ(kEpidBadArgErr,
  2004. EcIsIdentity(this->efq2, this->efq_identity, &result));
  2005. EXPECT_EQ(kEpidBadArgErr,
  2006. EcIsIdentity(this->efq, this->efq2_identity, &result));
  2007. }
  2008. TEST_F(EcGroupTest, IsIdentityFailsGivenNullPointer) {
  2009. bool result;
  2010. EXPECT_EQ(kEpidBadArgErr, EcIsIdentity(nullptr, this->efq_identity, &result));
  2011. EXPECT_EQ(kEpidBadArgErr, EcIsIdentity(this->efq, nullptr, &result));
  2012. EXPECT_EQ(kEpidBadArgErr,
  2013. EcIsIdentity(this->efq, this->efq_identity, nullptr));
  2014. }
  2015. TEST_F(EcGroupTest, IsIdentityDetectsIdentityElement) {
  2016. bool result;
  2017. EXPECT_EQ(kEpidNoErr, EcIsIdentity(this->efq, this->efq_identity, &result));
  2018. EXPECT_TRUE(result);
  2019. EXPECT_EQ(kEpidNoErr, EcIsIdentity(this->efq2, this->efq2_identity, &result));
  2020. EXPECT_TRUE(result);
  2021. }
  2022. TEST_F(EcGroupTest, IsIdentityDetectsNonIdentityElement) {
  2023. bool result;
  2024. EXPECT_EQ(kEpidNoErr, EcIsIdentity(this->efq, this->efq_a, &result));
  2025. EXPECT_FALSE(result);
  2026. EXPECT_EQ(kEpidNoErr, EcIsIdentity(this->efq2, this->efq2_a, &result));
  2027. EXPECT_FALSE(result);
  2028. }
  2029. } // namespace