2
0

ecp_nistz256-sparcv9.pl 76 KB

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