ecp_nistz256-armv8.pl 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the OpenSSL license (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. # ====================================================================
  9. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  10. # project. The module is, however, dual licensed under OpenSSL and
  11. # CRYPTOGAMS licenses depending on where you obtain it. For further
  12. # details see http://www.openssl.org/~appro/cryptogams/.
  13. # ====================================================================
  14. #
  15. # ECP_NISTZ256 module for ARMv8.
  16. #
  17. # February 2015.
  18. #
  19. # Original ECP_NISTZ256 submission targeting x86_64 is detailed in
  20. # http://eprint.iacr.org/2013/816.
  21. #
  22. # with/without -DECP_NISTZ256_ASM
  23. # Apple A7 +190-360%
  24. # Cortex-A53 +190-400%
  25. # Cortex-A57 +190-350%
  26. # Denver +230-400%
  27. #
  28. # Ranges denote minimum and maximum improvement coefficients depending
  29. # on benchmark. Lower coefficients are for ECDSA sign, server-side
  30. # operation. Keep in mind that +400% means 5x improvement.
  31. $flavour = shift;
  32. while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {}
  33. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  34. ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
  35. ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
  36. die "can't locate arm-xlate.pl";
  37. open OUT,"| \"$^X\" $xlate $flavour $output";
  38. *STDOUT=*OUT;
  39. {
  40. my ($rp,$ap,$bp,$bi,$a0,$a1,$a2,$a3,$t0,$t1,$t2,$t3,$poly1,$poly3,
  41. $acc0,$acc1,$acc2,$acc3,$acc4,$acc5) =
  42. map("x$_",(0..17,19,20));
  43. my ($acc6,$acc7)=($ap,$bp); # used in __ecp_nistz256_sqr_mont
  44. $code.=<<___;
  45. #include "arm_arch.h"
  46. .text
  47. ___
  48. ########################################################################
  49. # Convert ecp_nistz256_table.c to layout expected by ecp_nistz_gather_w7
  50. #
  51. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  52. open TABLE,"<ecp_nistz256_table.c" or
  53. open TABLE,"<${dir}../ecp_nistz256_table.c" or
  54. die "failed to open ecp_nistz256_table.c:",$!;
  55. use integer;
  56. foreach(<TABLE>) {
  57. s/TOBN\(\s*(0x[0-9a-f]+),\s*(0x[0-9a-f]+)\s*\)/push @arr,hex($2),hex($1)/geo;
  58. }
  59. close TABLE;
  60. # See ecp_nistz256_table.c for explanation for why it's 64*16*37.
  61. # 64*16*37-1 is because $#arr returns last valid index or @arr, not
  62. # amount of elements.
  63. die "insane number of elements" if ($#arr != 64*16*37-1);
  64. $code.=<<___;
  65. .globl ecp_nistz256_precomputed
  66. .type ecp_nistz256_precomputed,%object
  67. .align 12
  68. ecp_nistz256_precomputed:
  69. ___
  70. ########################################################################
  71. # this conversion smashes P256_POINT_AFFINE by individual bytes with
  72. # 64 byte interval, similar to
  73. # 1111222233334444
  74. # 1234123412341234
  75. for(1..37) {
  76. @tbl = splice(@arr,0,64*16);
  77. for($i=0;$i<64;$i++) {
  78. undef @line;
  79. for($j=0;$j<64;$j++) {
  80. push @line,(@tbl[$j*16+$i/4]>>(($i%4)*8))&0xff;
  81. }
  82. $code.=".byte\t";
  83. $code.=join(',',map { sprintf "0x%02x",$_} @line);
  84. $code.="\n";
  85. }
  86. }
  87. $code.=<<___;
  88. .size ecp_nistz256_precomputed,.-ecp_nistz256_precomputed
  89. .align 5
  90. .Lpoly:
  91. .quad 0xffffffffffffffff,0x00000000ffffffff,0x0000000000000000,0xffffffff00000001
  92. .LRR: // 2^512 mod P precomputed for NIST P256 polynomial
  93. .quad 0x0000000000000003,0xfffffffbffffffff,0xfffffffffffffffe,0x00000004fffffffd
  94. .Lone_mont:
  95. .quad 0x0000000000000001,0xffffffff00000000,0xffffffffffffffff,0x00000000fffffffe
  96. .Lone:
  97. .quad 1,0,0,0
  98. .Lord:
  99. .quad 0xf3b9cac2fc632551,0xbce6faada7179e84,0xffffffffffffffff,0xffffffff00000000
  100. .LordK:
  101. .quad 0xccd1c8aaee00bc4f
  102. .asciz "ECP_NISTZ256 for ARMv8, CRYPTOGAMS by <appro\@openssl.org>"
  103. // void ecp_nistz256_to_mont(BN_ULONG x0[4],const BN_ULONG x1[4]);
  104. .globl ecp_nistz256_to_mont
  105. .type ecp_nistz256_to_mont,%function
  106. .align 6
  107. ecp_nistz256_to_mont:
  108. .inst 0xd503233f // paciasp
  109. stp x29,x30,[sp,#-32]!
  110. add x29,sp,#0
  111. stp x19,x20,[sp,#16]
  112. ldr $bi,.LRR // bp[0]
  113. ldp $a0,$a1,[$ap]
  114. ldp $a2,$a3,[$ap,#16]
  115. ldr $poly1,.Lpoly+8
  116. ldr $poly3,.Lpoly+24
  117. adr $bp,.LRR // &bp[0]
  118. bl __ecp_nistz256_mul_mont
  119. ldp x19,x20,[sp,#16]
  120. ldp x29,x30,[sp],#32
  121. .inst 0xd50323bf // autiasp
  122. ret
  123. .size ecp_nistz256_to_mont,.-ecp_nistz256_to_mont
  124. // void ecp_nistz256_from_mont(BN_ULONG x0[4],const BN_ULONG x1[4]);
  125. .globl ecp_nistz256_from_mont
  126. .type ecp_nistz256_from_mont,%function
  127. .align 4
  128. ecp_nistz256_from_mont:
  129. .inst 0xd503233f // paciasp
  130. stp x29,x30,[sp,#-32]!
  131. add x29,sp,#0
  132. stp x19,x20,[sp,#16]
  133. mov $bi,#1 // bp[0]
  134. ldp $a0,$a1,[$ap]
  135. ldp $a2,$a3,[$ap,#16]
  136. ldr $poly1,.Lpoly+8
  137. ldr $poly3,.Lpoly+24
  138. adr $bp,.Lone // &bp[0]
  139. bl __ecp_nistz256_mul_mont
  140. ldp x19,x20,[sp,#16]
  141. ldp x29,x30,[sp],#32
  142. .inst 0xd50323bf // autiasp
  143. ret
  144. .size ecp_nistz256_from_mont,.-ecp_nistz256_from_mont
  145. // void ecp_nistz256_mul_mont(BN_ULONG x0[4],const BN_ULONG x1[4],
  146. // const BN_ULONG x2[4]);
  147. .globl ecp_nistz256_mul_mont
  148. .type ecp_nistz256_mul_mont,%function
  149. .align 4
  150. ecp_nistz256_mul_mont:
  151. .inst 0xd503233f // paciasp
  152. stp x29,x30,[sp,#-32]!
  153. add x29,sp,#0
  154. stp x19,x20,[sp,#16]
  155. ldr $bi,[$bp] // bp[0]
  156. ldp $a0,$a1,[$ap]
  157. ldp $a2,$a3,[$ap,#16]
  158. ldr $poly1,.Lpoly+8
  159. ldr $poly3,.Lpoly+24
  160. bl __ecp_nistz256_mul_mont
  161. ldp x19,x20,[sp,#16]
  162. ldp x29,x30,[sp],#32
  163. .inst 0xd50323bf // autiasp
  164. ret
  165. .size ecp_nistz256_mul_mont,.-ecp_nistz256_mul_mont
  166. // void ecp_nistz256_sqr_mont(BN_ULONG x0[4],const BN_ULONG x1[4]);
  167. .globl ecp_nistz256_sqr_mont
  168. .type ecp_nistz256_sqr_mont,%function
  169. .align 4
  170. ecp_nistz256_sqr_mont:
  171. .inst 0xd503233f // paciasp
  172. stp x29,x30,[sp,#-32]!
  173. add x29,sp,#0
  174. stp x19,x20,[sp,#16]
  175. ldp $a0,$a1,[$ap]
  176. ldp $a2,$a3,[$ap,#16]
  177. ldr $poly1,.Lpoly+8
  178. ldr $poly3,.Lpoly+24
  179. bl __ecp_nistz256_sqr_mont
  180. ldp x19,x20,[sp,#16]
  181. ldp x29,x30,[sp],#32
  182. .inst 0xd50323bf // autiasp
  183. ret
  184. .size ecp_nistz256_sqr_mont,.-ecp_nistz256_sqr_mont
  185. // void ecp_nistz256_add(BN_ULONG x0[4],const BN_ULONG x1[4],
  186. // const BN_ULONG x2[4]);
  187. .globl ecp_nistz256_add
  188. .type ecp_nistz256_add,%function
  189. .align 4
  190. ecp_nistz256_add:
  191. .inst 0xd503233f // paciasp
  192. stp x29,x30,[sp,#-16]!
  193. add x29,sp,#0
  194. ldp $acc0,$acc1,[$ap]
  195. ldp $t0,$t1,[$bp]
  196. ldp $acc2,$acc3,[$ap,#16]
  197. ldp $t2,$t3,[$bp,#16]
  198. ldr $poly1,.Lpoly+8
  199. ldr $poly3,.Lpoly+24
  200. bl __ecp_nistz256_add
  201. ldp x29,x30,[sp],#16
  202. .inst 0xd50323bf // autiasp
  203. ret
  204. .size ecp_nistz256_add,.-ecp_nistz256_add
  205. // void ecp_nistz256_div_by_2(BN_ULONG x0[4],const BN_ULONG x1[4]);
  206. .globl ecp_nistz256_div_by_2
  207. .type ecp_nistz256_div_by_2,%function
  208. .align 4
  209. ecp_nistz256_div_by_2:
  210. .inst 0xd503233f // paciasp
  211. stp x29,x30,[sp,#-16]!
  212. add x29,sp,#0
  213. ldp $acc0,$acc1,[$ap]
  214. ldp $acc2,$acc3,[$ap,#16]
  215. ldr $poly1,.Lpoly+8
  216. ldr $poly3,.Lpoly+24
  217. bl __ecp_nistz256_div_by_2
  218. ldp x29,x30,[sp],#16
  219. .inst 0xd50323bf // autiasp
  220. ret
  221. .size ecp_nistz256_div_by_2,.-ecp_nistz256_div_by_2
  222. // void ecp_nistz256_mul_by_2(BN_ULONG x0[4],const BN_ULONG x1[4]);
  223. .globl ecp_nistz256_mul_by_2
  224. .type ecp_nistz256_mul_by_2,%function
  225. .align 4
  226. ecp_nistz256_mul_by_2:
  227. .inst 0xd503233f // paciasp
  228. stp x29,x30,[sp,#-16]!
  229. add x29,sp,#0
  230. ldp $acc0,$acc1,[$ap]
  231. ldp $acc2,$acc3,[$ap,#16]
  232. ldr $poly1,.Lpoly+8
  233. ldr $poly3,.Lpoly+24
  234. mov $t0,$acc0
  235. mov $t1,$acc1
  236. mov $t2,$acc2
  237. mov $t3,$acc3
  238. bl __ecp_nistz256_add // ret = a+a // 2*a
  239. ldp x29,x30,[sp],#16
  240. .inst 0xd50323bf // autiasp
  241. ret
  242. .size ecp_nistz256_mul_by_2,.-ecp_nistz256_mul_by_2
  243. // void ecp_nistz256_mul_by_3(BN_ULONG x0[4],const BN_ULONG x1[4]);
  244. .globl ecp_nistz256_mul_by_3
  245. .type ecp_nistz256_mul_by_3,%function
  246. .align 4
  247. ecp_nistz256_mul_by_3:
  248. .inst 0xd503233f // paciasp
  249. stp x29,x30,[sp,#-16]!
  250. add x29,sp,#0
  251. ldp $acc0,$acc1,[$ap]
  252. ldp $acc2,$acc3,[$ap,#16]
  253. ldr $poly1,.Lpoly+8
  254. ldr $poly3,.Lpoly+24
  255. mov $t0,$acc0
  256. mov $t1,$acc1
  257. mov $t2,$acc2
  258. mov $t3,$acc3
  259. mov $a0,$acc0
  260. mov $a1,$acc1
  261. mov $a2,$acc2
  262. mov $a3,$acc3
  263. bl __ecp_nistz256_add // ret = a+a // 2*a
  264. mov $t0,$a0
  265. mov $t1,$a1
  266. mov $t2,$a2
  267. mov $t3,$a3
  268. bl __ecp_nistz256_add // ret += a // 2*a+a=3*a
  269. ldp x29,x30,[sp],#16
  270. .inst 0xd50323bf // autiasp
  271. ret
  272. .size ecp_nistz256_mul_by_3,.-ecp_nistz256_mul_by_3
  273. // void ecp_nistz256_sub(BN_ULONG x0[4],const BN_ULONG x1[4],
  274. // const BN_ULONG x2[4]);
  275. .globl ecp_nistz256_sub
  276. .type ecp_nistz256_sub,%function
  277. .align 4
  278. ecp_nistz256_sub:
  279. .inst 0xd503233f // paciasp
  280. stp x29,x30,[sp,#-16]!
  281. add x29,sp,#0
  282. ldp $acc0,$acc1,[$ap]
  283. ldp $acc2,$acc3,[$ap,#16]
  284. ldr $poly1,.Lpoly+8
  285. ldr $poly3,.Lpoly+24
  286. bl __ecp_nistz256_sub_from
  287. ldp x29,x30,[sp],#16
  288. .inst 0xd50323bf // autiasp
  289. ret
  290. .size ecp_nistz256_sub,.-ecp_nistz256_sub
  291. // void ecp_nistz256_neg(BN_ULONG x0[4],const BN_ULONG x1[4]);
  292. .globl ecp_nistz256_neg
  293. .type ecp_nistz256_neg,%function
  294. .align 4
  295. ecp_nistz256_neg:
  296. .inst 0xd503233f // paciasp
  297. stp x29,x30,[sp,#-16]!
  298. add x29,sp,#0
  299. mov $bp,$ap
  300. mov $acc0,xzr // a = 0
  301. mov $acc1,xzr
  302. mov $acc2,xzr
  303. mov $acc3,xzr
  304. ldr $poly1,.Lpoly+8
  305. ldr $poly3,.Lpoly+24
  306. bl __ecp_nistz256_sub_from
  307. ldp x29,x30,[sp],#16
  308. .inst 0xd50323bf // autiasp
  309. ret
  310. .size ecp_nistz256_neg,.-ecp_nistz256_neg
  311. // note that __ecp_nistz256_mul_mont expects a[0-3] input pre-loaded
  312. // to $a0-$a3 and b[0] - to $bi
  313. .type __ecp_nistz256_mul_mont,%function
  314. .align 4
  315. __ecp_nistz256_mul_mont:
  316. mul $acc0,$a0,$bi // a[0]*b[0]
  317. umulh $t0,$a0,$bi
  318. mul $acc1,$a1,$bi // a[1]*b[0]
  319. umulh $t1,$a1,$bi
  320. mul $acc2,$a2,$bi // a[2]*b[0]
  321. umulh $t2,$a2,$bi
  322. mul $acc3,$a3,$bi // a[3]*b[0]
  323. umulh $t3,$a3,$bi
  324. ldr $bi,[$bp,#8] // b[1]
  325. adds $acc1,$acc1,$t0 // accumulate high parts of multiplication
  326. lsl $t0,$acc0,#32
  327. adcs $acc2,$acc2,$t1
  328. lsr $t1,$acc0,#32
  329. adcs $acc3,$acc3,$t2
  330. adc $acc4,xzr,$t3
  331. mov $acc5,xzr
  332. ___
  333. for($i=1;$i<4;$i++) {
  334. # Reduction iteration is normally performed by accumulating
  335. # result of multiplication of modulus by "magic" digit [and
  336. # omitting least significant word, which is guaranteed to
  337. # be 0], but thanks to special form of modulus and "magic"
  338. # digit being equal to least significant word, it can be
  339. # performed with additions and subtractions alone. Indeed:
  340. #
  341. # ffff0001.00000000.0000ffff.ffffffff
  342. # * abcdefgh
  343. # + xxxxxxxx.xxxxxxxx.xxxxxxxx.xxxxxxxx.abcdefgh
  344. #
  345. # Now observing that ff..ff*x = (2^n-1)*x = 2^n*x-x, we
  346. # rewrite above as:
  347. #
  348. # xxxxxxxx.xxxxxxxx.xxxxxxxx.xxxxxxxx.abcdefgh
  349. # + abcdefgh.abcdefgh.0000abcd.efgh0000.00000000
  350. # - 0000abcd.efgh0000.00000000.00000000.abcdefgh
  351. #
  352. # or marking redundant operations:
  353. #
  354. # xxxxxxxx.xxxxxxxx.xxxxxxxx.xxxxxxxx.--------
  355. # + abcdefgh.abcdefgh.0000abcd.efgh0000.--------
  356. # - 0000abcd.efgh0000.--------.--------.--------
  357. $code.=<<___;
  358. subs $t2,$acc0,$t0 // "*0xffff0001"
  359. sbc $t3,$acc0,$t1
  360. adds $acc0,$acc1,$t0 // +=acc[0]<<96 and omit acc[0]
  361. mul $t0,$a0,$bi // lo(a[0]*b[i])
  362. adcs $acc1,$acc2,$t1
  363. mul $t1,$a1,$bi // lo(a[1]*b[i])
  364. adcs $acc2,$acc3,$t2 // +=acc[0]*0xffff0001
  365. mul $t2,$a2,$bi // lo(a[2]*b[i])
  366. adcs $acc3,$acc4,$t3
  367. mul $t3,$a3,$bi // lo(a[3]*b[i])
  368. adc $acc4,$acc5,xzr
  369. adds $acc0,$acc0,$t0 // accumulate low parts of multiplication
  370. umulh $t0,$a0,$bi // hi(a[0]*b[i])
  371. adcs $acc1,$acc1,$t1
  372. umulh $t1,$a1,$bi // hi(a[1]*b[i])
  373. adcs $acc2,$acc2,$t2
  374. umulh $t2,$a2,$bi // hi(a[2]*b[i])
  375. adcs $acc3,$acc3,$t3
  376. umulh $t3,$a3,$bi // hi(a[3]*b[i])
  377. adc $acc4,$acc4,xzr
  378. ___
  379. $code.=<<___ if ($i<3);
  380. ldr $bi,[$bp,#8*($i+1)] // b[$i+1]
  381. ___
  382. $code.=<<___;
  383. adds $acc1,$acc1,$t0 // accumulate high parts of multiplication
  384. lsl $t0,$acc0,#32
  385. adcs $acc2,$acc2,$t1
  386. lsr $t1,$acc0,#32
  387. adcs $acc3,$acc3,$t2
  388. adcs $acc4,$acc4,$t3
  389. adc $acc5,xzr,xzr
  390. ___
  391. }
  392. $code.=<<___;
  393. // last reduction
  394. subs $t2,$acc0,$t0 // "*0xffff0001"
  395. sbc $t3,$acc0,$t1
  396. adds $acc0,$acc1,$t0 // +=acc[0]<<96 and omit acc[0]
  397. adcs $acc1,$acc2,$t1
  398. adcs $acc2,$acc3,$t2 // +=acc[0]*0xffff0001
  399. adcs $acc3,$acc4,$t3
  400. adc $acc4,$acc5,xzr
  401. adds $t0,$acc0,#1 // subs $t0,$acc0,#-1 // tmp = ret-modulus
  402. sbcs $t1,$acc1,$poly1
  403. sbcs $t2,$acc2,xzr
  404. sbcs $t3,$acc3,$poly3
  405. sbcs xzr,$acc4,xzr // did it borrow?
  406. csel $acc0,$acc0,$t0,lo // ret = borrow ? ret : ret-modulus
  407. csel $acc1,$acc1,$t1,lo
  408. csel $acc2,$acc2,$t2,lo
  409. stp $acc0,$acc1,[$rp]
  410. csel $acc3,$acc3,$t3,lo
  411. stp $acc2,$acc3,[$rp,#16]
  412. ret
  413. .size __ecp_nistz256_mul_mont,.-__ecp_nistz256_mul_mont
  414. // note that __ecp_nistz256_sqr_mont expects a[0-3] input pre-loaded
  415. // to $a0-$a3
  416. .type __ecp_nistz256_sqr_mont,%function
  417. .align 4
  418. __ecp_nistz256_sqr_mont:
  419. // | | | | | |a1*a0| |
  420. // | | | | |a2*a0| | |
  421. // | |a3*a2|a3*a0| | | |
  422. // | | | |a2*a1| | | |
  423. // | | |a3*a1| | | | |
  424. // *| | | | | | | | 2|
  425. // +|a3*a3|a2*a2|a1*a1|a0*a0|
  426. // |--+--+--+--+--+--+--+--|
  427. // |A7|A6|A5|A4|A3|A2|A1|A0|, where Ax is $accx, i.e. follow $accx
  428. //
  429. // "can't overflow" below mark carrying into high part of
  430. // multiplication result, which can't overflow, because it
  431. // can never be all ones.
  432. mul $acc1,$a1,$a0 // a[1]*a[0]
  433. umulh $t1,$a1,$a0
  434. mul $acc2,$a2,$a0 // a[2]*a[0]
  435. umulh $t2,$a2,$a0
  436. mul $acc3,$a3,$a0 // a[3]*a[0]
  437. umulh $acc4,$a3,$a0
  438. adds $acc2,$acc2,$t1 // accumulate high parts of multiplication
  439. mul $t0,$a2,$a1 // a[2]*a[1]
  440. umulh $t1,$a2,$a1
  441. adcs $acc3,$acc3,$t2
  442. mul $t2,$a3,$a1 // a[3]*a[1]
  443. umulh $t3,$a3,$a1
  444. adc $acc4,$acc4,xzr // can't overflow
  445. mul $acc5,$a3,$a2 // a[3]*a[2]
  446. umulh $acc6,$a3,$a2
  447. adds $t1,$t1,$t2 // accumulate high parts of multiplication
  448. mul $acc0,$a0,$a0 // a[0]*a[0]
  449. adc $t2,$t3,xzr // can't overflow
  450. adds $acc3,$acc3,$t0 // accumulate low parts of multiplication
  451. umulh $a0,$a0,$a0
  452. adcs $acc4,$acc4,$t1
  453. mul $t1,$a1,$a1 // a[1]*a[1]
  454. adcs $acc5,$acc5,$t2
  455. umulh $a1,$a1,$a1
  456. adc $acc6,$acc6,xzr // can't overflow
  457. adds $acc1,$acc1,$acc1 // acc[1-6]*=2
  458. mul $t2,$a2,$a2 // a[2]*a[2]
  459. adcs $acc2,$acc2,$acc2
  460. umulh $a2,$a2,$a2
  461. adcs $acc3,$acc3,$acc3
  462. mul $t3,$a3,$a3 // a[3]*a[3]
  463. adcs $acc4,$acc4,$acc4
  464. umulh $a3,$a3,$a3
  465. adcs $acc5,$acc5,$acc5
  466. adcs $acc6,$acc6,$acc6
  467. adc $acc7,xzr,xzr
  468. adds $acc1,$acc1,$a0 // +a[i]*a[i]
  469. adcs $acc2,$acc2,$t1
  470. adcs $acc3,$acc3,$a1
  471. adcs $acc4,$acc4,$t2
  472. adcs $acc5,$acc5,$a2
  473. lsl $t0,$acc0,#32
  474. adcs $acc6,$acc6,$t3
  475. lsr $t1,$acc0,#32
  476. adc $acc7,$acc7,$a3
  477. ___
  478. for($i=0;$i<3;$i++) { # reductions, see commentary in
  479. # multiplication for details
  480. $code.=<<___;
  481. subs $t2,$acc0,$t0 // "*0xffff0001"
  482. sbc $t3,$acc0,$t1
  483. adds $acc0,$acc1,$t0 // +=acc[0]<<96 and omit acc[0]
  484. adcs $acc1,$acc2,$t1
  485. lsl $t0,$acc0,#32
  486. adcs $acc2,$acc3,$t2 // +=acc[0]*0xffff0001
  487. lsr $t1,$acc0,#32
  488. adc $acc3,$t3,xzr // can't overflow
  489. ___
  490. }
  491. $code.=<<___;
  492. subs $t2,$acc0,$t0 // "*0xffff0001"
  493. sbc $t3,$acc0,$t1
  494. adds $acc0,$acc1,$t0 // +=acc[0]<<96 and omit acc[0]
  495. adcs $acc1,$acc2,$t1
  496. adcs $acc2,$acc3,$t2 // +=acc[0]*0xffff0001
  497. adc $acc3,$t3,xzr // can't overflow
  498. adds $acc0,$acc0,$acc4 // accumulate upper half
  499. adcs $acc1,$acc1,$acc5
  500. adcs $acc2,$acc2,$acc6
  501. adcs $acc3,$acc3,$acc7
  502. adc $acc4,xzr,xzr
  503. adds $t0,$acc0,#1 // subs $t0,$acc0,#-1 // tmp = ret-modulus
  504. sbcs $t1,$acc1,$poly1
  505. sbcs $t2,$acc2,xzr
  506. sbcs $t3,$acc3,$poly3
  507. sbcs xzr,$acc4,xzr // did it borrow?
  508. csel $acc0,$acc0,$t0,lo // ret = borrow ? ret : ret-modulus
  509. csel $acc1,$acc1,$t1,lo
  510. csel $acc2,$acc2,$t2,lo
  511. stp $acc0,$acc1,[$rp]
  512. csel $acc3,$acc3,$t3,lo
  513. stp $acc2,$acc3,[$rp,#16]
  514. ret
  515. .size __ecp_nistz256_sqr_mont,.-__ecp_nistz256_sqr_mont
  516. // Note that __ecp_nistz256_add expects both input vectors pre-loaded to
  517. // $a0-$a3 and $t0-$t3. This is done because it's used in multiple
  518. // contexts, e.g. in multiplication by 2 and 3...
  519. .type __ecp_nistz256_add,%function
  520. .align 4
  521. __ecp_nistz256_add:
  522. adds $acc0,$acc0,$t0 // ret = a+b
  523. adcs $acc1,$acc1,$t1
  524. adcs $acc2,$acc2,$t2
  525. adcs $acc3,$acc3,$t3
  526. adc $ap,xzr,xzr // zap $ap
  527. adds $t0,$acc0,#1 // subs $t0,$a0,#-1 // tmp = ret-modulus
  528. sbcs $t1,$acc1,$poly1
  529. sbcs $t2,$acc2,xzr
  530. sbcs $t3,$acc3,$poly3
  531. sbcs xzr,$ap,xzr // did subtraction borrow?
  532. csel $acc0,$acc0,$t0,lo // ret = borrow ? ret : ret-modulus
  533. csel $acc1,$acc1,$t1,lo
  534. csel $acc2,$acc2,$t2,lo
  535. stp $acc0,$acc1,[$rp]
  536. csel $acc3,$acc3,$t3,lo
  537. stp $acc2,$acc3,[$rp,#16]
  538. ret
  539. .size __ecp_nistz256_add,.-__ecp_nistz256_add
  540. .type __ecp_nistz256_sub_from,%function
  541. .align 4
  542. __ecp_nistz256_sub_from:
  543. ldp $t0,$t1,[$bp]
  544. ldp $t2,$t3,[$bp,#16]
  545. subs $acc0,$acc0,$t0 // ret = a-b
  546. sbcs $acc1,$acc1,$t1
  547. sbcs $acc2,$acc2,$t2
  548. sbcs $acc3,$acc3,$t3
  549. sbc $ap,xzr,xzr // zap $ap
  550. subs $t0,$acc0,#1 // adds $t0,$a0,#-1 // tmp = ret+modulus
  551. adcs $t1,$acc1,$poly1
  552. adcs $t2,$acc2,xzr
  553. adc $t3,$acc3,$poly3
  554. cmp $ap,xzr // did subtraction borrow?
  555. csel $acc0,$acc0,$t0,eq // ret = borrow ? ret+modulus : ret
  556. csel $acc1,$acc1,$t1,eq
  557. csel $acc2,$acc2,$t2,eq
  558. stp $acc0,$acc1,[$rp]
  559. csel $acc3,$acc3,$t3,eq
  560. stp $acc2,$acc3,[$rp,#16]
  561. ret
  562. .size __ecp_nistz256_sub_from,.-__ecp_nistz256_sub_from
  563. .type __ecp_nistz256_sub_morf,%function
  564. .align 4
  565. __ecp_nistz256_sub_morf:
  566. ldp $t0,$t1,[$bp]
  567. ldp $t2,$t3,[$bp,#16]
  568. subs $acc0,$t0,$acc0 // ret = b-a
  569. sbcs $acc1,$t1,$acc1
  570. sbcs $acc2,$t2,$acc2
  571. sbcs $acc3,$t3,$acc3
  572. sbc $ap,xzr,xzr // zap $ap
  573. subs $t0,$acc0,#1 // adds $t0,$a0,#-1 // tmp = ret+modulus
  574. adcs $t1,$acc1,$poly1
  575. adcs $t2,$acc2,xzr
  576. adc $t3,$acc3,$poly3
  577. cmp $ap,xzr // did subtraction borrow?
  578. csel $acc0,$acc0,$t0,eq // ret = borrow ? ret+modulus : ret
  579. csel $acc1,$acc1,$t1,eq
  580. csel $acc2,$acc2,$t2,eq
  581. stp $acc0,$acc1,[$rp]
  582. csel $acc3,$acc3,$t3,eq
  583. stp $acc2,$acc3,[$rp,#16]
  584. ret
  585. .size __ecp_nistz256_sub_morf,.-__ecp_nistz256_sub_morf
  586. .type __ecp_nistz256_div_by_2,%function
  587. .align 4
  588. __ecp_nistz256_div_by_2:
  589. subs $t0,$acc0,#1 // adds $t0,$a0,#-1 // tmp = a+modulus
  590. adcs $t1,$acc1,$poly1
  591. adcs $t2,$acc2,xzr
  592. adcs $t3,$acc3,$poly3
  593. adc $ap,xzr,xzr // zap $ap
  594. tst $acc0,#1 // is a even?
  595. csel $acc0,$acc0,$t0,eq // ret = even ? a : a+modulus
  596. csel $acc1,$acc1,$t1,eq
  597. csel $acc2,$acc2,$t2,eq
  598. csel $acc3,$acc3,$t3,eq
  599. csel $ap,xzr,$ap,eq
  600. lsr $acc0,$acc0,#1 // ret >>= 1
  601. orr $acc0,$acc0,$acc1,lsl#63
  602. lsr $acc1,$acc1,#1
  603. orr $acc1,$acc1,$acc2,lsl#63
  604. lsr $acc2,$acc2,#1
  605. orr $acc2,$acc2,$acc3,lsl#63
  606. lsr $acc3,$acc3,#1
  607. stp $acc0,$acc1,[$rp]
  608. orr $acc3,$acc3,$ap,lsl#63
  609. stp $acc2,$acc3,[$rp,#16]
  610. ret
  611. .size __ecp_nistz256_div_by_2,.-__ecp_nistz256_div_by_2
  612. ___
  613. ########################################################################
  614. # following subroutines are "literal" implementation of those found in
  615. # ecp_nistz256.c
  616. #
  617. ########################################################################
  618. # void ecp_nistz256_point_double(P256_POINT *out,const P256_POINT *inp);
  619. #
  620. {
  621. my ($S,$M,$Zsqr,$tmp0)=map(32*$_,(0..3));
  622. # above map() describes stack layout with 4 temporary
  623. # 256-bit vectors on top.
  624. my ($rp_real,$ap_real) = map("x$_",(21,22));
  625. $code.=<<___;
  626. .globl ecp_nistz256_point_double
  627. .type ecp_nistz256_point_double,%function
  628. .align 5
  629. ecp_nistz256_point_double:
  630. .inst 0xd503233f // paciasp
  631. stp x29,x30,[sp,#-96]!
  632. add x29,sp,#0
  633. stp x19,x20,[sp,#16]
  634. stp x21,x22,[sp,#32]
  635. sub sp,sp,#32*4
  636. .Ldouble_shortcut:
  637. ldp $acc0,$acc1,[$ap,#32]
  638. mov $rp_real,$rp
  639. ldp $acc2,$acc3,[$ap,#48]
  640. mov $ap_real,$ap
  641. ldr $poly1,.Lpoly+8
  642. mov $t0,$acc0
  643. ldr $poly3,.Lpoly+24
  644. mov $t1,$acc1
  645. ldp $a0,$a1,[$ap_real,#64] // forward load for p256_sqr_mont
  646. mov $t2,$acc2
  647. mov $t3,$acc3
  648. ldp $a2,$a3,[$ap_real,#64+16]
  649. add $rp,sp,#$S
  650. bl __ecp_nistz256_add // p256_mul_by_2(S, in_y);
  651. add $rp,sp,#$Zsqr
  652. bl __ecp_nistz256_sqr_mont // p256_sqr_mont(Zsqr, in_z);
  653. ldp $t0,$t1,[$ap_real]
  654. ldp $t2,$t3,[$ap_real,#16]
  655. mov $a0,$acc0 // put Zsqr aside for p256_sub
  656. mov $a1,$acc1
  657. mov $a2,$acc2
  658. mov $a3,$acc3
  659. add $rp,sp,#$M
  660. bl __ecp_nistz256_add // p256_add(M, Zsqr, in_x);
  661. add $bp,$ap_real,#0
  662. mov $acc0,$a0 // restore Zsqr
  663. mov $acc1,$a1
  664. ldp $a0,$a1,[sp,#$S] // forward load for p256_sqr_mont
  665. mov $acc2,$a2
  666. mov $acc3,$a3
  667. ldp $a2,$a3,[sp,#$S+16]
  668. add $rp,sp,#$Zsqr
  669. bl __ecp_nistz256_sub_morf // p256_sub(Zsqr, in_x, Zsqr);
  670. add $rp,sp,#$S
  671. bl __ecp_nistz256_sqr_mont // p256_sqr_mont(S, S);
  672. ldr $bi,[$ap_real,#32]
  673. ldp $a0,$a1,[$ap_real,#64]
  674. ldp $a2,$a3,[$ap_real,#64+16]
  675. add $bp,$ap_real,#32
  676. add $rp,sp,#$tmp0
  677. bl __ecp_nistz256_mul_mont // p256_mul_mont(tmp0, in_z, in_y);
  678. mov $t0,$acc0
  679. mov $t1,$acc1
  680. ldp $a0,$a1,[sp,#$S] // forward load for p256_sqr_mont
  681. mov $t2,$acc2
  682. mov $t3,$acc3
  683. ldp $a2,$a3,[sp,#$S+16]
  684. add $rp,$rp_real,#64
  685. bl __ecp_nistz256_add // p256_mul_by_2(res_z, tmp0);
  686. add $rp,sp,#$tmp0
  687. bl __ecp_nistz256_sqr_mont // p256_sqr_mont(tmp0, S);
  688. ldr $bi,[sp,#$Zsqr] // forward load for p256_mul_mont
  689. ldp $a0,$a1,[sp,#$M]
  690. ldp $a2,$a3,[sp,#$M+16]
  691. add $rp,$rp_real,#32
  692. bl __ecp_nistz256_div_by_2 // p256_div_by_2(res_y, tmp0);
  693. add $bp,sp,#$Zsqr
  694. add $rp,sp,#$M
  695. bl __ecp_nistz256_mul_mont // p256_mul_mont(M, M, Zsqr);
  696. mov $t0,$acc0 // duplicate M
  697. mov $t1,$acc1
  698. mov $t2,$acc2
  699. mov $t3,$acc3
  700. mov $a0,$acc0 // put M aside
  701. mov $a1,$acc1
  702. mov $a2,$acc2
  703. mov $a3,$acc3
  704. add $rp,sp,#$M
  705. bl __ecp_nistz256_add
  706. mov $t0,$a0 // restore M
  707. mov $t1,$a1
  708. ldr $bi,[$ap_real] // forward load for p256_mul_mont
  709. mov $t2,$a2
  710. ldp $a0,$a1,[sp,#$S]
  711. mov $t3,$a3
  712. ldp $a2,$a3,[sp,#$S+16]
  713. bl __ecp_nistz256_add // p256_mul_by_3(M, M);
  714. add $bp,$ap_real,#0
  715. add $rp,sp,#$S
  716. bl __ecp_nistz256_mul_mont // p256_mul_mont(S, S, in_x);
  717. mov $t0,$acc0
  718. mov $t1,$acc1
  719. ldp $a0,$a1,[sp,#$M] // forward load for p256_sqr_mont
  720. mov $t2,$acc2
  721. mov $t3,$acc3
  722. ldp $a2,$a3,[sp,#$M+16]
  723. add $rp,sp,#$tmp0
  724. bl __ecp_nistz256_add // p256_mul_by_2(tmp0, S);
  725. add $rp,$rp_real,#0
  726. bl __ecp_nistz256_sqr_mont // p256_sqr_mont(res_x, M);
  727. add $bp,sp,#$tmp0
  728. bl __ecp_nistz256_sub_from // p256_sub(res_x, res_x, tmp0);
  729. add $bp,sp,#$S
  730. add $rp,sp,#$S
  731. bl __ecp_nistz256_sub_morf // p256_sub(S, S, res_x);
  732. ldr $bi,[sp,#$M]
  733. mov $a0,$acc0 // copy S
  734. mov $a1,$acc1
  735. mov $a2,$acc2
  736. mov $a3,$acc3
  737. add $bp,sp,#$M
  738. bl __ecp_nistz256_mul_mont // p256_mul_mont(S, S, M);
  739. add $bp,$rp_real,#32
  740. add $rp,$rp_real,#32
  741. bl __ecp_nistz256_sub_from // p256_sub(res_y, S, res_y);
  742. add sp,x29,#0 // destroy frame
  743. ldp x19,x20,[x29,#16]
  744. ldp x21,x22,[x29,#32]
  745. ldp x29,x30,[sp],#96
  746. .inst 0xd50323bf // autiasp
  747. ret
  748. .size ecp_nistz256_point_double,.-ecp_nistz256_point_double
  749. ___
  750. }
  751. ########################################################################
  752. # void ecp_nistz256_point_add(P256_POINT *out,const P256_POINT *in1,
  753. # const P256_POINT *in2);
  754. {
  755. my ($res_x,$res_y,$res_z,
  756. $H,$Hsqr,$R,$Rsqr,$Hcub,
  757. $U1,$U2,$S1,$S2)=map(32*$_,(0..11));
  758. my ($Z1sqr, $Z2sqr) = ($Hsqr, $Rsqr);
  759. # above map() describes stack layout with 12 temporary
  760. # 256-bit vectors on top.
  761. my ($rp_real,$ap_real,$bp_real,$in1infty,$in2infty,$temp0,$temp1,$temp2)=map("x$_",(21..28));
  762. $code.=<<___;
  763. .globl ecp_nistz256_point_add
  764. .type ecp_nistz256_point_add,%function
  765. .align 5
  766. ecp_nistz256_point_add:
  767. .inst 0xd503233f // paciasp
  768. stp x29,x30,[sp,#-96]!
  769. add x29,sp,#0
  770. stp x19,x20,[sp,#16]
  771. stp x21,x22,[sp,#32]
  772. stp x23,x24,[sp,#48]
  773. stp x25,x26,[sp,#64]
  774. stp x27,x28,[sp,#80]
  775. sub sp,sp,#32*12
  776. ldp $a0,$a1,[$bp,#64] // in2_z
  777. ldp $a2,$a3,[$bp,#64+16]
  778. mov $rp_real,$rp
  779. mov $ap_real,$ap
  780. mov $bp_real,$bp
  781. ldr $poly1,.Lpoly+8
  782. ldr $poly3,.Lpoly+24
  783. orr $t0,$a0,$a1
  784. orr $t2,$a2,$a3
  785. orr $in2infty,$t0,$t2
  786. cmp $in2infty,#0
  787. csetm $in2infty,ne // ~in2infty
  788. add $rp,sp,#$Z2sqr
  789. bl __ecp_nistz256_sqr_mont // p256_sqr_mont(Z2sqr, in2_z);
  790. ldp $a0,$a1,[$ap_real,#64] // in1_z
  791. ldp $a2,$a3,[$ap_real,#64+16]
  792. orr $t0,$a0,$a1
  793. orr $t2,$a2,$a3
  794. orr $in1infty,$t0,$t2
  795. cmp $in1infty,#0
  796. csetm $in1infty,ne // ~in1infty
  797. add $rp,sp,#$Z1sqr
  798. bl __ecp_nistz256_sqr_mont // p256_sqr_mont(Z1sqr, in1_z);
  799. ldr $bi,[$bp_real,#64]
  800. ldp $a0,$a1,[sp,#$Z2sqr]
  801. ldp $a2,$a3,[sp,#$Z2sqr+16]
  802. add $bp,$bp_real,#64
  803. add $rp,sp,#$S1
  804. bl __ecp_nistz256_mul_mont // p256_mul_mont(S1, Z2sqr, in2_z);
  805. ldr $bi,[$ap_real,#64]
  806. ldp $a0,$a1,[sp,#$Z1sqr]
  807. ldp $a2,$a3,[sp,#$Z1sqr+16]
  808. add $bp,$ap_real,#64
  809. add $rp,sp,#$S2
  810. bl __ecp_nistz256_mul_mont // p256_mul_mont(S2, Z1sqr, in1_z);
  811. ldr $bi,[$ap_real,#32]
  812. ldp $a0,$a1,[sp,#$S1]
  813. ldp $a2,$a3,[sp,#$S1+16]
  814. add $bp,$ap_real,#32
  815. add $rp,sp,#$S1
  816. bl __ecp_nistz256_mul_mont // p256_mul_mont(S1, S1, in1_y);
  817. ldr $bi,[$bp_real,#32]
  818. ldp $a0,$a1,[sp,#$S2]
  819. ldp $a2,$a3,[sp,#$S2+16]
  820. add $bp,$bp_real,#32
  821. add $rp,sp,#$S2
  822. bl __ecp_nistz256_mul_mont // p256_mul_mont(S2, S2, in2_y);
  823. add $bp,sp,#$S1
  824. ldr $bi,[sp,#$Z2sqr] // forward load for p256_mul_mont
  825. ldp $a0,$a1,[$ap_real]
  826. ldp $a2,$a3,[$ap_real,#16]
  827. add $rp,sp,#$R
  828. bl __ecp_nistz256_sub_from // p256_sub(R, S2, S1);
  829. orr $acc0,$acc0,$acc1 // see if result is zero
  830. orr $acc2,$acc2,$acc3
  831. orr $temp0,$acc0,$acc2 // ~is_equal(S1,S2)
  832. add $bp,sp,#$Z2sqr
  833. add $rp,sp,#$U1
  834. bl __ecp_nistz256_mul_mont // p256_mul_mont(U1, in1_x, Z2sqr);
  835. ldr $bi,[sp,#$Z1sqr]
  836. ldp $a0,$a1,[$bp_real]
  837. ldp $a2,$a3,[$bp_real,#16]
  838. add $bp,sp,#$Z1sqr
  839. add $rp,sp,#$U2
  840. bl __ecp_nistz256_mul_mont // p256_mul_mont(U2, in2_x, Z1sqr);
  841. add $bp,sp,#$U1
  842. ldp $a0,$a1,[sp,#$R] // forward load for p256_sqr_mont
  843. ldp $a2,$a3,[sp,#$R+16]
  844. add $rp,sp,#$H
  845. bl __ecp_nistz256_sub_from // p256_sub(H, U2, U1);
  846. orr $acc0,$acc0,$acc1 // see if result is zero
  847. orr $acc2,$acc2,$acc3
  848. orr $acc0,$acc0,$acc2 // ~is_equal(U1,U2)
  849. mvn $temp1,$in1infty // -1/0 -> 0/-1
  850. mvn $temp2,$in2infty // -1/0 -> 0/-1
  851. orr $acc0,$acc0,$temp1
  852. orr $acc0,$acc0,$temp2
  853. orr $acc0,$acc0,$temp0
  854. cbnz $acc0,.Ladd_proceed // if(~is_equal(U1,U2) | in1infty | in2infty | ~is_equal(S1,S2))
  855. .Ladd_double:
  856. mov $ap,$ap_real
  857. mov $rp,$rp_real
  858. ldp x23,x24,[x29,#48]
  859. ldp x25,x26,[x29,#64]
  860. ldp x27,x28,[x29,#80]
  861. add sp,sp,#32*(12-4) // difference in stack frames
  862. b .Ldouble_shortcut
  863. .align 4
  864. .Ladd_proceed:
  865. add $rp,sp,#$Rsqr
  866. bl __ecp_nistz256_sqr_mont // p256_sqr_mont(Rsqr, R);
  867. ldr $bi,[$ap_real,#64]
  868. ldp $a0,$a1,[sp,#$H]
  869. ldp $a2,$a3,[sp,#$H+16]
  870. add $bp,$ap_real,#64
  871. add $rp,sp,#$res_z
  872. bl __ecp_nistz256_mul_mont // p256_mul_mont(res_z, H, in1_z);
  873. ldp $a0,$a1,[sp,#$H]
  874. ldp $a2,$a3,[sp,#$H+16]
  875. add $rp,sp,#$Hsqr
  876. bl __ecp_nistz256_sqr_mont // p256_sqr_mont(Hsqr, H);
  877. ldr $bi,[$bp_real,#64]
  878. ldp $a0,$a1,[sp,#$res_z]
  879. ldp $a2,$a3,[sp,#$res_z+16]
  880. add $bp,$bp_real,#64
  881. add $rp,sp,#$res_z
  882. bl __ecp_nistz256_mul_mont // p256_mul_mont(res_z, res_z, in2_z);
  883. ldr $bi,[sp,#$H]
  884. ldp $a0,$a1,[sp,#$Hsqr]
  885. ldp $a2,$a3,[sp,#$Hsqr+16]
  886. add $bp,sp,#$H
  887. add $rp,sp,#$Hcub
  888. bl __ecp_nistz256_mul_mont // p256_mul_mont(Hcub, Hsqr, H);
  889. ldr $bi,[sp,#$Hsqr]
  890. ldp $a0,$a1,[sp,#$U1]
  891. ldp $a2,$a3,[sp,#$U1+16]
  892. add $bp,sp,#$Hsqr
  893. add $rp,sp,#$U2
  894. bl __ecp_nistz256_mul_mont // p256_mul_mont(U2, U1, Hsqr);
  895. mov $t0,$acc0
  896. mov $t1,$acc1
  897. mov $t2,$acc2
  898. mov $t3,$acc3
  899. add $rp,sp,#$Hsqr
  900. bl __ecp_nistz256_add // p256_mul_by_2(Hsqr, U2);
  901. add $bp,sp,#$Rsqr
  902. add $rp,sp,#$res_x
  903. bl __ecp_nistz256_sub_morf // p256_sub(res_x, Rsqr, Hsqr);
  904. add $bp,sp,#$Hcub
  905. bl __ecp_nistz256_sub_from // p256_sub(res_x, res_x, Hcub);
  906. add $bp,sp,#$U2
  907. ldr $bi,[sp,#$Hcub] // forward load for p256_mul_mont
  908. ldp $a0,$a1,[sp,#$S1]
  909. ldp $a2,$a3,[sp,#$S1+16]
  910. add $rp,sp,#$res_y
  911. bl __ecp_nistz256_sub_morf // p256_sub(res_y, U2, res_x);
  912. add $bp,sp,#$Hcub
  913. add $rp,sp,#$S2
  914. bl __ecp_nistz256_mul_mont // p256_mul_mont(S2, S1, Hcub);
  915. ldr $bi,[sp,#$R]
  916. ldp $a0,$a1,[sp,#$res_y]
  917. ldp $a2,$a3,[sp,#$res_y+16]
  918. add $bp,sp,#$R
  919. add $rp,sp,#$res_y
  920. bl __ecp_nistz256_mul_mont // p256_mul_mont(res_y, res_y, R);
  921. add $bp,sp,#$S2
  922. bl __ecp_nistz256_sub_from // p256_sub(res_y, res_y, S2);
  923. ldp $a0,$a1,[sp,#$res_x] // res
  924. ldp $a2,$a3,[sp,#$res_x+16]
  925. ldp $t0,$t1,[$bp_real] // in2
  926. ldp $t2,$t3,[$bp_real,#16]
  927. ___
  928. for($i=0;$i<64;$i+=32) { # conditional moves
  929. $code.=<<___;
  930. ldp $acc0,$acc1,[$ap_real,#$i] // in1
  931. cmp $in1infty,#0 // ~$in1intfy, remember?
  932. ldp $acc2,$acc3,[$ap_real,#$i+16]
  933. csel $t0,$a0,$t0,ne
  934. csel $t1,$a1,$t1,ne
  935. ldp $a0,$a1,[sp,#$res_x+$i+32] // res
  936. csel $t2,$a2,$t2,ne
  937. csel $t3,$a3,$t3,ne
  938. cmp $in2infty,#0 // ~$in2intfy, remember?
  939. ldp $a2,$a3,[sp,#$res_x+$i+48]
  940. csel $acc0,$t0,$acc0,ne
  941. csel $acc1,$t1,$acc1,ne
  942. ldp $t0,$t1,[$bp_real,#$i+32] // in2
  943. csel $acc2,$t2,$acc2,ne
  944. csel $acc3,$t3,$acc3,ne
  945. ldp $t2,$t3,[$bp_real,#$i+48]
  946. stp $acc0,$acc1,[$rp_real,#$i]
  947. stp $acc2,$acc3,[$rp_real,#$i+16]
  948. ___
  949. }
  950. $code.=<<___;
  951. ldp $acc0,$acc1,[$ap_real,#$i] // in1
  952. cmp $in1infty,#0 // ~$in1intfy, remember?
  953. ldp $acc2,$acc3,[$ap_real,#$i+16]
  954. csel $t0,$a0,$t0,ne
  955. csel $t1,$a1,$t1,ne
  956. csel $t2,$a2,$t2,ne
  957. csel $t3,$a3,$t3,ne
  958. cmp $in2infty,#0 // ~$in2intfy, remember?
  959. csel $acc0,$t0,$acc0,ne
  960. csel $acc1,$t1,$acc1,ne
  961. csel $acc2,$t2,$acc2,ne
  962. csel $acc3,$t3,$acc3,ne
  963. stp $acc0,$acc1,[$rp_real,#$i]
  964. stp $acc2,$acc3,[$rp_real,#$i+16]
  965. .Ladd_done:
  966. add sp,x29,#0 // destroy frame
  967. ldp x19,x20,[x29,#16]
  968. ldp x21,x22,[x29,#32]
  969. ldp x23,x24,[x29,#48]
  970. ldp x25,x26,[x29,#64]
  971. ldp x27,x28,[x29,#80]
  972. ldp x29,x30,[sp],#96
  973. .inst 0xd50323bf // autiasp
  974. ret
  975. .size ecp_nistz256_point_add,.-ecp_nistz256_point_add
  976. ___
  977. }
  978. ########################################################################
  979. # void ecp_nistz256_point_add_affine(P256_POINT *out,const P256_POINT *in1,
  980. # const P256_POINT_AFFINE *in2);
  981. {
  982. my ($res_x,$res_y,$res_z,
  983. $U2,$S2,$H,$R,$Hsqr,$Hcub,$Rsqr)=map(32*$_,(0..9));
  984. my $Z1sqr = $S2;
  985. # above map() describes stack layout with 10 temporary
  986. # 256-bit vectors on top.
  987. my ($rp_real,$ap_real,$bp_real,$in1infty,$in2infty,$temp)=map("x$_",(21..26));
  988. $code.=<<___;
  989. .globl ecp_nistz256_point_add_affine
  990. .type ecp_nistz256_point_add_affine,%function
  991. .align 5
  992. ecp_nistz256_point_add_affine:
  993. .inst 0xd503233f // paciasp
  994. stp x29,x30,[sp,#-80]!
  995. add x29,sp,#0
  996. stp x19,x20,[sp,#16]
  997. stp x21,x22,[sp,#32]
  998. stp x23,x24,[sp,#48]
  999. stp x25,x26,[sp,#64]
  1000. sub sp,sp,#32*10
  1001. mov $rp_real,$rp
  1002. mov $ap_real,$ap
  1003. mov $bp_real,$bp
  1004. ldr $poly1,.Lpoly+8
  1005. ldr $poly3,.Lpoly+24
  1006. ldp $a0,$a1,[$ap,#64] // in1_z
  1007. ldp $a2,$a3,[$ap,#64+16]
  1008. orr $t0,$a0,$a1
  1009. orr $t2,$a2,$a3
  1010. orr $in1infty,$t0,$t2
  1011. cmp $in1infty,#0
  1012. csetm $in1infty,ne // ~in1infty
  1013. ldp $acc0,$acc1,[$bp] // in2_x
  1014. ldp $acc2,$acc3,[$bp,#16]
  1015. ldp $t0,$t1,[$bp,#32] // in2_y
  1016. ldp $t2,$t3,[$bp,#48]
  1017. orr $acc0,$acc0,$acc1
  1018. orr $acc2,$acc2,$acc3
  1019. orr $t0,$t0,$t1
  1020. orr $t2,$t2,$t3
  1021. orr $acc0,$acc0,$acc2
  1022. orr $t0,$t0,$t2
  1023. orr $in2infty,$acc0,$t0
  1024. cmp $in2infty,#0
  1025. csetm $in2infty,ne // ~in2infty
  1026. add $rp,sp,#$Z1sqr
  1027. bl __ecp_nistz256_sqr_mont // p256_sqr_mont(Z1sqr, in1_z);
  1028. mov $a0,$acc0
  1029. mov $a1,$acc1
  1030. mov $a2,$acc2
  1031. mov $a3,$acc3
  1032. ldr $bi,[$bp_real]
  1033. add $bp,$bp_real,#0
  1034. add $rp,sp,#$U2
  1035. bl __ecp_nistz256_mul_mont // p256_mul_mont(U2, Z1sqr, in2_x);
  1036. add $bp,$ap_real,#0
  1037. ldr $bi,[$ap_real,#64] // forward load for p256_mul_mont
  1038. ldp $a0,$a1,[sp,#$Z1sqr]
  1039. ldp $a2,$a3,[sp,#$Z1sqr+16]
  1040. add $rp,sp,#$H
  1041. bl __ecp_nistz256_sub_from // p256_sub(H, U2, in1_x);
  1042. add $bp,$ap_real,#64
  1043. add $rp,sp,#$S2
  1044. bl __ecp_nistz256_mul_mont // p256_mul_mont(S2, Z1sqr, in1_z);
  1045. ldr $bi,[$ap_real,#64]
  1046. ldp $a0,$a1,[sp,#$H]
  1047. ldp $a2,$a3,[sp,#$H+16]
  1048. add $bp,$ap_real,#64
  1049. add $rp,sp,#$res_z
  1050. bl __ecp_nistz256_mul_mont // p256_mul_mont(res_z, H, in1_z);
  1051. ldr $bi,[$bp_real,#32]
  1052. ldp $a0,$a1,[sp,#$S2]
  1053. ldp $a2,$a3,[sp,#$S2+16]
  1054. add $bp,$bp_real,#32
  1055. add $rp,sp,#$S2
  1056. bl __ecp_nistz256_mul_mont // p256_mul_mont(S2, S2, in2_y);
  1057. add $bp,$ap_real,#32
  1058. ldp $a0,$a1,[sp,#$H] // forward load for p256_sqr_mont
  1059. ldp $a2,$a3,[sp,#$H+16]
  1060. add $rp,sp,#$R
  1061. bl __ecp_nistz256_sub_from // p256_sub(R, S2, in1_y);
  1062. add $rp,sp,#$Hsqr
  1063. bl __ecp_nistz256_sqr_mont // p256_sqr_mont(Hsqr, H);
  1064. ldp $a0,$a1,[sp,#$R]
  1065. ldp $a2,$a3,[sp,#$R+16]
  1066. add $rp,sp,#$Rsqr
  1067. bl __ecp_nistz256_sqr_mont // p256_sqr_mont(Rsqr, R);
  1068. ldr $bi,[sp,#$H]
  1069. ldp $a0,$a1,[sp,#$Hsqr]
  1070. ldp $a2,$a3,[sp,#$Hsqr+16]
  1071. add $bp,sp,#$H
  1072. add $rp,sp,#$Hcub
  1073. bl __ecp_nistz256_mul_mont // p256_mul_mont(Hcub, Hsqr, H);
  1074. ldr $bi,[$ap_real]
  1075. ldp $a0,$a1,[sp,#$Hsqr]
  1076. ldp $a2,$a3,[sp,#$Hsqr+16]
  1077. add $bp,$ap_real,#0
  1078. add $rp,sp,#$U2
  1079. bl __ecp_nistz256_mul_mont // p256_mul_mont(U2, in1_x, Hsqr);
  1080. mov $t0,$acc0
  1081. mov $t1,$acc1
  1082. mov $t2,$acc2
  1083. mov $t3,$acc3
  1084. add $rp,sp,#$Hsqr
  1085. bl __ecp_nistz256_add // p256_mul_by_2(Hsqr, U2);
  1086. add $bp,sp,#$Rsqr
  1087. add $rp,sp,#$res_x
  1088. bl __ecp_nistz256_sub_morf // p256_sub(res_x, Rsqr, Hsqr);
  1089. add $bp,sp,#$Hcub
  1090. bl __ecp_nistz256_sub_from // p256_sub(res_x, res_x, Hcub);
  1091. add $bp,sp,#$U2
  1092. ldr $bi,[$ap_real,#32] // forward load for p256_mul_mont
  1093. ldp $a0,$a1,[sp,#$Hcub]
  1094. ldp $a2,$a3,[sp,#$Hcub+16]
  1095. add $rp,sp,#$res_y
  1096. bl __ecp_nistz256_sub_morf // p256_sub(res_y, U2, res_x);
  1097. add $bp,$ap_real,#32
  1098. add $rp,sp,#$S2
  1099. bl __ecp_nistz256_mul_mont // p256_mul_mont(S2, in1_y, Hcub);
  1100. ldr $bi,[sp,#$R]
  1101. ldp $a0,$a1,[sp,#$res_y]
  1102. ldp $a2,$a3,[sp,#$res_y+16]
  1103. add $bp,sp,#$R
  1104. add $rp,sp,#$res_y
  1105. bl __ecp_nistz256_mul_mont // p256_mul_mont(res_y, res_y, R);
  1106. add $bp,sp,#$S2
  1107. bl __ecp_nistz256_sub_from // p256_sub(res_y, res_y, S2);
  1108. ldp $a0,$a1,[sp,#$res_x] // res
  1109. ldp $a2,$a3,[sp,#$res_x+16]
  1110. ldp $t0,$t1,[$bp_real] // in2
  1111. ldp $t2,$t3,[$bp_real,#16]
  1112. ___
  1113. for($i=0;$i<64;$i+=32) { # conditional moves
  1114. $code.=<<___;
  1115. ldp $acc0,$acc1,[$ap_real,#$i] // in1
  1116. cmp $in1infty,#0 // ~$in1intfy, remember?
  1117. ldp $acc2,$acc3,[$ap_real,#$i+16]
  1118. csel $t0,$a0,$t0,ne
  1119. csel $t1,$a1,$t1,ne
  1120. ldp $a0,$a1,[sp,#$res_x+$i+32] // res
  1121. csel $t2,$a2,$t2,ne
  1122. csel $t3,$a3,$t3,ne
  1123. cmp $in2infty,#0 // ~$in2intfy, remember?
  1124. ldp $a2,$a3,[sp,#$res_x+$i+48]
  1125. csel $acc0,$t0,$acc0,ne
  1126. csel $acc1,$t1,$acc1,ne
  1127. ldp $t0,$t1,[$bp_real,#$i+32] // in2
  1128. csel $acc2,$t2,$acc2,ne
  1129. csel $acc3,$t3,$acc3,ne
  1130. ldp $t2,$t3,[$bp_real,#$i+48]
  1131. stp $acc0,$acc1,[$rp_real,#$i]
  1132. stp $acc2,$acc3,[$rp_real,#$i+16]
  1133. ___
  1134. $code.=<<___ if ($i == 0);
  1135. adr $bp_real,.Lone_mont-64
  1136. ___
  1137. }
  1138. $code.=<<___;
  1139. ldp $acc0,$acc1,[$ap_real,#$i] // in1
  1140. cmp $in1infty,#0 // ~$in1intfy, remember?
  1141. ldp $acc2,$acc3,[$ap_real,#$i+16]
  1142. csel $t0,$a0,$t0,ne
  1143. csel $t1,$a1,$t1,ne
  1144. csel $t2,$a2,$t2,ne
  1145. csel $t3,$a3,$t3,ne
  1146. cmp $in2infty,#0 // ~$in2intfy, remember?
  1147. csel $acc0,$t0,$acc0,ne
  1148. csel $acc1,$t1,$acc1,ne
  1149. csel $acc2,$t2,$acc2,ne
  1150. csel $acc3,$t3,$acc3,ne
  1151. stp $acc0,$acc1,[$rp_real,#$i]
  1152. stp $acc2,$acc3,[$rp_real,#$i+16]
  1153. add sp,x29,#0 // destroy frame
  1154. ldp x19,x20,[x29,#16]
  1155. ldp x21,x22,[x29,#32]
  1156. ldp x23,x24,[x29,#48]
  1157. ldp x25,x26,[x29,#64]
  1158. ldp x29,x30,[sp],#80
  1159. .inst 0xd50323bf // autiasp
  1160. ret
  1161. .size ecp_nistz256_point_add_affine,.-ecp_nistz256_point_add_affine
  1162. ___
  1163. }
  1164. if (1) {
  1165. my ($ord0,$ord1) = ($poly1,$poly3);
  1166. my ($ord2,$ord3,$ordk,$t4) = map("x$_",(21..24));
  1167. my $acc7 = $bi;
  1168. $code.=<<___;
  1169. ////////////////////////////////////////////////////////////////////////
  1170. // void ecp_nistz256_ord_mul_mont(uint64_t res[4], uint64_t a[4],
  1171. // uint64_t b[4]);
  1172. .globl ecp_nistz256_ord_mul_mont
  1173. .type ecp_nistz256_ord_mul_mont,%function
  1174. .align 4
  1175. ecp_nistz256_ord_mul_mont:
  1176. stp x29,x30,[sp,#-64]!
  1177. add x29,sp,#0
  1178. stp x19,x20,[sp,#16]
  1179. stp x21,x22,[sp,#32]
  1180. stp x23,x24,[sp,#48]
  1181. adr $ordk,.Lord
  1182. ldr $bi,[$bp] // bp[0]
  1183. ldp $a0,$a1,[$ap]
  1184. ldp $a2,$a3,[$ap,#16]
  1185. ldp $ord0,$ord1,[$ordk,#0]
  1186. ldp $ord2,$ord3,[$ordk,#16]
  1187. ldr $ordk,[$ordk,#32]
  1188. mul $acc0,$a0,$bi // a[0]*b[0]
  1189. umulh $t0,$a0,$bi
  1190. mul $acc1,$a1,$bi // a[1]*b[0]
  1191. umulh $t1,$a1,$bi
  1192. mul $acc2,$a2,$bi // a[2]*b[0]
  1193. umulh $t2,$a2,$bi
  1194. mul $acc3,$a3,$bi // a[3]*b[0]
  1195. umulh $acc4,$a3,$bi
  1196. mul $t4,$acc0,$ordk
  1197. adds $acc1,$acc1,$t0 // accumulate high parts of multiplication
  1198. adcs $acc2,$acc2,$t1
  1199. adcs $acc3,$acc3,$t2
  1200. adc $acc4,$acc4,xzr
  1201. mov $acc5,xzr
  1202. ___
  1203. for ($i=1;$i<4;$i++) {
  1204. ################################################################
  1205. # ffff0000.ffffffff.yyyyyyyy.zzzzzzzz
  1206. # * abcdefgh
  1207. # + xxxxxxxx.xxxxxxxx.xxxxxxxx.xxxxxxxx.xxxxxxxx
  1208. #
  1209. # Now observing that ff..ff*x = (2^n-1)*x = 2^n*x-x, we
  1210. # rewrite above as:
  1211. #
  1212. # xxxxxxxx.xxxxxxxx.xxxxxxxx.xxxxxxxx.xxxxxxxx
  1213. # - 0000abcd.efgh0000.abcdefgh.00000000.00000000
  1214. # + abcdefgh.abcdefgh.yzayzbyz.cyzdyzey.zfyzgyzh
  1215. $code.=<<___;
  1216. ldr $bi,[$bp,#8*$i] // b[i]
  1217. lsl $t0,$t4,#32
  1218. subs $acc2,$acc2,$t4
  1219. lsr $t1,$t4,#32
  1220. sbcs $acc3,$acc3,$t0
  1221. sbcs $acc4,$acc4,$t1
  1222. sbc $acc5,$acc5,xzr
  1223. subs xzr,$acc0,#1
  1224. umulh $t1,$ord0,$t4
  1225. mul $t2,$ord1,$t4
  1226. umulh $t3,$ord1,$t4
  1227. adcs $t2,$t2,$t1
  1228. mul $t0,$a0,$bi
  1229. adc $t3,$t3,xzr
  1230. mul $t1,$a1,$bi
  1231. adds $acc0,$acc1,$t2
  1232. mul $t2,$a2,$bi
  1233. adcs $acc1,$acc2,$t3
  1234. mul $t3,$a3,$bi
  1235. adcs $acc2,$acc3,$t4
  1236. adcs $acc3,$acc4,$t4
  1237. adc $acc4,$acc5,xzr
  1238. adds $acc0,$acc0,$t0 // accumulate low parts
  1239. umulh $t0,$a0,$bi
  1240. adcs $acc1,$acc1,$t1
  1241. umulh $t1,$a1,$bi
  1242. adcs $acc2,$acc2,$t2
  1243. umulh $t2,$a2,$bi
  1244. adcs $acc3,$acc3,$t3
  1245. umulh $t3,$a3,$bi
  1246. adc $acc4,$acc4,xzr
  1247. mul $t4,$acc0,$ordk
  1248. adds $acc1,$acc1,$t0 // accumulate high parts
  1249. adcs $acc2,$acc2,$t1
  1250. adcs $acc3,$acc3,$t2
  1251. adcs $acc4,$acc4,$t3
  1252. adc $acc5,xzr,xzr
  1253. ___
  1254. }
  1255. $code.=<<___;
  1256. lsl $t0,$t4,#32 // last reduction
  1257. subs $acc2,$acc2,$t4
  1258. lsr $t1,$t4,#32
  1259. sbcs $acc3,$acc3,$t0
  1260. sbcs $acc4,$acc4,$t1
  1261. sbc $acc5,$acc5,xzr
  1262. subs xzr,$acc0,#1
  1263. umulh $t1,$ord0,$t4
  1264. mul $t2,$ord1,$t4
  1265. umulh $t3,$ord1,$t4
  1266. adcs $t2,$t2,$t1
  1267. adc $t3,$t3,xzr
  1268. adds $acc0,$acc1,$t2
  1269. adcs $acc1,$acc2,$t3
  1270. adcs $acc2,$acc3,$t4
  1271. adcs $acc3,$acc4,$t4
  1272. adc $acc4,$acc5,xzr
  1273. subs $t0,$acc0,$ord0 // ret -= modulus
  1274. sbcs $t1,$acc1,$ord1
  1275. sbcs $t2,$acc2,$ord2
  1276. sbcs $t3,$acc3,$ord3
  1277. sbcs xzr,$acc4,xzr
  1278. csel $acc0,$acc0,$t0,lo // ret = borrow ? ret : ret-modulus
  1279. csel $acc1,$acc1,$t1,lo
  1280. csel $acc2,$acc2,$t2,lo
  1281. stp $acc0,$acc1,[$rp]
  1282. csel $acc3,$acc3,$t3,lo
  1283. stp $acc2,$acc3,[$rp,#16]
  1284. ldp x19,x20,[sp,#16]
  1285. ldp x21,x22,[sp,#32]
  1286. ldp x23,x24,[sp,#48]
  1287. ldr x29,[sp],#64
  1288. ret
  1289. .size ecp_nistz256_ord_mul_mont,.-ecp_nistz256_ord_mul_mont
  1290. ////////////////////////////////////////////////////////////////////////
  1291. // void ecp_nistz256_ord_sqr_mont(uint64_t res[4], uint64_t a[4],
  1292. // int rep);
  1293. .globl ecp_nistz256_ord_sqr_mont
  1294. .type ecp_nistz256_ord_sqr_mont,%function
  1295. .align 4
  1296. ecp_nistz256_ord_sqr_mont:
  1297. stp x29,x30,[sp,#-64]!
  1298. add x29,sp,#0
  1299. stp x19,x20,[sp,#16]
  1300. stp x21,x22,[sp,#32]
  1301. stp x23,x24,[sp,#48]
  1302. adr $ordk,.Lord
  1303. ldp $a0,$a1,[$ap]
  1304. ldp $a2,$a3,[$ap,#16]
  1305. ldp $ord0,$ord1,[$ordk,#0]
  1306. ldp $ord2,$ord3,[$ordk,#16]
  1307. ldr $ordk,[$ordk,#32]
  1308. b .Loop_ord_sqr
  1309. .align 4
  1310. .Loop_ord_sqr:
  1311. sub $bp,$bp,#1
  1312. ////////////////////////////////////////////////////////////////
  1313. // | | | | | |a1*a0| |
  1314. // | | | | |a2*a0| | |
  1315. // | |a3*a2|a3*a0| | | |
  1316. // | | | |a2*a1| | | |
  1317. // | | |a3*a1| | | | |
  1318. // *| | | | | | | | 2|
  1319. // +|a3*a3|a2*a2|a1*a1|a0*a0|
  1320. // |--+--+--+--+--+--+--+--|
  1321. // |A7|A6|A5|A4|A3|A2|A1|A0|, where Ax is $accx, i.e. follow $accx
  1322. //
  1323. // "can't overflow" below mark carrying into high part of
  1324. // multiplication result, which can't overflow, because it
  1325. // can never be all ones.
  1326. mul $acc1,$a1,$a0 // a[1]*a[0]
  1327. umulh $t1,$a1,$a0
  1328. mul $acc2,$a2,$a0 // a[2]*a[0]
  1329. umulh $t2,$a2,$a0
  1330. mul $acc3,$a3,$a0 // a[3]*a[0]
  1331. umulh $acc4,$a3,$a0
  1332. adds $acc2,$acc2,$t1 // accumulate high parts of multiplication
  1333. mul $t0,$a2,$a1 // a[2]*a[1]
  1334. umulh $t1,$a2,$a1
  1335. adcs $acc3,$acc3,$t2
  1336. mul $t2,$a3,$a1 // a[3]*a[1]
  1337. umulh $t3,$a3,$a1
  1338. adc $acc4,$acc4,xzr // can't overflow
  1339. mul $acc5,$a3,$a2 // a[3]*a[2]
  1340. umulh $acc6,$a3,$a2
  1341. adds $t1,$t1,$t2 // accumulate high parts of multiplication
  1342. mul $acc0,$a0,$a0 // a[0]*a[0]
  1343. adc $t2,$t3,xzr // can't overflow
  1344. adds $acc3,$acc3,$t0 // accumulate low parts of multiplication
  1345. umulh $a0,$a0,$a0
  1346. adcs $acc4,$acc4,$t1
  1347. mul $t1,$a1,$a1 // a[1]*a[1]
  1348. adcs $acc5,$acc5,$t2
  1349. umulh $a1,$a1,$a1
  1350. adc $acc6,$acc6,xzr // can't overflow
  1351. adds $acc1,$acc1,$acc1 // acc[1-6]*=2
  1352. mul $t2,$a2,$a2 // a[2]*a[2]
  1353. adcs $acc2,$acc2,$acc2
  1354. umulh $a2,$a2,$a2
  1355. adcs $acc3,$acc3,$acc3
  1356. mul $t3,$a3,$a3 // a[3]*a[3]
  1357. adcs $acc4,$acc4,$acc4
  1358. umulh $a3,$a3,$a3
  1359. adcs $acc5,$acc5,$acc5
  1360. adcs $acc6,$acc6,$acc6
  1361. adc $acc7,xzr,xzr
  1362. adds $acc1,$acc1,$a0 // +a[i]*a[i]
  1363. mul $t4,$acc0,$ordk
  1364. adcs $acc2,$acc2,$t1
  1365. adcs $acc3,$acc3,$a1
  1366. adcs $acc4,$acc4,$t2
  1367. adcs $acc5,$acc5,$a2
  1368. adcs $acc6,$acc6,$t3
  1369. adc $acc7,$acc7,$a3
  1370. ___
  1371. for($i=0; $i<4; $i++) { # reductions
  1372. $code.=<<___;
  1373. subs xzr,$acc0,#1
  1374. umulh $t1,$ord0,$t4
  1375. mul $t2,$ord1,$t4
  1376. umulh $t3,$ord1,$t4
  1377. adcs $t2,$t2,$t1
  1378. adc $t3,$t3,xzr
  1379. adds $acc0,$acc1,$t2
  1380. adcs $acc1,$acc2,$t3
  1381. adcs $acc2,$acc3,$t4
  1382. adc $acc3,xzr,$t4 // can't overflow
  1383. ___
  1384. $code.=<<___ if ($i<3);
  1385. mul $t3,$acc0,$ordk
  1386. ___
  1387. $code.=<<___;
  1388. lsl $t0,$t4,#32
  1389. subs $acc1,$acc1,$t4
  1390. lsr $t1,$t4,#32
  1391. sbcs $acc2,$acc2,$t0
  1392. sbc $acc3,$acc3,$t1 // can't borrow
  1393. ___
  1394. ($t3,$t4) = ($t4,$t3);
  1395. }
  1396. $code.=<<___;
  1397. adds $acc0,$acc0,$acc4 // accumulate upper half
  1398. adcs $acc1,$acc1,$acc5
  1399. adcs $acc2,$acc2,$acc6
  1400. adcs $acc3,$acc3,$acc7
  1401. adc $acc4,xzr,xzr
  1402. subs $t0,$acc0,$ord0 // ret -= modulus
  1403. sbcs $t1,$acc1,$ord1
  1404. sbcs $t2,$acc2,$ord2
  1405. sbcs $t3,$acc3,$ord3
  1406. sbcs xzr,$acc4,xzr
  1407. csel $a0,$acc0,$t0,lo // ret = borrow ? ret : ret-modulus
  1408. csel $a1,$acc1,$t1,lo
  1409. csel $a2,$acc2,$t2,lo
  1410. csel $a3,$acc3,$t3,lo
  1411. cbnz $bp,.Loop_ord_sqr
  1412. stp $a0,$a1,[$rp]
  1413. stp $a2,$a3,[$rp,#16]
  1414. ldp x19,x20,[sp,#16]
  1415. ldp x21,x22,[sp,#32]
  1416. ldp x23,x24,[sp,#48]
  1417. ldr x29,[sp],#64
  1418. ret
  1419. .size ecp_nistz256_ord_sqr_mont,.-ecp_nistz256_ord_sqr_mont
  1420. ___
  1421. } }
  1422. ########################################################################
  1423. # scatter-gather subroutines
  1424. {
  1425. my ($out,$inp,$index,$mask)=map("x$_",(0..3));
  1426. $code.=<<___;
  1427. // void ecp_nistz256_scatter_w5(void *x0,const P256_POINT *x1,
  1428. // int x2);
  1429. .globl ecp_nistz256_scatter_w5
  1430. .type ecp_nistz256_scatter_w5,%function
  1431. .align 4
  1432. ecp_nistz256_scatter_w5:
  1433. stp x29,x30,[sp,#-16]!
  1434. add x29,sp,#0
  1435. add $out,$out,$index,lsl#2
  1436. ldp x4,x5,[$inp] // X
  1437. ldp x6,x7,[$inp,#16]
  1438. str w4,[$out,#64*0-4]
  1439. lsr x4,x4,#32
  1440. str w5,[$out,#64*1-4]
  1441. lsr x5,x5,#32
  1442. str w6,[$out,#64*2-4]
  1443. lsr x6,x6,#32
  1444. str w7,[$out,#64*3-4]
  1445. lsr x7,x7,#32
  1446. str w4,[$out,#64*4-4]
  1447. str w5,[$out,#64*5-4]
  1448. str w6,[$out,#64*6-4]
  1449. str w7,[$out,#64*7-4]
  1450. add $out,$out,#64*8
  1451. ldp x4,x5,[$inp,#32] // Y
  1452. ldp x6,x7,[$inp,#48]
  1453. str w4,[$out,#64*0-4]
  1454. lsr x4,x4,#32
  1455. str w5,[$out,#64*1-4]
  1456. lsr x5,x5,#32
  1457. str w6,[$out,#64*2-4]
  1458. lsr x6,x6,#32
  1459. str w7,[$out,#64*3-4]
  1460. lsr x7,x7,#32
  1461. str w4,[$out,#64*4-4]
  1462. str w5,[$out,#64*5-4]
  1463. str w6,[$out,#64*6-4]
  1464. str w7,[$out,#64*7-4]
  1465. add $out,$out,#64*8
  1466. ldp x4,x5,[$inp,#64] // Z
  1467. ldp x6,x7,[$inp,#80]
  1468. str w4,[$out,#64*0-4]
  1469. lsr x4,x4,#32
  1470. str w5,[$out,#64*1-4]
  1471. lsr x5,x5,#32
  1472. str w6,[$out,#64*2-4]
  1473. lsr x6,x6,#32
  1474. str w7,[$out,#64*3-4]
  1475. lsr x7,x7,#32
  1476. str w4,[$out,#64*4-4]
  1477. str w5,[$out,#64*5-4]
  1478. str w6,[$out,#64*6-4]
  1479. str w7,[$out,#64*7-4]
  1480. ldr x29,[sp],#16
  1481. ret
  1482. .size ecp_nistz256_scatter_w5,.-ecp_nistz256_scatter_w5
  1483. // void ecp_nistz256_gather_w5(P256_POINT *x0,const void *x1,
  1484. // int x2);
  1485. .globl ecp_nistz256_gather_w5
  1486. .type ecp_nistz256_gather_w5,%function
  1487. .align 4
  1488. ecp_nistz256_gather_w5:
  1489. stp x29,x30,[sp,#-16]!
  1490. add x29,sp,#0
  1491. cmp $index,xzr
  1492. csetm x3,ne
  1493. add $index,$index,x3
  1494. add $inp,$inp,$index,lsl#2
  1495. ldr w4,[$inp,#64*0]
  1496. ldr w5,[$inp,#64*1]
  1497. ldr w6,[$inp,#64*2]
  1498. ldr w7,[$inp,#64*3]
  1499. ldr w8,[$inp,#64*4]
  1500. ldr w9,[$inp,#64*5]
  1501. ldr w10,[$inp,#64*6]
  1502. ldr w11,[$inp,#64*7]
  1503. add $inp,$inp,#64*8
  1504. orr x4,x4,x8,lsl#32
  1505. orr x5,x5,x9,lsl#32
  1506. orr x6,x6,x10,lsl#32
  1507. orr x7,x7,x11,lsl#32
  1508. csel x4,x4,xzr,ne
  1509. csel x5,x5,xzr,ne
  1510. csel x6,x6,xzr,ne
  1511. csel x7,x7,xzr,ne
  1512. stp x4,x5,[$out] // X
  1513. stp x6,x7,[$out,#16]
  1514. ldr w4,[$inp,#64*0]
  1515. ldr w5,[$inp,#64*1]
  1516. ldr w6,[$inp,#64*2]
  1517. ldr w7,[$inp,#64*3]
  1518. ldr w8,[$inp,#64*4]
  1519. ldr w9,[$inp,#64*5]
  1520. ldr w10,[$inp,#64*6]
  1521. ldr w11,[$inp,#64*7]
  1522. add $inp,$inp,#64*8
  1523. orr x4,x4,x8,lsl#32
  1524. orr x5,x5,x9,lsl#32
  1525. orr x6,x6,x10,lsl#32
  1526. orr x7,x7,x11,lsl#32
  1527. csel x4,x4,xzr,ne
  1528. csel x5,x5,xzr,ne
  1529. csel x6,x6,xzr,ne
  1530. csel x7,x7,xzr,ne
  1531. stp x4,x5,[$out,#32] // Y
  1532. stp x6,x7,[$out,#48]
  1533. ldr w4,[$inp,#64*0]
  1534. ldr w5,[$inp,#64*1]
  1535. ldr w6,[$inp,#64*2]
  1536. ldr w7,[$inp,#64*3]
  1537. ldr w8,[$inp,#64*4]
  1538. ldr w9,[$inp,#64*5]
  1539. ldr w10,[$inp,#64*6]
  1540. ldr w11,[$inp,#64*7]
  1541. orr x4,x4,x8,lsl#32
  1542. orr x5,x5,x9,lsl#32
  1543. orr x6,x6,x10,lsl#32
  1544. orr x7,x7,x11,lsl#32
  1545. csel x4,x4,xzr,ne
  1546. csel x5,x5,xzr,ne
  1547. csel x6,x6,xzr,ne
  1548. csel x7,x7,xzr,ne
  1549. stp x4,x5,[$out,#64] // Z
  1550. stp x6,x7,[$out,#80]
  1551. ldr x29,[sp],#16
  1552. ret
  1553. .size ecp_nistz256_gather_w5,.-ecp_nistz256_gather_w5
  1554. // void ecp_nistz256_scatter_w7(void *x0,const P256_POINT_AFFINE *x1,
  1555. // int x2);
  1556. .globl ecp_nistz256_scatter_w7
  1557. .type ecp_nistz256_scatter_w7,%function
  1558. .align 4
  1559. ecp_nistz256_scatter_w7:
  1560. stp x29,x30,[sp,#-16]!
  1561. add x29,sp,#0
  1562. add $out,$out,$index
  1563. mov $index,#64/8
  1564. .Loop_scatter_w7:
  1565. ldr x3,[$inp],#8
  1566. subs $index,$index,#1
  1567. prfm pstl1strm,[$out,#4096+64*0]
  1568. prfm pstl1strm,[$out,#4096+64*1]
  1569. prfm pstl1strm,[$out,#4096+64*2]
  1570. prfm pstl1strm,[$out,#4096+64*3]
  1571. prfm pstl1strm,[$out,#4096+64*4]
  1572. prfm pstl1strm,[$out,#4096+64*5]
  1573. prfm pstl1strm,[$out,#4096+64*6]
  1574. prfm pstl1strm,[$out,#4096+64*7]
  1575. strb w3,[$out,#64*0]
  1576. lsr x3,x3,#8
  1577. strb w3,[$out,#64*1]
  1578. lsr x3,x3,#8
  1579. strb w3,[$out,#64*2]
  1580. lsr x3,x3,#8
  1581. strb w3,[$out,#64*3]
  1582. lsr x3,x3,#8
  1583. strb w3,[$out,#64*4]
  1584. lsr x3,x3,#8
  1585. strb w3,[$out,#64*5]
  1586. lsr x3,x3,#8
  1587. strb w3,[$out,#64*6]
  1588. lsr x3,x3,#8
  1589. strb w3,[$out,#64*7]
  1590. add $out,$out,#64*8
  1591. b.ne .Loop_scatter_w7
  1592. ldr x29,[sp],#16
  1593. ret
  1594. .size ecp_nistz256_scatter_w7,.-ecp_nistz256_scatter_w7
  1595. // void ecp_nistz256_gather_w7(P256_POINT_AFFINE *x0,const void *x1,
  1596. // int x2);
  1597. .globl ecp_nistz256_gather_w7
  1598. .type ecp_nistz256_gather_w7,%function
  1599. .align 4
  1600. ecp_nistz256_gather_w7:
  1601. stp x29,x30,[sp,#-16]!
  1602. add x29,sp,#0
  1603. cmp $index,xzr
  1604. csetm x3,ne
  1605. add $index,$index,x3
  1606. add $inp,$inp,$index
  1607. mov $index,#64/8
  1608. nop
  1609. .Loop_gather_w7:
  1610. ldrb w4,[$inp,#64*0]
  1611. prfm pldl1strm,[$inp,#4096+64*0]
  1612. subs $index,$index,#1
  1613. ldrb w5,[$inp,#64*1]
  1614. prfm pldl1strm,[$inp,#4096+64*1]
  1615. ldrb w6,[$inp,#64*2]
  1616. prfm pldl1strm,[$inp,#4096+64*2]
  1617. ldrb w7,[$inp,#64*3]
  1618. prfm pldl1strm,[$inp,#4096+64*3]
  1619. ldrb w8,[$inp,#64*4]
  1620. prfm pldl1strm,[$inp,#4096+64*4]
  1621. ldrb w9,[$inp,#64*5]
  1622. prfm pldl1strm,[$inp,#4096+64*5]
  1623. ldrb w10,[$inp,#64*6]
  1624. prfm pldl1strm,[$inp,#4096+64*6]
  1625. ldrb w11,[$inp,#64*7]
  1626. prfm pldl1strm,[$inp,#4096+64*7]
  1627. add $inp,$inp,#64*8
  1628. orr x4,x4,x5,lsl#8
  1629. orr x6,x6,x7,lsl#8
  1630. orr x8,x8,x9,lsl#8
  1631. orr x4,x4,x6,lsl#16
  1632. orr x10,x10,x11,lsl#8
  1633. orr x4,x4,x8,lsl#32
  1634. orr x4,x4,x10,lsl#48
  1635. and x4,x4,x3
  1636. str x4,[$out],#8
  1637. b.ne .Loop_gather_w7
  1638. ldr x29,[sp],#16
  1639. ret
  1640. .size ecp_nistz256_gather_w7,.-ecp_nistz256_gather_w7
  1641. ___
  1642. }
  1643. foreach (split("\n",$code)) {
  1644. s/\`([^\`]*)\`/eval $1/ge;
  1645. print $_,"\n";
  1646. }
  1647. close STDOUT or die "error closing STDOUT: $!"; # enforce flush