sm9_alg.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372
  1. /*
  2. * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdint.h>
  12. #include <stdlib.h>
  13. #include <assert.h>
  14. #include <gmssl/hex.h>
  15. #include <gmssl/mem.h>
  16. #include <gmssl/sm9.h>
  17. #include <gmssl/error.h>
  18. #include <gmssl/endian.h>
  19. #include <gmssl/rand.h>
  20. const sm9_bn_t SM9_ZERO = {0,0,0,0,0,0,0,0};
  21. const sm9_bn_t SM9_ONE = {1,0,0,0,0,0,0,0};
  22. static const sm9_bn_t SM9_TWO = {2,0,0,0,0,0,0,0};
  23. static const sm9_bn_t SM9_FIVE = {5,0,0,0,0,0,0,0};
  24. // p = b640000002a3a6f1d603ab4ff58ec74521f2934b1a7aeedbe56f9b27e351457d
  25. // n = b640000002a3a6f1d603ab4ff58ec74449f2934b18ea8beee56ee19cd69ecf25
  26. // mu_p = 2^512 // p = 167980e0beb5759a655f73aebdcd1312af2665f6d1e36081c71188f90d5c22146
  27. // mu_n = 2^512 // n
  28. const sm9_bn_t SM9_P = {0xe351457d, 0xe56f9b27, 0x1a7aeedb, 0x21f2934b, 0xf58ec745, 0xd603ab4f, 0x02a3a6f1, 0xb6400000};
  29. const sm9_bn_t SM9_N = {0xd69ecf25, 0xe56ee19c, 0x18ea8bee, 0x49f2934b, 0xf58ec744, 0xd603ab4f, 0x02a3a6f1, 0xb6400000};
  30. static const sm9_bn_t SM9_P_MINUS_ONE = {0xe351457c, 0xe56f9b27, 0x1a7aeedb, 0x21f2934b, 0xf58ec745, 0xd603ab4f, 0x02a3a6f1, 0xb6400000};
  31. static const sm9_bn_t SM9_N_MINUS_ONE = {0xd69ecf24, 0xe56ee19c, 0x18ea8bee, 0x49f2934b, 0xf58ec744, 0xd603ab4f, 0x02a3a6f1, 0xb6400000};
  32. static const sm9_barrett_bn_t SM9_MU_P = {0xd5c22146, 0x71188f90, 0x1e36081c, 0xf2665f6d, 0xdcd1312a, 0x55f73aeb, 0xeb5759a6, 0x67980e0b, 0x00000001};
  33. static const sm9_barrett_bn_t SM9_MU_N = {0xdfc97c2f, 0x74df4fd4, 0xc9c073b0, 0x9c95d85e, 0xdcd1312c, 0x55f73aeb, 0xeb5759a6, 0x67980e0b, 0x00000001};
  34. static const sm9_barrett_bn_t SM9_MU_N_MINUS_ONE = {0xdfc97c31, 0x74df4fd4, 0xc9c073b0, 0x9c95d85e, 0xdcd1312c, 0x55f73aeb, 0xeb5759a6, 0x67980e0b, 0x00000001};
  35. // P1.X 0x93DE051D62BF718FF5ED0704487D01D6E1E4086909DC3280E8C4E4817C66DDDD
  36. // P1.Y 0x21FE8DDA4F21E607631065125C395BBC1C1C00CBFA6024350C464CD70A3EA616
  37. const SM9_POINT _SM9_P1 = {
  38. {0x7c66dddd, 0xe8c4e481, 0x09dc3280, 0xe1e40869, 0x487d01d6, 0xf5ed0704, 0x62bf718f, 0x93de051d},
  39. {0x0a3ea616, 0x0c464cd7, 0xfa602435, 0x1c1c00cb, 0x5c395bbc, 0x63106512, 0x4f21e607, 0x21fe8dda},
  40. {1,0,0,0,0,0,0,0}
  41. };
  42. const SM9_POINT *SM9_P1 = &_SM9_P1;
  43. /*
  44. X : [0x3722755292130b08d2aab97fd34ec120ee265948d19c17abf9b7213baf82d65bn,
  45. 0x85aef3d078640c98597b6027b441a01ff1dd2c190f5e93c454806c11d8806141n],
  46. Y : [0xa7cf28d519be3da65f3170153d278ff247efba98a71a08116215bba5c999a7c7n,
  47. 0x17509b092e845c1266ba0d262cbee6ed0736a96fa347c8bd856dc76b84ebeb96n],
  48. Z : [1n, 0n],
  49. */
  50. const SM9_TWIST_POINT _SM9_P2 = {
  51. {{0xAF82D65B, 0xF9B7213B, 0xD19C17AB, 0xEE265948, 0xD34EC120, 0xD2AAB97F, 0x92130B08, 0x37227552},
  52. {0xD8806141, 0x54806C11, 0x0F5E93C4, 0xF1DD2C19, 0xB441A01F, 0x597B6027, 0x78640C98, 0x85AEF3D0}},
  53. {{0xC999A7C7, 0x6215BBA5, 0xA71A0811, 0x47EFBA98, 0x3D278FF2, 0x5F317015, 0x19BE3DA6, 0xA7CF28D5},
  54. {0x84EBEB96, 0x856DC76B, 0xA347C8BD, 0x0736A96F, 0x2CBEE6ED, 0x66BA0D26, 0x2E845C12, 0x17509B09}},
  55. {{1,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}},
  56. };
  57. const SM9_TWIST_POINT *SM9_P2 = &_SM9_P2;
  58. const SM9_TWIST_POINT _SM9_Ppubs = {
  59. {{0x96EA5E32, 0x8F14D656, 0x386A92DD, 0x414D2177, 0x24A3B573, 0x6CE843ED, 0x152D1F78, 0x29DBA116},
  60. {0x1B94C408, 0x0AB1B679, 0x5E392CFB, 0x1CE0711C, 0x41B56501, 0xE48AFF4B, 0x3084F733, 0x9F64080B}},
  61. {{0xB4E3216D, 0x0E75C05F, 0x5CDFF073, 0x1006E85F, 0xB7A46F74, 0x1A7CE027, 0xDDA532DA, 0x41E00A53},
  62. {0xD0EF1C25, 0xE89E1408, 0x1A77F335, 0xAD3E2FDB, 0x47E3A0CB, 0xB57329F4, 0xABEA0112, 0x69850938}},
  63. {{1,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}},
  64. };
  65. const SM9_TWIST_POINT *SM9_Ppubs = &_SM9_Ppubs;
  66. void sm9_bn_set_zero(sm9_bn_t r)
  67. {
  68. sm9_bn_copy(r, SM9_ZERO);
  69. }
  70. void sm9_bn_set_one(sm9_bn_t r)
  71. {
  72. sm9_bn_copy(r, SM9_ONE);
  73. }
  74. int sm9_bn_is_zero(const sm9_bn_t a)
  75. {
  76. return (sm9_bn_cmp(a, SM9_ZERO) == 0);
  77. }
  78. int sm9_bn_is_one(const sm9_bn_t a)
  79. {
  80. return (sm9_bn_cmp(a, SM9_ONE) == 0);
  81. }
  82. void sm9_bn_to_bytes(const sm9_bn_t a, uint8_t out[32])
  83. {
  84. int i;
  85. for (i = 7; i >= 0; i--) {
  86. PUTU32(out, (uint32_t)a[i]);
  87. out += sizeof(uint32_t);
  88. }
  89. }
  90. void sm9_bn_from_bytes(sm9_bn_t r, const uint8_t in[32])
  91. {
  92. int i;
  93. for (i = 7; i >= 0; i--) {
  94. r[i] = GETU32(in);
  95. in += sizeof(uint32_t);
  96. }
  97. }
  98. int sm9_bn_from_hex(sm9_bn_t r, const char hex[64])
  99. {
  100. uint8_t buf[32];
  101. size_t len;
  102. if (hex_to_bytes(hex, 64, buf, &len) < 0) {
  103. return -1;
  104. }
  105. sm9_bn_from_bytes(r, buf);
  106. return 1;
  107. }
  108. void sm9_bn_to_hex(const sm9_bn_t a, char hex[64])
  109. {
  110. int i;
  111. for (i = 7; i >= 0; i--) {
  112. (void)sprintf(hex + 8*(7-i), "%08x", (uint32_t)a[i]);
  113. //hex += 8;
  114. }
  115. }
  116. void sm9_print_bn(const char *prefix, const sm9_bn_t a)
  117. {
  118. char hex[65] = {0};
  119. sm9_bn_to_hex(a, hex);
  120. printf("%s\n%s\n", prefix, hex);
  121. }
  122. void sm9_bn_to_bits(const sm9_bn_t a, char bits[256])
  123. {
  124. int i, j;
  125. for (i = 7; i >= 0; i--) {
  126. uint32_t w = (uint32_t)a[i];
  127. for (j = 0; j < 32; j++) {
  128. *bits++ = (w & 0x80000000) ? '1' : '0';
  129. w <<= 1;
  130. }
  131. }
  132. }
  133. int sm9_bn_cmp(const sm9_bn_t a, const sm9_bn_t b)
  134. {
  135. int i;
  136. for (i = 7; i >= 0; i--) {
  137. if (a[i] > b[i])
  138. return 1;
  139. if (a[i] < b[i])
  140. return -1;
  141. }
  142. return 0;
  143. }
  144. void sm9_bn_copy(sm9_bn_t r, const sm9_bn_t a)
  145. {
  146. memcpy(r, a, sizeof(sm9_bn_t));
  147. }
  148. void sm9_bn_set_word(sm9_bn_t r, uint32_t a)
  149. {
  150. sm9_bn_set_zero(r);
  151. r[0] = a;
  152. }
  153. void sm9_bn_add(sm9_bn_t r, const sm9_bn_t a, const sm9_bn_t b)
  154. {
  155. int i;
  156. r[0] = a[0] + b[0];
  157. for (i = 1; i < 8; i++) {
  158. r[i] = a[i] + b[i] + (r[i-1] >> 32);
  159. }
  160. for (i = 0; i < 7; i++) {
  161. r[i] &= 0xffffffff;
  162. }
  163. }
  164. void sm9_bn_sub(sm9_bn_t ret, const sm9_bn_t a, const sm9_bn_t b)
  165. {
  166. int i;
  167. sm9_bn_t r;
  168. r[0] = ((uint64_t)1 << 32) + a[0] - b[0];
  169. for (i = 1; i < 7; i++) {
  170. r[i] = 0xffffffff + a[i] - b[i] + (r[i - 1] >> 32);
  171. r[i - 1] &= 0xffffffff;
  172. }
  173. r[i] = a[i] - b[i] + (r[i - 1] >> 32) - 1;
  174. r[i - 1] &= 0xffffffff;
  175. sm9_bn_copy(ret, r);
  176. }
  177. int sm9_bn_rand_range(sm9_bn_t r, const sm9_bn_t range)
  178. {
  179. uint8_t buf[256];
  180. do {
  181. rand_bytes(buf, sizeof(buf));
  182. sm9_bn_from_bytes(r, buf);
  183. } while (sm9_bn_cmp(r, range) >= 0);
  184. return 1;
  185. }
  186. int sm9_bn_equ(const sm9_bn_t a, const sm9_bn_t b)
  187. {
  188. int i;
  189. for (i = 0; i < 8; i++) {
  190. if (a[i] != b[i])
  191. return 0;
  192. }
  193. return 1;
  194. }
  195. void sm9_fp_add(sm9_fp_t r, const sm9_fp_t a, const sm9_fp_t b)
  196. {
  197. sm9_bn_add(r, a, b);
  198. if (sm9_bn_cmp(r, SM9_P) >= 0) {
  199. sm9_bn_sub(r, r, SM9_P);
  200. }
  201. }
  202. void sm9_fp_sub(sm9_fp_t r, const sm9_fp_t a, const sm9_fp_t b)
  203. {
  204. if (sm9_bn_cmp(a, b) >= 0) {
  205. sm9_bn_sub(r, a, b);
  206. } else {
  207. sm9_bn_t t;
  208. sm9_bn_sub(t, SM9_P, b);
  209. sm9_bn_add(r, t, a);
  210. }
  211. }
  212. void sm9_fp_dbl(sm9_fp_t r, const sm9_fp_t a)
  213. {
  214. sm9_fp_add(r, a, a);
  215. }
  216. void sm9_fp_tri(sm9_fp_t r, const sm9_fp_t a)
  217. {
  218. sm9_fp_t t;
  219. sm9_fp_dbl(t, a);
  220. sm9_fp_add(r, t, a);
  221. }
  222. void sm9_fp_div2(sm9_fp_t r, const sm9_fp_t a)
  223. {
  224. int i;
  225. sm9_bn_copy(r, a);
  226. if (r[0] & 0x01) {
  227. sm9_bn_add(r, r, SM9_P);
  228. }
  229. for (i = 0; i < 7; i++) {
  230. r[i] = (r[i] >> 1) | ((r[i + 1] & 0x01) << 31);
  231. }
  232. r[i] >>= 1;
  233. }
  234. void sm9_fp_neg(sm9_fp_t r, const sm9_fp_t a)
  235. {
  236. if (sm9_bn_is_zero(a)) {
  237. sm9_bn_copy(r, a);
  238. } else {
  239. sm9_bn_sub(r, SM9_P, a);
  240. }
  241. }
  242. int sm9_bn_print(FILE *fp, int fmt, int ind, const char *label, const sm9_bn_t a)
  243. {
  244. uint8_t buf[32];
  245. sm9_bn_to_bytes(a, buf);
  246. format_bytes(fp, fmt, ind, label, buf, sizeof(buf));
  247. return 1;
  248. }
  249. int sm9_barrett_bn_cmp(const sm9_barrett_bn_t a, const sm9_barrett_bn_t b)
  250. {
  251. int i;
  252. for (i = 8; i >= 0; i--) {
  253. if (a[i] > b[i])
  254. return 1;
  255. if (a[i] < b[i])
  256. return -1;
  257. }
  258. return 0;
  259. }
  260. void sm9_barrett_bn_add(sm9_barrett_bn_t r, const sm9_barrett_bn_t a, const sm9_barrett_bn_t b)
  261. {
  262. int i;
  263. r[0] = a[0] + b[0];
  264. for (i = 1; i < 9; i++) {
  265. r[i] = a[i] + b[i] + (r[i-1] >> 32);
  266. }
  267. for (i = 0; i < 8; i++) {
  268. r[i] &= 0xffffffff;
  269. }
  270. }
  271. void sm9_barrett_bn_sub(sm9_barrett_bn_t ret, const sm9_barrett_bn_t a, const sm9_barrett_bn_t b)
  272. {
  273. sm9_barrett_bn_t r;
  274. int i;
  275. r[0] = ((uint64_t)1 << 32) + a[0] - b[0];
  276. for (i = 1; i < 8; i++) {
  277. r[i] = 0xffffffff + a[i] - b[i] + (r[i - 1] >> 32);
  278. r[i - 1] &= 0xffffffff;
  279. }
  280. r[i] = a[i] - b[i] + (r[i - 1] >> 32) - 1;
  281. r[i - 1] &= 0xffffffff;
  282. for (i = 0; i < 9; i++) {
  283. ret[i] = r[i];
  284. }
  285. }
  286. void sm9_fp_mul(sm9_fp_t r, const sm9_fp_t a, const sm9_fp_t b)
  287. {
  288. uint64_t s[18];
  289. sm9_barrett_bn_t zh, zl, q;
  290. uint64_t w;
  291. int i, j;
  292. /* z = a * b */
  293. for (i = 0; i < 8; i++) {
  294. s[i] = 0;
  295. }
  296. for (i = 0; i < 8; i++) {
  297. w = 0;
  298. for (j = 0; j < 8; j++) {
  299. w += s[i + j] + a[i] * b[j];
  300. s[i + j] = w & 0xffffffff;
  301. w >>= 32;
  302. }
  303. s[i + 8] = w;
  304. }
  305. /* zl = z mod (2^32)^9 = z[0..8]
  306. * zh = z // (2^32)^7 = z[7..15] */
  307. for (i = 0; i < 9; i++) {
  308. zl[i] = s[i];
  309. zh[i] = s[7 + i];
  310. }
  311. /* q = zh * mu // (2^32)^9 */
  312. for (i = 0; i < 18; i++) {
  313. s[i] = 0;
  314. }
  315. for (i = 0; i < 9; i++) {
  316. w = 0;
  317. for (j = 0; j < 9; j++) {
  318. w += s[i + j] + zh[i] * SM9_MU_P[j];
  319. s[i + j] = w & 0xffffffff;
  320. w >>= 32;
  321. }
  322. s[i + 9] = w;
  323. }
  324. for (i = 0; i < 9; i++) {
  325. q[i] = s[9 + i];
  326. }
  327. /* q = q * p mod (2^32)^9 */
  328. for (i = 0; i < 18; i++) {
  329. s[i] = 0;
  330. }
  331. for (i = 0; i < 9; i++) {
  332. w = 0;
  333. for (j = 0; j < 8; j++) {
  334. w += s[i + j] + q[i] * SM9_P[j];
  335. s[i + j] = w & 0xffffffff;
  336. w >>= 32;
  337. }
  338. s[i + 8] = w;
  339. }
  340. for (i = 0; i < 9; i++) {
  341. q[i] = s[i];
  342. }
  343. /* r = zl - q (mod (2^32)^9) */
  344. if (sm9_barrett_bn_cmp(zl, q)) {
  345. sm9_barrett_bn_sub(zl, zl, q);
  346. } else {
  347. sm9_barrett_bn_t c = {0,0,0,0,0,0,0,0,0x100000000};
  348. sm9_barrett_bn_sub(q, c, q);
  349. sm9_barrett_bn_add(zl, q, zl);
  350. }
  351. for (i = 0; i < 8; i++) {
  352. r[i] = zl[i];
  353. }
  354. r[7] += (zl[8] << 32);
  355. /* while r >= p do: r = r - p */
  356. while (sm9_bn_cmp(r, SM9_P) >= 0) {
  357. sm9_bn_sub(r, r, SM9_P);
  358. }
  359. }
  360. void sm9_fp_sqr(sm9_fp_t r, const sm9_fp_t a)
  361. {
  362. sm9_fp_mul(r, a, a);
  363. }
  364. void sm9_fp_pow(sm9_fp_t r, const sm9_fp_t a, const sm9_bn_t e)
  365. {
  366. sm9_fp_t t;
  367. uint32_t w;
  368. int i, j;
  369. assert(sm9_bn_cmp(e, SM9_P_MINUS_ONE) < 0);
  370. sm9_bn_set_one(t);
  371. for (i = 7; i >= 0; i--) {
  372. w = (uint32_t)e[i];
  373. for (j = 0; j < 32; j++) {
  374. sm9_fp_sqr(t, t);
  375. if (w & 0x80000000)
  376. sm9_fp_mul(t, t, a);
  377. w <<= 1;
  378. }
  379. }
  380. sm9_bn_copy(r, t);
  381. }
  382. void sm9_fp_inv(sm9_fp_t r, const sm9_fp_t a)
  383. {
  384. sm9_fp_t e;
  385. sm9_bn_sub(e, SM9_P, SM9_TWO);
  386. sm9_fp_pow(r, a, e);
  387. }
  388. int sm9_fp_from_bytes(sm9_fp_t r, const uint8_t buf[32])
  389. {
  390. sm9_bn_from_bytes(r, buf);
  391. if (sm9_bn_cmp(r, SM9_P) >= 0) {
  392. error_print();
  393. return -1;
  394. }
  395. return 1;
  396. }
  397. int sm9_fp_from_hex(sm9_fp_t r, const char hex[64])
  398. {
  399. if (sm9_bn_from_hex(r, hex) != 1) {
  400. error_print();
  401. return -1;
  402. }
  403. if (sm9_bn_cmp(r, SM9_P) >= 0) {
  404. error_print();
  405. return -1;
  406. }
  407. return 1;
  408. }
  409. const sm9_fp2_t SM9_FP2_ZERO = {{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0}};
  410. const sm9_fp2_t SM9_FP2_ONE = {{1,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0}};
  411. const sm9_fp2_t SM9_FP2_U = {{0,0,0,0,0,0,0,0},{1,0,0,0,0,0,0,0}};
  412. static const sm9_fp2_t SM9_FP2_5U = {{0,0,0,0,0,0,0,0},{5,0,0,0,0,0,0,0}};
  413. int sm9_fp2_equ(const sm9_fp2_t a, const sm9_fp2_t b)
  414. {
  415. return (gmssl_secure_memcmp(a, b, sizeof(sm9_fp2_t)) == 0);
  416. }
  417. void sm9_fp2_copy(sm9_fp2_t r, const sm9_fp2_t a)
  418. {
  419. memcpy(r, a, sizeof(sm9_fp2_t));
  420. }
  421. int sm9_fp2_rand(sm9_fp2_t r)
  422. {
  423. if (sm9_fp_rand(r[0]) != 1
  424. || sm9_fp_rand(r[1]) != 1) {
  425. error_print();
  426. return -1;
  427. }
  428. return 1;
  429. }
  430. void sm9_fp2_to_bytes(const sm9_fp2_t a, uint8_t buf[64])
  431. {
  432. sm9_fp_to_bytes(a[1], buf);
  433. sm9_fp_to_bytes(a[0], buf + 32);
  434. }
  435. int sm9_fp2_from_bytes(sm9_fp2_t r, const uint8_t buf[64])
  436. {
  437. if (sm9_fp_from_bytes(r[1], buf) != 1
  438. || sm9_fp_from_bytes(r[0], buf + 32) != 1) {
  439. error_print();
  440. return -1;
  441. }
  442. return 1;
  443. }
  444. int sm9_fp2_from_hex(sm9_fp2_t r, const char hex[129])
  445. {
  446. if (sm9_fp_from_hex(r[1], hex) != 1
  447. || sm9_fp_from_hex(r[0], hex + 65) != 1) {
  448. error_print();
  449. return -1;
  450. }
  451. /*
  452. if (hex[64] != SM9_HEX_SEP) {
  453. error_print();
  454. return -1;
  455. }
  456. */
  457. return 1;
  458. }
  459. void sm9_fp2_to_hex(const sm9_fp2_t a, char hex[129])
  460. {
  461. sm9_fp_to_hex(a[1], hex);
  462. hex[64] = SM9_HEX_SEP;
  463. sm9_fp_to_hex(a[0], hex + 65);
  464. }
  465. void sm9_fp2_set_fp(sm9_fp2_t r, const sm9_fp_t a)
  466. {
  467. sm9_fp_copy(r[0], a);
  468. sm9_fp_set_zero(r[1]);
  469. }
  470. void sm9_fp2_set(sm9_fp2_t r, const sm9_fp_t a0, const sm9_fp_t a1)
  471. {
  472. sm9_fp_copy(r[0], a0);
  473. sm9_fp_copy(r[1], a1);
  474. }
  475. void sm9_fp2_add(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp2_t b)
  476. {
  477. sm9_fp_add(r[0], a[0], b[0]);
  478. sm9_fp_add(r[1], a[1], b[1]);
  479. }
  480. void sm9_fp2_dbl(sm9_fp2_t r, const sm9_fp2_t a)
  481. {
  482. sm9_fp_dbl(r[0], a[0]);
  483. sm9_fp_dbl(r[1], a[1]);
  484. }
  485. void sm9_fp2_tri(sm9_fp2_t r, const sm9_fp2_t a)
  486. {
  487. sm9_fp_tri(r[0], a[0]);
  488. sm9_fp_tri(r[1], a[1]);
  489. }
  490. void sm9_fp2_sub(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp2_t b)
  491. {
  492. sm9_fp_sub(r[0], a[0], b[0]);
  493. sm9_fp_sub(r[1], a[1], b[1]);
  494. }
  495. void sm9_fp2_neg(sm9_fp2_t r, const sm9_fp2_t a)
  496. {
  497. sm9_fp_neg(r[0], a[0]);
  498. sm9_fp_neg(r[1], a[1]);
  499. }
  500. void sm9_fp2_mul(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp2_t b)
  501. {
  502. sm9_fp_t r0, r1, t;
  503. // r0 = a0 * b0 - 2 * a1 * b1
  504. sm9_fp_mul(r0, a[0], b[0]);
  505. sm9_fp_mul(t, a[1], b[1]);
  506. sm9_fp_dbl(t, t);
  507. sm9_fp_sub(r0, r0, t);
  508. // r1 = a0 * b1 + a1 * b0
  509. sm9_fp_mul(r1, a[0], b[1]);
  510. sm9_fp_mul(t, a[1], b[0]);
  511. sm9_fp_add(r1, r1, t);
  512. sm9_fp_copy(r[0], r0);
  513. sm9_fp_copy(r[1], r1);
  514. }
  515. void sm9_fp2_mul_u(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp2_t b)
  516. {
  517. sm9_fp_t r0, r1, t;
  518. // r0 = -2 * (a0 * b1 + a1 * b0)
  519. sm9_fp_mul(r0, a[0], b[1]);
  520. sm9_fp_mul(t, a[1], b[0]);
  521. sm9_fp_add(r0, r0, t);
  522. sm9_fp_dbl(r0, r0);
  523. sm9_fp_neg(r0, r0);
  524. // r1 = a0 * b0 - 2 * a1 * b1
  525. sm9_fp_mul(r1, a[0], b[0]);
  526. sm9_fp_mul(t, a[1], b[1]);
  527. sm9_fp_dbl(t, t);
  528. sm9_fp_sub(r1, r1, t);
  529. sm9_fp_copy(r[0], r0);
  530. sm9_fp_copy(r[1], r1);
  531. }
  532. void sm9_fp2_mul_fp(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp_t k)
  533. {
  534. sm9_fp_mul(r[0], a[0], k);
  535. sm9_fp_mul(r[1], a[1], k);
  536. }
  537. void sm9_fp2_sqr(sm9_fp2_t r, const sm9_fp2_t a)
  538. {
  539. sm9_fp_t r0, r1, t;
  540. // a0^2 - 2 * a1^2
  541. sm9_fp_sqr(r0, a[0]);
  542. sm9_fp_sqr(t, a[1]);
  543. sm9_fp_dbl(t, t);
  544. sm9_fp_sub(r0, r0, t);
  545. // r1 = 2 * a0 * a1
  546. sm9_fp_mul(r1, a[0], a[1]);
  547. sm9_fp_dbl(r1, r1);
  548. sm9_bn_copy(r[0], r0);
  549. sm9_bn_copy(r[1], r1);
  550. }
  551. void sm9_fp2_sqr_u(sm9_fp2_t r, const sm9_fp2_t a)
  552. {
  553. sm9_fp_t r0, r1, t;
  554. // r0 = -4 * a0 * a1
  555. sm9_fp_mul(r0, a[0], a[1]);
  556. sm9_fp_dbl(r0, r0);
  557. sm9_fp_dbl(r0, r0);
  558. sm9_fp_neg(r0, r0);
  559. // r1 = a0^2 - 2 * a1^2
  560. sm9_fp_sqr(r1, a[0]);
  561. sm9_fp_sqr(t, a[1]);
  562. sm9_fp_dbl(t, t);
  563. sm9_fp_sub(r1, r1, t);
  564. sm9_fp_copy(r[0], r0);
  565. sm9_fp_copy(r[1], r1);
  566. }
  567. void sm9_fp2_inv(sm9_fp2_t r, const sm9_fp2_t a)
  568. {
  569. if (sm9_fp_is_zero(a[0])) {
  570. // r0 = 0
  571. sm9_fp_set_zero(r[0]);
  572. // r1 = -(2 * a1)^-1
  573. sm9_fp_dbl(r[1], a[1]);
  574. sm9_fp_inv(r[1], r[1]);
  575. sm9_fp_neg(r[1], r[1]);
  576. } else if (sm9_fp_is_zero(a[1])) {
  577. /* r1 = 0 */
  578. sm9_fp_set_zero(r[1]);
  579. /* r0 = a0^-1 */
  580. sm9_fp_inv(r[0], a[0]);
  581. } else {
  582. sm9_fp_t k, t;
  583. // k = (a[0]^2 + 2 * a[1]^2)^-1
  584. sm9_fp_sqr(k, a[0]);
  585. sm9_fp_sqr(t, a[1]);
  586. sm9_fp_dbl(t, t);
  587. sm9_fp_add(k, k, t);
  588. sm9_fp_inv(k, k);
  589. // r[0] = a[0] * k
  590. sm9_fp_mul(r[0], a[0], k);
  591. // r[1] = -a[1] * k
  592. sm9_fp_mul(r[1], a[1], k);
  593. sm9_fp_neg(r[1], r[1]);
  594. }
  595. }
  596. void sm9_fp2_div(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp2_t b)
  597. {
  598. sm9_fp2_t t;
  599. sm9_fp2_inv(t, b);
  600. sm9_fp2_mul(r, a, t);
  601. }
  602. void sm9_fp2_div2(sm9_fp2_t r, const sm9_fp2_t a)
  603. {
  604. sm9_fp_div2(r[0], a[0]);
  605. sm9_fp_div2(r[1], a[1]);
  606. }
  607. int sm9_fp2_print(FILE *fp, int fmt, int ind, const char *label, const sm9_fp2_t a)
  608. {
  609. return 1;
  610. }
  611. const sm9_fp4_t SM9_FP4_ZERO = {{{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0}}, {{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0}}};
  612. const sm9_fp4_t SM9_FP4_ONE = {{{1,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0}}, {{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0}}};
  613. const sm9_fp4_t SM9_FP4_U = {{{0,0,0,0,0,0,0,0},{1,0,0,0,0,0,0,0}}, {{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0}}};
  614. const sm9_fp4_t SM9_FP4_V = {{{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0}}, {{1,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0}}};
  615. int sm9_fp4_equ(const sm9_fp4_t a, const sm9_fp4_t b)
  616. {
  617. return (gmssl_secure_memcmp(a, b, sizeof(sm9_fp4_t)) == 0);
  618. }
  619. int sm9_fp4_rand(sm9_fp4_t r)
  620. {
  621. if (sm9_fp2_rand(r[1]) != 1
  622. || sm9_fp2_rand(r[0]) != 1) {
  623. error_print();
  624. return -1;
  625. }
  626. return 1;
  627. }
  628. void sm9_fp4_copy(sm9_fp4_t r, const sm9_fp4_t a)
  629. {
  630. memcpy(r, a, sizeof(sm9_fp4_t));
  631. }
  632. void sm9_fp4_to_bytes(const sm9_fp4_t a, uint8_t buf[128])
  633. {
  634. sm9_fp2_to_bytes(a[1], buf);
  635. sm9_fp2_to_bytes(a[0], buf + 64);
  636. }
  637. int sm9_fp4_from_bytes(sm9_fp4_t r, const uint8_t buf[128])
  638. {
  639. if (sm9_fp2_from_bytes(r[1], buf) != 1
  640. || sm9_fp2_from_bytes(r[0], buf + 64) != 1) {
  641. error_print();
  642. return -1;
  643. }
  644. return 1;
  645. }
  646. int sm9_fp4_from_hex(sm9_fp4_t r, const char hex[65 * 4])
  647. {
  648. if (sm9_fp2_from_hex(r[1], hex) != 1
  649. || hex[129] != SM9_HEX_SEP
  650. || sm9_fp2_from_hex(r[0], hex + 130) != 1) {
  651. error_print();
  652. return -1;
  653. }
  654. return 1;
  655. }
  656. void sm9_fp4_to_hex(const sm9_fp4_t a, char hex[259])
  657. {
  658. sm9_fp2_to_hex(a[1], hex);
  659. hex[129] = SM9_HEX_SEP;
  660. sm9_fp2_to_hex(a[0], hex + 130);
  661. }
  662. void sm9_fp4_set_fp(sm9_fp4_t r, const sm9_fp_t a)
  663. {
  664. sm9_fp2_set_fp(r[0], a);
  665. sm9_fp2_set_zero(r[1]);
  666. }
  667. void sm9_fp4_set_fp2(sm9_fp4_t r, const sm9_fp2_t a)
  668. {
  669. sm9_fp2_copy(r[0], a);
  670. sm9_fp2_set_zero(r[1]);
  671. }
  672. void sm9_fp4_set(sm9_fp4_t r, const sm9_fp2_t a0, const sm9_fp2_t a1)
  673. {
  674. sm9_fp2_copy(r[0], a0);
  675. sm9_fp2_copy(r[1], a1);
  676. }
  677. void sm9_fp4_set_u(sm9_fp4_t r)
  678. {
  679. sm9_fp2_set_u(r[0]);
  680. sm9_fp2_set_zero(r[1]);
  681. }
  682. void sm9_fp4_set_v(sm9_fp4_t r)
  683. {
  684. sm9_fp2_set_zero(r[0]);
  685. sm9_fp2_set_one(r[1]);
  686. }
  687. void sm9_fp4_add(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp4_t b)
  688. {
  689. sm9_fp2_add(r[0], a[0], b[0]);
  690. sm9_fp2_add(r[1], a[1], b[1]);
  691. }
  692. void sm9_fp4_dbl(sm9_fp4_t r, const sm9_fp4_t a)
  693. {
  694. sm9_fp2_dbl(r[0], a[0]);
  695. sm9_fp2_dbl(r[1], a[1]);
  696. }
  697. void sm9_fp4_sub(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp4_t b)
  698. {
  699. sm9_fp2_sub(r[0], a[0], b[0]);
  700. sm9_fp2_sub(r[1], a[1], b[1]);
  701. }
  702. void sm9_fp4_neg(sm9_fp4_t r, const sm9_fp4_t a)
  703. {
  704. sm9_fp2_neg(r[0], a[0]);
  705. sm9_fp2_neg(r[1], a[1]);
  706. }
  707. void sm9_fp4_mul(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp4_t b)
  708. {
  709. sm9_fp2_t r0, r1, t;
  710. sm9_fp2_mul(r0, a[0], b[0]);
  711. sm9_fp2_mul_u(t, a[1], b[1]);
  712. sm9_fp2_add(r0, r0, t);
  713. sm9_fp2_mul(r1, a[0], b[1]);
  714. sm9_fp2_mul(t, a[1], b[0]);
  715. sm9_fp2_add(r1, r1, t);
  716. sm9_fp2_copy(r[0], r0);
  717. sm9_fp2_copy(r[1], r1);
  718. }
  719. void sm9_fp4_mul_fp(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp_t k)
  720. {
  721. sm9_fp2_mul_fp(r[0], a[0], k);
  722. sm9_fp2_mul_fp(r[1], a[1], k);
  723. }
  724. void sm9_fp4_mul_fp2(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp2_t b0)
  725. {
  726. sm9_fp2_mul(r[0], a[0], b0);
  727. sm9_fp2_mul(r[1], a[1], b0);
  728. }
  729. void sm9_fp4_mul_v(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp4_t b)
  730. {
  731. sm9_fp2_t r0, r1, t;
  732. sm9_fp2_mul_u(r0, a[0], b[1]);
  733. sm9_fp2_mul_u(t, a[1], b[0]);
  734. sm9_fp2_add(r0, r0, t);
  735. sm9_fp2_mul(r1, a[0], b[0]);
  736. sm9_fp2_mul_u(t, a[1], b[1]);
  737. sm9_fp2_add(r1, r1, t);
  738. sm9_fp2_copy(r[0], r0);
  739. sm9_fp2_copy(r[1], r1);
  740. }
  741. void sm9_fp4_sqr(sm9_fp4_t r, const sm9_fp4_t a)
  742. {
  743. sm9_fp2_t r0, r1, t;
  744. sm9_fp2_sqr(r0, a[0]);
  745. sm9_fp2_sqr_u(t, a[1]);
  746. sm9_fp2_add(r0, r0, t);
  747. sm9_fp2_mul(r1, a[0], a[1]);
  748. sm9_fp2_dbl(r1, r1);
  749. sm9_fp2_copy(r[0], r0);
  750. sm9_fp2_copy(r[1], r1);
  751. }
  752. void sm9_fp4_sqr_v(sm9_fp4_t r, const sm9_fp4_t a)
  753. {
  754. sm9_fp2_t r0, r1, t;
  755. sm9_fp2_mul_u(t, a[0], a[1]);
  756. sm9_fp2_dbl(r0, t);
  757. sm9_fp2_sqr(r1, a[0]);
  758. sm9_fp2_sqr_u(t, a[1]);
  759. sm9_fp2_add(r1, r1, t);
  760. sm9_fp2_copy(r[0], r0);
  761. sm9_fp2_copy(r[1], r1);
  762. }
  763. void sm9_fp4_inv(sm9_fp4_t r, const sm9_fp4_t a)
  764. {
  765. sm9_fp2_t r0, r1, k;
  766. sm9_fp2_sqr_u(k, a[1]);
  767. sm9_fp2_sqr(r0, a[0]);
  768. sm9_fp2_sub(k, k, r0);
  769. sm9_fp2_inv(k, k);
  770. sm9_fp2_mul(r0, a[0], k);
  771. sm9_fp2_neg(r0, r0);
  772. sm9_fp2_mul(r1, a[1], k);
  773. sm9_fp2_copy(r[0], r0);
  774. sm9_fp2_copy(r[1], r1);
  775. }
  776. void sm9_fp12_copy(sm9_fp12_t r, const sm9_fp12_t a)
  777. {
  778. sm9_fp4_copy(r[0], a[0]);
  779. sm9_fp4_copy(r[1], a[1]);
  780. sm9_fp4_copy(r[2], a[2]);
  781. }
  782. int sm9_fp12_rand(sm9_fp12_t r)
  783. {
  784. if (sm9_fp4_rand(r[0]) != 1
  785. || sm9_fp4_rand(r[1]) != 1
  786. || sm9_fp4_rand(r[2]) != 1) {
  787. error_print();
  788. return -1;
  789. }
  790. return 1;
  791. }
  792. void sm9_fp12_set_zero(sm9_fp12_t r)
  793. {
  794. sm9_fp4_set_zero(r[0]);
  795. sm9_fp4_set_zero(r[1]);
  796. sm9_fp4_set_zero(r[2]);
  797. }
  798. void sm9_fp12_set_one(sm9_fp12_t r)
  799. {
  800. sm9_fp4_set_one(r[0]);
  801. sm9_fp4_set_zero(r[1]);
  802. sm9_fp4_set_zero(r[2]);
  803. }
  804. int sm9_fp12_is_one(const sm9_fp12_t a)
  805. {
  806. return sm9_fp4_is_one(a[0])
  807. && sm9_fp4_is_zero(a[1])
  808. && sm9_fp4_is_zero(a[2]);
  809. }
  810. int sm9_fp12_is_zero(const sm9_fp12_t a)
  811. {
  812. return sm9_fp4_is_zero(a[0])
  813. && sm9_fp4_is_zero(a[1])
  814. && sm9_fp4_is_zero(a[2]);
  815. }
  816. int sm9_fp12_from_hex(sm9_fp12_t r, const char hex[65 * 12 - 1])
  817. {
  818. if (sm9_fp4_from_hex(r[2], hex) != 1
  819. || hex[65 * 4 - 1] != SM9_HEX_SEP
  820. || sm9_fp4_from_hex(r[1], hex + 65 * 4) != 1
  821. || hex[65 * 4 - 1] != SM9_HEX_SEP
  822. || sm9_fp4_from_hex(r[0], hex + 65 * 8) != 1) {
  823. error_print();
  824. return -1;
  825. }
  826. return 1;
  827. }
  828. void sm9_fp12_to_hex(const sm9_fp12_t a, char hex[65 * 12 - 1])
  829. {
  830. sm9_fp4_to_hex(a[2], hex);
  831. hex[65 * 4 - 1] = SM9_HEX_SEP;
  832. sm9_fp4_to_hex(a[1], hex + 65 * 4);
  833. hex[65 * 8 - 1] = SM9_HEX_SEP;
  834. sm9_fp4_to_hex(a[0], hex + 65 * 8);
  835. }
  836. void sm9_fp12_print(const char *prefix, const sm9_fp12_t a)
  837. {
  838. char hex[65 * 12];
  839. sm9_fp12_to_hex(a, hex);
  840. printf("%s\n%s\n", prefix, hex);
  841. }
  842. void sm9_fp12_set(sm9_fp12_t r, const sm9_fp4_t a0, const sm9_fp4_t a1, const sm9_fp4_t a2)
  843. {
  844. sm9_fp4_copy(r[0], a0);
  845. sm9_fp4_copy(r[1], a1);
  846. sm9_fp4_copy(r[2], a2);
  847. }
  848. void sm9_fp12_set_fp(sm9_fp12_t r, const sm9_fp_t a)
  849. {
  850. sm9_fp4_set_fp(r[0], a);
  851. sm9_fp4_set_zero(r[1]);
  852. sm9_fp4_set_zero(r[2]);
  853. }
  854. void sm9_fp12_set_fp2(sm9_fp12_t r, const sm9_fp2_t a)
  855. {
  856. sm9_fp4_set_fp2(r[0], a);
  857. sm9_fp4_set_zero(r[1]);
  858. sm9_fp4_set_zero(r[2]);
  859. }
  860. void sm9_fp12_set_fp4(sm9_fp12_t r, const sm9_fp4_t a)
  861. {
  862. sm9_fp4_copy(r[0], a);
  863. sm9_fp4_set_zero(r[1]);
  864. sm9_fp4_set_zero(r[2]);
  865. }
  866. void sm9_fp12_set_u(sm9_fp12_t r)
  867. {
  868. sm9_fp4_set_u(r[0]);
  869. sm9_fp4_set_zero(r[1]);
  870. sm9_fp4_set_zero(r[2]);
  871. }
  872. void sm9_fp12_set_v(sm9_fp12_t r)
  873. {
  874. sm9_fp4_set_v(r[0]);
  875. sm9_fp4_set_zero(r[1]);
  876. sm9_fp4_set_zero(r[2]);
  877. }
  878. void sm9_fp12_set_w(sm9_fp12_t r)
  879. {
  880. sm9_fp4_set_zero(r[0]);
  881. sm9_fp4_set_one(r[1]);
  882. sm9_fp4_set_zero(r[2]);
  883. }
  884. void sm9_fp12_set_w_sqr(sm9_fp12_t r)
  885. {
  886. sm9_fp4_set_zero(r[0]);
  887. sm9_fp4_set_zero(r[1]);
  888. sm9_fp4_set_one(r[2]);
  889. }
  890. int sm9_fp12_equ(const sm9_fp12_t a, const sm9_fp12_t b)
  891. {
  892. return sm9_fp4_equ(a[0], b[0])
  893. && sm9_fp4_equ(a[1], b[1])
  894. && sm9_fp4_equ(a[2], b[2]);
  895. }
  896. void sm9_fp12_add(sm9_fp12_t r, const sm9_fp12_t a, const sm9_fp12_t b)
  897. {
  898. sm9_fp4_add(r[0], a[0], b[0]);
  899. sm9_fp4_add(r[1], a[1], b[1]);
  900. sm9_fp4_add(r[2], a[2], b[2]);
  901. }
  902. void sm9_fp12_dbl(sm9_fp12_t r, const sm9_fp12_t a)
  903. {
  904. sm9_fp4_dbl(r[0], a[0]);
  905. sm9_fp4_dbl(r[1], a[1]);
  906. sm9_fp4_dbl(r[2], a[2]);
  907. }
  908. void sm9_fp12_tri(sm9_fp12_t r, const sm9_fp12_t a)
  909. {
  910. sm9_fp12_t t;
  911. sm9_fp12_dbl(t, a);
  912. sm9_fp12_add(r, t, a);
  913. }
  914. void sm9_fp12_sub(sm9_fp12_t r, const sm9_fp12_t a, const sm9_fp12_t b)
  915. {
  916. sm9_fp4_sub(r[0], a[0], b[0]);
  917. sm9_fp4_sub(r[1], a[1], b[1]);
  918. sm9_fp4_sub(r[2], a[2], b[2]);
  919. }
  920. void sm9_fp12_neg(sm9_fp12_t r, const sm9_fp12_t a)
  921. {
  922. sm9_fp4_neg(r[0], a[0]);
  923. sm9_fp4_neg(r[1], a[1]);
  924. sm9_fp4_neg(r[2], a[2]);
  925. }
  926. void sm9_fp12_mul(sm9_fp12_t r, const sm9_fp12_t a, const sm9_fp12_t b)
  927. {
  928. sm9_fp4_t r0, r1, r2, t;
  929. sm9_fp4_mul(r0, a[0], b[0]);
  930. sm9_fp4_mul_v(t, a[1], b[2]);
  931. sm9_fp4_add(r0, r0, t);
  932. sm9_fp4_mul_v(t, a[2], b[1]);
  933. sm9_fp4_add(r0, r0, t);
  934. sm9_fp4_mul(r1, a[0], b[1]);
  935. sm9_fp4_mul(t, a[1], b[0]);
  936. sm9_fp4_add(r1, r1, t);
  937. sm9_fp4_mul_v(t, a[2], b[2]);
  938. sm9_fp4_add(r1, r1, t);
  939. sm9_fp4_mul(r2, a[0], b[2]);
  940. sm9_fp4_mul(t, a[1], b[1]);
  941. sm9_fp4_add(r2, r2, t);
  942. sm9_fp4_mul(t, a[2], b[0]);
  943. sm9_fp4_add(r2, r2, t);
  944. sm9_fp4_copy(r[0], r0);
  945. sm9_fp4_copy(r[1], r1);
  946. sm9_fp4_copy(r[2], r2);
  947. }
  948. void sm9_fp12_sqr(sm9_fp12_t r, const sm9_fp12_t a)
  949. {
  950. sm9_fp4_t r0, r1, r2, t;
  951. sm9_fp4_sqr(r0, a[0]);
  952. sm9_fp4_mul_v(t, a[1], a[2]);
  953. sm9_fp4_dbl(t, t);
  954. sm9_fp4_add(r0, r0, t);
  955. sm9_fp4_mul(r1, a[0], a[1]);
  956. sm9_fp4_dbl(r1, r1);
  957. sm9_fp4_sqr_v(t, a[2]);
  958. sm9_fp4_add(r1, r1, t);
  959. sm9_fp4_mul(r2, a[0], a[2]);
  960. sm9_fp4_dbl(r2, r2);
  961. sm9_fp4_sqr(t, a[1]);
  962. sm9_fp4_add(r2, r2, t);
  963. sm9_fp4_copy(r[0], r0);
  964. sm9_fp4_copy(r[1], r1);
  965. sm9_fp4_copy(r[2], r2);
  966. }
  967. void sm9_fp12_inv(sm9_fp12_t r, const sm9_fp12_t a)
  968. {
  969. if (sm9_fp4_is_zero(a[2])) {
  970. sm9_fp4_t k, t;
  971. sm9_fp4_sqr(k, a[0]);
  972. sm9_fp4_mul(k, k, a[0]);
  973. sm9_fp4_sqr_v(t, a[1]);
  974. sm9_fp4_mul(t, t, a[1]);
  975. sm9_fp4_add(k, k, t);
  976. sm9_fp4_inv(k, k);
  977. sm9_fp4_sqr(r[2], a[1]);
  978. sm9_fp4_mul(r[2], r[2], k);
  979. sm9_fp4_mul(r[1], a[0], a[1]);
  980. sm9_fp4_mul(r[1], r[1], k);
  981. sm9_fp4_neg(r[1], r[1]);
  982. sm9_fp4_sqr(r[0], a[0]);
  983. sm9_fp4_mul(r[0], r[0], k);
  984. } else {
  985. sm9_fp4_t t0, t1, t2, t3;
  986. sm9_fp4_sqr(t0, a[1]);
  987. sm9_fp4_mul(t1, a[0], a[2]);
  988. sm9_fp4_sub(t0, t0, t1);
  989. sm9_fp4_mul(t1, a[0], a[1]);
  990. sm9_fp4_sqr_v(t2, a[2]);
  991. sm9_fp4_sub(t1, t1, t2);
  992. sm9_fp4_sqr(t2, a[0]);
  993. sm9_fp4_mul_v(t3, a[1], a[2]);
  994. sm9_fp4_sub(t2, t2, t3);
  995. sm9_fp4_sqr(t3, t1);
  996. sm9_fp4_mul(r[0], t0, t2);
  997. sm9_fp4_sub(t3, t3, r[0]);
  998. sm9_fp4_inv(t3, t3);
  999. sm9_fp4_mul(t3, a[2], t3);
  1000. sm9_fp4_mul(r[0], t2, t3);
  1001. sm9_fp4_mul(r[1], t1, t3);
  1002. sm9_fp4_neg(r[1], r[1]);
  1003. sm9_fp4_mul(r[2], t0, t3);
  1004. }
  1005. }
  1006. void sm9_fp12_pow(sm9_fp12_t r, const sm9_fp12_t a, const sm9_bn_t k)
  1007. {
  1008. char kbits[257];
  1009. sm9_fp12_t t;
  1010. int i;
  1011. assert(sm9_bn_cmp(k, SM9_P_MINUS_ONE) < 0);
  1012. sm9_fp12_set_zero(t);
  1013. sm9_bn_to_bits(k, kbits);
  1014. sm9_fp12_set_one(t);
  1015. for (i = 0; i < 256; i++) {
  1016. sm9_fp12_sqr(t, t);
  1017. if (kbits[i] == '1') {
  1018. sm9_fp12_mul(t, t, a);
  1019. }
  1020. }
  1021. sm9_fp12_copy(r, t);
  1022. }
  1023. void sm9_fp2_conjugate(sm9_fp2_t r, const sm9_fp2_t a)
  1024. {
  1025. sm9_fp_copy(r[0], a[0]);
  1026. sm9_fp_neg (r[1], a[1]);
  1027. }
  1028. void sm9_fp2_frobenius(sm9_fp2_t r, const sm9_fp2_t a)
  1029. {
  1030. sm9_fp2_conjugate(r, a);
  1031. }
  1032. // beta = 0x6c648de5dc0a3f2cf55acc93ee0baf159f9d411806dc5177f5b21fd3da24d011
  1033. // alpha1 = 0x3f23ea58e5720bdb843c6cfa9c08674947c5c86e0ddd04eda91d8354377b698b
  1034. // alpha2 = 0xf300000002a3a6f2780272354f8b78f4d5fc11967be65334
  1035. // alpha3 = 0x6c648de5dc0a3f2cf55acc93ee0baf159f9d411806dc5177f5b21fd3da24d011
  1036. // alpha4 = 0xf300000002a3a6f2780272354f8b78f4d5fc11967be65333
  1037. // alpha5 = 0x2d40a38cf6983351711e5f99520347cc57d778a9f8ff4c8a4c949c7fa2a96686
  1038. static const sm9_fp2_t SM9_BETA = {{0xda24d011, 0xf5b21fd3, 0x06dc5177, 0x9f9d4118, 0xee0baf15, 0xf55acc93, 0xdc0a3f2c, 0x6c648de5}, {0}};
  1039. static const sm9_fp_t SM9_ALPHA1 = {0x377b698b, 0xa91d8354, 0x0ddd04ed, 0x47c5c86e, 0x9c086749, 0x843c6cfa, 0xe5720bdb, 0x3f23ea58};
  1040. static const sm9_fp_t SM9_ALPHA2 = {0x7be65334, 0xd5fc1196, 0x4f8b78f4, 0x78027235, 0x02a3a6f2, 0xf3000000, 0x0, 0x0 };
  1041. static const sm9_fp_t SM9_ALPHA3 = {0xda24d011, 0xf5b21fd3, 0x06dc5177, 0x9f9d4118, 0xee0baf15, 0xf55acc93, 0xdc0a3f2c, 0x6c648de5};
  1042. static const sm9_fp_t SM9_ALPHA4 = {0x7be65333, 0xd5fc1196, 0x4f8b78f4, 0x78027235, 0x02a3a6f2, 0xf3000000, 0x0, 0x0 };
  1043. static const sm9_fp_t SM9_ALPHA5 = {0xa2a96686, 0x4c949c7f, 0xf8ff4c8a, 0x57d778a9, 0x520347cc, 0x711e5f99, 0xf6983351, 0x2d40a38c};
  1044. void sm9_fp4_frobenius(sm9_fp4_t r, const sm9_fp4_t a)
  1045. {
  1046. sm9_fp2_conjugate(r[0], a[0]);
  1047. sm9_fp2_conjugate(r[1], a[1]);
  1048. sm9_fp2_mul(r[1], r[1], SM9_BETA);
  1049. }
  1050. void sm9_fp4_conjugate(sm9_fp4_t r, const sm9_fp4_t a)
  1051. {
  1052. sm9_fp2_copy(r[0], a[0]);
  1053. sm9_fp2_neg(r[1], a[1]);
  1054. }
  1055. void sm9_fp4_frobenius2(sm9_fp4_t r, const sm9_fp4_t a)
  1056. {
  1057. sm9_fp4_conjugate(r, a);
  1058. }
  1059. void sm9_fp4_frobenius3(sm9_fp4_t r, const sm9_fp4_t a)
  1060. {
  1061. sm9_fp2_conjugate(r[0], a[0]);
  1062. sm9_fp2_conjugate(r[1], a[1]);
  1063. sm9_fp2_mul(r[1], r[1], SM9_BETA);
  1064. sm9_fp2_neg(r[1], r[1]);
  1065. }
  1066. void sm9_fp12_frobenius(sm9_fp12_t r, const sm9_fp12_t x)
  1067. {
  1068. const sm9_fp2_t *xa = x[0];
  1069. const sm9_fp2_t *xb = x[1];
  1070. const sm9_fp2_t *xc = x[2];
  1071. sm9_fp4_t ra;
  1072. sm9_fp4_t rb;
  1073. sm9_fp4_t rc;
  1074. sm9_fp2_conjugate(ra[0], xa[0]);
  1075. sm9_fp2_conjugate(ra[1], xa[1]);
  1076. sm9_fp2_mul_fp(ra[1], ra[1], SM9_ALPHA3);
  1077. sm9_fp2_conjugate(rb[0], xb[0]);
  1078. sm9_fp2_mul_fp(rb[0], rb[0], SM9_ALPHA1);
  1079. sm9_fp2_conjugate(rb[1], xb[1]);
  1080. sm9_fp2_mul_fp(rb[1], rb[1], SM9_ALPHA4);
  1081. sm9_fp2_conjugate(rc[0], xc[0]);
  1082. sm9_fp2_mul_fp(rc[0], rc[0], SM9_ALPHA2);
  1083. sm9_fp2_conjugate(rc[1], xc[1]);
  1084. sm9_fp2_mul_fp(rc[1], rc[1], SM9_ALPHA5);
  1085. sm9_fp12_set(r, ra, rb, rc);
  1086. }
  1087. void sm9_fp12_frobenius2(sm9_fp12_t r, const sm9_fp12_t x)
  1088. {
  1089. sm9_fp4_t a;
  1090. sm9_fp4_t b;
  1091. sm9_fp4_t c;
  1092. sm9_fp4_conjugate(a, x[0]);
  1093. sm9_fp4_conjugate(b, x[1]);
  1094. sm9_fp4_mul_fp(b, b, SM9_ALPHA2);
  1095. sm9_fp4_conjugate(c, x[2]);
  1096. sm9_fp4_mul_fp(c, c, SM9_ALPHA4);
  1097. sm9_fp4_copy(r[0], a);
  1098. sm9_fp4_copy(r[1], b);
  1099. sm9_fp4_copy(r[2], c);
  1100. }
  1101. void sm9_fp12_frobenius3(sm9_fp12_t r, const sm9_fp12_t x)
  1102. {
  1103. const sm9_fp2_t *xa = x[0];
  1104. const sm9_fp2_t *xb = x[1];
  1105. const sm9_fp2_t *xc = x[2];
  1106. sm9_fp4_t ra;
  1107. sm9_fp4_t rb;
  1108. sm9_fp4_t rc;
  1109. sm9_fp2_conjugate(ra[0], xa[0]);
  1110. sm9_fp2_conjugate(ra[1], xa[1]);
  1111. sm9_fp2_mul(ra[1], ra[1], SM9_BETA);
  1112. sm9_fp2_neg(ra[1], ra[1]);
  1113. sm9_fp2_conjugate(rb[0], xb[0]);
  1114. sm9_fp2_mul(rb[0], rb[0], SM9_BETA);
  1115. sm9_fp2_conjugate(rb[1], xb[1]);
  1116. sm9_fp2_conjugate(rc[0], xc[0]);
  1117. sm9_fp2_neg(rc[0], rc[0]);
  1118. sm9_fp2_conjugate(rc[1], xc[1]);
  1119. sm9_fp2_mul(rc[1], rc[1], SM9_BETA);
  1120. sm9_fp4_copy(r[0], ra);
  1121. sm9_fp4_copy(r[1], rb);
  1122. sm9_fp4_copy(r[2], rc);
  1123. }
  1124. void sm9_fp12_frobenius6(sm9_fp12_t r, const sm9_fp12_t x)
  1125. {
  1126. sm9_fp4_t a;
  1127. sm9_fp4_t b;
  1128. sm9_fp4_t c;
  1129. sm9_fp4_copy(a, x[0]);
  1130. sm9_fp4_copy(b, x[1]);
  1131. sm9_fp4_copy(c, x[2]);
  1132. sm9_fp4_conjugate(a, a);
  1133. sm9_fp4_conjugate(b, b);
  1134. sm9_fp4_neg(b, b);
  1135. sm9_fp4_conjugate(c, c);
  1136. sm9_fp4_copy(r[0], a);
  1137. sm9_fp4_copy(r[1], b);
  1138. sm9_fp4_copy(r[2], c);
  1139. }
  1140. void sm9_point_from_hex(SM9_POINT *R, const char hex[65 * 2])
  1141. {
  1142. sm9_bn_from_hex(R->X, hex);
  1143. sm9_bn_from_hex(R->Y, hex + 65);
  1144. sm9_bn_set_one(R->Z);
  1145. }
  1146. int sm9_point_is_at_infinity(const SM9_POINT *P) {
  1147. return sm9_fp_is_zero(P->Z);
  1148. }
  1149. void sm9_point_set_infinity(SM9_POINT *R) {
  1150. sm9_fp_set_one(R->X);
  1151. sm9_fp_set_one(R->Y);
  1152. sm9_fp_set_zero(R->Z);
  1153. }
  1154. void sm9_point_copy(SM9_POINT *R, const SM9_POINT *P)
  1155. {
  1156. *R = *P;
  1157. }
  1158. void sm9_point_get_xy(const SM9_POINT *P, sm9_fp_t x, sm9_fp_t y)
  1159. {
  1160. sm9_fp_t z_inv;
  1161. assert(!sm9_fp_is_zero(P->Z));
  1162. if (sm9_fp_is_one(P->Z)) {
  1163. sm9_fp_copy(x, P->X);
  1164. sm9_fp_copy(y, P->Y);
  1165. }
  1166. sm9_fp_inv(z_inv, P->Z);
  1167. if (y)
  1168. sm9_fp_mul(y, P->Y, z_inv);
  1169. sm9_fp_sqr(z_inv, z_inv);
  1170. sm9_fp_mul(x, P->X, z_inv);
  1171. if (y)
  1172. sm9_fp_mul(y, y, z_inv);
  1173. }
  1174. int sm9_point_equ(const SM9_POINT *P, const SM9_POINT *Q)
  1175. {
  1176. sm9_fp_t t1, t2, t3, t4;
  1177. sm9_fp_sqr(t1, P->Z);
  1178. sm9_fp_sqr(t2, Q->Z);
  1179. sm9_fp_mul(t3, P->X, t2);
  1180. sm9_fp_mul(t4, Q->X, t1);
  1181. if (!sm9_fp_equ(t3, t4)) {
  1182. return 0;
  1183. }
  1184. sm9_fp_mul(t1, t1, P->Z);
  1185. sm9_fp_mul(t2, t2, Q->Z);
  1186. sm9_fp_mul(t3, P->Y, t2);
  1187. sm9_fp_mul(t4, Q->Y, t1);
  1188. return sm9_fp_equ(t3, t4);
  1189. }
  1190. int sm9_point_is_on_curve(const SM9_POINT *P)
  1191. {
  1192. sm9_fp_t t0, t1, t2;
  1193. if (sm9_fp_is_one(P->Z)) {
  1194. sm9_fp_sqr(t0, P->Y);
  1195. sm9_fp_sqr(t1, P->X);
  1196. sm9_fp_mul(t1, t1, P->X);
  1197. sm9_fp_add(t1, t1, SM9_FIVE);
  1198. } else {
  1199. sm9_fp_sqr(t0, P->X);
  1200. sm9_fp_mul(t0, t0, P->X);
  1201. sm9_fp_sqr(t1, P->Z);
  1202. sm9_fp_sqr(t2, t1);
  1203. sm9_fp_mul(t1, t1, t2);
  1204. sm9_fp_mul(t1, t1, SM9_FIVE);
  1205. sm9_fp_add(t1, t0, t1);
  1206. sm9_fp_sqr(t0, P->Y);
  1207. }
  1208. if (sm9_fp_equ(t0, t1) != 1) {
  1209. error_print();
  1210. return 0;
  1211. }
  1212. return 1;
  1213. }
  1214. void sm9_point_dbl(SM9_POINT *R, const SM9_POINT *P)
  1215. {
  1216. const uint64_t *X1 = P->X;
  1217. const uint64_t *Y1 = P->Y;
  1218. const uint64_t *Z1 = P->Z;
  1219. sm9_fp_t X3, Y3, Z3, T1, T2, T3;
  1220. if (sm9_point_is_at_infinity(P)) {
  1221. sm9_point_copy(R, P);
  1222. return;
  1223. }
  1224. sm9_fp_sqr(T2, X1);
  1225. sm9_fp_tri(T2, T2);
  1226. sm9_fp_dbl(Y3, Y1);
  1227. sm9_fp_mul(Z3, Y3, Z1);
  1228. sm9_fp_sqr(Y3, Y3);
  1229. sm9_fp_mul(T3, Y3, X1);
  1230. sm9_fp_sqr(Y3, Y3);
  1231. sm9_fp_div2(Y3, Y3);
  1232. sm9_fp_sqr(X3, T2);
  1233. sm9_fp_dbl(T1, T3);
  1234. sm9_fp_sub(X3, X3, T1);
  1235. sm9_fp_sub(T1, T3, X3);
  1236. sm9_fp_mul(T1, T1, T2);
  1237. sm9_fp_sub(Y3, T1, Y3);
  1238. sm9_fp_copy(R->X, X3);
  1239. sm9_fp_copy(R->Y, Y3);
  1240. sm9_fp_copy(R->Z, Z3);
  1241. }
  1242. void sm9_point_add(SM9_POINT *R, const SM9_POINT *P, const SM9_POINT *Q)
  1243. {
  1244. sm9_fp_t x;
  1245. sm9_fp_t y;
  1246. sm9_point_get_xy(Q, x, y);
  1247. const uint64_t *X1 = P->X;
  1248. const uint64_t *Y1 = P->Y;
  1249. const uint64_t *Z1 = P->Z;
  1250. const uint64_t *x2 = x;
  1251. const uint64_t *y2 = y;
  1252. sm9_fp_t X3, Y3, Z3, T1, T2, T3, T4;
  1253. if (sm9_point_is_at_infinity(Q)) {
  1254. sm9_point_copy(R, P);
  1255. return;
  1256. }
  1257. if (sm9_point_is_at_infinity(P)) {
  1258. sm9_point_copy(R, Q);
  1259. return;
  1260. }
  1261. sm9_fp_sqr(T1, Z1);
  1262. sm9_fp_mul(T2, T1, Z1);
  1263. sm9_fp_mul(T1, T1, x2);
  1264. sm9_fp_mul(T2, T2, y2);
  1265. sm9_fp_sub(T1, T1, X1);
  1266. sm9_fp_sub(T2, T2, Y1);
  1267. if (sm9_fp_is_zero(T1)) {
  1268. if (sm9_fp_is_zero(T2)) {
  1269. sm9_point_dbl(R, Q);
  1270. return;
  1271. } else {
  1272. sm9_point_set_infinity(R);
  1273. return;
  1274. }
  1275. }
  1276. sm9_fp_mul(Z3, Z1, T1);
  1277. sm9_fp_sqr(T3, T1);
  1278. sm9_fp_mul(T4, T3, T1);
  1279. sm9_fp_mul(T3, T3, X1);
  1280. sm9_fp_dbl(T1, T3);
  1281. sm9_fp_sqr(X3, T2);
  1282. sm9_fp_sub(X3, X3, T1);
  1283. sm9_fp_sub(X3, X3, T4);
  1284. sm9_fp_sub(T3, T3, X3);
  1285. sm9_fp_mul(T3, T3, T2);
  1286. sm9_fp_mul(T4, T4, Y1);
  1287. sm9_fp_sub(Y3, T3, T4);
  1288. sm9_fp_copy(R->X, X3);
  1289. sm9_fp_copy(R->Y, Y3);
  1290. sm9_fp_copy(R->Z, Z3);
  1291. }
  1292. void sm9_point_neg(SM9_POINT *R, const SM9_POINT *P)
  1293. {
  1294. sm9_fp_copy(R->X, P->X);
  1295. sm9_fp_neg(R->Y, P->Y);
  1296. sm9_fp_copy(R->Z, P->Z);
  1297. }
  1298. void sm9_point_sub(SM9_POINT *R, const SM9_POINT *P, const SM9_POINT *Q)
  1299. {
  1300. SM9_POINT _T, *T = &_T;
  1301. sm9_point_neg(T, Q);
  1302. sm9_point_add(R, P, T);
  1303. }
  1304. void sm9_point_mul(SM9_POINT *R, const sm9_bn_t k, const SM9_POINT *P)
  1305. {
  1306. char kbits[257];
  1307. SM9_POINT _Q, *Q = &_Q;
  1308. int i;
  1309. sm9_bn_to_bits(k, kbits);
  1310. sm9_point_set_infinity(Q);
  1311. for (i = 0; i < 256; i++) {
  1312. sm9_point_dbl(Q, Q);
  1313. if (kbits[i] == '1') {
  1314. sm9_point_add(Q, Q, P);
  1315. }
  1316. }
  1317. sm9_point_copy(R, Q);
  1318. }
  1319. void sm9_point_mul_generator(SM9_POINT *R, const sm9_bn_t k)
  1320. {
  1321. sm9_point_mul(R, k, SM9_P1);
  1322. }
  1323. int sm9_point_print(FILE *fp, int fmt, int ind, const char *label, const SM9_POINT *P)
  1324. {
  1325. uint8_t buf[65];
  1326. sm9_point_to_uncompressed_octets(P, buf);
  1327. format_bytes(fp, fmt, ind, label, buf, sizeof(buf));
  1328. return 1;
  1329. }
  1330. int sm9_twist_point_print(FILE *fp, int fmt, int ind, const char *label, const SM9_TWIST_POINT *P)
  1331. {
  1332. uint8_t buf[129];
  1333. sm9_twist_point_to_uncompressed_octets(P, buf);
  1334. format_bytes(fp, fmt, ind, label, buf, sizeof(buf));
  1335. return 1;
  1336. }
  1337. void sm9_twist_point_from_hex(SM9_TWIST_POINT *R, const char hex[65 * 4])
  1338. {
  1339. sm9_fp2_from_hex(R->X, hex);
  1340. sm9_fp2_from_hex(R->Y, hex + 65 * 2);
  1341. sm9_fp2_set_one(R->Z);
  1342. }
  1343. int sm9_twist_point_is_at_infinity(const SM9_TWIST_POINT *P)
  1344. {
  1345. return sm9_fp2_is_zero(P->Z);
  1346. }
  1347. void sm9_twist_point_set_infinity(SM9_TWIST_POINT *R)
  1348. {
  1349. sm9_fp2_set_one(R->X);
  1350. sm9_fp2_set_one(R->Y);
  1351. sm9_fp2_set_zero(R->Z);
  1352. }
  1353. void sm9_twist_point_get_xy(const SM9_TWIST_POINT *P, sm9_fp2_t x, sm9_fp2_t y)
  1354. {
  1355. sm9_fp2_t z_inv;
  1356. assert(!sm9_fp2_is_zero(P->Z));
  1357. if (sm9_fp2_is_one(P->Z)) {
  1358. sm9_fp2_copy(x, P->X);
  1359. sm9_fp2_copy(y, P->Y);
  1360. }
  1361. sm9_fp2_inv(z_inv, P->Z);
  1362. if (y)
  1363. sm9_fp2_mul(y, P->Y, z_inv);
  1364. sm9_fp2_sqr(z_inv, z_inv);
  1365. sm9_fp2_mul(x, P->X, z_inv);
  1366. if (y)
  1367. sm9_fp2_mul(y, y, z_inv);
  1368. }
  1369. int sm9_twist_point_equ(const SM9_TWIST_POINT *P, const SM9_TWIST_POINT *Q)
  1370. {
  1371. sm9_fp2_t t1, t2, t3, t4;
  1372. sm9_fp2_sqr(t1, P->Z);
  1373. sm9_fp2_sqr(t2, Q->Z);
  1374. sm9_fp2_mul(t3, P->X, t2);
  1375. sm9_fp2_mul(t4, Q->X, t1);
  1376. if (!sm9_fp2_equ(t3, t4)) {
  1377. return 0;
  1378. }
  1379. sm9_fp2_mul(t1, t1, P->Z);
  1380. sm9_fp2_mul(t2, t2, Q->Z);
  1381. sm9_fp2_mul(t3, P->Y, t2);
  1382. sm9_fp2_mul(t4, Q->Y, t1);
  1383. return sm9_fp2_equ(t3, t4);
  1384. }
  1385. int sm9_twist_point_is_on_curve(const SM9_TWIST_POINT *P)
  1386. {
  1387. sm9_fp2_t t0, t1, t2;
  1388. if (sm9_fp2_is_one(P->Z)) {
  1389. sm9_fp2_sqr(t0, P->Y);
  1390. sm9_fp2_sqr(t1, P->X);
  1391. sm9_fp2_mul(t1, t1, P->X);
  1392. sm9_fp2_add(t1, t1, SM9_FP2_5U);
  1393. } else {
  1394. sm9_fp2_sqr(t0, P->X);
  1395. sm9_fp2_mul(t0, t0, P->X);
  1396. sm9_fp2_sqr(t1, P->Z);
  1397. sm9_fp2_sqr(t2, t1);
  1398. sm9_fp2_mul(t1, t1, t2);
  1399. sm9_fp2_mul(t1, t1, SM9_FP2_5U);
  1400. sm9_fp2_add(t1, t0, t1);
  1401. sm9_fp2_sqr(t0, P->Y);
  1402. }
  1403. return sm9_fp2_equ(t0, t1);
  1404. }
  1405. void sm9_twist_point_neg(SM9_TWIST_POINT *R, const SM9_TWIST_POINT *P)
  1406. {
  1407. sm9_fp2_copy(R->X, P->X);
  1408. sm9_fp2_neg(R->Y, P->Y);
  1409. sm9_fp2_copy(R->Z, P->Z);
  1410. }
  1411. void sm9_twist_point_dbl(SM9_TWIST_POINT *R, const SM9_TWIST_POINT *P)
  1412. {
  1413. const sm9_fp_t *X1 = P->X;
  1414. const sm9_fp_t *Y1 = P->Y;
  1415. const sm9_fp_t *Z1 = P->Z;
  1416. sm9_fp2_t X3, Y3, Z3, T1, T2, T3;
  1417. if (sm9_twist_point_is_at_infinity(P)) {
  1418. sm9_twist_point_copy(R, P);
  1419. return;
  1420. }
  1421. sm9_fp2_sqr(T2, X1);
  1422. sm9_fp2_tri(T2, T2);
  1423. sm9_fp2_dbl(Y3, Y1);
  1424. sm9_fp2_mul(Z3, Y3, Z1);
  1425. sm9_fp2_sqr(Y3, Y3);
  1426. sm9_fp2_mul(T3, Y3, X1);
  1427. sm9_fp2_sqr(Y3, Y3);
  1428. sm9_fp2_div2(Y3, Y3);
  1429. sm9_fp2_sqr(X3, T2);
  1430. sm9_fp2_dbl(T1, T3);
  1431. sm9_fp2_sub(X3, X3, T1);
  1432. sm9_fp2_sub(T1, T3, X3);
  1433. sm9_fp2_mul(T1, T1, T2);
  1434. sm9_fp2_sub(Y3, T1, Y3);
  1435. sm9_fp2_copy(R->X, X3);
  1436. sm9_fp2_copy(R->Y, Y3);
  1437. sm9_fp2_copy(R->Z, Z3);
  1438. }
  1439. void sm9_twist_point_add(SM9_TWIST_POINT *R, const SM9_TWIST_POINT *P, const SM9_TWIST_POINT *Q)
  1440. {
  1441. const sm9_fp_t *X1 = P->X;
  1442. const sm9_fp_t *Y1 = P->Y;
  1443. const sm9_fp_t *Z1 = P->Z;
  1444. const sm9_fp_t *x2 = Q->X;
  1445. const sm9_fp_t *y2 = Q->Y;
  1446. sm9_fp2_t X3, Y3, Z3, T1, T2, T3, T4;
  1447. if (sm9_twist_point_is_at_infinity(Q)) {
  1448. sm9_twist_point_copy(R, P);
  1449. return;
  1450. }
  1451. if (sm9_twist_point_is_at_infinity(P)) {
  1452. sm9_twist_point_copy(R, Q);
  1453. return;
  1454. }
  1455. sm9_fp2_sqr(T1, Z1);
  1456. sm9_fp2_mul(T2, T1, Z1);
  1457. sm9_fp2_mul(T1, T1, x2);
  1458. sm9_fp2_mul(T2, T2, y2);
  1459. sm9_fp2_sub(T1, T1, X1);
  1460. sm9_fp2_sub(T2, T2, Y1);
  1461. if (sm9_fp2_is_zero(T1)) {
  1462. if (sm9_fp2_is_zero(T2)) {
  1463. sm9_twist_point_dbl(R, Q);
  1464. return;
  1465. } else {
  1466. sm9_twist_point_set_infinity(R);
  1467. return;
  1468. }
  1469. }
  1470. sm9_fp2_mul(Z3, Z1, T1);
  1471. sm9_fp2_sqr(T3, T1);
  1472. sm9_fp2_mul(T4, T3, T1);
  1473. sm9_fp2_mul(T3, T3, X1);
  1474. sm9_fp2_dbl(T1, T3);
  1475. sm9_fp2_sqr(X3, T2);
  1476. sm9_fp2_sub(X3, X3, T1);
  1477. sm9_fp2_sub(X3, X3, T4);
  1478. sm9_fp2_sub(T3, T3, X3);
  1479. sm9_fp2_mul(T3, T3, T2);
  1480. sm9_fp2_mul(T4, T4, Y1);
  1481. sm9_fp2_sub(Y3, T3, T4);
  1482. sm9_fp2_copy(R->X, X3);
  1483. sm9_fp2_copy(R->Y, Y3);
  1484. sm9_fp2_copy(R->Z, Z3);
  1485. }
  1486. void sm9_twist_point_sub(SM9_TWIST_POINT *R, const SM9_TWIST_POINT *P, const SM9_TWIST_POINT *Q)
  1487. {
  1488. SM9_TWIST_POINT _T, *T = &_T;
  1489. sm9_twist_point_neg(T, Q);
  1490. sm9_twist_point_add_full(R, P, T);
  1491. }
  1492. void sm9_twist_point_add_full(SM9_TWIST_POINT *R, const SM9_TWIST_POINT *P, const SM9_TWIST_POINT *Q)
  1493. {
  1494. const sm9_fp_t *X1 = P->X;
  1495. const sm9_fp_t *Y1 = P->Y;
  1496. const sm9_fp_t *Z1 = P->Z;
  1497. const sm9_fp_t *X2 = Q->X;
  1498. const sm9_fp_t *Y2 = Q->Y;
  1499. const sm9_fp_t *Z2 = Q->Z;
  1500. sm9_fp2_t T1, T2, T3, T4, T5, T6, T7, T8;
  1501. if (sm9_twist_point_is_at_infinity(Q)) {
  1502. sm9_twist_point_copy(R, P);
  1503. return;
  1504. }
  1505. if (sm9_twist_point_is_at_infinity(P)) {
  1506. sm9_twist_point_copy(R, Q);
  1507. return;
  1508. }
  1509. sm9_fp2_sqr(T1, Z1);
  1510. sm9_fp2_sqr(T2, Z2);
  1511. sm9_fp2_mul(T3, X2, T1);
  1512. sm9_fp2_mul(T4, X1, T2);
  1513. sm9_fp2_add(T5, T3, T4);
  1514. sm9_fp2_sub(T3, T3, T4);
  1515. sm9_fp2_mul(T1, T1, Z1);
  1516. sm9_fp2_mul(T1, T1, Y2);
  1517. sm9_fp2_mul(T2, T2, Z2);
  1518. sm9_fp2_mul(T2, T2, Y1);
  1519. sm9_fp2_add(T6, T1, T2);
  1520. sm9_fp2_sub(T1, T1, T2);
  1521. if (sm9_fp2_is_zero(T1) && sm9_fp2_is_zero(T3)) {
  1522. sm9_twist_point_dbl(R, P);
  1523. return;
  1524. }
  1525. if (sm9_fp2_is_zero(T1) && sm9_fp2_is_zero(T6)) {
  1526. sm9_twist_point_set_infinity(R);
  1527. return;
  1528. }
  1529. sm9_fp2_sqr(T6, T1);
  1530. sm9_fp2_mul(T7, T3, Z1);
  1531. sm9_fp2_mul(T7, T7, Z2);
  1532. sm9_fp2_sqr(T8, T3);
  1533. sm9_fp2_mul(T5, T5, T8);
  1534. sm9_fp2_mul(T3, T3, T8);
  1535. sm9_fp2_mul(T4, T4, T8);
  1536. sm9_fp2_sub(T6, T6, T5);
  1537. sm9_fp2_sub(T4, T4, T6);
  1538. sm9_fp2_mul(T1, T1, T4);
  1539. sm9_fp2_mul(T2, T2, T3);
  1540. sm9_fp2_sub(T1, T1, T2);
  1541. sm9_fp2_copy(R->X, T6);
  1542. sm9_fp2_copy(R->Y, T1);
  1543. sm9_fp2_copy(R->Z, T7);
  1544. }
  1545. void sm9_twist_point_mul(SM9_TWIST_POINT *R, const sm9_bn_t k, const SM9_TWIST_POINT *P)
  1546. {
  1547. SM9_TWIST_POINT _Q, *Q = &_Q;
  1548. char kbits[256];
  1549. int i;
  1550. sm9_bn_to_bits(k, kbits);
  1551. sm9_twist_point_set_infinity(Q);
  1552. for (i = 0; i < 256; i++) {
  1553. sm9_twist_point_dbl(Q, Q);
  1554. if (kbits[i] == '1') {
  1555. sm9_twist_point_add_full(Q, Q, P);
  1556. }
  1557. }
  1558. sm9_twist_point_copy(R, Q);
  1559. }
  1560. void sm9_twist_point_mul_generator(SM9_TWIST_POINT *R, const sm9_bn_t k)
  1561. {
  1562. sm9_twist_point_mul(R, k, SM9_P2);
  1563. }
  1564. void sm9_eval_g_tangent(sm9_fp12_t num, sm9_fp12_t den, const SM9_TWIST_POINT *P, const SM9_POINT *Q)
  1565. {
  1566. sm9_fp_t x;
  1567. sm9_fp_t y;
  1568. sm9_point_get_xy(Q, x, y);
  1569. const sm9_fp_t *XP = P->X;
  1570. const sm9_fp_t *YP = P->Y;
  1571. const sm9_fp_t *ZP = P->Z;
  1572. const uint64_t *xQ = x;
  1573. const uint64_t *yQ = y;
  1574. sm9_fp_t *a0 = num[0][0];
  1575. sm9_fp_t *a1 = num[0][1];
  1576. sm9_fp_t *a4 = num[2][0];
  1577. sm9_fp_t *b1 = den[0][1];
  1578. sm9_fp2_t t0;
  1579. sm9_fp2_t t1;
  1580. sm9_fp2_t t2;
  1581. sm9_fp12_set_zero(num);
  1582. sm9_fp12_set_zero(den);
  1583. sm9_fp2_sqr(t0, ZP);
  1584. sm9_fp2_mul(t1, t0, ZP);
  1585. sm9_fp2_mul(b1, t1, YP);
  1586. sm9_fp2_mul_fp(t2, b1, yQ);
  1587. sm9_fp2_neg(a1, t2);
  1588. sm9_fp2_sqr(t1, XP);
  1589. sm9_fp2_mul(t0, t0, t1);
  1590. sm9_fp2_mul_fp(t0, t0, xQ);
  1591. sm9_fp2_tri(t0, t0);
  1592. sm9_fp2_div2(a4, t0);
  1593. sm9_fp2_mul(t1, t1, XP);
  1594. sm9_fp2_tri(t1, t1);
  1595. sm9_fp2_div2(t1, t1);
  1596. sm9_fp2_sqr(t0, YP);
  1597. sm9_fp2_sub(a0, t0, t1);
  1598. }
  1599. void sm9_eval_g_line(sm9_fp12_t num, sm9_fp12_t den, const SM9_TWIST_POINT *T, const SM9_TWIST_POINT *P, const SM9_POINT *Q)
  1600. {
  1601. sm9_fp_t x;
  1602. sm9_fp_t y;
  1603. sm9_point_get_xy(Q, x, y);
  1604. const sm9_fp_t *XT = T->X;
  1605. const sm9_fp_t *YT = T->Y;
  1606. const sm9_fp_t *ZT = T->Z;
  1607. const sm9_fp_t *XP = P->X;
  1608. const sm9_fp_t *YP = P->Y;
  1609. const sm9_fp_t *ZP = P->Z;
  1610. const uint64_t *xQ = x;
  1611. const uint64_t *yQ = y;
  1612. sm9_fp_t *a0 = num[0][0];
  1613. sm9_fp_t *a1 = num[0][1];
  1614. sm9_fp_t *a4 = num[2][0];
  1615. sm9_fp_t *b1 = den[0][1];
  1616. sm9_fp2_t T0, T1, T2, T3, T4;
  1617. sm9_fp12_set_zero(num);
  1618. sm9_fp12_set_zero(den);
  1619. sm9_fp2_sqr(T0, ZP);
  1620. sm9_fp2_mul(T1, T0, XT);
  1621. sm9_fp2_mul(T0, T0, ZP);
  1622. sm9_fp2_sqr(T2, ZT);
  1623. sm9_fp2_mul(T3, T2, XP);
  1624. sm9_fp2_mul(T2, T2, ZT);
  1625. sm9_fp2_mul(T2, T2, YP);
  1626. sm9_fp2_sub(T1, T1, T3);
  1627. sm9_fp2_mul(T1, T1, ZT);
  1628. sm9_fp2_mul(T1, T1, ZP);
  1629. sm9_fp2_mul(T4, T1, T0);
  1630. sm9_fp2_copy(b1, T4);
  1631. sm9_fp2_mul(T1, T1, YP);
  1632. sm9_fp2_mul(T3, T0, YT);
  1633. sm9_fp2_sub(T3, T3, T2);
  1634. sm9_fp2_mul(T0, T0, T3);
  1635. sm9_fp2_mul_fp(T0, T0, xQ);
  1636. sm9_fp2_copy(a4, T0);
  1637. sm9_fp2_mul(T3, T3, XP);
  1638. sm9_fp2_mul(T3, T3, ZP);
  1639. sm9_fp2_sub(T1, T1, T3);
  1640. sm9_fp2_copy(a0, T1);
  1641. sm9_fp2_mul_fp(T2, T4, yQ);
  1642. sm9_fp2_neg(T2, T2);
  1643. sm9_fp2_copy(a1, T2);
  1644. }
  1645. void sm9_twist_point_pi1(SM9_TWIST_POINT *R, const SM9_TWIST_POINT *P)
  1646. {
  1647. //const c = 0x3f23ea58e5720bdb843c6cfa9c08674947c5c86e0ddd04eda91d8354377b698bn;
  1648. const sm9_fp_t c = {
  1649. 0x377b698b, 0xa91d8354, 0x0ddd04ed, 0x47c5c86e,
  1650. 0x9c086749, 0x843c6cfa, 0xe5720bdb, 0x3f23ea58,
  1651. };
  1652. sm9_fp2_conjugate(R->X, P->X);
  1653. sm9_fp2_conjugate(R->Y, P->Y);
  1654. sm9_fp2_conjugate(R->Z, P->Z);
  1655. sm9_fp2_mul_fp(R->Z, R->Z, c);
  1656. }
  1657. void sm9_twist_point_pi2(SM9_TWIST_POINT *R, const SM9_TWIST_POINT *P)
  1658. {
  1659. //c = 0xf300000002a3a6f2780272354f8b78f4d5fc11967be65334
  1660. const sm9_fp_t c = {
  1661. 0x7be65334, 0xd5fc1196, 0x4f8b78f4, 0x78027235,
  1662. 0x02a3a6f2, 0xf3000000, 0, 0,
  1663. };
  1664. sm9_fp2_copy(R->X, P->X);
  1665. sm9_fp2_copy(R->Y, P->Y);
  1666. sm9_fp2_mul_fp(R->Z, P->Z, c);
  1667. }
  1668. void sm9_twist_point_neg_pi2(SM9_TWIST_POINT *R, const SM9_TWIST_POINT *P)
  1669. {
  1670. // c = 0xf300000002a3a6f2780272354f8b78f4d5fc11967be65334
  1671. const sm9_fp_t c = {
  1672. 0x7be65334, 0xd5fc1196, 0x4f8b78f4, 0x78027235,
  1673. 0x02a3a6f2, 0xf3000000, 0, 0,
  1674. };
  1675. sm9_fp2_copy(R->X, P->X);
  1676. sm9_fp2_neg(R->Y, P->Y);
  1677. sm9_fp2_mul_fp(R->Z, P->Z, c);
  1678. }
  1679. void sm9_final_exponent_hard_part(sm9_fp12_t r, const sm9_fp12_t f)
  1680. {
  1681. // a2 = 0xd8000000019062ed0000b98b0cb27659
  1682. // a3 = 0x2400000000215d941
  1683. const sm9_bn_t a2 = {0xcb27659, 0x0000b98b, 0x019062ed, 0xd8000000, 0, 0, 0, 0};
  1684. const sm9_bn_t a3 = {0x215d941, 0x40000000, 0x2, 0, 0, 0, 0, 0};
  1685. const sm9_bn_t nine = {9,0,0,0,0,0,0,0};
  1686. sm9_fp12_t t0, t1, t2, t3;
  1687. sm9_fp12_pow(t0, f, a3);
  1688. sm9_fp12_inv(t0, t0);
  1689. sm9_fp12_frobenius(t1, t0);
  1690. sm9_fp12_mul(t1, t0, t1);
  1691. sm9_fp12_mul(t0, t0, t1);
  1692. sm9_fp12_frobenius(t2, f);
  1693. sm9_fp12_mul(t3, t2, f);
  1694. sm9_fp12_pow(t3, t3, nine);
  1695. sm9_fp12_mul(t0, t0, t3);
  1696. sm9_fp12_sqr(t3, f);
  1697. sm9_fp12_sqr(t3, t3);
  1698. sm9_fp12_mul(t0, t0, t3);
  1699. sm9_fp12_sqr(t2, t2);
  1700. sm9_fp12_mul(t2, t2, t1);
  1701. sm9_fp12_frobenius2(t1, f);
  1702. sm9_fp12_mul(t1, t1, t2);
  1703. sm9_fp12_pow(t2, t1, a2);
  1704. sm9_fp12_mul(t0, t2, t0);
  1705. sm9_fp12_frobenius3(t1, f);
  1706. sm9_fp12_mul(t1, t1, t0);
  1707. sm9_fp12_copy(r, t1);
  1708. }
  1709. void sm9_final_exponent(sm9_fp12_t r, const sm9_fp12_t f)
  1710. {
  1711. sm9_fp12_t t0;
  1712. sm9_fp12_t t1;
  1713. sm9_fp12_frobenius6(t0, f);
  1714. sm9_fp12_inv(t1, f);
  1715. sm9_fp12_mul(t0, t0, t1);
  1716. sm9_fp12_frobenius2(t1, t0);
  1717. sm9_fp12_mul(t0, t0, t1);
  1718. sm9_final_exponent_hard_part(t0, t0);
  1719. sm9_fp12_copy(r, t0);
  1720. }
  1721. void sm9_pairing(sm9_fp12_t r, const SM9_TWIST_POINT *Q, const SM9_POINT *P) {
  1722. const char *abits = "00100000000000000000000000000000000000010000101100020200101000020";
  1723. SM9_TWIST_POINT _T, *T = &_T;
  1724. SM9_TWIST_POINT _Q1, *Q1 = &_Q1;
  1725. SM9_TWIST_POINT _Q2, *Q2 = &_Q2;
  1726. sm9_fp12_t f_num;
  1727. sm9_fp12_t f_den;
  1728. sm9_fp12_t g_num;
  1729. sm9_fp12_t g_den;
  1730. int i;
  1731. sm9_twist_point_copy(T, Q);
  1732. sm9_fp12_set_one(f_num);
  1733. sm9_fp12_set_one(f_den);
  1734. for (i = 0; i < strlen(abits); i++) {
  1735. sm9_fp12_sqr(f_num, f_num);
  1736. sm9_fp12_sqr(f_den, f_den);
  1737. sm9_eval_g_tangent(g_num, g_den, T, P);
  1738. sm9_fp12_mul(f_num, f_num, g_num);
  1739. sm9_fp12_mul(f_den, f_den, g_den);
  1740. sm9_twist_point_dbl(T, T);
  1741. if (abits[i] == '1') {
  1742. sm9_eval_g_line(g_num, g_den, T, Q, P);
  1743. sm9_fp12_mul(f_num, f_num, g_num);
  1744. sm9_fp12_mul(f_den, f_den, g_den);
  1745. sm9_twist_point_add_full(T, T, Q);
  1746. } else if (abits[i] == '2') {
  1747. sm9_twist_point_neg(Q1, Q);
  1748. sm9_eval_g_line(g_num, g_den, T, Q1, P);
  1749. sm9_fp12_mul(f_num, f_num, g_num);
  1750. sm9_fp12_mul(f_den, f_den, g_den);
  1751. sm9_twist_point_add_full(T, T, Q1);
  1752. }
  1753. }
  1754. sm9_twist_point_pi1(Q1, Q);
  1755. sm9_twist_point_neg_pi2(Q2, Q);
  1756. sm9_eval_g_line(g_num, g_den, T, Q1, P);
  1757. sm9_fp12_mul(f_num, f_num, g_num);
  1758. sm9_fp12_mul(f_den, f_den, g_den);
  1759. sm9_twist_point_add_full(T, T, Q1);
  1760. sm9_eval_g_line(g_num, g_den, T, Q2, P);
  1761. sm9_fp12_mul(f_num, f_num, g_num);
  1762. sm9_fp12_mul(f_den, f_den, g_den);
  1763. sm9_twist_point_add_full(T, T, Q2);
  1764. sm9_fp12_inv(f_den, f_den);
  1765. sm9_fp12_mul(r, f_num, f_den);
  1766. sm9_final_exponent(r, r);
  1767. }
  1768. void sm9_fn_add(sm9_fn_t r, const sm9_fn_t a, const sm9_fn_t b)
  1769. {
  1770. sm9_bn_add(r, a, b);
  1771. if (sm9_bn_cmp(r, SM9_N) >= 0) {
  1772. sm9_bn_sub(r, r, SM9_N);
  1773. }
  1774. }
  1775. void sm9_fn_sub(sm9_fn_t r, const sm9_fn_t a, const sm9_fn_t b)
  1776. {
  1777. if (sm9_bn_cmp(a, b) >= 0) {
  1778. sm9_bn_sub(r, a, b);
  1779. } else {
  1780. sm9_bn_t t;
  1781. sm9_bn_sub(t, SM9_N, b);
  1782. sm9_bn_add(r, t, a);
  1783. }
  1784. }
  1785. void sm9_fn_mul(sm9_fn_t r, const sm9_fn_t a, const sm9_fn_t b)
  1786. {
  1787. uint64_t s[18];
  1788. sm9_barrett_bn_t zh, zl, q;
  1789. uint64_t w;
  1790. int i, j;
  1791. /* z = a * b */
  1792. for (i = 0; i < 8; i++) {
  1793. s[i] = 0;
  1794. }
  1795. for (i = 0; i < 8; i++) {
  1796. w = 0;
  1797. for (j = 0; j < 8; j++) {
  1798. w += s[i + j] + a[i] * b[j];
  1799. s[i + j] = w & 0xffffffff;
  1800. w >>= 32;
  1801. }
  1802. s[i + 8] = w;
  1803. }
  1804. /* zl = z mod (2^32)^9 = z[0..8]
  1805. * zh = z // (2^32)^7 = z[7..15] */
  1806. for (i = 0; i < 9; i++) {
  1807. zl[i] = s[i];
  1808. zh[i] = s[7 + i];
  1809. }
  1810. /* q = zh * mu // (2^32)^9 */
  1811. for (i = 0; i < 18; i++) {
  1812. s[i] = 0;
  1813. }
  1814. for (i = 0; i < 9; i++) {
  1815. w = 0;
  1816. for (j = 0; j < 9; j++) {
  1817. w += s[i + j] + zh[i] * SM9_MU_N[j];
  1818. s[i + j] = w & 0xffffffff;
  1819. w >>= 32;
  1820. }
  1821. s[i + 9] = w;
  1822. }
  1823. for (i = 0; i < 9; i++) {
  1824. q[i] = s[9 + i];
  1825. }
  1826. /* q = q * n mod (2^32)^9 */
  1827. for (i = 0; i < 18; i++) {
  1828. s[i] = 0;
  1829. }
  1830. for (i = 0; i < 9; i++) {
  1831. w = 0;
  1832. for (j = 0; j < 8; j++) {
  1833. w += s[i + j] + q[i] * SM9_N[j];
  1834. s[i + j] = w & 0xffffffff;
  1835. w >>= 32;
  1836. }
  1837. s[i + 8] = w;
  1838. }
  1839. for (i = 0; i < 9; i++) {
  1840. q[i] = s[i];
  1841. }
  1842. /* r = zl - q (mod (2^32)^9) */
  1843. if (sm9_barrett_bn_cmp(zl, q)) {
  1844. sm9_barrett_bn_sub(zl, zl, q);
  1845. } else {
  1846. sm9_barrett_bn_t c = {0,0,0,0,0,0,0,0,0x100000000};
  1847. sm9_barrett_bn_sub(q, c, q);
  1848. sm9_barrett_bn_add(zl, q, zl);
  1849. }
  1850. for (i = 0; i < 8; i++) {
  1851. r[i] = zl[i];
  1852. }
  1853. r[7] += (zl[8] << 32);
  1854. /* while r >= n do: r = r - n */
  1855. while (sm9_bn_cmp(r, SM9_N) >= 0) {
  1856. sm9_bn_sub(r, r, SM9_N);
  1857. }
  1858. }
  1859. void sm9_fn_pow(sm9_fn_t r, const sm9_fn_t a, const sm9_bn_t e)
  1860. {
  1861. sm9_fn_t t;
  1862. uint32_t w;
  1863. int i, j;
  1864. assert(sm9_bn_cmp(e, SM9_N_MINUS_ONE) < 0);
  1865. sm9_bn_set_one(t);
  1866. for (i = 7; i >= 0; i--) {
  1867. w = (uint32_t)e[i];
  1868. for (j = 0; j < 32; j++) {
  1869. sm9_fn_mul(t, t, t);
  1870. if (w & 0x80000000)
  1871. sm9_fn_mul(t, t, a);
  1872. w <<= 1;
  1873. }
  1874. }
  1875. sm9_bn_copy(r, t);
  1876. }
  1877. void sm9_fn_inv(sm9_fn_t r, const sm9_fn_t a)
  1878. {
  1879. sm9_fn_t e;
  1880. sm9_bn_sub(e, SM9_N, SM9_TWO);
  1881. sm9_fn_pow(r, a, e);
  1882. }
  1883. // for H1() and H2()
  1884. // h = (Ha mod (n-1)) + 1; h in [1, n-1], n is the curve order, Ha is 40 bytes from hash
  1885. void sm9_fn_from_hash(sm9_fn_t h, const uint8_t Ha[40])
  1886. {
  1887. uint64_t s[18] = {0};
  1888. sm9_barrett_bn_t zh, zl, q;
  1889. uint64_t w;
  1890. int i, j;
  1891. /* s = Ha -> int */
  1892. for (int i = 0; i < 10; i++) {
  1893. for (int j = 0; j < 4; j++) {
  1894. s[i] <<= 8;
  1895. s[i] += Ha[4 * (9-i) + j];
  1896. }
  1897. }
  1898. /* zl = z mod (2^32)^9 = z[0..8]
  1899. * zh = z // (2^32)^7 = z[7..15] */
  1900. for (i = 0; i < 9; i++) {
  1901. zl[i] = s[i];
  1902. zh[i] = s[7 + i];
  1903. }
  1904. /* q = zh * mu // (2^32)^9 */
  1905. for (i = 0; i < 18; i++) {
  1906. s[i] = 0;
  1907. }
  1908. for (i = 0; i < 9; i++) {
  1909. w = 0;
  1910. for (j = 0; j < 9; j++) {
  1911. w += s[i + j] + zh[i] * SM9_MU_N_MINUS_ONE[j]; //
  1912. s[i + j] = w & 0xffffffff;
  1913. w >>= 32;
  1914. }
  1915. s[i + 9] = w;
  1916. }
  1917. for (i = 0; i < 9; i++) {
  1918. q[i] = s[9 + i];
  1919. }
  1920. /* q = q * p mod (2^32)^9 */
  1921. for (i = 0; i < 18; i++) {
  1922. s[i] = 0;
  1923. }
  1924. for (i = 0; i < 9; i++) {
  1925. w = 0;
  1926. for (j = 0; j < 8; j++) {
  1927. w += s[i + j] + q[i] * SM9_N_MINUS_ONE[j];
  1928. s[i + j] = w & 0xffffffff;
  1929. w >>= 32;
  1930. }
  1931. s[i + 8] = w;
  1932. }
  1933. for (i = 0; i < 9; i++) {
  1934. q[i] = s[i];
  1935. }
  1936. /* h = zl - q (mod (2^32)^9) */
  1937. if (sm9_barrett_bn_cmp(zl, q)) {
  1938. sm9_barrett_bn_sub(zl, zl, q);
  1939. } else {
  1940. sm9_barrett_bn_t c = {0,0,0,0,0,0,0,0,0x100000000};
  1941. sm9_barrett_bn_sub(q, c, q);
  1942. sm9_barrett_bn_add(zl, q, zl);
  1943. }
  1944. for (i = 0; i < 8; i++) {
  1945. h[i] = zl[i];
  1946. }
  1947. h[7] += (zl[8] << 32);
  1948. /* while h >= (n-1) do: h = h - (n-1) */
  1949. while (sm9_bn_cmp(h, SM9_N_MINUS_ONE) >= 0) {
  1950. sm9_bn_sub(h, h, SM9_N_MINUS_ONE);
  1951. }
  1952. sm9_fn_add(h, h, SM9_ONE);
  1953. }
  1954. void sm9_fp12_to_bytes(const sm9_fp12_t a, uint8_t buf[32 * 12])
  1955. {
  1956. sm9_fp4_to_bytes(a[2], buf);
  1957. sm9_fp4_to_bytes(a[1], buf + 32 * 4);
  1958. sm9_fp4_to_bytes(a[0], buf + 32 * 8);
  1959. }
  1960. int sm9_fn_from_bytes(sm9_fn_t a, const uint8_t in[32])
  1961. {
  1962. sm9_bn_from_bytes(a, in);
  1963. return 1;
  1964. }
  1965. int sm9_point_to_uncompressed_octets(const SM9_POINT *P, uint8_t octets[65])
  1966. {
  1967. sm9_fp_t x;
  1968. sm9_fp_t y;
  1969. sm9_point_get_xy(P, x, y);
  1970. octets[0] = 0x04;
  1971. sm9_bn_to_bytes(x, octets + 1);
  1972. sm9_bn_to_bytes(y, octets + 32 + 1);
  1973. return 1;
  1974. }
  1975. int sm9_point_from_uncompressed_octets(SM9_POINT *P, const uint8_t octets[65])
  1976. {
  1977. if (octets[0] != 0x04) {
  1978. error_print();
  1979. return -1;
  1980. }
  1981. memset(P, 0, sizeof(*P));
  1982. sm9_bn_from_bytes(P->X, octets + 1);
  1983. sm9_bn_from_bytes(P->Y, octets + 32 + 1);
  1984. sm9_fp_set_one(P->Z);
  1985. if (!sm9_point_is_on_curve(P)) {
  1986. error_print();
  1987. return -1;
  1988. }
  1989. return 1;
  1990. }
  1991. int sm9_twist_point_to_uncompressed_octets(const SM9_TWIST_POINT *P, uint8_t octets[129])
  1992. {
  1993. octets[0] = 0x04;
  1994. sm9_fp2_t x;
  1995. sm9_fp2_t y;
  1996. sm9_twist_point_get_xy(P, x, y);
  1997. sm9_fp2_to_bytes(x, octets + 1);
  1998. sm9_fp2_to_bytes(y, octets + 32 * 2 + 1);
  1999. return 1;
  2000. }
  2001. int sm9_twist_point_from_uncompressed_octets(SM9_TWIST_POINT *P, const uint8_t octets[129])
  2002. {
  2003. assert(octets[0] == 0x04);
  2004. sm9_fp2_from_bytes(P->X, octets + 1);
  2005. sm9_fp2_from_bytes(P->Y, octets + 32 * 2 + 1);
  2006. sm9_fp2_set_one(P->Z);
  2007. if (!sm9_twist_point_is_on_curve(P)) return -1;
  2008. return 1;
  2009. }