ecp_nistz256-sparcv9.pl 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (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 SPARCv9.
  16. #
  17. # February 2015.
  18. #
  19. # Original ECP_NISTZ256 submission targeting x86_64 is detailed in
  20. # http://eprint.iacr.org/2013/816. In the process of adaptation
  21. # original .c module was made 32-bit savvy in order to make this
  22. # implementation possible.
  23. #
  24. # with/without -DECP_NISTZ256_ASM
  25. # UltraSPARC III +12-18%
  26. # SPARC T4 +99-550% (+66-150% on 32-bit Solaris)
  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 +200% means 3x improvement.
  31. $output = pop and open STDOUT,">$output";
  32. $code.=<<___;
  33. #include "sparc_arch.h"
  34. #define LOCALS (STACK_BIAS+STACK_FRAME)
  35. #ifdef __arch64__
  36. .register %g2,#scratch
  37. .register %g3,#scratch
  38. # define STACK64_FRAME STACK_FRAME
  39. # define LOCALS64 LOCALS
  40. #else
  41. # define STACK64_FRAME (2047+192)
  42. # define LOCALS64 STACK64_FRAME
  43. #endif
  44. .section ".text",#alloc,#execinstr
  45. ___
  46. ########################################################################
  47. # Convert ecp_nistz256_table.c to layout expected by ecp_nistz_gather_w7
  48. #
  49. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  50. open TABLE,"<ecp_nistz256_table.c" or
  51. open TABLE,"<${dir}../ecp_nistz256_table.c" or
  52. die "failed to open ecp_nistz256_table.c:",$!;
  53. use integer;
  54. foreach(<TABLE>) {
  55. s/TOBN\(\s*(0x[0-9a-f]+),\s*(0x[0-9a-f]+)\s*\)/push @arr,hex($2),hex($1)/geo;
  56. }
  57. close TABLE;
  58. # See ecp_nistz256_table.c for explanation for why it's 64*16*37.
  59. # 64*16*37-1 is because $#arr returns last valid index or @arr, not
  60. # amount of elements.
  61. die "insane number of elements" if ($#arr != 64*16*37-1);
  62. $code.=<<___;
  63. .globl ecp_nistz256_precomputed
  64. .align 4096
  65. ecp_nistz256_precomputed:
  66. ___
  67. ########################################################################
  68. # this conversion smashes P256_POINT_AFFINE by individual bytes with
  69. # 64 byte interval, similar to
  70. # 1111222233334444
  71. # 1234123412341234
  72. for(1..37) {
  73. @tbl = splice(@arr,0,64*16);
  74. for($i=0;$i<64;$i++) {
  75. undef @line;
  76. for($j=0;$j<64;$j++) {
  77. push @line,(@tbl[$j*16+$i/4]>>(($i%4)*8))&0xff;
  78. }
  79. $code.=".byte\t";
  80. $code.=join(',',map { sprintf "0x%02x",$_} @line);
  81. $code.="\n";
  82. }
  83. }
  84. {{{
  85. my ($rp,$ap,$bp)=map("%i$_",(0..2));
  86. my @acc=map("%l$_",(0..7));
  87. my ($t0,$t1,$t2,$t3,$t4,$t5,$t6,$t7)=(map("%o$_",(0..5)),"%g4","%g5");
  88. my ($bi,$a0,$mask,$carry)=(map("%i$_",(3..5)),"%g1");
  89. my ($rp_real,$ap_real)=("%g2","%g3");
  90. $code.=<<___;
  91. .type ecp_nistz256_precomputed,#object
  92. .size ecp_nistz256_precomputed,.-ecp_nistz256_precomputed
  93. .align 64
  94. .LRR: ! 2^512 mod P precomputed for NIST P256 polynomial
  95. .long 0x00000003, 0x00000000, 0xffffffff, 0xfffffffb
  96. .long 0xfffffffe, 0xffffffff, 0xfffffffd, 0x00000004
  97. .Lone:
  98. .long 1,0,0,0,0,0,0,0
  99. .asciz "ECP_NISTZ256 for SPARCv9, CRYPTOGAMS by <appro\@openssl.org>"
  100. ! void ecp_nistz256_to_mont(BN_ULONG %i0[8],const BN_ULONG %i1[8]);
  101. .globl ecp_nistz256_to_mont
  102. .align 64
  103. ecp_nistz256_to_mont:
  104. save %sp,-STACK_FRAME,%sp
  105. nop
  106. 1: call .+8
  107. add %o7,.LRR-1b,$bp
  108. call __ecp_nistz256_mul_mont
  109. nop
  110. ret
  111. restore
  112. .type ecp_nistz256_to_mont,#function
  113. .size ecp_nistz256_to_mont,.-ecp_nistz256_to_mont
  114. ! void ecp_nistz256_from_mont(BN_ULONG %i0[8],const BN_ULONG %i1[8]);
  115. .globl ecp_nistz256_from_mont
  116. .align 32
  117. ecp_nistz256_from_mont:
  118. save %sp,-STACK_FRAME,%sp
  119. nop
  120. 1: call .+8
  121. add %o7,.Lone-1b,$bp
  122. call __ecp_nistz256_mul_mont
  123. nop
  124. ret
  125. restore
  126. .type ecp_nistz256_from_mont,#function
  127. .size ecp_nistz256_from_mont,.-ecp_nistz256_from_mont
  128. ! void ecp_nistz256_mul_mont(BN_ULONG %i0[8],const BN_ULONG %i1[8],
  129. ! const BN_ULONG %i2[8]);
  130. .globl ecp_nistz256_mul_mont
  131. .align 32
  132. ecp_nistz256_mul_mont:
  133. save %sp,-STACK_FRAME,%sp
  134. nop
  135. call __ecp_nistz256_mul_mont
  136. nop
  137. ret
  138. restore
  139. .type ecp_nistz256_mul_mont,#function
  140. .size ecp_nistz256_mul_mont,.-ecp_nistz256_mul_mont
  141. ! void ecp_nistz256_sqr_mont(BN_ULONG %i0[8],const BN_ULONG %i2[8]);
  142. .globl ecp_nistz256_sqr_mont
  143. .align 32
  144. ecp_nistz256_sqr_mont:
  145. save %sp,-STACK_FRAME,%sp
  146. mov $ap,$bp
  147. call __ecp_nistz256_mul_mont
  148. nop
  149. ret
  150. restore
  151. .type ecp_nistz256_sqr_mont,#function
  152. .size ecp_nistz256_sqr_mont,.-ecp_nistz256_sqr_mont
  153. ___
  154. ########################################################################
  155. # Special thing to keep in mind is that $t0-$t7 hold 64-bit values,
  156. # while all others are meant to keep 32. "Meant to" means that additions
  157. # to @acc[0-7] do "contaminate" upper bits, but they are cleared before
  158. # they can affect outcome (follow 'and' with $mask). Also keep in mind
  159. # that addition with carry is addition with 32-bit carry, even though
  160. # CPU is 64-bit. [Addition with 64-bit carry was introduced in T3, see
  161. # below for VIS3 code paths.]
  162. $code.=<<___;
  163. .align 32
  164. __ecp_nistz256_mul_mont:
  165. ld [$bp+0],$bi ! b[0]
  166. mov -1,$mask
  167. ld [$ap+0],$a0
  168. srl $mask,0,$mask ! 0xffffffff
  169. ld [$ap+4],$t1
  170. ld [$ap+8],$t2
  171. ld [$ap+12],$t3
  172. ld [$ap+16],$t4
  173. ld [$ap+20],$t5
  174. ld [$ap+24],$t6
  175. ld [$ap+28],$t7
  176. mulx $a0,$bi,$t0 ! a[0-7]*b[0], 64-bit results
  177. mulx $t1,$bi,$t1
  178. mulx $t2,$bi,$t2
  179. mulx $t3,$bi,$t3
  180. mulx $t4,$bi,$t4
  181. mulx $t5,$bi,$t5
  182. mulx $t6,$bi,$t6
  183. mulx $t7,$bi,$t7
  184. srlx $t0,32,@acc[1] ! extract high parts
  185. srlx $t1,32,@acc[2]
  186. srlx $t2,32,@acc[3]
  187. srlx $t3,32,@acc[4]
  188. srlx $t4,32,@acc[5]
  189. srlx $t5,32,@acc[6]
  190. srlx $t6,32,@acc[7]
  191. srlx $t7,32,@acc[0] ! "@acc[8]"
  192. mov 0,$carry
  193. ___
  194. for($i=1;$i<8;$i++) {
  195. $code.=<<___;
  196. addcc @acc[1],$t1,@acc[1] ! accumulate high parts
  197. ld [$bp+4*$i],$bi ! b[$i]
  198. ld [$ap+4],$t1 ! re-load a[1-7]
  199. addccc @acc[2],$t2,@acc[2]
  200. addccc @acc[3],$t3,@acc[3]
  201. ld [$ap+8],$t2
  202. ld [$ap+12],$t3
  203. addccc @acc[4],$t4,@acc[4]
  204. addccc @acc[5],$t5,@acc[5]
  205. ld [$ap+16],$t4
  206. ld [$ap+20],$t5
  207. addccc @acc[6],$t6,@acc[6]
  208. addccc @acc[7],$t7,@acc[7]
  209. ld [$ap+24],$t6
  210. ld [$ap+28],$t7
  211. addccc @acc[0],$carry,@acc[0] ! "@acc[8]"
  212. addc %g0,%g0,$carry
  213. ___
  214. # Reduction iteration is normally performed by accumulating
  215. # result of multiplication of modulus by "magic" digit [and
  216. # omitting least significant word, which is guaranteed to
  217. # be 0], but thanks to special form of modulus and "magic"
  218. # digit being equal to least significant word, it can be
  219. # performed with additions and subtractions alone. Indeed:
  220. #
  221. # ffff.0001.0000.0000.0000.ffff.ffff.ffff
  222. # * abcd
  223. # + xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.abcd
  224. #
  225. # Now observing that ff..ff*x = (2^n-1)*x = 2^n*x-x, we
  226. # rewrite above as:
  227. #
  228. # xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.abcd
  229. # + abcd.0000.abcd.0000.0000.abcd.0000.0000.0000
  230. # - abcd.0000.0000.0000.0000.0000.0000.abcd
  231. #
  232. # or marking redundant operations:
  233. #
  234. # xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.----
  235. # + abcd.0000.abcd.0000.0000.abcd.----.----.----
  236. # - abcd.----.----.----.----.----.----.----
  237. $code.=<<___;
  238. ! multiplication-less reduction
  239. addcc @acc[3],$t0,@acc[3] ! r[3]+=r[0]
  240. addccc @acc[4],%g0,@acc[4] ! r[4]+=0
  241. and @acc[1],$mask,@acc[1]
  242. and @acc[2],$mask,@acc[2]
  243. addccc @acc[5],%g0,@acc[5] ! r[5]+=0
  244. addccc @acc[6],$t0,@acc[6] ! r[6]+=r[0]
  245. and @acc[3],$mask,@acc[3]
  246. and @acc[4],$mask,@acc[4]
  247. addccc @acc[7],%g0,@acc[7] ! r[7]+=0
  248. addccc @acc[0],$t0,@acc[0] ! r[8]+=r[0] "@acc[8]"
  249. and @acc[5],$mask,@acc[5]
  250. and @acc[6],$mask,@acc[6]
  251. addc $carry,%g0,$carry ! top-most carry
  252. subcc @acc[7],$t0,@acc[7] ! r[7]-=r[0]
  253. subccc @acc[0],%g0,@acc[0] ! r[8]-=0 "@acc[8]"
  254. subc $carry,%g0,$carry ! top-most carry
  255. and @acc[7],$mask,@acc[7]
  256. and @acc[0],$mask,@acc[0] ! "@acc[8]"
  257. ___
  258. push(@acc,shift(@acc)); # rotate registers to "omit" acc[0]
  259. $code.=<<___;
  260. mulx $a0,$bi,$t0 ! a[0-7]*b[$i], 64-bit results
  261. mulx $t1,$bi,$t1
  262. mulx $t2,$bi,$t2
  263. mulx $t3,$bi,$t3
  264. mulx $t4,$bi,$t4
  265. mulx $t5,$bi,$t5
  266. mulx $t6,$bi,$t6
  267. mulx $t7,$bi,$t7
  268. add @acc[0],$t0,$t0 ! accumulate low parts, can't overflow
  269. add @acc[1],$t1,$t1
  270. srlx $t0,32,@acc[1] ! extract high parts
  271. add @acc[2],$t2,$t2
  272. srlx $t1,32,@acc[2]
  273. add @acc[3],$t3,$t3
  274. srlx $t2,32,@acc[3]
  275. add @acc[4],$t4,$t4
  276. srlx $t3,32,@acc[4]
  277. add @acc[5],$t5,$t5
  278. srlx $t4,32,@acc[5]
  279. add @acc[6],$t6,$t6
  280. srlx $t5,32,@acc[6]
  281. add @acc[7],$t7,$t7
  282. srlx $t6,32,@acc[7]
  283. srlx $t7,32,@acc[0] ! "@acc[8]"
  284. ___
  285. }
  286. $code.=<<___;
  287. addcc @acc[1],$t1,@acc[1] ! accumulate high parts
  288. addccc @acc[2],$t2,@acc[2]
  289. addccc @acc[3],$t3,@acc[3]
  290. addccc @acc[4],$t4,@acc[4]
  291. addccc @acc[5],$t5,@acc[5]
  292. addccc @acc[6],$t6,@acc[6]
  293. addccc @acc[7],$t7,@acc[7]
  294. addccc @acc[0],$carry,@acc[0] ! "@acc[8]"
  295. addc %g0,%g0,$carry
  296. addcc @acc[3],$t0,@acc[3] ! multiplication-less reduction
  297. addccc @acc[4],%g0,@acc[4]
  298. addccc @acc[5],%g0,@acc[5]
  299. addccc @acc[6],$t0,@acc[6]
  300. addccc @acc[7],%g0,@acc[7]
  301. addccc @acc[0],$t0,@acc[0] ! "@acc[8]"
  302. addc $carry,%g0,$carry
  303. subcc @acc[7],$t0,@acc[7]
  304. subccc @acc[0],%g0,@acc[0] ! "@acc[8]"
  305. subc $carry,%g0,$carry ! top-most carry
  306. ___
  307. push(@acc,shift(@acc)); # rotate registers to omit acc[0]
  308. $code.=<<___;
  309. ! Final step is "if result > mod, subtract mod", but we do it
  310. ! "other way around", namely subtract modulus from result
  311. ! and if it borrowed, add modulus back.
  312. subcc @acc[0],-1,@acc[0] ! subtract modulus
  313. subccc @acc[1],-1,@acc[1]
  314. subccc @acc[2],-1,@acc[2]
  315. subccc @acc[3],0,@acc[3]
  316. subccc @acc[4],0,@acc[4]
  317. subccc @acc[5],0,@acc[5]
  318. subccc @acc[6],1,@acc[6]
  319. subccc @acc[7],-1,@acc[7]
  320. subc $carry,0,$carry ! broadcast borrow bit
  321. ! Note that because mod has special form, i.e. consists of
  322. ! 0xffffffff, 1 and 0s, we can conditionally synthesize it by
  323. ! using value of broadcasted borrow and the borrow bit itself.
  324. ! To minimize dependency chain we first broadcast and then
  325. ! extract the bit by negating (follow $bi).
  326. addcc @acc[0],$carry,@acc[0] ! add modulus or zero
  327. addccc @acc[1],$carry,@acc[1]
  328. neg $carry,$bi
  329. st @acc[0],[$rp]
  330. addccc @acc[2],$carry,@acc[2]
  331. st @acc[1],[$rp+4]
  332. addccc @acc[3],0,@acc[3]
  333. st @acc[2],[$rp+8]
  334. addccc @acc[4],0,@acc[4]
  335. st @acc[3],[$rp+12]
  336. addccc @acc[5],0,@acc[5]
  337. st @acc[4],[$rp+16]
  338. addccc @acc[6],$bi,@acc[6]
  339. st @acc[5],[$rp+20]
  340. addc @acc[7],$carry,@acc[7]
  341. st @acc[6],[$rp+24]
  342. retl
  343. st @acc[7],[$rp+28]
  344. .type __ecp_nistz256_mul_mont,#function
  345. .size __ecp_nistz256_mul_mont,.-__ecp_nistz256_mul_mont
  346. ! void ecp_nistz256_add(BN_ULONG %i0[8],const BN_ULONG %i1[8],
  347. ! const BN_ULONG %i2[8]);
  348. .globl ecp_nistz256_add
  349. .align 32
  350. ecp_nistz256_add:
  351. save %sp,-STACK_FRAME,%sp
  352. ld [$ap],@acc[0]
  353. ld [$ap+4],@acc[1]
  354. ld [$ap+8],@acc[2]
  355. ld [$ap+12],@acc[3]
  356. ld [$ap+16],@acc[4]
  357. ld [$ap+20],@acc[5]
  358. ld [$ap+24],@acc[6]
  359. call __ecp_nistz256_add
  360. ld [$ap+28],@acc[7]
  361. ret
  362. restore
  363. .type ecp_nistz256_add,#function
  364. .size ecp_nistz256_add,.-ecp_nistz256_add
  365. .align 32
  366. __ecp_nistz256_add:
  367. ld [$bp+0],$t0 ! b[0]
  368. ld [$bp+4],$t1
  369. ld [$bp+8],$t2
  370. ld [$bp+12],$t3
  371. addcc @acc[0],$t0,@acc[0]
  372. ld [$bp+16],$t4
  373. ld [$bp+20],$t5
  374. addccc @acc[1],$t1,@acc[1]
  375. ld [$bp+24],$t6
  376. ld [$bp+28],$t7
  377. addccc @acc[2],$t2,@acc[2]
  378. addccc @acc[3],$t3,@acc[3]
  379. addccc @acc[4],$t4,@acc[4]
  380. addccc @acc[5],$t5,@acc[5]
  381. addccc @acc[6],$t6,@acc[6]
  382. addccc @acc[7],$t7,@acc[7]
  383. addc %g0,%g0,$carry
  384. .Lreduce_by_sub:
  385. ! if a+b >= modulus, subtract modulus.
  386. !
  387. ! But since comparison implies subtraction, we subtract
  388. ! modulus and then add it back if subtraction borrowed.
  389. subcc @acc[0],-1,@acc[0]
  390. subccc @acc[1],-1,@acc[1]
  391. subccc @acc[2],-1,@acc[2]
  392. subccc @acc[3], 0,@acc[3]
  393. subccc @acc[4], 0,@acc[4]
  394. subccc @acc[5], 0,@acc[5]
  395. subccc @acc[6], 1,@acc[6]
  396. subccc @acc[7],-1,@acc[7]
  397. subc $carry,0,$carry
  398. ! Note that because mod has special form, i.e. consists of
  399. ! 0xffffffff, 1 and 0s, we can conditionally synthesize it by
  400. ! using value of borrow and its negative.
  401. addcc @acc[0],$carry,@acc[0] ! add synthesized modulus
  402. addccc @acc[1],$carry,@acc[1]
  403. neg $carry,$bi
  404. st @acc[0],[$rp]
  405. addccc @acc[2],$carry,@acc[2]
  406. st @acc[1],[$rp+4]
  407. addccc @acc[3],0,@acc[3]
  408. st @acc[2],[$rp+8]
  409. addccc @acc[4],0,@acc[4]
  410. st @acc[3],[$rp+12]
  411. addccc @acc[5],0,@acc[5]
  412. st @acc[4],[$rp+16]
  413. addccc @acc[6],$bi,@acc[6]
  414. st @acc[5],[$rp+20]
  415. addc @acc[7],$carry,@acc[7]
  416. st @acc[6],[$rp+24]
  417. retl
  418. st @acc[7],[$rp+28]
  419. .type __ecp_nistz256_add,#function
  420. .size __ecp_nistz256_add,.-__ecp_nistz256_add
  421. ! void ecp_nistz256_mul_by_2(BN_ULONG %i0[8],const BN_ULONG %i1[8]);
  422. .globl ecp_nistz256_mul_by_2
  423. .align 32
  424. ecp_nistz256_mul_by_2:
  425. save %sp,-STACK_FRAME,%sp
  426. ld [$ap],@acc[0]
  427. ld [$ap+4],@acc[1]
  428. ld [$ap+8],@acc[2]
  429. ld [$ap+12],@acc[3]
  430. ld [$ap+16],@acc[4]
  431. ld [$ap+20],@acc[5]
  432. ld [$ap+24],@acc[6]
  433. call __ecp_nistz256_mul_by_2
  434. ld [$ap+28],@acc[7]
  435. ret
  436. restore
  437. .type ecp_nistz256_mul_by_2,#function
  438. .size ecp_nistz256_mul_by_2,.-ecp_nistz256_mul_by_2
  439. .align 32
  440. __ecp_nistz256_mul_by_2:
  441. addcc @acc[0],@acc[0],@acc[0] ! a+a=2*a
  442. addccc @acc[1],@acc[1],@acc[1]
  443. addccc @acc[2],@acc[2],@acc[2]
  444. addccc @acc[3],@acc[3],@acc[3]
  445. addccc @acc[4],@acc[4],@acc[4]
  446. addccc @acc[5],@acc[5],@acc[5]
  447. addccc @acc[6],@acc[6],@acc[6]
  448. addccc @acc[7],@acc[7],@acc[7]
  449. b .Lreduce_by_sub
  450. addc %g0,%g0,$carry
  451. .type __ecp_nistz256_mul_by_2,#function
  452. .size __ecp_nistz256_mul_by_2,.-__ecp_nistz256_mul_by_2
  453. ! void ecp_nistz256_mul_by_3(BN_ULONG %i0[8],const BN_ULONG %i1[8]);
  454. .globl ecp_nistz256_mul_by_3
  455. .align 32
  456. ecp_nistz256_mul_by_3:
  457. save %sp,-STACK_FRAME,%sp
  458. ld [$ap],@acc[0]
  459. ld [$ap+4],@acc[1]
  460. ld [$ap+8],@acc[2]
  461. ld [$ap+12],@acc[3]
  462. ld [$ap+16],@acc[4]
  463. ld [$ap+20],@acc[5]
  464. ld [$ap+24],@acc[6]
  465. call __ecp_nistz256_mul_by_3
  466. ld [$ap+28],@acc[7]
  467. ret
  468. restore
  469. .type ecp_nistz256_mul_by_3,#function
  470. .size ecp_nistz256_mul_by_3,.-ecp_nistz256_mul_by_3
  471. .align 32
  472. __ecp_nistz256_mul_by_3:
  473. addcc @acc[0],@acc[0],$t0 ! a+a=2*a
  474. addccc @acc[1],@acc[1],$t1
  475. addccc @acc[2],@acc[2],$t2
  476. addccc @acc[3],@acc[3],$t3
  477. addccc @acc[4],@acc[4],$t4
  478. addccc @acc[5],@acc[5],$t5
  479. addccc @acc[6],@acc[6],$t6
  480. addccc @acc[7],@acc[7],$t7
  481. addc %g0,%g0,$carry
  482. subcc $t0,-1,$t0 ! .Lreduce_by_sub but without stores
  483. subccc $t1,-1,$t1
  484. subccc $t2,-1,$t2
  485. subccc $t3, 0,$t3
  486. subccc $t4, 0,$t4
  487. subccc $t5, 0,$t5
  488. subccc $t6, 1,$t6
  489. subccc $t7,-1,$t7
  490. subc $carry,0,$carry
  491. addcc $t0,$carry,$t0 ! add synthesized modulus
  492. addccc $t1,$carry,$t1
  493. neg $carry,$bi
  494. addccc $t2,$carry,$t2
  495. addccc $t3,0,$t3
  496. addccc $t4,0,$t4
  497. addccc $t5,0,$t5
  498. addccc $t6,$bi,$t6
  499. addc $t7,$carry,$t7
  500. addcc $t0,@acc[0],@acc[0] ! 2*a+a=3*a
  501. addccc $t1,@acc[1],@acc[1]
  502. addccc $t2,@acc[2],@acc[2]
  503. addccc $t3,@acc[3],@acc[3]
  504. addccc $t4,@acc[4],@acc[4]
  505. addccc $t5,@acc[5],@acc[5]
  506. addccc $t6,@acc[6],@acc[6]
  507. addccc $t7,@acc[7],@acc[7]
  508. b .Lreduce_by_sub
  509. addc %g0,%g0,$carry
  510. .type __ecp_nistz256_mul_by_3,#function
  511. .size __ecp_nistz256_mul_by_3,.-__ecp_nistz256_mul_by_3
  512. ! void ecp_nistz256_sub(BN_ULONG %i0[8],const BN_ULONG %i1[8],
  513. ! const BN_ULONG %i2[8]);
  514. .globl ecp_nistz256_sub
  515. .align 32
  516. ecp_nistz256_sub:
  517. save %sp,-STACK_FRAME,%sp
  518. ld [$ap],@acc[0]
  519. ld [$ap+4],@acc[1]
  520. ld [$ap+8],@acc[2]
  521. ld [$ap+12],@acc[3]
  522. ld [$ap+16],@acc[4]
  523. ld [$ap+20],@acc[5]
  524. ld [$ap+24],@acc[6]
  525. call __ecp_nistz256_sub_from
  526. ld [$ap+28],@acc[7]
  527. ret
  528. restore
  529. .type ecp_nistz256_sub,#function
  530. .size ecp_nistz256_sub,.-ecp_nistz256_sub
  531. ! void ecp_nistz256_neg(BN_ULONG %i0[8],const BN_ULONG %i1[8]);
  532. .globl ecp_nistz256_neg
  533. .align 32
  534. ecp_nistz256_neg:
  535. save %sp,-STACK_FRAME,%sp
  536. mov $ap,$bp
  537. mov 0,@acc[0]
  538. mov 0,@acc[1]
  539. mov 0,@acc[2]
  540. mov 0,@acc[3]
  541. mov 0,@acc[4]
  542. mov 0,@acc[5]
  543. mov 0,@acc[6]
  544. call __ecp_nistz256_sub_from
  545. mov 0,@acc[7]
  546. ret
  547. restore
  548. .type ecp_nistz256_neg,#function
  549. .size ecp_nistz256_neg,.-ecp_nistz256_neg
  550. .align 32
  551. __ecp_nistz256_sub_from:
  552. ld [$bp+0],$t0 ! b[0]
  553. ld [$bp+4],$t1
  554. ld [$bp+8],$t2
  555. ld [$bp+12],$t3
  556. subcc @acc[0],$t0,@acc[0]
  557. ld [$bp+16],$t4
  558. ld [$bp+20],$t5
  559. subccc @acc[1],$t1,@acc[1]
  560. subccc @acc[2],$t2,@acc[2]
  561. ld [$bp+24],$t6
  562. ld [$bp+28],$t7
  563. subccc @acc[3],$t3,@acc[3]
  564. subccc @acc[4],$t4,@acc[4]
  565. subccc @acc[5],$t5,@acc[5]
  566. subccc @acc[6],$t6,@acc[6]
  567. subccc @acc[7],$t7,@acc[7]
  568. subc %g0,%g0,$carry ! broadcast borrow bit
  569. .Lreduce_by_add:
  570. ! if a-b borrows, add modulus.
  571. !
  572. ! Note that because mod has special form, i.e. consists of
  573. ! 0xffffffff, 1 and 0s, we can conditionally synthesize it by
  574. ! using value of broadcasted borrow and the borrow bit itself.
  575. ! To minimize dependency chain we first broadcast and then
  576. ! extract the bit by negating (follow $bi).
  577. addcc @acc[0],$carry,@acc[0] ! add synthesized modulus
  578. addccc @acc[1],$carry,@acc[1]
  579. neg $carry,$bi
  580. st @acc[0],[$rp]
  581. addccc @acc[2],$carry,@acc[2]
  582. st @acc[1],[$rp+4]
  583. addccc @acc[3],0,@acc[3]
  584. st @acc[2],[$rp+8]
  585. addccc @acc[4],0,@acc[4]
  586. st @acc[3],[$rp+12]
  587. addccc @acc[5],0,@acc[5]
  588. st @acc[4],[$rp+16]
  589. addccc @acc[6],$bi,@acc[6]
  590. st @acc[5],[$rp+20]
  591. addc @acc[7],$carry,@acc[7]
  592. st @acc[6],[$rp+24]
  593. retl
  594. st @acc[7],[$rp+28]
  595. .type __ecp_nistz256_sub_from,#function
  596. .size __ecp_nistz256_sub_from,.-__ecp_nistz256_sub_from
  597. .align 32
  598. __ecp_nistz256_sub_morf:
  599. ld [$bp+0],$t0 ! b[0]
  600. ld [$bp+4],$t1
  601. ld [$bp+8],$t2
  602. ld [$bp+12],$t3
  603. subcc $t0,@acc[0],@acc[0]
  604. ld [$bp+16],$t4
  605. ld [$bp+20],$t5
  606. subccc $t1,@acc[1],@acc[1]
  607. subccc $t2,@acc[2],@acc[2]
  608. ld [$bp+24],$t6
  609. ld [$bp+28],$t7
  610. subccc $t3,@acc[3],@acc[3]
  611. subccc $t4,@acc[4],@acc[4]
  612. subccc $t5,@acc[5],@acc[5]
  613. subccc $t6,@acc[6],@acc[6]
  614. subccc $t7,@acc[7],@acc[7]
  615. b .Lreduce_by_add
  616. subc %g0,%g0,$carry ! broadcast borrow bit
  617. .type __ecp_nistz256_sub_morf,#function
  618. .size __ecp_nistz256_sub_morf,.-__ecp_nistz256_sub_morf
  619. ! void ecp_nistz256_div_by_2(BN_ULONG %i0[8],const BN_ULONG %i1[8]);
  620. .globl ecp_nistz256_div_by_2
  621. .align 32
  622. ecp_nistz256_div_by_2:
  623. save %sp,-STACK_FRAME,%sp
  624. ld [$ap],@acc[0]
  625. ld [$ap+4],@acc[1]
  626. ld [$ap+8],@acc[2]
  627. ld [$ap+12],@acc[3]
  628. ld [$ap+16],@acc[4]
  629. ld [$ap+20],@acc[5]
  630. ld [$ap+24],@acc[6]
  631. call __ecp_nistz256_div_by_2
  632. ld [$ap+28],@acc[7]
  633. ret
  634. restore
  635. .type ecp_nistz256_div_by_2,#function
  636. .size ecp_nistz256_div_by_2,.-ecp_nistz256_div_by_2
  637. .align 32
  638. __ecp_nistz256_div_by_2:
  639. ! ret = (a is odd ? a+mod : a) >> 1
  640. and @acc[0],1,$bi
  641. neg $bi,$carry
  642. addcc @acc[0],$carry,@acc[0]
  643. addccc @acc[1],$carry,@acc[1]
  644. addccc @acc[2],$carry,@acc[2]
  645. addccc @acc[3],0,@acc[3]
  646. addccc @acc[4],0,@acc[4]
  647. addccc @acc[5],0,@acc[5]
  648. addccc @acc[6],$bi,@acc[6]
  649. addccc @acc[7],$carry,@acc[7]
  650. addc %g0,%g0,$carry
  651. ! ret >>= 1
  652. srl @acc[0],1,@acc[0]
  653. sll @acc[1],31,$t0
  654. srl @acc[1],1,@acc[1]
  655. or @acc[0],$t0,@acc[0]
  656. sll @acc[2],31,$t1
  657. srl @acc[2],1,@acc[2]
  658. or @acc[1],$t1,@acc[1]
  659. sll @acc[3],31,$t2
  660. st @acc[0],[$rp]
  661. srl @acc[3],1,@acc[3]
  662. or @acc[2],$t2,@acc[2]
  663. sll @acc[4],31,$t3
  664. st @acc[1],[$rp+4]
  665. srl @acc[4],1,@acc[4]
  666. or @acc[3],$t3,@acc[3]
  667. sll @acc[5],31,$t4
  668. st @acc[2],[$rp+8]
  669. srl @acc[5],1,@acc[5]
  670. or @acc[4],$t4,@acc[4]
  671. sll @acc[6],31,$t5
  672. st @acc[3],[$rp+12]
  673. srl @acc[6],1,@acc[6]
  674. or @acc[5],$t5,@acc[5]
  675. sll @acc[7],31,$t6
  676. st @acc[4],[$rp+16]
  677. srl @acc[7],1,@acc[7]
  678. or @acc[6],$t6,@acc[6]
  679. sll $carry,31,$t7
  680. st @acc[5],[$rp+20]
  681. or @acc[7],$t7,@acc[7]
  682. st @acc[6],[$rp+24]
  683. retl
  684. st @acc[7],[$rp+28]
  685. .type __ecp_nistz256_div_by_2,#function
  686. .size __ecp_nistz256_div_by_2,.-__ecp_nistz256_div_by_2
  687. ___
  688. ########################################################################
  689. # following subroutines are "literal" implementation of those found in
  690. # ecp_nistz256.c
  691. #
  692. ########################################################################
  693. # void ecp_nistz256_point_double(P256_POINT *out,const P256_POINT *inp);
  694. #
  695. {
  696. my ($S,$M,$Zsqr,$tmp0)=map(32*$_,(0..3));
  697. # above map() describes stack layout with 4 temporary
  698. # 256-bit vectors on top.
  699. $code.=<<___;
  700. #ifdef __PIC__
  701. SPARC_PIC_THUNK(%g1)
  702. #endif
  703. .globl ecp_nistz256_point_double
  704. .align 32
  705. ecp_nistz256_point_double:
  706. SPARC_LOAD_ADDRESS_LEAF(OPENSSL_sparcv9cap_P,%g1,%g5)
  707. ld [%g1],%g1 ! OPENSSL_sparcv9cap_P[0]
  708. and %g1,(SPARCV9_VIS3|SPARCV9_64BIT_STACK),%g1
  709. cmp %g1,(SPARCV9_VIS3|SPARCV9_64BIT_STACK)
  710. be ecp_nistz256_point_double_vis3
  711. nop
  712. save %sp,-STACK_FRAME-32*4,%sp
  713. mov $rp,$rp_real
  714. mov $ap,$ap_real
  715. .Lpoint_double_shortcut:
  716. ld [$ap+32],@acc[0]
  717. ld [$ap+32+4],@acc[1]
  718. ld [$ap+32+8],@acc[2]
  719. ld [$ap+32+12],@acc[3]
  720. ld [$ap+32+16],@acc[4]
  721. ld [$ap+32+20],@acc[5]
  722. ld [$ap+32+24],@acc[6]
  723. ld [$ap+32+28],@acc[7]
  724. call __ecp_nistz256_mul_by_2 ! p256_mul_by_2(S, in_y);
  725. add %sp,LOCALS+$S,$rp
  726. add $ap_real,64,$bp
  727. add $ap_real,64,$ap
  728. call __ecp_nistz256_mul_mont ! p256_sqr_mont(Zsqr, in_z);
  729. add %sp,LOCALS+$Zsqr,$rp
  730. add $ap_real,0,$bp
  731. call __ecp_nistz256_add ! p256_add(M, Zsqr, in_x);
  732. add %sp,LOCALS+$M,$rp
  733. add %sp,LOCALS+$S,$bp
  734. add %sp,LOCALS+$S,$ap
  735. call __ecp_nistz256_mul_mont ! p256_sqr_mont(S, S);
  736. add %sp,LOCALS+$S,$rp
  737. ld [$ap_real],@acc[0]
  738. add %sp,LOCALS+$Zsqr,$bp
  739. ld [$ap_real+4],@acc[1]
  740. ld [$ap_real+8],@acc[2]
  741. ld [$ap_real+12],@acc[3]
  742. ld [$ap_real+16],@acc[4]
  743. ld [$ap_real+20],@acc[5]
  744. ld [$ap_real+24],@acc[6]
  745. ld [$ap_real+28],@acc[7]
  746. call __ecp_nistz256_sub_from ! p256_sub(Zsqr, in_x, Zsqr);
  747. add %sp,LOCALS+$Zsqr,$rp
  748. add $ap_real,32,$bp
  749. add $ap_real,64,$ap
  750. call __ecp_nistz256_mul_mont ! p256_mul_mont(tmp0, in_z, in_y);
  751. add %sp,LOCALS+$tmp0,$rp
  752. call __ecp_nistz256_mul_by_2 ! p256_mul_by_2(res_z, tmp0);
  753. add $rp_real,64,$rp
  754. add %sp,LOCALS+$Zsqr,$bp
  755. add %sp,LOCALS+$M,$ap
  756. call __ecp_nistz256_mul_mont ! p256_mul_mont(M, M, Zsqr);
  757. add %sp,LOCALS+$M,$rp
  758. call __ecp_nistz256_mul_by_3 ! p256_mul_by_3(M, M);
  759. add %sp,LOCALS+$M,$rp
  760. add %sp,LOCALS+$S,$bp
  761. add %sp,LOCALS+$S,$ap
  762. call __ecp_nistz256_mul_mont ! p256_sqr_mont(tmp0, S);
  763. add %sp,LOCALS+$tmp0,$rp
  764. call __ecp_nistz256_div_by_2 ! p256_div_by_2(res_y, tmp0);
  765. add $rp_real,32,$rp
  766. add $ap_real,0,$bp
  767. add %sp,LOCALS+$S,$ap
  768. call __ecp_nistz256_mul_mont ! p256_mul_mont(S, S, in_x);
  769. add %sp,LOCALS+$S,$rp
  770. call __ecp_nistz256_mul_by_2 ! p256_mul_by_2(tmp0, S);
  771. add %sp,LOCALS+$tmp0,$rp
  772. add %sp,LOCALS+$M,$bp
  773. add %sp,LOCALS+$M,$ap
  774. call __ecp_nistz256_mul_mont ! p256_sqr_mont(res_x, M);
  775. add $rp_real,0,$rp
  776. add %sp,LOCALS+$tmp0,$bp
  777. call __ecp_nistz256_sub_from ! p256_sub(res_x, res_x, tmp0);
  778. add $rp_real,0,$rp
  779. add %sp,LOCALS+$S,$bp
  780. call __ecp_nistz256_sub_morf ! p256_sub(S, S, res_x);
  781. add %sp,LOCALS+$S,$rp
  782. add %sp,LOCALS+$M,$bp
  783. add %sp,LOCALS+$S,$ap
  784. call __ecp_nistz256_mul_mont ! p256_mul_mont(S, S, M);
  785. add %sp,LOCALS+$S,$rp
  786. add $rp_real,32,$bp
  787. call __ecp_nistz256_sub_from ! p256_sub(res_y, S, res_y);
  788. add $rp_real,32,$rp
  789. ret
  790. restore
  791. .type ecp_nistz256_point_double,#function
  792. .size ecp_nistz256_point_double,.-ecp_nistz256_point_double
  793. ___
  794. }
  795. ########################################################################
  796. # void ecp_nistz256_point_add(P256_POINT *out,const P256_POINT *in1,
  797. # const P256_POINT *in2);
  798. {
  799. my ($res_x,$res_y,$res_z,
  800. $H,$Hsqr,$R,$Rsqr,$Hcub,
  801. $U1,$U2,$S1,$S2)=map(32*$_,(0..11));
  802. my ($Z1sqr, $Z2sqr) = ($Hsqr, $Rsqr);
  803. # above map() describes stack layout with 12 temporary
  804. # 256-bit vectors on top. Then we reserve some space for
  805. # !in1infty, !in2infty, result of check for zero and return pointer.
  806. my $bp_real=$rp_real;
  807. $code.=<<___;
  808. .globl ecp_nistz256_point_add
  809. .align 32
  810. ecp_nistz256_point_add:
  811. SPARC_LOAD_ADDRESS_LEAF(OPENSSL_sparcv9cap_P,%g1,%g5)
  812. ld [%g1],%g1 ! OPENSSL_sparcv9cap_P[0]
  813. and %g1,(SPARCV9_VIS3|SPARCV9_64BIT_STACK),%g1
  814. cmp %g1,(SPARCV9_VIS3|SPARCV9_64BIT_STACK)
  815. be ecp_nistz256_point_add_vis3
  816. nop
  817. save %sp,-STACK_FRAME-32*12-32,%sp
  818. stx $rp,[%fp+STACK_BIAS-8] ! off-load $rp
  819. mov $ap,$ap_real
  820. mov $bp,$bp_real
  821. ld [$bp+64],$t0 ! in2_z
  822. ld [$bp+64+4],$t1
  823. ld [$bp+64+8],$t2
  824. ld [$bp+64+12],$t3
  825. ld [$bp+64+16],$t4
  826. ld [$bp+64+20],$t5
  827. ld [$bp+64+24],$t6
  828. ld [$bp+64+28],$t7
  829. or $t1,$t0,$t0
  830. or $t3,$t2,$t2
  831. or $t5,$t4,$t4
  832. or $t7,$t6,$t6
  833. or $t2,$t0,$t0
  834. or $t6,$t4,$t4
  835. or $t4,$t0,$t0 ! !in2infty
  836. movrnz $t0,-1,$t0
  837. st $t0,[%fp+STACK_BIAS-12]
  838. ld [$ap+64],$t0 ! in1_z
  839. ld [$ap+64+4],$t1
  840. ld [$ap+64+8],$t2
  841. ld [$ap+64+12],$t3
  842. ld [$ap+64+16],$t4
  843. ld [$ap+64+20],$t5
  844. ld [$ap+64+24],$t6
  845. ld [$ap+64+28],$t7
  846. or $t1,$t0,$t0
  847. or $t3,$t2,$t2
  848. or $t5,$t4,$t4
  849. or $t7,$t6,$t6
  850. or $t2,$t0,$t0
  851. or $t6,$t4,$t4
  852. or $t4,$t0,$t0 ! !in1infty
  853. movrnz $t0,-1,$t0
  854. st $t0,[%fp+STACK_BIAS-16]
  855. add $bp_real,64,$bp
  856. add $bp_real,64,$ap
  857. call __ecp_nistz256_mul_mont ! p256_sqr_mont(Z2sqr, in2_z);
  858. add %sp,LOCALS+$Z2sqr,$rp
  859. add $ap_real,64,$bp
  860. add $ap_real,64,$ap
  861. call __ecp_nistz256_mul_mont ! p256_sqr_mont(Z1sqr, in1_z);
  862. add %sp,LOCALS+$Z1sqr,$rp
  863. add $bp_real,64,$bp
  864. add %sp,LOCALS+$Z2sqr,$ap
  865. call __ecp_nistz256_mul_mont ! p256_mul_mont(S1, Z2sqr, in2_z);
  866. add %sp,LOCALS+$S1,$rp
  867. add $ap_real,64,$bp
  868. add %sp,LOCALS+$Z1sqr,$ap
  869. call __ecp_nistz256_mul_mont ! p256_mul_mont(S2, Z1sqr, in1_z);
  870. add %sp,LOCALS+$S2,$rp
  871. add $ap_real,32,$bp
  872. add %sp,LOCALS+$S1,$ap
  873. call __ecp_nistz256_mul_mont ! p256_mul_mont(S1, S1, in1_y);
  874. add %sp,LOCALS+$S1,$rp
  875. add $bp_real,32,$bp
  876. add %sp,LOCALS+$S2,$ap
  877. call __ecp_nistz256_mul_mont ! p256_mul_mont(S2, S2, in2_y);
  878. add %sp,LOCALS+$S2,$rp
  879. add %sp,LOCALS+$S1,$bp
  880. call __ecp_nistz256_sub_from ! p256_sub(R, S2, S1);
  881. add %sp,LOCALS+$R,$rp
  882. or @acc[1],@acc[0],@acc[0] ! see if result is zero
  883. or @acc[3],@acc[2],@acc[2]
  884. or @acc[5],@acc[4],@acc[4]
  885. or @acc[7],@acc[6],@acc[6]
  886. or @acc[2],@acc[0],@acc[0]
  887. or @acc[6],@acc[4],@acc[4]
  888. or @acc[4],@acc[0],@acc[0]
  889. st @acc[0],[%fp+STACK_BIAS-20]
  890. add $ap_real,0,$bp
  891. add %sp,LOCALS+$Z2sqr,$ap
  892. call __ecp_nistz256_mul_mont ! p256_mul_mont(U1, in1_x, Z2sqr);
  893. add %sp,LOCALS+$U1,$rp
  894. add $bp_real,0,$bp
  895. add %sp,LOCALS+$Z1sqr,$ap
  896. call __ecp_nistz256_mul_mont ! p256_mul_mont(U2, in2_x, Z1sqr);
  897. add %sp,LOCALS+$U2,$rp
  898. add %sp,LOCALS+$U1,$bp
  899. call __ecp_nistz256_sub_from ! p256_sub(H, U2, U1);
  900. add %sp,LOCALS+$H,$rp
  901. or @acc[1],@acc[0],@acc[0] ! see if result is zero
  902. or @acc[3],@acc[2],@acc[2]
  903. or @acc[5],@acc[4],@acc[4]
  904. or @acc[7],@acc[6],@acc[6]
  905. or @acc[2],@acc[0],@acc[0]
  906. or @acc[6],@acc[4],@acc[4]
  907. orcc @acc[4],@acc[0],@acc[0]
  908. bne,pt %icc,.Ladd_proceed ! is_equal(U1,U2)?
  909. nop
  910. ld [%fp+STACK_BIAS-12],$t0
  911. ld [%fp+STACK_BIAS-16],$t1
  912. ld [%fp+STACK_BIAS-20],$t2
  913. andcc $t0,$t1,%g0
  914. be,pt %icc,.Ladd_proceed ! (in1infty || in2infty)?
  915. nop
  916. andcc $t2,$t2,%g0
  917. be,pt %icc,.Ladd_double ! is_equal(S1,S2)?
  918. nop
  919. ldx [%fp+STACK_BIAS-8],$rp
  920. st %g0,[$rp]
  921. st %g0,[$rp+4]
  922. st %g0,[$rp+8]
  923. st %g0,[$rp+12]
  924. st %g0,[$rp+16]
  925. st %g0,[$rp+20]
  926. st %g0,[$rp+24]
  927. st %g0,[$rp+28]
  928. st %g0,[$rp+32]
  929. st %g0,[$rp+32+4]
  930. st %g0,[$rp+32+8]
  931. st %g0,[$rp+32+12]
  932. st %g0,[$rp+32+16]
  933. st %g0,[$rp+32+20]
  934. st %g0,[$rp+32+24]
  935. st %g0,[$rp+32+28]
  936. st %g0,[$rp+64]
  937. st %g0,[$rp+64+4]
  938. st %g0,[$rp+64+8]
  939. st %g0,[$rp+64+12]
  940. st %g0,[$rp+64+16]
  941. st %g0,[$rp+64+20]
  942. st %g0,[$rp+64+24]
  943. st %g0,[$rp+64+28]
  944. b .Ladd_done
  945. nop
  946. .align 16
  947. .Ladd_double:
  948. ldx [%fp+STACK_BIAS-8],$rp_real
  949. mov $ap_real,$ap
  950. b .Lpoint_double_shortcut
  951. add %sp,32*(12-4)+32,%sp ! difference in frame sizes
  952. .align 16
  953. .Ladd_proceed:
  954. add %sp,LOCALS+$R,$bp
  955. add %sp,LOCALS+$R,$ap
  956. call __ecp_nistz256_mul_mont ! p256_sqr_mont(Rsqr, R);
  957. add %sp,LOCALS+$Rsqr,$rp
  958. add $ap_real,64,$bp
  959. add %sp,LOCALS+$H,$ap
  960. call __ecp_nistz256_mul_mont ! p256_mul_mont(res_z, H, in1_z);
  961. add %sp,LOCALS+$res_z,$rp
  962. add %sp,LOCALS+$H,$bp
  963. add %sp,LOCALS+$H,$ap
  964. call __ecp_nistz256_mul_mont ! p256_sqr_mont(Hsqr, H);
  965. add %sp,LOCALS+$Hsqr,$rp
  966. add $bp_real,64,$bp
  967. add %sp,LOCALS+$res_z,$ap
  968. call __ecp_nistz256_mul_mont ! p256_mul_mont(res_z, res_z, in2_z);
  969. add %sp,LOCALS+$res_z,$rp
  970. add %sp,LOCALS+$H,$bp
  971. add %sp,LOCALS+$Hsqr,$ap
  972. call __ecp_nistz256_mul_mont ! p256_mul_mont(Hcub, Hsqr, H);
  973. add %sp,LOCALS+$Hcub,$rp
  974. add %sp,LOCALS+$U1,$bp
  975. add %sp,LOCALS+$Hsqr,$ap
  976. call __ecp_nistz256_mul_mont ! p256_mul_mont(U2, U1, Hsqr);
  977. add %sp,LOCALS+$U2,$rp
  978. call __ecp_nistz256_mul_by_2 ! p256_mul_by_2(Hsqr, U2);
  979. add %sp,LOCALS+$Hsqr,$rp
  980. add %sp,LOCALS+$Rsqr,$bp
  981. call __ecp_nistz256_sub_morf ! p256_sub(res_x, Rsqr, Hsqr);
  982. add %sp,LOCALS+$res_x,$rp
  983. add %sp,LOCALS+$Hcub,$bp
  984. call __ecp_nistz256_sub_from ! p256_sub(res_x, res_x, Hcub);
  985. add %sp,LOCALS+$res_x,$rp
  986. add %sp,LOCALS+$U2,$bp
  987. call __ecp_nistz256_sub_morf ! p256_sub(res_y, U2, res_x);
  988. add %sp,LOCALS+$res_y,$rp
  989. add %sp,LOCALS+$Hcub,$bp
  990. add %sp,LOCALS+$S1,$ap
  991. call __ecp_nistz256_mul_mont ! p256_mul_mont(S2, S1, Hcub);
  992. add %sp,LOCALS+$S2,$rp
  993. add %sp,LOCALS+$R,$bp
  994. add %sp,LOCALS+$res_y,$ap
  995. call __ecp_nistz256_mul_mont ! p256_mul_mont(res_y, res_y, R);
  996. add %sp,LOCALS+$res_y,$rp
  997. add %sp,LOCALS+$S2,$bp
  998. call __ecp_nistz256_sub_from ! p256_sub(res_y, res_y, S2);
  999. add %sp,LOCALS+$res_y,$rp
  1000. ld [%fp+STACK_BIAS-16],$t1 ! !in1infty
  1001. ld [%fp+STACK_BIAS-12],$t2 ! !in2infty
  1002. ldx [%fp+STACK_BIAS-8],$rp
  1003. ___
  1004. for($i=0;$i<96;$i+=8) { # conditional moves
  1005. $code.=<<___;
  1006. ld [%sp+LOCALS+$i],@acc[0] ! res
  1007. ld [%sp+LOCALS+$i+4],@acc[1]
  1008. ld [$bp_real+$i],@acc[2] ! in2
  1009. ld [$bp_real+$i+4],@acc[3]
  1010. ld [$ap_real+$i],@acc[4] ! in1
  1011. ld [$ap_real+$i+4],@acc[5]
  1012. movrz $t1,@acc[2],@acc[0]
  1013. movrz $t1,@acc[3],@acc[1]
  1014. movrz $t2,@acc[4],@acc[0]
  1015. movrz $t2,@acc[5],@acc[1]
  1016. st @acc[0],[$rp+$i]
  1017. st @acc[1],[$rp+$i+4]
  1018. ___
  1019. }
  1020. $code.=<<___;
  1021. .Ladd_done:
  1022. ret
  1023. restore
  1024. .type ecp_nistz256_point_add,#function
  1025. .size ecp_nistz256_point_add,.-ecp_nistz256_point_add
  1026. ___
  1027. }
  1028. ########################################################################
  1029. # void ecp_nistz256_point_add_affine(P256_POINT *out,const P256_POINT *in1,
  1030. # const P256_POINT_AFFINE *in2);
  1031. {
  1032. my ($res_x,$res_y,$res_z,
  1033. $U2,$S2,$H,$R,$Hsqr,$Hcub,$Rsqr)=map(32*$_,(0..9));
  1034. my $Z1sqr = $S2;
  1035. # above map() describes stack layout with 10 temporary
  1036. # 256-bit vectors on top. Then we reserve some space for
  1037. # !in1infty, !in2infty, result of check for zero and return pointer.
  1038. my @ONE_mont=(1,0,0,-1,-1,-1,-2,0);
  1039. my $bp_real=$rp_real;
  1040. $code.=<<___;
  1041. .globl ecp_nistz256_point_add_affine
  1042. .align 32
  1043. ecp_nistz256_point_add_affine:
  1044. SPARC_LOAD_ADDRESS_LEAF(OPENSSL_sparcv9cap_P,%g1,%g5)
  1045. ld [%g1],%g1 ! OPENSSL_sparcv9cap_P[0]
  1046. and %g1,(SPARCV9_VIS3|SPARCV9_64BIT_STACK),%g1
  1047. cmp %g1,(SPARCV9_VIS3|SPARCV9_64BIT_STACK)
  1048. be ecp_nistz256_point_add_affine_vis3
  1049. nop
  1050. save %sp,-STACK_FRAME-32*10-32,%sp
  1051. stx $rp,[%fp+STACK_BIAS-8] ! off-load $rp
  1052. mov $ap,$ap_real
  1053. mov $bp,$bp_real
  1054. ld [$ap+64],$t0 ! in1_z
  1055. ld [$ap+64+4],$t1
  1056. ld [$ap+64+8],$t2
  1057. ld [$ap+64+12],$t3
  1058. ld [$ap+64+16],$t4
  1059. ld [$ap+64+20],$t5
  1060. ld [$ap+64+24],$t6
  1061. ld [$ap+64+28],$t7
  1062. or $t1,$t0,$t0
  1063. or $t3,$t2,$t2
  1064. or $t5,$t4,$t4
  1065. or $t7,$t6,$t6
  1066. or $t2,$t0,$t0
  1067. or $t6,$t4,$t4
  1068. or $t4,$t0,$t0 ! !in1infty
  1069. movrnz $t0,-1,$t0
  1070. st $t0,[%fp+STACK_BIAS-16]
  1071. ld [$bp],@acc[0] ! in2_x
  1072. ld [$bp+4],@acc[1]
  1073. ld [$bp+8],@acc[2]
  1074. ld [$bp+12],@acc[3]
  1075. ld [$bp+16],@acc[4]
  1076. ld [$bp+20],@acc[5]
  1077. ld [$bp+24],@acc[6]
  1078. ld [$bp+28],@acc[7]
  1079. ld [$bp+32],$t0 ! in2_y
  1080. ld [$bp+32+4],$t1
  1081. ld [$bp+32+8],$t2
  1082. ld [$bp+32+12],$t3
  1083. ld [$bp+32+16],$t4
  1084. ld [$bp+32+20],$t5
  1085. ld [$bp+32+24],$t6
  1086. ld [$bp+32+28],$t7
  1087. or @acc[1],@acc[0],@acc[0]
  1088. or @acc[3],@acc[2],@acc[2]
  1089. or @acc[5],@acc[4],@acc[4]
  1090. or @acc[7],@acc[6],@acc[6]
  1091. or @acc[2],@acc[0],@acc[0]
  1092. or @acc[6],@acc[4],@acc[4]
  1093. or @acc[4],@acc[0],@acc[0]
  1094. or $t1,$t0,$t0
  1095. or $t3,$t2,$t2
  1096. or $t5,$t4,$t4
  1097. or $t7,$t6,$t6
  1098. or $t2,$t0,$t0
  1099. or $t6,$t4,$t4
  1100. or $t4,$t0,$t0
  1101. or @acc[0],$t0,$t0 ! !in2infty
  1102. movrnz $t0,-1,$t0
  1103. st $t0,[%fp+STACK_BIAS-12]
  1104. add $ap_real,64,$bp
  1105. add $ap_real,64,$ap
  1106. call __ecp_nistz256_mul_mont ! p256_sqr_mont(Z1sqr, in1_z);
  1107. add %sp,LOCALS+$Z1sqr,$rp
  1108. add $bp_real,0,$bp
  1109. add %sp,LOCALS+$Z1sqr,$ap
  1110. call __ecp_nistz256_mul_mont ! p256_mul_mont(U2, Z1sqr, in2_x);
  1111. add %sp,LOCALS+$U2,$rp
  1112. add $ap_real,0,$bp
  1113. call __ecp_nistz256_sub_from ! p256_sub(H, U2, in1_x);
  1114. add %sp,LOCALS+$H,$rp
  1115. add $ap_real,64,$bp
  1116. add %sp,LOCALS+$Z1sqr,$ap
  1117. call __ecp_nistz256_mul_mont ! p256_mul_mont(S2, Z1sqr, in1_z);
  1118. add %sp,LOCALS+$S2,$rp
  1119. add $ap_real,64,$bp
  1120. add %sp,LOCALS+$H,$ap
  1121. call __ecp_nistz256_mul_mont ! p256_mul_mont(res_z, H, in1_z);
  1122. add %sp,LOCALS+$res_z,$rp
  1123. add $bp_real,32,$bp
  1124. add %sp,LOCALS+$S2,$ap
  1125. call __ecp_nistz256_mul_mont ! p256_mul_mont(S2, S2, in2_y);
  1126. add %sp,LOCALS+$S2,$rp
  1127. add $ap_real,32,$bp
  1128. call __ecp_nistz256_sub_from ! p256_sub(R, S2, in1_y);
  1129. add %sp,LOCALS+$R,$rp
  1130. add %sp,LOCALS+$H,$bp
  1131. add %sp,LOCALS+$H,$ap
  1132. call __ecp_nistz256_mul_mont ! p256_sqr_mont(Hsqr, H);
  1133. add %sp,LOCALS+$Hsqr,$rp
  1134. add %sp,LOCALS+$R,$bp
  1135. add %sp,LOCALS+$R,$ap
  1136. call __ecp_nistz256_mul_mont ! p256_sqr_mont(Rsqr, R);
  1137. add %sp,LOCALS+$Rsqr,$rp
  1138. add %sp,LOCALS+$H,$bp
  1139. add %sp,LOCALS+$Hsqr,$ap
  1140. call __ecp_nistz256_mul_mont ! p256_mul_mont(Hcub, Hsqr, H);
  1141. add %sp,LOCALS+$Hcub,$rp
  1142. add $ap_real,0,$bp
  1143. add %sp,LOCALS+$Hsqr,$ap
  1144. call __ecp_nistz256_mul_mont ! p256_mul_mont(U2, in1_x, Hsqr);
  1145. add %sp,LOCALS+$U2,$rp
  1146. call __ecp_nistz256_mul_by_2 ! p256_mul_by_2(Hsqr, U2);
  1147. add %sp,LOCALS+$Hsqr,$rp
  1148. add %sp,LOCALS+$Rsqr,$bp
  1149. call __ecp_nistz256_sub_morf ! p256_sub(res_x, Rsqr, Hsqr);
  1150. add %sp,LOCALS+$res_x,$rp
  1151. add %sp,LOCALS+$Hcub,$bp
  1152. call __ecp_nistz256_sub_from ! p256_sub(res_x, res_x, Hcub);
  1153. add %sp,LOCALS+$res_x,$rp
  1154. add %sp,LOCALS+$U2,$bp
  1155. call __ecp_nistz256_sub_morf ! p256_sub(res_y, U2, res_x);
  1156. add %sp,LOCALS+$res_y,$rp
  1157. add $ap_real,32,$bp
  1158. add %sp,LOCALS+$Hcub,$ap
  1159. call __ecp_nistz256_mul_mont ! p256_mul_mont(S2, in1_y, Hcub);
  1160. add %sp,LOCALS+$S2,$rp
  1161. add %sp,LOCALS+$R,$bp
  1162. add %sp,LOCALS+$res_y,$ap
  1163. call __ecp_nistz256_mul_mont ! p256_mul_mont(res_y, res_y, R);
  1164. add %sp,LOCALS+$res_y,$rp
  1165. add %sp,LOCALS+$S2,$bp
  1166. call __ecp_nistz256_sub_from ! p256_sub(res_y, res_y, S2);
  1167. add %sp,LOCALS+$res_y,$rp
  1168. ld [%fp+STACK_BIAS-16],$t1 ! !in1infty
  1169. ld [%fp+STACK_BIAS-12],$t2 ! !in2infty
  1170. ldx [%fp+STACK_BIAS-8],$rp
  1171. ___
  1172. for($i=0;$i<64;$i+=8) { # conditional moves
  1173. $code.=<<___;
  1174. ld [%sp+LOCALS+$i],@acc[0] ! res
  1175. ld [%sp+LOCALS+$i+4],@acc[1]
  1176. ld [$bp_real+$i],@acc[2] ! in2
  1177. ld [$bp_real+$i+4],@acc[3]
  1178. ld [$ap_real+$i],@acc[4] ! in1
  1179. ld [$ap_real+$i+4],@acc[5]
  1180. movrz $t1,@acc[2],@acc[0]
  1181. movrz $t1,@acc[3],@acc[1]
  1182. movrz $t2,@acc[4],@acc[0]
  1183. movrz $t2,@acc[5],@acc[1]
  1184. st @acc[0],[$rp+$i]
  1185. st @acc[1],[$rp+$i+4]
  1186. ___
  1187. }
  1188. for(;$i<96;$i+=8) {
  1189. my $j=($i-64)/4;
  1190. $code.=<<___;
  1191. ld [%sp+LOCALS+$i],@acc[0] ! res
  1192. ld [%sp+LOCALS+$i+4],@acc[1]
  1193. ld [$ap_real+$i],@acc[4] ! in1
  1194. ld [$ap_real+$i+4],@acc[5]
  1195. movrz $t1,@ONE_mont[$j],@acc[0]
  1196. movrz $t1,@ONE_mont[$j+1],@acc[1]
  1197. movrz $t2,@acc[4],@acc[0]
  1198. movrz $t2,@acc[5],@acc[1]
  1199. st @acc[0],[$rp+$i]
  1200. st @acc[1],[$rp+$i+4]
  1201. ___
  1202. }
  1203. $code.=<<___;
  1204. ret
  1205. restore
  1206. .type ecp_nistz256_point_add_affine,#function
  1207. .size ecp_nistz256_point_add_affine,.-ecp_nistz256_point_add_affine
  1208. ___
  1209. } }}}
  1210. {{{
  1211. my ($out,$inp,$index)=map("%i$_",(0..2));
  1212. my $mask="%o0";
  1213. $code.=<<___;
  1214. ! void ecp_nistz256_scatter_w5(void *%i0,const P256_POINT *%i1,
  1215. ! int %i2);
  1216. .globl ecp_nistz256_scatter_w5
  1217. .align 32
  1218. ecp_nistz256_scatter_w5:
  1219. save %sp,-STACK_FRAME,%sp
  1220. sll $index,2,$index
  1221. add $out,$index,$out
  1222. ld [$inp],%l0 ! X
  1223. ld [$inp+4],%l1
  1224. ld [$inp+8],%l2
  1225. ld [$inp+12],%l3
  1226. ld [$inp+16],%l4
  1227. ld [$inp+20],%l5
  1228. ld [$inp+24],%l6
  1229. ld [$inp+28],%l7
  1230. add $inp,32,$inp
  1231. st %l0,[$out+64*0-4]
  1232. st %l1,[$out+64*1-4]
  1233. st %l2,[$out+64*2-4]
  1234. st %l3,[$out+64*3-4]
  1235. st %l4,[$out+64*4-4]
  1236. st %l5,[$out+64*5-4]
  1237. st %l6,[$out+64*6-4]
  1238. st %l7,[$out+64*7-4]
  1239. add $out,64*8,$out
  1240. ld [$inp],%l0 ! Y
  1241. ld [$inp+4],%l1
  1242. ld [$inp+8],%l2
  1243. ld [$inp+12],%l3
  1244. ld [$inp+16],%l4
  1245. ld [$inp+20],%l5
  1246. ld [$inp+24],%l6
  1247. ld [$inp+28],%l7
  1248. add $inp,32,$inp
  1249. st %l0,[$out+64*0-4]
  1250. st %l1,[$out+64*1-4]
  1251. st %l2,[$out+64*2-4]
  1252. st %l3,[$out+64*3-4]
  1253. st %l4,[$out+64*4-4]
  1254. st %l5,[$out+64*5-4]
  1255. st %l6,[$out+64*6-4]
  1256. st %l7,[$out+64*7-4]
  1257. add $out,64*8,$out
  1258. ld [$inp],%l0 ! Z
  1259. ld [$inp+4],%l1
  1260. ld [$inp+8],%l2
  1261. ld [$inp+12],%l3
  1262. ld [$inp+16],%l4
  1263. ld [$inp+20],%l5
  1264. ld [$inp+24],%l6
  1265. ld [$inp+28],%l7
  1266. st %l0,[$out+64*0-4]
  1267. st %l1,[$out+64*1-4]
  1268. st %l2,[$out+64*2-4]
  1269. st %l3,[$out+64*3-4]
  1270. st %l4,[$out+64*4-4]
  1271. st %l5,[$out+64*5-4]
  1272. st %l6,[$out+64*6-4]
  1273. st %l7,[$out+64*7-4]
  1274. ret
  1275. restore
  1276. .type ecp_nistz256_scatter_w5,#function
  1277. .size ecp_nistz256_scatter_w5,.-ecp_nistz256_scatter_w5
  1278. ! void ecp_nistz256_gather_w5(P256_POINT *%i0,const void *%i1,
  1279. ! int %i2);
  1280. .globl ecp_nistz256_gather_w5
  1281. .align 32
  1282. ecp_nistz256_gather_w5:
  1283. save %sp,-STACK_FRAME,%sp
  1284. neg $index,$mask
  1285. srax $mask,63,$mask
  1286. add $index,$mask,$index
  1287. sll $index,2,$index
  1288. add $inp,$index,$inp
  1289. ld [$inp+64*0],%l0
  1290. ld [$inp+64*1],%l1
  1291. ld [$inp+64*2],%l2
  1292. ld [$inp+64*3],%l3
  1293. ld [$inp+64*4],%l4
  1294. ld [$inp+64*5],%l5
  1295. ld [$inp+64*6],%l6
  1296. ld [$inp+64*7],%l7
  1297. add $inp,64*8,$inp
  1298. and %l0,$mask,%l0
  1299. and %l1,$mask,%l1
  1300. st %l0,[$out] ! X
  1301. and %l2,$mask,%l2
  1302. st %l1,[$out+4]
  1303. and %l3,$mask,%l3
  1304. st %l2,[$out+8]
  1305. and %l4,$mask,%l4
  1306. st %l3,[$out+12]
  1307. and %l5,$mask,%l5
  1308. st %l4,[$out+16]
  1309. and %l6,$mask,%l6
  1310. st %l5,[$out+20]
  1311. and %l7,$mask,%l7
  1312. st %l6,[$out+24]
  1313. st %l7,[$out+28]
  1314. add $out,32,$out
  1315. ld [$inp+64*0],%l0
  1316. ld [$inp+64*1],%l1
  1317. ld [$inp+64*2],%l2
  1318. ld [$inp+64*3],%l3
  1319. ld [$inp+64*4],%l4
  1320. ld [$inp+64*5],%l5
  1321. ld [$inp+64*6],%l6
  1322. ld [$inp+64*7],%l7
  1323. add $inp,64*8,$inp
  1324. and %l0,$mask,%l0
  1325. and %l1,$mask,%l1
  1326. st %l0,[$out] ! Y
  1327. and %l2,$mask,%l2
  1328. st %l1,[$out+4]
  1329. and %l3,$mask,%l3
  1330. st %l2,[$out+8]
  1331. and %l4,$mask,%l4
  1332. st %l3,[$out+12]
  1333. and %l5,$mask,%l5
  1334. st %l4,[$out+16]
  1335. and %l6,$mask,%l6
  1336. st %l5,[$out+20]
  1337. and %l7,$mask,%l7
  1338. st %l6,[$out+24]
  1339. st %l7,[$out+28]
  1340. add $out,32,$out
  1341. ld [$inp+64*0],%l0
  1342. ld [$inp+64*1],%l1
  1343. ld [$inp+64*2],%l2
  1344. ld [$inp+64*3],%l3
  1345. ld [$inp+64*4],%l4
  1346. ld [$inp+64*5],%l5
  1347. ld [$inp+64*6],%l6
  1348. ld [$inp+64*7],%l7
  1349. and %l0,$mask,%l0
  1350. and %l1,$mask,%l1
  1351. st %l0,[$out] ! Z
  1352. and %l2,$mask,%l2
  1353. st %l1,[$out+4]
  1354. and %l3,$mask,%l3
  1355. st %l2,[$out+8]
  1356. and %l4,$mask,%l4
  1357. st %l3,[$out+12]
  1358. and %l5,$mask,%l5
  1359. st %l4,[$out+16]
  1360. and %l6,$mask,%l6
  1361. st %l5,[$out+20]
  1362. and %l7,$mask,%l7
  1363. st %l6,[$out+24]
  1364. st %l7,[$out+28]
  1365. ret
  1366. restore
  1367. .type ecp_nistz256_gather_w5,#function
  1368. .size ecp_nistz256_gather_w5,.-ecp_nistz256_gather_w5
  1369. ! void ecp_nistz256_scatter_w7(void *%i0,const P256_POINT_AFFINE *%i1,
  1370. ! int %i2);
  1371. .globl ecp_nistz256_scatter_w7
  1372. .align 32
  1373. ecp_nistz256_scatter_w7:
  1374. save %sp,-STACK_FRAME,%sp
  1375. nop
  1376. add $out,$index,$out
  1377. mov 64/4,$index
  1378. .Loop_scatter_w7:
  1379. ld [$inp],%l0
  1380. add $inp,4,$inp
  1381. subcc $index,1,$index
  1382. stb %l0,[$out+64*0]
  1383. srl %l0,8,%l1
  1384. stb %l1,[$out+64*1]
  1385. srl %l0,16,%l2
  1386. stb %l2,[$out+64*2]
  1387. srl %l0,24,%l3
  1388. stb %l3,[$out+64*3]
  1389. bne .Loop_scatter_w7
  1390. add $out,64*4,$out
  1391. ret
  1392. restore
  1393. .type ecp_nistz256_scatter_w7,#function
  1394. .size ecp_nistz256_scatter_w7,.-ecp_nistz256_scatter_w7
  1395. ! void ecp_nistz256_gather_w7(P256_POINT_AFFINE *%i0,const void *%i1,
  1396. ! int %i2);
  1397. .globl ecp_nistz256_gather_w7
  1398. .align 32
  1399. ecp_nistz256_gather_w7:
  1400. save %sp,-STACK_FRAME,%sp
  1401. neg $index,$mask
  1402. srax $mask,63,$mask
  1403. add $index,$mask,$index
  1404. add $inp,$index,$inp
  1405. mov 64/4,$index
  1406. .Loop_gather_w7:
  1407. ldub [$inp+64*0],%l0
  1408. prefetch [$inp+3840+64*0],1
  1409. subcc $index,1,$index
  1410. ldub [$inp+64*1],%l1
  1411. prefetch [$inp+3840+64*1],1
  1412. ldub [$inp+64*2],%l2
  1413. prefetch [$inp+3840+64*2],1
  1414. ldub [$inp+64*3],%l3
  1415. prefetch [$inp+3840+64*3],1
  1416. add $inp,64*4,$inp
  1417. sll %l1,8,%l1
  1418. sll %l2,16,%l2
  1419. or %l0,%l1,%l0
  1420. sll %l3,24,%l3
  1421. or %l0,%l2,%l0
  1422. or %l0,%l3,%l0
  1423. and %l0,$mask,%l0
  1424. st %l0,[$out]
  1425. bne .Loop_gather_w7
  1426. add $out,4,$out
  1427. ret
  1428. restore
  1429. .type ecp_nistz256_gather_w7,#function
  1430. .size ecp_nistz256_gather_w7,.-ecp_nistz256_gather_w7
  1431. ___
  1432. }}}
  1433. {{{
  1434. ########################################################################
  1435. # Following subroutines are VIS3 counterparts of those above that
  1436. # implement ones found in ecp_nistz256.c. Key difference is that they
  1437. # use 128-bit multiplication and addition with 64-bit carry, and in order
  1438. # to do that they perform conversion from uin32_t[8] to uint64_t[4] upon
  1439. # entry and vice versa on return.
  1440. #
  1441. my ($rp,$ap,$bp)=map("%i$_",(0..2));
  1442. my ($t0,$t1,$t2,$t3,$a0,$a1,$a2,$a3)=map("%l$_",(0..7));
  1443. my ($acc0,$acc1,$acc2,$acc3,$acc4,$acc5)=map("%o$_",(0..5));
  1444. my ($bi,$poly1,$poly3,$minus1)=(map("%i$_",(3..5)),"%g1");
  1445. my ($rp_real,$ap_real)=("%g2","%g3");
  1446. my ($acc6,$acc7)=($bp,$bi); # used in squaring
  1447. $code.=<<___;
  1448. .align 32
  1449. __ecp_nistz256_mul_by_2_vis3:
  1450. addcc $acc0,$acc0,$acc0
  1451. addxccc $acc1,$acc1,$acc1
  1452. addxccc $acc2,$acc2,$acc2
  1453. addxccc $acc3,$acc3,$acc3
  1454. b .Lreduce_by_sub_vis3
  1455. addxc %g0,%g0,$acc4 ! did it carry?
  1456. .type __ecp_nistz256_mul_by_2_vis3,#function
  1457. .size __ecp_nistz256_mul_by_2_vis3,.-__ecp_nistz256_mul_by_2_vis3
  1458. .align 32
  1459. __ecp_nistz256_add_vis3:
  1460. ldx [$bp+0],$t0
  1461. ldx [$bp+8],$t1
  1462. ldx [$bp+16],$t2
  1463. ldx [$bp+24],$t3
  1464. __ecp_nistz256_add_noload_vis3:
  1465. addcc $t0,$acc0,$acc0
  1466. addxccc $t1,$acc1,$acc1
  1467. addxccc $t2,$acc2,$acc2
  1468. addxccc $t3,$acc3,$acc3
  1469. addxc %g0,%g0,$acc4 ! did it carry?
  1470. .Lreduce_by_sub_vis3:
  1471. addcc $acc0,1,$t0 ! add -modulus, i.e. subtract
  1472. addxccc $acc1,$poly1,$t1
  1473. addxccc $acc2,$minus1,$t2
  1474. addxccc $acc3,$poly3,$t3
  1475. addxc $acc4,$minus1,$acc4
  1476. movrz $acc4,$t0,$acc0 ! ret = borrow ? ret : ret-modulus
  1477. movrz $acc4,$t1,$acc1
  1478. stx $acc0,[$rp]
  1479. movrz $acc4,$t2,$acc2
  1480. stx $acc1,[$rp+8]
  1481. movrz $acc4,$t3,$acc3
  1482. stx $acc2,[$rp+16]
  1483. retl
  1484. stx $acc3,[$rp+24]
  1485. .type __ecp_nistz256_add_vis3,#function
  1486. .size __ecp_nistz256_add_vis3,.-__ecp_nistz256_add_vis3
  1487. ! Trouble with subtraction is that there is no subtraction with 64-bit
  1488. ! borrow, only with 32-bit one. For this reason we "decompose" 64-bit
  1489. ! $acc0-$acc3 to 32-bit values and pick b[4] in 32-bit pieces. But
  1490. ! recall that SPARC is big-endian, which is why you'll observe that
  1491. ! b[4] is accessed as 4-0-12-8-20-16-28-24. And prior reduction we
  1492. ! "collect" result back to 64-bit $acc0-$acc3.
  1493. .align 32
  1494. __ecp_nistz256_sub_from_vis3:
  1495. ld [$bp+4],$t0
  1496. ld [$bp+0],$t1
  1497. ld [$bp+12],$t2
  1498. ld [$bp+8],$t3
  1499. srlx $acc0,32,$acc4
  1500. not $poly1,$poly1
  1501. srlx $acc1,32,$acc5
  1502. subcc $acc0,$t0,$acc0
  1503. ld [$bp+20],$t0
  1504. subccc $acc4,$t1,$acc4
  1505. ld [$bp+16],$t1
  1506. subccc $acc1,$t2,$acc1
  1507. ld [$bp+28],$t2
  1508. and $acc0,$poly1,$acc0
  1509. subccc $acc5,$t3,$acc5
  1510. ld [$bp+24],$t3
  1511. sllx $acc4,32,$acc4
  1512. and $acc1,$poly1,$acc1
  1513. sllx $acc5,32,$acc5
  1514. or $acc0,$acc4,$acc0
  1515. srlx $acc2,32,$acc4
  1516. or $acc1,$acc5,$acc1
  1517. srlx $acc3,32,$acc5
  1518. subccc $acc2,$t0,$acc2
  1519. subccc $acc4,$t1,$acc4
  1520. subccc $acc3,$t2,$acc3
  1521. and $acc2,$poly1,$acc2
  1522. subccc $acc5,$t3,$acc5
  1523. sllx $acc4,32,$acc4
  1524. and $acc3,$poly1,$acc3
  1525. sllx $acc5,32,$acc5
  1526. or $acc2,$acc4,$acc2
  1527. subc %g0,%g0,$acc4 ! did it borrow?
  1528. b .Lreduce_by_add_vis3
  1529. or $acc3,$acc5,$acc3
  1530. .type __ecp_nistz256_sub_from_vis3,#function
  1531. .size __ecp_nistz256_sub_from_vis3,.-__ecp_nistz256_sub_from_vis3
  1532. .align 32
  1533. __ecp_nistz256_sub_morf_vis3:
  1534. ld [$bp+4],$t0
  1535. ld [$bp+0],$t1
  1536. ld [$bp+12],$t2
  1537. ld [$bp+8],$t3
  1538. srlx $acc0,32,$acc4
  1539. not $poly1,$poly1
  1540. srlx $acc1,32,$acc5
  1541. subcc $t0,$acc0,$acc0
  1542. ld [$bp+20],$t0
  1543. subccc $t1,$acc4,$acc4
  1544. ld [$bp+16],$t1
  1545. subccc $t2,$acc1,$acc1
  1546. ld [$bp+28],$t2
  1547. and $acc0,$poly1,$acc0
  1548. subccc $t3,$acc5,$acc5
  1549. ld [$bp+24],$t3
  1550. sllx $acc4,32,$acc4
  1551. and $acc1,$poly1,$acc1
  1552. sllx $acc5,32,$acc5
  1553. or $acc0,$acc4,$acc0
  1554. srlx $acc2,32,$acc4
  1555. or $acc1,$acc5,$acc1
  1556. srlx $acc3,32,$acc5
  1557. subccc $t0,$acc2,$acc2
  1558. subccc $t1,$acc4,$acc4
  1559. subccc $t2,$acc3,$acc3
  1560. and $acc2,$poly1,$acc2
  1561. subccc $t3,$acc5,$acc5
  1562. sllx $acc4,32,$acc4
  1563. and $acc3,$poly1,$acc3
  1564. sllx $acc5,32,$acc5
  1565. or $acc2,$acc4,$acc2
  1566. subc %g0,%g0,$acc4 ! did it borrow?
  1567. or $acc3,$acc5,$acc3
  1568. .Lreduce_by_add_vis3:
  1569. addcc $acc0,-1,$t0 ! add modulus
  1570. not $poly3,$t3
  1571. addxccc $acc1,$poly1,$t1
  1572. not $poly1,$poly1 ! restore $poly1
  1573. addxccc $acc2,%g0,$t2
  1574. addxc $acc3,$t3,$t3
  1575. movrnz $acc4,$t0,$acc0 ! if a-b borrowed, ret = ret+mod
  1576. movrnz $acc4,$t1,$acc1
  1577. stx $acc0,[$rp]
  1578. movrnz $acc4,$t2,$acc2
  1579. stx $acc1,[$rp+8]
  1580. movrnz $acc4,$t3,$acc3
  1581. stx $acc2,[$rp+16]
  1582. retl
  1583. stx $acc3,[$rp+24]
  1584. .type __ecp_nistz256_sub_morf_vis3,#function
  1585. .size __ecp_nistz256_sub_morf_vis3,.-__ecp_nistz256_sub_morf_vis3
  1586. .align 32
  1587. __ecp_nistz256_div_by_2_vis3:
  1588. ! ret = (a is odd ? a+mod : a) >> 1
  1589. not $poly1,$t1
  1590. not $poly3,$t3
  1591. and $acc0,1,$acc5
  1592. addcc $acc0,-1,$t0 ! add modulus
  1593. addxccc $acc1,$t1,$t1
  1594. addxccc $acc2,%g0,$t2
  1595. addxccc $acc3,$t3,$t3
  1596. addxc %g0,%g0,$acc4 ! carry bit
  1597. movrnz $acc5,$t0,$acc0
  1598. movrnz $acc5,$t1,$acc1
  1599. movrnz $acc5,$t2,$acc2
  1600. movrnz $acc5,$t3,$acc3
  1601. movrz $acc5,%g0,$acc4
  1602. ! ret >>= 1
  1603. srlx $acc0,1,$acc0
  1604. sllx $acc1,63,$t0
  1605. srlx $acc1,1,$acc1
  1606. or $acc0,$t0,$acc0
  1607. sllx $acc2,63,$t1
  1608. srlx $acc2,1,$acc2
  1609. or $acc1,$t1,$acc1
  1610. sllx $acc3,63,$t2
  1611. stx $acc0,[$rp]
  1612. srlx $acc3,1,$acc3
  1613. or $acc2,$t2,$acc2
  1614. sllx $acc4,63,$t3 ! don't forget carry bit
  1615. stx $acc1,[$rp+8]
  1616. or $acc3,$t3,$acc3
  1617. stx $acc2,[$rp+16]
  1618. retl
  1619. stx $acc3,[$rp+24]
  1620. .type __ecp_nistz256_div_by_2_vis3,#function
  1621. .size __ecp_nistz256_div_by_2_vis3,.-__ecp_nistz256_div_by_2_vis3
  1622. ! compared to __ecp_nistz256_mul_mont it's almost 4x smaller and
  1623. ! 4x faster [on T4]...
  1624. .align 32
  1625. __ecp_nistz256_mul_mont_vis3:
  1626. mulx $a0,$bi,$acc0
  1627. not $poly3,$poly3 ! 0xFFFFFFFF00000001
  1628. umulxhi $a0,$bi,$t0
  1629. mulx $a1,$bi,$acc1
  1630. umulxhi $a1,$bi,$t1
  1631. mulx $a2,$bi,$acc2
  1632. umulxhi $a2,$bi,$t2
  1633. mulx $a3,$bi,$acc3
  1634. umulxhi $a3,$bi,$t3
  1635. ldx [$bp+8],$bi ! b[1]
  1636. addcc $acc1,$t0,$acc1 ! accumulate high parts of multiplication
  1637. sllx $acc0,32,$t0
  1638. addxccc $acc2,$t1,$acc2
  1639. srlx $acc0,32,$t1
  1640. addxccc $acc3,$t2,$acc3
  1641. addxc %g0,$t3,$acc4
  1642. mov 0,$acc5
  1643. ___
  1644. for($i=1;$i<4;$i++) {
  1645. # Reduction iteration is normally performed by accumulating
  1646. # result of multiplication of modulus by "magic" digit [and
  1647. # omitting least significant word, which is guaranteed to
  1648. # be 0], but thanks to special form of modulus and "magic"
  1649. # digit being equal to least significant word, it can be
  1650. # performed with additions and subtractions alone. Indeed:
  1651. #
  1652. # ffff0001.00000000.0000ffff.ffffffff
  1653. # * abcdefgh
  1654. # + xxxxxxxx.xxxxxxxx.xxxxxxxx.xxxxxxxx.abcdefgh
  1655. #
  1656. # Now observing that ff..ff*x = (2^n-1)*x = 2^n*x-x, we
  1657. # rewrite above as:
  1658. #
  1659. # xxxxxxxx.xxxxxxxx.xxxxxxxx.xxxxxxxx.abcdefgh
  1660. # + abcdefgh.abcdefgh.0000abcd.efgh0000.00000000
  1661. # - 0000abcd.efgh0000.00000000.00000000.abcdefgh
  1662. #
  1663. # or marking redundant operations:
  1664. #
  1665. # xxxxxxxx.xxxxxxxx.xxxxxxxx.xxxxxxxx.--------
  1666. # + abcdefgh.abcdefgh.0000abcd.efgh0000.--------
  1667. # - 0000abcd.efgh0000.--------.--------.--------
  1668. # ^^^^^^^^ but this word is calculated with umulxhi, because
  1669. # there is no subtract with 64-bit borrow:-(
  1670. $code.=<<___;
  1671. sub $acc0,$t0,$t2 ! acc0*0xFFFFFFFF00000001, low part
  1672. umulxhi $acc0,$poly3,$t3 ! acc0*0xFFFFFFFF00000001, high part
  1673. addcc $acc1,$t0,$acc0 ! +=acc[0]<<96 and omit acc[0]
  1674. mulx $a0,$bi,$t0
  1675. addxccc $acc2,$t1,$acc1
  1676. mulx $a1,$bi,$t1
  1677. addxccc $acc3,$t2,$acc2 ! +=acc[0]*0xFFFFFFFF00000001
  1678. mulx $a2,$bi,$t2
  1679. addxccc $acc4,$t3,$acc3
  1680. mulx $a3,$bi,$t3
  1681. addxc $acc5,%g0,$acc4
  1682. addcc $acc0,$t0,$acc0 ! accumulate low parts of multiplication
  1683. umulxhi $a0,$bi,$t0
  1684. addxccc $acc1,$t1,$acc1
  1685. umulxhi $a1,$bi,$t1
  1686. addxccc $acc2,$t2,$acc2
  1687. umulxhi $a2,$bi,$t2
  1688. addxccc $acc3,$t3,$acc3
  1689. umulxhi $a3,$bi,$t3
  1690. addxc $acc4,%g0,$acc4
  1691. ___
  1692. $code.=<<___ if ($i<3);
  1693. ldx [$bp+8*($i+1)],$bi ! bp[$i+1]
  1694. ___
  1695. $code.=<<___;
  1696. addcc $acc1,$t0,$acc1 ! accumulate high parts of multiplication
  1697. sllx $acc0,32,$t0
  1698. addxccc $acc2,$t1,$acc2
  1699. srlx $acc0,32,$t1
  1700. addxccc $acc3,$t2,$acc3
  1701. addxccc $acc4,$t3,$acc4
  1702. addxc %g0,%g0,$acc5
  1703. ___
  1704. }
  1705. $code.=<<___;
  1706. sub $acc0,$t0,$t2 ! acc0*0xFFFFFFFF00000001, low part
  1707. umulxhi $acc0,$poly3,$t3 ! acc0*0xFFFFFFFF00000001, high part
  1708. addcc $acc1,$t0,$acc0 ! +=acc[0]<<96 and omit acc[0]
  1709. addxccc $acc2,$t1,$acc1
  1710. addxccc $acc3,$t2,$acc2 ! +=acc[0]*0xFFFFFFFF00000001
  1711. addxccc $acc4,$t3,$acc3
  1712. b .Lmul_final_vis3 ! see below
  1713. addxc $acc5,%g0,$acc4
  1714. .type __ecp_nistz256_mul_mont_vis3,#function
  1715. .size __ecp_nistz256_mul_mont_vis3,.-__ecp_nistz256_mul_mont_vis3
  1716. ! compared to above __ecp_nistz256_mul_mont_vis3 it's 21% less
  1717. ! instructions, but only 14% faster [on T4]...
  1718. .align 32
  1719. __ecp_nistz256_sqr_mont_vis3:
  1720. ! | | | | | |a1*a0| |
  1721. ! | | | | |a2*a0| | |
  1722. ! | |a3*a2|a3*a0| | | |
  1723. ! | | | |a2*a1| | | |
  1724. ! | | |a3*a1| | | | |
  1725. ! *| | | | | | | | 2|
  1726. ! +|a3*a3|a2*a2|a1*a1|a0*a0|
  1727. ! |--+--+--+--+--+--+--+--|
  1728. ! |A7|A6|A5|A4|A3|A2|A1|A0|, where Ax is $accx, i.e. follow $accx
  1729. !
  1730. ! "can't overflow" below mark carrying into high part of
  1731. ! multiplication result, which can't overflow, because it
  1732. ! can never be all ones.
  1733. mulx $a1,$a0,$acc1 ! a[1]*a[0]
  1734. umulxhi $a1,$a0,$t1
  1735. mulx $a2,$a0,$acc2 ! a[2]*a[0]
  1736. umulxhi $a2,$a0,$t2
  1737. mulx $a3,$a0,$acc3 ! a[3]*a[0]
  1738. umulxhi $a3,$a0,$acc4
  1739. addcc $acc2,$t1,$acc2 ! accumulate high parts of multiplication
  1740. mulx $a2,$a1,$t0 ! a[2]*a[1]
  1741. umulxhi $a2,$a1,$t1
  1742. addxccc $acc3,$t2,$acc3
  1743. mulx $a3,$a1,$t2 ! a[3]*a[1]
  1744. umulxhi $a3,$a1,$t3
  1745. addxc $acc4,%g0,$acc4 ! can't overflow
  1746. mulx $a3,$a2,$acc5 ! a[3]*a[2]
  1747. not $poly3,$poly3 ! 0xFFFFFFFF00000001
  1748. umulxhi $a3,$a2,$acc6
  1749. addcc $t2,$t1,$t1 ! accumulate high parts of multiplication
  1750. mulx $a0,$a0,$acc0 ! a[0]*a[0]
  1751. addxc $t3,%g0,$t2 ! can't overflow
  1752. addcc $acc3,$t0,$acc3 ! accumulate low parts of multiplication
  1753. umulxhi $a0,$a0,$a0
  1754. addxccc $acc4,$t1,$acc4
  1755. mulx $a1,$a1,$t1 ! a[1]*a[1]
  1756. addxccc $acc5,$t2,$acc5
  1757. umulxhi $a1,$a1,$a1
  1758. addxc $acc6,%g0,$acc6 ! can't overflow
  1759. addcc $acc1,$acc1,$acc1 ! acc[1-6]*=2
  1760. mulx $a2,$a2,$t2 ! a[2]*a[2]
  1761. addxccc $acc2,$acc2,$acc2
  1762. umulxhi $a2,$a2,$a2
  1763. addxccc $acc3,$acc3,$acc3
  1764. mulx $a3,$a3,$t3 ! a[3]*a[3]
  1765. addxccc $acc4,$acc4,$acc4
  1766. umulxhi $a3,$a3,$a3
  1767. addxccc $acc5,$acc5,$acc5
  1768. addxccc $acc6,$acc6,$acc6
  1769. addxc %g0,%g0,$acc7
  1770. addcc $acc1,$a0,$acc1 ! +a[i]*a[i]
  1771. addxccc $acc2,$t1,$acc2
  1772. addxccc $acc3,$a1,$acc3
  1773. addxccc $acc4,$t2,$acc4
  1774. sllx $acc0,32,$t0
  1775. addxccc $acc5,$a2,$acc5
  1776. srlx $acc0,32,$t1
  1777. addxccc $acc6,$t3,$acc6
  1778. sub $acc0,$t0,$t2 ! acc0*0xFFFFFFFF00000001, low part
  1779. addxc $acc7,$a3,$acc7
  1780. ___
  1781. for($i=0;$i<3;$i++) { # reductions, see commentary
  1782. # in multiplication for details
  1783. $code.=<<___;
  1784. umulxhi $acc0,$poly3,$t3 ! acc0*0xFFFFFFFF00000001, high part
  1785. addcc $acc1,$t0,$acc0 ! +=acc[0]<<96 and omit acc[0]
  1786. sllx $acc0,32,$t0
  1787. addxccc $acc2,$t1,$acc1
  1788. srlx $acc0,32,$t1
  1789. addxccc $acc3,$t2,$acc2 ! +=acc[0]*0xFFFFFFFF00000001
  1790. sub $acc0,$t0,$t2 ! acc0*0xFFFFFFFF00000001, low part
  1791. addxc %g0,$t3,$acc3 ! can't overflow
  1792. ___
  1793. }
  1794. $code.=<<___;
  1795. umulxhi $acc0,$poly3,$t3 ! acc0*0xFFFFFFFF00000001, high part
  1796. addcc $acc1,$t0,$acc0 ! +=acc[0]<<96 and omit acc[0]
  1797. addxccc $acc2,$t1,$acc1
  1798. addxccc $acc3,$t2,$acc2 ! +=acc[0]*0xFFFFFFFF00000001
  1799. addxc %g0,$t3,$acc3 ! can't overflow
  1800. addcc $acc0,$acc4,$acc0 ! accumulate upper half
  1801. addxccc $acc1,$acc5,$acc1
  1802. addxccc $acc2,$acc6,$acc2
  1803. addxccc $acc3,$acc7,$acc3
  1804. addxc %g0,%g0,$acc4
  1805. .Lmul_final_vis3:
  1806. ! Final step is "if result > mod, subtract mod", but as comparison
  1807. ! means subtraction, we do the subtraction and then copy outcome
  1808. ! if it didn't borrow. But note that as we [have to] replace
  1809. ! subtraction with addition with negative, carry/borrow logic is
  1810. ! inverse.
  1811. addcc $acc0,1,$t0 ! add -modulus, i.e. subtract
  1812. not $poly3,$poly3 ! restore 0x00000000FFFFFFFE
  1813. addxccc $acc1,$poly1,$t1
  1814. addxccc $acc2,$minus1,$t2
  1815. addxccc $acc3,$poly3,$t3
  1816. addxccc $acc4,$minus1,%g0 ! did it carry?
  1817. movcs %xcc,$t0,$acc0
  1818. movcs %xcc,$t1,$acc1
  1819. stx $acc0,[$rp]
  1820. movcs %xcc,$t2,$acc2
  1821. stx $acc1,[$rp+8]
  1822. movcs %xcc,$t3,$acc3
  1823. stx $acc2,[$rp+16]
  1824. retl
  1825. stx $acc3,[$rp+24]
  1826. .type __ecp_nistz256_sqr_mont_vis3,#function
  1827. .size __ecp_nistz256_sqr_mont_vis3,.-__ecp_nistz256_sqr_mont_vis3
  1828. ___
  1829. ########################################################################
  1830. # void ecp_nistz256_point_double(P256_POINT *out,const P256_POINT *inp);
  1831. #
  1832. {
  1833. my ($res_x,$res_y,$res_z,
  1834. $in_x,$in_y,$in_z,
  1835. $S,$M,$Zsqr,$tmp0)=map(32*$_,(0..9));
  1836. # above map() describes stack layout with 10 temporary
  1837. # 256-bit vectors on top.
  1838. $code.=<<___;
  1839. .align 32
  1840. ecp_nistz256_point_double_vis3:
  1841. save %sp,-STACK64_FRAME-32*10,%sp
  1842. mov $rp,$rp_real
  1843. .Ldouble_shortcut_vis3:
  1844. mov -1,$minus1
  1845. mov -2,$poly3
  1846. sllx $minus1,32,$poly1 ! 0xFFFFFFFF00000000
  1847. srl $poly3,0,$poly3 ! 0x00000000FFFFFFFE
  1848. ! convert input to uint64_t[4]
  1849. ld [$ap],$a0 ! in_x
  1850. ld [$ap+4],$t0
  1851. ld [$ap+8],$a1
  1852. ld [$ap+12],$t1
  1853. ld [$ap+16],$a2
  1854. ld [$ap+20],$t2
  1855. ld [$ap+24],$a3
  1856. ld [$ap+28],$t3
  1857. sllx $t0,32,$t0
  1858. sllx $t1,32,$t1
  1859. ld [$ap+32],$acc0 ! in_y
  1860. or $a0,$t0,$a0
  1861. ld [$ap+32+4],$t0
  1862. sllx $t2,32,$t2
  1863. ld [$ap+32+8],$acc1
  1864. or $a1,$t1,$a1
  1865. ld [$ap+32+12],$t1
  1866. sllx $t3,32,$t3
  1867. ld [$ap+32+16],$acc2
  1868. or $a2,$t2,$a2
  1869. ld [$ap+32+20],$t2
  1870. or $a3,$t3,$a3
  1871. ld [$ap+32+24],$acc3
  1872. sllx $t0,32,$t0
  1873. ld [$ap+32+28],$t3
  1874. sllx $t1,32,$t1
  1875. stx $a0,[%sp+LOCALS64+$in_x]
  1876. sllx $t2,32,$t2
  1877. stx $a1,[%sp+LOCALS64+$in_x+8]
  1878. sllx $t3,32,$t3
  1879. stx $a2,[%sp+LOCALS64+$in_x+16]
  1880. or $acc0,$t0,$acc0
  1881. stx $a3,[%sp+LOCALS64+$in_x+24]
  1882. or $acc1,$t1,$acc1
  1883. stx $acc0,[%sp+LOCALS64+$in_y]
  1884. or $acc2,$t2,$acc2
  1885. stx $acc1,[%sp+LOCALS64+$in_y+8]
  1886. or $acc3,$t3,$acc3
  1887. stx $acc2,[%sp+LOCALS64+$in_y+16]
  1888. stx $acc3,[%sp+LOCALS64+$in_y+24]
  1889. ld [$ap+64],$a0 ! in_z
  1890. ld [$ap+64+4],$t0
  1891. ld [$ap+64+8],$a1
  1892. ld [$ap+64+12],$t1
  1893. ld [$ap+64+16],$a2
  1894. ld [$ap+64+20],$t2
  1895. ld [$ap+64+24],$a3
  1896. ld [$ap+64+28],$t3
  1897. sllx $t0,32,$t0
  1898. sllx $t1,32,$t1
  1899. or $a0,$t0,$a0
  1900. sllx $t2,32,$t2
  1901. or $a1,$t1,$a1
  1902. sllx $t3,32,$t3
  1903. or $a2,$t2,$a2
  1904. or $a3,$t3,$a3
  1905. sllx $t0,32,$t0
  1906. sllx $t1,32,$t1
  1907. stx $a0,[%sp+LOCALS64+$in_z]
  1908. sllx $t2,32,$t2
  1909. stx $a1,[%sp+LOCALS64+$in_z+8]
  1910. sllx $t3,32,$t3
  1911. stx $a2,[%sp+LOCALS64+$in_z+16]
  1912. stx $a3,[%sp+LOCALS64+$in_z+24]
  1913. ! in_y is still in $acc0-$acc3
  1914. call __ecp_nistz256_mul_by_2_vis3 ! p256_mul_by_2(S, in_y);
  1915. add %sp,LOCALS64+$S,$rp
  1916. ! in_z is still in $a0-$a3
  1917. call __ecp_nistz256_sqr_mont_vis3 ! p256_sqr_mont(Zsqr, in_z);
  1918. add %sp,LOCALS64+$Zsqr,$rp
  1919. mov $acc0,$a0 ! put Zsqr aside
  1920. mov $acc1,$a1
  1921. mov $acc2,$a2
  1922. mov $acc3,$a3
  1923. add %sp,LOCALS64+$in_x,$bp
  1924. call __ecp_nistz256_add_vis3 ! p256_add(M, Zsqr, in_x);
  1925. add %sp,LOCALS64+$M,$rp
  1926. mov $a0,$acc0 ! restore Zsqr
  1927. ldx [%sp+LOCALS64+$S],$a0 ! forward load
  1928. mov $a1,$acc1
  1929. ldx [%sp+LOCALS64+$S+8],$a1
  1930. mov $a2,$acc2
  1931. ldx [%sp+LOCALS64+$S+16],$a2
  1932. mov $a3,$acc3
  1933. ldx [%sp+LOCALS64+$S+24],$a3
  1934. add %sp,LOCALS64+$in_x,$bp
  1935. call __ecp_nistz256_sub_morf_vis3 ! p256_sub(Zsqr, in_x, Zsqr);
  1936. add %sp,LOCALS64+$Zsqr,$rp
  1937. call __ecp_nistz256_sqr_mont_vis3 ! p256_sqr_mont(S, S);
  1938. add %sp,LOCALS64+$S,$rp
  1939. ldx [%sp+LOCALS64+$in_z],$bi
  1940. ldx [%sp+LOCALS64+$in_y],$a0
  1941. ldx [%sp+LOCALS64+$in_y+8],$a1
  1942. ldx [%sp+LOCALS64+$in_y+16],$a2
  1943. ldx [%sp+LOCALS64+$in_y+24],$a3
  1944. add %sp,LOCALS64+$in_z,$bp
  1945. call __ecp_nistz256_mul_mont_vis3 ! p256_mul_mont(tmp0, in_z, in_y);
  1946. add %sp,LOCALS64+$tmp0,$rp
  1947. ldx [%sp+LOCALS64+$M],$bi ! forward load
  1948. ldx [%sp+LOCALS64+$Zsqr],$a0
  1949. ldx [%sp+LOCALS64+$Zsqr+8],$a1
  1950. ldx [%sp+LOCALS64+$Zsqr+16],$a2
  1951. ldx [%sp+LOCALS64+$Zsqr+24],$a3
  1952. call __ecp_nistz256_mul_by_2_vis3 ! p256_mul_by_2(res_z, tmp0);
  1953. add %sp,LOCALS64+$res_z,$rp
  1954. add %sp,LOCALS64+$M,$bp
  1955. call __ecp_nistz256_mul_mont_vis3 ! p256_mul_mont(M, M, Zsqr);
  1956. add %sp,LOCALS64+$M,$rp
  1957. mov $acc0,$a0 ! put aside M
  1958. mov $acc1,$a1
  1959. mov $acc2,$a2
  1960. mov $acc3,$a3
  1961. call __ecp_nistz256_mul_by_2_vis3
  1962. add %sp,LOCALS64+$M,$rp
  1963. mov $a0,$t0 ! copy M
  1964. ldx [%sp+LOCALS64+$S],$a0 ! forward load
  1965. mov $a1,$t1
  1966. ldx [%sp+LOCALS64+$S+8],$a1
  1967. mov $a2,$t2
  1968. ldx [%sp+LOCALS64+$S+16],$a2
  1969. mov $a3,$t3
  1970. ldx [%sp+LOCALS64+$S+24],$a3
  1971. call __ecp_nistz256_add_noload_vis3 ! p256_mul_by_3(M, M);
  1972. add %sp,LOCALS64+$M,$rp
  1973. call __ecp_nistz256_sqr_mont_vis3 ! p256_sqr_mont(tmp0, S);
  1974. add %sp,LOCALS64+$tmp0,$rp
  1975. ldx [%sp+LOCALS64+$S],$bi ! forward load
  1976. ldx [%sp+LOCALS64+$in_x],$a0
  1977. ldx [%sp+LOCALS64+$in_x+8],$a1
  1978. ldx [%sp+LOCALS64+$in_x+16],$a2
  1979. ldx [%sp+LOCALS64+$in_x+24],$a3
  1980. call __ecp_nistz256_div_by_2_vis3 ! p256_div_by_2(res_y, tmp0);
  1981. add %sp,LOCALS64+$res_y,$rp
  1982. add %sp,LOCALS64+$S,$bp
  1983. call __ecp_nistz256_mul_mont_vis3 ! p256_mul_mont(S, S, in_x);
  1984. add %sp,LOCALS64+$S,$rp
  1985. ldx [%sp+LOCALS64+$M],$a0 ! forward load
  1986. ldx [%sp+LOCALS64+$M+8],$a1
  1987. ldx [%sp+LOCALS64+$M+16],$a2
  1988. ldx [%sp+LOCALS64+$M+24],$a3
  1989. call __ecp_nistz256_mul_by_2_vis3 ! p256_mul_by_2(tmp0, S);
  1990. add %sp,LOCALS64+$tmp0,$rp
  1991. call __ecp_nistz256_sqr_mont_vis3 ! p256_sqr_mont(res_x, M);
  1992. add %sp,LOCALS64+$res_x,$rp
  1993. add %sp,LOCALS64+$tmp0,$bp
  1994. call __ecp_nistz256_sub_from_vis3 ! p256_sub(res_x, res_x, tmp0);
  1995. add %sp,LOCALS64+$res_x,$rp
  1996. ldx [%sp+LOCALS64+$M],$a0 ! forward load
  1997. ldx [%sp+LOCALS64+$M+8],$a1
  1998. ldx [%sp+LOCALS64+$M+16],$a2
  1999. ldx [%sp+LOCALS64+$M+24],$a3
  2000. add %sp,LOCALS64+$S,$bp
  2001. call __ecp_nistz256_sub_morf_vis3 ! p256_sub(S, S, res_x);
  2002. add %sp,LOCALS64+$S,$rp
  2003. mov $acc0,$bi
  2004. call __ecp_nistz256_mul_mont_vis3 ! p256_mul_mont(S, S, M);
  2005. add %sp,LOCALS64+$S,$rp
  2006. ldx [%sp+LOCALS64+$res_x],$a0 ! forward load
  2007. ldx [%sp+LOCALS64+$res_x+8],$a1
  2008. ldx [%sp+LOCALS64+$res_x+16],$a2
  2009. ldx [%sp+LOCALS64+$res_x+24],$a3
  2010. add %sp,LOCALS64+$res_y,$bp
  2011. call __ecp_nistz256_sub_from_vis3 ! p256_sub(res_y, S, res_y);
  2012. add %sp,LOCALS64+$res_y,$bp
  2013. ! convert output to uint_32[8]
  2014. srlx $a0,32,$t0
  2015. srlx $a1,32,$t1
  2016. st $a0,[$rp_real] ! res_x
  2017. srlx $a2,32,$t2
  2018. st $t0,[$rp_real+4]
  2019. srlx $a3,32,$t3
  2020. st $a1,[$rp_real+8]
  2021. st $t1,[$rp_real+12]
  2022. st $a2,[$rp_real+16]
  2023. st $t2,[$rp_real+20]
  2024. st $a3,[$rp_real+24]
  2025. st $t3,[$rp_real+28]
  2026. ldx [%sp+LOCALS64+$res_z],$a0 ! forward load
  2027. srlx $acc0,32,$t0
  2028. ldx [%sp+LOCALS64+$res_z+8],$a1
  2029. srlx $acc1,32,$t1
  2030. ldx [%sp+LOCALS64+$res_z+16],$a2
  2031. srlx $acc2,32,$t2
  2032. ldx [%sp+LOCALS64+$res_z+24],$a3
  2033. srlx $acc3,32,$t3
  2034. st $acc0,[$rp_real+32] ! res_y
  2035. st $t0, [$rp_real+32+4]
  2036. st $acc1,[$rp_real+32+8]
  2037. st $t1, [$rp_real+32+12]
  2038. st $acc2,[$rp_real+32+16]
  2039. st $t2, [$rp_real+32+20]
  2040. st $acc3,[$rp_real+32+24]
  2041. st $t3, [$rp_real+32+28]
  2042. srlx $a0,32,$t0
  2043. srlx $a1,32,$t1
  2044. st $a0,[$rp_real+64] ! res_z
  2045. srlx $a2,32,$t2
  2046. st $t0,[$rp_real+64+4]
  2047. srlx $a3,32,$t3
  2048. st $a1,[$rp_real+64+8]
  2049. st $t1,[$rp_real+64+12]
  2050. st $a2,[$rp_real+64+16]
  2051. st $t2,[$rp_real+64+20]
  2052. st $a3,[$rp_real+64+24]
  2053. st $t3,[$rp_real+64+28]
  2054. ret
  2055. restore
  2056. .type ecp_nistz256_point_double_vis3,#function
  2057. .size ecp_nistz256_point_double_vis3,.-ecp_nistz256_point_double_vis3
  2058. ___
  2059. }
  2060. ########################################################################
  2061. # void ecp_nistz256_point_add(P256_POINT *out,const P256_POINT *in1,
  2062. # const P256_POINT *in2);
  2063. {
  2064. my ($res_x,$res_y,$res_z,
  2065. $in1_x,$in1_y,$in1_z,
  2066. $in2_x,$in2_y,$in2_z,
  2067. $H,$Hsqr,$R,$Rsqr,$Hcub,
  2068. $U1,$U2,$S1,$S2)=map(32*$_,(0..17));
  2069. my ($Z1sqr, $Z2sqr) = ($Hsqr, $Rsqr);
  2070. # above map() describes stack layout with 18 temporary
  2071. # 256-bit vectors on top. Then we reserve some space for
  2072. # !in1infty, !in2infty and result of check for zero.
  2073. $code.=<<___;
  2074. .align 32
  2075. ecp_nistz256_point_add_vis3:
  2076. save %sp,-STACK64_FRAME-32*18-32,%sp
  2077. mov $rp,$rp_real
  2078. mov -1,$minus1
  2079. mov -2,$poly3
  2080. sllx $minus1,32,$poly1 ! 0xFFFFFFFF00000000
  2081. srl $poly3,0,$poly3 ! 0x00000000FFFFFFFE
  2082. ! convert input to uint64_t[4]
  2083. ld [$bp],$a0 ! in2_x
  2084. ld [$bp+4],$t0
  2085. ld [$bp+8],$a1
  2086. ld [$bp+12],$t1
  2087. ld [$bp+16],$a2
  2088. ld [$bp+20],$t2
  2089. ld [$bp+24],$a3
  2090. ld [$bp+28],$t3
  2091. sllx $t0,32,$t0
  2092. sllx $t1,32,$t1
  2093. ld [$bp+32],$acc0 ! in2_y
  2094. or $a0,$t0,$a0
  2095. ld [$bp+32+4],$t0
  2096. sllx $t2,32,$t2
  2097. ld [$bp+32+8],$acc1
  2098. or $a1,$t1,$a1
  2099. ld [$bp+32+12],$t1
  2100. sllx $t3,32,$t3
  2101. ld [$bp+32+16],$acc2
  2102. or $a2,$t2,$a2
  2103. ld [$bp+32+20],$t2
  2104. or $a3,$t3,$a3
  2105. ld [$bp+32+24],$acc3
  2106. sllx $t0,32,$t0
  2107. ld [$bp+32+28],$t3
  2108. sllx $t1,32,$t1
  2109. stx $a0,[%sp+LOCALS64+$in2_x]
  2110. sllx $t2,32,$t2
  2111. stx $a1,[%sp+LOCALS64+$in2_x+8]
  2112. sllx $t3,32,$t3
  2113. stx $a2,[%sp+LOCALS64+$in2_x+16]
  2114. or $acc0,$t0,$acc0
  2115. stx $a3,[%sp+LOCALS64+$in2_x+24]
  2116. or $acc1,$t1,$acc1
  2117. stx $acc0,[%sp+LOCALS64+$in2_y]
  2118. or $acc2,$t2,$acc2
  2119. stx $acc1,[%sp+LOCALS64+$in2_y+8]
  2120. or $acc3,$t3,$acc3
  2121. stx $acc2,[%sp+LOCALS64+$in2_y+16]
  2122. stx $acc3,[%sp+LOCALS64+$in2_y+24]
  2123. ld [$bp+64],$acc0 ! in2_z
  2124. ld [$bp+64+4],$t0
  2125. ld [$bp+64+8],$acc1
  2126. ld [$bp+64+12],$t1
  2127. ld [$bp+64+16],$acc2
  2128. ld [$bp+64+20],$t2
  2129. ld [$bp+64+24],$acc3
  2130. ld [$bp+64+28],$t3
  2131. sllx $t0,32,$t0
  2132. sllx $t1,32,$t1
  2133. ld [$ap],$a0 ! in1_x
  2134. or $acc0,$t0,$acc0
  2135. ld [$ap+4],$t0
  2136. sllx $t2,32,$t2
  2137. ld [$ap+8],$a1
  2138. or $acc1,$t1,$acc1
  2139. ld [$ap+12],$t1
  2140. sllx $t3,32,$t3
  2141. ld [$ap+16],$a2
  2142. or $acc2,$t2,$acc2
  2143. ld [$ap+20],$t2
  2144. or $acc3,$t3,$acc3
  2145. ld [$ap+24],$a3
  2146. sllx $t0,32,$t0
  2147. ld [$ap+28],$t3
  2148. sllx $t1,32,$t1
  2149. stx $acc0,[%sp+LOCALS64+$in2_z]
  2150. sllx $t2,32,$t2
  2151. stx $acc1,[%sp+LOCALS64+$in2_z+8]
  2152. sllx $t3,32,$t3
  2153. stx $acc2,[%sp+LOCALS64+$in2_z+16]
  2154. stx $acc3,[%sp+LOCALS64+$in2_z+24]
  2155. or $acc1,$acc0,$acc0
  2156. or $acc3,$acc2,$acc2
  2157. or $acc2,$acc0,$acc0
  2158. movrnz $acc0,-1,$acc0 ! !in2infty
  2159. stx $acc0,[%fp+STACK_BIAS-8]
  2160. or $a0,$t0,$a0
  2161. ld [$ap+32],$acc0 ! in1_y
  2162. or $a1,$t1,$a1
  2163. ld [$ap+32+4],$t0
  2164. or $a2,$t2,$a2
  2165. ld [$ap+32+8],$acc1
  2166. or $a3,$t3,$a3
  2167. ld [$ap+32+12],$t1
  2168. ld [$ap+32+16],$acc2
  2169. ld [$ap+32+20],$t2
  2170. ld [$ap+32+24],$acc3
  2171. sllx $t0,32,$t0
  2172. ld [$ap+32+28],$t3
  2173. sllx $t1,32,$t1
  2174. stx $a0,[%sp+LOCALS64+$in1_x]
  2175. sllx $t2,32,$t2
  2176. stx $a1,[%sp+LOCALS64+$in1_x+8]
  2177. sllx $t3,32,$t3
  2178. stx $a2,[%sp+LOCALS64+$in1_x+16]
  2179. or $acc0,$t0,$acc0
  2180. stx $a3,[%sp+LOCALS64+$in1_x+24]
  2181. or $acc1,$t1,$acc1
  2182. stx $acc0,[%sp+LOCALS64+$in1_y]
  2183. or $acc2,$t2,$acc2
  2184. stx $acc1,[%sp+LOCALS64+$in1_y+8]
  2185. or $acc3,$t3,$acc3
  2186. stx $acc2,[%sp+LOCALS64+$in1_y+16]
  2187. stx $acc3,[%sp+LOCALS64+$in1_y+24]
  2188. ldx [%sp+LOCALS64+$in2_z],$a0 ! forward load
  2189. ldx [%sp+LOCALS64+$in2_z+8],$a1
  2190. ldx [%sp+LOCALS64+$in2_z+16],$a2
  2191. ldx [%sp+LOCALS64+$in2_z+24],$a3
  2192. ld [$ap+64],$acc0 ! in1_z
  2193. ld [$ap+64+4],$t0
  2194. ld [$ap+64+8],$acc1
  2195. ld [$ap+64+12],$t1
  2196. ld [$ap+64+16],$acc2
  2197. ld [$ap+64+20],$t2
  2198. ld [$ap+64+24],$acc3
  2199. ld [$ap+64+28],$t3
  2200. sllx $t0,32,$t0
  2201. sllx $t1,32,$t1
  2202. or $acc0,$t0,$acc0
  2203. sllx $t2,32,$t2
  2204. or $acc1,$t1,$acc1
  2205. sllx $t3,32,$t3
  2206. stx $acc0,[%sp+LOCALS64+$in1_z]
  2207. or $acc2,$t2,$acc2
  2208. stx $acc1,[%sp+LOCALS64+$in1_z+8]
  2209. or $acc3,$t3,$acc3
  2210. stx $acc2,[%sp+LOCALS64+$in1_z+16]
  2211. stx $acc3,[%sp+LOCALS64+$in1_z+24]
  2212. or $acc1,$acc0,$acc0
  2213. or $acc3,$acc2,$acc2
  2214. or $acc2,$acc0,$acc0
  2215. movrnz $acc0,-1,$acc0 ! !in1infty
  2216. stx $acc0,[%fp+STACK_BIAS-16]
  2217. call __ecp_nistz256_sqr_mont_vis3 ! p256_sqr_mont(Z2sqr, in2_z);
  2218. add %sp,LOCALS64+$Z2sqr,$rp
  2219. ldx [%sp+LOCALS64+$in1_z],$a0
  2220. ldx [%sp+LOCALS64+$in1_z+8],$a1
  2221. ldx [%sp+LOCALS64+$in1_z+16],$a2
  2222. ldx [%sp+LOCALS64+$in1_z+24],$a3
  2223. call __ecp_nistz256_sqr_mont_vis3 ! p256_sqr_mont(Z1sqr, in1_z);
  2224. add %sp,LOCALS64+$Z1sqr,$rp
  2225. ldx [%sp+LOCALS64+$Z2sqr],$bi
  2226. ldx [%sp+LOCALS64+$in2_z],$a0
  2227. ldx [%sp+LOCALS64+$in2_z+8],$a1
  2228. ldx [%sp+LOCALS64+$in2_z+16],$a2
  2229. ldx [%sp+LOCALS64+$in2_z+24],$a3
  2230. add %sp,LOCALS64+$Z2sqr,$bp
  2231. call __ecp_nistz256_mul_mont_vis3 ! p256_mul_mont(S1, Z2sqr, in2_z);
  2232. add %sp,LOCALS64+$S1,$rp
  2233. ldx [%sp+LOCALS64+$Z1sqr],$bi
  2234. ldx [%sp+LOCALS64+$in1_z],$a0
  2235. ldx [%sp+LOCALS64+$in1_z+8],$a1
  2236. ldx [%sp+LOCALS64+$in1_z+16],$a2
  2237. ldx [%sp+LOCALS64+$in1_z+24],$a3
  2238. add %sp,LOCALS64+$Z1sqr,$bp
  2239. call __ecp_nistz256_mul_mont_vis3 ! p256_mul_mont(S2, Z1sqr, in1_z);
  2240. add %sp,LOCALS64+$S2,$rp
  2241. ldx [%sp+LOCALS64+$S1],$bi
  2242. ldx [%sp+LOCALS64+$in1_y],$a0
  2243. ldx [%sp+LOCALS64+$in1_y+8],$a1
  2244. ldx [%sp+LOCALS64+$in1_y+16],$a2
  2245. ldx [%sp+LOCALS64+$in1_y+24],$a3
  2246. add %sp,LOCALS64+$S1,$bp
  2247. call __ecp_nistz256_mul_mont_vis3 ! p256_mul_mont(S1, S1, in1_y);
  2248. add %sp,LOCALS64+$S1,$rp
  2249. ldx [%sp+LOCALS64+$S2],$bi
  2250. ldx [%sp+LOCALS64+$in2_y],$a0
  2251. ldx [%sp+LOCALS64+$in2_y+8],$a1
  2252. ldx [%sp+LOCALS64+$in2_y+16],$a2
  2253. ldx [%sp+LOCALS64+$in2_y+24],$a3
  2254. add %sp,LOCALS64+$S2,$bp
  2255. call __ecp_nistz256_mul_mont_vis3 ! p256_mul_mont(S2, S2, in2_y);
  2256. add %sp,LOCALS64+$S2,$rp
  2257. ldx [%sp+LOCALS64+$Z2sqr],$bi ! forward load
  2258. ldx [%sp+LOCALS64+$in1_x],$a0
  2259. ldx [%sp+LOCALS64+$in1_x+8],$a1
  2260. ldx [%sp+LOCALS64+$in1_x+16],$a2
  2261. ldx [%sp+LOCALS64+$in1_x+24],$a3
  2262. add %sp,LOCALS64+$S1,$bp
  2263. call __ecp_nistz256_sub_from_vis3 ! p256_sub(R, S2, S1);
  2264. add %sp,LOCALS64+$R,$rp
  2265. or $acc1,$acc0,$acc0 ! see if result is zero
  2266. or $acc3,$acc2,$acc2
  2267. or $acc2,$acc0,$acc0
  2268. stx $acc0,[%fp+STACK_BIAS-24]
  2269. add %sp,LOCALS64+$Z2sqr,$bp
  2270. call __ecp_nistz256_mul_mont_vis3 ! p256_mul_mont(U1, in1_x, Z2sqr);
  2271. add %sp,LOCALS64+$U1,$rp
  2272. ldx [%sp+LOCALS64+$Z1sqr],$bi
  2273. ldx [%sp+LOCALS64+$in2_x],$a0
  2274. ldx [%sp+LOCALS64+$in2_x+8],$a1
  2275. ldx [%sp+LOCALS64+$in2_x+16],$a2
  2276. ldx [%sp+LOCALS64+$in2_x+24],$a3
  2277. add %sp,LOCALS64+$Z1sqr,$bp
  2278. call __ecp_nistz256_mul_mont_vis3 ! p256_mul_mont(U2, in2_x, Z1sqr);
  2279. add %sp,LOCALS64+$U2,$rp
  2280. ldx [%sp+LOCALS64+$R],$a0 ! forward load
  2281. ldx [%sp+LOCALS64+$R+8],$a1
  2282. ldx [%sp+LOCALS64+$R+16],$a2
  2283. ldx [%sp+LOCALS64+$R+24],$a3
  2284. add %sp,LOCALS64+$U1,$bp
  2285. call __ecp_nistz256_sub_from_vis3 ! p256_sub(H, U2, U1);
  2286. add %sp,LOCALS64+$H,$rp
  2287. or $acc1,$acc0,$acc0 ! see if result is zero
  2288. or $acc3,$acc2,$acc2
  2289. orcc $acc2,$acc0,$acc0
  2290. bne,pt %xcc,.Ladd_proceed_vis3 ! is_equal(U1,U2)?
  2291. nop
  2292. ldx [%fp+STACK_BIAS-8],$t0
  2293. ldx [%fp+STACK_BIAS-16],$t1
  2294. ldx [%fp+STACK_BIAS-24],$t2
  2295. andcc $t0,$t1,%g0
  2296. be,pt %xcc,.Ladd_proceed_vis3 ! (in1infty || in2infty)?
  2297. nop
  2298. andcc $t2,$t2,%g0
  2299. be,a,pt %xcc,.Ldouble_shortcut_vis3 ! is_equal(S1,S2)?
  2300. add %sp,32*(12-10)+32,%sp ! difference in frame sizes
  2301. st %g0,[$rp_real]
  2302. st %g0,[$rp_real+4]
  2303. st %g0,[$rp_real+8]
  2304. st %g0,[$rp_real+12]
  2305. st %g0,[$rp_real+16]
  2306. st %g0,[$rp_real+20]
  2307. st %g0,[$rp_real+24]
  2308. st %g0,[$rp_real+28]
  2309. st %g0,[$rp_real+32]
  2310. st %g0,[$rp_real+32+4]
  2311. st %g0,[$rp_real+32+8]
  2312. st %g0,[$rp_real+32+12]
  2313. st %g0,[$rp_real+32+16]
  2314. st %g0,[$rp_real+32+20]
  2315. st %g0,[$rp_real+32+24]
  2316. st %g0,[$rp_real+32+28]
  2317. st %g0,[$rp_real+64]
  2318. st %g0,[$rp_real+64+4]
  2319. st %g0,[$rp_real+64+8]
  2320. st %g0,[$rp_real+64+12]
  2321. st %g0,[$rp_real+64+16]
  2322. st %g0,[$rp_real+64+20]
  2323. st %g0,[$rp_real+64+24]
  2324. st %g0,[$rp_real+64+28]
  2325. b .Ladd_done_vis3
  2326. nop
  2327. .align 16
  2328. .Ladd_proceed_vis3:
  2329. call __ecp_nistz256_sqr_mont_vis3 ! p256_sqr_mont(Rsqr, R);
  2330. add %sp,LOCALS64+$Rsqr,$rp
  2331. ldx [%sp+LOCALS64+$H],$bi
  2332. ldx [%sp+LOCALS64+$in1_z],$a0
  2333. ldx [%sp+LOCALS64+$in1_z+8],$a1
  2334. ldx [%sp+LOCALS64+$in1_z+16],$a2
  2335. ldx [%sp+LOCALS64+$in1_z+24],$a3
  2336. add %sp,LOCALS64+$H,$bp
  2337. call __ecp_nistz256_mul_mont_vis3 ! p256_mul_mont(res_z, H, in1_z);
  2338. add %sp,LOCALS64+$res_z,$rp
  2339. ldx [%sp+LOCALS64+$H],$a0
  2340. ldx [%sp+LOCALS64+$H+8],$a1
  2341. ldx [%sp+LOCALS64+$H+16],$a2
  2342. ldx [%sp+LOCALS64+$H+24],$a3
  2343. call __ecp_nistz256_sqr_mont_vis3 ! p256_sqr_mont(Hsqr, H);
  2344. add %sp,LOCALS64+$Hsqr,$rp
  2345. ldx [%sp+LOCALS64+$res_z],$bi
  2346. ldx [%sp+LOCALS64+$in2_z],$a0
  2347. ldx [%sp+LOCALS64+$in2_z+8],$a1
  2348. ldx [%sp+LOCALS64+$in2_z+16],$a2
  2349. ldx [%sp+LOCALS64+$in2_z+24],$a3
  2350. add %sp,LOCALS64+$res_z,$bp
  2351. call __ecp_nistz256_mul_mont_vis3 ! p256_mul_mont(res_z, res_z, in2_z);
  2352. add %sp,LOCALS64+$res_z,$rp
  2353. ldx [%sp+LOCALS64+$H],$bi
  2354. ldx [%sp+LOCALS64+$Hsqr],$a0
  2355. ldx [%sp+LOCALS64+$Hsqr+8],$a1
  2356. ldx [%sp+LOCALS64+$Hsqr+16],$a2
  2357. ldx [%sp+LOCALS64+$Hsqr+24],$a3
  2358. add %sp,LOCALS64+$H,$bp
  2359. call __ecp_nistz256_mul_mont_vis3 ! p256_mul_mont(Hcub, Hsqr, H);
  2360. add %sp,LOCALS64+$Hcub,$rp
  2361. ldx [%sp+LOCALS64+$U1],$bi
  2362. ldx [%sp+LOCALS64+$Hsqr],$a0
  2363. ldx [%sp+LOCALS64+$Hsqr+8],$a1
  2364. ldx [%sp+LOCALS64+$Hsqr+16],$a2
  2365. ldx [%sp+LOCALS64+$Hsqr+24],$a3
  2366. add %sp,LOCALS64+$U1,$bp
  2367. call __ecp_nistz256_mul_mont_vis3 ! p256_mul_mont(U2, U1, Hsqr);
  2368. add %sp,LOCALS64+$U2,$rp
  2369. call __ecp_nistz256_mul_by_2_vis3 ! p256_mul_by_2(Hsqr, U2);
  2370. add %sp,LOCALS64+$Hsqr,$rp
  2371. add %sp,LOCALS64+$Rsqr,$bp
  2372. call __ecp_nistz256_sub_morf_vis3 ! p256_sub(res_x, Rsqr, Hsqr);
  2373. add %sp,LOCALS64+$res_x,$rp
  2374. add %sp,LOCALS64+$Hcub,$bp
  2375. call __ecp_nistz256_sub_from_vis3 ! p256_sub(res_x, res_x, Hcub);
  2376. add %sp,LOCALS64+$res_x,$rp
  2377. ldx [%sp+LOCALS64+$S1],$bi ! forward load
  2378. ldx [%sp+LOCALS64+$Hcub],$a0
  2379. ldx [%sp+LOCALS64+$Hcub+8],$a1
  2380. ldx [%sp+LOCALS64+$Hcub+16],$a2
  2381. ldx [%sp+LOCALS64+$Hcub+24],$a3
  2382. add %sp,LOCALS64+$U2,$bp
  2383. call __ecp_nistz256_sub_morf_vis3 ! p256_sub(res_y, U2, res_x);
  2384. add %sp,LOCALS64+$res_y,$rp
  2385. add %sp,LOCALS64+$S1,$bp
  2386. call __ecp_nistz256_mul_mont_vis3 ! p256_mul_mont(S2, S1, Hcub);
  2387. add %sp,LOCALS64+$S2,$rp
  2388. ldx [%sp+LOCALS64+$R],$bi
  2389. ldx [%sp+LOCALS64+$res_y],$a0
  2390. ldx [%sp+LOCALS64+$res_y+8],$a1
  2391. ldx [%sp+LOCALS64+$res_y+16],$a2
  2392. ldx [%sp+LOCALS64+$res_y+24],$a3
  2393. add %sp,LOCALS64+$R,$bp
  2394. call __ecp_nistz256_mul_mont_vis3 ! p256_mul_mont(res_y, res_y, R);
  2395. add %sp,LOCALS64+$res_y,$rp
  2396. add %sp,LOCALS64+$S2,$bp
  2397. call __ecp_nistz256_sub_from_vis3 ! p256_sub(res_y, res_y, S2);
  2398. add %sp,LOCALS64+$res_y,$rp
  2399. ldx [%fp+STACK_BIAS-16],$t1 ! !in1infty
  2400. ldx [%fp+STACK_BIAS-8],$t2 ! !in2infty
  2401. ___
  2402. for($i=0;$i<96;$i+=16) { # conditional moves
  2403. $code.=<<___;
  2404. ldx [%sp+LOCALS64+$res_x+$i],$acc0 ! res
  2405. ldx [%sp+LOCALS64+$res_x+$i+8],$acc1
  2406. ldx [%sp+LOCALS64+$in2_x+$i],$acc2 ! in2
  2407. ldx [%sp+LOCALS64+$in2_x+$i+8],$acc3
  2408. ldx [%sp+LOCALS64+$in1_x+$i],$acc4 ! in1
  2409. ldx [%sp+LOCALS64+$in1_x+$i+8],$acc5
  2410. movrz $t1,$acc2,$acc0
  2411. movrz $t1,$acc3,$acc1
  2412. movrz $t2,$acc4,$acc0
  2413. movrz $t2,$acc5,$acc1
  2414. srlx $acc0,32,$acc2
  2415. srlx $acc1,32,$acc3
  2416. st $acc0,[$rp_real+$i]
  2417. st $acc2,[$rp_real+$i+4]
  2418. st $acc1,[$rp_real+$i+8]
  2419. st $acc3,[$rp_real+$i+12]
  2420. ___
  2421. }
  2422. $code.=<<___;
  2423. .Ladd_done_vis3:
  2424. ret
  2425. restore
  2426. .type ecp_nistz256_point_add_vis3,#function
  2427. .size ecp_nistz256_point_add_vis3,.-ecp_nistz256_point_add_vis3
  2428. ___
  2429. }
  2430. ########################################################################
  2431. # void ecp_nistz256_point_add_affine(P256_POINT *out,const P256_POINT *in1,
  2432. # const P256_POINT_AFFINE *in2);
  2433. {
  2434. my ($res_x,$res_y,$res_z,
  2435. $in1_x,$in1_y,$in1_z,
  2436. $in2_x,$in2_y,
  2437. $U2,$S2,$H,$R,$Hsqr,$Hcub,$Rsqr)=map(32*$_,(0..14));
  2438. my $Z1sqr = $S2;
  2439. # above map() describes stack layout with 15 temporary
  2440. # 256-bit vectors on top. Then we reserve some space for
  2441. # !in1infty and !in2infty.
  2442. $code.=<<___;
  2443. .align 32
  2444. ecp_nistz256_point_add_affine_vis3:
  2445. save %sp,-STACK64_FRAME-32*15-32,%sp
  2446. mov $rp,$rp_real
  2447. mov -1,$minus1
  2448. mov -2,$poly3
  2449. sllx $minus1,32,$poly1 ! 0xFFFFFFFF00000000
  2450. srl $poly3,0,$poly3 ! 0x00000000FFFFFFFE
  2451. ! convert input to uint64_t[4]
  2452. ld [$bp],$a0 ! in2_x
  2453. ld [$bp+4],$t0
  2454. ld [$bp+8],$a1
  2455. ld [$bp+12],$t1
  2456. ld [$bp+16],$a2
  2457. ld [$bp+20],$t2
  2458. ld [$bp+24],$a3
  2459. ld [$bp+28],$t3
  2460. sllx $t0,32,$t0
  2461. sllx $t1,32,$t1
  2462. ld [$bp+32],$acc0 ! in2_y
  2463. or $a0,$t0,$a0
  2464. ld [$bp+32+4],$t0
  2465. sllx $t2,32,$t2
  2466. ld [$bp+32+8],$acc1
  2467. or $a1,$t1,$a1
  2468. ld [$bp+32+12],$t1
  2469. sllx $t3,32,$t3
  2470. ld [$bp+32+16],$acc2
  2471. or $a2,$t2,$a2
  2472. ld [$bp+32+20],$t2
  2473. or $a3,$t3,$a3
  2474. ld [$bp+32+24],$acc3
  2475. sllx $t0,32,$t0
  2476. ld [$bp+32+28],$t3
  2477. sllx $t1,32,$t1
  2478. stx $a0,[%sp+LOCALS64+$in2_x]
  2479. sllx $t2,32,$t2
  2480. stx $a1,[%sp+LOCALS64+$in2_x+8]
  2481. sllx $t3,32,$t3
  2482. stx $a2,[%sp+LOCALS64+$in2_x+16]
  2483. or $acc0,$t0,$acc0
  2484. stx $a3,[%sp+LOCALS64+$in2_x+24]
  2485. or $acc1,$t1,$acc1
  2486. stx $acc0,[%sp+LOCALS64+$in2_y]
  2487. or $acc2,$t2,$acc2
  2488. stx $acc1,[%sp+LOCALS64+$in2_y+8]
  2489. or $acc3,$t3,$acc3
  2490. stx $acc2,[%sp+LOCALS64+$in2_y+16]
  2491. stx $acc3,[%sp+LOCALS64+$in2_y+24]
  2492. or $a1,$a0,$a0
  2493. or $a3,$a2,$a2
  2494. or $acc1,$acc0,$acc0
  2495. or $acc3,$acc2,$acc2
  2496. or $a2,$a0,$a0
  2497. or $acc2,$acc0,$acc0
  2498. or $acc0,$a0,$a0
  2499. movrnz $a0,-1,$a0 ! !in2infty
  2500. stx $a0,[%fp+STACK_BIAS-8]
  2501. ld [$ap],$a0 ! in1_x
  2502. ld [$ap+4],$t0
  2503. ld [$ap+8],$a1
  2504. ld [$ap+12],$t1
  2505. ld [$ap+16],$a2
  2506. ld [$ap+20],$t2
  2507. ld [$ap+24],$a3
  2508. ld [$ap+28],$t3
  2509. sllx $t0,32,$t0
  2510. sllx $t1,32,$t1
  2511. ld [$ap+32],$acc0 ! in1_y
  2512. or $a0,$t0,$a0
  2513. ld [$ap+32+4],$t0
  2514. sllx $t2,32,$t2
  2515. ld [$ap+32+8],$acc1
  2516. or $a1,$t1,$a1
  2517. ld [$ap+32+12],$t1
  2518. sllx $t3,32,$t3
  2519. ld [$ap+32+16],$acc2
  2520. or $a2,$t2,$a2
  2521. ld [$ap+32+20],$t2
  2522. or $a3,$t3,$a3
  2523. ld [$ap+32+24],$acc3
  2524. sllx $t0,32,$t0
  2525. ld [$ap+32+28],$t3
  2526. sllx $t1,32,$t1
  2527. stx $a0,[%sp+LOCALS64+$in1_x]
  2528. sllx $t2,32,$t2
  2529. stx $a1,[%sp+LOCALS64+$in1_x+8]
  2530. sllx $t3,32,$t3
  2531. stx $a2,[%sp+LOCALS64+$in1_x+16]
  2532. or $acc0,$t0,$acc0
  2533. stx $a3,[%sp+LOCALS64+$in1_x+24]
  2534. or $acc1,$t1,$acc1
  2535. stx $acc0,[%sp+LOCALS64+$in1_y]
  2536. or $acc2,$t2,$acc2
  2537. stx $acc1,[%sp+LOCALS64+$in1_y+8]
  2538. or $acc3,$t3,$acc3
  2539. stx $acc2,[%sp+LOCALS64+$in1_y+16]
  2540. stx $acc3,[%sp+LOCALS64+$in1_y+24]
  2541. ld [$ap+64],$a0 ! in1_z
  2542. ld [$ap+64+4],$t0
  2543. ld [$ap+64+8],$a1
  2544. ld [$ap+64+12],$t1
  2545. ld [$ap+64+16],$a2
  2546. ld [$ap+64+20],$t2
  2547. ld [$ap+64+24],$a3
  2548. ld [$ap+64+28],$t3
  2549. sllx $t0,32,$t0
  2550. sllx $t1,32,$t1
  2551. or $a0,$t0,$a0
  2552. sllx $t2,32,$t2
  2553. or $a1,$t1,$a1
  2554. sllx $t3,32,$t3
  2555. stx $a0,[%sp+LOCALS64+$in1_z]
  2556. or $a2,$t2,$a2
  2557. stx $a1,[%sp+LOCALS64+$in1_z+8]
  2558. or $a3,$t3,$a3
  2559. stx $a2,[%sp+LOCALS64+$in1_z+16]
  2560. stx $a3,[%sp+LOCALS64+$in1_z+24]
  2561. or $a1,$a0,$t0
  2562. or $a3,$a2,$t2
  2563. or $t2,$t0,$t0
  2564. movrnz $t0,-1,$t0 ! !in1infty
  2565. stx $t0,[%fp+STACK_BIAS-16]
  2566. call __ecp_nistz256_sqr_mont_vis3 ! p256_sqr_mont(Z1sqr, in1_z);
  2567. add %sp,LOCALS64+$Z1sqr,$rp
  2568. ldx [%sp+LOCALS64+$in2_x],$bi
  2569. mov $acc0,$a0
  2570. mov $acc1,$a1
  2571. mov $acc2,$a2
  2572. mov $acc3,$a3
  2573. add %sp,LOCALS64+$in2_x,$bp
  2574. call __ecp_nistz256_mul_mont_vis3 ! p256_mul_mont(U2, Z1sqr, in2_x);
  2575. add %sp,LOCALS64+$U2,$rp
  2576. ldx [%sp+LOCALS64+$Z1sqr],$bi ! forward load
  2577. ldx [%sp+LOCALS64+$in1_z],$a0
  2578. ldx [%sp+LOCALS64+$in1_z+8],$a1
  2579. ldx [%sp+LOCALS64+$in1_z+16],$a2
  2580. ldx [%sp+LOCALS64+$in1_z+24],$a3
  2581. add %sp,LOCALS64+$in1_x,$bp
  2582. call __ecp_nistz256_sub_from_vis3 ! p256_sub(H, U2, in1_x);
  2583. add %sp,LOCALS64+$H,$rp
  2584. add %sp,LOCALS64+$Z1sqr,$bp
  2585. call __ecp_nistz256_mul_mont_vis3 ! p256_mul_mont(S2, Z1sqr, in1_z);
  2586. add %sp,LOCALS64+$S2,$rp
  2587. ldx [%sp+LOCALS64+$H],$bi
  2588. ldx [%sp+LOCALS64+$in1_z],$a0
  2589. ldx [%sp+LOCALS64+$in1_z+8],$a1
  2590. ldx [%sp+LOCALS64+$in1_z+16],$a2
  2591. ldx [%sp+LOCALS64+$in1_z+24],$a3
  2592. add %sp,LOCALS64+$H,$bp
  2593. call __ecp_nistz256_mul_mont_vis3 ! p256_mul_mont(res_z, H, in1_z);
  2594. add %sp,LOCALS64+$res_z,$rp
  2595. ldx [%sp+LOCALS64+$S2],$bi
  2596. ldx [%sp+LOCALS64+$in2_y],$a0
  2597. ldx [%sp+LOCALS64+$in2_y+8],$a1
  2598. ldx [%sp+LOCALS64+$in2_y+16],$a2
  2599. ldx [%sp+LOCALS64+$in2_y+24],$a3
  2600. add %sp,LOCALS64+$S2,$bp
  2601. call __ecp_nistz256_mul_mont_vis3 ! p256_mul_mont(S2, S2, in2_y);
  2602. add %sp,LOCALS64+$S2,$rp
  2603. ldx [%sp+LOCALS64+$H],$a0 ! forward load
  2604. ldx [%sp+LOCALS64+$H+8],$a1
  2605. ldx [%sp+LOCALS64+$H+16],$a2
  2606. ldx [%sp+LOCALS64+$H+24],$a3
  2607. add %sp,LOCALS64+$in1_y,$bp
  2608. call __ecp_nistz256_sub_from_vis3 ! p256_sub(R, S2, in1_y);
  2609. add %sp,LOCALS64+$R,$rp
  2610. call __ecp_nistz256_sqr_mont_vis3 ! p256_sqr_mont(Hsqr, H);
  2611. add %sp,LOCALS64+$Hsqr,$rp
  2612. ldx [%sp+LOCALS64+$R],$a0
  2613. ldx [%sp+LOCALS64+$R+8],$a1
  2614. ldx [%sp+LOCALS64+$R+16],$a2
  2615. ldx [%sp+LOCALS64+$R+24],$a3
  2616. call __ecp_nistz256_sqr_mont_vis3 ! p256_sqr_mont(Rsqr, R);
  2617. add %sp,LOCALS64+$Rsqr,$rp
  2618. ldx [%sp+LOCALS64+$H],$bi
  2619. ldx [%sp+LOCALS64+$Hsqr],$a0
  2620. ldx [%sp+LOCALS64+$Hsqr+8],$a1
  2621. ldx [%sp+LOCALS64+$Hsqr+16],$a2
  2622. ldx [%sp+LOCALS64+$Hsqr+24],$a3
  2623. add %sp,LOCALS64+$H,$bp
  2624. call __ecp_nistz256_mul_mont_vis3 ! p256_mul_mont(Hcub, Hsqr, H);
  2625. add %sp,LOCALS64+$Hcub,$rp
  2626. ldx [%sp+LOCALS64+$Hsqr],$bi
  2627. ldx [%sp+LOCALS64+$in1_x],$a0
  2628. ldx [%sp+LOCALS64+$in1_x+8],$a1
  2629. ldx [%sp+LOCALS64+$in1_x+16],$a2
  2630. ldx [%sp+LOCALS64+$in1_x+24],$a3
  2631. add %sp,LOCALS64+$Hsqr,$bp
  2632. call __ecp_nistz256_mul_mont_vis3 ! p256_mul_mont(U2, in1_x, Hsqr);
  2633. add %sp,LOCALS64+$U2,$rp
  2634. call __ecp_nistz256_mul_by_2_vis3 ! p256_mul_by_2(Hsqr, U2);
  2635. add %sp,LOCALS64+$Hsqr,$rp
  2636. add %sp,LOCALS64+$Rsqr,$bp
  2637. call __ecp_nistz256_sub_morf_vis3 ! p256_sub(res_x, Rsqr, Hsqr);
  2638. add %sp,LOCALS64+$res_x,$rp
  2639. add %sp,LOCALS64+$Hcub,$bp
  2640. call __ecp_nistz256_sub_from_vis3 ! p256_sub(res_x, res_x, Hcub);
  2641. add %sp,LOCALS64+$res_x,$rp
  2642. ldx [%sp+LOCALS64+$Hcub],$bi ! forward load
  2643. ldx [%sp+LOCALS64+$in1_y],$a0
  2644. ldx [%sp+LOCALS64+$in1_y+8],$a1
  2645. ldx [%sp+LOCALS64+$in1_y+16],$a2
  2646. ldx [%sp+LOCALS64+$in1_y+24],$a3
  2647. add %sp,LOCALS64+$U2,$bp
  2648. call __ecp_nistz256_sub_morf_vis3 ! p256_sub(res_y, U2, res_x);
  2649. add %sp,LOCALS64+$res_y,$rp
  2650. add %sp,LOCALS64+$Hcub,$bp
  2651. call __ecp_nistz256_mul_mont_vis3 ! p256_mul_mont(S2, in1_y, Hcub);
  2652. add %sp,LOCALS64+$S2,$rp
  2653. ldx [%sp+LOCALS64+$R],$bi
  2654. ldx [%sp+LOCALS64+$res_y],$a0
  2655. ldx [%sp+LOCALS64+$res_y+8],$a1
  2656. ldx [%sp+LOCALS64+$res_y+16],$a2
  2657. ldx [%sp+LOCALS64+$res_y+24],$a3
  2658. add %sp,LOCALS64+$R,$bp
  2659. call __ecp_nistz256_mul_mont_vis3 ! p256_mul_mont(res_y, res_y, R);
  2660. add %sp,LOCALS64+$res_y,$rp
  2661. add %sp,LOCALS64+$S2,$bp
  2662. call __ecp_nistz256_sub_from_vis3 ! p256_sub(res_y, res_y, S2);
  2663. add %sp,LOCALS64+$res_y,$rp
  2664. ldx [%fp+STACK_BIAS-16],$t1 ! !in1infty
  2665. ldx [%fp+STACK_BIAS-8],$t2 ! !in2infty
  2666. 1: call .+8
  2667. add %o7,.Lone_mont_vis3-1b,$bp
  2668. ___
  2669. for($i=0;$i<64;$i+=16) { # conditional moves
  2670. $code.=<<___;
  2671. ldx [%sp+LOCALS64+$res_x+$i],$acc0 ! res
  2672. ldx [%sp+LOCALS64+$res_x+$i+8],$acc1
  2673. ldx [%sp+LOCALS64+$in2_x+$i],$acc2 ! in2
  2674. ldx [%sp+LOCALS64+$in2_x+$i+8],$acc3
  2675. ldx [%sp+LOCALS64+$in1_x+$i],$acc4 ! in1
  2676. ldx [%sp+LOCALS64+$in1_x+$i+8],$acc5
  2677. movrz $t1,$acc2,$acc0
  2678. movrz $t1,$acc3,$acc1
  2679. movrz $t2,$acc4,$acc0
  2680. movrz $t2,$acc5,$acc1
  2681. srlx $acc0,32,$acc2
  2682. srlx $acc1,32,$acc3
  2683. st $acc0,[$rp_real+$i]
  2684. st $acc2,[$rp_real+$i+4]
  2685. st $acc1,[$rp_real+$i+8]
  2686. st $acc3,[$rp_real+$i+12]
  2687. ___
  2688. }
  2689. for(;$i<96;$i+=16) {
  2690. $code.=<<___;
  2691. ldx [%sp+LOCALS64+$res_x+$i],$acc0 ! res
  2692. ldx [%sp+LOCALS64+$res_x+$i+8],$acc1
  2693. ldx [$bp+$i-64],$acc2 ! "in2"
  2694. ldx [$bp+$i-64+8],$acc3
  2695. ldx [%sp+LOCALS64+$in1_x+$i],$acc4 ! in1
  2696. ldx [%sp+LOCALS64+$in1_x+$i+8],$acc5
  2697. movrz $t1,$acc2,$acc0
  2698. movrz $t1,$acc3,$acc1
  2699. movrz $t2,$acc4,$acc0
  2700. movrz $t2,$acc5,$acc1
  2701. srlx $acc0,32,$acc2
  2702. srlx $acc1,32,$acc3
  2703. st $acc0,[$rp_real+$i]
  2704. st $acc2,[$rp_real+$i+4]
  2705. st $acc1,[$rp_real+$i+8]
  2706. st $acc3,[$rp_real+$i+12]
  2707. ___
  2708. }
  2709. $code.=<<___;
  2710. ret
  2711. restore
  2712. .type ecp_nistz256_point_add_affine_vis3,#function
  2713. .size ecp_nistz256_point_add_affine_vis3,.-ecp_nistz256_point_add_affine_vis3
  2714. .align 64
  2715. .Lone_mont_vis3:
  2716. .long 0x00000000,0x00000001, 0xffffffff,0x00000000
  2717. .long 0xffffffff,0xffffffff, 0x00000000,0xfffffffe
  2718. .align 64
  2719. ___
  2720. } }}}
  2721. # Purpose of these subroutines is to explicitly encode VIS instructions,
  2722. # so that one can compile the module without having to specify VIS
  2723. # extensions on compiler command line, e.g. -xarch=v9 vs. -xarch=v9a.
  2724. # Idea is to reserve for option to produce "universal" binary and let
  2725. # programmer detect if current CPU is VIS capable at run-time.
  2726. sub unvis3 {
  2727. my ($mnemonic,$rs1,$rs2,$rd)=@_;
  2728. my %bias = ( "g" => 0, "o" => 8, "l" => 16, "i" => 24 );
  2729. my ($ref,$opf);
  2730. my %visopf = ( "addxc" => 0x011,
  2731. "addxccc" => 0x013,
  2732. "umulxhi" => 0x016 );
  2733. $ref = "$mnemonic\t$rs1,$rs2,$rd";
  2734. if ($opf=$visopf{$mnemonic}) {
  2735. foreach ($rs1,$rs2,$rd) {
  2736. return $ref if (!/%([goli])([0-9])/);
  2737. $_=$bias{$1}+$2;
  2738. }
  2739. return sprintf ".word\t0x%08x !%s",
  2740. 0x81b00000|$rd<<25|$rs1<<14|$opf<<5|$rs2,
  2741. $ref;
  2742. } else {
  2743. return $ref;
  2744. }
  2745. }
  2746. foreach (split("\n",$code)) {
  2747. s/\`([^\`]*)\`/eval $1/ge;
  2748. s/\b(umulxhi|addxc[c]{0,2})\s+(%[goli][0-7]),\s*(%[goli][0-7]),\s*(%[goli][0-7])/
  2749. &unvis3($1,$2,$3,$4)
  2750. /ge;
  2751. print $_,"\n";
  2752. }
  2753. close STDOUT or die "error closing STDOUT: $!";