ecp_nistz256-x86_64.pl 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707
  1. #! /usr/bin/env perl
  2. # Copyright 2014-2018 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) ([3-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 # guranteed 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. ___
  1378. $code.=<<___ if ($addx);
  1379. mov \$0x80100, %ecx
  1380. and OPENSSL_ia32cap_P+8(%rip), %ecx
  1381. ___
  1382. $code.=<<___;
  1383. lea .LRR(%rip), $b_org
  1384. jmp .Lmul_mont
  1385. .size ecp_nistz256_to_mont,.-ecp_nistz256_to_mont
  1386. ################################################################################
  1387. # void ecp_nistz256_mul_mont(
  1388. # uint64_t res[4],
  1389. # uint64_t a[4],
  1390. # uint64_t b[4]);
  1391. .globl ecp_nistz256_mul_mont
  1392. .type ecp_nistz256_mul_mont,\@function,3
  1393. .align 32
  1394. ecp_nistz256_mul_mont:
  1395. .cfi_startproc
  1396. ___
  1397. $code.=<<___ if ($addx);
  1398. mov \$0x80100, %ecx
  1399. and OPENSSL_ia32cap_P+8(%rip), %ecx
  1400. ___
  1401. $code.=<<___;
  1402. .Lmul_mont:
  1403. push %rbp
  1404. .cfi_push %rbp
  1405. push %rbx
  1406. .cfi_push %rbx
  1407. push %r12
  1408. .cfi_push %r12
  1409. push %r13
  1410. .cfi_push %r13
  1411. push %r14
  1412. .cfi_push %r14
  1413. push %r15
  1414. .cfi_push %r15
  1415. .Lmul_body:
  1416. ___
  1417. $code.=<<___ if ($addx);
  1418. cmp \$0x80100, %ecx
  1419. je .Lmul_montx
  1420. ___
  1421. $code.=<<___;
  1422. mov $b_org, $b_ptr
  1423. mov 8*0($b_org), %rax
  1424. mov 8*0($a_ptr), $acc1
  1425. mov 8*1($a_ptr), $acc2
  1426. mov 8*2($a_ptr), $acc3
  1427. mov 8*3($a_ptr), $acc4
  1428. call __ecp_nistz256_mul_montq
  1429. ___
  1430. $code.=<<___ if ($addx);
  1431. jmp .Lmul_mont_done
  1432. .align 32
  1433. .Lmul_montx:
  1434. mov $b_org, $b_ptr
  1435. mov 8*0($b_org), %rdx
  1436. mov 8*0($a_ptr), $acc1
  1437. mov 8*1($a_ptr), $acc2
  1438. mov 8*2($a_ptr), $acc3
  1439. mov 8*3($a_ptr), $acc4
  1440. lea -128($a_ptr), $a_ptr # control u-op density
  1441. call __ecp_nistz256_mul_montx
  1442. ___
  1443. $code.=<<___;
  1444. .Lmul_mont_done:
  1445. mov 0(%rsp),%r15
  1446. .cfi_restore %r15
  1447. mov 8(%rsp),%r14
  1448. .cfi_restore %r14
  1449. mov 16(%rsp),%r13
  1450. .cfi_restore %r13
  1451. mov 24(%rsp),%r12
  1452. .cfi_restore %r12
  1453. mov 32(%rsp),%rbx
  1454. .cfi_restore %rbx
  1455. mov 40(%rsp),%rbp
  1456. .cfi_restore %rbp
  1457. lea 48(%rsp),%rsp
  1458. .cfi_adjust_cfa_offset -48
  1459. .Lmul_epilogue:
  1460. ret
  1461. .cfi_endproc
  1462. .size ecp_nistz256_mul_mont,.-ecp_nistz256_mul_mont
  1463. .type __ecp_nistz256_mul_montq,\@abi-omnipotent
  1464. .align 32
  1465. __ecp_nistz256_mul_montq:
  1466. ########################################################################
  1467. # Multiply a by b[0]
  1468. mov %rax, $t1
  1469. mulq $acc1
  1470. mov .Lpoly+8*1(%rip),$poly1
  1471. mov %rax, $acc0
  1472. mov $t1, %rax
  1473. mov %rdx, $acc1
  1474. mulq $acc2
  1475. mov .Lpoly+8*3(%rip),$poly3
  1476. add %rax, $acc1
  1477. mov $t1, %rax
  1478. adc \$0, %rdx
  1479. mov %rdx, $acc2
  1480. mulq $acc3
  1481. add %rax, $acc2
  1482. mov $t1, %rax
  1483. adc \$0, %rdx
  1484. mov %rdx, $acc3
  1485. mulq $acc4
  1486. add %rax, $acc3
  1487. mov $acc0, %rax
  1488. adc \$0, %rdx
  1489. xor $acc5, $acc5
  1490. mov %rdx, $acc4
  1491. ########################################################################
  1492. # First reduction step
  1493. # Basically now we want to multiply acc[0] by p256,
  1494. # and add the result to the acc.
  1495. # Due to the special form of p256 we do some optimizations
  1496. #
  1497. # acc[0] x p256[0..1] = acc[0] x 2^96 - acc[0]
  1498. # then we add acc[0] and get acc[0] x 2^96
  1499. mov $acc0, $t1
  1500. shl \$32, $acc0
  1501. mulq $poly3
  1502. shr \$32, $t1
  1503. add $acc0, $acc1 # +=acc[0]<<96
  1504. adc $t1, $acc2
  1505. adc %rax, $acc3
  1506. mov 8*1($b_ptr), %rax
  1507. adc %rdx, $acc4
  1508. adc \$0, $acc5
  1509. xor $acc0, $acc0
  1510. ########################################################################
  1511. # Multiply by b[1]
  1512. mov %rax, $t1
  1513. mulq 8*0($a_ptr)
  1514. add %rax, $acc1
  1515. mov $t1, %rax
  1516. adc \$0, %rdx
  1517. mov %rdx, $t0
  1518. mulq 8*1($a_ptr)
  1519. add $t0, $acc2
  1520. adc \$0, %rdx
  1521. add %rax, $acc2
  1522. mov $t1, %rax
  1523. adc \$0, %rdx
  1524. mov %rdx, $t0
  1525. mulq 8*2($a_ptr)
  1526. add $t0, $acc3
  1527. adc \$0, %rdx
  1528. add %rax, $acc3
  1529. mov $t1, %rax
  1530. adc \$0, %rdx
  1531. mov %rdx, $t0
  1532. mulq 8*3($a_ptr)
  1533. add $t0, $acc4
  1534. adc \$0, %rdx
  1535. add %rax, $acc4
  1536. mov $acc1, %rax
  1537. adc %rdx, $acc5
  1538. adc \$0, $acc0
  1539. ########################################################################
  1540. # Second reduction step
  1541. mov $acc1, $t1
  1542. shl \$32, $acc1
  1543. mulq $poly3
  1544. shr \$32, $t1
  1545. add $acc1, $acc2
  1546. adc $t1, $acc3
  1547. adc %rax, $acc4
  1548. mov 8*2($b_ptr), %rax
  1549. adc %rdx, $acc5
  1550. adc \$0, $acc0
  1551. xor $acc1, $acc1
  1552. ########################################################################
  1553. # Multiply by b[2]
  1554. mov %rax, $t1
  1555. mulq 8*0($a_ptr)
  1556. add %rax, $acc2
  1557. mov $t1, %rax
  1558. adc \$0, %rdx
  1559. mov %rdx, $t0
  1560. mulq 8*1($a_ptr)
  1561. add $t0, $acc3
  1562. adc \$0, %rdx
  1563. add %rax, $acc3
  1564. mov $t1, %rax
  1565. adc \$0, %rdx
  1566. mov %rdx, $t0
  1567. mulq 8*2($a_ptr)
  1568. add $t0, $acc4
  1569. adc \$0, %rdx
  1570. add %rax, $acc4
  1571. mov $t1, %rax
  1572. adc \$0, %rdx
  1573. mov %rdx, $t0
  1574. mulq 8*3($a_ptr)
  1575. add $t0, $acc5
  1576. adc \$0, %rdx
  1577. add %rax, $acc5
  1578. mov $acc2, %rax
  1579. adc %rdx, $acc0
  1580. adc \$0, $acc1
  1581. ########################################################################
  1582. # Third reduction step
  1583. mov $acc2, $t1
  1584. shl \$32, $acc2
  1585. mulq $poly3
  1586. shr \$32, $t1
  1587. add $acc2, $acc3
  1588. adc $t1, $acc4
  1589. adc %rax, $acc5
  1590. mov 8*3($b_ptr), %rax
  1591. adc %rdx, $acc0
  1592. adc \$0, $acc1
  1593. xor $acc2, $acc2
  1594. ########################################################################
  1595. # Multiply by b[3]
  1596. mov %rax, $t1
  1597. mulq 8*0($a_ptr)
  1598. add %rax, $acc3
  1599. mov $t1, %rax
  1600. adc \$0, %rdx
  1601. mov %rdx, $t0
  1602. mulq 8*1($a_ptr)
  1603. add $t0, $acc4
  1604. adc \$0, %rdx
  1605. add %rax, $acc4
  1606. mov $t1, %rax
  1607. adc \$0, %rdx
  1608. mov %rdx, $t0
  1609. mulq 8*2($a_ptr)
  1610. add $t0, $acc5
  1611. adc \$0, %rdx
  1612. add %rax, $acc5
  1613. mov $t1, %rax
  1614. adc \$0, %rdx
  1615. mov %rdx, $t0
  1616. mulq 8*3($a_ptr)
  1617. add $t0, $acc0
  1618. adc \$0, %rdx
  1619. add %rax, $acc0
  1620. mov $acc3, %rax
  1621. adc %rdx, $acc1
  1622. adc \$0, $acc2
  1623. ########################################################################
  1624. # Final reduction step
  1625. mov $acc3, $t1
  1626. shl \$32, $acc3
  1627. mulq $poly3
  1628. shr \$32, $t1
  1629. add $acc3, $acc4
  1630. adc $t1, $acc5
  1631. mov $acc4, $t0
  1632. adc %rax, $acc0
  1633. adc %rdx, $acc1
  1634. mov $acc5, $t1
  1635. adc \$0, $acc2
  1636. ########################################################################
  1637. # Branch-less conditional subtraction of P
  1638. sub \$-1, $acc4 # .Lpoly[0]
  1639. mov $acc0, $t2
  1640. sbb $poly1, $acc5 # .Lpoly[1]
  1641. sbb \$0, $acc0 # .Lpoly[2]
  1642. mov $acc1, $t3
  1643. sbb $poly3, $acc1 # .Lpoly[3]
  1644. sbb \$0, $acc2
  1645. cmovc $t0, $acc4
  1646. cmovc $t1, $acc5
  1647. mov $acc4, 8*0($r_ptr)
  1648. cmovc $t2, $acc0
  1649. mov $acc5, 8*1($r_ptr)
  1650. cmovc $t3, $acc1
  1651. mov $acc0, 8*2($r_ptr)
  1652. mov $acc1, 8*3($r_ptr)
  1653. ret
  1654. .size __ecp_nistz256_mul_montq,.-__ecp_nistz256_mul_montq
  1655. ################################################################################
  1656. # void ecp_nistz256_sqr_mont(
  1657. # uint64_t res[4],
  1658. # uint64_t a[4]);
  1659. # we optimize the square according to S.Gueron and V.Krasnov,
  1660. # "Speeding up Big-Number Squaring"
  1661. .globl ecp_nistz256_sqr_mont
  1662. .type ecp_nistz256_sqr_mont,\@function,2
  1663. .align 32
  1664. ecp_nistz256_sqr_mont:
  1665. .cfi_startproc
  1666. ___
  1667. $code.=<<___ if ($addx);
  1668. mov \$0x80100, %ecx
  1669. and OPENSSL_ia32cap_P+8(%rip), %ecx
  1670. ___
  1671. $code.=<<___;
  1672. push %rbp
  1673. .cfi_push %rbp
  1674. push %rbx
  1675. .cfi_push %rbx
  1676. push %r12
  1677. .cfi_push %r12
  1678. push %r13
  1679. .cfi_push %r13
  1680. push %r14
  1681. .cfi_push %r14
  1682. push %r15
  1683. .cfi_push %r15
  1684. .Lsqr_body:
  1685. ___
  1686. $code.=<<___ if ($addx);
  1687. cmp \$0x80100, %ecx
  1688. je .Lsqr_montx
  1689. ___
  1690. $code.=<<___;
  1691. mov 8*0($a_ptr), %rax
  1692. mov 8*1($a_ptr), $acc6
  1693. mov 8*2($a_ptr), $acc7
  1694. mov 8*3($a_ptr), $acc0
  1695. call __ecp_nistz256_sqr_montq
  1696. ___
  1697. $code.=<<___ if ($addx);
  1698. jmp .Lsqr_mont_done
  1699. .align 32
  1700. .Lsqr_montx:
  1701. mov 8*0($a_ptr), %rdx
  1702. mov 8*1($a_ptr), $acc6
  1703. mov 8*2($a_ptr), $acc7
  1704. mov 8*3($a_ptr), $acc0
  1705. lea -128($a_ptr), $a_ptr # control u-op density
  1706. call __ecp_nistz256_sqr_montx
  1707. ___
  1708. $code.=<<___;
  1709. .Lsqr_mont_done:
  1710. mov 0(%rsp),%r15
  1711. .cfi_restore %r15
  1712. mov 8(%rsp),%r14
  1713. .cfi_restore %r14
  1714. mov 16(%rsp),%r13
  1715. .cfi_restore %r13
  1716. mov 24(%rsp),%r12
  1717. .cfi_restore %r12
  1718. mov 32(%rsp),%rbx
  1719. .cfi_restore %rbx
  1720. mov 40(%rsp),%rbp
  1721. .cfi_restore %rbp
  1722. lea 48(%rsp),%rsp
  1723. .cfi_adjust_cfa_offset -48
  1724. .Lsqr_epilogue:
  1725. ret
  1726. .cfi_endproc
  1727. .size ecp_nistz256_sqr_mont,.-ecp_nistz256_sqr_mont
  1728. .type __ecp_nistz256_sqr_montq,\@abi-omnipotent
  1729. .align 32
  1730. __ecp_nistz256_sqr_montq:
  1731. mov %rax, $acc5
  1732. mulq $acc6 # a[1]*a[0]
  1733. mov %rax, $acc1
  1734. mov $acc7, %rax
  1735. mov %rdx, $acc2
  1736. mulq $acc5 # a[0]*a[2]
  1737. add %rax, $acc2
  1738. mov $acc0, %rax
  1739. adc \$0, %rdx
  1740. mov %rdx, $acc3
  1741. mulq $acc5 # a[0]*a[3]
  1742. add %rax, $acc3
  1743. mov $acc7, %rax
  1744. adc \$0, %rdx
  1745. mov %rdx, $acc4
  1746. #################################
  1747. mulq $acc6 # a[1]*a[2]
  1748. add %rax, $acc3
  1749. mov $acc0, %rax
  1750. adc \$0, %rdx
  1751. mov %rdx, $t1
  1752. mulq $acc6 # a[1]*a[3]
  1753. add %rax, $acc4
  1754. mov $acc0, %rax
  1755. adc \$0, %rdx
  1756. add $t1, $acc4
  1757. mov %rdx, $acc5
  1758. adc \$0, $acc5
  1759. #################################
  1760. mulq $acc7 # a[2]*a[3]
  1761. xor $acc7, $acc7
  1762. add %rax, $acc5
  1763. mov 8*0($a_ptr), %rax
  1764. mov %rdx, $acc6
  1765. adc \$0, $acc6
  1766. add $acc1, $acc1 # acc1:6<<1
  1767. adc $acc2, $acc2
  1768. adc $acc3, $acc3
  1769. adc $acc4, $acc4
  1770. adc $acc5, $acc5
  1771. adc $acc6, $acc6
  1772. adc \$0, $acc7
  1773. mulq %rax
  1774. mov %rax, $acc0
  1775. mov 8*1($a_ptr), %rax
  1776. mov %rdx, $t0
  1777. mulq %rax
  1778. add $t0, $acc1
  1779. adc %rax, $acc2
  1780. mov 8*2($a_ptr), %rax
  1781. adc \$0, %rdx
  1782. mov %rdx, $t0
  1783. mulq %rax
  1784. add $t0, $acc3
  1785. adc %rax, $acc4
  1786. mov 8*3($a_ptr), %rax
  1787. adc \$0, %rdx
  1788. mov %rdx, $t0
  1789. mulq %rax
  1790. add $t0, $acc5
  1791. adc %rax, $acc6
  1792. mov $acc0, %rax
  1793. adc %rdx, $acc7
  1794. mov .Lpoly+8*1(%rip), $a_ptr
  1795. mov .Lpoly+8*3(%rip), $t1
  1796. ##########################################
  1797. # Now the reduction
  1798. # First iteration
  1799. mov $acc0, $t0
  1800. shl \$32, $acc0
  1801. mulq $t1
  1802. shr \$32, $t0
  1803. add $acc0, $acc1 # +=acc[0]<<96
  1804. adc $t0, $acc2
  1805. adc %rax, $acc3
  1806. mov $acc1, %rax
  1807. adc \$0, %rdx
  1808. ##########################################
  1809. # Second iteration
  1810. mov $acc1, $t0
  1811. shl \$32, $acc1
  1812. mov %rdx, $acc0
  1813. mulq $t1
  1814. shr \$32, $t0
  1815. add $acc1, $acc2
  1816. adc $t0, $acc3
  1817. adc %rax, $acc0
  1818. mov $acc2, %rax
  1819. adc \$0, %rdx
  1820. ##########################################
  1821. # Third iteration
  1822. mov $acc2, $t0
  1823. shl \$32, $acc2
  1824. mov %rdx, $acc1
  1825. mulq $t1
  1826. shr \$32, $t0
  1827. add $acc2, $acc3
  1828. adc $t0, $acc0
  1829. adc %rax, $acc1
  1830. mov $acc3, %rax
  1831. adc \$0, %rdx
  1832. ###########################################
  1833. # Last iteration
  1834. mov $acc3, $t0
  1835. shl \$32, $acc3
  1836. mov %rdx, $acc2
  1837. mulq $t1
  1838. shr \$32, $t0
  1839. add $acc3, $acc0
  1840. adc $t0, $acc1
  1841. adc %rax, $acc2
  1842. adc \$0, %rdx
  1843. xor $acc3, $acc3
  1844. ############################################
  1845. # Add the rest of the acc
  1846. add $acc0, $acc4
  1847. adc $acc1, $acc5
  1848. mov $acc4, $acc0
  1849. adc $acc2, $acc6
  1850. adc %rdx, $acc7
  1851. mov $acc5, $acc1
  1852. adc \$0, $acc3
  1853. sub \$-1, $acc4 # .Lpoly[0]
  1854. mov $acc6, $acc2
  1855. sbb $a_ptr, $acc5 # .Lpoly[1]
  1856. sbb \$0, $acc6 # .Lpoly[2]
  1857. mov $acc7, $t0
  1858. sbb $t1, $acc7 # .Lpoly[3]
  1859. sbb \$0, $acc3
  1860. cmovc $acc0, $acc4
  1861. cmovc $acc1, $acc5
  1862. mov $acc4, 8*0($r_ptr)
  1863. cmovc $acc2, $acc6
  1864. mov $acc5, 8*1($r_ptr)
  1865. cmovc $t0, $acc7
  1866. mov $acc6, 8*2($r_ptr)
  1867. mov $acc7, 8*3($r_ptr)
  1868. ret
  1869. .size __ecp_nistz256_sqr_montq,.-__ecp_nistz256_sqr_montq
  1870. ___
  1871. if ($addx) {
  1872. $code.=<<___;
  1873. .type __ecp_nistz256_mul_montx,\@abi-omnipotent
  1874. .align 32
  1875. __ecp_nistz256_mul_montx:
  1876. ########################################################################
  1877. # Multiply by b[0]
  1878. mulx $acc1, $acc0, $acc1
  1879. mulx $acc2, $t0, $acc2
  1880. mov \$32, $poly1
  1881. xor $acc5, $acc5 # cf=0
  1882. mulx $acc3, $t1, $acc3
  1883. mov .Lpoly+8*3(%rip), $poly3
  1884. adc $t0, $acc1
  1885. mulx $acc4, $t0, $acc4
  1886. mov $acc0, %rdx
  1887. adc $t1, $acc2
  1888. shlx $poly1,$acc0,$t1
  1889. adc $t0, $acc3
  1890. shrx $poly1,$acc0,$t0
  1891. adc \$0, $acc4
  1892. ########################################################################
  1893. # First reduction step
  1894. add $t1, $acc1
  1895. adc $t0, $acc2
  1896. mulx $poly3, $t0, $t1
  1897. mov 8*1($b_ptr), %rdx
  1898. adc $t0, $acc3
  1899. adc $t1, $acc4
  1900. adc \$0, $acc5
  1901. xor $acc0, $acc0 # $acc0=0,cf=0,of=0
  1902. ########################################################################
  1903. # Multiply by b[1]
  1904. mulx 8*0+128($a_ptr), $t0, $t1
  1905. adcx $t0, $acc1
  1906. adox $t1, $acc2
  1907. mulx 8*1+128($a_ptr), $t0, $t1
  1908. adcx $t0, $acc2
  1909. adox $t1, $acc3
  1910. mulx 8*2+128($a_ptr), $t0, $t1
  1911. adcx $t0, $acc3
  1912. adox $t1, $acc4
  1913. mulx 8*3+128($a_ptr), $t0, $t1
  1914. mov $acc1, %rdx
  1915. adcx $t0, $acc4
  1916. shlx $poly1, $acc1, $t0
  1917. adox $t1, $acc5
  1918. shrx $poly1, $acc1, $t1
  1919. adcx $acc0, $acc5
  1920. adox $acc0, $acc0
  1921. adc \$0, $acc0
  1922. ########################################################################
  1923. # Second reduction step
  1924. add $t0, $acc2
  1925. adc $t1, $acc3
  1926. mulx $poly3, $t0, $t1
  1927. mov 8*2($b_ptr), %rdx
  1928. adc $t0, $acc4
  1929. adc $t1, $acc5
  1930. adc \$0, $acc0
  1931. xor $acc1 ,$acc1 # $acc1=0,cf=0,of=0
  1932. ########################################################################
  1933. # Multiply by b[2]
  1934. mulx 8*0+128($a_ptr), $t0, $t1
  1935. adcx $t0, $acc2
  1936. adox $t1, $acc3
  1937. mulx 8*1+128($a_ptr), $t0, $t1
  1938. adcx $t0, $acc3
  1939. adox $t1, $acc4
  1940. mulx 8*2+128($a_ptr), $t0, $t1
  1941. adcx $t0, $acc4
  1942. adox $t1, $acc5
  1943. mulx 8*3+128($a_ptr), $t0, $t1
  1944. mov $acc2, %rdx
  1945. adcx $t0, $acc5
  1946. shlx $poly1, $acc2, $t0
  1947. adox $t1, $acc0
  1948. shrx $poly1, $acc2, $t1
  1949. adcx $acc1, $acc0
  1950. adox $acc1, $acc1
  1951. adc \$0, $acc1
  1952. ########################################################################
  1953. # Third reduction step
  1954. add $t0, $acc3
  1955. adc $t1, $acc4
  1956. mulx $poly3, $t0, $t1
  1957. mov 8*3($b_ptr), %rdx
  1958. adc $t0, $acc5
  1959. adc $t1, $acc0
  1960. adc \$0, $acc1
  1961. xor $acc2, $acc2 # $acc2=0,cf=0,of=0
  1962. ########################################################################
  1963. # Multiply by b[3]
  1964. mulx 8*0+128($a_ptr), $t0, $t1
  1965. adcx $t0, $acc3
  1966. adox $t1, $acc4
  1967. mulx 8*1+128($a_ptr), $t0, $t1
  1968. adcx $t0, $acc4
  1969. adox $t1, $acc5
  1970. mulx 8*2+128($a_ptr), $t0, $t1
  1971. adcx $t0, $acc5
  1972. adox $t1, $acc0
  1973. mulx 8*3+128($a_ptr), $t0, $t1
  1974. mov $acc3, %rdx
  1975. adcx $t0, $acc0
  1976. shlx $poly1, $acc3, $t0
  1977. adox $t1, $acc1
  1978. shrx $poly1, $acc3, $t1
  1979. adcx $acc2, $acc1
  1980. adox $acc2, $acc2
  1981. adc \$0, $acc2
  1982. ########################################################################
  1983. # Fourth reduction step
  1984. add $t0, $acc4
  1985. adc $t1, $acc5
  1986. mulx $poly3, $t0, $t1
  1987. mov $acc4, $t2
  1988. mov .Lpoly+8*1(%rip), $poly1
  1989. adc $t0, $acc0
  1990. mov $acc5, $t3
  1991. adc $t1, $acc1
  1992. adc \$0, $acc2
  1993. ########################################################################
  1994. # Branch-less conditional subtraction of P
  1995. xor %eax, %eax
  1996. mov $acc0, $t0
  1997. sbb \$-1, $acc4 # .Lpoly[0]
  1998. sbb $poly1, $acc5 # .Lpoly[1]
  1999. sbb \$0, $acc0 # .Lpoly[2]
  2000. mov $acc1, $t1
  2001. sbb $poly3, $acc1 # .Lpoly[3]
  2002. sbb \$0, $acc2
  2003. cmovc $t2, $acc4
  2004. cmovc $t3, $acc5
  2005. mov $acc4, 8*0($r_ptr)
  2006. cmovc $t0, $acc0
  2007. mov $acc5, 8*1($r_ptr)
  2008. cmovc $t1, $acc1
  2009. mov $acc0, 8*2($r_ptr)
  2010. mov $acc1, 8*3($r_ptr)
  2011. ret
  2012. .size __ecp_nistz256_mul_montx,.-__ecp_nistz256_mul_montx
  2013. .type __ecp_nistz256_sqr_montx,\@abi-omnipotent
  2014. .align 32
  2015. __ecp_nistz256_sqr_montx:
  2016. mulx $acc6, $acc1, $acc2 # a[0]*a[1]
  2017. mulx $acc7, $t0, $acc3 # a[0]*a[2]
  2018. xor %eax, %eax
  2019. adc $t0, $acc2
  2020. mulx $acc0, $t1, $acc4 # a[0]*a[3]
  2021. mov $acc6, %rdx
  2022. adc $t1, $acc3
  2023. adc \$0, $acc4
  2024. xor $acc5, $acc5 # $acc5=0,cf=0,of=0
  2025. #################################
  2026. mulx $acc7, $t0, $t1 # a[1]*a[2]
  2027. adcx $t0, $acc3
  2028. adox $t1, $acc4
  2029. mulx $acc0, $t0, $t1 # a[1]*a[3]
  2030. mov $acc7, %rdx
  2031. adcx $t0, $acc4
  2032. adox $t1, $acc5
  2033. adc \$0, $acc5
  2034. #################################
  2035. mulx $acc0, $t0, $acc6 # a[2]*a[3]
  2036. mov 8*0+128($a_ptr), %rdx
  2037. xor $acc7, $acc7 # $acc7=0,cf=0,of=0
  2038. adcx $acc1, $acc1 # acc1:6<<1
  2039. adox $t0, $acc5
  2040. adcx $acc2, $acc2
  2041. adox $acc7, $acc6 # of=0
  2042. mulx %rdx, $acc0, $t1
  2043. mov 8*1+128($a_ptr), %rdx
  2044. adcx $acc3, $acc3
  2045. adox $t1, $acc1
  2046. adcx $acc4, $acc4
  2047. mulx %rdx, $t0, $t4
  2048. mov 8*2+128($a_ptr), %rdx
  2049. adcx $acc5, $acc5
  2050. adox $t0, $acc2
  2051. adcx $acc6, $acc6
  2052. .byte 0x67
  2053. mulx %rdx, $t0, $t1
  2054. mov 8*3+128($a_ptr), %rdx
  2055. adox $t4, $acc3
  2056. adcx $acc7, $acc7
  2057. adox $t0, $acc4
  2058. mov \$32, $a_ptr
  2059. adox $t1, $acc5
  2060. .byte 0x67,0x67
  2061. mulx %rdx, $t0, $t4
  2062. mov .Lpoly+8*3(%rip), %rdx
  2063. adox $t0, $acc6
  2064. shlx $a_ptr, $acc0, $t0
  2065. adox $t4, $acc7
  2066. shrx $a_ptr, $acc0, $t4
  2067. mov %rdx,$t1
  2068. # reduction step 1
  2069. add $t0, $acc1
  2070. adc $t4, $acc2
  2071. mulx $acc0, $t0, $acc0
  2072. adc $t0, $acc3
  2073. shlx $a_ptr, $acc1, $t0
  2074. adc \$0, $acc0
  2075. shrx $a_ptr, $acc1, $t4
  2076. # reduction step 2
  2077. add $t0, $acc2
  2078. adc $t4, $acc3
  2079. mulx $acc1, $t0, $acc1
  2080. adc $t0, $acc0
  2081. shlx $a_ptr, $acc2, $t0
  2082. adc \$0, $acc1
  2083. shrx $a_ptr, $acc2, $t4
  2084. # reduction step 3
  2085. add $t0, $acc3
  2086. adc $t4, $acc0
  2087. mulx $acc2, $t0, $acc2
  2088. adc $t0, $acc1
  2089. shlx $a_ptr, $acc3, $t0
  2090. adc \$0, $acc2
  2091. shrx $a_ptr, $acc3, $t4
  2092. # reduction step 4
  2093. add $t0, $acc0
  2094. adc $t4, $acc1
  2095. mulx $acc3, $t0, $acc3
  2096. adc $t0, $acc2
  2097. adc \$0, $acc3
  2098. xor $t3, $t3
  2099. add $acc0, $acc4 # accumulate upper half
  2100. mov .Lpoly+8*1(%rip), $a_ptr
  2101. adc $acc1, $acc5
  2102. mov $acc4, $acc0
  2103. adc $acc2, $acc6
  2104. adc $acc3, $acc7
  2105. mov $acc5, $acc1
  2106. adc \$0, $t3
  2107. sub \$-1, $acc4 # .Lpoly[0]
  2108. mov $acc6, $acc2
  2109. sbb $a_ptr, $acc5 # .Lpoly[1]
  2110. sbb \$0, $acc6 # .Lpoly[2]
  2111. mov $acc7, $acc3
  2112. sbb $t1, $acc7 # .Lpoly[3]
  2113. sbb \$0, $t3
  2114. cmovc $acc0, $acc4
  2115. cmovc $acc1, $acc5
  2116. mov $acc4, 8*0($r_ptr)
  2117. cmovc $acc2, $acc6
  2118. mov $acc5, 8*1($r_ptr)
  2119. cmovc $acc3, $acc7
  2120. mov $acc6, 8*2($r_ptr)
  2121. mov $acc7, 8*3($r_ptr)
  2122. ret
  2123. .size __ecp_nistz256_sqr_montx,.-__ecp_nistz256_sqr_montx
  2124. ___
  2125. }
  2126. }
  2127. {
  2128. my ($r_ptr,$in_ptr)=("%rdi","%rsi");
  2129. my ($acc0,$acc1,$acc2,$acc3)=map("%r$_",(8..11));
  2130. my ($t0,$t1,$t2)=("%rcx","%r12","%r13");
  2131. $code.=<<___;
  2132. ################################################################################
  2133. # void ecp_nistz256_from_mont(
  2134. # uint64_t res[4],
  2135. # uint64_t in[4]);
  2136. # This one performs Montgomery multiplication by 1, so we only need the reduction
  2137. .globl ecp_nistz256_from_mont
  2138. .type ecp_nistz256_from_mont,\@function,2
  2139. .align 32
  2140. ecp_nistz256_from_mont:
  2141. .cfi_startproc
  2142. push %r12
  2143. .cfi_push %r12
  2144. push %r13
  2145. .cfi_push %r13
  2146. .Lfrom_body:
  2147. mov 8*0($in_ptr), %rax
  2148. mov .Lpoly+8*3(%rip), $t2
  2149. mov 8*1($in_ptr), $acc1
  2150. mov 8*2($in_ptr), $acc2
  2151. mov 8*3($in_ptr), $acc3
  2152. mov %rax, $acc0
  2153. mov .Lpoly+8*1(%rip), $t1
  2154. #########################################
  2155. # First iteration
  2156. mov %rax, $t0
  2157. shl \$32, $acc0
  2158. mulq $t2
  2159. shr \$32, $t0
  2160. add $acc0, $acc1
  2161. adc $t0, $acc2
  2162. adc %rax, $acc3
  2163. mov $acc1, %rax
  2164. adc \$0, %rdx
  2165. #########################################
  2166. # Second iteration
  2167. mov $acc1, $t0
  2168. shl \$32, $acc1
  2169. mov %rdx, $acc0
  2170. mulq $t2
  2171. shr \$32, $t0
  2172. add $acc1, $acc2
  2173. adc $t0, $acc3
  2174. adc %rax, $acc0
  2175. mov $acc2, %rax
  2176. adc \$0, %rdx
  2177. ##########################################
  2178. # Third iteration
  2179. mov $acc2, $t0
  2180. shl \$32, $acc2
  2181. mov %rdx, $acc1
  2182. mulq $t2
  2183. shr \$32, $t0
  2184. add $acc2, $acc3
  2185. adc $t0, $acc0
  2186. adc %rax, $acc1
  2187. mov $acc3, %rax
  2188. adc \$0, %rdx
  2189. ###########################################
  2190. # Last iteration
  2191. mov $acc3, $t0
  2192. shl \$32, $acc3
  2193. mov %rdx, $acc2
  2194. mulq $t2
  2195. shr \$32, $t0
  2196. add $acc3, $acc0
  2197. adc $t0, $acc1
  2198. mov $acc0, $t0
  2199. adc %rax, $acc2
  2200. mov $acc1, $in_ptr
  2201. adc \$0, %rdx
  2202. ###########################################
  2203. # Branch-less conditional subtraction
  2204. sub \$-1, $acc0
  2205. mov $acc2, %rax
  2206. sbb $t1, $acc1
  2207. sbb \$0, $acc2
  2208. mov %rdx, $acc3
  2209. sbb $t2, %rdx
  2210. sbb $t2, $t2
  2211. cmovnz $t0, $acc0
  2212. cmovnz $in_ptr, $acc1
  2213. mov $acc0, 8*0($r_ptr)
  2214. cmovnz %rax, $acc2
  2215. mov $acc1, 8*1($r_ptr)
  2216. cmovz %rdx, $acc3
  2217. mov $acc2, 8*2($r_ptr)
  2218. mov $acc3, 8*3($r_ptr)
  2219. mov 0(%rsp),%r13
  2220. .cfi_restore %r13
  2221. mov 8(%rsp),%r12
  2222. .cfi_restore %r12
  2223. lea 16(%rsp),%rsp
  2224. .cfi_adjust_cfa_offset -16
  2225. .Lfrom_epilogue:
  2226. ret
  2227. .cfi_endproc
  2228. .size ecp_nistz256_from_mont,.-ecp_nistz256_from_mont
  2229. ___
  2230. }
  2231. {
  2232. my ($val,$in_t,$index)=$win64?("%rcx","%rdx","%r8d"):("%rdi","%rsi","%edx");
  2233. my ($ONE,$INDEX,$Ra,$Rb,$Rc,$Rd,$Re,$Rf)=map("%xmm$_",(0..7));
  2234. my ($M0,$T0a,$T0b,$T0c,$T0d,$T0e,$T0f,$TMP0)=map("%xmm$_",(8..15));
  2235. my ($M1,$T2a,$T2b,$TMP2,$M2,$T2a,$T2b,$TMP2)=map("%xmm$_",(8..15));
  2236. $code.=<<___;
  2237. ################################################################################
  2238. # void ecp_nistz256_scatter_w5(uint64_t *val, uint64_t *in_t, int index);
  2239. .globl ecp_nistz256_scatter_w5
  2240. .type ecp_nistz256_scatter_w5,\@abi-omnipotent
  2241. .align 32
  2242. ecp_nistz256_scatter_w5:
  2243. lea -3($index,$index,2), $index
  2244. movdqa 0x00($in_t), %xmm0
  2245. shl \$5, $index
  2246. movdqa 0x10($in_t), %xmm1
  2247. movdqa 0x20($in_t), %xmm2
  2248. movdqa 0x30($in_t), %xmm3
  2249. movdqa 0x40($in_t), %xmm4
  2250. movdqa 0x50($in_t), %xmm5
  2251. movdqa %xmm0, 0x00($val,$index)
  2252. movdqa %xmm1, 0x10($val,$index)
  2253. movdqa %xmm2, 0x20($val,$index)
  2254. movdqa %xmm3, 0x30($val,$index)
  2255. movdqa %xmm4, 0x40($val,$index)
  2256. movdqa %xmm5, 0x50($val,$index)
  2257. ret
  2258. .size ecp_nistz256_scatter_w5,.-ecp_nistz256_scatter_w5
  2259. ################################################################################
  2260. # void ecp_nistz256_gather_w5(uint64_t *val, uint64_t *in_t, int index);
  2261. .globl ecp_nistz256_gather_w5
  2262. .type ecp_nistz256_gather_w5,\@abi-omnipotent
  2263. .align 32
  2264. ecp_nistz256_gather_w5:
  2265. ___
  2266. $code.=<<___ if ($avx>1);
  2267. mov OPENSSL_ia32cap_P+8(%rip), %eax
  2268. test \$`1<<5`, %eax
  2269. jnz .Lavx2_gather_w5
  2270. ___
  2271. $code.=<<___ if ($win64);
  2272. lea -0x88(%rsp), %rax
  2273. .LSEH_begin_ecp_nistz256_gather_w5:
  2274. .byte 0x48,0x8d,0x60,0xe0 #lea -0x20(%rax), %rsp
  2275. .byte 0x0f,0x29,0x70,0xe0 #movaps %xmm6, -0x20(%rax)
  2276. .byte 0x0f,0x29,0x78,0xf0 #movaps %xmm7, -0x10(%rax)
  2277. .byte 0x44,0x0f,0x29,0x00 #movaps %xmm8, 0(%rax)
  2278. .byte 0x44,0x0f,0x29,0x48,0x10 #movaps %xmm9, 0x10(%rax)
  2279. .byte 0x44,0x0f,0x29,0x50,0x20 #movaps %xmm10, 0x20(%rax)
  2280. .byte 0x44,0x0f,0x29,0x58,0x30 #movaps %xmm11, 0x30(%rax)
  2281. .byte 0x44,0x0f,0x29,0x60,0x40 #movaps %xmm12, 0x40(%rax)
  2282. .byte 0x44,0x0f,0x29,0x68,0x50 #movaps %xmm13, 0x50(%rax)
  2283. .byte 0x44,0x0f,0x29,0x70,0x60 #movaps %xmm14, 0x60(%rax)
  2284. .byte 0x44,0x0f,0x29,0x78,0x70 #movaps %xmm15, 0x70(%rax)
  2285. ___
  2286. $code.=<<___;
  2287. movdqa .LOne(%rip), $ONE
  2288. movd $index, $INDEX
  2289. pxor $Ra, $Ra
  2290. pxor $Rb, $Rb
  2291. pxor $Rc, $Rc
  2292. pxor $Rd, $Rd
  2293. pxor $Re, $Re
  2294. pxor $Rf, $Rf
  2295. movdqa $ONE, $M0
  2296. pshufd \$0, $INDEX, $INDEX
  2297. mov \$16, %rax
  2298. .Lselect_loop_sse_w5:
  2299. movdqa $M0, $TMP0
  2300. paddd $ONE, $M0
  2301. pcmpeqd $INDEX, $TMP0
  2302. movdqa 16*0($in_t), $T0a
  2303. movdqa 16*1($in_t), $T0b
  2304. movdqa 16*2($in_t), $T0c
  2305. movdqa 16*3($in_t), $T0d
  2306. movdqa 16*4($in_t), $T0e
  2307. movdqa 16*5($in_t), $T0f
  2308. lea 16*6($in_t), $in_t
  2309. pand $TMP0, $T0a
  2310. pand $TMP0, $T0b
  2311. por $T0a, $Ra
  2312. pand $TMP0, $T0c
  2313. por $T0b, $Rb
  2314. pand $TMP0, $T0d
  2315. por $T0c, $Rc
  2316. pand $TMP0, $T0e
  2317. por $T0d, $Rd
  2318. pand $TMP0, $T0f
  2319. por $T0e, $Re
  2320. por $T0f, $Rf
  2321. dec %rax
  2322. jnz .Lselect_loop_sse_w5
  2323. movdqu $Ra, 16*0($val)
  2324. movdqu $Rb, 16*1($val)
  2325. movdqu $Rc, 16*2($val)
  2326. movdqu $Rd, 16*3($val)
  2327. movdqu $Re, 16*4($val)
  2328. movdqu $Rf, 16*5($val)
  2329. ___
  2330. $code.=<<___ if ($win64);
  2331. movaps (%rsp), %xmm6
  2332. movaps 0x10(%rsp), %xmm7
  2333. movaps 0x20(%rsp), %xmm8
  2334. movaps 0x30(%rsp), %xmm9
  2335. movaps 0x40(%rsp), %xmm10
  2336. movaps 0x50(%rsp), %xmm11
  2337. movaps 0x60(%rsp), %xmm12
  2338. movaps 0x70(%rsp), %xmm13
  2339. movaps 0x80(%rsp), %xmm14
  2340. movaps 0x90(%rsp), %xmm15
  2341. lea 0xa8(%rsp), %rsp
  2342. ___
  2343. $code.=<<___;
  2344. ret
  2345. .LSEH_end_ecp_nistz256_gather_w5:
  2346. .size ecp_nistz256_gather_w5,.-ecp_nistz256_gather_w5
  2347. ################################################################################
  2348. # void ecp_nistz256_scatter_w7(uint64_t *val, uint64_t *in_t, int index);
  2349. .globl ecp_nistz256_scatter_w7
  2350. .type ecp_nistz256_scatter_w7,\@abi-omnipotent
  2351. .align 32
  2352. ecp_nistz256_scatter_w7:
  2353. movdqu 0x00($in_t), %xmm0
  2354. shl \$6, $index
  2355. movdqu 0x10($in_t), %xmm1
  2356. movdqu 0x20($in_t), %xmm2
  2357. movdqu 0x30($in_t), %xmm3
  2358. movdqa %xmm0, 0x00($val,$index)
  2359. movdqa %xmm1, 0x10($val,$index)
  2360. movdqa %xmm2, 0x20($val,$index)
  2361. movdqa %xmm3, 0x30($val,$index)
  2362. ret
  2363. .size ecp_nistz256_scatter_w7,.-ecp_nistz256_scatter_w7
  2364. ################################################################################
  2365. # void ecp_nistz256_gather_w7(uint64_t *val, uint64_t *in_t, int index);
  2366. .globl ecp_nistz256_gather_w7
  2367. .type ecp_nistz256_gather_w7,\@abi-omnipotent
  2368. .align 32
  2369. ecp_nistz256_gather_w7:
  2370. ___
  2371. $code.=<<___ if ($avx>1);
  2372. mov OPENSSL_ia32cap_P+8(%rip), %eax
  2373. test \$`1<<5`, %eax
  2374. jnz .Lavx2_gather_w7
  2375. ___
  2376. $code.=<<___ if ($win64);
  2377. lea -0x88(%rsp), %rax
  2378. .LSEH_begin_ecp_nistz256_gather_w7:
  2379. .byte 0x48,0x8d,0x60,0xe0 #lea -0x20(%rax), %rsp
  2380. .byte 0x0f,0x29,0x70,0xe0 #movaps %xmm6, -0x20(%rax)
  2381. .byte 0x0f,0x29,0x78,0xf0 #movaps %xmm7, -0x10(%rax)
  2382. .byte 0x44,0x0f,0x29,0x00 #movaps %xmm8, 0(%rax)
  2383. .byte 0x44,0x0f,0x29,0x48,0x10 #movaps %xmm9, 0x10(%rax)
  2384. .byte 0x44,0x0f,0x29,0x50,0x20 #movaps %xmm10, 0x20(%rax)
  2385. .byte 0x44,0x0f,0x29,0x58,0x30 #movaps %xmm11, 0x30(%rax)
  2386. .byte 0x44,0x0f,0x29,0x60,0x40 #movaps %xmm12, 0x40(%rax)
  2387. .byte 0x44,0x0f,0x29,0x68,0x50 #movaps %xmm13, 0x50(%rax)
  2388. .byte 0x44,0x0f,0x29,0x70,0x60 #movaps %xmm14, 0x60(%rax)
  2389. .byte 0x44,0x0f,0x29,0x78,0x70 #movaps %xmm15, 0x70(%rax)
  2390. ___
  2391. $code.=<<___;
  2392. movdqa .LOne(%rip), $M0
  2393. movd $index, $INDEX
  2394. pxor $Ra, $Ra
  2395. pxor $Rb, $Rb
  2396. pxor $Rc, $Rc
  2397. pxor $Rd, $Rd
  2398. movdqa $M0, $ONE
  2399. pshufd \$0, $INDEX, $INDEX
  2400. mov \$64, %rax
  2401. .Lselect_loop_sse_w7:
  2402. movdqa $M0, $TMP0
  2403. paddd $ONE, $M0
  2404. movdqa 16*0($in_t), $T0a
  2405. movdqa 16*1($in_t), $T0b
  2406. pcmpeqd $INDEX, $TMP0
  2407. movdqa 16*2($in_t), $T0c
  2408. movdqa 16*3($in_t), $T0d
  2409. lea 16*4($in_t), $in_t
  2410. pand $TMP0, $T0a
  2411. pand $TMP0, $T0b
  2412. por $T0a, $Ra
  2413. pand $TMP0, $T0c
  2414. por $T0b, $Rb
  2415. pand $TMP0, $T0d
  2416. por $T0c, $Rc
  2417. prefetcht0 255($in_t)
  2418. por $T0d, $Rd
  2419. dec %rax
  2420. jnz .Lselect_loop_sse_w7
  2421. movdqu $Ra, 16*0($val)
  2422. movdqu $Rb, 16*1($val)
  2423. movdqu $Rc, 16*2($val)
  2424. movdqu $Rd, 16*3($val)
  2425. ___
  2426. $code.=<<___ if ($win64);
  2427. movaps (%rsp), %xmm6
  2428. movaps 0x10(%rsp), %xmm7
  2429. movaps 0x20(%rsp), %xmm8
  2430. movaps 0x30(%rsp), %xmm9
  2431. movaps 0x40(%rsp), %xmm10
  2432. movaps 0x50(%rsp), %xmm11
  2433. movaps 0x60(%rsp), %xmm12
  2434. movaps 0x70(%rsp), %xmm13
  2435. movaps 0x80(%rsp), %xmm14
  2436. movaps 0x90(%rsp), %xmm15
  2437. lea 0xa8(%rsp), %rsp
  2438. ___
  2439. $code.=<<___;
  2440. ret
  2441. .LSEH_end_ecp_nistz256_gather_w7:
  2442. .size ecp_nistz256_gather_w7,.-ecp_nistz256_gather_w7
  2443. ___
  2444. }
  2445. if ($avx>1) {
  2446. my ($val,$in_t,$index)=$win64?("%rcx","%rdx","%r8d"):("%rdi","%rsi","%edx");
  2447. my ($TWO,$INDEX,$Ra,$Rb,$Rc)=map("%ymm$_",(0..4));
  2448. my ($M0,$T0a,$T0b,$T0c,$TMP0)=map("%ymm$_",(5..9));
  2449. my ($M1,$T1a,$T1b,$T1c,$TMP1)=map("%ymm$_",(10..14));
  2450. $code.=<<___;
  2451. ################################################################################
  2452. # void ecp_nistz256_avx2_gather_w5(uint64_t *val, uint64_t *in_t, int index);
  2453. .type ecp_nistz256_avx2_gather_w5,\@abi-omnipotent
  2454. .align 32
  2455. ecp_nistz256_avx2_gather_w5:
  2456. .Lavx2_gather_w5:
  2457. vzeroupper
  2458. ___
  2459. $code.=<<___ if ($win64);
  2460. lea -0x88(%rsp), %rax
  2461. mov %rsp,%r11
  2462. .LSEH_begin_ecp_nistz256_avx2_gather_w5:
  2463. .byte 0x48,0x8d,0x60,0xe0 # lea -0x20(%rax), %rsp
  2464. .byte 0xc5,0xf8,0x29,0x70,0xe0 # vmovaps %xmm6, -0x20(%rax)
  2465. .byte 0xc5,0xf8,0x29,0x78,0xf0 # vmovaps %xmm7, -0x10(%rax)
  2466. .byte 0xc5,0x78,0x29,0x40,0x00 # vmovaps %xmm8, 8(%rax)
  2467. .byte 0xc5,0x78,0x29,0x48,0x10 # vmovaps %xmm9, 0x10(%rax)
  2468. .byte 0xc5,0x78,0x29,0x50,0x20 # vmovaps %xmm10, 0x20(%rax)
  2469. .byte 0xc5,0x78,0x29,0x58,0x30 # vmovaps %xmm11, 0x30(%rax)
  2470. .byte 0xc5,0x78,0x29,0x60,0x40 # vmovaps %xmm12, 0x40(%rax)
  2471. .byte 0xc5,0x78,0x29,0x68,0x50 # vmovaps %xmm13, 0x50(%rax)
  2472. .byte 0xc5,0x78,0x29,0x70,0x60 # vmovaps %xmm14, 0x60(%rax)
  2473. .byte 0xc5,0x78,0x29,0x78,0x70 # vmovaps %xmm15, 0x70(%rax)
  2474. ___
  2475. $code.=<<___;
  2476. vmovdqa .LTwo(%rip), $TWO
  2477. vpxor $Ra, $Ra, $Ra
  2478. vpxor $Rb, $Rb, $Rb
  2479. vpxor $Rc, $Rc, $Rc
  2480. vmovdqa .LOne(%rip), $M0
  2481. vmovdqa .LTwo(%rip), $M1
  2482. vmovd $index, %xmm1
  2483. vpermd $INDEX, $Ra, $INDEX
  2484. mov \$8, %rax
  2485. .Lselect_loop_avx2_w5:
  2486. vmovdqa 32*0($in_t), $T0a
  2487. vmovdqa 32*1($in_t), $T0b
  2488. vmovdqa 32*2($in_t), $T0c
  2489. vmovdqa 32*3($in_t), $T1a
  2490. vmovdqa 32*4($in_t), $T1b
  2491. vmovdqa 32*5($in_t), $T1c
  2492. vpcmpeqd $INDEX, $M0, $TMP0
  2493. vpcmpeqd $INDEX, $M1, $TMP1
  2494. vpaddd $TWO, $M0, $M0
  2495. vpaddd $TWO, $M1, $M1
  2496. lea 32*6($in_t), $in_t
  2497. vpand $TMP0, $T0a, $T0a
  2498. vpand $TMP0, $T0b, $T0b
  2499. vpand $TMP0, $T0c, $T0c
  2500. vpand $TMP1, $T1a, $T1a
  2501. vpand $TMP1, $T1b, $T1b
  2502. vpand $TMP1, $T1c, $T1c
  2503. vpxor $T0a, $Ra, $Ra
  2504. vpxor $T0b, $Rb, $Rb
  2505. vpxor $T0c, $Rc, $Rc
  2506. vpxor $T1a, $Ra, $Ra
  2507. vpxor $T1b, $Rb, $Rb
  2508. vpxor $T1c, $Rc, $Rc
  2509. dec %rax
  2510. jnz .Lselect_loop_avx2_w5
  2511. vmovdqu $Ra, 32*0($val)
  2512. vmovdqu $Rb, 32*1($val)
  2513. vmovdqu $Rc, 32*2($val)
  2514. vzeroupper
  2515. ___
  2516. $code.=<<___ if ($win64);
  2517. movaps (%rsp), %xmm6
  2518. movaps 0x10(%rsp), %xmm7
  2519. movaps 0x20(%rsp), %xmm8
  2520. movaps 0x30(%rsp), %xmm9
  2521. movaps 0x40(%rsp), %xmm10
  2522. movaps 0x50(%rsp), %xmm11
  2523. movaps 0x60(%rsp), %xmm12
  2524. movaps 0x70(%rsp), %xmm13
  2525. movaps 0x80(%rsp), %xmm14
  2526. movaps 0x90(%rsp), %xmm15
  2527. lea (%r11), %rsp
  2528. ___
  2529. $code.=<<___;
  2530. ret
  2531. .LSEH_end_ecp_nistz256_avx2_gather_w5:
  2532. .size ecp_nistz256_avx2_gather_w5,.-ecp_nistz256_avx2_gather_w5
  2533. ___
  2534. }
  2535. if ($avx>1) {
  2536. my ($val,$in_t,$index)=$win64?("%rcx","%rdx","%r8d"):("%rdi","%rsi","%edx");
  2537. my ($THREE,$INDEX,$Ra,$Rb)=map("%ymm$_",(0..3));
  2538. my ($M0,$T0a,$T0b,$TMP0)=map("%ymm$_",(4..7));
  2539. my ($M1,$T1a,$T1b,$TMP1)=map("%ymm$_",(8..11));
  2540. my ($M2,$T2a,$T2b,$TMP2)=map("%ymm$_",(12..15));
  2541. $code.=<<___;
  2542. ################################################################################
  2543. # void ecp_nistz256_avx2_gather_w7(uint64_t *val, uint64_t *in_t, int index);
  2544. .globl ecp_nistz256_avx2_gather_w7
  2545. .type ecp_nistz256_avx2_gather_w7,\@abi-omnipotent
  2546. .align 32
  2547. ecp_nistz256_avx2_gather_w7:
  2548. .Lavx2_gather_w7:
  2549. vzeroupper
  2550. ___
  2551. $code.=<<___ if ($win64);
  2552. mov %rsp,%r11
  2553. lea -0x88(%rsp), %rax
  2554. .LSEH_begin_ecp_nistz256_avx2_gather_w7:
  2555. .byte 0x48,0x8d,0x60,0xe0 # lea -0x20(%rax), %rsp
  2556. .byte 0xc5,0xf8,0x29,0x70,0xe0 # vmovaps %xmm6, -0x20(%rax)
  2557. .byte 0xc5,0xf8,0x29,0x78,0xf0 # vmovaps %xmm7, -0x10(%rax)
  2558. .byte 0xc5,0x78,0x29,0x40,0x00 # vmovaps %xmm8, 8(%rax)
  2559. .byte 0xc5,0x78,0x29,0x48,0x10 # vmovaps %xmm9, 0x10(%rax)
  2560. .byte 0xc5,0x78,0x29,0x50,0x20 # vmovaps %xmm10, 0x20(%rax)
  2561. .byte 0xc5,0x78,0x29,0x58,0x30 # vmovaps %xmm11, 0x30(%rax)
  2562. .byte 0xc5,0x78,0x29,0x60,0x40 # vmovaps %xmm12, 0x40(%rax)
  2563. .byte 0xc5,0x78,0x29,0x68,0x50 # vmovaps %xmm13, 0x50(%rax)
  2564. .byte 0xc5,0x78,0x29,0x70,0x60 # vmovaps %xmm14, 0x60(%rax)
  2565. .byte 0xc5,0x78,0x29,0x78,0x70 # vmovaps %xmm15, 0x70(%rax)
  2566. ___
  2567. $code.=<<___;
  2568. vmovdqa .LThree(%rip), $THREE
  2569. vpxor $Ra, $Ra, $Ra
  2570. vpxor $Rb, $Rb, $Rb
  2571. vmovdqa .LOne(%rip), $M0
  2572. vmovdqa .LTwo(%rip), $M1
  2573. vmovdqa .LThree(%rip), $M2
  2574. vmovd $index, %xmm1
  2575. vpermd $INDEX, $Ra, $INDEX
  2576. # Skip index = 0, because it is implicitly the point at infinity
  2577. mov \$21, %rax
  2578. .Lselect_loop_avx2_w7:
  2579. vmovdqa 32*0($in_t), $T0a
  2580. vmovdqa 32*1($in_t), $T0b
  2581. vmovdqa 32*2($in_t), $T1a
  2582. vmovdqa 32*3($in_t), $T1b
  2583. vmovdqa 32*4($in_t), $T2a
  2584. vmovdqa 32*5($in_t), $T2b
  2585. vpcmpeqd $INDEX, $M0, $TMP0
  2586. vpcmpeqd $INDEX, $M1, $TMP1
  2587. vpcmpeqd $INDEX, $M2, $TMP2
  2588. vpaddd $THREE, $M0, $M0
  2589. vpaddd $THREE, $M1, $M1
  2590. vpaddd $THREE, $M2, $M2
  2591. lea 32*6($in_t), $in_t
  2592. vpand $TMP0, $T0a, $T0a
  2593. vpand $TMP0, $T0b, $T0b
  2594. vpand $TMP1, $T1a, $T1a
  2595. vpand $TMP1, $T1b, $T1b
  2596. vpand $TMP2, $T2a, $T2a
  2597. vpand $TMP2, $T2b, $T2b
  2598. vpxor $T0a, $Ra, $Ra
  2599. vpxor $T0b, $Rb, $Rb
  2600. vpxor $T1a, $Ra, $Ra
  2601. vpxor $T1b, $Rb, $Rb
  2602. vpxor $T2a, $Ra, $Ra
  2603. vpxor $T2b, $Rb, $Rb
  2604. dec %rax
  2605. jnz .Lselect_loop_avx2_w7
  2606. vmovdqa 32*0($in_t), $T0a
  2607. vmovdqa 32*1($in_t), $T0b
  2608. vpcmpeqd $INDEX, $M0, $TMP0
  2609. vpand $TMP0, $T0a, $T0a
  2610. vpand $TMP0, $T0b, $T0b
  2611. vpxor $T0a, $Ra, $Ra
  2612. vpxor $T0b, $Rb, $Rb
  2613. vmovdqu $Ra, 32*0($val)
  2614. vmovdqu $Rb, 32*1($val)
  2615. vzeroupper
  2616. ___
  2617. $code.=<<___ if ($win64);
  2618. movaps (%rsp), %xmm6
  2619. movaps 0x10(%rsp), %xmm7
  2620. movaps 0x20(%rsp), %xmm8
  2621. movaps 0x30(%rsp), %xmm9
  2622. movaps 0x40(%rsp), %xmm10
  2623. movaps 0x50(%rsp), %xmm11
  2624. movaps 0x60(%rsp), %xmm12
  2625. movaps 0x70(%rsp), %xmm13
  2626. movaps 0x80(%rsp), %xmm14
  2627. movaps 0x90(%rsp), %xmm15
  2628. lea (%r11), %rsp
  2629. ___
  2630. $code.=<<___;
  2631. ret
  2632. .LSEH_end_ecp_nistz256_avx2_gather_w7:
  2633. .size ecp_nistz256_avx2_gather_w7,.-ecp_nistz256_avx2_gather_w7
  2634. ___
  2635. } else {
  2636. $code.=<<___;
  2637. .globl ecp_nistz256_avx2_gather_w7
  2638. .type ecp_nistz256_avx2_gather_w7,\@function,3
  2639. .align 32
  2640. ecp_nistz256_avx2_gather_w7:
  2641. .byte 0x0f,0x0b # ud2
  2642. ret
  2643. .size ecp_nistz256_avx2_gather_w7,.-ecp_nistz256_avx2_gather_w7
  2644. ___
  2645. }
  2646. {{{
  2647. ########################################################################
  2648. # This block implements higher level point_double, point_add and
  2649. # point_add_affine. The key to performance in this case is to allow
  2650. # out-of-order execution logic to overlap computations from next step
  2651. # with tail processing from current step. By using tailored calling
  2652. # sequence we minimize inter-step overhead to give processor better
  2653. # shot at overlapping operations...
  2654. #
  2655. # You will notice that input data is copied to stack. Trouble is that
  2656. # there are no registers to spare for holding original pointers and
  2657. # reloading them, pointers, would create undesired dependencies on
  2658. # effective addresses calculation paths. In other words it's too done
  2659. # to favour out-of-order execution logic.
  2660. # <appro@openssl.org>
  2661. my ($r_ptr,$a_ptr,$b_org,$b_ptr)=("%rdi","%rsi","%rdx","%rbx");
  2662. my ($acc0,$acc1,$acc2,$acc3,$acc4,$acc5,$acc6,$acc7)=map("%r$_",(8..15));
  2663. my ($t0,$t1,$t2,$t3,$t4)=("%rax","%rbp","%rcx",$acc4,$acc4);
  2664. my ($poly1,$poly3)=($acc6,$acc7);
  2665. sub load_for_mul () {
  2666. my ($a,$b,$src0) = @_;
  2667. my $bias = $src0 eq "%rax" ? 0 : -128;
  2668. " mov $b, $src0
  2669. lea $b, $b_ptr
  2670. mov 8*0+$a, $acc1
  2671. mov 8*1+$a, $acc2
  2672. lea $bias+$a, $a_ptr
  2673. mov 8*2+$a, $acc3
  2674. mov 8*3+$a, $acc4"
  2675. }
  2676. sub load_for_sqr () {
  2677. my ($a,$src0) = @_;
  2678. my $bias = $src0 eq "%rax" ? 0 : -128;
  2679. " mov 8*0+$a, $src0
  2680. mov 8*1+$a, $acc6
  2681. lea $bias+$a, $a_ptr
  2682. mov 8*2+$a, $acc7
  2683. mov 8*3+$a, $acc0"
  2684. }
  2685. {
  2686. ########################################################################
  2687. # operate in 4-5-0-1 "name space" that matches multiplication output
  2688. #
  2689. my ($a0,$a1,$a2,$a3,$t3,$t4)=($acc4,$acc5,$acc0,$acc1,$acc2,$acc3);
  2690. $code.=<<___;
  2691. .type __ecp_nistz256_add_toq,\@abi-omnipotent
  2692. .align 32
  2693. __ecp_nistz256_add_toq:
  2694. xor $t4,$t4
  2695. add 8*0($b_ptr), $a0
  2696. adc 8*1($b_ptr), $a1
  2697. mov $a0, $t0
  2698. adc 8*2($b_ptr), $a2
  2699. adc 8*3($b_ptr), $a3
  2700. mov $a1, $t1
  2701. adc \$0, $t4
  2702. sub \$-1, $a0
  2703. mov $a2, $t2
  2704. sbb $poly1, $a1
  2705. sbb \$0, $a2
  2706. mov $a3, $t3
  2707. sbb $poly3, $a3
  2708. sbb \$0, $t4
  2709. cmovc $t0, $a0
  2710. cmovc $t1, $a1
  2711. mov $a0, 8*0($r_ptr)
  2712. cmovc $t2, $a2
  2713. mov $a1, 8*1($r_ptr)
  2714. cmovc $t3, $a3
  2715. mov $a2, 8*2($r_ptr)
  2716. mov $a3, 8*3($r_ptr)
  2717. ret
  2718. .size __ecp_nistz256_add_toq,.-__ecp_nistz256_add_toq
  2719. .type __ecp_nistz256_sub_fromq,\@abi-omnipotent
  2720. .align 32
  2721. __ecp_nistz256_sub_fromq:
  2722. sub 8*0($b_ptr), $a0
  2723. sbb 8*1($b_ptr), $a1
  2724. mov $a0, $t0
  2725. sbb 8*2($b_ptr), $a2
  2726. sbb 8*3($b_ptr), $a3
  2727. mov $a1, $t1
  2728. sbb $t4, $t4
  2729. add \$-1, $a0
  2730. mov $a2, $t2
  2731. adc $poly1, $a1
  2732. adc \$0, $a2
  2733. mov $a3, $t3
  2734. adc $poly3, $a3
  2735. test $t4, $t4
  2736. cmovz $t0, $a0
  2737. cmovz $t1, $a1
  2738. mov $a0, 8*0($r_ptr)
  2739. cmovz $t2, $a2
  2740. mov $a1, 8*1($r_ptr)
  2741. cmovz $t3, $a3
  2742. mov $a2, 8*2($r_ptr)
  2743. mov $a3, 8*3($r_ptr)
  2744. ret
  2745. .size __ecp_nistz256_sub_fromq,.-__ecp_nistz256_sub_fromq
  2746. .type __ecp_nistz256_subq,\@abi-omnipotent
  2747. .align 32
  2748. __ecp_nistz256_subq:
  2749. sub $a0, $t0
  2750. sbb $a1, $t1
  2751. mov $t0, $a0
  2752. sbb $a2, $t2
  2753. sbb $a3, $t3
  2754. mov $t1, $a1
  2755. sbb $t4, $t4
  2756. add \$-1, $t0
  2757. mov $t2, $a2
  2758. adc $poly1, $t1
  2759. adc \$0, $t2
  2760. mov $t3, $a3
  2761. adc $poly3, $t3
  2762. test $t4, $t4
  2763. cmovnz $t0, $a0
  2764. cmovnz $t1, $a1
  2765. cmovnz $t2, $a2
  2766. cmovnz $t3, $a3
  2767. ret
  2768. .size __ecp_nistz256_subq,.-__ecp_nistz256_subq
  2769. .type __ecp_nistz256_mul_by_2q,\@abi-omnipotent
  2770. .align 32
  2771. __ecp_nistz256_mul_by_2q:
  2772. xor $t4, $t4
  2773. add $a0, $a0 # a0:a3+a0:a3
  2774. adc $a1, $a1
  2775. mov $a0, $t0
  2776. adc $a2, $a2
  2777. adc $a3, $a3
  2778. mov $a1, $t1
  2779. adc \$0, $t4
  2780. sub \$-1, $a0
  2781. mov $a2, $t2
  2782. sbb $poly1, $a1
  2783. sbb \$0, $a2
  2784. mov $a3, $t3
  2785. sbb $poly3, $a3
  2786. sbb \$0, $t4
  2787. cmovc $t0, $a0
  2788. cmovc $t1, $a1
  2789. mov $a0, 8*0($r_ptr)
  2790. cmovc $t2, $a2
  2791. mov $a1, 8*1($r_ptr)
  2792. cmovc $t3, $a3
  2793. mov $a2, 8*2($r_ptr)
  2794. mov $a3, 8*3($r_ptr)
  2795. ret
  2796. .size __ecp_nistz256_mul_by_2q,.-__ecp_nistz256_mul_by_2q
  2797. ___
  2798. }
  2799. sub gen_double () {
  2800. my $x = shift;
  2801. my ($src0,$sfx,$bias);
  2802. my ($S,$M,$Zsqr,$in_x,$tmp0)=map(32*$_,(0..4));
  2803. if ($x ne "x") {
  2804. $src0 = "%rax";
  2805. $sfx = "";
  2806. $bias = 0;
  2807. $code.=<<___;
  2808. .globl ecp_nistz256_point_double
  2809. .type ecp_nistz256_point_double,\@function,2
  2810. .align 32
  2811. ecp_nistz256_point_double:
  2812. .cfi_startproc
  2813. ___
  2814. $code.=<<___ if ($addx);
  2815. mov \$0x80100, %ecx
  2816. and OPENSSL_ia32cap_P+8(%rip), %ecx
  2817. cmp \$0x80100, %ecx
  2818. je .Lpoint_doublex
  2819. ___
  2820. } else {
  2821. $src0 = "%rdx";
  2822. $sfx = "x";
  2823. $bias = 128;
  2824. $code.=<<___;
  2825. .type ecp_nistz256_point_doublex,\@function,2
  2826. .align 32
  2827. ecp_nistz256_point_doublex:
  2828. .cfi_startproc
  2829. .Lpoint_doublex:
  2830. ___
  2831. }
  2832. $code.=<<___;
  2833. push %rbp
  2834. .cfi_push %rbp
  2835. push %rbx
  2836. .cfi_push %rbx
  2837. push %r12
  2838. .cfi_push %r12
  2839. push %r13
  2840. .cfi_push %r13
  2841. push %r14
  2842. .cfi_push %r14
  2843. push %r15
  2844. .cfi_push %r15
  2845. sub \$32*5+8, %rsp
  2846. .cfi_adjust_cfa_offset 32*5+8
  2847. .Lpoint_double${x}_body:
  2848. .Lpoint_double_shortcut$x:
  2849. movdqu 0x00($a_ptr), %xmm0 # copy *(P256_POINT *)$a_ptr.x
  2850. mov $a_ptr, $b_ptr # backup copy
  2851. movdqu 0x10($a_ptr), %xmm1
  2852. mov 0x20+8*0($a_ptr), $acc4 # load in_y in "5-4-0-1" order
  2853. mov 0x20+8*1($a_ptr), $acc5
  2854. mov 0x20+8*2($a_ptr), $acc0
  2855. mov 0x20+8*3($a_ptr), $acc1
  2856. mov .Lpoly+8*1(%rip), $poly1
  2857. mov .Lpoly+8*3(%rip), $poly3
  2858. movdqa %xmm0, $in_x(%rsp)
  2859. movdqa %xmm1, $in_x+0x10(%rsp)
  2860. lea 0x20($r_ptr), $acc2
  2861. lea 0x40($r_ptr), $acc3
  2862. movq $r_ptr, %xmm0
  2863. movq $acc2, %xmm1
  2864. movq $acc3, %xmm2
  2865. lea $S(%rsp), $r_ptr
  2866. call __ecp_nistz256_mul_by_2$x # p256_mul_by_2(S, in_y);
  2867. mov 0x40+8*0($a_ptr), $src0
  2868. mov 0x40+8*1($a_ptr), $acc6
  2869. mov 0x40+8*2($a_ptr), $acc7
  2870. mov 0x40+8*3($a_ptr), $acc0
  2871. lea 0x40-$bias($a_ptr), $a_ptr
  2872. lea $Zsqr(%rsp), $r_ptr
  2873. call __ecp_nistz256_sqr_mont$x # p256_sqr_mont(Zsqr, in_z);
  2874. `&load_for_sqr("$S(%rsp)", "$src0")`
  2875. lea $S(%rsp), $r_ptr
  2876. call __ecp_nistz256_sqr_mont$x # p256_sqr_mont(S, S);
  2877. mov 0x20($b_ptr), $src0 # $b_ptr is still valid
  2878. mov 0x40+8*0($b_ptr), $acc1
  2879. mov 0x40+8*1($b_ptr), $acc2
  2880. mov 0x40+8*2($b_ptr), $acc3
  2881. mov 0x40+8*3($b_ptr), $acc4
  2882. lea 0x40-$bias($b_ptr), $a_ptr
  2883. lea 0x20($b_ptr), $b_ptr
  2884. movq %xmm2, $r_ptr
  2885. call __ecp_nistz256_mul_mont$x # p256_mul_mont(res_z, in_z, in_y);
  2886. call __ecp_nistz256_mul_by_2$x # p256_mul_by_2(res_z, res_z);
  2887. mov $in_x+8*0(%rsp), $acc4 # "5-4-0-1" order
  2888. mov $in_x+8*1(%rsp), $acc5
  2889. lea $Zsqr(%rsp), $b_ptr
  2890. mov $in_x+8*2(%rsp), $acc0
  2891. mov $in_x+8*3(%rsp), $acc1
  2892. lea $M(%rsp), $r_ptr
  2893. call __ecp_nistz256_add_to$x # p256_add(M, in_x, Zsqr);
  2894. mov $in_x+8*0(%rsp), $acc4 # "5-4-0-1" order
  2895. mov $in_x+8*1(%rsp), $acc5
  2896. lea $Zsqr(%rsp), $b_ptr
  2897. mov $in_x+8*2(%rsp), $acc0
  2898. mov $in_x+8*3(%rsp), $acc1
  2899. lea $Zsqr(%rsp), $r_ptr
  2900. call __ecp_nistz256_sub_from$x # p256_sub(Zsqr, in_x, Zsqr);
  2901. `&load_for_sqr("$S(%rsp)", "$src0")`
  2902. movq %xmm1, $r_ptr
  2903. call __ecp_nistz256_sqr_mont$x # p256_sqr_mont(res_y, S);
  2904. ___
  2905. {
  2906. ######## ecp_nistz256_div_by_2(res_y, res_y); ##########################
  2907. # operate in 4-5-6-7 "name space" that matches squaring output
  2908. #
  2909. my ($poly1,$poly3)=($a_ptr,$t1);
  2910. my ($a0,$a1,$a2,$a3,$t3,$t4,$t1)=($acc4,$acc5,$acc6,$acc7,$acc0,$acc1,$acc2);
  2911. $code.=<<___;
  2912. xor $t4, $t4
  2913. mov $a0, $t0
  2914. add \$-1, $a0
  2915. mov $a1, $t1
  2916. adc $poly1, $a1
  2917. mov $a2, $t2
  2918. adc \$0, $a2
  2919. mov $a3, $t3
  2920. adc $poly3, $a3
  2921. adc \$0, $t4
  2922. xor $a_ptr, $a_ptr # borrow $a_ptr
  2923. test \$1, $t0
  2924. cmovz $t0, $a0
  2925. cmovz $t1, $a1
  2926. cmovz $t2, $a2
  2927. cmovz $t3, $a3
  2928. cmovz $a_ptr, $t4
  2929. mov $a1, $t0 # a0:a3>>1
  2930. shr \$1, $a0
  2931. shl \$63, $t0
  2932. mov $a2, $t1
  2933. shr \$1, $a1
  2934. or $t0, $a0
  2935. shl \$63, $t1
  2936. mov $a3, $t2
  2937. shr \$1, $a2
  2938. or $t1, $a1
  2939. shl \$63, $t2
  2940. mov $a0, 8*0($r_ptr)
  2941. shr \$1, $a3
  2942. mov $a1, 8*1($r_ptr)
  2943. shl \$63, $t4
  2944. or $t2, $a2
  2945. or $t4, $a3
  2946. mov $a2, 8*2($r_ptr)
  2947. mov $a3, 8*3($r_ptr)
  2948. ___
  2949. }
  2950. $code.=<<___;
  2951. `&load_for_mul("$M(%rsp)", "$Zsqr(%rsp)", "$src0")`
  2952. lea $M(%rsp), $r_ptr
  2953. call __ecp_nistz256_mul_mont$x # p256_mul_mont(M, M, Zsqr);
  2954. lea $tmp0(%rsp), $r_ptr
  2955. call __ecp_nistz256_mul_by_2$x
  2956. lea $M(%rsp), $b_ptr
  2957. lea $M(%rsp), $r_ptr
  2958. call __ecp_nistz256_add_to$x # p256_mul_by_3(M, M);
  2959. `&load_for_mul("$S(%rsp)", "$in_x(%rsp)", "$src0")`
  2960. lea $S(%rsp), $r_ptr
  2961. call __ecp_nistz256_mul_mont$x # p256_mul_mont(S, S, in_x);
  2962. lea $tmp0(%rsp), $r_ptr
  2963. call __ecp_nistz256_mul_by_2$x # p256_mul_by_2(tmp0, S);
  2964. `&load_for_sqr("$M(%rsp)", "$src0")`
  2965. movq %xmm0, $r_ptr
  2966. call __ecp_nistz256_sqr_mont$x # p256_sqr_mont(res_x, M);
  2967. lea $tmp0(%rsp), $b_ptr
  2968. mov $acc6, $acc0 # harmonize sqr output and sub input
  2969. mov $acc7, $acc1
  2970. mov $a_ptr, $poly1
  2971. mov $t1, $poly3
  2972. call __ecp_nistz256_sub_from$x # p256_sub(res_x, res_x, tmp0);
  2973. mov $S+8*0(%rsp), $t0
  2974. mov $S+8*1(%rsp), $t1
  2975. mov $S+8*2(%rsp), $t2
  2976. mov $S+8*3(%rsp), $acc2 # "4-5-0-1" order
  2977. lea $S(%rsp), $r_ptr
  2978. call __ecp_nistz256_sub$x # p256_sub(S, S, res_x);
  2979. mov $M(%rsp), $src0
  2980. lea $M(%rsp), $b_ptr
  2981. mov $acc4, $acc6 # harmonize sub output and mul input
  2982. xor %ecx, %ecx
  2983. mov $acc4, $S+8*0(%rsp) # have to save:-(
  2984. mov $acc5, $acc2
  2985. mov $acc5, $S+8*1(%rsp)
  2986. cmovz $acc0, $acc3
  2987. mov $acc0, $S+8*2(%rsp)
  2988. lea $S-$bias(%rsp), $a_ptr
  2989. cmovz $acc1, $acc4
  2990. mov $acc1, $S+8*3(%rsp)
  2991. mov $acc6, $acc1
  2992. lea $S(%rsp), $r_ptr
  2993. call __ecp_nistz256_mul_mont$x # p256_mul_mont(S, S, M);
  2994. movq %xmm1, $b_ptr
  2995. movq %xmm1, $r_ptr
  2996. call __ecp_nistz256_sub_from$x # p256_sub(res_y, S, res_y);
  2997. lea 32*5+56(%rsp), %rsi
  2998. .cfi_def_cfa %rsi,8
  2999. mov -48(%rsi),%r15
  3000. .cfi_restore %r15
  3001. mov -40(%rsi),%r14
  3002. .cfi_restore %r14
  3003. mov -32(%rsi),%r13
  3004. .cfi_restore %r13
  3005. mov -24(%rsi),%r12
  3006. .cfi_restore %r12
  3007. mov -16(%rsi),%rbx
  3008. .cfi_restore %rbx
  3009. mov -8(%rsi),%rbp
  3010. .cfi_restore %rbp
  3011. lea (%rsi),%rsp
  3012. .cfi_def_cfa_register %rsp
  3013. .Lpoint_double${x}_epilogue:
  3014. ret
  3015. .cfi_endproc
  3016. .size ecp_nistz256_point_double$sfx,.-ecp_nistz256_point_double$sfx
  3017. ___
  3018. }
  3019. &gen_double("q");
  3020. sub gen_add () {
  3021. my $x = shift;
  3022. my ($src0,$sfx,$bias);
  3023. my ($H,$Hsqr,$R,$Rsqr,$Hcub,
  3024. $U1,$U2,$S1,$S2,
  3025. $res_x,$res_y,$res_z,
  3026. $in1_x,$in1_y,$in1_z,
  3027. $in2_x,$in2_y,$in2_z)=map(32*$_,(0..17));
  3028. my ($Z1sqr, $Z2sqr) = ($Hsqr, $Rsqr);
  3029. if ($x ne "x") {
  3030. $src0 = "%rax";
  3031. $sfx = "";
  3032. $bias = 0;
  3033. $code.=<<___;
  3034. .globl ecp_nistz256_point_add
  3035. .type ecp_nistz256_point_add,\@function,3
  3036. .align 32
  3037. ecp_nistz256_point_add:
  3038. .cfi_startproc
  3039. ___
  3040. $code.=<<___ if ($addx);
  3041. mov \$0x80100, %ecx
  3042. and OPENSSL_ia32cap_P+8(%rip), %ecx
  3043. cmp \$0x80100, %ecx
  3044. je .Lpoint_addx
  3045. ___
  3046. } else {
  3047. $src0 = "%rdx";
  3048. $sfx = "x";
  3049. $bias = 128;
  3050. $code.=<<___;
  3051. .type ecp_nistz256_point_addx,\@function,3
  3052. .align 32
  3053. ecp_nistz256_point_addx:
  3054. .cfi_startproc
  3055. .Lpoint_addx:
  3056. ___
  3057. }
  3058. $code.=<<___;
  3059. push %rbp
  3060. .cfi_push %rbp
  3061. push %rbx
  3062. .cfi_push %rbx
  3063. push %r12
  3064. .cfi_push %r12
  3065. push %r13
  3066. .cfi_push %r13
  3067. push %r14
  3068. .cfi_push %r14
  3069. push %r15
  3070. .cfi_push %r15
  3071. sub \$32*18+8, %rsp
  3072. .cfi_adjust_cfa_offset 32*18+8
  3073. .Lpoint_add${x}_body:
  3074. movdqu 0x00($a_ptr), %xmm0 # copy *(P256_POINT *)$a_ptr
  3075. movdqu 0x10($a_ptr), %xmm1
  3076. movdqu 0x20($a_ptr), %xmm2
  3077. movdqu 0x30($a_ptr), %xmm3
  3078. movdqu 0x40($a_ptr), %xmm4
  3079. movdqu 0x50($a_ptr), %xmm5
  3080. mov $a_ptr, $b_ptr # reassign
  3081. mov $b_org, $a_ptr # reassign
  3082. movdqa %xmm0, $in1_x(%rsp)
  3083. movdqa %xmm1, $in1_x+0x10(%rsp)
  3084. movdqa %xmm2, $in1_y(%rsp)
  3085. movdqa %xmm3, $in1_y+0x10(%rsp)
  3086. movdqa %xmm4, $in1_z(%rsp)
  3087. movdqa %xmm5, $in1_z+0x10(%rsp)
  3088. por %xmm4, %xmm5
  3089. movdqu 0x00($a_ptr), %xmm0 # copy *(P256_POINT *)$b_ptr
  3090. pshufd \$0xb1, %xmm5, %xmm3
  3091. movdqu 0x10($a_ptr), %xmm1
  3092. movdqu 0x20($a_ptr), %xmm2
  3093. por %xmm3, %xmm5
  3094. movdqu 0x30($a_ptr), %xmm3
  3095. mov 0x40+8*0($a_ptr), $src0 # load original in2_z
  3096. mov 0x40+8*1($a_ptr), $acc6
  3097. mov 0x40+8*2($a_ptr), $acc7
  3098. mov 0x40+8*3($a_ptr), $acc0
  3099. movdqa %xmm0, $in2_x(%rsp)
  3100. pshufd \$0x1e, %xmm5, %xmm4
  3101. movdqa %xmm1, $in2_x+0x10(%rsp)
  3102. movdqu 0x40($a_ptr),%xmm0 # in2_z again
  3103. movdqu 0x50($a_ptr),%xmm1
  3104. movdqa %xmm2, $in2_y(%rsp)
  3105. movdqa %xmm3, $in2_y+0x10(%rsp)
  3106. por %xmm4, %xmm5
  3107. pxor %xmm4, %xmm4
  3108. por %xmm0, %xmm1
  3109. movq $r_ptr, %xmm0 # save $r_ptr
  3110. lea 0x40-$bias($a_ptr), $a_ptr # $a_ptr is still valid
  3111. mov $src0, $in2_z+8*0(%rsp) # make in2_z copy
  3112. mov $acc6, $in2_z+8*1(%rsp)
  3113. mov $acc7, $in2_z+8*2(%rsp)
  3114. mov $acc0, $in2_z+8*3(%rsp)
  3115. lea $Z2sqr(%rsp), $r_ptr # Z2^2
  3116. call __ecp_nistz256_sqr_mont$x # p256_sqr_mont(Z2sqr, in2_z);
  3117. pcmpeqd %xmm4, %xmm5
  3118. pshufd \$0xb1, %xmm1, %xmm4
  3119. por %xmm1, %xmm4
  3120. pshufd \$0, %xmm5, %xmm5 # in1infty
  3121. pshufd \$0x1e, %xmm4, %xmm3
  3122. por %xmm3, %xmm4
  3123. pxor %xmm3, %xmm3
  3124. pcmpeqd %xmm3, %xmm4
  3125. pshufd \$0, %xmm4, %xmm4 # in2infty
  3126. mov 0x40+8*0($b_ptr), $src0 # load original in1_z
  3127. mov 0x40+8*1($b_ptr), $acc6
  3128. mov 0x40+8*2($b_ptr), $acc7
  3129. mov 0x40+8*3($b_ptr), $acc0
  3130. movq $b_ptr, %xmm1
  3131. lea 0x40-$bias($b_ptr), $a_ptr
  3132. lea $Z1sqr(%rsp), $r_ptr # Z1^2
  3133. call __ecp_nistz256_sqr_mont$x # p256_sqr_mont(Z1sqr, in1_z);
  3134. `&load_for_mul("$Z2sqr(%rsp)", "$in2_z(%rsp)", "$src0")`
  3135. lea $S1(%rsp), $r_ptr # S1 = Z2^3
  3136. call __ecp_nistz256_mul_mont$x # p256_mul_mont(S1, Z2sqr, in2_z);
  3137. `&load_for_mul("$Z1sqr(%rsp)", "$in1_z(%rsp)", "$src0")`
  3138. lea $S2(%rsp), $r_ptr # S2 = Z1^3
  3139. call __ecp_nistz256_mul_mont$x # p256_mul_mont(S2, Z1sqr, in1_z);
  3140. `&load_for_mul("$S1(%rsp)", "$in1_y(%rsp)", "$src0")`
  3141. lea $S1(%rsp), $r_ptr # S1 = Y1*Z2^3
  3142. call __ecp_nistz256_mul_mont$x # p256_mul_mont(S1, S1, in1_y);
  3143. `&load_for_mul("$S2(%rsp)", "$in2_y(%rsp)", "$src0")`
  3144. lea $S2(%rsp), $r_ptr # S2 = Y2*Z1^3
  3145. call __ecp_nistz256_mul_mont$x # p256_mul_mont(S2, S2, in2_y);
  3146. lea $S1(%rsp), $b_ptr
  3147. lea $R(%rsp), $r_ptr # R = S2 - S1
  3148. call __ecp_nistz256_sub_from$x # p256_sub(R, S2, S1);
  3149. or $acc5, $acc4 # see if result is zero
  3150. movdqa %xmm4, %xmm2
  3151. or $acc0, $acc4
  3152. or $acc1, $acc4
  3153. por %xmm5, %xmm2 # in1infty || in2infty
  3154. movq $acc4, %xmm3
  3155. `&load_for_mul("$Z2sqr(%rsp)", "$in1_x(%rsp)", "$src0")`
  3156. lea $U1(%rsp), $r_ptr # U1 = X1*Z2^2
  3157. call __ecp_nistz256_mul_mont$x # p256_mul_mont(U1, in1_x, Z2sqr);
  3158. `&load_for_mul("$Z1sqr(%rsp)", "$in2_x(%rsp)", "$src0")`
  3159. lea $U2(%rsp), $r_ptr # U2 = X2*Z1^2
  3160. call __ecp_nistz256_mul_mont$x # p256_mul_mont(U2, in2_x, Z1sqr);
  3161. lea $U1(%rsp), $b_ptr
  3162. lea $H(%rsp), $r_ptr # H = U2 - U1
  3163. call __ecp_nistz256_sub_from$x # p256_sub(H, U2, U1);
  3164. or $acc5, $acc4 # see if result is zero
  3165. or $acc0, $acc4
  3166. or $acc1, $acc4
  3167. .byte 0x3e # predict taken
  3168. jnz .Ladd_proceed$x # is_equal(U1,U2)?
  3169. movq %xmm2, $acc0
  3170. movq %xmm3, $acc1
  3171. test $acc0, $acc0
  3172. jnz .Ladd_proceed$x # (in1infty || in2infty)?
  3173. test $acc1, $acc1
  3174. jz .Ladd_double$x # is_equal(S1,S2)?
  3175. movq %xmm0, $r_ptr # restore $r_ptr
  3176. pxor %xmm0, %xmm0
  3177. movdqu %xmm0, 0x00($r_ptr)
  3178. movdqu %xmm0, 0x10($r_ptr)
  3179. movdqu %xmm0, 0x20($r_ptr)
  3180. movdqu %xmm0, 0x30($r_ptr)
  3181. movdqu %xmm0, 0x40($r_ptr)
  3182. movdqu %xmm0, 0x50($r_ptr)
  3183. jmp .Ladd_done$x
  3184. .align 32
  3185. .Ladd_double$x:
  3186. movq %xmm1, $a_ptr # restore $a_ptr
  3187. movq %xmm0, $r_ptr # restore $r_ptr
  3188. add \$`32*(18-5)`, %rsp # difference in frame sizes
  3189. jmp .Lpoint_double_shortcut$x
  3190. .align 32
  3191. .Ladd_proceed$x:
  3192. `&load_for_sqr("$R(%rsp)", "$src0")`
  3193. lea $Rsqr(%rsp), $r_ptr # R^2
  3194. call __ecp_nistz256_sqr_mont$x # p256_sqr_mont(Rsqr, R);
  3195. `&load_for_mul("$H(%rsp)", "$in1_z(%rsp)", "$src0")`
  3196. lea $res_z(%rsp), $r_ptr # Z3 = H*Z1*Z2
  3197. call __ecp_nistz256_mul_mont$x # p256_mul_mont(res_z, H, in1_z);
  3198. `&load_for_sqr("$H(%rsp)", "$src0")`
  3199. lea $Hsqr(%rsp), $r_ptr # H^2
  3200. call __ecp_nistz256_sqr_mont$x # p256_sqr_mont(Hsqr, H);
  3201. `&load_for_mul("$res_z(%rsp)", "$in2_z(%rsp)", "$src0")`
  3202. lea $res_z(%rsp), $r_ptr # Z3 = H*Z1*Z2
  3203. call __ecp_nistz256_mul_mont$x # p256_mul_mont(res_z, res_z, in2_z);
  3204. `&load_for_mul("$Hsqr(%rsp)", "$H(%rsp)", "$src0")`
  3205. lea $Hcub(%rsp), $r_ptr # H^3
  3206. call __ecp_nistz256_mul_mont$x # p256_mul_mont(Hcub, Hsqr, H);
  3207. `&load_for_mul("$Hsqr(%rsp)", "$U1(%rsp)", "$src0")`
  3208. lea $U2(%rsp), $r_ptr # U1*H^2
  3209. call __ecp_nistz256_mul_mont$x # p256_mul_mont(U2, U1, Hsqr);
  3210. ___
  3211. {
  3212. #######################################################################
  3213. # operate in 4-5-0-1 "name space" that matches multiplication output
  3214. #
  3215. my ($acc0,$acc1,$acc2,$acc3,$t3,$t4)=($acc4,$acc5,$acc0,$acc1,$acc2,$acc3);
  3216. my ($poly1, $poly3)=($acc6,$acc7);
  3217. $code.=<<___;
  3218. #lea $U2(%rsp), $a_ptr
  3219. #lea $Hsqr(%rsp), $r_ptr # 2*U1*H^2
  3220. #call __ecp_nistz256_mul_by_2 # ecp_nistz256_mul_by_2(Hsqr, U2);
  3221. xor $t4, $t4
  3222. add $acc0, $acc0 # a0:a3+a0:a3
  3223. lea $Rsqr(%rsp), $a_ptr
  3224. adc $acc1, $acc1
  3225. mov $acc0, $t0
  3226. adc $acc2, $acc2
  3227. adc $acc3, $acc3
  3228. mov $acc1, $t1
  3229. adc \$0, $t4
  3230. sub \$-1, $acc0
  3231. mov $acc2, $t2
  3232. sbb $poly1, $acc1
  3233. sbb \$0, $acc2
  3234. mov $acc3, $t3
  3235. sbb $poly3, $acc3
  3236. sbb \$0, $t4
  3237. cmovc $t0, $acc0
  3238. mov 8*0($a_ptr), $t0
  3239. cmovc $t1, $acc1
  3240. mov 8*1($a_ptr), $t1
  3241. cmovc $t2, $acc2
  3242. mov 8*2($a_ptr), $t2
  3243. cmovc $t3, $acc3
  3244. mov 8*3($a_ptr), $t3
  3245. call __ecp_nistz256_sub$x # p256_sub(res_x, Rsqr, Hsqr);
  3246. lea $Hcub(%rsp), $b_ptr
  3247. lea $res_x(%rsp), $r_ptr
  3248. call __ecp_nistz256_sub_from$x # p256_sub(res_x, res_x, Hcub);
  3249. mov $U2+8*0(%rsp), $t0
  3250. mov $U2+8*1(%rsp), $t1
  3251. mov $U2+8*2(%rsp), $t2
  3252. mov $U2+8*3(%rsp), $t3
  3253. lea $res_y(%rsp), $r_ptr
  3254. call __ecp_nistz256_sub$x # p256_sub(res_y, U2, res_x);
  3255. mov $acc0, 8*0($r_ptr) # save the result, as
  3256. mov $acc1, 8*1($r_ptr) # __ecp_nistz256_sub doesn't
  3257. mov $acc2, 8*2($r_ptr)
  3258. mov $acc3, 8*3($r_ptr)
  3259. ___
  3260. }
  3261. $code.=<<___;
  3262. `&load_for_mul("$S1(%rsp)", "$Hcub(%rsp)", "$src0")`
  3263. lea $S2(%rsp), $r_ptr
  3264. call __ecp_nistz256_mul_mont$x # p256_mul_mont(S2, S1, Hcub);
  3265. `&load_for_mul("$R(%rsp)", "$res_y(%rsp)", "$src0")`
  3266. lea $res_y(%rsp), $r_ptr
  3267. call __ecp_nistz256_mul_mont$x # p256_mul_mont(res_y, R, res_y);
  3268. lea $S2(%rsp), $b_ptr
  3269. lea $res_y(%rsp), $r_ptr
  3270. call __ecp_nistz256_sub_from$x # p256_sub(res_y, res_y, S2);
  3271. movq %xmm0, $r_ptr # restore $r_ptr
  3272. movdqa %xmm5, %xmm0 # copy_conditional(res_z, in2_z, in1infty);
  3273. movdqa %xmm5, %xmm1
  3274. pandn $res_z(%rsp), %xmm0
  3275. movdqa %xmm5, %xmm2
  3276. pandn $res_z+0x10(%rsp), %xmm1
  3277. movdqa %xmm5, %xmm3
  3278. pand $in2_z(%rsp), %xmm2
  3279. pand $in2_z+0x10(%rsp), %xmm3
  3280. por %xmm0, %xmm2
  3281. por %xmm1, %xmm3
  3282. movdqa %xmm4, %xmm0 # copy_conditional(res_z, in1_z, in2infty);
  3283. movdqa %xmm4, %xmm1
  3284. pandn %xmm2, %xmm0
  3285. movdqa %xmm4, %xmm2
  3286. pandn %xmm3, %xmm1
  3287. movdqa %xmm4, %xmm3
  3288. pand $in1_z(%rsp), %xmm2
  3289. pand $in1_z+0x10(%rsp), %xmm3
  3290. por %xmm0, %xmm2
  3291. por %xmm1, %xmm3
  3292. movdqu %xmm2, 0x40($r_ptr)
  3293. movdqu %xmm3, 0x50($r_ptr)
  3294. movdqa %xmm5, %xmm0 # copy_conditional(res_x, in2_x, in1infty);
  3295. movdqa %xmm5, %xmm1
  3296. pandn $res_x(%rsp), %xmm0
  3297. movdqa %xmm5, %xmm2
  3298. pandn $res_x+0x10(%rsp), %xmm1
  3299. movdqa %xmm5, %xmm3
  3300. pand $in2_x(%rsp), %xmm2
  3301. pand $in2_x+0x10(%rsp), %xmm3
  3302. por %xmm0, %xmm2
  3303. por %xmm1, %xmm3
  3304. movdqa %xmm4, %xmm0 # copy_conditional(res_x, in1_x, in2infty);
  3305. movdqa %xmm4, %xmm1
  3306. pandn %xmm2, %xmm0
  3307. movdqa %xmm4, %xmm2
  3308. pandn %xmm3, %xmm1
  3309. movdqa %xmm4, %xmm3
  3310. pand $in1_x(%rsp), %xmm2
  3311. pand $in1_x+0x10(%rsp), %xmm3
  3312. por %xmm0, %xmm2
  3313. por %xmm1, %xmm3
  3314. movdqu %xmm2, 0x00($r_ptr)
  3315. movdqu %xmm3, 0x10($r_ptr)
  3316. movdqa %xmm5, %xmm0 # copy_conditional(res_y, in2_y, in1infty);
  3317. movdqa %xmm5, %xmm1
  3318. pandn $res_y(%rsp), %xmm0
  3319. movdqa %xmm5, %xmm2
  3320. pandn $res_y+0x10(%rsp), %xmm1
  3321. movdqa %xmm5, %xmm3
  3322. pand $in2_y(%rsp), %xmm2
  3323. pand $in2_y+0x10(%rsp), %xmm3
  3324. por %xmm0, %xmm2
  3325. por %xmm1, %xmm3
  3326. movdqa %xmm4, %xmm0 # copy_conditional(res_y, in1_y, in2infty);
  3327. movdqa %xmm4, %xmm1
  3328. pandn %xmm2, %xmm0
  3329. movdqa %xmm4, %xmm2
  3330. pandn %xmm3, %xmm1
  3331. movdqa %xmm4, %xmm3
  3332. pand $in1_y(%rsp), %xmm2
  3333. pand $in1_y+0x10(%rsp), %xmm3
  3334. por %xmm0, %xmm2
  3335. por %xmm1, %xmm3
  3336. movdqu %xmm2, 0x20($r_ptr)
  3337. movdqu %xmm3, 0x30($r_ptr)
  3338. .Ladd_done$x:
  3339. lea 32*18+56(%rsp), %rsi
  3340. .cfi_def_cfa %rsi,8
  3341. mov -48(%rsi),%r15
  3342. .cfi_restore %r15
  3343. mov -40(%rsi),%r14
  3344. .cfi_restore %r14
  3345. mov -32(%rsi),%r13
  3346. .cfi_restore %r13
  3347. mov -24(%rsi),%r12
  3348. .cfi_restore %r12
  3349. mov -16(%rsi),%rbx
  3350. .cfi_restore %rbx
  3351. mov -8(%rsi),%rbp
  3352. .cfi_restore %rbp
  3353. lea (%rsi),%rsp
  3354. .cfi_def_cfa_register %rsp
  3355. .Lpoint_add${x}_epilogue:
  3356. ret
  3357. .cfi_endproc
  3358. .size ecp_nistz256_point_add$sfx,.-ecp_nistz256_point_add$sfx
  3359. ___
  3360. }
  3361. &gen_add("q");
  3362. sub gen_add_affine () {
  3363. my $x = shift;
  3364. my ($src0,$sfx,$bias);
  3365. my ($U2,$S2,$H,$R,$Hsqr,$Hcub,$Rsqr,
  3366. $res_x,$res_y,$res_z,
  3367. $in1_x,$in1_y,$in1_z,
  3368. $in2_x,$in2_y)=map(32*$_,(0..14));
  3369. my $Z1sqr = $S2;
  3370. if ($x ne "x") {
  3371. $src0 = "%rax";
  3372. $sfx = "";
  3373. $bias = 0;
  3374. $code.=<<___;
  3375. .globl ecp_nistz256_point_add_affine
  3376. .type ecp_nistz256_point_add_affine,\@function,3
  3377. .align 32
  3378. ecp_nistz256_point_add_affine:
  3379. .cfi_startproc
  3380. ___
  3381. $code.=<<___ if ($addx);
  3382. mov \$0x80100, %ecx
  3383. and OPENSSL_ia32cap_P+8(%rip), %ecx
  3384. cmp \$0x80100, %ecx
  3385. je .Lpoint_add_affinex
  3386. ___
  3387. } else {
  3388. $src0 = "%rdx";
  3389. $sfx = "x";
  3390. $bias = 128;
  3391. $code.=<<___;
  3392. .type ecp_nistz256_point_add_affinex,\@function,3
  3393. .align 32
  3394. ecp_nistz256_point_add_affinex:
  3395. .cfi_startproc
  3396. .Lpoint_add_affinex:
  3397. ___
  3398. }
  3399. $code.=<<___;
  3400. push %rbp
  3401. .cfi_push %rbp
  3402. push %rbx
  3403. .cfi_push %rbx
  3404. push %r12
  3405. .cfi_push %r12
  3406. push %r13
  3407. .cfi_push %r13
  3408. push %r14
  3409. .cfi_push %r14
  3410. push %r15
  3411. .cfi_push %r15
  3412. sub \$32*15+8, %rsp
  3413. .cfi_adjust_cfa_offset 32*15+8
  3414. .Ladd_affine${x}_body:
  3415. movdqu 0x00($a_ptr), %xmm0 # copy *(P256_POINT *)$a_ptr
  3416. mov $b_org, $b_ptr # reassign
  3417. movdqu 0x10($a_ptr), %xmm1
  3418. movdqu 0x20($a_ptr), %xmm2
  3419. movdqu 0x30($a_ptr), %xmm3
  3420. movdqu 0x40($a_ptr), %xmm4
  3421. movdqu 0x50($a_ptr), %xmm5
  3422. mov 0x40+8*0($a_ptr), $src0 # load original in1_z
  3423. mov 0x40+8*1($a_ptr), $acc6
  3424. mov 0x40+8*2($a_ptr), $acc7
  3425. mov 0x40+8*3($a_ptr), $acc0
  3426. movdqa %xmm0, $in1_x(%rsp)
  3427. movdqa %xmm1, $in1_x+0x10(%rsp)
  3428. movdqa %xmm2, $in1_y(%rsp)
  3429. movdqa %xmm3, $in1_y+0x10(%rsp)
  3430. movdqa %xmm4, $in1_z(%rsp)
  3431. movdqa %xmm5, $in1_z+0x10(%rsp)
  3432. por %xmm4, %xmm5
  3433. movdqu 0x00($b_ptr), %xmm0 # copy *(P256_POINT_AFFINE *)$b_ptr
  3434. pshufd \$0xb1, %xmm5, %xmm3
  3435. movdqu 0x10($b_ptr), %xmm1
  3436. movdqu 0x20($b_ptr), %xmm2
  3437. por %xmm3, %xmm5
  3438. movdqu 0x30($b_ptr), %xmm3
  3439. movdqa %xmm0, $in2_x(%rsp)
  3440. pshufd \$0x1e, %xmm5, %xmm4
  3441. movdqa %xmm1, $in2_x+0x10(%rsp)
  3442. por %xmm0, %xmm1
  3443. movq $r_ptr, %xmm0 # save $r_ptr
  3444. movdqa %xmm2, $in2_y(%rsp)
  3445. movdqa %xmm3, $in2_y+0x10(%rsp)
  3446. por %xmm2, %xmm3
  3447. por %xmm4, %xmm5
  3448. pxor %xmm4, %xmm4
  3449. por %xmm1, %xmm3
  3450. lea 0x40-$bias($a_ptr), $a_ptr # $a_ptr is still valid
  3451. lea $Z1sqr(%rsp), $r_ptr # Z1^2
  3452. call __ecp_nistz256_sqr_mont$x # p256_sqr_mont(Z1sqr, in1_z);
  3453. pcmpeqd %xmm4, %xmm5
  3454. pshufd \$0xb1, %xmm3, %xmm4
  3455. mov 0x00($b_ptr), $src0 # $b_ptr is still valid
  3456. #lea 0x00($b_ptr), $b_ptr
  3457. mov $acc4, $acc1 # harmonize sqr output and mul input
  3458. por %xmm3, %xmm4
  3459. pshufd \$0, %xmm5, %xmm5 # in1infty
  3460. pshufd \$0x1e, %xmm4, %xmm3
  3461. mov $acc5, $acc2
  3462. por %xmm3, %xmm4
  3463. pxor %xmm3, %xmm3
  3464. mov $acc6, $acc3
  3465. pcmpeqd %xmm3, %xmm4
  3466. pshufd \$0, %xmm4, %xmm4 # in2infty
  3467. lea $Z1sqr-$bias(%rsp), $a_ptr
  3468. mov $acc7, $acc4
  3469. lea $U2(%rsp), $r_ptr # U2 = X2*Z1^2
  3470. call __ecp_nistz256_mul_mont$x # p256_mul_mont(U2, Z1sqr, in2_x);
  3471. lea $in1_x(%rsp), $b_ptr
  3472. lea $H(%rsp), $r_ptr # H = U2 - U1
  3473. call __ecp_nistz256_sub_from$x # p256_sub(H, U2, in1_x);
  3474. `&load_for_mul("$Z1sqr(%rsp)", "$in1_z(%rsp)", "$src0")`
  3475. lea $S2(%rsp), $r_ptr # S2 = Z1^3
  3476. call __ecp_nistz256_mul_mont$x # p256_mul_mont(S2, Z1sqr, in1_z);
  3477. `&load_for_mul("$H(%rsp)", "$in1_z(%rsp)", "$src0")`
  3478. lea $res_z(%rsp), $r_ptr # Z3 = H*Z1*Z2
  3479. call __ecp_nistz256_mul_mont$x # p256_mul_mont(res_z, H, in1_z);
  3480. `&load_for_mul("$S2(%rsp)", "$in2_y(%rsp)", "$src0")`
  3481. lea $S2(%rsp), $r_ptr # S2 = Y2*Z1^3
  3482. call __ecp_nistz256_mul_mont$x # p256_mul_mont(S2, S2, in2_y);
  3483. lea $in1_y(%rsp), $b_ptr
  3484. lea $R(%rsp), $r_ptr # R = S2 - S1
  3485. call __ecp_nistz256_sub_from$x # p256_sub(R, S2, in1_y);
  3486. `&load_for_sqr("$H(%rsp)", "$src0")`
  3487. lea $Hsqr(%rsp), $r_ptr # H^2
  3488. call __ecp_nistz256_sqr_mont$x # p256_sqr_mont(Hsqr, H);
  3489. `&load_for_sqr("$R(%rsp)", "$src0")`
  3490. lea $Rsqr(%rsp), $r_ptr # R^2
  3491. call __ecp_nistz256_sqr_mont$x # p256_sqr_mont(Rsqr, R);
  3492. `&load_for_mul("$H(%rsp)", "$Hsqr(%rsp)", "$src0")`
  3493. lea $Hcub(%rsp), $r_ptr # H^3
  3494. call __ecp_nistz256_mul_mont$x # p256_mul_mont(Hcub, Hsqr, H);
  3495. `&load_for_mul("$Hsqr(%rsp)", "$in1_x(%rsp)", "$src0")`
  3496. lea $U2(%rsp), $r_ptr # U1*H^2
  3497. call __ecp_nistz256_mul_mont$x # p256_mul_mont(U2, in1_x, Hsqr);
  3498. ___
  3499. {
  3500. #######################################################################
  3501. # operate in 4-5-0-1 "name space" that matches multiplication output
  3502. #
  3503. my ($acc0,$acc1,$acc2,$acc3,$t3,$t4)=($acc4,$acc5,$acc0,$acc1,$acc2,$acc3);
  3504. my ($poly1, $poly3)=($acc6,$acc7);
  3505. $code.=<<___;
  3506. #lea $U2(%rsp), $a_ptr
  3507. #lea $Hsqr(%rsp), $r_ptr # 2*U1*H^2
  3508. #call __ecp_nistz256_mul_by_2 # ecp_nistz256_mul_by_2(Hsqr, U2);
  3509. xor $t4, $t4
  3510. add $acc0, $acc0 # a0:a3+a0:a3
  3511. lea $Rsqr(%rsp), $a_ptr
  3512. adc $acc1, $acc1
  3513. mov $acc0, $t0
  3514. adc $acc2, $acc2
  3515. adc $acc3, $acc3
  3516. mov $acc1, $t1
  3517. adc \$0, $t4
  3518. sub \$-1, $acc0
  3519. mov $acc2, $t2
  3520. sbb $poly1, $acc1
  3521. sbb \$0, $acc2
  3522. mov $acc3, $t3
  3523. sbb $poly3, $acc3
  3524. sbb \$0, $t4
  3525. cmovc $t0, $acc0
  3526. mov 8*0($a_ptr), $t0
  3527. cmovc $t1, $acc1
  3528. mov 8*1($a_ptr), $t1
  3529. cmovc $t2, $acc2
  3530. mov 8*2($a_ptr), $t2
  3531. cmovc $t3, $acc3
  3532. mov 8*3($a_ptr), $t3
  3533. call __ecp_nistz256_sub$x # p256_sub(res_x, Rsqr, Hsqr);
  3534. lea $Hcub(%rsp), $b_ptr
  3535. lea $res_x(%rsp), $r_ptr
  3536. call __ecp_nistz256_sub_from$x # p256_sub(res_x, res_x, Hcub);
  3537. mov $U2+8*0(%rsp), $t0
  3538. mov $U2+8*1(%rsp), $t1
  3539. mov $U2+8*2(%rsp), $t2
  3540. mov $U2+8*3(%rsp), $t3
  3541. lea $H(%rsp), $r_ptr
  3542. call __ecp_nistz256_sub$x # p256_sub(H, U2, res_x);
  3543. mov $acc0, 8*0($r_ptr) # save the result, as
  3544. mov $acc1, 8*1($r_ptr) # __ecp_nistz256_sub doesn't
  3545. mov $acc2, 8*2($r_ptr)
  3546. mov $acc3, 8*3($r_ptr)
  3547. ___
  3548. }
  3549. $code.=<<___;
  3550. `&load_for_mul("$Hcub(%rsp)", "$in1_y(%rsp)", "$src0")`
  3551. lea $S2(%rsp), $r_ptr
  3552. call __ecp_nistz256_mul_mont$x # p256_mul_mont(S2, Hcub, in1_y);
  3553. `&load_for_mul("$H(%rsp)", "$R(%rsp)", "$src0")`
  3554. lea $H(%rsp), $r_ptr
  3555. call __ecp_nistz256_mul_mont$x # p256_mul_mont(H, H, R);
  3556. lea $S2(%rsp), $b_ptr
  3557. lea $res_y(%rsp), $r_ptr
  3558. call __ecp_nistz256_sub_from$x # p256_sub(res_y, H, S2);
  3559. movq %xmm0, $r_ptr # restore $r_ptr
  3560. movdqa %xmm5, %xmm0 # copy_conditional(res_z, ONE, in1infty);
  3561. movdqa %xmm5, %xmm1
  3562. pandn $res_z(%rsp), %xmm0
  3563. movdqa %xmm5, %xmm2
  3564. pandn $res_z+0x10(%rsp), %xmm1
  3565. movdqa %xmm5, %xmm3
  3566. pand .LONE_mont(%rip), %xmm2
  3567. pand .LONE_mont+0x10(%rip), %xmm3
  3568. por %xmm0, %xmm2
  3569. por %xmm1, %xmm3
  3570. movdqa %xmm4, %xmm0 # copy_conditional(res_z, in1_z, in2infty);
  3571. movdqa %xmm4, %xmm1
  3572. pandn %xmm2, %xmm0
  3573. movdqa %xmm4, %xmm2
  3574. pandn %xmm3, %xmm1
  3575. movdqa %xmm4, %xmm3
  3576. pand $in1_z(%rsp), %xmm2
  3577. pand $in1_z+0x10(%rsp), %xmm3
  3578. por %xmm0, %xmm2
  3579. por %xmm1, %xmm3
  3580. movdqu %xmm2, 0x40($r_ptr)
  3581. movdqu %xmm3, 0x50($r_ptr)
  3582. movdqa %xmm5, %xmm0 # copy_conditional(res_x, in2_x, in1infty);
  3583. movdqa %xmm5, %xmm1
  3584. pandn $res_x(%rsp), %xmm0
  3585. movdqa %xmm5, %xmm2
  3586. pandn $res_x+0x10(%rsp), %xmm1
  3587. movdqa %xmm5, %xmm3
  3588. pand $in2_x(%rsp), %xmm2
  3589. pand $in2_x+0x10(%rsp), %xmm3
  3590. por %xmm0, %xmm2
  3591. por %xmm1, %xmm3
  3592. movdqa %xmm4, %xmm0 # copy_conditional(res_x, in1_x, in2infty);
  3593. movdqa %xmm4, %xmm1
  3594. pandn %xmm2, %xmm0
  3595. movdqa %xmm4, %xmm2
  3596. pandn %xmm3, %xmm1
  3597. movdqa %xmm4, %xmm3
  3598. pand $in1_x(%rsp), %xmm2
  3599. pand $in1_x+0x10(%rsp), %xmm3
  3600. por %xmm0, %xmm2
  3601. por %xmm1, %xmm3
  3602. movdqu %xmm2, 0x00($r_ptr)
  3603. movdqu %xmm3, 0x10($r_ptr)
  3604. movdqa %xmm5, %xmm0 # copy_conditional(res_y, in2_y, in1infty);
  3605. movdqa %xmm5, %xmm1
  3606. pandn $res_y(%rsp), %xmm0
  3607. movdqa %xmm5, %xmm2
  3608. pandn $res_y+0x10(%rsp), %xmm1
  3609. movdqa %xmm5, %xmm3
  3610. pand $in2_y(%rsp), %xmm2
  3611. pand $in2_y+0x10(%rsp), %xmm3
  3612. por %xmm0, %xmm2
  3613. por %xmm1, %xmm3
  3614. movdqa %xmm4, %xmm0 # copy_conditional(res_y, in1_y, in2infty);
  3615. movdqa %xmm4, %xmm1
  3616. pandn %xmm2, %xmm0
  3617. movdqa %xmm4, %xmm2
  3618. pandn %xmm3, %xmm1
  3619. movdqa %xmm4, %xmm3
  3620. pand $in1_y(%rsp), %xmm2
  3621. pand $in1_y+0x10(%rsp), %xmm3
  3622. por %xmm0, %xmm2
  3623. por %xmm1, %xmm3
  3624. movdqu %xmm2, 0x20($r_ptr)
  3625. movdqu %xmm3, 0x30($r_ptr)
  3626. lea 32*15+56(%rsp), %rsi
  3627. .cfi_def_cfa %rsi,8
  3628. mov -48(%rsi),%r15
  3629. .cfi_restore %r15
  3630. mov -40(%rsi),%r14
  3631. .cfi_restore %r14
  3632. mov -32(%rsi),%r13
  3633. .cfi_restore %r13
  3634. mov -24(%rsi),%r12
  3635. .cfi_restore %r12
  3636. mov -16(%rsi),%rbx
  3637. .cfi_restore %rbx
  3638. mov -8(%rsi),%rbp
  3639. .cfi_restore %rbp
  3640. lea (%rsi),%rsp
  3641. .cfi_def_cfa_register %rsp
  3642. .Ladd_affine${x}_epilogue:
  3643. ret
  3644. .cfi_endproc
  3645. .size ecp_nistz256_point_add_affine$sfx,.-ecp_nistz256_point_add_affine$sfx
  3646. ___
  3647. }
  3648. &gen_add_affine("q");
  3649. ########################################################################
  3650. # AD*X magic
  3651. #
  3652. if ($addx) { {
  3653. ########################################################################
  3654. # operate in 4-5-0-1 "name space" that matches multiplication output
  3655. #
  3656. my ($a0,$a1,$a2,$a3,$t3,$t4)=($acc4,$acc5,$acc0,$acc1,$acc2,$acc3);
  3657. $code.=<<___;
  3658. .type __ecp_nistz256_add_tox,\@abi-omnipotent
  3659. .align 32
  3660. __ecp_nistz256_add_tox:
  3661. xor $t4, $t4
  3662. adc 8*0($b_ptr), $a0
  3663. adc 8*1($b_ptr), $a1
  3664. mov $a0, $t0
  3665. adc 8*2($b_ptr), $a2
  3666. adc 8*3($b_ptr), $a3
  3667. mov $a1, $t1
  3668. adc \$0, $t4
  3669. xor $t3, $t3
  3670. sbb \$-1, $a0
  3671. mov $a2, $t2
  3672. sbb $poly1, $a1
  3673. sbb \$0, $a2
  3674. mov $a3, $t3
  3675. sbb $poly3, $a3
  3676. sbb \$0, $t4
  3677. cmovc $t0, $a0
  3678. cmovc $t1, $a1
  3679. mov $a0, 8*0($r_ptr)
  3680. cmovc $t2, $a2
  3681. mov $a1, 8*1($r_ptr)
  3682. cmovc $t3, $a3
  3683. mov $a2, 8*2($r_ptr)
  3684. mov $a3, 8*3($r_ptr)
  3685. ret
  3686. .size __ecp_nistz256_add_tox,.-__ecp_nistz256_add_tox
  3687. .type __ecp_nistz256_sub_fromx,\@abi-omnipotent
  3688. .align 32
  3689. __ecp_nistz256_sub_fromx:
  3690. xor $t4, $t4
  3691. sbb 8*0($b_ptr), $a0
  3692. sbb 8*1($b_ptr), $a1
  3693. mov $a0, $t0
  3694. sbb 8*2($b_ptr), $a2
  3695. sbb 8*3($b_ptr), $a3
  3696. mov $a1, $t1
  3697. sbb \$0, $t4
  3698. xor $t3, $t3
  3699. adc \$-1, $a0
  3700. mov $a2, $t2
  3701. adc $poly1, $a1
  3702. adc \$0, $a2
  3703. mov $a3, $t3
  3704. adc $poly3, $a3
  3705. bt \$0, $t4
  3706. cmovnc $t0, $a0
  3707. cmovnc $t1, $a1
  3708. mov $a0, 8*0($r_ptr)
  3709. cmovnc $t2, $a2
  3710. mov $a1, 8*1($r_ptr)
  3711. cmovnc $t3, $a3
  3712. mov $a2, 8*2($r_ptr)
  3713. mov $a3, 8*3($r_ptr)
  3714. ret
  3715. .size __ecp_nistz256_sub_fromx,.-__ecp_nistz256_sub_fromx
  3716. .type __ecp_nistz256_subx,\@abi-omnipotent
  3717. .align 32
  3718. __ecp_nistz256_subx:
  3719. xor $t4, $t4
  3720. sbb $a0, $t0
  3721. sbb $a1, $t1
  3722. mov $t0, $a0
  3723. sbb $a2, $t2
  3724. sbb $a3, $t3
  3725. mov $t1, $a1
  3726. sbb \$0, $t4
  3727. xor $a3 ,$a3
  3728. adc \$-1, $t0
  3729. mov $t2, $a2
  3730. adc $poly1, $t1
  3731. adc \$0, $t2
  3732. mov $t3, $a3
  3733. adc $poly3, $t3
  3734. bt \$0, $t4
  3735. cmovc $t0, $a0
  3736. cmovc $t1, $a1
  3737. cmovc $t2, $a2
  3738. cmovc $t3, $a3
  3739. ret
  3740. .size __ecp_nistz256_subx,.-__ecp_nistz256_subx
  3741. .type __ecp_nistz256_mul_by_2x,\@abi-omnipotent
  3742. .align 32
  3743. __ecp_nistz256_mul_by_2x:
  3744. xor $t4, $t4
  3745. adc $a0, $a0 # a0:a3+a0:a3
  3746. adc $a1, $a1
  3747. mov $a0, $t0
  3748. adc $a2, $a2
  3749. adc $a3, $a3
  3750. mov $a1, $t1
  3751. adc \$0, $t4
  3752. xor $t3, $t3
  3753. sbb \$-1, $a0
  3754. mov $a2, $t2
  3755. sbb $poly1, $a1
  3756. sbb \$0, $a2
  3757. mov $a3, $t3
  3758. sbb $poly3, $a3
  3759. sbb \$0, $t4
  3760. cmovc $t0, $a0
  3761. cmovc $t1, $a1
  3762. mov $a0, 8*0($r_ptr)
  3763. cmovc $t2, $a2
  3764. mov $a1, 8*1($r_ptr)
  3765. cmovc $t3, $a3
  3766. mov $a2, 8*2($r_ptr)
  3767. mov $a3, 8*3($r_ptr)
  3768. ret
  3769. .size __ecp_nistz256_mul_by_2x,.-__ecp_nistz256_mul_by_2x
  3770. ___
  3771. }
  3772. &gen_double("x");
  3773. &gen_add("x");
  3774. &gen_add_affine("x");
  3775. }
  3776. }}}
  3777. # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
  3778. # CONTEXT *context,DISPATCHER_CONTEXT *disp)
  3779. if ($win64) {
  3780. $rec="%rcx";
  3781. $frame="%rdx";
  3782. $context="%r8";
  3783. $disp="%r9";
  3784. $code.=<<___;
  3785. .extern __imp_RtlVirtualUnwind
  3786. .type short_handler,\@abi-omnipotent
  3787. .align 16
  3788. short_handler:
  3789. push %rsi
  3790. push %rdi
  3791. push %rbx
  3792. push %rbp
  3793. push %r12
  3794. push %r13
  3795. push %r14
  3796. push %r15
  3797. pushfq
  3798. sub \$64,%rsp
  3799. mov 120($context),%rax # pull context->Rax
  3800. mov 248($context),%rbx # pull context->Rip
  3801. mov 8($disp),%rsi # disp->ImageBase
  3802. mov 56($disp),%r11 # disp->HandlerData
  3803. mov 0(%r11),%r10d # HandlerData[0]
  3804. lea (%rsi,%r10),%r10 # end of prologue label
  3805. cmp %r10,%rbx # context->Rip<end of prologue label
  3806. jb .Lcommon_seh_tail
  3807. mov 152($context),%rax # pull context->Rsp
  3808. mov 4(%r11),%r10d # HandlerData[1]
  3809. lea (%rsi,%r10),%r10 # epilogue label
  3810. cmp %r10,%rbx # context->Rip>=epilogue label
  3811. jae .Lcommon_seh_tail
  3812. lea 16(%rax),%rax
  3813. mov -8(%rax),%r12
  3814. mov -16(%rax),%r13
  3815. mov %r12,216($context) # restore context->R12
  3816. mov %r13,224($context) # restore context->R13
  3817. jmp .Lcommon_seh_tail
  3818. .size short_handler,.-short_handler
  3819. .type full_handler,\@abi-omnipotent
  3820. .align 16
  3821. full_handler:
  3822. push %rsi
  3823. push %rdi
  3824. push %rbx
  3825. push %rbp
  3826. push %r12
  3827. push %r13
  3828. push %r14
  3829. push %r15
  3830. pushfq
  3831. sub \$64,%rsp
  3832. mov 120($context),%rax # pull context->Rax
  3833. mov 248($context),%rbx # pull context->Rip
  3834. mov 8($disp),%rsi # disp->ImageBase
  3835. mov 56($disp),%r11 # disp->HandlerData
  3836. mov 0(%r11),%r10d # HandlerData[0]
  3837. lea (%rsi,%r10),%r10 # end of prologue label
  3838. cmp %r10,%rbx # context->Rip<end of prologue label
  3839. jb .Lcommon_seh_tail
  3840. mov 152($context),%rax # pull context->Rsp
  3841. mov 4(%r11),%r10d # HandlerData[1]
  3842. lea (%rsi,%r10),%r10 # epilogue label
  3843. cmp %r10,%rbx # context->Rip>=epilogue label
  3844. jae .Lcommon_seh_tail
  3845. mov 8(%r11),%r10d # HandlerData[2]
  3846. lea (%rax,%r10),%rax
  3847. mov -8(%rax),%rbp
  3848. mov -16(%rax),%rbx
  3849. mov -24(%rax),%r12
  3850. mov -32(%rax),%r13
  3851. mov -40(%rax),%r14
  3852. mov -48(%rax),%r15
  3853. mov %rbx,144($context) # restore context->Rbx
  3854. mov %rbp,160($context) # restore context->Rbp
  3855. mov %r12,216($context) # restore context->R12
  3856. mov %r13,224($context) # restore context->R13
  3857. mov %r14,232($context) # restore context->R14
  3858. mov %r15,240($context) # restore context->R15
  3859. .Lcommon_seh_tail:
  3860. mov 8(%rax),%rdi
  3861. mov 16(%rax),%rsi
  3862. mov %rax,152($context) # restore context->Rsp
  3863. mov %rsi,168($context) # restore context->Rsi
  3864. mov %rdi,176($context) # restore context->Rdi
  3865. mov 40($disp),%rdi # disp->ContextRecord
  3866. mov $context,%rsi # context
  3867. mov \$154,%ecx # sizeof(CONTEXT)
  3868. .long 0xa548f3fc # cld; rep movsq
  3869. mov $disp,%rsi
  3870. xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
  3871. mov 8(%rsi),%rdx # arg2, disp->ImageBase
  3872. mov 0(%rsi),%r8 # arg3, disp->ControlPc
  3873. mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
  3874. mov 40(%rsi),%r10 # disp->ContextRecord
  3875. lea 56(%rsi),%r11 # &disp->HandlerData
  3876. lea 24(%rsi),%r12 # &disp->EstablisherFrame
  3877. mov %r10,32(%rsp) # arg5
  3878. mov %r11,40(%rsp) # arg6
  3879. mov %r12,48(%rsp) # arg7
  3880. mov %rcx,56(%rsp) # arg8, (NULL)
  3881. call *__imp_RtlVirtualUnwind(%rip)
  3882. mov \$1,%eax # ExceptionContinueSearch
  3883. add \$64,%rsp
  3884. popfq
  3885. pop %r15
  3886. pop %r14
  3887. pop %r13
  3888. pop %r12
  3889. pop %rbp
  3890. pop %rbx
  3891. pop %rdi
  3892. pop %rsi
  3893. ret
  3894. .size full_handler,.-full_handler
  3895. .section .pdata
  3896. .align 4
  3897. .rva .LSEH_begin_ecp_nistz256_mul_by_2
  3898. .rva .LSEH_end_ecp_nistz256_mul_by_2
  3899. .rva .LSEH_info_ecp_nistz256_mul_by_2
  3900. .rva .LSEH_begin_ecp_nistz256_div_by_2
  3901. .rva .LSEH_end_ecp_nistz256_div_by_2
  3902. .rva .LSEH_info_ecp_nistz256_div_by_2
  3903. .rva .LSEH_begin_ecp_nistz256_mul_by_3
  3904. .rva .LSEH_end_ecp_nistz256_mul_by_3
  3905. .rva .LSEH_info_ecp_nistz256_mul_by_3
  3906. .rva .LSEH_begin_ecp_nistz256_add
  3907. .rva .LSEH_end_ecp_nistz256_add
  3908. .rva .LSEH_info_ecp_nistz256_add
  3909. .rva .LSEH_begin_ecp_nistz256_sub
  3910. .rva .LSEH_end_ecp_nistz256_sub
  3911. .rva .LSEH_info_ecp_nistz256_sub
  3912. .rva .LSEH_begin_ecp_nistz256_neg
  3913. .rva .LSEH_end_ecp_nistz256_neg
  3914. .rva .LSEH_info_ecp_nistz256_neg
  3915. .rva .LSEH_begin_ecp_nistz256_ord_mul_mont
  3916. .rva .LSEH_end_ecp_nistz256_ord_mul_mont
  3917. .rva .LSEH_info_ecp_nistz256_ord_mul_mont
  3918. .rva .LSEH_begin_ecp_nistz256_ord_sqr_mont
  3919. .rva .LSEH_end_ecp_nistz256_ord_sqr_mont
  3920. .rva .LSEH_info_ecp_nistz256_ord_sqr_mont
  3921. ___
  3922. $code.=<<___ if ($addx);
  3923. .rva .LSEH_begin_ecp_nistz256_ord_mul_montx
  3924. .rva .LSEH_end_ecp_nistz256_ord_mul_montx
  3925. .rva .LSEH_info_ecp_nistz256_ord_mul_montx
  3926. .rva .LSEH_begin_ecp_nistz256_ord_sqr_montx
  3927. .rva .LSEH_end_ecp_nistz256_ord_sqr_montx
  3928. .rva .LSEH_info_ecp_nistz256_ord_sqr_montx
  3929. ___
  3930. $code.=<<___;
  3931. .rva .LSEH_begin_ecp_nistz256_to_mont
  3932. .rva .LSEH_end_ecp_nistz256_to_mont
  3933. .rva .LSEH_info_ecp_nistz256_to_mont
  3934. .rva .LSEH_begin_ecp_nistz256_mul_mont
  3935. .rva .LSEH_end_ecp_nistz256_mul_mont
  3936. .rva .LSEH_info_ecp_nistz256_mul_mont
  3937. .rva .LSEH_begin_ecp_nistz256_sqr_mont
  3938. .rva .LSEH_end_ecp_nistz256_sqr_mont
  3939. .rva .LSEH_info_ecp_nistz256_sqr_mont
  3940. .rva .LSEH_begin_ecp_nistz256_from_mont
  3941. .rva .LSEH_end_ecp_nistz256_from_mont
  3942. .rva .LSEH_info_ecp_nistz256_from_mont
  3943. .rva .LSEH_begin_ecp_nistz256_gather_w5
  3944. .rva .LSEH_end_ecp_nistz256_gather_w5
  3945. .rva .LSEH_info_ecp_nistz256_gather_wX
  3946. .rva .LSEH_begin_ecp_nistz256_gather_w7
  3947. .rva .LSEH_end_ecp_nistz256_gather_w7
  3948. .rva .LSEH_info_ecp_nistz256_gather_wX
  3949. ___
  3950. $code.=<<___ if ($avx>1);
  3951. .rva .LSEH_begin_ecp_nistz256_avx2_gather_w5
  3952. .rva .LSEH_end_ecp_nistz256_avx2_gather_w5
  3953. .rva .LSEH_info_ecp_nistz256_avx2_gather_wX
  3954. .rva .LSEH_begin_ecp_nistz256_avx2_gather_w7
  3955. .rva .LSEH_end_ecp_nistz256_avx2_gather_w7
  3956. .rva .LSEH_info_ecp_nistz256_avx2_gather_wX
  3957. ___
  3958. $code.=<<___;
  3959. .rva .LSEH_begin_ecp_nistz256_point_double
  3960. .rva .LSEH_end_ecp_nistz256_point_double
  3961. .rva .LSEH_info_ecp_nistz256_point_double
  3962. .rva .LSEH_begin_ecp_nistz256_point_add
  3963. .rva .LSEH_end_ecp_nistz256_point_add
  3964. .rva .LSEH_info_ecp_nistz256_point_add
  3965. .rva .LSEH_begin_ecp_nistz256_point_add_affine
  3966. .rva .LSEH_end_ecp_nistz256_point_add_affine
  3967. .rva .LSEH_info_ecp_nistz256_point_add_affine
  3968. ___
  3969. $code.=<<___ if ($addx);
  3970. .rva .LSEH_begin_ecp_nistz256_point_doublex
  3971. .rva .LSEH_end_ecp_nistz256_point_doublex
  3972. .rva .LSEH_info_ecp_nistz256_point_doublex
  3973. .rva .LSEH_begin_ecp_nistz256_point_addx
  3974. .rva .LSEH_end_ecp_nistz256_point_addx
  3975. .rva .LSEH_info_ecp_nistz256_point_addx
  3976. .rva .LSEH_begin_ecp_nistz256_point_add_affinex
  3977. .rva .LSEH_end_ecp_nistz256_point_add_affinex
  3978. .rva .LSEH_info_ecp_nistz256_point_add_affinex
  3979. ___
  3980. $code.=<<___;
  3981. .section .xdata
  3982. .align 8
  3983. .LSEH_info_ecp_nistz256_mul_by_2:
  3984. .byte 9,0,0,0
  3985. .rva short_handler
  3986. .rva .Lmul_by_2_body,.Lmul_by_2_epilogue # HandlerData[]
  3987. .LSEH_info_ecp_nistz256_div_by_2:
  3988. .byte 9,0,0,0
  3989. .rva short_handler
  3990. .rva .Ldiv_by_2_body,.Ldiv_by_2_epilogue # HandlerData[]
  3991. .LSEH_info_ecp_nistz256_mul_by_3:
  3992. .byte 9,0,0,0
  3993. .rva short_handler
  3994. .rva .Lmul_by_3_body,.Lmul_by_3_epilogue # HandlerData[]
  3995. .LSEH_info_ecp_nistz256_add:
  3996. .byte 9,0,0,0
  3997. .rva short_handler
  3998. .rva .Ladd_body,.Ladd_epilogue # HandlerData[]
  3999. .LSEH_info_ecp_nistz256_sub:
  4000. .byte 9,0,0,0
  4001. .rva short_handler
  4002. .rva .Lsub_body,.Lsub_epilogue # HandlerData[]
  4003. .LSEH_info_ecp_nistz256_neg:
  4004. .byte 9,0,0,0
  4005. .rva short_handler
  4006. .rva .Lneg_body,.Lneg_epilogue # HandlerData[]
  4007. .LSEH_info_ecp_nistz256_ord_mul_mont:
  4008. .byte 9,0,0,0
  4009. .rva full_handler
  4010. .rva .Lord_mul_body,.Lord_mul_epilogue # HandlerData[]
  4011. .long 48,0
  4012. .LSEH_info_ecp_nistz256_ord_sqr_mont:
  4013. .byte 9,0,0,0
  4014. .rva full_handler
  4015. .rva .Lord_sqr_body,.Lord_sqr_epilogue # HandlerData[]
  4016. .long 48,0
  4017. ___
  4018. $code.=<<___ if ($addx);
  4019. .LSEH_info_ecp_nistz256_ord_mul_montx:
  4020. .byte 9,0,0,0
  4021. .rva full_handler
  4022. .rva .Lord_mulx_body,.Lord_mulx_epilogue # HandlerData[]
  4023. .long 48,0
  4024. .LSEH_info_ecp_nistz256_ord_sqr_montx:
  4025. .byte 9,0,0,0
  4026. .rva full_handler
  4027. .rva .Lord_sqrx_body,.Lord_sqrx_epilogue # HandlerData[]
  4028. .long 48,0
  4029. ___
  4030. $code.=<<___;
  4031. .LSEH_info_ecp_nistz256_to_mont:
  4032. .byte 9,0,0,0
  4033. .rva full_handler
  4034. .rva .Lmul_body,.Lmul_epilogue # HandlerData[]
  4035. .long 48,0
  4036. .LSEH_info_ecp_nistz256_mul_mont:
  4037. .byte 9,0,0,0
  4038. .rva full_handler
  4039. .rva .Lmul_body,.Lmul_epilogue # HandlerData[]
  4040. .long 48,0
  4041. .LSEH_info_ecp_nistz256_sqr_mont:
  4042. .byte 9,0,0,0
  4043. .rva full_handler
  4044. .rva .Lsqr_body,.Lsqr_epilogue # HandlerData[]
  4045. .long 48,0
  4046. .LSEH_info_ecp_nistz256_from_mont:
  4047. .byte 9,0,0,0
  4048. .rva short_handler
  4049. .rva .Lfrom_body,.Lfrom_epilogue # HandlerData[]
  4050. .LSEH_info_ecp_nistz256_gather_wX:
  4051. .byte 0x01,0x33,0x16,0x00
  4052. .byte 0x33,0xf8,0x09,0x00 #movaps 0x90(rsp),xmm15
  4053. .byte 0x2e,0xe8,0x08,0x00 #movaps 0x80(rsp),xmm14
  4054. .byte 0x29,0xd8,0x07,0x00 #movaps 0x70(rsp),xmm13
  4055. .byte 0x24,0xc8,0x06,0x00 #movaps 0x60(rsp),xmm12
  4056. .byte 0x1f,0xb8,0x05,0x00 #movaps 0x50(rsp),xmm11
  4057. .byte 0x1a,0xa8,0x04,0x00 #movaps 0x40(rsp),xmm10
  4058. .byte 0x15,0x98,0x03,0x00 #movaps 0x30(rsp),xmm9
  4059. .byte 0x10,0x88,0x02,0x00 #movaps 0x20(rsp),xmm8
  4060. .byte 0x0c,0x78,0x01,0x00 #movaps 0x10(rsp),xmm7
  4061. .byte 0x08,0x68,0x00,0x00 #movaps 0x00(rsp),xmm6
  4062. .byte 0x04,0x01,0x15,0x00 #sub rsp,0xa8
  4063. .align 8
  4064. ___
  4065. $code.=<<___ if ($avx>1);
  4066. .LSEH_info_ecp_nistz256_avx2_gather_wX:
  4067. .byte 0x01,0x36,0x17,0x0b
  4068. .byte 0x36,0xf8,0x09,0x00 # vmovaps 0x90(rsp),xmm15
  4069. .byte 0x31,0xe8,0x08,0x00 # vmovaps 0x80(rsp),xmm14
  4070. .byte 0x2c,0xd8,0x07,0x00 # vmovaps 0x70(rsp),xmm13
  4071. .byte 0x27,0xc8,0x06,0x00 # vmovaps 0x60(rsp),xmm12
  4072. .byte 0x22,0xb8,0x05,0x00 # vmovaps 0x50(rsp),xmm11
  4073. .byte 0x1d,0xa8,0x04,0x00 # vmovaps 0x40(rsp),xmm10
  4074. .byte 0x18,0x98,0x03,0x00 # vmovaps 0x30(rsp),xmm9
  4075. .byte 0x13,0x88,0x02,0x00 # vmovaps 0x20(rsp),xmm8
  4076. .byte 0x0e,0x78,0x01,0x00 # vmovaps 0x10(rsp),xmm7
  4077. .byte 0x09,0x68,0x00,0x00 # vmovaps 0x00(rsp),xmm6
  4078. .byte 0x04,0x01,0x15,0x00 # sub rsp,0xa8
  4079. .byte 0x00,0xb3,0x00,0x00 # set_frame r11
  4080. .align 8
  4081. ___
  4082. $code.=<<___;
  4083. .LSEH_info_ecp_nistz256_point_double:
  4084. .byte 9,0,0,0
  4085. .rva full_handler
  4086. .rva .Lpoint_doubleq_body,.Lpoint_doubleq_epilogue # HandlerData[]
  4087. .long 32*5+56,0
  4088. .LSEH_info_ecp_nistz256_point_add:
  4089. .byte 9,0,0,0
  4090. .rva full_handler
  4091. .rva .Lpoint_addq_body,.Lpoint_addq_epilogue # HandlerData[]
  4092. .long 32*18+56,0
  4093. .LSEH_info_ecp_nistz256_point_add_affine:
  4094. .byte 9,0,0,0
  4095. .rva full_handler
  4096. .rva .Ladd_affineq_body,.Ladd_affineq_epilogue # HandlerData[]
  4097. .long 32*15+56,0
  4098. ___
  4099. $code.=<<___ if ($addx);
  4100. .align 8
  4101. .LSEH_info_ecp_nistz256_point_doublex:
  4102. .byte 9,0,0,0
  4103. .rva full_handler
  4104. .rva .Lpoint_doublex_body,.Lpoint_doublex_epilogue # HandlerData[]
  4105. .long 32*5+56,0
  4106. .LSEH_info_ecp_nistz256_point_addx:
  4107. .byte 9,0,0,0
  4108. .rva full_handler
  4109. .rva .Lpoint_addx_body,.Lpoint_addx_epilogue # HandlerData[]
  4110. .long 32*18+56,0
  4111. .LSEH_info_ecp_nistz256_point_add_affinex:
  4112. .byte 9,0,0,0
  4113. .rva full_handler
  4114. .rva .Ladd_affinex_body,.Ladd_affinex_epilogue # HandlerData[]
  4115. .long 32*15+56,0
  4116. ___
  4117. }
  4118. ########################################################################
  4119. # Convert ecp_nistz256_table.c to layout expected by ecp_nistz_gather_w7
  4120. #
  4121. open TABLE,"<ecp_nistz256_table.c" or
  4122. open TABLE,"<${dir}../ecp_nistz256_table.c" or
  4123. die "failed to open ecp_nistz256_table.c:",$!;
  4124. use integer;
  4125. foreach(<TABLE>) {
  4126. s/TOBN\(\s*(0x[0-9a-f]+),\s*(0x[0-9a-f]+)\s*\)/push @arr,hex($2),hex($1)/geo;
  4127. }
  4128. close TABLE;
  4129. die "insane number of elements" if ($#arr != 64*16*37-1);
  4130. print <<___;
  4131. .text
  4132. .globl ecp_nistz256_precomputed
  4133. .type ecp_nistz256_precomputed,\@object
  4134. .align 4096
  4135. ecp_nistz256_precomputed:
  4136. ___
  4137. while (@line=splice(@arr,0,16)) {
  4138. print ".long\t",join(',',map { sprintf "0x%08x",$_} @line),"\n";
  4139. }
  4140. print <<___;
  4141. .size ecp_nistz256_precomputed,.-ecp_nistz256_precomputed
  4142. ___
  4143. $code =~ s/\`([^\`]*)\`/eval $1/gem;
  4144. print $code;
  4145. close STDOUT;