2
0

ecp_nistz256-x86_64.pl 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739
  1. #! /usr/bin/env perl
  2. # Copyright 2014-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. # Copyright (c) 2014, Intel Corporation. All Rights Reserved.
  4. # Copyright (c) 2015 CloudFlare, Inc.
  5. #
  6. # Licensed under the OpenSSL license (the "License"). You may not use
  7. # this file except in compliance with the License. You can obtain a copy
  8. # in the file LICENSE in the source distribution or at
  9. # https://www.openssl.org/source/license.html
  10. #
  11. # Originally written by Shay Gueron (1, 2), and Vlad Krasnov (1, 3)
  12. # (1) Intel Corporation, Israel Development Center, Haifa, Israel
  13. # (2) University of Haifa, Israel
  14. # (3) CloudFlare, Inc.
  15. #
  16. # Reference:
  17. # S.Gueron and V.Krasnov, "Fast Prime Field Elliptic Curve Cryptography with
  18. # 256 Bit Primes"
  19. # Further optimization by <appro@openssl.org>:
  20. #
  21. # this/original with/without -DECP_NISTZ256_ASM(*)
  22. # Opteron +15-49% +150-195%
  23. # Bulldozer +18-45% +175-240%
  24. # P4 +24-46% +100-150%
  25. # Westmere +18-34% +87-160%
  26. # Sandy Bridge +14-35% +120-185%
  27. # Ivy Bridge +11-35% +125-180%
  28. # Haswell +10-37% +160-200%
  29. # Broadwell +24-58% +210-270%
  30. # Atom +20-50% +180-240%
  31. # VIA Nano +50-160% +480-480%
  32. #
  33. # (*) "without -DECP_NISTZ256_ASM" refers to build with
  34. # "enable-ec_nistp_64_gcc_128";
  35. #
  36. # Ranges denote minimum and maximum improvement coefficients depending
  37. # on benchmark. In "this/original" column lower coefficient is for
  38. # ECDSA sign, while in "with/without" - for ECDH key agreement, and
  39. # higher - for ECDSA sign, relatively fastest server-side operation.
  40. # Keep in mind that +100% means 2x improvement.
  41. $flavour = shift;
  42. $output = shift;
  43. if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
  44. $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
  45. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  46. ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
  47. ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
  48. die "can't locate x86_64-xlate.pl";
  49. open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\"";
  50. *STDOUT=*OUT;
  51. if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
  52. =~ /GNU assembler version ([2-9]\.[0-9]+)/) {
  53. $avx = ($1>=2.19) + ($1>=2.22);
  54. $addx = ($1>=2.23);
  55. }
  56. if (!$addx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
  57. `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/) {
  58. $avx = ($1>=2.09) + ($1>=2.10);
  59. $addx = ($1>=2.10);
  60. }
  61. if (!$addx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
  62. `ml64 2>&1` =~ /Version ([0-9]+)\./) {
  63. $avx = ($1>=10) + ($1>=11);
  64. $addx = ($1>=12);
  65. }
  66. if (!$addx && `$ENV{CC} -v 2>&1` =~ /((?:clang|LLVM) version|.*based on LLVM) ([0-9]+)\.([0-9]+)/) {
  67. my $ver = $2 + $3/100.0; # 3.1->3.01, 3.10->3.10
  68. $avx = ($ver>=3.0) + ($ver>=3.01);
  69. $addx = ($ver>=3.03);
  70. }
  71. $code.=<<___;
  72. .text
  73. .extern OPENSSL_ia32cap_P
  74. # The polynomial
  75. .align 64
  76. .Lpoly:
  77. .quad 0xffffffffffffffff, 0x00000000ffffffff, 0x0000000000000000, 0xffffffff00000001
  78. # 2^512 mod P precomputed for NIST P256 polynomial
  79. .LRR:
  80. .quad 0x0000000000000003, 0xfffffffbffffffff, 0xfffffffffffffffe, 0x00000004fffffffd
  81. .LOne:
  82. .long 1,1,1,1,1,1,1,1
  83. .LTwo:
  84. .long 2,2,2,2,2,2,2,2
  85. .LThree:
  86. .long 3,3,3,3,3,3,3,3
  87. .LONE_mont:
  88. .quad 0x0000000000000001, 0xffffffff00000000, 0xffffffffffffffff, 0x00000000fffffffe
  89. # Constants for computations modulo ord(p256)
  90. .Lord:
  91. .quad 0xf3b9cac2fc632551, 0xbce6faada7179e84, 0xffffffffffffffff, 0xffffffff00000000
  92. .LordK:
  93. .quad 0xccd1c8aaee00bc4f
  94. ___
  95. {
  96. ################################################################################
  97. # void ecp_nistz256_mul_by_2(uint64_t res[4], uint64_t a[4]);
  98. my ($a0,$a1,$a2,$a3)=map("%r$_",(8..11));
  99. my ($t0,$t1,$t2,$t3,$t4)=("%rax","%rdx","%rcx","%r12","%r13");
  100. my ($r_ptr,$a_ptr,$b_ptr)=("%rdi","%rsi","%rdx");
  101. $code.=<<___;
  102. .globl ecp_nistz256_mul_by_2
  103. .type ecp_nistz256_mul_by_2,\@function,2
  104. .align 64
  105. ecp_nistz256_mul_by_2:
  106. .cfi_startproc
  107. push %r12
  108. .cfi_push %r12
  109. push %r13
  110. .cfi_push %r13
  111. .Lmul_by_2_body:
  112. mov 8*0($a_ptr), $a0
  113. xor $t4,$t4
  114. mov 8*1($a_ptr), $a1
  115. add $a0, $a0 # a0:a3+a0:a3
  116. mov 8*2($a_ptr), $a2
  117. adc $a1, $a1
  118. mov 8*3($a_ptr), $a3
  119. lea .Lpoly(%rip), $a_ptr
  120. mov $a0, $t0
  121. adc $a2, $a2
  122. adc $a3, $a3
  123. mov $a1, $t1
  124. adc \$0, $t4
  125. sub 8*0($a_ptr), $a0
  126. mov $a2, $t2
  127. sbb 8*1($a_ptr), $a1
  128. sbb 8*2($a_ptr), $a2
  129. mov $a3, $t3
  130. sbb 8*3($a_ptr), $a3
  131. sbb \$0, $t4
  132. cmovc $t0, $a0
  133. cmovc $t1, $a1
  134. mov $a0, 8*0($r_ptr)
  135. cmovc $t2, $a2
  136. mov $a1, 8*1($r_ptr)
  137. cmovc $t3, $a3
  138. mov $a2, 8*2($r_ptr)
  139. mov $a3, 8*3($r_ptr)
  140. mov 0(%rsp),%r13
  141. .cfi_restore %r13
  142. mov 8(%rsp),%r12
  143. .cfi_restore %r12
  144. lea 16(%rsp),%rsp
  145. .cfi_adjust_cfa_offset -16
  146. .Lmul_by_2_epilogue:
  147. ret
  148. .cfi_endproc
  149. .size ecp_nistz256_mul_by_2,.-ecp_nistz256_mul_by_2
  150. ################################################################################
  151. # void ecp_nistz256_div_by_2(uint64_t res[4], uint64_t a[4]);
  152. .globl ecp_nistz256_div_by_2
  153. .type ecp_nistz256_div_by_2,\@function,2
  154. .align 32
  155. ecp_nistz256_div_by_2:
  156. .cfi_startproc
  157. push %r12
  158. .cfi_push %r12
  159. push %r13
  160. .cfi_push %r13
  161. .Ldiv_by_2_body:
  162. mov 8*0($a_ptr), $a0
  163. mov 8*1($a_ptr), $a1
  164. mov 8*2($a_ptr), $a2
  165. mov $a0, $t0
  166. mov 8*3($a_ptr), $a3
  167. lea .Lpoly(%rip), $a_ptr
  168. mov $a1, $t1
  169. xor $t4, $t4
  170. add 8*0($a_ptr), $a0
  171. mov $a2, $t2
  172. adc 8*1($a_ptr), $a1
  173. adc 8*2($a_ptr), $a2
  174. mov $a3, $t3
  175. adc 8*3($a_ptr), $a3
  176. adc \$0, $t4
  177. xor $a_ptr, $a_ptr # borrow $a_ptr
  178. test \$1, $t0
  179. cmovz $t0, $a0
  180. cmovz $t1, $a1
  181. cmovz $t2, $a2
  182. cmovz $t3, $a3
  183. cmovz $a_ptr, $t4
  184. mov $a1, $t0 # a0:a3>>1
  185. shr \$1, $a0
  186. shl \$63, $t0
  187. mov $a2, $t1
  188. shr \$1, $a1
  189. or $t0, $a0
  190. shl \$63, $t1
  191. mov $a3, $t2
  192. shr \$1, $a2
  193. or $t1, $a1
  194. shl \$63, $t2
  195. shr \$1, $a3
  196. shl \$63, $t4
  197. or $t2, $a2
  198. or $t4, $a3
  199. mov $a0, 8*0($r_ptr)
  200. mov $a1, 8*1($r_ptr)
  201. mov $a2, 8*2($r_ptr)
  202. mov $a3, 8*3($r_ptr)
  203. mov 0(%rsp),%r13
  204. .cfi_restore %r13
  205. mov 8(%rsp),%r12
  206. .cfi_restore %r12
  207. lea 16(%rsp),%rsp
  208. .cfi_adjust_cfa_offset -16
  209. .Ldiv_by_2_epilogue:
  210. ret
  211. .cfi_endproc
  212. .size ecp_nistz256_div_by_2,.-ecp_nistz256_div_by_2
  213. ################################################################################
  214. # void ecp_nistz256_mul_by_3(uint64_t res[4], uint64_t a[4]);
  215. .globl ecp_nistz256_mul_by_3
  216. .type ecp_nistz256_mul_by_3,\@function,2
  217. .align 32
  218. ecp_nistz256_mul_by_3:
  219. .cfi_startproc
  220. push %r12
  221. .cfi_push %r12
  222. push %r13
  223. .cfi_push %r13
  224. .Lmul_by_3_body:
  225. mov 8*0($a_ptr), $a0
  226. xor $t4, $t4
  227. mov 8*1($a_ptr), $a1
  228. add $a0, $a0 # a0:a3+a0:a3
  229. mov 8*2($a_ptr), $a2
  230. adc $a1, $a1
  231. mov 8*3($a_ptr), $a3
  232. mov $a0, $t0
  233. adc $a2, $a2
  234. adc $a3, $a3
  235. mov $a1, $t1
  236. adc \$0, $t4
  237. sub \$-1, $a0
  238. mov $a2, $t2
  239. sbb .Lpoly+8*1(%rip), $a1
  240. sbb \$0, $a2
  241. mov $a3, $t3
  242. sbb .Lpoly+8*3(%rip), $a3
  243. sbb \$0, $t4
  244. cmovc $t0, $a0
  245. cmovc $t1, $a1
  246. cmovc $t2, $a2
  247. cmovc $t3, $a3
  248. xor $t4, $t4
  249. add 8*0($a_ptr), $a0 # a0:a3+=a_ptr[0:3]
  250. adc 8*1($a_ptr), $a1
  251. mov $a0, $t0
  252. adc 8*2($a_ptr), $a2
  253. adc 8*3($a_ptr), $a3
  254. mov $a1, $t1
  255. adc \$0, $t4
  256. sub \$-1, $a0
  257. mov $a2, $t2
  258. sbb .Lpoly+8*1(%rip), $a1
  259. sbb \$0, $a2
  260. mov $a3, $t3
  261. sbb .Lpoly+8*3(%rip), $a3
  262. sbb \$0, $t4
  263. cmovc $t0, $a0
  264. cmovc $t1, $a1
  265. mov $a0, 8*0($r_ptr)
  266. cmovc $t2, $a2
  267. mov $a1, 8*1($r_ptr)
  268. cmovc $t3, $a3
  269. mov $a2, 8*2($r_ptr)
  270. mov $a3, 8*3($r_ptr)
  271. mov 0(%rsp),%r13
  272. .cfi_restore %r13
  273. mov 8(%rsp),%r12
  274. .cfi_restore %r12
  275. lea 16(%rsp),%rsp
  276. .cfi_adjust_cfa_offset -16
  277. .Lmul_by_3_epilogue:
  278. ret
  279. .cfi_endproc
  280. .size ecp_nistz256_mul_by_3,.-ecp_nistz256_mul_by_3
  281. ################################################################################
  282. # void ecp_nistz256_add(uint64_t res[4], uint64_t a[4], uint64_t b[4]);
  283. .globl ecp_nistz256_add
  284. .type ecp_nistz256_add,\@function,3
  285. .align 32
  286. ecp_nistz256_add:
  287. .cfi_startproc
  288. push %r12
  289. .cfi_push %r12
  290. push %r13
  291. .cfi_push %r13
  292. .Ladd_body:
  293. mov 8*0($a_ptr), $a0
  294. xor $t4, $t4
  295. mov 8*1($a_ptr), $a1
  296. mov 8*2($a_ptr), $a2
  297. mov 8*3($a_ptr), $a3
  298. lea .Lpoly(%rip), $a_ptr
  299. add 8*0($b_ptr), $a0
  300. adc 8*1($b_ptr), $a1
  301. mov $a0, $t0
  302. adc 8*2($b_ptr), $a2
  303. adc 8*3($b_ptr), $a3
  304. mov $a1, $t1
  305. adc \$0, $t4
  306. sub 8*0($a_ptr), $a0
  307. mov $a2, $t2
  308. sbb 8*1($a_ptr), $a1
  309. sbb 8*2($a_ptr), $a2
  310. mov $a3, $t3
  311. sbb 8*3($a_ptr), $a3
  312. sbb \$0, $t4
  313. cmovc $t0, $a0
  314. cmovc $t1, $a1
  315. mov $a0, 8*0($r_ptr)
  316. cmovc $t2, $a2
  317. mov $a1, 8*1($r_ptr)
  318. cmovc $t3, $a3
  319. mov $a2, 8*2($r_ptr)
  320. mov $a3, 8*3($r_ptr)
  321. mov 0(%rsp),%r13
  322. .cfi_restore %r13
  323. mov 8(%rsp),%r12
  324. .cfi_restore %r12
  325. lea 16(%rsp),%rsp
  326. .cfi_adjust_cfa_offset -16
  327. .Ladd_epilogue:
  328. ret
  329. .cfi_endproc
  330. .size ecp_nistz256_add,.-ecp_nistz256_add
  331. ################################################################################
  332. # void ecp_nistz256_sub(uint64_t res[4], uint64_t a[4], uint64_t b[4]);
  333. .globl ecp_nistz256_sub
  334. .type ecp_nistz256_sub,\@function,3
  335. .align 32
  336. ecp_nistz256_sub:
  337. .cfi_startproc
  338. push %r12
  339. .cfi_push %r12
  340. push %r13
  341. .cfi_push %r13
  342. .Lsub_body:
  343. mov 8*0($a_ptr), $a0
  344. xor $t4, $t4
  345. mov 8*1($a_ptr), $a1
  346. mov 8*2($a_ptr), $a2
  347. mov 8*3($a_ptr), $a3
  348. lea .Lpoly(%rip), $a_ptr
  349. sub 8*0($b_ptr), $a0
  350. sbb 8*1($b_ptr), $a1
  351. mov $a0, $t0
  352. sbb 8*2($b_ptr), $a2
  353. sbb 8*3($b_ptr), $a3
  354. mov $a1, $t1
  355. sbb \$0, $t4
  356. add 8*0($a_ptr), $a0
  357. mov $a2, $t2
  358. adc 8*1($a_ptr), $a1
  359. adc 8*2($a_ptr), $a2
  360. mov $a3, $t3
  361. adc 8*3($a_ptr), $a3
  362. test $t4, $t4
  363. cmovz $t0, $a0
  364. cmovz $t1, $a1
  365. mov $a0, 8*0($r_ptr)
  366. cmovz $t2, $a2
  367. mov $a1, 8*1($r_ptr)
  368. cmovz $t3, $a3
  369. mov $a2, 8*2($r_ptr)
  370. mov $a3, 8*3($r_ptr)
  371. mov 0(%rsp),%r13
  372. .cfi_restore %r13
  373. mov 8(%rsp),%r12
  374. .cfi_restore %r12
  375. lea 16(%rsp),%rsp
  376. .cfi_adjust_cfa_offset -16
  377. .Lsub_epilogue:
  378. ret
  379. .cfi_endproc
  380. .size ecp_nistz256_sub,.-ecp_nistz256_sub
  381. ################################################################################
  382. # void ecp_nistz256_neg(uint64_t res[4], uint64_t a[4]);
  383. .globl ecp_nistz256_neg
  384. .type ecp_nistz256_neg,\@function,2
  385. .align 32
  386. ecp_nistz256_neg:
  387. .cfi_startproc
  388. push %r12
  389. .cfi_push %r12
  390. push %r13
  391. .cfi_push %r13
  392. .Lneg_body:
  393. xor $a0, $a0
  394. xor $a1, $a1
  395. xor $a2, $a2
  396. xor $a3, $a3
  397. xor $t4, $t4
  398. sub 8*0($a_ptr), $a0
  399. sbb 8*1($a_ptr), $a1
  400. sbb 8*2($a_ptr), $a2
  401. mov $a0, $t0
  402. sbb 8*3($a_ptr), $a3
  403. lea .Lpoly(%rip), $a_ptr
  404. mov $a1, $t1
  405. sbb \$0, $t4
  406. add 8*0($a_ptr), $a0
  407. mov $a2, $t2
  408. adc 8*1($a_ptr), $a1
  409. adc 8*2($a_ptr), $a2
  410. mov $a3, $t3
  411. adc 8*3($a_ptr), $a3
  412. test $t4, $t4
  413. cmovz $t0, $a0
  414. cmovz $t1, $a1
  415. mov $a0, 8*0($r_ptr)
  416. cmovz $t2, $a2
  417. mov $a1, 8*1($r_ptr)
  418. cmovz $t3, $a3
  419. mov $a2, 8*2($r_ptr)
  420. mov $a3, 8*3($r_ptr)
  421. mov 0(%rsp),%r13
  422. .cfi_restore %r13
  423. mov 8(%rsp),%r12
  424. .cfi_restore %r12
  425. lea 16(%rsp),%rsp
  426. .cfi_adjust_cfa_offset -16
  427. .Lneg_epilogue:
  428. ret
  429. .cfi_endproc
  430. .size ecp_nistz256_neg,.-ecp_nistz256_neg
  431. ___
  432. }
  433. {
  434. my ($r_ptr,$a_ptr,$b_org,$b_ptr)=("%rdi","%rsi","%rdx","%rbx");
  435. my ($acc0,$acc1,$acc2,$acc3,$acc4,$acc5,$acc6,$acc7)=map("%r$_",(8..15));
  436. my ($t0,$t1,$t2,$t3,$t4)=("%rcx","%rbp","%rbx","%rdx","%rax");
  437. my ($poly1,$poly3)=($acc6,$acc7);
  438. $code.=<<___;
  439. ################################################################################
  440. # void ecp_nistz256_ord_mul_mont(
  441. # uint64_t res[4],
  442. # uint64_t a[4],
  443. # uint64_t b[4]);
  444. .globl ecp_nistz256_ord_mul_mont
  445. .type ecp_nistz256_ord_mul_mont,\@function,3
  446. .align 32
  447. ecp_nistz256_ord_mul_mont:
  448. .cfi_startproc
  449. ___
  450. $code.=<<___ if ($addx);
  451. mov \$0x80100, %ecx
  452. and OPENSSL_ia32cap_P+8(%rip), %ecx
  453. cmp \$0x80100, %ecx
  454. je .Lecp_nistz256_ord_mul_montx
  455. ___
  456. $code.=<<___;
  457. push %rbp
  458. .cfi_push %rbp
  459. push %rbx
  460. .cfi_push %rbx
  461. push %r12
  462. .cfi_push %r12
  463. push %r13
  464. .cfi_push %r13
  465. push %r14
  466. .cfi_push %r14
  467. push %r15
  468. .cfi_push %r15
  469. .Lord_mul_body:
  470. mov 8*0($b_org), %rax
  471. mov $b_org, $b_ptr
  472. lea .Lord(%rip), %r14
  473. mov .LordK(%rip), %r15
  474. ################################# * b[0]
  475. mov %rax, $t0
  476. mulq 8*0($a_ptr)
  477. mov %rax, $acc0
  478. mov $t0, %rax
  479. mov %rdx, $acc1
  480. mulq 8*1($a_ptr)
  481. add %rax, $acc1
  482. mov $t0, %rax
  483. adc \$0, %rdx
  484. mov %rdx, $acc2
  485. mulq 8*2($a_ptr)
  486. add %rax, $acc2
  487. mov $t0, %rax
  488. adc \$0, %rdx
  489. mov $acc0, $acc5
  490. imulq %r15,$acc0
  491. mov %rdx, $acc3
  492. mulq 8*3($a_ptr)
  493. add %rax, $acc3
  494. mov $acc0, %rax
  495. adc \$0, %rdx
  496. mov %rdx, $acc4
  497. ################################# First reduction step
  498. mulq 8*0(%r14)
  499. mov $acc0, $t1
  500. add %rax, $acc5 # guaranteed to be zero
  501. mov $acc0, %rax
  502. adc \$0, %rdx
  503. mov %rdx, $t0
  504. sub $acc0, $acc2
  505. sbb \$0, $acc0 # can't borrow
  506. mulq 8*1(%r14)
  507. add $t0, $acc1
  508. adc \$0, %rdx
  509. add %rax, $acc1
  510. mov $t1, %rax
  511. adc %rdx, $acc2
  512. mov $t1, %rdx
  513. adc \$0, $acc0 # can't overflow
  514. shl \$32, %rax
  515. shr \$32, %rdx
  516. sub %rax, $acc3
  517. mov 8*1($b_ptr), %rax
  518. sbb %rdx, $t1 # can't borrow
  519. add $acc0, $acc3
  520. adc $t1, $acc4
  521. adc \$0, $acc5
  522. ################################# * b[1]
  523. mov %rax, $t0
  524. mulq 8*0($a_ptr)
  525. add %rax, $acc1
  526. mov $t0, %rax
  527. adc \$0, %rdx
  528. mov %rdx, $t1
  529. mulq 8*1($a_ptr)
  530. add $t1, $acc2
  531. adc \$0, %rdx
  532. add %rax, $acc2
  533. mov $t0, %rax
  534. adc \$0, %rdx
  535. mov %rdx, $t1
  536. mulq 8*2($a_ptr)
  537. add $t1, $acc3
  538. adc \$0, %rdx
  539. add %rax, $acc3
  540. mov $t0, %rax
  541. adc \$0, %rdx
  542. mov $acc1, $t0
  543. imulq %r15, $acc1
  544. mov %rdx, $t1
  545. mulq 8*3($a_ptr)
  546. add $t1, $acc4
  547. adc \$0, %rdx
  548. xor $acc0, $acc0
  549. add %rax, $acc4
  550. mov $acc1, %rax
  551. adc %rdx, $acc5
  552. adc \$0, $acc0
  553. ################################# Second reduction step
  554. mulq 8*0(%r14)
  555. mov $acc1, $t1
  556. add %rax, $t0 # guaranteed to be zero
  557. mov $acc1, %rax
  558. adc %rdx, $t0
  559. sub $acc1, $acc3
  560. sbb \$0, $acc1 # can't borrow
  561. mulq 8*1(%r14)
  562. add $t0, $acc2
  563. adc \$0, %rdx
  564. add %rax, $acc2
  565. mov $t1, %rax
  566. adc %rdx, $acc3
  567. mov $t1, %rdx
  568. adc \$0, $acc1 # can't overflow
  569. shl \$32, %rax
  570. shr \$32, %rdx
  571. sub %rax, $acc4
  572. mov 8*2($b_ptr), %rax
  573. sbb %rdx, $t1 # can't borrow
  574. add $acc1, $acc4
  575. adc $t1, $acc5
  576. adc \$0, $acc0
  577. ################################## * b[2]
  578. mov %rax, $t0
  579. mulq 8*0($a_ptr)
  580. add %rax, $acc2
  581. mov $t0, %rax
  582. adc \$0, %rdx
  583. mov %rdx, $t1
  584. mulq 8*1($a_ptr)
  585. add $t1, $acc3
  586. adc \$0, %rdx
  587. add %rax, $acc3
  588. mov $t0, %rax
  589. adc \$0, %rdx
  590. mov %rdx, $t1
  591. mulq 8*2($a_ptr)
  592. add $t1, $acc4
  593. adc \$0, %rdx
  594. add %rax, $acc4
  595. mov $t0, %rax
  596. adc \$0, %rdx
  597. mov $acc2, $t0
  598. imulq %r15, $acc2
  599. mov %rdx, $t1
  600. mulq 8*3($a_ptr)
  601. add $t1, $acc5
  602. adc \$0, %rdx
  603. xor $acc1, $acc1
  604. add %rax, $acc5
  605. mov $acc2, %rax
  606. adc %rdx, $acc0
  607. adc \$0, $acc1
  608. ################################# Third reduction step
  609. mulq 8*0(%r14)
  610. mov $acc2, $t1
  611. add %rax, $t0 # guaranteed to be zero
  612. mov $acc2, %rax
  613. adc %rdx, $t0
  614. sub $acc2, $acc4
  615. sbb \$0, $acc2 # can't borrow
  616. mulq 8*1(%r14)
  617. add $t0, $acc3
  618. adc \$0, %rdx
  619. add %rax, $acc3
  620. mov $t1, %rax
  621. adc %rdx, $acc4
  622. mov $t1, %rdx
  623. adc \$0, $acc2 # can't overflow
  624. shl \$32, %rax
  625. shr \$32, %rdx
  626. sub %rax, $acc5
  627. mov 8*3($b_ptr), %rax
  628. sbb %rdx, $t1 # can't borrow
  629. add $acc2, $acc5
  630. adc $t1, $acc0
  631. adc \$0, $acc1
  632. ################################# * b[3]
  633. mov %rax, $t0
  634. mulq 8*0($a_ptr)
  635. add %rax, $acc3
  636. mov $t0, %rax
  637. adc \$0, %rdx
  638. mov %rdx, $t1
  639. mulq 8*1($a_ptr)
  640. add $t1, $acc4
  641. adc \$0, %rdx
  642. add %rax, $acc4
  643. mov $t0, %rax
  644. adc \$0, %rdx
  645. mov %rdx, $t1
  646. mulq 8*2($a_ptr)
  647. add $t1, $acc5
  648. adc \$0, %rdx
  649. add %rax, $acc5
  650. mov $t0, %rax
  651. adc \$0, %rdx
  652. mov $acc3, $t0
  653. imulq %r15, $acc3
  654. mov %rdx, $t1
  655. mulq 8*3($a_ptr)
  656. add $t1, $acc0
  657. adc \$0, %rdx
  658. xor $acc2, $acc2
  659. add %rax, $acc0
  660. mov $acc3, %rax
  661. adc %rdx, $acc1
  662. adc \$0, $acc2
  663. ################################# Last reduction step
  664. mulq 8*0(%r14)
  665. mov $acc3, $t1
  666. add %rax, $t0 # guaranteed to be zero
  667. mov $acc3, %rax
  668. adc %rdx, $t0
  669. sub $acc3, $acc5
  670. sbb \$0, $acc3 # can't borrow
  671. mulq 8*1(%r14)
  672. add $t0, $acc4
  673. adc \$0, %rdx
  674. add %rax, $acc4
  675. mov $t1, %rax
  676. adc %rdx, $acc5
  677. mov $t1, %rdx
  678. adc \$0, $acc3 # can't overflow
  679. shl \$32, %rax
  680. shr \$32, %rdx
  681. sub %rax, $acc0
  682. sbb %rdx, $t1 # can't borrow
  683. add $acc3, $acc0
  684. adc $t1, $acc1
  685. adc \$0, $acc2
  686. ################################# Subtract ord
  687. mov $acc4, $a_ptr
  688. sub 8*0(%r14), $acc4
  689. mov $acc5, $acc3
  690. sbb 8*1(%r14), $acc5
  691. mov $acc0, $t0
  692. sbb 8*2(%r14), $acc0
  693. mov $acc1, $t1
  694. sbb 8*3(%r14), $acc1
  695. sbb \$0, $acc2
  696. cmovc $a_ptr, $acc4
  697. cmovc $acc3, $acc5
  698. cmovc $t0, $acc0
  699. cmovc $t1, $acc1
  700. mov $acc4, 8*0($r_ptr)
  701. mov $acc5, 8*1($r_ptr)
  702. mov $acc0, 8*2($r_ptr)
  703. mov $acc1, 8*3($r_ptr)
  704. mov 0(%rsp),%r15
  705. .cfi_restore %r15
  706. mov 8(%rsp),%r14
  707. .cfi_restore %r14
  708. mov 16(%rsp),%r13
  709. .cfi_restore %r13
  710. mov 24(%rsp),%r12
  711. .cfi_restore %r12
  712. mov 32(%rsp),%rbx
  713. .cfi_restore %rbx
  714. mov 40(%rsp),%rbp
  715. .cfi_restore %rbp
  716. lea 48(%rsp),%rsp
  717. .cfi_adjust_cfa_offset -48
  718. .Lord_mul_epilogue:
  719. ret
  720. .cfi_endproc
  721. .size ecp_nistz256_ord_mul_mont,.-ecp_nistz256_ord_mul_mont
  722. ################################################################################
  723. # void ecp_nistz256_ord_sqr_mont(
  724. # uint64_t res[4],
  725. # uint64_t a[4],
  726. # int rep);
  727. .globl ecp_nistz256_ord_sqr_mont
  728. .type ecp_nistz256_ord_sqr_mont,\@function,3
  729. .align 32
  730. ecp_nistz256_ord_sqr_mont:
  731. .cfi_startproc
  732. ___
  733. $code.=<<___ if ($addx);
  734. mov \$0x80100, %ecx
  735. and OPENSSL_ia32cap_P+8(%rip), %ecx
  736. cmp \$0x80100, %ecx
  737. je .Lecp_nistz256_ord_sqr_montx
  738. ___
  739. $code.=<<___;
  740. push %rbp
  741. .cfi_push %rbp
  742. push %rbx
  743. .cfi_push %rbx
  744. push %r12
  745. .cfi_push %r12
  746. push %r13
  747. .cfi_push %r13
  748. push %r14
  749. .cfi_push %r14
  750. push %r15
  751. .cfi_push %r15
  752. .Lord_sqr_body:
  753. mov 8*0($a_ptr), $acc0
  754. mov 8*1($a_ptr), %rax
  755. mov 8*2($a_ptr), $acc6
  756. mov 8*3($a_ptr), $acc7
  757. lea .Lord(%rip), $a_ptr # pointer to modulus
  758. mov $b_org, $b_ptr
  759. jmp .Loop_ord_sqr
  760. .align 32
  761. .Loop_ord_sqr:
  762. ################################# a[1:] * a[0]
  763. mov %rax, $t1 # put aside a[1]
  764. mul $acc0 # a[1] * a[0]
  765. mov %rax, $acc1
  766. movq $t1, %xmm1 # offload a[1]
  767. mov $acc6, %rax
  768. mov %rdx, $acc2
  769. mul $acc0 # a[2] * a[0]
  770. add %rax, $acc2
  771. mov $acc7, %rax
  772. movq $acc6, %xmm2 # offload a[2]
  773. adc \$0, %rdx
  774. mov %rdx, $acc3
  775. mul $acc0 # a[3] * a[0]
  776. add %rax, $acc3
  777. mov $acc7, %rax
  778. movq $acc7, %xmm3 # offload a[3]
  779. adc \$0, %rdx
  780. mov %rdx, $acc4
  781. ################################# a[3] * a[2]
  782. mul $acc6 # a[3] * a[2]
  783. mov %rax, $acc5
  784. mov $acc6, %rax
  785. mov %rdx, $acc6
  786. ################################# a[2:] * a[1]
  787. mul $t1 # a[2] * a[1]
  788. add %rax, $acc3
  789. mov $acc7, %rax
  790. adc \$0, %rdx
  791. mov %rdx, $acc7
  792. mul $t1 # a[3] * a[1]
  793. add %rax, $acc4
  794. adc \$0, %rdx
  795. add $acc7, $acc4
  796. adc %rdx, $acc5
  797. adc \$0, $acc6 # can't overflow
  798. ################################# *2
  799. xor $acc7, $acc7
  800. mov $acc0, %rax
  801. add $acc1, $acc1
  802. adc $acc2, $acc2
  803. adc $acc3, $acc3
  804. adc $acc4, $acc4
  805. adc $acc5, $acc5
  806. adc $acc6, $acc6
  807. adc \$0, $acc7
  808. ################################# Missing products
  809. mul %rax # a[0] * a[0]
  810. mov %rax, $acc0
  811. movq %xmm1, %rax
  812. mov %rdx, $t1
  813. mul %rax # a[1] * a[1]
  814. add $t1, $acc1
  815. adc %rax, $acc2
  816. movq %xmm2, %rax
  817. adc \$0, %rdx
  818. mov %rdx, $t1
  819. mul %rax # a[2] * a[2]
  820. add $t1, $acc3
  821. adc %rax, $acc4
  822. movq %xmm3, %rax
  823. adc \$0, %rdx
  824. mov %rdx, $t1
  825. mov $acc0, $t0
  826. imulq 8*4($a_ptr), $acc0 # *= .LordK
  827. mul %rax # a[3] * a[3]
  828. add $t1, $acc5
  829. adc %rax, $acc6
  830. mov 8*0($a_ptr), %rax # modulus[0]
  831. adc %rdx, $acc7 # can't overflow
  832. ################################# First reduction step
  833. mul $acc0
  834. mov $acc0, $t1
  835. add %rax, $t0 # guaranteed to be zero
  836. mov 8*1($a_ptr), %rax # modulus[1]
  837. adc %rdx, $t0
  838. sub $acc0, $acc2
  839. sbb \$0, $t1 # can't borrow
  840. mul $acc0
  841. add $t0, $acc1
  842. adc \$0, %rdx
  843. add %rax, $acc1
  844. mov $acc0, %rax
  845. adc %rdx, $acc2
  846. mov $acc0, %rdx
  847. adc \$0, $t1 # can't overflow
  848. mov $acc1, $t0
  849. imulq 8*4($a_ptr), $acc1 # *= .LordK
  850. shl \$32, %rax
  851. shr \$32, %rdx
  852. sub %rax, $acc3
  853. mov 8*0($a_ptr), %rax
  854. sbb %rdx, $acc0 # can't borrow
  855. add $t1, $acc3
  856. adc \$0, $acc0 # can't overflow
  857. ################################# Second reduction step
  858. mul $acc1
  859. mov $acc1, $t1
  860. add %rax, $t0 # guaranteed to be zero
  861. mov 8*1($a_ptr), %rax
  862. adc %rdx, $t0
  863. sub $acc1, $acc3
  864. sbb \$0, $t1 # can't borrow
  865. mul $acc1
  866. add $t0, $acc2
  867. adc \$0, %rdx
  868. add %rax, $acc2
  869. mov $acc1, %rax
  870. adc %rdx, $acc3
  871. mov $acc1, %rdx
  872. adc \$0, $t1 # can't overflow
  873. mov $acc2, $t0
  874. imulq 8*4($a_ptr), $acc2 # *= .LordK
  875. shl \$32, %rax
  876. shr \$32, %rdx
  877. sub %rax, $acc0
  878. mov 8*0($a_ptr), %rax
  879. sbb %rdx, $acc1 # can't borrow
  880. add $t1, $acc0
  881. adc \$0, $acc1 # can't overflow
  882. ################################# Third reduction step
  883. mul $acc2
  884. mov $acc2, $t1
  885. add %rax, $t0 # guaranteed to be zero
  886. mov 8*1($a_ptr), %rax
  887. adc %rdx, $t0
  888. sub $acc2, $acc0
  889. sbb \$0, $t1 # can't borrow
  890. mul $acc2
  891. add $t0, $acc3
  892. adc \$0, %rdx
  893. add %rax, $acc3
  894. mov $acc2, %rax
  895. adc %rdx, $acc0
  896. mov $acc2, %rdx
  897. adc \$0, $t1 # can't overflow
  898. mov $acc3, $t0
  899. imulq 8*4($a_ptr), $acc3 # *= .LordK
  900. shl \$32, %rax
  901. shr \$32, %rdx
  902. sub %rax, $acc1
  903. mov 8*0($a_ptr), %rax
  904. sbb %rdx, $acc2 # can't borrow
  905. add $t1, $acc1
  906. adc \$0, $acc2 # can't overflow
  907. ################################# Last reduction step
  908. mul $acc3
  909. mov $acc3, $t1
  910. add %rax, $t0 # guaranteed to be zero
  911. mov 8*1($a_ptr), %rax
  912. adc %rdx, $t0
  913. sub $acc3, $acc1
  914. sbb \$0, $t1 # can't borrow
  915. mul $acc3
  916. add $t0, $acc0
  917. adc \$0, %rdx
  918. add %rax, $acc0
  919. mov $acc3, %rax
  920. adc %rdx, $acc1
  921. mov $acc3, %rdx
  922. adc \$0, $t1 # can't overflow
  923. shl \$32, %rax
  924. shr \$32, %rdx
  925. sub %rax, $acc2
  926. sbb %rdx, $acc3 # can't borrow
  927. add $t1, $acc2
  928. adc \$0, $acc3 # can't overflow
  929. ################################# Add bits [511:256] of the sqr result
  930. xor %rdx, %rdx
  931. add $acc4, $acc0
  932. adc $acc5, $acc1
  933. mov $acc0, $acc4
  934. adc $acc6, $acc2
  935. adc $acc7, $acc3
  936. mov $acc1, %rax
  937. adc \$0, %rdx
  938. ################################# Compare to modulus
  939. sub 8*0($a_ptr), $acc0
  940. mov $acc2, $acc6
  941. sbb 8*1($a_ptr), $acc1
  942. sbb 8*2($a_ptr), $acc2
  943. mov $acc3, $acc7
  944. sbb 8*3($a_ptr), $acc3
  945. sbb \$0, %rdx
  946. cmovc $acc4, $acc0
  947. cmovnc $acc1, %rax
  948. cmovnc $acc2, $acc6
  949. cmovnc $acc3, $acc7
  950. dec $b_ptr
  951. jnz .Loop_ord_sqr
  952. mov $acc0, 8*0($r_ptr)
  953. mov %rax, 8*1($r_ptr)
  954. pxor %xmm1, %xmm1
  955. mov $acc6, 8*2($r_ptr)
  956. pxor %xmm2, %xmm2
  957. mov $acc7, 8*3($r_ptr)
  958. pxor %xmm3, %xmm3
  959. mov 0(%rsp),%r15
  960. .cfi_restore %r15
  961. mov 8(%rsp),%r14
  962. .cfi_restore %r14
  963. mov 16(%rsp),%r13
  964. .cfi_restore %r13
  965. mov 24(%rsp),%r12
  966. .cfi_restore %r12
  967. mov 32(%rsp),%rbx
  968. .cfi_restore %rbx
  969. mov 40(%rsp),%rbp
  970. .cfi_restore %rbp
  971. lea 48(%rsp),%rsp
  972. .cfi_adjust_cfa_offset -48
  973. .Lord_sqr_epilogue:
  974. ret
  975. .cfi_endproc
  976. .size ecp_nistz256_ord_sqr_mont,.-ecp_nistz256_ord_sqr_mont
  977. ___
  978. $code.=<<___ if ($addx);
  979. ################################################################################
  980. .type ecp_nistz256_ord_mul_montx,\@function,3
  981. .align 32
  982. ecp_nistz256_ord_mul_montx:
  983. .cfi_startproc
  984. .Lecp_nistz256_ord_mul_montx:
  985. push %rbp
  986. .cfi_push %rbp
  987. push %rbx
  988. .cfi_push %rbx
  989. push %r12
  990. .cfi_push %r12
  991. push %r13
  992. .cfi_push %r13
  993. push %r14
  994. .cfi_push %r14
  995. push %r15
  996. .cfi_push %r15
  997. .Lord_mulx_body:
  998. mov $b_org, $b_ptr
  999. mov 8*0($b_org), %rdx
  1000. mov 8*0($a_ptr), $acc1
  1001. mov 8*1($a_ptr), $acc2
  1002. mov 8*2($a_ptr), $acc3
  1003. mov 8*3($a_ptr), $acc4
  1004. lea -128($a_ptr), $a_ptr # control u-op density
  1005. lea .Lord-128(%rip), %r14
  1006. mov .LordK(%rip), %r15
  1007. ################################# Multiply by b[0]
  1008. mulx $acc1, $acc0, $acc1
  1009. mulx $acc2, $t0, $acc2
  1010. mulx $acc3, $t1, $acc3
  1011. add $t0, $acc1
  1012. mulx $acc4, $t0, $acc4
  1013. mov $acc0, %rdx
  1014. mulx %r15, %rdx, %rax
  1015. adc $t1, $acc2
  1016. adc $t0, $acc3
  1017. adc \$0, $acc4
  1018. ################################# reduction
  1019. xor $acc5, $acc5 # $acc5=0, cf=0, of=0
  1020. mulx 8*0+128(%r14), $t0, $t1
  1021. adcx $t0, $acc0 # guaranteed to be zero
  1022. adox $t1, $acc1
  1023. mulx 8*1+128(%r14), $t0, $t1
  1024. adcx $t0, $acc1
  1025. adox $t1, $acc2
  1026. mulx 8*2+128(%r14), $t0, $t1
  1027. adcx $t0, $acc2
  1028. adox $t1, $acc3
  1029. mulx 8*3+128(%r14), $t0, $t1
  1030. mov 8*1($b_ptr), %rdx
  1031. adcx $t0, $acc3
  1032. adox $t1, $acc4
  1033. adcx $acc0, $acc4
  1034. adox $acc0, $acc5
  1035. adc \$0, $acc5 # cf=0, of=0
  1036. ################################# Multiply by b[1]
  1037. mulx 8*0+128($a_ptr), $t0, $t1
  1038. adcx $t0, $acc1
  1039. adox $t1, $acc2
  1040. mulx 8*1+128($a_ptr), $t0, $t1
  1041. adcx $t0, $acc2
  1042. adox $t1, $acc3
  1043. mulx 8*2+128($a_ptr), $t0, $t1
  1044. adcx $t0, $acc3
  1045. adox $t1, $acc4
  1046. mulx 8*3+128($a_ptr), $t0, $t1
  1047. mov $acc1, %rdx
  1048. mulx %r15, %rdx, %rax
  1049. adcx $t0, $acc4
  1050. adox $t1, $acc5
  1051. adcx $acc0, $acc5
  1052. adox $acc0, $acc0
  1053. adc \$0, $acc0 # cf=0, of=0
  1054. ################################# reduction
  1055. mulx 8*0+128(%r14), $t0, $t1
  1056. adcx $t0, $acc1 # guaranteed to be zero
  1057. adox $t1, $acc2
  1058. mulx 8*1+128(%r14), $t0, $t1
  1059. adcx $t0, $acc2
  1060. adox $t1, $acc3
  1061. mulx 8*2+128(%r14), $t0, $t1
  1062. adcx $t0, $acc3
  1063. adox $t1, $acc4
  1064. mulx 8*3+128(%r14), $t0, $t1
  1065. mov 8*2($b_ptr), %rdx
  1066. adcx $t0, $acc4
  1067. adox $t1, $acc5
  1068. adcx $acc1, $acc5
  1069. adox $acc1, $acc0
  1070. adc \$0, $acc0 # cf=0, of=0
  1071. ################################# Multiply by b[2]
  1072. mulx 8*0+128($a_ptr), $t0, $t1
  1073. adcx $t0, $acc2
  1074. adox $t1, $acc3
  1075. mulx 8*1+128($a_ptr), $t0, $t1
  1076. adcx $t0, $acc3
  1077. adox $t1, $acc4
  1078. mulx 8*2+128($a_ptr), $t0, $t1
  1079. adcx $t0, $acc4
  1080. adox $t1, $acc5
  1081. mulx 8*3+128($a_ptr), $t0, $t1
  1082. mov $acc2, %rdx
  1083. mulx %r15, %rdx, %rax
  1084. adcx $t0, $acc5
  1085. adox $t1, $acc0
  1086. adcx $acc1, $acc0
  1087. adox $acc1, $acc1
  1088. adc \$0, $acc1 # cf=0, of=0
  1089. ################################# reduction
  1090. mulx 8*0+128(%r14), $t0, $t1
  1091. adcx $t0, $acc2 # guaranteed to be zero
  1092. adox $t1, $acc3
  1093. mulx 8*1+128(%r14), $t0, $t1
  1094. adcx $t0, $acc3
  1095. adox $t1, $acc4
  1096. mulx 8*2+128(%r14), $t0, $t1
  1097. adcx $t0, $acc4
  1098. adox $t1, $acc5
  1099. mulx 8*3+128(%r14), $t0, $t1
  1100. mov 8*3($b_ptr), %rdx
  1101. adcx $t0, $acc5
  1102. adox $t1, $acc0
  1103. adcx $acc2, $acc0
  1104. adox $acc2, $acc1
  1105. adc \$0, $acc1 # cf=0, of=0
  1106. ################################# Multiply by b[3]
  1107. mulx 8*0+128($a_ptr), $t0, $t1
  1108. adcx $t0, $acc3
  1109. adox $t1, $acc4
  1110. mulx 8*1+128($a_ptr), $t0, $t1
  1111. adcx $t0, $acc4
  1112. adox $t1, $acc5
  1113. mulx 8*2+128($a_ptr), $t0, $t1
  1114. adcx $t0, $acc5
  1115. adox $t1, $acc0
  1116. mulx 8*3+128($a_ptr), $t0, $t1
  1117. mov $acc3, %rdx
  1118. mulx %r15, %rdx, %rax
  1119. adcx $t0, $acc0
  1120. adox $t1, $acc1
  1121. adcx $acc2, $acc1
  1122. adox $acc2, $acc2
  1123. adc \$0, $acc2 # cf=0, of=0
  1124. ################################# reduction
  1125. mulx 8*0+128(%r14), $t0, $t1
  1126. adcx $t0, $acc3 # guaranteed to be zero
  1127. adox $t1, $acc4
  1128. mulx 8*1+128(%r14), $t0, $t1
  1129. adcx $t0, $acc4
  1130. adox $t1, $acc5
  1131. mulx 8*2+128(%r14), $t0, $t1
  1132. adcx $t0, $acc5
  1133. adox $t1, $acc0
  1134. mulx 8*3+128(%r14), $t0, $t1
  1135. lea 128(%r14),%r14
  1136. mov $acc4, $t2
  1137. adcx $t0, $acc0
  1138. adox $t1, $acc1
  1139. mov $acc5, $t3
  1140. adcx $acc3, $acc1
  1141. adox $acc3, $acc2
  1142. adc \$0, $acc2
  1143. #################################
  1144. # Branch-less conditional subtraction of P
  1145. mov $acc0, $t0
  1146. sub 8*0(%r14), $acc4
  1147. sbb 8*1(%r14), $acc5
  1148. sbb 8*2(%r14), $acc0
  1149. mov $acc1, $t1
  1150. sbb 8*3(%r14), $acc1
  1151. sbb \$0, $acc2
  1152. cmovc $t2, $acc4
  1153. cmovc $t3, $acc5
  1154. cmovc $t0, $acc0
  1155. cmovc $t1, $acc1
  1156. mov $acc4, 8*0($r_ptr)
  1157. mov $acc5, 8*1($r_ptr)
  1158. mov $acc0, 8*2($r_ptr)
  1159. mov $acc1, 8*3($r_ptr)
  1160. mov 0(%rsp),%r15
  1161. .cfi_restore %r15
  1162. mov 8(%rsp),%r14
  1163. .cfi_restore %r14
  1164. mov 16(%rsp),%r13
  1165. .cfi_restore %r13
  1166. mov 24(%rsp),%r12
  1167. .cfi_restore %r12
  1168. mov 32(%rsp),%rbx
  1169. .cfi_restore %rbx
  1170. mov 40(%rsp),%rbp
  1171. .cfi_restore %rbp
  1172. lea 48(%rsp),%rsp
  1173. .cfi_adjust_cfa_offset -48
  1174. .Lord_mulx_epilogue:
  1175. ret
  1176. .cfi_endproc
  1177. .size ecp_nistz256_ord_mul_montx,.-ecp_nistz256_ord_mul_montx
  1178. .type ecp_nistz256_ord_sqr_montx,\@function,3
  1179. .align 32
  1180. ecp_nistz256_ord_sqr_montx:
  1181. .cfi_startproc
  1182. .Lecp_nistz256_ord_sqr_montx:
  1183. push %rbp
  1184. .cfi_push %rbp
  1185. push %rbx
  1186. .cfi_push %rbx
  1187. push %r12
  1188. .cfi_push %r12
  1189. push %r13
  1190. .cfi_push %r13
  1191. push %r14
  1192. .cfi_push %r14
  1193. push %r15
  1194. .cfi_push %r15
  1195. .Lord_sqrx_body:
  1196. mov $b_org, $b_ptr
  1197. mov 8*0($a_ptr), %rdx
  1198. mov 8*1($a_ptr), $acc6
  1199. mov 8*2($a_ptr), $acc7
  1200. mov 8*3($a_ptr), $acc0
  1201. lea .Lord(%rip), $a_ptr
  1202. jmp .Loop_ord_sqrx
  1203. .align 32
  1204. .Loop_ord_sqrx:
  1205. mulx $acc6, $acc1, $acc2 # a[0]*a[1]
  1206. mulx $acc7, $t0, $acc3 # a[0]*a[2]
  1207. mov %rdx, %rax # offload a[0]
  1208. movq $acc6, %xmm1 # offload a[1]
  1209. mulx $acc0, $t1, $acc4 # a[0]*a[3]
  1210. mov $acc6, %rdx
  1211. add $t0, $acc2
  1212. movq $acc7, %xmm2 # offload a[2]
  1213. adc $t1, $acc3
  1214. adc \$0, $acc4
  1215. xor $acc5, $acc5 # $acc5=0,cf=0,of=0
  1216. #################################
  1217. mulx $acc7, $t0, $t1 # a[1]*a[2]
  1218. adcx $t0, $acc3
  1219. adox $t1, $acc4
  1220. mulx $acc0, $t0, $t1 # a[1]*a[3]
  1221. mov $acc7, %rdx
  1222. adcx $t0, $acc4
  1223. adox $t1, $acc5
  1224. adc \$0, $acc5
  1225. #################################
  1226. mulx $acc0, $t0, $acc6 # a[2]*a[3]
  1227. mov %rax, %rdx
  1228. movq $acc0, %xmm3 # offload a[3]
  1229. xor $acc7, $acc7 # $acc7=0,cf=0,of=0
  1230. adcx $acc1, $acc1 # acc1:6<<1
  1231. adox $t0, $acc5
  1232. adcx $acc2, $acc2
  1233. adox $acc7, $acc6 # of=0
  1234. ################################# a[i]*a[i]
  1235. mulx %rdx, $acc0, $t1
  1236. movq %xmm1, %rdx
  1237. adcx $acc3, $acc3
  1238. adox $t1, $acc1
  1239. adcx $acc4, $acc4
  1240. mulx %rdx, $t0, $t4
  1241. movq %xmm2, %rdx
  1242. adcx $acc5, $acc5
  1243. adox $t0, $acc2
  1244. adcx $acc6, $acc6
  1245. mulx %rdx, $t0, $t1
  1246. .byte 0x67
  1247. movq %xmm3, %rdx
  1248. adox $t4, $acc3
  1249. adcx $acc7, $acc7
  1250. adox $t0, $acc4
  1251. adox $t1, $acc5
  1252. mulx %rdx, $t0, $t4
  1253. adox $t0, $acc6
  1254. adox $t4, $acc7
  1255. ################################# reduction
  1256. mov $acc0, %rdx
  1257. mulx 8*4($a_ptr), %rdx, $t0
  1258. xor %rax, %rax # cf=0, of=0
  1259. mulx 8*0($a_ptr), $t0, $t1
  1260. adcx $t0, $acc0 # guaranteed to be zero
  1261. adox $t1, $acc1
  1262. mulx 8*1($a_ptr), $t0, $t1
  1263. adcx $t0, $acc1
  1264. adox $t1, $acc2
  1265. mulx 8*2($a_ptr), $t0, $t1
  1266. adcx $t0, $acc2
  1267. adox $t1, $acc3
  1268. mulx 8*3($a_ptr), $t0, $t1
  1269. adcx $t0, $acc3
  1270. adox $t1, $acc0 # of=0
  1271. adcx %rax, $acc0 # cf=0
  1272. #################################
  1273. mov $acc1, %rdx
  1274. mulx 8*4($a_ptr), %rdx, $t0
  1275. mulx 8*0($a_ptr), $t0, $t1
  1276. adox $t0, $acc1 # guaranteed to be zero
  1277. adcx $t1, $acc2
  1278. mulx 8*1($a_ptr), $t0, $t1
  1279. adox $t0, $acc2
  1280. adcx $t1, $acc3
  1281. mulx 8*2($a_ptr), $t0, $t1
  1282. adox $t0, $acc3
  1283. adcx $t1, $acc0
  1284. mulx 8*3($a_ptr), $t0, $t1
  1285. adox $t0, $acc0
  1286. adcx $t1, $acc1 # cf=0
  1287. adox %rax, $acc1 # of=0
  1288. #################################
  1289. mov $acc2, %rdx
  1290. mulx 8*4($a_ptr), %rdx, $t0
  1291. mulx 8*0($a_ptr), $t0, $t1
  1292. adcx $t0, $acc2 # guaranteed to be zero
  1293. adox $t1, $acc3
  1294. mulx 8*1($a_ptr), $t0, $t1
  1295. adcx $t0, $acc3
  1296. adox $t1, $acc0
  1297. mulx 8*2($a_ptr), $t0, $t1
  1298. adcx $t0, $acc0
  1299. adox $t1, $acc1
  1300. mulx 8*3($a_ptr), $t0, $t1
  1301. adcx $t0, $acc1
  1302. adox $t1, $acc2 # of=0
  1303. adcx %rax, $acc2 # cf=0
  1304. #################################
  1305. mov $acc3, %rdx
  1306. mulx 8*4($a_ptr), %rdx, $t0
  1307. mulx 8*0($a_ptr), $t0, $t1
  1308. adox $t0, $acc3 # guaranteed to be zero
  1309. adcx $t1, $acc0
  1310. mulx 8*1($a_ptr), $t0, $t1
  1311. adox $t0, $acc0
  1312. adcx $t1, $acc1
  1313. mulx 8*2($a_ptr), $t0, $t1
  1314. adox $t0, $acc1
  1315. adcx $t1, $acc2
  1316. mulx 8*3($a_ptr), $t0, $t1
  1317. adox $t0, $acc2
  1318. adcx $t1, $acc3
  1319. adox %rax, $acc3
  1320. ################################# accumulate upper half
  1321. add $acc0, $acc4 # add $acc4, $acc0
  1322. adc $acc5, $acc1
  1323. mov $acc4, %rdx
  1324. adc $acc6, $acc2
  1325. adc $acc7, $acc3
  1326. mov $acc1, $acc6
  1327. adc \$0, %rax
  1328. ################################# compare to modulus
  1329. sub 8*0($a_ptr), $acc4
  1330. mov $acc2, $acc7
  1331. sbb 8*1($a_ptr), $acc1
  1332. sbb 8*2($a_ptr), $acc2
  1333. mov $acc3, $acc0
  1334. sbb 8*3($a_ptr), $acc3
  1335. sbb \$0, %rax
  1336. cmovnc $acc4, %rdx
  1337. cmovnc $acc1, $acc6
  1338. cmovnc $acc2, $acc7
  1339. cmovnc $acc3, $acc0
  1340. dec $b_ptr
  1341. jnz .Loop_ord_sqrx
  1342. mov %rdx, 8*0($r_ptr)
  1343. mov $acc6, 8*1($r_ptr)
  1344. pxor %xmm1, %xmm1
  1345. mov $acc7, 8*2($r_ptr)
  1346. pxor %xmm2, %xmm2
  1347. mov $acc0, 8*3($r_ptr)
  1348. pxor %xmm3, %xmm3
  1349. mov 0(%rsp),%r15
  1350. .cfi_restore %r15
  1351. mov 8(%rsp),%r14
  1352. .cfi_restore %r14
  1353. mov 16(%rsp),%r13
  1354. .cfi_restore %r13
  1355. mov 24(%rsp),%r12
  1356. .cfi_restore %r12
  1357. mov 32(%rsp),%rbx
  1358. .cfi_restore %rbx
  1359. mov 40(%rsp),%rbp
  1360. .cfi_restore %rbp
  1361. lea 48(%rsp),%rsp
  1362. .cfi_adjust_cfa_offset -48
  1363. .Lord_sqrx_epilogue:
  1364. ret
  1365. .cfi_endproc
  1366. .size ecp_nistz256_ord_sqr_montx,.-ecp_nistz256_ord_sqr_montx
  1367. ___
  1368. $code.=<<___;
  1369. ################################################################################
  1370. # void ecp_nistz256_to_mont(
  1371. # uint64_t res[4],
  1372. # uint64_t in[4]);
  1373. .globl ecp_nistz256_to_mont
  1374. .type ecp_nistz256_to_mont,\@function,2
  1375. .align 32
  1376. ecp_nistz256_to_mont:
  1377. .cfi_startproc
  1378. ___
  1379. $code.=<<___ if ($addx);
  1380. mov \$0x80100, %ecx
  1381. and OPENSSL_ia32cap_P+8(%rip), %ecx
  1382. ___
  1383. $code.=<<___;
  1384. lea .LRR(%rip), $b_org
  1385. jmp .Lmul_mont
  1386. .cfi_endproc
  1387. .size ecp_nistz256_to_mont,.-ecp_nistz256_to_mont
  1388. ################################################################################
  1389. # void ecp_nistz256_mul_mont(
  1390. # uint64_t res[4],
  1391. # uint64_t a[4],
  1392. # uint64_t b[4]);
  1393. .globl ecp_nistz256_mul_mont
  1394. .type ecp_nistz256_mul_mont,\@function,3
  1395. .align 32
  1396. ecp_nistz256_mul_mont:
  1397. .cfi_startproc
  1398. ___
  1399. $code.=<<___ if ($addx);
  1400. mov \$0x80100, %ecx
  1401. and OPENSSL_ia32cap_P+8(%rip), %ecx
  1402. ___
  1403. $code.=<<___;
  1404. .Lmul_mont:
  1405. push %rbp
  1406. .cfi_push %rbp
  1407. push %rbx
  1408. .cfi_push %rbx
  1409. push %r12
  1410. .cfi_push %r12
  1411. push %r13
  1412. .cfi_push %r13
  1413. push %r14
  1414. .cfi_push %r14
  1415. push %r15
  1416. .cfi_push %r15
  1417. .Lmul_body:
  1418. ___
  1419. $code.=<<___ if ($addx);
  1420. cmp \$0x80100, %ecx
  1421. je .Lmul_montx
  1422. ___
  1423. $code.=<<___;
  1424. mov $b_org, $b_ptr
  1425. mov 8*0($b_org), %rax
  1426. mov 8*0($a_ptr), $acc1
  1427. mov 8*1($a_ptr), $acc2
  1428. mov 8*2($a_ptr), $acc3
  1429. mov 8*3($a_ptr), $acc4
  1430. call __ecp_nistz256_mul_montq
  1431. ___
  1432. $code.=<<___ if ($addx);
  1433. jmp .Lmul_mont_done
  1434. .align 32
  1435. .Lmul_montx:
  1436. mov $b_org, $b_ptr
  1437. mov 8*0($b_org), %rdx
  1438. mov 8*0($a_ptr), $acc1
  1439. mov 8*1($a_ptr), $acc2
  1440. mov 8*2($a_ptr), $acc3
  1441. mov 8*3($a_ptr), $acc4
  1442. lea -128($a_ptr), $a_ptr # control u-op density
  1443. call __ecp_nistz256_mul_montx
  1444. ___
  1445. $code.=<<___;
  1446. .Lmul_mont_done:
  1447. mov 0(%rsp),%r15
  1448. .cfi_restore %r15
  1449. mov 8(%rsp),%r14
  1450. .cfi_restore %r14
  1451. mov 16(%rsp),%r13
  1452. .cfi_restore %r13
  1453. mov 24(%rsp),%r12
  1454. .cfi_restore %r12
  1455. mov 32(%rsp),%rbx
  1456. .cfi_restore %rbx
  1457. mov 40(%rsp),%rbp
  1458. .cfi_restore %rbp
  1459. lea 48(%rsp),%rsp
  1460. .cfi_adjust_cfa_offset -48
  1461. .Lmul_epilogue:
  1462. ret
  1463. .cfi_endproc
  1464. .size ecp_nistz256_mul_mont,.-ecp_nistz256_mul_mont
  1465. .type __ecp_nistz256_mul_montq,\@abi-omnipotent
  1466. .align 32
  1467. __ecp_nistz256_mul_montq:
  1468. .cfi_startproc
  1469. ########################################################################
  1470. # Multiply a by b[0]
  1471. mov %rax, $t1
  1472. mulq $acc1
  1473. mov .Lpoly+8*1(%rip),$poly1
  1474. mov %rax, $acc0
  1475. mov $t1, %rax
  1476. mov %rdx, $acc1
  1477. mulq $acc2
  1478. mov .Lpoly+8*3(%rip),$poly3
  1479. add %rax, $acc1
  1480. mov $t1, %rax
  1481. adc \$0, %rdx
  1482. mov %rdx, $acc2
  1483. mulq $acc3
  1484. add %rax, $acc2
  1485. mov $t1, %rax
  1486. adc \$0, %rdx
  1487. mov %rdx, $acc3
  1488. mulq $acc4
  1489. add %rax, $acc3
  1490. mov $acc0, %rax
  1491. adc \$0, %rdx
  1492. xor $acc5, $acc5
  1493. mov %rdx, $acc4
  1494. ########################################################################
  1495. # First reduction step
  1496. # Basically now we want to multiply acc[0] by p256,
  1497. # and add the result to the acc.
  1498. # Due to the special form of p256 we do some optimizations
  1499. #
  1500. # acc[0] x p256[0..1] = acc[0] x 2^96 - acc[0]
  1501. # then we add acc[0] and get acc[0] x 2^96
  1502. mov $acc0, $t1
  1503. shl \$32, $acc0
  1504. mulq $poly3
  1505. shr \$32, $t1
  1506. add $acc0, $acc1 # +=acc[0]<<96
  1507. adc $t1, $acc2
  1508. adc %rax, $acc3
  1509. mov 8*1($b_ptr), %rax
  1510. adc %rdx, $acc4
  1511. adc \$0, $acc5
  1512. xor $acc0, $acc0
  1513. ########################################################################
  1514. # Multiply by b[1]
  1515. mov %rax, $t1
  1516. mulq 8*0($a_ptr)
  1517. add %rax, $acc1
  1518. mov $t1, %rax
  1519. adc \$0, %rdx
  1520. mov %rdx, $t0
  1521. mulq 8*1($a_ptr)
  1522. add $t0, $acc2
  1523. adc \$0, %rdx
  1524. add %rax, $acc2
  1525. mov $t1, %rax
  1526. adc \$0, %rdx
  1527. mov %rdx, $t0
  1528. mulq 8*2($a_ptr)
  1529. add $t0, $acc3
  1530. adc \$0, %rdx
  1531. add %rax, $acc3
  1532. mov $t1, %rax
  1533. adc \$0, %rdx
  1534. mov %rdx, $t0
  1535. mulq 8*3($a_ptr)
  1536. add $t0, $acc4
  1537. adc \$0, %rdx
  1538. add %rax, $acc4
  1539. mov $acc1, %rax
  1540. adc %rdx, $acc5
  1541. adc \$0, $acc0
  1542. ########################################################################
  1543. # Second reduction step
  1544. mov $acc1, $t1
  1545. shl \$32, $acc1
  1546. mulq $poly3
  1547. shr \$32, $t1
  1548. add $acc1, $acc2
  1549. adc $t1, $acc3
  1550. adc %rax, $acc4
  1551. mov 8*2($b_ptr), %rax
  1552. adc %rdx, $acc5
  1553. adc \$0, $acc0
  1554. xor $acc1, $acc1
  1555. ########################################################################
  1556. # Multiply by b[2]
  1557. mov %rax, $t1
  1558. mulq 8*0($a_ptr)
  1559. add %rax, $acc2
  1560. mov $t1, %rax
  1561. adc \$0, %rdx
  1562. mov %rdx, $t0
  1563. mulq 8*1($a_ptr)
  1564. add $t0, $acc3
  1565. adc \$0, %rdx
  1566. add %rax, $acc3
  1567. mov $t1, %rax
  1568. adc \$0, %rdx
  1569. mov %rdx, $t0
  1570. mulq 8*2($a_ptr)
  1571. add $t0, $acc4
  1572. adc \$0, %rdx
  1573. add %rax, $acc4
  1574. mov $t1, %rax
  1575. adc \$0, %rdx
  1576. mov %rdx, $t0
  1577. mulq 8*3($a_ptr)
  1578. add $t0, $acc5
  1579. adc \$0, %rdx
  1580. add %rax, $acc5
  1581. mov $acc2, %rax
  1582. adc %rdx, $acc0
  1583. adc \$0, $acc1
  1584. ########################################################################
  1585. # Third reduction step
  1586. mov $acc2, $t1
  1587. shl \$32, $acc2
  1588. mulq $poly3
  1589. shr \$32, $t1
  1590. add $acc2, $acc3
  1591. adc $t1, $acc4
  1592. adc %rax, $acc5
  1593. mov 8*3($b_ptr), %rax
  1594. adc %rdx, $acc0
  1595. adc \$0, $acc1
  1596. xor $acc2, $acc2
  1597. ########################################################################
  1598. # Multiply by b[3]
  1599. mov %rax, $t1
  1600. mulq 8*0($a_ptr)
  1601. add %rax, $acc3
  1602. mov $t1, %rax
  1603. adc \$0, %rdx
  1604. mov %rdx, $t0
  1605. mulq 8*1($a_ptr)
  1606. add $t0, $acc4
  1607. adc \$0, %rdx
  1608. add %rax, $acc4
  1609. mov $t1, %rax
  1610. adc \$0, %rdx
  1611. mov %rdx, $t0
  1612. mulq 8*2($a_ptr)
  1613. add $t0, $acc5
  1614. adc \$0, %rdx
  1615. add %rax, $acc5
  1616. mov $t1, %rax
  1617. adc \$0, %rdx
  1618. mov %rdx, $t0
  1619. mulq 8*3($a_ptr)
  1620. add $t0, $acc0
  1621. adc \$0, %rdx
  1622. add %rax, $acc0
  1623. mov $acc3, %rax
  1624. adc %rdx, $acc1
  1625. adc \$0, $acc2
  1626. ########################################################################
  1627. # Final reduction step
  1628. mov $acc3, $t1
  1629. shl \$32, $acc3
  1630. mulq $poly3
  1631. shr \$32, $t1
  1632. add $acc3, $acc4
  1633. adc $t1, $acc5
  1634. mov $acc4, $t0
  1635. adc %rax, $acc0
  1636. adc %rdx, $acc1
  1637. mov $acc5, $t1
  1638. adc \$0, $acc2
  1639. ########################################################################
  1640. # Branch-less conditional subtraction of P
  1641. sub \$-1, $acc4 # .Lpoly[0]
  1642. mov $acc0, $t2
  1643. sbb $poly1, $acc5 # .Lpoly[1]
  1644. sbb \$0, $acc0 # .Lpoly[2]
  1645. mov $acc1, $t3
  1646. sbb $poly3, $acc1 # .Lpoly[3]
  1647. sbb \$0, $acc2
  1648. cmovc $t0, $acc4
  1649. cmovc $t1, $acc5
  1650. mov $acc4, 8*0($r_ptr)
  1651. cmovc $t2, $acc0
  1652. mov $acc5, 8*1($r_ptr)
  1653. cmovc $t3, $acc1
  1654. mov $acc0, 8*2($r_ptr)
  1655. mov $acc1, 8*3($r_ptr)
  1656. ret
  1657. .cfi_endproc
  1658. .size __ecp_nistz256_mul_montq,.-__ecp_nistz256_mul_montq
  1659. ################################################################################
  1660. # void ecp_nistz256_sqr_mont(
  1661. # uint64_t res[4],
  1662. # uint64_t a[4]);
  1663. # we optimize the square according to S.Gueron and V.Krasnov,
  1664. # "Speeding up Big-Number Squaring"
  1665. .globl ecp_nistz256_sqr_mont
  1666. .type ecp_nistz256_sqr_mont,\@function,2
  1667. .align 32
  1668. ecp_nistz256_sqr_mont:
  1669. .cfi_startproc
  1670. ___
  1671. $code.=<<___ if ($addx);
  1672. mov \$0x80100, %ecx
  1673. and OPENSSL_ia32cap_P+8(%rip), %ecx
  1674. ___
  1675. $code.=<<___;
  1676. push %rbp
  1677. .cfi_push %rbp
  1678. push %rbx
  1679. .cfi_push %rbx
  1680. push %r12
  1681. .cfi_push %r12
  1682. push %r13
  1683. .cfi_push %r13
  1684. push %r14
  1685. .cfi_push %r14
  1686. push %r15
  1687. .cfi_push %r15
  1688. .Lsqr_body:
  1689. ___
  1690. $code.=<<___ if ($addx);
  1691. cmp \$0x80100, %ecx
  1692. je .Lsqr_montx
  1693. ___
  1694. $code.=<<___;
  1695. mov 8*0($a_ptr), %rax
  1696. mov 8*1($a_ptr), $acc6
  1697. mov 8*2($a_ptr), $acc7
  1698. mov 8*3($a_ptr), $acc0
  1699. call __ecp_nistz256_sqr_montq
  1700. ___
  1701. $code.=<<___ if ($addx);
  1702. jmp .Lsqr_mont_done
  1703. .align 32
  1704. .Lsqr_montx:
  1705. mov 8*0($a_ptr), %rdx
  1706. mov 8*1($a_ptr), $acc6
  1707. mov 8*2($a_ptr), $acc7
  1708. mov 8*3($a_ptr), $acc0
  1709. lea -128($a_ptr), $a_ptr # control u-op density
  1710. call __ecp_nistz256_sqr_montx
  1711. ___
  1712. $code.=<<___;
  1713. .Lsqr_mont_done:
  1714. mov 0(%rsp),%r15
  1715. .cfi_restore %r15
  1716. mov 8(%rsp),%r14
  1717. .cfi_restore %r14
  1718. mov 16(%rsp),%r13
  1719. .cfi_restore %r13
  1720. mov 24(%rsp),%r12
  1721. .cfi_restore %r12
  1722. mov 32(%rsp),%rbx
  1723. .cfi_restore %rbx
  1724. mov 40(%rsp),%rbp
  1725. .cfi_restore %rbp
  1726. lea 48(%rsp),%rsp
  1727. .cfi_adjust_cfa_offset -48
  1728. .Lsqr_epilogue:
  1729. ret
  1730. .cfi_endproc
  1731. .size ecp_nistz256_sqr_mont,.-ecp_nistz256_sqr_mont
  1732. .type __ecp_nistz256_sqr_montq,\@abi-omnipotent
  1733. .align 32
  1734. __ecp_nistz256_sqr_montq:
  1735. .cfi_startproc
  1736. mov %rax, $acc5
  1737. mulq $acc6 # a[1]*a[0]
  1738. mov %rax, $acc1
  1739. mov $acc7, %rax
  1740. mov %rdx, $acc2
  1741. mulq $acc5 # a[0]*a[2]
  1742. add %rax, $acc2
  1743. mov $acc0, %rax
  1744. adc \$0, %rdx
  1745. mov %rdx, $acc3
  1746. mulq $acc5 # a[0]*a[3]
  1747. add %rax, $acc3
  1748. mov $acc7, %rax
  1749. adc \$0, %rdx
  1750. mov %rdx, $acc4
  1751. #################################
  1752. mulq $acc6 # a[1]*a[2]
  1753. add %rax, $acc3
  1754. mov $acc0, %rax
  1755. adc \$0, %rdx
  1756. mov %rdx, $t1
  1757. mulq $acc6 # a[1]*a[3]
  1758. add %rax, $acc4
  1759. mov $acc0, %rax
  1760. adc \$0, %rdx
  1761. add $t1, $acc4
  1762. mov %rdx, $acc5
  1763. adc \$0, $acc5
  1764. #################################
  1765. mulq $acc7 # a[2]*a[3]
  1766. xor $acc7, $acc7
  1767. add %rax, $acc5
  1768. mov 8*0($a_ptr), %rax
  1769. mov %rdx, $acc6
  1770. adc \$0, $acc6
  1771. add $acc1, $acc1 # acc1:6<<1
  1772. adc $acc2, $acc2
  1773. adc $acc3, $acc3
  1774. adc $acc4, $acc4
  1775. adc $acc5, $acc5
  1776. adc $acc6, $acc6
  1777. adc \$0, $acc7
  1778. mulq %rax
  1779. mov %rax, $acc0
  1780. mov 8*1($a_ptr), %rax
  1781. mov %rdx, $t0
  1782. mulq %rax
  1783. add $t0, $acc1
  1784. adc %rax, $acc2
  1785. mov 8*2($a_ptr), %rax
  1786. adc \$0, %rdx
  1787. mov %rdx, $t0
  1788. mulq %rax
  1789. add $t0, $acc3
  1790. adc %rax, $acc4
  1791. mov 8*3($a_ptr), %rax
  1792. adc \$0, %rdx
  1793. mov %rdx, $t0
  1794. mulq %rax
  1795. add $t0, $acc5
  1796. adc %rax, $acc6
  1797. mov $acc0, %rax
  1798. adc %rdx, $acc7
  1799. mov .Lpoly+8*1(%rip), $a_ptr
  1800. mov .Lpoly+8*3(%rip), $t1
  1801. ##########################################
  1802. # Now the reduction
  1803. # First iteration
  1804. mov $acc0, $t0
  1805. shl \$32, $acc0
  1806. mulq $t1
  1807. shr \$32, $t0
  1808. add $acc0, $acc1 # +=acc[0]<<96
  1809. adc $t0, $acc2
  1810. adc %rax, $acc3
  1811. mov $acc1, %rax
  1812. adc \$0, %rdx
  1813. ##########################################
  1814. # Second iteration
  1815. mov $acc1, $t0
  1816. shl \$32, $acc1
  1817. mov %rdx, $acc0
  1818. mulq $t1
  1819. shr \$32, $t0
  1820. add $acc1, $acc2
  1821. adc $t0, $acc3
  1822. adc %rax, $acc0
  1823. mov $acc2, %rax
  1824. adc \$0, %rdx
  1825. ##########################################
  1826. # Third iteration
  1827. mov $acc2, $t0
  1828. shl \$32, $acc2
  1829. mov %rdx, $acc1
  1830. mulq $t1
  1831. shr \$32, $t0
  1832. add $acc2, $acc3
  1833. adc $t0, $acc0
  1834. adc %rax, $acc1
  1835. mov $acc3, %rax
  1836. adc \$0, %rdx
  1837. ###########################################
  1838. # Last iteration
  1839. mov $acc3, $t0
  1840. shl \$32, $acc3
  1841. mov %rdx, $acc2
  1842. mulq $t1
  1843. shr \$32, $t0
  1844. add $acc3, $acc0
  1845. adc $t0, $acc1
  1846. adc %rax, $acc2
  1847. adc \$0, %rdx
  1848. xor $acc3, $acc3
  1849. ############################################
  1850. # Add the rest of the acc
  1851. add $acc0, $acc4
  1852. adc $acc1, $acc5
  1853. mov $acc4, $acc0
  1854. adc $acc2, $acc6
  1855. adc %rdx, $acc7
  1856. mov $acc5, $acc1
  1857. adc \$0, $acc3
  1858. sub \$-1, $acc4 # .Lpoly[0]
  1859. mov $acc6, $acc2
  1860. sbb $a_ptr, $acc5 # .Lpoly[1]
  1861. sbb \$0, $acc6 # .Lpoly[2]
  1862. mov $acc7, $t0
  1863. sbb $t1, $acc7 # .Lpoly[3]
  1864. sbb \$0, $acc3
  1865. cmovc $acc0, $acc4
  1866. cmovc $acc1, $acc5
  1867. mov $acc4, 8*0($r_ptr)
  1868. cmovc $acc2, $acc6
  1869. mov $acc5, 8*1($r_ptr)
  1870. cmovc $t0, $acc7
  1871. mov $acc6, 8*2($r_ptr)
  1872. mov $acc7, 8*3($r_ptr)
  1873. ret
  1874. .cfi_endproc
  1875. .size __ecp_nistz256_sqr_montq,.-__ecp_nistz256_sqr_montq
  1876. ___
  1877. if ($addx) {
  1878. $code.=<<___;
  1879. .type __ecp_nistz256_mul_montx,\@abi-omnipotent
  1880. .align 32
  1881. __ecp_nistz256_mul_montx:
  1882. .cfi_startproc
  1883. ########################################################################
  1884. # Multiply by b[0]
  1885. mulx $acc1, $acc0, $acc1
  1886. mulx $acc2, $t0, $acc2
  1887. mov \$32, $poly1
  1888. xor $acc5, $acc5 # cf=0
  1889. mulx $acc3, $t1, $acc3
  1890. mov .Lpoly+8*3(%rip), $poly3
  1891. adc $t0, $acc1
  1892. mulx $acc4, $t0, $acc4
  1893. mov $acc0, %rdx
  1894. adc $t1, $acc2
  1895. shlx $poly1,$acc0,$t1
  1896. adc $t0, $acc3
  1897. shrx $poly1,$acc0,$t0
  1898. adc \$0, $acc4
  1899. ########################################################################
  1900. # First reduction step
  1901. add $t1, $acc1
  1902. adc $t0, $acc2
  1903. mulx $poly3, $t0, $t1
  1904. mov 8*1($b_ptr), %rdx
  1905. adc $t0, $acc3
  1906. adc $t1, $acc4
  1907. adc \$0, $acc5
  1908. xor $acc0, $acc0 # $acc0=0,cf=0,of=0
  1909. ########################################################################
  1910. # Multiply by b[1]
  1911. mulx 8*0+128($a_ptr), $t0, $t1
  1912. adcx $t0, $acc1
  1913. adox $t1, $acc2
  1914. mulx 8*1+128($a_ptr), $t0, $t1
  1915. adcx $t0, $acc2
  1916. adox $t1, $acc3
  1917. mulx 8*2+128($a_ptr), $t0, $t1
  1918. adcx $t0, $acc3
  1919. adox $t1, $acc4
  1920. mulx 8*3+128($a_ptr), $t0, $t1
  1921. mov $acc1, %rdx
  1922. adcx $t0, $acc4
  1923. shlx $poly1, $acc1, $t0
  1924. adox $t1, $acc5
  1925. shrx $poly1, $acc1, $t1
  1926. adcx $acc0, $acc5
  1927. adox $acc0, $acc0
  1928. adc \$0, $acc0
  1929. ########################################################################
  1930. # Second reduction step
  1931. add $t0, $acc2
  1932. adc $t1, $acc3
  1933. mulx $poly3, $t0, $t1
  1934. mov 8*2($b_ptr), %rdx
  1935. adc $t0, $acc4
  1936. adc $t1, $acc5
  1937. adc \$0, $acc0
  1938. xor $acc1 ,$acc1 # $acc1=0,cf=0,of=0
  1939. ########################################################################
  1940. # Multiply by b[2]
  1941. mulx 8*0+128($a_ptr), $t0, $t1
  1942. adcx $t0, $acc2
  1943. adox $t1, $acc3
  1944. mulx 8*1+128($a_ptr), $t0, $t1
  1945. adcx $t0, $acc3
  1946. adox $t1, $acc4
  1947. mulx 8*2+128($a_ptr), $t0, $t1
  1948. adcx $t0, $acc4
  1949. adox $t1, $acc5
  1950. mulx 8*3+128($a_ptr), $t0, $t1
  1951. mov $acc2, %rdx
  1952. adcx $t0, $acc5
  1953. shlx $poly1, $acc2, $t0
  1954. adox $t1, $acc0
  1955. shrx $poly1, $acc2, $t1
  1956. adcx $acc1, $acc0
  1957. adox $acc1, $acc1
  1958. adc \$0, $acc1
  1959. ########################################################################
  1960. # Third reduction step
  1961. add $t0, $acc3
  1962. adc $t1, $acc4
  1963. mulx $poly3, $t0, $t1
  1964. mov 8*3($b_ptr), %rdx
  1965. adc $t0, $acc5
  1966. adc $t1, $acc0
  1967. adc \$0, $acc1
  1968. xor $acc2, $acc2 # $acc2=0,cf=0,of=0
  1969. ########################################################################
  1970. # Multiply by b[3]
  1971. mulx 8*0+128($a_ptr), $t0, $t1
  1972. adcx $t0, $acc3
  1973. adox $t1, $acc4
  1974. mulx 8*1+128($a_ptr), $t0, $t1
  1975. adcx $t0, $acc4
  1976. adox $t1, $acc5
  1977. mulx 8*2+128($a_ptr), $t0, $t1
  1978. adcx $t0, $acc5
  1979. adox $t1, $acc0
  1980. mulx 8*3+128($a_ptr), $t0, $t1
  1981. mov $acc3, %rdx
  1982. adcx $t0, $acc0
  1983. shlx $poly1, $acc3, $t0
  1984. adox $t1, $acc1
  1985. shrx $poly1, $acc3, $t1
  1986. adcx $acc2, $acc1
  1987. adox $acc2, $acc2
  1988. adc \$0, $acc2
  1989. ########################################################################
  1990. # Fourth reduction step
  1991. add $t0, $acc4
  1992. adc $t1, $acc5
  1993. mulx $poly3, $t0, $t1
  1994. mov $acc4, $t2
  1995. mov .Lpoly+8*1(%rip), $poly1
  1996. adc $t0, $acc0
  1997. mov $acc5, $t3
  1998. adc $t1, $acc1
  1999. adc \$0, $acc2
  2000. ########################################################################
  2001. # Branch-less conditional subtraction of P
  2002. xor %eax, %eax
  2003. mov $acc0, $t0
  2004. sbb \$-1, $acc4 # .Lpoly[0]
  2005. sbb $poly1, $acc5 # .Lpoly[1]
  2006. sbb \$0, $acc0 # .Lpoly[2]
  2007. mov $acc1, $t1
  2008. sbb $poly3, $acc1 # .Lpoly[3]
  2009. sbb \$0, $acc2
  2010. cmovc $t2, $acc4
  2011. cmovc $t3, $acc5
  2012. mov $acc4, 8*0($r_ptr)
  2013. cmovc $t0, $acc0
  2014. mov $acc5, 8*1($r_ptr)
  2015. cmovc $t1, $acc1
  2016. mov $acc0, 8*2($r_ptr)
  2017. mov $acc1, 8*3($r_ptr)
  2018. ret
  2019. .cfi_endproc
  2020. .size __ecp_nistz256_mul_montx,.-__ecp_nistz256_mul_montx
  2021. .type __ecp_nistz256_sqr_montx,\@abi-omnipotent
  2022. .align 32
  2023. __ecp_nistz256_sqr_montx:
  2024. .cfi_startproc
  2025. mulx $acc6, $acc1, $acc2 # a[0]*a[1]
  2026. mulx $acc7, $t0, $acc3 # a[0]*a[2]
  2027. xor %eax, %eax
  2028. adc $t0, $acc2
  2029. mulx $acc0, $t1, $acc4 # a[0]*a[3]
  2030. mov $acc6, %rdx
  2031. adc $t1, $acc3
  2032. adc \$0, $acc4
  2033. xor $acc5, $acc5 # $acc5=0,cf=0,of=0
  2034. #################################
  2035. mulx $acc7, $t0, $t1 # a[1]*a[2]
  2036. adcx $t0, $acc3
  2037. adox $t1, $acc4
  2038. mulx $acc0, $t0, $t1 # a[1]*a[3]
  2039. mov $acc7, %rdx
  2040. adcx $t0, $acc4
  2041. adox $t1, $acc5
  2042. adc \$0, $acc5
  2043. #################################
  2044. mulx $acc0, $t0, $acc6 # a[2]*a[3]
  2045. mov 8*0+128($a_ptr), %rdx
  2046. xor $acc7, $acc7 # $acc7=0,cf=0,of=0
  2047. adcx $acc1, $acc1 # acc1:6<<1
  2048. adox $t0, $acc5
  2049. adcx $acc2, $acc2
  2050. adox $acc7, $acc6 # of=0
  2051. mulx %rdx, $acc0, $t1
  2052. mov 8*1+128($a_ptr), %rdx
  2053. adcx $acc3, $acc3
  2054. adox $t1, $acc1
  2055. adcx $acc4, $acc4
  2056. mulx %rdx, $t0, $t4
  2057. mov 8*2+128($a_ptr), %rdx
  2058. adcx $acc5, $acc5
  2059. adox $t0, $acc2
  2060. adcx $acc6, $acc6
  2061. .byte 0x67
  2062. mulx %rdx, $t0, $t1
  2063. mov 8*3+128($a_ptr), %rdx
  2064. adox $t4, $acc3
  2065. adcx $acc7, $acc7
  2066. adox $t0, $acc4
  2067. mov \$32, $a_ptr
  2068. adox $t1, $acc5
  2069. .byte 0x67,0x67
  2070. mulx %rdx, $t0, $t4
  2071. mov .Lpoly+8*3(%rip), %rdx
  2072. adox $t0, $acc6
  2073. shlx $a_ptr, $acc0, $t0
  2074. adox $t4, $acc7
  2075. shrx $a_ptr, $acc0, $t4
  2076. mov %rdx,$t1
  2077. # reduction step 1
  2078. add $t0, $acc1
  2079. adc $t4, $acc2
  2080. mulx $acc0, $t0, $acc0
  2081. adc $t0, $acc3
  2082. shlx $a_ptr, $acc1, $t0
  2083. adc \$0, $acc0
  2084. shrx $a_ptr, $acc1, $t4
  2085. # reduction step 2
  2086. add $t0, $acc2
  2087. adc $t4, $acc3
  2088. mulx $acc1, $t0, $acc1
  2089. adc $t0, $acc0
  2090. shlx $a_ptr, $acc2, $t0
  2091. adc \$0, $acc1
  2092. shrx $a_ptr, $acc2, $t4
  2093. # reduction step 3
  2094. add $t0, $acc3
  2095. adc $t4, $acc0
  2096. mulx $acc2, $t0, $acc2
  2097. adc $t0, $acc1
  2098. shlx $a_ptr, $acc3, $t0
  2099. adc \$0, $acc2
  2100. shrx $a_ptr, $acc3, $t4
  2101. # reduction step 4
  2102. add $t0, $acc0
  2103. adc $t4, $acc1
  2104. mulx $acc3, $t0, $acc3
  2105. adc $t0, $acc2
  2106. adc \$0, $acc3
  2107. xor $t3, $t3
  2108. add $acc0, $acc4 # accumulate upper half
  2109. mov .Lpoly+8*1(%rip), $a_ptr
  2110. adc $acc1, $acc5
  2111. mov $acc4, $acc0
  2112. adc $acc2, $acc6
  2113. adc $acc3, $acc7
  2114. mov $acc5, $acc1
  2115. adc \$0, $t3
  2116. sub \$-1, $acc4 # .Lpoly[0]
  2117. mov $acc6, $acc2
  2118. sbb $a_ptr, $acc5 # .Lpoly[1]
  2119. sbb \$0, $acc6 # .Lpoly[2]
  2120. mov $acc7, $acc3
  2121. sbb $t1, $acc7 # .Lpoly[3]
  2122. sbb \$0, $t3
  2123. cmovc $acc0, $acc4
  2124. cmovc $acc1, $acc5
  2125. mov $acc4, 8*0($r_ptr)
  2126. cmovc $acc2, $acc6
  2127. mov $acc5, 8*1($r_ptr)
  2128. cmovc $acc3, $acc7
  2129. mov $acc6, 8*2($r_ptr)
  2130. mov $acc7, 8*3($r_ptr)
  2131. ret
  2132. .cfi_endproc
  2133. .size __ecp_nistz256_sqr_montx,.-__ecp_nistz256_sqr_montx
  2134. ___
  2135. }
  2136. }
  2137. {
  2138. my ($r_ptr,$in_ptr)=("%rdi","%rsi");
  2139. my ($acc0,$acc1,$acc2,$acc3)=map("%r$_",(8..11));
  2140. my ($t0,$t1,$t2)=("%rcx","%r12","%r13");
  2141. $code.=<<___;
  2142. ################################################################################
  2143. # void ecp_nistz256_from_mont(
  2144. # uint64_t res[4],
  2145. # uint64_t in[4]);
  2146. # This one performs Montgomery multiplication by 1, so we only need the reduction
  2147. .globl ecp_nistz256_from_mont
  2148. .type ecp_nistz256_from_mont,\@function,2
  2149. .align 32
  2150. ecp_nistz256_from_mont:
  2151. .cfi_startproc
  2152. push %r12
  2153. .cfi_push %r12
  2154. push %r13
  2155. .cfi_push %r13
  2156. .Lfrom_body:
  2157. mov 8*0($in_ptr), %rax
  2158. mov .Lpoly+8*3(%rip), $t2
  2159. mov 8*1($in_ptr), $acc1
  2160. mov 8*2($in_ptr), $acc2
  2161. mov 8*3($in_ptr), $acc3
  2162. mov %rax, $acc0
  2163. mov .Lpoly+8*1(%rip), $t1
  2164. #########################################
  2165. # First iteration
  2166. mov %rax, $t0
  2167. shl \$32, $acc0
  2168. mulq $t2
  2169. shr \$32, $t0
  2170. add $acc0, $acc1
  2171. adc $t0, $acc2
  2172. adc %rax, $acc3
  2173. mov $acc1, %rax
  2174. adc \$0, %rdx
  2175. #########################################
  2176. # Second iteration
  2177. mov $acc1, $t0
  2178. shl \$32, $acc1
  2179. mov %rdx, $acc0
  2180. mulq $t2
  2181. shr \$32, $t0
  2182. add $acc1, $acc2
  2183. adc $t0, $acc3
  2184. adc %rax, $acc0
  2185. mov $acc2, %rax
  2186. adc \$0, %rdx
  2187. ##########################################
  2188. # Third iteration
  2189. mov $acc2, $t0
  2190. shl \$32, $acc2
  2191. mov %rdx, $acc1
  2192. mulq $t2
  2193. shr \$32, $t0
  2194. add $acc2, $acc3
  2195. adc $t0, $acc0
  2196. adc %rax, $acc1
  2197. mov $acc3, %rax
  2198. adc \$0, %rdx
  2199. ###########################################
  2200. # Last iteration
  2201. mov $acc3, $t0
  2202. shl \$32, $acc3
  2203. mov %rdx, $acc2
  2204. mulq $t2
  2205. shr \$32, $t0
  2206. add $acc3, $acc0
  2207. adc $t0, $acc1
  2208. mov $acc0, $t0
  2209. adc %rax, $acc2
  2210. mov $acc1, $in_ptr
  2211. adc \$0, %rdx
  2212. ###########################################
  2213. # Branch-less conditional subtraction
  2214. sub \$-1, $acc0
  2215. mov $acc2, %rax
  2216. sbb $t1, $acc1
  2217. sbb \$0, $acc2
  2218. mov %rdx, $acc3
  2219. sbb $t2, %rdx
  2220. sbb $t2, $t2
  2221. cmovnz $t0, $acc0
  2222. cmovnz $in_ptr, $acc1
  2223. mov $acc0, 8*0($r_ptr)
  2224. cmovnz %rax, $acc2
  2225. mov $acc1, 8*1($r_ptr)
  2226. cmovz %rdx, $acc3
  2227. mov $acc2, 8*2($r_ptr)
  2228. mov $acc3, 8*3($r_ptr)
  2229. mov 0(%rsp),%r13
  2230. .cfi_restore %r13
  2231. mov 8(%rsp),%r12
  2232. .cfi_restore %r12
  2233. lea 16(%rsp),%rsp
  2234. .cfi_adjust_cfa_offset -16
  2235. .Lfrom_epilogue:
  2236. ret
  2237. .cfi_endproc
  2238. .size ecp_nistz256_from_mont,.-ecp_nistz256_from_mont
  2239. ___
  2240. }
  2241. {
  2242. my ($val,$in_t,$index)=$win64?("%rcx","%rdx","%r8d"):("%rdi","%rsi","%edx");
  2243. my ($ONE,$INDEX,$Ra,$Rb,$Rc,$Rd,$Re,$Rf)=map("%xmm$_",(0..7));
  2244. my ($M0,$T0a,$T0b,$T0c,$T0d,$T0e,$T0f,$TMP0)=map("%xmm$_",(8..15));
  2245. my ($M1,$T2a,$T2b,$TMP2,$M2,$T2a,$T2b,$TMP2)=map("%xmm$_",(8..15));
  2246. $code.=<<___;
  2247. ################################################################################
  2248. # void ecp_nistz256_scatter_w5(uint64_t *val, uint64_t *in_t, int index);
  2249. .globl ecp_nistz256_scatter_w5
  2250. .type ecp_nistz256_scatter_w5,\@abi-omnipotent
  2251. .align 32
  2252. ecp_nistz256_scatter_w5:
  2253. .cfi_startproc
  2254. lea -3($index,$index,2), $index
  2255. movdqa 0x00($in_t), %xmm0
  2256. shl \$5, $index
  2257. movdqa 0x10($in_t), %xmm1
  2258. movdqa 0x20($in_t), %xmm2
  2259. movdqa 0x30($in_t), %xmm3
  2260. movdqa 0x40($in_t), %xmm4
  2261. movdqa 0x50($in_t), %xmm5
  2262. movdqa %xmm0, 0x00($val,$index)
  2263. movdqa %xmm1, 0x10($val,$index)
  2264. movdqa %xmm2, 0x20($val,$index)
  2265. movdqa %xmm3, 0x30($val,$index)
  2266. movdqa %xmm4, 0x40($val,$index)
  2267. movdqa %xmm5, 0x50($val,$index)
  2268. ret
  2269. .cfi_endproc
  2270. .size ecp_nistz256_scatter_w5,.-ecp_nistz256_scatter_w5
  2271. ################################################################################
  2272. # void ecp_nistz256_gather_w5(uint64_t *val, uint64_t *in_t, int index);
  2273. .globl ecp_nistz256_gather_w5
  2274. .type ecp_nistz256_gather_w5,\@abi-omnipotent
  2275. .align 32
  2276. ecp_nistz256_gather_w5:
  2277. .cfi_startproc
  2278. ___
  2279. $code.=<<___ if ($avx>1);
  2280. mov OPENSSL_ia32cap_P+8(%rip), %eax
  2281. test \$`1<<5`, %eax
  2282. jnz .Lavx2_gather_w5
  2283. ___
  2284. $code.=<<___ if ($win64);
  2285. lea -0x88(%rsp), %rax
  2286. .LSEH_begin_ecp_nistz256_gather_w5:
  2287. .byte 0x48,0x8d,0x60,0xe0 #lea -0x20(%rax), %rsp
  2288. .byte 0x0f,0x29,0x70,0xe0 #movaps %xmm6, -0x20(%rax)
  2289. .byte 0x0f,0x29,0x78,0xf0 #movaps %xmm7, -0x10(%rax)
  2290. .byte 0x44,0x0f,0x29,0x00 #movaps %xmm8, 0(%rax)
  2291. .byte 0x44,0x0f,0x29,0x48,0x10 #movaps %xmm9, 0x10(%rax)
  2292. .byte 0x44,0x0f,0x29,0x50,0x20 #movaps %xmm10, 0x20(%rax)
  2293. .byte 0x44,0x0f,0x29,0x58,0x30 #movaps %xmm11, 0x30(%rax)
  2294. .byte 0x44,0x0f,0x29,0x60,0x40 #movaps %xmm12, 0x40(%rax)
  2295. .byte 0x44,0x0f,0x29,0x68,0x50 #movaps %xmm13, 0x50(%rax)
  2296. .byte 0x44,0x0f,0x29,0x70,0x60 #movaps %xmm14, 0x60(%rax)
  2297. .byte 0x44,0x0f,0x29,0x78,0x70 #movaps %xmm15, 0x70(%rax)
  2298. ___
  2299. $code.=<<___;
  2300. movdqa .LOne(%rip), $ONE
  2301. movd $index, $INDEX
  2302. pxor $Ra, $Ra
  2303. pxor $Rb, $Rb
  2304. pxor $Rc, $Rc
  2305. pxor $Rd, $Rd
  2306. pxor $Re, $Re
  2307. pxor $Rf, $Rf
  2308. movdqa $ONE, $M0
  2309. pshufd \$0, $INDEX, $INDEX
  2310. mov \$16, %rax
  2311. .Lselect_loop_sse_w5:
  2312. movdqa $M0, $TMP0
  2313. paddd $ONE, $M0
  2314. pcmpeqd $INDEX, $TMP0
  2315. movdqa 16*0($in_t), $T0a
  2316. movdqa 16*1($in_t), $T0b
  2317. movdqa 16*2($in_t), $T0c
  2318. movdqa 16*3($in_t), $T0d
  2319. movdqa 16*4($in_t), $T0e
  2320. movdqa 16*5($in_t), $T0f
  2321. lea 16*6($in_t), $in_t
  2322. pand $TMP0, $T0a
  2323. pand $TMP0, $T0b
  2324. por $T0a, $Ra
  2325. pand $TMP0, $T0c
  2326. por $T0b, $Rb
  2327. pand $TMP0, $T0d
  2328. por $T0c, $Rc
  2329. pand $TMP0, $T0e
  2330. por $T0d, $Rd
  2331. pand $TMP0, $T0f
  2332. por $T0e, $Re
  2333. por $T0f, $Rf
  2334. dec %rax
  2335. jnz .Lselect_loop_sse_w5
  2336. movdqu $Ra, 16*0($val)
  2337. movdqu $Rb, 16*1($val)
  2338. movdqu $Rc, 16*2($val)
  2339. movdqu $Rd, 16*3($val)
  2340. movdqu $Re, 16*4($val)
  2341. movdqu $Rf, 16*5($val)
  2342. ___
  2343. $code.=<<___ if ($win64);
  2344. movaps (%rsp), %xmm6
  2345. movaps 0x10(%rsp), %xmm7
  2346. movaps 0x20(%rsp), %xmm8
  2347. movaps 0x30(%rsp), %xmm9
  2348. movaps 0x40(%rsp), %xmm10
  2349. movaps 0x50(%rsp), %xmm11
  2350. movaps 0x60(%rsp), %xmm12
  2351. movaps 0x70(%rsp), %xmm13
  2352. movaps 0x80(%rsp), %xmm14
  2353. movaps 0x90(%rsp), %xmm15
  2354. lea 0xa8(%rsp), %rsp
  2355. ___
  2356. $code.=<<___;
  2357. ret
  2358. .cfi_endproc
  2359. .LSEH_end_ecp_nistz256_gather_w5:
  2360. .size ecp_nistz256_gather_w5,.-ecp_nistz256_gather_w5
  2361. ################################################################################
  2362. # void ecp_nistz256_scatter_w7(uint64_t *val, uint64_t *in_t, int index);
  2363. .globl ecp_nistz256_scatter_w7
  2364. .type ecp_nistz256_scatter_w7,\@abi-omnipotent
  2365. .align 32
  2366. ecp_nistz256_scatter_w7:
  2367. .cfi_startproc
  2368. movdqu 0x00($in_t), %xmm0
  2369. shl \$6, $index
  2370. movdqu 0x10($in_t), %xmm1
  2371. movdqu 0x20($in_t), %xmm2
  2372. movdqu 0x30($in_t), %xmm3
  2373. movdqa %xmm0, 0x00($val,$index)
  2374. movdqa %xmm1, 0x10($val,$index)
  2375. movdqa %xmm2, 0x20($val,$index)
  2376. movdqa %xmm3, 0x30($val,$index)
  2377. ret
  2378. .cfi_endproc
  2379. .size ecp_nistz256_scatter_w7,.-ecp_nistz256_scatter_w7
  2380. ################################################################################
  2381. # void ecp_nistz256_gather_w7(uint64_t *val, uint64_t *in_t, int index);
  2382. .globl ecp_nistz256_gather_w7
  2383. .type ecp_nistz256_gather_w7,\@abi-omnipotent
  2384. .align 32
  2385. ecp_nistz256_gather_w7:
  2386. .cfi_startproc
  2387. ___
  2388. $code.=<<___ if ($avx>1);
  2389. mov OPENSSL_ia32cap_P+8(%rip), %eax
  2390. test \$`1<<5`, %eax
  2391. jnz .Lavx2_gather_w7
  2392. ___
  2393. $code.=<<___ if ($win64);
  2394. lea -0x88(%rsp), %rax
  2395. .LSEH_begin_ecp_nistz256_gather_w7:
  2396. .byte 0x48,0x8d,0x60,0xe0 #lea -0x20(%rax), %rsp
  2397. .byte 0x0f,0x29,0x70,0xe0 #movaps %xmm6, -0x20(%rax)
  2398. .byte 0x0f,0x29,0x78,0xf0 #movaps %xmm7, -0x10(%rax)
  2399. .byte 0x44,0x0f,0x29,0x00 #movaps %xmm8, 0(%rax)
  2400. .byte 0x44,0x0f,0x29,0x48,0x10 #movaps %xmm9, 0x10(%rax)
  2401. .byte 0x44,0x0f,0x29,0x50,0x20 #movaps %xmm10, 0x20(%rax)
  2402. .byte 0x44,0x0f,0x29,0x58,0x30 #movaps %xmm11, 0x30(%rax)
  2403. .byte 0x44,0x0f,0x29,0x60,0x40 #movaps %xmm12, 0x40(%rax)
  2404. .byte 0x44,0x0f,0x29,0x68,0x50 #movaps %xmm13, 0x50(%rax)
  2405. .byte 0x44,0x0f,0x29,0x70,0x60 #movaps %xmm14, 0x60(%rax)
  2406. .byte 0x44,0x0f,0x29,0x78,0x70 #movaps %xmm15, 0x70(%rax)
  2407. ___
  2408. $code.=<<___;
  2409. movdqa .LOne(%rip), $M0
  2410. movd $index, $INDEX
  2411. pxor $Ra, $Ra
  2412. pxor $Rb, $Rb
  2413. pxor $Rc, $Rc
  2414. pxor $Rd, $Rd
  2415. movdqa $M0, $ONE
  2416. pshufd \$0, $INDEX, $INDEX
  2417. mov \$64, %rax
  2418. .Lselect_loop_sse_w7:
  2419. movdqa $M0, $TMP0
  2420. paddd $ONE, $M0
  2421. movdqa 16*0($in_t), $T0a
  2422. movdqa 16*1($in_t), $T0b
  2423. pcmpeqd $INDEX, $TMP0
  2424. movdqa 16*2($in_t), $T0c
  2425. movdqa 16*3($in_t), $T0d
  2426. lea 16*4($in_t), $in_t
  2427. pand $TMP0, $T0a
  2428. pand $TMP0, $T0b
  2429. por $T0a, $Ra
  2430. pand $TMP0, $T0c
  2431. por $T0b, $Rb
  2432. pand $TMP0, $T0d
  2433. por $T0c, $Rc
  2434. prefetcht0 255($in_t)
  2435. por $T0d, $Rd
  2436. dec %rax
  2437. jnz .Lselect_loop_sse_w7
  2438. movdqu $Ra, 16*0($val)
  2439. movdqu $Rb, 16*1($val)
  2440. movdqu $Rc, 16*2($val)
  2441. movdqu $Rd, 16*3($val)
  2442. ___
  2443. $code.=<<___ if ($win64);
  2444. movaps (%rsp), %xmm6
  2445. movaps 0x10(%rsp), %xmm7
  2446. movaps 0x20(%rsp), %xmm8
  2447. movaps 0x30(%rsp), %xmm9
  2448. movaps 0x40(%rsp), %xmm10
  2449. movaps 0x50(%rsp), %xmm11
  2450. movaps 0x60(%rsp), %xmm12
  2451. movaps 0x70(%rsp), %xmm13
  2452. movaps 0x80(%rsp), %xmm14
  2453. movaps 0x90(%rsp), %xmm15
  2454. lea 0xa8(%rsp), %rsp
  2455. ___
  2456. $code.=<<___;
  2457. ret
  2458. .cfi_endproc
  2459. .LSEH_end_ecp_nistz256_gather_w7:
  2460. .size ecp_nistz256_gather_w7,.-ecp_nistz256_gather_w7
  2461. ___
  2462. }
  2463. if ($avx>1) {
  2464. my ($val,$in_t,$index)=$win64?("%rcx","%rdx","%r8d"):("%rdi","%rsi","%edx");
  2465. my ($TWO,$INDEX,$Ra,$Rb,$Rc)=map("%ymm$_",(0..4));
  2466. my ($M0,$T0a,$T0b,$T0c,$TMP0)=map("%ymm$_",(5..9));
  2467. my ($M1,$T1a,$T1b,$T1c,$TMP1)=map("%ymm$_",(10..14));
  2468. $code.=<<___;
  2469. ################################################################################
  2470. # void ecp_nistz256_avx2_gather_w5(uint64_t *val, uint64_t *in_t, int index);
  2471. .type ecp_nistz256_avx2_gather_w5,\@abi-omnipotent
  2472. .align 32
  2473. ecp_nistz256_avx2_gather_w5:
  2474. .cfi_startproc
  2475. .Lavx2_gather_w5:
  2476. vzeroupper
  2477. ___
  2478. $code.=<<___ if ($win64);
  2479. lea -0x88(%rsp), %rax
  2480. mov %rsp,%r11
  2481. .LSEH_begin_ecp_nistz256_avx2_gather_w5:
  2482. .byte 0x48,0x8d,0x60,0xe0 # lea -0x20(%rax), %rsp
  2483. .byte 0xc5,0xf8,0x29,0x70,0xe0 # vmovaps %xmm6, -0x20(%rax)
  2484. .byte 0xc5,0xf8,0x29,0x78,0xf0 # vmovaps %xmm7, -0x10(%rax)
  2485. .byte 0xc5,0x78,0x29,0x40,0x00 # vmovaps %xmm8, 8(%rax)
  2486. .byte 0xc5,0x78,0x29,0x48,0x10 # vmovaps %xmm9, 0x10(%rax)
  2487. .byte 0xc5,0x78,0x29,0x50,0x20 # vmovaps %xmm10, 0x20(%rax)
  2488. .byte 0xc5,0x78,0x29,0x58,0x30 # vmovaps %xmm11, 0x30(%rax)
  2489. .byte 0xc5,0x78,0x29,0x60,0x40 # vmovaps %xmm12, 0x40(%rax)
  2490. .byte 0xc5,0x78,0x29,0x68,0x50 # vmovaps %xmm13, 0x50(%rax)
  2491. .byte 0xc5,0x78,0x29,0x70,0x60 # vmovaps %xmm14, 0x60(%rax)
  2492. .byte 0xc5,0x78,0x29,0x78,0x70 # vmovaps %xmm15, 0x70(%rax)
  2493. ___
  2494. $code.=<<___;
  2495. vmovdqa .LTwo(%rip), $TWO
  2496. vpxor $Ra, $Ra, $Ra
  2497. vpxor $Rb, $Rb, $Rb
  2498. vpxor $Rc, $Rc, $Rc
  2499. vmovdqa .LOne(%rip), $M0
  2500. vmovdqa .LTwo(%rip), $M1
  2501. vmovd $index, %xmm1
  2502. vpermd $INDEX, $Ra, $INDEX
  2503. mov \$8, %rax
  2504. .Lselect_loop_avx2_w5:
  2505. vmovdqa 32*0($in_t), $T0a
  2506. vmovdqa 32*1($in_t), $T0b
  2507. vmovdqa 32*2($in_t), $T0c
  2508. vmovdqa 32*3($in_t), $T1a
  2509. vmovdqa 32*4($in_t), $T1b
  2510. vmovdqa 32*5($in_t), $T1c
  2511. vpcmpeqd $INDEX, $M0, $TMP0
  2512. vpcmpeqd $INDEX, $M1, $TMP1
  2513. vpaddd $TWO, $M0, $M0
  2514. vpaddd $TWO, $M1, $M1
  2515. lea 32*6($in_t), $in_t
  2516. vpand $TMP0, $T0a, $T0a
  2517. vpand $TMP0, $T0b, $T0b
  2518. vpand $TMP0, $T0c, $T0c
  2519. vpand $TMP1, $T1a, $T1a
  2520. vpand $TMP1, $T1b, $T1b
  2521. vpand $TMP1, $T1c, $T1c
  2522. vpxor $T0a, $Ra, $Ra
  2523. vpxor $T0b, $Rb, $Rb
  2524. vpxor $T0c, $Rc, $Rc
  2525. vpxor $T1a, $Ra, $Ra
  2526. vpxor $T1b, $Rb, $Rb
  2527. vpxor $T1c, $Rc, $Rc
  2528. dec %rax
  2529. jnz .Lselect_loop_avx2_w5
  2530. vmovdqu $Ra, 32*0($val)
  2531. vmovdqu $Rb, 32*1($val)
  2532. vmovdqu $Rc, 32*2($val)
  2533. vzeroupper
  2534. ___
  2535. $code.=<<___ if ($win64);
  2536. movaps (%rsp), %xmm6
  2537. movaps 0x10(%rsp), %xmm7
  2538. movaps 0x20(%rsp), %xmm8
  2539. movaps 0x30(%rsp), %xmm9
  2540. movaps 0x40(%rsp), %xmm10
  2541. movaps 0x50(%rsp), %xmm11
  2542. movaps 0x60(%rsp), %xmm12
  2543. movaps 0x70(%rsp), %xmm13
  2544. movaps 0x80(%rsp), %xmm14
  2545. movaps 0x90(%rsp), %xmm15
  2546. lea (%r11), %rsp
  2547. ___
  2548. $code.=<<___;
  2549. ret
  2550. .cfi_endproc
  2551. .LSEH_end_ecp_nistz256_avx2_gather_w5:
  2552. .size ecp_nistz256_avx2_gather_w5,.-ecp_nistz256_avx2_gather_w5
  2553. ___
  2554. }
  2555. if ($avx>1) {
  2556. my ($val,$in_t,$index)=$win64?("%rcx","%rdx","%r8d"):("%rdi","%rsi","%edx");
  2557. my ($THREE,$INDEX,$Ra,$Rb)=map("%ymm$_",(0..3));
  2558. my ($M0,$T0a,$T0b,$TMP0)=map("%ymm$_",(4..7));
  2559. my ($M1,$T1a,$T1b,$TMP1)=map("%ymm$_",(8..11));
  2560. my ($M2,$T2a,$T2b,$TMP2)=map("%ymm$_",(12..15));
  2561. $code.=<<___;
  2562. ################################################################################
  2563. # void ecp_nistz256_avx2_gather_w7(uint64_t *val, uint64_t *in_t, int index);
  2564. .globl ecp_nistz256_avx2_gather_w7
  2565. .type ecp_nistz256_avx2_gather_w7,\@abi-omnipotent
  2566. .align 32
  2567. ecp_nistz256_avx2_gather_w7:
  2568. .cfi_startproc
  2569. .Lavx2_gather_w7:
  2570. vzeroupper
  2571. ___
  2572. $code.=<<___ if ($win64);
  2573. mov %rsp,%r11
  2574. lea -0x88(%rsp), %rax
  2575. .LSEH_begin_ecp_nistz256_avx2_gather_w7:
  2576. .byte 0x48,0x8d,0x60,0xe0 # lea -0x20(%rax), %rsp
  2577. .byte 0xc5,0xf8,0x29,0x70,0xe0 # vmovaps %xmm6, -0x20(%rax)
  2578. .byte 0xc5,0xf8,0x29,0x78,0xf0 # vmovaps %xmm7, -0x10(%rax)
  2579. .byte 0xc5,0x78,0x29,0x40,0x00 # vmovaps %xmm8, 8(%rax)
  2580. .byte 0xc5,0x78,0x29,0x48,0x10 # vmovaps %xmm9, 0x10(%rax)
  2581. .byte 0xc5,0x78,0x29,0x50,0x20 # vmovaps %xmm10, 0x20(%rax)
  2582. .byte 0xc5,0x78,0x29,0x58,0x30 # vmovaps %xmm11, 0x30(%rax)
  2583. .byte 0xc5,0x78,0x29,0x60,0x40 # vmovaps %xmm12, 0x40(%rax)
  2584. .byte 0xc5,0x78,0x29,0x68,0x50 # vmovaps %xmm13, 0x50(%rax)
  2585. .byte 0xc5,0x78,0x29,0x70,0x60 # vmovaps %xmm14, 0x60(%rax)
  2586. .byte 0xc5,0x78,0x29,0x78,0x70 # vmovaps %xmm15, 0x70(%rax)
  2587. ___
  2588. $code.=<<___;
  2589. vmovdqa .LThree(%rip), $THREE
  2590. vpxor $Ra, $Ra, $Ra
  2591. vpxor $Rb, $Rb, $Rb
  2592. vmovdqa .LOne(%rip), $M0
  2593. vmovdqa .LTwo(%rip), $M1
  2594. vmovdqa .LThree(%rip), $M2
  2595. vmovd $index, %xmm1
  2596. vpermd $INDEX, $Ra, $INDEX
  2597. # Skip index = 0, because it is implicitly the point at infinity
  2598. mov \$21, %rax
  2599. .Lselect_loop_avx2_w7:
  2600. vmovdqa 32*0($in_t), $T0a
  2601. vmovdqa 32*1($in_t), $T0b
  2602. vmovdqa 32*2($in_t), $T1a
  2603. vmovdqa 32*3($in_t), $T1b
  2604. vmovdqa 32*4($in_t), $T2a
  2605. vmovdqa 32*5($in_t), $T2b
  2606. vpcmpeqd $INDEX, $M0, $TMP0
  2607. vpcmpeqd $INDEX, $M1, $TMP1
  2608. vpcmpeqd $INDEX, $M2, $TMP2
  2609. vpaddd $THREE, $M0, $M0
  2610. vpaddd $THREE, $M1, $M1
  2611. vpaddd $THREE, $M2, $M2
  2612. lea 32*6($in_t), $in_t
  2613. vpand $TMP0, $T0a, $T0a
  2614. vpand $TMP0, $T0b, $T0b
  2615. vpand $TMP1, $T1a, $T1a
  2616. vpand $TMP1, $T1b, $T1b
  2617. vpand $TMP2, $T2a, $T2a
  2618. vpand $TMP2, $T2b, $T2b
  2619. vpxor $T0a, $Ra, $Ra
  2620. vpxor $T0b, $Rb, $Rb
  2621. vpxor $T1a, $Ra, $Ra
  2622. vpxor $T1b, $Rb, $Rb
  2623. vpxor $T2a, $Ra, $Ra
  2624. vpxor $T2b, $Rb, $Rb
  2625. dec %rax
  2626. jnz .Lselect_loop_avx2_w7
  2627. vmovdqa 32*0($in_t), $T0a
  2628. vmovdqa 32*1($in_t), $T0b
  2629. vpcmpeqd $INDEX, $M0, $TMP0
  2630. vpand $TMP0, $T0a, $T0a
  2631. vpand $TMP0, $T0b, $T0b
  2632. vpxor $T0a, $Ra, $Ra
  2633. vpxor $T0b, $Rb, $Rb
  2634. vmovdqu $Ra, 32*0($val)
  2635. vmovdqu $Rb, 32*1($val)
  2636. vzeroupper
  2637. ___
  2638. $code.=<<___ if ($win64);
  2639. movaps (%rsp), %xmm6
  2640. movaps 0x10(%rsp), %xmm7
  2641. movaps 0x20(%rsp), %xmm8
  2642. movaps 0x30(%rsp), %xmm9
  2643. movaps 0x40(%rsp), %xmm10
  2644. movaps 0x50(%rsp), %xmm11
  2645. movaps 0x60(%rsp), %xmm12
  2646. movaps 0x70(%rsp), %xmm13
  2647. movaps 0x80(%rsp), %xmm14
  2648. movaps 0x90(%rsp), %xmm15
  2649. lea (%r11), %rsp
  2650. ___
  2651. $code.=<<___;
  2652. ret
  2653. .cfi_endproc
  2654. .LSEH_end_ecp_nistz256_avx2_gather_w7:
  2655. .size ecp_nistz256_avx2_gather_w7,.-ecp_nistz256_avx2_gather_w7
  2656. ___
  2657. } else {
  2658. $code.=<<___;
  2659. .globl ecp_nistz256_avx2_gather_w7
  2660. .type ecp_nistz256_avx2_gather_w7,\@function,3
  2661. .align 32
  2662. ecp_nistz256_avx2_gather_w7:
  2663. .cfi_startproc
  2664. .byte 0x0f,0x0b # ud2
  2665. ret
  2666. .cfi_endproc
  2667. .size ecp_nistz256_avx2_gather_w7,.-ecp_nistz256_avx2_gather_w7
  2668. ___
  2669. }
  2670. {{{
  2671. ########################################################################
  2672. # This block implements higher level point_double, point_add and
  2673. # point_add_affine. The key to performance in this case is to allow
  2674. # out-of-order execution logic to overlap computations from next step
  2675. # with tail processing from current step. By using tailored calling
  2676. # sequence we minimize inter-step overhead to give processor better
  2677. # shot at overlapping operations...
  2678. #
  2679. # You will notice that input data is copied to stack. Trouble is that
  2680. # there are no registers to spare for holding original pointers and
  2681. # reloading them, pointers, would create undesired dependencies on
  2682. # effective addresses calculation paths. In other words it's too done
  2683. # to favour out-of-order execution logic.
  2684. # <appro@openssl.org>
  2685. my ($r_ptr,$a_ptr,$b_org,$b_ptr)=("%rdi","%rsi","%rdx","%rbx");
  2686. my ($acc0,$acc1,$acc2,$acc3,$acc4,$acc5,$acc6,$acc7)=map("%r$_",(8..15));
  2687. my ($t0,$t1,$t2,$t3,$t4)=("%rax","%rbp","%rcx",$acc4,$acc4);
  2688. my ($poly1,$poly3)=($acc6,$acc7);
  2689. sub load_for_mul () {
  2690. my ($a,$b,$src0) = @_;
  2691. my $bias = $src0 eq "%rax" ? 0 : -128;
  2692. " mov $b, $src0
  2693. lea $b, $b_ptr
  2694. mov 8*0+$a, $acc1
  2695. mov 8*1+$a, $acc2
  2696. lea $bias+$a, $a_ptr
  2697. mov 8*2+$a, $acc3
  2698. mov 8*3+$a, $acc4"
  2699. }
  2700. sub load_for_sqr () {
  2701. my ($a,$src0) = @_;
  2702. my $bias = $src0 eq "%rax" ? 0 : -128;
  2703. " mov 8*0+$a, $src0
  2704. mov 8*1+$a, $acc6
  2705. lea $bias+$a, $a_ptr
  2706. mov 8*2+$a, $acc7
  2707. mov 8*3+$a, $acc0"
  2708. }
  2709. {
  2710. ########################################################################
  2711. # operate in 4-5-0-1 "name space" that matches multiplication output
  2712. #
  2713. my ($a0,$a1,$a2,$a3,$t3,$t4)=($acc4,$acc5,$acc0,$acc1,$acc2,$acc3);
  2714. $code.=<<___;
  2715. .type __ecp_nistz256_add_toq,\@abi-omnipotent
  2716. .align 32
  2717. __ecp_nistz256_add_toq:
  2718. .cfi_startproc
  2719. xor $t4,$t4
  2720. add 8*0($b_ptr), $a0
  2721. adc 8*1($b_ptr), $a1
  2722. mov $a0, $t0
  2723. adc 8*2($b_ptr), $a2
  2724. adc 8*3($b_ptr), $a3
  2725. mov $a1, $t1
  2726. adc \$0, $t4
  2727. sub \$-1, $a0
  2728. mov $a2, $t2
  2729. sbb $poly1, $a1
  2730. sbb \$0, $a2
  2731. mov $a3, $t3
  2732. sbb $poly3, $a3
  2733. sbb \$0, $t4
  2734. cmovc $t0, $a0
  2735. cmovc $t1, $a1
  2736. mov $a0, 8*0($r_ptr)
  2737. cmovc $t2, $a2
  2738. mov $a1, 8*1($r_ptr)
  2739. cmovc $t3, $a3
  2740. mov $a2, 8*2($r_ptr)
  2741. mov $a3, 8*3($r_ptr)
  2742. ret
  2743. .cfi_endproc
  2744. .size __ecp_nistz256_add_toq,.-__ecp_nistz256_add_toq
  2745. .type __ecp_nistz256_sub_fromq,\@abi-omnipotent
  2746. .align 32
  2747. __ecp_nistz256_sub_fromq:
  2748. .cfi_startproc
  2749. sub 8*0($b_ptr), $a0
  2750. sbb 8*1($b_ptr), $a1
  2751. mov $a0, $t0
  2752. sbb 8*2($b_ptr), $a2
  2753. sbb 8*3($b_ptr), $a3
  2754. mov $a1, $t1
  2755. sbb $t4, $t4
  2756. add \$-1, $a0
  2757. mov $a2, $t2
  2758. adc $poly1, $a1
  2759. adc \$0, $a2
  2760. mov $a3, $t3
  2761. adc $poly3, $a3
  2762. test $t4, $t4
  2763. cmovz $t0, $a0
  2764. cmovz $t1, $a1
  2765. mov $a0, 8*0($r_ptr)
  2766. cmovz $t2, $a2
  2767. mov $a1, 8*1($r_ptr)
  2768. cmovz $t3, $a3
  2769. mov $a2, 8*2($r_ptr)
  2770. mov $a3, 8*3($r_ptr)
  2771. ret
  2772. .cfi_endproc
  2773. .size __ecp_nistz256_sub_fromq,.-__ecp_nistz256_sub_fromq
  2774. .type __ecp_nistz256_subq,\@abi-omnipotent
  2775. .align 32
  2776. __ecp_nistz256_subq:
  2777. .cfi_startproc
  2778. sub $a0, $t0
  2779. sbb $a1, $t1
  2780. mov $t0, $a0
  2781. sbb $a2, $t2
  2782. sbb $a3, $t3
  2783. mov $t1, $a1
  2784. sbb $t4, $t4
  2785. add \$-1, $t0
  2786. mov $t2, $a2
  2787. adc $poly1, $t1
  2788. adc \$0, $t2
  2789. mov $t3, $a3
  2790. adc $poly3, $t3
  2791. test $t4, $t4
  2792. cmovnz $t0, $a0
  2793. cmovnz $t1, $a1
  2794. cmovnz $t2, $a2
  2795. cmovnz $t3, $a3
  2796. ret
  2797. .cfi_endproc
  2798. .size __ecp_nistz256_subq,.-__ecp_nistz256_subq
  2799. .type __ecp_nistz256_mul_by_2q,\@abi-omnipotent
  2800. .align 32
  2801. __ecp_nistz256_mul_by_2q:
  2802. .cfi_startproc
  2803. xor $t4, $t4
  2804. add $a0, $a0 # a0:a3+a0:a3
  2805. adc $a1, $a1
  2806. mov $a0, $t0
  2807. adc $a2, $a2
  2808. adc $a3, $a3
  2809. mov $a1, $t1
  2810. adc \$0, $t4
  2811. sub \$-1, $a0
  2812. mov $a2, $t2
  2813. sbb $poly1, $a1
  2814. sbb \$0, $a2
  2815. mov $a3, $t3
  2816. sbb $poly3, $a3
  2817. sbb \$0, $t4
  2818. cmovc $t0, $a0
  2819. cmovc $t1, $a1
  2820. mov $a0, 8*0($r_ptr)
  2821. cmovc $t2, $a2
  2822. mov $a1, 8*1($r_ptr)
  2823. cmovc $t3, $a3
  2824. mov $a2, 8*2($r_ptr)
  2825. mov $a3, 8*3($r_ptr)
  2826. ret
  2827. .cfi_endproc
  2828. .size __ecp_nistz256_mul_by_2q,.-__ecp_nistz256_mul_by_2q
  2829. ___
  2830. }
  2831. sub gen_double () {
  2832. my $x = shift;
  2833. my ($src0,$sfx,$bias);
  2834. my ($S,$M,$Zsqr,$in_x,$tmp0)=map(32*$_,(0..4));
  2835. if ($x ne "x") {
  2836. $src0 = "%rax";
  2837. $sfx = "";
  2838. $bias = 0;
  2839. $code.=<<___;
  2840. .globl ecp_nistz256_point_double
  2841. .type ecp_nistz256_point_double,\@function,2
  2842. .align 32
  2843. ecp_nistz256_point_double:
  2844. .cfi_startproc
  2845. ___
  2846. $code.=<<___ if ($addx);
  2847. mov \$0x80100, %ecx
  2848. and OPENSSL_ia32cap_P+8(%rip), %ecx
  2849. cmp \$0x80100, %ecx
  2850. je .Lpoint_doublex
  2851. ___
  2852. } else {
  2853. $src0 = "%rdx";
  2854. $sfx = "x";
  2855. $bias = 128;
  2856. $code.=<<___;
  2857. .type ecp_nistz256_point_doublex,\@function,2
  2858. .align 32
  2859. ecp_nistz256_point_doublex:
  2860. .cfi_startproc
  2861. .Lpoint_doublex:
  2862. ___
  2863. }
  2864. $code.=<<___;
  2865. push %rbp
  2866. .cfi_push %rbp
  2867. push %rbx
  2868. .cfi_push %rbx
  2869. push %r12
  2870. .cfi_push %r12
  2871. push %r13
  2872. .cfi_push %r13
  2873. push %r14
  2874. .cfi_push %r14
  2875. push %r15
  2876. .cfi_push %r15
  2877. sub \$32*5+8, %rsp
  2878. .cfi_adjust_cfa_offset 32*5+8
  2879. .Lpoint_double${x}_body:
  2880. .Lpoint_double_shortcut$x:
  2881. movdqu 0x00($a_ptr), %xmm0 # copy *(P256_POINT *)$a_ptr.x
  2882. mov $a_ptr, $b_ptr # backup copy
  2883. movdqu 0x10($a_ptr), %xmm1
  2884. mov 0x20+8*0($a_ptr), $acc4 # load in_y in "5-4-0-1" order
  2885. mov 0x20+8*1($a_ptr), $acc5
  2886. mov 0x20+8*2($a_ptr), $acc0
  2887. mov 0x20+8*3($a_ptr), $acc1
  2888. mov .Lpoly+8*1(%rip), $poly1
  2889. mov .Lpoly+8*3(%rip), $poly3
  2890. movdqa %xmm0, $in_x(%rsp)
  2891. movdqa %xmm1, $in_x+0x10(%rsp)
  2892. lea 0x20($r_ptr), $acc2
  2893. lea 0x40($r_ptr), $acc3
  2894. movq $r_ptr, %xmm0
  2895. movq $acc2, %xmm1
  2896. movq $acc3, %xmm2
  2897. lea $S(%rsp), $r_ptr
  2898. call __ecp_nistz256_mul_by_2$x # p256_mul_by_2(S, in_y);
  2899. mov 0x40+8*0($a_ptr), $src0
  2900. mov 0x40+8*1($a_ptr), $acc6
  2901. mov 0x40+8*2($a_ptr), $acc7
  2902. mov 0x40+8*3($a_ptr), $acc0
  2903. lea 0x40-$bias($a_ptr), $a_ptr
  2904. lea $Zsqr(%rsp), $r_ptr
  2905. call __ecp_nistz256_sqr_mont$x # p256_sqr_mont(Zsqr, in_z);
  2906. `&load_for_sqr("$S(%rsp)", "$src0")`
  2907. lea $S(%rsp), $r_ptr
  2908. call __ecp_nistz256_sqr_mont$x # p256_sqr_mont(S, S);
  2909. mov 0x20($b_ptr), $src0 # $b_ptr is still valid
  2910. mov 0x40+8*0($b_ptr), $acc1
  2911. mov 0x40+8*1($b_ptr), $acc2
  2912. mov 0x40+8*2($b_ptr), $acc3
  2913. mov 0x40+8*3($b_ptr), $acc4
  2914. lea 0x40-$bias($b_ptr), $a_ptr
  2915. lea 0x20($b_ptr), $b_ptr
  2916. movq %xmm2, $r_ptr
  2917. call __ecp_nistz256_mul_mont$x # p256_mul_mont(res_z, in_z, in_y);
  2918. call __ecp_nistz256_mul_by_2$x # p256_mul_by_2(res_z, res_z);
  2919. mov $in_x+8*0(%rsp), $acc4 # "5-4-0-1" order
  2920. mov $in_x+8*1(%rsp), $acc5
  2921. lea $Zsqr(%rsp), $b_ptr
  2922. mov $in_x+8*2(%rsp), $acc0
  2923. mov $in_x+8*3(%rsp), $acc1
  2924. lea $M(%rsp), $r_ptr
  2925. call __ecp_nistz256_add_to$x # p256_add(M, in_x, Zsqr);
  2926. mov $in_x+8*0(%rsp), $acc4 # "5-4-0-1" order
  2927. mov $in_x+8*1(%rsp), $acc5
  2928. lea $Zsqr(%rsp), $b_ptr
  2929. mov $in_x+8*2(%rsp), $acc0
  2930. mov $in_x+8*3(%rsp), $acc1
  2931. lea $Zsqr(%rsp), $r_ptr
  2932. call __ecp_nistz256_sub_from$x # p256_sub(Zsqr, in_x, Zsqr);
  2933. `&load_for_sqr("$S(%rsp)", "$src0")`
  2934. movq %xmm1, $r_ptr
  2935. call __ecp_nistz256_sqr_mont$x # p256_sqr_mont(res_y, S);
  2936. ___
  2937. {
  2938. ######## ecp_nistz256_div_by_2(res_y, res_y); ##########################
  2939. # operate in 4-5-6-7 "name space" that matches squaring output
  2940. #
  2941. my ($poly1,$poly3)=($a_ptr,$t1);
  2942. my ($a0,$a1,$a2,$a3,$t3,$t4,$t1)=($acc4,$acc5,$acc6,$acc7,$acc0,$acc1,$acc2);
  2943. $code.=<<___;
  2944. xor $t4, $t4
  2945. mov $a0, $t0
  2946. add \$-1, $a0
  2947. mov $a1, $t1
  2948. adc $poly1, $a1
  2949. mov $a2, $t2
  2950. adc \$0, $a2
  2951. mov $a3, $t3
  2952. adc $poly3, $a3
  2953. adc \$0, $t4
  2954. xor $a_ptr, $a_ptr # borrow $a_ptr
  2955. test \$1, $t0
  2956. cmovz $t0, $a0
  2957. cmovz $t1, $a1
  2958. cmovz $t2, $a2
  2959. cmovz $t3, $a3
  2960. cmovz $a_ptr, $t4
  2961. mov $a1, $t0 # a0:a3>>1
  2962. shr \$1, $a0
  2963. shl \$63, $t0
  2964. mov $a2, $t1
  2965. shr \$1, $a1
  2966. or $t0, $a0
  2967. shl \$63, $t1
  2968. mov $a3, $t2
  2969. shr \$1, $a2
  2970. or $t1, $a1
  2971. shl \$63, $t2
  2972. mov $a0, 8*0($r_ptr)
  2973. shr \$1, $a3
  2974. mov $a1, 8*1($r_ptr)
  2975. shl \$63, $t4
  2976. or $t2, $a2
  2977. or $t4, $a3
  2978. mov $a2, 8*2($r_ptr)
  2979. mov $a3, 8*3($r_ptr)
  2980. ___
  2981. }
  2982. $code.=<<___;
  2983. `&load_for_mul("$M(%rsp)", "$Zsqr(%rsp)", "$src0")`
  2984. lea $M(%rsp), $r_ptr
  2985. call __ecp_nistz256_mul_mont$x # p256_mul_mont(M, M, Zsqr);
  2986. lea $tmp0(%rsp), $r_ptr
  2987. call __ecp_nistz256_mul_by_2$x
  2988. lea $M(%rsp), $b_ptr
  2989. lea $M(%rsp), $r_ptr
  2990. call __ecp_nistz256_add_to$x # p256_mul_by_3(M, M);
  2991. `&load_for_mul("$S(%rsp)", "$in_x(%rsp)", "$src0")`
  2992. lea $S(%rsp), $r_ptr
  2993. call __ecp_nistz256_mul_mont$x # p256_mul_mont(S, S, in_x);
  2994. lea $tmp0(%rsp), $r_ptr
  2995. call __ecp_nistz256_mul_by_2$x # p256_mul_by_2(tmp0, S);
  2996. `&load_for_sqr("$M(%rsp)", "$src0")`
  2997. movq %xmm0, $r_ptr
  2998. call __ecp_nistz256_sqr_mont$x # p256_sqr_mont(res_x, M);
  2999. lea $tmp0(%rsp), $b_ptr
  3000. mov $acc6, $acc0 # harmonize sqr output and sub input
  3001. mov $acc7, $acc1
  3002. mov $a_ptr, $poly1
  3003. mov $t1, $poly3
  3004. call __ecp_nistz256_sub_from$x # p256_sub(res_x, res_x, tmp0);
  3005. mov $S+8*0(%rsp), $t0
  3006. mov $S+8*1(%rsp), $t1
  3007. mov $S+8*2(%rsp), $t2
  3008. mov $S+8*3(%rsp), $acc2 # "4-5-0-1" order
  3009. lea $S(%rsp), $r_ptr
  3010. call __ecp_nistz256_sub$x # p256_sub(S, S, res_x);
  3011. mov $M(%rsp), $src0
  3012. lea $M(%rsp), $b_ptr
  3013. mov $acc4, $acc6 # harmonize sub output and mul input
  3014. xor %ecx, %ecx
  3015. mov $acc4, $S+8*0(%rsp) # have to save:-(
  3016. mov $acc5, $acc2
  3017. mov $acc5, $S+8*1(%rsp)
  3018. cmovz $acc0, $acc3
  3019. mov $acc0, $S+8*2(%rsp)
  3020. lea $S-$bias(%rsp), $a_ptr
  3021. cmovz $acc1, $acc4
  3022. mov $acc1, $S+8*3(%rsp)
  3023. mov $acc6, $acc1
  3024. lea $S(%rsp), $r_ptr
  3025. call __ecp_nistz256_mul_mont$x # p256_mul_mont(S, S, M);
  3026. movq %xmm1, $b_ptr
  3027. movq %xmm1, $r_ptr
  3028. call __ecp_nistz256_sub_from$x # p256_sub(res_y, S, res_y);
  3029. lea 32*5+56(%rsp), %rsi
  3030. .cfi_def_cfa %rsi,8
  3031. mov -48(%rsi),%r15
  3032. .cfi_restore %r15
  3033. mov -40(%rsi),%r14
  3034. .cfi_restore %r14
  3035. mov -32(%rsi),%r13
  3036. .cfi_restore %r13
  3037. mov -24(%rsi),%r12
  3038. .cfi_restore %r12
  3039. mov -16(%rsi),%rbx
  3040. .cfi_restore %rbx
  3041. mov -8(%rsi),%rbp
  3042. .cfi_restore %rbp
  3043. lea (%rsi),%rsp
  3044. .cfi_def_cfa_register %rsp
  3045. .Lpoint_double${x}_epilogue:
  3046. ret
  3047. .cfi_endproc
  3048. .size ecp_nistz256_point_double$sfx,.-ecp_nistz256_point_double$sfx
  3049. ___
  3050. }
  3051. &gen_double("q");
  3052. sub gen_add () {
  3053. my $x = shift;
  3054. my ($src0,$sfx,$bias);
  3055. my ($H,$Hsqr,$R,$Rsqr,$Hcub,
  3056. $U1,$U2,$S1,$S2,
  3057. $res_x,$res_y,$res_z,
  3058. $in1_x,$in1_y,$in1_z,
  3059. $in2_x,$in2_y,$in2_z)=map(32*$_,(0..17));
  3060. my ($Z1sqr, $Z2sqr) = ($Hsqr, $Rsqr);
  3061. if ($x ne "x") {
  3062. $src0 = "%rax";
  3063. $sfx = "";
  3064. $bias = 0;
  3065. $code.=<<___;
  3066. .globl ecp_nistz256_point_add
  3067. .type ecp_nistz256_point_add,\@function,3
  3068. .align 32
  3069. ecp_nistz256_point_add:
  3070. .cfi_startproc
  3071. ___
  3072. $code.=<<___ if ($addx);
  3073. mov \$0x80100, %ecx
  3074. and OPENSSL_ia32cap_P+8(%rip), %ecx
  3075. cmp \$0x80100, %ecx
  3076. je .Lpoint_addx
  3077. ___
  3078. } else {
  3079. $src0 = "%rdx";
  3080. $sfx = "x";
  3081. $bias = 128;
  3082. $code.=<<___;
  3083. .type ecp_nistz256_point_addx,\@function,3
  3084. .align 32
  3085. ecp_nistz256_point_addx:
  3086. .cfi_startproc
  3087. .Lpoint_addx:
  3088. ___
  3089. }
  3090. $code.=<<___;
  3091. push %rbp
  3092. .cfi_push %rbp
  3093. push %rbx
  3094. .cfi_push %rbx
  3095. push %r12
  3096. .cfi_push %r12
  3097. push %r13
  3098. .cfi_push %r13
  3099. push %r14
  3100. .cfi_push %r14
  3101. push %r15
  3102. .cfi_push %r15
  3103. sub \$32*18+8, %rsp
  3104. .cfi_adjust_cfa_offset 32*18+8
  3105. .Lpoint_add${x}_body:
  3106. movdqu 0x00($a_ptr), %xmm0 # copy *(P256_POINT *)$a_ptr
  3107. movdqu 0x10($a_ptr), %xmm1
  3108. movdqu 0x20($a_ptr), %xmm2
  3109. movdqu 0x30($a_ptr), %xmm3
  3110. movdqu 0x40($a_ptr), %xmm4
  3111. movdqu 0x50($a_ptr), %xmm5
  3112. mov $a_ptr, $b_ptr # reassign
  3113. mov $b_org, $a_ptr # reassign
  3114. movdqa %xmm0, $in1_x(%rsp)
  3115. movdqa %xmm1, $in1_x+0x10(%rsp)
  3116. movdqa %xmm2, $in1_y(%rsp)
  3117. movdqa %xmm3, $in1_y+0x10(%rsp)
  3118. movdqa %xmm4, $in1_z(%rsp)
  3119. movdqa %xmm5, $in1_z+0x10(%rsp)
  3120. por %xmm4, %xmm5
  3121. movdqu 0x00($a_ptr), %xmm0 # copy *(P256_POINT *)$b_ptr
  3122. pshufd \$0xb1, %xmm5, %xmm3
  3123. movdqu 0x10($a_ptr), %xmm1
  3124. movdqu 0x20($a_ptr), %xmm2
  3125. por %xmm3, %xmm5
  3126. movdqu 0x30($a_ptr), %xmm3
  3127. mov 0x40+8*0($a_ptr), $src0 # load original in2_z
  3128. mov 0x40+8*1($a_ptr), $acc6
  3129. mov 0x40+8*2($a_ptr), $acc7
  3130. mov 0x40+8*3($a_ptr), $acc0
  3131. movdqa %xmm0, $in2_x(%rsp)
  3132. pshufd \$0x1e, %xmm5, %xmm4
  3133. movdqa %xmm1, $in2_x+0x10(%rsp)
  3134. movdqu 0x40($a_ptr),%xmm0 # in2_z again
  3135. movdqu 0x50($a_ptr),%xmm1
  3136. movdqa %xmm2, $in2_y(%rsp)
  3137. movdqa %xmm3, $in2_y+0x10(%rsp)
  3138. por %xmm4, %xmm5
  3139. pxor %xmm4, %xmm4
  3140. por %xmm0, %xmm1
  3141. movq $r_ptr, %xmm0 # save $r_ptr
  3142. lea 0x40-$bias($a_ptr), $a_ptr # $a_ptr is still valid
  3143. mov $src0, $in2_z+8*0(%rsp) # make in2_z copy
  3144. mov $acc6, $in2_z+8*1(%rsp)
  3145. mov $acc7, $in2_z+8*2(%rsp)
  3146. mov $acc0, $in2_z+8*3(%rsp)
  3147. lea $Z2sqr(%rsp), $r_ptr # Z2^2
  3148. call __ecp_nistz256_sqr_mont$x # p256_sqr_mont(Z2sqr, in2_z);
  3149. pcmpeqd %xmm4, %xmm5
  3150. pshufd \$0xb1, %xmm1, %xmm4
  3151. por %xmm1, %xmm4
  3152. pshufd \$0, %xmm5, %xmm5 # in1infty
  3153. pshufd \$0x1e, %xmm4, %xmm3
  3154. por %xmm3, %xmm4
  3155. pxor %xmm3, %xmm3
  3156. pcmpeqd %xmm3, %xmm4
  3157. pshufd \$0, %xmm4, %xmm4 # in2infty
  3158. mov 0x40+8*0($b_ptr), $src0 # load original in1_z
  3159. mov 0x40+8*1($b_ptr), $acc6
  3160. mov 0x40+8*2($b_ptr), $acc7
  3161. mov 0x40+8*3($b_ptr), $acc0
  3162. movq $b_ptr, %xmm1
  3163. lea 0x40-$bias($b_ptr), $a_ptr
  3164. lea $Z1sqr(%rsp), $r_ptr # Z1^2
  3165. call __ecp_nistz256_sqr_mont$x # p256_sqr_mont(Z1sqr, in1_z);
  3166. `&load_for_mul("$Z2sqr(%rsp)", "$in2_z(%rsp)", "$src0")`
  3167. lea $S1(%rsp), $r_ptr # S1 = Z2^3
  3168. call __ecp_nistz256_mul_mont$x # p256_mul_mont(S1, Z2sqr, in2_z);
  3169. `&load_for_mul("$Z1sqr(%rsp)", "$in1_z(%rsp)", "$src0")`
  3170. lea $S2(%rsp), $r_ptr # S2 = Z1^3
  3171. call __ecp_nistz256_mul_mont$x # p256_mul_mont(S2, Z1sqr, in1_z);
  3172. `&load_for_mul("$S1(%rsp)", "$in1_y(%rsp)", "$src0")`
  3173. lea $S1(%rsp), $r_ptr # S1 = Y1*Z2^3
  3174. call __ecp_nistz256_mul_mont$x # p256_mul_mont(S1, S1, in1_y);
  3175. `&load_for_mul("$S2(%rsp)", "$in2_y(%rsp)", "$src0")`
  3176. lea $S2(%rsp), $r_ptr # S2 = Y2*Z1^3
  3177. call __ecp_nistz256_mul_mont$x # p256_mul_mont(S2, S2, in2_y);
  3178. lea $S1(%rsp), $b_ptr
  3179. lea $R(%rsp), $r_ptr # R = S2 - S1
  3180. call __ecp_nistz256_sub_from$x # p256_sub(R, S2, S1);
  3181. or $acc5, $acc4 # see if result is zero
  3182. movdqa %xmm4, %xmm2
  3183. or $acc0, $acc4
  3184. or $acc1, $acc4
  3185. por %xmm5, %xmm2 # in1infty || in2infty
  3186. movq $acc4, %xmm3
  3187. `&load_for_mul("$Z2sqr(%rsp)", "$in1_x(%rsp)", "$src0")`
  3188. lea $U1(%rsp), $r_ptr # U1 = X1*Z2^2
  3189. call __ecp_nistz256_mul_mont$x # p256_mul_mont(U1, in1_x, Z2sqr);
  3190. `&load_for_mul("$Z1sqr(%rsp)", "$in2_x(%rsp)", "$src0")`
  3191. lea $U2(%rsp), $r_ptr # U2 = X2*Z1^2
  3192. call __ecp_nistz256_mul_mont$x # p256_mul_mont(U2, in2_x, Z1sqr);
  3193. lea $U1(%rsp), $b_ptr
  3194. lea $H(%rsp), $r_ptr # H = U2 - U1
  3195. call __ecp_nistz256_sub_from$x # p256_sub(H, U2, U1);
  3196. or $acc5, $acc4 # see if result is zero
  3197. or $acc0, $acc4
  3198. or $acc1, $acc4 # !is_equal(U1, U2)
  3199. movq %xmm2, $acc0 # in1infty | in2infty
  3200. movq %xmm3, $acc1 # !is_equal(S1, S2)
  3201. or $acc0, $acc4
  3202. or $acc1, $acc4
  3203. # if (!is_equal(U1, U2) | in1infty | in2infty | !is_equal(S1, S2))
  3204. .byte 0x3e # predict taken
  3205. jnz .Ladd_proceed$x
  3206. .Ladd_double$x:
  3207. movq %xmm1, $a_ptr # restore $a_ptr
  3208. movq %xmm0, $r_ptr # restore $r_ptr
  3209. add \$`32*(18-5)`, %rsp # difference in frame sizes
  3210. .cfi_adjust_cfa_offset `-32*(18-5)`
  3211. jmp .Lpoint_double_shortcut$x
  3212. .cfi_adjust_cfa_offset `32*(18-5)`
  3213. .align 32
  3214. .Ladd_proceed$x:
  3215. `&load_for_sqr("$R(%rsp)", "$src0")`
  3216. lea $Rsqr(%rsp), $r_ptr # R^2
  3217. call __ecp_nistz256_sqr_mont$x # p256_sqr_mont(Rsqr, R);
  3218. `&load_for_mul("$H(%rsp)", "$in1_z(%rsp)", "$src0")`
  3219. lea $res_z(%rsp), $r_ptr # Z3 = H*Z1*Z2
  3220. call __ecp_nistz256_mul_mont$x # p256_mul_mont(res_z, H, in1_z);
  3221. `&load_for_sqr("$H(%rsp)", "$src0")`
  3222. lea $Hsqr(%rsp), $r_ptr # H^2
  3223. call __ecp_nistz256_sqr_mont$x # p256_sqr_mont(Hsqr, H);
  3224. `&load_for_mul("$res_z(%rsp)", "$in2_z(%rsp)", "$src0")`
  3225. lea $res_z(%rsp), $r_ptr # Z3 = H*Z1*Z2
  3226. call __ecp_nistz256_mul_mont$x # p256_mul_mont(res_z, res_z, in2_z);
  3227. `&load_for_mul("$Hsqr(%rsp)", "$H(%rsp)", "$src0")`
  3228. lea $Hcub(%rsp), $r_ptr # H^3
  3229. call __ecp_nistz256_mul_mont$x # p256_mul_mont(Hcub, Hsqr, H);
  3230. `&load_for_mul("$Hsqr(%rsp)", "$U1(%rsp)", "$src0")`
  3231. lea $U2(%rsp), $r_ptr # U1*H^2
  3232. call __ecp_nistz256_mul_mont$x # p256_mul_mont(U2, U1, Hsqr);
  3233. ___
  3234. {
  3235. #######################################################################
  3236. # operate in 4-5-0-1 "name space" that matches multiplication output
  3237. #
  3238. my ($acc0,$acc1,$acc2,$acc3,$t3,$t4)=($acc4,$acc5,$acc0,$acc1,$acc2,$acc3);
  3239. my ($poly1, $poly3)=($acc6,$acc7);
  3240. $code.=<<___;
  3241. #lea $U2(%rsp), $a_ptr
  3242. #lea $Hsqr(%rsp), $r_ptr # 2*U1*H^2
  3243. #call __ecp_nistz256_mul_by_2 # ecp_nistz256_mul_by_2(Hsqr, U2);
  3244. xor $t4, $t4
  3245. add $acc0, $acc0 # a0:a3+a0:a3
  3246. lea $Rsqr(%rsp), $a_ptr
  3247. adc $acc1, $acc1
  3248. mov $acc0, $t0
  3249. adc $acc2, $acc2
  3250. adc $acc3, $acc3
  3251. mov $acc1, $t1
  3252. adc \$0, $t4
  3253. sub \$-1, $acc0
  3254. mov $acc2, $t2
  3255. sbb $poly1, $acc1
  3256. sbb \$0, $acc2
  3257. mov $acc3, $t3
  3258. sbb $poly3, $acc3
  3259. sbb \$0, $t4
  3260. cmovc $t0, $acc0
  3261. mov 8*0($a_ptr), $t0
  3262. cmovc $t1, $acc1
  3263. mov 8*1($a_ptr), $t1
  3264. cmovc $t2, $acc2
  3265. mov 8*2($a_ptr), $t2
  3266. cmovc $t3, $acc3
  3267. mov 8*3($a_ptr), $t3
  3268. call __ecp_nistz256_sub$x # p256_sub(res_x, Rsqr, Hsqr);
  3269. lea $Hcub(%rsp), $b_ptr
  3270. lea $res_x(%rsp), $r_ptr
  3271. call __ecp_nistz256_sub_from$x # p256_sub(res_x, res_x, Hcub);
  3272. mov $U2+8*0(%rsp), $t0
  3273. mov $U2+8*1(%rsp), $t1
  3274. mov $U2+8*2(%rsp), $t2
  3275. mov $U2+8*3(%rsp), $t3
  3276. lea $res_y(%rsp), $r_ptr
  3277. call __ecp_nistz256_sub$x # p256_sub(res_y, U2, res_x);
  3278. mov $acc0, 8*0($r_ptr) # save the result, as
  3279. mov $acc1, 8*1($r_ptr) # __ecp_nistz256_sub doesn't
  3280. mov $acc2, 8*2($r_ptr)
  3281. mov $acc3, 8*3($r_ptr)
  3282. ___
  3283. }
  3284. $code.=<<___;
  3285. `&load_for_mul("$S1(%rsp)", "$Hcub(%rsp)", "$src0")`
  3286. lea $S2(%rsp), $r_ptr
  3287. call __ecp_nistz256_mul_mont$x # p256_mul_mont(S2, S1, Hcub);
  3288. `&load_for_mul("$R(%rsp)", "$res_y(%rsp)", "$src0")`
  3289. lea $res_y(%rsp), $r_ptr
  3290. call __ecp_nistz256_mul_mont$x # p256_mul_mont(res_y, R, res_y);
  3291. lea $S2(%rsp), $b_ptr
  3292. lea $res_y(%rsp), $r_ptr
  3293. call __ecp_nistz256_sub_from$x # p256_sub(res_y, res_y, S2);
  3294. movq %xmm0, $r_ptr # restore $r_ptr
  3295. movdqa %xmm5, %xmm0 # copy_conditional(res_z, in2_z, in1infty);
  3296. movdqa %xmm5, %xmm1
  3297. pandn $res_z(%rsp), %xmm0
  3298. movdqa %xmm5, %xmm2
  3299. pandn $res_z+0x10(%rsp), %xmm1
  3300. movdqa %xmm5, %xmm3
  3301. pand $in2_z(%rsp), %xmm2
  3302. pand $in2_z+0x10(%rsp), %xmm3
  3303. por %xmm0, %xmm2
  3304. por %xmm1, %xmm3
  3305. movdqa %xmm4, %xmm0 # copy_conditional(res_z, in1_z, in2infty);
  3306. movdqa %xmm4, %xmm1
  3307. pandn %xmm2, %xmm0
  3308. movdqa %xmm4, %xmm2
  3309. pandn %xmm3, %xmm1
  3310. movdqa %xmm4, %xmm3
  3311. pand $in1_z(%rsp), %xmm2
  3312. pand $in1_z+0x10(%rsp), %xmm3
  3313. por %xmm0, %xmm2
  3314. por %xmm1, %xmm3
  3315. movdqu %xmm2, 0x40($r_ptr)
  3316. movdqu %xmm3, 0x50($r_ptr)
  3317. movdqa %xmm5, %xmm0 # copy_conditional(res_x, in2_x, in1infty);
  3318. movdqa %xmm5, %xmm1
  3319. pandn $res_x(%rsp), %xmm0
  3320. movdqa %xmm5, %xmm2
  3321. pandn $res_x+0x10(%rsp), %xmm1
  3322. movdqa %xmm5, %xmm3
  3323. pand $in2_x(%rsp), %xmm2
  3324. pand $in2_x+0x10(%rsp), %xmm3
  3325. por %xmm0, %xmm2
  3326. por %xmm1, %xmm3
  3327. movdqa %xmm4, %xmm0 # copy_conditional(res_x, in1_x, in2infty);
  3328. movdqa %xmm4, %xmm1
  3329. pandn %xmm2, %xmm0
  3330. movdqa %xmm4, %xmm2
  3331. pandn %xmm3, %xmm1
  3332. movdqa %xmm4, %xmm3
  3333. pand $in1_x(%rsp), %xmm2
  3334. pand $in1_x+0x10(%rsp), %xmm3
  3335. por %xmm0, %xmm2
  3336. por %xmm1, %xmm3
  3337. movdqu %xmm2, 0x00($r_ptr)
  3338. movdqu %xmm3, 0x10($r_ptr)
  3339. movdqa %xmm5, %xmm0 # copy_conditional(res_y, in2_y, in1infty);
  3340. movdqa %xmm5, %xmm1
  3341. pandn $res_y(%rsp), %xmm0
  3342. movdqa %xmm5, %xmm2
  3343. pandn $res_y+0x10(%rsp), %xmm1
  3344. movdqa %xmm5, %xmm3
  3345. pand $in2_y(%rsp), %xmm2
  3346. pand $in2_y+0x10(%rsp), %xmm3
  3347. por %xmm0, %xmm2
  3348. por %xmm1, %xmm3
  3349. movdqa %xmm4, %xmm0 # copy_conditional(res_y, in1_y, in2infty);
  3350. movdqa %xmm4, %xmm1
  3351. pandn %xmm2, %xmm0
  3352. movdqa %xmm4, %xmm2
  3353. pandn %xmm3, %xmm1
  3354. movdqa %xmm4, %xmm3
  3355. pand $in1_y(%rsp), %xmm2
  3356. pand $in1_y+0x10(%rsp), %xmm3
  3357. por %xmm0, %xmm2
  3358. por %xmm1, %xmm3
  3359. movdqu %xmm2, 0x20($r_ptr)
  3360. movdqu %xmm3, 0x30($r_ptr)
  3361. .Ladd_done$x:
  3362. lea 32*18+56(%rsp), %rsi
  3363. .cfi_def_cfa %rsi,8
  3364. mov -48(%rsi),%r15
  3365. .cfi_restore %r15
  3366. mov -40(%rsi),%r14
  3367. .cfi_restore %r14
  3368. mov -32(%rsi),%r13
  3369. .cfi_restore %r13
  3370. mov -24(%rsi),%r12
  3371. .cfi_restore %r12
  3372. mov -16(%rsi),%rbx
  3373. .cfi_restore %rbx
  3374. mov -8(%rsi),%rbp
  3375. .cfi_restore %rbp
  3376. lea (%rsi),%rsp
  3377. .cfi_def_cfa_register %rsp
  3378. .Lpoint_add${x}_epilogue:
  3379. ret
  3380. .cfi_endproc
  3381. .size ecp_nistz256_point_add$sfx,.-ecp_nistz256_point_add$sfx
  3382. ___
  3383. }
  3384. &gen_add("q");
  3385. sub gen_add_affine () {
  3386. my $x = shift;
  3387. my ($src0,$sfx,$bias);
  3388. my ($U2,$S2,$H,$R,$Hsqr,$Hcub,$Rsqr,
  3389. $res_x,$res_y,$res_z,
  3390. $in1_x,$in1_y,$in1_z,
  3391. $in2_x,$in2_y)=map(32*$_,(0..14));
  3392. my $Z1sqr = $S2;
  3393. if ($x ne "x") {
  3394. $src0 = "%rax";
  3395. $sfx = "";
  3396. $bias = 0;
  3397. $code.=<<___;
  3398. .globl ecp_nistz256_point_add_affine
  3399. .type ecp_nistz256_point_add_affine,\@function,3
  3400. .align 32
  3401. ecp_nistz256_point_add_affine:
  3402. .cfi_startproc
  3403. ___
  3404. $code.=<<___ if ($addx);
  3405. mov \$0x80100, %ecx
  3406. and OPENSSL_ia32cap_P+8(%rip), %ecx
  3407. cmp \$0x80100, %ecx
  3408. je .Lpoint_add_affinex
  3409. ___
  3410. } else {
  3411. $src0 = "%rdx";
  3412. $sfx = "x";
  3413. $bias = 128;
  3414. $code.=<<___;
  3415. .type ecp_nistz256_point_add_affinex,\@function,3
  3416. .align 32
  3417. ecp_nistz256_point_add_affinex:
  3418. .cfi_startproc
  3419. .Lpoint_add_affinex:
  3420. ___
  3421. }
  3422. $code.=<<___;
  3423. push %rbp
  3424. .cfi_push %rbp
  3425. push %rbx
  3426. .cfi_push %rbx
  3427. push %r12
  3428. .cfi_push %r12
  3429. push %r13
  3430. .cfi_push %r13
  3431. push %r14
  3432. .cfi_push %r14
  3433. push %r15
  3434. .cfi_push %r15
  3435. sub \$32*15+8, %rsp
  3436. .cfi_adjust_cfa_offset 32*15+8
  3437. .Ladd_affine${x}_body:
  3438. movdqu 0x00($a_ptr), %xmm0 # copy *(P256_POINT *)$a_ptr
  3439. mov $b_org, $b_ptr # reassign
  3440. movdqu 0x10($a_ptr), %xmm1
  3441. movdqu 0x20($a_ptr), %xmm2
  3442. movdqu 0x30($a_ptr), %xmm3
  3443. movdqu 0x40($a_ptr), %xmm4
  3444. movdqu 0x50($a_ptr), %xmm5
  3445. mov 0x40+8*0($a_ptr), $src0 # load original in1_z
  3446. mov 0x40+8*1($a_ptr), $acc6
  3447. mov 0x40+8*2($a_ptr), $acc7
  3448. mov 0x40+8*3($a_ptr), $acc0
  3449. movdqa %xmm0, $in1_x(%rsp)
  3450. movdqa %xmm1, $in1_x+0x10(%rsp)
  3451. movdqa %xmm2, $in1_y(%rsp)
  3452. movdqa %xmm3, $in1_y+0x10(%rsp)
  3453. movdqa %xmm4, $in1_z(%rsp)
  3454. movdqa %xmm5, $in1_z+0x10(%rsp)
  3455. por %xmm4, %xmm5
  3456. movdqu 0x00($b_ptr), %xmm0 # copy *(P256_POINT_AFFINE *)$b_ptr
  3457. pshufd \$0xb1, %xmm5, %xmm3
  3458. movdqu 0x10($b_ptr), %xmm1
  3459. movdqu 0x20($b_ptr), %xmm2
  3460. por %xmm3, %xmm5
  3461. movdqu 0x30($b_ptr), %xmm3
  3462. movdqa %xmm0, $in2_x(%rsp)
  3463. pshufd \$0x1e, %xmm5, %xmm4
  3464. movdqa %xmm1, $in2_x+0x10(%rsp)
  3465. por %xmm0, %xmm1
  3466. movq $r_ptr, %xmm0 # save $r_ptr
  3467. movdqa %xmm2, $in2_y(%rsp)
  3468. movdqa %xmm3, $in2_y+0x10(%rsp)
  3469. por %xmm2, %xmm3
  3470. por %xmm4, %xmm5
  3471. pxor %xmm4, %xmm4
  3472. por %xmm1, %xmm3
  3473. lea 0x40-$bias($a_ptr), $a_ptr # $a_ptr is still valid
  3474. lea $Z1sqr(%rsp), $r_ptr # Z1^2
  3475. call __ecp_nistz256_sqr_mont$x # p256_sqr_mont(Z1sqr, in1_z);
  3476. pcmpeqd %xmm4, %xmm5
  3477. pshufd \$0xb1, %xmm3, %xmm4
  3478. mov 0x00($b_ptr), $src0 # $b_ptr is still valid
  3479. #lea 0x00($b_ptr), $b_ptr
  3480. mov $acc4, $acc1 # harmonize sqr output and mul input
  3481. por %xmm3, %xmm4
  3482. pshufd \$0, %xmm5, %xmm5 # in1infty
  3483. pshufd \$0x1e, %xmm4, %xmm3
  3484. mov $acc5, $acc2
  3485. por %xmm3, %xmm4
  3486. pxor %xmm3, %xmm3
  3487. mov $acc6, $acc3
  3488. pcmpeqd %xmm3, %xmm4
  3489. pshufd \$0, %xmm4, %xmm4 # in2infty
  3490. lea $Z1sqr-$bias(%rsp), $a_ptr
  3491. mov $acc7, $acc4
  3492. lea $U2(%rsp), $r_ptr # U2 = X2*Z1^2
  3493. call __ecp_nistz256_mul_mont$x # p256_mul_mont(U2, Z1sqr, in2_x);
  3494. lea $in1_x(%rsp), $b_ptr
  3495. lea $H(%rsp), $r_ptr # H = U2 - U1
  3496. call __ecp_nistz256_sub_from$x # p256_sub(H, U2, in1_x);
  3497. `&load_for_mul("$Z1sqr(%rsp)", "$in1_z(%rsp)", "$src0")`
  3498. lea $S2(%rsp), $r_ptr # S2 = Z1^3
  3499. call __ecp_nistz256_mul_mont$x # p256_mul_mont(S2, Z1sqr, in1_z);
  3500. `&load_for_mul("$H(%rsp)", "$in1_z(%rsp)", "$src0")`
  3501. lea $res_z(%rsp), $r_ptr # Z3 = H*Z1*Z2
  3502. call __ecp_nistz256_mul_mont$x # p256_mul_mont(res_z, H, in1_z);
  3503. `&load_for_mul("$S2(%rsp)", "$in2_y(%rsp)", "$src0")`
  3504. lea $S2(%rsp), $r_ptr # S2 = Y2*Z1^3
  3505. call __ecp_nistz256_mul_mont$x # p256_mul_mont(S2, S2, in2_y);
  3506. lea $in1_y(%rsp), $b_ptr
  3507. lea $R(%rsp), $r_ptr # R = S2 - S1
  3508. call __ecp_nistz256_sub_from$x # p256_sub(R, S2, in1_y);
  3509. `&load_for_sqr("$H(%rsp)", "$src0")`
  3510. lea $Hsqr(%rsp), $r_ptr # H^2
  3511. call __ecp_nistz256_sqr_mont$x # p256_sqr_mont(Hsqr, H);
  3512. `&load_for_sqr("$R(%rsp)", "$src0")`
  3513. lea $Rsqr(%rsp), $r_ptr # R^2
  3514. call __ecp_nistz256_sqr_mont$x # p256_sqr_mont(Rsqr, R);
  3515. `&load_for_mul("$H(%rsp)", "$Hsqr(%rsp)", "$src0")`
  3516. lea $Hcub(%rsp), $r_ptr # H^3
  3517. call __ecp_nistz256_mul_mont$x # p256_mul_mont(Hcub, Hsqr, H);
  3518. `&load_for_mul("$Hsqr(%rsp)", "$in1_x(%rsp)", "$src0")`
  3519. lea $U2(%rsp), $r_ptr # U1*H^2
  3520. call __ecp_nistz256_mul_mont$x # p256_mul_mont(U2, in1_x, Hsqr);
  3521. ___
  3522. {
  3523. #######################################################################
  3524. # operate in 4-5-0-1 "name space" that matches multiplication output
  3525. #
  3526. my ($acc0,$acc1,$acc2,$acc3,$t3,$t4)=($acc4,$acc5,$acc0,$acc1,$acc2,$acc3);
  3527. my ($poly1, $poly3)=($acc6,$acc7);
  3528. $code.=<<___;
  3529. #lea $U2(%rsp), $a_ptr
  3530. #lea $Hsqr(%rsp), $r_ptr # 2*U1*H^2
  3531. #call __ecp_nistz256_mul_by_2 # ecp_nistz256_mul_by_2(Hsqr, U2);
  3532. xor $t4, $t4
  3533. add $acc0, $acc0 # a0:a3+a0:a3
  3534. lea $Rsqr(%rsp), $a_ptr
  3535. adc $acc1, $acc1
  3536. mov $acc0, $t0
  3537. adc $acc2, $acc2
  3538. adc $acc3, $acc3
  3539. mov $acc1, $t1
  3540. adc \$0, $t4
  3541. sub \$-1, $acc0
  3542. mov $acc2, $t2
  3543. sbb $poly1, $acc1
  3544. sbb \$0, $acc2
  3545. mov $acc3, $t3
  3546. sbb $poly3, $acc3
  3547. sbb \$0, $t4
  3548. cmovc $t0, $acc0
  3549. mov 8*0($a_ptr), $t0
  3550. cmovc $t1, $acc1
  3551. mov 8*1($a_ptr), $t1
  3552. cmovc $t2, $acc2
  3553. mov 8*2($a_ptr), $t2
  3554. cmovc $t3, $acc3
  3555. mov 8*3($a_ptr), $t3
  3556. call __ecp_nistz256_sub$x # p256_sub(res_x, Rsqr, Hsqr);
  3557. lea $Hcub(%rsp), $b_ptr
  3558. lea $res_x(%rsp), $r_ptr
  3559. call __ecp_nistz256_sub_from$x # p256_sub(res_x, res_x, Hcub);
  3560. mov $U2+8*0(%rsp), $t0
  3561. mov $U2+8*1(%rsp), $t1
  3562. mov $U2+8*2(%rsp), $t2
  3563. mov $U2+8*3(%rsp), $t3
  3564. lea $H(%rsp), $r_ptr
  3565. call __ecp_nistz256_sub$x # p256_sub(H, U2, res_x);
  3566. mov $acc0, 8*0($r_ptr) # save the result, as
  3567. mov $acc1, 8*1($r_ptr) # __ecp_nistz256_sub doesn't
  3568. mov $acc2, 8*2($r_ptr)
  3569. mov $acc3, 8*3($r_ptr)
  3570. ___
  3571. }
  3572. $code.=<<___;
  3573. `&load_for_mul("$Hcub(%rsp)", "$in1_y(%rsp)", "$src0")`
  3574. lea $S2(%rsp), $r_ptr
  3575. call __ecp_nistz256_mul_mont$x # p256_mul_mont(S2, Hcub, in1_y);
  3576. `&load_for_mul("$H(%rsp)", "$R(%rsp)", "$src0")`
  3577. lea $H(%rsp), $r_ptr
  3578. call __ecp_nistz256_mul_mont$x # p256_mul_mont(H, H, R);
  3579. lea $S2(%rsp), $b_ptr
  3580. lea $res_y(%rsp), $r_ptr
  3581. call __ecp_nistz256_sub_from$x # p256_sub(res_y, H, S2);
  3582. movq %xmm0, $r_ptr # restore $r_ptr
  3583. movdqa %xmm5, %xmm0 # copy_conditional(res_z, ONE, in1infty);
  3584. movdqa %xmm5, %xmm1
  3585. pandn $res_z(%rsp), %xmm0
  3586. movdqa %xmm5, %xmm2
  3587. pandn $res_z+0x10(%rsp), %xmm1
  3588. movdqa %xmm5, %xmm3
  3589. pand .LONE_mont(%rip), %xmm2
  3590. pand .LONE_mont+0x10(%rip), %xmm3
  3591. por %xmm0, %xmm2
  3592. por %xmm1, %xmm3
  3593. movdqa %xmm4, %xmm0 # copy_conditional(res_z, in1_z, in2infty);
  3594. movdqa %xmm4, %xmm1
  3595. pandn %xmm2, %xmm0
  3596. movdqa %xmm4, %xmm2
  3597. pandn %xmm3, %xmm1
  3598. movdqa %xmm4, %xmm3
  3599. pand $in1_z(%rsp), %xmm2
  3600. pand $in1_z+0x10(%rsp), %xmm3
  3601. por %xmm0, %xmm2
  3602. por %xmm1, %xmm3
  3603. movdqu %xmm2, 0x40($r_ptr)
  3604. movdqu %xmm3, 0x50($r_ptr)
  3605. movdqa %xmm5, %xmm0 # copy_conditional(res_x, in2_x, in1infty);
  3606. movdqa %xmm5, %xmm1
  3607. pandn $res_x(%rsp), %xmm0
  3608. movdqa %xmm5, %xmm2
  3609. pandn $res_x+0x10(%rsp), %xmm1
  3610. movdqa %xmm5, %xmm3
  3611. pand $in2_x(%rsp), %xmm2
  3612. pand $in2_x+0x10(%rsp), %xmm3
  3613. por %xmm0, %xmm2
  3614. por %xmm1, %xmm3
  3615. movdqa %xmm4, %xmm0 # copy_conditional(res_x, in1_x, in2infty);
  3616. movdqa %xmm4, %xmm1
  3617. pandn %xmm2, %xmm0
  3618. movdqa %xmm4, %xmm2
  3619. pandn %xmm3, %xmm1
  3620. movdqa %xmm4, %xmm3
  3621. pand $in1_x(%rsp), %xmm2
  3622. pand $in1_x+0x10(%rsp), %xmm3
  3623. por %xmm0, %xmm2
  3624. por %xmm1, %xmm3
  3625. movdqu %xmm2, 0x00($r_ptr)
  3626. movdqu %xmm3, 0x10($r_ptr)
  3627. movdqa %xmm5, %xmm0 # copy_conditional(res_y, in2_y, in1infty);
  3628. movdqa %xmm5, %xmm1
  3629. pandn $res_y(%rsp), %xmm0
  3630. movdqa %xmm5, %xmm2
  3631. pandn $res_y+0x10(%rsp), %xmm1
  3632. movdqa %xmm5, %xmm3
  3633. pand $in2_y(%rsp), %xmm2
  3634. pand $in2_y+0x10(%rsp), %xmm3
  3635. por %xmm0, %xmm2
  3636. por %xmm1, %xmm3
  3637. movdqa %xmm4, %xmm0 # copy_conditional(res_y, in1_y, in2infty);
  3638. movdqa %xmm4, %xmm1
  3639. pandn %xmm2, %xmm0
  3640. movdqa %xmm4, %xmm2
  3641. pandn %xmm3, %xmm1
  3642. movdqa %xmm4, %xmm3
  3643. pand $in1_y(%rsp), %xmm2
  3644. pand $in1_y+0x10(%rsp), %xmm3
  3645. por %xmm0, %xmm2
  3646. por %xmm1, %xmm3
  3647. movdqu %xmm2, 0x20($r_ptr)
  3648. movdqu %xmm3, 0x30($r_ptr)
  3649. lea 32*15+56(%rsp), %rsi
  3650. .cfi_def_cfa %rsi,8
  3651. mov -48(%rsi),%r15
  3652. .cfi_restore %r15
  3653. mov -40(%rsi),%r14
  3654. .cfi_restore %r14
  3655. mov -32(%rsi),%r13
  3656. .cfi_restore %r13
  3657. mov -24(%rsi),%r12
  3658. .cfi_restore %r12
  3659. mov -16(%rsi),%rbx
  3660. .cfi_restore %rbx
  3661. mov -8(%rsi),%rbp
  3662. .cfi_restore %rbp
  3663. lea (%rsi),%rsp
  3664. .cfi_def_cfa_register %rsp
  3665. .Ladd_affine${x}_epilogue:
  3666. ret
  3667. .cfi_endproc
  3668. .size ecp_nistz256_point_add_affine$sfx,.-ecp_nistz256_point_add_affine$sfx
  3669. ___
  3670. }
  3671. &gen_add_affine("q");
  3672. ########################################################################
  3673. # AD*X magic
  3674. #
  3675. if ($addx) { {
  3676. ########################################################################
  3677. # operate in 4-5-0-1 "name space" that matches multiplication output
  3678. #
  3679. my ($a0,$a1,$a2,$a3,$t3,$t4)=($acc4,$acc5,$acc0,$acc1,$acc2,$acc3);
  3680. $code.=<<___;
  3681. .type __ecp_nistz256_add_tox,\@abi-omnipotent
  3682. .align 32
  3683. __ecp_nistz256_add_tox:
  3684. .cfi_startproc
  3685. xor $t4, $t4
  3686. adc 8*0($b_ptr), $a0
  3687. adc 8*1($b_ptr), $a1
  3688. mov $a0, $t0
  3689. adc 8*2($b_ptr), $a2
  3690. adc 8*3($b_ptr), $a3
  3691. mov $a1, $t1
  3692. adc \$0, $t4
  3693. xor $t3, $t3
  3694. sbb \$-1, $a0
  3695. mov $a2, $t2
  3696. sbb $poly1, $a1
  3697. sbb \$0, $a2
  3698. mov $a3, $t3
  3699. sbb $poly3, $a3
  3700. sbb \$0, $t4
  3701. cmovc $t0, $a0
  3702. cmovc $t1, $a1
  3703. mov $a0, 8*0($r_ptr)
  3704. cmovc $t2, $a2
  3705. mov $a1, 8*1($r_ptr)
  3706. cmovc $t3, $a3
  3707. mov $a2, 8*2($r_ptr)
  3708. mov $a3, 8*3($r_ptr)
  3709. ret
  3710. .cfi_endproc
  3711. .size __ecp_nistz256_add_tox,.-__ecp_nistz256_add_tox
  3712. .type __ecp_nistz256_sub_fromx,\@abi-omnipotent
  3713. .align 32
  3714. __ecp_nistz256_sub_fromx:
  3715. .cfi_startproc
  3716. xor $t4, $t4
  3717. sbb 8*0($b_ptr), $a0
  3718. sbb 8*1($b_ptr), $a1
  3719. mov $a0, $t0
  3720. sbb 8*2($b_ptr), $a2
  3721. sbb 8*3($b_ptr), $a3
  3722. mov $a1, $t1
  3723. sbb \$0, $t4
  3724. xor $t3, $t3
  3725. adc \$-1, $a0
  3726. mov $a2, $t2
  3727. adc $poly1, $a1
  3728. adc \$0, $a2
  3729. mov $a3, $t3
  3730. adc $poly3, $a3
  3731. bt \$0, $t4
  3732. cmovnc $t0, $a0
  3733. cmovnc $t1, $a1
  3734. mov $a0, 8*0($r_ptr)
  3735. cmovnc $t2, $a2
  3736. mov $a1, 8*1($r_ptr)
  3737. cmovnc $t3, $a3
  3738. mov $a2, 8*2($r_ptr)
  3739. mov $a3, 8*3($r_ptr)
  3740. ret
  3741. .cfi_endproc
  3742. .size __ecp_nistz256_sub_fromx,.-__ecp_nistz256_sub_fromx
  3743. .type __ecp_nistz256_subx,\@abi-omnipotent
  3744. .align 32
  3745. __ecp_nistz256_subx:
  3746. .cfi_startproc
  3747. xor $t4, $t4
  3748. sbb $a0, $t0
  3749. sbb $a1, $t1
  3750. mov $t0, $a0
  3751. sbb $a2, $t2
  3752. sbb $a3, $t3
  3753. mov $t1, $a1
  3754. sbb \$0, $t4
  3755. xor $a3 ,$a3
  3756. adc \$-1, $t0
  3757. mov $t2, $a2
  3758. adc $poly1, $t1
  3759. adc \$0, $t2
  3760. mov $t3, $a3
  3761. adc $poly3, $t3
  3762. bt \$0, $t4
  3763. cmovc $t0, $a0
  3764. cmovc $t1, $a1
  3765. cmovc $t2, $a2
  3766. cmovc $t3, $a3
  3767. ret
  3768. .cfi_endproc
  3769. .size __ecp_nistz256_subx,.-__ecp_nistz256_subx
  3770. .type __ecp_nistz256_mul_by_2x,\@abi-omnipotent
  3771. .align 32
  3772. __ecp_nistz256_mul_by_2x:
  3773. .cfi_startproc
  3774. xor $t4, $t4
  3775. adc $a0, $a0 # a0:a3+a0:a3
  3776. adc $a1, $a1
  3777. mov $a0, $t0
  3778. adc $a2, $a2
  3779. adc $a3, $a3
  3780. mov $a1, $t1
  3781. adc \$0, $t4
  3782. xor $t3, $t3
  3783. sbb \$-1, $a0
  3784. mov $a2, $t2
  3785. sbb $poly1, $a1
  3786. sbb \$0, $a2
  3787. mov $a3, $t3
  3788. sbb $poly3, $a3
  3789. sbb \$0, $t4
  3790. cmovc $t0, $a0
  3791. cmovc $t1, $a1
  3792. mov $a0, 8*0($r_ptr)
  3793. cmovc $t2, $a2
  3794. mov $a1, 8*1($r_ptr)
  3795. cmovc $t3, $a3
  3796. mov $a2, 8*2($r_ptr)
  3797. mov $a3, 8*3($r_ptr)
  3798. ret
  3799. .cfi_endproc
  3800. .size __ecp_nistz256_mul_by_2x,.-__ecp_nistz256_mul_by_2x
  3801. ___
  3802. }
  3803. &gen_double("x");
  3804. &gen_add("x");
  3805. &gen_add_affine("x");
  3806. }
  3807. }}}
  3808. # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
  3809. # CONTEXT *context,DISPATCHER_CONTEXT *disp)
  3810. if ($win64) {
  3811. $rec="%rcx";
  3812. $frame="%rdx";
  3813. $context="%r8";
  3814. $disp="%r9";
  3815. $code.=<<___;
  3816. .extern __imp_RtlVirtualUnwind
  3817. .type short_handler,\@abi-omnipotent
  3818. .align 16
  3819. short_handler:
  3820. push %rsi
  3821. push %rdi
  3822. push %rbx
  3823. push %rbp
  3824. push %r12
  3825. push %r13
  3826. push %r14
  3827. push %r15
  3828. pushfq
  3829. sub \$64,%rsp
  3830. mov 120($context),%rax # pull context->Rax
  3831. mov 248($context),%rbx # pull context->Rip
  3832. mov 8($disp),%rsi # disp->ImageBase
  3833. mov 56($disp),%r11 # disp->HandlerData
  3834. mov 0(%r11),%r10d # HandlerData[0]
  3835. lea (%rsi,%r10),%r10 # end of prologue label
  3836. cmp %r10,%rbx # context->Rip<end of prologue label
  3837. jb .Lcommon_seh_tail
  3838. mov 152($context),%rax # pull context->Rsp
  3839. mov 4(%r11),%r10d # HandlerData[1]
  3840. lea (%rsi,%r10),%r10 # epilogue label
  3841. cmp %r10,%rbx # context->Rip>=epilogue label
  3842. jae .Lcommon_seh_tail
  3843. lea 16(%rax),%rax
  3844. mov -8(%rax),%r12
  3845. mov -16(%rax),%r13
  3846. mov %r12,216($context) # restore context->R12
  3847. mov %r13,224($context) # restore context->R13
  3848. jmp .Lcommon_seh_tail
  3849. .size short_handler,.-short_handler
  3850. .type full_handler,\@abi-omnipotent
  3851. .align 16
  3852. full_handler:
  3853. push %rsi
  3854. push %rdi
  3855. push %rbx
  3856. push %rbp
  3857. push %r12
  3858. push %r13
  3859. push %r14
  3860. push %r15
  3861. pushfq
  3862. sub \$64,%rsp
  3863. mov 120($context),%rax # pull context->Rax
  3864. mov 248($context),%rbx # pull context->Rip
  3865. mov 8($disp),%rsi # disp->ImageBase
  3866. mov 56($disp),%r11 # disp->HandlerData
  3867. mov 0(%r11),%r10d # HandlerData[0]
  3868. lea (%rsi,%r10),%r10 # end of prologue label
  3869. cmp %r10,%rbx # context->Rip<end of prologue label
  3870. jb .Lcommon_seh_tail
  3871. mov 152($context),%rax # pull context->Rsp
  3872. mov 4(%r11),%r10d # HandlerData[1]
  3873. lea (%rsi,%r10),%r10 # epilogue label
  3874. cmp %r10,%rbx # context->Rip>=epilogue label
  3875. jae .Lcommon_seh_tail
  3876. mov 8(%r11),%r10d # HandlerData[2]
  3877. lea (%rax,%r10),%rax
  3878. mov -8(%rax),%rbp
  3879. mov -16(%rax),%rbx
  3880. mov -24(%rax),%r12
  3881. mov -32(%rax),%r13
  3882. mov -40(%rax),%r14
  3883. mov -48(%rax),%r15
  3884. mov %rbx,144($context) # restore context->Rbx
  3885. mov %rbp,160($context) # restore context->Rbp
  3886. mov %r12,216($context) # restore context->R12
  3887. mov %r13,224($context) # restore context->R13
  3888. mov %r14,232($context) # restore context->R14
  3889. mov %r15,240($context) # restore context->R15
  3890. .Lcommon_seh_tail:
  3891. mov 8(%rax),%rdi
  3892. mov 16(%rax),%rsi
  3893. mov %rax,152($context) # restore context->Rsp
  3894. mov %rsi,168($context) # restore context->Rsi
  3895. mov %rdi,176($context) # restore context->Rdi
  3896. mov 40($disp),%rdi # disp->ContextRecord
  3897. mov $context,%rsi # context
  3898. mov \$154,%ecx # sizeof(CONTEXT)
  3899. .long 0xa548f3fc # cld; rep movsq
  3900. mov $disp,%rsi
  3901. xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
  3902. mov 8(%rsi),%rdx # arg2, disp->ImageBase
  3903. mov 0(%rsi),%r8 # arg3, disp->ControlPc
  3904. mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
  3905. mov 40(%rsi),%r10 # disp->ContextRecord
  3906. lea 56(%rsi),%r11 # &disp->HandlerData
  3907. lea 24(%rsi),%r12 # &disp->EstablisherFrame
  3908. mov %r10,32(%rsp) # arg5
  3909. mov %r11,40(%rsp) # arg6
  3910. mov %r12,48(%rsp) # arg7
  3911. mov %rcx,56(%rsp) # arg8, (NULL)
  3912. call *__imp_RtlVirtualUnwind(%rip)
  3913. mov \$1,%eax # ExceptionContinueSearch
  3914. add \$64,%rsp
  3915. popfq
  3916. pop %r15
  3917. pop %r14
  3918. pop %r13
  3919. pop %r12
  3920. pop %rbp
  3921. pop %rbx
  3922. pop %rdi
  3923. pop %rsi
  3924. ret
  3925. .size full_handler,.-full_handler
  3926. .section .pdata
  3927. .align 4
  3928. .rva .LSEH_begin_ecp_nistz256_mul_by_2
  3929. .rva .LSEH_end_ecp_nistz256_mul_by_2
  3930. .rva .LSEH_info_ecp_nistz256_mul_by_2
  3931. .rva .LSEH_begin_ecp_nistz256_div_by_2
  3932. .rva .LSEH_end_ecp_nistz256_div_by_2
  3933. .rva .LSEH_info_ecp_nistz256_div_by_2
  3934. .rva .LSEH_begin_ecp_nistz256_mul_by_3
  3935. .rva .LSEH_end_ecp_nistz256_mul_by_3
  3936. .rva .LSEH_info_ecp_nistz256_mul_by_3
  3937. .rva .LSEH_begin_ecp_nistz256_add
  3938. .rva .LSEH_end_ecp_nistz256_add
  3939. .rva .LSEH_info_ecp_nistz256_add
  3940. .rva .LSEH_begin_ecp_nistz256_sub
  3941. .rva .LSEH_end_ecp_nistz256_sub
  3942. .rva .LSEH_info_ecp_nistz256_sub
  3943. .rva .LSEH_begin_ecp_nistz256_neg
  3944. .rva .LSEH_end_ecp_nistz256_neg
  3945. .rva .LSEH_info_ecp_nistz256_neg
  3946. .rva .LSEH_begin_ecp_nistz256_ord_mul_mont
  3947. .rva .LSEH_end_ecp_nistz256_ord_mul_mont
  3948. .rva .LSEH_info_ecp_nistz256_ord_mul_mont
  3949. .rva .LSEH_begin_ecp_nistz256_ord_sqr_mont
  3950. .rva .LSEH_end_ecp_nistz256_ord_sqr_mont
  3951. .rva .LSEH_info_ecp_nistz256_ord_sqr_mont
  3952. ___
  3953. $code.=<<___ if ($addx);
  3954. .rva .LSEH_begin_ecp_nistz256_ord_mul_montx
  3955. .rva .LSEH_end_ecp_nistz256_ord_mul_montx
  3956. .rva .LSEH_info_ecp_nistz256_ord_mul_montx
  3957. .rva .LSEH_begin_ecp_nistz256_ord_sqr_montx
  3958. .rva .LSEH_end_ecp_nistz256_ord_sqr_montx
  3959. .rva .LSEH_info_ecp_nistz256_ord_sqr_montx
  3960. ___
  3961. $code.=<<___;
  3962. .rva .LSEH_begin_ecp_nistz256_to_mont
  3963. .rva .LSEH_end_ecp_nistz256_to_mont
  3964. .rva .LSEH_info_ecp_nistz256_to_mont
  3965. .rva .LSEH_begin_ecp_nistz256_mul_mont
  3966. .rva .LSEH_end_ecp_nistz256_mul_mont
  3967. .rva .LSEH_info_ecp_nistz256_mul_mont
  3968. .rva .LSEH_begin_ecp_nistz256_sqr_mont
  3969. .rva .LSEH_end_ecp_nistz256_sqr_mont
  3970. .rva .LSEH_info_ecp_nistz256_sqr_mont
  3971. .rva .LSEH_begin_ecp_nistz256_from_mont
  3972. .rva .LSEH_end_ecp_nistz256_from_mont
  3973. .rva .LSEH_info_ecp_nistz256_from_mont
  3974. .rva .LSEH_begin_ecp_nistz256_gather_w5
  3975. .rva .LSEH_end_ecp_nistz256_gather_w5
  3976. .rva .LSEH_info_ecp_nistz256_gather_wX
  3977. .rva .LSEH_begin_ecp_nistz256_gather_w7
  3978. .rva .LSEH_end_ecp_nistz256_gather_w7
  3979. .rva .LSEH_info_ecp_nistz256_gather_wX
  3980. ___
  3981. $code.=<<___ if ($avx>1);
  3982. .rva .LSEH_begin_ecp_nistz256_avx2_gather_w5
  3983. .rva .LSEH_end_ecp_nistz256_avx2_gather_w5
  3984. .rva .LSEH_info_ecp_nistz256_avx2_gather_wX
  3985. .rva .LSEH_begin_ecp_nistz256_avx2_gather_w7
  3986. .rva .LSEH_end_ecp_nistz256_avx2_gather_w7
  3987. .rva .LSEH_info_ecp_nistz256_avx2_gather_wX
  3988. ___
  3989. $code.=<<___;
  3990. .rva .LSEH_begin_ecp_nistz256_point_double
  3991. .rva .LSEH_end_ecp_nistz256_point_double
  3992. .rva .LSEH_info_ecp_nistz256_point_double
  3993. .rva .LSEH_begin_ecp_nistz256_point_add
  3994. .rva .LSEH_end_ecp_nistz256_point_add
  3995. .rva .LSEH_info_ecp_nistz256_point_add
  3996. .rva .LSEH_begin_ecp_nistz256_point_add_affine
  3997. .rva .LSEH_end_ecp_nistz256_point_add_affine
  3998. .rva .LSEH_info_ecp_nistz256_point_add_affine
  3999. ___
  4000. $code.=<<___ if ($addx);
  4001. .rva .LSEH_begin_ecp_nistz256_point_doublex
  4002. .rva .LSEH_end_ecp_nistz256_point_doublex
  4003. .rva .LSEH_info_ecp_nistz256_point_doublex
  4004. .rva .LSEH_begin_ecp_nistz256_point_addx
  4005. .rva .LSEH_end_ecp_nistz256_point_addx
  4006. .rva .LSEH_info_ecp_nistz256_point_addx
  4007. .rva .LSEH_begin_ecp_nistz256_point_add_affinex
  4008. .rva .LSEH_end_ecp_nistz256_point_add_affinex
  4009. .rva .LSEH_info_ecp_nistz256_point_add_affinex
  4010. ___
  4011. $code.=<<___;
  4012. .section .xdata
  4013. .align 8
  4014. .LSEH_info_ecp_nistz256_mul_by_2:
  4015. .byte 9,0,0,0
  4016. .rva short_handler
  4017. .rva .Lmul_by_2_body,.Lmul_by_2_epilogue # HandlerData[]
  4018. .LSEH_info_ecp_nistz256_div_by_2:
  4019. .byte 9,0,0,0
  4020. .rva short_handler
  4021. .rva .Ldiv_by_2_body,.Ldiv_by_2_epilogue # HandlerData[]
  4022. .LSEH_info_ecp_nistz256_mul_by_3:
  4023. .byte 9,0,0,0
  4024. .rva short_handler
  4025. .rva .Lmul_by_3_body,.Lmul_by_3_epilogue # HandlerData[]
  4026. .LSEH_info_ecp_nistz256_add:
  4027. .byte 9,0,0,0
  4028. .rva short_handler
  4029. .rva .Ladd_body,.Ladd_epilogue # HandlerData[]
  4030. .LSEH_info_ecp_nistz256_sub:
  4031. .byte 9,0,0,0
  4032. .rva short_handler
  4033. .rva .Lsub_body,.Lsub_epilogue # HandlerData[]
  4034. .LSEH_info_ecp_nistz256_neg:
  4035. .byte 9,0,0,0
  4036. .rva short_handler
  4037. .rva .Lneg_body,.Lneg_epilogue # HandlerData[]
  4038. .LSEH_info_ecp_nistz256_ord_mul_mont:
  4039. .byte 9,0,0,0
  4040. .rva full_handler
  4041. .rva .Lord_mul_body,.Lord_mul_epilogue # HandlerData[]
  4042. .long 48,0
  4043. .LSEH_info_ecp_nistz256_ord_sqr_mont:
  4044. .byte 9,0,0,0
  4045. .rva full_handler
  4046. .rva .Lord_sqr_body,.Lord_sqr_epilogue # HandlerData[]
  4047. .long 48,0
  4048. ___
  4049. $code.=<<___ if ($addx);
  4050. .LSEH_info_ecp_nistz256_ord_mul_montx:
  4051. .byte 9,0,0,0
  4052. .rva full_handler
  4053. .rva .Lord_mulx_body,.Lord_mulx_epilogue # HandlerData[]
  4054. .long 48,0
  4055. .LSEH_info_ecp_nistz256_ord_sqr_montx:
  4056. .byte 9,0,0,0
  4057. .rva full_handler
  4058. .rva .Lord_sqrx_body,.Lord_sqrx_epilogue # HandlerData[]
  4059. .long 48,0
  4060. ___
  4061. $code.=<<___;
  4062. .LSEH_info_ecp_nistz256_to_mont:
  4063. .byte 9,0,0,0
  4064. .rva full_handler
  4065. .rva .Lmul_body,.Lmul_epilogue # HandlerData[]
  4066. .long 48,0
  4067. .LSEH_info_ecp_nistz256_mul_mont:
  4068. .byte 9,0,0,0
  4069. .rva full_handler
  4070. .rva .Lmul_body,.Lmul_epilogue # HandlerData[]
  4071. .long 48,0
  4072. .LSEH_info_ecp_nistz256_sqr_mont:
  4073. .byte 9,0,0,0
  4074. .rva full_handler
  4075. .rva .Lsqr_body,.Lsqr_epilogue # HandlerData[]
  4076. .long 48,0
  4077. .LSEH_info_ecp_nistz256_from_mont:
  4078. .byte 9,0,0,0
  4079. .rva short_handler
  4080. .rva .Lfrom_body,.Lfrom_epilogue # HandlerData[]
  4081. .LSEH_info_ecp_nistz256_gather_wX:
  4082. .byte 0x01,0x33,0x16,0x00
  4083. .byte 0x33,0xf8,0x09,0x00 #movaps 0x90(rsp),xmm15
  4084. .byte 0x2e,0xe8,0x08,0x00 #movaps 0x80(rsp),xmm14
  4085. .byte 0x29,0xd8,0x07,0x00 #movaps 0x70(rsp),xmm13
  4086. .byte 0x24,0xc8,0x06,0x00 #movaps 0x60(rsp),xmm12
  4087. .byte 0x1f,0xb8,0x05,0x00 #movaps 0x50(rsp),xmm11
  4088. .byte 0x1a,0xa8,0x04,0x00 #movaps 0x40(rsp),xmm10
  4089. .byte 0x15,0x98,0x03,0x00 #movaps 0x30(rsp),xmm9
  4090. .byte 0x10,0x88,0x02,0x00 #movaps 0x20(rsp),xmm8
  4091. .byte 0x0c,0x78,0x01,0x00 #movaps 0x10(rsp),xmm7
  4092. .byte 0x08,0x68,0x00,0x00 #movaps 0x00(rsp),xmm6
  4093. .byte 0x04,0x01,0x15,0x00 #sub rsp,0xa8
  4094. .align 8
  4095. ___
  4096. $code.=<<___ if ($avx>1);
  4097. .LSEH_info_ecp_nistz256_avx2_gather_wX:
  4098. .byte 0x01,0x36,0x17,0x0b
  4099. .byte 0x36,0xf8,0x09,0x00 # vmovaps 0x90(rsp),xmm15
  4100. .byte 0x31,0xe8,0x08,0x00 # vmovaps 0x80(rsp),xmm14
  4101. .byte 0x2c,0xd8,0x07,0x00 # vmovaps 0x70(rsp),xmm13
  4102. .byte 0x27,0xc8,0x06,0x00 # vmovaps 0x60(rsp),xmm12
  4103. .byte 0x22,0xb8,0x05,0x00 # vmovaps 0x50(rsp),xmm11
  4104. .byte 0x1d,0xa8,0x04,0x00 # vmovaps 0x40(rsp),xmm10
  4105. .byte 0x18,0x98,0x03,0x00 # vmovaps 0x30(rsp),xmm9
  4106. .byte 0x13,0x88,0x02,0x00 # vmovaps 0x20(rsp),xmm8
  4107. .byte 0x0e,0x78,0x01,0x00 # vmovaps 0x10(rsp),xmm7
  4108. .byte 0x09,0x68,0x00,0x00 # vmovaps 0x00(rsp),xmm6
  4109. .byte 0x04,0x01,0x15,0x00 # sub rsp,0xa8
  4110. .byte 0x00,0xb3,0x00,0x00 # set_frame r11
  4111. .align 8
  4112. ___
  4113. $code.=<<___;
  4114. .LSEH_info_ecp_nistz256_point_double:
  4115. .byte 9,0,0,0
  4116. .rva full_handler
  4117. .rva .Lpoint_doubleq_body,.Lpoint_doubleq_epilogue # HandlerData[]
  4118. .long 32*5+56,0
  4119. .LSEH_info_ecp_nistz256_point_add:
  4120. .byte 9,0,0,0
  4121. .rva full_handler
  4122. .rva .Lpoint_addq_body,.Lpoint_addq_epilogue # HandlerData[]
  4123. .long 32*18+56,0
  4124. .LSEH_info_ecp_nistz256_point_add_affine:
  4125. .byte 9,0,0,0
  4126. .rva full_handler
  4127. .rva .Ladd_affineq_body,.Ladd_affineq_epilogue # HandlerData[]
  4128. .long 32*15+56,0
  4129. ___
  4130. $code.=<<___ if ($addx);
  4131. .align 8
  4132. .LSEH_info_ecp_nistz256_point_doublex:
  4133. .byte 9,0,0,0
  4134. .rva full_handler
  4135. .rva .Lpoint_doublex_body,.Lpoint_doublex_epilogue # HandlerData[]
  4136. .long 32*5+56,0
  4137. .LSEH_info_ecp_nistz256_point_addx:
  4138. .byte 9,0,0,0
  4139. .rva full_handler
  4140. .rva .Lpoint_addx_body,.Lpoint_addx_epilogue # HandlerData[]
  4141. .long 32*18+56,0
  4142. .LSEH_info_ecp_nistz256_point_add_affinex:
  4143. .byte 9,0,0,0
  4144. .rva full_handler
  4145. .rva .Ladd_affinex_body,.Ladd_affinex_epilogue # HandlerData[]
  4146. .long 32*15+56,0
  4147. ___
  4148. }
  4149. ########################################################################
  4150. # Convert ecp_nistz256_table.c to layout expected by ecp_nistz_gather_w7
  4151. #
  4152. open TABLE,"<ecp_nistz256_table.c" or
  4153. open TABLE,"<${dir}../ecp_nistz256_table.c" or
  4154. die "failed to open ecp_nistz256_table.c:",$!;
  4155. use integer;
  4156. foreach(<TABLE>) {
  4157. s/TOBN\(\s*(0x[0-9a-f]+),\s*(0x[0-9a-f]+)\s*\)/push @arr,hex($2),hex($1)/geo;
  4158. }
  4159. close TABLE;
  4160. die "insane number of elements" if ($#arr != 64*16*37-1);
  4161. print <<___;
  4162. .text
  4163. .globl ecp_nistz256_precomputed
  4164. .type ecp_nistz256_precomputed,\@object
  4165. .align 4096
  4166. ecp_nistz256_precomputed:
  4167. ___
  4168. while (@line=splice(@arr,0,16)) {
  4169. print ".long\t",join(',',map { sprintf "0x%08x",$_} @line),"\n";
  4170. }
  4171. print <<___;
  4172. .size ecp_nistz256_precomputed,.-ecp_nistz256_precomputed
  4173. ___
  4174. $code =~ s/\`([^\`]*)\`/eval $1/gem;
  4175. print $code;
  4176. close STDOUT or die "error closing STDOUT: $!";