3 # ====================================================================
4 # Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
5 # project. The module is, however, dual licensed under OpenSSL and
6 # CRYPTOGAMS licenses depending on where you obtain it. For further
7 # details see http://www.openssl.org/~appro/cryptogams/.
8 # ====================================================================
12 # Montgomery multiplication routine for x86_64. While it gives modest
13 # 9% improvement of rsa4096 sign on Opteron, rsa512 sign runs more
14 # than twice, >2x, as fast. Most common rsa1024 sign is improved by
15 # respectful 50%. It remains to be seen if loop unrolling and
16 # dedicated squaring routine can provide further improvement...
20 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
22 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
24 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
25 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
26 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
27 die "can't locate x86_64-xlate.pl";
29 open STDOUT,"| $^X $xlate $flavour $output";
32 $rp="%rdi"; # BN_ULONG *rp,
33 $ap="%rsi"; # const BN_ULONG *ap,
34 $bp="%rdx"; # const BN_ULONG *bp,
35 $np="%rcx"; # const BN_ULONG *np,
36 $n0="%r8"; # const BN_ULONG *n0,
37 $num="%r9"; # int num);
40 $bp="%r12"; # reassign $bp
48 .section .note.GNU-stack,"",\@progbits
52 .type bn_mul_mont,\@function,6
66 lea (%rsp,%r10,8),%rsp # tp=alloca(8*(num+2))
67 and \$-1024,%rsp # minimize TLB usage
69 mov %r11,8(%rsp,$num,8) # tp[num+1]=%rsp
71 mov %rdx,$bp # $bp reassigned, remember?
73 mov ($n0),$n0 # pull n0[0] value
78 mov ($bp),$m0 # m0=bp[0]
80 mulq $m0 # ap[0]*bp[0]
84 imulq $n0,%rax # "tp[0]"*n0
88 add $lo0,%rax # discarded
95 mulq $m0 # ap[j]*bp[0]
106 add $lo0,%rax # np[j]*m1+ap[j]*bp[0]
108 mov %rax,-16(%rsp,$j,8) # tp[j-1]
116 mov $hi1,-8(%rsp,$num,8)
117 mov %rdx,(%rsp,$num,8) # store upmost overflow bit
124 mov ($bp,$i,8),$m0 # m0=bp[i]
125 mov ($ap),%rax # ap[0]
126 mulq $m0 # ap[0]*bp[i]
127 add (%rsp),%rax # ap[0]*bp[i]+tp[0]
132 imulq $n0,%rax # tp[0]*n0
135 mulq ($np,$j,8) # np[0]*m1
136 add $lo0,%rax # discarded
137 mov 8(%rsp),$lo0 # tp[1]
145 mulq $m0 # ap[j]*bp[i]
148 add %rax,$lo0 # ap[j]*bp[i]+tp[j]
157 add $lo0,%rax # np[j]*m1+ap[j]*bp[i]+tp[j]
161 mov %rax,-16(%rsp,$j,8) # tp[j-1]
168 add $lo0,$hi1 # pull upmost overflow bit
170 mov $hi1,-8(%rsp,$num,8)
171 mov %rdx,(%rsp,$num,8) # store upmost overflow bit
177 lea (%rsp),$ap # borrow ap for tp
178 lea -1($num),$j # j=num-1
180 mov ($ap),%rax # tp[0]
181 xor $i,$i # i=0 and clear CF!
184 .Lsub: sbb ($np,$i,8),%rax
185 mov %rax,($rp,$i,8) # rp[i]=tp[i]-np[i]
186 dec $j # doesn't affect CF!
187 mov 8($ap,$i,8),%rax # tp[i+1]
191 sbb \$0,%rax # handle upmost overflow bit
197 or $np,$ap # ap=borrow?tp:rp
199 .Lcopy: # copy or in-place refresh
201 mov %rax,($rp,$j,8) # rp[i]=tp[i]
202 mov $i,(%rsp,$j,8) # zap temporary vector
206 mov 8(%rsp,$num,8),%rsi # restore %rsp
217 .size bn_mul_mont,.-bn_mul_mont
218 .asciz "Montgomery Multiplication for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
222 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
223 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
231 .extern __imp_RtlVirtualUnwind
232 .type se_handler,\@abi-omnipotent
246 mov 120($context),%rax # pull context->Rax
247 mov 248($context),%rbx # pull context->Rip
249 lea .Lprologue(%rip),%r10
250 cmp %r10,%rbx # context->Rip<.Lprologue
253 mov 152($context),%rax # pull context->Rsp
255 lea .Lepilogue(%rip),%r10
256 cmp %r10,%rbx # context->Rip>=.Lepilogue
259 mov 192($context),%r10 # pull $num
260 mov 8(%rax,%r10,8),%rax # pull saved stack pointer
269 mov %rbx,144($context) # restore context->Rbx
270 mov %rbp,160($context) # restore context->Rbp
271 mov %r12,216($context) # restore context->R12
272 mov %r13,224($context) # restore context->R13
273 mov %r14,232($context) # restore context->R14
274 mov %r15,240($context) # restore context->R15
279 mov %rax,152($context) # restore context->Rsp
280 mov %rsi,168($context) # restore context->Rsi
281 mov %rdi,176($context) # restore context->Rdi
283 mov 40($disp),%rdi # disp->ContextRecord
284 mov $context,%rsi # context
285 mov \$154,%ecx # sizeof(CONTEXT)
286 .long 0xa548f3fc # cld; rep movsq
289 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
290 mov 8(%rsi),%rdx # arg2, disp->ImageBase
291 mov 0(%rsi),%r8 # arg3, disp->ControlPc
292 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
293 mov 40(%rsi),%r10 # disp->ContextRecord
294 lea 56(%rsi),%r11 # &disp->HandlerData
295 lea 24(%rsi),%r12 # &disp->EstablisherFrame
296 mov %r10,32(%rsp) # arg5
297 mov %r11,40(%rsp) # arg6
298 mov %r12,48(%rsp) # arg7
299 mov %rcx,56(%rsp) # arg8, (NULL)
300 call *__imp_RtlVirtualUnwind(%rip)
302 mov \$1,%eax # ExceptionContinueSearch
314 .size se_handler,.-se_handler
318 .rva .LSEH_begin_bn_mul_mont
319 .rva .LSEH_end_bn_mul_mont
320 .rva .LSEH_info_bn_mul_mont
324 .LSEH_info_bn_mul_mont: