vis3-mont.pl 9.1 KB

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