2
0

ecp_nistz256-armv4.pl 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the OpenSSL license (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. # ====================================================================
  9. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  10. # project. The module is, however, dual licensed under OpenSSL and
  11. # CRYPTOGAMS licenses depending on where you obtain it. For further
  12. # details see http://www.openssl.org/~appro/cryptogams/.
  13. # ====================================================================
  14. #
  15. # ECP_NISTZ256 module for ARMv4.
  16. #
  17. # October 2014.
  18. #
  19. # Original ECP_NISTZ256 submission targeting x86_64 is detailed in
  20. # http://eprint.iacr.org/2013/816. In the process of adaptation
  21. # original .c module was made 32-bit savvy in order to make this
  22. # implementation possible.
  23. #
  24. # with/without -DECP_NISTZ256_ASM
  25. # Cortex-A8 +53-170%
  26. # Cortex-A9 +76-205%
  27. # Cortex-A15 +100-316%
  28. # Snapdragon S4 +66-187%
  29. #
  30. # Ranges denote minimum and maximum improvement coefficients depending
  31. # on benchmark. Lower coefficients are for ECDSA sign, server-side
  32. # operation. Keep in mind that +200% means 3x improvement.
  33. $flavour = shift;
  34. if ($flavour=~/\w[\w\-]*\.\w+$/) { $output=$flavour; undef $flavour; }
  35. else { while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {} }
  36. if ($flavour && $flavour ne "void") {
  37. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  38. ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
  39. ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
  40. die "can't locate arm-xlate.pl";
  41. open STDOUT,"| \"$^X\" $xlate $flavour $output";
  42. } else {
  43. open STDOUT,">$output";
  44. }
  45. $code.=<<___;
  46. #include "arm_arch.h"
  47. .text
  48. #if defined(__thumb2__)
  49. .syntax unified
  50. .thumb
  51. #else
  52. .code 32
  53. #endif
  54. ___
  55. ########################################################################
  56. # Convert ecp_nistz256_table.c to layout expected by ecp_nistz_gather_w7
  57. #
  58. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  59. open TABLE,"<ecp_nistz256_table.c" or
  60. open TABLE,"<${dir}../ecp_nistz256_table.c" or
  61. die "failed to open ecp_nistz256_table.c:",$!;
  62. use integer;
  63. foreach(<TABLE>) {
  64. s/TOBN\(\s*(0x[0-9a-f]+),\s*(0x[0-9a-f]+)\s*\)/push @arr,hex($2),hex($1)/geo;
  65. }
  66. close TABLE;
  67. # See ecp_nistz256_table.c for explanation for why it's 64*16*37.
  68. # 64*16*37-1 is because $#arr returns last valid index or @arr, not
  69. # amount of elements.
  70. die "insane number of elements" if ($#arr != 64*16*37-1);
  71. $code.=<<___;
  72. .globl ecp_nistz256_precomputed
  73. .type ecp_nistz256_precomputed,%object
  74. .align 12
  75. ecp_nistz256_precomputed:
  76. ___
  77. ########################################################################
  78. # this conversion smashes P256_POINT_AFFINE by individual bytes with
  79. # 64 byte interval, similar to
  80. # 1111222233334444
  81. # 1234123412341234
  82. for(1..37) {
  83. @tbl = splice(@arr,0,64*16);
  84. for($i=0;$i<64;$i++) {
  85. undef @line;
  86. for($j=0;$j<64;$j++) {
  87. push @line,(@tbl[$j*16+$i/4]>>(($i%4)*8))&0xff;
  88. }
  89. $code.=".byte\t";
  90. $code.=join(',',map { sprintf "0x%02x",$_} @line);
  91. $code.="\n";
  92. }
  93. }
  94. $code.=<<___;
  95. .size ecp_nistz256_precomputed,.-ecp_nistz256_precomputed
  96. .align 5
  97. .LRR: @ 2^512 mod P precomputed for NIST P256 polynomial
  98. .long 0x00000003, 0x00000000, 0xffffffff, 0xfffffffb
  99. .long 0xfffffffe, 0xffffffff, 0xfffffffd, 0x00000004
  100. .Lone:
  101. .long 1,0,0,0,0,0,0,0
  102. .asciz "ECP_NISTZ256 for ARMv4, CRYPTOGAMS by <appro\@openssl.org>"
  103. .align 6
  104. ___
  105. ########################################################################
  106. # common register layout, note that $t2 is link register, so that if
  107. # internal subroutine uses $t2, then it has to offload lr...
  108. ($r_ptr,$a_ptr,$b_ptr,$ff,$a0,$a1,$a2,$a3,$a4,$a5,$a6,$a7,$t1,$t2)=
  109. map("r$_",(0..12,14));
  110. ($t0,$t3)=($ff,$a_ptr);
  111. $code.=<<___;
  112. @ void ecp_nistz256_to_mont(BN_ULONG r0[8],const BN_ULONG r1[8]);
  113. .globl ecp_nistz256_to_mont
  114. .type ecp_nistz256_to_mont,%function
  115. ecp_nistz256_to_mont:
  116. adr $b_ptr,.LRR
  117. b .Lecp_nistz256_mul_mont
  118. .size ecp_nistz256_to_mont,.-ecp_nistz256_to_mont
  119. @ void ecp_nistz256_from_mont(BN_ULONG r0[8],const BN_ULONG r1[8]);
  120. .globl ecp_nistz256_from_mont
  121. .type ecp_nistz256_from_mont,%function
  122. ecp_nistz256_from_mont:
  123. adr $b_ptr,.Lone
  124. b .Lecp_nistz256_mul_mont
  125. .size ecp_nistz256_from_mont,.-ecp_nistz256_from_mont
  126. @ void ecp_nistz256_mul_by_2(BN_ULONG r0[8],const BN_ULONG r1[8]);
  127. .globl ecp_nistz256_mul_by_2
  128. .type ecp_nistz256_mul_by_2,%function
  129. .align 4
  130. ecp_nistz256_mul_by_2:
  131. stmdb sp!,{r4-r12,lr}
  132. bl __ecp_nistz256_mul_by_2
  133. #if __ARM_ARCH__>=5 || !defined(__thumb__)
  134. ldmia sp!,{r4-r12,pc}
  135. #else
  136. ldmia sp!,{r4-r12,lr}
  137. bx lr @ interoperable with Thumb ISA:-)
  138. #endif
  139. .size ecp_nistz256_mul_by_2,.-ecp_nistz256_mul_by_2
  140. .type __ecp_nistz256_mul_by_2,%function
  141. .align 4
  142. __ecp_nistz256_mul_by_2:
  143. ldr $a0,[$a_ptr,#0]
  144. ldr $a1,[$a_ptr,#4]
  145. ldr $a2,[$a_ptr,#8]
  146. adds $a0,$a0,$a0 @ a[0:7]+=a[0:7], i.e. add with itself
  147. ldr $a3,[$a_ptr,#12]
  148. adcs $a1,$a1,$a1
  149. ldr $a4,[$a_ptr,#16]
  150. adcs $a2,$a2,$a2
  151. ldr $a5,[$a_ptr,#20]
  152. adcs $a3,$a3,$a3
  153. ldr $a6,[$a_ptr,#24]
  154. adcs $a4,$a4,$a4
  155. ldr $a7,[$a_ptr,#28]
  156. adcs $a5,$a5,$a5
  157. adcs $a6,$a6,$a6
  158. mov $ff,#0
  159. adcs $a7,$a7,$a7
  160. adc $ff,$ff,#0
  161. b .Lreduce_by_sub
  162. .size __ecp_nistz256_mul_by_2,.-__ecp_nistz256_mul_by_2
  163. @ void ecp_nistz256_add(BN_ULONG r0[8],const BN_ULONG r1[8],
  164. @ const BN_ULONG r2[8]);
  165. .globl ecp_nistz256_add
  166. .type ecp_nistz256_add,%function
  167. .align 4
  168. ecp_nistz256_add:
  169. stmdb sp!,{r4-r12,lr}
  170. bl __ecp_nistz256_add
  171. #if __ARM_ARCH__>=5 || !defined(__thumb__)
  172. ldmia sp!,{r4-r12,pc}
  173. #else
  174. ldmia sp!,{r4-r12,lr}
  175. bx lr @ interoperable with Thumb ISA:-)
  176. #endif
  177. .size ecp_nistz256_add,.-ecp_nistz256_add
  178. .type __ecp_nistz256_add,%function
  179. .align 4
  180. __ecp_nistz256_add:
  181. str lr,[sp,#-4]! @ push lr
  182. ldr $a0,[$a_ptr,#0]
  183. ldr $a1,[$a_ptr,#4]
  184. ldr $a2,[$a_ptr,#8]
  185. ldr $a3,[$a_ptr,#12]
  186. ldr $a4,[$a_ptr,#16]
  187. ldr $t0,[$b_ptr,#0]
  188. ldr $a5,[$a_ptr,#20]
  189. ldr $t1,[$b_ptr,#4]
  190. ldr $a6,[$a_ptr,#24]
  191. ldr $t2,[$b_ptr,#8]
  192. ldr $a7,[$a_ptr,#28]
  193. ldr $t3,[$b_ptr,#12]
  194. adds $a0,$a0,$t0
  195. ldr $t0,[$b_ptr,#16]
  196. adcs $a1,$a1,$t1
  197. ldr $t1,[$b_ptr,#20]
  198. adcs $a2,$a2,$t2
  199. ldr $t2,[$b_ptr,#24]
  200. adcs $a3,$a3,$t3
  201. ldr $t3,[$b_ptr,#28]
  202. adcs $a4,$a4,$t0
  203. adcs $a5,$a5,$t1
  204. adcs $a6,$a6,$t2
  205. mov $ff,#0
  206. adcs $a7,$a7,$t3
  207. adc $ff,$ff,#0
  208. ldr lr,[sp],#4 @ pop lr
  209. .Lreduce_by_sub:
  210. @ if a+b >= modulus, subtract modulus.
  211. @
  212. @ But since comparison implies subtraction, we subtract
  213. @ modulus and then add it back if subtraction borrowed.
  214. subs $a0,$a0,#-1
  215. sbcs $a1,$a1,#-1
  216. sbcs $a2,$a2,#-1
  217. sbcs $a3,$a3,#0
  218. sbcs $a4,$a4,#0
  219. sbcs $a5,$a5,#0
  220. sbcs $a6,$a6,#1
  221. sbcs $a7,$a7,#-1
  222. sbc $ff,$ff,#0
  223. @ Note that because mod has special form, i.e. consists of
  224. @ 0xffffffff, 1 and 0s, we can conditionally synthesize it by
  225. @ using value of borrow as a whole or extracting single bit.
  226. @ Follow $ff register...
  227. adds $a0,$a0,$ff @ add synthesized modulus
  228. adcs $a1,$a1,$ff
  229. str $a0,[$r_ptr,#0]
  230. adcs $a2,$a2,$ff
  231. str $a1,[$r_ptr,#4]
  232. adcs $a3,$a3,#0
  233. str $a2,[$r_ptr,#8]
  234. adcs $a4,$a4,#0
  235. str $a3,[$r_ptr,#12]
  236. adcs $a5,$a5,#0
  237. str $a4,[$r_ptr,#16]
  238. adcs $a6,$a6,$ff,lsr#31
  239. str $a5,[$r_ptr,#20]
  240. adcs $a7,$a7,$ff
  241. str $a6,[$r_ptr,#24]
  242. str $a7,[$r_ptr,#28]
  243. mov pc,lr
  244. .size __ecp_nistz256_add,.-__ecp_nistz256_add
  245. @ void ecp_nistz256_mul_by_3(BN_ULONG r0[8],const BN_ULONG r1[8]);
  246. .globl ecp_nistz256_mul_by_3
  247. .type ecp_nistz256_mul_by_3,%function
  248. .align 4
  249. ecp_nistz256_mul_by_3:
  250. stmdb sp!,{r4-r12,lr}
  251. bl __ecp_nistz256_mul_by_3
  252. #if __ARM_ARCH__>=5 || !defined(__thumb__)
  253. ldmia sp!,{r4-r12,pc}
  254. #else
  255. ldmia sp!,{r4-r12,lr}
  256. bx lr @ interoperable with Thumb ISA:-)
  257. #endif
  258. .size ecp_nistz256_mul_by_3,.-ecp_nistz256_mul_by_3
  259. .type __ecp_nistz256_mul_by_3,%function
  260. .align 4
  261. __ecp_nistz256_mul_by_3:
  262. str lr,[sp,#-4]! @ push lr
  263. @ As multiplication by 3 is performed as 2*n+n, below are inline
  264. @ copies of __ecp_nistz256_mul_by_2 and __ecp_nistz256_add, see
  265. @ corresponding subroutines for details.
  266. ldr $a0,[$a_ptr,#0]
  267. ldr $a1,[$a_ptr,#4]
  268. ldr $a2,[$a_ptr,#8]
  269. adds $a0,$a0,$a0 @ a[0:7]+=a[0:7]
  270. ldr $a3,[$a_ptr,#12]
  271. adcs $a1,$a1,$a1
  272. ldr $a4,[$a_ptr,#16]
  273. adcs $a2,$a2,$a2
  274. ldr $a5,[$a_ptr,#20]
  275. adcs $a3,$a3,$a3
  276. ldr $a6,[$a_ptr,#24]
  277. adcs $a4,$a4,$a4
  278. ldr $a7,[$a_ptr,#28]
  279. adcs $a5,$a5,$a5
  280. adcs $a6,$a6,$a6
  281. mov $ff,#0
  282. adcs $a7,$a7,$a7
  283. adc $ff,$ff,#0
  284. subs $a0,$a0,#-1 @ .Lreduce_by_sub but without stores
  285. sbcs $a1,$a1,#-1
  286. sbcs $a2,$a2,#-1
  287. sbcs $a3,$a3,#0
  288. sbcs $a4,$a4,#0
  289. sbcs $a5,$a5,#0
  290. sbcs $a6,$a6,#1
  291. sbcs $a7,$a7,#-1
  292. sbc $ff,$ff,#0
  293. adds $a0,$a0,$ff @ add synthesized modulus
  294. adcs $a1,$a1,$ff
  295. adcs $a2,$a2,$ff
  296. adcs $a3,$a3,#0
  297. adcs $a4,$a4,#0
  298. ldr $b_ptr,[$a_ptr,#0]
  299. adcs $a5,$a5,#0
  300. ldr $t1,[$a_ptr,#4]
  301. adcs $a6,$a6,$ff,lsr#31
  302. ldr $t2,[$a_ptr,#8]
  303. adc $a7,$a7,$ff
  304. ldr $t0,[$a_ptr,#12]
  305. adds $a0,$a0,$b_ptr @ 2*a[0:7]+=a[0:7]
  306. ldr $b_ptr,[$a_ptr,#16]
  307. adcs $a1,$a1,$t1
  308. ldr $t1,[$a_ptr,#20]
  309. adcs $a2,$a2,$t2
  310. ldr $t2,[$a_ptr,#24]
  311. adcs $a3,$a3,$t0
  312. ldr $t3,[$a_ptr,#28]
  313. adcs $a4,$a4,$b_ptr
  314. adcs $a5,$a5,$t1
  315. adcs $a6,$a6,$t2
  316. mov $ff,#0
  317. adcs $a7,$a7,$t3
  318. adc $ff,$ff,#0
  319. ldr lr,[sp],#4 @ pop lr
  320. b .Lreduce_by_sub
  321. .size ecp_nistz256_mul_by_3,.-ecp_nistz256_mul_by_3
  322. @ void ecp_nistz256_div_by_2(BN_ULONG r0[8],const BN_ULONG r1[8]);
  323. .globl ecp_nistz256_div_by_2
  324. .type ecp_nistz256_div_by_2,%function
  325. .align 4
  326. ecp_nistz256_div_by_2:
  327. stmdb sp!,{r4-r12,lr}
  328. bl __ecp_nistz256_div_by_2
  329. #if __ARM_ARCH__>=5 || !defined(__thumb__)
  330. ldmia sp!,{r4-r12,pc}
  331. #else
  332. ldmia sp!,{r4-r12,lr}
  333. bx lr @ interoperable with Thumb ISA:-)
  334. #endif
  335. .size ecp_nistz256_div_by_2,.-ecp_nistz256_div_by_2
  336. .type __ecp_nistz256_div_by_2,%function
  337. .align 4
  338. __ecp_nistz256_div_by_2:
  339. @ ret = (a is odd ? a+mod : a) >> 1
  340. ldr $a0,[$a_ptr,#0]
  341. ldr $a1,[$a_ptr,#4]
  342. ldr $a2,[$a_ptr,#8]
  343. mov $ff,$a0,lsl#31 @ place least significant bit to most
  344. @ significant position, now arithmetic
  345. @ right shift by 31 will produce -1 or
  346. @ 0, while logical right shift 1 or 0,
  347. @ this is how modulus is conditionally
  348. @ synthesized in this case...
  349. ldr $a3,[$a_ptr,#12]
  350. adds $a0,$a0,$ff,asr#31
  351. ldr $a4,[$a_ptr,#16]
  352. adcs $a1,$a1,$ff,asr#31
  353. ldr $a5,[$a_ptr,#20]
  354. adcs $a2,$a2,$ff,asr#31
  355. ldr $a6,[$a_ptr,#24]
  356. adcs $a3,$a3,#0
  357. ldr $a7,[$a_ptr,#28]
  358. adcs $a4,$a4,#0
  359. mov $a0,$a0,lsr#1 @ a[0:7]>>=1, we can start early
  360. @ because it doesn't affect flags
  361. adcs $a5,$a5,#0
  362. orr $a0,$a0,$a1,lsl#31
  363. adcs $a6,$a6,$ff,lsr#31
  364. mov $b_ptr,#0
  365. adcs $a7,$a7,$ff,asr#31
  366. mov $a1,$a1,lsr#1
  367. adc $b_ptr,$b_ptr,#0 @ top-most carry bit from addition
  368. orr $a1,$a1,$a2,lsl#31
  369. mov $a2,$a2,lsr#1
  370. str $a0,[$r_ptr,#0]
  371. orr $a2,$a2,$a3,lsl#31
  372. mov $a3,$a3,lsr#1
  373. str $a1,[$r_ptr,#4]
  374. orr $a3,$a3,$a4,lsl#31
  375. mov $a4,$a4,lsr#1
  376. str $a2,[$r_ptr,#8]
  377. orr $a4,$a4,$a5,lsl#31
  378. mov $a5,$a5,lsr#1
  379. str $a3,[$r_ptr,#12]
  380. orr $a5,$a5,$a6,lsl#31
  381. mov $a6,$a6,lsr#1
  382. str $a4,[$r_ptr,#16]
  383. orr $a6,$a6,$a7,lsl#31
  384. mov $a7,$a7,lsr#1
  385. str $a5,[$r_ptr,#20]
  386. orr $a7,$a7,$b_ptr,lsl#31 @ don't forget the top-most carry bit
  387. str $a6,[$r_ptr,#24]
  388. str $a7,[$r_ptr,#28]
  389. mov pc,lr
  390. .size __ecp_nistz256_div_by_2,.-__ecp_nistz256_div_by_2
  391. @ void ecp_nistz256_sub(BN_ULONG r0[8],const BN_ULONG r1[8],
  392. @ const BN_ULONG r2[8]);
  393. .globl ecp_nistz256_sub
  394. .type ecp_nistz256_sub,%function
  395. .align 4
  396. ecp_nistz256_sub:
  397. stmdb sp!,{r4-r12,lr}
  398. bl __ecp_nistz256_sub
  399. #if __ARM_ARCH__>=5 || !defined(__thumb__)
  400. ldmia sp!,{r4-r12,pc}
  401. #else
  402. ldmia sp!,{r4-r12,lr}
  403. bx lr @ interoperable with Thumb ISA:-)
  404. #endif
  405. .size ecp_nistz256_sub,.-ecp_nistz256_sub
  406. .type __ecp_nistz256_sub,%function
  407. .align 4
  408. __ecp_nistz256_sub:
  409. str lr,[sp,#-4]! @ push lr
  410. ldr $a0,[$a_ptr,#0]
  411. ldr $a1,[$a_ptr,#4]
  412. ldr $a2,[$a_ptr,#8]
  413. ldr $a3,[$a_ptr,#12]
  414. ldr $a4,[$a_ptr,#16]
  415. ldr $t0,[$b_ptr,#0]
  416. ldr $a5,[$a_ptr,#20]
  417. ldr $t1,[$b_ptr,#4]
  418. ldr $a6,[$a_ptr,#24]
  419. ldr $t2,[$b_ptr,#8]
  420. ldr $a7,[$a_ptr,#28]
  421. ldr $t3,[$b_ptr,#12]
  422. subs $a0,$a0,$t0
  423. ldr $t0,[$b_ptr,#16]
  424. sbcs $a1,$a1,$t1
  425. ldr $t1,[$b_ptr,#20]
  426. sbcs $a2,$a2,$t2
  427. ldr $t2,[$b_ptr,#24]
  428. sbcs $a3,$a3,$t3
  429. ldr $t3,[$b_ptr,#28]
  430. sbcs $a4,$a4,$t0
  431. sbcs $a5,$a5,$t1
  432. sbcs $a6,$a6,$t2
  433. sbcs $a7,$a7,$t3
  434. sbc $ff,$ff,$ff @ broadcast borrow bit
  435. ldr lr,[sp],#4 @ pop lr
  436. .Lreduce_by_add:
  437. @ if a-b borrows, add modulus.
  438. @
  439. @ Note that because mod has special form, i.e. consists of
  440. @ 0xffffffff, 1 and 0s, we can conditionally synthesize it by
  441. @ broadcasting borrow bit to a register, $ff, and using it as
  442. @ a whole or extracting single bit.
  443. adds $a0,$a0,$ff @ add synthesized modulus
  444. adcs $a1,$a1,$ff
  445. str $a0,[$r_ptr,#0]
  446. adcs $a2,$a2,$ff
  447. str $a1,[$r_ptr,#4]
  448. adcs $a3,$a3,#0
  449. str $a2,[$r_ptr,#8]
  450. adcs $a4,$a4,#0
  451. str $a3,[$r_ptr,#12]
  452. adcs $a5,$a5,#0
  453. str $a4,[$r_ptr,#16]
  454. adcs $a6,$a6,$ff,lsr#31
  455. str $a5,[$r_ptr,#20]
  456. adcs $a7,$a7,$ff
  457. str $a6,[$r_ptr,#24]
  458. str $a7,[$r_ptr,#28]
  459. mov pc,lr
  460. .size __ecp_nistz256_sub,.-__ecp_nistz256_sub
  461. @ void ecp_nistz256_neg(BN_ULONG r0[8],const BN_ULONG r1[8]);
  462. .globl ecp_nistz256_neg
  463. .type ecp_nistz256_neg,%function
  464. .align 4
  465. ecp_nistz256_neg:
  466. stmdb sp!,{r4-r12,lr}
  467. bl __ecp_nistz256_neg
  468. #if __ARM_ARCH__>=5 || !defined(__thumb__)
  469. ldmia sp!,{r4-r12,pc}
  470. #else
  471. ldmia sp!,{r4-r12,lr}
  472. bx lr @ interoperable with Thumb ISA:-)
  473. #endif
  474. .size ecp_nistz256_neg,.-ecp_nistz256_neg
  475. .type __ecp_nistz256_neg,%function
  476. .align 4
  477. __ecp_nistz256_neg:
  478. ldr $a0,[$a_ptr,#0]
  479. eor $ff,$ff,$ff
  480. ldr $a1,[$a_ptr,#4]
  481. ldr $a2,[$a_ptr,#8]
  482. subs $a0,$ff,$a0
  483. ldr $a3,[$a_ptr,#12]
  484. sbcs $a1,$ff,$a1
  485. ldr $a4,[$a_ptr,#16]
  486. sbcs $a2,$ff,$a2
  487. ldr $a5,[$a_ptr,#20]
  488. sbcs $a3,$ff,$a3
  489. ldr $a6,[$a_ptr,#24]
  490. sbcs $a4,$ff,$a4
  491. ldr $a7,[$a_ptr,#28]
  492. sbcs $a5,$ff,$a5
  493. sbcs $a6,$ff,$a6
  494. sbcs $a7,$ff,$a7
  495. sbc $ff,$ff,$ff
  496. b .Lreduce_by_add
  497. .size __ecp_nistz256_neg,.-__ecp_nistz256_neg
  498. ___
  499. {
  500. my @acc=map("r$_",(3..11));
  501. my ($t0,$t1,$bj,$t2,$t3)=map("r$_",(0,1,2,12,14));
  502. $code.=<<___;
  503. @ void ecp_nistz256_sqr_mont(BN_ULONG r0[8],const BN_ULONG r1[8]);
  504. .globl ecp_nistz256_sqr_mont
  505. .type ecp_nistz256_sqr_mont,%function
  506. .align 4
  507. ecp_nistz256_sqr_mont:
  508. mov $b_ptr,$a_ptr
  509. b .Lecp_nistz256_mul_mont
  510. .size ecp_nistz256_sqr_mont,.-ecp_nistz256_sqr_mont
  511. @ void ecp_nistz256_mul_mont(BN_ULONG r0[8],const BN_ULONG r1[8],
  512. @ const BN_ULONG r2[8]);
  513. .globl ecp_nistz256_mul_mont
  514. .type ecp_nistz256_mul_mont,%function
  515. .align 4
  516. ecp_nistz256_mul_mont:
  517. .Lecp_nistz256_mul_mont:
  518. stmdb sp!,{r4-r12,lr}
  519. bl __ecp_nistz256_mul_mont
  520. #if __ARM_ARCH__>=5 || !defined(__thumb__)
  521. ldmia sp!,{r4-r12,pc}
  522. #else
  523. ldmia sp!,{r4-r12,lr}
  524. bx lr @ interoperable with Thumb ISA:-)
  525. #endif
  526. .size ecp_nistz256_mul_mont,.-ecp_nistz256_mul_mont
  527. .type __ecp_nistz256_mul_mont,%function
  528. .align 4
  529. __ecp_nistz256_mul_mont:
  530. stmdb sp!,{r0-r2,lr} @ make a copy of arguments too
  531. ldr $bj,[$b_ptr,#0] @ b[0]
  532. ldmia $a_ptr,{@acc[1]-@acc[8]}
  533. umull @acc[0],$t3,@acc[1],$bj @ r[0]=a[0]*b[0]
  534. stmdb sp!,{$acc[1]-@acc[8]} @ copy a[0-7] to stack, so
  535. @ that it can be addressed
  536. @ without spending register
  537. @ on address
  538. umull @acc[1],$t0,@acc[2],$bj @ r[1]=a[1]*b[0]
  539. umull @acc[2],$t1,@acc[3],$bj
  540. adds @acc[1],@acc[1],$t3 @ accumulate high part of mult
  541. umull @acc[3],$t2,@acc[4],$bj
  542. adcs @acc[2],@acc[2],$t0
  543. umull @acc[4],$t3,@acc[5],$bj
  544. adcs @acc[3],@acc[3],$t1
  545. umull @acc[5],$t0,@acc[6],$bj
  546. adcs @acc[4],@acc[4],$t2
  547. umull @acc[6],$t1,@acc[7],$bj
  548. adcs @acc[5],@acc[5],$t3
  549. umull @acc[7],$t2,@acc[8],$bj
  550. adcs @acc[6],@acc[6],$t0
  551. adcs @acc[7],@acc[7],$t1
  552. eor $t3,$t3,$t3 @ first overflow bit is zero
  553. adc @acc[8],$t2,#0
  554. ___
  555. for(my $i=1;$i<8;$i++) {
  556. my $t4=@acc[0];
  557. # Reduction iteration is normally performed by accumulating
  558. # result of multiplication of modulus by "magic" digit [and
  559. # omitting least significant word, which is guaranteed to
  560. # be 0], but thanks to special form of modulus and "magic"
  561. # digit being equal to least significant word, it can be
  562. # performed with additions and subtractions alone. Indeed:
  563. #
  564. # ffff.0001.0000.0000.0000.ffff.ffff.ffff
  565. # * abcd
  566. # + xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.abcd
  567. #
  568. # Now observing that ff..ff*x = (2^n-1)*x = 2^n*x-x, we
  569. # rewrite above as:
  570. #
  571. # xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.abcd
  572. # + abcd.0000.abcd.0000.0000.abcd.0000.0000.0000
  573. # - abcd.0000.0000.0000.0000.0000.0000.abcd
  574. #
  575. # or marking redundant operations:
  576. #
  577. # xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.----
  578. # + abcd.0000.abcd.0000.0000.abcd.----.----.----
  579. # - abcd.----.----.----.----.----.----.----
  580. $code.=<<___;
  581. @ multiplication-less reduction $i
  582. adds @acc[3],@acc[3],@acc[0] @ r[3]+=r[0]
  583. ldr $bj,[sp,#40] @ restore b_ptr
  584. adcs @acc[4],@acc[4],#0 @ r[4]+=0
  585. adcs @acc[5],@acc[5],#0 @ r[5]+=0
  586. adcs @acc[6],@acc[6],@acc[0] @ r[6]+=r[0]
  587. ldr $t1,[sp,#0] @ load a[0]
  588. adcs @acc[7],@acc[7],#0 @ r[7]+=0
  589. ldr $bj,[$bj,#4*$i] @ load b[i]
  590. adcs @acc[8],@acc[8],@acc[0] @ r[8]+=r[0]
  591. eor $t0,$t0,$t0
  592. adc $t3,$t3,#0 @ overflow bit
  593. subs @acc[7],@acc[7],@acc[0] @ r[7]-=r[0]
  594. ldr $t2,[sp,#4] @ a[1]
  595. sbcs @acc[8],@acc[8],#0 @ r[8]-=0
  596. umlal @acc[1],$t0,$t1,$bj @ "r[0]"+=a[0]*b[i]
  597. eor $t1,$t1,$t1
  598. sbc @acc[0],$t3,#0 @ overflow bit, keep in mind
  599. @ that netto result is
  600. @ addition of a value which
  601. @ makes underflow impossible
  602. ldr $t3,[sp,#8] @ a[2]
  603. umlal @acc[2],$t1,$t2,$bj @ "r[1]"+=a[1]*b[i]
  604. str @acc[0],[sp,#36] @ temporarily offload overflow
  605. eor $t2,$t2,$t2
  606. ldr $t4,[sp,#12] @ a[3], $t4 is alias @acc[0]
  607. umlal @acc[3],$t2,$t3,$bj @ "r[2]"+=a[2]*b[i]
  608. eor $t3,$t3,$t3
  609. adds @acc[2],@acc[2],$t0 @ accumulate high part of mult
  610. ldr $t0,[sp,#16] @ a[4]
  611. umlal @acc[4],$t3,$t4,$bj @ "r[3]"+=a[3]*b[i]
  612. eor $t4,$t4,$t4
  613. adcs @acc[3],@acc[3],$t1
  614. ldr $t1,[sp,#20] @ a[5]
  615. umlal @acc[5],$t4,$t0,$bj @ "r[4]"+=a[4]*b[i]
  616. eor $t0,$t0,$t0
  617. adcs @acc[4],@acc[4],$t2
  618. ldr $t2,[sp,#24] @ a[6]
  619. umlal @acc[6],$t0,$t1,$bj @ "r[5]"+=a[5]*b[i]
  620. eor $t1,$t1,$t1
  621. adcs @acc[5],@acc[5],$t3
  622. ldr $t3,[sp,#28] @ a[7]
  623. umlal @acc[7],$t1,$t2,$bj @ "r[6]"+=a[6]*b[i]
  624. eor $t2,$t2,$t2
  625. adcs @acc[6],@acc[6],$t4
  626. ldr @acc[0],[sp,#36] @ restore overflow bit
  627. umlal @acc[8],$t2,$t3,$bj @ "r[7]"+=a[7]*b[i]
  628. eor $t3,$t3,$t3
  629. adcs @acc[7],@acc[7],$t0
  630. adcs @acc[8],@acc[8],$t1
  631. adcs @acc[0],$acc[0],$t2
  632. adc $t3,$t3,#0 @ new overflow bit
  633. ___
  634. push(@acc,shift(@acc)); # rotate registers, so that
  635. # "r[i]" becomes r[i]
  636. }
  637. $code.=<<___;
  638. @ last multiplication-less reduction
  639. adds @acc[3],@acc[3],@acc[0]
  640. ldr $r_ptr,[sp,#32] @ restore r_ptr
  641. adcs @acc[4],@acc[4],#0
  642. adcs @acc[5],@acc[5],#0
  643. adcs @acc[6],@acc[6],@acc[0]
  644. adcs @acc[7],@acc[7],#0
  645. adcs @acc[8],@acc[8],@acc[0]
  646. adc $t3,$t3,#0
  647. subs @acc[7],@acc[7],@acc[0]
  648. sbcs @acc[8],@acc[8],#0
  649. sbc @acc[0],$t3,#0 @ overflow bit
  650. @ Final step is "if result > mod, subtract mod", but we do it
  651. @ "other way around", namely subtract modulus from result
  652. @ and if it borrowed, add modulus back.
  653. adds @acc[1],@acc[1],#1 @ subs @acc[1],@acc[1],#-1
  654. adcs @acc[2],@acc[2],#0 @ sbcs @acc[2],@acc[2],#-1
  655. adcs @acc[3],@acc[3],#0 @ sbcs @acc[3],@acc[3],#-1
  656. sbcs @acc[4],@acc[4],#0
  657. sbcs @acc[5],@acc[5],#0
  658. sbcs @acc[6],@acc[6],#0
  659. sbcs @acc[7],@acc[7],#1
  660. adcs @acc[8],@acc[8],#0 @ sbcs @acc[8],@acc[8],#-1
  661. ldr lr,[sp,#44] @ restore lr
  662. sbc @acc[0],@acc[0],#0 @ broadcast borrow bit
  663. add sp,sp,#48
  664. @ Note that because mod has special form, i.e. consists of
  665. @ 0xffffffff, 1 and 0s, we can conditionally synthesize it by
  666. @ broadcasting borrow bit to a register, @acc[0], and using it as
  667. @ a whole or extracting single bit.
  668. adds @acc[1],@acc[1],@acc[0] @ add modulus or zero
  669. adcs @acc[2],@acc[2],@acc[0]
  670. str @acc[1],[$r_ptr,#0]
  671. adcs @acc[3],@acc[3],@acc[0]
  672. str @acc[2],[$r_ptr,#4]
  673. adcs @acc[4],@acc[4],#0
  674. str @acc[3],[$r_ptr,#8]
  675. adcs @acc[5],@acc[5],#0
  676. str @acc[4],[$r_ptr,#12]
  677. adcs @acc[6],@acc[6],#0
  678. str @acc[5],[$r_ptr,#16]
  679. adcs @acc[7],@acc[7],@acc[0],lsr#31
  680. str @acc[6],[$r_ptr,#20]
  681. adc @acc[8],@acc[8],@acc[0]
  682. str @acc[7],[$r_ptr,#24]
  683. str @acc[8],[$r_ptr,#28]
  684. mov pc,lr
  685. .size __ecp_nistz256_mul_mont,.-__ecp_nistz256_mul_mont
  686. ___
  687. }
  688. {
  689. my ($out,$inp,$index,$mask)=map("r$_",(0..3));
  690. $code.=<<___;
  691. @ void ecp_nistz256_scatter_w5(void *r0,const P256_POINT *r1,
  692. @ int r2);
  693. .globl ecp_nistz256_scatter_w5
  694. .type ecp_nistz256_scatter_w5,%function
  695. .align 5
  696. ecp_nistz256_scatter_w5:
  697. stmdb sp!,{r4-r11}
  698. add $out,$out,$index,lsl#2
  699. ldmia $inp!,{r4-r11} @ X
  700. str r4,[$out,#64*0-4]
  701. str r5,[$out,#64*1-4]
  702. str r6,[$out,#64*2-4]
  703. str r7,[$out,#64*3-4]
  704. str r8,[$out,#64*4-4]
  705. str r9,[$out,#64*5-4]
  706. str r10,[$out,#64*6-4]
  707. str r11,[$out,#64*7-4]
  708. add $out,$out,#64*8
  709. ldmia $inp!,{r4-r11} @ Y
  710. str r4,[$out,#64*0-4]
  711. str r5,[$out,#64*1-4]
  712. str r6,[$out,#64*2-4]
  713. str r7,[$out,#64*3-4]
  714. str r8,[$out,#64*4-4]
  715. str r9,[$out,#64*5-4]
  716. str r10,[$out,#64*6-4]
  717. str r11,[$out,#64*7-4]
  718. add $out,$out,#64*8
  719. ldmia $inp,{r4-r11} @ Z
  720. str r4,[$out,#64*0-4]
  721. str r5,[$out,#64*1-4]
  722. str r6,[$out,#64*2-4]
  723. str r7,[$out,#64*3-4]
  724. str r8,[$out,#64*4-4]
  725. str r9,[$out,#64*5-4]
  726. str r10,[$out,#64*6-4]
  727. str r11,[$out,#64*7-4]
  728. ldmia sp!,{r4-r11}
  729. #if __ARM_ARCH__>=5 || defined(__thumb__)
  730. bx lr
  731. #else
  732. mov pc,lr
  733. #endif
  734. .size ecp_nistz256_scatter_w5,.-ecp_nistz256_scatter_w5
  735. @ void ecp_nistz256_gather_w5(P256_POINT *r0,const void *r1,
  736. @ int r2);
  737. .globl ecp_nistz256_gather_w5
  738. .type ecp_nistz256_gather_w5,%function
  739. .align 5
  740. ecp_nistz256_gather_w5:
  741. stmdb sp!,{r4-r11}
  742. cmp $index,#0
  743. mov $mask,#0
  744. #ifdef __thumb2__
  745. itt ne
  746. #endif
  747. subne $index,$index,#1
  748. movne $mask,#-1
  749. add $inp,$inp,$index,lsl#2
  750. ldr r4,[$inp,#64*0]
  751. ldr r5,[$inp,#64*1]
  752. ldr r6,[$inp,#64*2]
  753. and r4,r4,$mask
  754. ldr r7,[$inp,#64*3]
  755. and r5,r5,$mask
  756. ldr r8,[$inp,#64*4]
  757. and r6,r6,$mask
  758. ldr r9,[$inp,#64*5]
  759. and r7,r7,$mask
  760. ldr r10,[$inp,#64*6]
  761. and r8,r8,$mask
  762. ldr r11,[$inp,#64*7]
  763. add $inp,$inp,#64*8
  764. and r9,r9,$mask
  765. and r10,r10,$mask
  766. and r11,r11,$mask
  767. stmia $out!,{r4-r11} @ X
  768. ldr r4,[$inp,#64*0]
  769. ldr r5,[$inp,#64*1]
  770. ldr r6,[$inp,#64*2]
  771. and r4,r4,$mask
  772. ldr r7,[$inp,#64*3]
  773. and r5,r5,$mask
  774. ldr r8,[$inp,#64*4]
  775. and r6,r6,$mask
  776. ldr r9,[$inp,#64*5]
  777. and r7,r7,$mask
  778. ldr r10,[$inp,#64*6]
  779. and r8,r8,$mask
  780. ldr r11,[$inp,#64*7]
  781. add $inp,$inp,#64*8
  782. and r9,r9,$mask
  783. and r10,r10,$mask
  784. and r11,r11,$mask
  785. stmia $out!,{r4-r11} @ Y
  786. ldr r4,[$inp,#64*0]
  787. ldr r5,[$inp,#64*1]
  788. ldr r6,[$inp,#64*2]
  789. and r4,r4,$mask
  790. ldr r7,[$inp,#64*3]
  791. and r5,r5,$mask
  792. ldr r8,[$inp,#64*4]
  793. and r6,r6,$mask
  794. ldr r9,[$inp,#64*5]
  795. and r7,r7,$mask
  796. ldr r10,[$inp,#64*6]
  797. and r8,r8,$mask
  798. ldr r11,[$inp,#64*7]
  799. and r9,r9,$mask
  800. and r10,r10,$mask
  801. and r11,r11,$mask
  802. stmia $out,{r4-r11} @ Z
  803. ldmia sp!,{r4-r11}
  804. #if __ARM_ARCH__>=5 || defined(__thumb__)
  805. bx lr
  806. #else
  807. mov pc,lr
  808. #endif
  809. .size ecp_nistz256_gather_w5,.-ecp_nistz256_gather_w5
  810. @ void ecp_nistz256_scatter_w7(void *r0,const P256_POINT_AFFINE *r1,
  811. @ int r2);
  812. .globl ecp_nistz256_scatter_w7
  813. .type ecp_nistz256_scatter_w7,%function
  814. .align 5
  815. ecp_nistz256_scatter_w7:
  816. add $out,$out,$index
  817. mov $index,#64/4
  818. .Loop_scatter_w7:
  819. ldr $mask,[$inp],#4
  820. subs $index,$index,#1
  821. strb $mask,[$out,#64*0]
  822. mov $mask,$mask,lsr#8
  823. strb $mask,[$out,#64*1]
  824. mov $mask,$mask,lsr#8
  825. strb $mask,[$out,#64*2]
  826. mov $mask,$mask,lsr#8
  827. strb $mask,[$out,#64*3]
  828. add $out,$out,#64*4
  829. bne .Loop_scatter_w7
  830. #if __ARM_ARCH__>=5 || defined(__thumb__)
  831. bx lr
  832. #else
  833. mov pc,lr
  834. #endif
  835. .size ecp_nistz256_scatter_w7,.-ecp_nistz256_scatter_w7
  836. @ void ecp_nistz256_gather_w7(P256_POINT_AFFINE *r0,const void *r1,
  837. @ int r2);
  838. .globl ecp_nistz256_gather_w7
  839. .type ecp_nistz256_gather_w7,%function
  840. .align 5
  841. ecp_nistz256_gather_w7:
  842. stmdb sp!,{r4-r7}
  843. cmp $index,#0
  844. mov $mask,#0
  845. #ifdef __thumb2__
  846. itt ne
  847. #endif
  848. subne $index,$index,#1
  849. movne $mask,#-1
  850. add $inp,$inp,$index
  851. mov $index,#64/4
  852. nop
  853. .Loop_gather_w7:
  854. ldrb r4,[$inp,#64*0]
  855. subs $index,$index,#1
  856. ldrb r5,[$inp,#64*1]
  857. ldrb r6,[$inp,#64*2]
  858. ldrb r7,[$inp,#64*3]
  859. add $inp,$inp,#64*4
  860. orr r4,r4,r5,lsl#8
  861. orr r4,r4,r6,lsl#16
  862. orr r4,r4,r7,lsl#24
  863. and r4,r4,$mask
  864. str r4,[$out],#4
  865. bne .Loop_gather_w7
  866. ldmia sp!,{r4-r7}
  867. #if __ARM_ARCH__>=5 || defined(__thumb__)
  868. bx lr
  869. #else
  870. mov pc,lr
  871. #endif
  872. .size ecp_nistz256_gather_w7,.-ecp_nistz256_gather_w7
  873. ___
  874. }
  875. if (0) {
  876. # In comparison to integer-only equivalent of below subroutine:
  877. #
  878. # Cortex-A8 +10%
  879. # Cortex-A9 -10%
  880. # Snapdragon S4 +5%
  881. #
  882. # As not all time is spent in multiplication, overall impact is deemed
  883. # too low to care about.
  884. my ($A0,$A1,$A2,$A3,$Bi,$zero,$temp)=map("d$_",(0..7));
  885. my $mask="q4";
  886. my $mult="q5";
  887. my @AxB=map("q$_",(8..15));
  888. my ($rptr,$aptr,$bptr,$toutptr)=map("r$_",(0..3));
  889. $code.=<<___;
  890. #if __ARM_ARCH__>=7
  891. .fpu neon
  892. .globl ecp_nistz256_mul_mont_neon
  893. .type ecp_nistz256_mul_mont_neon,%function
  894. .align 5
  895. ecp_nistz256_mul_mont_neon:
  896. mov ip,sp
  897. stmdb sp!,{r4-r9}
  898. vstmdb sp!,{q4-q5} @ ABI specification says so
  899. sub $toutptr,sp,#40
  900. vld1.32 {${Bi}[0]},[$bptr,:32]!
  901. veor $zero,$zero,$zero
  902. vld1.32 {$A0-$A3}, [$aptr] @ can't specify :32 :-(
  903. vzip.16 $Bi,$zero
  904. mov sp,$toutptr @ alloca
  905. vmov.i64 $mask,#0xffff
  906. vmull.u32 @AxB[0],$Bi,${A0}[0]
  907. vmull.u32 @AxB[1],$Bi,${A0}[1]
  908. vmull.u32 @AxB[2],$Bi,${A1}[0]
  909. vmull.u32 @AxB[3],$Bi,${A1}[1]
  910. vshr.u64 $temp,@AxB[0]#lo,#16
  911. vmull.u32 @AxB[4],$Bi,${A2}[0]
  912. vadd.u64 @AxB[0]#hi,@AxB[0]#hi,$temp
  913. vmull.u32 @AxB[5],$Bi,${A2}[1]
  914. vshr.u64 $temp,@AxB[0]#hi,#16 @ upper 32 bits of a[0]*b[0]
  915. vmull.u32 @AxB[6],$Bi,${A3}[0]
  916. vand.u64 @AxB[0],@AxB[0],$mask @ lower 32 bits of a[0]*b[0]
  917. vmull.u32 @AxB[7],$Bi,${A3}[1]
  918. ___
  919. for($i=1;$i<8;$i++) {
  920. $code.=<<___;
  921. vld1.32 {${Bi}[0]},[$bptr,:32]!
  922. veor $zero,$zero,$zero
  923. vadd.u64 @AxB[1]#lo,@AxB[1]#lo,$temp @ reduction
  924. vshl.u64 $mult,@AxB[0],#32
  925. vadd.u64 @AxB[3],@AxB[3],@AxB[0]
  926. vsub.u64 $mult,$mult,@AxB[0]
  927. vzip.16 $Bi,$zero
  928. vadd.u64 @AxB[6],@AxB[6],@AxB[0]
  929. vadd.u64 @AxB[7],@AxB[7],$mult
  930. ___
  931. push(@AxB,shift(@AxB));
  932. $code.=<<___;
  933. vmlal.u32 @AxB[0],$Bi,${A0}[0]
  934. vmlal.u32 @AxB[1],$Bi,${A0}[1]
  935. vmlal.u32 @AxB[2],$Bi,${A1}[0]
  936. vmlal.u32 @AxB[3],$Bi,${A1}[1]
  937. vshr.u64 $temp,@AxB[0]#lo,#16
  938. vmlal.u32 @AxB[4],$Bi,${A2}[0]
  939. vadd.u64 @AxB[0]#hi,@AxB[0]#hi,$temp
  940. vmlal.u32 @AxB[5],$Bi,${A2}[1]
  941. vshr.u64 $temp,@AxB[0]#hi,#16 @ upper 33 bits of a[0]*b[i]+t[0]
  942. vmlal.u32 @AxB[6],$Bi,${A3}[0]
  943. vand.u64 @AxB[0],@AxB[0],$mask @ lower 32 bits of a[0]*b[0]
  944. vmull.u32 @AxB[7],$Bi,${A3}[1]
  945. ___
  946. }
  947. $code.=<<___;
  948. vadd.u64 @AxB[1]#lo,@AxB[1]#lo,$temp @ last reduction
  949. vshl.u64 $mult,@AxB[0],#32
  950. vadd.u64 @AxB[3],@AxB[3],@AxB[0]
  951. vsub.u64 $mult,$mult,@AxB[0]
  952. vadd.u64 @AxB[6],@AxB[6],@AxB[0]
  953. vadd.u64 @AxB[7],@AxB[7],$mult
  954. vshr.u64 $temp,@AxB[1]#lo,#16 @ convert
  955. vadd.u64 @AxB[1]#hi,@AxB[1]#hi,$temp
  956. vshr.u64 $temp,@AxB[1]#hi,#16
  957. vzip.16 @AxB[1]#lo,@AxB[1]#hi
  958. ___
  959. foreach (2..7) {
  960. $code.=<<___;
  961. vadd.u64 @AxB[$_]#lo,@AxB[$_]#lo,$temp
  962. vst1.32 {@AxB[$_-1]#lo[0]},[$toutptr,:32]!
  963. vshr.u64 $temp,@AxB[$_]#lo,#16
  964. vadd.u64 @AxB[$_]#hi,@AxB[$_]#hi,$temp
  965. vshr.u64 $temp,@AxB[$_]#hi,#16
  966. vzip.16 @AxB[$_]#lo,@AxB[$_]#hi
  967. ___
  968. }
  969. $code.=<<___;
  970. vst1.32 {@AxB[7]#lo[0]},[$toutptr,:32]!
  971. vst1.32 {$temp},[$toutptr] @ upper 33 bits
  972. ldr r1,[sp,#0]
  973. ldr r2,[sp,#4]
  974. ldr r3,[sp,#8]
  975. subs r1,r1,#-1
  976. ldr r4,[sp,#12]
  977. sbcs r2,r2,#-1
  978. ldr r5,[sp,#16]
  979. sbcs r3,r3,#-1
  980. ldr r6,[sp,#20]
  981. sbcs r4,r4,#0
  982. ldr r7,[sp,#24]
  983. sbcs r5,r5,#0
  984. ldr r8,[sp,#28]
  985. sbcs r6,r6,#0
  986. ldr r9,[sp,#32] @ top-most bit
  987. sbcs r7,r7,#1
  988. sub sp,ip,#40+16
  989. sbcs r8,r8,#-1
  990. sbc r9,r9,#0
  991. vldmia sp!,{q4-q5}
  992. adds r1,r1,r9
  993. adcs r2,r2,r9
  994. str r1,[$rptr,#0]
  995. adcs r3,r3,r9
  996. str r2,[$rptr,#4]
  997. adcs r4,r4,#0
  998. str r3,[$rptr,#8]
  999. adcs r5,r5,#0
  1000. str r4,[$rptr,#12]
  1001. adcs r6,r6,#0
  1002. str r5,[$rptr,#16]
  1003. adcs r7,r7,r9,lsr#31
  1004. str r6,[$rptr,#20]
  1005. adcs r8,r8,r9
  1006. str r7,[$rptr,#24]
  1007. str r8,[$rptr,#28]
  1008. ldmia sp!,{r4-r9}
  1009. bx lr
  1010. .size ecp_nistz256_mul_mont_neon,.-ecp_nistz256_mul_mont_neon
  1011. #endif
  1012. ___
  1013. }
  1014. {{{
  1015. ########################################################################
  1016. # Below $aN assignment matches order in which 256-bit result appears in
  1017. # register bank at return from __ecp_nistz256_mul_mont, so that we can
  1018. # skip over reloading it from memory. This means that below functions
  1019. # use custom calling sequence accepting 256-bit input in registers,
  1020. # output pointer in r0, $r_ptr, and optional pointer in r2, $b_ptr.
  1021. #
  1022. # See their "normal" counterparts for insights on calculations.
  1023. my ($a0,$a1,$a2,$a3,$a4,$a5,$a6,$a7,
  1024. $t0,$t1,$t2,$t3)=map("r$_",(11,3..10,12,14,1));
  1025. my $ff=$b_ptr;
  1026. $code.=<<___;
  1027. .type __ecp_nistz256_sub_from,%function
  1028. .align 5
  1029. __ecp_nistz256_sub_from:
  1030. str lr,[sp,#-4]! @ push lr
  1031. ldr $t0,[$b_ptr,#0]
  1032. ldr $t1,[$b_ptr,#4]
  1033. ldr $t2,[$b_ptr,#8]
  1034. ldr $t3,[$b_ptr,#12]
  1035. subs $a0,$a0,$t0
  1036. ldr $t0,[$b_ptr,#16]
  1037. sbcs $a1,$a1,$t1
  1038. ldr $t1,[$b_ptr,#20]
  1039. sbcs $a2,$a2,$t2
  1040. ldr $t2,[$b_ptr,#24]
  1041. sbcs $a3,$a3,$t3
  1042. ldr $t3,[$b_ptr,#28]
  1043. sbcs $a4,$a4,$t0
  1044. sbcs $a5,$a5,$t1
  1045. sbcs $a6,$a6,$t2
  1046. sbcs $a7,$a7,$t3
  1047. sbc $ff,$ff,$ff @ broadcast borrow bit
  1048. ldr lr,[sp],#4 @ pop lr
  1049. adds $a0,$a0,$ff @ add synthesized modulus
  1050. adcs $a1,$a1,$ff
  1051. str $a0,[$r_ptr,#0]
  1052. adcs $a2,$a2,$ff
  1053. str $a1,[$r_ptr,#4]
  1054. adcs $a3,$a3,#0
  1055. str $a2,[$r_ptr,#8]
  1056. adcs $a4,$a4,#0
  1057. str $a3,[$r_ptr,#12]
  1058. adcs $a5,$a5,#0
  1059. str $a4,[$r_ptr,#16]
  1060. adcs $a6,$a6,$ff,lsr#31
  1061. str $a5,[$r_ptr,#20]
  1062. adcs $a7,$a7,$ff
  1063. str $a6,[$r_ptr,#24]
  1064. str $a7,[$r_ptr,#28]
  1065. mov pc,lr
  1066. .size __ecp_nistz256_sub_from,.-__ecp_nistz256_sub_from
  1067. .type __ecp_nistz256_sub_morf,%function
  1068. .align 5
  1069. __ecp_nistz256_sub_morf:
  1070. str lr,[sp,#-4]! @ push lr
  1071. ldr $t0,[$b_ptr,#0]
  1072. ldr $t1,[$b_ptr,#4]
  1073. ldr $t2,[$b_ptr,#8]
  1074. ldr $t3,[$b_ptr,#12]
  1075. subs $a0,$t0,$a0
  1076. ldr $t0,[$b_ptr,#16]
  1077. sbcs $a1,$t1,$a1
  1078. ldr $t1,[$b_ptr,#20]
  1079. sbcs $a2,$t2,$a2
  1080. ldr $t2,[$b_ptr,#24]
  1081. sbcs $a3,$t3,$a3
  1082. ldr $t3,[$b_ptr,#28]
  1083. sbcs $a4,$t0,$a4
  1084. sbcs $a5,$t1,$a5
  1085. sbcs $a6,$t2,$a6
  1086. sbcs $a7,$t3,$a7
  1087. sbc $ff,$ff,$ff @ broadcast borrow bit
  1088. ldr lr,[sp],#4 @ pop lr
  1089. adds $a0,$a0,$ff @ add synthesized modulus
  1090. adcs $a1,$a1,$ff
  1091. str $a0,[$r_ptr,#0]
  1092. adcs $a2,$a2,$ff
  1093. str $a1,[$r_ptr,#4]
  1094. adcs $a3,$a3,#0
  1095. str $a2,[$r_ptr,#8]
  1096. adcs $a4,$a4,#0
  1097. str $a3,[$r_ptr,#12]
  1098. adcs $a5,$a5,#0
  1099. str $a4,[$r_ptr,#16]
  1100. adcs $a6,$a6,$ff,lsr#31
  1101. str $a5,[$r_ptr,#20]
  1102. adcs $a7,$a7,$ff
  1103. str $a6,[$r_ptr,#24]
  1104. str $a7,[$r_ptr,#28]
  1105. mov pc,lr
  1106. .size __ecp_nistz256_sub_morf,.-__ecp_nistz256_sub_morf
  1107. .type __ecp_nistz256_add_self,%function
  1108. .align 4
  1109. __ecp_nistz256_add_self:
  1110. adds $a0,$a0,$a0 @ a[0:7]+=a[0:7]
  1111. adcs $a1,$a1,$a1
  1112. adcs $a2,$a2,$a2
  1113. adcs $a3,$a3,$a3
  1114. adcs $a4,$a4,$a4
  1115. adcs $a5,$a5,$a5
  1116. adcs $a6,$a6,$a6
  1117. mov $ff,#0
  1118. adcs $a7,$a7,$a7
  1119. adc $ff,$ff,#0
  1120. @ if a+b >= modulus, subtract modulus.
  1121. @
  1122. @ But since comparison implies subtraction, we subtract
  1123. @ modulus and then add it back if subtraction borrowed.
  1124. subs $a0,$a0,#-1
  1125. sbcs $a1,$a1,#-1
  1126. sbcs $a2,$a2,#-1
  1127. sbcs $a3,$a3,#0
  1128. sbcs $a4,$a4,#0
  1129. sbcs $a5,$a5,#0
  1130. sbcs $a6,$a6,#1
  1131. sbcs $a7,$a7,#-1
  1132. sbc $ff,$ff,#0
  1133. @ Note that because mod has special form, i.e. consists of
  1134. @ 0xffffffff, 1 and 0s, we can conditionally synthesize it by
  1135. @ using value of borrow as a whole or extracting single bit.
  1136. @ Follow $ff register...
  1137. adds $a0,$a0,$ff @ add synthesized modulus
  1138. adcs $a1,$a1,$ff
  1139. str $a0,[$r_ptr,#0]
  1140. adcs $a2,$a2,$ff
  1141. str $a1,[$r_ptr,#4]
  1142. adcs $a3,$a3,#0
  1143. str $a2,[$r_ptr,#8]
  1144. adcs $a4,$a4,#0
  1145. str $a3,[$r_ptr,#12]
  1146. adcs $a5,$a5,#0
  1147. str $a4,[$r_ptr,#16]
  1148. adcs $a6,$a6,$ff,lsr#31
  1149. str $a5,[$r_ptr,#20]
  1150. adcs $a7,$a7,$ff
  1151. str $a6,[$r_ptr,#24]
  1152. str $a7,[$r_ptr,#28]
  1153. mov pc,lr
  1154. .size __ecp_nistz256_add_self,.-__ecp_nistz256_add_self
  1155. ___
  1156. ########################################################################
  1157. # following subroutines are "literal" implementation of those found in
  1158. # ecp_nistz256.c
  1159. #
  1160. ########################################################################
  1161. # void ecp_nistz256_point_double(P256_POINT *out,const P256_POINT *inp);
  1162. #
  1163. {
  1164. my ($S,$M,$Zsqr,$in_x,$tmp0)=map(32*$_,(0..4));
  1165. # above map() describes stack layout with 5 temporary
  1166. # 256-bit vectors on top. Then note that we push
  1167. # starting from r0, which means that we have copy of
  1168. # input arguments just below these temporary vectors.
  1169. $code.=<<___;
  1170. .globl ecp_nistz256_point_double
  1171. .type ecp_nistz256_point_double,%function
  1172. .align 5
  1173. ecp_nistz256_point_double:
  1174. stmdb sp!,{r0-r12,lr} @ push from r0, unusual, but intentional
  1175. sub sp,sp,#32*5
  1176. .Lpoint_double_shortcut:
  1177. add r3,sp,#$in_x
  1178. ldmia $a_ptr!,{r4-r11} @ copy in_x
  1179. stmia r3,{r4-r11}
  1180. add $r_ptr,sp,#$S
  1181. bl __ecp_nistz256_mul_by_2 @ p256_mul_by_2(S, in_y);
  1182. add $b_ptr,$a_ptr,#32
  1183. add $a_ptr,$a_ptr,#32
  1184. add $r_ptr,sp,#$Zsqr
  1185. bl __ecp_nistz256_mul_mont @ p256_sqr_mont(Zsqr, in_z);
  1186. add $a_ptr,sp,#$S
  1187. add $b_ptr,sp,#$S
  1188. add $r_ptr,sp,#$S
  1189. bl __ecp_nistz256_mul_mont @ p256_sqr_mont(S, S);
  1190. ldr $b_ptr,[sp,#32*5+4]
  1191. add $a_ptr,$b_ptr,#32
  1192. add $b_ptr,$b_ptr,#64
  1193. add $r_ptr,sp,#$tmp0
  1194. bl __ecp_nistz256_mul_mont @ p256_mul_mont(tmp0, in_z, in_y);
  1195. ldr $r_ptr,[sp,#32*5]
  1196. add $r_ptr,$r_ptr,#64
  1197. bl __ecp_nistz256_add_self @ p256_mul_by_2(res_z, tmp0);
  1198. add $a_ptr,sp,#$in_x
  1199. add $b_ptr,sp,#$Zsqr
  1200. add $r_ptr,sp,#$M
  1201. bl __ecp_nistz256_add @ p256_add(M, in_x, Zsqr);
  1202. add $a_ptr,sp,#$in_x
  1203. add $b_ptr,sp,#$Zsqr
  1204. add $r_ptr,sp,#$Zsqr
  1205. bl __ecp_nistz256_sub @ p256_sub(Zsqr, in_x, Zsqr);
  1206. add $a_ptr,sp,#$S
  1207. add $b_ptr,sp,#$S
  1208. add $r_ptr,sp,#$tmp0
  1209. bl __ecp_nistz256_mul_mont @ p256_sqr_mont(tmp0, S);
  1210. add $a_ptr,sp,#$Zsqr
  1211. add $b_ptr,sp,#$M
  1212. add $r_ptr,sp,#$M
  1213. bl __ecp_nistz256_mul_mont @ p256_mul_mont(M, M, Zsqr);
  1214. ldr $r_ptr,[sp,#32*5]
  1215. add $a_ptr,sp,#$tmp0
  1216. add $r_ptr,$r_ptr,#32
  1217. bl __ecp_nistz256_div_by_2 @ p256_div_by_2(res_y, tmp0);
  1218. add $a_ptr,sp,#$M
  1219. add $r_ptr,sp,#$M
  1220. bl __ecp_nistz256_mul_by_3 @ p256_mul_by_3(M, M);
  1221. add $a_ptr,sp,#$in_x
  1222. add $b_ptr,sp,#$S
  1223. add $r_ptr,sp,#$S
  1224. bl __ecp_nistz256_mul_mont @ p256_mul_mont(S, S, in_x);
  1225. add $r_ptr,sp,#$tmp0
  1226. bl __ecp_nistz256_add_self @ p256_mul_by_2(tmp0, S);
  1227. ldr $r_ptr,[sp,#32*5]
  1228. add $a_ptr,sp,#$M
  1229. add $b_ptr,sp,#$M
  1230. bl __ecp_nistz256_mul_mont @ p256_sqr_mont(res_x, M);
  1231. add $b_ptr,sp,#$tmp0
  1232. bl __ecp_nistz256_sub_from @ p256_sub(res_x, res_x, tmp0);
  1233. add $b_ptr,sp,#$S
  1234. add $r_ptr,sp,#$S
  1235. bl __ecp_nistz256_sub_morf @ p256_sub(S, S, res_x);
  1236. add $a_ptr,sp,#$M
  1237. add $b_ptr,sp,#$S
  1238. bl __ecp_nistz256_mul_mont @ p256_mul_mont(S, S, M);
  1239. ldr $r_ptr,[sp,#32*5]
  1240. add $b_ptr,$r_ptr,#32
  1241. add $r_ptr,$r_ptr,#32
  1242. bl __ecp_nistz256_sub_from @ p256_sub(res_y, S, res_y);
  1243. add sp,sp,#32*5+16 @ +16 means "skip even over saved r0-r3"
  1244. #if __ARM_ARCH__>=5 || !defined(__thumb__)
  1245. ldmia sp!,{r4-r12,pc}
  1246. #else
  1247. ldmia sp!,{r4-r12,lr}
  1248. bx lr @ interoperable with Thumb ISA:-)
  1249. #endif
  1250. .size ecp_nistz256_point_double,.-ecp_nistz256_point_double
  1251. ___
  1252. }
  1253. ########################################################################
  1254. # void ecp_nistz256_point_add(P256_POINT *out,const P256_POINT *in1,
  1255. # const P256_POINT *in2);
  1256. {
  1257. my ($res_x,$res_y,$res_z,
  1258. $in1_x,$in1_y,$in1_z,
  1259. $in2_x,$in2_y,$in2_z,
  1260. $H,$Hsqr,$R,$Rsqr,$Hcub,
  1261. $U1,$U2,$S1,$S2)=map(32*$_,(0..17));
  1262. my ($Z1sqr, $Z2sqr) = ($Hsqr, $Rsqr);
  1263. # above map() describes stack layout with 18 temporary
  1264. # 256-bit vectors on top. Then note that we push
  1265. # starting from r0, which means that we have copy of
  1266. # input arguments just below these temporary vectors.
  1267. # We use three of them for ~in1infty, ~in2infty and
  1268. # result of check for zero.
  1269. $code.=<<___;
  1270. .globl ecp_nistz256_point_add
  1271. .type ecp_nistz256_point_add,%function
  1272. .align 5
  1273. ecp_nistz256_point_add:
  1274. stmdb sp!,{r0-r12,lr} @ push from r0, unusual, but intentional
  1275. sub sp,sp,#32*18+16
  1276. ldmia $b_ptr!,{r4-r11} @ copy in2_x
  1277. add r3,sp,#$in2_x
  1278. stmia r3!,{r4-r11}
  1279. ldmia $b_ptr!,{r4-r11} @ copy in2_y
  1280. stmia r3!,{r4-r11}
  1281. ldmia $b_ptr,{r4-r11} @ copy in2_z
  1282. orr r12,r4,r5
  1283. orr r12,r12,r6
  1284. orr r12,r12,r7
  1285. orr r12,r12,r8
  1286. orr r12,r12,r9
  1287. orr r12,r12,r10
  1288. orr r12,r12,r11
  1289. cmp r12,#0
  1290. #ifdef __thumb2__
  1291. it ne
  1292. #endif
  1293. movne r12,#-1
  1294. stmia r3,{r4-r11}
  1295. str r12,[sp,#32*18+8] @ ~in2infty
  1296. ldmia $a_ptr!,{r4-r11} @ copy in1_x
  1297. add r3,sp,#$in1_x
  1298. stmia r3!,{r4-r11}
  1299. ldmia $a_ptr!,{r4-r11} @ copy in1_y
  1300. stmia r3!,{r4-r11}
  1301. ldmia $a_ptr,{r4-r11} @ copy in1_z
  1302. orr r12,r4,r5
  1303. orr r12,r12,r6
  1304. orr r12,r12,r7
  1305. orr r12,r12,r8
  1306. orr r12,r12,r9
  1307. orr r12,r12,r10
  1308. orr r12,r12,r11
  1309. cmp r12,#0
  1310. #ifdef __thumb2__
  1311. it ne
  1312. #endif
  1313. movne r12,#-1
  1314. stmia r3,{r4-r11}
  1315. str r12,[sp,#32*18+4] @ ~in1infty
  1316. add $a_ptr,sp,#$in2_z
  1317. add $b_ptr,sp,#$in2_z
  1318. add $r_ptr,sp,#$Z2sqr
  1319. bl __ecp_nistz256_mul_mont @ p256_sqr_mont(Z2sqr, in2_z);
  1320. add $a_ptr,sp,#$in1_z
  1321. add $b_ptr,sp,#$in1_z
  1322. add $r_ptr,sp,#$Z1sqr
  1323. bl __ecp_nistz256_mul_mont @ p256_sqr_mont(Z1sqr, in1_z);
  1324. add $a_ptr,sp,#$in2_z
  1325. add $b_ptr,sp,#$Z2sqr
  1326. add $r_ptr,sp,#$S1
  1327. bl __ecp_nistz256_mul_mont @ p256_mul_mont(S1, Z2sqr, in2_z);
  1328. add $a_ptr,sp,#$in1_z
  1329. add $b_ptr,sp,#$Z1sqr
  1330. add $r_ptr,sp,#$S2
  1331. bl __ecp_nistz256_mul_mont @ p256_mul_mont(S2, Z1sqr, in1_z);
  1332. add $a_ptr,sp,#$in1_y
  1333. add $b_ptr,sp,#$S1
  1334. add $r_ptr,sp,#$S1
  1335. bl __ecp_nistz256_mul_mont @ p256_mul_mont(S1, S1, in1_y);
  1336. add $a_ptr,sp,#$in2_y
  1337. add $b_ptr,sp,#$S2
  1338. add $r_ptr,sp,#$S2
  1339. bl __ecp_nistz256_mul_mont @ p256_mul_mont(S2, S2, in2_y);
  1340. add $b_ptr,sp,#$S1
  1341. add $r_ptr,sp,#$R
  1342. bl __ecp_nistz256_sub_from @ p256_sub(R, S2, S1);
  1343. orr $a0,$a0,$a1 @ see if result is zero
  1344. orr $a2,$a2,$a3
  1345. orr $a4,$a4,$a5
  1346. orr $a0,$a0,$a2
  1347. orr $a4,$a4,$a6
  1348. orr $a0,$a0,$a7
  1349. add $a_ptr,sp,#$in1_x
  1350. orr $a0,$a0,$a4
  1351. add $b_ptr,sp,#$Z2sqr
  1352. str $a0,[sp,#32*18+12]
  1353. add $r_ptr,sp,#$U1
  1354. bl __ecp_nistz256_mul_mont @ p256_mul_mont(U1, in1_x, Z2sqr);
  1355. add $a_ptr,sp,#$in2_x
  1356. add $b_ptr,sp,#$Z1sqr
  1357. add $r_ptr,sp,#$U2
  1358. bl __ecp_nistz256_mul_mont @ p256_mul_mont(U2, in2_x, Z1sqr);
  1359. add $b_ptr,sp,#$U1
  1360. add $r_ptr,sp,#$H
  1361. bl __ecp_nistz256_sub_from @ p256_sub(H, U2, U1);
  1362. orr $a0,$a0,$a1 @ see if result is zero
  1363. orr $a2,$a2,$a3
  1364. orr $a4,$a4,$a5
  1365. orr $a0,$a0,$a2
  1366. orr $a4,$a4,$a6
  1367. orr $a0,$a0,$a7
  1368. orr $a0,$a0,$a4 @ ~is_equal(U1,U2)
  1369. ldr $t0,[sp,#32*18+4] @ ~in1infty
  1370. ldr $t1,[sp,#32*18+8] @ ~in2infty
  1371. ldr $t2,[sp,#32*18+12] @ ~is_equal(S1,S2)
  1372. mvn $t0,$t0 @ -1/0 -> 0/-1
  1373. mvn $t1,$t1 @ -1/0 -> 0/-1
  1374. orr $a0,$a0,$t0
  1375. orr $a0,$a0,$t1
  1376. orrs $a0,$a0,$t2 @ set flags
  1377. @ if(~is_equal(U1,U2) | in1infty | in2infty | ~is_equal(S1,S2))
  1378. bne .Ladd_proceed
  1379. .Ladd_double:
  1380. ldr $a_ptr,[sp,#32*18+20]
  1381. add sp,sp,#32*(18-5)+16 @ difference in frame sizes
  1382. b .Lpoint_double_shortcut
  1383. .align 4
  1384. .Ladd_proceed:
  1385. add $a_ptr,sp,#$R
  1386. add $b_ptr,sp,#$R
  1387. add $r_ptr,sp,#$Rsqr
  1388. bl __ecp_nistz256_mul_mont @ p256_sqr_mont(Rsqr, R);
  1389. add $a_ptr,sp,#$H
  1390. add $b_ptr,sp,#$in1_z
  1391. add $r_ptr,sp,#$res_z
  1392. bl __ecp_nistz256_mul_mont @ p256_mul_mont(res_z, H, in1_z);
  1393. add $a_ptr,sp,#$H
  1394. add $b_ptr,sp,#$H
  1395. add $r_ptr,sp,#$Hsqr
  1396. bl __ecp_nistz256_mul_mont @ p256_sqr_mont(Hsqr, H);
  1397. add $a_ptr,sp,#$in2_z
  1398. add $b_ptr,sp,#$res_z
  1399. add $r_ptr,sp,#$res_z
  1400. bl __ecp_nistz256_mul_mont @ p256_mul_mont(res_z, res_z, in2_z);
  1401. add $a_ptr,sp,#$H
  1402. add $b_ptr,sp,#$Hsqr
  1403. add $r_ptr,sp,#$Hcub
  1404. bl __ecp_nistz256_mul_mont @ p256_mul_mont(Hcub, Hsqr, H);
  1405. add $a_ptr,sp,#$Hsqr
  1406. add $b_ptr,sp,#$U1
  1407. add $r_ptr,sp,#$U2
  1408. bl __ecp_nistz256_mul_mont @ p256_mul_mont(U2, U1, Hsqr);
  1409. add $r_ptr,sp,#$Hsqr
  1410. bl __ecp_nistz256_add_self @ p256_mul_by_2(Hsqr, U2);
  1411. add $b_ptr,sp,#$Rsqr
  1412. add $r_ptr,sp,#$res_x
  1413. bl __ecp_nistz256_sub_morf @ p256_sub(res_x, Rsqr, Hsqr);
  1414. add $b_ptr,sp,#$Hcub
  1415. bl __ecp_nistz256_sub_from @ p256_sub(res_x, res_x, Hcub);
  1416. add $b_ptr,sp,#$U2
  1417. add $r_ptr,sp,#$res_y
  1418. bl __ecp_nistz256_sub_morf @ p256_sub(res_y, U2, res_x);
  1419. add $a_ptr,sp,#$Hcub
  1420. add $b_ptr,sp,#$S1
  1421. add $r_ptr,sp,#$S2
  1422. bl __ecp_nistz256_mul_mont @ p256_mul_mont(S2, S1, Hcub);
  1423. add $a_ptr,sp,#$R
  1424. add $b_ptr,sp,#$res_y
  1425. add $r_ptr,sp,#$res_y
  1426. bl __ecp_nistz256_mul_mont @ p256_mul_mont(res_y, res_y, R);
  1427. add $b_ptr,sp,#$S2
  1428. bl __ecp_nistz256_sub_from @ p256_sub(res_y, res_y, S2);
  1429. ldr r11,[sp,#32*18+4] @ ~in1infty
  1430. ldr r12,[sp,#32*18+8] @ ~in2infty
  1431. add r1,sp,#$res_x
  1432. add r2,sp,#$in2_x
  1433. and r10,r11,r12 @ ~in1infty & ~in2infty
  1434. mvn r11,r11
  1435. add r3,sp,#$in1_x
  1436. and r11,r11,r12 @ in1infty & ~in2infty
  1437. mvn r12,r12 @ in2infty
  1438. ldr $r_ptr,[sp,#32*18+16]
  1439. ___
  1440. for($i=0;$i<96;$i+=8) { # conditional moves
  1441. $code.=<<___;
  1442. ldmia r1!,{r4-r5} @ res_x
  1443. ldmia r2!,{r6-r7} @ in2_x
  1444. ldmia r3!,{r8-r9} @ in1_x
  1445. and r4,r4,r10 @ ~in1infty & ~in2infty
  1446. and r5,r5,r10
  1447. and r6,r6,r11 @ in1infty & ~in2infty
  1448. and r7,r7,r11
  1449. and r8,r8,r12 @ in2infty
  1450. and r9,r9,r12
  1451. orr r4,r4,r6
  1452. orr r5,r5,r7
  1453. orr r4,r4,r8
  1454. orr r5,r5,r9
  1455. stmia $r_ptr!,{r4-r5}
  1456. ___
  1457. }
  1458. $code.=<<___;
  1459. .Ladd_done:
  1460. add sp,sp,#32*18+16+16 @ +16 means "skip even over saved r0-r3"
  1461. #if __ARM_ARCH__>=5 || !defined(__thumb__)
  1462. ldmia sp!,{r4-r12,pc}
  1463. #else
  1464. ldmia sp!,{r4-r12,lr}
  1465. bx lr @ interoperable with Thumb ISA:-)
  1466. #endif
  1467. .size ecp_nistz256_point_add,.-ecp_nistz256_point_add
  1468. ___
  1469. }
  1470. ########################################################################
  1471. # void ecp_nistz256_point_add_affine(P256_POINT *out,const P256_POINT *in1,
  1472. # const P256_POINT_AFFINE *in2);
  1473. {
  1474. my ($res_x,$res_y,$res_z,
  1475. $in1_x,$in1_y,$in1_z,
  1476. $in2_x,$in2_y,
  1477. $U2,$S2,$H,$R,$Hsqr,$Hcub,$Rsqr)=map(32*$_,(0..14));
  1478. my $Z1sqr = $S2;
  1479. # above map() describes stack layout with 18 temporary
  1480. # 256-bit vectors on top. Then note that we push
  1481. # starting from r0, which means that we have copy of
  1482. # input arguments just below these temporary vectors.
  1483. # We use two of them for ~in1infty, ~in2infty.
  1484. my @ONE_mont=(1,0,0,-1,-1,-1,-2,0);
  1485. $code.=<<___;
  1486. .globl ecp_nistz256_point_add_affine
  1487. .type ecp_nistz256_point_add_affine,%function
  1488. .align 5
  1489. ecp_nistz256_point_add_affine:
  1490. stmdb sp!,{r0-r12,lr} @ push from r0, unusual, but intentional
  1491. sub sp,sp,#32*15
  1492. ldmia $a_ptr!,{r4-r11} @ copy in1_x
  1493. add r3,sp,#$in1_x
  1494. stmia r3!,{r4-r11}
  1495. ldmia $a_ptr!,{r4-r11} @ copy in1_y
  1496. stmia r3!,{r4-r11}
  1497. ldmia $a_ptr,{r4-r11} @ copy in1_z
  1498. orr r12,r4,r5
  1499. orr r12,r12,r6
  1500. orr r12,r12,r7
  1501. orr r12,r12,r8
  1502. orr r12,r12,r9
  1503. orr r12,r12,r10
  1504. orr r12,r12,r11
  1505. cmp r12,#0
  1506. #ifdef __thumb2__
  1507. it ne
  1508. #endif
  1509. movne r12,#-1
  1510. stmia r3,{r4-r11}
  1511. str r12,[sp,#32*15+4] @ ~in1infty
  1512. ldmia $b_ptr!,{r4-r11} @ copy in2_x
  1513. add r3,sp,#$in2_x
  1514. orr r12,r4,r5
  1515. orr r12,r12,r6
  1516. orr r12,r12,r7
  1517. orr r12,r12,r8
  1518. orr r12,r12,r9
  1519. orr r12,r12,r10
  1520. orr r12,r12,r11
  1521. stmia r3!,{r4-r11}
  1522. ldmia $b_ptr!,{r4-r11} @ copy in2_y
  1523. orr r12,r12,r4
  1524. orr r12,r12,r5
  1525. orr r12,r12,r6
  1526. orr r12,r12,r7
  1527. orr r12,r12,r8
  1528. orr r12,r12,r9
  1529. orr r12,r12,r10
  1530. orr r12,r12,r11
  1531. stmia r3!,{r4-r11}
  1532. cmp r12,#0
  1533. #ifdef __thumb2__
  1534. it ne
  1535. #endif
  1536. movne r12,#-1
  1537. str r12,[sp,#32*15+8] @ ~in2infty
  1538. add $a_ptr,sp,#$in1_z
  1539. add $b_ptr,sp,#$in1_z
  1540. add $r_ptr,sp,#$Z1sqr
  1541. bl __ecp_nistz256_mul_mont @ p256_sqr_mont(Z1sqr, in1_z);
  1542. add $a_ptr,sp,#$Z1sqr
  1543. add $b_ptr,sp,#$in2_x
  1544. add $r_ptr,sp,#$U2
  1545. bl __ecp_nistz256_mul_mont @ p256_mul_mont(U2, Z1sqr, in2_x);
  1546. add $b_ptr,sp,#$in1_x
  1547. add $r_ptr,sp,#$H
  1548. bl __ecp_nistz256_sub_from @ p256_sub(H, U2, in1_x);
  1549. add $a_ptr,sp,#$Z1sqr
  1550. add $b_ptr,sp,#$in1_z
  1551. add $r_ptr,sp,#$S2
  1552. bl __ecp_nistz256_mul_mont @ p256_mul_mont(S2, Z1sqr, in1_z);
  1553. add $a_ptr,sp,#$H
  1554. add $b_ptr,sp,#$in1_z
  1555. add $r_ptr,sp,#$res_z
  1556. bl __ecp_nistz256_mul_mont @ p256_mul_mont(res_z, H, in1_z);
  1557. add $a_ptr,sp,#$in2_y
  1558. add $b_ptr,sp,#$S2
  1559. add $r_ptr,sp,#$S2
  1560. bl __ecp_nistz256_mul_mont @ p256_mul_mont(S2, S2, in2_y);
  1561. add $b_ptr,sp,#$in1_y
  1562. add $r_ptr,sp,#$R
  1563. bl __ecp_nistz256_sub_from @ p256_sub(R, S2, in1_y);
  1564. add $a_ptr,sp,#$H
  1565. add $b_ptr,sp,#$H
  1566. add $r_ptr,sp,#$Hsqr
  1567. bl __ecp_nistz256_mul_mont @ p256_sqr_mont(Hsqr, H);
  1568. add $a_ptr,sp,#$R
  1569. add $b_ptr,sp,#$R
  1570. add $r_ptr,sp,#$Rsqr
  1571. bl __ecp_nistz256_mul_mont @ p256_sqr_mont(Rsqr, R);
  1572. add $a_ptr,sp,#$H
  1573. add $b_ptr,sp,#$Hsqr
  1574. add $r_ptr,sp,#$Hcub
  1575. bl __ecp_nistz256_mul_mont @ p256_mul_mont(Hcub, Hsqr, H);
  1576. add $a_ptr,sp,#$Hsqr
  1577. add $b_ptr,sp,#$in1_x
  1578. add $r_ptr,sp,#$U2
  1579. bl __ecp_nistz256_mul_mont @ p256_mul_mont(U2, in1_x, Hsqr);
  1580. add $r_ptr,sp,#$Hsqr
  1581. bl __ecp_nistz256_add_self @ p256_mul_by_2(Hsqr, U2);
  1582. add $b_ptr,sp,#$Rsqr
  1583. add $r_ptr,sp,#$res_x
  1584. bl __ecp_nistz256_sub_morf @ p256_sub(res_x, Rsqr, Hsqr);
  1585. add $b_ptr,sp,#$Hcub
  1586. bl __ecp_nistz256_sub_from @ p256_sub(res_x, res_x, Hcub);
  1587. add $b_ptr,sp,#$U2
  1588. add $r_ptr,sp,#$res_y
  1589. bl __ecp_nistz256_sub_morf @ p256_sub(res_y, U2, res_x);
  1590. add $a_ptr,sp,#$Hcub
  1591. add $b_ptr,sp,#$in1_y
  1592. add $r_ptr,sp,#$S2
  1593. bl __ecp_nistz256_mul_mont @ p256_mul_mont(S2, in1_y, Hcub);
  1594. add $a_ptr,sp,#$R
  1595. add $b_ptr,sp,#$res_y
  1596. add $r_ptr,sp,#$res_y
  1597. bl __ecp_nistz256_mul_mont @ p256_mul_mont(res_y, res_y, R);
  1598. add $b_ptr,sp,#$S2
  1599. bl __ecp_nistz256_sub_from @ p256_sub(res_y, res_y, S2);
  1600. ldr r11,[sp,#32*15+4] @ ~in1infty
  1601. ldr r12,[sp,#32*15+8] @ ~in2infty
  1602. add r1,sp,#$res_x
  1603. add r2,sp,#$in2_x
  1604. and r10,r11,r12 @ ~in1infty & ~in2infty
  1605. mvn r11,r11
  1606. add r3,sp,#$in1_x
  1607. and r11,r11,r12 @ in1infty & ~in2infty
  1608. mvn r12,r12 @ in2infty
  1609. ldr $r_ptr,[sp,#32*15]
  1610. ___
  1611. for($i=0;$i<64;$i+=8) { # conditional moves
  1612. $code.=<<___;
  1613. ldmia r1!,{r4-r5} @ res_x
  1614. ldmia r2!,{r6-r7} @ in2_x
  1615. ldmia r3!,{r8-r9} @ in1_x
  1616. and r4,r4,r10 @ ~in1infty & ~in2infty
  1617. and r5,r5,r10
  1618. and r6,r6,r11 @ in1infty & ~in2infty
  1619. and r7,r7,r11
  1620. and r8,r8,r12 @ in2infty
  1621. and r9,r9,r12
  1622. orr r4,r4,r6
  1623. orr r5,r5,r7
  1624. orr r4,r4,r8
  1625. orr r5,r5,r9
  1626. stmia $r_ptr!,{r4-r5}
  1627. ___
  1628. }
  1629. for(;$i<96;$i+=8) {
  1630. my $j=($i-64)/4;
  1631. $code.=<<___;
  1632. ldmia r1!,{r4-r5} @ res_z
  1633. ldmia r3!,{r8-r9} @ in1_z
  1634. and r4,r4,r10
  1635. and r5,r5,r10
  1636. and r6,r11,#@ONE_mont[$j]
  1637. and r7,r11,#@ONE_mont[$j+1]
  1638. and r8,r8,r12
  1639. and r9,r9,r12
  1640. orr r4,r4,r6
  1641. orr r5,r5,r7
  1642. orr r4,r4,r8
  1643. orr r5,r5,r9
  1644. stmia $r_ptr!,{r4-r5}
  1645. ___
  1646. }
  1647. $code.=<<___;
  1648. add sp,sp,#32*15+16 @ +16 means "skip even over saved r0-r3"
  1649. #if __ARM_ARCH__>=5 || !defined(__thumb__)
  1650. ldmia sp!,{r4-r12,pc}
  1651. #else
  1652. ldmia sp!,{r4-r12,lr}
  1653. bx lr @ interoperable with Thumb ISA:-)
  1654. #endif
  1655. .size ecp_nistz256_point_add_affine,.-ecp_nistz256_point_add_affine
  1656. ___
  1657. } }}}
  1658. foreach (split("\n",$code)) {
  1659. s/\`([^\`]*)\`/eval $1/geo;
  1660. s/\bq([0-9]+)#(lo|hi)/sprintf "d%d",2*$1+($2 eq "hi")/geo;
  1661. print $_,"\n";
  1662. }
  1663. close STDOUT or die "error closing STDOUT: $!"; # enforce flush