ecgroup-test.cc 95 KB

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