fe_448.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408
  1. /* fe_448.c
  2. *
  3. * Copyright (C) 2006-2022 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /* Based On Daniel J Bernstein's curve25519 Public Domain ref10 work.
  22. * Small implementation based on Daniel Beer's curve25519 public domain work.
  23. * Reworked for curve448 by Sean Parkinson.
  24. */
  25. #ifdef HAVE_CONFIG_H
  26. #include <config.h>
  27. #endif
  28. #include <wolfssl/wolfcrypt/settings.h>
  29. #if defined(HAVE_CURVE448) || defined(HAVE_ED448)
  30. #include <wolfssl/wolfcrypt/fe_448.h>
  31. #ifdef NO_INLINE
  32. #include <wolfssl/wolfcrypt/misc.h>
  33. #else
  34. #define WOLFSSL_MISC_INCLUDED
  35. #include <wolfcrypt/src/misc.c>
  36. #endif
  37. #if defined(CURVE448_SMALL) || defined(ED448_SMALL)
  38. /* Initialize the field element operations.
  39. */
  40. void fe448_init(void)
  41. {
  42. }
  43. /* Normalize the field element.
  44. * Ensure result is in range: 0..2^448-2^224-2
  45. *
  46. * a [in] Field element in range 0..2^448-1.
  47. */
  48. void fe448_norm(word8* a)
  49. {
  50. int i;
  51. sword16 c = 0;
  52. sword16 o = 0;
  53. for (i = 0; i < 56; i++) {
  54. c += a[i];
  55. if ((i == 0) || (i == 28))
  56. c += 1;
  57. c >>= 8;
  58. }
  59. for (i = 0; i < 56; i++) {
  60. if ((i == 0) || (i == 28)) o += c;
  61. o += a[i];
  62. a[i] = (word8)o;
  63. o >>= 8;
  64. }
  65. }
  66. /* Copy one field element into another: d = a.
  67. *
  68. * d [in] Destination field element.
  69. * a [in] Source field element.
  70. */
  71. void fe448_copy(word8* d, const word8* a)
  72. {
  73. int i;
  74. for (i = 0; i < 56; i++) {
  75. d[i] = a[i];
  76. }
  77. }
  78. /* Conditionally swap the elements.
  79. * Constant time implementation.
  80. *
  81. * a [in] First field element.
  82. * b [in] Second field element.
  83. * c [in] Swap when 1. Valid values: 0, 1.
  84. */
  85. static void fe448_cswap(word8* a, word8* b, int c)
  86. {
  87. int i;
  88. word8 mask = -(word8)c;
  89. word8 t[56];
  90. for (i = 0; i < 56; i++)
  91. t[i] = (a[i] ^ b[i]) & mask;
  92. for (i = 0; i < 56; i++)
  93. a[i] ^= t[i];
  94. for (i = 0; i < 56; i++)
  95. b[i] ^= t[i];
  96. }
  97. /* Add two field elements. r = (a + b) mod (2^448 - 2^224 - 1)
  98. *
  99. * r [in] Field element to hold sum.
  100. * a [in] Field element to add.
  101. * b [in] Field element to add.
  102. */
  103. void fe448_add(word8* r, const word8* a, const word8* b)
  104. {
  105. int i;
  106. sword16 c = 0;
  107. sword16 o = 0;
  108. for (i = 0; i < 56; i++) {
  109. c += a[i];
  110. c += b[i];
  111. r[i] = (word8)c;
  112. c >>= 8;
  113. }
  114. for (i = 0; i < 56; i++) {
  115. if ((i == 0) || (i == 28)) o += c;
  116. o += r[i];
  117. r[i] = (word8)o;
  118. o >>= 8;
  119. }
  120. }
  121. /* Subtract a field element from another. r = (a - b) mod (2^448 - 2^224 - 1)
  122. *
  123. * r [in] Field element to hold difference.
  124. * a [in] Field element to subtract from.
  125. * b [in] Field element to subtract.
  126. */
  127. void fe448_sub(word8* r, const word8* a, const word8* b)
  128. {
  129. int i;
  130. sword16 c = 0;
  131. sword16 o = 0;
  132. for (i = 0; i < 56; i++) {
  133. if (i == 28)
  134. c += 0x1fc;
  135. else
  136. c += 0x1fe;
  137. c += a[i];
  138. c -= b[i];
  139. r[i] = (word8)c;
  140. c >>= 8;
  141. }
  142. for (i = 0; i < 56; i++) {
  143. if ((i == 0) || (i == 28)) o += c;
  144. o += r[i];
  145. r[i] = (word8)o;
  146. o >>= 8;
  147. }
  148. }
  149. /* Mulitply a field element by 39081. r = (39081 * a) mod (2^448 - 2^224 - 1)
  150. *
  151. * r [in] Field element to hold result.
  152. * a [in] Field element to multiply.
  153. */
  154. void fe448_mul39081(word8* r, const word8* a)
  155. {
  156. int i;
  157. sword32 c = 0;
  158. sword32 o = 0;
  159. for (i = 0; i < 56; i++) {
  160. c += a[i] * (sword32)39081;
  161. r[i] = (word8)c;
  162. c >>= 8;
  163. }
  164. for (i = 0; i < 56; i++) {
  165. if ((i == 0) || (i == 28)) o += c;
  166. o += r[i];
  167. r[i] = (word8)o;
  168. o >>= 8;
  169. }
  170. }
  171. /* Mulitply two field elements. r = (a * b) mod (2^448 - 2^224 - 1)
  172. *
  173. * r [in] Field element to hold result.
  174. * a [in] Field element to multiply.
  175. * b [in] Field element to multiply.
  176. */
  177. void fe448_mul(word8* r, const word8* a, const word8* b)
  178. {
  179. int i, k;
  180. sword32 c = 0;
  181. sword16 o = 0, cc = 0;
  182. word8 t[112];
  183. for (k = 0; k < 56; k++) {
  184. i = 0;
  185. for (; i <= k; i++) {
  186. c += (sword32)a[i] * b[k - i];
  187. }
  188. t[k] = (word8)c;
  189. c >>= 8;
  190. }
  191. for (; k < 111; k++) {
  192. i = k - 55;
  193. for (; i < 56; i++) {
  194. c += (sword32)a[i] * b[k - i];
  195. }
  196. t[k] = (word8)c;
  197. c >>= 8;
  198. }
  199. t[k] = (word8)c;
  200. for (i = 0; i < 28; i++) {
  201. o += t[i];
  202. o += t[i + 56];
  203. o += t[i + 84];
  204. r[i] = (word8)o;
  205. o >>= 8;
  206. }
  207. for (i = 28; i < 56; i++) {
  208. o += t[i];
  209. o += t[i + 56];
  210. o += t[i + 28];
  211. o += t[i + 56];
  212. r[i] = (word8)o;
  213. o >>= 8;
  214. }
  215. for (i = 0; i < 56; i++) {
  216. if ((i == 0) || (i == 28)) cc += o;
  217. cc += r[i];
  218. r[i] = (word8)cc;
  219. cc >>= 8;
  220. }
  221. }
  222. /* Square a field element. r = (a * a) mod (2^448 - 2^224 - 1)
  223. *
  224. * r [in] Field element to hold result.
  225. * a [in] Field element to square.
  226. */
  227. void fe448_sqr(word8* r, const word8* a)
  228. {
  229. int i, k;
  230. sword32 c = 0;
  231. sword32 p;
  232. sword16 o = 0, cc = 0;
  233. word8 t[112];
  234. for (k = 0; k < 56; k++) {
  235. i = 0;
  236. for (; i <= k; i++) {
  237. if (k - i < i)
  238. break;
  239. p = (sword32)a[i] * a[k - i];
  240. if (k - i != i)
  241. p *= 2;
  242. c += p;
  243. }
  244. t[k] = (word8)c;
  245. c >>= 8;
  246. }
  247. for (; k < 111; k++) {
  248. i = k - 55;
  249. for (; i < 56; i++) {
  250. if (k - i < i)
  251. break;
  252. p = (sword32)a[i] * a[k - i];
  253. if (k - i != i)
  254. p *= 2;
  255. c += p;
  256. }
  257. t[k] = (word8)c;
  258. c >>= 8;
  259. }
  260. t[k] = (word8)c;
  261. for (i = 0; i < 28; i++) {
  262. o += t[i];
  263. o += t[i + 56];
  264. o += t[i + 84];
  265. r[i] = (word8)o;
  266. o >>= 8;
  267. }
  268. for (i = 28; i < 56; i++) {
  269. o += t[i];
  270. o += t[i + 56];
  271. o += t[i + 28];
  272. o += t[i + 56];
  273. r[i] = (word8)o;
  274. o >>= 8;
  275. }
  276. for (i = 0; i < 56; i++) {
  277. if ((i == 0) || (i == 28)) cc += o;
  278. cc += r[i];
  279. r[i] = (word8)cc;
  280. cc >>= 8;
  281. }
  282. fe448_norm(r);
  283. }
  284. /* Invert the field element. (r * a) mod (2^448 - 2^224 - 1) = 1
  285. * Constant time implementation - using Fermat's little theorem:
  286. * a^(p-1) mod p = 1 => a^(p-2) mod p = 1/a
  287. * For curve448: p - 2 = 2^448 - 2^224 - 3
  288. *
  289. * r [in] Field element to hold result.
  290. * a [in] Field element to invert.
  291. */
  292. void fe448_invert(word8* r, const word8* a)
  293. {
  294. int i;
  295. word8 t[56];
  296. fe448_sqr(t, a);
  297. fe448_mul(t, t, a);
  298. for (i = 0; i < 221; i++) {
  299. fe448_sqr(t, t);
  300. fe448_mul(t, t, a);
  301. }
  302. fe448_sqr(t, t);
  303. for (i = 0; i < 222; i++) {
  304. fe448_sqr(t, t);
  305. fe448_mul(t, t, a);
  306. }
  307. fe448_sqr(t, t);
  308. fe448_sqr(t, t);
  309. fe448_mul(r, t, a);
  310. }
  311. /* Scalar multiply the point by a number. r = n.a
  312. * Uses Montgomery ladder and only requires the x-ordinate.
  313. *
  314. * r [in] Field element to hold result.
  315. * n [in] Scalar as an array of bytes.
  316. * a [in] Point to multiply - x-ordinate only.
  317. */
  318. int curve448(byte* r, const byte* n, const byte* a)
  319. {
  320. word8 x1[56];
  321. word8 x2[56] = {1};
  322. word8 z2[56] = {0};
  323. word8 x3[56];
  324. word8 z3[56] = {1};
  325. word8 t0[56];
  326. word8 t1[56];
  327. int i;
  328. unsigned int swap;
  329. unsigned int b;
  330. fe448_copy(x1, a);
  331. fe448_copy(x3, a);
  332. swap = 0;
  333. for (i = 447; i >= 0; --i) {
  334. b = (n[i >> 3] >> (i & 7)) & 1;
  335. swap ^= b;
  336. fe448_cswap(x2, x3, swap);
  337. fe448_cswap(z2, z3, swap);
  338. swap = b;
  339. /* Montgomery Ladder - double and add */
  340. fe448_add(t0, x2, z2);
  341. fe448_add(t1, x3, z3);
  342. fe448_sub(x2, x2, z2);
  343. fe448_sub(x3, x3, z3);
  344. fe448_mul(t1, t1, x2);
  345. fe448_mul(z3, x3, t0);
  346. fe448_sqr(t0, t0);
  347. fe448_sqr(x2, x2);
  348. fe448_add(x3, z3, t1);
  349. fe448_sqr(x3, x3);
  350. fe448_sub(z3, z3, t1);
  351. fe448_sqr(z3, z3);
  352. fe448_mul(z3, z3, x1);
  353. fe448_sub(t1, t0, x2);
  354. fe448_mul(x2, t0, x2);
  355. fe448_mul39081(z2, t1);
  356. fe448_add(z2, t0, z2);
  357. fe448_mul(z2, z2, t1);
  358. }
  359. fe448_cswap(x2, x3, swap);
  360. fe448_cswap(z2, z3, swap);
  361. fe448_invert(z2, z2);
  362. fe448_mul(r, x2, z2);
  363. fe448_norm(r);
  364. return 0;
  365. }
  366. #ifdef HAVE_ED448
  367. /* Check whether field element is not 0.
  368. * Field element must have been normalized before call.
  369. *
  370. * a [in] Field element.
  371. * returns 0 when zero, and any other value otherwise.
  372. */
  373. int fe448_isnonzero(const word8* a)
  374. {
  375. int i;
  376. byte c = 0;
  377. for (i = 0; i < 56; i++)
  378. c |= a[i];
  379. return c;
  380. }
  381. /* Negates the field element. r = -a mod (2^448 - 2^224 - 1)
  382. * Add 0x200 to each element and subtract 2 from next.
  383. * Top element overflow handled by subtracting 2 from index 0 and 28.
  384. *
  385. * r [in] Field element to hold result.
  386. * a [in] Field element.
  387. */
  388. void fe448_neg(word8* r, const word8* a)
  389. {
  390. int i;
  391. sword16 c = 0;
  392. sword16 o = 0;
  393. for (i = 0; i < 56; i++) {
  394. if (i == 28)
  395. c += 0x1fc;
  396. else
  397. c += 0x1fe;
  398. c -= a[i];
  399. r[i] = (word8)c;
  400. c >>= 8;
  401. }
  402. for (i = 0; i < 56; i++) {
  403. if ((i == 0) || (i == 28)) o += c;
  404. o += r[i];
  405. r[i] = (word8)o;
  406. o >>= 8;
  407. }
  408. }
  409. /* Raise field element to (p-3) / 4: 2^446 - 2^222 - 1
  410. * Used for calcualting y-ordinate from x-ordinate for Ed448.
  411. *
  412. * r [in] Field element to hold result.
  413. * a [in] Field element to exponentiate.
  414. */
  415. void fe448_pow_2_446_222_1(word8* r, const word8* a)
  416. {
  417. int i;
  418. word8 t[56];
  419. fe448_sqr(t, a);
  420. fe448_mul(t, t, a);
  421. for (i = 0; i < 221; i++) {
  422. fe448_sqr(t, t);
  423. fe448_mul(t, t, a);
  424. }
  425. fe448_sqr(t, t);
  426. for (i = 0; i < 221; i++) {
  427. fe448_sqr(t, t);
  428. fe448_mul(t, t, a);
  429. }
  430. fe448_sqr(t, t);
  431. fe448_mul(r, t, a);
  432. }
  433. /* Constant time, conditional move of b into a.
  434. * a is not changed if the condition is 0.
  435. *
  436. * a A field element.
  437. * b A field element.
  438. * c If 1 then copy and if 0 then don't copy.
  439. */
  440. void fe448_cmov(word8* a, const word8* b, int c)
  441. {
  442. int i;
  443. word8 m = -(word8)c;
  444. word8 t[56];
  445. for (i = 0; i < 56; i++)
  446. t[i] = m & (a[i] ^ b[i]);
  447. for (i = 0; i < 56; i++)
  448. a[i] ^= t[i];
  449. }
  450. #endif /* HAVE_ED448 */
  451. #elif defined(CURVED448_128BIT)
  452. /* Initialize the field element operations.
  453. */
  454. void fe448_init(void)
  455. {
  456. }
  457. /* Convert the field element from a byte array to an array of 56-bits.
  458. *
  459. * r [in] Array to encode into.
  460. * b [in] Byte array.
  461. */
  462. void fe448_from_bytes(sword64* r, const unsigned char* b)
  463. {
  464. r[ 0] = ((sword64) (b[ 0]) << 0)
  465. | ((sword64) (b[ 1]) << 8)
  466. | ((sword64) (b[ 2]) << 16)
  467. | ((sword64) (b[ 3]) << 24)
  468. | ((sword64) (b[ 4]) << 32)
  469. | ((sword64) (b[ 5]) << 40)
  470. | ((sword64) (b[ 6]) << 48);
  471. r[ 1] = ((sword64) (b[ 7]) << 0)
  472. | ((sword64) (b[ 8]) << 8)
  473. | ((sword64) (b[ 9]) << 16)
  474. | ((sword64) (b[10]) << 24)
  475. | ((sword64) (b[11]) << 32)
  476. | ((sword64) (b[12]) << 40)
  477. | ((sword64) (b[13]) << 48);
  478. r[ 2] = ((sword64) (b[14]) << 0)
  479. | ((sword64) (b[15]) << 8)
  480. | ((sword64) (b[16]) << 16)
  481. | ((sword64) (b[17]) << 24)
  482. | ((sword64) (b[18]) << 32)
  483. | ((sword64) (b[19]) << 40)
  484. | ((sword64) (b[20]) << 48);
  485. r[ 3] = ((sword64) (b[21]) << 0)
  486. | ((sword64) (b[22]) << 8)
  487. | ((sword64) (b[23]) << 16)
  488. | ((sword64) (b[24]) << 24)
  489. | ((sword64) (b[25]) << 32)
  490. | ((sword64) (b[26]) << 40)
  491. | ((sword64) (b[27]) << 48);
  492. r[ 4] = ((sword64) (b[28]) << 0)
  493. | ((sword64) (b[29]) << 8)
  494. | ((sword64) (b[30]) << 16)
  495. | ((sword64) (b[31]) << 24)
  496. | ((sword64) (b[32]) << 32)
  497. | ((sword64) (b[33]) << 40)
  498. | ((sword64) (b[34]) << 48);
  499. r[ 5] = ((sword64) (b[35]) << 0)
  500. | ((sword64) (b[36]) << 8)
  501. | ((sword64) (b[37]) << 16)
  502. | ((sword64) (b[38]) << 24)
  503. | ((sword64) (b[39]) << 32)
  504. | ((sword64) (b[40]) << 40)
  505. | ((sword64) (b[41]) << 48);
  506. r[ 6] = ((sword64) (b[42]) << 0)
  507. | ((sword64) (b[43]) << 8)
  508. | ((sword64) (b[44]) << 16)
  509. | ((sword64) (b[45]) << 24)
  510. | ((sword64) (b[46]) << 32)
  511. | ((sword64) (b[47]) << 40)
  512. | ((sword64) (b[48]) << 48);
  513. r[ 7] = ((sword64) (b[49]) << 0)
  514. | ((sword64) (b[50]) << 8)
  515. | ((sword64) (b[51]) << 16)
  516. | ((sword64) (b[52]) << 24)
  517. | ((sword64) (b[53]) << 32)
  518. | ((sword64) (b[54]) << 40)
  519. | ((sword64) (b[55]) << 48);
  520. }
  521. /* Convert the field element to a byte array from an array of 56-bits.
  522. *
  523. * b [in] Byte array.
  524. * a [in] Array to encode into.
  525. */
  526. void fe448_to_bytes(unsigned char* b, const sword64* a)
  527. {
  528. sword128 t;
  529. /* Mod */
  530. sword64 in0 = a[0];
  531. sword64 in1 = a[1];
  532. sword64 in2 = a[2];
  533. sword64 in3 = a[3];
  534. sword64 in4 = a[4];
  535. sword64 in5 = a[5];
  536. sword64 in6 = a[6];
  537. sword64 in7 = a[7];
  538. sword64 o = in7 >> 56;
  539. in7 -= o << 56;
  540. in0 += o;
  541. in4 += o;
  542. o = (in0 + 1) >> 56;
  543. o = (o + in1) >> 56;
  544. o = (o + in2) >> 56;
  545. o = (o + in3) >> 56;
  546. o = (o + in4 + 1) >> 56;
  547. o = (o + in5) >> 56;
  548. o = (o + in6) >> 56;
  549. o = (o + in7) >> 56;
  550. in0 += o;
  551. in4 += o;
  552. in7 -= o << 56;
  553. o = (in0 >> 56); in1 += o; t = o << 56; in0 -= (sword64)t;
  554. o = (in1 >> 56); in2 += o; t = o << 56; in1 -= (sword64)t;
  555. o = (in2 >> 56); in3 += o; t = o << 56; in2 -= (sword64)t;
  556. o = (in3 >> 56); in4 += o; t = o << 56; in3 -= (sword64)t;
  557. o = (in4 >> 56); in5 += o; t = o << 56; in4 -= (sword64)t;
  558. o = (in5 >> 56); in6 += o; t = o << 56; in5 -= (sword64)t;
  559. o = (in6 >> 56); in7 += o; t = o << 56; in6 -= (sword64)t;
  560. o = (in7 >> 56); in0 += o;
  561. in4 += o; t = o << 56; in7 -= (sword64)t;
  562. /* Output as bytes */
  563. b[ 0] = (in0 >> 0);
  564. b[ 1] = (in0 >> 8);
  565. b[ 2] = (in0 >> 16);
  566. b[ 3] = (in0 >> 24);
  567. b[ 4] = (in0 >> 32);
  568. b[ 5] = (in0 >> 40);
  569. b[ 6] = (in0 >> 48);
  570. b[ 7] = (in1 >> 0);
  571. b[ 8] = (in1 >> 8);
  572. b[ 9] = (in1 >> 16);
  573. b[10] = (in1 >> 24);
  574. b[11] = (in1 >> 32);
  575. b[12] = (in1 >> 40);
  576. b[13] = (in1 >> 48);
  577. b[14] = (in2 >> 0);
  578. b[15] = (in2 >> 8);
  579. b[16] = (in2 >> 16);
  580. b[17] = (in2 >> 24);
  581. b[18] = (in2 >> 32);
  582. b[19] = (in2 >> 40);
  583. b[20] = (in2 >> 48);
  584. b[21] = (in3 >> 0);
  585. b[22] = (in3 >> 8);
  586. b[23] = (in3 >> 16);
  587. b[24] = (in3 >> 24);
  588. b[25] = (in3 >> 32);
  589. b[26] = (in3 >> 40);
  590. b[27] = (in3 >> 48);
  591. b[28] = (in4 >> 0);
  592. b[29] = (in4 >> 8);
  593. b[30] = (in4 >> 16);
  594. b[31] = (in4 >> 24);
  595. b[32] = (in4 >> 32);
  596. b[33] = (in4 >> 40);
  597. b[34] = (in4 >> 48);
  598. b[35] = (in5 >> 0);
  599. b[36] = (in5 >> 8);
  600. b[37] = (in5 >> 16);
  601. b[38] = (in5 >> 24);
  602. b[39] = (in5 >> 32);
  603. b[40] = (in5 >> 40);
  604. b[41] = (in5 >> 48);
  605. b[42] = (in6 >> 0);
  606. b[43] = (in6 >> 8);
  607. b[44] = (in6 >> 16);
  608. b[45] = (in6 >> 24);
  609. b[46] = (in6 >> 32);
  610. b[47] = (in6 >> 40);
  611. b[48] = (in6 >> 48);
  612. b[49] = (in7 >> 0);
  613. b[50] = (in7 >> 8);
  614. b[51] = (in7 >> 16);
  615. b[52] = (in7 >> 24);
  616. b[53] = (in7 >> 32);
  617. b[54] = (in7 >> 40);
  618. b[55] = (in7 >> 48);
  619. }
  620. /* Set the field element to 0.
  621. *
  622. * a [in] Field element.
  623. */
  624. void fe448_1(sword64* a)
  625. {
  626. a[0] = 1;
  627. a[1] = 0;
  628. a[2] = 0;
  629. a[3] = 0;
  630. a[4] = 0;
  631. a[5] = 0;
  632. a[6] = 0;
  633. a[7] = 0;
  634. }
  635. /* Set the field element to 0.
  636. *
  637. * a [in] Field element.
  638. */
  639. void fe448_0(sword64* a)
  640. {
  641. a[0] = 0;
  642. a[1] = 0;
  643. a[2] = 0;
  644. a[3] = 0;
  645. a[4] = 0;
  646. a[5] = 0;
  647. a[6] = 0;
  648. a[7] = 0;
  649. }
  650. /* Copy one field element into another: d = a.
  651. *
  652. * d [in] Destination field element.
  653. * a [in] Source field element.
  654. */
  655. void fe448_copy(sword64* d, const sword64* a)
  656. {
  657. d[0] = a[0];
  658. d[1] = a[1];
  659. d[2] = a[2];
  660. d[3] = a[3];
  661. d[4] = a[4];
  662. d[5] = a[5];
  663. d[6] = a[6];
  664. d[7] = a[7];
  665. }
  666. /* Conditionally swap the elements.
  667. * Constant time implementation.
  668. *
  669. * a [in] First field element.
  670. * b [in] Second field element.
  671. * c [in] Swap when 1. Valid values: 0, 1.
  672. */
  673. static void fe448_cswap(sword64* a, sword64* b, int c)
  674. {
  675. sword64 mask = -(sword64)c;
  676. sword64 t0 = (a[0] ^ b[0]) & mask;
  677. sword64 t1 = (a[1] ^ b[1]) & mask;
  678. sword64 t2 = (a[2] ^ b[2]) & mask;
  679. sword64 t3 = (a[3] ^ b[3]) & mask;
  680. sword64 t4 = (a[4] ^ b[4]) & mask;
  681. sword64 t5 = (a[5] ^ b[5]) & mask;
  682. sword64 t6 = (a[6] ^ b[6]) & mask;
  683. sword64 t7 = (a[7] ^ b[7]) & mask;
  684. a[0] ^= t0;
  685. a[1] ^= t1;
  686. a[2] ^= t2;
  687. a[3] ^= t3;
  688. a[4] ^= t4;
  689. a[5] ^= t5;
  690. a[6] ^= t6;
  691. a[7] ^= t7;
  692. b[0] ^= t0;
  693. b[1] ^= t1;
  694. b[2] ^= t2;
  695. b[3] ^= t3;
  696. b[4] ^= t4;
  697. b[5] ^= t5;
  698. b[6] ^= t6;
  699. b[7] ^= t7;
  700. }
  701. /* Add two field elements. r = (a + b) mod (2^448 - 2^224 - 1)
  702. *
  703. * r [in] Field element to hold sum.
  704. * a [in] Field element to add.
  705. * b [in] Field element to add.
  706. */
  707. void fe448_add(sword64* r, const sword64* a, const sword64* b)
  708. {
  709. r[0] = a[0] + b[0];
  710. r[1] = a[1] + b[1];
  711. r[2] = a[2] + b[2];
  712. r[3] = a[3] + b[3];
  713. r[4] = a[4] + b[4];
  714. r[5] = a[5] + b[5];
  715. r[6] = a[6] + b[6];
  716. r[7] = a[7] + b[7];
  717. }
  718. /* Subtract a field element from another. r = (a - b) mod (2^448 - 2^224 - 1)
  719. *
  720. * r [in] Field element to hold difference.
  721. * a [in] Field element to subtract from.
  722. * b [in] Field element to subtract.
  723. */
  724. void fe448_sub(sword64* r, const sword64* a, const sword64* b)
  725. {
  726. r[0] = a[0] - b[0];
  727. r[1] = a[1] - b[1];
  728. r[2] = a[2] - b[2];
  729. r[3] = a[3] - b[3];
  730. r[4] = a[4] - b[4];
  731. r[5] = a[5] - b[5];
  732. r[6] = a[6] - b[6];
  733. r[7] = a[7] - b[7];
  734. }
  735. /* Mulitply a field element by 39081. r = (39081 * a) mod (2^448 - 2^224 - 1)
  736. *
  737. * r [in] Field element to hold result.
  738. * a [in] Field element to multiply.
  739. */
  740. void fe448_mul39081(sword64* r, const sword64* a)
  741. {
  742. sword128 t;
  743. sword64 o;
  744. sword128 t0 = a[0] * (sword128)39081;
  745. sword128 t1 = a[1] * (sword128)39081;
  746. sword128 t2 = a[2] * (sword128)39081;
  747. sword128 t3 = a[3] * (sword128)39081;
  748. sword128 t4 = a[4] * (sword128)39081;
  749. sword128 t5 = a[5] * (sword128)39081;
  750. sword128 t6 = a[6] * (sword128)39081;
  751. sword128 t7 = a[7] * (sword128)39081;
  752. o = (sword64)(t0 >> 56); t1 += o; t = (sword128)o << 56; t0 -= t;
  753. o = (sword64)(t1 >> 56); t2 += o; t = (sword128)o << 56; t1 -= t;
  754. o = (sword64)(t2 >> 56); t3 += o; t = (sword128)o << 56; t2 -= t;
  755. o = (sword64)(t3 >> 56); t4 += o; t = (sword128)o << 56; t3 -= t;
  756. o = (sword64)(t4 >> 56); t5 += o; t = (sword128)o << 56; t4 -= t;
  757. o = (sword64)(t5 >> 56); t6 += o; t = (sword128)o << 56; t5 -= t;
  758. o = (sword64)(t6 >> 56); t7 += o; t = (sword128)o << 56; t6 -= t;
  759. o = (sword64)(t7 >> 56); t0 += o;
  760. t4 += o; t = (sword128)o << 56; t7 -= t;
  761. /* Store */
  762. r[0] = (sword64)t0;
  763. r[1] = (sword64)t1;
  764. r[2] = (sword64)t2;
  765. r[3] = (sword64)t3;
  766. r[4] = (sword64)t4;
  767. r[5] = (sword64)t5;
  768. r[6] = (sword64)t6;
  769. r[7] = (sword64)t7;
  770. }
  771. /* Mulitply two field elements. r = (a * b) mod (2^448 - 2^224 - 1)
  772. *
  773. * r [in] Field element to hold result.
  774. * a [in] Field element to multiply.
  775. * b [in] Field element to multiply.
  776. */
  777. void fe448_mul(sword64* r, const sword64* a, const sword64* b)
  778. {
  779. sword64 o;
  780. sword64 a1[4];
  781. sword64 b1[4];
  782. sword128 t0;
  783. sword128 t1;
  784. sword128 t2;
  785. sword128 t3;
  786. sword128 t4;
  787. sword128 t5;
  788. sword128 t6;
  789. sword128 t7;
  790. sword128 t03;
  791. sword128 t04;
  792. sword128 t13;
  793. sword128 t14;
  794. a1[0] = a[0] + a[4];
  795. a1[1] = a[1] + a[5];
  796. a1[2] = a[2] + a[6];
  797. a1[3] = a[3] + a[7];
  798. b1[0] = b[0] + b[4];
  799. b1[1] = b[1] + b[5];
  800. b1[2] = b[2] + b[6];
  801. b1[3] = b[3] + b[7];
  802. t03 = ((sword128)a[0] * b[3]) + ((sword128)a[1] * b[2])
  803. + ((sword128)a[2] * b[1]) + ((sword128)a[3] * b[0]);
  804. t04 = ((sword128)a[1] * b[3]) + ((sword128)a[2] * b[2])
  805. + ((sword128)a[3] * b[1]);
  806. t04 += t03 >> 56;
  807. t03 &= 0xffffffffffffffL;
  808. t13 = ((sword128)a1[0] * b1[3]) + ((sword128)a1[1] * b1[2])
  809. + ((sword128)a1[2] * b1[1]) + ((sword128)a1[3] * b1[0]);
  810. t14 = ((sword128)a1[1] * b1[3]) + ((sword128)a1[2] * b1[2])
  811. + ((sword128)a1[3] * b1[1]);
  812. t14 += t13 >> 56;
  813. t13 &= 0xffffffffffffffL;
  814. t0 = ((sword128)a[0] * b[0]) + ((sword128)a[4] * b[4]) + t14 + -t04;
  815. t1 = ((sword128)a[0] * b[1]) + ((sword128)a[1] * b[0])
  816. + ((sword128)a[4] * b[5]) + ((sword128)a[5] * b[4])
  817. + ((sword128)a1[2] * b1[3]) + ((sword128)a1[3] * b1[2])
  818. - ((sword128)a[2] * b[3]) - ((sword128)a[3] * b[2]);
  819. o = (sword64)(t0 >> 56); t1 += o; t0 &= 0xffffffffffffffL;
  820. t2 = ((sword128)a[0] * b[2]) + ((sword128)a[1] * b[1])
  821. + ((sword128)a[2] * b[0]) + ((sword128)a[4] * b[6])
  822. + ((sword128)a[5] * b[5]) + ((sword128)a[6] * b[4])
  823. + ((sword128)a1[3] * b1[3]) - ((sword128)a[3] * b[3]);
  824. o = (sword64)(t1 >> 56); t2 += o; t1 &= 0xffffffffffffffL;
  825. t3 = t03 + ((sword128)a[4] * b[7]) + ((sword128)a[5] * b[6])
  826. + ((sword128)a[6] * b[5]) + ((sword128)a[7] * b[4]);
  827. o = (sword64)(t2 >> 56); t3 += o; t2 &= 0xffffffffffffffL;
  828. t4 = ((sword128)a[5] * b[7]) + ((sword128)a[6] * b[6])
  829. + ((sword128)a[7] * b[5]) + ((sword128)a1[0] * b1[0])
  830. - ((sword128)a[0] * b[0]) + t14;
  831. o = (sword64)(t3 >> 56); t4 += o; t3 &= 0xffffffffffffffL;
  832. t5 = ((sword128)a[6] * b[7]) + ((sword128)a[7] * b[6])
  833. + ((sword128)a1[0] * b1[1]) + ((sword128)a1[1] * b1[0])
  834. - ((sword128)a[0] * b[1]) - ((sword128)a[1] * b[0])
  835. + ((sword128)a1[2] * b1[3]) + ((sword128)a1[3] * b1[2]);
  836. o = (sword64)(t4 >> 56); t5 += o; t4 &= 0xffffffffffffffL;
  837. t6 = ((sword128)a[7] * b[7]) + ((sword128)a1[0] * b1[2])
  838. + ((sword128)a1[1] * b1[1]) + ((sword128)a1[2] * b1[0])
  839. - ((sword128)a[0] * b[2]) - ((sword128)a[1] * b[1])
  840. - ((sword128)a[2] * b[0]) + ((sword128)a1[3] * b1[3]);
  841. o = (sword64)(t5 >> 56); t6 += o; t5 &= 0xffffffffffffffL;
  842. t7 = t13 + -t03;
  843. o = (sword64)(t6 >> 56); t7 += o; t6 &= 0xffffffffffffffL;
  844. o = (sword64)(t7 >> 56); t0 += o;
  845. t4 += o; t7 &= 0xffffffffffffffL;
  846. /* Store */
  847. r[0] = (sword64)t0;
  848. r[1] = (sword64)t1;
  849. r[2] = (sword64)t2;
  850. r[3] = (sword64)t3;
  851. r[4] = (sword64)t4;
  852. r[5] = (sword64)t5;
  853. r[6] = (sword64)t6;
  854. r[7] = (sword64)t7;
  855. }
  856. /* Square a field element. r = (a * a) mod (2^448 - 2^224 - 1)
  857. *
  858. * r [in] Field element to hold result.
  859. * a [in] Field element to square.
  860. */
  861. void fe448_sqr(sword64* r, const sword64* a)
  862. {
  863. sword64 o;
  864. sword64 a1[4];
  865. sword128 t0;
  866. sword128 t1;
  867. sword128 t2;
  868. sword128 t3;
  869. sword128 t4;
  870. sword128 t5;
  871. sword128 t6;
  872. sword128 t7;
  873. sword128 t03;
  874. sword128 t04;
  875. sword128 t13;
  876. sword128 t14;
  877. a1[0] = a[0] + a[4];
  878. a1[1] = a[1] + a[5];
  879. a1[2] = a[2] + a[6];
  880. a1[3] = a[3] + a[7];
  881. t03 = ((sword128)a[0] * (2 * a[3])) + ((sword128)a[1] * (2 * a[2]));
  882. t04 = ((sword128)a[1] * (2 * a[3])) + ((sword128)a[2] * a[2]);
  883. t04 += t03 >> 56;
  884. t03 &= 0xffffffffffffffL;
  885. t13 = ((sword128)a1[0] * (2 * a1[3])) + ((sword128)a1[1] * (2 * a1[2]));
  886. t14 = ((sword128)a1[1] * (2 * a1[3])) + ((sword128)a1[2] * a1[2]);
  887. t14 += t13 >> 56;
  888. t13 &= 0xffffffffffffffL;
  889. t0 = ((sword128)a[0] * a[0]) + ((sword128)a[4] * a[4]) + t14 + -t04;
  890. t1 = ((sword128)a[0] * (2 * a[1])) + ((sword128)a[4] * (2 * a[5]))
  891. + ((sword128)a1[2] * (2 * a1[3])) - ((sword128)a[2] * (2 * a[3]));
  892. o = (sword64)(t0 >> 56); t1 += o; t0 &= 0xffffffffffffffL;
  893. t2 = ((sword128)a[0] * (2 * a[2])) + ((sword128)a[1] * a[1])
  894. + ((sword128)a[4] * (2 * a[6])) + ((sword128)a[5] * a[5])
  895. + ((sword128)a1[3] * a1[3]) - ((sword128)a[3] * a[3]);
  896. o = (sword64)(t1 >> 56); t2 += o; t1 &= 0xffffffffffffffL;
  897. t3 = t03 + ((sword128)a[4] * (2 * a[7])) + ((sword128)a[5] * (2 * a[6]));
  898. o = (sword64)(t2 >> 56); t3 += o; t2 &= 0xffffffffffffffL;
  899. t4 = ((sword128)a[5] * (2 * a[7])) + ((sword128)a[6] * a[6])
  900. + ((sword128)a1[0] * a1[0]) - ((sword128)a[0] * a[0]) + t14;
  901. o = (sword64)(t3 >> 56); t4 += o; t3 &= 0xffffffffffffffL;
  902. t5 = ((sword128)a[6] * (2 * a[7])) + ((sword128)a1[0] * (2 * a1[1]))
  903. - ((sword128)a[0] * (2 * a[1])) + ((sword128)a1[2] * (2 * a1[3]));
  904. o = (sword64)(t4 >> 56); t5 += o; t4 &= 0xffffffffffffffL;
  905. t6 = ((sword128)a[7] * a[7]) + ((sword128)a1[0] * (2 * a1[2]))
  906. + ((sword128)a1[1] * a1[1]) - ((sword128)a[0] * (2 * a[2]))
  907. - ((sword128)a[1] * a[1]) + ((sword128)a1[3] * a1[3]);
  908. o = (sword64)(t5 >> 56); t6 += o; t5 &= 0xffffffffffffffL;
  909. t7 = t13 + -t03;
  910. o = (sword64)(t6 >> 56); t7 += o; t6 &= 0xffffffffffffffL;
  911. o = (sword64)(t7 >> 56); t0 += o;
  912. t4 += o; t7 &= 0xffffffffffffffL;
  913. /* Store */
  914. r[0] = (sword64)t0;
  915. r[1] = (sword64)t1;
  916. r[2] = (sword64)t2;
  917. r[3] = (sword64)t3;
  918. r[4] = (sword64)t4;
  919. r[5] = (sword64)t5;
  920. r[6] = (sword64)t6;
  921. r[7] = (sword64)t7;
  922. }
  923. /* Invert the field element. (r * a) mod (2^448 - 2^224 - 1) = 1
  924. * Constant time implementation - using Fermat's little theorem:
  925. * a^(p-1) mod p = 1 => a^(p-2) mod p = 1/a
  926. * For curve448: p - 2 = 2^448 - 2^224 - 3
  927. *
  928. * r [in] Field element to hold result.
  929. * a [in] Field element to invert.
  930. */
  931. void fe448_invert(sword64* r, const sword64* a)
  932. {
  933. sword64 t1[8];
  934. sword64 t2[8];
  935. sword64 t3[8];
  936. sword64 t4[8];
  937. int i;
  938. fe448_sqr(t1, a);
  939. /* t1 = 2 */
  940. fe448_mul(t1, t1, a);
  941. /* t1 = 3 */
  942. fe448_sqr(t2, t1); for (i = 1; i < 2; ++i) fe448_sqr(t2, t2);
  943. /* t2 = c */
  944. fe448_mul(t3, t2, a);
  945. /* t3 = d */
  946. fe448_mul(t1, t2, t1);
  947. /* t1 = f */
  948. fe448_sqr(t2, t1);
  949. /* t2 = 1e */
  950. fe448_mul(t4, t2, a);
  951. /* t4 = 1f */
  952. fe448_sqr(t2, t4); for (i = 1; i < 5; ++i) fe448_sqr(t2, t2);
  953. /* t2 = 3e0 */
  954. fe448_mul(t1, t2, t4);
  955. /* t1 = 3ff */
  956. fe448_sqr(t2, t1); for (i = 1; i < 10; ++i) fe448_sqr(t2, t2);
  957. /* t2 = ffc00 */
  958. fe448_mul(t1, t2, t1);
  959. /* t1 = fffff */
  960. fe448_sqr(t2, t1); for (i = 1; i < 5; ++i) fe448_sqr(t2, t2);
  961. /* t2 = 1ffffe0 */
  962. fe448_mul(t1, t2, t4);
  963. /* t1 = 1ffffff */
  964. fe448_sqr(t2, t1); for (i = 1; i < 25; ++i) fe448_sqr(t2, t2);
  965. /* t2 = 3fffffe000000 */
  966. fe448_mul(t1, t2, t1);
  967. /* t1 = 3ffffffffffff */
  968. fe448_sqr(t2, t1); for (i = 1; i < 5; ++i) fe448_sqr(t2, t2);
  969. /* t2 = 7fffffffffffe0 */
  970. fe448_mul(t1, t2, t4);
  971. /* t1 = 7fffffffffffff */
  972. fe448_sqr(t2, t1); for (i = 1; i < 55; ++i) fe448_sqr(t2, t2);
  973. /* t2 = 3fffffffffffff80000000000000 */
  974. fe448_mul(t1, t2, t1);
  975. /* t1 = 3fffffffffffffffffffffffffff */
  976. fe448_sqr(t2, t1); for (i = 1; i < 110; ++i) fe448_sqr(t2, t2);
  977. /* t2 = fffffffffffffffffffffffffffc000000000000000000000000000 */
  978. fe448_mul(t1, t2, t1);
  979. /* t1 = fffffffffffffffffffffffffffffffffffffffffffffffffffffff */
  980. fe448_sqr(t2, t1); for (i = 1; i < 4; ++i) fe448_sqr(t2, t2);
  981. /* t2 = fffffffffffffffffffffffffffffffffffffffffffffffffffffff0 */
  982. fe448_mul(t3, t3, t2);
  983. /* t3 = fffffffffffffffffffffffffffffffffffffffffffffffffffffffd */
  984. fe448_mul(t1, t3, a);
  985. /* t1 = fffffffffffffffffffffffffffffffffffffffffffffffffffffffe */
  986. fe448_sqr(t1, t1); for (i = 1; i < 224; ++i) fe448_sqr(t1, t1);
  987. /* t1 = fffffffffffffffffffffffffffffffffffffffffffffffffffffffe00000000000000000000000000000000000000000000000000000000 */
  988. fe448_mul(r, t3, t1);
  989. /* r = fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffffffffffffffffffffffffffffffffffffffffffffffffffffd */
  990. }
  991. /* Scalar multiply the point by a number. r = n.a
  992. * Uses Montgomery ladder and only requires the x-ordinate.
  993. *
  994. * r [in] Field element to hold result.
  995. * n [in] Scalar as an array of bytes.
  996. * a [in] Point to multiply - x-ordinate only.
  997. */
  998. int curve448(byte* r, const byte* n, const byte* a)
  999. {
  1000. sword64 x1[8];
  1001. sword64 x2[8];
  1002. sword64 z2[8];
  1003. sword64 x3[8];
  1004. sword64 z3[8];
  1005. sword64 t0[8];
  1006. sword64 t1[8];
  1007. int i;
  1008. unsigned int swap;
  1009. unsigned int b;
  1010. fe448_from_bytes(x1, a);
  1011. fe448_1(x2);
  1012. fe448_0(z2);
  1013. fe448_copy(x3, x1);
  1014. fe448_1(z3);
  1015. swap = 0;
  1016. for (i = 447; i >= 0; --i) {
  1017. b = (n[i >> 3] >> (i & 7)) & 1;
  1018. swap ^= b;
  1019. fe448_cswap(x2, x3, swap);
  1020. fe448_cswap(z2, z3, swap);
  1021. swap = b;
  1022. /* Montgomery Ladder - double and add */
  1023. fe448_add(t0, x2, z2);
  1024. fe448_reduce(t0);
  1025. fe448_add(t1, x3, z3);
  1026. fe448_reduce(t1);
  1027. fe448_sub(x2, x2, z2);
  1028. fe448_sub(x3, x3, z3);
  1029. fe448_mul(t1, t1, x2);
  1030. fe448_mul(z3, x3, t0);
  1031. fe448_sqr(t0, t0);
  1032. fe448_sqr(x2, x2);
  1033. fe448_add(x3, z3, t1);
  1034. fe448_reduce(x3);
  1035. fe448_sqr(x3, x3);
  1036. fe448_sub(z3, z3, t1);
  1037. fe448_sqr(z3, z3);
  1038. fe448_mul(z3, z3, x1);
  1039. fe448_sub(t1, t0, x2);
  1040. fe448_mul(x2, t0, x2);
  1041. fe448_mul39081(z2, t1);
  1042. fe448_add(z2, t0, z2);
  1043. fe448_mul(z2, z2, t1);
  1044. }
  1045. /* Last two bits are 0 - no final swap check required. */
  1046. fe448_invert(z2, z2);
  1047. fe448_mul(x2, x2, z2);
  1048. fe448_to_bytes(r, x2);
  1049. return 0;
  1050. }
  1051. #ifdef HAVE_ED448
  1052. /* Check whether field element is not 0.
  1053. * Must convert to a normalized form before checking.
  1054. *
  1055. * a [in] Field element.
  1056. * returns 0 when zero, and any other value otherwise.
  1057. */
  1058. int fe448_isnonzero(const sword64* a)
  1059. {
  1060. byte b[56];
  1061. int i;
  1062. byte c = 0;
  1063. fe448_to_bytes(b, a);
  1064. for (i = 0; i < 56; i++)
  1065. c |= b[i];
  1066. return c;
  1067. }
  1068. /* Check whether field element is negative.
  1069. * Must convert to a normalized form before checking.
  1070. *
  1071. * a [in] Field element.
  1072. * returns 1 when negative, and 0 otherwise.
  1073. */
  1074. int fe448_isnegative(const sword64* a)
  1075. {
  1076. byte b[56];
  1077. fe448_to_bytes(b, a);
  1078. return b[0] & 1;
  1079. }
  1080. /* Negates the field element. r = -a
  1081. *
  1082. * r [in] Field element to hold result.
  1083. * a [in] Field element.
  1084. */
  1085. void fe448_neg(sword64* r, const sword64* a)
  1086. {
  1087. r[0] = -a[0];
  1088. r[1] = -a[1];
  1089. r[2] = -a[2];
  1090. r[3] = -a[3];
  1091. r[4] = -a[4];
  1092. r[5] = -a[5];
  1093. r[6] = -a[6];
  1094. r[7] = -a[7];
  1095. }
  1096. /* Raise field element to (p-3) / 4: 2^446 - 2^222 - 1
  1097. * Used for calcualting y-ordinate from x-ordinate for Ed448.
  1098. *
  1099. * r [in] Field element to hold result.
  1100. * a [in] Field element to exponentiate.
  1101. */
  1102. void fe448_pow_2_446_222_1(sword64* r, const sword64* a)
  1103. {
  1104. sword64 t1[8];
  1105. sword64 t2[8];
  1106. sword64 t3[8];
  1107. sword64 t4[8];
  1108. sword64 t5[8];
  1109. int i;
  1110. fe448_sqr(t3, a);
  1111. /* t3 = 2 */
  1112. fe448_mul(t1, t3, a);
  1113. /* t1 = 3 */
  1114. fe448_sqr(t5, t1);
  1115. /* t5 = 6 */
  1116. fe448_mul(t5, t5, a);
  1117. /* t5 = 7 */
  1118. fe448_sqr(t2, t1); for (i = 1; i < 2; ++i) fe448_sqr(t2, t2);
  1119. /* t2 = c */
  1120. fe448_mul(t3, t2, t3);
  1121. /* t3 = e */
  1122. fe448_mul(t1, t2, t1);
  1123. /* t1 = f */
  1124. fe448_sqr(t2, t1); for (i = 1; i < 3; ++i) fe448_sqr(t2, t2);
  1125. /* t2 = 78 */
  1126. fe448_mul(t5, t2, t5);
  1127. /* t5 = 7f */
  1128. fe448_sqr(t2, t1); for (i = 1; i < 4; ++i) fe448_sqr(t2, t2);
  1129. /* t2 = f0 */
  1130. fe448_mul(t1, t2, t1);
  1131. /* t1 = ff */
  1132. fe448_mul(t3, t3, t2);
  1133. /* t3 = fe */
  1134. fe448_sqr(t2, t1); for (i = 1; i < 7; ++i) fe448_sqr(t2, t2);
  1135. /* t2 = 7f80 */
  1136. fe448_mul(t5, t2, t5);
  1137. /* t5 = 7fff */
  1138. fe448_sqr(t2, t1); for (i = 1; i < 8; ++i) fe448_sqr(t2, t2);
  1139. /* t2 = ff00 */
  1140. fe448_mul(t1, t2, t1);
  1141. /* t1 = ffff */
  1142. fe448_mul(t3, t3, t2);
  1143. /* t3 = fffe */
  1144. fe448_sqr(t2, t5); for (i = 1; i < 15; ++i) fe448_sqr(t2, t2);
  1145. /* t2 = 3fff8000 */
  1146. fe448_mul(t5, t2, t5);
  1147. /* t5 = 3fffffff */
  1148. fe448_sqr(t2, t1); for (i = 1; i < 16; ++i) fe448_sqr(t2, t2);
  1149. /* t2 = ffff0000 */
  1150. fe448_mul(t1, t2, t1);
  1151. /* t1 = ffffffff */
  1152. fe448_mul(t3, t3, t2);
  1153. /* t3 = fffffffe */
  1154. fe448_sqr(t2, t1); for (i = 1; i < 32; ++i) fe448_sqr(t2, t2);
  1155. /* t2 = ffffffff00000000 */
  1156. fe448_mul(t2, t2, t1);
  1157. /* t2 = ffffffffffffffff */
  1158. fe448_sqr(t1, t2); for (i = 1; i < 64; ++i) fe448_sqr(t1, t1);
  1159. /* t1 = ffffffffffffffff0000000000000000 */
  1160. fe448_mul(t1, t1, t2);
  1161. /* t1 = ffffffffffffffffffffffffffffffff */
  1162. fe448_sqr(t1, t1); for (i = 1; i < 64; ++i) fe448_sqr(t1, t1);
  1163. /* t1 = ffffffffffffffffffffffffffffffff0000000000000000 */
  1164. fe448_mul(t4, t1, t2);
  1165. /* t4 = ffffffffffffffffffffffffffffffffffffffffffffffff */
  1166. fe448_sqr(t2, t4); for (i = 1; i < 32; ++i) fe448_sqr(t2, t2);
  1167. /* t2 = ffffffffffffffffffffffffffffffffffffffffffffffff00000000 */
  1168. fe448_mul(t3, t3, t2);
  1169. /* t3 = fffffffffffffffffffffffffffffffffffffffffffffffffffffffe */
  1170. fe448_sqr(t1, t3); for (i = 1; i < 192; ++i) fe448_sqr(t1, t1);
  1171. /* t1 = fffffffffffffffffffffffffffffffffffffffffffffffffffffffe000000000000000000000000000000000000000000000000 */
  1172. fe448_mul(t1, t1, t4);
  1173. /* t1 = fffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffffffffffffffffffffffffffffffffffffffffffff */
  1174. fe448_sqr(t1, t1); for (i = 1; i < 30; ++i) fe448_sqr(t1, t1);
  1175. /* t1 = 3fffffffffffffffffffffffffffffffffffffffffffffffffffffffbfffffffffffffffffffffffffffffffffffffffffffffffc0000000 */
  1176. fe448_mul(r, t5, t1);
  1177. /* r = 3fffffffffffffffffffffffffffffffffffffffffffffffffffffffbfffffffffffffffffffffffffffffffffffffffffffffffffffffff */
  1178. }
  1179. /* Constant time, conditional move of b into a.
  1180. * a is not changed if the condition is 0.
  1181. *
  1182. * a A field element.
  1183. * b A field element.
  1184. * c If 1 then copy and if 0 then don't copy.
  1185. */
  1186. void fe448_cmov(sword64* a, const sword64* b, int c)
  1187. {
  1188. sword64 m = -(sword64)c;
  1189. sword64 t0 = m & (a[0] ^ b[0]);
  1190. sword64 t1 = m & (a[1] ^ b[1]);
  1191. sword64 t2 = m & (a[2] ^ b[2]);
  1192. sword64 t3 = m & (a[3] ^ b[3]);
  1193. sword64 t4 = m & (a[4] ^ b[4]);
  1194. sword64 t5 = m & (a[5] ^ b[5]);
  1195. sword64 t6 = m & (a[6] ^ b[6]);
  1196. sword64 t7 = m & (a[7] ^ b[7]);
  1197. a[0] ^= t0;
  1198. a[1] ^= t1;
  1199. a[2] ^= t2;
  1200. a[3] ^= t3;
  1201. a[4] ^= t4;
  1202. a[5] ^= t5;
  1203. a[6] ^= t6;
  1204. a[7] ^= t7;
  1205. }
  1206. #endif /* HAVE_ED448 */
  1207. #else
  1208. /* Initialize the field element operations.
  1209. */
  1210. void fe448_init(void)
  1211. {
  1212. }
  1213. /* Convert the field element from a byte array to an array of 28-bits.
  1214. *
  1215. * r [in] Array to encode into.
  1216. * b [in] Byte array.
  1217. */
  1218. void fe448_from_bytes(sword32* r, const unsigned char* b)
  1219. {
  1220. r[ 0] = (((sword32)((b[ 0] ) >> 0)) << 0)
  1221. | (((sword32)((b[ 1] ) >> 0)) << 8)
  1222. | (((sword32)((b[ 2] ) >> 0)) << 16)
  1223. | ((((sword32)((b[ 3] & 0xf )) >> 0)) << 24);
  1224. r[ 1] = (((sword32)((b[ 3] ) >> 4)) << 0)
  1225. | (((sword32)((b[ 4] ) >> 0)) << 4)
  1226. | (((sword32)((b[ 5] ) >> 0)) << 12)
  1227. | (((sword32)((b[ 6] ) >> 0)) << 20);
  1228. r[ 2] = (((sword32)((b[ 7] ) >> 0)) << 0)
  1229. | (((sword32)((b[ 8] ) >> 0)) << 8)
  1230. | (((sword32)((b[ 9] ) >> 0)) << 16)
  1231. | ((((sword32)((b[10] & 0xf )) >> 0)) << 24);
  1232. r[ 3] = (((sword32)((b[10] ) >> 4)) << 0)
  1233. | (((sword32)((b[11] ) >> 0)) << 4)
  1234. | (((sword32)((b[12] ) >> 0)) << 12)
  1235. | (((sword32)((b[13] ) >> 0)) << 20);
  1236. r[ 4] = (((sword32)((b[14] ) >> 0)) << 0)
  1237. | (((sword32)((b[15] ) >> 0)) << 8)
  1238. | (((sword32)((b[16] ) >> 0)) << 16)
  1239. | ((((sword32)((b[17] & 0xf )) >> 0)) << 24);
  1240. r[ 5] = (((sword32)((b[17] ) >> 4)) << 0)
  1241. | (((sword32)((b[18] ) >> 0)) << 4)
  1242. | (((sword32)((b[19] ) >> 0)) << 12)
  1243. | (((sword32)((b[20] ) >> 0)) << 20);
  1244. r[ 6] = (((sword32)((b[21] ) >> 0)) << 0)
  1245. | (((sword32)((b[22] ) >> 0)) << 8)
  1246. | (((sword32)((b[23] ) >> 0)) << 16)
  1247. | ((((sword32)((b[24] & 0xf )) >> 0)) << 24);
  1248. r[ 7] = (((sword32)((b[24] ) >> 4)) << 0)
  1249. | (((sword32)((b[25] ) >> 0)) << 4)
  1250. | (((sword32)((b[26] ) >> 0)) << 12)
  1251. | (((sword32)((b[27] ) >> 0)) << 20);
  1252. r[ 8] = (((sword32)((b[28] ) >> 0)) << 0)
  1253. | (((sword32)((b[29] ) >> 0)) << 8)
  1254. | (((sword32)((b[30] ) >> 0)) << 16)
  1255. | ((((sword32)((b[31] & 0xf )) >> 0)) << 24);
  1256. r[ 9] = (((sword32)((b[31] ) >> 4)) << 0)
  1257. | (((sword32)((b[32] ) >> 0)) << 4)
  1258. | (((sword32)((b[33] ) >> 0)) << 12)
  1259. | (((sword32)((b[34] ) >> 0)) << 20);
  1260. r[10] = (((sword32)((b[35] ) >> 0)) << 0)
  1261. | (((sword32)((b[36] ) >> 0)) << 8)
  1262. | (((sword32)((b[37] ) >> 0)) << 16)
  1263. | ((((sword32)((b[38] & 0xf )) >> 0)) << 24);
  1264. r[11] = (((sword32)((b[38] ) >> 4)) << 0)
  1265. | (((sword32)((b[39] ) >> 0)) << 4)
  1266. | (((sword32)((b[40] ) >> 0)) << 12)
  1267. | (((sword32)((b[41] ) >> 0)) << 20);
  1268. r[12] = (((sword32)((b[42] ) >> 0)) << 0)
  1269. | (((sword32)((b[43] ) >> 0)) << 8)
  1270. | (((sword32)((b[44] ) >> 0)) << 16)
  1271. | ((((sword32)((b[45] & 0xf )) >> 0)) << 24);
  1272. r[13] = (((sword32)((b[45] ) >> 4)) << 0)
  1273. | (((sword32)((b[46] ) >> 0)) << 4)
  1274. | (((sword32)((b[47] ) >> 0)) << 12)
  1275. | (((sword32)((b[48] ) >> 0)) << 20);
  1276. r[14] = (((sword32)((b[49] ) >> 0)) << 0)
  1277. | (((sword32)((b[50] ) >> 0)) << 8)
  1278. | (((sword32)((b[51] ) >> 0)) << 16)
  1279. | ((((sword32)((b[52] & 0xf )) >> 0)) << 24);
  1280. r[15] = (((sword32)((b[52] ) >> 4)) << 0)
  1281. | (((sword32)((b[53] ) >> 0)) << 4)
  1282. | (((sword32)((b[54] ) >> 0)) << 12)
  1283. | (((sword32)((b[55] ) >> 0)) << 20);
  1284. }
  1285. /* Convert the field element to a byte array from an array of 28-bits.
  1286. *
  1287. * b [in] Byte array.
  1288. * a [in] Array to encode into.
  1289. */
  1290. void fe448_to_bytes(unsigned char* b, const sword32* a)
  1291. {
  1292. sword64 t;
  1293. /* Mod */
  1294. sword32 in0 = a[0];
  1295. sword32 in1 = a[1];
  1296. sword32 in2 = a[2];
  1297. sword32 in3 = a[3];
  1298. sword32 in4 = a[4];
  1299. sword32 in5 = a[5];
  1300. sword32 in6 = a[6];
  1301. sword32 in7 = a[7];
  1302. sword32 in8 = a[8];
  1303. sword32 in9 = a[9];
  1304. sword32 in10 = a[10];
  1305. sword32 in11 = a[11];
  1306. sword32 in12 = a[12];
  1307. sword32 in13 = a[13];
  1308. sword32 in14 = a[14];
  1309. sword32 in15 = a[15];
  1310. sword32 o = in15 >> 28;
  1311. in15 -= o << 28;
  1312. in0 += o;
  1313. in8 += o;
  1314. o = (in0 + 1) >> 28;
  1315. o = (o + in1) >> 28;
  1316. o = (o + in2) >> 28;
  1317. o = (o + in3) >> 28;
  1318. o = (o + in4) >> 28;
  1319. o = (o + in5) >> 28;
  1320. o = (o + in6) >> 28;
  1321. o = (o + in7) >> 28;
  1322. o = (o + in8 + 1) >> 28;
  1323. o = (o + in9) >> 28;
  1324. o = (o + in10) >> 28;
  1325. o = (o + in11) >> 28;
  1326. o = (o + in12) >> 28;
  1327. o = (o + in13) >> 28;
  1328. o = (o + in14) >> 28;
  1329. o = (o + in15) >> 28;
  1330. in0 += o;
  1331. in8 += o;
  1332. in15 -= o << 28;
  1333. o = (in0 >> 28); in1 += o; t = o << 28; in0 -= (sword32)t;
  1334. o = (in1 >> 28); in2 += o; t = o << 28; in1 -= (sword32)t;
  1335. o = (in2 >> 28); in3 += o; t = o << 28; in2 -= (sword32)t;
  1336. o = (in3 >> 28); in4 += o; t = o << 28; in3 -= (sword32)t;
  1337. o = (in4 >> 28); in5 += o; t = o << 28; in4 -= (sword32)t;
  1338. o = (in5 >> 28); in6 += o; t = o << 28; in5 -= (sword32)t;
  1339. o = (in6 >> 28); in7 += o; t = o << 28; in6 -= (sword32)t;
  1340. o = (in7 >> 28); in8 += o; t = o << 28; in7 -= (sword32)t;
  1341. o = (in8 >> 28); in9 += o; t = o << 28; in8 -= (sword32)t;
  1342. o = (in9 >> 28); in10 += o; t = o << 28; in9 -= (sword32)t;
  1343. o = (in10 >> 28); in11 += o; t = o << 28; in10 -= (sword32)t;
  1344. o = (in11 >> 28); in12 += o; t = o << 28; in11 -= (sword32)t;
  1345. o = (in12 >> 28); in13 += o; t = o << 28; in12 -= (sword32)t;
  1346. o = (in13 >> 28); in14 += o; t = o << 28; in13 -= (sword32)t;
  1347. o = (in14 >> 28); in15 += o; t = o << 28; in14 -= (sword32)t;
  1348. o = (in15 >> 28); in0 += o;
  1349. in8 += o; t = o << 28; in15 -= (sword32)t;
  1350. /* Output as bytes */
  1351. b[ 0] = (in0 >> 0);
  1352. b[ 1] = (in0 >> 8);
  1353. b[ 2] = (in0 >> 16);
  1354. b[ 3] = (in0 >> 24) + ((in1 >> 0) << 4);
  1355. b[ 4] = (in1 >> 4);
  1356. b[ 5] = (in1 >> 12);
  1357. b[ 6] = (in1 >> 20);
  1358. b[ 7] = (in2 >> 0);
  1359. b[ 8] = (in2 >> 8);
  1360. b[ 9] = (in2 >> 16);
  1361. b[10] = (in2 >> 24) + ((in3 >> 0) << 4);
  1362. b[11] = (in3 >> 4);
  1363. b[12] = (in3 >> 12);
  1364. b[13] = (in3 >> 20);
  1365. b[14] = (in4 >> 0);
  1366. b[15] = (in4 >> 8);
  1367. b[16] = (in4 >> 16);
  1368. b[17] = (in4 >> 24) + ((in5 >> 0) << 4);
  1369. b[18] = (in5 >> 4);
  1370. b[19] = (in5 >> 12);
  1371. b[20] = (in5 >> 20);
  1372. b[21] = (in6 >> 0);
  1373. b[22] = (in6 >> 8);
  1374. b[23] = (in6 >> 16);
  1375. b[24] = (in6 >> 24) + ((in7 >> 0) << 4);
  1376. b[25] = (in7 >> 4);
  1377. b[26] = (in7 >> 12);
  1378. b[27] = (in7 >> 20);
  1379. b[28] = (in8 >> 0);
  1380. b[29] = (in8 >> 8);
  1381. b[30] = (in8 >> 16);
  1382. b[31] = (in8 >> 24) + ((in9 >> 0) << 4);
  1383. b[32] = (in9 >> 4);
  1384. b[33] = (in9 >> 12);
  1385. b[34] = (in9 >> 20);
  1386. b[35] = (in10 >> 0);
  1387. b[36] = (in10 >> 8);
  1388. b[37] = (in10 >> 16);
  1389. b[38] = (in10 >> 24) + ((in11 >> 0) << 4);
  1390. b[39] = (in11 >> 4);
  1391. b[40] = (in11 >> 12);
  1392. b[41] = (in11 >> 20);
  1393. b[42] = (in12 >> 0);
  1394. b[43] = (in12 >> 8);
  1395. b[44] = (in12 >> 16);
  1396. b[45] = (in12 >> 24) + ((in13 >> 0) << 4);
  1397. b[46] = (in13 >> 4);
  1398. b[47] = (in13 >> 12);
  1399. b[48] = (in13 >> 20);
  1400. b[49] = (in14 >> 0);
  1401. b[50] = (in14 >> 8);
  1402. b[51] = (in14 >> 16);
  1403. b[52] = (in14 >> 24) + ((in15 >> 0) << 4);
  1404. b[53] = (in15 >> 4);
  1405. b[54] = (in15 >> 12);
  1406. b[55] = (in15 >> 20);
  1407. }
  1408. /* Set the field element to 0.
  1409. *
  1410. * a [in] Field element.
  1411. */
  1412. void fe448_1(sword32* a)
  1413. {
  1414. a[0] = 1;
  1415. a[1] = 0;
  1416. a[2] = 0;
  1417. a[3] = 0;
  1418. a[4] = 0;
  1419. a[5] = 0;
  1420. a[6] = 0;
  1421. a[7] = 0;
  1422. a[8] = 0;
  1423. a[9] = 0;
  1424. a[10] = 0;
  1425. a[11] = 0;
  1426. a[12] = 0;
  1427. a[13] = 0;
  1428. a[14] = 0;
  1429. a[15] = 0;
  1430. }
  1431. /* Set the field element to 0.
  1432. *
  1433. * a [in] Field element.
  1434. */
  1435. void fe448_0(sword32* a)
  1436. {
  1437. a[0] = 0;
  1438. a[1] = 0;
  1439. a[2] = 0;
  1440. a[3] = 0;
  1441. a[4] = 0;
  1442. a[5] = 0;
  1443. a[6] = 0;
  1444. a[7] = 0;
  1445. a[8] = 0;
  1446. a[9] = 0;
  1447. a[10] = 0;
  1448. a[11] = 0;
  1449. a[12] = 0;
  1450. a[13] = 0;
  1451. a[14] = 0;
  1452. a[15] = 0;
  1453. }
  1454. /* Copy one field element into another: d = a.
  1455. *
  1456. * d [in] Destination field element.
  1457. * a [in] Source field element.
  1458. */
  1459. void fe448_copy(sword32* d, const sword32* a)
  1460. {
  1461. d[0] = a[0];
  1462. d[1] = a[1];
  1463. d[2] = a[2];
  1464. d[3] = a[3];
  1465. d[4] = a[4];
  1466. d[5] = a[5];
  1467. d[6] = a[6];
  1468. d[7] = a[7];
  1469. d[8] = a[8];
  1470. d[9] = a[9];
  1471. d[10] = a[10];
  1472. d[11] = a[11];
  1473. d[12] = a[12];
  1474. d[13] = a[13];
  1475. d[14] = a[14];
  1476. d[15] = a[15];
  1477. }
  1478. /* Conditionally swap the elements.
  1479. * Constant time implementation.
  1480. *
  1481. * a [in] First field element.
  1482. * b [in] Second field element.
  1483. * c [in] Swap when 1. Valid values: 0, 1.
  1484. */
  1485. static void fe448_cswap(sword32* a, sword32* b, int c)
  1486. {
  1487. sword32 mask = -(sword32)c;
  1488. sword32 t0 = (a[0] ^ b[0]) & mask;
  1489. sword32 t1 = (a[1] ^ b[1]) & mask;
  1490. sword32 t2 = (a[2] ^ b[2]) & mask;
  1491. sword32 t3 = (a[3] ^ b[3]) & mask;
  1492. sword32 t4 = (a[4] ^ b[4]) & mask;
  1493. sword32 t5 = (a[5] ^ b[5]) & mask;
  1494. sword32 t6 = (a[6] ^ b[6]) & mask;
  1495. sword32 t7 = (a[7] ^ b[7]) & mask;
  1496. sword32 t8 = (a[8] ^ b[8]) & mask;
  1497. sword32 t9 = (a[9] ^ b[9]) & mask;
  1498. sword32 t10 = (a[10] ^ b[10]) & mask;
  1499. sword32 t11 = (a[11] ^ b[11]) & mask;
  1500. sword32 t12 = (a[12] ^ b[12]) & mask;
  1501. sword32 t13 = (a[13] ^ b[13]) & mask;
  1502. sword32 t14 = (a[14] ^ b[14]) & mask;
  1503. sword32 t15 = (a[15] ^ b[15]) & mask;
  1504. a[0] ^= t0;
  1505. a[1] ^= t1;
  1506. a[2] ^= t2;
  1507. a[3] ^= t3;
  1508. a[4] ^= t4;
  1509. a[5] ^= t5;
  1510. a[6] ^= t6;
  1511. a[7] ^= t7;
  1512. a[8] ^= t8;
  1513. a[9] ^= t9;
  1514. a[10] ^= t10;
  1515. a[11] ^= t11;
  1516. a[12] ^= t12;
  1517. a[13] ^= t13;
  1518. a[14] ^= t14;
  1519. a[15] ^= t15;
  1520. b[0] ^= t0;
  1521. b[1] ^= t1;
  1522. b[2] ^= t2;
  1523. b[3] ^= t3;
  1524. b[4] ^= t4;
  1525. b[5] ^= t5;
  1526. b[6] ^= t6;
  1527. b[7] ^= t7;
  1528. b[8] ^= t8;
  1529. b[9] ^= t9;
  1530. b[10] ^= t10;
  1531. b[11] ^= t11;
  1532. b[12] ^= t12;
  1533. b[13] ^= t13;
  1534. b[14] ^= t14;
  1535. b[15] ^= t15;
  1536. }
  1537. /* Add two field elements. r = (a + b) mod (2^448 - 2^224 - 1)
  1538. *
  1539. * r [in] Field element to hold sum.
  1540. * a [in] Field element to add.
  1541. * b [in] Field element to add.
  1542. */
  1543. void fe448_add(sword32* r, const sword32* a, const sword32* b)
  1544. {
  1545. r[0] = a[0] + b[0];
  1546. r[1] = a[1] + b[1];
  1547. r[2] = a[2] + b[2];
  1548. r[3] = a[3] + b[3];
  1549. r[4] = a[4] + b[4];
  1550. r[5] = a[5] + b[5];
  1551. r[6] = a[6] + b[6];
  1552. r[7] = a[7] + b[7];
  1553. r[8] = a[8] + b[8];
  1554. r[9] = a[9] + b[9];
  1555. r[10] = a[10] + b[10];
  1556. r[11] = a[11] + b[11];
  1557. r[12] = a[12] + b[12];
  1558. r[13] = a[13] + b[13];
  1559. r[14] = a[14] + b[14];
  1560. r[15] = a[15] + b[15];
  1561. }
  1562. /* Subtract a field element from another. r = (a - b) mod (2^448 - 2^224 - 1)
  1563. *
  1564. * r [in] Field element to hold difference.
  1565. * a [in] Field element to subtract from.
  1566. * b [in] Field element to subtract.
  1567. */
  1568. void fe448_sub(sword32* r, const sword32* a, const sword32* b)
  1569. {
  1570. r[0] = a[0] - b[0];
  1571. r[1] = a[1] - b[1];
  1572. r[2] = a[2] - b[2];
  1573. r[3] = a[3] - b[3];
  1574. r[4] = a[4] - b[4];
  1575. r[5] = a[5] - b[5];
  1576. r[6] = a[6] - b[6];
  1577. r[7] = a[7] - b[7];
  1578. r[8] = a[8] - b[8];
  1579. r[9] = a[9] - b[9];
  1580. r[10] = a[10] - b[10];
  1581. r[11] = a[11] - b[11];
  1582. r[12] = a[12] - b[12];
  1583. r[13] = a[13] - b[13];
  1584. r[14] = a[14] - b[14];
  1585. r[15] = a[15] - b[15];
  1586. }
  1587. void fe448_reduce(sword32* a)
  1588. {
  1589. sword64 o;
  1590. o = a[0 ] >> 28; a[1 ] += (sword32)o; a[0 ] -= (sword32)(o << 28);
  1591. o = a[1 ] >> 28; a[2 ] += (sword32)o; a[1 ] -= (sword32)(o << 28);
  1592. o = a[2 ] >> 28; a[3 ] += (sword32)o; a[2 ] -= (sword32)(o << 28);
  1593. o = a[3 ] >> 28; a[4 ] += (sword32)o; a[3 ] -= (sword32)(o << 28);
  1594. o = a[4 ] >> 28; a[5 ] += (sword32)o; a[4 ] -= (sword32)(o << 28);
  1595. o = a[5 ] >> 28; a[6 ] += (sword32)o; a[5 ] -= (sword32)(o << 28);
  1596. o = a[6 ] >> 28; a[7 ] += (sword32)o; a[6 ] -= (sword32)(o << 28);
  1597. o = a[7 ] >> 28; a[8 ] += (sword32)o; a[7 ] -= (sword32)(o << 28);
  1598. o = a[8 ] >> 28; a[9 ] += (sword32)o; a[8 ] -= (sword32)(o << 28);
  1599. o = a[9 ] >> 28; a[10] += (sword32)o; a[9 ] -= (sword32)(o << 28);
  1600. o = a[10] >> 28; a[11] += (sword32)o; a[10] -= (sword32)(o << 28);
  1601. o = a[11] >> 28; a[12] += (sword32)o; a[11] -= (sword32)(o << 28);
  1602. o = a[12] >> 28; a[13] += (sword32)o; a[12] -= (sword32)(o << 28);
  1603. o = a[13] >> 28; a[14] += (sword32)o; a[13] -= (sword32)(o << 28);
  1604. o = a[14] >> 28; a[15] += (sword32)o; a[14] -= (sword32)(o << 28);
  1605. o = a[15] >> 28; a[0] += (sword32)o;
  1606. a[8] += (sword32)o; a[15] -= (sword32)(o << 28);
  1607. }
  1608. /* Mulitply a field element by 39081. r = (39081 * a) mod (2^448 - 2^224 - 1)
  1609. *
  1610. * r [in] Field element to hold result.
  1611. * a [in] Field element to multiply.
  1612. */
  1613. void fe448_mul39081(sword32* r, const sword32* a)
  1614. {
  1615. sword64 t;
  1616. sword32 o;
  1617. sword64 t0 = a[0] * (sword64)39081;
  1618. sword64 t1 = a[1] * (sword64)39081;
  1619. sword64 t2 = a[2] * (sword64)39081;
  1620. sword64 t3 = a[3] * (sword64)39081;
  1621. sword64 t4 = a[4] * (sword64)39081;
  1622. sword64 t5 = a[5] * (sword64)39081;
  1623. sword64 t6 = a[6] * (sword64)39081;
  1624. sword64 t7 = a[7] * (sword64)39081;
  1625. sword64 t8 = a[8] * (sword64)39081;
  1626. sword64 t9 = a[9] * (sword64)39081;
  1627. sword64 t10 = a[10] * (sword64)39081;
  1628. sword64 t11 = a[11] * (sword64)39081;
  1629. sword64 t12 = a[12] * (sword64)39081;
  1630. sword64 t13 = a[13] * (sword64)39081;
  1631. sword64 t14 = a[14] * (sword64)39081;
  1632. sword64 t15 = a[15] * (sword64)39081;
  1633. o = (sword32)(t0 >> 28); t1 += o; t = (sword64)o << 28; t0 -= t;
  1634. o = (sword32)(t1 >> 28); t2 += o; t = (sword64)o << 28; t1 -= t;
  1635. o = (sword32)(t2 >> 28); t3 += o; t = (sword64)o << 28; t2 -= t;
  1636. o = (sword32)(t3 >> 28); t4 += o; t = (sword64)o << 28; t3 -= t;
  1637. o = (sword32)(t4 >> 28); t5 += o; t = (sword64)o << 28; t4 -= t;
  1638. o = (sword32)(t5 >> 28); t6 += o; t = (sword64)o << 28; t5 -= t;
  1639. o = (sword32)(t6 >> 28); t7 += o; t = (sword64)o << 28; t6 -= t;
  1640. o = (sword32)(t7 >> 28); t8 += o; t = (sword64)o << 28; t7 -= t;
  1641. o = (sword32)(t8 >> 28); t9 += o; t = (sword64)o << 28; t8 -= t;
  1642. o = (sword32)(t9 >> 28); t10 += o; t = (sword64)o << 28; t9 -= t;
  1643. o = (sword32)(t10 >> 28); t11 += o; t = (sword64)o << 28; t10 -= t;
  1644. o = (sword32)(t11 >> 28); t12 += o; t = (sword64)o << 28; t11 -= t;
  1645. o = (sword32)(t12 >> 28); t13 += o; t = (sword64)o << 28; t12 -= t;
  1646. o = (sword32)(t13 >> 28); t14 += o; t = (sword64)o << 28; t13 -= t;
  1647. o = (sword32)(t14 >> 28); t15 += o; t = (sword64)o << 28; t14 -= t;
  1648. o = (sword32)(t15 >> 28); t0 += o;
  1649. t8 += o; t = (sword64)o << 28; t15 -= t;
  1650. /* Store */
  1651. r[0] = (sword32)t0;
  1652. r[1] = (sword32)t1;
  1653. r[2] = (sword32)t2;
  1654. r[3] = (sword32)t3;
  1655. r[4] = (sword32)t4;
  1656. r[5] = (sword32)t5;
  1657. r[6] = (sword32)t6;
  1658. r[7] = (sword32)t7;
  1659. r[8] = (sword32)t8;
  1660. r[9] = (sword32)t9;
  1661. r[10] = (sword32)t10;
  1662. r[11] = (sword32)t11;
  1663. r[12] = (sword32)t12;
  1664. r[13] = (sword32)t13;
  1665. r[14] = (sword32)t14;
  1666. r[15] = (sword32)t15;
  1667. }
  1668. /* Mulitply two field elements. r = a * b
  1669. *
  1670. * r [in] Field element to hold result.
  1671. * a [in] Field element to multiply.
  1672. * b [in] Field element to multiply.
  1673. */
  1674. static WC_INLINE void fe448_mul_8(sword32* r, const sword32* a, const sword32* b)
  1675. {
  1676. sword64 t;
  1677. sword64 t0 = (sword64)a[ 0] * b[ 0];
  1678. sword64 t1 = (sword64)a[ 0] * b[ 1];
  1679. sword64 t101 = (sword64)a[ 1] * b[ 0];
  1680. sword64 t2 = (sword64)a[ 0] * b[ 2];
  1681. sword64 t102 = (sword64)a[ 1] * b[ 1];
  1682. sword64 t202 = (sword64)a[ 2] * b[ 0];
  1683. sword64 t3 = (sword64)a[ 0] * b[ 3];
  1684. sword64 t103 = (sword64)a[ 1] * b[ 2];
  1685. sword64 t203 = (sword64)a[ 2] * b[ 1];
  1686. sword64 t303 = (sword64)a[ 3] * b[ 0];
  1687. sword64 t4 = (sword64)a[ 0] * b[ 4];
  1688. sword64 t104 = (sword64)a[ 1] * b[ 3];
  1689. sword64 t204 = (sword64)a[ 2] * b[ 2];
  1690. sword64 t304 = (sword64)a[ 3] * b[ 1];
  1691. sword64 t404 = (sword64)a[ 4] * b[ 0];
  1692. sword64 t5 = (sword64)a[ 0] * b[ 5];
  1693. sword64 t105 = (sword64)a[ 1] * b[ 4];
  1694. sword64 t205 = (sword64)a[ 2] * b[ 3];
  1695. sword64 t305 = (sword64)a[ 3] * b[ 2];
  1696. sword64 t405 = (sword64)a[ 4] * b[ 1];
  1697. sword64 t505 = (sword64)a[ 5] * b[ 0];
  1698. sword64 t6 = (sword64)a[ 0] * b[ 6];
  1699. sword64 t106 = (sword64)a[ 1] * b[ 5];
  1700. sword64 t206 = (sword64)a[ 2] * b[ 4];
  1701. sword64 t306 = (sword64)a[ 3] * b[ 3];
  1702. sword64 t406 = (sword64)a[ 4] * b[ 2];
  1703. sword64 t506 = (sword64)a[ 5] * b[ 1];
  1704. sword64 t606 = (sword64)a[ 6] * b[ 0];
  1705. sword64 t7 = (sword64)a[ 0] * b[ 7];
  1706. sword64 t107 = (sword64)a[ 1] * b[ 6];
  1707. sword64 t207 = (sword64)a[ 2] * b[ 5];
  1708. sword64 t307 = (sword64)a[ 3] * b[ 4];
  1709. sword64 t407 = (sword64)a[ 4] * b[ 3];
  1710. sword64 t507 = (sword64)a[ 5] * b[ 2];
  1711. sword64 t607 = (sword64)a[ 6] * b[ 1];
  1712. sword64 t707 = (sword64)a[ 7] * b[ 0];
  1713. sword64 t8 = (sword64)a[ 1] * b[ 7];
  1714. sword64 t108 = (sword64)a[ 2] * b[ 6];
  1715. sword64 t208 = (sword64)a[ 3] * b[ 5];
  1716. sword64 t308 = (sword64)a[ 4] * b[ 4];
  1717. sword64 t408 = (sword64)a[ 5] * b[ 3];
  1718. sword64 t508 = (sword64)a[ 6] * b[ 2];
  1719. sword64 t608 = (sword64)a[ 7] * b[ 1];
  1720. sword64 t9 = (sword64)a[ 2] * b[ 7];
  1721. sword64 t109 = (sword64)a[ 3] * b[ 6];
  1722. sword64 t209 = (sword64)a[ 4] * b[ 5];
  1723. sword64 t309 = (sword64)a[ 5] * b[ 4];
  1724. sword64 t409 = (sword64)a[ 6] * b[ 3];
  1725. sword64 t509 = (sword64)a[ 7] * b[ 2];
  1726. sword64 t10 = (sword64)a[ 3] * b[ 7];
  1727. sword64 t110 = (sword64)a[ 4] * b[ 6];
  1728. sword64 t210 = (sword64)a[ 5] * b[ 5];
  1729. sword64 t310 = (sword64)a[ 6] * b[ 4];
  1730. sword64 t410 = (sword64)a[ 7] * b[ 3];
  1731. sword64 t11 = (sword64)a[ 4] * b[ 7];
  1732. sword64 t111 = (sword64)a[ 5] * b[ 6];
  1733. sword64 t211 = (sword64)a[ 6] * b[ 5];
  1734. sword64 t311 = (sword64)a[ 7] * b[ 4];
  1735. sword64 t12 = (sword64)a[ 5] * b[ 7];
  1736. sword64 t112 = (sword64)a[ 6] * b[ 6];
  1737. sword64 t212 = (sword64)a[ 7] * b[ 5];
  1738. sword64 t13 = (sword64)a[ 6] * b[ 7];
  1739. sword64 t113 = (sword64)a[ 7] * b[ 6];
  1740. sword64 t14 = (sword64)a[ 7] * b[ 7];
  1741. t1 += t101;
  1742. t2 += t102; t2 += t202;
  1743. t3 += t103; t3 += t203; t3 += t303;
  1744. t4 += t104; t4 += t204; t4 += t304; t4 += t404;
  1745. t5 += t105; t5 += t205; t5 += t305; t5 += t405; t5 += t505;
  1746. t6 += t106; t6 += t206; t6 += t306; t6 += t406; t6 += t506;
  1747. t6 += t606;
  1748. t7 += t107; t7 += t207; t7 += t307; t7 += t407; t7 += t507;
  1749. t7 += t607;
  1750. t7 += t707;
  1751. t8 += t108; t8 += t208; t8 += t308; t8 += t408; t8 += t508;
  1752. t8 += t608;
  1753. t9 += t109; t9 += t209; t9 += t309; t9 += t409; t9 += t509;
  1754. t10 += t110; t10 += t210; t10 += t310; t10 += t410;
  1755. t11 += t111; t11 += t211; t11 += t311;
  1756. t12 += t112; t12 += t212;
  1757. t13 += t113;
  1758. sword64 o = t14 >> 28;
  1759. sword64 t15 = o;
  1760. t14 -= o << 28;
  1761. o = (t0 >> 28); t1 += o; t = o << 28; t0 -= t;
  1762. o = (t1 >> 28); t2 += o; t = o << 28; t1 -= t;
  1763. o = (t2 >> 28); t3 += o; t = o << 28; t2 -= t;
  1764. o = (t3 >> 28); t4 += o; t = o << 28; t3 -= t;
  1765. o = (t4 >> 28); t5 += o; t = o << 28; t4 -= t;
  1766. o = (t5 >> 28); t6 += o; t = o << 28; t5 -= t;
  1767. o = (t6 >> 28); t7 += o; t = o << 28; t6 -= t;
  1768. o = (t7 >> 28); t8 += o; t = o << 28; t7 -= t;
  1769. o = (t8 >> 28); t9 += o; t = o << 28; t8 -= t;
  1770. o = (t9 >> 28); t10 += o; t = o << 28; t9 -= t;
  1771. o = (t10 >> 28); t11 += o; t = o << 28; t10 -= t;
  1772. o = (t11 >> 28); t12 += o; t = o << 28; t11 -= t;
  1773. o = (t12 >> 28); t13 += o; t = o << 28; t12 -= t;
  1774. o = (t13 >> 28); t14 += o; t = o << 28; t13 -= t;
  1775. o = (t14 >> 28); t15 += o; t = o << 28; t14 -= t;
  1776. o = (t15 >> 28); t0 += o;
  1777. t8 += o; t = o << 28; t15 -= t;
  1778. /* Store */
  1779. r[0] = (sword32)t0;
  1780. r[1] = (sword32)t1;
  1781. r[2] = (sword32)t2;
  1782. r[3] = (sword32)t3;
  1783. r[4] = (sword32)t4;
  1784. r[5] = (sword32)t5;
  1785. r[6] = (sword32)t6;
  1786. r[7] = (sword32)t7;
  1787. r[8] = (sword32)t8;
  1788. r[9] = (sword32)t9;
  1789. r[10] = (sword32)t10;
  1790. r[11] = (sword32)t11;
  1791. r[12] = (sword32)t12;
  1792. r[13] = (sword32)t13;
  1793. r[14] = (sword32)t14;
  1794. r[15] = (sword32)t15;
  1795. }
  1796. /* Mulitply two field elements. r = (a * b) mod (2^448 - 2^224 - 1)
  1797. *
  1798. * r [in] Field element to hold result.
  1799. * a [in] Field element to multiply.
  1800. * b [in] Field element to multiply.
  1801. */
  1802. void fe448_mul(sword32* r, const sword32* a, const sword32* b)
  1803. {
  1804. sword32 r0[16];
  1805. sword32 r1[16];
  1806. sword32* a1 = r1;
  1807. sword32 b1[8];
  1808. sword32 r2[16];
  1809. a1[0] = a[0] + a[8];
  1810. a1[1] = a[1] + a[9];
  1811. a1[2] = a[2] + a[10];
  1812. a1[3] = a[3] + a[11];
  1813. a1[4] = a[4] + a[12];
  1814. a1[5] = a[5] + a[13];
  1815. a1[6] = a[6] + a[14];
  1816. a1[7] = a[7] + a[15];
  1817. b1[0] = b[0] + b[8];
  1818. b1[1] = b[1] + b[9];
  1819. b1[2] = b[2] + b[10];
  1820. b1[3] = b[3] + b[11];
  1821. b1[4] = b[4] + b[12];
  1822. b1[5] = b[5] + b[13];
  1823. b1[6] = b[6] + b[14];
  1824. b1[7] = b[7] + b[15];
  1825. fe448_mul_8(r2, a + 8, b + 8);
  1826. fe448_mul_8(r0, a, b);
  1827. fe448_mul_8(r1, a1, b1);
  1828. r[ 0] = r0[ 0] + r2[ 0] + r1[ 8] - r0[ 8];
  1829. r[ 1] = r0[ 1] + r2[ 1] + r1[ 9] - r0[ 9];
  1830. r[ 2] = r0[ 2] + r2[ 2] + r1[10] - r0[10];
  1831. r[ 3] = r0[ 3] + r2[ 3] + r1[11] - r0[11];
  1832. r[ 4] = r0[ 4] + r2[ 4] + r1[12] - r0[12];
  1833. r[ 5] = r0[ 5] + r2[ 5] + r1[13] - r0[13];
  1834. r[ 6] = r0[ 6] + r2[ 6] + r1[14] - r0[14];
  1835. r[ 7] = r0[ 7] + r2[ 7] + r1[15] - r0[15];
  1836. r[ 8] = r2[ 8] + r1[ 0] - r0[ 0] + r1[ 8];
  1837. r[ 9] = r2[ 9] + r1[ 1] - r0[ 1] + r1[ 9];
  1838. r[10] = r2[10] + r1[ 2] - r0[ 2] + r1[10];
  1839. r[11] = r2[11] + r1[ 3] - r0[ 3] + r1[11];
  1840. r[12] = r2[12] + r1[ 4] - r0[ 4] + r1[12];
  1841. r[13] = r2[13] + r1[ 5] - r0[ 5] + r1[13];
  1842. r[14] = r2[14] + r1[ 6] - r0[ 6] + r1[14];
  1843. r[15] = r2[15] + r1[ 7] - r0[ 7] + r1[15];
  1844. }
  1845. /* Square a field element. r = a * a
  1846. *
  1847. * r [in] Field element to hold result.
  1848. * a [in] Field element to square.
  1849. */
  1850. static WC_INLINE void fe448_sqr_8(sword32* r, const sword32* a)
  1851. {
  1852. sword64 o;
  1853. sword64 t15;
  1854. sword64 t;
  1855. sword64 t0 = (sword64)a[ 0] * a[ 0];
  1856. sword64 t1 = 2 * (sword64)a[ 0] * a[ 1];
  1857. sword64 t2 = 2 * (sword64)a[ 0] * a[ 2];
  1858. sword64 t102 = (sword64)a[ 1] * a[ 1];
  1859. sword64 t3 = 2 * (sword64)a[ 0] * a[ 3];
  1860. sword64 t103 = 2 * (sword64)a[ 1] * a[ 2];
  1861. sword64 t4 = 2 * (sword64)a[ 0] * a[ 4];
  1862. sword64 t104 = 2 * (sword64)a[ 1] * a[ 3];
  1863. sword64 t204 = (sword64)a[ 2] * a[ 2];
  1864. sword64 t5 = 2 * (sword64)a[ 0] * a[ 5];
  1865. sword64 t105 = 2 * (sword64)a[ 1] * a[ 4];
  1866. sword64 t205 = 2 * (sword64)a[ 2] * a[ 3];
  1867. sword64 t6 = 2 * (sword64)a[ 0] * a[ 6];
  1868. sword64 t106 = 2 * (sword64)a[ 1] * a[ 5];
  1869. sword64 t206 = 2 * (sword64)a[ 2] * a[ 4];
  1870. sword64 t306 = (sword64)a[ 3] * a[ 3];
  1871. sword64 t7 = 2 * (sword64)a[ 0] * a[ 7];
  1872. sword64 t107 = 2 * (sword64)a[ 1] * a[ 6];
  1873. sword64 t207 = 2 * (sword64)a[ 2] * a[ 5];
  1874. sword64 t307 = 2 * (sword64)a[ 3] * a[ 4];
  1875. sword64 t8 = 2 * (sword64)a[ 1] * a[ 7];
  1876. sword64 t108 = 2 * (sword64)a[ 2] * a[ 6];
  1877. sword64 t208 = 2 * (sword64)a[ 3] * a[ 5];
  1878. sword64 t308 = (sword64)a[ 4] * a[ 4];
  1879. sword64 t9 = 2 * (sword64)a[ 2] * a[ 7];
  1880. sword64 t109 = 2 * (sword64)a[ 3] * a[ 6];
  1881. sword64 t209 = 2 * (sword64)a[ 4] * a[ 5];
  1882. sword64 t10 = 2 * (sword64)a[ 3] * a[ 7];
  1883. sword64 t110 = 2 * (sword64)a[ 4] * a[ 6];
  1884. sword64 t210 = (sword64)a[ 5] * a[ 5];
  1885. sword64 t11 = 2 * (sword64)a[ 4] * a[ 7];
  1886. sword64 t111 = 2 * (sword64)a[ 5] * a[ 6];
  1887. sword64 t12 = 2 * (sword64)a[ 5] * a[ 7];
  1888. sword64 t112 = (sword64)a[ 6] * a[ 6];
  1889. sword64 t13 = 2 * (sword64)a[ 6] * a[ 7];
  1890. sword64 t14 = (sword64)a[ 7] * a[ 7];
  1891. t2 += t102;
  1892. t3 += t103;
  1893. t4 += t104; t4 += t204;
  1894. t5 += t105; t5 += t205;
  1895. t6 += t106; t6 += t206; t6 += t306;
  1896. t7 += t107; t7 += t207; t7 += t307;
  1897. t8 += t108; t8 += t208; t8 += t308;
  1898. t9 += t109; t9 += t209;
  1899. t10 += t110; t10 += t210;
  1900. t11 += t111;
  1901. t12 += t112;
  1902. o = t14 >> 28;
  1903. t15 = o;
  1904. t14 -= o << 28;
  1905. o = (t0 >> 28); t1 += o; t = o << 28; t0 -= t;
  1906. o = (t1 >> 28); t2 += o; t = o << 28; t1 -= t;
  1907. o = (t2 >> 28); t3 += o; t = o << 28; t2 -= t;
  1908. o = (t3 >> 28); t4 += o; t = o << 28; t3 -= t;
  1909. o = (t4 >> 28); t5 += o; t = o << 28; t4 -= t;
  1910. o = (t5 >> 28); t6 += o; t = o << 28; t5 -= t;
  1911. o = (t6 >> 28); t7 += o; t = o << 28; t6 -= t;
  1912. o = (t7 >> 28); t8 += o; t = o << 28; t7 -= t;
  1913. o = (t8 >> 28); t9 += o; t = o << 28; t8 -= t;
  1914. o = (t9 >> 28); t10 += o; t = o << 28; t9 -= t;
  1915. o = (t10 >> 28); t11 += o; t = o << 28; t10 -= t;
  1916. o = (t11 >> 28); t12 += o; t = o << 28; t11 -= t;
  1917. o = (t12 >> 28); t13 += o; t = o << 28; t12 -= t;
  1918. o = (t13 >> 28); t14 += o; t = o << 28; t13 -= t;
  1919. o = (t14 >> 28); t15 += o; t = o << 28; t14 -= t;
  1920. o = (t15 >> 28); t0 += o;
  1921. t8 += o; t = o << 28; t15 -= t;
  1922. /* Store */
  1923. r[0] = (sword32)t0;
  1924. r[1] = (sword32)t1;
  1925. r[2] = (sword32)t2;
  1926. r[3] = (sword32)t3;
  1927. r[4] = (sword32)t4;
  1928. r[5] = (sword32)t5;
  1929. r[6] = (sword32)t6;
  1930. r[7] = (sword32)t7;
  1931. r[8] = (sword32)t8;
  1932. r[9] = (sword32)t9;
  1933. r[10] = (sword32)t10;
  1934. r[11] = (sword32)t11;
  1935. r[12] = (sword32)t12;
  1936. r[13] = (sword32)t13;
  1937. r[14] = (sword32)t14;
  1938. r[15] = (sword32)t15;
  1939. }
  1940. /* Square a field element. r = (a * a) mod (2^448 - 2^224 - 1)
  1941. *
  1942. * r [in] Field element to hold result.
  1943. * a [in] Field element to square.
  1944. */
  1945. void fe448_sqr(sword32* r, const sword32* a)
  1946. {
  1947. sword32 r0[16];
  1948. sword32 r1[16];
  1949. sword32* a1 = r1;
  1950. sword32 r2[16];
  1951. a1[0] = a[0] + a[8];
  1952. a1[1] = a[1] + a[9];
  1953. a1[2] = a[2] + a[10];
  1954. a1[3] = a[3] + a[11];
  1955. a1[4] = a[4] + a[12];
  1956. a1[5] = a[5] + a[13];
  1957. a1[6] = a[6] + a[14];
  1958. a1[7] = a[7] + a[15];
  1959. fe448_sqr_8(r2, a + 8);
  1960. fe448_sqr_8(r0, a);
  1961. fe448_sqr_8(r1, a1);
  1962. r[ 0] = r0[ 0] + r2[ 0] + r1[ 8] - r0[ 8];
  1963. r[ 1] = r0[ 1] + r2[ 1] + r1[ 9] - r0[ 9];
  1964. r[ 2] = r0[ 2] + r2[ 2] + r1[10] - r0[10];
  1965. r[ 3] = r0[ 3] + r2[ 3] + r1[11] - r0[11];
  1966. r[ 4] = r0[ 4] + r2[ 4] + r1[12] - r0[12];
  1967. r[ 5] = r0[ 5] + r2[ 5] + r1[13] - r0[13];
  1968. r[ 6] = r0[ 6] + r2[ 6] + r1[14] - r0[14];
  1969. r[ 7] = r0[ 7] + r2[ 7] + r1[15] - r0[15];
  1970. r[ 8] = r2[ 8] + r1[ 0] - r0[ 0] + r1[ 8];
  1971. r[ 9] = r2[ 9] + r1[ 1] - r0[ 1] + r1[ 9];
  1972. r[10] = r2[10] + r1[ 2] - r0[ 2] + r1[10];
  1973. r[11] = r2[11] + r1[ 3] - r0[ 3] + r1[11];
  1974. r[12] = r2[12] + r1[ 4] - r0[ 4] + r1[12];
  1975. r[13] = r2[13] + r1[ 5] - r0[ 5] + r1[13];
  1976. r[14] = r2[14] + r1[ 6] - r0[ 6] + r1[14];
  1977. r[15] = r2[15] + r1[ 7] - r0[ 7] + r1[15];
  1978. }
  1979. /* Invert the field element. (r * a) mod (2^448 - 2^224 - 1) = 1
  1980. * Constant time implementation - using Fermat's little theorem:
  1981. * a^(p-1) mod p = 1 => a^(p-2) mod p = 1/a
  1982. * For curve448: p - 2 = 2^448 - 2^224 - 3
  1983. *
  1984. * r [in] Field element to hold result.
  1985. * a [in] Field element to invert.
  1986. */
  1987. void fe448_invert(sword32* r, const sword32* a)
  1988. {
  1989. sword32 t1[16];
  1990. sword32 t2[16];
  1991. sword32 t3[16];
  1992. sword32 t4[16];
  1993. int i;
  1994. fe448_sqr(t1, a);
  1995. /* t1 = 2 */
  1996. fe448_mul(t1, t1, a);
  1997. /* t1 = 3 */
  1998. fe448_sqr(t2, t1); for (i = 1; i < 2; ++i) fe448_sqr(t2, t2);
  1999. /* t2 = c */
  2000. fe448_mul(t3, t2, a);
  2001. /* t3 = d */
  2002. fe448_mul(t1, t2, t1);
  2003. /* t1 = f */
  2004. fe448_sqr(t2, t1);
  2005. /* t2 = 1e */
  2006. fe448_mul(t4, t2, a);
  2007. /* t4 = 1f */
  2008. fe448_sqr(t2, t4); for (i = 1; i < 5; ++i) fe448_sqr(t2, t2);
  2009. /* t2 = 3e0 */
  2010. fe448_mul(t1, t2, t4);
  2011. /* t1 = 3ff */
  2012. fe448_sqr(t2, t1); for (i = 1; i < 10; ++i) fe448_sqr(t2, t2);
  2013. /* t2 = ffc00 */
  2014. fe448_mul(t1, t2, t1);
  2015. /* t1 = fffff */
  2016. fe448_sqr(t2, t1); for (i = 1; i < 5; ++i) fe448_sqr(t2, t2);
  2017. /* t2 = 1ffffe0 */
  2018. fe448_mul(t1, t2, t4);
  2019. /* t1 = 1ffffff */
  2020. fe448_sqr(t2, t1); for (i = 1; i < 25; ++i) fe448_sqr(t2, t2);
  2021. /* t2 = 3fffffe000000 */
  2022. fe448_mul(t1, t2, t1);
  2023. /* t1 = 3ffffffffffff */
  2024. fe448_sqr(t2, t1); for (i = 1; i < 5; ++i) fe448_sqr(t2, t2);
  2025. /* t2 = 7fffffffffffe0 */
  2026. fe448_mul(t1, t2, t4);
  2027. /* t1 = 7fffffffffffff */
  2028. fe448_sqr(t2, t1); for (i = 1; i < 55; ++i) fe448_sqr(t2, t2);
  2029. /* t2 = 3fffffffffffff80000000000000 */
  2030. fe448_mul(t1, t2, t1);
  2031. /* t1 = 3fffffffffffffffffffffffffff */
  2032. fe448_sqr(t2, t1); for (i = 1; i < 110; ++i) fe448_sqr(t2, t2);
  2033. /* t2 = fffffffffffffffffffffffffffc000000000000000000000000000 */
  2034. fe448_mul(t1, t2, t1);
  2035. /* t1 = fffffffffffffffffffffffffffffffffffffffffffffffffffffff */
  2036. fe448_sqr(t2, t1); for (i = 1; i < 4; ++i) fe448_sqr(t2, t2);
  2037. /* t2 = fffffffffffffffffffffffffffffffffffffffffffffffffffffff0 */
  2038. fe448_mul(t3, t3, t2);
  2039. /* t3 = fffffffffffffffffffffffffffffffffffffffffffffffffffffffd */
  2040. fe448_mul(t1, t3, a);
  2041. /* t1 = fffffffffffffffffffffffffffffffffffffffffffffffffffffffe */
  2042. fe448_sqr(t1, t1); for (i = 1; i < 224; ++i) fe448_sqr(t1, t1);
  2043. /* t1 = fffffffffffffffffffffffffffffffffffffffffffffffffffffffe00000000000000000000000000000000000000000000000000000000 */
  2044. fe448_mul(r, t3, t1);
  2045. /* r = fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffffffffffffffffffffffffffffffffffffffffffffffffffffd */
  2046. }
  2047. /* Scalar multiply the point by a number. r = n.a
  2048. * Uses Montgomery ladder and only requires the x-ordinate.
  2049. *
  2050. * r [in] Field element to hold result.
  2051. * n [in] Scalar as an array of bytes.
  2052. * a [in] Point to multiply - x-ordinate only.
  2053. */
  2054. int curve448(byte* r, const byte* n, const byte* a)
  2055. {
  2056. sword32 x1[16];
  2057. sword32 x2[16];
  2058. sword32 z2[16];
  2059. sword32 x3[16];
  2060. sword32 z3[16];
  2061. sword32 t0[16];
  2062. sword32 t1[16];
  2063. int i;
  2064. unsigned int swap;
  2065. unsigned int b;
  2066. fe448_from_bytes(x1, a);
  2067. fe448_1(x2);
  2068. fe448_0(z2);
  2069. fe448_copy(x3, x1);
  2070. fe448_1(z3);
  2071. swap = 0;
  2072. for (i = 447; i >= 0; --i) {
  2073. b = (n[i >> 3] >> (i & 7)) & 1;
  2074. swap ^= b;
  2075. fe448_cswap(x2, x3, swap);
  2076. fe448_cswap(z2, z3, swap);
  2077. swap = b;
  2078. /* Montgomery Ladder - double and add */
  2079. fe448_add(t0, x2, z2);
  2080. fe448_reduce(t0);
  2081. fe448_add(t1, x3, z3);
  2082. fe448_reduce(t1);
  2083. fe448_sub(x2, x2, z2);
  2084. fe448_sub(x3, x3, z3);
  2085. fe448_mul(t1, t1, x2);
  2086. fe448_mul(z3, x3, t0);
  2087. fe448_sqr(t0, t0);
  2088. fe448_sqr(x2, x2);
  2089. fe448_add(x3, z3, t1);
  2090. fe448_reduce(x3);
  2091. fe448_sqr(x3, x3);
  2092. fe448_sub(z3, z3, t1);
  2093. fe448_sqr(z3, z3);
  2094. fe448_mul(z3, z3, x1);
  2095. fe448_sub(t1, t0, x2);
  2096. fe448_mul(x2, t0, x2);
  2097. fe448_mul39081(z2, t1);
  2098. fe448_add(z2, t0, z2);
  2099. fe448_mul(z2, z2, t1);
  2100. }
  2101. /* Last two bits are 0 - no final swap check required. */
  2102. fe448_invert(z2, z2);
  2103. fe448_mul(x2, x2, z2);
  2104. fe448_to_bytes(r, x2);
  2105. return 0;
  2106. }
  2107. #ifdef HAVE_ED448
  2108. /* Check whether field element is not 0.
  2109. * Must convert to a normalized form before checking.
  2110. *
  2111. * a [in] Field element.
  2112. * returns 0 when zero, and any other value otherwise.
  2113. */
  2114. int fe448_isnonzero(const sword32* a)
  2115. {
  2116. byte b[56];
  2117. int i;
  2118. byte c = 0;
  2119. fe448_to_bytes(b, a);
  2120. for (i = 0; i < 56; i++)
  2121. c |= b[i];
  2122. return c;
  2123. }
  2124. /* Check whether field element is negative.
  2125. * Must convert to a normalized form before checking.
  2126. *
  2127. * a [in] Field element.
  2128. * returns 1 when negative, and 0 otherwise.
  2129. */
  2130. int fe448_isnegative(const sword32* a)
  2131. {
  2132. byte b[56];
  2133. fe448_to_bytes(b, a);
  2134. return b[0] & 1;
  2135. }
  2136. /* Negates the field element. r = -a
  2137. *
  2138. * r [in] Field element to hold result.
  2139. * a [in] Field element.
  2140. */
  2141. void fe448_neg(sword32* r, const sword32* a)
  2142. {
  2143. r[0] = -a[0];
  2144. r[1] = -a[1];
  2145. r[2] = -a[2];
  2146. r[3] = -a[3];
  2147. r[4] = -a[4];
  2148. r[5] = -a[5];
  2149. r[6] = -a[6];
  2150. r[7] = -a[7];
  2151. r[8] = -a[8];
  2152. r[9] = -a[9];
  2153. r[10] = -a[10];
  2154. r[11] = -a[11];
  2155. r[12] = -a[12];
  2156. r[13] = -a[13];
  2157. r[14] = -a[14];
  2158. r[15] = -a[15];
  2159. }
  2160. /* Raise field element to (p-3) / 4: 2^446 - 2^222 - 1
  2161. * Used for calcualting y-ordinate from x-ordinate for Ed448.
  2162. *
  2163. * r [in] Field element to hold result.
  2164. * a [in] Field element to exponentiate.
  2165. */
  2166. void fe448_pow_2_446_222_1(sword32* r, const sword32* a)
  2167. {
  2168. sword32 t1[16];
  2169. sword32 t2[16];
  2170. sword32 t3[16];
  2171. sword32 t4[16];
  2172. sword32 t5[16];
  2173. int i;
  2174. fe448_sqr(t3, a);
  2175. /* t3 = 2 */
  2176. fe448_mul(t1, t3, a);
  2177. /* t1 = 3 */
  2178. fe448_sqr(t5, t1);
  2179. /* t5 = 6 */
  2180. fe448_mul(t5, t5, a);
  2181. /* t5 = 7 */
  2182. fe448_sqr(t2, t1); for (i = 1; i < 2; ++i) fe448_sqr(t2, t2);
  2183. /* t2 = c */
  2184. fe448_mul(t3, t2, t3);
  2185. /* t3 = e */
  2186. fe448_mul(t1, t2, t1);
  2187. /* t1 = f */
  2188. fe448_sqr(t2, t1); for (i = 1; i < 3; ++i) fe448_sqr(t2, t2);
  2189. /* t2 = 78 */
  2190. fe448_mul(t5, t2, t5);
  2191. /* t5 = 7f */
  2192. fe448_sqr(t2, t1); for (i = 1; i < 4; ++i) fe448_sqr(t2, t2);
  2193. /* t2 = f0 */
  2194. fe448_mul(t1, t2, t1);
  2195. /* t1 = ff */
  2196. fe448_mul(t3, t3, t2);
  2197. /* t3 = fe */
  2198. fe448_sqr(t2, t1); for (i = 1; i < 7; ++i) fe448_sqr(t2, t2);
  2199. /* t2 = 7f80 */
  2200. fe448_mul(t5, t2, t5);
  2201. /* t5 = 7fff */
  2202. fe448_sqr(t2, t1); for (i = 1; i < 8; ++i) fe448_sqr(t2, t2);
  2203. /* t2 = ff00 */
  2204. fe448_mul(t1, t2, t1);
  2205. /* t1 = ffff */
  2206. fe448_mul(t3, t3, t2);
  2207. /* t3 = fffe */
  2208. fe448_sqr(t2, t5); for (i = 1; i < 15; ++i) fe448_sqr(t2, t2);
  2209. /* t2 = 3fff8000 */
  2210. fe448_mul(t5, t2, t5);
  2211. /* t5 = 3fffffff */
  2212. fe448_sqr(t2, t1); for (i = 1; i < 16; ++i) fe448_sqr(t2, t2);
  2213. /* t2 = ffff0000 */
  2214. fe448_mul(t1, t2, t1);
  2215. /* t1 = ffffffff */
  2216. fe448_mul(t3, t3, t2);
  2217. /* t3 = fffffffe */
  2218. fe448_sqr(t2, t1); for (i = 1; i < 32; ++i) fe448_sqr(t2, t2);
  2219. /* t2 = ffffffff00000000 */
  2220. fe448_mul(t2, t2, t1);
  2221. /* t2 = ffffffffffffffff */
  2222. fe448_sqr(t1, t2); for (i = 1; i < 64; ++i) fe448_sqr(t1, t1);
  2223. /* t1 = ffffffffffffffff0000000000000000 */
  2224. fe448_mul(t1, t1, t2);
  2225. /* t1 = ffffffffffffffffffffffffffffffff */
  2226. fe448_sqr(t1, t1); for (i = 1; i < 64; ++i) fe448_sqr(t1, t1);
  2227. /* t1 = ffffffffffffffffffffffffffffffff0000000000000000 */
  2228. fe448_mul(t4, t1, t2);
  2229. /* t4 = ffffffffffffffffffffffffffffffffffffffffffffffff */
  2230. fe448_sqr(t2, t4); for (i = 1; i < 32; ++i) fe448_sqr(t2, t2);
  2231. /* t2 = ffffffffffffffffffffffffffffffffffffffffffffffff00000000 */
  2232. fe448_mul(t3, t3, t2);
  2233. /* t3 = fffffffffffffffffffffffffffffffffffffffffffffffffffffffe */
  2234. fe448_sqr(t1, t3); for (i = 1; i < 192; ++i) fe448_sqr(t1, t1);
  2235. /* t1 = fffffffffffffffffffffffffffffffffffffffffffffffffffffffe000000000000000000000000000000000000000000000000 */
  2236. fe448_mul(t1, t1, t4);
  2237. /* t1 = fffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffffffffffffffffffffffffffffffffffffffffffff */
  2238. fe448_sqr(t1, t1); for (i = 1; i < 30; ++i) fe448_sqr(t1, t1);
  2239. /* t1 = 3fffffffffffffffffffffffffffffffffffffffffffffffffffffffbfffffffffffffffffffffffffffffffffffffffffffffffc0000000 */
  2240. fe448_mul(r, t5, t1);
  2241. /* r = 3fffffffffffffffffffffffffffffffffffffffffffffffffffffffbfffffffffffffffffffffffffffffffffffffffffffffffffffffff */
  2242. }
  2243. /* Constant time, conditional move of b into a.
  2244. * a is not changed if the condition is 0.
  2245. *
  2246. * a A field element.
  2247. * b A field element.
  2248. * c If 1 then copy and if 0 then don't copy.
  2249. */
  2250. void fe448_cmov(sword32* a, const sword32* b, int c)
  2251. {
  2252. sword32 m = -(sword32)c;
  2253. sword32 t0 = m & (a[0] ^ b[0]);
  2254. sword32 t1 = m & (a[1] ^ b[1]);
  2255. sword32 t2 = m & (a[2] ^ b[2]);
  2256. sword32 t3 = m & (a[3] ^ b[3]);
  2257. sword32 t4 = m & (a[4] ^ b[4]);
  2258. sword32 t5 = m & (a[5] ^ b[5]);
  2259. sword32 t6 = m & (a[6] ^ b[6]);
  2260. sword32 t7 = m & (a[7] ^ b[7]);
  2261. sword32 t8 = m & (a[8] ^ b[8]);
  2262. sword32 t9 = m & (a[9] ^ b[9]);
  2263. sword32 t10 = m & (a[10] ^ b[10]);
  2264. sword32 t11 = m & (a[11] ^ b[11]);
  2265. sword32 t12 = m & (a[12] ^ b[12]);
  2266. sword32 t13 = m & (a[13] ^ b[13]);
  2267. sword32 t14 = m & (a[14] ^ b[14]);
  2268. sword32 t15 = m & (a[15] ^ b[15]);
  2269. a[0] ^= t0;
  2270. a[1] ^= t1;
  2271. a[2] ^= t2;
  2272. a[3] ^= t3;
  2273. a[4] ^= t4;
  2274. a[5] ^= t5;
  2275. a[6] ^= t6;
  2276. a[7] ^= t7;
  2277. a[8] ^= t8;
  2278. a[9] ^= t9;
  2279. a[10] ^= t10;
  2280. a[11] ^= t11;
  2281. a[12] ^= t12;
  2282. a[13] ^= t13;
  2283. a[14] ^= t14;
  2284. a[15] ^= t15;
  2285. }
  2286. #endif /* HAVE_ED448 */
  2287. #endif
  2288. #endif /* HAVE_CURVE448 || HAVE_ED448 */