ecp_nistz256-x86_64.pl 102 KB

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