2 # Copyright 2012-2016 The OpenSSL Project Authors. All Rights Reserved.
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
10 # ====================================================================
11 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
12 # project. The module is, however, dual licensed under OpenSSL and
13 # CRYPTOGAMS licenses depending on where you obtain it. For further
14 # details see http://www.openssl.org/~appro/cryptogams/.
15 # ====================================================================
19 # SPARCv9 VIS3 Montgomery multiplicaion procedure suitable for T3 and
20 # onward. There are three new instructions used here: umulxhi,
21 # addxc[cc] and initializing store. On T3 RSA private key operations
22 # are 1.54/1.87/2.11/2.26 times faster for 512/1024/2048/4096-bit key
23 # lengths. This is without dedicated squaring procedure. On T4
24 # corresponding coefficients are 1.47/2.10/2.80/2.90x, which is mostly
25 # for reference purposes, because T4 has dedicated Montgomery
26 # multiplication and squaring *instructions* that deliver even more.
29 open STDOUT,">$output";
31 $frame = "STACK_FRAME";
35 #include "sparc_arch.h"
38 .register %g2,#scratch
39 .register %g3,#scratch
42 .section ".text",#alloc,#execinstr
45 ($n0,$m0,$m1,$lo0,$hi0, $lo1,$hi1,$aj,$alo,$nj,$nlo,$tj)=
46 (map("%g$_",(1..5)),map("%o$_",(0..5,7)));
49 $rp="%o0"; # BN_ULONG *rp,
50 $ap="%o1"; # const BN_ULONG *ap,
51 $bp="%o2"; # const BN_ULONG *bp,
52 $np="%o3"; # const BN_ULONG *np,
53 $n0p="%o4"; # const BN_ULONG *n0,
54 $num="%o5"; # int num); # caller ensures that num is even
57 .globl bn_mul_mont_vis3
60 add %sp, $bias, %g4 ! real top of stack
61 sll $num, 2, $num ! size in bytes
63 andn %g5, 63, %g5 ! buffer size rounded up to 64 bytes
65 add %g5, %g1, %g1 ! 3*buffer size
67 andn %g1, 63, %g1 ! align at 64 byte
68 sub %g1, $frame, %g1 ! new top of stack
74 # +-------------------------------+<----- %sp
76 # +-------------------------------+<----- aligned at 64 bytes
78 # +-------------------------------+
81 # +-------------------------------+<----- aligned at 64 bytes
82 # | __int64 ap[1..0] | converted ap[]
83 # +-------------------------------+
84 # | __int64 np[1..0] | converted np[]
85 # +-------------------------------+
86 # | __int64 ap[3..2] |
89 # +-------------------------------+
90 ($rp,$ap,$bp,$np,$n0p,$num)=map("%i$_",(0..5));
91 ($t0,$t1,$t2,$t3,$cnt,$tp,$bufsz,$anp)=map("%l$_",(0..7));
94 ld [$n0p+0], $t0 ! pull n0[0..1] value
95 add %sp, $bias+$frame, $tp
98 ld [$bp+0], $t2 ! m0=bp[0]
104 ld [$ap+0], $t0 ! ap[0]
109 ld [$ap+8], $t2 ! ap[1]
114 stx $aj, [$anp] ! converted ap[0]
116 mulx $aj, $m0, $lo0 ! ap[0]*bp[0]
117 umulxhi $aj, $m0, $hi0
119 ld [$np+0], $t0 ! np[0]
124 ld [$np+8], $t2 ! np[1]
129 stx $nj, [$anp+8] ! converted np[0]
131 mulx $lo0, $n0, $m1 ! "tp[0]"*n0
132 stx $aj, [$anp+16] ! converted ap[1]
134 mulx $aj, $m0, $alo ! ap[1]*bp[0]
135 umulxhi $aj, $m0, $aj ! ahi=aj
137 mulx $nj, $m1, $lo1 ! np[0]*m1
138 umulxhi $nj, $m1, $hi1
142 stx $nj, [$anp+24] ! converted np[1]
145 addcc $lo0, $lo1, $lo1
146 addxc %g0, $hi1, $hi1
148 mulx $nj, $m1, $nlo ! np[1]*m1
149 umulxhi $nj, $m1, $nj ! nhi=nj
152 sub $num, 24, $cnt ! cnt=num-3
156 ld [$ap+0], $t0 ! ap[j]
157 addcc $alo, $hi0, $lo0
164 stx $aj, [$anp] ! converted ap[j]
166 ld [$np+0], $t2 ! np[j]
167 addcc $nlo, $hi1, $lo1
169 addxc $nj, %g0, $hi1 ! nhi=nj
173 mulx $aj, $m0, $alo ! ap[j]*bp[0]
175 umulxhi $aj, $m0, $aj ! ahi=aj
176 stx $nj, [$anp+8] ! converted np[j]
177 add $anp, 16, $anp ! anp++
179 mulx $nj, $m1, $nlo ! np[j]*m1
180 addcc $lo0, $lo1, $lo1 ! np[j]*m1+ap[j]*bp[0]
181 umulxhi $nj, $m1, $nj ! nhi=nj
182 addxc %g0, $hi1, $hi1
183 stx $lo1, [$tp] ! tp[j-1]
184 add $tp, 8, $tp ! tp++
187 sub $cnt, 8, $cnt ! j--
189 addcc $alo, $hi0, $lo0
190 addxc $aj, %g0, $hi0 ! ahi=aj
192 addcc $nlo, $hi1, $lo1
194 addcc $lo0, $lo1, $lo1 ! np[j]*m1+ap[j]*bp[0]
195 addxc %g0, $hi1, $hi1
196 stx $lo1, [$tp] ! tp[j-1]
199 addcc $hi0, $hi1, $hi1
200 addxc %g0, %g0, $ovf ! upmost overflow bit
205 sub $num, 16, $i ! i=num-2
209 ld [$bp+0], $t2 ! m0=bp[i]
212 sub $anp, $num, $anp ! rewind
218 ldx [$anp+0], $aj ! ap[0]
220 ldx [$anp+8], $nj ! np[0]
222 mulx $aj, $m0, $lo0 ! ap[0]*bp[i]
223 ldx [$tp], $tj ! tp[0]
224 umulxhi $aj, $m0, $hi0
225 ldx [$anp+16], $aj ! ap[1]
226 addcc $lo0, $tj, $lo0 ! ap[0]*bp[i]+tp[0]
227 mulx $aj, $m0, $alo ! ap[1]*bp[i]
228 addxc %g0, $hi0, $hi0
229 mulx $lo0, $n0, $m1 ! tp[0]*n0
230 umulxhi $aj, $m0, $aj ! ahi=aj
231 mulx $nj, $m1, $lo1 ! np[0]*m1
232 umulxhi $nj, $m1, $hi1
233 ldx [$anp+24], $nj ! np[1]
235 addcc $lo1, $lo0, $lo1
236 mulx $nj, $m1, $nlo ! np[1]*m1
237 addxc %g0, $hi1, $hi1
238 umulxhi $nj, $m1, $nj ! nhi=nj
241 sub $num, 24, $cnt ! cnt=num-3
244 addcc $alo, $hi0, $lo0
245 ldx [$tp+8], $tj ! tp[j]
246 addxc $aj, %g0, $hi0 ! ahi=aj
247 ldx [$anp+0], $aj ! ap[j]
248 addcc $nlo, $hi1, $lo1
249 mulx $aj, $m0, $alo ! ap[j]*bp[i]
250 addxc $nj, %g0, $hi1 ! nhi=nj
251 ldx [$anp+8], $nj ! np[j]
253 umulxhi $aj, $m0, $aj ! ahi=aj
254 addcc $lo0, $tj, $lo0 ! ap[j]*bp[i]+tp[j]
255 mulx $nj, $m1, $nlo ! np[j]*m1
256 addxc %g0, $hi0, $hi0
257 umulxhi $nj, $m1, $nj ! nhi=nj
258 addcc $lo1, $lo0, $lo1 ! np[j]*m1+ap[j]*bp[i]+tp[j]
259 addxc %g0, $hi1, $hi1
260 stx $lo1, [$tp] ! tp[j-1]
262 brnz,pt $cnt, .Linner
265 ldx [$tp+8], $tj ! tp[j]
266 addcc $alo, $hi0, $lo0
267 addxc $aj, %g0, $hi0 ! ahi=aj
268 addcc $lo0, $tj, $lo0 ! ap[j]*bp[i]+tp[j]
269 addxc %g0, $hi0, $hi0
271 addcc $nlo, $hi1, $lo1
272 addxc $nj, %g0, $hi1 ! nhi=nj
273 addcc $lo1, $lo0, $lo1 ! np[j]*m1+ap[j]*bp[i]+tp[j]
274 addxc %g0, $hi1, $hi1
275 stx $lo1, [$tp] ! tp[j-1]
277 subcc %g0, $ovf, %g0 ! move upmost overflow to CCR.xcc
278 addxccc $hi1, $hi0, $hi1
286 sub $anp, $num, $anp ! rewind
290 subcc $num, 8, $cnt ! cnt=num-1 and clear CCR.xcc
298 subccc $tj, $nj, $t2 ! tp[j]-np[j]
303 st $t2, [$rp-4] ! reverse order
308 sub $anp, $num, $anp ! rewind
313 subc $ovf, %g0, $ovf ! handle upmost overflow bit
316 or $np, $ap, $ap ! ap=borrow?tp:rp
321 .Lcopy: ! copy or in-place refresh
327 stx %g0, [$anp] ! zap
330 st $t3, [$rp+0] ! flip order
339 .type bn_mul_mont_vis3, #function
340 .size bn_mul_mont_vis3, .-bn_mul_mont_vis3
341 .asciz "Montgomery Multiplication for SPARCv9 VIS3, CRYPTOGAMS by <appro\@openssl.org>"
345 # Purpose of these subroutines is to explicitly encode VIS instructions,
346 # so that one can compile the module without having to specify VIS
347 # extensions on compiler command line, e.g. -xarch=v9 vs. -xarch=v9a.
348 # Idea is to reserve for option to produce "universal" binary and let
349 # programmer detect if current CPU is VIS capable at run-time.
351 my ($mnemonic,$rs1,$rs2,$rd)=@_;
352 my %bias = ( "g" => 0, "o" => 8, "l" => 16, "i" => 24 );
354 my %visopf = ( "addxc" => 0x011,
356 "umulxhi" => 0x016 );
358 $ref = "$mnemonic\t$rs1,$rs2,$rd";
360 if ($opf=$visopf{$mnemonic}) {
361 foreach ($rs1,$rs2,$rd) {
362 return $ref if (!/%([goli])([0-9])/);
366 return sprintf ".word\t0x%08x !%s",
367 0x81b00000|$rd<<25|$rs1<<14|$opf<<5|$rs2,
374 foreach (split("\n",$code)) {
375 s/\`([^\`]*)\`/eval $1/ge;
377 s/\b(umulxhi|addxc[c]{0,2})\s+(%[goli][0-7]),\s*(%[goli][0-7]),\s*(%[goli][0-7])/