ecp_nistz256-x86_64.pl 102 KB

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