vis3-mont.pl 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. #! /usr/bin/env perl
  2. # Copyright 2012-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (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. # October 2012.
  15. #
  16. # SPARCv9 VIS3 Montgomery multiplication procedure suitable for T3 and
  17. # onward. There are three new instructions used here: umulxhi,
  18. # addxc[cc] and initializing store. On T3 RSA private key operations
  19. # are 1.54/1.87/2.11/2.26 times faster for 512/1024/2048/4096-bit key
  20. # lengths. This is without dedicated squaring procedure. On T4
  21. # corresponding coefficients are 1.47/2.10/2.80/2.90x, which is mostly
  22. # for reference purposes, because T4 has dedicated Montgomery
  23. # multiplication and squaring *instructions* that deliver even more.
  24. $output = pop and open STDOUT,">$output";
  25. $frame = "STACK_FRAME";
  26. $bias = "STACK_BIAS";
  27. $code.=<<___;
  28. #include "sparc_arch.h"
  29. #ifdef __arch64__
  30. .register %g2,#scratch
  31. .register %g3,#scratch
  32. #endif
  33. .section ".text",#alloc,#execinstr
  34. ___
  35. ($n0,$m0,$m1,$lo0,$hi0, $lo1,$hi1,$aj,$alo,$nj,$nlo,$tj)=
  36. (map("%g$_",(1..5)),map("%o$_",(0..5,7)));
  37. # int bn_mul_mont(
  38. $rp="%o0"; # BN_ULONG *rp,
  39. $ap="%o1"; # const BN_ULONG *ap,
  40. $bp="%o2"; # const BN_ULONG *bp,
  41. $np="%o3"; # const BN_ULONG *np,
  42. $n0p="%o4"; # const BN_ULONG *n0,
  43. $num="%o5"; # int num); # caller ensures that num is even
  44. # and >=6
  45. $code.=<<___;
  46. .globl bn_mul_mont_vis3
  47. .align 32
  48. bn_mul_mont_vis3:
  49. add %sp, $bias, %g4 ! real top of stack
  50. sll $num, 2, $num ! size in bytes
  51. add $num, 63, %g5
  52. andn %g5, 63, %g5 ! buffer size rounded up to 64 bytes
  53. add %g5, %g5, %g1
  54. add %g5, %g1, %g1 ! 3*buffer size
  55. sub %g4, %g1, %g1
  56. andn %g1, 63, %g1 ! align at 64 byte
  57. sub %g1, $frame, %g1 ! new top of stack
  58. sub %g1, %g4, %g1
  59. save %sp, %g1, %sp
  60. ___
  61. # +-------------------------------+<----- %sp
  62. # . .
  63. # +-------------------------------+<----- aligned at 64 bytes
  64. # | __int64 tmp[0] |
  65. # +-------------------------------+
  66. # . .
  67. # . .
  68. # +-------------------------------+<----- aligned at 64 bytes
  69. # | __int64 ap[1..0] | converted ap[]
  70. # +-------------------------------+
  71. # | __int64 np[1..0] | converted np[]
  72. # +-------------------------------+
  73. # | __int64 ap[3..2] |
  74. # . .
  75. # . .
  76. # +-------------------------------+
  77. ($rp,$ap,$bp,$np,$n0p,$num)=map("%i$_",(0..5));
  78. ($t0,$t1,$t2,$t3,$cnt,$tp,$bufsz,$anp)=map("%l$_",(0..7));
  79. ($ovf,$i)=($t0,$t1);
  80. $code.=<<___;
  81. ld [$n0p+0], $t0 ! pull n0[0..1] value
  82. add %sp, $bias+$frame, $tp
  83. ld [$n0p+4], $t1
  84. add $tp, %g5, $anp
  85. ld [$bp+0], $t2 ! m0=bp[0]
  86. sllx $t1, 32, $n0
  87. ld [$bp+4], $t3
  88. or $t0, $n0, $n0
  89. add $bp, 8, $bp
  90. ld [$ap+0], $t0 ! ap[0]
  91. sllx $t3, 32, $m0
  92. ld [$ap+4], $t1
  93. or $t2, $m0, $m0
  94. ld [$ap+8], $t2 ! ap[1]
  95. sllx $t1, 32, $aj
  96. ld [$ap+12], $t3
  97. or $t0, $aj, $aj
  98. add $ap, 16, $ap
  99. stx $aj, [$anp] ! converted ap[0]
  100. mulx $aj, $m0, $lo0 ! ap[0]*bp[0]
  101. umulxhi $aj, $m0, $hi0
  102. ld [$np+0], $t0 ! np[0]
  103. sllx $t3, 32, $aj
  104. ld [$np+4], $t1
  105. or $t2, $aj, $aj
  106. ld [$np+8], $t2 ! np[1]
  107. sllx $t1, 32, $nj
  108. ld [$np+12], $t3
  109. or $t0, $nj, $nj
  110. add $np, 16, $np
  111. stx $nj, [$anp+8] ! converted np[0]
  112. mulx $lo0, $n0, $m1 ! "tp[0]"*n0
  113. stx $aj, [$anp+16] ! converted ap[1]
  114. mulx $aj, $m0, $alo ! ap[1]*bp[0]
  115. umulxhi $aj, $m0, $aj ! ahi=aj
  116. mulx $nj, $m1, $lo1 ! np[0]*m1
  117. umulxhi $nj, $m1, $hi1
  118. sllx $t3, 32, $nj
  119. or $t2, $nj, $nj
  120. stx $nj, [$anp+24] ! converted np[1]
  121. add $anp, 32, $anp
  122. addcc $lo0, $lo1, $lo1
  123. addxc %g0, $hi1, $hi1
  124. mulx $nj, $m1, $nlo ! np[1]*m1
  125. umulxhi $nj, $m1, $nj ! nhi=nj
  126. ba .L1st
  127. sub $num, 24, $cnt ! cnt=num-3
  128. .align 16
  129. .L1st:
  130. ld [$ap+0], $t0 ! ap[j]
  131. addcc $alo, $hi0, $lo0
  132. ld [$ap+4], $t1
  133. addxc $aj, %g0, $hi0
  134. sllx $t1, 32, $aj
  135. add $ap, 8, $ap
  136. or $t0, $aj, $aj
  137. stx $aj, [$anp] ! converted ap[j]
  138. ld [$np+0], $t2 ! np[j]
  139. addcc $nlo, $hi1, $lo1
  140. ld [$np+4], $t3
  141. addxc $nj, %g0, $hi1 ! nhi=nj
  142. sllx $t3, 32, $nj
  143. add $np, 8, $np
  144. mulx $aj, $m0, $alo ! ap[j]*bp[0]
  145. or $t2, $nj, $nj
  146. umulxhi $aj, $m0, $aj ! ahi=aj
  147. stx $nj, [$anp+8] ! converted np[j]
  148. add $anp, 16, $anp ! anp++
  149. mulx $nj, $m1, $nlo ! np[j]*m1
  150. addcc $lo0, $lo1, $lo1 ! np[j]*m1+ap[j]*bp[0]
  151. umulxhi $nj, $m1, $nj ! nhi=nj
  152. addxc %g0, $hi1, $hi1
  153. stx $lo1, [$tp] ! tp[j-1]
  154. add $tp, 8, $tp ! tp++
  155. brnz,pt $cnt, .L1st
  156. sub $cnt, 8, $cnt ! j--
  157. !.L1st
  158. addcc $alo, $hi0, $lo0
  159. addxc $aj, %g0, $hi0 ! ahi=aj
  160. addcc $nlo, $hi1, $lo1
  161. addxc $nj, %g0, $hi1
  162. addcc $lo0, $lo1, $lo1 ! np[j]*m1+ap[j]*bp[0]
  163. addxc %g0, $hi1, $hi1
  164. stx $lo1, [$tp] ! tp[j-1]
  165. add $tp, 8, $tp
  166. addcc $hi0, $hi1, $hi1
  167. addxc %g0, %g0, $ovf ! upmost overflow bit
  168. stx $hi1, [$tp]
  169. add $tp, 8, $tp
  170. ba .Louter
  171. sub $num, 16, $i ! i=num-2
  172. .align 16
  173. .Louter:
  174. ld [$bp+0], $t2 ! m0=bp[i]
  175. ld [$bp+4], $t3
  176. sub $anp, $num, $anp ! rewind
  177. sub $tp, $num, $tp
  178. sub $anp, $num, $anp
  179. add $bp, 8, $bp
  180. sllx $t3, 32, $m0
  181. ldx [$anp+0], $aj ! ap[0]
  182. or $t2, $m0, $m0
  183. ldx [$anp+8], $nj ! np[0]
  184. mulx $aj, $m0, $lo0 ! ap[0]*bp[i]
  185. ldx [$tp], $tj ! tp[0]
  186. umulxhi $aj, $m0, $hi0
  187. ldx [$anp+16], $aj ! ap[1]
  188. addcc $lo0, $tj, $lo0 ! ap[0]*bp[i]+tp[0]
  189. mulx $aj, $m0, $alo ! ap[1]*bp[i]
  190. addxc %g0, $hi0, $hi0
  191. mulx $lo0, $n0, $m1 ! tp[0]*n0
  192. umulxhi $aj, $m0, $aj ! ahi=aj
  193. mulx $nj, $m1, $lo1 ! np[0]*m1
  194. umulxhi $nj, $m1, $hi1
  195. ldx [$anp+24], $nj ! np[1]
  196. add $anp, 32, $anp
  197. addcc $lo1, $lo0, $lo1
  198. mulx $nj, $m1, $nlo ! np[1]*m1
  199. addxc %g0, $hi1, $hi1
  200. umulxhi $nj, $m1, $nj ! nhi=nj
  201. ba .Linner
  202. sub $num, 24, $cnt ! cnt=num-3
  203. .align 16
  204. .Linner:
  205. addcc $alo, $hi0, $lo0
  206. ldx [$tp+8], $tj ! tp[j]
  207. addxc $aj, %g0, $hi0 ! ahi=aj
  208. ldx [$anp+0], $aj ! ap[j]
  209. addcc $nlo, $hi1, $lo1
  210. mulx $aj, $m0, $alo ! ap[j]*bp[i]
  211. addxc $nj, %g0, $hi1 ! nhi=nj
  212. ldx [$anp+8], $nj ! np[j]
  213. add $anp, 16, $anp
  214. umulxhi $aj, $m0, $aj ! ahi=aj
  215. addcc $lo0, $tj, $lo0 ! ap[j]*bp[i]+tp[j]
  216. mulx $nj, $m1, $nlo ! np[j]*m1
  217. addxc %g0, $hi0, $hi0
  218. umulxhi $nj, $m1, $nj ! nhi=nj
  219. addcc $lo1, $lo0, $lo1 ! np[j]*m1+ap[j]*bp[i]+tp[j]
  220. addxc %g0, $hi1, $hi1
  221. stx $lo1, [$tp] ! tp[j-1]
  222. add $tp, 8, $tp
  223. brnz,pt $cnt, .Linner
  224. sub $cnt, 8, $cnt
  225. !.Linner
  226. ldx [$tp+8], $tj ! tp[j]
  227. addcc $alo, $hi0, $lo0
  228. addxc $aj, %g0, $hi0 ! ahi=aj
  229. addcc $lo0, $tj, $lo0 ! ap[j]*bp[i]+tp[j]
  230. addxc %g0, $hi0, $hi0
  231. addcc $nlo, $hi1, $lo1
  232. addxc $nj, %g0, $hi1 ! nhi=nj
  233. addcc $lo1, $lo0, $lo1 ! np[j]*m1+ap[j]*bp[i]+tp[j]
  234. addxc %g0, $hi1, $hi1
  235. stx $lo1, [$tp] ! tp[j-1]
  236. subcc %g0, $ovf, %g0 ! move upmost overflow to CCR.xcc
  237. addxccc $hi1, $hi0, $hi1
  238. addxc %g0, %g0, $ovf
  239. stx $hi1, [$tp+8]
  240. add $tp, 16, $tp
  241. brnz,pt $i, .Louter
  242. sub $i, 8, $i
  243. sub $anp, $num, $anp ! rewind
  244. sub $tp, $num, $tp
  245. sub $anp, $num, $anp
  246. ba .Lsub
  247. subcc $num, 8, $cnt ! cnt=num-1 and clear CCR.xcc
  248. .align 16
  249. .Lsub:
  250. ldx [$tp], $tj
  251. add $tp, 8, $tp
  252. ldx [$anp+8], $nj
  253. add $anp, 16, $anp
  254. subccc $tj, $nj, $t2 ! tp[j]-np[j]
  255. srlx $tj, 32, $tj
  256. srlx $nj, 32, $nj
  257. subccc $tj, $nj, $t3
  258. add $rp, 8, $rp
  259. st $t2, [$rp-4] ! reverse order
  260. st $t3, [$rp-8]
  261. brnz,pt $cnt, .Lsub
  262. sub $cnt, 8, $cnt
  263. sub $anp, $num, $anp ! rewind
  264. sub $tp, $num, $tp
  265. sub $anp, $num, $anp
  266. sub $rp, $num, $rp
  267. subccc $ovf, %g0, $ovf ! handle upmost overflow bit
  268. ba .Lcopy
  269. sub $num, 8, $cnt
  270. .align 16
  271. .Lcopy: ! conditional copy
  272. ld [$tp+0], $t0
  273. ld [$tp+4], $t1
  274. ld [$rp+0], $t2
  275. ld [$rp+4], $t3
  276. stx %g0, [$tp] ! zap
  277. add $tp, 8, $tp
  278. stx %g0, [$anp] ! zap
  279. stx %g0, [$anp+8]
  280. add $anp, 16, $anp
  281. movcs %icc, $t0, $t2
  282. movcs %icc, $t1, $t3
  283. st $t3, [$rp+0] ! flip order
  284. st $t2, [$rp+4]
  285. add $rp, 8, $rp
  286. brnz $cnt, .Lcopy
  287. sub $cnt, 8, $cnt
  288. mov 1, %o0
  289. ret
  290. restore
  291. .type bn_mul_mont_vis3, #function
  292. .size bn_mul_mont_vis3, .-bn_mul_mont_vis3
  293. .asciz "Montgomery Multiplication for SPARCv9 VIS3, CRYPTOGAMS by <appro\@openssl.org>"
  294. .align 4
  295. ___
  296. # Purpose of these subroutines is to explicitly encode VIS instructions,
  297. # so that one can compile the module without having to specify VIS
  298. # extensions on compiler command line, e.g. -xarch=v9 vs. -xarch=v9a.
  299. # Idea is to reserve for option to produce "universal" binary and let
  300. # programmer detect if current CPU is VIS capable at run-time.
  301. sub unvis3 {
  302. my ($mnemonic,$rs1,$rs2,$rd)=@_;
  303. my %bias = ( "g" => 0, "o" => 8, "l" => 16, "i" => 24 );
  304. my ($ref,$opf);
  305. my %visopf = ( "addxc" => 0x011,
  306. "addxccc" => 0x013,
  307. "umulxhi" => 0x016 );
  308. $ref = "$mnemonic\t$rs1,$rs2,$rd";
  309. if ($opf=$visopf{$mnemonic}) {
  310. foreach ($rs1,$rs2,$rd) {
  311. return $ref if (!/%([goli])([0-9])/);
  312. $_=$bias{$1}+$2;
  313. }
  314. return sprintf ".word\t0x%08x !%s",
  315. 0x81b00000|$rd<<25|$rs1<<14|$opf<<5|$rs2,
  316. $ref;
  317. } else {
  318. return $ref;
  319. }
  320. }
  321. foreach (split("\n",$code)) {
  322. s/\`([^\`]*)\`/eval $1/ge;
  323. s/\b(umulxhi|addxc[c]{0,2})\s+(%[goli][0-7]),\s*(%[goli][0-7]),\s*(%[goli][0-7])/
  324. &unvis3($1,$2,$3,$4)
  325. /ge;
  326. print $_,"\n";
  327. }
  328. close STDOUT or die "error closing STDOUT: $!";