ecp_nistz256-sparcv9.pl 76 KB

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